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