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