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