Commit | Line | Data |
---|---|---|
b991bc2d JSP |
1 | /* |
2 | * Copyright (c) 1994 Jan-Simon Pendry | |
3 | * Copyright (c) 1994 | |
4 | * The Regents of the University of California. All rights reserved. | |
5 | * | |
6 | * This code is derived from software contributed to Berkeley by | |
7 | * Jan-Simon Pendry. | |
8 | * | |
9 | * %sccs.include.redist.c% | |
10 | * | |
b5b5f291 | 11 | * @(#)union_subr.c 8.6 (Berkeley) %G% |
b991bc2d JSP |
12 | */ |
13 | ||
14 | #include <sys/param.h> | |
15 | #include <sys/systm.h> | |
16 | #include <sys/time.h> | |
17 | #include <sys/kernel.h> | |
18 | #include <sys/vnode.h> | |
19 | #include <sys/namei.h> | |
20 | #include <sys/malloc.h> | |
81be6ee0 | 21 | #include <sys/file.h> |
3b293975 | 22 | #include <sys/filedesc.h> |
58572fc0 | 23 | #include <sys/queue.h> |
c93d29a8 | 24 | #include <sys/mount.h> |
2e1ae99e | 25 | #include <miscfs/union/union.h> |
b991bc2d | 26 | |
79f80c71 JSP |
27 | #ifdef DIAGNOSTIC |
28 | #include <sys/proc.h> | |
29 | #endif | |
30 | ||
58572fc0 JSP |
31 | /* must be power of two, otherwise change UNION_HASH() */ |
32 | #define NHASH 32 | |
33 | ||
34 | /* unsigned int ... */ | |
35 | #define UNION_HASH(u, l) \ | |
36 | (((((unsigned long) (u)) + ((unsigned long) l)) >> 8) & (NHASH-1)) | |
37 | ||
38 | static LIST_HEAD(unhead, union_node) unhead[NHASH]; | |
39 | static int unvplock[NHASH]; | |
b991bc2d JSP |
40 | |
41 | int | |
42 | union_init() | |
43 | { | |
58572fc0 JSP |
44 | int i; |
45 | ||
46 | for (i = 0; i < NHASH; i++) | |
47 | LIST_INIT(&unhead[i]); | |
48 | bzero((caddr_t) unvplock, sizeof(unvplock)); | |
49 | } | |
50 | ||
51 | static int | |
52 | union_list_lock(ix) | |
53 | int ix; | |
54 | { | |
55 | ||
56 | if (unvplock[ix] & UN_LOCKED) { | |
57 | unvplock[ix] |= UN_WANT; | |
58 | sleep((caddr_t) &unvplock[ix], PINOD); | |
59 | return (1); | |
60 | } | |
b991bc2d | 61 | |
58572fc0 JSP |
62 | unvplock[ix] |= UN_LOCKED; |
63 | ||
64 | return (0); | |
b991bc2d JSP |
65 | } |
66 | ||
e2a2f3a7 | 67 | static void |
58572fc0 JSP |
68 | union_list_unlock(ix) |
69 | int ix; | |
70 | { | |
71 | ||
72 | unvplock[ix] &= ~UN_LOCKED; | |
73 | ||
74 | if (unvplock[ix] & UN_WANT) { | |
75 | unvplock[ix] &= ~UN_WANT; | |
76 | wakeup((caddr_t) &unvplock[ix]); | |
77 | } | |
78 | } | |
79 | ||
80 | void | |
81 | union_updatevp(un, uppervp, lowervp) | |
e2a2f3a7 | 82 | struct union_node *un; |
58572fc0 JSP |
83 | struct vnode *uppervp; |
84 | struct vnode *lowervp; | |
e2a2f3a7 | 85 | { |
58572fc0 JSP |
86 | int ohash = UNION_HASH(un->un_uppervp, un->un_lowervp); |
87 | int nhash = UNION_HASH(uppervp, lowervp); | |
b5b5f291 | 88 | int docache = (lowervp != NULLVP || uppervp != NULLVP); |
e2a2f3a7 | 89 | |
b5b5f291 JSP |
90 | /* |
91 | * Ensure locking is ordered from lower to higher | |
92 | * to avoid deadlocks. | |
93 | */ | |
94 | if (nhash < ohash) { | |
95 | int t = ohash; | |
96 | ohash = nhash; | |
97 | nhash = t; | |
98 | } | |
58572fc0 | 99 | |
b5b5f291 | 100 | if (ohash != nhash) |
58572fc0 JSP |
101 | while (union_list_lock(ohash)) |
102 | continue; | |
103 | ||
b5b5f291 JSP |
104 | while (union_list_lock(nhash)) |
105 | continue; | |
58572fc0 | 106 | |
b5b5f291 JSP |
107 | if (ohash != nhash || !docache) { |
108 | if (un->un_flags & UN_CACHED) { | |
109 | LIST_REMOVE(un, un_cache); | |
110 | un->un_flags &= ~UN_CACHED; | |
111 | } | |
58572fc0 JSP |
112 | } |
113 | ||
b5b5f291 JSP |
114 | if (ohash != nhash) |
115 | union_list_unlock(ohash); | |
116 | ||
58572fc0 JSP |
117 | if (un->un_lowervp != lowervp) { |
118 | if (un->un_lowervp) { | |
119 | vrele(un->un_lowervp); | |
120 | if (un->un_path) { | |
121 | free(un->un_path, M_TEMP); | |
122 | un->un_path = 0; | |
123 | } | |
124 | if (un->un_dirvp) { | |
125 | vrele(un->un_dirvp); | |
126 | un->un_dirvp = NULLVP; | |
127 | } | |
e2a2f3a7 | 128 | } |
58572fc0 JSP |
129 | un->un_lowervp = lowervp; |
130 | } | |
131 | ||
132 | if (un->un_uppervp != uppervp) { | |
133 | if (un->un_uppervp) | |
134 | vrele(un->un_uppervp); | |
135 | ||
136 | un->un_uppervp = uppervp; | |
e2a2f3a7 | 137 | } |
58572fc0 | 138 | |
b5b5f291 | 139 | if (docache && (ohash != nhash)) { |
58572fc0 | 140 | LIST_INSERT_HEAD(&unhead[nhash], un, un_cache); |
b5b5f291 JSP |
141 | un->un_flags |= UN_CACHED; |
142 | } | |
58572fc0 JSP |
143 | |
144 | union_list_unlock(nhash); | |
145 | } | |
146 | ||
147 | void | |
148 | union_newlower(un, lowervp) | |
149 | struct union_node *un; | |
150 | struct vnode *lowervp; | |
151 | { | |
152 | ||
153 | union_updatevp(un, un->un_uppervp, lowervp); | |
154 | } | |
155 | ||
156 | void | |
157 | union_newupper(un, uppervp) | |
158 | struct union_node *un; | |
159 | struct vnode *uppervp; | |
160 | { | |
161 | ||
162 | union_updatevp(un, uppervp, un->un_lowervp); | |
e2a2f3a7 JSP |
163 | } |
164 | ||
b991bc2d JSP |
165 | /* |
166 | * allocate a union_node/vnode pair. the vnode is | |
01dac67e JSP |
167 | * referenced and locked. the new vnode is returned |
168 | * via (vpp). (mp) is the mountpoint of the union filesystem, | |
169 | * (dvp) is the parent directory where the upper layer object | |
170 | * should exist (but doesn't) and (cnp) is the componentname | |
171 | * information which is partially copied to allow the upper | |
172 | * layer object to be created at a later time. (uppervp) | |
173 | * and (lowervp) reference the upper and lower layer objects | |
174 | * being mapped. either, but not both, can be nil. | |
e2a2f3a7 | 175 | * if supplied, (uppervp) is locked. |
3b293975 JSP |
176 | * the reference is either maintained in the new union_node |
177 | * object which is allocated, or they are vrele'd. | |
b991bc2d JSP |
178 | * |
179 | * all union_nodes are maintained on a singly-linked | |
180 | * list. new nodes are only allocated when they cannot | |
181 | * be found on this list. entries on the list are | |
182 | * removed when the vfs reclaim entry is called. | |
183 | * | |
184 | * a single lock is kept for the entire list. this is | |
185 | * needed because the getnewvnode() function can block | |
186 | * waiting for a vnode to become free, in which case there | |
187 | * may be more than one process trying to get the same | |
188 | * vnode. this lock is only taken if we are going to | |
189 | * call getnewvnode, since the kernel itself is single-threaded. | |
190 | * | |
191 | * if an entry is found on the list, then call vget() to | |
192 | * take a reference. this is done because there may be | |
193 | * zero references to it and so it needs to removed from | |
194 | * the vnode free list. | |
195 | */ | |
196 | int | |
79f80c71 | 197 | union_allocvp(vpp, mp, undvp, dvp, cnp, uppervp, lowervp) |
b991bc2d JSP |
198 | struct vnode **vpp; |
199 | struct mount *mp; | |
79f80c71 | 200 | struct vnode *undvp; |
b991bc2d JSP |
201 | struct vnode *dvp; /* may be null */ |
202 | struct componentname *cnp; /* may be null */ | |
203 | struct vnode *uppervp; /* may be null */ | |
204 | struct vnode *lowervp; /* may be null */ | |
205 | { | |
206 | int error; | |
207 | struct union_node *un; | |
208 | struct union_node **pp; | |
58572fc0 | 209 | struct vnode *xlowervp = NULLVP; |
c93d29a8 | 210 | struct union_mount *um = MOUNTTOUNIONMOUNT(mp); |
58572fc0 | 211 | int hash; |
c93d29a8 | 212 | int vflag; |
58572fc0 | 213 | int try; |
01dac67e | 214 | |
58572fc0 | 215 | if (uppervp == NULLVP && lowervp == NULLVP) |
01dac67e JSP |
216 | panic("union: unidentifiable allocation"); |
217 | ||
218 | if (uppervp && lowervp && (uppervp->v_type != lowervp->v_type)) { | |
219 | xlowervp = lowervp; | |
58572fc0 | 220 | lowervp = NULLVP; |
01dac67e | 221 | } |
b991bc2d | 222 | |
c93d29a8 JSP |
223 | /* detect the root vnode (and aliases) */ |
224 | vflag = 0; | |
225 | if ((uppervp == um->um_uppervp) && | |
226 | ((lowervp == NULLVP) || lowervp == um->um_lowervp)) { | |
227 | if (lowervp == NULLVP) { | |
228 | lowervp = um->um_lowervp; | |
229 | VREF(lowervp); | |
230 | } | |
231 | vflag = VROOT; | |
232 | } | |
233 | ||
b991bc2d | 234 | loop: |
58572fc0 JSP |
235 | for (try = 0; try < 3; try++) { |
236 | switch (try) { | |
237 | case 0: | |
238 | if (lowervp == NULLVP) | |
239 | continue; | |
240 | hash = UNION_HASH(uppervp, lowervp); | |
e2a2f3a7 | 241 | break; |
58572fc0 JSP |
242 | |
243 | case 1: | |
244 | if (uppervp == NULLVP) | |
245 | continue; | |
246 | hash = UNION_HASH(uppervp, NULLVP); | |
247 | break; | |
248 | ||
249 | case 2: | |
250 | if (lowervp == NULLVP) | |
251 | continue; | |
252 | hash = UNION_HASH(NULLVP, lowervp); | |
253 | break; | |
254 | } | |
255 | ||
256 | while (union_list_lock(hash)) | |
257 | continue; | |
258 | ||
259 | for (un = unhead[hash].lh_first; un != 0; | |
260 | un = un->un_cache.le_next) { | |
261 | if ((un->un_lowervp == lowervp || | |
262 | un->un_lowervp == NULLVP) && | |
263 | (un->un_uppervp == uppervp || | |
264 | un->un_uppervp == NULLVP) && | |
265 | (UNIONTOV(un)->v_mount == mp)) { | |
266 | if (vget(UNIONTOV(un), 0)) { | |
267 | union_list_unlock(hash); | |
268 | goto loop; | |
269 | } | |
270 | break; | |
271 | } | |
e2a2f3a7 | 272 | } |
58572fc0 JSP |
273 | |
274 | union_list_unlock(hash); | |
275 | ||
276 | if (un) | |
277 | break; | |
e2a2f3a7 | 278 | } |
ed5969c8 | 279 | |
e2a2f3a7 JSP |
280 | if (un) { |
281 | /* | |
282 | * Obtain a lock on the union_node. | |
283 | * uppervp is locked, though un->un_uppervp | |
284 | * may not be. this doesn't break the locking | |
285 | * hierarchy since in the case that un->un_uppervp | |
286 | * is not yet locked it will be vrele'd and replaced | |
287 | * with uppervp. | |
288 | */ | |
289 | ||
290 | if ((dvp != NULLVP) && (uppervp == dvp)) { | |
ed5969c8 | 291 | /* |
e2a2f3a7 JSP |
292 | * Access ``.'', so (un) will already |
293 | * be locked. Since this process has | |
294 | * the lock on (uppervp) no other | |
295 | * process can hold the lock on (un). | |
ed5969c8 | 296 | */ |
e2a2f3a7 JSP |
297 | #ifdef DIAGNOSTIC |
298 | if ((un->un_flags & UN_LOCKED) == 0) | |
299 | panic("union: . not locked"); | |
300 | else if (curproc && un->un_pid != curproc->p_pid && | |
301 | un->un_pid > -1 && curproc->p_pid > -1) | |
302 | panic("union: allocvp not lock owner"); | |
303 | #endif | |
304 | } else { | |
305 | if (un->un_flags & UN_LOCKED) { | |
306 | vrele(UNIONTOV(un)); | |
307 | un->un_flags |= UN_WANT; | |
308 | sleep((caddr_t) &un->un_flags, PINOD); | |
309 | goto loop; | |
79f80c71 | 310 | } |
e2a2f3a7 | 311 | un->un_flags |= UN_LOCKED; |
ed5969c8 | 312 | |
e2a2f3a7 JSP |
313 | #ifdef DIAGNOSTIC |
314 | if (curproc) | |
315 | un->un_pid = curproc->p_pid; | |
316 | else | |
317 | un->un_pid = -1; | |
318 | #endif | |
319 | } | |
320 | ||
321 | /* | |
322 | * At this point, the union_node is locked, | |
323 | * un->un_uppervp may not be locked, and uppervp | |
324 | * is locked or nil. | |
325 | */ | |
326 | ||
327 | /* | |
328 | * Save information about the upper layer. | |
329 | */ | |
330 | if (uppervp != un->un_uppervp) { | |
58572fc0 | 331 | union_newupper(un, uppervp); |
e2a2f3a7 JSP |
332 | } else if (uppervp) { |
333 | vrele(uppervp); | |
334 | } | |
335 | ||
336 | if (un->un_uppervp) { | |
337 | un->un_flags |= UN_ULOCK; | |
338 | un->un_flags &= ~UN_KLOCK; | |
339 | } | |
340 | ||
341 | /* | |
342 | * Save information about the lower layer. | |
343 | * This needs to keep track of pathname | |
344 | * and directory information which union_vn_create | |
345 | * might need. | |
346 | */ | |
347 | if (lowervp != un->un_lowervp) { | |
58572fc0 | 348 | union_newlower(un, lowervp); |
e2a2f3a7 JSP |
349 | if (cnp && (lowervp != NULLVP) && |
350 | (lowervp->v_type == VREG)) { | |
351 | un->un_hash = cnp->cn_hash; | |
352 | un->un_path = malloc(cnp->cn_namelen+1, | |
353 | M_TEMP, M_WAITOK); | |
354 | bcopy(cnp->cn_nameptr, un->un_path, | |
355 | cnp->cn_namelen); | |
356 | un->un_path[cnp->cn_namelen] = '\0'; | |
357 | VREF(dvp); | |
358 | un->un_dirvp = dvp; | |
79f80c71 | 359 | } |
e2a2f3a7 JSP |
360 | } else if (lowervp) { |
361 | vrele(lowervp); | |
b991bc2d | 362 | } |
e2a2f3a7 JSP |
363 | *vpp = UNIONTOV(un); |
364 | return (0); | |
b991bc2d JSP |
365 | } |
366 | ||
367 | /* | |
368 | * otherwise lock the vp list while we call getnewvnode | |
369 | * since that can block. | |
370 | */ | |
58572fc0 JSP |
371 | hash = UNION_HASH(uppervp, lowervp); |
372 | ||
373 | if (union_list_lock(hash)) | |
b991bc2d | 374 | goto loop; |
b991bc2d JSP |
375 | |
376 | error = getnewvnode(VT_UNION, mp, union_vnodeop_p, vpp); | |
e2a2f3a7 JSP |
377 | if (error) { |
378 | if (uppervp) { | |
379 | if (dvp == uppervp) | |
380 | vrele(uppervp); | |
381 | else | |
382 | vput(uppervp); | |
383 | } | |
384 | if (lowervp) | |
385 | vrele(lowervp); | |
386 | ||
b991bc2d | 387 | goto out; |
e2a2f3a7 | 388 | } |
b991bc2d JSP |
389 | |
390 | MALLOC((*vpp)->v_data, void *, sizeof(struct union_node), | |
391 | M_TEMP, M_WAITOK); | |
392 | ||
c93d29a8 | 393 | (*vpp)->v_flag |= vflag; |
01dac67e JSP |
394 | if (uppervp) |
395 | (*vpp)->v_type = uppervp->v_type; | |
396 | else | |
397 | (*vpp)->v_type = lowervp->v_type; | |
b991bc2d | 398 | un = VTOUNION(*vpp); |
79f80c71 | 399 | un->un_vnode = *vpp; |
b991bc2d JSP |
400 | un->un_uppervp = uppervp; |
401 | un->un_lowervp = lowervp; | |
15a86f7e | 402 | un->un_openl = 0; |
e2a2f3a7 JSP |
403 | un->un_flags = UN_LOCKED; |
404 | if (un->un_uppervp) | |
405 | un->un_flags |= UN_ULOCK; | |
406 | #ifdef DIAGNOSTIC | |
407 | if (curproc) | |
408 | un->un_pid = curproc->p_pid; | |
409 | else | |
410 | un->un_pid = -1; | |
411 | #endif | |
ed5969c8 JSP |
412 | if (cnp && (lowervp != NULLVP) && (lowervp->v_type == VREG)) { |
413 | un->un_hash = cnp->cn_hash; | |
b991bc2d JSP |
414 | un->un_path = malloc(cnp->cn_namelen+1, M_TEMP, M_WAITOK); |
415 | bcopy(cnp->cn_nameptr, un->un_path, cnp->cn_namelen); | |
416 | un->un_path[cnp->cn_namelen] = '\0'; | |
01dac67e JSP |
417 | VREF(dvp); |
418 | un->un_dirvp = dvp; | |
b991bc2d | 419 | } else { |
ed5969c8 | 420 | un->un_hash = 0; |
b991bc2d | 421 | un->un_path = 0; |
01dac67e | 422 | un->un_dirvp = 0; |
b991bc2d JSP |
423 | } |
424 | ||
58572fc0 | 425 | LIST_INSERT_HEAD(&unhead[hash], un, un_cache); |
b5b5f291 | 426 | un->un_flags |= UN_CACHED; |
b991bc2d | 427 | |
01dac67e JSP |
428 | if (xlowervp) |
429 | vrele(xlowervp); | |
430 | ||
b991bc2d | 431 | out: |
58572fc0 | 432 | union_list_unlock(hash); |
b991bc2d | 433 | |
b991bc2d JSP |
434 | return (error); |
435 | } | |
436 | ||
437 | int | |
438 | union_freevp(vp) | |
439 | struct vnode *vp; | |
440 | { | |
b991bc2d JSP |
441 | struct union_node *un = VTOUNION(vp); |
442 | ||
b5b5f291 JSP |
443 | if (un->un_flags & UN_CACHED) { |
444 | LIST_REMOVE(un, un_cache); | |
445 | un->un_flags &= ~UN_CACHED; | |
446 | } | |
58572fc0 | 447 | |
b5b5f291 | 448 | if (un->un_uppervp != NULLVP) |
58572fc0 | 449 | vrele(un->un_uppervp); |
b5b5f291 | 450 | if (un->un_lowervp != NULLVP) |
58572fc0 | 451 | vrele(un->un_lowervp); |
b5b5f291 | 452 | if (un->un_dirvp != NULLVP) |
58572fc0 JSP |
453 | vrele(un->un_dirvp); |
454 | if (un->un_path) | |
455 | free(un->un_path, M_TEMP); | |
b991bc2d | 456 | |
b991bc2d JSP |
457 | FREE(vp->v_data, M_TEMP); |
458 | vp->v_data = 0; | |
58572fc0 | 459 | |
b991bc2d JSP |
460 | return (0); |
461 | } | |
81be6ee0 JSP |
462 | |
463 | /* | |
464 | * copyfile. copy the vnode (fvp) to the vnode (tvp) | |
465 | * using a sequence of reads and writes. both (fvp) | |
466 | * and (tvp) are locked on entry and exit. | |
467 | */ | |
468 | int | |
469 | union_copyfile(p, cred, fvp, tvp) | |
470 | struct proc *p; | |
471 | struct ucred *cred; | |
472 | struct vnode *fvp; | |
473 | struct vnode *tvp; | |
474 | { | |
475 | char *buf; | |
476 | struct uio uio; | |
477 | struct iovec iov; | |
478 | int error = 0; | |
479 | ||
480 | /* | |
481 | * strategy: | |
482 | * allocate a buffer of size MAXBSIZE. | |
483 | * loop doing reads and writes, keeping track | |
484 | * of the current uio offset. | |
485 | * give up at the first sign of trouble. | |
486 | */ | |
487 | ||
488 | uio.uio_procp = p; | |
489 | uio.uio_segflg = UIO_SYSSPACE; | |
490 | uio.uio_offset = 0; | |
491 | ||
492 | VOP_UNLOCK(fvp); /* XXX */ | |
493 | LEASE_CHECK(fvp, p, cred, LEASE_READ); | |
494 | VOP_LOCK(fvp); /* XXX */ | |
495 | VOP_UNLOCK(tvp); /* XXX */ | |
496 | LEASE_CHECK(tvp, p, cred, LEASE_WRITE); | |
497 | VOP_LOCK(tvp); /* XXX */ | |
498 | ||
499 | buf = malloc(MAXBSIZE, M_TEMP, M_WAITOK); | |
500 | ||
501 | /* ugly loop follows... */ | |
502 | do { | |
503 | off_t offset = uio.uio_offset; | |
504 | ||
505 | uio.uio_iov = &iov; | |
506 | uio.uio_iovcnt = 1; | |
507 | iov.iov_base = buf; | |
508 | iov.iov_len = MAXBSIZE; | |
509 | uio.uio_resid = iov.iov_len; | |
510 | uio.uio_rw = UIO_READ; | |
511 | error = VOP_READ(fvp, &uio, 0, cred); | |
512 | ||
513 | if (error == 0) { | |
514 | uio.uio_iov = &iov; | |
515 | uio.uio_iovcnt = 1; | |
516 | iov.iov_base = buf; | |
517 | iov.iov_len = MAXBSIZE - uio.uio_resid; | |
518 | uio.uio_offset = offset; | |
519 | uio.uio_rw = UIO_WRITE; | |
520 | uio.uio_resid = iov.iov_len; | |
521 | ||
522 | if (uio.uio_resid == 0) | |
523 | break; | |
524 | ||
525 | do { | |
526 | error = VOP_WRITE(tvp, &uio, 0, cred); | |
527 | } while ((uio.uio_resid > 0) && (error == 0)); | |
528 | } | |
529 | ||
530 | } while (error == 0); | |
531 | ||
532 | free(buf, M_TEMP); | |
533 | return (error); | |
534 | } | |
535 | ||
3b293975 JSP |
536 | /* |
537 | * Create a shadow directory in the upper layer. | |
538 | * The new vnode is returned locked. | |
539 | * | |
540 | * (um) points to the union mount structure for access to the | |
541 | * the mounting process's credentials. | |
542 | * (dvp) is the directory in which to create the shadow directory. | |
543 | * it is unlocked on entry and exit. | |
544 | * (cnp) is the componentname to be created. | |
545 | * (vpp) is the returned newly created shadow directory, which | |
546 | * is returned locked. | |
547 | */ | |
548 | int | |
549 | union_mkshadow(um, dvp, cnp, vpp) | |
550 | struct union_mount *um; | |
551 | struct vnode *dvp; | |
552 | struct componentname *cnp; | |
553 | struct vnode **vpp; | |
554 | { | |
555 | int error; | |
556 | struct vattr va; | |
557 | struct proc *p = cnp->cn_proc; | |
558 | struct componentname cn; | |
559 | ||
560 | /* | |
561 | * policy: when creating the shadow directory in the | |
692a90df JSP |
562 | * upper layer, create it owned by the user who did |
563 | * the mount, group from parent directory, and mode | |
564 | * 777 modified by umask (ie mostly identical to the | |
565 | * mkdir syscall). (jsp, kb) | |
3b293975 JSP |
566 | */ |
567 | ||
568 | /* | |
569 | * A new componentname structure must be faked up because | |
570 | * there is no way to know where the upper level cnp came | |
571 | * from or what it is being used for. This must duplicate | |
572 | * some of the work done by NDINIT, some of the work done | |
573 | * by namei, some of the work done by lookup and some of | |
574 | * the work done by VOP_LOOKUP when given a CREATE flag. | |
575 | * Conclusion: Horrible. | |
576 | * | |
577 | * The pathname buffer will be FREEed by VOP_MKDIR. | |
578 | */ | |
579 | cn.cn_pnbuf = malloc(cnp->cn_namelen+1, M_NAMEI, M_WAITOK); | |
ed5969c8 JSP |
580 | bcopy(cnp->cn_nameptr, cn.cn_pnbuf, cnp->cn_namelen); |
581 | cn.cn_pnbuf[cnp->cn_namelen] = '\0'; | |
3b293975 JSP |
582 | |
583 | cn.cn_nameiop = CREATE; | |
bf006388 | 584 | cn.cn_flags = (LOCKPARENT|HASBUF|SAVENAME|SAVESTART|ISLASTCN); |
3b293975 | 585 | cn.cn_proc = cnp->cn_proc; |
c1b204a8 JSP |
586 | if (um->um_op == UNMNT_ABOVE) |
587 | cn.cn_cred = cnp->cn_cred; | |
588 | else | |
589 | cn.cn_cred = um->um_cred; | |
3b293975 JSP |
590 | cn.cn_nameptr = cn.cn_pnbuf; |
591 | cn.cn_namelen = cnp->cn_namelen; | |
592 | cn.cn_hash = cnp->cn_hash; | |
593 | cn.cn_consume = cnp->cn_consume; | |
594 | ||
ed5969c8 | 595 | VREF(dvp); |
3b293975 JSP |
596 | if (error = relookup(dvp, vpp, &cn)) |
597 | return (error); | |
ed5969c8 | 598 | vrele(dvp); |
3b293975 JSP |
599 | |
600 | if (*vpp) { | |
601 | VOP_ABORTOP(dvp, &cn); | |
602 | VOP_UNLOCK(dvp); | |
603 | vrele(*vpp); | |
604 | *vpp = NULLVP; | |
605 | return (EEXIST); | |
606 | } | |
607 | ||
608 | VATTR_NULL(&va); | |
609 | va.va_type = VDIR; | |
692a90df | 610 | va.va_mode = um->um_cmode; |
3b293975 JSP |
611 | |
612 | /* LEASE_CHECK: dvp is locked */ | |
613 | LEASE_CHECK(dvp, p, p->p_ucred, LEASE_WRITE); | |
614 | ||
3b293975 JSP |
615 | error = VOP_MKDIR(dvp, vpp, &cn, &va); |
616 | return (error); | |
617 | } | |
618 | ||
81be6ee0 JSP |
619 | /* |
620 | * union_vn_create: creates and opens a new shadow file | |
621 | * on the upper union layer. this function is similar | |
622 | * in spirit to calling vn_open but it avoids calling namei(). | |
623 | * the problem with calling namei is that a) it locks too many | |
624 | * things, and b) it doesn't start at the "right" directory, | |
625 | * whereas relookup is told where to start. | |
626 | */ | |
627 | int | |
3b293975 | 628 | union_vn_create(vpp, un, p) |
81be6ee0 JSP |
629 | struct vnode **vpp; |
630 | struct union_node *un; | |
81be6ee0 JSP |
631 | struct proc *p; |
632 | { | |
633 | struct vnode *vp; | |
634 | struct ucred *cred = p->p_ucred; | |
635 | struct vattr vat; | |
636 | struct vattr *vap = &vat; | |
637 | int fmode = FFLAGS(O_WRONLY|O_CREAT|O_TRUNC|O_EXCL); | |
638 | int error; | |
ed5969c8 | 639 | int cmode = UN_FILEMODE & ~p->p_fd->fd_cmask; |
81be6ee0 JSP |
640 | char *cp; |
641 | struct componentname cn; | |
642 | ||
643 | *vpp = NULLVP; | |
644 | ||
692a90df JSP |
645 | /* |
646 | * Build a new componentname structure (for the same | |
647 | * reasons outlines in union_mkshadow). | |
648 | * The difference here is that the file is owned by | |
649 | * the current user, rather than by the person who | |
650 | * did the mount, since the current user needs to be | |
651 | * able to write the file (that's why it is being | |
652 | * copied in the first place). | |
653 | */ | |
81be6ee0 JSP |
654 | cn.cn_namelen = strlen(un->un_path); |
655 | cn.cn_pnbuf = (caddr_t) malloc(cn.cn_namelen, M_NAMEI, M_WAITOK); | |
656 | bcopy(un->un_path, cn.cn_pnbuf, cn.cn_namelen+1); | |
657 | cn.cn_nameiop = CREATE; | |
bf006388 | 658 | cn.cn_flags = (LOCKPARENT|HASBUF|SAVENAME|SAVESTART|ISLASTCN); |
81be6ee0 JSP |
659 | cn.cn_proc = p; |
660 | cn.cn_cred = p->p_ucred; | |
661 | cn.cn_nameptr = cn.cn_pnbuf; | |
ed5969c8 | 662 | cn.cn_hash = un->un_hash; |
81be6ee0 JSP |
663 | cn.cn_consume = 0; |
664 | ||
ed5969c8 | 665 | VREF(un->un_dirvp); |
81be6ee0 JSP |
666 | if (error = relookup(un->un_dirvp, &vp, &cn)) |
667 | return (error); | |
ed5969c8 JSP |
668 | vrele(un->un_dirvp); |
669 | ||
bf006388 | 670 | if (vp) { |
81be6ee0 JSP |
671 | VOP_ABORTOP(un->un_dirvp, &cn); |
672 | if (un->un_dirvp == vp) | |
673 | vrele(un->un_dirvp); | |
674 | else | |
bf006388 JSP |
675 | vput(un->un_dirvp); |
676 | vrele(vp); | |
677 | return (EEXIST); | |
81be6ee0 JSP |
678 | } |
679 | ||
bf006388 JSP |
680 | /* |
681 | * Good - there was no race to create the file | |
682 | * so go ahead and create it. The permissions | |
683 | * on the file will be 0666 modified by the | |
684 | * current user's umask. Access to the file, while | |
685 | * it is unioned, will require access to the top *and* | |
686 | * bottom files. Access when not unioned will simply | |
687 | * require access to the top-level file. | |
688 | * TODO: confirm choice of access permissions. | |
689 | */ | |
81be6ee0 | 690 | VATTR_NULL(vap); |
bf006388 JSP |
691 | vap->va_type = VREG; |
692 | vap->va_mode = cmode; | |
693 | LEASE_CHECK(un->un_dirvp, p, cred, LEASE_WRITE); | |
694 | if (error = VOP_CREATE(un->un_dirvp, &vp, &cn, vap)) | |
695 | return (error); | |
81be6ee0 | 696 | |
bf006388 JSP |
697 | if (error = VOP_OPEN(vp, fmode, cred, p)) { |
698 | vput(vp); | |
699 | return (error); | |
700 | } | |
81be6ee0 JSP |
701 | |
702 | vp->v_writecount++; | |
703 | *vpp = vp; | |
704 | return (0); | |
81be6ee0 | 705 | } |
ed5969c8 JSP |
706 | |
707 | int | |
15a86f7e | 708 | union_vn_close(vp, fmode, cred, p) |
ed5969c8 JSP |
709 | struct vnode *vp; |
710 | int fmode; | |
15a86f7e JSP |
711 | struct ucred *cred; |
712 | struct proc *p; | |
ed5969c8 JSP |
713 | { |
714 | if (fmode & FWRITE) | |
715 | --vp->v_writecount; | |
716 | return (VOP_CLOSE(vp, fmode)); | |
717 | } | |
718 | ||
719 | void | |
720 | union_removed_upper(un) | |
721 | struct union_node *un; | |
722 | { | |
e2a2f3a7 JSP |
723 | if (un->un_flags & UN_ULOCK) { |
724 | un->un_flags &= ~UN_ULOCK; | |
58572fc0 | 725 | VOP_UNLOCK(un->un_uppervp); |
e2a2f3a7 | 726 | } |
58572fc0 JSP |
727 | |
728 | union_newupper(un, NULLVP); | |
ed5969c8 | 729 | } |
15a86f7e JSP |
730 | |
731 | struct vnode * | |
732 | union_lowervp(vp) | |
733 | struct vnode *vp; | |
734 | { | |
735 | struct union_node *un = VTOUNION(vp); | |
736 | ||
737 | if (un->un_lowervp && (vp->v_type == un->un_lowervp->v_type)) { | |
738 | if (vget(un->un_lowervp, 0)) | |
739 | return (NULLVP); | |
740 | } | |
741 | ||
742 | return (un->un_lowervp); | |
743 | } |