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 /* mark the filesystem thred safe */
284 mp
->mnt_vtable
->vfc_threadsafe
= TRUE
;
286 #ifdef UNION_DIAGNOSTIC
287 printf("union_mount: from %s, on %s\n",
288 mp
->mnt_vfsstat
.f_mntfromname
, mp
->mnt_vfsstat
.f_mntonname
);
295 if (IS_VALID_CRED(cred
))
296 kauth_cred_unref(&cred
);
298 vnode_put(upperrootvp
);
300 vnode_put(lowerrootvp
);
305 * VFS start. Nothing needed here - the start routine
306 * on the underlying filesystem(s) will have been called
307 * when that filesystem was mounted.
310 union_start(__unused
struct mount
*mp
, __unused
int flags
, __unused vfs_context_t context
)
317 union_itercallback(__unused vnode_t vp
, void *args
)
319 int num
= *(int *)args
;
321 *(int *)args
= num
+ 1;
322 return(VNODE_RETURNED
);
328 * Free reference to union layer
331 union_unmount(mount_t mp
, int mntflags
, vfs_context_t context
)
333 struct union_mount
*um
= MOUNTTOUNIONMOUNT(mp
);
334 struct vnode
*um_rootvp
;
339 #ifdef UNION_DIAGNOSTIC
340 printf("union_unmount(mp = %x)\n", mp
);
343 if (mntflags
& MNT_FORCE
)
346 if ((error
= union_root(mp
, &um_rootvp
, context
)))
350 * Keep flushing vnodes from the mount list.
351 * This is needed because of the un_pvp held
352 * reference to the parent vnode.
353 * If more vnodes have been freed on a given pass,
354 * the try again. The loop will iterate at most
355 * (d) times, where (d) is the maximum tree depth
358 for (freeing
= 0; vflush(mp
, um_rootvp
, flags
) != 0;) {
361 vnode_iterate(mp
, VNODE_NOLOCK_INTERNAL
, union_itercallback
, &n
);
363 /* if this is unchanged then stop */
367 /* otherwise try once more time */
371 /* At this point the root vnode should have a single reference */
372 if (vnode_isinuse(um_rootvp
, 0)) {
373 vnode_put(um_rootvp
);
377 #ifdef UNION_DIAGNOSTIC
378 vprint("union root", um_rootvp
);
381 * Discard references to upper and lower target vnodes.
384 vnode_put(um
->um_lowervp
);
385 vnode_put(um
->um_uppervp
);
386 if (IS_VALID_CRED(um
->um_cred
)) {
387 kauth_cred_unref(&um
->um_cred
);
390 * Release reference on underlying root vnode
392 vnode_put(um_rootvp
);
394 * And blow it away for future re-use
396 vnode_reclaim(um_rootvp
);
398 * Finally, throw away the union_mount structure
400 _FREE(mp
->mnt_data
, M_UFSMNT
); /* XXX */
406 union_root(mount_t mp
, vnode_t
*vpp
, __unused vfs_context_t context
)
408 struct union_mount
*um
= MOUNTTOUNIONMOUNT(mp
);
412 * Return locked reference to root.
414 vnode_get(um
->um_uppervp
);
416 vnode_get(um
->um_lowervp
);
419 error
= union_allocvp(vpp
, mp
,
422 (struct componentname
*) 0,
429 vnode_put(um
->um_uppervp
);
431 vnode_put(um
->um_lowervp
);
438 union_vfs_getattr(mount_t mp
, struct vfs_attr
*fsap
, vfs_context_t context
)
441 struct union_mount
*um
= MOUNTTOUNIONMOUNT(mp
);
442 struct vfs_attr attr
;
445 #ifdef UNION_DIAGNOSTIC
446 printf("union_vfs_getattr(mp = %x, lvp = %x, uvp = %x)\n", mp
,
451 /* Get values from lower file system (if any) */
452 if (um
->um_lowervp
) {
454 VFSATTR_WANTED(&attr
, f_bsize
);
455 VFSATTR_WANTED(&attr
, f_blocks
);
456 VFSATTR_WANTED(&attr
, f_bused
);
457 VFSATTR_WANTED(&attr
, f_files
);
458 error
= vfs_getattr(um
->um_lowervp
->v_mount
, &attr
, context
);
462 /* now copy across the "interesting" information and fake the rest */
463 if (VFSATTR_IS_SUPPORTED(&attr
, f_bsize
))
464 lbsize
= attr
.f_bsize
;
466 lbsize
= um
->um_lowervp
->v_mount
->mnt_devblocksize
;
467 fsap
->f_blocks
= VFSATTR_IS_SUPPORTED(&attr
, f_blocks
) ? attr
.f_blocks
: 0;
468 fsap
->f_bused
= VFSATTR_IS_SUPPORTED(&attr
, f_bused
) ? attr
.f_bused
: 0;
469 fsap
->f_files
= VFSATTR_IS_SUPPORTED(&attr
, f_files
) ? attr
.f_files
: 0;
477 VFSATTR_WANTED(&attr
, f_bsize
);
478 VFSATTR_WANTED(&attr
, f_blocks
);
479 VFSATTR_WANTED(&attr
, f_bfree
);
480 VFSATTR_WANTED(&attr
, f_bavail
);
481 VFSATTR_WANTED(&attr
, f_files
);
482 VFSATTR_WANTED(&attr
, f_ffree
);
483 error
= vfs_getattr(um
->um_uppervp
->v_mount
, &attr
, context
);
487 if (VFSATTR_IS_SUPPORTED(&attr
, f_bsize
)) {
488 fsap
->f_bsize
= attr
.f_bsize
;
489 VFSATTR_SET_SUPPORTED(fsap
, f_bsize
);
491 if (VFSATTR_IS_SUPPORTED(&attr
, f_iosize
)) {
492 fsap
->f_iosize
= attr
.f_iosize
;
493 VFSATTR_SET_SUPPORTED(fsap
, f_iosize
);
497 * if the lower and upper blocksizes differ, then frig the
498 * block counts so that the sizes reported by df make some
499 * kind of sense. none of this makes sense though.
501 if (VFSATTR_IS_SUPPORTED(&attr
, f_bsize
))
502 fsap
->f_bsize
= attr
.f_bsize
;
504 fsap
->f_bsize
= um
->um_uppervp
->v_mount
->mnt_devblocksize
;
505 VFSATTR_RETURN(fsap
, f_bsize
, attr
.f_bsize
);
506 if (fsap
->f_bsize
!= lbsize
)
507 fsap
->f_blocks
= fsap
->f_blocks
* lbsize
/ attr
.f_bsize
;
510 * The "total" fields count total resources in all layers,
511 * the "free" fields count only those resources which are
512 * free in the upper layer (since only the upper layer
515 if (VFSATTR_IS_SUPPORTED(&attr
, f_blocks
))
516 fsap
->f_blocks
+= attr
.f_blocks
;
517 if (VFSATTR_IS_SUPPORTED(&attr
, f_bfree
))
518 fsap
->f_bfree
= attr
.f_bfree
;
519 if (VFSATTR_IS_SUPPORTED(&attr
, f_bavail
))
520 fsap
->f_bavail
= attr
.f_bavail
;
521 if (VFSATTR_IS_SUPPORTED(&attr
, f_bused
))
522 fsap
->f_bused
+= attr
.f_bused
;
523 if (VFSATTR_IS_SUPPORTED(&attr
, f_files
))
524 fsap
->f_files
+= attr
.f_files
;
525 if (VFSATTR_IS_SUPPORTED(&attr
, f_ffree
))
526 fsap
->f_ffree
= attr
.f_ffree
;
528 VFSATTR_SET_SUPPORTED(fsap
, f_bsize
);
529 VFSATTR_SET_SUPPORTED(fsap
, f_blocks
);
530 VFSATTR_SET_SUPPORTED(fsap
, f_bfree
);
531 VFSATTR_SET_SUPPORTED(fsap
, f_bavail
);
532 VFSATTR_SET_SUPPORTED(fsap
, f_bused
);
533 VFSATTR_SET_SUPPORTED(fsap
, f_files
);
534 VFSATTR_SET_SUPPORTED(fsap
, f_ffree
);
540 * XXX - Assumes no data cached at union layer.
542 #define union_sync (int (*) (mount_t, int, vfs_context_t))nullop
544 #define union_fhtovp (int (*) (mount_t, int, unsigned char *, vnode_t *, vfs_context_t))eopnotsupp
545 #define union_sysctl (int (*) (int *, u_int, user_addr_t, size_t *, user_addr_t, size_t, vfs_context_t))eopnotsupp
546 #define union_vget (int (*) (mount_t, ino64_t, vnode_t *, vfs_context_t))eopnotsupp
547 #define union_vptofh (int (*) (vnode_t, int *, unsigned char *, vfs_context_t))eopnotsupp
549 struct vfsops union_vfsops
= {