union_vfsops.c revision 1.29 1 /* $NetBSD: union_vfsops.c,v 1.29 2005/08/30 19:11:43 xtraeme 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.29 2005/08/30 19:11:43 xtraeme 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(struct mount *, const char *, void *, struct nameidata *,
98 struct proc *);
99 int union_start(struct mount *, int, struct proc *);
100 int union_unmount(struct mount *, int, struct proc *);
101 int union_root(struct mount *, struct vnode **);
102 int union_quotactl(struct mount *, int, uid_t, void *, struct proc *);
103 int union_statvfs(struct mount *, struct statvfs *, struct proc *);
104 int union_sync(struct mount *, int, struct ucred *, struct proc *);
105 int union_vget(struct mount *, ino_t, struct vnode **);
106 int union_fhtovp(struct mount *, struct fid *, struct vnode **);
107 int union_checkexp(struct mount *, struct mbuf *, int *,
108 struct ucred **);
109 int union_vptofh(struct vnode *, struct fid *);
110
111 /*
112 * Mount union filesystem
113 */
114 int
115 union_mount(mp, path, data, ndp, p)
116 struct mount *mp;
117 const char *path;
118 void *data;
119 struct nameidata *ndp;
120 struct proc *p;
121 {
122 int error = 0;
123 struct union_args args;
124 struct vnode *lowerrootvp = NULLVP;
125 struct vnode *upperrootvp = NULLVP;
126 struct union_mount *um = 0;
127 struct ucred *cred = 0;
128 const char *cp;
129 char *xp;
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, p);
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 = p->p_ucred;
231 crhold(um->um_cred);
232 um->um_cmode = UN_DIRMODE &~ p->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, p);
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 xp = mp->mnt_stat.f_mntfromname + len;
289 len = MNAMELEN - len;
290
291 (void) copyinstr(args.target, xp, len - 1, &size);
292 memset(xp + 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, p)
325 struct mount *mp;
326 int flags;
327 struct proc *p;
328 {
329
330 return (0);
331 }
332
333 /*
334 * Free reference to union layer
335 */
336 int
337 union_unmount(mp, mntflags, p)
338 struct mount *mp;
339 int mntflags;
340 struct proc *p;
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)) != 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)
423 struct mount *mp;
424 struct vnode **vpp;
425 {
426 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
427 int error;
428 int loselock;
429
430 /*
431 * Return locked reference to root.
432 */
433 VREF(um->um_uppervp);
434 if ((um->um_op == UNMNT_BELOW) &&
435 VOP_ISLOCKED(um->um_uppervp)) {
436 loselock = 1;
437 } else {
438 vn_lock(um->um_uppervp, LK_EXCLUSIVE | LK_RETRY);
439 loselock = 0;
440 }
441 if (um->um_lowervp)
442 VREF(um->um_lowervp);
443 error = union_allocvp(vpp, mp,
444 (struct vnode *) 0,
445 (struct vnode *) 0,
446 (struct componentname *) 0,
447 um->um_uppervp,
448 um->um_lowervp,
449 1);
450
451 if (error) {
452 if (!loselock)
453 VOP_UNLOCK(um->um_uppervp, 0);
454 vrele(um->um_uppervp);
455 if (um->um_lowervp)
456 vrele(um->um_lowervp);
457 } else {
458 if (loselock)
459 VTOUNION(*vpp)->un_flags &= ~UN_ULOCK;
460 }
461
462 return (error);
463 }
464
465 /*ARGSUSED*/
466 int
467 union_quotactl(mp, cmd, uid, arg, p)
468 struct mount *mp;
469 int cmd;
470 uid_t uid;
471 void *arg;
472 struct proc *p;
473 {
474
475 return (EOPNOTSUPP);
476 }
477
478 int
479 union_statvfs(mp, sbp, p)
480 struct mount *mp;
481 struct statvfs *sbp;
482 struct proc *p;
483 {
484 int error;
485 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
486 struct statvfs *sbuf = malloc(sizeof(*sbuf), M_TEMP, M_WAITOK | M_ZERO);
487 unsigned long lbsize;
488
489 #ifdef UNION_DIAGNOSTIC
490 printf("union_statvfs(mp = %p, lvp = %p, uvp = %p)\n", mp,
491 um->um_lowervp, um->um_uppervp);
492 #endif
493
494 if (um->um_lowervp) {
495 error = VFS_STATVFS(um->um_lowervp->v_mount, sbuf, p);
496 if (error)
497 goto done;
498 }
499
500 /* now copy across the "interesting" information and fake the rest */
501 lbsize = sbuf->f_bsize;
502 sbp->f_blocks = sbuf->f_blocks - sbuf->f_bfree;
503 sbp->f_files = sbuf->f_files - sbuf->f_ffree;
504
505 error = VFS_STATVFS(um->um_uppervp->v_mount, sbuf, p);
506 if (error)
507 goto done;
508
509 sbp->f_flag = sbuf->f_flag;
510 sbp->f_bsize = sbuf->f_bsize;
511 sbp->f_frsize = sbuf->f_frsize;
512 sbp->f_iosize = sbuf->f_iosize;
513
514 /*
515 * The "total" fields count total resources in all layers,
516 * the "free" fields count only those resources which are
517 * free in the upper layer (since only the upper layer
518 * is writable).
519 */
520
521 if (sbuf->f_bsize != lbsize)
522 sbp->f_blocks = sbp->f_blocks * lbsize / sbuf->f_bsize;
523 sbp->f_blocks += sbuf->f_blocks;
524 sbp->f_bfree = sbuf->f_bfree;
525 sbp->f_bavail = sbuf->f_bavail;
526 sbp->f_bresvd = sbuf->f_bresvd;
527 sbp->f_files += sbuf->f_files;
528 sbp->f_ffree = sbuf->f_ffree;
529 sbp->f_favail = sbuf->f_favail;
530 sbp->f_fresvd = sbuf->f_fresvd;
531
532 copy_statvfs_info(sbp, mp);
533 done:
534 free(sbuf, M_TEMP);
535 return error;
536 }
537
538 /*ARGSUSED*/
539 int
540 union_sync(mp, waitfor, cred, p)
541 struct mount *mp;
542 int waitfor;
543 struct ucred *cred;
544 struct proc *p;
545 {
546
547 /*
548 * XXX - Assumes no data cached at union layer.
549 */
550 return (0);
551 }
552
553 /*ARGSUSED*/
554 int
555 union_vget(mp, ino, vpp)
556 struct mount *mp;
557 ino_t ino;
558 struct vnode **vpp;
559 {
560
561 return (EOPNOTSUPP);
562 }
563
564 /*ARGSUSED*/
565 int
566 union_fhtovp(mp, fidp, vpp)
567 struct mount *mp;
568 struct fid *fidp;
569 struct vnode **vpp;
570 {
571
572 return (EOPNOTSUPP);
573 }
574
575 /*ARGSUSED*/
576 int
577 union_checkexp(mp, nam, exflagsp, credanonp)
578 struct mount *mp;
579 struct mbuf *nam;
580 int *exflagsp;
581 struct ucred **credanonp;
582 {
583
584 return (EOPNOTSUPP);
585 }
586
587 /*ARGSUSED*/
588 int
589 union_vptofh(vp, fhp)
590 struct vnode *vp;
591 struct fid *fhp;
592 {
593
594 return (EOPNOTSUPP);
595 }
596
597 SYSCTL_SETUP(sysctl_vfs_union_setup, "sysctl vfs.union subtree setup")
598 {
599
600 sysctl_createv(clog, 0, NULL, NULL,
601 CTLFLAG_PERMANENT,
602 CTLTYPE_NODE, "vfs", NULL,
603 NULL, 0, NULL, 0,
604 CTL_VFS, CTL_EOL);
605 sysctl_createv(clog, 0, NULL, NULL,
606 CTLFLAG_PERMANENT,
607 CTLTYPE_NODE, "union",
608 SYSCTL_DESCR("Union file system"),
609 NULL, 0, NULL, 0,
610 CTL_VFS, 15, CTL_EOL);
611 /*
612 * XXX the "15" above could be dynamic, thereby eliminating
613 * one more instance of the "number to vfs" mapping problem,
614 * but "15" is the order as taken from sys/mount.h
615 */
616 }
617
618 extern const struct vnodeopv_desc union_vnodeop_opv_desc;
619
620 const struct vnodeopv_desc * const union_vnodeopv_descs[] = {
621 &union_vnodeop_opv_desc,
622 NULL,
623 };
624
625 struct vfsops union_vfsops = {
626 MOUNT_UNION,
627 union_mount,
628 union_start,
629 union_unmount,
630 union_root,
631 union_quotactl,
632 union_statvfs,
633 union_sync,
634 union_vget,
635 union_fhtovp,
636 union_vptofh,
637 union_init,
638 NULL, /* vfs_reinit */
639 union_done,
640 NULL,
641 NULL, /* vfs_mountroot */
642 union_checkexp,
643 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
644 vfs_stdextattrctl,
645 union_vnodeopv_descs,
646 };
647 VFS_ATTACH(union_vfsops);
648