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 <[email protected]>, ONGS Inc.
7 * Copyright (c) 2006, 2012 Daichi Goto <[email protected]>
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 */
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kdb.h>
43 #include <sys/fcntl.h>
44 #include <sys/kernel.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/namei.h>
49 #include <sys/proc.h>
50 #include <sys/vnode.h>
51 #include <sys/stat.h>
52
53 #include <fs/unionfs/union.h>
54
55 static MALLOC_DEFINE(M_UNIONFSMNT, "UNIONFS mount", "UNIONFS mount structure");
56
57 static vfs_fhtovp_t unionfs_fhtovp;
58 static vfs_checkexp_t unionfs_checkexp;
59 static vfs_mount_t unionfs_domount;
60 static vfs_quotactl_t unionfs_quotactl;
61 static vfs_root_t unionfs_root;
62 static vfs_sync_t unionfs_sync;
63 static vfs_statfs_t unionfs_statfs;
64 static vfs_unmount_t unionfs_unmount;
65 static vfs_vget_t unionfs_vget;
66 static vfs_extattrctl_t unionfs_extattrctl;
67
68 static struct vfsops unionfs_vfsops;
69
70 /*
71 * Mount unionfs layer.
72 */
73 static int
unionfs_domount(struct mount * mp)74 unionfs_domount(struct mount *mp)
75 {
76 struct vnode *lowerrootvp;
77 struct vnode *upperrootvp;
78 struct unionfs_mount *ump;
79 char *target;
80 char *tmp;
81 char *ep;
82 struct nameidata nd, *ndp;
83 struct vattr va;
84 unionfs_copymode copymode;
85 unionfs_whitemode whitemode;
86 int below;
87 int error;
88 int len;
89 uid_t uid;
90 gid_t gid;
91 u_short udir;
92 u_short ufile;
93
94 UNIONFSDEBUG("unionfs_mount(mp = %p)\n", mp);
95
96 error = 0;
97 below = 0;
98 uid = 0;
99 gid = 0;
100 udir = 0;
101 ufile = 0;
102 copymode = UNIONFS_TRANSPARENT; /* default */
103 whitemode = UNIONFS_WHITE_ALWAYS;
104 ndp = &nd;
105
106 if (mp->mnt_flag & MNT_ROOTFS) {
107 vfs_mount_error(mp, "Cannot union mount root filesystem");
108 return (EOPNOTSUPP);
109 }
110
111 /*
112 * Update is a no operation.
113 */
114 if (mp->mnt_flag & MNT_UPDATE) {
115 vfs_mount_error(mp, "unionfs does not support mount update");
116 return (EOPNOTSUPP);
117 }
118
119 /*
120 * Get argument
121 */
122 error = vfs_getopt(mp->mnt_optnew, "target", (void **)&target, &len);
123 if (error)
124 error = vfs_getopt(mp->mnt_optnew, "from", (void **)&target,
125 &len);
126 if (error || target[len - 1] != '\0') {
127 vfs_mount_error(mp, "Invalid target");
128 return (EINVAL);
129 }
130 if (vfs_getopt(mp->mnt_optnew, "below", NULL, NULL) == 0)
131 below = 1;
132 if (vfs_getopt(mp->mnt_optnew, "udir", (void **)&tmp, NULL) == 0) {
133 if (tmp != NULL)
134 udir = (mode_t)strtol(tmp, &ep, 8);
135 if (tmp == NULL || *ep) {
136 vfs_mount_error(mp, "Invalid udir");
137 return (EINVAL);
138 }
139 udir &= S_IRWXU | S_IRWXG | S_IRWXO;
140 }
141 if (vfs_getopt(mp->mnt_optnew, "ufile", (void **)&tmp, NULL) == 0) {
142 if (tmp != NULL)
143 ufile = (mode_t)strtol(tmp, &ep, 8);
144 if (tmp == NULL || *ep) {
145 vfs_mount_error(mp, "Invalid ufile");
146 return (EINVAL);
147 }
148 ufile &= S_IRWXU | S_IRWXG | S_IRWXO;
149 }
150 /* check umask, uid and gid */
151 if (udir == 0 && ufile != 0)
152 udir = ufile;
153 if (ufile == 0 && udir != 0)
154 ufile = udir;
155
156 vn_lock(mp->mnt_vnodecovered, LK_SHARED | LK_RETRY);
157 error = VOP_GETATTR(mp->mnt_vnodecovered, &va, mp->mnt_cred);
158 if (!error) {
159 if (udir == 0)
160 udir = va.va_mode;
161 if (ufile == 0)
162 ufile = va.va_mode;
163 uid = va.va_uid;
164 gid = va.va_gid;
165 }
166 VOP_UNLOCK(mp->mnt_vnodecovered);
167 if (error)
168 return (error);
169
170 if (mp->mnt_cred->cr_ruid == 0) { /* root only */
171 if (vfs_getopt(mp->mnt_optnew, "uid", (void **)&tmp,
172 NULL) == 0) {
173 if (tmp != NULL)
174 uid = (uid_t)strtol(tmp, &ep, 10);
175 if (tmp == NULL || *ep) {
176 vfs_mount_error(mp, "Invalid uid");
177 return (EINVAL);
178 }
179 }
180 if (vfs_getopt(mp->mnt_optnew, "gid", (void **)&tmp,
181 NULL) == 0) {
182 if (tmp != NULL)
183 gid = (gid_t)strtol(tmp, &ep, 10);
184 if (tmp == NULL || *ep) {
185 vfs_mount_error(mp, "Invalid gid");
186 return (EINVAL);
187 }
188 }
189 if (vfs_getopt(mp->mnt_optnew, "copymode", (void **)&tmp,
190 NULL) == 0) {
191 if (tmp == NULL) {
192 vfs_mount_error(mp, "Invalid copymode");
193 return (EINVAL);
194 } else if (strcasecmp(tmp, "traditional") == 0)
195 copymode = UNIONFS_TRADITIONAL;
196 else if (strcasecmp(tmp, "transparent") == 0)
197 copymode = UNIONFS_TRANSPARENT;
198 else if (strcasecmp(tmp, "masquerade") == 0)
199 copymode = UNIONFS_MASQUERADE;
200 else {
201 vfs_mount_error(mp, "Invalid copymode");
202 return (EINVAL);
203 }
204 }
205 if (vfs_getopt(mp->mnt_optnew, "whiteout", (void **)&tmp,
206 NULL) == 0) {
207 if (tmp == NULL) {
208 vfs_mount_error(mp, "Invalid whiteout mode");
209 return (EINVAL);
210 } else if (strcasecmp(tmp, "always") == 0)
211 whitemode = UNIONFS_WHITE_ALWAYS;
212 else if (strcasecmp(tmp, "whenneeded") == 0)
213 whitemode = UNIONFS_WHITE_WHENNEEDED;
214 else {
215 vfs_mount_error(mp, "Invalid whiteout mode");
216 return (EINVAL);
217 }
218 }
219 }
220 /* If copymode is UNIONFS_TRADITIONAL, uid/gid is mounted user. */
221 if (copymode == UNIONFS_TRADITIONAL) {
222 uid = mp->mnt_cred->cr_ruid;
223 gid = mp->mnt_cred->cr_rgid;
224 }
225
226 UNIONFSDEBUG("unionfs_mount: uid=%d, gid=%d\n", uid, gid);
227 UNIONFSDEBUG("unionfs_mount: udir=0%03o, ufile=0%03o\n", udir, ufile);
228 UNIONFSDEBUG("unionfs_mount: copymode=%d\n", copymode);
229
230 /*
231 * Find upper node
232 */
233 NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, target);
234 if ((error = namei(ndp)))
235 return (error);
236
237 NDFREE_PNBUF(ndp);
238
239 /* get root vnodes */
240 lowerrootvp = mp->mnt_vnodecovered;
241 upperrootvp = ndp->ni_vp;
242 KASSERT(lowerrootvp != NULL, ("%s: NULL lower root vp", __func__));
243 KASSERT(upperrootvp != NULL, ("%s: NULL upper root vp", __func__));
244
245 /* create unionfs_mount */
246 ump = malloc(sizeof(struct unionfs_mount), M_UNIONFSMNT,
247 M_WAITOK | M_ZERO);
248
249 /*
250 * Save reference
251 */
252 if (below) {
253 VOP_UNLOCK(upperrootvp);
254 vn_lock(lowerrootvp, LK_EXCLUSIVE | LK_RETRY);
255 ump->um_lowervp = upperrootvp;
256 ump->um_uppervp = lowerrootvp;
257 } else {
258 ump->um_lowervp = lowerrootvp;
259 ump->um_uppervp = upperrootvp;
260 }
261 ump->um_rootvp = NULLVP;
262 ump->um_uid = uid;
263 ump->um_gid = gid;
264 ump->um_udir = udir;
265 ump->um_ufile = ufile;
266 ump->um_copymode = copymode;
267 ump->um_whitemode = whitemode;
268
269 mp->mnt_data = ump;
270
271 /*
272 * Copy upper layer's RDONLY flag.
273 */
274 mp->mnt_flag |= ump->um_uppervp->v_mount->mnt_flag & MNT_RDONLY;
275
276 /*
277 * Unlock the node
278 */
279 VOP_UNLOCK(ump->um_uppervp);
280
281 /*
282 * Get the unionfs root vnode.
283 */
284 error = unionfs_nodeget(mp, ump->um_uppervp, ump->um_lowervp,
285 NULLVP, &(ump->um_rootvp), NULL);
286 if (error != 0) {
287 vrele(upperrootvp);
288 free(ump, M_UNIONFSMNT);
289 mp->mnt_data = NULL;
290 return (error);
291 }
292 KASSERT(ump->um_rootvp != NULL, ("rootvp cannot be NULL"));
293 KASSERT((ump->um_rootvp->v_vflag & VV_ROOT) != 0,
294 ("%s: rootvp without VV_ROOT", __func__));
295
296 /*
297 * Do not release the namei() reference on upperrootvp until after
298 * we attempt to register the upper mounts. A concurrent unmount
299 * of the upper or lower FS may have caused unionfs_nodeget() to
300 * create a unionfs node with a NULL upper or lower vp and with
301 * no reference held on upperrootvp or lowerrootvp.
302 * vfs_register_upper() should subsequently fail, which is what
303 * we want, but we must ensure neither underlying vnode can be
304 * reused until that happens. We assume the caller holds a reference
305 * to lowerrootvp as it is the mount's covered vnode.
306 */
307 ump->um_lowermp = vfs_register_upper_from_vp(ump->um_lowervp, mp,
308 &ump->um_lower_link);
309 ump->um_uppermp = vfs_register_upper_from_vp(ump->um_uppervp, mp,
310 &ump->um_upper_link);
311
312 vrele(upperrootvp);
313
314 if (ump->um_lowermp == NULL || ump->um_uppermp == NULL) {
315 if (ump->um_lowermp != NULL)
316 vfs_unregister_upper(ump->um_lowermp, &ump->um_lower_link);
317 if (ump->um_uppermp != NULL)
318 vfs_unregister_upper(ump->um_uppermp, &ump->um_upper_link);
319 vflush(mp, 1, FORCECLOSE, curthread);
320 free(ump, M_UNIONFSMNT);
321 mp->mnt_data = NULL;
322 return (ENOENT);
323 }
324
325 /*
326 * Specify that the covered vnode lock should remain held while
327 * lookup() performs the cross-mount walk. This prevents a lock-order
328 * reversal between the covered vnode lock (which is also locked by
329 * unionfs_lock()) and the mountpoint's busy count. Without this,
330 * unmount will lock the covered vnode lock (directly through the
331 * covered vnode) and wait for the busy count to drain, while a
332 * concurrent lookup will increment the busy count and then lock
333 * the covered vnode lock (indirectly through unionfs_lock()).
334 *
335 * Note that we can't yet use this facility for the 'below' case
336 * in which the upper vnode is the covered vnode, because that would
337 * introduce a different LOR in which the cross-mount lookup would
338 * effectively hold the upper vnode lock before acquiring the lower
339 * vnode lock, while an unrelated lock operation would still acquire
340 * the lower vnode lock before the upper vnode lock, which is the
341 * order unionfs currently requires.
342 */
343 if (!below) {
344 vn_lock(mp->mnt_vnodecovered, LK_EXCLUSIVE | LK_RETRY | LK_CANRECURSE);
345 mp->mnt_vnodecovered->v_vflag |= VV_CROSSLOCK;
346 VOP_UNLOCK(mp->mnt_vnodecovered);
347 }
348
349 MNT_ILOCK(mp);
350 if ((ump->um_lowermp->mnt_flag & MNT_LOCAL) != 0 &&
351 (ump->um_uppermp->mnt_flag & MNT_LOCAL) != 0)
352 mp->mnt_flag |= MNT_LOCAL;
353 mp->mnt_kern_flag |= MNTK_NOMSYNC | MNTK_UNIONFS |
354 (ump->um_uppermp->mnt_kern_flag & MNTK_SHARED_WRITES);
355 MNT_IUNLOCK(mp);
356
357 /*
358 * Get new fsid
359 */
360 vfs_getnewfsid(mp);
361
362 snprintf(mp->mnt_stat.f_mntfromname, MNAMELEN, "<%s>:%s",
363 below ? "below" : "above", target);
364
365 UNIONFSDEBUG("unionfs_mount: from %s, on %s\n",
366 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
367
368 return (0);
369 }
370
371 /*
372 * Free reference to unionfs layer
373 */
374 static int
unionfs_unmount(struct mount * mp,int mntflags)375 unionfs_unmount(struct mount *mp, int mntflags)
376 {
377 struct unionfs_mount *ump;
378 int error;
379 int num;
380 int freeing;
381 int flags;
382
383 UNIONFSDEBUG("unionfs_unmount: mp = %p\n", mp);
384
385 ump = MOUNTTOUNIONFSMOUNT(mp);
386 flags = 0;
387
388 if (mntflags & MNT_FORCE)
389 flags |= FORCECLOSE;
390
391 /* vflush (no need to call vrele) */
392 for (freeing = 0; (error = vflush(mp, 1, flags, curthread)) != 0;) {
393 num = mp->mnt_nvnodelistsize;
394 if (num == freeing)
395 break;
396 freeing = num;
397 }
398
399 if (error)
400 return (error);
401
402 vn_lock(mp->mnt_vnodecovered, LK_EXCLUSIVE | LK_RETRY | LK_CANRECURSE);
403 mp->mnt_vnodecovered->v_vflag &= ~VV_CROSSLOCK;
404 VOP_UNLOCK(mp->mnt_vnodecovered);
405 vfs_unregister_upper(ump->um_lowermp, &ump->um_lower_link);
406 vfs_unregister_upper(ump->um_uppermp, &ump->um_upper_link);
407 free(ump, M_UNIONFSMNT);
408 mp->mnt_data = NULL;
409
410 return (0);
411 }
412
413 static int
unionfs_root(struct mount * mp,int flags,struct vnode ** vpp)414 unionfs_root(struct mount *mp, int flags, struct vnode **vpp)
415 {
416 struct unionfs_mount *ump;
417 struct vnode *vp;
418
419 ump = MOUNTTOUNIONFSMOUNT(mp);
420 vp = ump->um_rootvp;
421
422 UNIONFSDEBUG("unionfs_root: rootvp=%p locked=%x\n",
423 vp, VOP_ISLOCKED(vp));
424
425 vref(vp);
426 if (flags & LK_TYPE_MASK)
427 vn_lock(vp, flags);
428
429 *vpp = vp;
430
431 return (0);
432 }
433
434 static int
unionfs_quotactl(struct mount * mp,int cmd,uid_t uid,void * arg,bool * mp_busy)435 unionfs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg,
436 bool *mp_busy)
437 {
438 struct mount *uppermp;
439 struct unionfs_mount *ump;
440 int error;
441 bool unbusy;
442
443 ump = MOUNTTOUNIONFSMOUNT(mp);
444 /*
445 * Issue a volatile load of um_uppermp here, as the mount may be
446 * torn down after we call vfs_unbusy().
447 */
448 uppermp = atomic_load_ptr(&ump->um_uppermp);
449 KASSERT(*mp_busy == true, ("upper mount not busy"));
450 /*
451 * See comment in sys_quotactl() for an explanation of why the
452 * lower mount needs to be busied by the caller of VFS_QUOTACTL()
453 * but may be unbusied by the implementation. We must unbusy
454 * the upper mount for the same reason; otherwise a namei lookup
455 * issued by the VFS_QUOTACTL() implementation could traverse the
456 * upper mount and deadlock.
457 */
458 vfs_unbusy(mp);
459 *mp_busy = false;
460 unbusy = true;
461 error = vfs_busy(uppermp, 0);
462 /*
463 * Writing is always performed to upper vnode.
464 */
465 if (error == 0)
466 error = VFS_QUOTACTL(uppermp, cmd, uid, arg, &unbusy);
467 if (unbusy)
468 vfs_unbusy(uppermp);
469
470 return (error);
471 }
472
473 static int
unionfs_statfs(struct mount * mp,struct statfs * sbp)474 unionfs_statfs(struct mount *mp, struct statfs *sbp)
475 {
476 struct unionfs_mount *ump;
477 struct statfs *mstat;
478 uint64_t lbsize;
479 int error;
480
481 ump = MOUNTTOUNIONFSMOUNT(mp);
482
483 UNIONFSDEBUG("unionfs_statfs(mp = %p, lvp = %p, uvp = %p)\n",
484 mp, ump->um_lowervp, ump->um_uppervp);
485
486 mstat = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK | M_ZERO);
487
488 error = VFS_STATFS(ump->um_lowermp, mstat);
489 if (error) {
490 free(mstat, M_STATFS);
491 return (error);
492 }
493
494 /* now copy across the "interesting" information and fake the rest */
495 sbp->f_blocks = mstat->f_blocks;
496 sbp->f_files = mstat->f_files;
497
498 lbsize = mstat->f_bsize;
499
500 error = VFS_STATFS(ump->um_uppermp, mstat);
501 if (error) {
502 free(mstat, M_STATFS);
503 return (error);
504 }
505
506 /*
507 * The FS type etc is copy from upper vfs.
508 * (write able vfs have priority)
509 */
510 sbp->f_type = mstat->f_type;
511 sbp->f_flags = mstat->f_flags;
512 sbp->f_bsize = mstat->f_bsize;
513 sbp->f_iosize = mstat->f_iosize;
514
515 if (mstat->f_bsize != lbsize)
516 sbp->f_blocks = ((off_t)sbp->f_blocks * lbsize) /
517 mstat->f_bsize;
518
519 sbp->f_blocks += mstat->f_blocks;
520 sbp->f_bfree = mstat->f_bfree;
521 sbp->f_bavail = mstat->f_bavail;
522 sbp->f_files += mstat->f_files;
523 sbp->f_ffree = mstat->f_ffree;
524
525 free(mstat, M_STATFS);
526 return (0);
527 }
528
529 static int
unionfs_sync(struct mount * mp,int waitfor)530 unionfs_sync(struct mount *mp, int waitfor)
531 {
532 /* nothing to do */
533 return (0);
534 }
535
536 static int
unionfs_vget(struct mount * mp,ino_t ino,int flags,struct vnode ** vpp)537 unionfs_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
538 {
539 return (EOPNOTSUPP);
540 }
541
542 static int
unionfs_fhtovp(struct mount * mp,struct fid * fidp,int flags,struct vnode ** vpp)543 unionfs_fhtovp(struct mount *mp, struct fid *fidp, int flags,
544 struct vnode **vpp)
545 {
546 return (EOPNOTSUPP);
547 }
548
549 static int
unionfs_checkexp(struct mount * mp,struct sockaddr * nam,uint64_t * extflagsp,struct ucred ** credanonp,int * numsecflavors,int * secflavors)550 unionfs_checkexp(struct mount *mp, struct sockaddr *nam, uint64_t *extflagsp,
551 struct ucred **credanonp, int *numsecflavors, int *secflavors)
552 {
553 return (EOPNOTSUPP);
554 }
555
556 static int
unionfs_extattrctl(struct mount * mp,int cmd,struct vnode * filename_vp,int namespace,const char * attrname)557 unionfs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
558 int namespace, const char *attrname)
559 {
560 struct unionfs_mount *ump;
561 struct unionfs_node *unp;
562
563 ump = MOUNTTOUNIONFSMOUNT(mp);
564 unp = VTOUNIONFS(filename_vp);
565
566 if (unp->un_uppervp != NULLVP) {
567 return (VFS_EXTATTRCTL(ump->um_uppermp, cmd,
568 unp->un_uppervp, namespace, attrname));
569 } else {
570 return (VFS_EXTATTRCTL(ump->um_lowermp, cmd,
571 unp->un_lowervp, namespace, attrname));
572 }
573 }
574
575 static struct vfsops unionfs_vfsops = {
576 .vfs_checkexp = unionfs_checkexp,
577 .vfs_extattrctl = unionfs_extattrctl,
578 .vfs_fhtovp = unionfs_fhtovp,
579 .vfs_init = unionfs_init,
580 .vfs_mount = unionfs_domount,
581 .vfs_quotactl = unionfs_quotactl,
582 .vfs_root = unionfs_root,
583 .vfs_statfs = unionfs_statfs,
584 .vfs_sync = unionfs_sync,
585 .vfs_uninit = unionfs_uninit,
586 .vfs_unmount = unionfs_unmount,
587 .vfs_vget = unionfs_vget,
588 };
589
590 VFS_SET(unionfs_vfsops, unionfs, VFCF_LOOPBACK);
591