union_subr.c revision 1.48 1 1.48 hannken /* $NetBSD: union_subr.c,v 1.48 2011/08/12 17:41:17 hannken Exp $ */
2 1.1 jdolecek
3 1.1 jdolecek /*
4 1.1 jdolecek * Copyright (c) 1994
5 1.1 jdolecek * The Regents of the University of California. All rights reserved.
6 1.1 jdolecek *
7 1.1 jdolecek * This code is derived from software contributed to Berkeley by
8 1.1 jdolecek * Jan-Simon Pendry.
9 1.1 jdolecek *
10 1.1 jdolecek * Redistribution and use in source and binary forms, with or without
11 1.1 jdolecek * modification, are permitted provided that the following conditions
12 1.1 jdolecek * are met:
13 1.1 jdolecek * 1. Redistributions of source code must retain the above copyright
14 1.1 jdolecek * notice, this list of conditions and the following disclaimer.
15 1.1 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 jdolecek * notice, this list of conditions and the following disclaimer in the
17 1.1 jdolecek * documentation and/or other materials provided with the distribution.
18 1.7 agc * 3. Neither the name of the University nor the names of its contributors
19 1.7 agc * may be used to endorse or promote products derived from this software
20 1.7 agc * without specific prior written permission.
21 1.7 agc *
22 1.7 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.7 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.7 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.7 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.7 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.7 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.7 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.7 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.7 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.7 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.7 agc * SUCH DAMAGE.
33 1.7 agc *
34 1.7 agc * @(#)union_subr.c 8.20 (Berkeley) 5/20/95
35 1.7 agc */
36 1.7 agc
37 1.7 agc /*
38 1.7 agc * Copyright (c) 1994 Jan-Simon Pendry
39 1.7 agc *
40 1.7 agc * This code is derived from software contributed to Berkeley by
41 1.7 agc * Jan-Simon Pendry.
42 1.7 agc *
43 1.7 agc * Redistribution and use in source and binary forms, with or without
44 1.7 agc * modification, are permitted provided that the following conditions
45 1.7 agc * are met:
46 1.7 agc * 1. Redistributions of source code must retain the above copyright
47 1.7 agc * notice, this list of conditions and the following disclaimer.
48 1.7 agc * 2. Redistributions in binary form must reproduce the above copyright
49 1.7 agc * notice, this list of conditions and the following disclaimer in the
50 1.7 agc * documentation and/or other materials provided with the distribution.
51 1.1 jdolecek * 3. All advertising materials mentioning features or use of this software
52 1.1 jdolecek * must display the following acknowledgement:
53 1.1 jdolecek * This product includes software developed by the University of
54 1.1 jdolecek * California, Berkeley and its contributors.
55 1.1 jdolecek * 4. Neither the name of the University nor the names of its contributors
56 1.1 jdolecek * may be used to endorse or promote products derived from this software
57 1.1 jdolecek * without specific prior written permission.
58 1.1 jdolecek *
59 1.1 jdolecek * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 1.1 jdolecek * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 1.1 jdolecek * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 1.1 jdolecek * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 1.1 jdolecek * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 1.1 jdolecek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 1.1 jdolecek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 1.1 jdolecek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 1.1 jdolecek * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 1.1 jdolecek * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 1.1 jdolecek * SUCH DAMAGE.
70 1.1 jdolecek *
71 1.1 jdolecek * @(#)union_subr.c 8.20 (Berkeley) 5/20/95
72 1.1 jdolecek */
73 1.1 jdolecek
74 1.1 jdolecek #include <sys/cdefs.h>
75 1.48 hannken __KERNEL_RCSID(0, "$NetBSD: union_subr.c,v 1.48 2011/08/12 17:41:17 hannken Exp $");
76 1.1 jdolecek
77 1.1 jdolecek #include <sys/param.h>
78 1.1 jdolecek #include <sys/systm.h>
79 1.1 jdolecek #include <sys/proc.h>
80 1.1 jdolecek #include <sys/time.h>
81 1.1 jdolecek #include <sys/kernel.h>
82 1.1 jdolecek #include <sys/vnode.h>
83 1.1 jdolecek #include <sys/namei.h>
84 1.1 jdolecek #include <sys/malloc.h>
85 1.44 hannken #include <sys/dirent.h>
86 1.1 jdolecek #include <sys/file.h>
87 1.1 jdolecek #include <sys/filedesc.h>
88 1.1 jdolecek #include <sys/queue.h>
89 1.1 jdolecek #include <sys/mount.h>
90 1.1 jdolecek #include <sys/stat.h>
91 1.18 elad #include <sys/kauth.h>
92 1.1 jdolecek
93 1.1 jdolecek #include <uvm/uvm_extern.h>
94 1.1 jdolecek
95 1.1 jdolecek #include <fs/union/union.h>
96 1.47 hannken #include <miscfs/specfs/specdev.h>
97 1.1 jdolecek
98 1.1 jdolecek /* must be power of two, otherwise change UNION_HASH() */
99 1.1 jdolecek #define NHASH 32
100 1.1 jdolecek
101 1.1 jdolecek /* unsigned int ... */
102 1.1 jdolecek #define UNION_HASH(u, l) \
103 1.1 jdolecek (((((unsigned long) (u)) + ((unsigned long) l)) >> 8) & (NHASH-1))
104 1.1 jdolecek
105 1.1 jdolecek static LIST_HEAD(unhead, union_node) unhead[NHASH];
106 1.1 jdolecek static int unvplock[NHASH];
107 1.1 jdolecek
108 1.14 xtraeme static int union_list_lock(int);
109 1.14 xtraeme static void union_list_unlock(int);
110 1.14 xtraeme void union_updatevp(struct union_node *, struct vnode *, struct vnode *);
111 1.14 xtraeme static int union_relookup(struct union_mount *, struct vnode *,
112 1.1 jdolecek struct vnode **, struct componentname *,
113 1.40 dholland struct componentname *, char **,
114 1.40 dholland const char *, int);
115 1.18 elad int union_vn_close(struct vnode *, int, kauth_cred_t, struct lwp *);
116 1.14 xtraeme static void union_dircache_r(struct vnode *, struct vnode ***, int *);
117 1.15 christos struct vnode *union_dircache(struct vnode *, struct lwp *);
118 1.1 jdolecek
119 1.1 jdolecek void
120 1.32 matt union_init(void)
121 1.1 jdolecek {
122 1.1 jdolecek int i;
123 1.1 jdolecek
124 1.1 jdolecek for (i = 0; i < NHASH; i++)
125 1.1 jdolecek LIST_INIT(&unhead[i]);
126 1.9 jrf memset(unvplock, 0, sizeof(unvplock));
127 1.1 jdolecek }
128 1.1 jdolecek
129 1.1 jdolecek /*
130 1.1 jdolecek * Free global unionfs resources.
131 1.1 jdolecek */
132 1.1 jdolecek void
133 1.32 matt union_done(void)
134 1.1 jdolecek {
135 1.12 perry
136 1.2 jdolecek /* Make sure to unset the readdir hook. */
137 1.2 jdolecek vn_union_readdir_hook = NULL;
138 1.1 jdolecek }
139 1.1 jdolecek
140 1.1 jdolecek static int
141 1.32 matt union_list_lock(int ix)
142 1.1 jdolecek {
143 1.1 jdolecek
144 1.1 jdolecek if (unvplock[ix] & UN_LOCKED) {
145 1.1 jdolecek unvplock[ix] |= UN_WANTED;
146 1.1 jdolecek (void) tsleep(&unvplock[ix], PINOD, "unionlk", 0);
147 1.1 jdolecek return (1);
148 1.1 jdolecek }
149 1.1 jdolecek
150 1.1 jdolecek unvplock[ix] |= UN_LOCKED;
151 1.1 jdolecek
152 1.1 jdolecek return (0);
153 1.1 jdolecek }
154 1.1 jdolecek
155 1.1 jdolecek static void
156 1.32 matt union_list_unlock(int ix)
157 1.1 jdolecek {
158 1.1 jdolecek
159 1.1 jdolecek unvplock[ix] &= ~UN_LOCKED;
160 1.1 jdolecek
161 1.1 jdolecek if (unvplock[ix] & UN_WANTED) {
162 1.1 jdolecek unvplock[ix] &= ~UN_WANTED;
163 1.9 jrf wakeup(&unvplock[ix]);
164 1.1 jdolecek }
165 1.1 jdolecek }
166 1.1 jdolecek
167 1.1 jdolecek void
168 1.32 matt union_updatevp(struct union_node *un, struct vnode *uppervp,
169 1.32 matt struct vnode *lowervp)
170 1.1 jdolecek {
171 1.1 jdolecek int ohash = UNION_HASH(un->un_uppervp, un->un_lowervp);
172 1.1 jdolecek int nhash = UNION_HASH(uppervp, lowervp);
173 1.1 jdolecek int docache = (lowervp != NULLVP || uppervp != NULLVP);
174 1.1 jdolecek int lhash, uhash;
175 1.1 jdolecek
176 1.1 jdolecek /*
177 1.1 jdolecek * Ensure locking is ordered from lower to higher
178 1.1 jdolecek * to avoid deadlocks.
179 1.1 jdolecek */
180 1.1 jdolecek if (nhash < ohash) {
181 1.1 jdolecek lhash = nhash;
182 1.1 jdolecek uhash = ohash;
183 1.1 jdolecek } else {
184 1.1 jdolecek lhash = ohash;
185 1.1 jdolecek uhash = nhash;
186 1.1 jdolecek }
187 1.1 jdolecek
188 1.1 jdolecek if (lhash != uhash)
189 1.1 jdolecek while (union_list_lock(lhash))
190 1.1 jdolecek continue;
191 1.1 jdolecek
192 1.1 jdolecek while (union_list_lock(uhash))
193 1.1 jdolecek continue;
194 1.1 jdolecek
195 1.1 jdolecek if (ohash != nhash || !docache) {
196 1.1 jdolecek if (un->un_flags & UN_CACHED) {
197 1.1 jdolecek un->un_flags &= ~UN_CACHED;
198 1.1 jdolecek LIST_REMOVE(un, un_cache);
199 1.1 jdolecek }
200 1.1 jdolecek }
201 1.1 jdolecek
202 1.1 jdolecek if (ohash != nhash)
203 1.1 jdolecek union_list_unlock(ohash);
204 1.1 jdolecek
205 1.1 jdolecek if (un->un_lowervp != lowervp) {
206 1.1 jdolecek if (un->un_lowervp) {
207 1.1 jdolecek vrele(un->un_lowervp);
208 1.1 jdolecek if (un->un_path) {
209 1.1 jdolecek free(un->un_path, M_TEMP);
210 1.1 jdolecek un->un_path = 0;
211 1.1 jdolecek }
212 1.1 jdolecek if (un->un_dirvp) {
213 1.1 jdolecek vrele(un->un_dirvp);
214 1.1 jdolecek un->un_dirvp = NULLVP;
215 1.1 jdolecek }
216 1.1 jdolecek }
217 1.1 jdolecek un->un_lowervp = lowervp;
218 1.1 jdolecek un->un_lowersz = VNOVAL;
219 1.1 jdolecek }
220 1.1 jdolecek
221 1.1 jdolecek if (un->un_uppervp != uppervp) {
222 1.1 jdolecek if (un->un_uppervp)
223 1.1 jdolecek vrele(un->un_uppervp);
224 1.1 jdolecek
225 1.1 jdolecek un->un_uppervp = uppervp;
226 1.1 jdolecek un->un_uppersz = VNOVAL;
227 1.46 hannken /* Update union vnode interlock. */
228 1.46 hannken if (uppervp != NULL) {
229 1.46 hannken mutex_obj_hold(uppervp->v_interlock);
230 1.46 hannken uvm_obj_setlock(&UNIONTOV(un)->v_uobj,
231 1.46 hannken uppervp->v_interlock);
232 1.46 hannken }
233 1.1 jdolecek }
234 1.1 jdolecek
235 1.1 jdolecek if (docache && (ohash != nhash)) {
236 1.1 jdolecek LIST_INSERT_HEAD(&unhead[nhash], un, un_cache);
237 1.1 jdolecek un->un_flags |= UN_CACHED;
238 1.1 jdolecek }
239 1.1 jdolecek
240 1.1 jdolecek union_list_unlock(nhash);
241 1.1 jdolecek }
242 1.1 jdolecek
243 1.1 jdolecek void
244 1.32 matt union_newlower(struct union_node *un, struct vnode *lowervp)
245 1.1 jdolecek {
246 1.1 jdolecek
247 1.1 jdolecek union_updatevp(un, un->un_uppervp, lowervp);
248 1.1 jdolecek }
249 1.1 jdolecek
250 1.1 jdolecek void
251 1.32 matt union_newupper(struct union_node *un, struct vnode *uppervp)
252 1.1 jdolecek {
253 1.1 jdolecek
254 1.1 jdolecek union_updatevp(un, uppervp, un->un_lowervp);
255 1.1 jdolecek }
256 1.1 jdolecek
257 1.1 jdolecek /*
258 1.1 jdolecek * Keep track of size changes in the underlying vnodes.
259 1.1 jdolecek * If the size changes, then callback to the vm layer
260 1.1 jdolecek * giving priority to the upper layer size.
261 1.1 jdolecek */
262 1.1 jdolecek void
263 1.32 matt union_newsize(struct vnode *vp, off_t uppersz, off_t lowersz)
264 1.1 jdolecek {
265 1.1 jdolecek struct union_node *un;
266 1.1 jdolecek off_t sz;
267 1.1 jdolecek
268 1.1 jdolecek /* only interested in regular files */
269 1.26 pooka if (vp->v_type != VREG) {
270 1.26 pooka uvm_vnp_setsize(vp, 0);
271 1.1 jdolecek return;
272 1.26 pooka }
273 1.1 jdolecek
274 1.1 jdolecek un = VTOUNION(vp);
275 1.1 jdolecek sz = VNOVAL;
276 1.1 jdolecek
277 1.1 jdolecek if ((uppersz != VNOVAL) && (un->un_uppersz != uppersz)) {
278 1.1 jdolecek un->un_uppersz = uppersz;
279 1.1 jdolecek if (sz == VNOVAL)
280 1.1 jdolecek sz = un->un_uppersz;
281 1.1 jdolecek }
282 1.1 jdolecek
283 1.1 jdolecek if ((lowersz != VNOVAL) && (un->un_lowersz != lowersz)) {
284 1.1 jdolecek un->un_lowersz = lowersz;
285 1.1 jdolecek if (sz == VNOVAL)
286 1.1 jdolecek sz = un->un_lowersz;
287 1.1 jdolecek }
288 1.1 jdolecek
289 1.1 jdolecek if (sz != VNOVAL) {
290 1.1 jdolecek #ifdef UNION_DIAGNOSTIC
291 1.1 jdolecek printf("union: %s size now %qd\n",
292 1.1 jdolecek uppersz != VNOVAL ? "upper" : "lower", sz);
293 1.1 jdolecek #endif
294 1.1 jdolecek uvm_vnp_setsize(vp, sz);
295 1.1 jdolecek }
296 1.1 jdolecek }
297 1.1 jdolecek
298 1.1 jdolecek /*
299 1.1 jdolecek * allocate a union_node/vnode pair. the vnode is
300 1.1 jdolecek * referenced and locked. the new vnode is returned
301 1.1 jdolecek * via (vpp). (mp) is the mountpoint of the union filesystem,
302 1.1 jdolecek * (dvp) is the parent directory where the upper layer object
303 1.1 jdolecek * should exist (but doesn't) and (cnp) is the componentname
304 1.1 jdolecek * information which is partially copied to allow the upper
305 1.1 jdolecek * layer object to be created at a later time. (uppervp)
306 1.1 jdolecek * and (lowervp) reference the upper and lower layer objects
307 1.1 jdolecek * being mapped. either, but not both, can be nil.
308 1.1 jdolecek * if supplied, (uppervp) is locked.
309 1.1 jdolecek * the reference is either maintained in the new union_node
310 1.1 jdolecek * object which is allocated, or they are vrele'd.
311 1.1 jdolecek *
312 1.1 jdolecek * all union_nodes are maintained on a singly-linked
313 1.1 jdolecek * list. new nodes are only allocated when they cannot
314 1.1 jdolecek * be found on this list. entries on the list are
315 1.1 jdolecek * removed when the vfs reclaim entry is called.
316 1.1 jdolecek *
317 1.1 jdolecek * a single lock is kept for the entire list. this is
318 1.1 jdolecek * needed because the getnewvnode() function can block
319 1.1 jdolecek * waiting for a vnode to become free, in which case there
320 1.1 jdolecek * may be more than one process trying to get the same
321 1.1 jdolecek * vnode. this lock is only taken if we are going to
322 1.1 jdolecek * call getnewvnode, since the kernel itself is single-threaded.
323 1.1 jdolecek *
324 1.1 jdolecek * if an entry is found on the list, then call vget() to
325 1.1 jdolecek * take a reference. this is done because there may be
326 1.1 jdolecek * zero references to it and so it needs to removed from
327 1.1 jdolecek * the vnode free list.
328 1.1 jdolecek */
329 1.1 jdolecek int
330 1.32 matt union_allocvp(
331 1.32 matt struct vnode **vpp,
332 1.32 matt struct mount *mp,
333 1.32 matt struct vnode *undvp, /* parent union vnode */
334 1.32 matt struct vnode *dvp, /* may be null */
335 1.32 matt struct componentname *cnp, /* may be null */
336 1.32 matt struct vnode *uppervp, /* may be null */
337 1.32 matt struct vnode *lowervp, /* may be null */
338 1.32 matt int docache)
339 1.1 jdolecek {
340 1.1 jdolecek int error;
341 1.26 pooka struct vattr va;
342 1.38 hannken struct union_node *un = NULL, *un1;
343 1.38 hannken struct vnode *vp, *xlowervp = NULLVP;
344 1.1 jdolecek struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
345 1.26 pooka voff_t uppersz, lowersz;
346 1.47 hannken dev_t rdev;
347 1.1 jdolecek int hash = 0;
348 1.28 ad int vflag, iflag;
349 1.1 jdolecek int try;
350 1.1 jdolecek
351 1.1 jdolecek if (uppervp == NULLVP && lowervp == NULLVP)
352 1.1 jdolecek panic("union: unidentifiable allocation");
353 1.1 jdolecek
354 1.1 jdolecek if (uppervp && lowervp && (uppervp->v_type != lowervp->v_type)) {
355 1.1 jdolecek xlowervp = lowervp;
356 1.1 jdolecek lowervp = NULLVP;
357 1.1 jdolecek }
358 1.1 jdolecek
359 1.1 jdolecek /* detect the root vnode (and aliases) */
360 1.28 ad iflag = VI_LAYER;
361 1.28 ad vflag = 0;
362 1.1 jdolecek if ((uppervp == um->um_uppervp) &&
363 1.1 jdolecek ((lowervp == NULLVP) || lowervp == um->um_lowervp)) {
364 1.1 jdolecek if (lowervp == NULLVP) {
365 1.1 jdolecek lowervp = um->um_lowervp;
366 1.1 jdolecek if (lowervp != NULLVP)
367 1.35 pooka vref(lowervp);
368 1.1 jdolecek }
369 1.28 ad iflag = 0;
370 1.28 ad vflag = VV_ROOT;
371 1.1 jdolecek }
372 1.1 jdolecek
373 1.1 jdolecek loop:
374 1.1 jdolecek if (!docache) {
375 1.1 jdolecek un = 0;
376 1.1 jdolecek } else for (try = 0; try < 3; try++) {
377 1.1 jdolecek switch (try) {
378 1.1 jdolecek case 0:
379 1.1 jdolecek if (lowervp == NULLVP)
380 1.1 jdolecek continue;
381 1.1 jdolecek hash = UNION_HASH(uppervp, lowervp);
382 1.1 jdolecek break;
383 1.1 jdolecek
384 1.1 jdolecek case 1:
385 1.1 jdolecek if (uppervp == NULLVP)
386 1.1 jdolecek continue;
387 1.1 jdolecek hash = UNION_HASH(uppervp, NULLVP);
388 1.1 jdolecek break;
389 1.1 jdolecek
390 1.1 jdolecek case 2:
391 1.1 jdolecek if (lowervp == NULLVP)
392 1.1 jdolecek continue;
393 1.1 jdolecek hash = UNION_HASH(NULLVP, lowervp);
394 1.1 jdolecek break;
395 1.1 jdolecek }
396 1.1 jdolecek
397 1.1 jdolecek while (union_list_lock(hash))
398 1.1 jdolecek continue;
399 1.1 jdolecek
400 1.1 jdolecek for (un = unhead[hash].lh_first; un != 0;
401 1.1 jdolecek un = un->un_cache.le_next) {
402 1.1 jdolecek if ((un->un_lowervp == lowervp ||
403 1.1 jdolecek un->un_lowervp == NULLVP) &&
404 1.1 jdolecek (un->un_uppervp == uppervp ||
405 1.1 jdolecek un->un_uppervp == NULLVP) &&
406 1.1 jdolecek (UNIONTOV(un)->v_mount == mp)) {
407 1.38 hannken vp = UNIONTOV(un);
408 1.43 rmind mutex_enter(vp->v_interlock);
409 1.39 hannken if (vget(vp, 0)) {
410 1.1 jdolecek union_list_unlock(hash);
411 1.1 jdolecek goto loop;
412 1.1 jdolecek }
413 1.1 jdolecek break;
414 1.1 jdolecek }
415 1.1 jdolecek }
416 1.1 jdolecek
417 1.1 jdolecek union_list_unlock(hash);
418 1.1 jdolecek
419 1.1 jdolecek if (un)
420 1.1 jdolecek break;
421 1.1 jdolecek }
422 1.1 jdolecek
423 1.1 jdolecek if (un) {
424 1.1 jdolecek /*
425 1.1 jdolecek * Obtain a lock on the union_node.
426 1.1 jdolecek * uppervp is locked, though un->un_uppervp
427 1.1 jdolecek * may not be. this doesn't break the locking
428 1.1 jdolecek * hierarchy since in the case that un->un_uppervp
429 1.1 jdolecek * is not yet locked it will be vrele'd and replaced
430 1.1 jdolecek * with uppervp.
431 1.1 jdolecek */
432 1.1 jdolecek
433 1.1 jdolecek if ((dvp != NULLVP) && (uppervp == dvp)) {
434 1.1 jdolecek /*
435 1.1 jdolecek * Access ``.'', so (un) will already
436 1.1 jdolecek * be locked. Since this process has
437 1.1 jdolecek * the lock on (uppervp) no other
438 1.1 jdolecek * process can hold the lock on (un).
439 1.1 jdolecek */
440 1.48 hannken KASSERT((un->un_flags & UN_LOCKED) != 0);
441 1.48 hannken KASSERT(curlwp == NULL || un->un_lwp == NULL ||
442 1.48 hannken un->un_lwp == curlwp);
443 1.1 jdolecek } else {
444 1.1 jdolecek if (un->un_flags & UN_LOCKED) {
445 1.1 jdolecek vrele(UNIONTOV(un));
446 1.1 jdolecek un->un_flags |= UN_WANTED;
447 1.1 jdolecek (void) tsleep(&un->un_flags, PINOD,
448 1.1 jdolecek "unionalloc", 0);
449 1.1 jdolecek goto loop;
450 1.1 jdolecek }
451 1.1 jdolecek un->un_flags |= UN_LOCKED;
452 1.1 jdolecek
453 1.48 hannken un->un_lwp = curlwp;
454 1.1 jdolecek }
455 1.1 jdolecek
456 1.1 jdolecek /*
457 1.1 jdolecek * At this point, the union_node is locked,
458 1.1 jdolecek * un->un_uppervp may not be locked, and uppervp
459 1.1 jdolecek * is locked or nil.
460 1.1 jdolecek */
461 1.1 jdolecek
462 1.1 jdolecek /*
463 1.1 jdolecek * Save information about the upper layer.
464 1.1 jdolecek */
465 1.1 jdolecek if (uppervp != un->un_uppervp) {
466 1.1 jdolecek union_newupper(un, uppervp);
467 1.1 jdolecek } else if (uppervp) {
468 1.1 jdolecek vrele(uppervp);
469 1.1 jdolecek }
470 1.1 jdolecek
471 1.1 jdolecek if (un->un_uppervp) {
472 1.1 jdolecek un->un_flags |= UN_ULOCK;
473 1.1 jdolecek un->un_flags &= ~UN_KLOCK;
474 1.1 jdolecek }
475 1.1 jdolecek
476 1.1 jdolecek /*
477 1.1 jdolecek * Save information about the lower layer.
478 1.1 jdolecek * This needs to keep track of pathname
479 1.1 jdolecek * and directory information which union_vn_create
480 1.1 jdolecek * might need.
481 1.1 jdolecek */
482 1.1 jdolecek if (lowervp != un->un_lowervp) {
483 1.1 jdolecek union_newlower(un, lowervp);
484 1.1 jdolecek if (cnp && (lowervp != NULLVP)) {
485 1.1 jdolecek un->un_hash = cnp->cn_hash;
486 1.1 jdolecek un->un_path = malloc(cnp->cn_namelen+1,
487 1.1 jdolecek M_TEMP, M_WAITOK);
488 1.1 jdolecek memcpy(un->un_path, cnp->cn_nameptr,
489 1.1 jdolecek cnp->cn_namelen);
490 1.1 jdolecek un->un_path[cnp->cn_namelen] = '\0';
491 1.35 pooka vref(dvp);
492 1.1 jdolecek un->un_dirvp = dvp;
493 1.1 jdolecek }
494 1.1 jdolecek } else if (lowervp) {
495 1.1 jdolecek vrele(lowervp);
496 1.1 jdolecek }
497 1.1 jdolecek *vpp = UNIONTOV(un);
498 1.1 jdolecek return (0);
499 1.1 jdolecek }
500 1.1 jdolecek
501 1.27 pooka uppersz = lowersz = VNOVAL;
502 1.27 pooka if (uppervp != NULLVP)
503 1.29 pooka if (VOP_GETATTR(uppervp, &va, FSCRED) == 0)
504 1.27 pooka uppersz = va.va_size;
505 1.27 pooka if (lowervp != NULLVP)
506 1.29 pooka if (VOP_GETATTR(lowervp, &va, FSCRED) == 0)
507 1.27 pooka lowersz = va.va_size;
508 1.38 hannken hash = UNION_HASH(uppervp, lowervp);
509 1.1 jdolecek
510 1.43 rmind /*
511 1.43 rmind * Get a new vnode and share the lock with upper layer vnode,
512 1.43 rmind * unless layers are inverted.
513 1.43 rmind */
514 1.43 rmind vnode_t *svp = (uppervp != NULLVP) ? uppervp : lowervp;
515 1.43 rmind error = getnewvnode(VT_UNION, mp, union_vnodeop_p,
516 1.43 rmind svp->v_interlock, vpp);
517 1.1 jdolecek if (error) {
518 1.1 jdolecek if (uppervp) {
519 1.1 jdolecek if (dvp == uppervp)
520 1.1 jdolecek vrele(uppervp);
521 1.1 jdolecek else
522 1.1 jdolecek vput(uppervp);
523 1.1 jdolecek }
524 1.1 jdolecek if (lowervp)
525 1.1 jdolecek vrele(lowervp);
526 1.1 jdolecek
527 1.1 jdolecek goto out;
528 1.1 jdolecek }
529 1.1 jdolecek
530 1.38 hannken if (docache) {
531 1.38 hannken while (union_list_lock(hash))
532 1.38 hannken continue;
533 1.38 hannken LIST_FOREACH(un1, &unhead[hash], un_cache) {
534 1.38 hannken if (un1->un_lowervp == lowervp &&
535 1.38 hannken un1->un_uppervp == uppervp &&
536 1.38 hannken UNIONTOV(un1)->v_mount == mp) {
537 1.38 hannken /*
538 1.38 hannken * Another thread beat us, push back freshly
539 1.38 hannken * allocated vnode and retry.
540 1.38 hannken */
541 1.38 hannken union_list_unlock(hash);
542 1.38 hannken ungetnewvnode(*vpp);
543 1.38 hannken goto loop;
544 1.38 hannken }
545 1.38 hannken }
546 1.38 hannken }
547 1.38 hannken
548 1.34 cegger (*vpp)->v_data = malloc(sizeof(struct union_node), M_TEMP, M_WAITOK);
549 1.1 jdolecek
550 1.28 ad (*vpp)->v_vflag |= vflag;
551 1.28 ad (*vpp)->v_iflag |= iflag;
552 1.47 hannken rdev = NODEV;
553 1.47 hannken if (uppervp) {
554 1.1 jdolecek (*vpp)->v_type = uppervp->v_type;
555 1.47 hannken if (uppervp->v_type == VCHR || uppervp->v_type == VBLK)
556 1.47 hannken rdev = uppervp->v_rdev;
557 1.47 hannken } else {
558 1.1 jdolecek (*vpp)->v_type = lowervp->v_type;
559 1.47 hannken if (lowervp->v_type == VCHR || lowervp->v_type == VBLK)
560 1.47 hannken rdev = lowervp->v_rdev;
561 1.47 hannken }
562 1.47 hannken if (rdev != NODEV)
563 1.47 hannken spec_node_init(*vpp, rdev);
564 1.47 hannken
565 1.1 jdolecek un = VTOUNION(*vpp);
566 1.1 jdolecek un->un_vnode = *vpp;
567 1.1 jdolecek un->un_uppervp = uppervp;
568 1.1 jdolecek un->un_lowervp = lowervp;
569 1.1 jdolecek un->un_pvp = undvp;
570 1.1 jdolecek if (undvp != NULLVP)
571 1.35 pooka vref(undvp);
572 1.1 jdolecek un->un_dircache = 0;
573 1.1 jdolecek un->un_openl = 0;
574 1.1 jdolecek un->un_flags = UN_LOCKED;
575 1.26 pooka
576 1.26 pooka un->un_uppersz = VNOVAL;
577 1.26 pooka un->un_lowersz = VNOVAL;
578 1.26 pooka union_newsize(*vpp, uppersz, lowersz);
579 1.26 pooka
580 1.1 jdolecek if (un->un_uppervp)
581 1.1 jdolecek un->un_flags |= UN_ULOCK;
582 1.48 hannken un->un_lwp = curlwp;
583 1.17 christos if (dvp && cnp && (lowervp != NULLVP)) {
584 1.1 jdolecek un->un_hash = cnp->cn_hash;
585 1.1 jdolecek un->un_path = malloc(cnp->cn_namelen+1, M_TEMP, M_WAITOK);
586 1.1 jdolecek memcpy(un->un_path, cnp->cn_nameptr, cnp->cn_namelen);
587 1.1 jdolecek un->un_path[cnp->cn_namelen] = '\0';
588 1.35 pooka vref(dvp);
589 1.1 jdolecek un->un_dirvp = dvp;
590 1.1 jdolecek } else {
591 1.1 jdolecek un->un_hash = 0;
592 1.1 jdolecek un->un_path = 0;
593 1.1 jdolecek un->un_dirvp = 0;
594 1.1 jdolecek }
595 1.1 jdolecek
596 1.1 jdolecek if (docache) {
597 1.1 jdolecek LIST_INSERT_HEAD(&unhead[hash], un, un_cache);
598 1.1 jdolecek un->un_flags |= UN_CACHED;
599 1.1 jdolecek }
600 1.1 jdolecek
601 1.1 jdolecek if (xlowervp)
602 1.1 jdolecek vrele(xlowervp);
603 1.1 jdolecek
604 1.1 jdolecek out:
605 1.1 jdolecek if (docache)
606 1.1 jdolecek union_list_unlock(hash);
607 1.1 jdolecek
608 1.1 jdolecek return (error);
609 1.1 jdolecek }
610 1.1 jdolecek
611 1.1 jdolecek int
612 1.32 matt union_freevp(struct vnode *vp)
613 1.1 jdolecek {
614 1.38 hannken int hash;
615 1.1 jdolecek struct union_node *un = VTOUNION(vp);
616 1.1 jdolecek
617 1.38 hannken hash = UNION_HASH(un->un_uppervp, un->un_lowervp);
618 1.38 hannken
619 1.38 hannken while (union_list_lock(hash))
620 1.38 hannken continue;
621 1.1 jdolecek if (un->un_flags & UN_CACHED) {
622 1.1 jdolecek un->un_flags &= ~UN_CACHED;
623 1.1 jdolecek LIST_REMOVE(un, un_cache);
624 1.1 jdolecek }
625 1.38 hannken union_list_unlock(hash);
626 1.1 jdolecek
627 1.1 jdolecek if (un->un_pvp != NULLVP)
628 1.1 jdolecek vrele(un->un_pvp);
629 1.1 jdolecek if (un->un_uppervp != NULLVP)
630 1.1 jdolecek vrele(un->un_uppervp);
631 1.1 jdolecek if (un->un_lowervp != NULLVP)
632 1.1 jdolecek vrele(un->un_lowervp);
633 1.1 jdolecek if (un->un_dirvp != NULLVP)
634 1.1 jdolecek vrele(un->un_dirvp);
635 1.1 jdolecek if (un->un_path)
636 1.1 jdolecek free(un->un_path, M_TEMP);
637 1.1 jdolecek
638 1.34 cegger free(vp->v_data, M_TEMP);
639 1.34 cegger vp->v_data = NULL;
640 1.1 jdolecek
641 1.1 jdolecek return (0);
642 1.1 jdolecek }
643 1.1 jdolecek
644 1.1 jdolecek /*
645 1.1 jdolecek * copyfile. copy the vnode (fvp) to the vnode (tvp)
646 1.1 jdolecek * using a sequence of reads and writes. both (fvp)
647 1.1 jdolecek * and (tvp) are locked on entry and exit.
648 1.1 jdolecek */
649 1.1 jdolecek int
650 1.32 matt union_copyfile(struct vnode *fvp, struct vnode *tvp, kauth_cred_t cred,
651 1.32 matt struct lwp *l)
652 1.1 jdolecek {
653 1.13 christos char *tbuf;
654 1.1 jdolecek struct uio uio;
655 1.1 jdolecek struct iovec iov;
656 1.1 jdolecek int error = 0;
657 1.1 jdolecek
658 1.1 jdolecek /*
659 1.1 jdolecek * strategy:
660 1.1 jdolecek * allocate a buffer of size MAXBSIZE.
661 1.1 jdolecek * loop doing reads and writes, keeping track
662 1.1 jdolecek * of the current uio offset.
663 1.1 jdolecek * give up at the first sign of trouble.
664 1.1 jdolecek */
665 1.1 jdolecek
666 1.1 jdolecek uio.uio_offset = 0;
667 1.16 yamt UIO_SETUP_SYSSPACE(&uio);
668 1.1 jdolecek
669 1.37 hannken VOP_UNLOCK(fvp); /* XXX */
670 1.1 jdolecek vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
671 1.37 hannken VOP_UNLOCK(tvp); /* XXX */
672 1.1 jdolecek vn_lock(tvp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
673 1.1 jdolecek
674 1.13 christos tbuf = malloc(MAXBSIZE, M_TEMP, M_WAITOK);
675 1.1 jdolecek
676 1.1 jdolecek /* ugly loop follows... */
677 1.1 jdolecek do {
678 1.1 jdolecek off_t offset = uio.uio_offset;
679 1.1 jdolecek
680 1.1 jdolecek uio.uio_iov = &iov;
681 1.1 jdolecek uio.uio_iovcnt = 1;
682 1.13 christos iov.iov_base = tbuf;
683 1.1 jdolecek iov.iov_len = MAXBSIZE;
684 1.1 jdolecek uio.uio_resid = iov.iov_len;
685 1.1 jdolecek uio.uio_rw = UIO_READ;
686 1.1 jdolecek error = VOP_READ(fvp, &uio, 0, cred);
687 1.1 jdolecek
688 1.1 jdolecek if (error == 0) {
689 1.1 jdolecek uio.uio_iov = &iov;
690 1.1 jdolecek uio.uio_iovcnt = 1;
691 1.13 christos iov.iov_base = tbuf;
692 1.1 jdolecek iov.iov_len = MAXBSIZE - uio.uio_resid;
693 1.1 jdolecek uio.uio_offset = offset;
694 1.1 jdolecek uio.uio_rw = UIO_WRITE;
695 1.1 jdolecek uio.uio_resid = iov.iov_len;
696 1.1 jdolecek
697 1.1 jdolecek if (uio.uio_resid == 0)
698 1.1 jdolecek break;
699 1.1 jdolecek
700 1.1 jdolecek do {
701 1.1 jdolecek error = VOP_WRITE(tvp, &uio, 0, cred);
702 1.1 jdolecek } while ((uio.uio_resid > 0) && (error == 0));
703 1.1 jdolecek }
704 1.1 jdolecek
705 1.1 jdolecek } while (error == 0);
706 1.1 jdolecek
707 1.13 christos free(tbuf, M_TEMP);
708 1.1 jdolecek return (error);
709 1.1 jdolecek }
710 1.1 jdolecek
711 1.1 jdolecek /*
712 1.1 jdolecek * (un) is assumed to be locked on entry and remains
713 1.1 jdolecek * locked on exit.
714 1.1 jdolecek */
715 1.1 jdolecek int
716 1.32 matt union_copyup(struct union_node *un, int docopy, kauth_cred_t cred,
717 1.32 matt struct lwp *l)
718 1.1 jdolecek {
719 1.1 jdolecek int error;
720 1.1 jdolecek struct vnode *lvp, *uvp;
721 1.1 jdolecek struct vattr lvattr, uvattr;
722 1.1 jdolecek
723 1.15 christos error = union_vn_create(&uvp, un, l);
724 1.25 hannken if (error)
725 1.1 jdolecek return (error);
726 1.1 jdolecek
727 1.1 jdolecek /* at this point, uppervp is locked */
728 1.1 jdolecek union_newupper(un, uvp);
729 1.1 jdolecek un->un_flags |= UN_ULOCK;
730 1.1 jdolecek
731 1.1 jdolecek lvp = un->un_lowervp;
732 1.1 jdolecek
733 1.1 jdolecek if (docopy) {
734 1.1 jdolecek /*
735 1.1 jdolecek * XX - should not ignore errors
736 1.1 jdolecek * from VOP_CLOSE
737 1.1 jdolecek */
738 1.1 jdolecek vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
739 1.1 jdolecek
740 1.29 pooka error = VOP_GETATTR(lvp, &lvattr, cred);
741 1.1 jdolecek if (error == 0)
742 1.29 pooka error = VOP_OPEN(lvp, FREAD, cred);
743 1.1 jdolecek if (error == 0) {
744 1.15 christos error = union_copyfile(lvp, uvp, cred, l);
745 1.29 pooka (void) VOP_CLOSE(lvp, FREAD, cred);
746 1.1 jdolecek }
747 1.1 jdolecek if (error == 0) {
748 1.1 jdolecek /* Copy permissions up too */
749 1.35 pooka vattr_null(&uvattr);
750 1.1 jdolecek uvattr.va_mode = lvattr.va_mode;
751 1.1 jdolecek uvattr.va_flags = lvattr.va_flags;
752 1.29 pooka error = VOP_SETATTR(uvp, &uvattr, cred);
753 1.1 jdolecek }
754 1.37 hannken VOP_UNLOCK(lvp);
755 1.1 jdolecek #ifdef UNION_DIAGNOSTIC
756 1.1 jdolecek if (error == 0)
757 1.1 jdolecek uprintf("union: copied up %s\n", un->un_path);
758 1.1 jdolecek #endif
759 1.1 jdolecek
760 1.1 jdolecek }
761 1.15 christos union_vn_close(uvp, FWRITE, cred, l);
762 1.1 jdolecek
763 1.1 jdolecek /*
764 1.1 jdolecek * Subsequent IOs will go to the top layer, so
765 1.1 jdolecek * call close on the lower vnode and open on the
766 1.1 jdolecek * upper vnode to ensure that the filesystem keeps
767 1.1 jdolecek * its references counts right. This doesn't do
768 1.1 jdolecek * the right thing with (cred) and (FREAD) though.
769 1.1 jdolecek * Ignoring error returns is not right, either.
770 1.1 jdolecek */
771 1.1 jdolecek if (error == 0) {
772 1.1 jdolecek int i;
773 1.1 jdolecek
774 1.1 jdolecek vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
775 1.1 jdolecek for (i = 0; i < un->un_openl; i++) {
776 1.29 pooka (void) VOP_CLOSE(lvp, FREAD, cred);
777 1.29 pooka (void) VOP_OPEN(uvp, FREAD, cred);
778 1.1 jdolecek }
779 1.1 jdolecek un->un_openl = 0;
780 1.37 hannken VOP_UNLOCK(lvp);
781 1.1 jdolecek }
782 1.1 jdolecek
783 1.1 jdolecek return (error);
784 1.1 jdolecek
785 1.1 jdolecek }
786 1.1 jdolecek
787 1.1 jdolecek static int
788 1.32 matt union_relookup(
789 1.32 matt struct union_mount *um,
790 1.32 matt struct vnode *dvp,
791 1.32 matt struct vnode **vpp,
792 1.32 matt struct componentname *cnp,
793 1.32 matt struct componentname *cn,
794 1.40 dholland char **pnbuf_ret,
795 1.32 matt const char *path,
796 1.32 matt int pathlen)
797 1.1 jdolecek {
798 1.1 jdolecek int error;
799 1.40 dholland char *pnbuf;
800 1.1 jdolecek
801 1.1 jdolecek /*
802 1.1 jdolecek * A new componentname structure must be faked up because
803 1.1 jdolecek * there is no way to know where the upper level cnp came
804 1.1 jdolecek * from or what it is being used for. This must duplicate
805 1.1 jdolecek * some of the work done by NDINIT, some of the work done
806 1.1 jdolecek * by namei, some of the work done by lookup and some of
807 1.1 jdolecek * the work done by VOP_LOOKUP when given a CREATE flag.
808 1.1 jdolecek * Conclusion: Horrible.
809 1.1 jdolecek */
810 1.1 jdolecek cn->cn_namelen = pathlen;
811 1.1 jdolecek if ((cn->cn_namelen + 1) > MAXPATHLEN)
812 1.1 jdolecek return (ENAMETOOLONG);
813 1.40 dholland pnbuf = PNBUF_GET();
814 1.40 dholland memcpy(pnbuf, path, cn->cn_namelen);
815 1.40 dholland pnbuf[cn->cn_namelen] = '\0';
816 1.40 dholland *pnbuf_ret = pnbuf;
817 1.1 jdolecek
818 1.1 jdolecek cn->cn_nameiop = CREATE;
819 1.41 dholland cn->cn_flags = (LOCKPARENT|ISLASTCN);
820 1.1 jdolecek if (um->um_op == UNMNT_ABOVE)
821 1.1 jdolecek cn->cn_cred = cnp->cn_cred;
822 1.1 jdolecek else
823 1.1 jdolecek cn->cn_cred = um->um_cred;
824 1.40 dholland cn->cn_nameptr = pnbuf;
825 1.1 jdolecek cn->cn_hash = cnp->cn_hash;
826 1.1 jdolecek cn->cn_consume = cnp->cn_consume;
827 1.1 jdolecek
828 1.42 dholland error = relookup(dvp, vpp, cn, 0);
829 1.22 chs if (error) {
830 1.40 dholland PNBUF_PUT(pnbuf);
831 1.40 dholland *pnbuf_ret = NULL;
832 1.1 jdolecek }
833 1.1 jdolecek
834 1.1 jdolecek return (error);
835 1.1 jdolecek }
836 1.1 jdolecek
837 1.1 jdolecek /*
838 1.1 jdolecek * Create a shadow directory in the upper layer.
839 1.1 jdolecek * The new vnode is returned locked.
840 1.1 jdolecek *
841 1.1 jdolecek * (um) points to the union mount structure for access to the
842 1.1 jdolecek * the mounting process's credentials.
843 1.1 jdolecek * (dvp) is the directory in which to create the shadow directory.
844 1.1 jdolecek * it is unlocked on entry and exit.
845 1.1 jdolecek * (cnp) is the componentname to be created.
846 1.1 jdolecek * (vpp) is the returned newly created shadow directory, which
847 1.1 jdolecek * is returned locked.
848 1.1 jdolecek *
849 1.1 jdolecek * N.B. We still attempt to create shadow directories even if the union
850 1.1 jdolecek * is mounted read-only, which is a little nonintuitive.
851 1.1 jdolecek */
852 1.1 jdolecek int
853 1.32 matt union_mkshadow(struct union_mount *um, struct vnode *dvp,
854 1.32 matt struct componentname *cnp, struct vnode **vpp)
855 1.1 jdolecek {
856 1.1 jdolecek int error;
857 1.1 jdolecek struct vattr va;
858 1.1 jdolecek struct componentname cn;
859 1.40 dholland char *pnbuf;
860 1.1 jdolecek
861 1.22 chs vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
862 1.40 dholland error = union_relookup(um, dvp, vpp, cnp, &cn, &pnbuf,
863 1.1 jdolecek cnp->cn_nameptr, cnp->cn_namelen);
864 1.8 hannken if (error) {
865 1.37 hannken VOP_UNLOCK(dvp);
866 1.1 jdolecek return (error);
867 1.8 hannken }
868 1.1 jdolecek
869 1.1 jdolecek if (*vpp) {
870 1.1 jdolecek VOP_ABORTOP(dvp, &cn);
871 1.40 dholland PNBUF_PUT(pnbuf);
872 1.24 chs if (dvp != *vpp)
873 1.37 hannken VOP_UNLOCK(dvp);
874 1.22 chs vput(*vpp);
875 1.1 jdolecek *vpp = NULLVP;
876 1.1 jdolecek return (EEXIST);
877 1.1 jdolecek }
878 1.1 jdolecek
879 1.1 jdolecek /*
880 1.1 jdolecek * policy: when creating the shadow directory in the
881 1.1 jdolecek * upper layer, create it owned by the user who did
882 1.1 jdolecek * the mount, group from parent directory, and mode
883 1.1 jdolecek * 777 modified by umask (ie mostly identical to the
884 1.1 jdolecek * mkdir syscall). (jsp, kb)
885 1.1 jdolecek */
886 1.1 jdolecek
887 1.35 pooka vattr_null(&va);
888 1.1 jdolecek va.va_type = VDIR;
889 1.1 jdolecek va.va_mode = um->um_cmode;
890 1.1 jdolecek
891 1.24 chs vref(dvp);
892 1.1 jdolecek error = VOP_MKDIR(dvp, vpp, &cn, &va);
893 1.40 dholland PNBUF_PUT(pnbuf);
894 1.1 jdolecek return (error);
895 1.1 jdolecek }
896 1.1 jdolecek
897 1.1 jdolecek /*
898 1.1 jdolecek * Create a whiteout entry in the upper layer.
899 1.1 jdolecek *
900 1.1 jdolecek * (um) points to the union mount structure for access to the
901 1.1 jdolecek * the mounting process's credentials.
902 1.1 jdolecek * (dvp) is the directory in which to create the whiteout.
903 1.1 jdolecek * it is locked on entry and exit.
904 1.1 jdolecek * (cnp) is the componentname to be created.
905 1.1 jdolecek */
906 1.1 jdolecek int
907 1.32 matt union_mkwhiteout(struct union_mount *um, struct vnode *dvp,
908 1.32 matt struct componentname *cnp, char *path)
909 1.1 jdolecek {
910 1.1 jdolecek int error;
911 1.1 jdolecek struct vnode *wvp;
912 1.1 jdolecek struct componentname cn;
913 1.40 dholland char *pnbuf;
914 1.1 jdolecek
915 1.37 hannken VOP_UNLOCK(dvp);
916 1.22 chs vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
917 1.40 dholland error = union_relookup(um, dvp, &wvp, cnp, &cn, &pnbuf,
918 1.40 dholland path, strlen(path));
919 1.25 hannken if (error)
920 1.1 jdolecek return (error);
921 1.1 jdolecek
922 1.1 jdolecek if (wvp) {
923 1.1 jdolecek VOP_ABORTOP(dvp, &cn);
924 1.40 dholland PNBUF_PUT(pnbuf);
925 1.24 chs if (dvp != wvp)
926 1.37 hannken VOP_UNLOCK(dvp);
927 1.22 chs vput(wvp);
928 1.1 jdolecek return (EEXIST);
929 1.1 jdolecek }
930 1.1 jdolecek
931 1.1 jdolecek error = VOP_WHITEOUT(dvp, &cn, CREATE);
932 1.40 dholland if (error) {
933 1.1 jdolecek VOP_ABORTOP(dvp, &cn);
934 1.40 dholland }
935 1.1 jdolecek
936 1.40 dholland PNBUF_PUT(pnbuf);
937 1.1 jdolecek return (error);
938 1.1 jdolecek }
939 1.1 jdolecek
940 1.1 jdolecek /*
941 1.1 jdolecek * union_vn_create: creates and opens a new shadow file
942 1.1 jdolecek * on the upper union layer. this function is similar
943 1.1 jdolecek * in spirit to calling vn_open but it avoids calling namei().
944 1.1 jdolecek * the problem with calling namei is that a) it locks too many
945 1.1 jdolecek * things, and b) it doesn't start at the "right" directory,
946 1.1 jdolecek * whereas relookup is told where to start.
947 1.1 jdolecek */
948 1.1 jdolecek int
949 1.32 matt union_vn_create(struct vnode **vpp, struct union_node *un, struct lwp *l)
950 1.1 jdolecek {
951 1.1 jdolecek struct vnode *vp;
952 1.19 ad kauth_cred_t cred = l->l_cred;
953 1.1 jdolecek struct vattr vat;
954 1.1 jdolecek struct vattr *vap = &vat;
955 1.1 jdolecek int fmode = FFLAGS(O_WRONLY|O_CREAT|O_TRUNC|O_EXCL);
956 1.1 jdolecek int error;
957 1.15 christos int cmode = UN_FILEMODE & ~l->l_proc->p_cwdi->cwdi_cmask;
958 1.1 jdolecek struct componentname cn;
959 1.40 dholland char *pnbuf;
960 1.1 jdolecek
961 1.1 jdolecek *vpp = NULLVP;
962 1.1 jdolecek
963 1.1 jdolecek /*
964 1.1 jdolecek * Build a new componentname structure (for the same
965 1.1 jdolecek * reasons outlines in union_mkshadow).
966 1.1 jdolecek * The difference here is that the file is owned by
967 1.1 jdolecek * the current user, rather than by the person who
968 1.1 jdolecek * did the mount, since the current user needs to be
969 1.1 jdolecek * able to write the file (that's why it is being
970 1.1 jdolecek * copied in the first place).
971 1.1 jdolecek */
972 1.1 jdolecek cn.cn_namelen = strlen(un->un_path);
973 1.1 jdolecek if ((cn.cn_namelen + 1) > MAXPATHLEN)
974 1.1 jdolecek return (ENAMETOOLONG);
975 1.40 dholland pnbuf = PNBUF_GET();
976 1.40 dholland memcpy(pnbuf, un->un_path, cn.cn_namelen+1);
977 1.1 jdolecek cn.cn_nameiop = CREATE;
978 1.41 dholland cn.cn_flags = (LOCKPARENT|ISLASTCN);
979 1.19 ad cn.cn_cred = l->l_cred;
980 1.40 dholland cn.cn_nameptr = pnbuf;
981 1.1 jdolecek cn.cn_hash = un->un_hash;
982 1.1 jdolecek cn.cn_consume = 0;
983 1.1 jdolecek
984 1.24 chs vn_lock(un->un_dirvp, LK_EXCLUSIVE | LK_RETRY);
985 1.42 dholland error = relookup(un->un_dirvp, &vp, &cn, 0);
986 1.22 chs if (error) {
987 1.40 dholland PNBUF_PUT(pnbuf);
988 1.37 hannken VOP_UNLOCK(un->un_dirvp);
989 1.1 jdolecek return (error);
990 1.22 chs }
991 1.1 jdolecek
992 1.1 jdolecek if (vp) {
993 1.1 jdolecek VOP_ABORTOP(un->un_dirvp, &cn);
994 1.40 dholland PNBUF_PUT(pnbuf);
995 1.24 chs if (un->un_dirvp != vp)
996 1.37 hannken VOP_UNLOCK(un->un_dirvp);
997 1.22 chs vput(vp);
998 1.1 jdolecek return (EEXIST);
999 1.1 jdolecek }
1000 1.1 jdolecek
1001 1.1 jdolecek /*
1002 1.1 jdolecek * Good - there was no race to create the file
1003 1.1 jdolecek * so go ahead and create it. The permissions
1004 1.1 jdolecek * on the file will be 0666 modified by the
1005 1.1 jdolecek * current user's umask. Access to the file, while
1006 1.1 jdolecek * it is unioned, will require access to the top *and*
1007 1.1 jdolecek * bottom files. Access when not unioned will simply
1008 1.1 jdolecek * require access to the top-level file.
1009 1.1 jdolecek * TODO: confirm choice of access permissions.
1010 1.1 jdolecek */
1011 1.35 pooka vattr_null(vap);
1012 1.1 jdolecek vap->va_type = VREG;
1013 1.1 jdolecek vap->va_mode = cmode;
1014 1.24 chs vref(un->un_dirvp);
1015 1.40 dholland if ((error = VOP_CREATE(un->un_dirvp, &vp, &cn, vap)) != 0) {
1016 1.40 dholland PNBUF_PUT(pnbuf);
1017 1.1 jdolecek return (error);
1018 1.40 dholland }
1019 1.1 jdolecek
1020 1.29 pooka if ((error = VOP_OPEN(vp, fmode, cred)) != 0) {
1021 1.1 jdolecek vput(vp);
1022 1.40 dholland PNBUF_PUT(pnbuf);
1023 1.1 jdolecek return (error);
1024 1.1 jdolecek }
1025 1.1 jdolecek
1026 1.1 jdolecek vp->v_writecount++;
1027 1.1 jdolecek *vpp = vp;
1028 1.40 dholland PNBUF_PUT(pnbuf);
1029 1.1 jdolecek return (0);
1030 1.1 jdolecek }
1031 1.1 jdolecek
1032 1.1 jdolecek int
1033 1.32 matt union_vn_close(struct vnode *vp, int fmode, kauth_cred_t cred, struct lwp *l)
1034 1.1 jdolecek {
1035 1.1 jdolecek
1036 1.1 jdolecek if (fmode & FWRITE)
1037 1.1 jdolecek --vp->v_writecount;
1038 1.29 pooka return (VOP_CLOSE(vp, fmode, cred));
1039 1.1 jdolecek }
1040 1.1 jdolecek
1041 1.1 jdolecek void
1042 1.32 matt union_removed_upper(struct union_node *un)
1043 1.1 jdolecek {
1044 1.38 hannken int hash;
1045 1.38 hannken
1046 1.1 jdolecek #if 1
1047 1.1 jdolecek /*
1048 1.1 jdolecek * We do not set the uppervp to NULLVP here, because lowervp
1049 1.1 jdolecek * may also be NULLVP, so this routine would end up creating
1050 1.1 jdolecek * a bogus union node with no upper or lower VP (that causes
1051 1.1 jdolecek * pain in many places that assume at least one VP exists).
1052 1.1 jdolecek * Since we've removed this node from the cache hash chains,
1053 1.1 jdolecek * it won't be found again. When all current holders
1054 1.1 jdolecek * release it, union_inactive() will vgone() it.
1055 1.1 jdolecek */
1056 1.1 jdolecek union_diruncache(un);
1057 1.1 jdolecek #else
1058 1.1 jdolecek union_newupper(un, NULLVP);
1059 1.1 jdolecek #endif
1060 1.1 jdolecek
1061 1.38 hannken hash = UNION_HASH(un->un_uppervp, un->un_lowervp);
1062 1.38 hannken
1063 1.38 hannken while (union_list_lock(hash))
1064 1.38 hannken continue;
1065 1.1 jdolecek if (un->un_flags & UN_CACHED) {
1066 1.1 jdolecek un->un_flags &= ~UN_CACHED;
1067 1.1 jdolecek LIST_REMOVE(un, un_cache);
1068 1.1 jdolecek }
1069 1.38 hannken union_list_unlock(hash);
1070 1.1 jdolecek
1071 1.1 jdolecek if (un->un_flags & UN_ULOCK) {
1072 1.1 jdolecek un->un_flags &= ~UN_ULOCK;
1073 1.37 hannken VOP_UNLOCK(un->un_uppervp);
1074 1.1 jdolecek }
1075 1.1 jdolecek }
1076 1.1 jdolecek
1077 1.1 jdolecek #if 0
1078 1.1 jdolecek struct vnode *
1079 1.32 matt union_lowervp(struct vnode *vp)
1080 1.1 jdolecek {
1081 1.1 jdolecek struct union_node *un = VTOUNION(vp);
1082 1.1 jdolecek
1083 1.1 jdolecek if ((un->un_lowervp != NULLVP) &&
1084 1.1 jdolecek (vp->v_type == un->un_lowervp->v_type)) {
1085 1.1 jdolecek if (vget(un->un_lowervp, 0) == 0)
1086 1.1 jdolecek return (un->un_lowervp);
1087 1.1 jdolecek }
1088 1.1 jdolecek
1089 1.1 jdolecek return (NULLVP);
1090 1.1 jdolecek }
1091 1.1 jdolecek #endif
1092 1.1 jdolecek
1093 1.1 jdolecek /*
1094 1.1 jdolecek * determine whether a whiteout is needed
1095 1.1 jdolecek * during a remove/rmdir operation.
1096 1.1 jdolecek */
1097 1.1 jdolecek int
1098 1.32 matt union_dowhiteout(struct union_node *un, kauth_cred_t cred)
1099 1.1 jdolecek {
1100 1.1 jdolecek struct vattr va;
1101 1.1 jdolecek
1102 1.1 jdolecek if (un->un_lowervp != NULLVP)
1103 1.1 jdolecek return (1);
1104 1.1 jdolecek
1105 1.29 pooka if (VOP_GETATTR(un->un_uppervp, &va, cred) == 0 &&
1106 1.1 jdolecek (va.va_flags & OPAQUE))
1107 1.1 jdolecek return (1);
1108 1.1 jdolecek
1109 1.1 jdolecek return (0);
1110 1.1 jdolecek }
1111 1.1 jdolecek
1112 1.1 jdolecek static void
1113 1.32 matt union_dircache_r(struct vnode *vp, struct vnode ***vppp, int *cntp)
1114 1.1 jdolecek {
1115 1.1 jdolecek struct union_node *un;
1116 1.1 jdolecek
1117 1.1 jdolecek if (vp->v_op != union_vnodeop_p) {
1118 1.1 jdolecek if (vppp) {
1119 1.35 pooka vref(vp);
1120 1.1 jdolecek *(*vppp)++ = vp;
1121 1.1 jdolecek if (--(*cntp) == 0)
1122 1.1 jdolecek panic("union: dircache table too small");
1123 1.1 jdolecek } else {
1124 1.1 jdolecek (*cntp)++;
1125 1.1 jdolecek }
1126 1.1 jdolecek
1127 1.1 jdolecek return;
1128 1.1 jdolecek }
1129 1.1 jdolecek
1130 1.1 jdolecek un = VTOUNION(vp);
1131 1.1 jdolecek if (un->un_uppervp != NULLVP)
1132 1.1 jdolecek union_dircache_r(un->un_uppervp, vppp, cntp);
1133 1.1 jdolecek if (un->un_lowervp != NULLVP)
1134 1.1 jdolecek union_dircache_r(un->un_lowervp, vppp, cntp);
1135 1.1 jdolecek }
1136 1.1 jdolecek
1137 1.1 jdolecek struct vnode *
1138 1.21 christos union_dircache(struct vnode *vp, struct lwp *l)
1139 1.1 jdolecek {
1140 1.1 jdolecek int cnt;
1141 1.1 jdolecek struct vnode *nvp = NULLVP;
1142 1.1 jdolecek struct vnode **vpp;
1143 1.1 jdolecek struct vnode **dircache;
1144 1.1 jdolecek int error;
1145 1.1 jdolecek
1146 1.1 jdolecek vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1147 1.1 jdolecek dircache = VTOUNION(vp)->un_dircache;
1148 1.1 jdolecek
1149 1.1 jdolecek nvp = NULLVP;
1150 1.1 jdolecek
1151 1.1 jdolecek if (dircache == 0) {
1152 1.1 jdolecek cnt = 0;
1153 1.1 jdolecek union_dircache_r(vp, 0, &cnt);
1154 1.1 jdolecek cnt++;
1155 1.1 jdolecek dircache = (struct vnode **)
1156 1.1 jdolecek malloc(cnt * sizeof(struct vnode *),
1157 1.1 jdolecek M_TEMP, M_WAITOK);
1158 1.1 jdolecek vpp = dircache;
1159 1.1 jdolecek union_dircache_r(vp, &vpp, &cnt);
1160 1.1 jdolecek VTOUNION(vp)->un_dircache = dircache;
1161 1.1 jdolecek *vpp = NULLVP;
1162 1.1 jdolecek vpp = dircache + 1;
1163 1.1 jdolecek } else {
1164 1.1 jdolecek vpp = dircache;
1165 1.1 jdolecek do {
1166 1.1 jdolecek if (*vpp++ == VTOUNION(vp)->un_uppervp)
1167 1.1 jdolecek break;
1168 1.1 jdolecek } while (*vpp != NULLVP);
1169 1.1 jdolecek }
1170 1.1 jdolecek
1171 1.1 jdolecek if (*vpp == NULLVP)
1172 1.1 jdolecek goto out;
1173 1.1 jdolecek
1174 1.1 jdolecek vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
1175 1.35 pooka vref(*vpp);
1176 1.5 thorpej error = union_allocvp(&nvp, vp->v_mount, NULLVP, NULLVP, 0, *vpp, NULLVP, 0);
1177 1.1 jdolecek if (!error) {
1178 1.1 jdolecek VTOUNION(vp)->un_dircache = 0;
1179 1.1 jdolecek VTOUNION(nvp)->un_dircache = dircache;
1180 1.1 jdolecek }
1181 1.1 jdolecek
1182 1.1 jdolecek out:
1183 1.37 hannken VOP_UNLOCK(vp);
1184 1.1 jdolecek return (nvp);
1185 1.1 jdolecek }
1186 1.1 jdolecek
1187 1.1 jdolecek void
1188 1.32 matt union_diruncache(struct union_node *un)
1189 1.1 jdolecek {
1190 1.1 jdolecek struct vnode **vpp;
1191 1.1 jdolecek
1192 1.1 jdolecek if (un->un_dircache != 0) {
1193 1.1 jdolecek for (vpp = un->un_dircache; *vpp != NULLVP; vpp++)
1194 1.1 jdolecek vrele(*vpp);
1195 1.1 jdolecek free(un->un_dircache, M_TEMP);
1196 1.1 jdolecek un->un_dircache = 0;
1197 1.1 jdolecek }
1198 1.2 jdolecek }
1199 1.2 jdolecek
1200 1.2 jdolecek /*
1201 1.44 hannken * Check whether node can rmdir (check empty).
1202 1.44 hannken */
1203 1.44 hannken int
1204 1.44 hannken union_check_rmdir(struct union_node *un, kauth_cred_t cred)
1205 1.44 hannken {
1206 1.44 hannken int dirlen, eofflag, error;
1207 1.44 hannken char *dirbuf;
1208 1.44 hannken struct vattr va;
1209 1.44 hannken struct vnode *tvp;
1210 1.44 hannken struct dirent *dp, *edp;
1211 1.44 hannken struct componentname cn;
1212 1.44 hannken struct iovec aiov;
1213 1.44 hannken struct uio auio;
1214 1.44 hannken
1215 1.44 hannken KASSERT(un->un_uppervp != NULL);
1216 1.44 hannken
1217 1.44 hannken /* Check upper for being opaque. */
1218 1.44 hannken KASSERT(VOP_ISLOCKED(un->un_uppervp));
1219 1.44 hannken error = VOP_GETATTR(un->un_uppervp, &va, cred);
1220 1.44 hannken if (error || (va.va_flags & OPAQUE))
1221 1.44 hannken return error;
1222 1.44 hannken
1223 1.44 hannken if (un->un_lowervp == NULL)
1224 1.44 hannken return 0;
1225 1.44 hannken
1226 1.44 hannken /* Check lower for being empty. */
1227 1.45 hannken vn_lock(un->un_lowervp, LK_SHARED | LK_RETRY);
1228 1.44 hannken error = VOP_GETATTR(un->un_lowervp, &va, cred);
1229 1.44 hannken if (error) {
1230 1.44 hannken VOP_UNLOCK(un->un_lowervp);
1231 1.44 hannken return error;
1232 1.44 hannken }
1233 1.44 hannken dirlen = va.va_blocksize;
1234 1.44 hannken dirbuf = kmem_alloc(dirlen, KM_SLEEP);
1235 1.44 hannken if (dirbuf == NULL) {
1236 1.44 hannken VOP_UNLOCK(un->un_lowervp);
1237 1.44 hannken return ENOMEM;
1238 1.44 hannken }
1239 1.44 hannken /* error = 0; */
1240 1.44 hannken eofflag = 0;
1241 1.44 hannken auio.uio_offset = 0;
1242 1.44 hannken do {
1243 1.44 hannken aiov.iov_len = dirlen;
1244 1.44 hannken aiov.iov_base = dirbuf;
1245 1.44 hannken auio.uio_iov = &aiov;
1246 1.44 hannken auio.uio_iovcnt = 1;
1247 1.44 hannken auio.uio_resid = aiov.iov_len;
1248 1.44 hannken auio.uio_rw = UIO_READ;
1249 1.44 hannken UIO_SETUP_SYSSPACE(&auio);
1250 1.44 hannken error = VOP_READDIR(un->un_lowervp, &auio, cred, &eofflag,
1251 1.44 hannken NULL, NULL);
1252 1.44 hannken if (error)
1253 1.44 hannken break;
1254 1.44 hannken edp = (struct dirent *)&dirbuf[dirlen - auio.uio_resid];
1255 1.44 hannken for (dp = (struct dirent *)dirbuf;
1256 1.44 hannken error == 0 && dp < edp;
1257 1.44 hannken dp = (struct dirent *)((char *)dp + dp->d_reclen)) {
1258 1.44 hannken if (dp->d_reclen == 0) {
1259 1.44 hannken error = ENOTEMPTY;
1260 1.44 hannken break;
1261 1.44 hannken }
1262 1.44 hannken if (dp->d_type == DT_WHT ||
1263 1.44 hannken (dp->d_namlen == 1 && dp->d_name[0] == '.') ||
1264 1.44 hannken (dp->d_namlen == 2 && !memcmp(dp->d_name, "..", 2)))
1265 1.44 hannken continue;
1266 1.44 hannken /* Check for presence in the upper layer. */
1267 1.44 hannken cn.cn_nameiop = LOOKUP;
1268 1.44 hannken cn.cn_flags = ISLASTCN | RDONLY;
1269 1.44 hannken cn.cn_cred = cred;
1270 1.44 hannken cn.cn_nameptr = dp->d_name;
1271 1.44 hannken cn.cn_namelen = dp->d_namlen;
1272 1.44 hannken cn.cn_hash = 0;
1273 1.44 hannken cn.cn_consume = 0;
1274 1.44 hannken error = VOP_LOOKUP(un->un_uppervp, &tvp, &cn);
1275 1.44 hannken if (error == ENOENT && (cn.cn_flags & ISWHITEOUT)) {
1276 1.44 hannken error = 0;
1277 1.44 hannken continue;
1278 1.44 hannken }
1279 1.44 hannken if (error == 0)
1280 1.44 hannken vput(tvp);
1281 1.44 hannken error = ENOTEMPTY;
1282 1.44 hannken }
1283 1.44 hannken } while (error == 0 && !eofflag);
1284 1.44 hannken kmem_free(dirbuf, dirlen);
1285 1.44 hannken VOP_UNLOCK(un->un_lowervp);
1286 1.44 hannken
1287 1.44 hannken return error;
1288 1.44 hannken }
1289 1.44 hannken
1290 1.44 hannken /*
1291 1.2 jdolecek * This hook is called from vn_readdir() to switch to lower directory
1292 1.2 jdolecek * entry after the upper directory is read.
1293 1.2 jdolecek */
1294 1.2 jdolecek int
1295 1.15 christos union_readdirhook(struct vnode **vpp, struct file *fp, struct lwp *l)
1296 1.2 jdolecek {
1297 1.2 jdolecek struct vnode *vp = *vpp, *lvp;
1298 1.2 jdolecek struct vattr va;
1299 1.2 jdolecek int error;
1300 1.2 jdolecek
1301 1.2 jdolecek if (vp->v_op != union_vnodeop_p)
1302 1.2 jdolecek return (0);
1303 1.2 jdolecek
1304 1.15 christos if ((lvp = union_dircache(vp, l)) == NULLVP)
1305 1.2 jdolecek return (0);
1306 1.2 jdolecek
1307 1.2 jdolecek /*
1308 1.2 jdolecek * If the directory is opaque,
1309 1.2 jdolecek * then don't show lower entries
1310 1.2 jdolecek */
1311 1.29 pooka error = VOP_GETATTR(vp, &va, fp->f_cred);
1312 1.2 jdolecek if (error || (va.va_flags & OPAQUE)) {
1313 1.2 jdolecek vput(lvp);
1314 1.2 jdolecek return (error);
1315 1.2 jdolecek }
1316 1.12 perry
1317 1.29 pooka error = VOP_OPEN(lvp, FREAD, fp->f_cred);
1318 1.2 jdolecek if (error) {
1319 1.2 jdolecek vput(lvp);
1320 1.2 jdolecek return (error);
1321 1.2 jdolecek }
1322 1.37 hannken VOP_UNLOCK(lvp);
1323 1.10 jrf fp->f_data = lvp;
1324 1.2 jdolecek fp->f_offset = 0;
1325 1.33 ad error = vn_close(vp, FREAD, fp->f_cred);
1326 1.2 jdolecek if (error)
1327 1.2 jdolecek return (error);
1328 1.2 jdolecek *vpp = lvp;
1329 1.2 jdolecek return (0);
1330 1.1 jdolecek }
1331