union_vfsops.c revision 1.78.14.2 1 /* $NetBSD: union_vfsops.c,v 1.78.14.2 2020/04/08 14:08:51 martin Exp $ */
2
3 /*
4 * Copyright (c) 1994 The Regents of the University of California.
5 * 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 * @(#)union_vfsops.c 8.20 (Berkeley) 5/20/95
35 */
36
37 /*
38 * Copyright (c) 1994 Jan-Simon Pendry.
39 * All rights reserved.
40 *
41 * This code is derived from software donated to Berkeley by
42 * Jan-Simon Pendry.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by the University of
55 * California, Berkeley and its contributors.
56 * 4. Neither the name of the University nor the names of its contributors
57 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * SUCH DAMAGE.
71 *
72 * @(#)union_vfsops.c 8.20 (Berkeley) 5/20/95
73 */
74
75 /*
76 * Union Layer
77 */
78
79 #include <sys/cdefs.h>
80 __KERNEL_RCSID(0, "$NetBSD: union_vfsops.c,v 1.78.14.2 2020/04/08 14:08:51 martin Exp $");
81
82 #include <sys/param.h>
83 #include <sys/systm.h>
84 #include <sys/sysctl.h>
85 #include <sys/time.h>
86 #include <sys/proc.h>
87 #include <sys/vnode.h>
88 #include <sys/mount.h>
89 #include <sys/namei.h>
90 #include <sys/malloc.h>
91 #include <sys/filedesc.h>
92 #include <sys/queue.h>
93 #include <sys/stat.h>
94 #include <sys/kauth.h>
95 #include <sys/module.h>
96
97 #include <miscfs/genfs/genfs.h>
98 #include <fs/union/union.h>
99
100 MODULE(MODULE_CLASS_VFS, union, NULL);
101
102 /*
103 * Mount union filesystem
104 */
105 int
106 union_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
107 {
108 struct lwp *l = curlwp;
109 int error = 0;
110 struct union_args *args = data;
111 struct vnode *lowerrootvp = NULLVP;
112 struct vnode *upperrootvp = NULLVP;
113 struct union_mount *um = 0;
114 const char *cp;
115 char *xp;
116 int len;
117 size_t size;
118
119 if (args == NULL)
120 return EINVAL;
121 if (*data_len < sizeof *args)
122 return EINVAL;
123
124 #ifdef UNION_DIAGNOSTIC
125 printf("union_mount(mp = %p)\n", mp);
126 #endif
127
128 if (mp->mnt_flag & MNT_GETARGS) {
129 um = MOUNTTOUNIONMOUNT(mp);
130 if (um == NULL)
131 return EIO;
132 args->target = NULL;
133 args->mntflags = um->um_op;
134 *data_len = sizeof *args;
135 return 0;
136 }
137 /*
138 * Update is a no-op
139 */
140 if (mp->mnt_flag & MNT_UPDATE) {
141 /*
142 * Need to provide.
143 * 1. a way to convert between rdonly and rdwr mounts.
144 * 2. support for nfs exports.
145 */
146 error = EOPNOTSUPP;
147 goto bad;
148 }
149
150 lowerrootvp = mp->mnt_vnodecovered;
151 vref(lowerrootvp);
152
153 /*
154 * Find upper node.
155 */
156 error = namei_simple_user(args->target,
157 NSM_FOLLOW_NOEMULROOT, &upperrootvp);
158 if (error != 0)
159 goto bad;
160
161 if (upperrootvp->v_type != VDIR) {
162 error = EINVAL;
163 goto bad;
164 }
165
166 um = kmem_zalloc(sizeof(struct union_mount), KM_SLEEP);
167
168 /*
169 * Keep a held reference to the target vnodes.
170 * They are vrele'd in union_unmount.
171 *
172 * Depending on the _BELOW flag, the filesystems are
173 * viewed in a different order. In effect, this is the
174 * same as providing a mount under option to the mount syscall.
175 */
176
177 um->um_op = args->mntflags & UNMNT_OPMASK;
178 switch (um->um_op) {
179 case UNMNT_ABOVE:
180 um->um_lowervp = lowerrootvp;
181 um->um_uppervp = upperrootvp;
182 break;
183
184 case UNMNT_BELOW:
185 um->um_lowervp = upperrootvp;
186 um->um_uppervp = lowerrootvp;
187 break;
188
189 case UNMNT_REPLACE:
190 vrele(lowerrootvp);
191 lowerrootvp = NULLVP;
192 um->um_uppervp = upperrootvp;
193 um->um_lowervp = lowerrootvp;
194 break;
195
196 default:
197 error = EINVAL;
198 goto bad;
199 }
200
201 mp->mnt_iflag |= IMNT_MPSAFE;
202
203 /*
204 * Unless the mount is readonly, ensure that the top layer
205 * supports whiteout operations
206 */
207 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
208 vn_lock(um->um_uppervp, LK_EXCLUSIVE | LK_RETRY);
209 error = VOP_WHITEOUT(um->um_uppervp,
210 (struct componentname *) 0, LOOKUP);
211 VOP_UNLOCK(um->um_uppervp);
212 if (error)
213 goto bad;
214 }
215
216 um->um_cred = l->l_cred;
217 kauth_cred_hold(um->um_cred);
218 um->um_cmode = UN_DIRMODE &~ l->l_proc->p_cwdi->cwdi_cmask;
219
220 /*
221 * Depending on what you think the MNT_LOCAL flag might mean,
222 * you may want the && to be || on the conditional below.
223 * At the moment it has been defined that the filesystem is
224 * only local if it is all local, ie the MNT_LOCAL flag implies
225 * that the entire namespace is local. If you think the MNT_LOCAL
226 * flag implies that some of the files might be stored locally
227 * then you will want to change the conditional.
228 */
229 if (um->um_op == UNMNT_ABOVE) {
230 if (((um->um_lowervp == NULLVP) ||
231 (um->um_lowervp->v_mount->mnt_flag & MNT_LOCAL)) &&
232 (um->um_uppervp->v_mount->mnt_flag & MNT_LOCAL))
233 mp->mnt_flag |= MNT_LOCAL;
234 }
235
236 /*
237 * Copy in the upper layer's RDONLY flag. This is for the benefit
238 * of lookup() which explicitly checks the flag, rather than asking
239 * the filesystem for its own opinion. This means, that an update
240 * mount of the underlying filesystem to go from rdonly to rdwr
241 * will leave the unioned view as read-only.
242 */
243 mp->mnt_flag |= (um->um_uppervp->v_mount->mnt_flag & MNT_RDONLY);
244
245 mp->mnt_data = um;
246 vfs_getnewfsid(mp);
247 mp->mnt_lower = um->um_uppervp->v_mount;
248
249 error = set_statvfs_info( path, UIO_USERSPACE, NULL, UIO_USERSPACE,
250 mp->mnt_op->vfs_name, mp, l);
251 if (error)
252 goto bad;
253
254 switch (um->um_op) {
255 case UNMNT_ABOVE:
256 cp = "<above>:";
257 break;
258 case UNMNT_BELOW:
259 cp = "<below>:";
260 break;
261 case UNMNT_REPLACE:
262 cp = "";
263 break;
264 default:
265 cp = "<invalid>:";
266 #ifdef DIAGNOSTIC
267 panic("union_mount: bad um_op");
268 #endif
269 break;
270 }
271 len = strlen(cp);
272 memcpy(mp->mnt_stat.f_mntfromname, cp, len);
273
274 xp = mp->mnt_stat.f_mntfromname + len;
275 len = MNAMELEN - len;
276
277 (void) copyinstr(args->target, xp, len - 1, &size);
278 memset(xp + size, 0, len - size);
279
280 #ifdef UNION_DIAGNOSTIC
281 printf("union_mount: from %s, on %s\n",
282 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
283 #endif
284
285 /* Setup the readdir hook if it's not set already */
286 if (!vn_union_readdir_hook)
287 vn_union_readdir_hook = union_readdirhook;
288
289 return (0);
290
291 bad:
292 if (um)
293 kmem_free(um, sizeof(struct union_mount));
294 if (upperrootvp)
295 vrele(upperrootvp);
296 if (lowerrootvp)
297 vrele(lowerrootvp);
298 return (error);
299 }
300
301 /*
302 * VFS start. Nothing needed here - the start routine
303 * on the underlying filesystem(s) will have been called
304 * when that filesystem was mounted.
305 */
306 /*ARGSUSED*/
307 int
308 union_start(struct mount *mp, int flags)
309 {
310
311 return (0);
312 }
313
314 /*
315 * Free reference to union layer
316 */
317 static bool
318 union_unmount_selector(void *cl, struct vnode *vp)
319 {
320 int *count = cl;
321
322 KASSERT(mutex_owned(vp->v_interlock));
323
324 *count += 1;
325 return false;
326 }
327
328 int
329 union_unmount(struct mount *mp, int mntflags)
330 {
331 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
332 int freeing;
333 int error;
334
335 #ifdef UNION_DIAGNOSTIC
336 printf("union_unmount(mp = %p)\n", mp);
337 #endif
338
339 /*
340 * Keep flushing vnodes from the mount list.
341 * This is needed because of the un_pvp held
342 * reference to the parent vnode.
343 * If more vnodes have been freed on a given pass,
344 * the try again. The loop will iterate at most
345 * (d) times, where (d) is the maximum tree depth
346 * in the filesystem.
347 */
348 for (freeing = 0; (error = vflush(mp, NULL, 0)) != 0;) {
349 struct vnode_iterator *marker;
350 int n;
351
352 /* count #vnodes held on mount list */
353 n = 0;
354 vfs_vnode_iterator_init(mp, &marker);
355 vfs_vnode_iterator_next(marker, union_unmount_selector, &n);
356 vfs_vnode_iterator_destroy(marker);
357
358 /* if this is unchanged then stop */
359 if (n == freeing)
360 break;
361
362 /* otherwise try once more time */
363 freeing = n;
364 }
365
366 /*
367 * Ok, now that we've tried doing it gently, get out the hammer.
368 */
369
370 if (mntflags & MNT_FORCE)
371 error = vflush(mp, NULL, FORCECLOSE);
372
373 if (error)
374 return error;
375
376 /*
377 * Discard references to upper and lower target vnodes.
378 */
379 if (um->um_lowervp)
380 vrele(um->um_lowervp);
381 vrele(um->um_uppervp);
382 kauth_cred_free(um->um_cred);
383 /*
384 * Finally, throw away the union_mount structure
385 */
386 kmem_free(um, sizeof(struct union_mount));
387 mp->mnt_data = NULL;
388 return 0;
389 }
390
391 int
392 union_root(struct mount *mp, int lktype, struct vnode **vpp)
393 {
394 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
395 int error;
396
397 /*
398 * Return locked reference to root.
399 */
400 vref(um->um_uppervp);
401 if (um->um_lowervp)
402 vref(um->um_lowervp);
403 error = union_allocvp(vpp, mp, NULL, NULL, NULL,
404 um->um_uppervp, um->um_lowervp, 1);
405
406 if (error) {
407 vrele(um->um_uppervp);
408 if (um->um_lowervp)
409 vrele(um->um_lowervp);
410 return error;
411 }
412
413 vn_lock(*vpp, lktype | LK_RETRY);
414
415 return 0;
416 }
417
418 int
419 union_statvfs(struct mount *mp, struct statvfs *sbp)
420 {
421 int error;
422 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
423 struct statvfs *sbuf = malloc(sizeof(*sbuf), M_TEMP, M_WAITOK | M_ZERO);
424 unsigned long lbsize;
425
426 #ifdef UNION_DIAGNOSTIC
427 printf("union_statvfs(mp = %p, lvp = %p, uvp = %p)\n", mp,
428 um->um_lowervp, um->um_uppervp);
429 #endif
430
431 if (um->um_lowervp) {
432 error = VFS_STATVFS(um->um_lowervp->v_mount, sbuf);
433 if (error)
434 goto done;
435 }
436
437 /* now copy across the "interesting" information and fake the rest */
438 lbsize = sbuf->f_bsize;
439 sbp->f_blocks = sbuf->f_blocks - sbuf->f_bfree;
440 sbp->f_files = sbuf->f_files - sbuf->f_ffree;
441
442 error = VFS_STATVFS(um->um_uppervp->v_mount, sbuf);
443 if (error)
444 goto done;
445
446 sbp->f_flag = sbuf->f_flag;
447 sbp->f_bsize = sbuf->f_bsize;
448 sbp->f_frsize = sbuf->f_frsize;
449 sbp->f_iosize = sbuf->f_iosize;
450
451 /*
452 * The "total" fields count total resources in all layers,
453 * the "free" fields count only those resources which are
454 * free in the upper layer (since only the upper layer
455 * is writable).
456 */
457
458 if (sbuf->f_bsize != lbsize)
459 sbp->f_blocks = sbp->f_blocks * lbsize / sbuf->f_bsize;
460 sbp->f_blocks += sbuf->f_blocks;
461 sbp->f_bfree = sbuf->f_bfree;
462 sbp->f_bavail = sbuf->f_bavail;
463 sbp->f_bresvd = sbuf->f_bresvd;
464 sbp->f_files += sbuf->f_files;
465 sbp->f_ffree = sbuf->f_ffree;
466 sbp->f_favail = sbuf->f_favail;
467 sbp->f_fresvd = sbuf->f_fresvd;
468
469 copy_statvfs_info(sbp, mp);
470 done:
471 free(sbuf, M_TEMP);
472 return error;
473 }
474
475 /*ARGSUSED*/
476 int
477 union_sync(struct mount *mp, int waitfor,
478 kauth_cred_t cred)
479 {
480
481 /*
482 * XXX - Assumes no data cached at union layer.
483 */
484 return (0);
485 }
486
487 /*ARGSUSED*/
488 int
489 union_vget(struct mount *mp, ino_t ino, int lktype,
490 struct vnode **vpp)
491 {
492
493 return (EOPNOTSUPP);
494 }
495
496 static int
497 union_renamelock_enter(struct mount *mp)
498 {
499 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
500
501 /* Lock just the upper fs, where the action happens. */
502 return VFS_RENAMELOCK_ENTER(um->um_uppervp->v_mount);
503 }
504
505 static void
506 union_renamelock_exit(struct mount *mp)
507 {
508 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
509
510 VFS_RENAMELOCK_EXIT(um->um_uppervp->v_mount);
511 }
512
513 extern const struct vnodeopv_desc union_vnodeop_opv_desc;
514
515 const struct vnodeopv_desc * const union_vnodeopv_descs[] = {
516 &union_vnodeop_opv_desc,
517 NULL,
518 };
519
520 struct vfsops union_vfsops = {
521 .vfs_name = MOUNT_UNION,
522 .vfs_min_mount_data = sizeof (struct union_args),
523 .vfs_mount = union_mount,
524 .vfs_start = union_start,
525 .vfs_unmount = union_unmount,
526 .vfs_root = union_root,
527 .vfs_quotactl = (void *)eopnotsupp,
528 .vfs_statvfs = union_statvfs,
529 .vfs_sync = union_sync,
530 .vfs_vget = union_vget,
531 .vfs_loadvnode = union_loadvnode,
532 .vfs_fhtovp = (void *)eopnotsupp,
533 .vfs_vptofh = (void *)eopnotsupp,
534 .vfs_init = union_init,
535 .vfs_reinit = union_reinit,
536 .vfs_done = union_done,
537 .vfs_snapshot = (void *)eopnotsupp,
538 .vfs_extattrctl = vfs_stdextattrctl,
539 .vfs_suspendctl = genfs_suspendctl,
540 .vfs_renamelock_enter = union_renamelock_enter,
541 .vfs_renamelock_exit = union_renamelock_exit,
542 .vfs_fsync = (void *)eopnotsupp,
543 .vfs_opv_descs = union_vnodeopv_descs
544 };
545
546 SYSCTL_SETUP(unionfs_sysctl_setup, "unionfs sysctl")
547 {
548
549 sysctl_createv(clog, 0, NULL, NULL,
550 CTLFLAG_PERMANENT,
551 CTLTYPE_NODE, "union",
552 SYSCTL_DESCR("Union file system"),
553 NULL, 0, NULL, 0,
554 CTL_VFS, 15, CTL_EOL);
555 /*
556 * XXX the "15" above could be dynamic, thereby eliminating
557 * one more instance of the "number to vfs" mapping problem,
558 * but "15" is the order as taken from sys/mount.h
559 */
560 }
561
562 static int
563 union_modcmd(modcmd_t cmd, void *arg)
564 {
565 int error;
566
567 switch (cmd) {
568 case MODULE_CMD_INIT:
569 error = vfs_attach(&union_vfsops);
570 if (error != 0)
571 break;
572 break;
573 case MODULE_CMD_FINI:
574 error = vfs_detach(&union_vfsops);
575 if (error != 0)
576 break;
577 break;
578 default:
579 error = ENOTTY;
580 break;
581 }
582
583 return (error);
584 }
585