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