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