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