xref: /freebsd/sys/fs/unionfs/union_vfsops.c (revision c1d255d3)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1994, 1995 The Regents of the University of California.
5  * Copyright (c) 1994, 1995 Jan-Simon Pendry.
6  * Copyright (c) 2005, 2006, 2012 Masanori Ozawa <ozawa@ongs.co.jp>, ONGS Inc.
7  * Copyright (c) 2006, 2012 Daichi Goto <daichi@freebsd.org>
8  * All rights reserved.
9  *
10  * This code is derived from software donated to Berkeley by
11  * Jan-Simon Pendry.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	@(#)union_vfsops.c	8.20 (Berkeley) 5/20/95
38  * $FreeBSD$
39  */
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kdb.h>
44 #include <sys/fcntl.h>
45 #include <sys/kernel.h>
46 #include <sys/lock.h>
47 #include <sys/malloc.h>
48 #include <sys/mount.h>
49 #include <sys/namei.h>
50 #include <sys/proc.h>
51 #include <sys/vnode.h>
52 #include <sys/stat.h>
53 
54 #include <fs/unionfs/union.h>
55 
56 static MALLOC_DEFINE(M_UNIONFSMNT, "UNIONFS mount", "UNIONFS mount structure");
57 
58 static vfs_fhtovp_t	unionfs_fhtovp;
59 static vfs_checkexp_t	unionfs_checkexp;
60 static vfs_mount_t	unionfs_domount;
61 static vfs_quotactl_t	unionfs_quotactl;
62 static vfs_root_t	unionfs_root;
63 static vfs_sync_t	unionfs_sync;
64 static vfs_statfs_t	unionfs_statfs;
65 static vfs_unmount_t	unionfs_unmount;
66 static vfs_vget_t	unionfs_vget;
67 static vfs_extattrctl_t	unionfs_extattrctl;
68 
69 static struct vfsops unionfs_vfsops;
70 
71 /*
72  * Mount unionfs layer.
73  */
74 static int
75 unionfs_domount(struct mount *mp)
76 {
77 	struct mount   *lowermp, *uppermp;
78 	struct vnode   *lowerrootvp;
79 	struct vnode   *upperrootvp;
80 	struct unionfs_mount *ump;
81 	struct thread *td;
82 	char           *target;
83 	char           *tmp;
84 	char           *ep;
85 	struct nameidata nd, *ndp;
86 	struct vattr	va;
87 	unionfs_copymode copymode;
88 	unionfs_whitemode whitemode;
89 	int		below;
90 	int		error;
91 	int		len;
92 	uid_t		uid;
93 	gid_t		gid;
94 	u_short		udir;
95 	u_short		ufile;
96 
97 	UNIONFSDEBUG("unionfs_mount(mp = %p)\n", mp);
98 
99 	error = 0;
100 	below = 0;
101 	uid = 0;
102 	gid = 0;
103 	udir = 0;
104 	ufile = 0;
105 	copymode = UNIONFS_TRANSPARENT;	/* default */
106 	whitemode = UNIONFS_WHITE_ALWAYS;
107 	ndp = &nd;
108 	td = curthread;
109 
110 	if (mp->mnt_flag & MNT_ROOTFS) {
111 		vfs_mount_error(mp, "Cannot union mount root filesystem");
112 		return (EOPNOTSUPP);
113 	}
114 
115 	/*
116 	 * Update is a no operation.
117 	 */
118 	if (mp->mnt_flag & MNT_UPDATE) {
119 		vfs_mount_error(mp, "unionfs does not support mount update");
120 		return (EOPNOTSUPP);
121 	}
122 
123 	/*
124 	 * Get argument
125 	 */
126 	error = vfs_getopt(mp->mnt_optnew, "target", (void **)&target, &len);
127 	if (error)
128 		error = vfs_getopt(mp->mnt_optnew, "from", (void **)&target,
129 		    &len);
130 	if (error || target[len - 1] != '\0') {
131 		vfs_mount_error(mp, "Invalid target");
132 		return (EINVAL);
133 	}
134 	if (vfs_getopt(mp->mnt_optnew, "below", NULL, NULL) == 0)
135 		below = 1;
136 	if (vfs_getopt(mp->mnt_optnew, "udir", (void **)&tmp, NULL) == 0) {
137 		if (tmp != NULL)
138 			udir = (mode_t)strtol(tmp, &ep, 8);
139 		if (tmp == NULL || *ep) {
140 			vfs_mount_error(mp, "Invalid udir");
141 			return (EINVAL);
142 		}
143 		udir &= S_IRWXU | S_IRWXG | S_IRWXO;
144 	}
145 	if (vfs_getopt(mp->mnt_optnew, "ufile", (void **)&tmp, NULL) == 0) {
146 		if (tmp != NULL)
147 			ufile = (mode_t)strtol(tmp, &ep, 8);
148 		if (tmp == NULL || *ep) {
149 			vfs_mount_error(mp, "Invalid ufile");
150 			return (EINVAL);
151 		}
152 		ufile &= S_IRWXU | S_IRWXG | S_IRWXO;
153 	}
154 	/* check umask, uid and gid */
155 	if (udir == 0 && ufile != 0)
156 		udir = ufile;
157 	if (ufile == 0 && udir != 0)
158 		ufile = udir;
159 
160 	vn_lock(mp->mnt_vnodecovered, LK_SHARED | LK_RETRY);
161 	error = VOP_GETATTR(mp->mnt_vnodecovered, &va, mp->mnt_cred);
162 	if (!error) {
163 		if (udir == 0)
164 			udir = va.va_mode;
165 		if (ufile == 0)
166 			ufile = va.va_mode;
167 		uid = va.va_uid;
168 		gid = va.va_gid;
169 	}
170 	VOP_UNLOCK(mp->mnt_vnodecovered);
171 	if (error)
172 		return (error);
173 
174 	if (mp->mnt_cred->cr_ruid == 0) {	/* root only */
175 		if (vfs_getopt(mp->mnt_optnew, "uid", (void **)&tmp,
176 		    NULL) == 0) {
177 			if (tmp != NULL)
178 				uid = (uid_t)strtol(tmp, &ep, 10);
179 			if (tmp == NULL || *ep) {
180 				vfs_mount_error(mp, "Invalid uid");
181 				return (EINVAL);
182 			}
183 		}
184 		if (vfs_getopt(mp->mnt_optnew, "gid", (void **)&tmp,
185 		    NULL) == 0) {
186 			if (tmp != NULL)
187 				gid = (gid_t)strtol(tmp, &ep, 10);
188 			if (tmp == NULL || *ep) {
189 				vfs_mount_error(mp, "Invalid gid");
190 				return (EINVAL);
191 			}
192 		}
193 		if (vfs_getopt(mp->mnt_optnew, "copymode", (void **)&tmp,
194 		    NULL) == 0) {
195 			if (tmp == NULL) {
196 				vfs_mount_error(mp, "Invalid copymode");
197 				return (EINVAL);
198 			} else if (strcasecmp(tmp, "traditional") == 0)
199 				copymode = UNIONFS_TRADITIONAL;
200 			else if (strcasecmp(tmp, "transparent") == 0)
201 				copymode = UNIONFS_TRANSPARENT;
202 			else if (strcasecmp(tmp, "masquerade") == 0)
203 				copymode = UNIONFS_MASQUERADE;
204 			else {
205 				vfs_mount_error(mp, "Invalid copymode");
206 				return (EINVAL);
207 			}
208 		}
209 		if (vfs_getopt(mp->mnt_optnew, "whiteout", (void **)&tmp,
210 		    NULL) == 0) {
211 			if (tmp == NULL) {
212 				vfs_mount_error(mp, "Invalid whiteout mode");
213 				return (EINVAL);
214 			} else if (strcasecmp(tmp, "always") == 0)
215 				whitemode = UNIONFS_WHITE_ALWAYS;
216 			else if (strcasecmp(tmp, "whenneeded") == 0)
217 				whitemode = UNIONFS_WHITE_WHENNEEDED;
218 			else {
219 				vfs_mount_error(mp, "Invalid whiteout mode");
220 				return (EINVAL);
221 			}
222 		}
223 	}
224 	/* If copymode is UNIONFS_TRADITIONAL, uid/gid is mounted user. */
225 	if (copymode == UNIONFS_TRADITIONAL) {
226 		uid = mp->mnt_cred->cr_ruid;
227 		gid = mp->mnt_cred->cr_rgid;
228 	}
229 
230 	UNIONFSDEBUG("unionfs_mount: uid=%d, gid=%d\n", uid, gid);
231 	UNIONFSDEBUG("unionfs_mount: udir=0%03o, ufile=0%03o\n", udir, ufile);
232 	UNIONFSDEBUG("unionfs_mount: copymode=%d\n", copymode);
233 
234 	/*
235 	 * Find upper node
236 	 */
237 	NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, target, td);
238 	if ((error = namei(ndp)))
239 		return (error);
240 
241 	NDFREE(ndp, NDF_ONLY_PNBUF);
242 
243 	/* get root vnodes */
244 	lowerrootvp = mp->mnt_vnodecovered;
245 	upperrootvp = ndp->ni_vp;
246 
247 	/* create unionfs_mount */
248 	ump = malloc(sizeof(struct unionfs_mount), M_UNIONFSMNT,
249 	    M_WAITOK | M_ZERO);
250 
251 	/*
252 	 * Save reference
253 	 */
254 	if (below) {
255 		VOP_UNLOCK(upperrootvp);
256 		vn_lock(lowerrootvp, LK_EXCLUSIVE | LK_RETRY);
257 		ump->um_lowervp = upperrootvp;
258 		ump->um_uppervp = lowerrootvp;
259 	} else {
260 		ump->um_lowervp = lowerrootvp;
261 		ump->um_uppervp = upperrootvp;
262 	}
263 	ump->um_rootvp = NULLVP;
264 	ump->um_uid = uid;
265 	ump->um_gid = gid;
266 	ump->um_udir = udir;
267 	ump->um_ufile = ufile;
268 	ump->um_copymode = copymode;
269 	ump->um_whitemode = whitemode;
270 
271 	mp->mnt_data = ump;
272 
273 	/*
274 	 * Copy upper layer's RDONLY flag.
275 	 */
276 	mp->mnt_flag |= ump->um_uppervp->v_mount->mnt_flag & MNT_RDONLY;
277 
278 	/*
279 	 * Unlock the node
280 	 */
281 	VOP_UNLOCK(ump->um_uppervp);
282 
283 	/*
284 	 * Get the unionfs root vnode.
285 	 */
286 	error = unionfs_nodeget(mp, ump->um_uppervp, ump->um_lowervp,
287 	    NULLVP, &(ump->um_rootvp), NULL, td);
288 	vrele(upperrootvp);
289 	if (error != 0) {
290 		free(ump, M_UNIONFSMNT);
291 		mp->mnt_data = NULL;
292 		return (error);
293 	}
294 
295 	lowermp = vfs_register_upper_from_vp(ump->um_lowervp, mp,
296 	    &ump->um_lower_link);
297 	uppermp = vfs_register_upper_from_vp(ump->um_uppervp, mp,
298 	    &ump->um_upper_link);
299 
300 	if (lowermp == NULL || uppermp == NULL) {
301 		if (lowermp != NULL)
302 			vfs_unregister_upper(lowermp, &ump->um_lower_link);
303 		if (uppermp != NULL)
304 			vfs_unregister_upper(uppermp, &ump->um_upper_link);
305 		free(ump, M_UNIONFSMNT);
306 		mp->mnt_data = NULL;
307 		return (ENOENT);
308 	}
309 
310 	MNT_ILOCK(mp);
311 	if ((lowermp->mnt_flag & MNT_LOCAL) != 0 &&
312 	    (uppermp->mnt_flag & MNT_LOCAL) != 0)
313 		mp->mnt_flag |= MNT_LOCAL;
314 	mp->mnt_kern_flag |= MNTK_NOMSYNC | MNTK_UNIONFS;
315 	MNT_IUNLOCK(mp);
316 
317 	/*
318 	 * Get new fsid
319 	 */
320 	vfs_getnewfsid(mp);
321 
322 	snprintf(mp->mnt_stat.f_mntfromname, MNAMELEN, "<%s>:%s",
323 	    below ? "below" : "above", target);
324 
325 	UNIONFSDEBUG("unionfs_mount: from %s, on %s\n",
326 	    mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
327 
328 	return (0);
329 }
330 
331 /*
332  * Free reference to unionfs layer
333  */
334 static int
335 unionfs_unmount(struct mount *mp, int mntflags)
336 {
337 	struct unionfs_mount *ump;
338 	int		error;
339 	int		num;
340 	int		freeing;
341 	int		flags;
342 
343 	UNIONFSDEBUG("unionfs_unmount: mp = %p\n", mp);
344 
345 	ump = MOUNTTOUNIONFSMOUNT(mp);
346 	flags = 0;
347 
348 	if (mntflags & MNT_FORCE)
349 		flags |= FORCECLOSE;
350 
351 	/* vflush (no need to call vrele) */
352 	for (freeing = 0; (error = vflush(mp, 1, flags, curthread)) != 0;) {
353 		num = mp->mnt_nvnodelistsize;
354 		if (num == freeing)
355 			break;
356 		freeing = num;
357 	}
358 
359 	if (error)
360 		return (error);
361 
362 	vfs_unregister_upper(ump->um_lowervp->v_mount, &ump->um_lower_link);
363 	vfs_unregister_upper(ump->um_uppervp->v_mount, &ump->um_upper_link);
364 	free(ump, M_UNIONFSMNT);
365 	mp->mnt_data = NULL;
366 
367 	return (0);
368 }
369 
370 static int
371 unionfs_root(struct mount *mp, int flags, struct vnode **vpp)
372 {
373 	struct unionfs_mount *ump;
374 	struct vnode *vp;
375 
376 	ump = MOUNTTOUNIONFSMOUNT(mp);
377 	vp = ump->um_rootvp;
378 
379 	UNIONFSDEBUG("unionfs_root: rootvp=%p locked=%x\n",
380 	    vp, VOP_ISLOCKED(vp));
381 
382 	vref(vp);
383 	if (flags & LK_TYPE_MASK)
384 		vn_lock(vp, flags);
385 
386 	*vpp = vp;
387 
388 	return (0);
389 }
390 
391 static int
392 unionfs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg,
393     bool *mp_busy)
394 {
395 	struct mount *uppermp;
396 	struct unionfs_mount *ump;
397 	int error;
398 	bool unbusy;
399 
400 	ump = MOUNTTOUNIONFSMOUNT(mp);
401 	uppermp = atomic_load_ptr(&ump->um_uppervp->v_mount);
402 	KASSERT(*mp_busy == true, ("upper mount not busy"));
403 	/*
404 	 * See comment in sys_quotactl() for an explanation of why the
405 	 * lower mount needs to be busied by the caller of VFS_QUOTACTL()
406 	 * but may be unbusied by the implementation.  We must unbusy
407 	 * the upper mount for the same reason; otherwise a namei lookup
408 	 * issued by the VFS_QUOTACTL() implementation could traverse the
409 	 * upper mount and deadlock.
410 	 */
411 	vfs_unbusy(mp);
412 	*mp_busy = false;
413 	unbusy = true;
414 	error = vfs_busy(uppermp, 0);
415 	/*
416 	 * Writing is always performed to upper vnode.
417 	 */
418 	if (error == 0)
419 		error = VFS_QUOTACTL(uppermp, cmd, uid, arg, &unbusy);
420 	if (unbusy)
421 		vfs_unbusy(uppermp);
422 
423 	return (error);
424 }
425 
426 static int
427 unionfs_statfs(struct mount *mp, struct statfs *sbp)
428 {
429 	struct unionfs_mount *ump;
430 	struct statfs	*mstat;
431 	uint64_t	lbsize;
432 	int		error;
433 
434 	ump = MOUNTTOUNIONFSMOUNT(mp);
435 
436 	UNIONFSDEBUG("unionfs_statfs(mp = %p, lvp = %p, uvp = %p)\n",
437 	    mp, ump->um_lowervp, ump->um_uppervp);
438 
439 	mstat = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK | M_ZERO);
440 
441 	error = VFS_STATFS(ump->um_lowervp->v_mount, mstat);
442 	if (error) {
443 		free(mstat, M_STATFS);
444 		return (error);
445 	}
446 
447 	/* now copy across the "interesting" information and fake the rest */
448 	sbp->f_blocks = mstat->f_blocks;
449 	sbp->f_files = mstat->f_files;
450 
451 	lbsize = mstat->f_bsize;
452 
453 	error = VFS_STATFS(ump->um_uppervp->v_mount, mstat);
454 	if (error) {
455 		free(mstat, M_STATFS);
456 		return (error);
457 	}
458 
459 	/*
460 	 * The FS type etc is copy from upper vfs.
461 	 * (write able vfs have priority)
462 	 */
463 	sbp->f_type = mstat->f_type;
464 	sbp->f_flags = mstat->f_flags;
465 	sbp->f_bsize = mstat->f_bsize;
466 	sbp->f_iosize = mstat->f_iosize;
467 
468 	if (mstat->f_bsize != lbsize)
469 		sbp->f_blocks = ((off_t)sbp->f_blocks * lbsize) /
470 		    mstat->f_bsize;
471 
472 	sbp->f_blocks += mstat->f_blocks;
473 	sbp->f_bfree = mstat->f_bfree;
474 	sbp->f_bavail = mstat->f_bavail;
475 	sbp->f_files += mstat->f_files;
476 	sbp->f_ffree = mstat->f_ffree;
477 
478 	free(mstat, M_STATFS);
479 	return (0);
480 }
481 
482 static int
483 unionfs_sync(struct mount *mp, int waitfor)
484 {
485 	/* nothing to do */
486 	return (0);
487 }
488 
489 static int
490 unionfs_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
491 {
492 	return (EOPNOTSUPP);
493 }
494 
495 static int
496 unionfs_fhtovp(struct mount *mp, struct fid *fidp, int flags,
497     struct vnode **vpp)
498 {
499 	return (EOPNOTSUPP);
500 }
501 
502 static int
503 unionfs_checkexp(struct mount *mp, struct sockaddr *nam, uint64_t *extflagsp,
504     struct ucred **credanonp, int *numsecflavors, int *secflavors)
505 {
506 	return (EOPNOTSUPP);
507 }
508 
509 static int
510 unionfs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
511     int namespace, const char *attrname)
512 {
513 	struct unionfs_mount *ump;
514 	struct unionfs_node *unp;
515 
516 	ump = MOUNTTOUNIONFSMOUNT(mp);
517 	unp = VTOUNIONFS(filename_vp);
518 
519 	if (unp->un_uppervp != NULLVP) {
520 		return (VFS_EXTATTRCTL(ump->um_uppervp->v_mount, cmd,
521 		    unp->un_uppervp, namespace, attrname));
522 	} else {
523 		return (VFS_EXTATTRCTL(ump->um_lowervp->v_mount, cmd,
524 		    unp->un_lowervp, namespace, attrname));
525 	}
526 }
527 
528 static struct vfsops unionfs_vfsops = {
529 	.vfs_checkexp =		unionfs_checkexp,
530 	.vfs_extattrctl =	unionfs_extattrctl,
531 	.vfs_fhtovp =		unionfs_fhtovp,
532 	.vfs_init =		unionfs_init,
533 	.vfs_mount =		unionfs_domount,
534 	.vfs_quotactl =		unionfs_quotactl,
535 	.vfs_root =		unionfs_root,
536 	.vfs_statfs =		unionfs_statfs,
537 	.vfs_sync =		unionfs_sync,
538 	.vfs_uninit =		unionfs_uninit,
539 	.vfs_unmount =		unionfs_unmount,
540 	.vfs_vget =		unionfs_vget,
541 };
542 
543 VFS_SET(unionfs_vfsops, unionfs, VFCF_LOOPBACK);
544