]>
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 * 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
,
230 MNAMELEN
- 1, (size_t *)&size
);
231 bzero(mp
->mnt_stat
.f_mntonname
+ size
, MNAMELEN
- size
);
245 bcopy(cp
, mp
->mnt_stat
.f_mntfromname
, len
);
247 cp
= mp
->mnt_stat
.f_mntfromname
+ len
;
248 len
= MNAMELEN
- len
;
250 (void) copyinstr(args
.target
, cp
, len
- 1, (size_t *)&size
);
251 bzero(cp
+ size
, len
- size
);
253 #ifdef UNION_DIAGNOSTIC
254 printf("union_mount: from %s, on %s\n",
255 mp
->mnt_stat
.f_mntfromname
, mp
->mnt_stat
.f_mntonname
);
272 * VFS start. Nothing needed here - the start routine
273 * on the underlying filesystem(s) will have been called
274 * when that filesystem was mounted.
277 union_start(mp
, flags
, p
)
287 * Free reference to union layer
290 union_unmount(mp
, mntflags
, p
)
295 struct union_mount
*um
= MOUNTTOUNIONMOUNT(mp
);
296 struct vnode
*um_rootvp
;
302 #ifdef UNION_DIAGNOSTIC
303 printf("union_unmount(mp = %x)\n", mp
);
306 if (mntflags
& MNT_FORCE
)
309 if (error
= union_root(mp
, &um_rootvp
))
313 * Keep flushing vnodes from the mount list.
314 * This is needed because of the un_pvp held
315 * reference to the parent vnode.
316 * If more vnodes have been freed on a given pass,
317 * the try again. The loop will iterate at most
318 * (d) times, where (d) is the maximum tree depth
321 for (freeing
= 0; vflush(mp
, um_rootvp
, flags
) != 0;) {
325 /* count #vnodes held on mount list */
326 for (n
= 0, vp
= mp
->mnt_vnodelist
.lh_first
;
328 vp
= vp
->v_mntvnodes
.le_next
)
331 /* if this is unchanged then stop */
335 /* otherwise try once more time */
339 /* At this point the root vnode should have a single reference */
340 if (um_rootvp
->v_usecount
> 1) {
345 #ifdef UNION_DIAGNOSTIC
346 vprint("union root", um_rootvp
);
349 * Discard references to upper and lower target vnodes.
352 vrele(um
->um_lowervp
);
353 vrele(um
->um_uppervp
);
355 if (cred
!= NOCRED
) {
356 um
->um_cred
= NOCRED
;
360 * Release reference on underlying root vnode
364 * And blow it away for future re-use
368 * Finally, throw away the union_mount structure
370 _FREE(mp
->mnt_data
, M_UFSMNT
); /* XXX */
380 struct proc
*p
= current_proc(); /* XXX */
381 struct union_mount
*um
= MOUNTTOUNIONMOUNT(mp
);
386 * Return locked reference to root.
388 VREF(um
->um_uppervp
);
389 if ((um
->um_op
== UNMNT_BELOW
) &&
390 VOP_ISLOCKED(um
->um_uppervp
)) {
393 vn_lock(um
->um_uppervp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
397 VREF(um
->um_lowervp
);
398 error
= union_allocvp(vpp
, mp
,
401 (struct componentname
*) 0,
408 vrele(um
->um_uppervp
);
410 vput(um
->um_uppervp
);
412 vrele(um
->um_lowervp
);
415 VTOUNION(*vpp
)->un_flags
&= ~UN_ULOCK
;
422 union_statfs(mp
, sbp
, p
)
428 struct union_mount
*um
= MOUNTTOUNIONMOUNT(mp
);
432 #ifdef UNION_DIAGNOSTIC
433 printf("union_statfs(mp = %x, lvp = %x, uvp = %x)\n", mp
,
438 bzero(&mstat
, sizeof(mstat
));
440 if (um
->um_lowervp
) {
441 error
= VFS_STATFS(um
->um_lowervp
->v_mount
, &mstat
, p
);
446 /* now copy across the "interesting" information and fake the rest */
448 sbp
->f_type
= mstat
.f_type
;
449 sbp
->f_flags
= mstat
.f_flags
;
450 sbp
->f_bsize
= mstat
.f_bsize
;
451 sbp
->f_iosize
= mstat
.f_iosize
;
453 lbsize
= mstat
.f_bsize
;
454 sbp
->f_blocks
= mstat
.f_blocks
;
455 sbp
->f_bfree
= mstat
.f_bfree
;
456 sbp
->f_bavail
= mstat
.f_bavail
;
457 sbp
->f_files
= mstat
.f_files
;
458 sbp
->f_ffree
= mstat
.f_ffree
;
460 error
= VFS_STATFS(um
->um_uppervp
->v_mount
, &mstat
, p
);
464 sbp
->f_flags
= mstat
.f_flags
;
465 sbp
->f_bsize
= mstat
.f_bsize
;
466 sbp
->f_iosize
= mstat
.f_iosize
;
469 * if the lower and upper blocksizes differ, then frig the
470 * block counts so that the sizes reported by df make some
471 * kind of sense. none of this makes sense though.
474 if (mstat
.f_bsize
!= lbsize
)
475 sbp
->f_blocks
= sbp
->f_blocks
* lbsize
/ mstat
.f_bsize
;
478 * The "total" fields count total resources in all layers,
479 * the "free" fields count only those resources which are
480 * free in the upper layer (since only the upper layer
483 sbp
->f_blocks
+= mstat
.f_blocks
;
484 sbp
->f_bfree
= mstat
.f_bfree
;
485 sbp
->f_bavail
= mstat
.f_bavail
;
486 sbp
->f_files
+= mstat
.f_files
;
487 sbp
->f_ffree
= mstat
.f_ffree
;
489 if (sbp
!= &mp
->mnt_stat
) {
490 sbp
->f_type
= mp
->mnt_vfc
->vfc_typenum
;
491 bcopy(&mp
->mnt_stat
.f_fsid
, &sbp
->f_fsid
, sizeof(sbp
->f_fsid
));
492 bcopy(mp
->mnt_stat
.f_mntonname
, sbp
->f_mntonname
, MNAMELEN
);
493 bcopy(mp
->mnt_stat
.f_mntfromname
, sbp
->f_mntfromname
, MNAMELEN
);
499 * XXX - Assumes no data cached at union layer.
501 #define union_sync ((int (*) __P((struct mount *, int, struct ucred *, \
502 struct proc *)))nullop)
504 #define union_fhtovp ((int (*) __P((struct mount *, struct fid *, \
505 struct mbuf *, struct vnode **, int *, struct ucred **)))eopnotsupp)
506 int union_init
__P((struct vfsconf
*));
507 #define union_quotactl ((int (*) __P((struct mount *, int, uid_t, caddr_t, \
508 struct proc *)))eopnotsupp)
509 #define union_sysctl ((int (*) __P((int *, u_int, void *, size_t *, void *, \
510 size_t, struct proc *)))eopnotsupp)
511 #define union_vget ((int (*) __P((struct mount *, void *, struct vnode **))) \
513 #define union_vptofh ((int (*) __P((struct vnode *, struct fid *)))eopnotsupp)
515 struct vfsops union_vfsops
= {