union_vfsops.c revision 1.15 1 1.15 christos /* $NetBSD: union_vfsops.c,v 1.15 2004/05/22 20:42:08 christos Exp $ */
2 1.1 jdolecek
3 1.1 jdolecek /*
4 1.1 jdolecek * Copyright (c) 1994 The Regents of the University of California.
5 1.9 agc * All rights reserved.
6 1.9 agc *
7 1.9 agc * This code is derived from software donated to Berkeley by
8 1.9 agc * Jan-Simon Pendry.
9 1.9 agc *
10 1.9 agc * Redistribution and use in source and binary forms, with or without
11 1.9 agc * modification, are permitted provided that the following conditions
12 1.9 agc * are met:
13 1.9 agc * 1. Redistributions of source code must retain the above copyright
14 1.9 agc * notice, this list of conditions and the following disclaimer.
15 1.9 agc * 2. Redistributions in binary form must reproduce the above copyright
16 1.9 agc * notice, this list of conditions and the following disclaimer in the
17 1.9 agc * documentation and/or other materials provided with the distribution.
18 1.9 agc * 3. Neither the name of the University nor the names of its contributors
19 1.9 agc * may be used to endorse or promote products derived from this software
20 1.9 agc * without specific prior written permission.
21 1.9 agc *
22 1.9 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.9 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.9 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.9 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.9 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.9 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.9 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.9 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.9 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.9 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.9 agc * SUCH DAMAGE.
33 1.9 agc *
34 1.9 agc * @(#)union_vfsops.c 8.20 (Berkeley) 5/20/95
35 1.9 agc */
36 1.9 agc
37 1.9 agc /*
38 1.1 jdolecek * Copyright (c) 1994 Jan-Simon Pendry.
39 1.1 jdolecek * All rights reserved.
40 1.1 jdolecek *
41 1.1 jdolecek * This code is derived from software donated to Berkeley by
42 1.1 jdolecek * Jan-Simon Pendry.
43 1.1 jdolecek *
44 1.1 jdolecek * Redistribution and use in source and binary forms, with or without
45 1.1 jdolecek * modification, are permitted provided that the following conditions
46 1.1 jdolecek * are met:
47 1.1 jdolecek * 1. Redistributions of source code must retain the above copyright
48 1.1 jdolecek * notice, this list of conditions and the following disclaimer.
49 1.1 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
50 1.1 jdolecek * notice, this list of conditions and the following disclaimer in the
51 1.1 jdolecek * documentation and/or other materials provided with the distribution.
52 1.1 jdolecek * 3. All advertising materials mentioning features or use of this software
53 1.1 jdolecek * must display the following acknowledgement:
54 1.1 jdolecek * This product includes software developed by the University of
55 1.1 jdolecek * California, Berkeley and its contributors.
56 1.1 jdolecek * 4. Neither the name of the University nor the names of its contributors
57 1.1 jdolecek * may be used to endorse or promote products derived from this software
58 1.1 jdolecek * without specific prior written permission.
59 1.1 jdolecek *
60 1.1 jdolecek * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 1.1 jdolecek * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 1.1 jdolecek * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 1.1 jdolecek * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 1.1 jdolecek * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 1.1 jdolecek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 1.1 jdolecek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 1.1 jdolecek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 1.1 jdolecek * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 1.1 jdolecek * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 1.1 jdolecek * SUCH DAMAGE.
71 1.1 jdolecek *
72 1.1 jdolecek * @(#)union_vfsops.c 8.20 (Berkeley) 5/20/95
73 1.1 jdolecek */
74 1.1 jdolecek
75 1.1 jdolecek /*
76 1.1 jdolecek * Union Layer
77 1.1 jdolecek */
78 1.1 jdolecek
79 1.1 jdolecek #include <sys/cdefs.h>
80 1.15 christos __KERNEL_RCSID(0, "$NetBSD: union_vfsops.c,v 1.15 2004/05/22 20:42:08 christos Exp $");
81 1.1 jdolecek
82 1.1 jdolecek #include <sys/param.h>
83 1.1 jdolecek #include <sys/systm.h>
84 1.10 atatat #include <sys/sysctl.h>
85 1.1 jdolecek #include <sys/time.h>
86 1.1 jdolecek #include <sys/proc.h>
87 1.1 jdolecek #include <sys/vnode.h>
88 1.1 jdolecek #include <sys/mount.h>
89 1.1 jdolecek #include <sys/namei.h>
90 1.1 jdolecek #include <sys/malloc.h>
91 1.1 jdolecek #include <sys/filedesc.h>
92 1.1 jdolecek #include <sys/queue.h>
93 1.1 jdolecek #include <sys/stat.h>
94 1.1 jdolecek
95 1.1 jdolecek #include <fs/union/union.h>
96 1.1 jdolecek
97 1.1 jdolecek int union_mount __P((struct mount *, const char *, void *, struct nameidata *,
98 1.8 fvdl struct proc *));
99 1.8 fvdl int union_start __P((struct mount *, int, struct proc *));
100 1.8 fvdl int union_unmount __P((struct mount *, int, struct proc *));
101 1.7 thorpej int union_root __P((struct mount *, struct vnode **));
102 1.13 jrf int union_quotactl __P((struct mount *, int, uid_t, void *, struct proc *));
103 1.12 christos int union_statvfs __P((struct mount *, struct statvfs *, struct proc *));
104 1.8 fvdl int union_sync __P((struct mount *, int, struct ucred *, struct proc *));
105 1.7 thorpej int union_vget __P((struct mount *, ino_t, struct vnode **));
106 1.7 thorpej int union_fhtovp __P((struct mount *, struct fid *, struct vnode **));
107 1.1 jdolecek int union_checkexp __P((struct mount *, struct mbuf *, int *,
108 1.1 jdolecek struct ucred **));
109 1.1 jdolecek int union_vptofh __P((struct vnode *, struct fid *));
110 1.1 jdolecek
111 1.1 jdolecek /*
112 1.1 jdolecek * Mount union filesystem
113 1.1 jdolecek */
114 1.1 jdolecek int
115 1.8 fvdl union_mount(mp, path, data, ndp, p)
116 1.1 jdolecek struct mount *mp;
117 1.1 jdolecek const char *path;
118 1.1 jdolecek void *data;
119 1.1 jdolecek struct nameidata *ndp;
120 1.8 fvdl struct proc *p;
121 1.1 jdolecek {
122 1.1 jdolecek int error = 0;
123 1.1 jdolecek struct union_args args;
124 1.1 jdolecek struct vnode *lowerrootvp = NULLVP;
125 1.1 jdolecek struct vnode *upperrootvp = NULLVP;
126 1.1 jdolecek struct union_mount *um = 0;
127 1.1 jdolecek struct ucred *cred = 0;
128 1.1 jdolecek char *cp;
129 1.1 jdolecek int len;
130 1.1 jdolecek size_t size;
131 1.1 jdolecek
132 1.1 jdolecek #ifdef UNION_DIAGNOSTIC
133 1.1 jdolecek printf("union_mount(mp = %p)\n", mp);
134 1.1 jdolecek #endif
135 1.1 jdolecek
136 1.1 jdolecek if (mp->mnt_flag & MNT_GETARGS) {
137 1.1 jdolecek um = MOUNTTOUNIONMOUNT(mp);
138 1.1 jdolecek if (um == NULL)
139 1.1 jdolecek return EIO;
140 1.1 jdolecek args.target = NULL;
141 1.1 jdolecek args.mntflags = um->um_op;
142 1.1 jdolecek return copyout(&args, data, sizeof(args));
143 1.1 jdolecek }
144 1.1 jdolecek /*
145 1.1 jdolecek * Update is a no-op
146 1.1 jdolecek */
147 1.1 jdolecek if (mp->mnt_flag & MNT_UPDATE) {
148 1.1 jdolecek /*
149 1.1 jdolecek * Need to provide.
150 1.1 jdolecek * 1. a way to convert between rdonly and rdwr mounts.
151 1.1 jdolecek * 2. support for nfs exports.
152 1.1 jdolecek */
153 1.1 jdolecek error = EOPNOTSUPP;
154 1.1 jdolecek goto bad;
155 1.1 jdolecek }
156 1.1 jdolecek
157 1.1 jdolecek /*
158 1.1 jdolecek * Get argument
159 1.1 jdolecek */
160 1.14 jrf error = copyin(data, &args, sizeof(struct union_args));
161 1.1 jdolecek if (error)
162 1.1 jdolecek goto bad;
163 1.1 jdolecek
164 1.1 jdolecek lowerrootvp = mp->mnt_vnodecovered;
165 1.1 jdolecek VREF(lowerrootvp);
166 1.1 jdolecek
167 1.1 jdolecek /*
168 1.1 jdolecek * Find upper node.
169 1.1 jdolecek */
170 1.1 jdolecek NDINIT(ndp, LOOKUP, FOLLOW,
171 1.8 fvdl UIO_USERSPACE, args.target, p);
172 1.1 jdolecek
173 1.1 jdolecek if ((error = namei(ndp)) != 0)
174 1.1 jdolecek goto bad;
175 1.1 jdolecek
176 1.1 jdolecek upperrootvp = ndp->ni_vp;
177 1.1 jdolecek
178 1.1 jdolecek if (upperrootvp->v_type != VDIR) {
179 1.1 jdolecek error = EINVAL;
180 1.1 jdolecek goto bad;
181 1.1 jdolecek }
182 1.1 jdolecek
183 1.1 jdolecek um = (struct union_mount *) malloc(sizeof(struct union_mount),
184 1.1 jdolecek M_UFSMNT, M_WAITOK); /* XXX */
185 1.1 jdolecek
186 1.1 jdolecek /*
187 1.1 jdolecek * Keep a held reference to the target vnodes.
188 1.1 jdolecek * They are vrele'd in union_unmount.
189 1.1 jdolecek *
190 1.1 jdolecek * Depending on the _BELOW flag, the filesystems are
191 1.1 jdolecek * viewed in a different order. In effect, this is the
192 1.1 jdolecek * same as providing a mount under option to the mount syscall.
193 1.1 jdolecek */
194 1.1 jdolecek
195 1.1 jdolecek um->um_op = args.mntflags & UNMNT_OPMASK;
196 1.1 jdolecek switch (um->um_op) {
197 1.1 jdolecek case UNMNT_ABOVE:
198 1.1 jdolecek um->um_lowervp = lowerrootvp;
199 1.1 jdolecek um->um_uppervp = upperrootvp;
200 1.1 jdolecek break;
201 1.1 jdolecek
202 1.1 jdolecek case UNMNT_BELOW:
203 1.1 jdolecek um->um_lowervp = upperrootvp;
204 1.1 jdolecek um->um_uppervp = lowerrootvp;
205 1.1 jdolecek break;
206 1.1 jdolecek
207 1.1 jdolecek case UNMNT_REPLACE:
208 1.1 jdolecek vrele(lowerrootvp);
209 1.1 jdolecek lowerrootvp = NULLVP;
210 1.1 jdolecek um->um_uppervp = upperrootvp;
211 1.1 jdolecek um->um_lowervp = lowerrootvp;
212 1.1 jdolecek break;
213 1.1 jdolecek
214 1.1 jdolecek default:
215 1.1 jdolecek error = EINVAL;
216 1.1 jdolecek goto bad;
217 1.1 jdolecek }
218 1.1 jdolecek
219 1.1 jdolecek /*
220 1.1 jdolecek * Unless the mount is readonly, ensure that the top layer
221 1.1 jdolecek * supports whiteout operations
222 1.1 jdolecek */
223 1.1 jdolecek if ((mp->mnt_flag & MNT_RDONLY) == 0) {
224 1.1 jdolecek error = VOP_WHITEOUT(um->um_uppervp, (struct componentname *) 0, LOOKUP);
225 1.1 jdolecek if (error)
226 1.1 jdolecek goto bad;
227 1.1 jdolecek }
228 1.1 jdolecek
229 1.1 jdolecek um->um_cred = p->p_ucred;
230 1.1 jdolecek crhold(um->um_cred);
231 1.1 jdolecek um->um_cmode = UN_DIRMODE &~ p->p_cwdi->cwdi_cmask;
232 1.1 jdolecek
233 1.1 jdolecek /*
234 1.1 jdolecek * Depending on what you think the MNT_LOCAL flag might mean,
235 1.1 jdolecek * you may want the && to be || on the conditional below.
236 1.1 jdolecek * At the moment it has been defined that the filesystem is
237 1.1 jdolecek * only local if it is all local, ie the MNT_LOCAL flag implies
238 1.1 jdolecek * that the entire namespace is local. If you think the MNT_LOCAL
239 1.1 jdolecek * flag implies that some of the files might be stored locally
240 1.1 jdolecek * then you will want to change the conditional.
241 1.1 jdolecek */
242 1.1 jdolecek if (um->um_op == UNMNT_ABOVE) {
243 1.1 jdolecek if (((um->um_lowervp == NULLVP) ||
244 1.1 jdolecek (um->um_lowervp->v_mount->mnt_flag & MNT_LOCAL)) &&
245 1.1 jdolecek (um->um_uppervp->v_mount->mnt_flag & MNT_LOCAL))
246 1.1 jdolecek mp->mnt_flag |= MNT_LOCAL;
247 1.1 jdolecek }
248 1.1 jdolecek
249 1.1 jdolecek /*
250 1.1 jdolecek * Copy in the upper layer's RDONLY flag. This is for the benefit
251 1.1 jdolecek * of lookup() which explicitly checks the flag, rather than asking
252 1.1 jdolecek * the filesystem for it's own opinion. This means, that an update
253 1.1 jdolecek * mount of the underlying filesystem to go from rdonly to rdwr
254 1.1 jdolecek * will leave the unioned view as read-only.
255 1.1 jdolecek */
256 1.1 jdolecek mp->mnt_flag |= (um->um_uppervp->v_mount->mnt_flag & MNT_RDONLY);
257 1.1 jdolecek
258 1.1 jdolecek mp->mnt_data = um;
259 1.1 jdolecek vfs_getnewfsid(mp);
260 1.1 jdolecek
261 1.12 christos error = set_statvfs_info( path, UIO_USERSPACE, NULL, UIO_USERSPACE,
262 1.8 fvdl mp, p);
263 1.4 christos if (error)
264 1.4 christos goto bad;
265 1.1 jdolecek
266 1.1 jdolecek switch (um->um_op) {
267 1.1 jdolecek case UNMNT_ABOVE:
268 1.1 jdolecek cp = "<above>:";
269 1.1 jdolecek break;
270 1.1 jdolecek case UNMNT_BELOW:
271 1.1 jdolecek cp = "<below>:";
272 1.1 jdolecek break;
273 1.1 jdolecek case UNMNT_REPLACE:
274 1.1 jdolecek cp = "";
275 1.1 jdolecek break;
276 1.1 jdolecek default:
277 1.1 jdolecek cp = "<invalid>:";
278 1.1 jdolecek #ifdef DIAGNOSTIC
279 1.1 jdolecek panic("union_mount: bad um_op");
280 1.1 jdolecek #endif
281 1.1 jdolecek break;
282 1.1 jdolecek }
283 1.1 jdolecek len = strlen(cp);
284 1.1 jdolecek memcpy(mp->mnt_stat.f_mntfromname, cp, len);
285 1.1 jdolecek
286 1.1 jdolecek cp = mp->mnt_stat.f_mntfromname + len;
287 1.1 jdolecek len = MNAMELEN - len;
288 1.1 jdolecek
289 1.1 jdolecek (void) copyinstr(args.target, cp, len - 1, &size);
290 1.1 jdolecek memset(cp + size, 0, len - size);
291 1.1 jdolecek
292 1.1 jdolecek #ifdef UNION_DIAGNOSTIC
293 1.1 jdolecek printf("union_mount: from %s, on %s\n",
294 1.1 jdolecek mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
295 1.1 jdolecek #endif
296 1.2 jdolecek
297 1.2 jdolecek /* Setup the readdir hook if it's not set already */
298 1.2 jdolecek if (!vn_union_readdir_hook)
299 1.2 jdolecek vn_union_readdir_hook = union_readdirhook;
300 1.2 jdolecek
301 1.1 jdolecek return (0);
302 1.1 jdolecek
303 1.1 jdolecek bad:
304 1.1 jdolecek if (um)
305 1.1 jdolecek free(um, M_UFSMNT);
306 1.1 jdolecek if (cred)
307 1.1 jdolecek crfree(cred);
308 1.1 jdolecek if (upperrootvp)
309 1.1 jdolecek vrele(upperrootvp);
310 1.1 jdolecek if (lowerrootvp)
311 1.1 jdolecek vrele(lowerrootvp);
312 1.1 jdolecek return (error);
313 1.1 jdolecek }
314 1.1 jdolecek
315 1.1 jdolecek /*
316 1.1 jdolecek * VFS start. Nothing needed here - the start routine
317 1.1 jdolecek * on the underlying filesystem(s) will have been called
318 1.1 jdolecek * when that filesystem was mounted.
319 1.1 jdolecek */
320 1.1 jdolecek /*ARGSUSED*/
321 1.1 jdolecek int
322 1.8 fvdl union_start(mp, flags, p)
323 1.1 jdolecek struct mount *mp;
324 1.1 jdolecek int flags;
325 1.8 fvdl struct proc *p;
326 1.1 jdolecek {
327 1.1 jdolecek
328 1.1 jdolecek return (0);
329 1.1 jdolecek }
330 1.1 jdolecek
331 1.1 jdolecek /*
332 1.1 jdolecek * Free reference to union layer
333 1.1 jdolecek */
334 1.1 jdolecek int
335 1.8 fvdl union_unmount(mp, mntflags, p)
336 1.1 jdolecek struct mount *mp;
337 1.1 jdolecek int mntflags;
338 1.8 fvdl struct proc *p;
339 1.1 jdolecek {
340 1.1 jdolecek struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
341 1.1 jdolecek struct vnode *um_rootvp;
342 1.1 jdolecek int error;
343 1.1 jdolecek int freeing;
344 1.1 jdolecek
345 1.1 jdolecek #ifdef UNION_DIAGNOSTIC
346 1.1 jdolecek printf("union_unmount(mp = %p)\n", mp);
347 1.1 jdolecek #endif
348 1.1 jdolecek
349 1.7 thorpej if ((error = union_root(mp, &um_rootvp)) != 0)
350 1.1 jdolecek return (error);
351 1.1 jdolecek
352 1.1 jdolecek /*
353 1.1 jdolecek * Keep flushing vnodes from the mount list.
354 1.1 jdolecek * This is needed because of the un_pvp held
355 1.1 jdolecek * reference to the parent vnode.
356 1.1 jdolecek * If more vnodes have been freed on a given pass,
357 1.1 jdolecek * the try again. The loop will iterate at most
358 1.1 jdolecek * (d) times, where (d) is the maximum tree depth
359 1.1 jdolecek * in the filesystem.
360 1.1 jdolecek */
361 1.1 jdolecek for (freeing = 0; vflush(mp, um_rootvp, 0) != 0;) {
362 1.1 jdolecek struct vnode *vp;
363 1.1 jdolecek int n;
364 1.1 jdolecek
365 1.1 jdolecek /* count #vnodes held on mount list */
366 1.1 jdolecek for (n = 0, vp = mp->mnt_vnodelist.lh_first;
367 1.1 jdolecek vp != NULLVP;
368 1.1 jdolecek vp = vp->v_mntvnodes.le_next)
369 1.1 jdolecek n++;
370 1.1 jdolecek
371 1.1 jdolecek /* if this is unchanged then stop */
372 1.1 jdolecek if (n == freeing)
373 1.1 jdolecek break;
374 1.1 jdolecek
375 1.1 jdolecek /* otherwise try once more time */
376 1.1 jdolecek freeing = n;
377 1.1 jdolecek }
378 1.1 jdolecek
379 1.1 jdolecek /*
380 1.1 jdolecek * Ok, now that we've tried doing it gently, get out the hammer.
381 1.1 jdolecek */
382 1.1 jdolecek
383 1.1 jdolecek if (mntflags & MNT_FORCE)
384 1.1 jdolecek vflush(mp, um_rootvp, FORCECLOSE);
385 1.1 jdolecek
386 1.1 jdolecek
387 1.1 jdolecek /* At this point the root vnode should have a single reference */
388 1.1 jdolecek if (um_rootvp->v_usecount > 1) {
389 1.1 jdolecek vput(um_rootvp);
390 1.1 jdolecek return (EBUSY);
391 1.1 jdolecek }
392 1.1 jdolecek
393 1.1 jdolecek #ifdef UNION_DIAGNOSTIC
394 1.1 jdolecek vprint("union root", um_rootvp);
395 1.1 jdolecek #endif
396 1.1 jdolecek /*
397 1.1 jdolecek * Discard references to upper and lower target vnodes.
398 1.1 jdolecek */
399 1.1 jdolecek if (um->um_lowervp)
400 1.1 jdolecek vrele(um->um_lowervp);
401 1.1 jdolecek vrele(um->um_uppervp);
402 1.1 jdolecek crfree(um->um_cred);
403 1.1 jdolecek /*
404 1.1 jdolecek * Release reference on underlying root vnode
405 1.1 jdolecek */
406 1.1 jdolecek vput(um_rootvp);
407 1.1 jdolecek /*
408 1.1 jdolecek * And blow it away for future re-use
409 1.1 jdolecek */
410 1.1 jdolecek vgone(um_rootvp);
411 1.1 jdolecek /*
412 1.1 jdolecek * Finally, throw away the union_mount structure
413 1.1 jdolecek */
414 1.1 jdolecek free(mp->mnt_data, M_UFSMNT); /* XXX */
415 1.1 jdolecek mp->mnt_data = 0;
416 1.1 jdolecek return (0);
417 1.1 jdolecek }
418 1.1 jdolecek
419 1.1 jdolecek int
420 1.7 thorpej union_root(mp, vpp)
421 1.1 jdolecek struct mount *mp;
422 1.1 jdolecek struct vnode **vpp;
423 1.1 jdolecek {
424 1.1 jdolecek struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
425 1.1 jdolecek int error;
426 1.1 jdolecek int loselock;
427 1.1 jdolecek
428 1.1 jdolecek /*
429 1.1 jdolecek * Return locked reference to root.
430 1.1 jdolecek */
431 1.1 jdolecek VREF(um->um_uppervp);
432 1.1 jdolecek if ((um->um_op == UNMNT_BELOW) &&
433 1.1 jdolecek VOP_ISLOCKED(um->um_uppervp)) {
434 1.1 jdolecek loselock = 1;
435 1.1 jdolecek } else {
436 1.1 jdolecek vn_lock(um->um_uppervp, LK_EXCLUSIVE | LK_RETRY);
437 1.1 jdolecek loselock = 0;
438 1.1 jdolecek }
439 1.1 jdolecek if (um->um_lowervp)
440 1.1 jdolecek VREF(um->um_lowervp);
441 1.1 jdolecek error = union_allocvp(vpp, mp,
442 1.1 jdolecek (struct vnode *) 0,
443 1.1 jdolecek (struct vnode *) 0,
444 1.1 jdolecek (struct componentname *) 0,
445 1.1 jdolecek um->um_uppervp,
446 1.1 jdolecek um->um_lowervp,
447 1.7 thorpej 1);
448 1.1 jdolecek
449 1.1 jdolecek if (error) {
450 1.1 jdolecek if (!loselock)
451 1.1 jdolecek VOP_UNLOCK(um->um_uppervp, 0);
452 1.1 jdolecek vrele(um->um_uppervp);
453 1.1 jdolecek if (um->um_lowervp)
454 1.1 jdolecek vrele(um->um_lowervp);
455 1.1 jdolecek } else {
456 1.1 jdolecek if (loselock)
457 1.1 jdolecek VTOUNION(*vpp)->un_flags &= ~UN_ULOCK;
458 1.1 jdolecek }
459 1.1 jdolecek
460 1.1 jdolecek return (error);
461 1.1 jdolecek }
462 1.1 jdolecek
463 1.1 jdolecek /*ARGSUSED*/
464 1.1 jdolecek int
465 1.8 fvdl union_quotactl(mp, cmd, uid, arg, p)
466 1.1 jdolecek struct mount *mp;
467 1.1 jdolecek int cmd;
468 1.1 jdolecek uid_t uid;
469 1.13 jrf void *arg;
470 1.8 fvdl struct proc *p;
471 1.1 jdolecek {
472 1.1 jdolecek
473 1.1 jdolecek return (EOPNOTSUPP);
474 1.1 jdolecek }
475 1.1 jdolecek
476 1.1 jdolecek int
477 1.12 christos union_statvfs(mp, sbp, p)
478 1.1 jdolecek struct mount *mp;
479 1.12 christos struct statvfs *sbp;
480 1.8 fvdl struct proc *p;
481 1.1 jdolecek {
482 1.1 jdolecek int error;
483 1.1 jdolecek struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
484 1.15 christos unsigned long l_bsize;
485 1.15 christos fsblkcnt_t l_blocks, l_bfree;
486 1.1 jdolecek
487 1.1 jdolecek #ifdef UNION_DIAGNOSTIC
488 1.12 christos printf("union_statvfs(mp = %p, lvp = %p, uvp = %p)\n", mp,
489 1.1 jdolecek um->um_lowervp, um->um_uppervp);
490 1.1 jdolecek #endif
491 1.1 jdolecek
492 1.1 jdolecek if (um->um_lowervp) {
493 1.15 christos error = VFS_STATVFS(um->um_lowervp->v_mount, sbp, p);
494 1.1 jdolecek if (error)
495 1.1 jdolecek return (error);
496 1.15 christos l_bsize = sbp->f_bsize;
497 1.15 christos l_blocks = sbp->f_blocks - sbp->f_bfree;
498 1.15 christos l_files = sbp->f_files - sbp->f_ffree;
499 1.15 christos } else {
500 1.15 christos l_bsize = 0;
501 1.15 christos l_blocks = 0;
502 1.15 christos l_files = 0;
503 1.1 jdolecek }
504 1.1 jdolecek
505 1.15 christos error = VFS_STATVFS(um->um_uppervp->v_mount, sbp, p);
506 1.1 jdolecek if (error)
507 1.1 jdolecek return (error);
508 1.1 jdolecek /*
509 1.1 jdolecek * The "total" fields count total resources in all layers,
510 1.1 jdolecek * the "free" fields count only those resources which are
511 1.1 jdolecek * free in the upper layer (since only the upper layer
512 1.1 jdolecek * is writable).
513 1.1 jdolecek */
514 1.15 christos sbp->f_blocks += l_blocks * l_bsize / sbp->f_bsize;
515 1.15 christos sbp->f_files += l_files;
516 1.1 jdolecek
517 1.12 christos copy_statvfs_info(sbp, mp);
518 1.1 jdolecek return (0);
519 1.1 jdolecek }
520 1.1 jdolecek
521 1.1 jdolecek /*ARGSUSED*/
522 1.1 jdolecek int
523 1.8 fvdl union_sync(mp, waitfor, cred, p)
524 1.1 jdolecek struct mount *mp;
525 1.1 jdolecek int waitfor;
526 1.1 jdolecek struct ucred *cred;
527 1.8 fvdl struct proc *p;
528 1.1 jdolecek {
529 1.1 jdolecek
530 1.1 jdolecek /*
531 1.1 jdolecek * XXX - Assumes no data cached at union layer.
532 1.1 jdolecek */
533 1.1 jdolecek return (0);
534 1.1 jdolecek }
535 1.1 jdolecek
536 1.1 jdolecek /*ARGSUSED*/
537 1.1 jdolecek int
538 1.7 thorpej union_vget(mp, ino, vpp)
539 1.1 jdolecek struct mount *mp;
540 1.1 jdolecek ino_t ino;
541 1.1 jdolecek struct vnode **vpp;
542 1.1 jdolecek {
543 1.1 jdolecek
544 1.1 jdolecek return (EOPNOTSUPP);
545 1.1 jdolecek }
546 1.1 jdolecek
547 1.1 jdolecek /*ARGSUSED*/
548 1.1 jdolecek int
549 1.7 thorpej union_fhtovp(mp, fidp, vpp)
550 1.1 jdolecek struct mount *mp;
551 1.1 jdolecek struct fid *fidp;
552 1.1 jdolecek struct vnode **vpp;
553 1.1 jdolecek {
554 1.1 jdolecek
555 1.1 jdolecek return (EOPNOTSUPP);
556 1.1 jdolecek }
557 1.1 jdolecek
558 1.1 jdolecek /*ARGSUSED*/
559 1.1 jdolecek int
560 1.1 jdolecek union_checkexp(mp, nam, exflagsp, credanonp)
561 1.1 jdolecek struct mount *mp;
562 1.1 jdolecek struct mbuf *nam;
563 1.1 jdolecek int *exflagsp;
564 1.1 jdolecek struct ucred **credanonp;
565 1.1 jdolecek {
566 1.1 jdolecek
567 1.1 jdolecek return (EOPNOTSUPP);
568 1.1 jdolecek }
569 1.1 jdolecek
570 1.1 jdolecek /*ARGSUSED*/
571 1.1 jdolecek int
572 1.1 jdolecek union_vptofh(vp, fhp)
573 1.1 jdolecek struct vnode *vp;
574 1.1 jdolecek struct fid *fhp;
575 1.1 jdolecek {
576 1.1 jdolecek
577 1.1 jdolecek return (EOPNOTSUPP);
578 1.1 jdolecek }
579 1.1 jdolecek
580 1.10 atatat SYSCTL_SETUP(sysctl_vfs_union_setup, "sysctl vfs.union subtree setup")
581 1.1 jdolecek {
582 1.10 atatat
583 1.11 atatat sysctl_createv(clog, 0, NULL, NULL,
584 1.11 atatat CTLFLAG_PERMANENT,
585 1.10 atatat CTLTYPE_NODE, "vfs", NULL,
586 1.10 atatat NULL, 0, NULL, 0,
587 1.10 atatat CTL_VFS, CTL_EOL);
588 1.11 atatat sysctl_createv(clog, 0, NULL, NULL,
589 1.11 atatat CTLFLAG_PERMANENT,
590 1.10 atatat CTLTYPE_NODE, "union", NULL,
591 1.10 atatat NULL, 0, NULL, 0,
592 1.10 atatat CTL_VFS, 15, CTL_EOL);
593 1.10 atatat /*
594 1.10 atatat * XXX the "15" above could be dynamic, thereby eliminating
595 1.10 atatat * one more instance of the "number to vfs" mapping problem,
596 1.10 atatat * but "15" is the order as taken from sys/mount.h
597 1.10 atatat */
598 1.1 jdolecek }
599 1.1 jdolecek
600 1.1 jdolecek extern const struct vnodeopv_desc union_vnodeop_opv_desc;
601 1.1 jdolecek
602 1.1 jdolecek const struct vnodeopv_desc * const union_vnodeopv_descs[] = {
603 1.1 jdolecek &union_vnodeop_opv_desc,
604 1.1 jdolecek NULL,
605 1.1 jdolecek };
606 1.1 jdolecek
607 1.1 jdolecek struct vfsops union_vfsops = {
608 1.1 jdolecek MOUNT_UNION,
609 1.1 jdolecek union_mount,
610 1.1 jdolecek union_start,
611 1.1 jdolecek union_unmount,
612 1.1 jdolecek union_root,
613 1.1 jdolecek union_quotactl,
614 1.12 christos union_statvfs,
615 1.1 jdolecek union_sync,
616 1.1 jdolecek union_vget,
617 1.1 jdolecek union_fhtovp,
618 1.1 jdolecek union_vptofh,
619 1.1 jdolecek union_init,
620 1.3 jdolecek NULL, /* vfs_reinit */
621 1.1 jdolecek union_done,
622 1.10 atatat NULL,
623 1.1 jdolecek NULL, /* vfs_mountroot */
624 1.1 jdolecek union_checkexp,
625 1.1 jdolecek union_vnodeopv_descs,
626 1.1 jdolecek };
627