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