union_vfsops.c revision 1.51 1 /* $NetBSD: union_vfsops.c,v 1.51 2008/01/28 14:31:17 dholland 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.51 2008/01/28 14:31:17 dholland 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 #include <sys/kauth.h>
95
96 #include <fs/union/union.h>
97
98 VFS_PROTOS(union);
99
100 /*
101 * Mount union filesystem
102 */
103 int
104 union_mount(mp, path, data, data_len)
105 struct mount *mp;
106 const char *path;
107 void *data;
108 size_t *data_len;
109 {
110 struct lwp *l = curlwp;
111 struct nameidata nd;
112 int error = 0;
113 struct union_args *args = data;
114 struct vnode *lowerrootvp = NULLVP;
115 struct vnode *upperrootvp = NULLVP;
116 struct union_mount *um = 0;
117 const char *cp;
118 char *xp;
119 int len;
120 size_t size;
121
122 if (*data_len < sizeof *args)
123 return EINVAL;
124
125 #ifdef UNION_DIAGNOSTIC
126 printf("union_mount(mp = %p)\n", mp);
127 #endif
128
129 if (mp->mnt_flag & MNT_GETARGS) {
130 um = MOUNTTOUNIONMOUNT(mp);
131 if (um == NULL)
132 return EIO;
133 args->target = NULL;
134 args->mntflags = um->um_op;
135 *data_len = sizeof *args;
136 return 0;
137 }
138 /*
139 * Update is a no-op
140 */
141 if (mp->mnt_flag & MNT_UPDATE) {
142 /*
143 * Need to provide.
144 * 1. a way to convert between rdonly and rdwr mounts.
145 * 2. support for nfs exports.
146 */
147 error = EOPNOTSUPP;
148 goto bad;
149 }
150
151 lowerrootvp = mp->mnt_vnodecovered;
152 VREF(lowerrootvp);
153
154 /*
155 * Find upper node.
156 */
157 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, args->target);
158
159 if ((error = namei(&nd)) != 0)
160 goto bad;
161
162 upperrootvp = nd.ni_vp;
163
164 if (upperrootvp->v_type != VDIR) {
165 error = EINVAL;
166 goto bad;
167 }
168
169 um = (struct union_mount *) malloc(sizeof(struct union_mount),
170 M_UFSMNT, M_WAITOK); /* XXX */
171
172 /*
173 * Keep a held reference to the target vnodes.
174 * They are vrele'd in union_unmount.
175 *
176 * Depending on the _BELOW flag, the filesystems are
177 * viewed in a different order. In effect, this is the
178 * same as providing a mount under option to the mount syscall.
179 */
180
181 um->um_op = args->mntflags & UNMNT_OPMASK;
182 switch (um->um_op) {
183 case UNMNT_ABOVE:
184 um->um_lowervp = lowerrootvp;
185 um->um_uppervp = upperrootvp;
186 break;
187
188 case UNMNT_BELOW:
189 um->um_lowervp = upperrootvp;
190 um->um_uppervp = lowerrootvp;
191 break;
192
193 case UNMNT_REPLACE:
194 vrele(lowerrootvp);
195 lowerrootvp = NULLVP;
196 um->um_uppervp = upperrootvp;
197 um->um_lowervp = lowerrootvp;
198 break;
199
200 default:
201 error = EINVAL;
202 goto bad;
203 }
204
205 /*
206 * Unless the mount is readonly, ensure that the top layer
207 * supports whiteout operations
208 */
209 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
210 error = VOP_WHITEOUT(um->um_uppervp, (struct componentname *) 0, LOOKUP);
211 if (error)
212 goto bad;
213 }
214
215 um->um_cred = l->l_cred;
216 kauth_cred_hold(um->um_cred);
217 um->um_cmode = UN_DIRMODE &~ l->l_proc->p_cwdi->cwdi_cmask;
218
219 /*
220 * Depending on what you think the MNT_LOCAL flag might mean,
221 * you may want the && to be || on the conditional below.
222 * At the moment it has been defined that the filesystem is
223 * only local if it is all local, ie the MNT_LOCAL flag implies
224 * that the entire namespace is local. If you think the MNT_LOCAL
225 * flag implies that some of the files might be stored locally
226 * then you will want to change the conditional.
227 */
228 if (um->um_op == UNMNT_ABOVE) {
229 if (((um->um_lowervp == NULLVP) ||
230 (um->um_lowervp->v_mount->mnt_flag & MNT_LOCAL)) &&
231 (um->um_uppervp->v_mount->mnt_flag & MNT_LOCAL))
232 mp->mnt_flag |= MNT_LOCAL;
233 }
234
235 /*
236 * Copy in the upper layer's RDONLY flag. This is for the benefit
237 * of lookup() which explicitly checks the flag, rather than asking
238 * the filesystem for it's own opinion. This means, that an update
239 * mount of the underlying filesystem to go from rdonly to rdwr
240 * will leave the unioned view as read-only.
241 */
242 mp->mnt_flag |= (um->um_uppervp->v_mount->mnt_flag & MNT_RDONLY);
243
244 mp->mnt_data = um;
245 vfs_getnewfsid(mp);
246
247 error = set_statvfs_info( path, UIO_USERSPACE, NULL, UIO_USERSPACE,
248 mp->mnt_op->vfs_name, mp, l);
249 if (error)
250 goto bad;
251
252 switch (um->um_op) {
253 case UNMNT_ABOVE:
254 cp = "<above>:";
255 break;
256 case UNMNT_BELOW:
257 cp = "<below>:";
258 break;
259 case UNMNT_REPLACE:
260 cp = "";
261 break;
262 default:
263 cp = "<invalid>:";
264 #ifdef DIAGNOSTIC
265 panic("union_mount: bad um_op");
266 #endif
267 break;
268 }
269 len = strlen(cp);
270 memcpy(mp->mnt_stat.f_mntfromname, cp, len);
271
272 xp = mp->mnt_stat.f_mntfromname + len;
273 len = MNAMELEN - len;
274
275 (void) copyinstr(args->target, xp, len - 1, &size);
276 memset(xp + size, 0, len - size);
277
278 #ifdef UNION_DIAGNOSTIC
279 printf("union_mount: from %s, on %s\n",
280 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
281 #endif
282
283 /* Setup the readdir hook if it's not set already */
284 if (!vn_union_readdir_hook)
285 vn_union_readdir_hook = union_readdirhook;
286
287 return (0);
288
289 bad:
290 if (um)
291 free(um, M_UFSMNT);
292 if (upperrootvp)
293 vrele(upperrootvp);
294 if (lowerrootvp)
295 vrele(lowerrootvp);
296 return (error);
297 }
298
299 /*
300 * VFS start. Nothing needed here - the start routine
301 * on the underlying filesystem(s) will have been called
302 * when that filesystem was mounted.
303 */
304 /*ARGSUSED*/
305 int
306 union_start(struct mount *mp, int flags)
307 {
308
309 return (0);
310 }
311
312 /*
313 * Free reference to union layer
314 */
315 int
316 union_unmount(struct mount *mp, int mntflags)
317 {
318 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
319 int freeing;
320 int error;
321
322 #ifdef UNION_DIAGNOSTIC
323 printf("union_unmount(mp = %p)\n", mp);
324 #endif
325
326 /*
327 * Keep flushing vnodes from the mount list.
328 * This is needed because of the un_pvp held
329 * reference to the parent vnode.
330 * If more vnodes have been freed on a given pass,
331 * the try again. The loop will iterate at most
332 * (d) times, where (d) is the maximum tree depth
333 * in the filesystem.
334 */
335 for (freeing = 0; (error = vflush(mp, NULL, 0)) != 0;) {
336 struct vnode *vp;
337 int n;
338
339 /* count #vnodes held on mount list */
340 n = 0;
341 TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes)
342 n++;
343
344 /* if this is unchanged then stop */
345 if (n == freeing)
346 break;
347
348 /* otherwise try once more time */
349 freeing = n;
350 }
351
352 /*
353 * Ok, now that we've tried doing it gently, get out the hammer.
354 */
355
356 if (mntflags & MNT_FORCE)
357 error = vflush(mp, NULL, FORCECLOSE);
358
359 if (error)
360 return error;
361
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 kauth_cred_free(um->um_cred);
369 /*
370 * Finally, throw away the union_mount structure
371 */
372 free(mp->mnt_data, M_UFSMNT); /* XXX */
373 mp->mnt_data = 0;
374 return (0);
375 }
376
377 int
378 union_root(mp, vpp)
379 struct mount *mp;
380 struct vnode **vpp;
381 {
382 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
383 int error;
384
385 /*
386 * Return locked reference to root.
387 */
388 VREF(um->um_uppervp);
389 vn_lock(um->um_uppervp, LK_EXCLUSIVE | LK_RETRY);
390 if (um->um_lowervp)
391 VREF(um->um_lowervp);
392 error = union_allocvp(vpp, mp, NULL, NULL, NULL,
393 um->um_uppervp, um->um_lowervp, 1);
394
395 if (error) {
396 vput(um->um_uppervp);
397 if (um->um_lowervp)
398 vrele(um->um_lowervp);
399 }
400
401 return (error);
402 }
403
404 int
405 union_statvfs(mp, sbp)
406 struct mount *mp;
407 struct statvfs *sbp;
408 {
409 int error;
410 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
411 struct statvfs *sbuf = malloc(sizeof(*sbuf), M_TEMP, M_WAITOK | M_ZERO);
412 unsigned long lbsize;
413
414 #ifdef UNION_DIAGNOSTIC
415 printf("union_statvfs(mp = %p, lvp = %p, uvp = %p)\n", mp,
416 um->um_lowervp, um->um_uppervp);
417 #endif
418
419 if (um->um_lowervp) {
420 error = VFS_STATVFS(um->um_lowervp->v_mount, sbuf);
421 if (error)
422 goto done;
423 }
424
425 /* now copy across the "interesting" information and fake the rest */
426 lbsize = sbuf->f_bsize;
427 sbp->f_blocks = sbuf->f_blocks - sbuf->f_bfree;
428 sbp->f_files = sbuf->f_files - sbuf->f_ffree;
429
430 error = VFS_STATVFS(um->um_uppervp->v_mount, sbuf);
431 if (error)
432 goto done;
433
434 sbp->f_flag = sbuf->f_flag;
435 sbp->f_bsize = sbuf->f_bsize;
436 sbp->f_frsize = sbuf->f_frsize;
437 sbp->f_iosize = sbuf->f_iosize;
438
439 /*
440 * The "total" fields count total resources in all layers,
441 * the "free" fields count only those resources which are
442 * free in the upper layer (since only the upper layer
443 * is writable).
444 */
445
446 if (sbuf->f_bsize != lbsize)
447 sbp->f_blocks = sbp->f_blocks * lbsize / sbuf->f_bsize;
448 sbp->f_blocks += sbuf->f_blocks;
449 sbp->f_bfree = sbuf->f_bfree;
450 sbp->f_bavail = sbuf->f_bavail;
451 sbp->f_bresvd = sbuf->f_bresvd;
452 sbp->f_files += sbuf->f_files;
453 sbp->f_ffree = sbuf->f_ffree;
454 sbp->f_favail = sbuf->f_favail;
455 sbp->f_fresvd = sbuf->f_fresvd;
456
457 copy_statvfs_info(sbp, mp);
458 done:
459 free(sbuf, M_TEMP);
460 return error;
461 }
462
463 /*ARGSUSED*/
464 int
465 union_sync(struct mount *mp, int waitfor,
466 kauth_cred_t cred)
467 {
468
469 /*
470 * XXX - Assumes no data cached at union layer.
471 */
472 return (0);
473 }
474
475 /*ARGSUSED*/
476 int
477 union_vget(struct mount *mp, ino_t ino,
478 struct vnode **vpp)
479 {
480
481 return (EOPNOTSUPP);
482 }
483
484 static int
485 union_renamelock_enter(struct mount *mp)
486 {
487 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
488
489 /* Lock just the upper fs, where the action happens. */
490 return VFS_RENAMELOCK_ENTER(um->um_uppervp->v_mount);
491 }
492
493 static void
494 union_renamelock_exit(struct mount *mp)
495 {
496 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
497
498 VFS_RENAMELOCK_EXIT(um->um_uppervp->v_mount);
499 }
500
501 SYSCTL_SETUP(sysctl_vfs_union_setup, "sysctl vfs.union subtree setup")
502 {
503
504 sysctl_createv(clog, 0, NULL, NULL,
505 CTLFLAG_PERMANENT,
506 CTLTYPE_NODE, "vfs", NULL,
507 NULL, 0, NULL, 0,
508 CTL_VFS, CTL_EOL);
509 sysctl_createv(clog, 0, NULL, NULL,
510 CTLFLAG_PERMANENT,
511 CTLTYPE_NODE, "union",
512 SYSCTL_DESCR("Union file system"),
513 NULL, 0, NULL, 0,
514 CTL_VFS, 15, CTL_EOL);
515 /*
516 * XXX the "15" above could be dynamic, thereby eliminating
517 * one more instance of the "number to vfs" mapping problem,
518 * but "15" is the order as taken from sys/mount.h
519 */
520 }
521
522 extern const struct vnodeopv_desc union_vnodeop_opv_desc;
523
524 const struct vnodeopv_desc * const union_vnodeopv_descs[] = {
525 &union_vnodeop_opv_desc,
526 NULL,
527 };
528
529 struct vfsops union_vfsops = {
530 MOUNT_UNION,
531 sizeof (struct union_args),
532 union_mount,
533 union_start,
534 union_unmount,
535 union_root,
536 (void *)eopnotsupp, /* vfs_quotactl */
537 union_statvfs,
538 union_sync,
539 union_vget,
540 (void *)eopnotsupp, /* vfs_fhtovp */
541 (void *)eopnotsupp, /* vfs_vptofh */
542 union_init,
543 NULL, /* vfs_reinit */
544 union_done,
545 NULL, /* vfs_mountroot */
546 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
547 vfs_stdextattrctl,
548 (void *)eopnotsupp, /* vfs_suspendctl */
549 union_renamelock_enter,
550 union_renamelock_exit,
551 union_vnodeopv_descs,
552 0, /* vfs_refcount */
553 { NULL, NULL },
554 };
555 VFS_ATTACH(union_vfsops);
556