xref: /freebsd/sys/fs/nullfs/null_vfsops.c (revision 39ee7a7a6bdd1557b1c3532abf60d139798ac88b)
1 /*-
2  * Copyright (c) 1992, 1993, 1995
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software donated to Berkeley by
6  * Jan-Simon Pendry.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)null_vfsops.c	8.2 (Berkeley) 1/21/94
33  *
34  * @(#)lofs_vfsops.c	1.2 (Berkeley) 6/18/92
35  * $FreeBSD$
36  */
37 
38 /*
39  * Null Layer
40  * (See null_vnops.c for a description of what this does.)
41  */
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/fcntl.h>
46 #include <sys/kernel.h>
47 #include <sys/lock.h>
48 #include <sys/malloc.h>
49 #include <sys/mount.h>
50 #include <sys/namei.h>
51 #include <sys/proc.h>
52 #include <sys/vnode.h>
53 #include <sys/jail.h>
54 
55 #include <fs/nullfs/null.h>
56 
57 static MALLOC_DEFINE(M_NULLFSMNT, "nullfs_mount", "NULLFS mount structure");
58 
59 static vfs_fhtovp_t	nullfs_fhtovp;
60 static vfs_mount_t	nullfs_mount;
61 static vfs_quotactl_t	nullfs_quotactl;
62 static vfs_root_t	nullfs_root;
63 static vfs_sync_t	nullfs_sync;
64 static vfs_statfs_t	nullfs_statfs;
65 static vfs_unmount_t	nullfs_unmount;
66 static vfs_vget_t	nullfs_vget;
67 static vfs_extattrctl_t	nullfs_extattrctl;
68 
69 /*
70  * Mount null layer
71  */
72 static int
73 nullfs_mount(struct mount *mp)
74 {
75 	int error = 0;
76 	struct vnode *lowerrootvp, *vp;
77 	struct vnode *nullm_rootvp;
78 	struct null_mount *xmp;
79 	struct thread *td = curthread;
80 	char *target;
81 	int isvnunlocked = 0, len;
82 	struct nameidata nd, *ndp = &nd;
83 
84 	NULLFSDEBUG("nullfs_mount(mp = %p)\n", (void *)mp);
85 
86 	if (!prison_allow(td->td_ucred, PR_ALLOW_MOUNT_NULLFS))
87 		return (EPERM);
88 	if (mp->mnt_flag & MNT_ROOTFS)
89 		return (EOPNOTSUPP);
90 
91 	/*
92 	 * Update is a no-op
93 	 */
94 	if (mp->mnt_flag & MNT_UPDATE) {
95 		/*
96 		 * Only support update mounts for NFS export.
97 		 */
98 		if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0))
99 			return (0);
100 		else
101 			return (EOPNOTSUPP);
102 	}
103 
104 	/*
105 	 * Get argument
106 	 */
107 	error = vfs_getopt(mp->mnt_optnew, "target", (void **)&target, &len);
108 	if (error || target[len - 1] != '\0')
109 		return (EINVAL);
110 
111 	/*
112 	 * Unlock lower node to avoid possible deadlock.
113 	 */
114 	if ((mp->mnt_vnodecovered->v_op == &null_vnodeops) &&
115 	    VOP_ISLOCKED(mp->mnt_vnodecovered) == LK_EXCLUSIVE) {
116 		VOP_UNLOCK(mp->mnt_vnodecovered, 0);
117 		isvnunlocked = 1;
118 	}
119 	/*
120 	 * Find lower node
121 	 */
122 	NDINIT(ndp, LOOKUP, FOLLOW|LOCKLEAF, UIO_SYSSPACE, target, curthread);
123 	error = namei(ndp);
124 
125 	/*
126 	 * Re-lock vnode.
127 	 * XXXKIB This is deadlock-prone as well.
128 	 */
129 	if (isvnunlocked)
130 		vn_lock(mp->mnt_vnodecovered, LK_EXCLUSIVE | LK_RETRY);
131 
132 	if (error)
133 		return (error);
134 	NDFREE(ndp, NDF_ONLY_PNBUF);
135 
136 	/*
137 	 * Sanity check on lower vnode
138 	 */
139 	lowerrootvp = ndp->ni_vp;
140 
141 	/*
142 	 * Check multi null mount to avoid `lock against myself' panic.
143 	 */
144 	if (lowerrootvp == VTONULL(mp->mnt_vnodecovered)->null_lowervp) {
145 		NULLFSDEBUG("nullfs_mount: multi null mount?\n");
146 		vput(lowerrootvp);
147 		return (EDEADLK);
148 	}
149 
150 	xmp = (struct null_mount *) malloc(sizeof(struct null_mount),
151 	    M_NULLFSMNT, M_WAITOK | M_ZERO);
152 
153 	/*
154 	 * Save reference to underlying FS
155 	 */
156 	xmp->nullm_vfs = lowerrootvp->v_mount;
157 
158 	/*
159 	 * Save reference.  Each mount also holds
160 	 * a reference on the root vnode.
161 	 */
162 	error = null_nodeget(mp, lowerrootvp, &vp);
163 	/*
164 	 * Make sure the node alias worked
165 	 */
166 	if (error) {
167 		free(xmp, M_NULLFSMNT);
168 		return (error);
169 	}
170 
171 	/*
172 	 * Keep a held reference to the root vnode.
173 	 * It is vrele'd in nullfs_unmount.
174 	 */
175 	nullm_rootvp = vp;
176 	nullm_rootvp->v_vflag |= VV_ROOT;
177 	xmp->nullm_rootvp = nullm_rootvp;
178 
179 	/*
180 	 * Unlock the node (either the lower or the alias)
181 	 */
182 	VOP_UNLOCK(vp, 0);
183 
184 	if (NULLVPTOLOWERVP(nullm_rootvp)->v_mount->mnt_flag & MNT_LOCAL) {
185 		MNT_ILOCK(mp);
186 		mp->mnt_flag |= MNT_LOCAL;
187 		MNT_IUNLOCK(mp);
188 	}
189 
190 	xmp->nullm_flags |= NULLM_CACHE;
191 	if (vfs_getopt(mp->mnt_optnew, "nocache", NULL, NULL) == 0)
192 		xmp->nullm_flags &= ~NULLM_CACHE;
193 
194 	MNT_ILOCK(mp);
195 	if ((xmp->nullm_flags & NULLM_CACHE) != 0) {
196 		mp->mnt_kern_flag |= lowerrootvp->v_mount->mnt_kern_flag &
197 		    (MNTK_SHARED_WRITES | MNTK_LOOKUP_SHARED |
198 		    MNTK_EXTENDED_SHARED);
199 	}
200 	mp->mnt_kern_flag |= MNTK_LOOKUP_EXCL_DOTDOT;
201 	mp->mnt_kern_flag |= lowerrootvp->v_mount->mnt_kern_flag &
202 	    MNTK_USES_BCACHE;
203 	MNT_IUNLOCK(mp);
204 	mp->mnt_data = xmp;
205 	vfs_getnewfsid(mp);
206 	if ((xmp->nullm_flags & NULLM_CACHE) != 0) {
207 		MNT_ILOCK(xmp->nullm_vfs);
208 		TAILQ_INSERT_TAIL(&xmp->nullm_vfs->mnt_uppers, mp,
209 		    mnt_upper_link);
210 		MNT_IUNLOCK(xmp->nullm_vfs);
211 	}
212 
213 	vfs_mountedfrom(mp, target);
214 
215 	NULLFSDEBUG("nullfs_mount: lower %s, alias at %s\n",
216 		mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
217 	return (0);
218 }
219 
220 /*
221  * Free reference to null layer
222  */
223 static int
224 nullfs_unmount(mp, mntflags)
225 	struct mount *mp;
226 	int mntflags;
227 {
228 	struct null_mount *mntdata;
229 	struct mount *ump;
230 	int error, flags;
231 
232 	NULLFSDEBUG("nullfs_unmount: mp = %p\n", (void *)mp);
233 
234 	if (mntflags & MNT_FORCE)
235 		flags = FORCECLOSE;
236 	else
237 		flags = 0;
238 
239 	/* There is 1 extra root vnode reference (nullm_rootvp). */
240 	error = vflush(mp, 1, flags, curthread);
241 	if (error)
242 		return (error);
243 
244 	/*
245 	 * Finally, throw away the null_mount structure
246 	 */
247 	mntdata = mp->mnt_data;
248 	ump = mntdata->nullm_vfs;
249 	if ((mntdata->nullm_flags & NULLM_CACHE) != 0) {
250 		MNT_ILOCK(ump);
251 		while ((ump->mnt_kern_flag & MNTK_VGONE_UPPER) != 0) {
252 			ump->mnt_kern_flag |= MNTK_VGONE_WAITER;
253 			msleep(&ump->mnt_uppers, &ump->mnt_mtx, 0, "vgnupw", 0);
254 		}
255 		TAILQ_REMOVE(&ump->mnt_uppers, mp, mnt_upper_link);
256 		MNT_IUNLOCK(ump);
257 	}
258 	mp->mnt_data = NULL;
259 	free(mntdata, M_NULLFSMNT);
260 	return (0);
261 }
262 
263 static int
264 nullfs_root(mp, flags, vpp)
265 	struct mount *mp;
266 	int flags;
267 	struct vnode **vpp;
268 {
269 	struct vnode *vp;
270 
271 	NULLFSDEBUG("nullfs_root(mp = %p, vp = %p->%p)\n", (void *)mp,
272 	    (void *)MOUNTTONULLMOUNT(mp)->nullm_rootvp,
273 	    (void *)NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp));
274 
275 	/*
276 	 * Return locked reference to root.
277 	 */
278 	vp = MOUNTTONULLMOUNT(mp)->nullm_rootvp;
279 	VREF(vp);
280 
281 	ASSERT_VOP_UNLOCKED(vp, "root vnode is locked");
282 	vn_lock(vp, flags | LK_RETRY);
283 	*vpp = vp;
284 	return 0;
285 }
286 
287 static int
288 nullfs_quotactl(mp, cmd, uid, arg)
289 	struct mount *mp;
290 	int cmd;
291 	uid_t uid;
292 	void *arg;
293 {
294 	return VFS_QUOTACTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, uid, arg);
295 }
296 
297 static int
298 nullfs_statfs(mp, sbp)
299 	struct mount *mp;
300 	struct statfs *sbp;
301 {
302 	int error;
303 	struct statfs mstat;
304 
305 	NULLFSDEBUG("nullfs_statfs(mp = %p, vp = %p->%p)\n", (void *)mp,
306 	    (void *)MOUNTTONULLMOUNT(mp)->nullm_rootvp,
307 	    (void *)NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp));
308 
309 	bzero(&mstat, sizeof(mstat));
310 
311 	error = VFS_STATFS(MOUNTTONULLMOUNT(mp)->nullm_vfs, &mstat);
312 	if (error)
313 		return (error);
314 
315 	/* now copy across the "interesting" information and fake the rest */
316 	sbp->f_type = mstat.f_type;
317 	sbp->f_flags = (sbp->f_flags & (MNT_RDONLY | MNT_NOEXEC | MNT_NOSUID |
318 	    MNT_UNION | MNT_NOSYMFOLLOW)) | (mstat.f_flags & ~MNT_ROOTFS);
319 	sbp->f_bsize = mstat.f_bsize;
320 	sbp->f_iosize = mstat.f_iosize;
321 	sbp->f_blocks = mstat.f_blocks;
322 	sbp->f_bfree = mstat.f_bfree;
323 	sbp->f_bavail = mstat.f_bavail;
324 	sbp->f_files = mstat.f_files;
325 	sbp->f_ffree = mstat.f_ffree;
326 	return (0);
327 }
328 
329 static int
330 nullfs_sync(mp, waitfor)
331 	struct mount *mp;
332 	int waitfor;
333 {
334 	/*
335 	 * XXX - Assumes no data cached at null layer.
336 	 */
337 	return (0);
338 }
339 
340 static int
341 nullfs_vget(mp, ino, flags, vpp)
342 	struct mount *mp;
343 	ino_t ino;
344 	int flags;
345 	struct vnode **vpp;
346 {
347 	int error;
348 
349 	KASSERT((flags & LK_TYPE_MASK) != 0,
350 	    ("nullfs_vget: no lock requested"));
351 
352 	error = VFS_VGET(MOUNTTONULLMOUNT(mp)->nullm_vfs, ino, flags, vpp);
353 	if (error != 0)
354 		return (error);
355 	return (null_nodeget(mp, *vpp, vpp));
356 }
357 
358 static int
359 nullfs_fhtovp(mp, fidp, flags, vpp)
360 	struct mount *mp;
361 	struct fid *fidp;
362 	int flags;
363 	struct vnode **vpp;
364 {
365 	int error;
366 
367 	error = VFS_FHTOVP(MOUNTTONULLMOUNT(mp)->nullm_vfs, fidp, flags,
368 	    vpp);
369 	if (error != 0)
370 		return (error);
371 	return (null_nodeget(mp, *vpp, vpp));
372 }
373 
374 static int
375 nullfs_extattrctl(mp, cmd, filename_vp, namespace, attrname)
376 	struct mount *mp;
377 	int cmd;
378 	struct vnode *filename_vp;
379 	int namespace;
380 	const char *attrname;
381 {
382 
383 	return (VFS_EXTATTRCTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd,
384 	    filename_vp, namespace, attrname));
385 }
386 
387 static void
388 nullfs_reclaim_lowervp(struct mount *mp, struct vnode *lowervp)
389 {
390 	struct vnode *vp;
391 
392 	vp = null_hashget(mp, lowervp);
393 	if (vp == NULL)
394 		return;
395 	VTONULL(vp)->null_flags |= NULLV_NOUNLOCK;
396 	vgone(vp);
397 	vput(vp);
398 }
399 
400 static void
401 nullfs_unlink_lowervp(struct mount *mp, struct vnode *lowervp)
402 {
403 	struct vnode *vp;
404 	struct null_node *xp;
405 
406 	vp = null_hashget(mp, lowervp);
407 	if (vp == NULL)
408 		return;
409 	xp = VTONULL(vp);
410 	xp->null_flags |= NULLV_DROP | NULLV_NOUNLOCK;
411 	vhold(vp);
412 	vunref(vp);
413 
414 	if (vp->v_usecount == 0) {
415 		/*
416 		 * If vunref() dropped the last use reference on the
417 		 * nullfs vnode, it must be reclaimed, and its lock
418 		 * was split from the lower vnode lock.  Need to do
419 		 * extra unlock before allowing the final vdrop() to
420 		 * free the vnode.
421 		 */
422 		KASSERT((vp->v_iflag & VI_DOOMED) != 0,
423 		    ("not reclaimed nullfs vnode %p", vp));
424 		VOP_UNLOCK(vp, 0);
425 	} else {
426 		/*
427 		 * Otherwise, the nullfs vnode still shares the lock
428 		 * with the lower vnode, and must not be unlocked.
429 		 * Also clear the NULLV_NOUNLOCK, the flag is not
430 		 * relevant for future reclamations.
431 		 */
432 		ASSERT_VOP_ELOCKED(vp, "unlink_lowervp");
433 		KASSERT((vp->v_iflag & VI_DOOMED) == 0,
434 		    ("reclaimed nullfs vnode %p", vp));
435 		xp->null_flags &= ~NULLV_NOUNLOCK;
436 	}
437 	vdrop(vp);
438 }
439 
440 static struct vfsops null_vfsops = {
441 	.vfs_extattrctl =	nullfs_extattrctl,
442 	.vfs_fhtovp =		nullfs_fhtovp,
443 	.vfs_init =		nullfs_init,
444 	.vfs_mount =		nullfs_mount,
445 	.vfs_quotactl =		nullfs_quotactl,
446 	.vfs_root =		nullfs_root,
447 	.vfs_statfs =		nullfs_statfs,
448 	.vfs_sync =		nullfs_sync,
449 	.vfs_uninit =		nullfs_uninit,
450 	.vfs_unmount =		nullfs_unmount,
451 	.vfs_vget =		nullfs_vget,
452 	.vfs_reclaim_lowervp =	nullfs_reclaim_lowervp,
453 	.vfs_unlink_lowervp =	nullfs_unlink_lowervp,
454 };
455 
456 VFS_SET(null_vfsops, nullfs, VFCF_LOOPBACK | VFCF_JAIL);
457