]>
git.saurik.com Git - apple/xnu.git/blob - bsd/miscfs/union/union_vfsops.c
2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1994, 1995 The Regents of the University of California.
25 * Copyright (c) 1994, 1995 Jan-Simon Pendry.
26 * All rights reserved.
28 * This code is derived from software donated to Berkeley by
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * @(#)union_vfsops.c 8.20 (Berkeley) 5/20/95
66 #include <sys/param.h>
67 #include <sys/systm.h>
69 #include <sys/types.h>
71 #include <sys/vnode.h>
72 #include <sys/mount.h>
73 #include <sys/namei.h>
74 #include <sys/malloc.h>
75 #include <sys/filedesc.h>
76 #include <sys/queue.h>
77 #include <miscfs/union/union.h>
80 * Mount union filesystem
83 union_mount(mp
, path
, data
, ndp
, p
)
87 struct nameidata
*ndp
;
91 struct union_args args
;
92 struct vnode
*lowerrootvp
= NULLVP
;
93 struct vnode
*upperrootvp
= NULLVP
;
94 struct union_mount
*um
= 0;
95 struct ucred
*cred
= 0;
102 #ifdef UNION_DIAGNOSTIC
103 printf("union_mount(mp = %x)\n", mp
);
109 if (mp
->mnt_flag
& MNT_UPDATE
) {
112 * 1. a way to convert between rdonly and rdwr mounts.
113 * 2. support for nfs exports.
122 if (error
= copyin(data
, (caddr_t
)&args
, sizeof(struct union_args
)))
125 lowerrootvp
= mp
->mnt_vnodecovered
;
131 NDINIT(ndp
, LOOKUP
, FOLLOW
|WANTPARENT
,
132 UIO_USERSPACE
, args
.target
, p
);
134 if (error
= namei(ndp
))
137 upperrootvp
= ndp
->ni_vp
;
141 if (upperrootvp
->v_type
!= VDIR
) {
146 // um = (struct union_mount *) malloc(sizeof(struct union_mount),
147 // M_UFSMNT, M_WAITOK); /* XXX */
148 MALLOC(um
, struct union_mount
*, sizeof(struct union_mount
),
152 * Keep a held reference to the target vnodes.
153 * They are vrele'd in union_unmount.
155 * Depending on the _BELOW flag, the filesystems are
156 * viewed in a different order. In effect, this is the
157 * same as providing a mount under option to the mount syscall.
160 um
->um_op
= args
.mntflags
& UNMNT_OPMASK
;
163 um
->um_lowervp
= lowerrootvp
;
164 um
->um_uppervp
= upperrootvp
;
168 um
->um_lowervp
= upperrootvp
;
169 um
->um_uppervp
= lowerrootvp
;
174 lowerrootvp
= NULLVP
;
175 um
->um_uppervp
= upperrootvp
;
176 um
->um_lowervp
= lowerrootvp
;
185 * Unless the mount is readonly, ensure that the top layer
186 * supports whiteout operations
188 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0) {
189 error
= VOP_WHITEOUT(um
->um_uppervp
, (struct componentname
*) 0, LOOKUP
);
194 um
->um_cred
= p
->p_ucred
;
196 um
->um_cmode
= UN_DIRMODE
&~ p
->p_fd
->fd_cmask
;
199 * Depending on what you think the MNT_LOCAL flag might mean,
200 * you may want the && to be || on the conditional below.
201 * At the moment it has been defined that the filesystem is
202 * only local if it is all local, ie the MNT_LOCAL flag implies
203 * that the entire namespace is local. If you think the MNT_LOCAL
204 * flag implies that some of the files might be stored locally
205 * then you will want to change the conditional.
207 if (um
->um_op
== UNMNT_ABOVE
) {
208 if (((um
->um_lowervp
== NULLVP
) ||
209 (um
->um_lowervp
->v_mount
->mnt_flag
& MNT_LOCAL
)) &&
210 (um
->um_uppervp
->v_mount
->mnt_flag
& MNT_LOCAL
))
211 mp
->mnt_flag
|= MNT_LOCAL
;
215 * Copy in the upper layer's RDONLY flag. This is for the benefit
216 * of lookup() which explicitly checks the flag, rather than asking
217 * the filesystem for it's own opinion. This means, that an update
218 * mount of the underlying filesystem to go from rdonly to rdwr
219 * will leave the unioned view as read-only.
221 mp
->mnt_flag
|= (um
->um_uppervp
->v_mount
->mnt_flag
& MNT_RDONLY
);
223 mp
->mnt_data
= (qaddr_t
) um
;
226 (void) copyinstr(path
, mp
->mnt_stat
.f_mntonname
,
227 MNAMELEN
- 1, (size_t *)&size
);
228 bzero(mp
->mnt_stat
.f_mntonname
+ size
, MNAMELEN
- size
);
242 bcopy(cp
, mp
->mnt_stat
.f_mntfromname
, len
);
244 cp
= mp
->mnt_stat
.f_mntfromname
+ len
;
245 len
= MNAMELEN
- len
;
247 (void) copyinstr(args
.target
, cp
, len
- 1, (size_t *)&size
);
248 bzero(cp
+ size
, len
- size
);
250 #ifdef UNION_DIAGNOSTIC
251 printf("union_mount: from %s, on %s\n",
252 mp
->mnt_stat
.f_mntfromname
, mp
->mnt_stat
.f_mntonname
);
269 * VFS start. Nothing needed here - the start routine
270 * on the underlying filesystem(s) will have been called
271 * when that filesystem was mounted.
274 union_start(mp
, flags
, p
)
284 * Free reference to union layer
287 union_unmount(mp
, mntflags
, p
)
292 struct union_mount
*um
= MOUNTTOUNIONMOUNT(mp
);
293 struct vnode
*um_rootvp
;
299 #ifdef UNION_DIAGNOSTIC
300 printf("union_unmount(mp = %x)\n", mp
);
303 if (mntflags
& MNT_FORCE
)
306 if (error
= union_root(mp
, &um_rootvp
))
310 * Keep flushing vnodes from the mount list.
311 * This is needed because of the un_pvp held
312 * reference to the parent vnode.
313 * If more vnodes have been freed on a given pass,
314 * the try again. The loop will iterate at most
315 * (d) times, where (d) is the maximum tree depth
318 for (freeing
= 0; vflush(mp
, um_rootvp
, flags
) != 0;) {
322 /* count #vnodes held on mount list */
323 for (n
= 0, vp
= mp
->mnt_vnodelist
.lh_first
;
325 vp
= vp
->v_mntvnodes
.le_next
)
328 /* if this is unchanged then stop */
332 /* otherwise try once more time */
336 /* At this point the root vnode should have a single reference */
337 if (um_rootvp
->v_usecount
> 1) {
342 #ifdef UNION_DIAGNOSTIC
343 vprint("union root", um_rootvp
);
346 * Discard references to upper and lower target vnodes.
349 vrele(um
->um_lowervp
);
350 vrele(um
->um_uppervp
);
352 if (cred
!= NOCRED
) {
353 um
->um_cred
= NOCRED
;
357 * Release reference on underlying root vnode
361 * And blow it away for future re-use
365 * Finally, throw away the union_mount structure
367 _FREE(mp
->mnt_data
, M_UFSMNT
); /* XXX */
377 struct proc
*p
= current_proc(); /* XXX */
378 struct union_mount
*um
= MOUNTTOUNIONMOUNT(mp
);
383 * Return locked reference to root.
385 VREF(um
->um_uppervp
);
386 if ((um
->um_op
== UNMNT_BELOW
) &&
387 VOP_ISLOCKED(um
->um_uppervp
)) {
390 vn_lock(um
->um_uppervp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
394 VREF(um
->um_lowervp
);
395 error
= union_allocvp(vpp
, mp
,
398 (struct componentname
*) 0,
405 vrele(um
->um_uppervp
);
407 vput(um
->um_uppervp
);
409 vrele(um
->um_lowervp
);
412 VTOUNION(*vpp
)->un_flags
&= ~UN_ULOCK
;
419 union_statfs(mp
, sbp
, p
)
425 struct union_mount
*um
= MOUNTTOUNIONMOUNT(mp
);
429 #ifdef UNION_DIAGNOSTIC
430 printf("union_statfs(mp = %x, lvp = %x, uvp = %x)\n", mp
,
435 bzero(&mstat
, sizeof(mstat
));
437 if (um
->um_lowervp
) {
438 error
= VFS_STATFS(um
->um_lowervp
->v_mount
, &mstat
, p
);
443 /* now copy across the "interesting" information and fake the rest */
445 sbp
->f_type
= mstat
.f_type
;
446 sbp
->f_flags
= mstat
.f_flags
;
447 sbp
->f_bsize
= mstat
.f_bsize
;
448 sbp
->f_iosize
= mstat
.f_iosize
;
450 lbsize
= mstat
.f_bsize
;
451 sbp
->f_blocks
= mstat
.f_blocks
;
452 sbp
->f_bfree
= mstat
.f_bfree
;
453 sbp
->f_bavail
= mstat
.f_bavail
;
454 sbp
->f_files
= mstat
.f_files
;
455 sbp
->f_ffree
= mstat
.f_ffree
;
457 error
= VFS_STATFS(um
->um_uppervp
->v_mount
, &mstat
, p
);
461 sbp
->f_flags
= mstat
.f_flags
;
462 sbp
->f_bsize
= mstat
.f_bsize
;
463 sbp
->f_iosize
= mstat
.f_iosize
;
466 * if the lower and upper blocksizes differ, then frig the
467 * block counts so that the sizes reported by df make some
468 * kind of sense. none of this makes sense though.
471 if (mstat
.f_bsize
!= lbsize
)
472 sbp
->f_blocks
= sbp
->f_blocks
* lbsize
/ mstat
.f_bsize
;
475 * The "total" fields count total resources in all layers,
476 * the "free" fields count only those resources which are
477 * free in the upper layer (since only the upper layer
480 sbp
->f_blocks
+= mstat
.f_blocks
;
481 sbp
->f_bfree
= mstat
.f_bfree
;
482 sbp
->f_bavail
= mstat
.f_bavail
;
483 sbp
->f_files
+= mstat
.f_files
;
484 sbp
->f_ffree
= mstat
.f_ffree
;
486 if (sbp
!= &mp
->mnt_stat
) {
487 sbp
->f_type
= mp
->mnt_vfc
->vfc_typenum
;
488 bcopy(&mp
->mnt_stat
.f_fsid
, &sbp
->f_fsid
, sizeof(sbp
->f_fsid
));
489 bcopy(mp
->mnt_stat
.f_mntonname
, sbp
->f_mntonname
, MNAMELEN
);
490 bcopy(mp
->mnt_stat
.f_mntfromname
, sbp
->f_mntfromname
, MNAMELEN
);
496 * XXX - Assumes no data cached at union layer.
498 #define union_sync ((int (*) __P((struct mount *, int, struct ucred *, \
499 struct proc *)))nullop)
501 #define union_fhtovp ((int (*) __P((struct mount *, struct fid *, \
502 struct mbuf *, struct vnode **, int *, struct ucred **)))eopnotsupp)
503 int union_init
__P((struct vfsconf
*));
504 #define union_quotactl ((int (*) __P((struct mount *, int, uid_t, caddr_t, \
505 struct proc *)))eopnotsupp)
506 #define union_sysctl ((int (*) __P((int *, u_int, void *, size_t *, void *, \
507 size_t, struct proc *)))eopnotsupp)
508 #define union_vget ((int (*) __P((struct mount *, void *, struct vnode **))) \
510 #define union_vptofh ((int (*) __P((struct vnode *, struct fid *)))eopnotsupp)
512 struct vfsops union_vfsops
= {