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
,
197 if (unlikely(err
== -EWOULDBLOCK
)) {
198 pr_warn("cannot retry for NFSv4 delegation"
199 " for an internal rename\n");
209 /* ---------------------------------------------------------------------- */
211 * functions for removing a whiteout
214 static int do_unlink_wh(struct inode
*h_dir
, struct path
*h_path
)
217 struct inode
*delegated
;
220 * forces superio when the dir has a sticky bit.
221 * this may be a violation of unix fs semantics.
223 force
= (h_dir
->i_mode
& S_ISVTX
)
224 && !uid_eq(current_fsuid(), d_inode(h_path
->dentry
)->i_uid
);
226 err
= vfsub_unlink(h_dir
, h_path
, &delegated
, force
);
227 if (unlikely(err
== -EWOULDBLOCK
)) {
228 pr_warn("cannot retry for NFSv4 delegation"
229 " for an internal unlink\n");
235 int au_wh_unlink_dentry(struct inode
*h_dir
, struct path
*h_path
,
236 struct dentry
*dentry
)
240 err
= do_unlink_wh(h_dir
, h_path
);
242 au_set_dbwh(dentry
, -1);
247 static int unlink_wh_name(struct dentry
*h_parent
, struct qstr
*wh
,
248 struct au_branch
*br
)
251 struct path h_path
= {
256 h_path
.dentry
= vfsub_lkup_one(wh
, h_parent
);
257 if (IS_ERR(h_path
.dentry
))
258 err
= PTR_ERR(h_path
.dentry
);
260 if (d_is_reg(h_path
.dentry
))
261 err
= do_unlink_wh(d_inode(h_parent
), &h_path
);
268 /* ---------------------------------------------------------------------- */
270 * initialize/clean whiteout for a branch
273 static void au_wh_clean(struct inode
*h_dir
, struct path
*whpath
,
277 struct inode
*delegated
;
279 if (d_is_negative(whpath
->dentry
))
283 err
= vfsub_rmdir(h_dir
, whpath
);
286 err
= vfsub_unlink(h_dir
, whpath
, &delegated
, /*force*/0);
287 if (unlikely(err
== -EWOULDBLOCK
)) {
288 pr_warn("cannot retry for NFSv4 delegation"
289 " for an internal unlink\n");
294 pr_warn("failed removing %pd (%d), ignored.\n",
295 whpath
->dentry
, err
);
298 static int test_linkable(struct dentry
*h_root
)
300 struct inode
*h_dir
= d_inode(h_root
);
302 if (h_dir
->i_op
->link
)
305 pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
306 h_root
, au_sbtype(h_root
->d_sb
));
310 /* todo: should this mkdir be done in /sbin/mount.aufs helper? */
311 static int au_whdir(struct inode
*h_dir
, struct path
*path
)
316 if (d_is_negative(path
->dentry
)) {
319 if (au_test_nfs(path
->dentry
->d_sb
))
321 err
= vfsub_mkdir(h_dir
, path
, mode
);
322 } else if (d_is_dir(path
->dentry
))
325 pr_err("unknown %pd exists\n", path
->dentry
);
331 const struct qstr
*name
;
332 struct dentry
*dentry
;
335 static void au_wh_init_ro(struct inode
*h_dir
, struct au_wh_base base
[],
338 h_path
->dentry
= base
[AuBrWh_BASE
].dentry
;
339 au_wh_clean(h_dir
, h_path
, /*isdir*/0);
340 h_path
->dentry
= base
[AuBrWh_PLINK
].dentry
;
341 au_wh_clean(h_dir
, h_path
, /*isdir*/1);
342 h_path
->dentry
= base
[AuBrWh_ORPH
].dentry
;
343 au_wh_clean(h_dir
, h_path
, /*isdir*/1);
348 * minus: error, caller should print the message
350 * plus: error, caller should NOT print the message
352 static int au_wh_init_rw_nolink(struct dentry
*h_root
, struct au_wbr
*wbr
,
353 int do_plink
, struct au_wh_base base
[],
359 h_dir
= d_inode(h_root
);
360 h_path
->dentry
= base
[AuBrWh_BASE
].dentry
;
361 au_wh_clean(h_dir
, h_path
, /*isdir*/0);
362 h_path
->dentry
= base
[AuBrWh_PLINK
].dentry
;
364 err
= test_linkable(h_root
);
370 err
= au_whdir(h_dir
, h_path
);
373 wbr
->wbr_plink
= dget(base
[AuBrWh_PLINK
].dentry
);
375 au_wh_clean(h_dir
, h_path
, /*isdir*/1);
376 h_path
->dentry
= base
[AuBrWh_ORPH
].dentry
;
377 err
= au_whdir(h_dir
, h_path
);
380 wbr
->wbr_orph
= dget(base
[AuBrWh_ORPH
].dentry
);
387 * for the moment, aufs supports the branch filesystem which does not support
388 * link(2). testing on FAT which does not support i_op->setattr() fully either,
389 * copyup failed. finally, such filesystem will not be used as the writable
392 * returns tri-state, see above.
394 static int au_wh_init_rw(struct dentry
*h_root
, struct au_wbr
*wbr
,
395 int do_plink
, struct au_wh_base base
[],
401 WbrWhMustWriteLock(wbr
);
403 err
= test_linkable(h_root
);
410 * todo: should this create be done in /sbin/mount.aufs helper?
413 h_dir
= d_inode(h_root
);
414 if (d_is_negative(base
[AuBrWh_BASE
].dentry
)) {
415 h_path
->dentry
= base
[AuBrWh_BASE
].dentry
;
416 err
= vfsub_create(h_dir
, h_path
, WH_MASK
, /*want_excl*/true);
417 } else if (d_is_reg(base
[AuBrWh_BASE
].dentry
))
420 pr_err("unknown %pd2 exists\n", base
[AuBrWh_BASE
].dentry
);
424 h_path
->dentry
= base
[AuBrWh_PLINK
].dentry
;
426 err
= au_whdir(h_dir
, h_path
);
429 wbr
->wbr_plink
= dget(base
[AuBrWh_PLINK
].dentry
);
431 au_wh_clean(h_dir
, h_path
, /*isdir*/1);
432 wbr
->wbr_whbase
= dget(base
[AuBrWh_BASE
].dentry
);
434 h_path
->dentry
= base
[AuBrWh_ORPH
].dentry
;
435 err
= au_whdir(h_dir
, h_path
);
438 wbr
->wbr_orph
= dget(base
[AuBrWh_ORPH
].dentry
);
445 * initialize the whiteout base file/dir for @br.
447 int au_wh_init(struct au_branch
*br
, struct super_block
*sb
)
450 const unsigned char do_plink
451 = !!au_opt_test(au_mntflags(sb
), PLINK
);
453 struct path path
= br
->br_path
;
454 struct dentry
*h_root
= path
.dentry
;
455 struct au_wbr
*wbr
= br
->br_wbr
;
456 static const struct qstr base_name
[] = {
457 [AuBrWh_BASE
] = QSTR_INIT(AUFS_BASE_NAME
,
458 sizeof(AUFS_BASE_NAME
) - 1),
459 [AuBrWh_PLINK
] = QSTR_INIT(AUFS_PLINKDIR_NAME
,
460 sizeof(AUFS_PLINKDIR_NAME
) - 1),
461 [AuBrWh_ORPH
] = QSTR_INIT(AUFS_ORPHDIR_NAME
,
462 sizeof(AUFS_ORPHDIR_NAME
) - 1)
464 struct au_wh_base base
[] = {
466 .name
= base_name
+ AuBrWh_BASE
,
470 .name
= base_name
+ AuBrWh_PLINK
,
474 .name
= base_name
+ AuBrWh_ORPH
,
480 WbrWhMustWriteLock(wbr
);
482 for (i
= 0; i
< AuBrWh_Last
; i
++) {
483 /* doubly whiteouted */
486 d
= au_wh_lkup(h_root
, (void *)base
[i
].name
, br
);
494 && wbr
->wbr_wh
[i
] != base
[i
].dentry
);
498 for (i
= 0; i
< AuBrWh_Last
; i
++) {
499 dput(wbr
->wbr_wh
[i
]);
500 wbr
->wbr_wh
[i
] = NULL
;
504 if (!au_br_writable(br
->br_perm
)) {
505 h_dir
= d_inode(h_root
);
506 au_wh_init_ro(h_dir
, base
, &path
);
507 } else if (!au_br_wh_linkable(br
->br_perm
)) {
508 err
= au_wh_init_rw_nolink(h_root
, wbr
, do_plink
, base
, &path
);
514 err
= au_wh_init_rw(h_root
, wbr
, do_plink
, base
, &path
);
520 goto out
; /* success */
523 pr_err("an error(%d) on the writable branch %pd(%s)\n",
524 err
, h_root
, au_sbtype(h_root
->d_sb
));
526 for (i
= 0; i
< AuBrWh_Last
; i
++)
527 dput(base
[i
].dentry
);
531 /* ---------------------------------------------------------------------- */
533 * whiteouts are all hard-linked usually.
534 * when its link count reaches a ceiling, we create a new whiteout base
538 struct reinit_br_wh
{
539 struct super_block
*sb
;
540 struct au_branch
*br
;
543 static void reinit_br_wh(void *arg
)
546 aufs_bindex_t bindex
;
548 struct reinit_br_wh
*a
= arg
;
550 struct inode
*dir
, *delegated
;
551 struct dentry
*h_root
;
552 struct au_hinode
*hdir
;
557 si_noflush_write_lock(a
->sb
);
558 if (!au_br_writable(a
->br
->br_perm
))
560 bindex
= au_br_index(a
->sb
, a
->br
->br_id
);
561 if (unlikely(bindex
< 0))
564 di_read_lock_parent(a
->sb
->s_root
, AuLock_IR
);
565 dir
= d_inode(a
->sb
->s_root
);
566 hdir
= au_hi(dir
, bindex
);
567 h_root
= au_h_dptr(a
->sb
->s_root
, bindex
);
568 AuDebugOn(h_root
!= au_br_dentry(a
->br
));
570 au_hn_inode_lock_nested(hdir
, AuLsc_I_PARENT
);
571 wbr_wh_write_lock(wbr
);
572 err
= au_h_verify(wbr
->wbr_whbase
, au_opt_udba(a
->sb
), hdir
->hi_inode
,
575 h_path
.dentry
= wbr
->wbr_whbase
;
576 h_path
.mnt
= au_br_mnt(a
->br
);
578 err
= vfsub_unlink(hdir
->hi_inode
, &h_path
, &delegated
,
580 if (unlikely(err
== -EWOULDBLOCK
)) {
581 pr_warn("cannot retry for NFSv4 delegation"
582 " for an internal unlink\n");
586 pr_warn("%pd is moved, ignored\n", wbr
->wbr_whbase
);
589 dput(wbr
->wbr_whbase
);
590 wbr
->wbr_whbase
= NULL
;
592 err
= au_wh_init(a
->br
, a
->sb
);
593 wbr_wh_write_unlock(wbr
);
594 au_hn_inode_unlock(hdir
);
595 di_read_unlock(a
->sb
->s_root
, AuLock_IR
);
597 au_fhsm_wrote(a
->sb
, bindex
, /*force*/0);
601 atomic_dec(&wbr
->wbr_wh_running
);
603 si_write_unlock(a
->sb
);
604 au_nwt_done(&au_sbi(a
->sb
)->si_nowait
);
605 au_delayed_kfree(arg
);
607 AuIOErr("err %d\n", err
);
610 static void kick_reinit_br_wh(struct super_block
*sb
, struct au_branch
*br
)
613 struct reinit_br_wh
*arg
;
616 if (atomic_inc_return(&br
->br_wbr
->wbr_wh_running
) != 1)
620 arg
= kmalloc(sizeof(*arg
), GFP_NOFS
);
623 * dec(wh_running), kfree(arg) and dec(br_count)
629 wkq_err
= au_wkq_nowait(reinit_br_wh
, arg
, sb
, /*flags*/0);
630 if (unlikely(wkq_err
)) {
631 atomic_dec(&br
->br_wbr
->wbr_wh_running
);
633 au_delayed_kfree(arg
);
640 atomic_dec(&br
->br_wbr
->wbr_wh_running
);
643 /* ---------------------------------------------------------------------- */
646 * create the whiteout @wh.
648 static int link_or_create_wh(struct super_block
*sb
, aufs_bindex_t bindex
,
652 struct path h_path
= {
655 struct au_branch
*br
;
657 struct dentry
*h_parent
;
658 struct inode
*h_dir
, *delegated
;
660 h_parent
= wh
->d_parent
; /* dir inode is locked */
661 h_dir
= d_inode(h_parent
);
664 br
= au_sbr(sb
, bindex
);
665 h_path
.mnt
= au_br_mnt(br
);
667 wbr_wh_read_lock(wbr
);
668 if (wbr
->wbr_whbase
) {
670 err
= vfsub_link(wbr
->wbr_whbase
, h_dir
, &h_path
, &delegated
);
671 if (unlikely(err
== -EWOULDBLOCK
)) {
672 pr_warn("cannot retry for NFSv4 delegation"
673 " for an internal link\n");
676 if (!err
|| err
!= -EMLINK
)
679 /* link count full. re-initialize br_whbase. */
680 kick_reinit_br_wh(sb
, br
);
683 /* return this error in this context */
684 err
= vfsub_create(h_dir
, &h_path
, WH_MASK
, /*want_excl*/true);
686 au_fhsm_wrote(sb
, bindex
, /*force*/0);
689 wbr_wh_read_unlock(wbr
);
693 /* ---------------------------------------------------------------------- */
696 * create or remove the diropq.
698 static struct dentry
*do_diropq(struct dentry
*dentry
, aufs_bindex_t bindex
,
701 struct dentry
*opq_dentry
, *h_dentry
;
702 struct super_block
*sb
;
703 struct au_branch
*br
;
707 br
= au_sbr(sb
, bindex
);
708 h_dentry
= au_h_dptr(dentry
, bindex
);
709 opq_dentry
= vfsub_lkup_one(&diropq_name
, h_dentry
);
710 if (IS_ERR(opq_dentry
))
713 if (au_ftest_diropq(flags
, CREATE
)) {
714 err
= link_or_create_wh(sb
, bindex
, opq_dentry
);
716 au_set_dbdiropq(dentry
, bindex
);
717 goto out
; /* success */
721 .dentry
= opq_dentry
,
724 err
= do_unlink_wh(au_h_iptr(d_inode(dentry
), bindex
), &tmp
);
726 au_set_dbdiropq(dentry
, -1);
729 opq_dentry
= ERR_PTR(err
);
735 struct do_diropq_args
{
736 struct dentry
**errp
;
737 struct dentry
*dentry
;
738 aufs_bindex_t bindex
;
742 static void call_do_diropq(void *args
)
744 struct do_diropq_args
*a
= args
;
745 *a
->errp
= do_diropq(a
->dentry
, a
->bindex
, a
->flags
);
748 struct dentry
*au_diropq_sio(struct dentry
*dentry
, aufs_bindex_t bindex
,
751 struct dentry
*diropq
, *h_dentry
;
753 h_dentry
= au_h_dptr(dentry
, bindex
);
754 if (!au_test_h_perm_sio(d_inode(h_dentry
), MAY_EXEC
| MAY_WRITE
))
755 diropq
= do_diropq(dentry
, bindex
, flags
);
758 struct do_diropq_args args
= {
765 wkq_err
= au_wkq_wait(call_do_diropq
, &args
);
766 if (unlikely(wkq_err
))
767 diropq
= ERR_PTR(wkq_err
);
773 /* ---------------------------------------------------------------------- */
776 * lookup whiteout dentry.
777 * @h_parent: lower parent dentry which must exist and be locked
778 * @base_name: name of dentry which will be whiteouted
779 * returns dentry for whiteout.
781 struct dentry
*au_wh_lkup(struct dentry
*h_parent
, struct qstr
*base_name
,
782 struct au_branch
*br
)
786 struct dentry
*wh_dentry
;
788 err
= au_wh_name_alloc(&wh_name
, base_name
);
789 wh_dentry
= ERR_PTR(err
);
791 wh_dentry
= vfsub_lkup_one(&wh_name
, h_parent
);
792 au_delayed_kfree(wh_name
.name
);
798 * link/create a whiteout for @dentry on @bindex.
800 struct dentry
*au_wh_create(struct dentry
*dentry
, aufs_bindex_t bindex
,
801 struct dentry
*h_parent
)
803 struct dentry
*wh_dentry
;
804 struct super_block
*sb
;
808 wh_dentry
= au_wh_lkup(h_parent
, &dentry
->d_name
, au_sbr(sb
, bindex
));
809 if (!IS_ERR(wh_dentry
) && d_is_negative(wh_dentry
)) {
810 err
= link_or_create_wh(sb
, bindex
, wh_dentry
);
812 au_set_dbwh(dentry
, bindex
);
813 au_fhsm_wrote(sb
, bindex
, /*force*/0);
816 wh_dentry
= ERR_PTR(err
);
823 /* ---------------------------------------------------------------------- */
825 /* Delete all whiteouts in this directory on branch bindex. */
826 static int del_wh_children(struct dentry
*h_dentry
, struct au_nhash
*whlist
,
827 aufs_bindex_t bindex
, struct au_branch
*br
)
833 struct hlist_head
*head
;
834 struct au_vdir_wh
*pos
;
835 struct au_vdir_destr
*str
;
838 p
= (void *)__get_free_page(GFP_NOFS
);
840 if (unlikely(!wh_name
.name
))
844 memcpy(p
, AUFS_WH_PFX
, AUFS_WH_PFX_LEN
);
845 p
+= AUFS_WH_PFX_LEN
;
847 head
= whlist
->nh_head
;
848 for (ul
= 0; !err
&& ul
< n
; ul
++, head
++) {
849 hlist_for_each_entry(pos
, head
, wh_hash
) {
850 if (pos
->wh_bindex
!= bindex
)
854 if (str
->len
+ AUFS_WH_PFX_LEN
<= PATH_MAX
) {
855 memcpy(p
, str
->name
, str
->len
);
856 wh_name
.len
= AUFS_WH_PFX_LEN
+ str
->len
;
857 err
= unlink_wh_name(h_dentry
, &wh_name
, br
);
862 AuIOErr("whiteout name too long %.*s\n",
863 str
->len
, str
->name
);
868 au_delayed_free_page((unsigned long)wh_name
.name
);
874 struct del_wh_children_args
{
876 struct dentry
*h_dentry
;
877 struct au_nhash
*whlist
;
878 aufs_bindex_t bindex
;
879 struct au_branch
*br
;
882 static void call_del_wh_children(void *args
)
884 struct del_wh_children_args
*a
= args
;
885 *a
->errp
= del_wh_children(a
->h_dentry
, a
->whlist
, a
->bindex
, a
->br
);
888 /* ---------------------------------------------------------------------- */
890 struct au_whtmp_rmdir
*au_whtmp_rmdir_alloc(struct super_block
*sb
, gfp_t gfp
)
892 struct au_whtmp_rmdir
*whtmp
;
898 whtmp
= kzalloc(sizeof(*whtmp
), gfp
);
899 if (unlikely(!whtmp
)) {
900 whtmp
= ERR_PTR(-ENOMEM
);
904 /* no estimation for dir size */
905 rdhash
= au_sbi(sb
)->si_rdhash
;
907 rdhash
= AUFS_RDHASH_DEF
;
908 err
= au_nhash_alloc(&whtmp
->whlist
, rdhash
, gfp
);
910 au_delayed_kfree(whtmp
);
911 whtmp
= ERR_PTR(err
);
918 void au_whtmp_rmdir_free(struct au_whtmp_rmdir
*whtmp
)
921 au_br_put(whtmp
->br
);
922 dput(whtmp
->wh_dentry
);
924 au_nhash_wh_free(&whtmp
->whlist
);
925 au_delayed_kfree(whtmp
);
929 * rmdir the whiteouted temporary named dir @h_dentry.
930 * @whlist: whiteouted children.
932 int au_whtmp_rmdir(struct inode
*dir
, aufs_bindex_t bindex
,
933 struct dentry
*wh_dentry
, struct au_nhash
*whlist
)
936 unsigned int h_nlink
;
938 struct inode
*wh_inode
, *h_dir
;
939 struct au_branch
*br
;
941 h_dir
= d_inode(wh_dentry
->d_parent
); /* dir inode is locked */
944 br
= au_sbr(dir
->i_sb
, bindex
);
945 wh_inode
= d_inode(wh_dentry
);
946 inode_lock_nested(wh_inode
, AuLsc_I_CHILD
);
949 * someone else might change some whiteouts while we were sleeping.
950 * it means this whlist may have an obsoleted entry.
952 if (!au_test_h_perm_sio(wh_inode
, MAY_EXEC
| MAY_WRITE
))
953 err
= del_wh_children(wh_dentry
, whlist
, bindex
, br
);
956 struct del_wh_children_args args
= {
958 .h_dentry
= wh_dentry
,
964 wkq_err
= au_wkq_wait(call_del_wh_children
, &args
);
965 if (unlikely(wkq_err
))
968 inode_unlock(wh_inode
);
971 h_tmp
.dentry
= wh_dentry
;
972 h_tmp
.mnt
= au_br_mnt(br
);
973 h_nlink
= h_dir
->i_nlink
;
974 err
= vfsub_rmdir(h_dir
, &h_tmp
);
975 /* some fs doesn't change the parent nlink in some cases */
976 h_nlink
-= h_dir
->i_nlink
;
980 if (au_ibtop(dir
) == bindex
) {
981 /* todo: dir->i_mutex is necessary */
982 au_cpup_attr_timesizes(dir
);
984 vfsub_drop_nlink(dir
);
986 return 0; /* success */
989 pr_warn("failed removing %pd(%d), ignored\n", wh_dentry
, err
);
993 static void call_rmdir_whtmp(void *args
)
996 aufs_bindex_t bindex
;
997 struct au_whtmp_rmdir
*a
= args
;
998 struct super_block
*sb
;
999 struct dentry
*h_parent
;
1000 struct inode
*h_dir
;
1001 struct au_hinode
*hdir
;
1003 /* rmdir by nfsd may cause deadlock with this i_mutex */
1004 /* inode_lock(a->dir); */
1007 si_read_lock(sb
, !AuLock_FLUSH
);
1008 if (!au_br_writable(a
->br
->br_perm
))
1010 bindex
= au_br_index(sb
, a
->br
->br_id
);
1011 if (unlikely(bindex
< 0))
1015 ii_write_lock_parent(a
->dir
);
1016 h_parent
= dget_parent(a
->wh_dentry
);
1017 h_dir
= d_inode(h_parent
);
1018 hdir
= au_hi(a
->dir
, bindex
);
1019 err
= vfsub_mnt_want_write(au_br_mnt(a
->br
));
1022 au_hn_inode_lock_nested(hdir
, AuLsc_I_PARENT
);
1023 err
= au_h_verify(a
->wh_dentry
, au_opt_udba(sb
), h_dir
, h_parent
,
1026 err
= au_whtmp_rmdir(a
->dir
, bindex
, a
->wh_dentry
, &a
->whlist
);
1027 au_hn_inode_unlock(hdir
);
1028 vfsub_mnt_drop_write(au_br_mnt(a
->br
));
1032 ii_write_unlock(a
->dir
);
1034 /* inode_unlock(a->dir); */
1035 au_whtmp_rmdir_free(a
);
1037 au_nwt_done(&au_sbi(sb
)->si_nowait
);
1039 AuIOErr("err %d\n", err
);
1042 void au_whtmp_kick_rmdir(struct inode
*dir
, aufs_bindex_t bindex
,
1043 struct dentry
*wh_dentry
, struct au_whtmp_rmdir
*args
)
1046 struct super_block
*sb
;
1050 /* all post-process will be done in do_rmdir_whtmp(). */
1052 args
->dir
= au_igrab(dir
);
1053 args
->br
= au_sbr(sb
, bindex
);
1054 au_br_get(args
->br
);
1055 args
->wh_dentry
= dget(wh_dentry
);
1056 wkq_err
= au_wkq_nowait(call_rmdir_whtmp
, args
, sb
, /*flags*/0);
1057 if (unlikely(wkq_err
)) {
1058 pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry
, wkq_err
);
1059 au_whtmp_rmdir_free(args
);