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