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