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