union_vfsops.c revision 1.57.6.2.2.1 1 /* $NetBSD: union_vfsops.c,v 1.57.6.2.2.1 2014/04/28 16:05:36 sborrill 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.57.6.2.2.1 2014/04/28 16:05:36 sborrill 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 <fs/union/union.h>
98
99 MODULE(MODULE_CLASS_VFS, union, NULL);
100
101 VFS_PROTOS(union);
102
103 static struct sysctllog *union_sysctl_log;
104 static const char *warn_user =
105 "WARNING: the union file system is experimental\n"
106 "WARNING: it can cause crashes and file system corruption\n";
107
108 /*
109 * Mount union filesystem
110 */
111 int
112 union_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
113 {
114 struct lwp *l = curlwp;
115 struct nameidata nd;
116 int error = 0;
117 struct union_args *args = data;
118 struct vnode *lowerrootvp = NULLVP;
119 struct vnode *upperrootvp = NULLVP;
120 struct union_mount *um = 0;
121 const char *cp;
122 char *xp;
123 int len;
124 size_t size;
125
126 if (args == NULL)
127 return EINVAL;
128 if (*data_len < sizeof *args)
129 return EINVAL;
130
131 #ifdef UNION_DIAGNOSTIC
132 printf("union_mount(mp = %p)\n", mp);
133 #endif
134
135 if (mp->mnt_flag & MNT_GETARGS) {
136 um = MOUNTTOUNIONMOUNT(mp);
137 if (um == NULL)
138 return EIO;
139 args->target = NULL;
140 args->mntflags = um->um_op;
141 *data_len = sizeof *args;
142 return 0;
143 }
144 /*
145 * Update is a no-op
146 */
147 if (mp->mnt_flag & MNT_UPDATE) {
148 /*
149 * Need to provide.
150 * 1. a way to convert between rdonly and rdwr mounts.
151 * 2. support for nfs exports.
152 */
153 error = EOPNOTSUPP;
154 goto bad;
155 }
156
157 if (warn_user != NULL) {
158 printf("%s", warn_user);
159 warn_user = NULL;
160 }
161
162 lowerrootvp = mp->mnt_vnodecovered;
163 VREF(lowerrootvp);
164
165 /*
166 * Find upper node.
167 */
168 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, args->target);
169
170 if ((error = namei(&nd)) != 0)
171 goto bad;
172
173 upperrootvp = nd.ni_vp;
174
175 if (upperrootvp->v_type != VDIR) {
176 error = EINVAL;
177 goto bad;
178 }
179
180 um = (struct union_mount *) malloc(sizeof(struct union_mount),
181 M_UFSMNT, M_WAITOK); /* XXX */
182
183 /*
184 * Keep a held reference to the target vnodes.
185 * They are vrele'd in union_unmount.
186 *
187 * Depending on the _BELOW flag, the filesystems are
188 * viewed in a different order. In effect, this is the
189 * same as providing a mount under option to the mount syscall.
190 */
191
192 um->um_op = args->mntflags & UNMNT_OPMASK;
193 switch (um->um_op) {
194 case UNMNT_ABOVE:
195 um->um_lowervp = lowerrootvp;
196 um->um_uppervp = upperrootvp;
197 break;
198
199 case UNMNT_BELOW:
200 um->um_lowervp = upperrootvp;
201 um->um_uppervp = lowerrootvp;
202 break;
203
204 case UNMNT_REPLACE:
205 vrele(lowerrootvp);
206 lowerrootvp = NULLVP;
207 um->um_uppervp = upperrootvp;
208 um->um_lowervp = lowerrootvp;
209 break;
210
211 default:
212 error = EINVAL;
213 goto bad;
214 }
215
216 /*
217 * Unless the mount is readonly, ensure that the top layer
218 * supports whiteout operations
219 */
220 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
221 error = VOP_WHITEOUT(um->um_uppervp, (struct componentname *) 0, LOOKUP);
222 if (error)
223 goto bad;
224 }
225
226 um->um_cred = l->l_cred;
227 kauth_cred_hold(um->um_cred);
228 um->um_cmode = UN_DIRMODE &~ l->l_proc->p_cwdi->cwdi_cmask;
229
230 /*
231 * Depending on what you think the MNT_LOCAL flag might mean,
232 * you may want the && to be || on the conditional below.
233 * At the moment it has been defined that the filesystem is
234 * only local if it is all local, ie the MNT_LOCAL flag implies
235 * that the entire namespace is local. If you think the MNT_LOCAL
236 * flag implies that some of the files might be stored locally
237 * then you will want to change the conditional.
238 */
239 if (um->um_op == UNMNT_ABOVE) {
240 if (((um->um_lowervp == NULLVP) ||
241 (um->um_lowervp->v_mount->mnt_flag & MNT_LOCAL)) &&
242 (um->um_uppervp->v_mount->mnt_flag & MNT_LOCAL))
243 mp->mnt_flag |= MNT_LOCAL;
244 }
245
246 /*
247 * Copy in the upper layer's RDONLY flag. This is for the benefit
248 * of lookup() which explicitly checks the flag, rather than asking
249 * the filesystem for it's own opinion. This means, that an update
250 * mount of the underlying filesystem to go from rdonly to rdwr
251 * will leave the unioned view as read-only.
252 */
253 mp->mnt_flag |= (um->um_uppervp->v_mount->mnt_flag & MNT_RDONLY);
254
255 mp->mnt_data = um;
256 vfs_getnewfsid(mp);
257
258 error = set_statvfs_info( path, UIO_USERSPACE, NULL, UIO_USERSPACE,
259 mp->mnt_op->vfs_name, mp, l);
260 if (error)
261 goto bad;
262
263 switch (um->um_op) {
264 case UNMNT_ABOVE:
265 cp = "<above>:";
266 break;
267 case UNMNT_BELOW:
268 cp = "<below>:";
269 break;
270 case UNMNT_REPLACE:
271 cp = "";
272 break;
273 default:
274 cp = "<invalid>:";
275 #ifdef DIAGNOSTIC
276 panic("union_mount: bad um_op");
277 #endif
278 break;
279 }
280 len = strlen(cp);
281 memcpy(mp->mnt_stat.f_mntfromname, cp, len);
282
283 xp = mp->mnt_stat.f_mntfromname + len;
284 len = MNAMELEN - len;
285
286 (void) copyinstr(args->target, xp, len - 1, &size);
287 memset(xp + size, 0, len - size);
288
289 #ifdef UNION_DIAGNOSTIC
290 printf("union_mount: from %s, on %s\n",
291 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
292 #endif
293
294 /* Setup the readdir hook if it's not set already */
295 if (!vn_union_readdir_hook)
296 vn_union_readdir_hook = union_readdirhook;
297
298 return (0);
299
300 bad:
301 if (um)
302 free(um, M_UFSMNT);
303 if (upperrootvp)
304 vrele(upperrootvp);
305 if (lowerrootvp)
306 vrele(lowerrootvp);
307 return (error);
308 }
309
310 /*
311 * VFS start. Nothing needed here - the start routine
312 * on the underlying filesystem(s) will have been called
313 * when that filesystem was mounted.
314 */
315 /*ARGSUSED*/
316 int
317 union_start(struct mount *mp, int flags)
318 {
319
320 return (0);
321 }
322
323 /*
324 * Free reference to union layer
325 */
326 int
327 union_unmount(struct mount *mp, int mntflags)
328 {
329 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
330 int freeing;
331 int error;
332
333 #ifdef UNION_DIAGNOSTIC
334 printf("union_unmount(mp = %p)\n", mp);
335 #endif
336
337 /*
338 * Keep flushing vnodes from the mount list.
339 * This is needed because of the un_pvp held
340 * reference to the parent vnode.
341 * If more vnodes have been freed on a given pass,
342 * the try again. The loop will iterate at most
343 * (d) times, where (d) is the maximum tree depth
344 * in the filesystem.
345 */
346 for (freeing = 0; (error = vflush(mp, NULL, 0)) != 0;) {
347 struct vnode *vp;
348 int n;
349
350 /* count #vnodes held on mount list */
351 n = 0;
352 TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes)
353 n++;
354
355 /* if this is unchanged then stop */
356 if (n == freeing)
357 break;
358
359 /* otherwise try once more time */
360 freeing = n;
361 }
362
363 /*
364 * Ok, now that we've tried doing it gently, get out the hammer.
365 */
366
367 if (mntflags & MNT_FORCE)
368 error = vflush(mp, NULL, FORCECLOSE);
369
370 if (error)
371 return error;
372
373 /*
374 * Discard references to upper and lower target vnodes.
375 */
376 if (um->um_lowervp)
377 vrele(um->um_lowervp);
378 vrele(um->um_uppervp);
379 kauth_cred_free(um->um_cred);
380 /*
381 * Finally, throw away the union_mount structure
382 */
383 free(mp->mnt_data, M_UFSMNT); /* XXX */
384 mp->mnt_data = NULL;
385 return (0);
386 }
387
388 int
389 union_root(struct mount *mp, struct vnode **vpp)
390 {
391 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
392 int error;
393
394 /*
395 * Return locked reference to root.
396 */
397 VREF(um->um_uppervp);
398 vn_lock(um->um_uppervp, LK_EXCLUSIVE | LK_RETRY);
399 if (um->um_lowervp)
400 VREF(um->um_lowervp);
401 error = union_allocvp(vpp, mp, NULL, NULL, NULL,
402 um->um_uppervp, um->um_lowervp, 1);
403
404 if (error) {
405 vput(um->um_uppervp);
406 if (um->um_lowervp)
407 vrele(um->um_lowervp);
408 }
409
410 return (error);
411 }
412
413 int
414 union_statvfs(struct mount *mp, struct statvfs *sbp)
415 {
416 int error;
417 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
418 struct statvfs *sbuf = malloc(sizeof(*sbuf), M_TEMP, M_WAITOK | M_ZERO);
419 unsigned long lbsize;
420
421 #ifdef UNION_DIAGNOSTIC
422 printf("union_statvfs(mp = %p, lvp = %p, uvp = %p)\n", mp,
423 um->um_lowervp, um->um_uppervp);
424 #endif
425
426 if (um->um_lowervp) {
427 error = VFS_STATVFS(um->um_lowervp->v_mount, sbuf);
428 if (error)
429 goto done;
430 }
431
432 /* now copy across the "interesting" information and fake the rest */
433 lbsize = sbuf->f_bsize;
434 sbp->f_blocks = sbuf->f_blocks - sbuf->f_bfree;
435 sbp->f_files = sbuf->f_files - sbuf->f_ffree;
436
437 error = VFS_STATVFS(um->um_uppervp->v_mount, sbuf);
438 if (error)
439 goto done;
440
441 sbp->f_flag = sbuf->f_flag;
442 sbp->f_bsize = sbuf->f_bsize;
443 sbp->f_frsize = sbuf->f_frsize;
444 sbp->f_iosize = sbuf->f_iosize;
445
446 /*
447 * The "total" fields count total resources in all layers,
448 * the "free" fields count only those resources which are
449 * free in the upper layer (since only the upper layer
450 * is writable).
451 */
452
453 if (sbuf->f_bsize != lbsize)
454 sbp->f_blocks = sbp->f_blocks * lbsize / sbuf->f_bsize;
455 sbp->f_blocks += sbuf->f_blocks;
456 sbp->f_bfree = sbuf->f_bfree;
457 sbp->f_bavail = sbuf->f_bavail;
458 sbp->f_bresvd = sbuf->f_bresvd;
459 sbp->f_files += sbuf->f_files;
460 sbp->f_ffree = sbuf->f_ffree;
461 sbp->f_favail = sbuf->f_favail;
462 sbp->f_fresvd = sbuf->f_fresvd;
463
464 copy_statvfs_info(sbp, mp);
465 done:
466 free(sbuf, M_TEMP);
467 return error;
468 }
469
470 /*ARGSUSED*/
471 int
472 union_sync(struct mount *mp, int waitfor,
473 kauth_cred_t cred)
474 {
475
476 /*
477 * XXX - Assumes no data cached at union layer.
478 */
479 return (0);
480 }
481
482 /*ARGSUSED*/
483 int
484 union_vget(struct mount *mp, ino_t ino,
485 struct vnode **vpp)
486 {
487
488 return (EOPNOTSUPP);
489 }
490
491 static int
492 union_renamelock_enter(struct mount *mp)
493 {
494 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
495
496 /* Lock just the upper fs, where the action happens. */
497 return VFS_RENAMELOCK_ENTER(um->um_uppervp->v_mount);
498 }
499
500 static void
501 union_renamelock_exit(struct mount *mp)
502 {
503 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
504
505 VFS_RENAMELOCK_EXIT(um->um_uppervp->v_mount);
506 }
507
508 extern const struct vnodeopv_desc union_vnodeop_opv_desc;
509
510 const struct vnodeopv_desc * const union_vnodeopv_descs[] = {
511 &union_vnodeop_opv_desc,
512 NULL,
513 };
514
515 struct vfsops union_vfsops = {
516 MOUNT_UNION,
517 sizeof (struct union_args),
518 union_mount,
519 union_start,
520 union_unmount,
521 union_root,
522 (void *)eopnotsupp, /* vfs_quotactl */
523 union_statvfs,
524 union_sync,
525 union_vget,
526 (void *)eopnotsupp, /* vfs_fhtovp */
527 (void *)eopnotsupp, /* vfs_vptofh */
528 union_init,
529 NULL, /* vfs_reinit */
530 union_done,
531 NULL, /* vfs_mountroot */
532 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
533 vfs_stdextattrctl,
534 (void *)eopnotsupp, /* vfs_suspendctl */
535 union_renamelock_enter,
536 union_renamelock_exit,
537 (void *)eopnotsupp,
538 union_vnodeopv_descs,
539 0, /* vfs_refcount */
540 { NULL, NULL },
541 };
542
543 static int
544 union_modcmd(modcmd_t cmd, void *arg)
545 {
546 int error;
547
548 switch (cmd) {
549 case MODULE_CMD_INIT:
550 error = vfs_attach(&union_vfsops);
551 if (error != 0)
552 break;
553 sysctl_createv(&union_sysctl_log, 0, NULL, NULL,
554 CTLFLAG_PERMANENT,
555 CTLTYPE_NODE, "vfs", NULL,
556 NULL, 0, NULL, 0,
557 CTL_VFS, CTL_EOL);
558 sysctl_createv(&union_sysctl_log, 0, NULL, NULL,
559 CTLFLAG_PERMANENT,
560 CTLTYPE_NODE, "union",
561 SYSCTL_DESCR("Union file system"),
562 NULL, 0, NULL, 0,
563 CTL_VFS, 15, CTL_EOL);
564 /*
565 * XXX the "15" above could be dynamic, thereby eliminating
566 * one more instance of the "number to vfs" mapping problem,
567 * but "15" is the order as taken from sys/mount.h
568 */
569 break;
570 case MODULE_CMD_FINI:
571 error = vfs_detach(&union_vfsops);
572 if (error != 0)
573 break;
574 sysctl_teardown(&union_sysctl_log);
575 break;
576 default:
577 error = ENOTTY;
578 break;
579 }
580
581 return (error);
582 }
583