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