]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 2000 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, MNAMELEN - 1, &size); | |
227 | bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size); | |
228 | ||
229 | switch (um->um_op) { | |
230 | case UNMNT_ABOVE: | |
231 | cp = "<above>:"; | |
232 | break; | |
233 | case UNMNT_BELOW: | |
234 | cp = "<below>:"; | |
235 | break; | |
236 | case UNMNT_REPLACE: | |
237 | cp = ""; | |
238 | break; | |
239 | } | |
240 | len = strlen(cp); | |
241 | bcopy(cp, mp->mnt_stat.f_mntfromname, len); | |
242 | ||
243 | cp = mp->mnt_stat.f_mntfromname + len; | |
244 | len = MNAMELEN - len; | |
245 | ||
246 | (void) copyinstr(args.target, cp, len - 1, &size); | |
247 | bzero(cp + size, len - size); | |
248 | ||
249 | #ifdef UNION_DIAGNOSTIC | |
250 | printf("union_mount: from %s, on %s\n", | |
251 | mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname); | |
252 | #endif | |
253 | return (0); | |
254 | ||
255 | bad: | |
256 | if (um) | |
257 | _FREE(um, M_UFSMNT); | |
258 | if (cred != NOCRED) | |
259 | crfree(cred); | |
260 | if (upperrootvp) | |
261 | vrele(upperrootvp); | |
262 | if (lowerrootvp) | |
263 | vrele(lowerrootvp); | |
264 | return (error); | |
265 | } | |
266 | ||
267 | /* | |
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. | |
271 | */ | |
272 | int | |
273 | union_start(mp, flags, p) | |
274 | struct mount *mp; | |
275 | int flags; | |
276 | struct proc *p; | |
277 | { | |
278 | ||
279 | return (0); | |
280 | } | |
281 | ||
282 | /* | |
283 | * Free reference to union layer | |
284 | */ | |
285 | int | |
286 | union_unmount(mp, mntflags, p) | |
287 | struct mount *mp; | |
288 | int mntflags; | |
289 | struct proc *p; | |
290 | { | |
291 | struct union_mount *um = MOUNTTOUNIONMOUNT(mp); | |
292 | struct vnode *um_rootvp; | |
293 | int error; | |
294 | int freeing; | |
295 | int flags = 0; | |
296 | struct ucred *cred; | |
297 | ||
298 | #ifdef UNION_DIAGNOSTIC | |
299 | printf("union_unmount(mp = %x)\n", mp); | |
300 | #endif | |
301 | ||
302 | if (mntflags & MNT_FORCE) | |
303 | flags |= FORCECLOSE; | |
304 | ||
305 | if (error = union_root(mp, &um_rootvp)) | |
306 | return (error); | |
307 | ||
308 | /* | |
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 | |
315 | * in the filesystem. | |
316 | */ | |
317 | for (freeing = 0; vflush(mp, um_rootvp, flags) != 0;) { | |
318 | struct vnode *vp; | |
319 | int n; | |
320 | ||
321 | /* count #vnodes held on mount list */ | |
322 | for (n = 0, vp = mp->mnt_vnodelist.lh_first; | |
323 | vp != NULLVP; | |
324 | vp = vp->v_mntvnodes.le_next) | |
325 | n++; | |
326 | ||
327 | /* if this is unchanged then stop */ | |
328 | if (n == freeing) | |
329 | break; | |
330 | ||
331 | /* otherwise try once more time */ | |
332 | freeing = n; | |
333 | } | |
334 | ||
335 | /* At this point the root vnode should have a single reference */ | |
336 | if (um_rootvp->v_usecount > 1) { | |
337 | vput(um_rootvp); | |
338 | return (EBUSY); | |
339 | } | |
340 | ||
341 | #ifdef UNION_DIAGNOSTIC | |
342 | vprint("union root", um_rootvp); | |
343 | #endif | |
344 | /* | |
345 | * Discard references to upper and lower target vnodes. | |
346 | */ | |
347 | if (um->um_lowervp) | |
348 | vrele(um->um_lowervp); | |
349 | vrele(um->um_uppervp); | |
350 | cred = um->um_cred; | |
351 | if (cred != NOCRED) { | |
352 | um->um_cred = NOCRED; | |
353 | crfree(cred); | |
354 | } | |
355 | /* | |
356 | * Release reference on underlying root vnode | |
357 | */ | |
358 | vput(um_rootvp); | |
359 | /* | |
360 | * And blow it away for future re-use | |
361 | */ | |
362 | vgone(um_rootvp); | |
363 | /* | |
364 | * Finally, throw away the union_mount structure | |
365 | */ | |
366 | _FREE(mp->mnt_data, M_UFSMNT); /* XXX */ | |
367 | mp->mnt_data = 0; | |
368 | return (0); | |
369 | } | |
370 | ||
371 | int | |
372 | union_root(mp, vpp) | |
373 | struct mount *mp; | |
374 | struct vnode **vpp; | |
375 | { | |
376 | struct proc *p = current_proc(); /* XXX */ | |
377 | struct union_mount *um = MOUNTTOUNIONMOUNT(mp); | |
378 | int error; | |
379 | int loselock; | |
380 | ||
381 | /* | |
382 | * Return locked reference to root. | |
383 | */ | |
384 | VREF(um->um_uppervp); | |
385 | if ((um->um_op == UNMNT_BELOW) && | |
386 | VOP_ISLOCKED(um->um_uppervp)) { | |
387 | loselock = 1; | |
388 | } else { | |
389 | vn_lock(um->um_uppervp, LK_EXCLUSIVE | LK_RETRY, p); | |
390 | loselock = 0; | |
391 | } | |
392 | if (um->um_lowervp) | |
393 | VREF(um->um_lowervp); | |
394 | error = union_allocvp(vpp, mp, | |
395 | (struct vnode *) 0, | |
396 | (struct vnode *) 0, | |
397 | (struct componentname *) 0, | |
398 | um->um_uppervp, | |
399 | um->um_lowervp, | |
400 | 1); | |
401 | ||
402 | if (error) { | |
403 | if (loselock) | |
404 | vrele(um->um_uppervp); | |
405 | else | |
406 | vput(um->um_uppervp); | |
407 | if (um->um_lowervp) | |
408 | vrele(um->um_lowervp); | |
409 | } else { | |
410 | if (loselock) | |
411 | VTOUNION(*vpp)->un_flags &= ~UN_ULOCK; | |
412 | } | |
413 | ||
414 | return (error); | |
415 | } | |
416 | ||
417 | int | |
418 | union_statfs(mp, sbp, p) | |
419 | struct mount *mp; | |
420 | struct statfs *sbp; | |
421 | struct proc *p; | |
422 | { | |
423 | int error; | |
424 | struct union_mount *um = MOUNTTOUNIONMOUNT(mp); | |
425 | struct statfs mstat; | |
426 | int lbsize; | |
427 | ||
428 | #ifdef UNION_DIAGNOSTIC | |
429 | printf("union_statfs(mp = %x, lvp = %x, uvp = %x)\n", mp, | |
430 | um->um_lowervp, | |
431 | um->um_uppervp); | |
432 | #endif | |
433 | ||
434 | bzero(&mstat, sizeof(mstat)); | |
435 | ||
436 | if (um->um_lowervp) { | |
437 | error = VFS_STATFS(um->um_lowervp->v_mount, &mstat, p); | |
438 | if (error) | |
439 | return (error); | |
440 | } | |
441 | ||
442 | /* now copy across the "interesting" information and fake the rest */ | |
443 | #if 0 | |
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; | |
448 | #endif | |
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; | |
455 | ||
456 | error = VFS_STATFS(um->um_uppervp->v_mount, &mstat, p); | |
457 | if (error) | |
458 | return (error); | |
459 | ||
460 | sbp->f_flags = mstat.f_flags; | |
461 | sbp->f_bsize = mstat.f_bsize; | |
462 | sbp->f_iosize = mstat.f_iosize; | |
463 | ||
464 | /* | |
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. | |
468 | */ | |
469 | ||
470 | if (mstat.f_bsize != lbsize) | |
471 | sbp->f_blocks = sbp->f_blocks * lbsize / mstat.f_bsize; | |
472 | ||
473 | /* | |
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 | |
477 | * is writeable). | |
478 | */ | |
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; | |
484 | ||
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); | |
490 | } | |
491 | return (0); | |
492 | } | |
493 | ||
494 | /* | |
495 | * XXX - Assumes no data cached at union layer. | |
496 | */ | |
497 | #define union_sync ((int (*) __P((struct mount *, int, struct ucred *, \ | |
498 | struct proc *)))nullop) | |
499 | ||
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 **))) \ | |
508 | eopnotsupp) | |
509 | #define union_vptofh ((int (*) __P((struct vnode *, struct fid *)))eopnotsupp) | |
510 | ||
511 | struct vfsops union_vfsops = { | |
512 | union_mount, | |
513 | union_start, | |
514 | union_unmount, | |
515 | union_root, | |
516 | union_quotactl, | |
517 | union_statfs, | |
518 | union_sync, | |
519 | union_vget, | |
520 | union_fhtovp, | |
521 | union_vptofh, | |
522 | union_init, | |
523 | union_sysctl, | |
524 | }; |