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