unionfs_subr.c revision 1.16 1 1.1 ad /*-
2 1.1 ad * Copyright (c) 1994 Jan-Simon Pendry
3 1.1 ad * Copyright (c) 1994
4 1.1 ad * The Regents of the University of California. All rights reserved.
5 1.1 ad * Copyright (c) 2005, 2006 Masanori Ozawa <ozawa (at) ongs.co.jp>, ONGS Inc.
6 1.1 ad * Copyright (c) 2006 Daichi Goto <daichi (at) freebsd.org>
7 1.1 ad *
8 1.1 ad * This code is derived from software contributed to Berkeley by
9 1.1 ad * Jan-Simon Pendry.
10 1.1 ad *
11 1.1 ad * Redistribution and use in source and binary forms, with or without
12 1.1 ad * modification, are permitted provided that the following conditions
13 1.1 ad * are met:
14 1.1 ad * 1. Redistributions of source code must retain the above copyright
15 1.1 ad * notice, this list of conditions and the following disclaimer.
16 1.1 ad * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 ad * notice, this list of conditions and the following disclaimer in the
18 1.1 ad * documentation and/or other materials provided with the distribution.
19 1.1 ad * 4. Neither the name of the University nor the names of its contributors
20 1.1 ad * may be used to endorse or promote products derived from this software
21 1.1 ad * without specific prior written permission.
22 1.1 ad *
23 1.1 ad * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 ad * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 ad * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 ad * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 ad * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 ad * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 ad * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 ad * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 ad * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 ad * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 ad * SUCH DAMAGE.
34 1.1 ad *
35 1.1 ad * @(#)union_subr.c 8.20 (Berkeley) 5/20/95
36 1.1 ad * $FreeBSD: src/sys/fs/unionfs/union_subr.c,v 1.99 2008/01/24 12:34:27 attilio Exp $
37 1.1 ad */
38 1.1 ad
39 1.1 ad #include <sys/param.h>
40 1.1 ad #include <sys/systm.h>
41 1.1 ad #include <sys/kernel.h>
42 1.1 ad #include <sys/lock.h>
43 1.1 ad #include <sys/mutex.h>
44 1.1 ad #include <sys/malloc.h>
45 1.1 ad #include <sys/mount.h>
46 1.1 ad #include <sys/namei.h>
47 1.1 ad #include <sys/proc.h>
48 1.1 ad #include <sys/vnode.h>
49 1.1 ad #include <sys/dirent.h>
50 1.1 ad #include <sys/fcntl.h>
51 1.1 ad #include <sys/filedesc.h>
52 1.1 ad #include <sys/stat.h>
53 1.1 ad #include <sys/kauth.h>
54 1.1 ad #include <sys/resourcevar.h>
55 1.1 ad
56 1.1 ad #include <fs/unionfs/unionfs.h>
57 1.1 ad
58 1.1 ad MALLOC_DEFINE(M_UNIONFSPATH, "UNIONFS path", "UNIONFS path private part");
59 1.1 ad
60 1.1 ad /*
61 1.1 ad * Make a new or get existing unionfs node.
62 1.1 ad *
63 1.1 ad * uppervp and lowervp should be unlocked. Because if new unionfs vnode is
64 1.1 ad * locked, uppervp or lowervp is locked too. In order to prevent dead lock,
65 1.1 ad * you should not lock plurality simultaneously.
66 1.1 ad */
67 1.1 ad int
68 1.1 ad unionfs_nodeget(struct mount *mp, struct vnode *uppervp,
69 1.1 ad struct vnode *lowervp, struct vnode *dvp,
70 1.1 ad struct vnode **vpp, struct componentname *cnp)
71 1.1 ad {
72 1.1 ad struct unionfs_mount *ump;
73 1.1 ad struct unionfs_node *unp;
74 1.1 ad struct vnode *vp;
75 1.1 ad int error;
76 1.1 ad const char *path;
77 1.1 ad
78 1.1 ad ump = MOUNTTOUNIONFSMOUNT(mp);
79 1.1 ad path = (cnp ? cnp->cn_nameptr : NULL);
80 1.1 ad
81 1.1 ad if (uppervp == NULLVP && lowervp == NULLVP)
82 1.1 ad panic("unionfs_nodeget: upper and lower is null");
83 1.1 ad
84 1.1 ad /* If it has no ISLASTCN flag, path check is skipped. */
85 1.1 ad if (cnp && !(cnp->cn_flags & ISLASTCN))
86 1.1 ad path = NULL;
87 1.1 ad
88 1.1 ad if ((uppervp == NULLVP || ump->um_uppervp != uppervp) ||
89 1.1 ad (lowervp == NULLVP || ump->um_lowervp != lowervp)) {
90 1.1 ad if (dvp == NULLVP)
91 1.1 ad return (EINVAL);
92 1.1 ad }
93 1.1 ad
94 1.13 rmind /*
95 1.13 rmind * Get a new vnode and share the lock with upper layer vnode,
96 1.13 rmind * unless layers are inverted.
97 1.13 rmind */
98 1.13 rmind vnode_t *svp = (uppervp != NULLVP) ? uppervp : lowervp;
99 1.16 christos error = vcache_get(mp, svp, sizeof(svp), &vp);
100 1.1 ad if (error != 0) {
101 1.1 ad return (error);
102 1.1 ad }
103 1.1 ad if (dvp != NULLVP)
104 1.1 ad vref(dvp);
105 1.1 ad if (uppervp != NULLVP)
106 1.1 ad vref(uppervp);
107 1.1 ad if (lowervp != NULLVP)
108 1.1 ad vref(lowervp);
109 1.1 ad
110 1.13 rmind unp = kmem_zalloc(sizeof(*unp), KM_SLEEP);
111 1.1 ad unp->un_vnode = vp;
112 1.1 ad unp->un_uppervp = uppervp;
113 1.1 ad unp->un_lowervp = lowervp;
114 1.1 ad unp->un_dvp = dvp;
115 1.1 ad
116 1.1 ad if (path != NULL) {
117 1.1 ad unp->un_path = (char *)
118 1.1 ad malloc(cnp->cn_namelen +1, M_UNIONFSPATH, M_WAITOK|M_ZERO);
119 1.4 tsutsui memcpy(unp->un_path, cnp->cn_nameptr, cnp->cn_namelen);
120 1.1 ad unp->un_path[cnp->cn_namelen] = '\0';
121 1.1 ad }
122 1.1 ad vp->v_type = (uppervp != NULLVP ? uppervp->v_type : lowervp->v_type);
123 1.1 ad vp->v_data = unp;
124 1.1 ad uvm_vnp_setsize(vp, 0);
125 1.1 ad
126 1.1 ad if ((uppervp != NULLVP && ump->um_uppervp == uppervp) &&
127 1.1 ad (lowervp != NULLVP && ump->um_lowervp == lowervp))
128 1.1 ad vp->v_vflag |= VV_ROOT;
129 1.1 ad
130 1.1 ad *vpp = vp;
131 1.1 ad
132 1.1 ad return (0);
133 1.1 ad }
134 1.1 ad
135 1.1 ad /*
136 1.1 ad * Clean up the unionfs node.
137 1.1 ad */
138 1.1 ad void
139 1.1 ad unionfs_noderem(struct vnode *vp)
140 1.1 ad {
141 1.1 ad struct unionfs_node *unp;
142 1.1 ad struct unionfs_node_status *unsp;
143 1.1 ad struct vnode *lvp;
144 1.1 ad struct vnode *uvp;
145 1.1 ad
146 1.1 ad /*
147 1.1 ad * Use the interlock to protect the clearing of v_data to
148 1.1 ad * prevent faults in unionfs_lock().
149 1.1 ad */
150 1.1 ad unp = VTOUNIONFS(vp);
151 1.1 ad lvp = unp->un_lowervp;
152 1.1 ad uvp = unp->un_uppervp;
153 1.1 ad unp->un_lowervp = unp->un_uppervp = NULLVP;
154 1.1 ad vp->v_data = NULL;
155 1.1 ad
156 1.1 ad if (lvp != NULLVP)
157 1.1 ad vrele(lvp);
158 1.1 ad if (uvp != NULLVP)
159 1.1 ad vrele(uvp);
160 1.1 ad if (unp->un_dvp != NULLVP) {
161 1.1 ad vrele(unp->un_dvp);
162 1.1 ad unp->un_dvp = NULLVP;
163 1.1 ad }
164 1.1 ad if (unp->un_path) {
165 1.1 ad free(unp->un_path, M_UNIONFSPATH);
166 1.1 ad unp->un_path = NULL;
167 1.1 ad }
168 1.1 ad
169 1.1 ad while ((unsp = LIST_FIRST(&(unp->un_unshead))) != NULL) {
170 1.1 ad LIST_REMOVE(unsp, uns_list);
171 1.1 ad free(unsp, M_TEMP);
172 1.1 ad }
173 1.1 ad kmem_free(unp, sizeof(*unp));
174 1.1 ad }
175 1.1 ad
176 1.1 ad /*
177 1.1 ad * Get the unionfs node status.
178 1.1 ad * You need exclusive lock this vnode.
179 1.1 ad */
180 1.1 ad void
181 1.1 ad unionfs_get_node_status(struct unionfs_node *unp,
182 1.1 ad struct unionfs_node_status **unspp)
183 1.1 ad {
184 1.1 ad struct unionfs_node_status *unsp;
185 1.1 ad pid_t pid;
186 1.1 ad lwpid_t lid;
187 1.1 ad
188 1.1 ad KASSERT(NULL != unspp);
189 1.1 ad KASSERT(VOP_ISLOCKED(UNIONFSTOV(unp)) == LK_EXCLUSIVE);
190 1.1 ad
191 1.1 ad pid = curproc->p_pid;
192 1.1 ad lid = curlwp->l_lid;
193 1.1 ad
194 1.1 ad LIST_FOREACH(unsp, &(unp->un_unshead), uns_list) {
195 1.1 ad if (unsp->uns_pid == pid && unsp->uns_lid == lid) {
196 1.1 ad *unspp = unsp;
197 1.1 ad return;
198 1.1 ad }
199 1.1 ad }
200 1.1 ad
201 1.1 ad /* create a new unionfs node status */
202 1.1 ad unsp = kmem_zalloc(sizeof(*unsp), KM_SLEEP);
203 1.1 ad unsp->uns_pid = pid;
204 1.1 ad unsp->uns_lid = lid;
205 1.1 ad LIST_INSERT_HEAD(&(unp->un_unshead), unsp, uns_list);
206 1.1 ad
207 1.1 ad *unspp = unsp;
208 1.1 ad }
209 1.1 ad
210 1.1 ad /*
211 1.1 ad * Remove the unionfs node status, if you can.
212 1.1 ad * You need exclusive lock this vnode.
213 1.1 ad */
214 1.1 ad void
215 1.1 ad unionfs_tryrem_node_status(struct unionfs_node *unp,
216 1.1 ad struct unionfs_node_status *unsp)
217 1.1 ad {
218 1.1 ad KASSERT(NULL != unsp);
219 1.1 ad KASSERT(VOP_ISLOCKED(UNIONFSTOV(unp)) == LK_EXCLUSIVE);
220 1.1 ad
221 1.1 ad if (0 < unsp->uns_lower_opencnt || 0 < unsp->uns_upper_opencnt)
222 1.1 ad return;
223 1.1 ad
224 1.1 ad LIST_REMOVE(unsp, uns_list);
225 1.1 ad kmem_free(unsp, sizeof(*unsp));
226 1.1 ad }
227 1.1 ad
228 1.1 ad /*
229 1.1 ad * Create upper node attr.
230 1.1 ad */
231 1.1 ad void
232 1.1 ad unionfs_create_uppervattr_core(struct unionfs_mount *ump,
233 1.1 ad struct vattr *lva,
234 1.1 ad struct vattr *uva)
235 1.1 ad {
236 1.5 pooka vattr_null(uva);
237 1.1 ad uva->va_type = lva->va_type;
238 1.1 ad uva->va_atime = lva->va_atime;
239 1.1 ad uva->va_mtime = lva->va_mtime;
240 1.1 ad uva->va_ctime = lva->va_ctime;
241 1.1 ad
242 1.1 ad switch (ump->um_copymode) {
243 1.1 ad case UNIONFS_TRANSPARENT:
244 1.1 ad uva->va_mode = lva->va_mode;
245 1.1 ad uva->va_uid = lva->va_uid;
246 1.1 ad uva->va_gid = lva->va_gid;
247 1.1 ad break;
248 1.1 ad case UNIONFS_MASQUERADE:
249 1.1 ad if (ump->um_uid == lva->va_uid) {
250 1.1 ad uva->va_mode = lva->va_mode & 077077;
251 1.1 ad uva->va_mode |= (lva->va_type == VDIR ? ump->um_udir : ump->um_ufile) & 0700;
252 1.1 ad uva->va_uid = lva->va_uid;
253 1.1 ad uva->va_gid = lva->va_gid;
254 1.1 ad } else {
255 1.1 ad uva->va_mode = (lva->va_type == VDIR ? ump->um_udir : ump->um_ufile);
256 1.1 ad uva->va_uid = ump->um_uid;
257 1.1 ad uva->va_gid = ump->um_gid;
258 1.1 ad }
259 1.1 ad break;
260 1.1 ad default: /* UNIONFS_TRADITIONAL */
261 1.1 ad uva->va_mode = 0777 & ~curproc->p_cwdi->cwdi_cmask;
262 1.1 ad uva->va_uid = ump->um_uid;
263 1.1 ad uva->va_gid = ump->um_gid;
264 1.1 ad break;
265 1.1 ad }
266 1.1 ad }
267 1.1 ad
268 1.1 ad /*
269 1.1 ad * Create upper node attr.
270 1.1 ad */
271 1.1 ad int
272 1.1 ad unionfs_create_uppervattr(struct unionfs_mount *ump,
273 1.1 ad struct vnode *lvp,
274 1.1 ad struct vattr *uva,
275 1.1 ad kauth_cred_t cred)
276 1.1 ad {
277 1.1 ad int error;
278 1.1 ad struct vattr lva;
279 1.1 ad
280 1.1 ad if ((error = VOP_GETATTR(lvp, &lva, cred)))
281 1.1 ad return (error);
282 1.1 ad
283 1.1 ad unionfs_create_uppervattr_core(ump, &lva, uva);
284 1.1 ad
285 1.1 ad return (error);
286 1.1 ad }
287 1.1 ad
288 1.1 ad /*
289 1.1 ad * relookup
290 1.1 ad *
291 1.1 ad * dvp should be locked on entry and will be locked on return.
292 1.1 ad *
293 1.1 ad * If an error is returned, *vpp will be invalid, otherwise it will hold a
294 1.1 ad * locked, referenced vnode. If *vpp == dvp then remember that only one
295 1.1 ad * LK_EXCLUSIVE lock is held.
296 1.1 ad */
297 1.1 ad static int
298 1.1 ad unionfs_relookup(struct vnode *dvp, struct vnode **vpp,
299 1.1 ad struct componentname *cnp, struct componentname *cn,
300 1.10 dholland char **pnbuf_ret,
301 1.1 ad const char *path, int pathlen, u_long nameiop)
302 1.1 ad {
303 1.1 ad int error;
304 1.10 dholland char *pnbuf;
305 1.1 ad
306 1.1 ad cn->cn_namelen = pathlen;
307 1.10 dholland pnbuf = PNBUF_GET();
308 1.10 dholland memcpy(pnbuf, path, pathlen);
309 1.10 dholland pnbuf[pathlen] = '\0';
310 1.1 ad
311 1.1 ad cn->cn_nameiop = nameiop;
312 1.11 dholland cn->cn_flags = (LOCKPARENT | LOCKLEAF | ISLASTCN);
313 1.1 ad cn->cn_cred = cnp->cn_cred;
314 1.1 ad
315 1.10 dholland cn->cn_nameptr = pnbuf;
316 1.1 ad
317 1.1 ad if (nameiop == DELETE)
318 1.12 dholland cn->cn_flags |= (cnp->cn_flags & DOWHITEOUT);
319 1.1 ad
320 1.1 ad vref(dvp);
321 1.9 hannken VOP_UNLOCK(dvp);
322 1.1 ad
323 1.12 dholland if ((error = relookup(dvp, vpp, cn, 0))) {
324 1.10 dholland PNBUF_PUT(pnbuf);
325 1.10 dholland *pnbuf_ret = NULL;
326 1.1 ad vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
327 1.10 dholland } else {
328 1.10 dholland *pnbuf_ret = pnbuf;
329 1.1 ad vrele(dvp);
330 1.10 dholland }
331 1.1 ad
332 1.1 ad return (error);
333 1.1 ad }
334 1.1 ad
335 1.1 ad /*
336 1.1 ad * relookup for CREATE namei operation.
337 1.1 ad *
338 1.1 ad * dvp is unionfs vnode. dvp should be locked.
339 1.1 ad *
340 1.1 ad * If it called 'unionfs_copyfile' function by unionfs_link etc,
341 1.1 ad * VOP_LOOKUP information is broken.
342 1.1 ad * So it need relookup in order to create link etc.
343 1.1 ad */
344 1.1 ad int
345 1.1 ad unionfs_relookup_for_create(struct vnode *dvp, struct componentname *cnp)
346 1.1 ad {
347 1.1 ad int error;
348 1.1 ad struct vnode *udvp;
349 1.1 ad struct vnode *vp;
350 1.1 ad struct componentname cn;
351 1.10 dholland char *pnbuf;
352 1.1 ad
353 1.1 ad udvp = UNIONFSVPTOUPPERVP(dvp);
354 1.1 ad vp = NULLVP;
355 1.1 ad
356 1.10 dholland error = unionfs_relookup(udvp, &vp, cnp, &cn, &pnbuf,
357 1.10 dholland cnp->cn_nameptr,
358 1.1 ad strlen(cnp->cn_nameptr), CREATE);
359 1.1 ad if (error)
360 1.1 ad return (error);
361 1.1 ad
362 1.1 ad if (vp != NULLVP) {
363 1.1 ad if (udvp == vp)
364 1.1 ad vrele(vp);
365 1.1 ad else
366 1.1 ad vput(vp);
367 1.1 ad
368 1.1 ad error = EEXIST;
369 1.1 ad }
370 1.1 ad
371 1.10 dholland PNBUF_PUT(pnbuf);
372 1.1 ad
373 1.1 ad if (!error) {
374 1.1 ad cnp->cn_flags = cn.cn_flags;
375 1.1 ad }
376 1.1 ad
377 1.1 ad return (error);
378 1.1 ad }
379 1.1 ad
380 1.1 ad /*
381 1.1 ad * relookup for DELETE namei operation.
382 1.1 ad *
383 1.1 ad * dvp is unionfs vnode. dvp should be locked.
384 1.1 ad */
385 1.1 ad int
386 1.1 ad unionfs_relookup_for_delete(struct vnode *dvp, struct componentname *cnp)
387 1.1 ad {
388 1.1 ad int error;
389 1.1 ad struct vnode *udvp;
390 1.1 ad struct vnode *vp;
391 1.1 ad struct componentname cn;
392 1.10 dholland char *pnbuf;
393 1.1 ad
394 1.1 ad udvp = UNIONFSVPTOUPPERVP(dvp);
395 1.1 ad vp = NULLVP;
396 1.1 ad
397 1.10 dholland error = unionfs_relookup(udvp, &vp, cnp, &cn, &pnbuf, cnp->cn_nameptr,
398 1.1 ad strlen(cnp->cn_nameptr), DELETE);
399 1.1 ad if (error)
400 1.1 ad return (error);
401 1.1 ad
402 1.1 ad if (vp == NULLVP)
403 1.1 ad error = ENOENT;
404 1.1 ad else {
405 1.1 ad if (udvp == vp)
406 1.1 ad vrele(vp);
407 1.1 ad else
408 1.1 ad vput(vp);
409 1.1 ad }
410 1.1 ad
411 1.10 dholland PNBUF_PUT(pnbuf);
412 1.1 ad
413 1.1 ad if (!error) {
414 1.1 ad cnp->cn_flags = cn.cn_flags;
415 1.1 ad }
416 1.1 ad
417 1.1 ad return (error);
418 1.1 ad }
419 1.1 ad
420 1.1 ad /*
421 1.1 ad * relookup for RENAME namei operation.
422 1.1 ad *
423 1.1 ad * dvp is unionfs vnode. dvp should be locked.
424 1.1 ad */
425 1.1 ad int
426 1.1 ad unionfs_relookup_for_rename(struct vnode *dvp, struct componentname *cnp)
427 1.1 ad {
428 1.1 ad int error;
429 1.1 ad struct vnode *udvp;
430 1.1 ad struct vnode *vp;
431 1.1 ad struct componentname cn;
432 1.10 dholland char *pnbuf;
433 1.1 ad
434 1.1 ad udvp = UNIONFSVPTOUPPERVP(dvp);
435 1.1 ad vp = NULLVP;
436 1.1 ad
437 1.10 dholland error = unionfs_relookup(udvp, &vp, cnp, &cn, &pnbuf, cnp->cn_nameptr,
438 1.1 ad strlen(cnp->cn_nameptr), RENAME);
439 1.1 ad if (error)
440 1.1 ad return (error);
441 1.1 ad
442 1.1 ad if (vp != NULLVP) {
443 1.1 ad if (udvp == vp)
444 1.1 ad vrele(vp);
445 1.1 ad else
446 1.1 ad vput(vp);
447 1.1 ad }
448 1.1 ad
449 1.10 dholland PNBUF_PUT(pnbuf);
450 1.1 ad
451 1.1 ad if (!error) {
452 1.1 ad cnp->cn_flags = cn.cn_flags;
453 1.1 ad }
454 1.1 ad
455 1.1 ad return (error);
456 1.1 ad
457 1.1 ad }
458 1.1 ad
459 1.1 ad /*
460 1.1 ad * Update the unionfs_node.
461 1.1 ad *
462 1.1 ad * uvp is new locked upper vnode. unionfs vnode's lock will be exchanged to the
463 1.1 ad * uvp's lock and lower's lock will be unlocked.
464 1.1 ad */
465 1.1 ad static void
466 1.1 ad unionfs_node_update(struct unionfs_node *unp, struct vnode *uvp)
467 1.1 ad {
468 1.1 ad struct vnode *vp;
469 1.1 ad struct vnode *lvp;
470 1.1 ad
471 1.1 ad vp = UNIONFSTOV(unp);
472 1.1 ad lvp = unp->un_lowervp;
473 1.1 ad
474 1.1 ad /*
475 1.1 ad * lock update
476 1.1 ad */
477 1.13 rmind mutex_enter(vp->v_interlock);
478 1.1 ad unp->un_uppervp = uvp;
479 1.8 hannken KASSERT(VOP_ISLOCKED(lvp) == LK_EXCLUSIVE);
480 1.13 rmind mutex_exit(vp->v_interlock);
481 1.1 ad }
482 1.1 ad
483 1.1 ad /*
484 1.1 ad * Create a new shadow dir.
485 1.1 ad *
486 1.1 ad * udvp should be locked on entry and will be locked on return.
487 1.1 ad *
488 1.1 ad * If no error returned, unp will be updated.
489 1.1 ad */
490 1.1 ad int
491 1.1 ad unionfs_mkshadowdir(struct unionfs_mount *ump, struct vnode *udvp,
492 1.1 ad struct unionfs_node *unp, struct componentname *cnp)
493 1.1 ad {
494 1.1 ad int error;
495 1.1 ad struct vnode *lvp;
496 1.1 ad struct vnode *uvp;
497 1.1 ad struct vattr va;
498 1.1 ad struct vattr lva;
499 1.1 ad struct componentname cn;
500 1.10 dholland char *pnbuf;
501 1.1 ad
502 1.1 ad if (unp->un_uppervp != NULLVP)
503 1.1 ad return (EEXIST);
504 1.1 ad
505 1.1 ad lvp = unp->un_lowervp;
506 1.1 ad uvp = NULLVP;
507 1.1 ad
508 1.1 ad memset(&cn, 0, sizeof(cn));
509 1.1 ad
510 1.1 ad if ((error = VOP_GETATTR(lvp, &lva, cnp->cn_cred)))
511 1.1 ad goto unionfs_mkshadowdir_abort;
512 1.1 ad
513 1.10 dholland if ((error = unionfs_relookup(udvp, &uvp, cnp, &cn, &pnbuf,
514 1.10 dholland cnp->cn_nameptr, cnp->cn_namelen, CREATE)))
515 1.1 ad goto unionfs_mkshadowdir_abort;
516 1.1 ad if (uvp != NULLVP) {
517 1.1 ad if (udvp == uvp)
518 1.1 ad vrele(uvp);
519 1.1 ad else
520 1.1 ad vput(uvp);
521 1.1 ad
522 1.1 ad error = EEXIST;
523 1.1 ad goto unionfs_mkshadowdir_free_out;
524 1.1 ad }
525 1.1 ad
526 1.1 ad unionfs_create_uppervattr_core(ump, &lva, &va);
527 1.1 ad
528 1.1 ad error = VOP_MKDIR(udvp, &uvp, &cn, &va);
529 1.1 ad
530 1.1 ad if (!error) {
531 1.1 ad unionfs_node_update(unp, uvp);
532 1.1 ad
533 1.1 ad /*
534 1.1 ad * XXX The bug which cannot set uid/gid was corrected.
535 1.1 ad * Ignore errors. XXXNETBSD Why is this done as root?
536 1.1 ad */
537 1.1 ad va.va_type = VNON;
538 1.1 ad VOP_SETATTR(uvp, &va, lwp0.l_cred);
539 1.1 ad }
540 1.1 ad
541 1.1 ad unionfs_mkshadowdir_free_out:
542 1.10 dholland PNBUF_PUT(pnbuf);
543 1.1 ad
544 1.1 ad unionfs_mkshadowdir_abort:
545 1.1 ad
546 1.1 ad return (error);
547 1.1 ad }
548 1.1 ad
549 1.1 ad /*
550 1.1 ad * Create a new whiteout.
551 1.1 ad *
552 1.1 ad * dvp should be locked on entry and will be locked on return.
553 1.1 ad */
554 1.1 ad int
555 1.1 ad unionfs_mkwhiteout(struct vnode *dvp, struct componentname *cnp, const char *path)
556 1.1 ad {
557 1.1 ad int error;
558 1.1 ad struct vnode *wvp;
559 1.1 ad struct componentname cn;
560 1.10 dholland char *pnbuf;
561 1.1 ad
562 1.1 ad if (path == NULL)
563 1.1 ad path = cnp->cn_nameptr;
564 1.1 ad
565 1.1 ad wvp = NULLVP;
566 1.10 dholland if ((error = unionfs_relookup(dvp, &wvp, cnp, &cn, &pnbuf,
567 1.10 dholland path, strlen(path), CREATE)))
568 1.1 ad return (error);
569 1.1 ad if (wvp != NULLVP) {
570 1.10 dholland PNBUF_PUT(pnbuf);
571 1.1 ad if (dvp == wvp)
572 1.1 ad vrele(wvp);
573 1.1 ad else
574 1.1 ad vput(wvp);
575 1.1 ad
576 1.1 ad return (EEXIST);
577 1.1 ad }
578 1.1 ad
579 1.10 dholland PNBUF_PUT(pnbuf);
580 1.1 ad
581 1.1 ad return (error);
582 1.1 ad }
583 1.1 ad
584 1.1 ad /*
585 1.1 ad * Create a new vnode for create a new shadow file.
586 1.1 ad *
587 1.1 ad * If an error is returned, *vpp will be invalid, otherwise it will hold a
588 1.1 ad * locked, referenced and opened vnode.
589 1.1 ad *
590 1.1 ad * unp is never updated.
591 1.1 ad */
592 1.1 ad static int
593 1.1 ad unionfs_vn_create_on_upper(struct vnode **vpp, struct vnode *udvp,
594 1.1 ad struct unionfs_node *unp, struct vattr *uvap)
595 1.1 ad {
596 1.1 ad struct unionfs_mount *ump;
597 1.1 ad struct vnode *vp;
598 1.1 ad struct vnode *lvp;
599 1.1 ad kauth_cred_t cred;
600 1.1 ad struct vattr lva;
601 1.1 ad int fmode;
602 1.1 ad int error;
603 1.1 ad struct componentname cn;
604 1.10 dholland char *pnbuf;
605 1.1 ad
606 1.1 ad ump = MOUNTTOUNIONFSMOUNT(UNIONFSTOV(unp)->v_mount);
607 1.1 ad vp = NULLVP;
608 1.1 ad lvp = unp->un_lowervp;
609 1.1 ad cred = kauth_cred_get();
610 1.1 ad fmode = FFLAGS(O_WRONLY | O_CREAT | O_TRUNC | O_EXCL);
611 1.1 ad error = 0;
612 1.1 ad
613 1.1 ad if ((error = VOP_GETATTR(lvp, &lva, cred)) != 0)
614 1.1 ad return (error);
615 1.1 ad unionfs_create_uppervattr_core(ump, &lva, uvap);
616 1.1 ad
617 1.1 ad if (unp->un_path == NULL)
618 1.1 ad panic("unionfs: un_path is null");
619 1.1 ad
620 1.1 ad cn.cn_namelen = strlen(unp->un_path);
621 1.10 dholland pnbuf = PNBUF_GET();
622 1.10 dholland memcpy(pnbuf, unp->un_path, cn.cn_namelen + 1);
623 1.1 ad cn.cn_nameiop = CREATE;
624 1.11 dholland cn.cn_flags = (LOCKPARENT | LOCKLEAF | ISLASTCN);
625 1.1 ad cn.cn_cred = cred;
626 1.10 dholland cn.cn_nameptr = pnbuf;
627 1.1 ad
628 1.1 ad vref(udvp);
629 1.12 dholland if ((error = relookup(udvp, &vp, &cn, 0)) != 0)
630 1.1 ad goto unionfs_vn_create_on_upper_free_out2;
631 1.1 ad vrele(udvp);
632 1.1 ad
633 1.1 ad if (vp != NULLVP) {
634 1.1 ad if (vp == udvp)
635 1.1 ad vrele(vp);
636 1.1 ad else
637 1.1 ad vput(vp);
638 1.1 ad error = EEXIST;
639 1.1 ad goto unionfs_vn_create_on_upper_free_out1;
640 1.1 ad }
641 1.1 ad
642 1.1 ad if ((error = VOP_CREATE(udvp, &vp, &cn, uvap)) != 0)
643 1.1 ad goto unionfs_vn_create_on_upper_free_out1;
644 1.1 ad
645 1.1 ad if ((error = VOP_OPEN(vp, fmode, cred)) != 0) {
646 1.1 ad vput(vp);
647 1.1 ad goto unionfs_vn_create_on_upper_free_out1;
648 1.1 ad }
649 1.1 ad vp->v_writecount++;
650 1.1 ad *vpp = vp;
651 1.1 ad
652 1.1 ad unionfs_vn_create_on_upper_free_out1:
653 1.9 hannken VOP_UNLOCK(udvp);
654 1.1 ad
655 1.1 ad unionfs_vn_create_on_upper_free_out2:
656 1.10 dholland PNBUF_PUT(pnbuf);
657 1.1 ad
658 1.1 ad return (error);
659 1.1 ad }
660 1.1 ad
661 1.1 ad /*
662 1.1 ad * Copy from lvp to uvp.
663 1.1 ad *
664 1.1 ad * lvp and uvp should be locked and opened on entry and will be locked and
665 1.1 ad * opened on return.
666 1.1 ad */
667 1.1 ad static int
668 1.1 ad unionfs_copyfile_core(struct vnode *lvp, struct vnode *uvp,
669 1.1 ad kauth_cred_t cred)
670 1.1 ad {
671 1.1 ad int error;
672 1.1 ad off_t offset;
673 1.1 ad int count;
674 1.1 ad int bufoffset;
675 1.1 ad char *buf;
676 1.1 ad struct uio uio;
677 1.1 ad struct iovec iov;
678 1.1 ad
679 1.1 ad error = 0;
680 1.1 ad memset(&uio, 0, sizeof(uio));
681 1.1 ad UIO_SETUP_SYSSPACE(&uio);
682 1.1 ad uio.uio_offset = 0;
683 1.1 ad
684 1.1 ad buf = kmem_alloc(MAXBSIZE, KM_SLEEP);
685 1.1 ad while (error == 0) {
686 1.1 ad offset = uio.uio_offset;
687 1.1 ad
688 1.1 ad uio.uio_iov = &iov;
689 1.1 ad uio.uio_iovcnt = 1;
690 1.1 ad iov.iov_base = buf;
691 1.1 ad iov.iov_len = MAXBSIZE;
692 1.1 ad uio.uio_resid = iov.iov_len;
693 1.1 ad uio.uio_rw = UIO_READ;
694 1.1 ad
695 1.1 ad if ((error = VOP_READ(lvp, &uio, 0, cred)) != 0)
696 1.1 ad break;
697 1.1 ad if ((count = MAXBSIZE - uio.uio_resid) == 0)
698 1.1 ad break;
699 1.1 ad
700 1.1 ad bufoffset = 0;
701 1.1 ad while (bufoffset < count) {
702 1.1 ad uio.uio_iov = &iov;
703 1.1 ad uio.uio_iovcnt = 1;
704 1.1 ad iov.iov_base = buf + bufoffset;
705 1.1 ad iov.iov_len = count - bufoffset;
706 1.1 ad uio.uio_offset = offset + bufoffset;
707 1.1 ad uio.uio_resid = iov.iov_len;
708 1.1 ad uio.uio_rw = UIO_WRITE;
709 1.1 ad
710 1.1 ad if ((error = VOP_WRITE(uvp, &uio, 0, cred)) != 0)
711 1.1 ad break;
712 1.1 ad
713 1.1 ad bufoffset += (count - bufoffset) - uio.uio_resid;
714 1.1 ad }
715 1.1 ad
716 1.1 ad uio.uio_offset = offset + bufoffset;
717 1.1 ad }
718 1.1 ad
719 1.1 ad kmem_free(buf, MAXBSIZE);
720 1.1 ad
721 1.1 ad return (error);
722 1.1 ad }
723 1.1 ad
724 1.1 ad /*
725 1.1 ad * Copy file from lower to upper.
726 1.1 ad *
727 1.1 ad * If you need copy of the contents, set 1 to docopy. Otherwise, set 0 to
728 1.1 ad * docopy.
729 1.1 ad *
730 1.1 ad * If no error returned, unp will be updated.
731 1.1 ad */
732 1.1 ad int
733 1.1 ad unionfs_copyfile(struct unionfs_node *unp, int docopy, kauth_cred_t cred)
734 1.1 ad {
735 1.1 ad int error;
736 1.1 ad struct vnode *udvp;
737 1.1 ad struct vnode *lvp;
738 1.1 ad struct vnode *uvp;
739 1.1 ad struct vattr uva;
740 1.1 ad
741 1.1 ad lvp = unp->un_lowervp;
742 1.1 ad uvp = NULLVP;
743 1.1 ad
744 1.1 ad if ((UNIONFSTOV(unp)->v_mount->mnt_flag & MNT_RDONLY))
745 1.1 ad return (EROFS);
746 1.1 ad if (unp->un_dvp == NULLVP)
747 1.1 ad return (EINVAL);
748 1.1 ad if (unp->un_uppervp != NULLVP)
749 1.1 ad return (EEXIST);
750 1.1 ad udvp = VTOUNIONFS(unp->un_dvp)->un_uppervp;
751 1.1 ad if (udvp == NULLVP)
752 1.1 ad return (EROFS);
753 1.1 ad if ((udvp->v_mount->mnt_flag & MNT_RDONLY))
754 1.1 ad return (EROFS);
755 1.1 ad
756 1.1 ad error = VOP_ACCESS(lvp, VREAD, cred);
757 1.1 ad if (error != 0)
758 1.1 ad return (error);
759 1.1 ad
760 1.1 ad error = unionfs_vn_create_on_upper(&uvp, udvp, unp, &uva);
761 1.1 ad if (error != 0)
762 1.1 ad return (error);
763 1.1 ad
764 1.1 ad if (docopy != 0) {
765 1.1 ad error = VOP_OPEN(lvp, FREAD, cred);
766 1.1 ad if (error == 0) {
767 1.1 ad error = unionfs_copyfile_core(lvp, uvp, cred);
768 1.1 ad VOP_CLOSE(lvp, FREAD, cred);
769 1.1 ad }
770 1.1 ad }
771 1.1 ad VOP_CLOSE(uvp, FWRITE, cred);
772 1.1 ad uvp->v_writecount--;
773 1.1 ad
774 1.1 ad if (error == 0) {
775 1.1 ad /* Reset the attributes. Ignore errors. */
776 1.1 ad uva.va_type = VNON;
777 1.1 ad VOP_SETATTR(uvp, &uva, cred);
778 1.1 ad }
779 1.1 ad
780 1.1 ad unionfs_node_update(unp, uvp);
781 1.1 ad
782 1.1 ad return (error);
783 1.1 ad }
784 1.1 ad
785 1.1 ad /*
786 1.1 ad * It checks whether vp can rmdir. (check empty)
787 1.1 ad *
788 1.1 ad * vp is unionfs vnode.
789 1.1 ad * vp should be locked.
790 1.1 ad */
791 1.1 ad int
792 1.1 ad unionfs_check_rmdir(struct vnode *vp, kauth_cred_t cred)
793 1.1 ad {
794 1.1 ad int error;
795 1.1 ad int eofflag;
796 1.1 ad int lookuperr;
797 1.1 ad struct vnode *uvp;
798 1.1 ad struct vnode *lvp;
799 1.1 ad struct vnode *tvp;
800 1.1 ad struct vattr va;
801 1.1 ad struct componentname cn;
802 1.1 ad /*
803 1.1 ad * The size of buf needs to be larger than DIRBLKSIZ.
804 1.1 ad */
805 1.1 ad char buf[256 * 6];
806 1.1 ad struct dirent *dp;
807 1.1 ad struct dirent *edp;
808 1.1 ad struct uio uio;
809 1.1 ad struct iovec iov;
810 1.1 ad
811 1.1 ad KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
812 1.1 ad
813 1.1 ad eofflag = 0;
814 1.1 ad uvp = UNIONFSVPTOUPPERVP(vp);
815 1.1 ad lvp = UNIONFSVPTOLOWERVP(vp);
816 1.1 ad
817 1.1 ad /* check opaque */
818 1.1 ad if ((error = VOP_GETATTR(uvp, &va, cred)) != 0)
819 1.1 ad return (error);
820 1.1 ad if (va.va_flags & OPAQUE)
821 1.1 ad return (0);
822 1.1 ad
823 1.1 ad /* open vnode */
824 1.1 ad if ((error = VOP_ACCESS(vp, VEXEC|VREAD, cred)) != 0)
825 1.1 ad return (error);
826 1.1 ad if ((error = VOP_OPEN(vp, FREAD, cred)) != 0)
827 1.1 ad return (error);
828 1.1 ad
829 1.1 ad UIO_SETUP_SYSSPACE(&uio);
830 1.1 ad uio.uio_rw = UIO_READ;
831 1.1 ad uio.uio_offset = 0;
832 1.1 ad
833 1.1 ad while (!error && !eofflag) {
834 1.1 ad iov.iov_base = buf;
835 1.1 ad iov.iov_len = sizeof(buf);
836 1.1 ad uio.uio_iov = &iov;
837 1.1 ad uio.uio_iovcnt = 1;
838 1.1 ad uio.uio_resid = iov.iov_len;
839 1.1 ad
840 1.1 ad error = VOP_READDIR(lvp, &uio, cred, &eofflag, NULL, NULL);
841 1.1 ad if (error)
842 1.1 ad break;
843 1.1 ad
844 1.1 ad edp = (struct dirent*)&buf[sizeof(buf) - uio.uio_resid];
845 1.1 ad for (dp = (struct dirent*)buf; !error && dp < edp;
846 1.1 ad dp = (struct dirent*)((char *)dp + dp->d_reclen)) {
847 1.1 ad if (dp->d_type == DT_WHT ||
848 1.1 ad (dp->d_namlen == 1 && dp->d_name[0] == '.') ||
849 1.2 cegger (dp->d_namlen == 2 && !memcmp(dp->d_name, "..", 2)))
850 1.1 ad continue;
851 1.1 ad
852 1.1 ad cn.cn_namelen = dp->d_namlen;
853 1.1 ad cn.cn_nameptr = dp->d_name;
854 1.1 ad cn.cn_nameiop = LOOKUP;
855 1.11 dholland cn.cn_flags = (LOCKPARENT | LOCKLEAF | RDONLY | ISLASTCN);
856 1.1 ad cn.cn_cred = cred;
857 1.1 ad
858 1.1 ad /*
859 1.1 ad * check entry in lower.
860 1.1 ad * Sometimes, readdir function returns
861 1.1 ad * wrong entry.
862 1.1 ad */
863 1.1 ad lookuperr = VOP_LOOKUP(lvp, &tvp, &cn);
864 1.1 ad
865 1.1 ad if (!lookuperr)
866 1.1 ad vput(tvp);
867 1.1 ad else
868 1.1 ad continue; /* skip entry */
869 1.1 ad
870 1.1 ad /*
871 1.1 ad * check entry
872 1.1 ad * If it has no exist/whiteout entry in upper,
873 1.1 ad * directory is not empty.
874 1.1 ad */
875 1.11 dholland cn.cn_flags = (LOCKPARENT | LOCKLEAF | RDONLY | ISLASTCN);
876 1.1 ad lookuperr = VOP_LOOKUP(uvp, &tvp, &cn);
877 1.1 ad
878 1.1 ad if (!lookuperr)
879 1.1 ad vput(tvp);
880 1.1 ad
881 1.1 ad /* ignore exist or whiteout entry */
882 1.1 ad if (!lookuperr ||
883 1.1 ad (lookuperr == ENOENT && (cn.cn_flags & ISWHITEOUT)))
884 1.1 ad continue;
885 1.1 ad
886 1.1 ad error = ENOTEMPTY;
887 1.1 ad }
888 1.1 ad }
889 1.1 ad
890 1.1 ad /* close vnode */
891 1.1 ad VOP_CLOSE(vp, FREAD, cred);
892 1.1 ad
893 1.1 ad return (error);
894 1.1 ad }
895 1.1 ad
896 1.1 ad #ifdef DIAGNOSTIC
897 1.1 ad
898 1.1 ad struct vnode *
899 1.1 ad unionfs_checkuppervp(struct vnode *vp, const char *fil, int lno)
900 1.1 ad {
901 1.1 ad struct unionfs_node *unp;
902 1.1 ad
903 1.1 ad unp = VTOUNIONFS(vp);
904 1.1 ad
905 1.1 ad #ifdef notyet
906 1.1 ad if (vp->v_op != unionfs_vnodeop_p) {
907 1.1 ad printf("unionfs_checkuppervp: on non-unionfs-node.\n");
908 1.1 ad #ifdef KDB
909 1.1 ad kdb_enter(KDB_WHY_UNIONFS,
910 1.1 ad "unionfs_checkuppervp: on non-unionfs-node.\n");
911 1.1 ad #endif
912 1.1 ad panic("unionfs_checkuppervp");
913 1.1 ad };
914 1.1 ad #endif
915 1.1 ad return (unp->un_uppervp);
916 1.1 ad }
917 1.1 ad
918 1.1 ad struct vnode *
919 1.1 ad unionfs_checklowervp(struct vnode *vp, const char *fil, int lno)
920 1.1 ad {
921 1.1 ad struct unionfs_node *unp;
922 1.1 ad
923 1.1 ad unp = VTOUNIONFS(vp);
924 1.1 ad
925 1.1 ad #ifdef notyet
926 1.1 ad if (vp->v_op != unionfs_vnodeop_p) {
927 1.1 ad printf("unionfs_checklowervp: on non-unionfs-node.\n");
928 1.1 ad #ifdef KDB
929 1.1 ad kdb_enter(KDB_WHY_UNIONFS,
930 1.1 ad "unionfs_checklowervp: on non-unionfs-node.\n");
931 1.1 ad #endif
932 1.1 ad panic("unionfs_checklowervp");
933 1.1 ad };
934 1.1 ad #endif
935 1.1 ad return (unp->un_lowervp);
936 1.1 ad }
937 1.1 ad #endif
938