union_vfsops.c revision 1.21 1 /* $NetBSD: union_vfsops.c,v 1.21 2004/05/30 09:48:37 tron 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.21 2004/05/30 09:48:37 tron 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 #include <miscfs/genfs/layer_extern.h>
97
98 int union_mount __P((struct mount *, const char *, void *, struct nameidata *,
99 struct proc *));
100 int union_start __P((struct mount *, int, struct proc *));
101 int union_unmount __P((struct mount *, int, struct proc *));
102 int union_root __P((struct mount *, struct vnode **));
103 int union_quotactl __P((struct mount *, int, uid_t, void *, struct proc *));
104 int union_statvfs __P((struct mount *, struct statvfs *, struct proc *));
105 int union_sync __P((struct mount *, int, struct ucred *, struct proc *));
106 int union_vget __P((struct mount *, ino_t, struct vnode **));
107 int union_fhtovp __P((struct mount *, struct fid *, struct vnode **));
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, p)
117 struct mount *mp;
118 const char *path;
119 void *data;
120 struct nameidata *ndp;
121 struct proc *p;
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, 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 vfs_getnewfsid(mp);
261
262 error = set_statvfs_info( path, UIO_USERSPACE, NULL, UIO_USERSPACE,
263 mp, p);
264 if (error)
265 goto bad;
266
267 switch (um->um_op) {
268 case UNMNT_ABOVE:
269 cp = "<above>:";
270 break;
271 case UNMNT_BELOW:
272 cp = "<below>:";
273 break;
274 case UNMNT_REPLACE:
275 cp = "";
276 break;
277 default:
278 cp = "<invalid>:";
279 #ifdef DIAGNOSTIC
280 panic("union_mount: bad um_op");
281 #endif
282 break;
283 }
284 len = strlen(cp);
285 memcpy(mp->mnt_stat.f_mntfromname, cp, len);
286
287 cp = mp->mnt_stat.f_mntfromname + len;
288 len = MNAMELEN - len;
289
290 (void) copyinstr(args.target, cp, len - 1, &size);
291 memset(cp + size, 0, len - size);
292
293 #ifdef UNION_DIAGNOSTIC
294 printf("union_mount: from %s, on %s\n",
295 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
296 #endif
297
298 /* Setup the readdir hook if it's not set already */
299 if (!vn_union_readdir_hook)
300 vn_union_readdir_hook = union_readdirhook;
301
302 return (0);
303
304 bad:
305 if (um)
306 free(um, M_UFSMNT);
307 if (cred)
308 crfree(cred);
309 if (upperrootvp)
310 vrele(upperrootvp);
311 if (lowerrootvp)
312 vrele(lowerrootvp);
313 return (error);
314 }
315
316 /*
317 * VFS start. Nothing needed here - the start routine
318 * on the underlying filesystem(s) will have been called
319 * when that filesystem was mounted.
320 */
321 /*ARGSUSED*/
322 int
323 union_start(mp, flags, p)
324 struct mount *mp;
325 int flags;
326 struct proc *p;
327 {
328
329 return (0);
330 }
331
332 /*
333 * Free reference to union layer
334 */
335 int
336 union_unmount(mp, mntflags, p)
337 struct mount *mp;
338 int mntflags;
339 struct proc *p;
340 {
341 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
342 struct vnode *um_rootvp;
343 int error;
344 int freeing;
345
346 #ifdef UNION_DIAGNOSTIC
347 printf("union_unmount(mp = %p)\n", mp);
348 #endif
349
350 if ((error = union_root(mp, &um_rootvp)) != 0)
351 return (error);
352
353 /*
354 * Keep flushing vnodes from the mount list.
355 * This is needed because of the un_pvp held
356 * reference to the parent vnode.
357 * If more vnodes have been freed on a given pass,
358 * the try again. The loop will iterate at most
359 * (d) times, where (d) is the maximum tree depth
360 * in the filesystem.
361 */
362 for (freeing = 0; vflush(mp, um_rootvp, 0) != 0;) {
363 struct vnode *vp;
364 int n;
365
366 /* count #vnodes held on mount list */
367 for (n = 0, vp = mp->mnt_vnodelist.lh_first;
368 vp != NULLVP;
369 vp = vp->v_mntvnodes.le_next)
370 n++;
371
372 /* if this is unchanged then stop */
373 if (n == freeing)
374 break;
375
376 /* otherwise try once more time */
377 freeing = n;
378 }
379
380 /*
381 * Ok, now that we've tried doing it gently, get out the hammer.
382 */
383
384 if (mntflags & MNT_FORCE)
385 vflush(mp, um_rootvp, FORCECLOSE);
386
387
388 /* At this point the root vnode should have a single reference */
389 if (um_rootvp->v_usecount > 1) {
390 vput(um_rootvp);
391 return (EBUSY);
392 }
393
394 #ifdef UNION_DIAGNOSTIC
395 vprint("union root", um_rootvp);
396 #endif
397 /*
398 * Discard references to upper and lower target vnodes.
399 */
400 if (um->um_lowervp)
401 vrele(um->um_lowervp);
402 vrele(um->um_uppervp);
403 crfree(um->um_cred);
404 /*
405 * Release reference on underlying root vnode
406 */
407 vput(um_rootvp);
408 /*
409 * And blow it away for future re-use
410 */
411 vgone(um_rootvp);
412 /*
413 * Finally, throw away the union_mount structure
414 */
415 free(mp->mnt_data, M_UFSMNT); /* XXX */
416 mp->mnt_data = 0;
417 return (0);
418 }
419
420 int
421 union_root(mp, vpp)
422 struct mount *mp;
423 struct vnode **vpp;
424 {
425 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
426 int error;
427 int loselock;
428
429 /*
430 * Return locked reference to root.
431 */
432 VREF(um->um_uppervp);
433 if ((um->um_op == UNMNT_BELOW) &&
434 VOP_ISLOCKED(um->um_uppervp)) {
435 loselock = 1;
436 } else {
437 vn_lock(um->um_uppervp, LK_EXCLUSIVE | LK_RETRY);
438 loselock = 0;
439 }
440 if (um->um_lowervp)
441 VREF(um->um_lowervp);
442 error = union_allocvp(vpp, mp,
443 (struct vnode *) 0,
444 (struct vnode *) 0,
445 (struct componentname *) 0,
446 um->um_uppervp,
447 um->um_lowervp,
448 1);
449
450 if (error) {
451 if (!loselock)
452 VOP_UNLOCK(um->um_uppervp, 0);
453 vrele(um->um_uppervp);
454 if (um->um_lowervp)
455 vrele(um->um_lowervp);
456 } else {
457 if (loselock)
458 VTOUNION(*vpp)->un_flags &= ~UN_ULOCK;
459 }
460
461 return (error);
462 }
463
464 /*ARGSUSED*/
465 int
466 union_quotactl(mp, cmd, uid, arg, p)
467 struct mount *mp;
468 int cmd;
469 uid_t uid;
470 void *arg;
471 struct proc *p;
472 {
473
474 return (EOPNOTSUPP);
475 }
476
477 int
478 union_statvfs(mp, sbp, p)
479 struct mount *mp;
480 struct statvfs *sbp;
481 struct proc *p;
482 {
483 int error;
484 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
485 struct statvfs *sbuf = malloc(sizeof(*sbuf), M_TEMP, M_WAITOK);
486 unsigned long lbsize;
487
488 #ifdef UNION_DIAGNOSTIC
489 printf("union_statvfs(mp = %p, lvp = %p, uvp = %p)\n", mp,
490 um->um_lowervp, um->um_uppervp);
491 #endif
492
493 if (um->um_lowervp) {
494 error = VFS_STATVFS(um->um_lowervp->v_mount, sbuf, p);
495 if (error)
496 goto done;
497 }
498
499 /* now copy across the "interesting" information and fake the rest */
500 lbsize = sbuf->f_bsize;
501 sbp->f_blocks = sbuf->f_blocks - sbuf->f_bfree;
502 sbp->f_files = sbuf->f_files - sbuf->f_ffree;
503
504 error = VFS_STATVFS(um->um_uppervp->v_mount, sbuf, p);
505 if (error)
506 goto done;
507
508 sbp->f_flag = sbuf->f_flag;
509 sbp->f_bsize = sbuf->f_bsize;
510 sbp->f_frsize = sbuf->f_frsize;
511 sbp->f_iosize = sbuf->f_iosize;
512
513 /*
514 * The "total" fields count total resources in all layers,
515 * the "free" fields count only those resources which are
516 * free in the upper layer (since only the upper layer
517 * is writable).
518 */
519
520 if (sbuf->f_bsize != lbsize)
521 sbp->f_blocks = sbp->f_blocks * lbsize / sbuf->f_bsize;
522 sbp->f_blocks += sbuf->f_blocks;
523 sbp->f_bfree = sbuf->f_bfree;
524 sbp->f_bavail = sbuf->f_bavail;
525 sbp->f_bresvd = sbuf->f_bresvd;
526 sbp->f_files += sbuf->f_files;
527 sbp->f_ffree = sbuf->f_ffree;
528 sbp->f_favail = sbuf->f_favail;
529 sbp->f_fresvd = sbuf->f_fresvd;
530
531 copy_statvfs_info(sbp, mp);
532 done:
533 free(sbuf, M_TEMP);
534 return error;
535 }
536
537 /*ARGSUSED*/
538 int
539 union_sync(mp, waitfor, cred, p)
540 struct mount *mp;
541 int waitfor;
542 struct ucred *cred;
543 struct proc *p;
544 {
545
546 /*
547 * XXX - Assumes no data cached at union layer.
548 */
549 return (0);
550 }
551
552 /*ARGSUSED*/
553 int
554 union_vget(mp, ino, vpp)
555 struct mount *mp;
556 ino_t ino;
557 struct vnode **vpp;
558 {
559
560 return (EOPNOTSUPP);
561 }
562
563 /*ARGSUSED*/
564 int
565 union_fhtovp(mp, fidp, vpp)
566 struct mount *mp;
567 struct fid *fidp;
568 struct vnode **vpp;
569 {
570
571 return (EOPNOTSUPP);
572 }
573
574 /*ARGSUSED*/
575 int
576 union_checkexp(mp, nam, exflagsp, credanonp)
577 struct mount *mp;
578 struct mbuf *nam;
579 int *exflagsp;
580 struct ucred **credanonp;
581 {
582
583 return (EOPNOTSUPP);
584 }
585
586 /*ARGSUSED*/
587 int
588 union_vptofh(vp, fhp)
589 struct vnode *vp;
590 struct fid *fhp;
591 {
592
593 return (EOPNOTSUPP);
594 }
595
596 SYSCTL_SETUP(sysctl_vfs_union_setup, "sysctl vfs.union subtree setup")
597 {
598
599 sysctl_createv(clog, 0, NULL, NULL,
600 CTLFLAG_PERMANENT,
601 CTLTYPE_NODE, "vfs", NULL,
602 NULL, 0, NULL, 0,
603 CTL_VFS, CTL_EOL);
604 sysctl_createv(clog, 0, NULL, NULL,
605 CTLFLAG_PERMANENT,
606 CTLTYPE_NODE, "union",
607 SYSCTL_DESCR("Union file system"),
608 NULL, 0, NULL, 0,
609 CTL_VFS, 15, CTL_EOL);
610 /*
611 * XXX the "15" above could be dynamic, thereby eliminating
612 * one more instance of the "number to vfs" mapping problem,
613 * but "15" is the order as taken from sys/mount.h
614 */
615 }
616
617 extern const struct vnodeopv_desc union_vnodeop_opv_desc;
618
619 const struct vnodeopv_desc * const union_vnodeopv_descs[] = {
620 &union_vnodeop_opv_desc,
621 NULL,
622 };
623
624 struct vfsops union_vfsops = {
625 MOUNT_UNION,
626 union_mount,
627 union_start,
628 union_unmount,
629 union_root,
630 union_quotactl,
631 union_statvfs,
632 union_sync,
633 union_vget,
634 union_fhtovp,
635 union_vptofh,
636 union_init,
637 NULL, /* vfs_reinit */
638 union_done,
639 NULL,
640 NULL, /* vfs_mountroot */
641 union_checkexp,
642 layerfs_snapshot,
643 union_vnodeopv_descs,
644 };
645