union_vfsops.c revision 1.16 1 /* $NetBSD: union_vfsops.c,v 1.16 2004/05/22 22:13:50 christos 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.16 2004/05/22 22:13:50 christos 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 unsigned long l_bsize;
485 fsblkcnt_t l_blocks, l_files;
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, sbp, p);
494 if (error)
495 return (error);
496 l_bsize = sbp->f_bsize;
497 l_blocks = sbp->f_blocks - sbp->f_bfree;
498 l_files = sbp->f_files - sbp->f_ffree;
499 } else {
500 l_bsize = 0;
501 l_blocks = 0;
502 l_files = 0;
503 }
504
505 error = VFS_STATVFS(um->um_uppervp->v_mount, sbp, p);
506 if (error)
507 return (error);
508 /*
509 * The "total" fields count total resources in all layers,
510 * the "free" fields count only those resources which are
511 * free in the upper layer (since only the upper layer
512 * is writable).
513 */
514 sbp->f_blocks += l_blocks * l_bsize / sbp->f_bsize;
515 sbp->f_files += l_files;
516
517 copy_statvfs_info(sbp, mp);
518 return (0);
519 }
520
521 /*ARGSUSED*/
522 int
523 union_sync(mp, waitfor, cred, p)
524 struct mount *mp;
525 int waitfor;
526 struct ucred *cred;
527 struct proc *p;
528 {
529
530 /*
531 * XXX - Assumes no data cached at union layer.
532 */
533 return (0);
534 }
535
536 /*ARGSUSED*/
537 int
538 union_vget(mp, ino, vpp)
539 struct mount *mp;
540 ino_t ino;
541 struct vnode **vpp;
542 {
543
544 return (EOPNOTSUPP);
545 }
546
547 /*ARGSUSED*/
548 int
549 union_fhtovp(mp, fidp, vpp)
550 struct mount *mp;
551 struct fid *fidp;
552 struct vnode **vpp;
553 {
554
555 return (EOPNOTSUPP);
556 }
557
558 /*ARGSUSED*/
559 int
560 union_checkexp(mp, nam, exflagsp, credanonp)
561 struct mount *mp;
562 struct mbuf *nam;
563 int *exflagsp;
564 struct ucred **credanonp;
565 {
566
567 return (EOPNOTSUPP);
568 }
569
570 /*ARGSUSED*/
571 int
572 union_vptofh(vp, fhp)
573 struct vnode *vp;
574 struct fid *fhp;
575 {
576
577 return (EOPNOTSUPP);
578 }
579
580 SYSCTL_SETUP(sysctl_vfs_union_setup, "sysctl vfs.union subtree setup")
581 {
582
583 sysctl_createv(clog, 0, NULL, NULL,
584 CTLFLAG_PERMANENT,
585 CTLTYPE_NODE, "vfs", NULL,
586 NULL, 0, NULL, 0,
587 CTL_VFS, CTL_EOL);
588 sysctl_createv(clog, 0, NULL, NULL,
589 CTLFLAG_PERMANENT,
590 CTLTYPE_NODE, "union", NULL,
591 NULL, 0, NULL, 0,
592 CTL_VFS, 15, CTL_EOL);
593 /*
594 * XXX the "15" above could be dynamic, thereby eliminating
595 * one more instance of the "number to vfs" mapping problem,
596 * but "15" is the order as taken from sys/mount.h
597 */
598 }
599
600 extern const struct vnodeopv_desc union_vnodeop_opv_desc;
601
602 const struct vnodeopv_desc * const union_vnodeopv_descs[] = {
603 &union_vnodeop_opv_desc,
604 NULL,
605 };
606
607 struct vfsops union_vfsops = {
608 MOUNT_UNION,
609 union_mount,
610 union_start,
611 union_unmount,
612 union_root,
613 union_quotactl,
614 union_statvfs,
615 union_sync,
616 union_vget,
617 union_fhtovp,
618 union_vptofh,
619 union_init,
620 NULL, /* vfs_reinit */
621 union_done,
622 NULL,
623 NULL, /* vfs_mountroot */
624 union_checkexp,
625 union_vnodeopv_descs,
626 };
627