unionfs_vnops.c revision 1.14.6.1 1 /*-
2 * Copyright (c) 1992, 1993, 1994, 1995 Jan-Simon Pendry.
3 * Copyright (c) 1992, 1993, 1994, 1995
4 * The Regents of the University of California.
5 * Copyright (c) 2005, 2006 Masanori Ozawa <ozawa (at) ongs.co.jp>, ONGS Inc.
6 * Copyright (c) 2006 Daichi Goto <daichi (at) freebsd.org>
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to Berkeley by
10 * Jan-Simon Pendry.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)union_vnops.c 8.32 (Berkeley) 6/23/95
37 * $FreeBSD: src/sys/fs/unionfs/union_vnops.c,v 1.152 2008/01/13 14:44:06 attilio Exp $
38 *
39 */
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/conf.h>
44 #include <sys/kernel.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/mutex.h>
49 #include <sys/namei.h>
50 #include <sys/sysctl.h>
51 #include <sys/vnode.h>
52 #include <sys/buf.h>
53 #include <sys/fcntl.h>
54 #include <sys/stat.h>
55 #include <sys/dirent.h>
56 #include <sys/proc.h>
57
58 #include <fs/unionfs/unionfs.h>
59
60 #if 0
61 #define UNIONFS_INTERNAL_DEBUG(msg, args...) printf(msg, ## args)
62 #define UNIONFS_IDBG_RENAME
63 #else
64 #define UNIONFS_INTERNAL_DEBUG(msg, args...)
65 #endif
66
67 static int
68 unionfs_parsepath(void *v)
69 {
70 struct vop_parsepath_args /* {
71 struct vnode *a_dvp;
72 const char *a_name;
73 size_t *a_retval;
74 } */ *ap = v;
75 struct unionfs_node *dunp;
76 struct vnode *upperdvp, *lowerdvp;
77 size_t upper, lower;
78 int error;
79
80 dunp = VTOUNIONFS(ap->a_dvp);
81 upperdvp = dunp->un_uppervp;
82 lowerdvp = dunp->un_lowervp;
83
84 error = VOP_PARSEPATH(upperdvp, ap->a_name, &upper);
85 if (error) {
86 return error;
87 }
88
89 error = VOP_PARSEPATH(lowerdvp, ap->a_name, &lower);
90 if (error) {
91 return error;
92 }
93
94 /*
95 * If they're different, use the larger one. This is not a
96 * comprehensive solution, but it's sufficient for the
97 * non-default cases of parsepath that currently exist.
98 */
99 *ap->a_retval = MAX(upper, lower);
100 return 0;
101 }
102
103 static int
104 unionfs_lookup(void *v)
105 {
106 struct vop_lookup_args *ap = v;
107 int iswhiteout;
108 int lockflag;
109 int error , uerror, lerror;
110 u_long nameiop;
111 u_long cnflags, cnflagsbk;
112 struct unionfs_node *dunp;
113 struct vnode *dvp, *udvp, *ldvp, *vp, *uvp, *lvp, *dtmpvp;
114 struct vattr va;
115 struct componentname *cnp;
116
117 iswhiteout = 0;
118 lockflag = 0;
119 error = uerror = lerror = ENOENT;
120 cnp = ap->a_cnp;
121 nameiop = cnp->cn_nameiop;
122 cnflags = cnp->cn_flags;
123 dvp = ap->a_dvp;
124 dunp = VTOUNIONFS(dvp);
125 udvp = dunp->un_uppervp;
126 ldvp = dunp->un_lowervp;
127 vp = uvp = lvp = NULLVP;
128 *(ap->a_vpp) = NULLVP;
129
130 UNIONFS_INTERNAL_DEBUG("unionfs_lookup: enter: nameiop=%ld, flags=%lx, path=%s\n", nameiop, cnflags, cnp->cn_nameptr);
131
132 if (dvp->v_type != VDIR)
133 return (ENOTDIR);
134
135 /*
136 * If read-only and op is not LOOKUP, will return EROFS.
137 */
138 if ((cnflags & ISLASTCN) &&
139 (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
140 LOOKUP != nameiop)
141 return (EROFS);
142
143 /*
144 * lookup dotdot
145 */
146 if (cnflags & ISDOTDOT) {
147 if (LOOKUP != nameiop && udvp == NULLVP)
148 return (EROFS);
149
150 if (udvp != NULLVP) {
151 dtmpvp = udvp;
152 if (ldvp != NULLVP)
153 VOP_UNLOCK(ldvp);
154 }
155 else
156 dtmpvp = ldvp;
157
158 error = VOP_LOOKUP(dtmpvp, &vp, cnp);
159
160 if (dtmpvp == udvp && ldvp != NULLVP) {
161 VOP_UNLOCK(udvp);
162 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
163 }
164
165 if (error == 0) {
166 /*
167 * Exchange lock and reference from vp to
168 * dunp->un_dvp. vp is upper/lower vnode, but it
169 * will need to return the unionfs vnode.
170 */
171 if (nameiop == DELETE || nameiop == RENAME)
172 VOP_UNLOCK(vp);
173 vrele(vp);
174
175 VOP_UNLOCK(dvp);
176 *(ap->a_vpp) = dunp->un_dvp;
177 vref(dunp->un_dvp);
178
179 vn_lock(dunp->un_dvp, LK_EXCLUSIVE | LK_RETRY);
180 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
181 } else if (error == ENOENT && nameiop != CREATE)
182 cache_enter(dvp, NULLVP, cnp->cn_nameptr,
183 cnp->cn_namelen, cnp->cn_flags);
184
185 UNIONFS_INTERNAL_DEBUG("unionfs_lookup: leave (%d)\n", error);
186
187 return (error);
188 }
189
190 /*
191 * lookup upper layer
192 */
193 if (udvp != NULLVP) {
194 uerror = VOP_LOOKUP(udvp, &uvp, cnp);
195
196 if (uerror == 0) {
197 if (udvp == uvp) { /* is dot */
198 vrele(uvp);
199 *(ap->a_vpp) = dvp;
200 vref(dvp);
201
202 UNIONFS_INTERNAL_DEBUG("unionfs_lookup: leave (%d)\n", uerror);
203
204 return (uerror);
205 }
206 }
207
208 /* check whiteout */
209 if (uerror == ENOENT || uerror == EJUSTRETURN)
210 if (cnp->cn_flags & ISWHITEOUT)
211 iswhiteout = 1; /* don't lookup lower */
212 if (iswhiteout == 0 && ldvp != NULLVP)
213 if (VOP_GETATTR(udvp, &va, cnp->cn_cred) == 0 &&
214 (va.va_flags & OPAQUE))
215 iswhiteout = 1; /* don't lookup lower */
216 #if 0
217 UNIONFS_INTERNAL_DEBUG("unionfs_lookup: debug: whiteout=%d, path=%s\n", iswhiteout, cnp->cn_nameptr);
218 #endif
219 }
220
221 /*
222 * lookup lower layer
223 */
224 if (ldvp != NULLVP && !(cnflags & DOWHITEOUT) && iswhiteout == 0) {
225 /* always op is LOOKUP */
226 cnp->cn_nameiop = LOOKUP;
227 cnflagsbk = cnp->cn_flags;
228 cnp->cn_flags = cnflags;
229
230 lerror = VOP_LOOKUP(ldvp, &lvp, cnp);
231
232 cnp->cn_nameiop = nameiop;
233 if (udvp != NULLVP && (uerror == 0 || uerror == EJUSTRETURN))
234 cnp->cn_flags = cnflagsbk;
235
236 if (lerror == 0) {
237 if (ldvp == lvp) { /* is dot */
238 if (uvp != NULLVP)
239 vrele(uvp); /* no need? */
240 vrele(lvp);
241 *(ap->a_vpp) = dvp;
242 vref(dvp);
243
244 UNIONFS_INTERNAL_DEBUG("unionfs_lookup: leave (%d)\n", lerror);
245 if (uvp != NULL)
246 VOP_UNLOCK(uvp);
247 return (lerror);
248 }
249 }
250 }
251
252 /*
253 * check lookup result
254 */
255 if (uvp == NULLVP && lvp == NULLVP) {
256 UNIONFS_INTERNAL_DEBUG("unionfs_lookup: leave (%d)\n",
257 (udvp != NULLVP ? uerror : lerror));
258 return (udvp != NULLVP ? uerror : lerror);
259 }
260
261 /*
262 * check vnode type
263 */
264 if (uvp != NULLVP && lvp != NULLVP && uvp->v_type != lvp->v_type) {
265 vput(lvp);
266 lvp = NULLVP;
267 }
268
269 /*
270 * check shadow dir
271 */
272 if (uerror != 0 && uerror != EJUSTRETURN && udvp != NULLVP &&
273 lerror == 0 && lvp != NULLVP && lvp->v_type == VDIR &&
274 !(dvp->v_mount->mnt_flag & MNT_RDONLY) &&
275 (1 < cnp->cn_namelen || '.' != *(cnp->cn_nameptr))) {
276 /* get unionfs vnode in order to create a new shadow dir. */
277 error = unionfs_nodeget(dvp->v_mount, NULLVP, lvp, dvp, &vp,
278 cnp);
279 if (error != 0)
280 goto unionfs_lookup_out;
281 error = unionfs_mkshadowdir(MOUNTTOUNIONFSMOUNT(dvp->v_mount),
282 udvp, VTOUNIONFS(vp), cnp);
283 if (error != 0) {
284 UNIONFSDEBUG("unionfs_lookup: Unable to create shadow dir.");
285 vput(vp);
286 goto unionfs_lookup_out;
287 }
288 }
289 /*
290 * get unionfs vnode.
291 */
292 else {
293 if (uvp != NULLVP)
294 error = uerror;
295 else
296 error = lerror;
297 if (error != 0)
298 goto unionfs_lookup_out;
299 error = unionfs_nodeget(dvp->v_mount, uvp, lvp, dvp, &vp, cnp);
300 if (error != 0) {
301 UNIONFSDEBUG("unionfs_lookup: Unable to create unionfs vnode.");
302 goto unionfs_lookup_out;
303 }
304 }
305
306 *(ap->a_vpp) = vp;
307
308 cache_enter(dvp, vp, cnp->cn_nameptr, cnp->cn_namelen,
309 cnp->cn_flags);
310
311 /* XXXAD lock status on error */
312 unionfs_lookup_out:
313 if (uvp != NULLVP)
314 vrele(uvp);
315 if (lvp != NULLVP)
316 vrele(lvp);
317
318 if (error == ENOENT && nameiop != CREATE)
319 cache_enter(dvp, NULLVP, cnp->cn_nameptr, cnp->cn_namelen,
320 cnp->cn_flags);
321
322 UNIONFS_INTERNAL_DEBUG("unionfs_lookup: leave (%d)\n", error);
323
324 return (error);
325 }
326
327 static int
328 unionfs_create(void *v)
329 {
330 struct vop_create_args *ap = v;
331 struct unionfs_node *dunp;
332 struct componentname *cnp;
333 struct vnode *udvp;
334 struct vnode *vp;
335 int error;
336
337 UNIONFS_INTERNAL_DEBUG("unionfs_create: enter\n");
338
339 dunp = VTOUNIONFS(ap->a_dvp);
340 cnp = ap->a_cnp;
341 udvp = dunp->un_uppervp;
342 error = EROFS;
343
344 if (udvp != NULLVP) {
345 if ((error = VOP_CREATE(udvp, &vp, cnp, ap->a_vap)) == 0) {
346 error = unionfs_nodeget(ap->a_dvp->v_mount, vp, NULLVP,
347 ap->a_dvp, ap->a_vpp, cnp);
348 if (error) {
349 vput(vp);
350 } else {
351 vrele(vp);
352 }
353 }
354 }
355
356 UNIONFS_INTERNAL_DEBUG("unionfs_create: leave (%d)\n", error);
357
358 return (error);
359 }
360
361 static int
362 unionfs_whiteout(void *v)
363 {
364 struct vop_whiteout_args *ap = v;
365 struct unionfs_node *dunp;
366 struct componentname *cnp;
367 struct vnode *udvp;
368 int error;
369
370 UNIONFS_INTERNAL_DEBUG("unionfs_whiteout: enter\n");
371
372 dunp = VTOUNIONFS(ap->a_dvp);
373 cnp = ap->a_cnp;
374 udvp = dunp->un_uppervp;
375 error = EOPNOTSUPP;
376
377 if (udvp != NULLVP) {
378 switch (ap->a_flags) {
379 case CREATE:
380 case DELETE:
381 case LOOKUP:
382 error = VOP_WHITEOUT(udvp, cnp, ap->a_flags);
383 break;
384 default:
385 error = EINVAL;
386 break;
387 }
388 }
389
390 UNIONFS_INTERNAL_DEBUG("unionfs_whiteout: leave (%d)\n", error);
391
392 return (error);
393 }
394
395 static int
396 unionfs_mknod(void *v)
397 {
398 struct vop_mknod_args *ap = v;
399 struct unionfs_node *dunp;
400 struct componentname *cnp;
401 struct vnode *udvp;
402 struct vnode *vp;
403 int error;
404
405 UNIONFS_INTERNAL_DEBUG("unionfs_mknod: enter\n");
406
407 dunp = VTOUNIONFS(ap->a_dvp);
408 cnp = ap->a_cnp;
409 udvp = dunp->un_uppervp;
410 error = EROFS;
411
412 if (udvp != NULLVP) {
413 if ((error = VOP_MKNOD(udvp, &vp, cnp, ap->a_vap)) == 0) {
414 error = unionfs_nodeget(ap->a_dvp->v_mount, vp, NULLVP,
415 ap->a_dvp, ap->a_vpp, cnp);
416 if (error) {
417 vput(vp);
418 } else {
419 vrele(vp);
420 }
421 }
422 }
423
424 UNIONFS_INTERNAL_DEBUG("unionfs_mknod: leave (%d)\n", error);
425
426 return (error);
427 }
428
429 static int
430 unionfs_open(void *v)
431 {
432 struct vop_open_args *ap = v;
433 int error;
434 struct unionfs_node *unp;
435 struct unionfs_node_status *unsp;
436 struct vnode *uvp;
437 struct vnode *lvp;
438 struct vnode *targetvp;
439 kauth_cred_t cred;
440
441 UNIONFS_INTERNAL_DEBUG("unionfs_open: enter\n");
442
443 error = 0;
444 unp = VTOUNIONFS(ap->a_vp);
445 uvp = unp->un_uppervp;
446 lvp = unp->un_lowervp;
447 targetvp = NULLVP;
448 cred = ap->a_cred;
449
450 unionfs_get_node_status(unp, &unsp);
451
452 if (unsp->uns_lower_opencnt > 0 || unsp->uns_upper_opencnt > 0) {
453 /* vnode is already opend. */
454 if (unsp->uns_upper_opencnt > 0)
455 targetvp = uvp;
456 else
457 targetvp = lvp;
458
459 if (targetvp == lvp &&
460 (ap->a_mode & FWRITE) && lvp->v_type == VREG)
461 targetvp = NULLVP;
462 }
463 if (targetvp == NULLVP) {
464 if (uvp == NULLVP) {
465 if ((ap->a_mode & FWRITE) && lvp->v_type == VREG) {
466 error = unionfs_copyfile(unp,
467 !(ap->a_mode & O_TRUNC), cred);
468 if (error != 0)
469 goto unionfs_open_abort;
470 targetvp = uvp = unp->un_uppervp;
471 } else
472 targetvp = lvp;
473 } else
474 targetvp = uvp;
475 }
476
477 error = VOP_OPEN(targetvp, ap->a_mode, cred);
478 if (error == 0) {
479 if (targetvp == uvp) {
480 if (uvp->v_type == VDIR && lvp != NULLVP &&
481 unsp->uns_lower_opencnt <= 0) {
482 /* open lower for readdir */
483 error = VOP_OPEN(lvp, FREAD, cred);
484 if (error != 0) {
485 VOP_CLOSE(uvp, ap->a_mode, cred);
486 goto unionfs_open_abort;
487 }
488 unsp->uns_node_flag |= UNS_OPENL_4_READDIR;
489 unsp->uns_lower_opencnt++;
490 }
491 unsp->uns_upper_opencnt++;
492 } else {
493 unsp->uns_lower_opencnt++;
494 unsp->uns_lower_openmode = ap->a_mode;
495 }
496 }
497
498 unionfs_open_abort:
499 if (error != 0)
500 unionfs_tryrem_node_status(unp, unsp);
501
502 UNIONFS_INTERNAL_DEBUG("unionfs_open: leave (%d)\n", error);
503
504 return (error);
505 }
506
507 static int
508 unionfs_close(void *v)
509 {
510 struct vop_close_args *ap = v;
511 int error;
512 struct unionfs_node *unp;
513 struct unionfs_node_status *unsp;
514 kauth_cred_t cred;
515 struct vnode *ovp;
516
517 UNIONFS_INTERNAL_DEBUG("unionfs_close: enter\n");
518
519 KASSERT(VOP_ISLOCKED(ap->a_vp) == LK_EXCLUSIVE);
520 unp = VTOUNIONFS(ap->a_vp);
521 cred = ap->a_cred;
522
523 unionfs_get_node_status(unp, &unsp);
524
525 if (unsp->uns_lower_opencnt <= 0 && unsp->uns_upper_opencnt <= 0) {
526 #ifdef DIAGNOSTIC
527 printf("unionfs_close: warning: open count is 0\n");
528 #endif
529 if (unp->un_uppervp != NULLVP)
530 ovp = unp->un_uppervp;
531 else
532 ovp = unp->un_lowervp;
533 } else if (unsp->uns_upper_opencnt > 0)
534 ovp = unp->un_uppervp;
535 else
536 ovp = unp->un_lowervp;
537
538 error = VOP_CLOSE(ovp, ap->a_fflag, cred);
539
540 if (error != 0)
541 goto unionfs_close_abort;
542
543 if (ovp == unp->un_uppervp) {
544 unsp->uns_upper_opencnt--;
545 if (unsp->uns_upper_opencnt == 0) {
546 if (unsp->uns_node_flag & UNS_OPENL_4_READDIR) {
547 VOP_CLOSE(unp->un_lowervp, FREAD, cred);
548 unsp->uns_node_flag &= ~UNS_OPENL_4_READDIR;
549 unsp->uns_lower_opencnt--;
550 }
551 }
552 } else
553 unsp->uns_lower_opencnt--;
554
555 unionfs_close_abort:
556 unionfs_tryrem_node_status(unp, unsp);
557
558 UNIONFS_INTERNAL_DEBUG("unionfs_close: leave (%d)\n", error);
559
560 return (error);
561 }
562
563 /*
564 * Check the access mode toward shadow file/dir.
565 */
566 static int
567 unionfs_check_corrected_access(u_short mode, struct vattr *va, kauth_cred_t cred)
568 {
569 int result;
570 int error;
571 uid_t uid; /* upper side vnode's uid */
572 gid_t gid; /* upper side vnode's gid */
573 u_short vmode; /* upper side vnode's mode */
574 u_short mask;
575
576 mask = 0;
577 uid = va->va_uid;
578 gid = va->va_gid;
579 vmode = va->va_mode;
580
581 /* check owner */
582 if (kauth_cred_getuid(cred) == uid) {
583 if (mode & VEXEC)
584 mask |= S_IXUSR;
585 if (mode & VREAD)
586 mask |= S_IRUSR;
587 if (mode & VWRITE)
588 mask |= S_IWUSR;
589 return ((vmode & mask) == mask ? 0 : EACCES);
590 }
591
592 /* check group */
593 error = kauth_cred_ismember_gid(cred, gid, &result);
594 if (error != 0)
595 return error;
596 if (result) {
597 if (mode & VEXEC)
598 mask |= S_IXGRP;
599 if (mode & VREAD)
600 mask |= S_IRGRP;
601 if (mode & VWRITE)
602 mask |= S_IWGRP;
603 return ((vmode & mask) == mask ? 0 : EACCES);
604 }
605
606 /* check other */
607 if (mode & VEXEC)
608 mask |= S_IXOTH;
609 if (mode & VREAD)
610 mask |= S_IROTH;
611 if (mode & VWRITE)
612 mask |= S_IWOTH;
613
614 return ((vmode & mask) == mask ? 0 : EACCES);
615 }
616
617 static int
618 unionfs_access(void *v)
619 {
620 struct vop_access_args *ap = v;
621 struct unionfs_mount *ump;
622 struct unionfs_node *unp;
623 struct vnode *uvp;
624 struct vnode *lvp;
625 struct vattr va;
626 int mode;
627 int error;
628
629 UNIONFS_INTERNAL_DEBUG("unionfs_access: enter\n");
630
631 ump = MOUNTTOUNIONFSMOUNT(ap->a_vp->v_mount);
632 unp = VTOUNIONFS(ap->a_vp);
633 uvp = unp->un_uppervp;
634 lvp = unp->un_lowervp;
635 mode = ap->a_mode;
636 error = EACCES;
637
638 if ((mode & VWRITE) &&
639 (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)) {
640 switch (ap->a_vp->v_type) {
641 case VREG:
642 case VDIR:
643 case VLNK:
644 return (EROFS);
645 default:
646 break;
647 }
648 }
649
650 if (uvp != NULLVP) {
651 error = VOP_ACCESS(uvp, mode, ap->a_cred);
652
653 UNIONFS_INTERNAL_DEBUG("unionfs_access: leave (%d)\n", error);
654
655 return (error);
656 }
657
658 if (lvp != NULLVP) {
659 if (mode & VWRITE) {
660 if (ump->um_uppervp->v_mount->mnt_flag & MNT_RDONLY) {
661 switch (ap->a_vp->v_type) {
662 case VREG:
663 case VDIR:
664 case VLNK:
665 return (EROFS);
666 default:
667 break;
668 }
669 } else if (ap->a_vp->v_type == VREG || ap->a_vp->v_type == VDIR) {
670 /* check shadow file/dir */
671 if (ump->um_copymode != UNIONFS_TRANSPARENT) {
672 error = unionfs_create_uppervattr(ump,
673 lvp, &va, ap->a_cred);
674 if (error != 0)
675 return (error);
676
677 error = unionfs_check_corrected_access(
678 mode, &va, ap->a_cred);
679 if (error != 0)
680 return (error);
681 }
682 }
683 mode &= ~VWRITE;
684 mode |= VREAD; /* will copy to upper */
685 }
686 error = VOP_ACCESS(lvp, mode, ap->a_cred);
687 }
688
689 UNIONFS_INTERNAL_DEBUG("unionfs_access: leave (%d)\n", error);
690
691 return (error);
692 }
693
694 static int
695 unionfs_getattr(void *v)
696 {
697 struct vop_getattr_args *ap = v;
698 int error;
699 struct unionfs_node *unp;
700 struct unionfs_mount *ump;
701 struct vnode *uvp;
702 struct vnode *lvp;
703 struct vattr va;
704
705 UNIONFS_INTERNAL_DEBUG("unionfs_getattr: enter\n");
706
707 unp = VTOUNIONFS(ap->a_vp);
708 ump = MOUNTTOUNIONFSMOUNT(ap->a_vp->v_mount);
709 uvp = unp->un_uppervp;
710 lvp = unp->un_lowervp;
711
712 if (uvp != NULLVP) {
713 if ((error = VOP_GETATTR(uvp, ap->a_vap, ap->a_cred)) == 0)
714 ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid;
715
716 UNIONFS_INTERNAL_DEBUG("unionfs_getattr: leave mode=%o, uid=%d, gid=%d (%d)\n",
717 ap->a_vap->va_mode, ap->a_vap->va_uid,
718 ap->a_vap->va_gid, error);
719
720 return (error);
721 }
722
723 error = VOP_GETATTR(lvp, ap->a_vap, ap->a_cred);
724
725 if (error == 0 && !(ump->um_uppervp->v_mount->mnt_flag & MNT_RDONLY)) {
726 /* correct the attr toward shadow file/dir. */
727 if (ap->a_vp->v_type == VREG || ap->a_vp->v_type == VDIR) {
728 unionfs_create_uppervattr_core(ump, ap->a_vap, &va);
729 ap->a_vap->va_mode = va.va_mode;
730 ap->a_vap->va_uid = va.va_uid;
731 ap->a_vap->va_gid = va.va_gid;
732 }
733 }
734
735 if (error == 0)
736 ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid;
737
738 UNIONFS_INTERNAL_DEBUG("unionfs_getattr: leave mode=%o, uid=%d, gid=%d (%d)\n",
739 ap->a_vap->va_mode, ap->a_vap->va_uid, ap->a_vap->va_gid, error);
740
741 return (error);
742 }
743
744 static int
745 unionfs_setattr(void *v)
746 {
747 struct vop_setattr_args *ap = v;
748 int error;
749 struct unionfs_node *unp;
750 struct vnode *uvp;
751 struct vnode *lvp;
752 struct vattr *vap;
753
754 UNIONFS_INTERNAL_DEBUG("unionfs_setattr: enter\n");
755
756 error = EROFS;
757 unp = VTOUNIONFS(ap->a_vp);
758 uvp = unp->un_uppervp;
759 lvp = unp->un_lowervp;
760 vap = ap->a_vap;
761
762 if ((ap->a_vp->v_mount->mnt_flag & MNT_RDONLY) &&
763 (vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
764 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
765 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL))
766 return (EROFS);
767
768 if (uvp == NULLVP && lvp->v_type == VREG) {
769 error = unionfs_copyfile(unp, (vap->va_size != 0),
770 ap->a_cred);
771 if (error != 0)
772 return (error);
773 uvp = unp->un_uppervp;
774 }
775
776 if (uvp != NULLVP)
777 error = VOP_SETATTR(uvp, vap, ap->a_cred);
778
779 UNIONFS_INTERNAL_DEBUG("unionfs_setattr: leave (%d)\n", error);
780
781 return (error);
782 }
783
784 static int
785 unionfs_read(void *v)
786 {
787 struct vop_read_args *ap = v;
788 int error;
789 struct unionfs_node *unp;
790 struct vnode *tvp;
791
792 /* UNIONFS_INTERNAL_DEBUG("unionfs_read: enter\n"); */
793
794 unp = VTOUNIONFS(ap->a_vp);
795 tvp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp);
796
797 error = VOP_READ(tvp, ap->a_uio, ap->a_ioflag, ap->a_cred);
798
799 /* UNIONFS_INTERNAL_DEBUG("unionfs_read: leave (%d)\n", error); */
800
801 return (error);
802 }
803
804 static int
805 unionfs_write(void *v)
806 {
807 struct vop_write_args *ap = v;
808 int error;
809 struct unionfs_node *unp;
810 struct vnode *tvp;
811
812 /* UNIONFS_INTERNAL_DEBUG("unionfs_write: enter\n"); */
813
814 unp = VTOUNIONFS(ap->a_vp);
815 tvp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp);
816
817 error = VOP_WRITE(tvp, ap->a_uio, ap->a_ioflag, ap->a_cred);
818
819 /* UNIONFS_INTERNAL_DEBUG("unionfs_write: leave (%d)\n", error); */
820
821 return (error);
822 }
823
824 static int
825 unionfs_ioctl(void *v)
826 {
827 struct vop_ioctl_args *ap = v;
828 int error;
829 struct unionfs_node *unp;
830 struct unionfs_node_status *unsp;
831 struct vnode *ovp;
832
833 UNIONFS_INTERNAL_DEBUG("unionfs_ioctl: enter\n");
834
835 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
836 unp = VTOUNIONFS(ap->a_vp);
837 unionfs_get_node_status(unp, &unsp);
838 ovp = (unsp->uns_upper_opencnt ? unp->un_uppervp : unp->un_lowervp);
839 unionfs_tryrem_node_status(unp, unsp);
840 VOP_UNLOCK(ap->a_vp);
841
842 if (ovp == NULLVP)
843 return (EBADF);
844
845 error = VOP_IOCTL(ovp, ap->a_command, ap->a_data, ap->a_fflag,
846 ap->a_cred);
847
848 UNIONFS_INTERNAL_DEBUG("unionfs_ioctl: lease (%d)\n", error);
849
850 return (error);
851 }
852
853 static int
854 unionfs_poll(void *v)
855 {
856 struct vop_poll_args *ap = v;
857 struct unionfs_node *unp;
858 struct unionfs_node_status *unsp;
859 struct vnode *ovp;
860
861 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
862 unp = VTOUNIONFS(ap->a_vp);
863 unionfs_get_node_status(unp, &unsp);
864 ovp = (unsp->uns_upper_opencnt ? unp->un_uppervp : unp->un_lowervp);
865 unionfs_tryrem_node_status(unp, unsp);
866 VOP_UNLOCK(ap->a_vp);
867
868 if (ovp == NULLVP)
869 return (EBADF);
870
871 return (VOP_POLL(ovp, ap->a_events));
872 }
873
874 static int
875 unionfs_fsync(void *v)
876 {
877 struct vop_fsync_args *ap = v;
878 struct unionfs_node *unp;
879 struct unionfs_node_status *unsp;
880 struct vnode *ovp;
881
882 unp = VTOUNIONFS(ap->a_vp);
883 unionfs_get_node_status(unp, &unsp);
884 ovp = (unsp->uns_upper_opencnt ? unp->un_uppervp : unp->un_lowervp);
885 unionfs_tryrem_node_status(unp, unsp);
886
887 if (ovp == NULLVP)
888 return (EBADF);
889
890 return (VOP_FSYNC(ovp, ap->a_cred, ap->a_flags, ap->a_offlo, ap->a_offhi));
891 }
892
893 static int
894 unionfs_remove(void *v)
895 {
896 struct vop_remove_v2_args *ap = v;
897 int error;
898 struct unionfs_node *dunp;
899 struct unionfs_node *unp;
900 struct unionfs_mount *ump;
901 struct vnode *udvp;
902 struct vnode *uvp;
903 struct vnode *lvp;
904 struct componentname *cnp;
905
906 UNIONFS_INTERNAL_DEBUG("unionfs_remove: enter\n");
907
908 error = 0;
909 dunp = VTOUNIONFS(ap->a_dvp);
910 unp = VTOUNIONFS(ap->a_vp);
911 udvp = dunp->un_uppervp;
912 uvp = unp->un_uppervp;
913 lvp = unp->un_lowervp;
914 cnp = ap->a_cnp;
915
916 if (udvp == NULLVP) {
917 vput(ap->a_vp);
918 return (EROFS);
919 }
920
921 if (uvp != NULLVP) {
922 ump = MOUNTTOUNIONFSMOUNT(ap->a_vp->v_mount);
923 if (ump->um_whitemode == UNIONFS_WHITE_ALWAYS || lvp != NULLVP)
924 cnp->cn_flags |= DOWHITEOUT;
925 error = VOP_REMOVE(udvp, uvp, cnp);
926 } else if (lvp != NULLVP)
927 error = unionfs_mkwhiteout(udvp, cnp, unp->un_path);
928
929 UNIONFS_INTERNAL_DEBUG("unionfs_remove: leave (%d)\n", error);
930
931 return (error);
932 }
933
934 static int
935 unionfs_link(void *v)
936 {
937 #if 0
938 struct vop_link_v2_args *ap = v;
939 int error;
940 int needrelookup;
941 struct unionfs_node *dunp;
942 struct unionfs_node *unp;
943 struct vnode *udvp;
944 struct vnode *uvp;
945 struct componentname *cnp;
946
947 UNIONFS_INTERNAL_DEBUG("unionfs_link: enter\n");
948
949 error = 0;
950 needrelookup = 0;
951 dunp = VTOUNIONFS(ap->a_tdvp);
952 unp = NULL;
953 udvp = dunp->un_uppervp;
954 uvp = NULLVP;
955 cnp = ap->a_cnp;
956
957 if (udvp == NULLVP)
958 return (EROFS);
959
960 if (ap->a_vp->v_op != unionfs_vnodeop_p)
961 uvp = ap->a_vp;
962 else {
963 unp = VTOUNIONFS(ap->a_vp);
964
965 if (unp->un_uppervp == NULLVP) {
966 if (ap->a_vp->v_type != VREG)
967 return (EOPNOTSUPP);
968
969 error = unionfs_copyfile(unp, 1, cnp->cn_cred);
970 if (error != 0)
971 return (error);
972 needrelookup = 1;
973 }
974 uvp = unp->un_uppervp;
975 }
976
977 if (needrelookup != 0)
978 error = unionfs_relookup_for_create(ap->a_tdvp, cnp);
979
980 if (error == 0)
981 error = VOP_LINK(udvp, uvp, cnp);
982
983 UNIONFS_INTERNAL_DEBUG("unionfs_link: leave (%d)\n", error);
984
985 return (error);
986 #else
987 panic("XXXAD");
988 return 0;
989 #endif
990 }
991
992 static int
993 unionfs_rename(void *v)
994 {
995 struct vop_rename_args *ap = v;
996 int error;
997 struct vnode *fdvp;
998 struct vnode *fvp;
999 struct componentname *fcnp;
1000 struct vnode *tdvp;
1001 struct vnode *tvp;
1002 struct componentname *tcnp;
1003 struct vnode *ltdvp;
1004 struct vnode *ltvp;
1005
1006 /* rename target vnodes */
1007 struct vnode *rfdvp;
1008 struct vnode *rfvp;
1009 struct vnode *rtdvp;
1010 struct vnode *rtvp;
1011
1012 int needrelookup;
1013 struct unionfs_mount *ump;
1014 struct unionfs_node *unp;
1015
1016 UNIONFS_INTERNAL_DEBUG("unionfs_rename: enter\n");
1017
1018 error = 0;
1019 fdvp = ap->a_fdvp;
1020 fvp = ap->a_fvp;
1021 fcnp = ap->a_fcnp;
1022 tdvp = ap->a_tdvp;
1023 tvp = ap->a_tvp;
1024 tcnp = ap->a_tcnp;
1025 ltdvp = NULLVP;
1026 ltvp = NULLVP;
1027 rfdvp = fdvp;
1028 rfvp = fvp;
1029 rtdvp = tdvp;
1030 rtvp = tvp;
1031 needrelookup = 0;
1032
1033 /* check for cross device rename */
1034 if (fvp->v_mount != tdvp->v_mount ||
1035 (tvp != NULLVP && fvp->v_mount != tvp->v_mount)) {
1036 error = EXDEV;
1037 goto unionfs_rename_abort;
1038 }
1039
1040 /* Renaming a file to itself has no effect. */
1041 if (fvp == tvp)
1042 goto unionfs_rename_abort;
1043
1044 /*
1045 * from/to vnode is unionfs node.
1046 */
1047
1048 unp = VTOUNIONFS(fdvp);
1049 #ifdef UNIONFS_IDBG_RENAME
1050 UNIONFS_INTERNAL_DEBUG("fdvp=%p, ufdvp=%p, lfdvp=%p\n", fdvp, unp->un_uppervp, unp->un_lowervp);
1051 #endif
1052 if (unp->un_uppervp == NULLVP) {
1053 error = ENODEV;
1054 goto unionfs_rename_abort;
1055 }
1056 rfdvp = unp->un_uppervp;
1057 vref(rfdvp);
1058
1059 unp = VTOUNIONFS(fvp);
1060 #ifdef UNIONFS_IDBG_RENAME
1061 UNIONFS_INTERNAL_DEBUG("fvp=%p, ufvp=%p, lfvp=%p\n", fvp, unp->un_uppervp, unp->un_lowervp);
1062 #endif
1063 ump = MOUNTTOUNIONFSMOUNT(fvp->v_mount);
1064 if (unp->un_uppervp == NULLVP) {
1065 switch (fvp->v_type) {
1066 case VREG:
1067 if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0)
1068 goto unionfs_rename_abort;
1069 error = unionfs_copyfile(unp, 1, fcnp->cn_cred);
1070 VOP_UNLOCK(fvp);
1071 if (error != 0)
1072 goto unionfs_rename_abort;
1073 break;
1074 case VDIR:
1075 if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0)
1076 goto unionfs_rename_abort;
1077 error = unionfs_mkshadowdir(ump, rfdvp, unp, fcnp);
1078 VOP_UNLOCK(fvp);
1079 if (error != 0)
1080 goto unionfs_rename_abort;
1081 break;
1082 default:
1083 error = ENODEV;
1084 goto unionfs_rename_abort;
1085 }
1086
1087 needrelookup = 1;
1088 }
1089
1090 if (unp->un_lowervp != NULLVP)
1091 fcnp->cn_flags |= DOWHITEOUT;
1092 rfvp = unp->un_uppervp;
1093 vref(rfvp);
1094
1095 unp = VTOUNIONFS(tdvp);
1096 #ifdef UNIONFS_IDBG_RENAME
1097 UNIONFS_INTERNAL_DEBUG("tdvp=%p, utdvp=%p, ltdvp=%p\n", tdvp, unp->un_uppervp, unp->un_lowervp);
1098 #endif
1099 if (unp->un_uppervp == NULLVP) {
1100 error = ENODEV;
1101 goto unionfs_rename_abort;
1102 }
1103 rtdvp = unp->un_uppervp;
1104 ltdvp = unp->un_lowervp;
1105 vref(rtdvp);
1106
1107 if (tdvp == tvp) {
1108 rtvp = rtdvp;
1109 vref(rtvp);
1110 } else if (tvp != NULLVP) {
1111 unp = VTOUNIONFS(tvp);
1112 #ifdef UNIONFS_IDBG_RENAME
1113 UNIONFS_INTERNAL_DEBUG("tvp=%p, utvp=%p, ltvp=%p\n", tvp, unp->un_uppervp, unp->un_lowervp);
1114 #endif
1115 if (unp->un_uppervp == NULLVP)
1116 rtvp = NULLVP;
1117 else {
1118 if (tvp->v_type == VDIR) {
1119 error = EINVAL;
1120 goto unionfs_rename_abort;
1121 }
1122 rtvp = unp->un_uppervp;
1123 ltvp = unp->un_lowervp;
1124 vref(rtvp);
1125 }
1126 }
1127
1128 if (needrelookup != 0) {
1129 if ((error = vn_lock(fdvp, LK_EXCLUSIVE)) != 0)
1130 goto unionfs_rename_abort;
1131 error = unionfs_relookup_for_delete(fdvp, fcnp);
1132 VOP_UNLOCK(fdvp);
1133 if (error != 0)
1134 goto unionfs_rename_abort;
1135
1136 /* Locke of tvp is canceled in order to avoid recursive lock. */
1137 if (tvp != NULLVP && tvp != tdvp)
1138 VOP_UNLOCK(tvp);
1139 error = unionfs_relookup_for_rename(tdvp, tcnp);
1140 if (tvp != NULLVP && tvp != tdvp)
1141 vn_lock(tvp, LK_EXCLUSIVE | LK_RETRY);
1142 if (error != 0)
1143 goto unionfs_rename_abort;
1144 }
1145
1146 error = VOP_RENAME(rfdvp, rfvp, fcnp, rtdvp, rtvp, tcnp);
1147
1148 if (error == 0) {
1149 if (rtvp != NULLVP && rtvp->v_type == VDIR)
1150 cache_purge(tdvp);
1151 if (fvp->v_type == VDIR && fdvp != tdvp)
1152 cache_purge(fdvp);
1153 }
1154
1155 if (fdvp != rfdvp)
1156 vrele(fdvp);
1157 if (fvp != rfvp)
1158 vrele(fvp);
1159 if (ltdvp != NULLVP)
1160 VOP_UNLOCK(ltdvp);
1161 if (tdvp != rtdvp)
1162 vrele(tdvp);
1163 if (ltvp != NULLVP)
1164 VOP_UNLOCK(ltvp);
1165 if (tvp != rtvp && tvp != NULLVP) {
1166 if (rtvp == NULLVP)
1167 vput(tvp);
1168 else
1169 vrele(tvp);
1170 }
1171
1172 UNIONFS_INTERNAL_DEBUG("unionfs_rename: leave (%d)\n", error);
1173
1174 return (error);
1175
1176 unionfs_rename_abort:
1177 if (fdvp != rfdvp)
1178 vrele(rfdvp);
1179 if (fvp != rfvp)
1180 vrele(rfvp);
1181 if (tdvp != rtdvp)
1182 vrele(rtdvp);
1183 vput(tdvp);
1184 if (tvp != rtvp && rtvp != NULLVP)
1185 vrele(rtvp);
1186 if (tvp != NULLVP) {
1187 if (tdvp != tvp)
1188 vput(tvp);
1189 else
1190 vrele(tvp);
1191 }
1192 vrele(fdvp);
1193 vrele(fvp);
1194
1195 UNIONFS_INTERNAL_DEBUG("unionfs_rename: leave (%d)\n", error);
1196
1197 return (error);
1198 }
1199
1200 static int
1201 unionfs_mkdir(void *v)
1202 {
1203 struct vop_mkdir_args *ap = v;
1204 int error;
1205 struct unionfs_node *dunp;
1206 struct componentname *cnp;
1207 struct vnode *udvp;
1208 struct vnode *uvp;
1209 struct vattr va;
1210
1211 UNIONFS_INTERNAL_DEBUG("unionfs_mkdir: enter\n");
1212
1213 error = EROFS;
1214 dunp = VTOUNIONFS(ap->a_dvp);
1215 cnp = ap->a_cnp;
1216 udvp = dunp->un_uppervp;
1217
1218 if (udvp != NULLVP) {
1219 /* check opaque */
1220 if (!(cnp->cn_flags & ISWHITEOUT)) {
1221 error = VOP_GETATTR(udvp, &va, cnp->cn_cred);
1222 if (error != 0)
1223 return (error);
1224 if (va.va_flags & OPAQUE)
1225 cnp->cn_flags |= ISWHITEOUT;
1226 }
1227
1228 if ((error = VOP_MKDIR(udvp, &uvp, cnp, ap->a_vap)) == 0) {
1229 error = unionfs_nodeget(ap->a_dvp->v_mount, uvp, NULLVP,
1230 ap->a_dvp, ap->a_vpp, cnp);
1231 if (error) {
1232 vput(uvp);
1233 } else {
1234 vrele(uvp);
1235 }
1236 }
1237 }
1238
1239 UNIONFS_INTERNAL_DEBUG("unionfs_mkdir: leave (%d)\n", error);
1240
1241 return (error);
1242 }
1243
1244 static int
1245 unionfs_rmdir(void *v)
1246 {
1247 struct vop_rmdir_v2_args *ap = v;
1248 int error;
1249 struct unionfs_node *dunp;
1250 struct unionfs_node *unp;
1251 struct unionfs_mount *ump;
1252 struct componentname *cnp;
1253 struct vnode *udvp;
1254 struct vnode *uvp;
1255 struct vnode *lvp;
1256
1257 UNIONFS_INTERNAL_DEBUG("unionfs_rmdir: enter\n");
1258
1259 error = 0;
1260 dunp = VTOUNIONFS(ap->a_dvp);
1261 unp = VTOUNIONFS(ap->a_vp);
1262 cnp = ap->a_cnp;
1263 udvp = dunp->un_uppervp;
1264 uvp = unp->un_uppervp;
1265 lvp = unp->un_lowervp;
1266
1267 if (udvp == NULLVP) {
1268 vput(ap->a_vp);
1269 return (EROFS);
1270 }
1271
1272 if (udvp == uvp)
1273 return (EOPNOTSUPP);
1274
1275 if (uvp != NULLVP) {
1276 if (lvp != NULLVP) {
1277 error = unionfs_check_rmdir(ap->a_vp, cnp->cn_cred);
1278 if (error != 0)
1279 return (error);
1280 }
1281 ump = MOUNTTOUNIONFSMOUNT(ap->a_vp->v_mount);
1282 if (ump->um_whitemode == UNIONFS_WHITE_ALWAYS || lvp != NULLVP)
1283 cnp->cn_flags |= DOWHITEOUT;
1284 error = VOP_RMDIR(udvp, uvp, cnp);
1285 }
1286 else if (lvp != NULLVP)
1287 error = unionfs_mkwhiteout(udvp, cnp, unp->un_path);
1288
1289 if (error == 0) {
1290 cache_purge(ap->a_dvp);
1291 cache_purge(ap->a_vp);
1292 }
1293
1294 UNIONFS_INTERNAL_DEBUG("unionfs_rmdir: leave (%d)\n", error);
1295
1296 return (error);
1297 }
1298
1299 static int
1300 unionfs_symlink(void *v)
1301 {
1302 struct vop_symlink_args *ap = v;
1303 int error;
1304 struct unionfs_node *dunp;
1305 struct componentname *cnp;
1306 struct vnode *udvp;
1307 struct vnode *uvp;
1308
1309 UNIONFS_INTERNAL_DEBUG("unionfs_symlink: enter\n");
1310
1311 error = EROFS;
1312 dunp = VTOUNIONFS(ap->a_dvp);
1313 cnp = ap->a_cnp;
1314 udvp = dunp->un_uppervp;
1315
1316 if (udvp != NULLVP) {
1317 error = VOP_SYMLINK(udvp, &uvp, cnp, ap->a_vap, ap->a_target);
1318 if (error == 0) {
1319 error = unionfs_nodeget(ap->a_dvp->v_mount, uvp, NULLVP,
1320 ap->a_dvp, ap->a_vpp, cnp);
1321 if (error) {
1322 vput(uvp);
1323 } else {
1324 vrele(uvp);
1325 }
1326 }
1327 }
1328
1329 UNIONFS_INTERNAL_DEBUG("unionfs_symlink: leave (%d)\n", error);
1330
1331 return (error);
1332 }
1333
1334 static int
1335 unionfs_readdir(void *v)
1336 {
1337 struct vop_readdir_args *ap = v;
1338 int error;
1339 int eofflag;
1340 int locked;
1341 struct unionfs_node *unp;
1342 struct unionfs_node_status *unsp;
1343 struct uio *uio;
1344 struct vnode *uvp;
1345 struct vnode *lvp;
1346 struct vattr va;
1347
1348 int ncookies_bk;
1349 off_t *cookies_bk;
1350
1351 UNIONFS_INTERNAL_DEBUG("unionfs_readdir: enter\n");
1352
1353 error = 0;
1354 eofflag = 0;
1355 locked = 0;
1356 unp = VTOUNIONFS(ap->a_vp);
1357 uio = ap->a_uio;
1358 uvp = unp->un_uppervp;
1359 lvp = unp->un_lowervp;
1360 ncookies_bk = 0;
1361 cookies_bk = NULL;
1362
1363 if (ap->a_vp->v_type != VDIR)
1364 return (ENOTDIR);
1365
1366 /* check opaque */
1367 if (uvp != NULLVP && lvp != NULLVP) {
1368 if ((error = VOP_GETATTR(uvp, &va, ap->a_cred)) != 0)
1369 goto unionfs_readdir_exit;
1370 if (va.va_flags & OPAQUE)
1371 lvp = NULLVP;
1372 }
1373
1374 /* check the open count. unionfs needs to open before readdir. */
1375 VOP_UNLOCK(ap->a_vp);
1376 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
1377 unionfs_get_node_status(unp, &unsp);
1378 if ((uvp != NULLVP && unsp->uns_upper_opencnt <= 0) ||
1379 (lvp != NULLVP && unsp->uns_lower_opencnt <= 0)) {
1380 unionfs_tryrem_node_status(unp, unsp);
1381 error = EBADF;
1382 }
1383 if (error != 0)
1384 goto unionfs_readdir_exit;
1385
1386 /* upper only */
1387 if (uvp != NULLVP && lvp == NULLVP) {
1388 error = VOP_READDIR(uvp, uio, ap->a_cred, ap->a_eofflag,
1389 ap->a_cookies, ap->a_ncookies);
1390 unsp->uns_readdir_status = 0;
1391
1392 goto unionfs_readdir_exit;
1393 }
1394
1395 /* lower only */
1396 if (uvp == NULLVP && lvp != NULLVP) {
1397 error = VOP_READDIR(lvp, uio, ap->a_cred, ap->a_eofflag,
1398 ap->a_cookies, ap->a_ncookies);
1399 unsp->uns_readdir_status = 2;
1400
1401 goto unionfs_readdir_exit;
1402 }
1403
1404 /*
1405 * readdir upper and lower
1406 */
1407 KASSERT(uvp != NULLVP);
1408 KASSERT(lvp != NULLVP);
1409 if (uio->uio_offset == 0)
1410 unsp->uns_readdir_status = 0;
1411
1412 if (unsp->uns_readdir_status == 0) {
1413 /* read upper */
1414 error = VOP_READDIR(uvp, uio, ap->a_cred, &eofflag,
1415 ap->a_cookies, ap->a_ncookies);
1416
1417 if (error != 0 || eofflag == 0)
1418 goto unionfs_readdir_exit;
1419 unsp->uns_readdir_status = 1;
1420
1421 /*
1422 * ufs(and other fs) needs size of uio_resid larger than
1423 * DIRBLKSIZ.
1424 * size of DIRBLKSIZ equals DEV_BSIZE.
1425 * (see: ufs/ufs/ufs_vnops.c ufs_readdir func , ufs/ufs/dir.h)
1426 */
1427 if (uio->uio_resid <= (uio->uio_resid & (DEV_BSIZE -1)))
1428 goto unionfs_readdir_exit;
1429
1430 /*
1431 * backup cookies
1432 * It prepares to readdir in lower.
1433 */
1434 if (ap->a_ncookies != NULL) {
1435 ncookies_bk = *(ap->a_ncookies);
1436 *(ap->a_ncookies) = 0;
1437 }
1438 if (ap->a_cookies != NULL) {
1439 cookies_bk = *(ap->a_cookies);
1440 *(ap->a_cookies) = NULL;
1441 }
1442 }
1443
1444 /* initialize for readdir in lower */
1445 if (unsp->uns_readdir_status == 1) {
1446 unsp->uns_readdir_status = 2;
1447 uio->uio_offset = 0;
1448 }
1449
1450 if (lvp == NULLVP) {
1451 error = EBADF;
1452 goto unionfs_readdir_exit;
1453 }
1454 /* read lower */
1455 error = VOP_READDIR(lvp, uio, ap->a_cred, ap->a_eofflag,
1456 ap->a_cookies, ap->a_ncookies);
1457
1458 if (cookies_bk != NULL) {
1459 /* merge cookies */
1460 int size;
1461 off_t *newcookies, *pos;
1462
1463 size = *(ap->a_ncookies) + ncookies_bk;
1464 newcookies = (off_t *) malloc(size * sizeof(off_t),
1465 M_TEMP, M_WAITOK);
1466 pos = newcookies;
1467
1468 memcpy(pos, cookies_bk, ncookies_bk * sizeof(off_t));
1469 pos += ncookies_bk * sizeof(off_t);
1470 memcpy(pos, *(ap->a_cookies), *(ap->a_ncookies) * sizeof(off_t));
1471 free(cookies_bk, M_TEMP);
1472 free(*(ap->a_cookies), M_TEMP);
1473 *(ap->a_ncookies) = size;
1474 *(ap->a_cookies) = newcookies;
1475 }
1476
1477 unionfs_readdir_exit:
1478 if (error != 0 && ap->a_eofflag != NULL)
1479 *(ap->a_eofflag) = 1;
1480
1481 UNIONFS_INTERNAL_DEBUG("unionfs_readdir: leave (%d)\n", error);
1482
1483 return (error);
1484 }
1485
1486 static int
1487 unionfs_readlink(void *v)
1488 {
1489 struct vop_readlink_args *ap = v;
1490 int error;
1491 struct unionfs_node *unp;
1492 struct vnode *vp;
1493
1494 UNIONFS_INTERNAL_DEBUG("unionfs_readlink: enter\n");
1495
1496 unp = VTOUNIONFS(ap->a_vp);
1497 vp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp);
1498
1499 error = VOP_READLINK(vp, ap->a_uio, ap->a_cred);
1500
1501 UNIONFS_INTERNAL_DEBUG("unionfs_readlink: leave (%d)\n", error);
1502
1503 return (error);
1504 }
1505
1506 static int
1507 unionfs_inactive(void *v)
1508 {
1509 struct vop_inactive_v2_args *ap = v;
1510 *ap->a_recycle = true;
1511 return (0);
1512 }
1513
1514 static int
1515 unionfs_reclaim(void *v)
1516 {
1517 struct vop_reclaim_v2_args *ap = v;
1518
1519 /* UNIONFS_INTERNAL_DEBUG("unionfs_reclaim: enter\n"); */
1520
1521 VOP_UNLOCK(ap->a_vp);
1522
1523 unionfs_noderem(ap->a_vp);
1524
1525 /* UNIONFS_INTERNAL_DEBUG("unionfs_reclaim: leave\n"); */
1526
1527 return (0);
1528 }
1529
1530 static int
1531 unionfs_print(void *v)
1532 {
1533 struct vop_print_args *ap = v;
1534 struct unionfs_node *unp;
1535 /* struct unionfs_node_status *unsp; */
1536
1537 unp = VTOUNIONFS(ap->a_vp);
1538 /* unionfs_get_node_status(unp, &unsp); */
1539
1540 printf("unionfs_vp=%p, uppervp=%p, lowervp=%p\n",
1541 ap->a_vp, unp->un_uppervp, unp->un_lowervp);
1542 /*
1543 printf("unionfs opencnt: uppervp=%d, lowervp=%d\n",
1544 unsp->uns_upper_opencnt, unsp->uns_lower_opencnt);
1545 */
1546
1547 if (unp->un_uppervp != NULLVP)
1548 vprint("unionfs: upper", unp->un_uppervp);
1549 if (unp->un_lowervp != NULLVP)
1550 vprint("unionfs: lower", unp->un_lowervp);
1551
1552 return (0);
1553 }
1554
1555 static int
1556 unionfs_lock(void *v)
1557 {
1558 struct vop_lock_args *ap = v;
1559 int error;
1560 int flags;
1561 struct vnode *lvp;
1562 struct vnode *uvp;
1563 struct unionfs_node *unp;
1564
1565 unp = VTOUNIONFS(ap->a_vp);
1566 lvp = unp->un_lowervp;
1567 uvp = unp->un_uppervp;
1568 flags = ap->a_flags;
1569 error = 0;
1570
1571 if (lvp != NULLVP) {
1572 error = VOP_LOCK(lvp, flags);
1573 }
1574 if (error == 0 && uvp != NULLVP) {
1575 error = VOP_LOCK(uvp, flags);
1576 if (error != 0) {
1577 VOP_UNLOCK(lvp);
1578 }
1579 }
1580
1581 return error;
1582 }
1583
1584 static int
1585 unionfs_unlock(void *v)
1586 {
1587 struct vop_unlock_args *ap = v;
1588 int error;
1589 struct vnode *lvp;
1590 struct vnode *uvp;
1591 struct unionfs_node *unp;
1592
1593 unp = VTOUNIONFS(ap->a_vp);
1594 lvp = unp->un_lowervp;
1595 uvp = unp->un_uppervp;
1596 error = 0;
1597
1598 if (lvp != NULLVP) {
1599 error = VOP_UNLOCK(lvp);
1600 }
1601 if (error == 0 && uvp != NULLVP) {
1602 error = VOP_UNLOCK(uvp);
1603 }
1604
1605 return error;
1606 }
1607
1608 static int
1609 unionfs_pathconf(void *v)
1610 {
1611 struct vop_pathconf_args *ap = v;
1612 struct unionfs_node *unp;
1613 struct vnode *vp;
1614
1615 unp = VTOUNIONFS(ap->a_vp);
1616 vp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp);
1617
1618 return (VOP_PATHCONF(vp, ap->a_name, ap->a_retval));
1619 }
1620
1621 static int
1622 unionfs_advlock(void *v)
1623 {
1624 struct vop_advlock_args *ap = v;
1625 int error;
1626 struct unionfs_node *unp;
1627 struct unionfs_node_status *unsp;
1628 struct vnode *vp;
1629 struct vnode *uvp;
1630 kauth_cred_t cred;
1631
1632 UNIONFS_INTERNAL_DEBUG("unionfs_advlock: enter\n");
1633
1634 vp = ap->a_vp;
1635 cred = kauth_cred_get();
1636
1637 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1638
1639 unp = VTOUNIONFS(ap->a_vp);
1640 uvp = unp->un_uppervp;
1641
1642 if (uvp == NULLVP) {
1643 error = unionfs_copyfile(unp, 1, cred);
1644 if (error != 0)
1645 goto unionfs_advlock_abort;
1646 uvp = unp->un_uppervp;
1647
1648 unionfs_get_node_status(unp, &unsp);
1649 if (unsp->uns_lower_opencnt > 0) {
1650 /* try reopen the vnode */
1651 error = VOP_OPEN(uvp, unsp->uns_lower_openmode, cred);
1652 if (error)
1653 goto unionfs_advlock_abort;
1654 unsp->uns_upper_opencnt++;
1655 VOP_CLOSE(unp->un_lowervp, unsp->uns_lower_openmode, cred);
1656 unsp->uns_lower_opencnt--;
1657 } else
1658 unionfs_tryrem_node_status(unp, unsp);
1659 }
1660
1661 VOP_UNLOCK(vp);
1662
1663 error = VOP_ADVLOCK(uvp, ap->a_id, ap->a_op, ap->a_fl, ap->a_flags);
1664
1665 UNIONFS_INTERNAL_DEBUG("unionfs_advlock: leave (%d)\n", error);
1666
1667 return error;
1668
1669 unionfs_advlock_abort:
1670 VOP_UNLOCK(vp);
1671
1672 UNIONFS_INTERNAL_DEBUG("unionfs_advlock: leave (%d)\n", error);
1673
1674 return error;
1675 }
1676
1677 static int
1678 unionfs_strategy(void *v)
1679 {
1680 struct vop_strategy_args *ap = v;
1681 struct unionfs_node *unp;
1682 struct vnode *vp;
1683
1684 unp = VTOUNIONFS(ap->a_vp);
1685 vp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp);
1686
1687 #ifdef DIAGNOSTIC
1688 if (vp == NULLVP)
1689 panic("unionfs_strategy: nullvp");
1690 if ((ap->a_bp->b_flags & B_READ) == 0 && vp == unp->un_lowervp)
1691 panic("unionfs_strategy: writing to lowervp");
1692 #endif
1693
1694 return (VOP_STRATEGY(vp, ap->a_bp));
1695 }
1696
1697 static int
1698 unionfs_kqfilter(void *v)
1699 {
1700 struct vop_kqfilter_args *ap = v;
1701 struct unionfs_node *unp;
1702 struct vnode *tvp;
1703
1704 unp = VTOUNIONFS(ap->a_vp);
1705 tvp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp);
1706
1707 return VOP_KQFILTER(tvp, ap->a_kn);
1708 }
1709
1710 static int
1711 unionfs_bmap(void *v)
1712 {
1713 struct vop_bmap_args *ap = v;
1714 struct unionfs_node *unp;
1715 struct vnode *tvp;
1716
1717 unp = VTOUNIONFS(ap->a_vp);
1718 tvp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp);
1719
1720 return VOP_BMAP(tvp, ap->a_bn, ap->a_vpp, ap->a_bnp, ap->a_runp);
1721 }
1722
1723 static int
1724 unionfs_mmap(void *v)
1725 {
1726 struct vop_mmap_args *ap = v;
1727 struct unionfs_node *unp;
1728 struct vnode *tvp;
1729
1730 unp = VTOUNIONFS(ap->a_vp);
1731 tvp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp);
1732
1733 return VOP_MMAP(tvp, ap->a_prot, ap->a_cred);
1734 }
1735
1736 static int
1737 unionfs_abortop(void *v)
1738 {
1739 struct vop_abortop_args *ap = v;
1740 struct unionfs_node *unp;
1741 struct vnode *tvp;
1742
1743 unp = VTOUNIONFS(ap->a_dvp);
1744 tvp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp);
1745
1746 return VOP_ABORTOP(tvp, ap->a_cnp);
1747 }
1748
1749 static int
1750 unionfs_islocked(void *v)
1751 {
1752 struct vop_islocked_args *ap = v;
1753 struct unionfs_node *unp;
1754 struct vnode *tvp;
1755
1756 unp = VTOUNIONFS(ap->a_vp);
1757 tvp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp);
1758
1759 return VOP_ISLOCKED(tvp);
1760 }
1761
1762 static int
1763 unionfs_seek(void *v)
1764 {
1765 struct vop_seek_args *ap = v;
1766 struct unionfs_node *unp;
1767 struct vnode *tvp;
1768
1769 unp = VTOUNIONFS(ap->a_vp);
1770 tvp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp);
1771
1772 return VOP_SEEK(tvp, ap->a_oldoff, ap->a_newoff, ap->a_cred);
1773 }
1774
1775 static int
1776 unionfs_putpages(void *v)
1777 {
1778 struct vop_putpages_args /* {
1779 struct vnode *a_vp;
1780 voff_t a_offlo;
1781 voff_t a_offhi;
1782 int a_flags;
1783 } */ *ap = v;
1784 struct vnode *vp = ap->a_vp, *tvp;
1785 struct unionfs_node *unp;
1786
1787 KASSERT(rw_lock_held(vp->v_uobj.vmobjlock));
1788
1789 unp = VTOUNIONFS(vp);
1790 tvp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp);
1791 KASSERT(tvp->v_uobj.vmobjlock == vp->v_uobj.vmobjlock);
1792
1793 if (ap->a_flags & PGO_RECLAIM) {
1794 rw_exit(vp->v_uobj.vmobjlock);
1795 return 0;
1796 }
1797 return VOP_PUTPAGES(tvp, ap->a_offlo, ap->a_offhi, ap->a_flags);
1798 }
1799
1800 static int
1801 unionfs_getpages(void *v)
1802 {
1803 struct vop_getpages_args /* {
1804 struct vnode *a_vp;
1805 voff_t a_offset;
1806 struct vm_page **a_m;
1807 int *a_count;
1808 int a_centeridx;
1809 vm_prot_t a_access_type;
1810 int a_advice;
1811 int a_flags;
1812 } */ *ap = v;
1813 struct vnode *vp = ap->a_vp, *tvp;
1814 struct unionfs_node *unp;
1815
1816 KASSERT(rw_lock_held(vp->v_uobj.vmobjlock));
1817
1818 unp = VTOUNIONFS(vp);
1819 tvp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp);
1820 KASSERT(tvp->v_uobj.vmobjlock == vp->v_uobj.vmobjlock);
1821
1822 if (ap->a_flags & PGO_LOCKED) {
1823 return EBUSY;
1824 }
1825 return VOP_GETPAGES(tvp, ap->a_offset, ap->a_m, ap->a_count,
1826 ap->a_centeridx, ap->a_access_type, ap->a_advice, ap->a_flags);
1827 }
1828
1829 static int
1830 unionfs_revoke(void *v)
1831 {
1832 struct vop_revoke_args *ap = v;
1833 struct unionfs_node *unp;
1834 struct vnode *tvp;
1835 int error;
1836
1837 unp = VTOUNIONFS(ap->a_vp);
1838 tvp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp);
1839
1840 error = VOP_REVOKE(tvp, ap->a_flags);
1841 if (error == 0) {
1842 vgone(ap->a_vp); /* ??? */
1843 }
1844 return error;
1845 }
1846
1847 /*
1848 * Global vfs data structures
1849 */
1850 int (**unionfs_vnodeop_p)(void *);
1851 const struct vnodeopv_entry_desc unionfs_vnodeop_entries[] = {
1852 { &vop_default_desc, vn_default_error },
1853 { &vop_parsepath_desc, unionfs_parsepath }, /* parsepath */
1854 { &vop_lookup_desc, unionfs_lookup }, /* lookup */
1855 { &vop_create_desc, unionfs_create }, /* create */
1856 { &vop_whiteout_desc, unionfs_whiteout }, /* whiteout */
1857 { &vop_mknod_desc, unionfs_mknod }, /* mknod */
1858 { &vop_open_desc, unionfs_open }, /* open */
1859 { &vop_close_desc, unionfs_close }, /* close */
1860 { &vop_access_desc, unionfs_access }, /* access */
1861 { &vop_accessx_desc, genfs_accessx }, /* accessx */
1862 { &vop_getattr_desc, unionfs_getattr }, /* getattr */
1863 { &vop_setattr_desc, unionfs_setattr }, /* setattr */
1864 { &vop_read_desc, unionfs_read }, /* read */
1865 { &vop_write_desc, unionfs_write }, /* write */
1866 { &vop_fallocate_desc, genfs_eopnotsupp }, /* fallocate */
1867 { &vop_fdiscard_desc, genfs_eopnotsupp }, /* fdiscard */
1868 { &vop_ioctl_desc, unionfs_ioctl }, /* ioctl */
1869 { &vop_poll_desc, unionfs_poll }, /* select */
1870 { &vop_revoke_desc, unionfs_revoke }, /* revoke */
1871 { &vop_mmap_desc, unionfs_mmap }, /* mmap */
1872 { &vop_fsync_desc, unionfs_fsync }, /* fsync */
1873 { &vop_seek_desc, unionfs_seek }, /* seek */
1874 { &vop_remove_desc, unionfs_remove }, /* remove */
1875 { &vop_link_desc, unionfs_link }, /* link */
1876 { &vop_rename_desc, unionfs_rename }, /* rename */
1877 { &vop_mkdir_desc, unionfs_mkdir }, /* mkdir */
1878 { &vop_rmdir_desc, unionfs_rmdir }, /* rmdir */
1879 { &vop_symlink_desc, unionfs_symlink }, /* symlink */
1880 { &vop_readdir_desc, unionfs_readdir }, /* readdir */
1881 { &vop_readlink_desc, unionfs_readlink }, /* readlink */
1882 { &vop_abortop_desc, unionfs_abortop }, /* abortop */
1883 { &vop_inactive_desc, unionfs_inactive }, /* inactive */
1884 { &vop_reclaim_desc, unionfs_reclaim }, /* reclaim */
1885 { &vop_lock_desc, unionfs_lock }, /* lock */
1886 { &vop_unlock_desc, unionfs_unlock }, /* unlock */
1887 { &vop_bmap_desc, unionfs_bmap }, /* bmap */
1888 { &vop_strategy_desc, unionfs_strategy }, /* strategy */
1889 { &vop_print_desc, unionfs_print }, /* print */
1890 { &vop_islocked_desc, unionfs_islocked }, /* islocked */
1891 { &vop_pathconf_desc, unionfs_pathconf }, /* pathconf */
1892 { &vop_advlock_desc, unionfs_advlock }, /* advlock */
1893 { &vop_getpages_desc, unionfs_getpages }, /* getpages */
1894 { &vop_putpages_desc, unionfs_putpages }, /* putpages */
1895 { &vop_kqfilter_desc, unionfs_kqfilter }, /* kqfilter */
1896 #ifdef notdef
1897 { &vop_bwrite_desc, unionfs_bwrite }, /* bwrite */
1898 #endif
1899 { NULL, NULL }
1900 };
1901 const struct vnodeopv_desc unionfs_vnodeop_opv_desc =
1902 { &unionfs_vnodeop_p, unionfs_vnodeop_entries };
1903