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