union_vfsops.c revision 1.1 1 /* $NetBSD: union_vfsops.c,v 1.1 2003/03/16 08:26:54 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.1 2003/03/16 08:26:54 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 return (0);
263
264 bad:
265 if (um)
266 free(um, M_UFSMNT);
267 if (cred)
268 crfree(cred);
269 if (upperrootvp)
270 vrele(upperrootvp);
271 if (lowerrootvp)
272 vrele(lowerrootvp);
273 return (error);
274 }
275
276 /*
277 * VFS start. Nothing needed here - the start routine
278 * on the underlying filesystem(s) will have been called
279 * when that filesystem was mounted.
280 */
281 /*ARGSUSED*/
282 int
283 union_start(mp, flags, p)
284 struct mount *mp;
285 int flags;
286 struct proc *p;
287 {
288
289 return (0);
290 }
291
292 /*
293 * Free reference to union layer
294 */
295 int
296 union_unmount(mp, mntflags, p)
297 struct mount *mp;
298 int mntflags;
299 struct proc *p;
300 {
301 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
302 struct vnode *um_rootvp;
303 int error;
304 int freeing;
305
306 #ifdef UNION_DIAGNOSTIC
307 printf("union_unmount(mp = %p)\n", mp);
308 #endif
309
310 if ((error = union_root(mp, &um_rootvp)) != 0)
311 return (error);
312
313 /*
314 * Keep flushing vnodes from the mount list.
315 * This is needed because of the un_pvp held
316 * reference to the parent vnode.
317 * If more vnodes have been freed on a given pass,
318 * the try again. The loop will iterate at most
319 * (d) times, where (d) is the maximum tree depth
320 * in the filesystem.
321 */
322 for (freeing = 0; vflush(mp, um_rootvp, 0) != 0;) {
323 struct vnode *vp;
324 int n;
325
326 /* count #vnodes held on mount list */
327 for (n = 0, vp = mp->mnt_vnodelist.lh_first;
328 vp != NULLVP;
329 vp = vp->v_mntvnodes.le_next)
330 n++;
331
332 /* if this is unchanged then stop */
333 if (n == freeing)
334 break;
335
336 /* otherwise try once more time */
337 freeing = n;
338 }
339
340 /*
341 * Ok, now that we've tried doing it gently, get out the hammer.
342 */
343
344 if (mntflags & MNT_FORCE)
345 vflush(mp, um_rootvp, FORCECLOSE);
346
347
348 /* At this point the root vnode should have a single reference */
349 if (um_rootvp->v_usecount > 1) {
350 vput(um_rootvp);
351 return (EBUSY);
352 }
353
354 #ifdef UNION_DIAGNOSTIC
355 vprint("union root", um_rootvp);
356 #endif
357 /*
358 * Discard references to upper and lower target vnodes.
359 */
360 if (um->um_lowervp)
361 vrele(um->um_lowervp);
362 vrele(um->um_uppervp);
363 crfree(um->um_cred);
364 /*
365 * Release reference on underlying root vnode
366 */
367 vput(um_rootvp);
368 /*
369 * And blow it away for future re-use
370 */
371 vgone(um_rootvp);
372 /*
373 * Finally, throw away the union_mount structure
374 */
375 free(mp->mnt_data, M_UFSMNT); /* XXX */
376 mp->mnt_data = 0;
377 return (0);
378 }
379
380 int
381 union_root(mp, vpp)
382 struct mount *mp;
383 struct vnode **vpp;
384 {
385 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
386 int error;
387 int loselock;
388
389 /*
390 * Return locked reference to root.
391 */
392 VREF(um->um_uppervp);
393 if ((um->um_op == UNMNT_BELOW) &&
394 VOP_ISLOCKED(um->um_uppervp)) {
395 loselock = 1;
396 } else {
397 vn_lock(um->um_uppervp, LK_EXCLUSIVE | LK_RETRY);
398 loselock = 0;
399 }
400 if (um->um_lowervp)
401 VREF(um->um_lowervp);
402 error = union_allocvp(vpp, mp,
403 (struct vnode *) 0,
404 (struct vnode *) 0,
405 (struct componentname *) 0,
406 um->um_uppervp,
407 um->um_lowervp,
408 1);
409
410 if (error) {
411 if (!loselock)
412 VOP_UNLOCK(um->um_uppervp, 0);
413 vrele(um->um_uppervp);
414 if (um->um_lowervp)
415 vrele(um->um_lowervp);
416 } else {
417 if (loselock)
418 VTOUNION(*vpp)->un_flags &= ~UN_ULOCK;
419 }
420
421 return (error);
422 }
423
424 /*ARGSUSED*/
425 int
426 union_quotactl(mp, cmd, uid, arg, p)
427 struct mount *mp;
428 int cmd;
429 uid_t uid;
430 caddr_t arg;
431 struct proc *p;
432 {
433
434 return (EOPNOTSUPP);
435 }
436
437 int
438 union_statfs(mp, sbp, p)
439 struct mount *mp;
440 struct statfs *sbp;
441 struct proc *p;
442 {
443 int error;
444 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
445 struct statfs mstat;
446 int lbsize;
447
448 #ifdef UNION_DIAGNOSTIC
449 printf("union_statfs(mp = %p, lvp = %p, uvp = %p)\n", mp,
450 um->um_lowervp, um->um_uppervp);
451 #endif
452
453 memset(&mstat, 0, sizeof(mstat));
454
455 if (um->um_lowervp) {
456 error = VFS_STATFS(um->um_lowervp->v_mount, &mstat, p);
457 if (error)
458 return (error);
459 }
460
461 /* now copy across the "interesting" information and fake the rest */
462 lbsize = mstat.f_bsize;
463 sbp->f_blocks = mstat.f_blocks - mstat.f_bfree;
464 sbp->f_files = mstat.f_files - mstat.f_ffree;
465
466 error = VFS_STATFS(um->um_uppervp->v_mount, &mstat, p);
467 if (error)
468 return (error);
469
470 sbp->f_type = 0;
471 sbp->f_flags = mstat.f_flags;
472 sbp->f_bsize = mstat.f_bsize;
473 sbp->f_iosize = mstat.f_iosize;
474
475 /*
476 * The "total" fields count total resources in all layers,
477 * the "free" fields count only those resources which are
478 * free in the upper layer (since only the upper layer
479 * is writable).
480 */
481
482 if (mstat.f_bsize != lbsize)
483 sbp->f_blocks = sbp->f_blocks * lbsize / mstat.f_bsize;
484 sbp->f_blocks += mstat.f_blocks;
485 sbp->f_bfree = mstat.f_bfree;
486 sbp->f_bavail = mstat.f_bavail;
487 sbp->f_files += mstat.f_files;
488 sbp->f_ffree = mstat.f_ffree;
489
490 if (sbp != &mp->mnt_stat) {
491 memcpy(&sbp->f_fsid, &mp->mnt_stat.f_fsid, sizeof(sbp->f_fsid));
492 memcpy(sbp->f_mntonname, mp->mnt_stat.f_mntonname, MNAMELEN);
493 memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname, MNAMELEN);
494 }
495 strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
496 return (0);
497 }
498
499 /*ARGSUSED*/
500 int
501 union_sync(mp, waitfor, cred, p)
502 struct mount *mp;
503 int waitfor;
504 struct ucred *cred;
505 struct proc *p;
506 {
507
508 /*
509 * XXX - Assumes no data cached at union layer.
510 */
511 return (0);
512 }
513
514 /*ARGSUSED*/
515 int
516 union_vget(mp, ino, vpp)
517 struct mount *mp;
518 ino_t ino;
519 struct vnode **vpp;
520 {
521
522 return (EOPNOTSUPP);
523 }
524
525 /*ARGSUSED*/
526 int
527 union_fhtovp(mp, fidp, vpp)
528 struct mount *mp;
529 struct fid *fidp;
530 struct vnode **vpp;
531 {
532
533 return (EOPNOTSUPP);
534 }
535
536 /*ARGSUSED*/
537 int
538 union_checkexp(mp, nam, exflagsp, credanonp)
539 struct mount *mp;
540 struct mbuf *nam;
541 int *exflagsp;
542 struct ucred **credanonp;
543 {
544
545 return (EOPNOTSUPP);
546 }
547
548 /*ARGSUSED*/
549 int
550 union_vptofh(vp, fhp)
551 struct vnode *vp;
552 struct fid *fhp;
553 {
554
555 return (EOPNOTSUPP);
556 }
557
558 int
559 union_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
560 int *name;
561 u_int namelen;
562 void *oldp;
563 size_t *oldlenp;
564 void *newp;
565 size_t newlen;
566 struct proc *p;
567 {
568 return (EOPNOTSUPP);
569 }
570
571 extern const struct vnodeopv_desc union_vnodeop_opv_desc;
572
573 const struct vnodeopv_desc * const union_vnodeopv_descs[] = {
574 &union_vnodeop_opv_desc,
575 NULL,
576 };
577
578 struct vfsops union_vfsops = {
579 MOUNT_UNION,
580 union_mount,
581 union_start,
582 union_unmount,
583 union_root,
584 union_quotactl,
585 union_statfs,
586 union_sync,
587 union_vget,
588 union_fhtovp,
589 union_vptofh,
590 union_init,
591 NULL,
592 union_done,
593 union_sysctl,
594 NULL, /* vfs_mountroot */
595 union_checkexp,
596 union_vnodeopv_descs,
597 };
598