union_vfsops.c revision 1.73.2.1 1 /* $NetBSD: union_vfsops.c,v 1.73.2.1 2015/04/06 15:18:19 skrll 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.73.2.1 2015/04/06 15:18:19 skrll 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 static struct sysctllog *union_sysctl_log;
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 int error = 0;
111 struct union_args *args = data;
112 struct vnode *lowerrootvp = NULLVP;
113 struct vnode *upperrootvp = NULLVP;
114 struct union_mount *um = 0;
115 const char *cp;
116 char *xp;
117 int len;
118 size_t size;
119
120 if (args == NULL)
121 return EINVAL;
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 error = namei_simple_user(args->target,
158 NSM_FOLLOW_NOEMULROOT, &upperrootvp);
159 if (error != 0)
160 goto bad;
161
162 if (upperrootvp->v_type != VDIR) {
163 error = EINVAL;
164 goto bad;
165 }
166
167 um = kmem_zalloc(sizeof(struct union_mount), KM_SLEEP);
168
169 /*
170 * Keep a held reference to the target vnodes.
171 * They are vrele'd in union_unmount.
172 *
173 * Depending on the _BELOW flag, the filesystems are
174 * viewed in a different order. In effect, this is the
175 * same as providing a mount under option to the mount syscall.
176 */
177
178 um->um_op = args->mntflags & UNMNT_OPMASK;
179 switch (um->um_op) {
180 case UNMNT_ABOVE:
181 um->um_lowervp = lowerrootvp;
182 um->um_uppervp = upperrootvp;
183 break;
184
185 case UNMNT_BELOW:
186 um->um_lowervp = upperrootvp;
187 um->um_uppervp = lowerrootvp;
188 break;
189
190 case UNMNT_REPLACE:
191 vrele(lowerrootvp);
192 lowerrootvp = NULLVP;
193 um->um_uppervp = upperrootvp;
194 um->um_lowervp = lowerrootvp;
195 break;
196
197 default:
198 error = EINVAL;
199 goto bad;
200 }
201
202 mp->mnt_iflag |= IMNT_MPSAFE;
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 vn_lock(um->um_uppervp, LK_EXCLUSIVE | LK_RETRY);
210 error = VOP_WHITEOUT(um->um_uppervp,
211 (struct componentname *) 0, LOOKUP);
212 VOP_UNLOCK(um->um_uppervp);
213 if (error)
214 goto bad;
215 }
216
217 um->um_cred = l->l_cred;
218 kauth_cred_hold(um->um_cred);
219 um->um_cmode = UN_DIRMODE &~ l->l_proc->p_cwdi->cwdi_cmask;
220
221 /*
222 * Depending on what you think the MNT_LOCAL flag might mean,
223 * you may want the && to be || on the conditional below.
224 * At the moment it has been defined that the filesystem is
225 * only local if it is all local, ie the MNT_LOCAL flag implies
226 * that the entire namespace is local. If you think the MNT_LOCAL
227 * flag implies that some of the files might be stored locally
228 * then you will want to change the conditional.
229 */
230 if (um->um_op == UNMNT_ABOVE) {
231 if (((um->um_lowervp == NULLVP) ||
232 (um->um_lowervp->v_mount->mnt_flag & MNT_LOCAL)) &&
233 (um->um_uppervp->v_mount->mnt_flag & MNT_LOCAL))
234 mp->mnt_flag |= MNT_LOCAL;
235 }
236
237 /*
238 * Copy in the upper layer's RDONLY flag. This is for the benefit
239 * of lookup() which explicitly checks the flag, rather than asking
240 * the filesystem for its own opinion. This means, that an update
241 * mount of the underlying filesystem to go from rdonly to rdwr
242 * will leave the unioned view as read-only.
243 */
244 mp->mnt_flag |= (um->um_uppervp->v_mount->mnt_flag & MNT_RDONLY);
245
246 mp->mnt_data = um;
247 vfs_getnewfsid(mp);
248
249 error = set_statvfs_info( path, UIO_USERSPACE, NULL, UIO_USERSPACE,
250 mp->mnt_op->vfs_name, mp, l);
251 if (error)
252 goto bad;
253
254 switch (um->um_op) {
255 case UNMNT_ABOVE:
256 cp = "<above>:";
257 break;
258 case UNMNT_BELOW:
259 cp = "<below>:";
260 break;
261 case UNMNT_REPLACE:
262 cp = "";
263 break;
264 default:
265 cp = "<invalid>:";
266 #ifdef DIAGNOSTIC
267 panic("union_mount: bad um_op");
268 #endif
269 break;
270 }
271 len = strlen(cp);
272 memcpy(mp->mnt_stat.f_mntfromname, cp, len);
273
274 xp = mp->mnt_stat.f_mntfromname + len;
275 len = MNAMELEN - len;
276
277 (void) copyinstr(args->target, xp, len - 1, &size);
278 memset(xp + size, 0, len - size);
279
280 #ifdef UNION_DIAGNOSTIC
281 printf("union_mount: from %s, on %s\n",
282 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
283 #endif
284
285 /* Setup the readdir hook if it's not set already */
286 if (!vn_union_readdir_hook)
287 vn_union_readdir_hook = union_readdirhook;
288
289 return (0);
290
291 bad:
292 if (um)
293 kmem_free(um, sizeof(struct union_mount));
294 if (upperrootvp)
295 vrele(upperrootvp);
296 if (lowerrootvp)
297 vrele(lowerrootvp);
298 return (error);
299 }
300
301 /*
302 * VFS start. Nothing needed here - the start routine
303 * on the underlying filesystem(s) will have been called
304 * when that filesystem was mounted.
305 */
306 /*ARGSUSED*/
307 int
308 union_start(struct mount *mp, int flags)
309 {
310
311 return (0);
312 }
313
314 /*
315 * Free reference to union layer
316 */
317 int
318 union_unmount(struct mount *mp, int mntflags)
319 {
320 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
321 int freeing;
322 int error;
323
324 #ifdef UNION_DIAGNOSTIC
325 printf("union_unmount(mp = %p)\n", mp);
326 #endif
327
328 /*
329 * Keep flushing vnodes from the mount list.
330 * This is needed because of the un_pvp held
331 * reference to the parent vnode.
332 * If more vnodes have been freed on a given pass,
333 * the try again. The loop will iterate at most
334 * (d) times, where (d) is the maximum tree depth
335 * in the filesystem.
336 */
337 for (freeing = 0; (error = vflush(mp, NULL, 0)) != 0;) {
338 struct vnode *vp;
339 int n;
340
341 /* count #vnodes held on mount list */
342 n = 0;
343 TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes)
344 n++;
345
346 /* if this is unchanged then stop */
347 if (n == freeing)
348 break;
349
350 /* otherwise try once more time */
351 freeing = n;
352 }
353
354 /*
355 * Ok, now that we've tried doing it gently, get out the hammer.
356 */
357
358 if (mntflags & MNT_FORCE)
359 error = vflush(mp, NULL, FORCECLOSE);
360
361 if (error)
362 return error;
363
364 /*
365 * Discard references to upper and lower target vnodes.
366 */
367 if (um->um_lowervp)
368 vrele(um->um_lowervp);
369 vrele(um->um_uppervp);
370 kauth_cred_free(um->um_cred);
371 /*
372 * Finally, throw away the union_mount structure
373 */
374 kmem_free(um, sizeof(struct union_mount));
375 mp->mnt_data = NULL;
376 return 0;
377 }
378
379 int
380 union_root(struct mount *mp, 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 if (um->um_lowervp)
390 vref(um->um_lowervp);
391 error = union_allocvp(vpp, mp, NULL, NULL, NULL,
392 um->um_uppervp, um->um_lowervp, 1);
393
394 if (error) {
395 vrele(um->um_uppervp);
396 if (um->um_lowervp)
397 vrele(um->um_lowervp);
398 return error;
399 }
400
401 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
402
403 return 0;
404 }
405
406 int
407 union_statvfs(struct mount *mp, 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 extern const struct vnodeopv_desc union_vnodeop_opv_desc;
502
503 const struct vnodeopv_desc * const union_vnodeopv_descs[] = {
504 &union_vnodeop_opv_desc,
505 NULL,
506 };
507
508 struct vfsops union_vfsops = {
509 .vfs_name = MOUNT_UNION,
510 .vfs_min_mount_data = sizeof (struct union_args),
511 .vfs_mount = union_mount,
512 .vfs_start = union_start,
513 .vfs_unmount = union_unmount,
514 .vfs_root = union_root,
515 .vfs_quotactl = (void *)eopnotsupp,
516 .vfs_statvfs = union_statvfs,
517 .vfs_sync = union_sync,
518 .vfs_vget = union_vget,
519 .vfs_loadvnode = union_loadvnode,
520 .vfs_fhtovp = (void *)eopnotsupp,
521 .vfs_vptofh = (void *)eopnotsupp,
522 .vfs_init = union_init,
523 .vfs_reinit = union_reinit,
524 .vfs_done = union_done,
525 .vfs_snapshot = (void *)eopnotsupp,
526 .vfs_extattrctl = vfs_stdextattrctl,
527 .vfs_suspendctl = (void *)eopnotsupp,
528 .vfs_renamelock_enter = union_renamelock_enter,
529 .vfs_renamelock_exit = union_renamelock_exit,
530 .vfs_fsync = (void *)eopnotsupp,
531 .vfs_opv_descs = union_vnodeopv_descs
532 };
533
534 static int
535 union_modcmd(modcmd_t cmd, void *arg)
536 {
537 int error;
538
539 switch (cmd) {
540 case MODULE_CMD_INIT:
541 error = vfs_attach(&union_vfsops);
542 if (error != 0)
543 break;
544 sysctl_createv(&union_sysctl_log, 0, NULL, NULL,
545 CTLFLAG_PERMANENT,
546 CTLTYPE_NODE, "union",
547 SYSCTL_DESCR("Union file system"),
548 NULL, 0, NULL, 0,
549 CTL_VFS, 15, CTL_EOL);
550 /*
551 * XXX the "15" above could be dynamic, thereby eliminating
552 * one more instance of the "number to vfs" mapping problem,
553 * but "15" is the order as taken from sys/mount.h
554 */
555 break;
556 case MODULE_CMD_FINI:
557 error = vfs_detach(&union_vfsops);
558 if (error != 0)
559 break;
560 sysctl_teardown(&union_sysctl_log);
561 break;
562 default:
563 error = ENOTTY;
564 break;
565 }
566
567 return (error);
568 }
569