union_vfsops.c revision 1.25.2.1.2.1 1 /* $NetBSD: union_vfsops.c,v 1.25.2.1.2.1 2006/01/08 15:45:15 riz 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.25.2.1.2.1 2006/01/08 15:45:15 riz 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 mp->mnt_leaf = um->um_uppervp->v_mount->mnt_leaf;
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 int freeing;
343
344 #ifdef UNION_DIAGNOSTIC
345 printf("union_unmount(mp = %p)\n", mp);
346 #endif
347
348 /*
349 * Keep flushing vnodes from the mount list.
350 * This is needed because of the un_pvp held
351 * reference to the parent vnode.
352 * If more vnodes have been freed on a given pass,
353 * the try again. The loop will iterate at most
354 * (d) times, where (d) is the maximum tree depth
355 * in the filesystem.
356 */
357 for (freeing = 0; vflush(mp, NULL, 0) != 0;) {
358 struct vnode *vp;
359 int n;
360
361 /* count #vnodes held on mount list */
362 for (n = 0, vp = mp->mnt_vnodelist.lh_first;
363 vp != NULLVP;
364 vp = vp->v_mntvnodes.le_next)
365 n++;
366
367 /* if this is unchanged then stop */
368 if (n == freeing)
369 break;
370
371 /* otherwise try once more time */
372 freeing = n;
373 }
374
375 /*
376 * Ok, now that we've tried doing it gently, get out the hammer.
377 */
378
379 if (mntflags & MNT_FORCE)
380 vflush(mp, NULL, FORCECLOSE);
381
382
383 /*
384 * Discard references to upper and lower target vnodes.
385 */
386 if (um->um_lowervp)
387 vrele(um->um_lowervp);
388 vrele(um->um_uppervp);
389 crfree(um->um_cred);
390 /*
391 * Finally, throw away the union_mount structure
392 */
393 free(mp->mnt_data, M_UFSMNT); /* XXX */
394 mp->mnt_data = 0;
395 return (0);
396 }
397
398 int
399 union_root(mp, vpp)
400 struct mount *mp;
401 struct vnode **vpp;
402 {
403 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
404 int error;
405 int loselock;
406
407 /*
408 * Return locked reference to root.
409 */
410 VREF(um->um_uppervp);
411 if ((um->um_op == UNMNT_BELOW) &&
412 VOP_ISLOCKED(um->um_uppervp)) {
413 loselock = 1;
414 } else {
415 vn_lock(um->um_uppervp, LK_EXCLUSIVE | LK_RETRY);
416 loselock = 0;
417 }
418 if (um->um_lowervp)
419 VREF(um->um_lowervp);
420 error = union_allocvp(vpp, mp,
421 (struct vnode *) 0,
422 (struct vnode *) 0,
423 (struct componentname *) 0,
424 um->um_uppervp,
425 um->um_lowervp,
426 1);
427
428 if (error) {
429 if (!loselock)
430 VOP_UNLOCK(um->um_uppervp, 0);
431 vrele(um->um_uppervp);
432 if (um->um_lowervp)
433 vrele(um->um_lowervp);
434 } else {
435 if (loselock)
436 VTOUNION(*vpp)->un_flags &= ~UN_ULOCK;
437 }
438
439 return (error);
440 }
441
442 /*ARGSUSED*/
443 int
444 union_quotactl(mp, cmd, uid, arg, p)
445 struct mount *mp;
446 int cmd;
447 uid_t uid;
448 void *arg;
449 struct proc *p;
450 {
451
452 return (EOPNOTSUPP);
453 }
454
455 int
456 union_statvfs(mp, sbp, p)
457 struct mount *mp;
458 struct statvfs *sbp;
459 struct proc *p;
460 {
461 int error;
462 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
463 struct statvfs *sbuf = malloc(sizeof(*sbuf), M_TEMP, M_WAITOK | M_ZERO);
464 unsigned long lbsize;
465
466 #ifdef UNION_DIAGNOSTIC
467 printf("union_statvfs(mp = %p, lvp = %p, uvp = %p)\n", mp,
468 um->um_lowervp, um->um_uppervp);
469 #endif
470
471 if (um->um_lowervp) {
472 error = VFS_STATVFS(um->um_lowervp->v_mount, sbuf, p);
473 if (error)
474 goto done;
475 }
476
477 /* now copy across the "interesting" information and fake the rest */
478 lbsize = sbuf->f_bsize;
479 sbp->f_blocks = sbuf->f_blocks - sbuf->f_bfree;
480 sbp->f_files = sbuf->f_files - sbuf->f_ffree;
481
482 error = VFS_STATVFS(um->um_uppervp->v_mount, sbuf, p);
483 if (error)
484 goto done;
485
486 sbp->f_flag = sbuf->f_flag;
487 sbp->f_bsize = sbuf->f_bsize;
488 sbp->f_frsize = sbuf->f_frsize;
489 sbp->f_iosize = sbuf->f_iosize;
490
491 /*
492 * The "total" fields count total resources in all layers,
493 * the "free" fields count only those resources which are
494 * free in the upper layer (since only the upper layer
495 * is writable).
496 */
497
498 if (sbuf->f_bsize != lbsize)
499 sbp->f_blocks = sbp->f_blocks * lbsize / sbuf->f_bsize;
500 sbp->f_blocks += sbuf->f_blocks;
501 sbp->f_bfree = sbuf->f_bfree;
502 sbp->f_bavail = sbuf->f_bavail;
503 sbp->f_bresvd = sbuf->f_bresvd;
504 sbp->f_files += sbuf->f_files;
505 sbp->f_ffree = sbuf->f_ffree;
506 sbp->f_favail = sbuf->f_favail;
507 sbp->f_fresvd = sbuf->f_fresvd;
508
509 copy_statvfs_info(sbp, mp);
510 done:
511 free(sbuf, M_TEMP);
512 return error;
513 }
514
515 /*ARGSUSED*/
516 int
517 union_sync(mp, waitfor, cred, p)
518 struct mount *mp;
519 int waitfor;
520 struct ucred *cred;
521 struct proc *p;
522 {
523
524 /*
525 * XXX - Assumes no data cached at union layer.
526 */
527 return (0);
528 }
529
530 /*ARGSUSED*/
531 int
532 union_vget(mp, ino, vpp)
533 struct mount *mp;
534 ino_t ino;
535 struct vnode **vpp;
536 {
537
538 return (EOPNOTSUPP);
539 }
540
541 /*ARGSUSED*/
542 int
543 union_fhtovp(mp, fidp, vpp)
544 struct mount *mp;
545 struct fid *fidp;
546 struct vnode **vpp;
547 {
548
549 return (EOPNOTSUPP);
550 }
551
552 /*ARGSUSED*/
553 int
554 union_checkexp(mp, nam, exflagsp, credanonp)
555 struct mount *mp;
556 struct mbuf *nam;
557 int *exflagsp;
558 struct ucred **credanonp;
559 {
560
561 return (EOPNOTSUPP);
562 }
563
564 /*ARGSUSED*/
565 int
566 union_vptofh(vp, fhp)
567 struct vnode *vp;
568 struct fid *fhp;
569 {
570
571 return (EOPNOTSUPP);
572 }
573
574 SYSCTL_SETUP(sysctl_vfs_union_setup, "sysctl vfs.union subtree setup")
575 {
576
577 sysctl_createv(clog, 0, NULL, NULL,
578 CTLFLAG_PERMANENT,
579 CTLTYPE_NODE, "vfs", NULL,
580 NULL, 0, NULL, 0,
581 CTL_VFS, CTL_EOL);
582 sysctl_createv(clog, 0, NULL, NULL,
583 CTLFLAG_PERMANENT,
584 CTLTYPE_NODE, "union",
585 SYSCTL_DESCR("Union file system"),
586 NULL, 0, NULL, 0,
587 CTL_VFS, 15, CTL_EOL);
588 /*
589 * XXX the "15" above could be dynamic, thereby eliminating
590 * one more instance of the "number to vfs" mapping problem,
591 * but "15" is the order as taken from sys/mount.h
592 */
593 }
594
595 extern const struct vnodeopv_desc union_vnodeop_opv_desc;
596
597 const struct vnodeopv_desc * const union_vnodeopv_descs[] = {
598 &union_vnodeop_opv_desc,
599 NULL,
600 };
601
602 struct vfsops union_vfsops = {
603 MOUNT_UNION,
604 union_mount,
605 union_start,
606 union_unmount,
607 union_root,
608 union_quotactl,
609 union_statvfs,
610 union_sync,
611 union_vget,
612 union_fhtovp,
613 union_vptofh,
614 union_init,
615 NULL, /* vfs_reinit */
616 union_done,
617 NULL,
618 NULL, /* vfs_mountroot */
619 union_checkexp,
620 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
621 vfs_stdextattrctl,
622 union_vnodeopv_descs,
623 };
624