2 * Copyright (C) 2005-2016 Junjiro R. Okajima
4 * This program, aufs is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * whiteout for logical deletion and opaque directory
24 #define WH_MASK S_IRUGO
27 * If a directory contains this file, then it is opaque. We start with the
28 * .wh. flag so that it is blocked by lookup.
30 static struct qstr diropq_name
= QSTR_INIT(AUFS_WH_DIROPQ
,
31 sizeof(AUFS_WH_DIROPQ
) - 1);
34 * generate whiteout name, which is NOT terminated by NULL.
35 * @name: original d_name.name
36 * @len: original d_name.len
38 * returns zero when succeeds, otherwise error.
39 * succeeded value as wh->name should be freed by kfree().
41 int au_wh_name_alloc(struct qstr
*wh
, const struct qstr
*name
)
45 if (unlikely(name
->len
> PATH_MAX
- AUFS_WH_PFX_LEN
))
48 wh
->len
= name
->len
+ AUFS_WH_PFX_LEN
;
49 p
= kmalloc(wh
->len
, GFP_NOFS
);
52 memcpy(p
, AUFS_WH_PFX
, AUFS_WH_PFX_LEN
);
53 memcpy(p
+ AUFS_WH_PFX_LEN
, name
->name
, name
->len
);
60 /* ---------------------------------------------------------------------- */
63 * test if the @wh_name exists under @h_parent.
64 * @try_sio specifies the necessary of super-io.
66 int au_wh_test(struct dentry
*h_parent
, struct qstr
*wh_name
, int try_sio
)
69 struct dentry
*wh_dentry
;
72 wh_dentry
= vfsub_lkup_one(wh_name
, h_parent
);
74 wh_dentry
= au_sio_lkup_one(wh_name
, h_parent
);
75 err
= PTR_ERR(wh_dentry
);
76 if (IS_ERR(wh_dentry
)) {
77 if (err
== -ENAMETOOLONG
)
83 if (d_is_negative(wh_dentry
))
84 goto out_wh
; /* success */
87 if (d_is_reg(wh_dentry
))
88 goto out_wh
; /* success */
91 AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
92 wh_dentry
, d_inode(wh_dentry
)->i_mode
);
101 * test if the @h_dentry sets opaque or not.
103 int au_diropq_test(struct dentry
*h_dentry
)
108 h_dir
= d_inode(h_dentry
);
109 err
= au_wh_test(h_dentry
, &diropq_name
,
110 au_test_h_perm_sio(h_dir
, MAY_EXEC
));
115 * returns a negative dentry whose name is unique and temporary.
117 struct dentry
*au_whtmp_lkup(struct dentry
*h_parent
, struct au_branch
*br
,
120 struct dentry
*dentry
;
122 char defname
[NAME_MAX
- AUFS_MAX_NAMELEN
+ DNAME_INLINE_LEN
+ 1],
124 /* strict atomic_t is unnecessary here */
125 static unsigned short cnt
;
128 BUILD_BUG_ON(sizeof(cnt
) * 2 > AUFS_WH_TMP_LEN
);
131 qs
.len
= sizeof(defname
) - DNAME_INLINE_LEN
+ prefix
->len
- 1;
132 if (unlikely(prefix
->len
> DNAME_INLINE_LEN
)) {
133 dentry
= ERR_PTR(-ENAMETOOLONG
);
134 if (unlikely(qs
.len
> NAME_MAX
))
136 dentry
= ERR_PTR(-ENOMEM
);
137 name
= kmalloc(qs
.len
+ 1, GFP_NOFS
);
142 /* doubly whiteout-ed */
143 memcpy(name
, AUFS_WH_PFX AUFS_WH_PFX
, AUFS_WH_PFX_LEN
* 2);
144 p
= name
+ AUFS_WH_PFX_LEN
* 2;
145 memcpy(p
, prefix
->name
, prefix
->len
);
148 AuDebugOn(name
+ qs
.len
+ 1 - p
<= AUFS_WH_TMP_LEN
);
151 for (i
= 0; i
< 3; i
++) {
152 sprintf(p
, "%.*x", AUFS_WH_TMP_LEN
, cnt
++);
153 dentry
= au_sio_lkup_one(&qs
, h_parent
);
154 if (IS_ERR(dentry
) || d_is_negative(dentry
))
158 /* pr_warn("could not get random name\n"); */
159 dentry
= ERR_PTR(-EEXIST
);
160 AuDbg("%.*s\n", AuLNPair(&qs
));
165 au_delayed_kfree(name
);
167 AuTraceErrPtr(dentry
);
172 * rename the @h_dentry on @br to the whiteouted temporary name.
174 int au_whtmp_ren(struct dentry
*h_dentry
, struct au_branch
*br
)
177 struct path h_path
= {
180 struct inode
*h_dir
, *delegated
;
181 struct dentry
*h_parent
;
183 h_parent
= h_dentry
->d_parent
; /* dir inode is locked */
184 h_dir
= d_inode(h_parent
);
187 h_path
.dentry
= au_whtmp_lkup(h_parent
, br
, &h_dentry
->d_name
);
188 err
= PTR_ERR(h_path
.dentry
);
189 if (IS_ERR(h_path
.dentry
))
192 /* under the same dir, no need to lock_rename() */
194 err
= vfsub_rename(h_dir
, h_dentry
, h_dir
, &h_path
, &delegated
);
196 if (unlikely(err
== -EWOULDBLOCK
)) {
197 pr_warn("cannot retry for NFSv4 delegation"
198 " for an internal rename\n");
208 /* ---------------------------------------------------------------------- */
210 * functions for removing a whiteout
213 static int do_unlink_wh(struct inode
*h_dir
, struct path
*h_path
)
216 struct inode
*delegated
;
219 * forces superio when the dir has a sticky bit.
220 * this may be a violation of unix fs semantics.
222 force
= (h_dir
->i_mode
& S_ISVTX
)
223 && !uid_eq(current_fsuid(), d_inode(h_path
->dentry
)->i_uid
);
225 err
= vfsub_unlink(h_dir
, h_path
, &delegated
, force
);
226 if (unlikely(err
== -EWOULDBLOCK
)) {
227 pr_warn("cannot retry for NFSv4 delegation"
228 " for an internal unlink\n");
234 int au_wh_unlink_dentry(struct inode
*h_dir
, struct path
*h_path
,
235 struct dentry
*dentry
)
239 err
= do_unlink_wh(h_dir
, h_path
);
241 au_set_dbwh(dentry
, -1);
246 static int unlink_wh_name(struct dentry
*h_parent
, struct qstr
*wh
,
247 struct au_branch
*br
)
250 struct path h_path
= {
255 h_path
.dentry
= vfsub_lkup_one(wh
, h_parent
);
256 if (IS_ERR(h_path
.dentry
))
257 err
= PTR_ERR(h_path
.dentry
);
259 if (d_is_reg(h_path
.dentry
))
260 err
= do_unlink_wh(d_inode(h_parent
), &h_path
);
267 /* ---------------------------------------------------------------------- */
269 * initialize/clean whiteout for a branch
272 static void au_wh_clean(struct inode
*h_dir
, struct path
*whpath
,
276 struct inode
*delegated
;
278 if (d_is_negative(whpath
->dentry
))
282 err
= vfsub_rmdir(h_dir
, whpath
);
285 err
= vfsub_unlink(h_dir
, whpath
, &delegated
, /*force*/0);
286 if (unlikely(err
== -EWOULDBLOCK
)) {
287 pr_warn("cannot retry for NFSv4 delegation"
288 " for an internal unlink\n");
293 pr_warn("failed removing %pd (%d), ignored.\n",
294 whpath
->dentry
, err
);
297 static int test_linkable(struct dentry
*h_root
)
299 struct inode
*h_dir
= d_inode(h_root
);
301 if (h_dir
->i_op
->link
)
304 pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
305 h_root
, au_sbtype(h_root
->d_sb
));
309 /* todo: should this mkdir be done in /sbin/mount.aufs helper? */
310 static int au_whdir(struct inode
*h_dir
, struct path
*path
)
315 if (d_is_negative(path
->dentry
)) {
318 if (au_test_nfs(path
->dentry
->d_sb
))
320 err
= vfsub_mkdir(h_dir
, path
, mode
);
321 } else if (d_is_dir(path
->dentry
))
324 pr_err("unknown %pd exists\n", path
->dentry
);
330 const struct qstr
*name
;
331 struct dentry
*dentry
;
334 static void au_wh_init_ro(struct inode
*h_dir
, struct au_wh_base base
[],
337 h_path
->dentry
= base
[AuBrWh_BASE
].dentry
;
338 au_wh_clean(h_dir
, h_path
, /*isdir*/0);
339 h_path
->dentry
= base
[AuBrWh_PLINK
].dentry
;
340 au_wh_clean(h_dir
, h_path
, /*isdir*/1);
341 h_path
->dentry
= base
[AuBrWh_ORPH
].dentry
;
342 au_wh_clean(h_dir
, h_path
, /*isdir*/1);
347 * minus: error, caller should print the message
349 * plus: error, caller should NOT print the message
351 static int au_wh_init_rw_nolink(struct dentry
*h_root
, struct au_wbr
*wbr
,
352 int do_plink
, struct au_wh_base base
[],
358 h_dir
= d_inode(h_root
);
359 h_path
->dentry
= base
[AuBrWh_BASE
].dentry
;
360 au_wh_clean(h_dir
, h_path
, /*isdir*/0);
361 h_path
->dentry
= base
[AuBrWh_PLINK
].dentry
;
363 err
= test_linkable(h_root
);
369 err
= au_whdir(h_dir
, h_path
);
372 wbr
->wbr_plink
= dget(base
[AuBrWh_PLINK
].dentry
);
374 au_wh_clean(h_dir
, h_path
, /*isdir*/1);
375 h_path
->dentry
= base
[AuBrWh_ORPH
].dentry
;
376 err
= au_whdir(h_dir
, h_path
);
379 wbr
->wbr_orph
= dget(base
[AuBrWh_ORPH
].dentry
);
386 * for the moment, aufs supports the branch filesystem which does not support
387 * link(2). testing on FAT which does not support i_op->setattr() fully either,
388 * copyup failed. finally, such filesystem will not be used as the writable
391 * returns tri-state, see above.
393 static int au_wh_init_rw(struct dentry
*h_root
, struct au_wbr
*wbr
,
394 int do_plink
, struct au_wh_base base
[],
400 WbrWhMustWriteLock(wbr
);
402 err
= test_linkable(h_root
);
409 * todo: should this create be done in /sbin/mount.aufs helper?
412 h_dir
= d_inode(h_root
);
413 if (d_is_negative(base
[AuBrWh_BASE
].dentry
)) {
414 h_path
->dentry
= base
[AuBrWh_BASE
].dentry
;
415 err
= vfsub_create(h_dir
, h_path
, WH_MASK
, /*want_excl*/true);
416 } else if (d_is_reg(base
[AuBrWh_BASE
].dentry
))
419 pr_err("unknown %pd2 exists\n", base
[AuBrWh_BASE
].dentry
);
423 h_path
->dentry
= base
[AuBrWh_PLINK
].dentry
;
425 err
= au_whdir(h_dir
, h_path
);
428 wbr
->wbr_plink
= dget(base
[AuBrWh_PLINK
].dentry
);
430 au_wh_clean(h_dir
, h_path
, /*isdir*/1);
431 wbr
->wbr_whbase
= dget(base
[AuBrWh_BASE
].dentry
);
433 h_path
->dentry
= base
[AuBrWh_ORPH
].dentry
;
434 err
= au_whdir(h_dir
, h_path
);
437 wbr
->wbr_orph
= dget(base
[AuBrWh_ORPH
].dentry
);
444 * initialize the whiteout base file/dir for @br.
446 int au_wh_init(struct au_branch
*br
, struct super_block
*sb
)
449 const unsigned char do_plink
450 = !!au_opt_test(au_mntflags(sb
), PLINK
);
452 struct path path
= br
->br_path
;
453 struct dentry
*h_root
= path
.dentry
;
454 struct au_wbr
*wbr
= br
->br_wbr
;
455 static const struct qstr base_name
[] = {
456 [AuBrWh_BASE
] = QSTR_INIT(AUFS_BASE_NAME
,
457 sizeof(AUFS_BASE_NAME
) - 1),
458 [AuBrWh_PLINK
] = QSTR_INIT(AUFS_PLINKDIR_NAME
,
459 sizeof(AUFS_PLINKDIR_NAME
) - 1),
460 [AuBrWh_ORPH
] = QSTR_INIT(AUFS_ORPHDIR_NAME
,
461 sizeof(AUFS_ORPHDIR_NAME
) - 1)
463 struct au_wh_base base
[] = {
465 .name
= base_name
+ AuBrWh_BASE
,
469 .name
= base_name
+ AuBrWh_PLINK
,
473 .name
= base_name
+ AuBrWh_ORPH
,
479 WbrWhMustWriteLock(wbr
);
481 for (i
= 0; i
< AuBrWh_Last
; i
++) {
482 /* doubly whiteouted */
485 d
= au_wh_lkup(h_root
, (void *)base
[i
].name
, br
);
493 && wbr
->wbr_wh
[i
] != base
[i
].dentry
);
497 for (i
= 0; i
< AuBrWh_Last
; i
++) {
498 dput(wbr
->wbr_wh
[i
]);
499 wbr
->wbr_wh
[i
] = NULL
;
503 if (!au_br_writable(br
->br_perm
)) {
504 h_dir
= d_inode(h_root
);
505 au_wh_init_ro(h_dir
, base
, &path
);
506 } else if (!au_br_wh_linkable(br
->br_perm
)) {
507 err
= au_wh_init_rw_nolink(h_root
, wbr
, do_plink
, base
, &path
);
513 err
= au_wh_init_rw(h_root
, wbr
, do_plink
, base
, &path
);
519 goto out
; /* success */
522 pr_err("an error(%d) on the writable branch %pd(%s)\n",
523 err
, h_root
, au_sbtype(h_root
->d_sb
));
525 for (i
= 0; i
< AuBrWh_Last
; i
++)
526 dput(base
[i
].dentry
);
530 /* ---------------------------------------------------------------------- */
532 * whiteouts are all hard-linked usually.
533 * when its link count reaches a ceiling, we create a new whiteout base
537 struct reinit_br_wh
{
538 struct super_block
*sb
;
539 struct au_branch
*br
;
542 static void reinit_br_wh(void *arg
)
545 aufs_bindex_t bindex
;
547 struct reinit_br_wh
*a
= arg
;
549 struct inode
*dir
, *delegated
;
550 struct dentry
*h_root
;
551 struct au_hinode
*hdir
;
556 si_noflush_write_lock(a
->sb
);
557 if (!au_br_writable(a
->br
->br_perm
))
559 bindex
= au_br_index(a
->sb
, a
->br
->br_id
);
560 if (unlikely(bindex
< 0))
563 di_read_lock_parent(a
->sb
->s_root
, AuLock_IR
);
564 dir
= d_inode(a
->sb
->s_root
);
565 hdir
= au_hi(dir
, bindex
);
566 h_root
= au_h_dptr(a
->sb
->s_root
, bindex
);
567 AuDebugOn(h_root
!= au_br_dentry(a
->br
));
569 au_hn_inode_lock_nested(hdir
, AuLsc_I_PARENT
);
570 wbr_wh_write_lock(wbr
);
571 err
= au_h_verify(wbr
->wbr_whbase
, au_opt_udba(a
->sb
), hdir
->hi_inode
,
574 h_path
.dentry
= wbr
->wbr_whbase
;
575 h_path
.mnt
= au_br_mnt(a
->br
);
577 err
= vfsub_unlink(hdir
->hi_inode
, &h_path
, &delegated
,
579 if (unlikely(err
== -EWOULDBLOCK
)) {
580 pr_warn("cannot retry for NFSv4 delegation"
581 " for an internal unlink\n");
585 pr_warn("%pd is moved, ignored\n", wbr
->wbr_whbase
);
588 dput(wbr
->wbr_whbase
);
589 wbr
->wbr_whbase
= NULL
;
591 err
= au_wh_init(a
->br
, a
->sb
);
592 wbr_wh_write_unlock(wbr
);
593 au_hn_inode_unlock(hdir
);
594 di_read_unlock(a
->sb
->s_root
, AuLock_IR
);
596 au_fhsm_wrote(a
->sb
, bindex
, /*force*/0);
600 atomic_dec(&wbr
->wbr_wh_running
);
602 si_write_unlock(a
->sb
);
603 au_nwt_done(&au_sbi(a
->sb
)->si_nowait
);
604 au_delayed_kfree(arg
);
606 AuIOErr("err %d\n", err
);
609 static void kick_reinit_br_wh(struct super_block
*sb
, struct au_branch
*br
)
612 struct reinit_br_wh
*arg
;
615 if (atomic_inc_return(&br
->br_wbr
->wbr_wh_running
) != 1)
619 arg
= kmalloc(sizeof(*arg
), GFP_NOFS
);
622 * dec(wh_running), kfree(arg) and dec(br_count)
628 wkq_err
= au_wkq_nowait(reinit_br_wh
, arg
, sb
, /*flags*/0);
629 if (unlikely(wkq_err
)) {
630 atomic_dec(&br
->br_wbr
->wbr_wh_running
);
632 au_delayed_kfree(arg
);
639 atomic_dec(&br
->br_wbr
->wbr_wh_running
);
642 /* ---------------------------------------------------------------------- */
645 * create the whiteout @wh.
647 static int link_or_create_wh(struct super_block
*sb
, aufs_bindex_t bindex
,
651 struct path h_path
= {
654 struct au_branch
*br
;
656 struct dentry
*h_parent
;
657 struct inode
*h_dir
, *delegated
;
659 h_parent
= wh
->d_parent
; /* dir inode is locked */
660 h_dir
= d_inode(h_parent
);
663 br
= au_sbr(sb
, bindex
);
664 h_path
.mnt
= au_br_mnt(br
);
666 wbr_wh_read_lock(wbr
);
667 if (wbr
->wbr_whbase
) {
669 err
= vfsub_link(wbr
->wbr_whbase
, h_dir
, &h_path
, &delegated
);
670 if (unlikely(err
== -EWOULDBLOCK
)) {
671 pr_warn("cannot retry for NFSv4 delegation"
672 " for an internal link\n");
675 if (!err
|| err
!= -EMLINK
)
678 /* link count full. re-initialize br_whbase. */
679 kick_reinit_br_wh(sb
, br
);
682 /* return this error in this context */
683 err
= vfsub_create(h_dir
, &h_path
, WH_MASK
, /*want_excl*/true);
685 au_fhsm_wrote(sb
, bindex
, /*force*/0);
688 wbr_wh_read_unlock(wbr
);
692 /* ---------------------------------------------------------------------- */
695 * create or remove the diropq.
697 static struct dentry
*do_diropq(struct dentry
*dentry
, aufs_bindex_t bindex
,
700 struct dentry
*opq_dentry
, *h_dentry
;
701 struct super_block
*sb
;
702 struct au_branch
*br
;
706 br
= au_sbr(sb
, bindex
);
707 h_dentry
= au_h_dptr(dentry
, bindex
);
708 opq_dentry
= vfsub_lkup_one(&diropq_name
, h_dentry
);
709 if (IS_ERR(opq_dentry
))
712 if (au_ftest_diropq(flags
, CREATE
)) {
713 err
= link_or_create_wh(sb
, bindex
, opq_dentry
);
715 au_set_dbdiropq(dentry
, bindex
);
716 goto out
; /* success */
720 .dentry
= opq_dentry
,
723 err
= do_unlink_wh(au_h_iptr(d_inode(dentry
), bindex
), &tmp
);
725 au_set_dbdiropq(dentry
, -1);
728 opq_dentry
= ERR_PTR(err
);
734 struct do_diropq_args
{
735 struct dentry
**errp
;
736 struct dentry
*dentry
;
737 aufs_bindex_t bindex
;
741 static void call_do_diropq(void *args
)
743 struct do_diropq_args
*a
= args
;
744 *a
->errp
= do_diropq(a
->dentry
, a
->bindex
, a
->flags
);
747 struct dentry
*au_diropq_sio(struct dentry
*dentry
, aufs_bindex_t bindex
,
750 struct dentry
*diropq
, *h_dentry
;
752 h_dentry
= au_h_dptr(dentry
, bindex
);
753 if (!au_test_h_perm_sio(d_inode(h_dentry
), MAY_EXEC
| MAY_WRITE
))
754 diropq
= do_diropq(dentry
, bindex
, flags
);
757 struct do_diropq_args args
= {
764 wkq_err
= au_wkq_wait(call_do_diropq
, &args
);
765 if (unlikely(wkq_err
))
766 diropq
= ERR_PTR(wkq_err
);
772 /* ---------------------------------------------------------------------- */
775 * lookup whiteout dentry.
776 * @h_parent: lower parent dentry which must exist and be locked
777 * @base_name: name of dentry which will be whiteouted
778 * returns dentry for whiteout.
780 struct dentry
*au_wh_lkup(struct dentry
*h_parent
, struct qstr
*base_name
,
781 struct au_branch
*br
)
785 struct dentry
*wh_dentry
;
787 err
= au_wh_name_alloc(&wh_name
, base_name
);
788 wh_dentry
= ERR_PTR(err
);
790 wh_dentry
= vfsub_lkup_one(&wh_name
, h_parent
);
791 au_delayed_kfree(wh_name
.name
);
797 * link/create a whiteout for @dentry on @bindex.
799 struct dentry
*au_wh_create(struct dentry
*dentry
, aufs_bindex_t bindex
,
800 struct dentry
*h_parent
)
802 struct dentry
*wh_dentry
;
803 struct super_block
*sb
;
807 wh_dentry
= au_wh_lkup(h_parent
, &dentry
->d_name
, au_sbr(sb
, bindex
));
808 if (!IS_ERR(wh_dentry
) && d_is_negative(wh_dentry
)) {
809 err
= link_or_create_wh(sb
, bindex
, wh_dentry
);
811 au_set_dbwh(dentry
, bindex
);
812 au_fhsm_wrote(sb
, bindex
, /*force*/0);
815 wh_dentry
= ERR_PTR(err
);
822 /* ---------------------------------------------------------------------- */
824 /* Delete all whiteouts in this directory on branch bindex. */
825 static int del_wh_children(struct dentry
*h_dentry
, struct au_nhash
*whlist
,
826 aufs_bindex_t bindex
, struct au_branch
*br
)
832 struct hlist_head
*head
;
833 struct au_vdir_wh
*pos
;
834 struct au_vdir_destr
*str
;
837 p
= (void *)__get_free_page(GFP_NOFS
);
839 if (unlikely(!wh_name
.name
))
843 memcpy(p
, AUFS_WH_PFX
, AUFS_WH_PFX_LEN
);
844 p
+= AUFS_WH_PFX_LEN
;
846 head
= whlist
->nh_head
;
847 for (ul
= 0; !err
&& ul
< n
; ul
++, head
++) {
848 hlist_for_each_entry(pos
, head
, wh_hash
) {
849 if (pos
->wh_bindex
!= bindex
)
853 if (str
->len
+ AUFS_WH_PFX_LEN
<= PATH_MAX
) {
854 memcpy(p
, str
->name
, str
->len
);
855 wh_name
.len
= AUFS_WH_PFX_LEN
+ str
->len
;
856 err
= unlink_wh_name(h_dentry
, &wh_name
, br
);
861 AuIOErr("whiteout name too long %.*s\n",
862 str
->len
, str
->name
);
867 au_delayed_free_page((unsigned long)wh_name
.name
);
873 struct del_wh_children_args
{
875 struct dentry
*h_dentry
;
876 struct au_nhash
*whlist
;
877 aufs_bindex_t bindex
;
878 struct au_branch
*br
;
881 static void call_del_wh_children(void *args
)
883 struct del_wh_children_args
*a
= args
;
884 *a
->errp
= del_wh_children(a
->h_dentry
, a
->whlist
, a
->bindex
, a
->br
);
887 /* ---------------------------------------------------------------------- */
889 struct au_whtmp_rmdir
*au_whtmp_rmdir_alloc(struct super_block
*sb
, gfp_t gfp
)
891 struct au_whtmp_rmdir
*whtmp
;
897 whtmp
= kzalloc(sizeof(*whtmp
), gfp
);
898 if (unlikely(!whtmp
)) {
899 whtmp
= ERR_PTR(-ENOMEM
);
903 /* no estimation for dir size */
904 rdhash
= au_sbi(sb
)->si_rdhash
;
906 rdhash
= AUFS_RDHASH_DEF
;
907 err
= au_nhash_alloc(&whtmp
->whlist
, rdhash
, gfp
);
909 au_delayed_kfree(whtmp
);
910 whtmp
= ERR_PTR(err
);
917 void au_whtmp_rmdir_free(struct au_whtmp_rmdir
*whtmp
)
920 au_br_put(whtmp
->br
);
921 dput(whtmp
->wh_dentry
);
923 au_nhash_wh_free(&whtmp
->whlist
);
924 au_delayed_kfree(whtmp
);
928 * rmdir the whiteouted temporary named dir @h_dentry.
929 * @whlist: whiteouted children.
931 int au_whtmp_rmdir(struct inode
*dir
, aufs_bindex_t bindex
,
932 struct dentry
*wh_dentry
, struct au_nhash
*whlist
)
935 unsigned int h_nlink
;
937 struct inode
*wh_inode
, *h_dir
;
938 struct au_branch
*br
;
940 h_dir
= d_inode(wh_dentry
->d_parent
); /* dir inode is locked */
943 br
= au_sbr(dir
->i_sb
, bindex
);
944 wh_inode
= d_inode(wh_dentry
);
945 inode_lock_nested(wh_inode
, AuLsc_I_CHILD
);
948 * someone else might change some whiteouts while we were sleeping.
949 * it means this whlist may have an obsoleted entry.
951 if (!au_test_h_perm_sio(wh_inode
, MAY_EXEC
| MAY_WRITE
))
952 err
= del_wh_children(wh_dentry
, whlist
, bindex
, br
);
955 struct del_wh_children_args args
= {
957 .h_dentry
= wh_dentry
,
963 wkq_err
= au_wkq_wait(call_del_wh_children
, &args
);
964 if (unlikely(wkq_err
))
967 inode_unlock(wh_inode
);
970 h_tmp
.dentry
= wh_dentry
;
971 h_tmp
.mnt
= au_br_mnt(br
);
972 h_nlink
= h_dir
->i_nlink
;
973 err
= vfsub_rmdir(h_dir
, &h_tmp
);
974 /* some fs doesn't change the parent nlink in some cases */
975 h_nlink
-= h_dir
->i_nlink
;
979 if (au_ibtop(dir
) == bindex
) {
980 /* todo: dir->i_mutex is necessary */
981 au_cpup_attr_timesizes(dir
);
983 vfsub_drop_nlink(dir
);
985 return 0; /* success */
988 pr_warn("failed removing %pd(%d), ignored\n", wh_dentry
, err
);
992 static void call_rmdir_whtmp(void *args
)
995 aufs_bindex_t bindex
;
996 struct au_whtmp_rmdir
*a
= args
;
997 struct super_block
*sb
;
998 struct dentry
*h_parent
;
1000 struct au_hinode
*hdir
;
1002 /* rmdir by nfsd may cause deadlock with this i_mutex */
1003 /* inode_lock(a->dir); */
1006 si_read_lock(sb
, !AuLock_FLUSH
);
1007 if (!au_br_writable(a
->br
->br_perm
))
1009 bindex
= au_br_index(sb
, a
->br
->br_id
);
1010 if (unlikely(bindex
< 0))
1014 ii_write_lock_parent(a
->dir
);
1015 h_parent
= dget_parent(a
->wh_dentry
);
1016 h_dir
= d_inode(h_parent
);
1017 hdir
= au_hi(a
->dir
, bindex
);
1018 err
= vfsub_mnt_want_write(au_br_mnt(a
->br
));
1021 au_hn_inode_lock_nested(hdir
, AuLsc_I_PARENT
);
1022 err
= au_h_verify(a
->wh_dentry
, au_opt_udba(sb
), h_dir
, h_parent
,
1025 err
= au_whtmp_rmdir(a
->dir
, bindex
, a
->wh_dentry
, &a
->whlist
);
1026 au_hn_inode_unlock(hdir
);
1027 vfsub_mnt_drop_write(au_br_mnt(a
->br
));
1031 ii_write_unlock(a
->dir
);
1033 /* inode_unlock(a->dir); */
1034 au_whtmp_rmdir_free(a
);
1036 au_nwt_done(&au_sbi(sb
)->si_nowait
);
1038 AuIOErr("err %d\n", err
);
1041 void au_whtmp_kick_rmdir(struct inode
*dir
, aufs_bindex_t bindex
,
1042 struct dentry
*wh_dentry
, struct au_whtmp_rmdir
*args
)
1045 struct super_block
*sb
;
1049 /* all post-process will be done in do_rmdir_whtmp(). */
1051 args
->dir
= au_igrab(dir
);
1052 args
->br
= au_sbr(sb
, bindex
);
1053 au_br_get(args
->br
);
1054 args
->wh_dentry
= dget(wh_dentry
);
1055 wkq_err
= au_wkq_nowait(call_rmdir_whtmp
, args
, sb
, /*flags*/0);
1056 if (unlikely(wkq_err
)) {
1057 pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry
, wkq_err
);
1058 au_whtmp_rmdir_free(args
);