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