union_vfsops.c revision 1.56 1 /* $NetBSD: union_vfsops.c,v 1.56 2008/05/18 13:56:12 ad 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.56 2008/05/18 13:56:12 ad 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 /*
104 * Mount union filesystem
105 */
106 int
107 union_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
108 {
109 struct lwp *l = curlwp;
110 struct nameidata nd;
111 int error = 0;
112 struct union_args *args = data;
113 struct vnode *lowerrootvp = NULLVP;
114 struct vnode *upperrootvp = NULLVP;
115 struct union_mount *um = 0;
116 const char *cp;
117 char *xp;
118 int len;
119 size_t size;
120
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 printf("WARNING: the union file system is experimental and "
151 "may be unstable\n");
152
153 lowerrootvp = mp->mnt_vnodecovered;
154 VREF(lowerrootvp);
155
156 /*
157 * Find upper node.
158 */
159 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, args->target);
160
161 if ((error = namei(&nd)) != 0)
162 goto bad;
163
164 upperrootvp = nd.ni_vp;
165
166 if (upperrootvp->v_type != VDIR) {
167 error = EINVAL;
168 goto bad;
169 }
170
171 um = (struct union_mount *) malloc(sizeof(struct union_mount),
172 M_UFSMNT, M_WAITOK); /* XXX */
173
174 /*
175 * Keep a held reference to the target vnodes.
176 * They are vrele'd in union_unmount.
177 *
178 * Depending on the _BELOW flag, the filesystems are
179 * viewed in a different order. In effect, this is the
180 * same as providing a mount under option to the mount syscall.
181 */
182
183 um->um_op = args->mntflags & UNMNT_OPMASK;
184 switch (um->um_op) {
185 case UNMNT_ABOVE:
186 um->um_lowervp = lowerrootvp;
187 um->um_uppervp = upperrootvp;
188 break;
189
190 case UNMNT_BELOW:
191 um->um_lowervp = upperrootvp;
192 um->um_uppervp = lowerrootvp;
193 break;
194
195 case UNMNT_REPLACE:
196 vrele(lowerrootvp);
197 lowerrootvp = NULLVP;
198 um->um_uppervp = upperrootvp;
199 um->um_lowervp = lowerrootvp;
200 break;
201
202 default:
203 error = EINVAL;
204 goto bad;
205 }
206
207 /*
208 * Unless the mount is readonly, ensure that the top layer
209 * supports whiteout operations
210 */
211 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
212 error = VOP_WHITEOUT(um->um_uppervp, (struct componentname *) 0, LOOKUP);
213 if (error)
214 goto bad;
215 }
216
217 um->um_cred = l->l_cred;
218 kauth_cred_hold(um->um_cred);
219 um->um_cmode = UN_DIRMODE &~ l->l_proc->p_cwdi->cwdi_cmask;
220
221 /*
222 * Depending on what you think the MNT_LOCAL flag might mean,
223 * you may want the && to be || on the conditional below.
224 * At the moment it has been defined that the filesystem is
225 * only local if it is all local, ie the MNT_LOCAL flag implies
226 * that the entire namespace is local. If you think the MNT_LOCAL
227 * flag implies that some of the files might be stored locally
228 * then you will want to change the conditional.
229 */
230 if (um->um_op == UNMNT_ABOVE) {
231 if (((um->um_lowervp == NULLVP) ||
232 (um->um_lowervp->v_mount->mnt_flag & MNT_LOCAL)) &&
233 (um->um_uppervp->v_mount->mnt_flag & MNT_LOCAL))
234 mp->mnt_flag |= MNT_LOCAL;
235 }
236
237 /*
238 * Copy in the upper layer's RDONLY flag. This is for the benefit
239 * of lookup() which explicitly checks the flag, rather than asking
240 * the filesystem for it's own opinion. This means, that an update
241 * mount of the underlying filesystem to go from rdonly to rdwr
242 * will leave the unioned view as read-only.
243 */
244 mp->mnt_flag |= (um->um_uppervp->v_mount->mnt_flag & MNT_RDONLY);
245
246 mp->mnt_data = um;
247 vfs_getnewfsid(mp);
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 free(um, M_UFSMNT);
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 int
318 union_unmount(struct mount *mp, int mntflags)
319 {
320 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
321 int freeing;
322 int error;
323
324 #ifdef UNION_DIAGNOSTIC
325 printf("union_unmount(mp = %p)\n", mp);
326 #endif
327
328 /*
329 * Keep flushing vnodes from the mount list.
330 * This is needed because of the un_pvp held
331 * reference to the parent vnode.
332 * If more vnodes have been freed on a given pass,
333 * the try again. The loop will iterate at most
334 * (d) times, where (d) is the maximum tree depth
335 * in the filesystem.
336 */
337 for (freeing = 0; (error = vflush(mp, NULL, 0)) != 0;) {
338 struct vnode *vp;
339 int n;
340
341 /* count #vnodes held on mount list */
342 n = 0;
343 TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes)
344 n++;
345
346 /* if this is unchanged then stop */
347 if (n == freeing)
348 break;
349
350 /* otherwise try once more time */
351 freeing = n;
352 }
353
354 /*
355 * Ok, now that we've tried doing it gently, get out the hammer.
356 */
357
358 if (mntflags & MNT_FORCE)
359 error = vflush(mp, NULL, FORCECLOSE);
360
361 if (error)
362 return error;
363
364 /*
365 * Discard references to upper and lower target vnodes.
366 */
367 if (um->um_lowervp)
368 vrele(um->um_lowervp);
369 vrele(um->um_uppervp);
370 kauth_cred_free(um->um_cred);
371 /*
372 * Finally, throw away the union_mount structure
373 */
374 free(mp->mnt_data, M_UFSMNT); /* XXX */
375 mp->mnt_data = NULL;
376 return (0);
377 }
378
379 int
380 union_root(struct mount *mp, struct vnode **vpp)
381 {
382 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
383 int error;
384
385 /*
386 * Return locked reference to root.
387 */
388 VREF(um->um_uppervp);
389 vn_lock(um->um_uppervp, LK_EXCLUSIVE | LK_RETRY);
390 if (um->um_lowervp)
391 VREF(um->um_lowervp);
392 error = union_allocvp(vpp, mp, NULL, NULL, NULL,
393 um->um_uppervp, um->um_lowervp, 1);
394
395 if (error) {
396 vput(um->um_uppervp);
397 if (um->um_lowervp)
398 vrele(um->um_lowervp);
399 }
400
401 return (error);
402 }
403
404 int
405 union_statvfs(struct mount *mp, struct statvfs *sbp)
406 {
407 int error;
408 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
409 struct statvfs *sbuf = malloc(sizeof(*sbuf), M_TEMP, M_WAITOK | M_ZERO);
410 unsigned long lbsize;
411
412 #ifdef UNION_DIAGNOSTIC
413 printf("union_statvfs(mp = %p, lvp = %p, uvp = %p)\n", mp,
414 um->um_lowervp, um->um_uppervp);
415 #endif
416
417 if (um->um_lowervp) {
418 error = VFS_STATVFS(um->um_lowervp->v_mount, sbuf);
419 if (error)
420 goto done;
421 }
422
423 /* now copy across the "interesting" information and fake the rest */
424 lbsize = sbuf->f_bsize;
425 sbp->f_blocks = sbuf->f_blocks - sbuf->f_bfree;
426 sbp->f_files = sbuf->f_files - sbuf->f_ffree;
427
428 error = VFS_STATVFS(um->um_uppervp->v_mount, sbuf);
429 if (error)
430 goto done;
431
432 sbp->f_flag = sbuf->f_flag;
433 sbp->f_bsize = sbuf->f_bsize;
434 sbp->f_frsize = sbuf->f_frsize;
435 sbp->f_iosize = sbuf->f_iosize;
436
437 /*
438 * The "total" fields count total resources in all layers,
439 * the "free" fields count only those resources which are
440 * free in the upper layer (since only the upper layer
441 * is writable).
442 */
443
444 if (sbuf->f_bsize != lbsize)
445 sbp->f_blocks = sbp->f_blocks * lbsize / sbuf->f_bsize;
446 sbp->f_blocks += sbuf->f_blocks;
447 sbp->f_bfree = sbuf->f_bfree;
448 sbp->f_bavail = sbuf->f_bavail;
449 sbp->f_bresvd = sbuf->f_bresvd;
450 sbp->f_files += sbuf->f_files;
451 sbp->f_ffree = sbuf->f_ffree;
452 sbp->f_favail = sbuf->f_favail;
453 sbp->f_fresvd = sbuf->f_fresvd;
454
455 copy_statvfs_info(sbp, mp);
456 done:
457 free(sbuf, M_TEMP);
458 return error;
459 }
460
461 /*ARGSUSED*/
462 int
463 union_sync(struct mount *mp, int waitfor,
464 kauth_cred_t cred)
465 {
466
467 /*
468 * XXX - Assumes no data cached at union layer.
469 */
470 return (0);
471 }
472
473 /*ARGSUSED*/
474 int
475 union_vget(struct mount *mp, ino_t ino,
476 struct vnode **vpp)
477 {
478
479 return (EOPNOTSUPP);
480 }
481
482 static int
483 union_renamelock_enter(struct mount *mp)
484 {
485 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
486
487 /* Lock just the upper fs, where the action happens. */
488 return VFS_RENAMELOCK_ENTER(um->um_uppervp->v_mount);
489 }
490
491 static void
492 union_renamelock_exit(struct mount *mp)
493 {
494 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
495
496 VFS_RENAMELOCK_EXIT(um->um_uppervp->v_mount);
497 }
498
499 SYSCTL_SETUP(sysctl_vfs_union_setup, "sysctl vfs.union subtree setup")
500 {
501
502 sysctl_createv(clog, 0, NULL, NULL,
503 CTLFLAG_PERMANENT,
504 CTLTYPE_NODE, "vfs", NULL,
505 NULL, 0, NULL, 0,
506 CTL_VFS, CTL_EOL);
507 sysctl_createv(clog, 0, NULL, NULL,
508 CTLFLAG_PERMANENT,
509 CTLTYPE_NODE, "union",
510 SYSCTL_DESCR("Union file system"),
511 NULL, 0, NULL, 0,
512 CTL_VFS, 15, CTL_EOL);
513 /*
514 * XXX the "15" above could be dynamic, thereby eliminating
515 * one more instance of the "number to vfs" mapping problem,
516 * but "15" is the order as taken from sys/mount.h
517 */
518 }
519
520 extern const struct vnodeopv_desc union_vnodeop_opv_desc;
521
522 const struct vnodeopv_desc * const union_vnodeopv_descs[] = {
523 &union_vnodeop_opv_desc,
524 NULL,
525 };
526
527 struct vfsops union_vfsops = {
528 MOUNT_UNION,
529 sizeof (struct union_args),
530 union_mount,
531 union_start,
532 union_unmount,
533 union_root,
534 (void *)eopnotsupp, /* vfs_quotactl */
535 union_statvfs,
536 union_sync,
537 union_vget,
538 (void *)eopnotsupp, /* vfs_fhtovp */
539 (void *)eopnotsupp, /* vfs_vptofh */
540 union_init,
541 NULL, /* vfs_reinit */
542 union_done,
543 NULL, /* vfs_mountroot */
544 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
545 vfs_stdextattrctl,
546 (void *)eopnotsupp, /* vfs_suspendctl */
547 union_renamelock_enter,
548 union_renamelock_exit,
549 (void *)eopnotsupp,
550 union_vnodeopv_descs,
551 0, /* vfs_refcount */
552 { NULL, NULL },
553 };
554
555 static int
556 union_modcmd(modcmd_t cmd, void *arg)
557 {
558
559 switch (cmd) {
560 case MODULE_CMD_INIT:
561 return vfs_attach(&union_vfsops);
562 case MODULE_CMD_FINI:
563 return vfs_detach(&union_vfsops);
564 default:
565 return ENOTTY;
566 }
567 }
568