unionfs_subr.c revision 1.12 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.12 dholland cn->cn_flags |= (cnp->cn_flags & DOWHITEOUT);
321 1.1 ad
322 1.1 ad vref(dvp);
323 1.9 hannken VOP_UNLOCK(dvp);
324 1.1 ad
325 1.12 dholland if ((error = relookup(dvp, vpp, cn, 0))) {
326 1.10 dholland PNBUF_PUT(pnbuf);
327 1.10 dholland *pnbuf_ret = NULL;
328 1.1 ad vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
329 1.10 dholland } else {
330 1.10 dholland *pnbuf_ret = pnbuf;
331 1.1 ad vrele(dvp);
332 1.10 dholland }
333 1.1 ad
334 1.1 ad return (error);
335 1.1 ad }
336 1.1 ad
337 1.1 ad /*
338 1.1 ad * relookup for CREATE namei operation.
339 1.1 ad *
340 1.1 ad * dvp is unionfs vnode. dvp should be locked.
341 1.1 ad *
342 1.1 ad * If it called 'unionfs_copyfile' function by unionfs_link etc,
343 1.1 ad * VOP_LOOKUP information is broken.
344 1.1 ad * So it need relookup in order to create link etc.
345 1.1 ad */
346 1.1 ad int
347 1.1 ad unionfs_relookup_for_create(struct vnode *dvp, struct componentname *cnp)
348 1.1 ad {
349 1.1 ad int error;
350 1.1 ad struct vnode *udvp;
351 1.1 ad struct vnode *vp;
352 1.1 ad struct componentname cn;
353 1.10 dholland char *pnbuf;
354 1.1 ad
355 1.1 ad udvp = UNIONFSVPTOUPPERVP(dvp);
356 1.1 ad vp = NULLVP;
357 1.1 ad
358 1.10 dholland error = unionfs_relookup(udvp, &vp, cnp, &cn, &pnbuf,
359 1.10 dholland cnp->cn_nameptr,
360 1.1 ad strlen(cnp->cn_nameptr), CREATE);
361 1.1 ad if (error)
362 1.1 ad return (error);
363 1.1 ad
364 1.1 ad if (vp != NULLVP) {
365 1.1 ad if (udvp == vp)
366 1.1 ad vrele(vp);
367 1.1 ad else
368 1.1 ad vput(vp);
369 1.1 ad
370 1.1 ad error = EEXIST;
371 1.1 ad }
372 1.1 ad
373 1.10 dholland PNBUF_PUT(pnbuf);
374 1.1 ad
375 1.1 ad if (!error) {
376 1.1 ad cnp->cn_flags = cn.cn_flags;
377 1.1 ad }
378 1.1 ad
379 1.1 ad return (error);
380 1.1 ad }
381 1.1 ad
382 1.1 ad /*
383 1.1 ad * relookup for DELETE namei operation.
384 1.1 ad *
385 1.1 ad * dvp is unionfs vnode. dvp should be locked.
386 1.1 ad */
387 1.1 ad int
388 1.1 ad unionfs_relookup_for_delete(struct vnode *dvp, struct componentname *cnp)
389 1.1 ad {
390 1.1 ad int error;
391 1.1 ad struct vnode *udvp;
392 1.1 ad struct vnode *vp;
393 1.1 ad struct componentname cn;
394 1.10 dholland char *pnbuf;
395 1.1 ad
396 1.1 ad udvp = UNIONFSVPTOUPPERVP(dvp);
397 1.1 ad vp = NULLVP;
398 1.1 ad
399 1.10 dholland error = unionfs_relookup(udvp, &vp, cnp, &cn, &pnbuf, cnp->cn_nameptr,
400 1.1 ad strlen(cnp->cn_nameptr), DELETE);
401 1.1 ad if (error)
402 1.1 ad return (error);
403 1.1 ad
404 1.1 ad if (vp == NULLVP)
405 1.1 ad error = ENOENT;
406 1.1 ad else {
407 1.1 ad if (udvp == vp)
408 1.1 ad vrele(vp);
409 1.1 ad else
410 1.1 ad vput(vp);
411 1.1 ad }
412 1.1 ad
413 1.10 dholland PNBUF_PUT(pnbuf);
414 1.1 ad
415 1.1 ad if (!error) {
416 1.1 ad cnp->cn_flags = cn.cn_flags;
417 1.1 ad }
418 1.1 ad
419 1.1 ad return (error);
420 1.1 ad }
421 1.1 ad
422 1.1 ad /*
423 1.1 ad * relookup for RENAME namei operation.
424 1.1 ad *
425 1.1 ad * dvp is unionfs vnode. dvp should be locked.
426 1.1 ad */
427 1.1 ad int
428 1.1 ad unionfs_relookup_for_rename(struct vnode *dvp, struct componentname *cnp)
429 1.1 ad {
430 1.1 ad int error;
431 1.1 ad struct vnode *udvp;
432 1.1 ad struct vnode *vp;
433 1.1 ad struct componentname cn;
434 1.10 dholland char *pnbuf;
435 1.1 ad
436 1.1 ad udvp = UNIONFSVPTOUPPERVP(dvp);
437 1.1 ad vp = NULLVP;
438 1.1 ad
439 1.10 dholland error = unionfs_relookup(udvp, &vp, cnp, &cn, &pnbuf, cnp->cn_nameptr,
440 1.1 ad strlen(cnp->cn_nameptr), RENAME);
441 1.1 ad if (error)
442 1.1 ad return (error);
443 1.1 ad
444 1.1 ad if (vp != NULLVP) {
445 1.1 ad if (udvp == vp)
446 1.1 ad vrele(vp);
447 1.1 ad else
448 1.1 ad vput(vp);
449 1.1 ad }
450 1.1 ad
451 1.10 dholland PNBUF_PUT(pnbuf);
452 1.1 ad
453 1.1 ad if (!error) {
454 1.1 ad cnp->cn_flags = cn.cn_flags;
455 1.1 ad }
456 1.1 ad
457 1.1 ad return (error);
458 1.1 ad
459 1.1 ad }
460 1.1 ad
461 1.1 ad /*
462 1.1 ad * Update the unionfs_node.
463 1.1 ad *
464 1.1 ad * uvp is new locked upper vnode. unionfs vnode's lock will be exchanged to the
465 1.1 ad * uvp's lock and lower's lock will be unlocked.
466 1.1 ad */
467 1.1 ad static void
468 1.1 ad unionfs_node_update(struct unionfs_node *unp, struct vnode *uvp)
469 1.1 ad {
470 1.1 ad struct vnode *vp;
471 1.1 ad struct vnode *lvp;
472 1.1 ad
473 1.1 ad vp = UNIONFSTOV(unp);
474 1.1 ad lvp = unp->un_lowervp;
475 1.1 ad
476 1.1 ad /*
477 1.1 ad * lock update
478 1.1 ad */
479 1.1 ad mutex_enter(&vp->v_interlock);
480 1.1 ad unp->un_uppervp = uvp;
481 1.8 hannken KASSERT(VOP_ISLOCKED(lvp) == LK_EXCLUSIVE);
482 1.1 ad mutex_exit(&vp->v_interlock);
483 1.1 ad }
484 1.1 ad
485 1.1 ad /*
486 1.1 ad * Create a new shadow dir.
487 1.1 ad *
488 1.1 ad * udvp should be locked on entry and will be locked on return.
489 1.1 ad *
490 1.1 ad * If no error returned, unp will be updated.
491 1.1 ad */
492 1.1 ad int
493 1.1 ad unionfs_mkshadowdir(struct unionfs_mount *ump, struct vnode *udvp,
494 1.1 ad struct unionfs_node *unp, struct componentname *cnp)
495 1.1 ad {
496 1.1 ad int error;
497 1.1 ad struct vnode *lvp;
498 1.1 ad struct vnode *uvp;
499 1.1 ad struct vattr va;
500 1.1 ad struct vattr lva;
501 1.1 ad struct componentname cn;
502 1.10 dholland char *pnbuf;
503 1.1 ad
504 1.1 ad if (unp->un_uppervp != NULLVP)
505 1.1 ad return (EEXIST);
506 1.1 ad
507 1.1 ad lvp = unp->un_lowervp;
508 1.1 ad uvp = NULLVP;
509 1.1 ad
510 1.1 ad memset(&cn, 0, sizeof(cn));
511 1.1 ad
512 1.1 ad if ((error = VOP_GETATTR(lvp, &lva, cnp->cn_cred)))
513 1.1 ad goto unionfs_mkshadowdir_abort;
514 1.1 ad
515 1.10 dholland if ((error = unionfs_relookup(udvp, &uvp, cnp, &cn, &pnbuf,
516 1.10 dholland cnp->cn_nameptr, cnp->cn_namelen, CREATE)))
517 1.1 ad goto unionfs_mkshadowdir_abort;
518 1.1 ad if (uvp != NULLVP) {
519 1.1 ad if (udvp == uvp)
520 1.1 ad vrele(uvp);
521 1.1 ad else
522 1.1 ad vput(uvp);
523 1.1 ad
524 1.1 ad error = EEXIST;
525 1.1 ad goto unionfs_mkshadowdir_free_out;
526 1.1 ad }
527 1.1 ad
528 1.1 ad unionfs_create_uppervattr_core(ump, &lva, &va);
529 1.1 ad
530 1.1 ad error = VOP_MKDIR(udvp, &uvp, &cn, &va);
531 1.1 ad
532 1.1 ad if (!error) {
533 1.1 ad unionfs_node_update(unp, uvp);
534 1.1 ad
535 1.1 ad /*
536 1.1 ad * XXX The bug which cannot set uid/gid was corrected.
537 1.1 ad * Ignore errors. XXXNETBSD Why is this done as root?
538 1.1 ad */
539 1.1 ad va.va_type = VNON;
540 1.1 ad VOP_SETATTR(uvp, &va, lwp0.l_cred);
541 1.1 ad }
542 1.1 ad
543 1.1 ad unionfs_mkshadowdir_free_out:
544 1.10 dholland PNBUF_PUT(pnbuf);
545 1.1 ad
546 1.1 ad unionfs_mkshadowdir_abort:
547 1.1 ad
548 1.1 ad return (error);
549 1.1 ad }
550 1.1 ad
551 1.1 ad /*
552 1.1 ad * Create a new whiteout.
553 1.1 ad *
554 1.1 ad * dvp should be locked on entry and will be locked on return.
555 1.1 ad */
556 1.1 ad int
557 1.1 ad unionfs_mkwhiteout(struct vnode *dvp, struct componentname *cnp, const char *path)
558 1.1 ad {
559 1.1 ad int error;
560 1.1 ad struct vnode *wvp;
561 1.1 ad struct componentname cn;
562 1.10 dholland char *pnbuf;
563 1.1 ad
564 1.1 ad if (path == NULL)
565 1.1 ad path = cnp->cn_nameptr;
566 1.1 ad
567 1.1 ad wvp = NULLVP;
568 1.10 dholland if ((error = unionfs_relookup(dvp, &wvp, cnp, &cn, &pnbuf,
569 1.10 dholland path, strlen(path), CREATE)))
570 1.1 ad return (error);
571 1.1 ad if (wvp != NULLVP) {
572 1.10 dholland PNBUF_PUT(pnbuf);
573 1.1 ad if (dvp == wvp)
574 1.1 ad vrele(wvp);
575 1.1 ad else
576 1.1 ad vput(wvp);
577 1.1 ad
578 1.1 ad return (EEXIST);
579 1.1 ad }
580 1.1 ad
581 1.10 dholland PNBUF_PUT(pnbuf);
582 1.1 ad
583 1.1 ad return (error);
584 1.1 ad }
585 1.1 ad
586 1.1 ad /*
587 1.1 ad * Create a new vnode for create a new shadow file.
588 1.1 ad *
589 1.1 ad * If an error is returned, *vpp will be invalid, otherwise it will hold a
590 1.1 ad * locked, referenced and opened vnode.
591 1.1 ad *
592 1.1 ad * unp is never updated.
593 1.1 ad */
594 1.1 ad static int
595 1.1 ad unionfs_vn_create_on_upper(struct vnode **vpp, struct vnode *udvp,
596 1.1 ad struct unionfs_node *unp, struct vattr *uvap)
597 1.1 ad {
598 1.1 ad struct unionfs_mount *ump;
599 1.1 ad struct vnode *vp;
600 1.1 ad struct vnode *lvp;
601 1.1 ad kauth_cred_t cred;
602 1.1 ad struct vattr lva;
603 1.1 ad int fmode;
604 1.1 ad int error;
605 1.1 ad struct componentname cn;
606 1.10 dholland char *pnbuf;
607 1.1 ad
608 1.1 ad ump = MOUNTTOUNIONFSMOUNT(UNIONFSTOV(unp)->v_mount);
609 1.1 ad vp = NULLVP;
610 1.1 ad lvp = unp->un_lowervp;
611 1.1 ad cred = kauth_cred_get();
612 1.1 ad fmode = FFLAGS(O_WRONLY | O_CREAT | O_TRUNC | O_EXCL);
613 1.1 ad error = 0;
614 1.1 ad
615 1.1 ad if ((error = VOP_GETATTR(lvp, &lva, cred)) != 0)
616 1.1 ad return (error);
617 1.1 ad unionfs_create_uppervattr_core(ump, &lva, uvap);
618 1.1 ad
619 1.1 ad if (unp->un_path == NULL)
620 1.1 ad panic("unionfs: un_path is null");
621 1.1 ad
622 1.1 ad cn.cn_namelen = strlen(unp->un_path);
623 1.10 dholland pnbuf = PNBUF_GET();
624 1.10 dholland memcpy(pnbuf, unp->un_path, cn.cn_namelen + 1);
625 1.1 ad cn.cn_nameiop = CREATE;
626 1.11 dholland cn.cn_flags = (LOCKPARENT | LOCKLEAF | ISLASTCN);
627 1.1 ad cn.cn_cred = cred;
628 1.10 dholland cn.cn_nameptr = pnbuf;
629 1.1 ad cn.cn_consume = 0;
630 1.1 ad
631 1.1 ad vref(udvp);
632 1.12 dholland if ((error = relookup(udvp, &vp, &cn, 0)) != 0)
633 1.1 ad goto unionfs_vn_create_on_upper_free_out2;
634 1.1 ad vrele(udvp);
635 1.1 ad
636 1.1 ad if (vp != NULLVP) {
637 1.1 ad if (vp == udvp)
638 1.1 ad vrele(vp);
639 1.1 ad else
640 1.1 ad vput(vp);
641 1.1 ad error = EEXIST;
642 1.1 ad goto unionfs_vn_create_on_upper_free_out1;
643 1.1 ad }
644 1.1 ad
645 1.1 ad if ((error = VOP_CREATE(udvp, &vp, &cn, uvap)) != 0)
646 1.1 ad goto unionfs_vn_create_on_upper_free_out1;
647 1.1 ad
648 1.1 ad if ((error = VOP_OPEN(vp, fmode, cred)) != 0) {
649 1.1 ad vput(vp);
650 1.1 ad goto unionfs_vn_create_on_upper_free_out1;
651 1.1 ad }
652 1.1 ad vp->v_writecount++;
653 1.1 ad *vpp = vp;
654 1.1 ad
655 1.1 ad unionfs_vn_create_on_upper_free_out1:
656 1.9 hannken VOP_UNLOCK(udvp);
657 1.1 ad
658 1.1 ad unionfs_vn_create_on_upper_free_out2:
659 1.10 dholland PNBUF_PUT(pnbuf);
660 1.1 ad
661 1.1 ad return (error);
662 1.1 ad }
663 1.1 ad
664 1.1 ad /*
665 1.1 ad * Copy from lvp to uvp.
666 1.1 ad *
667 1.1 ad * lvp and uvp should be locked and opened on entry and will be locked and
668 1.1 ad * opened on return.
669 1.1 ad */
670 1.1 ad static int
671 1.1 ad unionfs_copyfile_core(struct vnode *lvp, struct vnode *uvp,
672 1.1 ad kauth_cred_t cred)
673 1.1 ad {
674 1.1 ad int error;
675 1.1 ad off_t offset;
676 1.1 ad int count;
677 1.1 ad int bufoffset;
678 1.1 ad char *buf;
679 1.1 ad struct uio uio;
680 1.1 ad struct iovec iov;
681 1.1 ad
682 1.1 ad error = 0;
683 1.1 ad memset(&uio, 0, sizeof(uio));
684 1.1 ad UIO_SETUP_SYSSPACE(&uio);
685 1.1 ad uio.uio_offset = 0;
686 1.1 ad
687 1.1 ad buf = kmem_alloc(MAXBSIZE, KM_SLEEP);
688 1.1 ad if (buf == NULL)
689 1.1 ad return ENOMEM;
690 1.1 ad
691 1.1 ad while (error == 0) {
692 1.1 ad offset = uio.uio_offset;
693 1.1 ad
694 1.1 ad uio.uio_iov = &iov;
695 1.1 ad uio.uio_iovcnt = 1;
696 1.1 ad iov.iov_base = buf;
697 1.1 ad iov.iov_len = MAXBSIZE;
698 1.1 ad uio.uio_resid = iov.iov_len;
699 1.1 ad uio.uio_rw = UIO_READ;
700 1.1 ad
701 1.1 ad if ((error = VOP_READ(lvp, &uio, 0, cred)) != 0)
702 1.1 ad break;
703 1.1 ad if ((count = MAXBSIZE - uio.uio_resid) == 0)
704 1.1 ad break;
705 1.1 ad
706 1.1 ad bufoffset = 0;
707 1.1 ad while (bufoffset < count) {
708 1.1 ad uio.uio_iov = &iov;
709 1.1 ad uio.uio_iovcnt = 1;
710 1.1 ad iov.iov_base = buf + bufoffset;
711 1.1 ad iov.iov_len = count - bufoffset;
712 1.1 ad uio.uio_offset = offset + bufoffset;
713 1.1 ad uio.uio_resid = iov.iov_len;
714 1.1 ad uio.uio_rw = UIO_WRITE;
715 1.1 ad
716 1.1 ad if ((error = VOP_WRITE(uvp, &uio, 0, cred)) != 0)
717 1.1 ad break;
718 1.1 ad
719 1.1 ad bufoffset += (count - bufoffset) - uio.uio_resid;
720 1.1 ad }
721 1.1 ad
722 1.1 ad uio.uio_offset = offset + bufoffset;
723 1.1 ad }
724 1.1 ad
725 1.1 ad kmem_free(buf, MAXBSIZE);
726 1.1 ad
727 1.1 ad return (error);
728 1.1 ad }
729 1.1 ad
730 1.1 ad /*
731 1.1 ad * Copy file from lower to upper.
732 1.1 ad *
733 1.1 ad * If you need copy of the contents, set 1 to docopy. Otherwise, set 0 to
734 1.1 ad * docopy.
735 1.1 ad *
736 1.1 ad * If no error returned, unp will be updated.
737 1.1 ad */
738 1.1 ad int
739 1.1 ad unionfs_copyfile(struct unionfs_node *unp, int docopy, kauth_cred_t cred)
740 1.1 ad {
741 1.1 ad int error;
742 1.1 ad struct vnode *udvp;
743 1.1 ad struct vnode *lvp;
744 1.1 ad struct vnode *uvp;
745 1.1 ad struct vattr uva;
746 1.1 ad
747 1.1 ad lvp = unp->un_lowervp;
748 1.1 ad uvp = NULLVP;
749 1.1 ad
750 1.1 ad if ((UNIONFSTOV(unp)->v_mount->mnt_flag & MNT_RDONLY))
751 1.1 ad return (EROFS);
752 1.1 ad if (unp->un_dvp == NULLVP)
753 1.1 ad return (EINVAL);
754 1.1 ad if (unp->un_uppervp != NULLVP)
755 1.1 ad return (EEXIST);
756 1.1 ad udvp = VTOUNIONFS(unp->un_dvp)->un_uppervp;
757 1.1 ad if (udvp == NULLVP)
758 1.1 ad return (EROFS);
759 1.1 ad if ((udvp->v_mount->mnt_flag & MNT_RDONLY))
760 1.1 ad return (EROFS);
761 1.1 ad
762 1.1 ad error = VOP_ACCESS(lvp, VREAD, cred);
763 1.1 ad if (error != 0)
764 1.1 ad return (error);
765 1.1 ad
766 1.1 ad error = unionfs_vn_create_on_upper(&uvp, udvp, unp, &uva);
767 1.1 ad if (error != 0)
768 1.1 ad return (error);
769 1.1 ad
770 1.1 ad if (docopy != 0) {
771 1.1 ad error = VOP_OPEN(lvp, FREAD, cred);
772 1.1 ad if (error == 0) {
773 1.1 ad error = unionfs_copyfile_core(lvp, uvp, cred);
774 1.1 ad VOP_CLOSE(lvp, FREAD, cred);
775 1.1 ad }
776 1.1 ad }
777 1.1 ad VOP_CLOSE(uvp, FWRITE, cred);
778 1.1 ad uvp->v_writecount--;
779 1.1 ad
780 1.1 ad if (error == 0) {
781 1.1 ad /* Reset the attributes. Ignore errors. */
782 1.1 ad uva.va_type = VNON;
783 1.1 ad VOP_SETATTR(uvp, &uva, cred);
784 1.1 ad }
785 1.1 ad
786 1.1 ad unionfs_node_update(unp, uvp);
787 1.1 ad
788 1.1 ad return (error);
789 1.1 ad }
790 1.1 ad
791 1.1 ad /*
792 1.1 ad * It checks whether vp can rmdir. (check empty)
793 1.1 ad *
794 1.1 ad * vp is unionfs vnode.
795 1.1 ad * vp should be locked.
796 1.1 ad */
797 1.1 ad int
798 1.1 ad unionfs_check_rmdir(struct vnode *vp, kauth_cred_t cred)
799 1.1 ad {
800 1.1 ad int error;
801 1.1 ad int eofflag;
802 1.1 ad int lookuperr;
803 1.1 ad struct vnode *uvp;
804 1.1 ad struct vnode *lvp;
805 1.1 ad struct vnode *tvp;
806 1.1 ad struct vattr va;
807 1.1 ad struct componentname cn;
808 1.1 ad /*
809 1.1 ad * The size of buf needs to be larger than DIRBLKSIZ.
810 1.1 ad */
811 1.1 ad char buf[256 * 6];
812 1.1 ad struct dirent *dp;
813 1.1 ad struct dirent *edp;
814 1.1 ad struct uio uio;
815 1.1 ad struct iovec iov;
816 1.1 ad
817 1.1 ad KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
818 1.1 ad
819 1.1 ad eofflag = 0;
820 1.1 ad uvp = UNIONFSVPTOUPPERVP(vp);
821 1.1 ad lvp = UNIONFSVPTOLOWERVP(vp);
822 1.1 ad
823 1.1 ad /* check opaque */
824 1.1 ad if ((error = VOP_GETATTR(uvp, &va, cred)) != 0)
825 1.1 ad return (error);
826 1.1 ad if (va.va_flags & OPAQUE)
827 1.1 ad return (0);
828 1.1 ad
829 1.1 ad /* open vnode */
830 1.1 ad if ((error = VOP_ACCESS(vp, VEXEC|VREAD, cred)) != 0)
831 1.1 ad return (error);
832 1.1 ad if ((error = VOP_OPEN(vp, FREAD, cred)) != 0)
833 1.1 ad return (error);
834 1.1 ad
835 1.1 ad UIO_SETUP_SYSSPACE(&uio);
836 1.1 ad uio.uio_rw = UIO_READ;
837 1.1 ad uio.uio_offset = 0;
838 1.1 ad
839 1.1 ad while (!error && !eofflag) {
840 1.1 ad iov.iov_base = buf;
841 1.1 ad iov.iov_len = sizeof(buf);
842 1.1 ad uio.uio_iov = &iov;
843 1.1 ad uio.uio_iovcnt = 1;
844 1.1 ad uio.uio_resid = iov.iov_len;
845 1.1 ad
846 1.1 ad error = VOP_READDIR(lvp, &uio, cred, &eofflag, NULL, NULL);
847 1.1 ad if (error)
848 1.1 ad break;
849 1.1 ad
850 1.1 ad edp = (struct dirent*)&buf[sizeof(buf) - uio.uio_resid];
851 1.1 ad for (dp = (struct dirent*)buf; !error && dp < edp;
852 1.1 ad dp = (struct dirent*)((char *)dp + dp->d_reclen)) {
853 1.1 ad if (dp->d_type == DT_WHT ||
854 1.1 ad (dp->d_namlen == 1 && dp->d_name[0] == '.') ||
855 1.2 cegger (dp->d_namlen == 2 && !memcmp(dp->d_name, "..", 2)))
856 1.1 ad continue;
857 1.1 ad
858 1.1 ad cn.cn_namelen = dp->d_namlen;
859 1.1 ad cn.cn_nameptr = dp->d_name;
860 1.1 ad cn.cn_nameiop = LOOKUP;
861 1.11 dholland cn.cn_flags = (LOCKPARENT | LOCKLEAF | RDONLY | ISLASTCN);
862 1.1 ad cn.cn_cred = cred;
863 1.1 ad cn.cn_consume = 0;
864 1.1 ad
865 1.1 ad /*
866 1.1 ad * check entry in lower.
867 1.1 ad * Sometimes, readdir function returns
868 1.1 ad * wrong entry.
869 1.1 ad */
870 1.1 ad lookuperr = VOP_LOOKUP(lvp, &tvp, &cn);
871 1.1 ad
872 1.1 ad if (!lookuperr)
873 1.1 ad vput(tvp);
874 1.1 ad else
875 1.1 ad continue; /* skip entry */
876 1.1 ad
877 1.1 ad /*
878 1.1 ad * check entry
879 1.1 ad * If it has no exist/whiteout entry in upper,
880 1.1 ad * directory is not empty.
881 1.1 ad */
882 1.11 dholland cn.cn_flags = (LOCKPARENT | LOCKLEAF | RDONLY | ISLASTCN);
883 1.1 ad lookuperr = VOP_LOOKUP(uvp, &tvp, &cn);
884 1.1 ad
885 1.1 ad if (!lookuperr)
886 1.1 ad vput(tvp);
887 1.1 ad
888 1.1 ad /* ignore exist or whiteout entry */
889 1.1 ad if (!lookuperr ||
890 1.1 ad (lookuperr == ENOENT && (cn.cn_flags & ISWHITEOUT)))
891 1.1 ad continue;
892 1.1 ad
893 1.1 ad error = ENOTEMPTY;
894 1.1 ad }
895 1.1 ad }
896 1.1 ad
897 1.1 ad /* close vnode */
898 1.1 ad VOP_CLOSE(vp, FREAD, cred);
899 1.1 ad
900 1.1 ad return (error);
901 1.1 ad }
902 1.1 ad
903 1.1 ad #ifdef DIAGNOSTIC
904 1.1 ad
905 1.1 ad struct vnode *
906 1.1 ad unionfs_checkuppervp(struct vnode *vp, const char *fil, int lno)
907 1.1 ad {
908 1.1 ad struct unionfs_node *unp;
909 1.1 ad
910 1.1 ad unp = VTOUNIONFS(vp);
911 1.1 ad
912 1.1 ad #ifdef notyet
913 1.1 ad if (vp->v_op != unionfs_vnodeop_p) {
914 1.1 ad printf("unionfs_checkuppervp: on non-unionfs-node.\n");
915 1.1 ad #ifdef KDB
916 1.1 ad kdb_enter(KDB_WHY_UNIONFS,
917 1.1 ad "unionfs_checkuppervp: on non-unionfs-node.\n");
918 1.1 ad #endif
919 1.1 ad panic("unionfs_checkuppervp");
920 1.1 ad };
921 1.1 ad #endif
922 1.1 ad return (unp->un_uppervp);
923 1.1 ad }
924 1.1 ad
925 1.1 ad struct vnode *
926 1.1 ad unionfs_checklowervp(struct vnode *vp, const char *fil, int lno)
927 1.1 ad {
928 1.1 ad struct unionfs_node *unp;
929 1.1 ad
930 1.1 ad unp = VTOUNIONFS(vp);
931 1.1 ad
932 1.1 ad #ifdef notyet
933 1.1 ad if (vp->v_op != unionfs_vnodeop_p) {
934 1.1 ad printf("unionfs_checklowervp: on non-unionfs-node.\n");
935 1.1 ad #ifdef KDB
936 1.1 ad kdb_enter(KDB_WHY_UNIONFS,
937 1.1 ad "unionfs_checklowervp: on non-unionfs-node.\n");
938 1.1 ad #endif
939 1.1 ad panic("unionfs_checklowervp");
940 1.1 ad };
941 1.1 ad #endif
942 1.1 ad return (unp->un_lowervp);
943 1.1 ad }
944 1.1 ad #endif
945