]> git.saurik.com Git - apple/xnu.git/blob - bsd/miscfs/union/union_vfsops.c
xnu-517.7.21.tar.gz
[apple/xnu.git] / bsd / miscfs / union / union_vfsops.c
1 /*
2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
23 /*
24 * Copyright (c) 1994, 1995 The Regents of the University of California.
25 * Copyright (c) 1994, 1995 Jan-Simon Pendry.
26 * All rights reserved.
27 *
28 * This code is derived from software donated to Berkeley by
29 * Jan-Simon Pendry.
30 *
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
33 * are met:
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.
46 *
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
57 * SUCH DAMAGE.
58 *
59 * @(#)union_vfsops.c 8.20 (Berkeley) 5/20/95
60 */
61
62 /*
63 * Union Layer
64 */
65
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/time.h>
69 #include <sys/types.h>
70 #include <sys/proc.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>
78
79 /*
80 * Mount union filesystem
81 */
82 int
83 union_mount(mp, path, data, ndp, p)
84 struct mount *mp;
85 char *path;
86 caddr_t data;
87 struct nameidata *ndp;
88 struct proc *p;
89 {
90 int error = 0;
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;
96 struct ucred *scred;
97 struct vattr va;
98 char *cp;
99 int len;
100 u_int size;
101
102 #ifdef UNION_DIAGNOSTIC
103 printf("union_mount(mp = %x)\n", mp);
104 #endif
105
106 /*
107 * Update is a no-op
108 */
109 if (mp->mnt_flag & MNT_UPDATE) {
110 /*
111 * Need to provide.
112 * 1. a way to convert between rdonly and rdwr mounts.
113 * 2. support for nfs exports.
114 */
115 error = EOPNOTSUPP;
116 goto bad;
117 }
118
119 /*
120 * Get argument
121 */
122 if (error = copyin(data, (caddr_t)&args, sizeof(struct union_args)))
123 goto bad;
124
125 lowerrootvp = mp->mnt_vnodecovered;
126 VREF(lowerrootvp);
127
128 /*
129 * Find upper node.
130 */
131 NDINIT(ndp, LOOKUP, FOLLOW|WANTPARENT,
132 UIO_USERSPACE, args.target, p);
133
134 if (error = namei(ndp))
135 goto bad;
136
137 upperrootvp = ndp->ni_vp;
138 vrele(ndp->ni_dvp);
139 ndp->ni_dvp = NULL;
140
141 if (upperrootvp->v_type != VDIR) {
142 error = EINVAL;
143 goto bad;
144 }
145
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),
149 M_UFSMNT, M_WAITOK);
150
151 /*
152 * Keep a held reference to the target vnodes.
153 * They are vrele'd in union_unmount.
154 *
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.
158 */
159
160 um->um_op = args.mntflags & UNMNT_OPMASK;
161 switch (um->um_op) {
162 case UNMNT_ABOVE:
163 um->um_lowervp = lowerrootvp;
164 um->um_uppervp = upperrootvp;
165 break;
166
167 case UNMNT_BELOW:
168 um->um_lowervp = upperrootvp;
169 um->um_uppervp = lowerrootvp;
170 break;
171
172 case UNMNT_REPLACE:
173 vrele(lowerrootvp);
174 lowerrootvp = NULLVP;
175 um->um_uppervp = upperrootvp;
176 um->um_lowervp = lowerrootvp;
177 break;
178
179 default:
180 error = EINVAL;
181 goto bad;
182 }
183
184 /*
185 * Unless the mount is readonly, ensure that the top layer
186 * supports whiteout operations
187 */
188 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
189 error = VOP_WHITEOUT(um->um_uppervp, (struct componentname *) 0, LOOKUP);
190 if (error)
191 goto bad;
192 }
193
194 um->um_cred = p->p_ucred;
195 crhold(um->um_cred);
196 um->um_cmode = UN_DIRMODE &~ p->p_fd->fd_cmask;
197
198 /*
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.
206 */
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;
212 }
213
214 /*
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.
220 */
221 mp->mnt_flag |= (um->um_uppervp->v_mount->mnt_flag & MNT_RDONLY);
222
223 mp->mnt_data = (qaddr_t) um;
224 vfs_getnewfsid(mp);
225
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);
229
230 switch (um->um_op) {
231 case UNMNT_ABOVE:
232 cp = "<above>:";
233 break;
234 case UNMNT_BELOW:
235 cp = "<below>:";
236 break;
237 case UNMNT_REPLACE:
238 cp = "";
239 break;
240 }
241 len = strlen(cp);
242 bcopy(cp, mp->mnt_stat.f_mntfromname, len);
243
244 cp = mp->mnt_stat.f_mntfromname + len;
245 len = MNAMELEN - len;
246
247 (void) copyinstr(args.target, cp, len - 1, (size_t *)&size);
248 bzero(cp + size, len - size);
249
250 #ifdef UNION_DIAGNOSTIC
251 printf("union_mount: from %s, on %s\n",
252 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
253 #endif
254 return (0);
255
256 bad:
257 if (um)
258 _FREE(um, M_UFSMNT);
259 if (cred != NOCRED)
260 crfree(cred);
261 if (upperrootvp)
262 vrele(upperrootvp);
263 if (lowerrootvp)
264 vrele(lowerrootvp);
265 return (error);
266 }
267
268 /*
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.
272 */
273 int
274 union_start(mp, flags, p)
275 struct mount *mp;
276 int flags;
277 struct proc *p;
278 {
279
280 return (0);
281 }
282
283 /*
284 * Free reference to union layer
285 */
286 int
287 union_unmount(mp, mntflags, p)
288 struct mount *mp;
289 int mntflags;
290 struct proc *p;
291 {
292 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
293 struct vnode *um_rootvp;
294 int error;
295 int freeing;
296 int flags = 0;
297 struct ucred *cred;
298
299 #ifdef UNION_DIAGNOSTIC
300 printf("union_unmount(mp = %x)\n", mp);
301 #endif
302
303 if (mntflags & MNT_FORCE)
304 flags |= FORCECLOSE;
305
306 if (error = union_root(mp, &um_rootvp))
307 return (error);
308
309 /*
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
316 * in the filesystem.
317 */
318 for (freeing = 0; vflush(mp, um_rootvp, flags) != 0;) {
319 struct vnode *vp;
320 int n;
321
322 /* count #vnodes held on mount list */
323 for (n = 0, vp = mp->mnt_vnodelist.lh_first;
324 vp != NULLVP;
325 vp = vp->v_mntvnodes.le_next)
326 n++;
327
328 /* if this is unchanged then stop */
329 if (n == freeing)
330 break;
331
332 /* otherwise try once more time */
333 freeing = n;
334 }
335
336 /* At this point the root vnode should have a single reference */
337 if (um_rootvp->v_usecount > 1) {
338 vput(um_rootvp);
339 return (EBUSY);
340 }
341
342 #ifdef UNION_DIAGNOSTIC
343 vprint("union root", um_rootvp);
344 #endif
345 /*
346 * Discard references to upper and lower target vnodes.
347 */
348 if (um->um_lowervp)
349 vrele(um->um_lowervp);
350 vrele(um->um_uppervp);
351 cred = um->um_cred;
352 if (cred != NOCRED) {
353 um->um_cred = NOCRED;
354 crfree(cred);
355 }
356 /*
357 * Release reference on underlying root vnode
358 */
359 vput(um_rootvp);
360 /*
361 * And blow it away for future re-use
362 */
363 vgone(um_rootvp);
364 /*
365 * Finally, throw away the union_mount structure
366 */
367 _FREE(mp->mnt_data, M_UFSMNT); /* XXX */
368 mp->mnt_data = 0;
369 return (0);
370 }
371
372 int
373 union_root(mp, vpp)
374 struct mount *mp;
375 struct vnode **vpp;
376 {
377 struct proc *p = current_proc(); /* XXX */
378 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
379 int error;
380 int loselock;
381
382 /*
383 * Return locked reference to root.
384 */
385 VREF(um->um_uppervp);
386 if ((um->um_op == UNMNT_BELOW) &&
387 VOP_ISLOCKED(um->um_uppervp)) {
388 loselock = 1;
389 } else {
390 vn_lock(um->um_uppervp, LK_EXCLUSIVE | LK_RETRY, p);
391 loselock = 0;
392 }
393 if (um->um_lowervp)
394 VREF(um->um_lowervp);
395 error = union_allocvp(vpp, mp,
396 (struct vnode *) 0,
397 (struct vnode *) 0,
398 (struct componentname *) 0,
399 um->um_uppervp,
400 um->um_lowervp,
401 1);
402
403 if (error) {
404 if (loselock)
405 vrele(um->um_uppervp);
406 else
407 vput(um->um_uppervp);
408 if (um->um_lowervp)
409 vrele(um->um_lowervp);
410 } else {
411 if (loselock)
412 VTOUNION(*vpp)->un_flags &= ~UN_ULOCK;
413 }
414
415 return (error);
416 }
417
418 int
419 union_statfs(mp, sbp, p)
420 struct mount *mp;
421 struct statfs *sbp;
422 struct proc *p;
423 {
424 int error;
425 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
426 struct statfs mstat;
427 int lbsize;
428
429 #ifdef UNION_DIAGNOSTIC
430 printf("union_statfs(mp = %x, lvp = %x, uvp = %x)\n", mp,
431 um->um_lowervp,
432 um->um_uppervp);
433 #endif
434
435 bzero(&mstat, sizeof(mstat));
436
437 if (um->um_lowervp) {
438 error = VFS_STATFS(um->um_lowervp->v_mount, &mstat, p);
439 if (error)
440 return (error);
441 }
442
443 /* now copy across the "interesting" information and fake the rest */
444 #if 0
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;
449 #endif
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;
456
457 error = VFS_STATFS(um->um_uppervp->v_mount, &mstat, p);
458 if (error)
459 return (error);
460
461 sbp->f_flags = mstat.f_flags;
462 sbp->f_bsize = mstat.f_bsize;
463 sbp->f_iosize = mstat.f_iosize;
464
465 /*
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.
469 */
470
471 if (mstat.f_bsize != lbsize)
472 sbp->f_blocks = sbp->f_blocks * lbsize / mstat.f_bsize;
473
474 /*
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
478 * is writeable).
479 */
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;
485
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);
491 }
492 return (0);
493 }
494
495 /*
496 * XXX - Assumes no data cached at union layer.
497 */
498 #define union_sync ((int (*) __P((struct mount *, int, struct ucred *, \
499 struct proc *)))nullop)
500
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 **))) \
509 eopnotsupp)
510 #define union_vptofh ((int (*) __P((struct vnode *, struct fid *)))eopnotsupp)
511
512 struct vfsops union_vfsops = {
513 union_mount,
514 union_start,
515 union_unmount,
516 union_root,
517 union_quotactl,
518 union_statfs,
519 union_sync,
520 union_vget,
521 union_fhtovp,
522 union_vptofh,
523 union_init,
524 union_sysctl,
525 };