2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
32 * Copyright (c) 1994, 1995 The Regents of the University of California.
33 * Copyright (c) 1994, 1995 Jan-Simon Pendry.
34 * All rights reserved.
36 * This code is derived from software donated to Berkeley by
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. All advertising materials mentioning features or use of this software
48 * must display the following acknowledgement:
49 * This product includes software developed by the University of
50 * California, Berkeley and its contributors.
51 * 4. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 * @(#)union_vfsops.c 8.20 (Berkeley) 5/20/95
74 #include <sys/param.h>
75 #include <sys/systm.h>
77 #include <sys/types.h>
78 #include <sys/proc_internal.h>
79 #include <sys/kauth.h>
80 #include <sys/vnode_internal.h>
81 #include <sys/mount_internal.h>
82 #include <sys/namei.h>
83 #include <sys/malloc.h>
84 #include <sys/filedesc.h>
85 #include <sys/queue.h>
86 #include <miscfs/union/union.h>
88 static int union_itercallback(__unused vnode_t
, void *);
91 * Mount union filesystem
94 union_mount(mount_t mp
, __unused vnode_t devvp
, user_addr_t data
, vfs_context_t context
)
96 proc_t p
= vfs_context_proc(context
);
98 struct user_union_args args
;
99 struct vnode
*lowerrootvp
= NULLVP
;
100 struct vnode
*upperrootvp
= NULLVP
;
101 struct union_mount
*um
= 0;
102 kauth_cred_t cred
= NOCRED
;
108 #ifdef UNION_DIAGNOSTIC
109 printf("union_mount(mp = %x)\n", mp
);
115 if (mp
->mnt_flag
& MNT_UPDATE
) {
118 * 1. a way to convert between rdonly and rdwr mounts.
119 * 2. support for nfs exports.
128 if (vfs_context_is64bit(context
)) {
129 error
= copyin(data
, (caddr_t
)&args
, sizeof(args
));
132 struct union_args temp
;
133 error
= copyin(data
, (caddr_t
)&temp
, sizeof (temp
));
134 args
.target
= CAST_USER_ADDR_T(temp
.target
);
135 args
.mntflags
= temp
.mntflags
;
140 lowerrootvp
= mp
->mnt_vnodecovered
;
141 vnode_get(lowerrootvp
);
146 NDINIT(&nd
, LOOKUP
, FOLLOW
|WANTPARENT
,
147 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
148 args
.target
, context
);
150 if ((error
= namei(&nd
)))
154 upperrootvp
= nd
.ni_vp
;
155 vnode_put(nd
.ni_dvp
);
158 if (upperrootvp
->v_type
!= VDIR
) {
163 // um = (struct union_mount *) malloc(sizeof(struct union_mount),
164 // M_UFSMNT, M_WAITOK); /* XXX */
165 MALLOC(um
, struct union_mount
*, sizeof(struct union_mount
),
169 * Keep a held reference to the target vnodes.
170 * They are vnode_put'd in union_unmount.
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.
177 um
->um_op
= args
.mntflags
& UNMNT_OPMASK
;
180 um
->um_lowervp
= lowerrootvp
;
181 um
->um_uppervp
= upperrootvp
;
185 um
->um_lowervp
= upperrootvp
;
186 um
->um_uppervp
= lowerrootvp
;
190 vnode_put(lowerrootvp
);
191 lowerrootvp
= NULLVP
;
192 um
->um_uppervp
= upperrootvp
;
193 um
->um_lowervp
= lowerrootvp
;
202 * Unless the mount is readonly, ensure that the top layer
203 * supports whiteout operations
205 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0) {
206 error
= VNOP_WHITEOUT(um
->um_uppervp
, (struct componentname
*) 0,
212 um
->um_cred
= kauth_cred_get_with_ref();
213 um
->um_cmode
= UN_DIRMODE
&~ p
->p_fd
->fd_cmask
;
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.
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
;
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.
238 mp
->mnt_flag
|= (um
->um_uppervp
->v_mount
->mnt_flag
& MNT_RDONLY
);
240 mp
->mnt_data
= (qaddr_t
) um
;
256 bcopy(cp
, mp
->mnt_vfsstat
.f_mntfromname
, len
);
258 cp
= mp
->mnt_vfsstat
.f_mntfromname
+ len
;
259 len
= MNAMELEN
- len
;
261 (void) copyinstr(args
.target
, cp
, len
- 1, (size_t *)&size
);
262 bzero(cp
+ size
, len
- size
);
264 #ifdef UNION_DIAGNOSTIC
265 printf("union_mount: from %s, on %s\n",
266 mp
->mnt_vfsstat
.f_mntfromname
, mp
->mnt_vfsstat
.f_mntonname
);
274 kauth_cred_rele(cred
);
276 vnode_put(upperrootvp
);
278 vnode_put(lowerrootvp
);
283 * VFS start. Nothing needed here - the start routine
284 * on the underlying filesystem(s) will have been called
285 * when that filesystem was mounted.
288 union_start(__unused
struct mount
*mp
, __unused
int flags
, __unused vfs_context_t context
)
295 union_itercallback(__unused vnode_t vp
, void *args
)
297 int num
= *(int *)args
;
299 *(int *)args
= num
+ 1;
300 return(VNODE_RETURNED
);
306 * Free reference to union layer
309 union_unmount(mount_t mp
, int mntflags
, __unused vfs_context_t context
)
311 struct union_mount
*um
= MOUNTTOUNIONMOUNT(mp
);
312 struct vnode
*um_rootvp
;
318 #ifdef UNION_DIAGNOSTIC
319 printf("union_unmount(mp = %x)\n", mp
);
322 if (mntflags
& MNT_FORCE
)
325 if ((error
= union_root(mp
, &um_rootvp
)))
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
337 for (freeing
= 0; vflush(mp
, um_rootvp
, flags
) != 0;) {
340 vnode_iterate(mp
, VNODE_NOLOCK_INTERNAL
, union_itercallback
, &n
);
342 /* if this is unchanged then stop */
346 /* otherwise try once more time */
350 /* At this point the root vnode should have a single reference */
351 if (vnode_isinuse(um_rootvp
, 0)) {
352 vnode_put(um_rootvp
);
356 #ifdef UNION_DIAGNOSTIC
357 vprint("union root", um_rootvp
);
360 * Discard references to upper and lower target vnodes.
363 vnode_put(um
->um_lowervp
);
364 vnode_put(um
->um_uppervp
);
366 if (cred
!= NOCRED
) {
367 um
->um_cred
= NOCRED
;
368 kauth_cred_rele(cred
);
371 * Release reference on underlying root vnode
373 vnode_put(um_rootvp
);
375 * And blow it away for future re-use
377 vnode_reclaim(um_rootvp
);
379 * Finally, throw away the union_mount structure
381 _FREE(mp
->mnt_data
, M_UFSMNT
); /* XXX */
387 union_root(mount_t mp
, vnode_t
*vpp
, __unused vfs_context_t context
)
389 struct union_mount
*um
= MOUNTTOUNIONMOUNT(mp
);
393 * Return locked reference to root.
395 vnode_get(um
->um_uppervp
);
397 vnode_get(um
->um_lowervp
);
398 error
= union_allocvp(vpp
, mp
,
401 (struct componentname
*) 0,
407 vnode_put(um
->um_uppervp
);
409 vnode_put(um
->um_lowervp
);
416 union_vfs_getattr(mount_t mp
, struct vfs_attr
*fsap
, vfs_context_t context
)
419 struct union_mount
*um
= MOUNTTOUNIONMOUNT(mp
);
420 struct vfs_attr attr
;
423 #ifdef UNION_DIAGNOSTIC
424 printf("union_vfs_getattr(mp = %x, lvp = %x, uvp = %x)\n", mp
,
429 /* Get values from lower file system (if any) */
430 if (um
->um_lowervp
) {
432 VFSATTR_WANTED(&attr
, f_bsize
);
433 VFSATTR_WANTED(&attr
, f_blocks
);
434 VFSATTR_WANTED(&attr
, f_bused
);
435 VFSATTR_WANTED(&attr
, f_files
);
436 error
= vfs_getattr(um
->um_lowervp
->v_mount
, &attr
, context
);
440 /* now copy across the "interesting" information and fake the rest */
441 if (VFSATTR_IS_SUPPORTED(&attr
, f_bsize
))
442 lbsize
= attr
.f_bsize
;
444 lbsize
= um
->um_lowervp
->v_mount
->mnt_devblocksize
;
445 fsap
->f_blocks
= VFSATTR_IS_SUPPORTED(&attr
, f_blocks
) ? attr
.f_blocks
: 0;
446 fsap
->f_bused
= VFSATTR_IS_SUPPORTED(&attr
, f_bused
) ? attr
.f_bused
: 0;
447 fsap
->f_files
= VFSATTR_IS_SUPPORTED(&attr
, f_files
) ? attr
.f_files
: 0;
455 VFSATTR_WANTED(&attr
, f_bsize
);
456 VFSATTR_WANTED(&attr
, f_blocks
);
457 VFSATTR_WANTED(&attr
, f_bfree
);
458 VFSATTR_WANTED(&attr
, f_bavail
);
459 VFSATTR_WANTED(&attr
, f_files
);
460 VFSATTR_WANTED(&attr
, f_ffree
);
461 error
= vfs_getattr(um
->um_uppervp
->v_mount
, &attr
, context
);
465 if (VFSATTR_IS_SUPPORTED(&attr
, f_bsize
)) {
466 fsap
->f_bsize
= attr
.f_bsize
;
467 VFSATTR_SET_SUPPORTED(fsap
, f_bsize
);
469 if (VFSATTR_IS_SUPPORTED(&attr
, f_iosize
)) {
470 fsap
->f_iosize
= attr
.f_iosize
;
471 VFSATTR_SET_SUPPORTED(fsap
, f_iosize
);
475 * if the lower and upper blocksizes differ, then frig the
476 * block counts so that the sizes reported by df make some
477 * kind of sense. none of this makes sense though.
479 if (VFSATTR_IS_SUPPORTED(&attr
, f_bsize
))
480 fsap
->f_bsize
= attr
.f_bsize
;
482 fsap
->f_bsize
= um
->um_uppervp
->v_mount
->mnt_devblocksize
;
483 VFSATTR_RETURN(fsap
, f_bsize
, attr
.f_bsize
);
484 if (fsap
->f_bsize
!= lbsize
)
485 fsap
->f_blocks
= fsap
->f_blocks
* lbsize
/ attr
.f_bsize
;
488 * The "total" fields count total resources in all layers,
489 * the "free" fields count only those resources which are
490 * free in the upper layer (since only the upper layer
493 if (VFSATTR_IS_SUPPORTED(&attr
, f_blocks
))
494 fsap
->f_blocks
+= attr
.f_blocks
;
495 if (VFSATTR_IS_SUPPORTED(&attr
, f_bfree
))
496 fsap
->f_bfree
= attr
.f_bfree
;
497 if (VFSATTR_IS_SUPPORTED(&attr
, f_bavail
))
498 fsap
->f_bavail
= attr
.f_bavail
;
499 if (VFSATTR_IS_SUPPORTED(&attr
, f_bused
))
500 fsap
->f_bused
+= attr
.f_bused
;
501 if (VFSATTR_IS_SUPPORTED(&attr
, f_files
))
502 fsap
->f_files
+= attr
.f_files
;
503 if (VFSATTR_IS_SUPPORTED(&attr
, f_ffree
))
504 fsap
->f_ffree
= attr
.f_ffree
;
506 VFSATTR_SET_SUPPORTED(fsap
, f_bsize
);
507 VFSATTR_SET_SUPPORTED(fsap
, f_blocks
);
508 VFSATTR_SET_SUPPORTED(fsap
, f_bfree
);
509 VFSATTR_SET_SUPPORTED(fsap
, f_bavail
);
510 VFSATTR_SET_SUPPORTED(fsap
, f_bused
);
511 VFSATTR_SET_SUPPORTED(fsap
, f_files
);
512 VFSATTR_SET_SUPPORTED(fsap
, f_ffree
);
518 * XXX - Assumes no data cached at union layer.
520 #define union_sync (int (*) (mount_t, int, ucred_t, vfs_context_t))nullop
522 #define union_fhtovp (int (*) (mount_t, int, unsigned char *, vnode_t *, vfs_context_t))eopnotsupp
523 int union_init (struct vfsconf
*);
524 #define union_sysctl (int (*) (int *, u_int, user_addr_t, size_t *, user_addr_t, size_t, vfs_context_t))eopnotsupp
525 #define union_vget (int (*) (mount_t, ino64_t, vnode_t *, vfs_context_t))eopnotsupp
526 #define union_vptofh (int (*) (vnode_t, int *, unsigned char *, vfs_context_t))eopnotsupp
528 struct vfsops union_vfsops
= {