]>
git.saurik.com Git - apple/xnu.git/blob - bsd/miscfs/union/union_vfsops.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
27 * Copyright (c) 1994, 1995 The Regents of the University of California.
28 * Copyright (c) 1994, 1995 Jan-Simon Pendry.
29 * All rights reserved.
31 * This code is derived from software donated to Berkeley by
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * @(#)union_vfsops.c 8.20 (Berkeley) 5/20/95
69 #include <sys/param.h>
70 #include <sys/systm.h>
72 #include <sys/types.h>
74 #include <sys/vnode.h>
75 #include <sys/mount.h>
76 #include <sys/namei.h>
77 #include <sys/malloc.h>
78 #include <sys/filedesc.h>
79 #include <sys/queue.h>
80 #include <miscfs/union/union.h>
83 * Mount union filesystem
86 union_mount(mp
, path
, data
, ndp
, p
)
90 struct nameidata
*ndp
;
94 struct union_args args
;
95 struct vnode
*lowerrootvp
= NULLVP
;
96 struct vnode
*upperrootvp
= NULLVP
;
97 struct union_mount
*um
= 0;
98 struct ucred
*cred
= 0;
105 #ifdef UNION_DIAGNOSTIC
106 printf("union_mount(mp = %x)\n", mp
);
112 if (mp
->mnt_flag
& MNT_UPDATE
) {
115 * 1. a way to convert between rdonly and rdwr mounts.
116 * 2. support for nfs exports.
125 if (error
= copyin(data
, (caddr_t
)&args
, sizeof(struct union_args
)))
128 lowerrootvp
= mp
->mnt_vnodecovered
;
134 NDINIT(ndp
, LOOKUP
, FOLLOW
|WANTPARENT
,
135 UIO_USERSPACE
, args
.target
, p
);
137 if (error
= namei(ndp
))
140 upperrootvp
= ndp
->ni_vp
;
144 if (upperrootvp
->v_type
!= VDIR
) {
149 // um = (struct union_mount *) malloc(sizeof(struct union_mount),
150 // M_UFSMNT, M_WAITOK); /* XXX */
151 MALLOC(um
, struct union_mount
*, sizeof(struct union_mount
),
155 * Keep a held reference to the target vnodes.
156 * They are vrele'd in union_unmount.
158 * Depending on the _BELOW flag, the filesystems are
159 * viewed in a different order. In effect, this is the
160 * same as providing a mount under option to the mount syscall.
163 um
->um_op
= args
.mntflags
& UNMNT_OPMASK
;
166 um
->um_lowervp
= lowerrootvp
;
167 um
->um_uppervp
= upperrootvp
;
171 um
->um_lowervp
= upperrootvp
;
172 um
->um_uppervp
= lowerrootvp
;
177 lowerrootvp
= NULLVP
;
178 um
->um_uppervp
= upperrootvp
;
179 um
->um_lowervp
= lowerrootvp
;
188 * Unless the mount is readonly, ensure that the top layer
189 * supports whiteout operations
191 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0) {
192 error
= VOP_WHITEOUT(um
->um_uppervp
, (struct componentname
*) 0, LOOKUP
);
197 um
->um_cred
= p
->p_ucred
;
199 um
->um_cmode
= UN_DIRMODE
&~ p
->p_fd
->fd_cmask
;
202 * Depending on what you think the MNT_LOCAL flag might mean,
203 * you may want the && to be || on the conditional below.
204 * At the moment it has been defined that the filesystem is
205 * only local if it is all local, ie the MNT_LOCAL flag implies
206 * that the entire namespace is local. If you think the MNT_LOCAL
207 * flag implies that some of the files might be stored locally
208 * then you will want to change the conditional.
210 if (um
->um_op
== UNMNT_ABOVE
) {
211 if (((um
->um_lowervp
== NULLVP
) ||
212 (um
->um_lowervp
->v_mount
->mnt_flag
& MNT_LOCAL
)) &&
213 (um
->um_uppervp
->v_mount
->mnt_flag
& MNT_LOCAL
))
214 mp
->mnt_flag
|= MNT_LOCAL
;
218 * Copy in the upper layer's RDONLY flag. This is for the benefit
219 * of lookup() which explicitly checks the flag, rather than asking
220 * the filesystem for it's own opinion. This means, that an update
221 * mount of the underlying filesystem to go from rdonly to rdwr
222 * will leave the unioned view as read-only.
224 mp
->mnt_flag
|= (um
->um_uppervp
->v_mount
->mnt_flag
& MNT_RDONLY
);
226 mp
->mnt_data
= (qaddr_t
) um
;
229 (void) copyinstr(path
, mp
->mnt_stat
.f_mntonname
, MNAMELEN
- 1, &size
);
230 bzero(mp
->mnt_stat
.f_mntonname
+ size
, MNAMELEN
- size
);
244 bcopy(cp
, mp
->mnt_stat
.f_mntfromname
, len
);
246 cp
= mp
->mnt_stat
.f_mntfromname
+ len
;
247 len
= MNAMELEN
- len
;
249 (void) copyinstr(args
.target
, cp
, len
- 1, &size
);
250 bzero(cp
+ size
, len
- size
);
252 #ifdef UNION_DIAGNOSTIC
253 printf("union_mount: from %s, on %s\n",
254 mp
->mnt_stat
.f_mntfromname
, mp
->mnt_stat
.f_mntonname
);
271 * VFS start. Nothing needed here - the start routine
272 * on the underlying filesystem(s) will have been called
273 * when that filesystem was mounted.
276 union_start(mp
, flags
, p
)
286 * Free reference to union layer
289 union_unmount(mp
, mntflags
, p
)
294 struct union_mount
*um
= MOUNTTOUNIONMOUNT(mp
);
295 struct vnode
*um_rootvp
;
301 #ifdef UNION_DIAGNOSTIC
302 printf("union_unmount(mp = %x)\n", mp
);
305 if (mntflags
& MNT_FORCE
)
308 if (error
= union_root(mp
, &um_rootvp
))
312 * Keep flushing vnodes from the mount list.
313 * This is needed because of the un_pvp held
314 * reference to the parent vnode.
315 * If more vnodes have been freed on a given pass,
316 * the try again. The loop will iterate at most
317 * (d) times, where (d) is the maximum tree depth
320 for (freeing
= 0; vflush(mp
, um_rootvp
, flags
) != 0;) {
324 /* count #vnodes held on mount list */
325 for (n
= 0, vp
= mp
->mnt_vnodelist
.lh_first
;
327 vp
= vp
->v_mntvnodes
.le_next
)
330 /* if this is unchanged then stop */
334 /* otherwise try once more time */
338 /* At this point the root vnode should have a single reference */
339 if (um_rootvp
->v_usecount
> 1) {
344 #ifdef UNION_DIAGNOSTIC
345 vprint("union root", um_rootvp
);
348 * Discard references to upper and lower target vnodes.
351 vrele(um
->um_lowervp
);
352 vrele(um
->um_uppervp
);
354 if (cred
!= NOCRED
) {
355 um
->um_cred
= NOCRED
;
359 * Release reference on underlying root vnode
363 * And blow it away for future re-use
367 * Finally, throw away the union_mount structure
369 _FREE(mp
->mnt_data
, M_UFSMNT
); /* XXX */
379 struct proc
*p
= current_proc(); /* XXX */
380 struct union_mount
*um
= MOUNTTOUNIONMOUNT(mp
);
385 * Return locked reference to root.
387 VREF(um
->um_uppervp
);
388 if ((um
->um_op
== UNMNT_BELOW
) &&
389 VOP_ISLOCKED(um
->um_uppervp
)) {
392 vn_lock(um
->um_uppervp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
396 VREF(um
->um_lowervp
);
397 error
= union_allocvp(vpp
, mp
,
400 (struct componentname
*) 0,
407 vrele(um
->um_uppervp
);
409 vput(um
->um_uppervp
);
411 vrele(um
->um_lowervp
);
414 VTOUNION(*vpp
)->un_flags
&= ~UN_ULOCK
;
421 union_statfs(mp
, sbp
, p
)
427 struct union_mount
*um
= MOUNTTOUNIONMOUNT(mp
);
431 #ifdef UNION_DIAGNOSTIC
432 printf("union_statfs(mp = %x, lvp = %x, uvp = %x)\n", mp
,
437 bzero(&mstat
, sizeof(mstat
));
439 if (um
->um_lowervp
) {
440 error
= VFS_STATFS(um
->um_lowervp
->v_mount
, &mstat
, p
);
445 /* now copy across the "interesting" information and fake the rest */
447 sbp
->f_type
= mstat
.f_type
;
448 sbp
->f_flags
= mstat
.f_flags
;
449 sbp
->f_bsize
= mstat
.f_bsize
;
450 sbp
->f_iosize
= mstat
.f_iosize
;
452 lbsize
= mstat
.f_bsize
;
453 sbp
->f_blocks
= mstat
.f_blocks
;
454 sbp
->f_bfree
= mstat
.f_bfree
;
455 sbp
->f_bavail
= mstat
.f_bavail
;
456 sbp
->f_files
= mstat
.f_files
;
457 sbp
->f_ffree
= mstat
.f_ffree
;
459 error
= VFS_STATFS(um
->um_uppervp
->v_mount
, &mstat
, p
);
463 sbp
->f_flags
= mstat
.f_flags
;
464 sbp
->f_bsize
= mstat
.f_bsize
;
465 sbp
->f_iosize
= mstat
.f_iosize
;
468 * if the lower and upper blocksizes differ, then frig the
469 * block counts so that the sizes reported by df make some
470 * kind of sense. none of this makes sense though.
473 if (mstat
.f_bsize
!= lbsize
)
474 sbp
->f_blocks
= sbp
->f_blocks
* lbsize
/ mstat
.f_bsize
;
477 * The "total" fields count total resources in all layers,
478 * the "free" fields count only those resources which are
479 * free in the upper layer (since only the upper layer
482 sbp
->f_blocks
+= mstat
.f_blocks
;
483 sbp
->f_bfree
= mstat
.f_bfree
;
484 sbp
->f_bavail
= mstat
.f_bavail
;
485 sbp
->f_files
+= mstat
.f_files
;
486 sbp
->f_ffree
= mstat
.f_ffree
;
488 if (sbp
!= &mp
->mnt_stat
) {
489 sbp
->f_type
= mp
->mnt_vfc
->vfc_typenum
;
490 bcopy(&mp
->mnt_stat
.f_fsid
, &sbp
->f_fsid
, sizeof(sbp
->f_fsid
));
491 bcopy(mp
->mnt_stat
.f_mntonname
, sbp
->f_mntonname
, MNAMELEN
);
492 bcopy(mp
->mnt_stat
.f_mntfromname
, sbp
->f_mntfromname
, MNAMELEN
);
498 * XXX - Assumes no data cached at union layer.
500 #define union_sync ((int (*) __P((struct mount *, int, struct ucred *, \
501 struct proc *)))nullop)
503 #define union_fhtovp ((int (*) __P((struct mount *, struct fid *, \
504 struct mbuf *, struct vnode **, int *, struct ucred **)))eopnotsupp)
505 int union_init
__P((struct vfsconf
*));
506 #define union_quotactl ((int (*) __P((struct mount *, int, uid_t, caddr_t, \
507 struct proc *)))eopnotsupp)
508 #define union_sysctl ((int (*) __P((int *, u_int, void *, size_t *, void *, \
509 size_t, struct proc *)))eopnotsupp)
510 #define union_vget ((int (*) __P((struct mount *, ino_t, struct vnode **))) \
512 #define union_vptofh ((int (*) __P((struct vnode *, struct fid *)))eopnotsupp)
514 struct vfsops union_vfsops
= {