summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/syscall/umount.c
blob: a2deedb163975f62641d687b09fb8bda7949c68d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved  	*/

/*
 * Portions of this source code were derived from Berkeley 4.3 BSD
 * under license from the Regents of the University of California.
 */

#include <sys/types.h>
#include <sys/t_lock.h>
#include <sys/param.h>
#include <sys/errno.h>
#include <sys/fstyp.h>
#include <sys/kmem.h>
#include <sys/systm.h>
#include <sys/mount.h>
#include <sys/vfs.h>
#include <sys/cred.h>
#include <sys/vnode.h>
#include <sys/cmn_err.h>
#include <sys/debug.h>
#include <sys/pathname.h>
#include <sys/policy.h>
#include <sys/zone.h>

#define	UMOUNT2_SET_ERRNO(e, is_syscall) ((is_syscall) ? set_errno((e)) : (e))

/*
 * The heart of the umount2 call - it is pulled out to allow kernel
 * level particpation when the only reference is the vfs pointer.
 *
 * Note that some of the callers may not be in the context of a
 * syscall (created by zthread_create() for example) and as such
 * may not have an associated curthread->t_lwp. This is handled
 * by is_syscall.
 */
int
umount2_engine(vfs_t *vfsp, int flag, cred_t *cr, int is_syscall)
{
	int	error;

	/*
	 * Protect the call to vn_vfswlock() with the vfs reflock.  This
	 * ensures vfs_vnodecovered will either be NULL (because someone
	 * beat us to the umount) or valid (because vfs_lock() prevents
	 * another umount from getting through here until we've called
	 * vn_vfswlock() on the covered vnode).
	 *
	 * At one point, we did the non-blocking version (vfs_lock()),
	 * and if it failed, bailed out with EBUSY.  However, dounmount()
	 * calls vfs_lock_wait() and we drop the vfs lock before calling
	 * dounmount(), so there's no difference between waiting here
	 * for the lock or waiting there because grabbed it as soon as
	 * we drop it below.  No returning with EBUSY at this point
	 * reduces the number of spurious unmount failures that happen
	 * as a side-effect of fsflush() and other mount and unmount
	 * operations that might be going on simultaneously.
	 */
	vfs_lock_wait(vfsp);

	/*
	 * Call vn_vfswlock() on the covered vnode so that dounmount()
	 * can do its thing.  It will call the corresponding vn_vfsunlock().
	 * Note that vfsp->vfs_vnodecovered can be NULL here, either because
	 * someone did umount on "/" or because someone beat us to the umount
	 * before we did the vfs_lock() above.  In these cases, vn_vfswlock()
	 * returns EBUSY and we just pass that up.  Also note that we're
	 * looking at a vnode without doing a VN_HOLD() on it.  This is
	 * safe because it can't go away while something is mounted on it
	 * and we're locking out other umounts at this point.
	 */
	if (vn_vfswlock(vfsp->vfs_vnodecovered)) {
		vfs_unlock(vfsp);
		VFS_RELE(vfsp);
		return (UMOUNT2_SET_ERRNO(EBUSY, is_syscall));
	}

	/*
	 * Now that the VVFSLOCK in the covered vnode is protecting this
	 * path, we don't need the vfs reflock or the hold on the vfs anymore.
	 */
	vfs_unlock(vfsp);
	VFS_RELE(vfsp);

	/*
	 * Perform the unmount.
	 */
	if ((error = dounmount(vfsp, flag, cr)) != 0)
		return (UMOUNT2_SET_ERRNO(error, is_syscall));
	return (0);
}

/*
 * New umount() system call (for force unmount flag and perhaps others later).
 */
int
umount2(char *pathp, int flag)
{
	struct pathname pn;
	struct vfs *vfsp;
	int error;

	/*
	 * Some flags are disallowed through the system call interface.
	 */
	flag &= MS_UMOUNT_MASK;

	/*
	 * Lookup user-supplied name by trying to match it against the
	 * mount points recorded at mount time.  If no match is found
	 * (which can happen if the path to the mount point is specified
	 * differently between mount & umount, or if a block device were
	 * passed to umount) then we fall back to calling lookupname()
	 * to find the vfs.  Doing it this way prevents calling lookupname()
	 * in most cases and that allows forcible umount to work even if
	 * lookupname() would hang (i.e. because an NFS server is dead).
	 */

	if (error = pn_get(pathp, UIO_USERSPACE, &pn))
		return (set_errno(error));

	/*
	 * Only a privileged user is allowed to bypass the security
	 * checks done by lookupname() and use the results from
	 * vfs_mntpoint2vfsp() instead.  It could be argued that the
	 * proper check is FILE_DAC_SEARCH but we put it all
	 * under the mount privilege.  Also, make sure the caller
	 * isn't in an environment with an alternate root (to the zone's root)
	 * directory, i.e. chroot(2).
	 */
	if (secpolicy_fs_unmount(CRED(), NULL) != 0 ||
	    (PTOU(curproc)->u_rdir != NULL &&
	    PTOU(curproc)->u_rdir != curproc->p_zone->zone_rootvp) ||
	    (vfsp = vfs_mntpoint2vfsp(pn.pn_path)) == NULL) {
		vnode_t *fsrootvp;

		/* fall back to lookupname() on path given to us */
		if (error = lookupname(pn.pn_path, UIO_SYSSPACE, FOLLOW,
		    NULLVPP, &fsrootvp)) {
			pn_free(&pn);
			return (set_errno(error));
		}
		/*
		 * Find the vfs to be unmounted.  The caller may have specified
		 * either the directory mount point (preferred) or else (for a
		 * disk-based file system) the block device which was mounted.
		 * Check to see which it is; if it's the device, search the VFS
		 * list to find the associated vfs entry.
		 */
		if (fsrootvp->v_flag & VROOT) {
			vfsp = fsrootvp->v_vfsp;
			VFS_HOLD(vfsp);
		} else if (fsrootvp->v_type == VBLK)
			vfsp = vfs_dev2vfsp(fsrootvp->v_rdev);
		else
			vfsp = NULL;

		VN_RELE(fsrootvp);

		if (vfsp == NULL) {
			pn_free(&pn);
			return (set_errno(EINVAL));
		}
	}
	pn_free(&pn);

	return (umount2_engine(vfsp, flag, CRED(), 1));
}