xref: /illumos-gate/usr/src/uts/common/syscall/umount.c (revision 7c478bd95313f5f23a4c958a745db2134aa0324)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
32*7c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
33*7c478bd9Sstevel@tonic-gate  */
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/fstyp.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/mount.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/cred.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
51*7c478bd9Sstevel@tonic-gate #include <sys/policy.h>
52*7c478bd9Sstevel@tonic-gate #include <sys/zone.h>
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate /*
56*7c478bd9Sstevel@tonic-gate  * New umount() system call (for force unmount flag and perhaps others later).
57*7c478bd9Sstevel@tonic-gate  */
58*7c478bd9Sstevel@tonic-gate int
59*7c478bd9Sstevel@tonic-gate umount2(char *pathp, int flag)
60*7c478bd9Sstevel@tonic-gate {
61*7c478bd9Sstevel@tonic-gate 	struct pathname pn;
62*7c478bd9Sstevel@tonic-gate 	struct vfs *vfsp;
63*7c478bd9Sstevel@tonic-gate 	int error;
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 	/*
66*7c478bd9Sstevel@tonic-gate 	 * Some flags are disallowed through the system call interface.
67*7c478bd9Sstevel@tonic-gate 	 */
68*7c478bd9Sstevel@tonic-gate 	flag &= MS_UMOUNT_MASK;
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate 	/*
71*7c478bd9Sstevel@tonic-gate 	 * Lookup user-supplied name by trying to match it against the
72*7c478bd9Sstevel@tonic-gate 	 * mount points recorded at mount time.  If no match is found
73*7c478bd9Sstevel@tonic-gate 	 * (which can happen if the path to the mount point is specified
74*7c478bd9Sstevel@tonic-gate 	 * differently between mount & umount, or if a block device were
75*7c478bd9Sstevel@tonic-gate 	 * passed to umount) then we fall back to calling lookupname()
76*7c478bd9Sstevel@tonic-gate 	 * to find the vfs.  Doing it this way prevents calling lookupname()
77*7c478bd9Sstevel@tonic-gate 	 * in most cases and that allows forcible umount to work even if
78*7c478bd9Sstevel@tonic-gate 	 * lookupname() would hang (i.e. because an NFS server is dead).
79*7c478bd9Sstevel@tonic-gate 	 */
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 	if (error = pn_get(pathp, UIO_USERSPACE, &pn))
82*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 	/*
85*7c478bd9Sstevel@tonic-gate 	 * Only a privileged user is allowed to bypass the security
86*7c478bd9Sstevel@tonic-gate 	 * checks done by lookupname() and use the results from
87*7c478bd9Sstevel@tonic-gate 	 * vfs_mntpoint2vfsp() instead.  It could be argued that the
88*7c478bd9Sstevel@tonic-gate 	 * proper check is FILE_DAC_SEARCH but we put it all
89*7c478bd9Sstevel@tonic-gate 	 * under the mount privilege.  Also, make sure the caller
90*7c478bd9Sstevel@tonic-gate 	 * isn't in an environment with an alternate root (to the zone's root)
91*7c478bd9Sstevel@tonic-gate 	 * directory, i.e. chroot(2).
92*7c478bd9Sstevel@tonic-gate 	 */
93*7c478bd9Sstevel@tonic-gate 	if (secpolicy_fs_unmount(CRED(), NULL) != 0 ||
94*7c478bd9Sstevel@tonic-gate 	    (PTOU(curproc)->u_rdir != NULL &&
95*7c478bd9Sstevel@tonic-gate 	    PTOU(curproc)->u_rdir != curproc->p_zone->zone_rootvp) ||
96*7c478bd9Sstevel@tonic-gate 	    (vfsp = vfs_mntpoint2vfsp(pn.pn_path)) == NULL) {
97*7c478bd9Sstevel@tonic-gate 		vnode_t *fsrootvp;
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 		/* fall back to lookupname() on path given to us */
100*7c478bd9Sstevel@tonic-gate 		if (error = lookupname(pn.pn_path, UIO_SYSSPACE, FOLLOW,
101*7c478bd9Sstevel@tonic-gate 		    NULLVPP, &fsrootvp)) {
102*7c478bd9Sstevel@tonic-gate 			pn_free(&pn);
103*7c478bd9Sstevel@tonic-gate 			return (set_errno(error));
104*7c478bd9Sstevel@tonic-gate 		}
105*7c478bd9Sstevel@tonic-gate 		/*
106*7c478bd9Sstevel@tonic-gate 		 * Find the vfs to be unmounted.  The caller may have specified
107*7c478bd9Sstevel@tonic-gate 		 * either the directory mount point (preferred) or else (for a
108*7c478bd9Sstevel@tonic-gate 		 * disk-based file system) the block device which was mounted.
109*7c478bd9Sstevel@tonic-gate 		 * Check to see which it is; if it's the device, search the VFS
110*7c478bd9Sstevel@tonic-gate 		 * list to find the associated vfs entry.
111*7c478bd9Sstevel@tonic-gate 		 */
112*7c478bd9Sstevel@tonic-gate 		if (fsrootvp->v_flag & VROOT) {
113*7c478bd9Sstevel@tonic-gate 			vfsp = fsrootvp->v_vfsp;
114*7c478bd9Sstevel@tonic-gate 			VFS_HOLD(vfsp);
115*7c478bd9Sstevel@tonic-gate 		} else if (fsrootvp->v_type == VBLK)
116*7c478bd9Sstevel@tonic-gate 			vfsp = vfs_dev2vfsp(fsrootvp->v_rdev);
117*7c478bd9Sstevel@tonic-gate 		else
118*7c478bd9Sstevel@tonic-gate 			vfsp = NULL;
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 		VN_RELE(fsrootvp);
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate 		if (vfsp == NULL) {
123*7c478bd9Sstevel@tonic-gate 			pn_free(&pn);
124*7c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
125*7c478bd9Sstevel@tonic-gate 		}
126*7c478bd9Sstevel@tonic-gate 	}
127*7c478bd9Sstevel@tonic-gate 	pn_free(&pn);
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate 	/*
130*7c478bd9Sstevel@tonic-gate 	 * Protect the call to vn_vfswlock() with the vfs reflock.  This
131*7c478bd9Sstevel@tonic-gate 	 * ensures vfs_vnodecovered will either be NULL (because someone
132*7c478bd9Sstevel@tonic-gate 	 * beat us to the umount) or valid (because vfs_lock() prevents
133*7c478bd9Sstevel@tonic-gate 	 * another umount from getting through here until we've called
134*7c478bd9Sstevel@tonic-gate 	 * vn_vfswlock() on the covered vnode).
135*7c478bd9Sstevel@tonic-gate 	 *
136*7c478bd9Sstevel@tonic-gate 	 * At one point, we did the non-blocking version (vfs_lock()),
137*7c478bd9Sstevel@tonic-gate 	 * and if it failed, bailed out with EBUSY.  However, dounmount()
138*7c478bd9Sstevel@tonic-gate 	 * calls vfs_lock_wait() and we drop the vfs lock before calling
139*7c478bd9Sstevel@tonic-gate 	 * dounmount(), so there's no difference between waiting here
140*7c478bd9Sstevel@tonic-gate 	 * for the lock or waiting there because grabbed it as soon as
141*7c478bd9Sstevel@tonic-gate 	 * we drop it below.  No returning with EBUSY at this point
142*7c478bd9Sstevel@tonic-gate 	 * reduces the number of spurious unmount failures that happen
143*7c478bd9Sstevel@tonic-gate 	 * as a side-effect of fsflush() and other mount and unmount
144*7c478bd9Sstevel@tonic-gate 	 * operations that might be going on simultaneously.
145*7c478bd9Sstevel@tonic-gate 	 */
146*7c478bd9Sstevel@tonic-gate 	vfs_lock_wait(vfsp);
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 	/*
149*7c478bd9Sstevel@tonic-gate 	 * Call vn_vfswlock() on the covered vnode so that dounmount()
150*7c478bd9Sstevel@tonic-gate 	 * can do its thing.  It will call the corresponding vn_vfsunlock().
151*7c478bd9Sstevel@tonic-gate 	 * Note that vfsp->vfs_vnodecovered can be NULL here, either because
152*7c478bd9Sstevel@tonic-gate 	 * someone did umount on "/" or because someone beat us to the umount
153*7c478bd9Sstevel@tonic-gate 	 * before we did the vfs_lock() above.  In these cases, vn_vfswlock()
154*7c478bd9Sstevel@tonic-gate 	 * returns EBUSY and we just pass that up.  Also note that we're
155*7c478bd9Sstevel@tonic-gate 	 * looking at a vnode without doing a VN_HOLD() on it.  This is
156*7c478bd9Sstevel@tonic-gate 	 * safe because it can't go away while something is mounted on it
157*7c478bd9Sstevel@tonic-gate 	 * and we're locking out other umounts at this point.
158*7c478bd9Sstevel@tonic-gate 	 */
159*7c478bd9Sstevel@tonic-gate 	if (vn_vfswlock(vfsp->vfs_vnodecovered)) {
160*7c478bd9Sstevel@tonic-gate 		vfs_unlock(vfsp);
161*7c478bd9Sstevel@tonic-gate 		VFS_RELE(vfsp);
162*7c478bd9Sstevel@tonic-gate 		return (set_errno(EBUSY));
163*7c478bd9Sstevel@tonic-gate 	}
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 	/*
166*7c478bd9Sstevel@tonic-gate 	 * Now that the VVFSLOCK in the covered vnode is protecting this
167*7c478bd9Sstevel@tonic-gate 	 * path, we don't need the vfs reflock or the hold on the vfs anymore.
168*7c478bd9Sstevel@tonic-gate 	 */
169*7c478bd9Sstevel@tonic-gate 	vfs_unlock(vfsp);
170*7c478bd9Sstevel@tonic-gate 	VFS_RELE(vfsp);
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate 	/*
173*7c478bd9Sstevel@tonic-gate 	 * Perform the unmount.
174*7c478bd9Sstevel@tonic-gate 	 */
175*7c478bd9Sstevel@tonic-gate 	if ((error = dounmount(vfsp, flag, CRED())) != 0)
176*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
177*7c478bd9Sstevel@tonic-gate 	return (0);
178*7c478bd9Sstevel@tonic-gate }
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate /*
181*7c478bd9Sstevel@tonic-gate  * Old umount() system call for compatibility.
182*7c478bd9Sstevel@tonic-gate  * Changes due to support for forced unmount.
183*7c478bd9Sstevel@tonic-gate  */
184*7c478bd9Sstevel@tonic-gate int
185*7c478bd9Sstevel@tonic-gate umount(char *pathp)
186*7c478bd9Sstevel@tonic-gate {
187*7c478bd9Sstevel@tonic-gate 	return (umount2(pathp, 0));
188*7c478bd9Sstevel@tonic-gate }
189