xref: /illumos-gate/usr/src/uts/common/syscall/umount.c (revision b9238976491622ad75a67ab0c12edf99e36212b9)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*b9238976Sth  * Common Development and Distribution License (the "License").
6*b9238976Sth  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*b9238976Sth  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
317c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
387c478bd9Sstevel@tonic-gate #include <sys/param.h>
397c478bd9Sstevel@tonic-gate #include <sys/errno.h>
407c478bd9Sstevel@tonic-gate #include <sys/fstyp.h>
417c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
427c478bd9Sstevel@tonic-gate #include <sys/systm.h>
437c478bd9Sstevel@tonic-gate #include <sys/mount.h>
447c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
457c478bd9Sstevel@tonic-gate #include <sys/cred.h>
467c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
477c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
487c478bd9Sstevel@tonic-gate #include <sys/debug.h>
497c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
507c478bd9Sstevel@tonic-gate #include <sys/policy.h>
517c478bd9Sstevel@tonic-gate #include <sys/zone.h>
527c478bd9Sstevel@tonic-gate 
53*b9238976Sth #define	UMOUNT2_SET_ERRNO(e, is_syscall) ((is_syscall) ? set_errno((e)) : (e))
54*b9238976Sth 
55*b9238976Sth /*
56*b9238976Sth  * The heart of the umount2 call - it is pulled out to allow kernel
57*b9238976Sth  * level particpation when the only reference is the vfs pointer.
58*b9238976Sth  *
59*b9238976Sth  * Note that some of the callers may not be in the context of a
60*b9238976Sth  * syscall (created by zthread_create() for example) and as such
61*b9238976Sth  * may not have an associated curthread->t_lwp. This is handled
62*b9238976Sth  * by is_syscall.
63*b9238976Sth  */
64*b9238976Sth int
65*b9238976Sth umount2_engine(vfs_t *vfsp, int flag, cred_t *cr, int is_syscall)
66*b9238976Sth {
67*b9238976Sth 	int	error;
68*b9238976Sth 
69*b9238976Sth 	/*
70*b9238976Sth 	 * Protect the call to vn_vfswlock() with the vfs reflock.  This
71*b9238976Sth 	 * ensures vfs_vnodecovered will either be NULL (because someone
72*b9238976Sth 	 * beat us to the umount) or valid (because vfs_lock() prevents
73*b9238976Sth 	 * another umount from getting through here until we've called
74*b9238976Sth 	 * vn_vfswlock() on the covered vnode).
75*b9238976Sth 	 *
76*b9238976Sth 	 * At one point, we did the non-blocking version (vfs_lock()),
77*b9238976Sth 	 * and if it failed, bailed out with EBUSY.  However, dounmount()
78*b9238976Sth 	 * calls vfs_lock_wait() and we drop the vfs lock before calling
79*b9238976Sth 	 * dounmount(), so there's no difference between waiting here
80*b9238976Sth 	 * for the lock or waiting there because grabbed it as soon as
81*b9238976Sth 	 * we drop it below.  No returning with EBUSY at this point
82*b9238976Sth 	 * reduces the number of spurious unmount failures that happen
83*b9238976Sth 	 * as a side-effect of fsflush() and other mount and unmount
84*b9238976Sth 	 * operations that might be going on simultaneously.
85*b9238976Sth 	 */
86*b9238976Sth 	vfs_lock_wait(vfsp);
87*b9238976Sth 
88*b9238976Sth 	/*
89*b9238976Sth 	 * Call vn_vfswlock() on the covered vnode so that dounmount()
90*b9238976Sth 	 * can do its thing.  It will call the corresponding vn_vfsunlock().
91*b9238976Sth 	 * Note that vfsp->vfs_vnodecovered can be NULL here, either because
92*b9238976Sth 	 * someone did umount on "/" or because someone beat us to the umount
93*b9238976Sth 	 * before we did the vfs_lock() above.  In these cases, vn_vfswlock()
94*b9238976Sth 	 * returns EBUSY and we just pass that up.  Also note that we're
95*b9238976Sth 	 * looking at a vnode without doing a VN_HOLD() on it.  This is
96*b9238976Sth 	 * safe because it can't go away while something is mounted on it
97*b9238976Sth 	 * and we're locking out other umounts at this point.
98*b9238976Sth 	 */
99*b9238976Sth 	if (vn_vfswlock(vfsp->vfs_vnodecovered)) {
100*b9238976Sth 		vfs_unlock(vfsp);
101*b9238976Sth 		VFS_RELE(vfsp);
102*b9238976Sth 		return (UMOUNT2_SET_ERRNO(EBUSY, is_syscall));
103*b9238976Sth 	}
104*b9238976Sth 
105*b9238976Sth 	/*
106*b9238976Sth 	 * Now that the VVFSLOCK in the covered vnode is protecting this
107*b9238976Sth 	 * path, we don't need the vfs reflock or the hold on the vfs anymore.
108*b9238976Sth 	 */
109*b9238976Sth 	vfs_unlock(vfsp);
110*b9238976Sth 	VFS_RELE(vfsp);
111*b9238976Sth 
112*b9238976Sth 	/*
113*b9238976Sth 	 * Perform the unmount.
114*b9238976Sth 	 */
115*b9238976Sth 	if ((error = dounmount(vfsp, flag, cr)) != 0)
116*b9238976Sth 		return (UMOUNT2_SET_ERRNO(error, is_syscall));
117*b9238976Sth 	return (0);
118*b9238976Sth }
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate /*
1217c478bd9Sstevel@tonic-gate  * New umount() system call (for force unmount flag and perhaps others later).
1227c478bd9Sstevel@tonic-gate  */
1237c478bd9Sstevel@tonic-gate int
1247c478bd9Sstevel@tonic-gate umount2(char *pathp, int flag)
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate 	struct pathname pn;
1277c478bd9Sstevel@tonic-gate 	struct vfs *vfsp;
1287c478bd9Sstevel@tonic-gate 	int error;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	/*
1317c478bd9Sstevel@tonic-gate 	 * Some flags are disallowed through the system call interface.
1327c478bd9Sstevel@tonic-gate 	 */
1337c478bd9Sstevel@tonic-gate 	flag &= MS_UMOUNT_MASK;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	/*
1367c478bd9Sstevel@tonic-gate 	 * Lookup user-supplied name by trying to match it against the
1377c478bd9Sstevel@tonic-gate 	 * mount points recorded at mount time.  If no match is found
1387c478bd9Sstevel@tonic-gate 	 * (which can happen if the path to the mount point is specified
1397c478bd9Sstevel@tonic-gate 	 * differently between mount & umount, or if a block device were
1407c478bd9Sstevel@tonic-gate 	 * passed to umount) then we fall back to calling lookupname()
1417c478bd9Sstevel@tonic-gate 	 * to find the vfs.  Doing it this way prevents calling lookupname()
1427c478bd9Sstevel@tonic-gate 	 * in most cases and that allows forcible umount to work even if
1437c478bd9Sstevel@tonic-gate 	 * lookupname() would hang (i.e. because an NFS server is dead).
1447c478bd9Sstevel@tonic-gate 	 */
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	if (error = pn_get(pathp, UIO_USERSPACE, &pn))
1477c478bd9Sstevel@tonic-gate 		return (set_errno(error));
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	/*
1507c478bd9Sstevel@tonic-gate 	 * Only a privileged user is allowed to bypass the security
1517c478bd9Sstevel@tonic-gate 	 * checks done by lookupname() and use the results from
1527c478bd9Sstevel@tonic-gate 	 * vfs_mntpoint2vfsp() instead.  It could be argued that the
1537c478bd9Sstevel@tonic-gate 	 * proper check is FILE_DAC_SEARCH but we put it all
1547c478bd9Sstevel@tonic-gate 	 * under the mount privilege.  Also, make sure the caller
1557c478bd9Sstevel@tonic-gate 	 * isn't in an environment with an alternate root (to the zone's root)
1567c478bd9Sstevel@tonic-gate 	 * directory, i.e. chroot(2).
1577c478bd9Sstevel@tonic-gate 	 */
1587c478bd9Sstevel@tonic-gate 	if (secpolicy_fs_unmount(CRED(), NULL) != 0 ||
1597c478bd9Sstevel@tonic-gate 	    (PTOU(curproc)->u_rdir != NULL &&
1607c478bd9Sstevel@tonic-gate 	    PTOU(curproc)->u_rdir != curproc->p_zone->zone_rootvp) ||
1617c478bd9Sstevel@tonic-gate 	    (vfsp = vfs_mntpoint2vfsp(pn.pn_path)) == NULL) {
1627c478bd9Sstevel@tonic-gate 		vnode_t *fsrootvp;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 		/* fall back to lookupname() on path given to us */
1657c478bd9Sstevel@tonic-gate 		if (error = lookupname(pn.pn_path, UIO_SYSSPACE, FOLLOW,
1667c478bd9Sstevel@tonic-gate 		    NULLVPP, &fsrootvp)) {
1677c478bd9Sstevel@tonic-gate 			pn_free(&pn);
1687c478bd9Sstevel@tonic-gate 			return (set_errno(error));
1697c478bd9Sstevel@tonic-gate 		}
1707c478bd9Sstevel@tonic-gate 		/*
1717c478bd9Sstevel@tonic-gate 		 * Find the vfs to be unmounted.  The caller may have specified
1727c478bd9Sstevel@tonic-gate 		 * either the directory mount point (preferred) or else (for a
1737c478bd9Sstevel@tonic-gate 		 * disk-based file system) the block device which was mounted.
1747c478bd9Sstevel@tonic-gate 		 * Check to see which it is; if it's the device, search the VFS
1757c478bd9Sstevel@tonic-gate 		 * list to find the associated vfs entry.
1767c478bd9Sstevel@tonic-gate 		 */
1777c478bd9Sstevel@tonic-gate 		if (fsrootvp->v_flag & VROOT) {
1787c478bd9Sstevel@tonic-gate 			vfsp = fsrootvp->v_vfsp;
1797c478bd9Sstevel@tonic-gate 			VFS_HOLD(vfsp);
1807c478bd9Sstevel@tonic-gate 		} else if (fsrootvp->v_type == VBLK)
1817c478bd9Sstevel@tonic-gate 			vfsp = vfs_dev2vfsp(fsrootvp->v_rdev);
1827c478bd9Sstevel@tonic-gate 		else
1837c478bd9Sstevel@tonic-gate 			vfsp = NULL;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 		VN_RELE(fsrootvp);
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 		if (vfsp == NULL) {
1887c478bd9Sstevel@tonic-gate 			pn_free(&pn);
1897c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
1907c478bd9Sstevel@tonic-gate 		}
1917c478bd9Sstevel@tonic-gate 	}
1927c478bd9Sstevel@tonic-gate 	pn_free(&pn);
1937c478bd9Sstevel@tonic-gate 
194*b9238976Sth 	return (umount2_engine(vfsp, flag, CRED(), 1));
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate /*
1987c478bd9Sstevel@tonic-gate  * Old umount() system call for compatibility.
1997c478bd9Sstevel@tonic-gate  * Changes due to support for forced unmount.
2007c478bd9Sstevel@tonic-gate  */
2017c478bd9Sstevel@tonic-gate int
2027c478bd9Sstevel@tonic-gate umount(char *pathp)
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate 	return (umount2(pathp, 0));
2057c478bd9Sstevel@tonic-gate }
206