]>
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 * 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
, MNAMELEN
- 1, &size
);
227 bzero(mp
->mnt_stat
.f_mntonname
+ size
, MNAMELEN
- size
);
241 bcopy(cp
, mp
->mnt_stat
.f_mntfromname
, len
);
243 cp
= mp
->mnt_stat
.f_mntfromname
+ len
;
244 len
= MNAMELEN
- len
;
246 (void) copyinstr(args
.target
, cp
, len
- 1, &size
);
247 bzero(cp
+ size
, len
- size
);
249 #ifdef UNION_DIAGNOSTIC
250 printf("union_mount: from %s, on %s\n",
251 mp
->mnt_stat
.f_mntfromname
, mp
->mnt_stat
.f_mntonname
);
268 * VFS start. Nothing needed here - the start routine
269 * on the underlying filesystem(s) will have been called
270 * when that filesystem was mounted.
273 union_start(mp
, flags
, p
)
283 * Free reference to union layer
286 union_unmount(mp
, mntflags
, p
)
291 struct union_mount
*um
= MOUNTTOUNIONMOUNT(mp
);
292 struct vnode
*um_rootvp
;
298 #ifdef UNION_DIAGNOSTIC
299 printf("union_unmount(mp = %x)\n", mp
);
302 if (mntflags
& MNT_FORCE
)
305 if (error
= union_root(mp
, &um_rootvp
))
309 * Keep flushing vnodes from the mount list.
310 * This is needed because of the un_pvp held
311 * reference to the parent vnode.
312 * If more vnodes have been freed on a given pass,
313 * the try again. The loop will iterate at most
314 * (d) times, where (d) is the maximum tree depth
317 for (freeing
= 0; vflush(mp
, um_rootvp
, flags
) != 0;) {
321 /* count #vnodes held on mount list */
322 for (n
= 0, vp
= mp
->mnt_vnodelist
.lh_first
;
324 vp
= vp
->v_mntvnodes
.le_next
)
327 /* if this is unchanged then stop */
331 /* otherwise try once more time */
335 /* At this point the root vnode should have a single reference */
336 if (um_rootvp
->v_usecount
> 1) {
341 #ifdef UNION_DIAGNOSTIC
342 vprint("union root", um_rootvp
);
345 * Discard references to upper and lower target vnodes.
348 vrele(um
->um_lowervp
);
349 vrele(um
->um_uppervp
);
351 if (cred
!= NOCRED
) {
352 um
->um_cred
= NOCRED
;
356 * Release reference on underlying root vnode
360 * And blow it away for future re-use
364 * Finally, throw away the union_mount structure
366 _FREE(mp
->mnt_data
, M_UFSMNT
); /* XXX */
376 struct proc
*p
= current_proc(); /* XXX */
377 struct union_mount
*um
= MOUNTTOUNIONMOUNT(mp
);
382 * Return locked reference to root.
384 VREF(um
->um_uppervp
);
385 if ((um
->um_op
== UNMNT_BELOW
) &&
386 VOP_ISLOCKED(um
->um_uppervp
)) {
389 vn_lock(um
->um_uppervp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
393 VREF(um
->um_lowervp
);
394 error
= union_allocvp(vpp
, mp
,
397 (struct componentname
*) 0,
404 vrele(um
->um_uppervp
);
406 vput(um
->um_uppervp
);
408 vrele(um
->um_lowervp
);
411 VTOUNION(*vpp
)->un_flags
&= ~UN_ULOCK
;
418 union_statfs(mp
, sbp
, p
)
424 struct union_mount
*um
= MOUNTTOUNIONMOUNT(mp
);
428 #ifdef UNION_DIAGNOSTIC
429 printf("union_statfs(mp = %x, lvp = %x, uvp = %x)\n", mp
,
434 bzero(&mstat
, sizeof(mstat
));
436 if (um
->um_lowervp
) {
437 error
= VFS_STATFS(um
->um_lowervp
->v_mount
, &mstat
, p
);
442 /* now copy across the "interesting" information and fake the rest */
444 sbp
->f_type
= mstat
.f_type
;
445 sbp
->f_flags
= mstat
.f_flags
;
446 sbp
->f_bsize
= mstat
.f_bsize
;
447 sbp
->f_iosize
= mstat
.f_iosize
;
449 lbsize
= mstat
.f_bsize
;
450 sbp
->f_blocks
= mstat
.f_blocks
;
451 sbp
->f_bfree
= mstat
.f_bfree
;
452 sbp
->f_bavail
= mstat
.f_bavail
;
453 sbp
->f_files
= mstat
.f_files
;
454 sbp
->f_ffree
= mstat
.f_ffree
;
456 error
= VFS_STATFS(um
->um_uppervp
->v_mount
, &mstat
, p
);
460 sbp
->f_flags
= mstat
.f_flags
;
461 sbp
->f_bsize
= mstat
.f_bsize
;
462 sbp
->f_iosize
= mstat
.f_iosize
;
465 * if the lower and upper blocksizes differ, then frig the
466 * block counts so that the sizes reported by df make some
467 * kind of sense. none of this makes sense though.
470 if (mstat
.f_bsize
!= lbsize
)
471 sbp
->f_blocks
= sbp
->f_blocks
* lbsize
/ mstat
.f_bsize
;
474 * The "total" fields count total resources in all layers,
475 * the "free" fields count only those resources which are
476 * free in the upper layer (since only the upper layer
479 sbp
->f_blocks
+= mstat
.f_blocks
;
480 sbp
->f_bfree
= mstat
.f_bfree
;
481 sbp
->f_bavail
= mstat
.f_bavail
;
482 sbp
->f_files
+= mstat
.f_files
;
483 sbp
->f_ffree
= mstat
.f_ffree
;
485 if (sbp
!= &mp
->mnt_stat
) {
486 sbp
->f_type
= mp
->mnt_vfc
->vfc_typenum
;
487 bcopy(&mp
->mnt_stat
.f_fsid
, &sbp
->f_fsid
, sizeof(sbp
->f_fsid
));
488 bcopy(mp
->mnt_stat
.f_mntonname
, sbp
->f_mntonname
, MNAMELEN
);
489 bcopy(mp
->mnt_stat
.f_mntfromname
, sbp
->f_mntfromname
, MNAMELEN
);
495 * XXX - Assumes no data cached at union layer.
497 #define union_sync ((int (*) __P((struct mount *, int, struct ucred *, \
498 struct proc *)))nullop)
500 #define union_fhtovp ((int (*) __P((struct mount *, struct fid *, \
501 struct mbuf *, struct vnode **, int *, struct ucred **)))eopnotsupp)
502 int union_init
__P((struct vfsconf
*));
503 #define union_quotactl ((int (*) __P((struct mount *, int, uid_t, caddr_t, \
504 struct proc *)))eopnotsupp)
505 #define union_sysctl ((int (*) __P((int *, u_int, void *, size_t *, void *, \
506 size_t, struct proc *)))eopnotsupp)
507 #define union_vget ((int (*) __P((struct mount *, ino_t, struct vnode **))) \
509 #define union_vptofh ((int (*) __P((struct vnode *, struct fid *)))eopnotsupp)
511 struct vfsops union_vfsops
= {