2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1994, 1995 The Regents of the University of California.
31 * Copyright (c) 1994, 1995 Jan-Simon Pendry.
32 * All rights reserved.
34 * This code is derived from software donated to Berkeley by
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * @(#)union_vfsops.c 8.20 (Berkeley) 5/20/95
72 #include <sys/param.h>
73 #include <sys/systm.h>
75 #include <sys/types.h>
76 #include <sys/proc_internal.h>
77 #include <sys/kauth.h>
78 #include <sys/vnode_internal.h>
79 #include <sys/mount_internal.h>
80 #include <sys/namei.h>
81 #include <sys/malloc.h>
82 #include <sys/filedesc.h>
83 #include <sys/queue.h>
84 #include <miscfs/union/union.h>
86 static int union_itercallback(vnode_t
, void *);
87 static int union_root(mount_t
, vnode_t
*, vfs_context_t
);
90 * Mount union filesystem
93 union_mount(mount_t mp
, __unused vnode_t devvp
, user_addr_t data
, vfs_context_t context
)
95 proc_t p
= vfs_context_proc(context
);
97 struct user_union_args args
;
98 struct vnode
*lowerrootvp
= NULLVP
;
99 struct vnode
*upperrootvp
= NULLVP
;
100 struct union_mount
*um
= NULL
;
101 kauth_cred_t cred
= NOCRED
;
102 const char *cp
= NULL
;
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 MALLOC(um
, struct union_mount
*, sizeof(struct union_mount
),
167 * Keep a held reference to the target vnodes.
168 * They are vnode_put'd in union_unmount.
170 * Depending on the _BELOW flag, the filesystems are
171 * viewed in a different order. In effect, this is the
172 * same as providing a mount under option to the mount syscall.
175 um
->um_op
= args
.mntflags
& UNMNT_OPMASK
;
178 um
->um_lowervp
= lowerrootvp
;
179 um
->um_uppervp
= upperrootvp
;
183 um
->um_lowervp
= upperrootvp
;
184 um
->um_uppervp
= lowerrootvp
;
188 vnode_put(lowerrootvp
);
189 lowerrootvp
= NULLVP
;
190 um
->um_uppervp
= upperrootvp
;
191 um
->um_lowervp
= lowerrootvp
;
196 um
->um_lowervp
= upperrootvp
;
197 um
->um_uppervp
= lowerrootvp
;
206 if (um
->um_lowervp
!= NULLVP
)
207 um
->um_lowervid
= vnode_vid(um
->um_lowervp
);
208 if (um
->um_uppervp
!= NULLVP
)
209 um
->um_uppervid
= vnode_vid(um
->um_uppervp
);
211 * Unless the mount is readonly, ensure that the top layer
212 * supports whiteout operations
215 if ((um
->um_op
!= UNMNT_FAULTIN
) && (mp
->mnt_flag
& MNT_RDONLY
) == 0)
217 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0)
220 error
= VNOP_WHITEOUT(um
->um_uppervp
, (struct componentname
*) 0,
226 um
->um_cred
= kauth_cred_get_with_ref();
227 um
->um_cmode
= UN_DIRMODE
&~ p
->p_fd
->fd_cmask
;
230 * Depending on what you think the MNT_LOCAL flag might mean,
231 * you may want the && to be || on the conditional below.
232 * At the moment it has been defined that the filesystem is
233 * only local if it is all local, ie the MNT_LOCAL flag implies
234 * that the entire namespace is local. If you think the MNT_LOCAL
235 * flag implies that some of the files might be stored locally
236 * then you will want to change the conditional.
238 if (um
->um_op
== UNMNT_ABOVE
) {
239 if (((um
->um_lowervp
== NULLVP
) ||
240 (um
->um_lowervp
->v_mount
->mnt_flag
& MNT_LOCAL
)) &&
241 (um
->um_uppervp
->v_mount
->mnt_flag
& MNT_LOCAL
))
242 mp
->mnt_flag
|= MNT_LOCAL
;
246 * Copy in the upper layer's RDONLY flag. This is for the benefit
247 * of lookup() which explicitly checks the flag, rather than asking
248 * the filesystem for it's own opinion. This means, that an update
249 * mount of the underlying filesystem to go from rdonly to rdwr
250 * will leave the unioned view as read-only.
252 mp
->mnt_flag
|= (um
->um_uppervp
->v_mount
->mnt_flag
& MNT_RDONLY
);
254 mp
->mnt_data
= (qaddr_t
) um
;
275 bcopy(cp
, mp
->mnt_vfsstat
.f_mntfromname
, len
);
277 vcp
= mp
->mnt_vfsstat
.f_mntfromname
+ len
;
278 len
= MNAMELEN
- len
;
280 (void) copyinstr(args
.target
, vcp
, len
- 1, (size_t *)&size
);
281 bzero(vcp
+ size
, len
- size
);
283 #ifdef UNION_DIAGNOSTIC
284 printf("union_mount: from %s, on %s\n",
285 mp
->mnt_vfsstat
.f_mntfromname
, mp
->mnt_vfsstat
.f_mntonname
);
292 if (IS_VALID_CRED(cred
))
293 kauth_cred_unref(&cred
);
295 vnode_put(upperrootvp
);
297 vnode_put(lowerrootvp
);
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.
307 union_start(__unused
struct mount
*mp
, __unused
int flags
, __unused vfs_context_t context
)
314 union_itercallback(__unused vnode_t vp
, void *args
)
316 int num
= *(int *)args
;
318 *(int *)args
= num
+ 1;
319 return(VNODE_RETURNED
);
325 * Free reference to union layer
328 union_unmount(mount_t mp
, int mntflags
, vfs_context_t context
)
330 struct union_mount
*um
= MOUNTTOUNIONMOUNT(mp
);
331 struct vnode
*um_rootvp
;
336 #ifdef UNION_DIAGNOSTIC
337 printf("union_unmount(mp = %x)\n", mp
);
340 if (mntflags
& MNT_FORCE
)
343 if ((error
= union_root(mp
, &um_rootvp
, context
)))
347 * Keep flushing vnodes from the mount list.
348 * This is needed because of the un_pvp held
349 * reference to the parent vnode.
350 * If more vnodes have been freed on a given pass,
351 * the try again. The loop will iterate at most
352 * (d) times, where (d) is the maximum tree depth
355 for (freeing
= 0; vflush(mp
, um_rootvp
, flags
) != 0;) {
358 vnode_iterate(mp
, VNODE_NOLOCK_INTERNAL
, union_itercallback
, &n
);
360 /* if this is unchanged then stop */
364 /* otherwise try once more time */
368 /* At this point the root vnode should have a single reference */
369 if (vnode_isinuse(um_rootvp
, 0)) {
370 vnode_put(um_rootvp
);
374 #ifdef UNION_DIAGNOSTIC
375 vprint("union root", um_rootvp
);
378 * Discard references to upper and lower target vnodes.
381 vnode_put(um
->um_lowervp
);
382 vnode_put(um
->um_uppervp
);
383 if (IS_VALID_CRED(um
->um_cred
)) {
384 kauth_cred_unref(&um
->um_cred
);
387 * Release reference on underlying root vnode
389 vnode_put(um_rootvp
);
391 * And blow it away for future re-use
393 vnode_reclaim(um_rootvp
);
395 * Finally, throw away the union_mount structure
397 _FREE(mp
->mnt_data
, M_UFSMNT
); /* XXX */
403 union_root(mount_t mp
, vnode_t
*vpp
, __unused vfs_context_t context
)
405 struct union_mount
*um
= MOUNTTOUNIONMOUNT(mp
);
409 * Return locked reference to root.
411 vnode_get(um
->um_uppervp
);
413 vnode_get(um
->um_lowervp
);
416 error
= union_allocvp(vpp
, mp
,
419 (struct componentname
*) 0,
426 vnode_put(um
->um_uppervp
);
428 vnode_put(um
->um_lowervp
);
435 union_vfs_getattr(mount_t mp
, struct vfs_attr
*fsap
, vfs_context_t context
)
438 struct union_mount
*um
= MOUNTTOUNIONMOUNT(mp
);
439 struct vfs_attr attr
;
442 #ifdef UNION_DIAGNOSTIC
443 printf("union_vfs_getattr(mp = %x, lvp = %x, uvp = %x)\n", mp
,
448 /* Get values from lower file system (if any) */
449 if (um
->um_lowervp
) {
451 VFSATTR_WANTED(&attr
, f_bsize
);
452 VFSATTR_WANTED(&attr
, f_blocks
);
453 VFSATTR_WANTED(&attr
, f_bused
);
454 VFSATTR_WANTED(&attr
, f_files
);
455 error
= vfs_getattr(um
->um_lowervp
->v_mount
, &attr
, context
);
459 /* now copy across the "interesting" information and fake the rest */
460 if (VFSATTR_IS_SUPPORTED(&attr
, f_bsize
))
461 lbsize
= attr
.f_bsize
;
463 lbsize
= um
->um_lowervp
->v_mount
->mnt_devblocksize
;
464 fsap
->f_blocks
= VFSATTR_IS_SUPPORTED(&attr
, f_blocks
) ? attr
.f_blocks
: 0;
465 fsap
->f_bused
= VFSATTR_IS_SUPPORTED(&attr
, f_bused
) ? attr
.f_bused
: 0;
466 fsap
->f_files
= VFSATTR_IS_SUPPORTED(&attr
, f_files
) ? attr
.f_files
: 0;
474 VFSATTR_WANTED(&attr
, f_bsize
);
475 VFSATTR_WANTED(&attr
, f_blocks
);
476 VFSATTR_WANTED(&attr
, f_bfree
);
477 VFSATTR_WANTED(&attr
, f_bavail
);
478 VFSATTR_WANTED(&attr
, f_files
);
479 VFSATTR_WANTED(&attr
, f_ffree
);
480 error
= vfs_getattr(um
->um_uppervp
->v_mount
, &attr
, context
);
484 if (VFSATTR_IS_SUPPORTED(&attr
, f_bsize
)) {
485 fsap
->f_bsize
= attr
.f_bsize
;
486 VFSATTR_SET_SUPPORTED(fsap
, f_bsize
);
488 if (VFSATTR_IS_SUPPORTED(&attr
, f_iosize
)) {
489 fsap
->f_iosize
= attr
.f_iosize
;
490 VFSATTR_SET_SUPPORTED(fsap
, f_iosize
);
494 * if the lower and upper blocksizes differ, then frig the
495 * block counts so that the sizes reported by df make some
496 * kind of sense. none of this makes sense though.
498 if (VFSATTR_IS_SUPPORTED(&attr
, f_bsize
))
499 fsap
->f_bsize
= attr
.f_bsize
;
501 fsap
->f_bsize
= um
->um_uppervp
->v_mount
->mnt_devblocksize
;
502 VFSATTR_RETURN(fsap
, f_bsize
, attr
.f_bsize
);
503 if (fsap
->f_bsize
!= lbsize
)
504 fsap
->f_blocks
= fsap
->f_blocks
* lbsize
/ attr
.f_bsize
;
507 * The "total" fields count total resources in all layers,
508 * the "free" fields count only those resources which are
509 * free in the upper layer (since only the upper layer
512 if (VFSATTR_IS_SUPPORTED(&attr
, f_blocks
))
513 fsap
->f_blocks
+= attr
.f_blocks
;
514 if (VFSATTR_IS_SUPPORTED(&attr
, f_bfree
))
515 fsap
->f_bfree
= attr
.f_bfree
;
516 if (VFSATTR_IS_SUPPORTED(&attr
, f_bavail
))
517 fsap
->f_bavail
= attr
.f_bavail
;
518 if (VFSATTR_IS_SUPPORTED(&attr
, f_bused
))
519 fsap
->f_bused
+= attr
.f_bused
;
520 if (VFSATTR_IS_SUPPORTED(&attr
, f_files
))
521 fsap
->f_files
+= attr
.f_files
;
522 if (VFSATTR_IS_SUPPORTED(&attr
, f_ffree
))
523 fsap
->f_ffree
= attr
.f_ffree
;
525 VFSATTR_SET_SUPPORTED(fsap
, f_bsize
);
526 VFSATTR_SET_SUPPORTED(fsap
, f_blocks
);
527 VFSATTR_SET_SUPPORTED(fsap
, f_bfree
);
528 VFSATTR_SET_SUPPORTED(fsap
, f_bavail
);
529 VFSATTR_SET_SUPPORTED(fsap
, f_bused
);
530 VFSATTR_SET_SUPPORTED(fsap
, f_files
);
531 VFSATTR_SET_SUPPORTED(fsap
, f_ffree
);
537 * XXX - Assumes no data cached at union layer.
539 #define union_sync (int (*) (mount_t, int, vfs_context_t))nullop
541 #define union_fhtovp (int (*) (mount_t, int, unsigned char *, vnode_t *, vfs_context_t))eopnotsupp
542 #define union_sysctl (int (*) (int *, u_int, user_addr_t, size_t *, user_addr_t, size_t, vfs_context_t))eopnotsupp
543 #define union_vget (int (*) (mount_t, ino64_t, vnode_t *, vfs_context_t))eopnotsupp
544 #define union_vptofh (int (*) (vnode_t, int *, unsigned char *, vfs_context_t))eopnotsupp
546 struct vfsops union_vfsops
= {