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