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