union_vfsops.c revision 1.7 1 /* $NetBSD: union_vfsops.c,v 1.7 2003/06/29 18:43:27 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1994 The Regents of the University of California.
5 * Copyright (c) 1994 Jan-Simon Pendry.
6 * All rights reserved.
7 *
8 * This code is derived from software donated to Berkeley by
9 * Jan-Simon Pendry.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 * @(#)union_vfsops.c 8.20 (Berkeley) 5/20/95
40 */
41
42 /*
43 * Union Layer
44 */
45
46 #include <sys/cdefs.h>
47 __KERNEL_RCSID(0, "$NetBSD: union_vfsops.c,v 1.7 2003/06/29 18:43:27 thorpej Exp $");
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/time.h>
52 #include <sys/proc.h>
53 #include <sys/vnode.h>
54 #include <sys/mount.h>
55 #include <sys/namei.h>
56 #include <sys/malloc.h>
57 #include <sys/filedesc.h>
58 #include <sys/queue.h>
59 #include <sys/stat.h>
60
61 #include <fs/union/union.h>
62
63 int union_mount __P((struct mount *, const char *, void *, struct nameidata *,
64 struct lwp *));
65 int union_start __P((struct mount *, int, struct lwp *));
66 int union_unmount __P((struct mount *, int, struct lwp *));
67 int union_root __P((struct mount *, struct vnode **));
68 int union_quotactl __P((struct mount *, int, uid_t, caddr_t, struct lwp *));
69 int union_statfs __P((struct mount *, struct statfs *, struct lwp *));
70 int union_sync __P((struct mount *, int, struct ucred *, struct lwp *));
71 int union_vget __P((struct mount *, ino_t, struct vnode **));
72 int union_fhtovp __P((struct mount *, struct fid *, struct vnode **));
73 int union_checkexp __P((struct mount *, struct mbuf *, int *,
74 struct ucred **));
75 int union_vptofh __P((struct vnode *, struct fid *));
76 int union_sysctl __P((int *, u_int, void *, size_t *, void *, size_t,
77 struct lwp *));
78
79 /*
80 * Mount union filesystem
81 */
82 int
83 union_mount(mp, path, data, ndp, l)
84 struct mount *mp;
85 const char *path;
86 void *data;
87 struct nameidata *ndp;
88 struct lwp *l;
89 {
90 int error = 0;
91 struct union_args args;
92 struct proc *p = l->l_proc;
93 struct vnode *lowerrootvp = NULLVP;
94 struct vnode *upperrootvp = NULLVP;
95 struct union_mount *um = 0;
96 struct ucred *cred = 0;
97 char *cp;
98 int len;
99 size_t size;
100
101 #ifdef UNION_DIAGNOSTIC
102 printf("union_mount(mp = %p)\n", mp);
103 #endif
104
105 if (mp->mnt_flag & MNT_GETARGS) {
106 um = MOUNTTOUNIONMOUNT(mp);
107 if (um == NULL)
108 return EIO;
109 args.target = NULL;
110 args.mntflags = um->um_op;
111 return copyout(&args, data, sizeof(args));
112 }
113 /*
114 * Update is a no-op
115 */
116 if (mp->mnt_flag & MNT_UPDATE) {
117 /*
118 * Need to provide.
119 * 1. a way to convert between rdonly and rdwr mounts.
120 * 2. support for nfs exports.
121 */
122 error = EOPNOTSUPP;
123 goto bad;
124 }
125
126 /*
127 * Get argument
128 */
129 error = copyin(data, (caddr_t)&args, sizeof(struct union_args));
130 if (error)
131 goto bad;
132
133 lowerrootvp = mp->mnt_vnodecovered;
134 VREF(lowerrootvp);
135
136 /*
137 * Find upper node.
138 */
139 NDINIT(ndp, LOOKUP, FOLLOW,
140 UIO_USERSPACE, args.target, l);
141
142 if ((error = namei(ndp)) != 0)
143 goto bad;
144
145 upperrootvp = ndp->ni_vp;
146
147 if (upperrootvp->v_type != VDIR) {
148 error = EINVAL;
149 goto bad;
150 }
151
152 um = (struct union_mount *) malloc(sizeof(struct union_mount),
153 M_UFSMNT, M_WAITOK); /* XXX */
154
155 /*
156 * Keep a held reference to the target vnodes.
157 * They are vrele'd in union_unmount.
158 *
159 * Depending on the _BELOW flag, the filesystems are
160 * viewed in a different order. In effect, this is the
161 * same as providing a mount under option to the mount syscall.
162 */
163
164 um->um_op = args.mntflags & UNMNT_OPMASK;
165 switch (um->um_op) {
166 case UNMNT_ABOVE:
167 um->um_lowervp = lowerrootvp;
168 um->um_uppervp = upperrootvp;
169 break;
170
171 case UNMNT_BELOW:
172 um->um_lowervp = upperrootvp;
173 um->um_uppervp = lowerrootvp;
174 break;
175
176 case UNMNT_REPLACE:
177 vrele(lowerrootvp);
178 lowerrootvp = NULLVP;
179 um->um_uppervp = upperrootvp;
180 um->um_lowervp = lowerrootvp;
181 break;
182
183 default:
184 error = EINVAL;
185 goto bad;
186 }
187
188 /*
189 * Unless the mount is readonly, ensure that the top layer
190 * supports whiteout operations
191 */
192 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
193 error = VOP_WHITEOUT(um->um_uppervp, (struct componentname *) 0, LOOKUP);
194 if (error)
195 goto bad;
196 }
197
198 um->um_cred = p->p_ucred;
199 crhold(um->um_cred);
200 um->um_cmode = UN_DIRMODE &~ p->p_cwdi->cwdi_cmask;
201
202 /*
203 * Depending on what you think the MNT_LOCAL flag might mean,
204 * you may want the && to be || on the conditional below.
205 * At the moment it has been defined that the filesystem is
206 * only local if it is all local, ie the MNT_LOCAL flag implies
207 * that the entire namespace is local. If you think the MNT_LOCAL
208 * flag implies that some of the files might be stored locally
209 * then you will want to change the conditional.
210 */
211 if (um->um_op == UNMNT_ABOVE) {
212 if (((um->um_lowervp == NULLVP) ||
213 (um->um_lowervp->v_mount->mnt_flag & MNT_LOCAL)) &&
214 (um->um_uppervp->v_mount->mnt_flag & MNT_LOCAL))
215 mp->mnt_flag |= MNT_LOCAL;
216 }
217
218 /*
219 * Copy in the upper layer's RDONLY flag. This is for the benefit
220 * of lookup() which explicitly checks the flag, rather than asking
221 * the filesystem for it's own opinion. This means, that an update
222 * mount of the underlying filesystem to go from rdonly to rdwr
223 * will leave the unioned view as read-only.
224 */
225 mp->mnt_flag |= (um->um_uppervp->v_mount->mnt_flag & MNT_RDONLY);
226
227 mp->mnt_data = um;
228 vfs_getnewfsid(mp);
229
230 error = set_statfs_info( path, UIO_USERSPACE, NULL, UIO_USERSPACE,
231 mp, l);
232 if (error)
233 goto bad;
234
235 switch (um->um_op) {
236 case UNMNT_ABOVE:
237 cp = "<above>:";
238 break;
239 case UNMNT_BELOW:
240 cp = "<below>:";
241 break;
242 case UNMNT_REPLACE:
243 cp = "";
244 break;
245 default:
246 cp = "<invalid>:";
247 #ifdef DIAGNOSTIC
248 panic("union_mount: bad um_op");
249 #endif
250 break;
251 }
252 len = strlen(cp);
253 memcpy(mp->mnt_stat.f_mntfromname, cp, len);
254
255 cp = mp->mnt_stat.f_mntfromname + len;
256 len = MNAMELEN - len;
257
258 (void) copyinstr(args.target, cp, len - 1, &size);
259 memset(cp + size, 0, len - size);
260
261 #ifdef UNION_DIAGNOSTIC
262 printf("union_mount: from %s, on %s\n",
263 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
264 #endif
265
266 /* Setup the readdir hook if it's not set already */
267 if (!vn_union_readdir_hook)
268 vn_union_readdir_hook = union_readdirhook;
269
270 return (0);
271
272 bad:
273 if (um)
274 free(um, M_UFSMNT);
275 if (cred)
276 crfree(cred);
277 if (upperrootvp)
278 vrele(upperrootvp);
279 if (lowerrootvp)
280 vrele(lowerrootvp);
281 return (error);
282 }
283
284 /*
285 * VFS start. Nothing needed here - the start routine
286 * on the underlying filesystem(s) will have been called
287 * when that filesystem was mounted.
288 */
289 /*ARGSUSED*/
290 int
291 union_start(mp, flags, l)
292 struct mount *mp;
293 int flags;
294 struct lwp *l;
295 {
296
297 return (0);
298 }
299
300 /*
301 * Free reference to union layer
302 */
303 int
304 union_unmount(mp, mntflags, l)
305 struct mount *mp;
306 int mntflags;
307 struct lwp *l;
308 {
309 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
310 struct vnode *um_rootvp;
311 int error;
312 int freeing;
313
314 #ifdef UNION_DIAGNOSTIC
315 printf("union_unmount(mp = %p)\n", mp);
316 #endif
317
318 if ((error = union_root(mp, &um_rootvp)) != 0)
319 return (error);
320
321 /*
322 * Keep flushing vnodes from the mount list.
323 * This is needed because of the un_pvp held
324 * reference to the parent vnode.
325 * If more vnodes have been freed on a given pass,
326 * the try again. The loop will iterate at most
327 * (d) times, where (d) is the maximum tree depth
328 * in the filesystem.
329 */
330 for (freeing = 0; vflush(mp, um_rootvp, 0) != 0;) {
331 struct vnode *vp;
332 int n;
333
334 /* count #vnodes held on mount list */
335 for (n = 0, vp = mp->mnt_vnodelist.lh_first;
336 vp != NULLVP;
337 vp = vp->v_mntvnodes.le_next)
338 n++;
339
340 /* if this is unchanged then stop */
341 if (n == freeing)
342 break;
343
344 /* otherwise try once more time */
345 freeing = n;
346 }
347
348 /*
349 * Ok, now that we've tried doing it gently, get out the hammer.
350 */
351
352 if (mntflags & MNT_FORCE)
353 vflush(mp, um_rootvp, FORCECLOSE);
354
355
356 /* At this point the root vnode should have a single reference */
357 if (um_rootvp->v_usecount > 1) {
358 vput(um_rootvp);
359 return (EBUSY);
360 }
361
362 #ifdef UNION_DIAGNOSTIC
363 vprint("union root", um_rootvp);
364 #endif
365 /*
366 * Discard references to upper and lower target vnodes.
367 */
368 if (um->um_lowervp)
369 vrele(um->um_lowervp);
370 vrele(um->um_uppervp);
371 crfree(um->um_cred);
372 /*
373 * Release reference on underlying root vnode
374 */
375 vput(um_rootvp);
376 /*
377 * And blow it away for future re-use
378 */
379 vgone(um_rootvp);
380 /*
381 * Finally, throw away the union_mount structure
382 */
383 free(mp->mnt_data, M_UFSMNT); /* XXX */
384 mp->mnt_data = 0;
385 return (0);
386 }
387
388 int
389 union_root(mp, vpp)
390 struct mount *mp;
391 struct vnode **vpp;
392 {
393 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
394 int error;
395 int loselock;
396
397 /*
398 * Return locked reference to root.
399 */
400 VREF(um->um_uppervp);
401 if ((um->um_op == UNMNT_BELOW) &&
402 VOP_ISLOCKED(um->um_uppervp)) {
403 loselock = 1;
404 } else {
405 vn_lock(um->um_uppervp, LK_EXCLUSIVE | LK_RETRY);
406 loselock = 0;
407 }
408 if (um->um_lowervp)
409 VREF(um->um_lowervp);
410 error = union_allocvp(vpp, mp,
411 (struct vnode *) 0,
412 (struct vnode *) 0,
413 (struct componentname *) 0,
414 um->um_uppervp,
415 um->um_lowervp,
416 1);
417
418 if (error) {
419 if (!loselock)
420 VOP_UNLOCK(um->um_uppervp, 0);
421 vrele(um->um_uppervp);
422 if (um->um_lowervp)
423 vrele(um->um_lowervp);
424 } else {
425 if (loselock)
426 VTOUNION(*vpp)->un_flags &= ~UN_ULOCK;
427 }
428
429 return (error);
430 }
431
432 /*ARGSUSED*/
433 int
434 union_quotactl(mp, cmd, uid, arg, l)
435 struct mount *mp;
436 int cmd;
437 uid_t uid;
438 caddr_t arg;
439 struct lwp *l;
440 {
441
442 return (EOPNOTSUPP);
443 }
444
445 int
446 union_statfs(mp, sbp, l)
447 struct mount *mp;
448 struct statfs *sbp;
449 struct lwp *l;
450 {
451 int error;
452 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
453 struct statfs mstat;
454 int lbsize;
455
456 #ifdef UNION_DIAGNOSTIC
457 printf("union_statfs(mp = %p, lvp = %p, uvp = %p)\n", mp,
458 um->um_lowervp, um->um_uppervp);
459 #endif
460
461 memset(&mstat, 0, sizeof(mstat));
462
463 if (um->um_lowervp) {
464 error = VFS_STATFS(um->um_lowervp->v_mount, &mstat, l);
465 if (error)
466 return (error);
467 }
468
469 /* now copy across the "interesting" information and fake the rest */
470 lbsize = mstat.f_bsize;
471 sbp->f_blocks = mstat.f_blocks - mstat.f_bfree;
472 sbp->f_files = mstat.f_files - mstat.f_ffree;
473
474 error = VFS_STATFS(um->um_uppervp->v_mount, &mstat, l);
475 if (error)
476 return (error);
477
478 sbp->f_type = 0;
479 sbp->f_flags = mstat.f_flags;
480 sbp->f_bsize = mstat.f_bsize;
481 sbp->f_iosize = mstat.f_iosize;
482
483 /*
484 * The "total" fields count total resources in all layers,
485 * the "free" fields count only those resources which are
486 * free in the upper layer (since only the upper layer
487 * is writable).
488 */
489
490 if (mstat.f_bsize != lbsize)
491 sbp->f_blocks = sbp->f_blocks * lbsize / mstat.f_bsize;
492 sbp->f_blocks += mstat.f_blocks;
493 sbp->f_bfree = mstat.f_bfree;
494 sbp->f_bavail = mstat.f_bavail;
495 sbp->f_files += mstat.f_files;
496 sbp->f_ffree = mstat.f_ffree;
497
498 copy_statfs_info(sbp, mp);
499 return (0);
500 }
501
502 /*ARGSUSED*/
503 int
504 union_sync(mp, waitfor, cred, l)
505 struct mount *mp;
506 int waitfor;
507 struct ucred *cred;
508 struct lwp *l;
509 {
510
511 /*
512 * XXX - Assumes no data cached at union layer.
513 */
514 return (0);
515 }
516
517 /*ARGSUSED*/
518 int
519 union_vget(mp, ino, vpp)
520 struct mount *mp;
521 ino_t ino;
522 struct vnode **vpp;
523 {
524
525 return (EOPNOTSUPP);
526 }
527
528 /*ARGSUSED*/
529 int
530 union_fhtovp(mp, fidp, vpp)
531 struct mount *mp;
532 struct fid *fidp;
533 struct vnode **vpp;
534 {
535
536 return (EOPNOTSUPP);
537 }
538
539 /*ARGSUSED*/
540 int
541 union_checkexp(mp, nam, exflagsp, credanonp)
542 struct mount *mp;
543 struct mbuf *nam;
544 int *exflagsp;
545 struct ucred **credanonp;
546 {
547
548 return (EOPNOTSUPP);
549 }
550
551 /*ARGSUSED*/
552 int
553 union_vptofh(vp, fhp)
554 struct vnode *vp;
555 struct fid *fhp;
556 {
557
558 return (EOPNOTSUPP);
559 }
560
561 int
562 union_sysctl(name, namelen, oldp, oldlenp, newp, newlen, l)
563 int *name;
564 u_int namelen;
565 void *oldp;
566 size_t *oldlenp;
567 void *newp;
568 size_t newlen;
569 struct lwp *l;
570 {
571 return (EOPNOTSUPP);
572 }
573
574 extern const struct vnodeopv_desc union_vnodeop_opv_desc;
575
576 const struct vnodeopv_desc * const union_vnodeopv_descs[] = {
577 &union_vnodeop_opv_desc,
578 NULL,
579 };
580
581 struct vfsops union_vfsops = {
582 MOUNT_UNION,
583 union_mount,
584 union_start,
585 union_unmount,
586 union_root,
587 union_quotactl,
588 union_statfs,
589 union_sync,
590 union_vget,
591 union_fhtovp,
592 union_vptofh,
593 union_init,
594 NULL, /* vfs_reinit */
595 union_done,
596 union_sysctl,
597 NULL, /* vfs_mountroot */
598 union_checkexp,
599 union_vnodeopv_descs,
600 };
601