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