2 * Copyright (C) 2017 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 * special handling in renaming a directoy
20 * in order to support looking-up the before-renamed name on the lower readonly
24 #include <linux/byteorder/generic.h>
27 static void au_dr_hino_del(struct au_dr_br
*dr
, struct au_dr_hino
*ent
)
31 idx
= au_dr_ihash(ent
->dr_h_ino
);
32 au_hbl_del(&ent
->dr_hnode
, dr
->dr_h_ino
+ idx
);
35 static int au_dr_hino_test_empty(struct au_dr_br
*dr
)
38 struct hlist_bl_head
*hbl
;
41 for (i
= 0; ret
&& i
< AuDirren_NHASH
; i
++) {
42 hbl
= dr
->dr_h_ino
+ i
;
44 ret
&= hlist_bl_empty(hbl
);
51 static struct au_dr_hino
*au_dr_hino_find(struct au_dr_br
*dr
, ino_t ino
)
53 struct au_dr_hino
*found
, *ent
;
54 struct hlist_bl_head
*hbl
;
55 struct hlist_bl_node
*pos
;
59 idx
= au_dr_ihash(ino
);
60 hbl
= dr
->dr_h_ino
+ idx
;
62 hlist_bl_for_each_entry(ent
, pos
, hbl
, dr_hnode
)
63 if (ent
->dr_h_ino
== ino
) {
72 int au_dr_hino_test_add(struct au_dr_br
*dr
, ino_t ino
,
73 struct au_dr_hino
*add_ent
)
76 struct hlist_bl_head
*hbl
;
77 struct hlist_bl_node
*pos
;
78 struct au_dr_hino
*ent
;
81 idx
= au_dr_ihash(ino
);
82 hbl
= dr
->dr_h_ino
+ idx
;
85 struct hlist_bl_node
*tmp
;
87 hlist_bl_for_each_entry_safe(ent
, pos
, tmp
, hbl
, dr_hnode
)
88 AuDbg("hi%llu\n", (unsigned long long)ent
->dr_h_ino
);
92 hlist_bl_for_each_entry(ent
, pos
, hbl
, dr_hnode
)
93 if (ent
->dr_h_ino
== ino
) {
97 if (!found
&& add_ent
)
98 hlist_bl_add_head(&add_ent
->dr_hnode
, hbl
);
101 if (!found
&& add_ent
)
102 AuDbg("i%llu added\n", (unsigned long long)add_ent
->dr_h_ino
);
107 void au_dr_hino_free(struct au_dr_br
*dr
)
110 struct hlist_bl_head
*hbl
;
111 struct hlist_bl_node
*pos
, *tmp
;
112 struct au_dr_hino
*ent
;
114 /* SiMustWriteLock(sb); */
116 for (i
= 0; i
< AuDirren_NHASH
; i
++) {
117 hbl
= dr
->dr_h_ino
+ i
;
118 /* no spinlock since sbinfo must be write-locked */
119 hlist_bl_for_each_entry_safe(ent
, pos
, tmp
, hbl
, dr_hnode
)
121 INIT_HLIST_BL_HEAD(hbl
);
125 /* returns the number of inodes or an error */
126 static int au_dr_hino_store(struct super_block
*sb
, struct au_branch
*br
,
127 struct file
*hinofile
)
133 struct inode
*hinoinode
;
134 struct hlist_bl_head
*hbl
;
135 struct hlist_bl_node
*n1
, *n2
;
136 struct au_dr_hino
*ent
;
139 AuDebugOn(!au_br_writable(br
->br_perm
));
141 hinoinode
= file_inode(hinofile
);
142 oldsize
= i_size_read(hinoinode
);
146 hbl
= br
->br_dirren
.dr_h_ino
;
147 for (i
= 0; !err
&& i
< AuDirren_NHASH
; i
++, hbl
++) {
148 /* no bit-lock since sbinfo must be write-locked */
149 hlist_bl_for_each_entry_safe(ent
, n1
, n2
, hbl
, dr_hnode
) {
150 AuDbg("hi%llu, %pD2\n",
151 (unsigned long long)ent
->dr_h_ino
, hinofile
);
152 u64
= cpu_to_be64(ent
->dr_h_ino
);
153 ssz
= vfsub_write_k(hinofile
, &u64
, sizeof(u64
), &pos
);
154 if (ssz
== sizeof(u64
))
158 pr_err("ssz %zd, %pD2\n", ssz
, hinofile
);
165 /* regardless the error */
167 err
= vfsub_trunc(&hinofile
->f_path
, pos
, /*attr*/0, hinofile
);
175 static int au_dr_hino_load(struct au_dr_br
*dr
, struct file
*hinofile
)
182 struct au_dr_hino
*ent
;
183 struct inode
*hinoinode
;
184 struct hlist_bl_head
*hbl
;
189 hinoinode
= file_inode(hinofile
);
190 sz
= i_size_read(hinoinode
);
191 AuDebugOn(sz
% sizeof(u64
));
192 n
= sz
/ sizeof(u64
);
194 ssz
= vfsub_read_k(hinofile
, &u64
, sizeof(u64
), &pos
);
195 if (unlikely(ssz
!= sizeof(u64
))) {
196 pr_err("ssz %zd, %pD2\n", ssz
, hinofile
);
203 ent
= kmalloc(sizeof(*ent
), GFP_NOFS
);
209 ent
->dr_h_ino
= be64_to_cpu((__force __be64
)u64
);
210 AuDbg("hi%llu, %pD2\n",
211 (unsigned long long)ent
->dr_h_ino
, hinofile
);
212 hidx
= au_dr_ihash(ent
->dr_h_ino
);
213 au_hbl_add(&ent
->dr_hnode
, hbl
+ hidx
);
215 goto out
; /* success */
225 * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
226 * @path is a switch to distinguish load and store.
228 static int au_dr_hino(struct super_block
*sb
, aufs_bindex_t bindex
,
229 struct au_branch
*br
, const struct path
*path
)
232 unsigned char load
, suspend
;
233 struct file
*hinofile
;
234 struct au_hinode
*hdir
;
235 struct inode
*dir
, *delegated
;
236 struct path hinopath
;
237 struct qstr hinoname
= QSTR_INIT(AUFS_WH_DR_BRHINO
,
238 sizeof(AUFS_WH_DR_BRHINO
) - 1);
240 AuDebugOn(bindex
< 0 && !br
);
241 AuDebugOn(bindex
>= 0 && br
);
246 br
= au_sbr(sb
, bindex
);
250 AuDebugOn(!au_br_writable(br
->br_perm
));
251 if (unlikely(!au_br_writable(br
->br_perm
)))
257 dir
= d_inode(sb
->s_root
);
258 hdir
= au_hinode(au_ii(dir
), bindex
);
259 dir
= hdir
->hi_inode
;
260 au_hn_inode_lock_nested(hdir
, AuLsc_I_CHILD
);
262 dir
= d_inode(path
->dentry
);
263 inode_lock_nested(dir
, AuLsc_I_CHILD
);
265 hinopath
.dentry
= vfsub_lkup_one(&hinoname
, path
->dentry
);
266 err
= PTR_ERR(hinopath
.dentry
);
267 if (IS_ERR(hinopath
.dentry
))
273 if (d_is_negative(hinopath
.dentry
))
274 goto out_dput
; /* success */
276 if (au_dr_hino_test_empty(&br
->br_dirren
)) {
277 if (d_is_positive(hinopath
.dentry
)) {
279 err
= vfsub_unlink(dir
, &hinopath
, &delegated
,
283 pr_err("ignored err %d, %pd2\n",
284 err
, hinopath
.dentry
);
285 if (unlikely(err
== -EWOULDBLOCK
))
290 } else if (!d_is_positive(hinopath
.dentry
)) {
291 err
= vfsub_create(dir
, &hinopath
, 0600,
299 hinopath
.mnt
= path
->mnt
;
300 hinofile
= vfsub_dentry_open(&hinopath
, flags
);
302 au_hn_inode_unlock(hdir
);
305 dput(hinopath
.dentry
);
306 AuTraceErrPtr(hinofile
);
307 if (IS_ERR(hinofile
)) {
308 err
= PTR_ERR(hinofile
);
313 err
= au_dr_hino_load(&br
->br_dirren
, hinofile
);
315 err
= au_dr_hino_store(sb
, br
, hinofile
);
320 dput(hinopath
.dentry
);
323 au_hn_inode_unlock(hdir
);
331 /* ---------------------------------------------------------------------- */
333 static int au_dr_brid_init(struct au_dr_brid
*brid
, const struct path
*path
)
336 struct kstatfs kstfs
;
338 struct dentry
*dentry
;
339 struct super_block
*sb
;
341 err
= vfs_statfs((void *)path
, &kstfs
);
346 /* todo: support for UUID */
348 if (kstfs
.f_fsid
.val
[0] || kstfs
.f_fsid
.val
[1]) {
349 brid
->type
= AuBrid_FSID
;
350 brid
->fsid
= kstfs
.f_fsid
;
352 dentry
= path
->dentry
;
356 brid
->type
= AuBrid_DEV
;
365 int au_dr_br_init(struct super_block
*sb
, struct au_branch
*br
,
366 const struct path
*path
)
370 struct hlist_bl_head
*hbl
;
374 for (i
= 0; i
< AuDirren_NHASH
; i
++, hbl
++)
375 INIT_HLIST_BL_HEAD(hbl
);
377 err
= au_dr_brid_init(&dr
->dr_brid
, path
);
381 if (au_opt_test(au_mntflags(sb
), DIRREN
))
382 err
= au_dr_hino(sb
, /*bindex*/-1, br
, path
);
389 int au_dr_br_fin(struct super_block
*sb
, struct au_branch
*br
)
394 if (au_br_writable(br
->br_perm
))
395 err
= au_dr_hino(sb
, /*bindex*/-1, br
, /*path*/NULL
);
397 au_dr_hino_free(&br
->br_dirren
);
402 /* ---------------------------------------------------------------------- */
404 static int au_brid_str(struct au_dr_brid
*brid
, struct inode
*h_inode
,
405 char *buf
, size_t sz
)
408 unsigned int major
, minor
;
412 err
= snprintf(p
, sz
, "%d_", brid
->type
);
416 switch (brid
->type
) {
420 err
= snprintf(p
, sz
, "%pU", brid
->uuid
.b
);
423 err
= snprintf(p
, sz
, "%08x-%08x",
424 brid
->fsid
.val
[0], brid
->fsid
.val
[1]);
427 major
= MAJOR(brid
->dev
);
428 minor
= MINOR(brid
->dev
);
429 if (major
<= 0xff && minor
<= 0xff)
430 err
= snprintf(p
, sz
, "%02x%02x", major
, minor
);
432 err
= snprintf(p
, sz
, "%03x:%05x", major
, minor
);
438 err
= snprintf(p
, sz
, "_%llu", (unsigned long long)h_inode
->i_ino
);
446 static int au_drinfo_name(struct au_branch
*br
, char *name
, int len
)
449 struct dentry
*br_dentry
;
450 struct inode
*br_inode
;
452 br_dentry
= au_br_dentry(br
);
453 br_inode
= d_inode(br_dentry
);
454 rlen
= au_brid_str(&br
->br_dirren
.dr_brid
, br_inode
, name
, len
);
455 AuDebugOn(rlen
>= AUFS_DIRREN_ENV_VAL_SZ
);
456 AuDebugOn(rlen
> len
);
461 /* ---------------------------------------------------------------------- */
464 * from the given @h_dentry, construct drinfo at @*fdata.
465 * when the size of @*fdata is not enough, reallocate and return new @fdata and
468 static int au_drinfo_construct(struct au_drinfo_fdata
**fdata
,
469 struct dentry
*h_dentry
,
470 unsigned char *allocated
)
473 struct au_drinfo_fdata
*f
, *p
;
474 struct au_drinfo
*drinfo
;
475 struct inode
*h_inode
;
480 h_inode
= d_inode(h_dentry
);
481 qname
= &h_dentry
->d_name
;
483 drinfo
->ino
= (__force
uint64_t)cpu_to_be64(h_inode
->i_ino
);
484 drinfo
->oldnamelen
= qname
->len
;
485 if (*allocated
< sizeof(*f
) + qname
->len
) {
486 v
= roundup_pow_of_two(*allocated
+ qname
->len
);
487 p
= au_krealloc(f
, v
, GFP_NOFS
, /*may_shrink*/0);
498 memcpy(drinfo
->oldname
, qname
->name
, qname
->len
);
499 AuDbg("i%llu, %.*s\n",
500 be64_to_cpu((__force __be64
)drinfo
->ino
), drinfo
->oldnamelen
,
508 /* callers have to free the return value */
509 static struct au_drinfo
*au_drinfo_read_k(struct file
*file
, ino_t h_ino
)
511 struct au_drinfo
*ret
, *drinfo
;
512 struct au_drinfo_fdata fdata
;
519 ssz
= vfsub_read_k(file
, &fdata
, sizeof(fdata
), &pos
);
520 if (unlikely(ssz
!= sizeof(fdata
))) {
521 AuIOErr("ssz %zd, %u, %pD2\n",
522 ssz
, (unsigned int)sizeof(fdata
), file
);
526 fdata
.magic
= ntohl((__force __be32
)fdata
.magic
);
527 switch (fdata
.magic
) {
528 case AUFS_DRINFO_MAGIC_V1
:
531 AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
532 fdata
.magic
, AUFS_DRINFO_MAGIC_V1
, file
);
536 drinfo
= &fdata
.drinfo
;
537 len
= drinfo
->oldnamelen
;
539 AuIOErr("broken drinfo %pD2\n", file
);
544 drinfo
->ino
= be64_to_cpu((__force __be64
)drinfo
->ino
);
545 if (unlikely(h_ino
&& drinfo
->ino
!= h_ino
)) {
546 AuDbg("ignored i%llu, i%llu, %pD2\n",
547 (unsigned long long)drinfo
->ino
,
548 (unsigned long long)h_ino
, file
);
549 goto out
; /* success */
552 ret
= kmalloc(sizeof(*ret
) + len
, GFP_NOFS
);
553 if (unlikely(!ret
)) {
554 ret
= ERR_PTR(-ENOMEM
);
560 ssz
= vfsub_read_k(file
, (void *)ret
->oldname
, len
, &pos
);
561 if (unlikely(ssz
!= len
)) {
564 AuIOErr("ssz %zd, %u, %pD2\n", ssz
, len
, file
);
568 AuDbg("oldname %.*s\n", ret
->oldnamelen
, ret
->oldname
);
574 /* ---------------------------------------------------------------------- */
576 /* in order to be revertible */
577 struct au_drinfo_rev_elm
{
579 struct dentry
*info_dentry
;
580 struct au_drinfo
*info_last
;
583 struct au_drinfo_rev
{
584 unsigned char already
;
586 struct au_drinfo_rev_elm elm
[0];
589 /* todo: isn't it too large? */
590 struct au_drinfo_store
{
592 struct dentry
*h_dentry
;
593 struct au_drinfo_fdata
*fdata
;
594 char *infoname
; /* inside of whname, just after PFX */
595 char whname
[sizeof(AUFS_WH_DR_INFO_PFX
) + AUFS_DIRREN_ENV_VAL_SZ
];
596 aufs_bindex_t btgt
, btail
;
597 unsigned char no_sio
,
598 allocated
, /* current size of *fdata */
599 infonamelen
, /* room size for p */
600 whnamelen
, /* length of the genarated name */
601 renameback
; /* renamed back */
604 /* on rename(2) error, the caller should revert it using @elm */
605 static int au_drinfo_do_store(struct au_drinfo_store
*w
,
606 struct au_drinfo_rev_elm
*elm
)
611 struct path infopath
= {
612 .mnt
= w
->h_ppath
.mnt
614 struct inode
*h_dir
, *h_inode
, *delegated
;
615 struct file
*infofile
;
619 && memcmp(elm
, page_address(ZERO_PAGE(0)), sizeof(*elm
)));
621 infopath
.dentry
= vfsub_lookup_one_len(w
->whname
, w
->h_ppath
.dentry
,
623 AuTraceErrPtr(infopath
.dentry
);
624 if (IS_ERR(infopath
.dentry
)) {
625 err
= PTR_ERR(infopath
.dentry
);
630 h_dir
= d_inode(w
->h_ppath
.dentry
);
631 if (elm
&& d_is_negative(infopath
.dentry
)) {
632 err
= vfsub_create(h_dir
, &infopath
, 0600, /*want_excl*/true);
637 elm
->info_dentry
= dget(infopath
.dentry
);
640 infofile
= vfsub_dentry_open(&infopath
, O_RDWR
);
641 AuTraceErrPtr(infofile
);
642 if (IS_ERR(infofile
)) {
643 err
= PTR_ERR(infofile
);
647 h_inode
= d_inode(infopath
.dentry
);
648 if (elm
&& i_size_read(h_inode
)) {
649 h_inode
= d_inode(w
->h_dentry
);
650 elm
->info_last
= au_drinfo_read_k(infofile
, h_inode
->i_ino
);
651 AuTraceErrPtr(elm
->info_last
);
652 if (IS_ERR(elm
->info_last
)) {
653 err
= PTR_ERR(elm
->info_last
);
654 elm
->info_last
= NULL
;
655 AuDebugOn(elm
->info_dentry
);
660 if (elm
&& w
->renameback
) {
662 err
= vfsub_unlink(h_dir
, &infopath
, &delegated
, /*force*/0);
664 if (unlikely(err
== -EWOULDBLOCK
))
670 qname
= &w
->h_dentry
->d_name
;
671 len
= sizeof(*w
->fdata
) + qname
->len
;
673 len
= sizeof(*w
->fdata
) + w
->fdata
->drinfo
.oldnamelen
;
674 ssz
= vfsub_write_k(infofile
, w
->fdata
, len
, &pos
);
676 AuDbg("hi%llu, %.*s\n", w
->fdata
->drinfo
.ino
,
677 w
->fdata
->drinfo
.oldnamelen
, w
->fdata
->drinfo
.oldname
);
678 goto out_fput
; /* success */
683 /* the caller should revert it using @elm */
689 dput(infopath
.dentry
);
695 struct au_call_drinfo_do_store_args
{
697 struct au_drinfo_store
*w
;
698 struct au_drinfo_rev_elm
*elm
;
701 static void au_call_drinfo_do_store(void *args
)
703 struct au_call_drinfo_do_store_args
*a
= args
;
705 *a
->errp
= au_drinfo_do_store(a
->w
, a
->elm
);
708 static int au_drinfo_store_sio(struct au_drinfo_store
*w
,
709 struct au_drinfo_rev_elm
*elm
)
714 err
= au_drinfo_do_store(w
, elm
);
716 struct au_call_drinfo_do_store_args a
= {
721 wkq_err
= au_wkq_wait(au_call_drinfo_do_store
, &a
);
722 if (unlikely(wkq_err
))
730 static int au_drinfo_store_work_init(struct au_drinfo_store
*w
,
735 memset(w
, 0, sizeof(*w
));
736 w
->allocated
= roundup_pow_of_two(sizeof(*w
->fdata
) + 40);
737 strcpy(w
->whname
, AUFS_WH_DR_INFO_PFX
);
738 w
->infoname
= w
->whname
+ sizeof(AUFS_WH_DR_INFO_PFX
) - 1;
739 w
->infonamelen
= sizeof(w
->whname
) - sizeof(AUFS_WH_DR_INFO_PFX
);
741 w
->no_sio
= !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID
);
744 w
->fdata
= kcalloc(1, w
->allocated
, GFP_NOFS
);
745 if (unlikely(!w
->fdata
)) {
749 w
->fdata
->magic
= (__force
uint32_t)htonl(AUFS_DRINFO_MAGIC_V1
);
756 static void au_drinfo_store_work_fin(struct au_drinfo_store
*w
)
761 static void au_drinfo_store_rev(struct au_drinfo_rev
*rev
,
762 struct au_drinfo_store
*w
)
764 struct au_drinfo_rev_elm
*elm
;
765 struct inode
*h_dir
, *delegated
;
767 struct path infopath
= {
768 .mnt
= w
->h_ppath
.mnt
771 h_dir
= d_inode(w
->h_ppath
.dentry
);
776 for (nelm
= rev
->nelm
; nelm
> 0; nelm
--, elm
++) {
777 AuDebugOn(elm
->created
&& elm
->info_last
);
781 infopath
.dentry
= elm
->info_dentry
;
782 err
= vfsub_unlink(h_dir
, &infopath
, &delegated
,
785 if (unlikely(err
== -EWOULDBLOCK
))
787 dput(elm
->info_dentry
);
788 } else if (elm
->info_last
) {
790 w
->fdata
->drinfo
= *elm
->info_last
;
791 memcpy(w
->fdata
->drinfo
.oldname
,
792 elm
->info_last
->oldname
,
793 elm
->info_last
->oldnamelen
);
794 err
= au_drinfo_store_sio(w
, /*elm*/NULL
);
795 kfree(elm
->info_last
);
798 AuIOErr("%d, %s\n", err
, w
->whname
);
799 /* go on even if err */
803 /* caller has to call au_dr_rename_fin() later */
804 static int au_drinfo_store(struct dentry
*dentry
, aufs_bindex_t btgt
,
805 struct qstr
*dst_name
, void *_rev
)
808 aufs_bindex_t bindex
, btail
;
809 struct au_drinfo_store work
;
810 struct au_drinfo_rev
*rev
, **p
;
811 struct au_drinfo_rev_elm
*elm
;
812 struct super_block
*sb
;
813 struct au_branch
*br
;
814 struct au_hinode
*hdir
;
816 err
= au_drinfo_store_work_init(&work
, btgt
);
822 btail
= au_dbtaildir(dentry
);
824 sz
= sizeof(*rev
) + sizeof(*elm
) * nelm
;
825 rev
= kcalloc(1, sz
, GFP_NOFS
);
826 if (unlikely(!rev
)) {
837 work
.h_ppath
.dentry
= au_h_dptr(dentry
, btgt
);
838 work
.h_ppath
.mnt
= au_sbr_mnt(sb
, btgt
);
839 hdir
= au_hi(d_inode(dentry
), btgt
);
840 au_hn_inode_lock_nested(hdir
, AuLsc_I_CHILD
);
841 for (bindex
= btgt
+ 1; bindex
<= btail
; bindex
++, elm
++) {
842 work
.h_dentry
= au_h_dptr(dentry
, bindex
);
846 err
= au_drinfo_construct(&work
.fdata
, work
.h_dentry
,
852 work
.renameback
= au_qstreq(&work
.h_dentry
->d_name
, dst_name
);
853 br
= au_sbr(sb
, bindex
);
854 work
.whnamelen
= sizeof(AUFS_WH_DR_INFO_PFX
) - 1;
855 work
.whnamelen
+= au_drinfo_name(br
, work
.infoname
,
857 AuDbg("whname %.*s, i%llu, %.*s\n",
858 work
.whnamelen
, work
.whname
,
859 be64_to_cpu((__force __be64
)work
.fdata
->drinfo
.ino
),
860 work
.fdata
->drinfo
.oldnamelen
,
861 work
.fdata
->drinfo
.oldname
);
863 err
= au_drinfo_store_sio(&work
, elm
);
869 /* revert all drinfo */
870 au_drinfo_store_rev(rev
, &work
);
874 au_hn_inode_unlock(hdir
);
877 au_drinfo_store_work_fin(&work
);
882 /* ---------------------------------------------------------------------- */
884 int au_dr_rename(struct dentry
*src
, aufs_bindex_t bindex
,
885 struct qstr
*dst_name
, void *_rev
)
889 struct super_block
*sb
;
890 struct au_branch
*br
;
892 struct dentry
*h_dentry
;
893 struct inode
*h_inode
;
894 struct au_dr_hino
*ent
;
895 struct au_drinfo_rev
*rev
, **p
;
897 AuDbg("bindex %d\n", bindex
);
900 ent
= kmalloc(sizeof(*ent
), GFP_NOFS
);
905 br
= au_sbr(sb
, bindex
);
907 h_dentry
= au_h_dptr(src
, bindex
);
908 h_inode
= d_inode(h_dentry
);
909 ino
= h_inode
->i_ino
;
911 already
= au_dr_hino_test_add(dr
, ino
, ent
);
912 AuDbg("b%d, hi%llu, already %d\n",
913 bindex
, (unsigned long long)ino
, already
);
915 err
= au_drinfo_store(src
, bindex
, dst_name
, _rev
);
920 rev
->already
= already
;
921 goto out
; /* success */
926 au_dr_hino_del(dr
, ent
);
934 void au_dr_rename_fin(struct dentry
*src
, aufs_bindex_t btgt
, void *_rev
)
936 struct au_drinfo_rev
*rev
;
937 struct au_drinfo_rev_elm
*elm
;
942 for (nelm
= rev
->nelm
; nelm
> 0; nelm
--, elm
++) {
943 dput(elm
->info_dentry
);
944 kfree(elm
->info_last
);
949 void au_dr_rename_rev(struct dentry
*src
, aufs_bindex_t btgt
, void *_rev
)
952 struct au_drinfo_store work
;
953 struct au_drinfo_rev
*rev
= _rev
;
954 struct super_block
*sb
;
955 struct au_branch
*br
;
956 struct inode
*h_inode
;
958 struct au_dr_hino
*ent
;
960 err
= au_drinfo_store_work_init(&work
, btgt
);
965 br
= au_sbr(sb
, btgt
);
966 work
.h_ppath
.dentry
= au_h_dptr(src
, btgt
);
967 work
.h_ppath
.mnt
= au_br_mnt(br
);
968 au_drinfo_store_rev(rev
, &work
);
969 au_drinfo_store_work_fin(&work
);
974 h_inode
= d_inode(work
.h_ppath
.dentry
);
975 ent
= au_dr_hino_find(dr
, h_inode
->i_ino
);
977 au_dr_hino_del(dr
, ent
);
983 pr_err("failed to remove dirren info\n");
986 /* ---------------------------------------------------------------------- */
988 static struct au_drinfo
*au_drinfo_do_load(struct path
*h_ppath
,
989 char *whname
, int whnamelen
,
990 struct dentry
**info_dentry
)
992 struct au_drinfo
*drinfo
;
995 struct path infopath
;
998 AuDbg("%pd/%.*s\n", h_ppath
->dentry
, whnamelen
, whname
);
1000 *info_dentry
= NULL
;
1003 h_dir
= d_inode(h_ppath
->dentry
);
1004 vfsub_inode_lock_shared_nested(h_dir
, AuLsc_I_PARENT
);
1005 infopath
.dentry
= vfsub_lookup_one_len(whname
, h_ppath
->dentry
,
1007 if (IS_ERR(infopath
.dentry
)) {
1008 drinfo
= (void *)infopath
.dentry
;
1012 if (d_is_negative(infopath
.dentry
))
1013 goto out_dput
; /* success */
1015 infopath
.mnt
= h_ppath
->mnt
;
1016 f
= vfsub_dentry_open(&infopath
, O_RDONLY
);
1017 inode_unlock_shared(h_dir
);
1024 drinfo
= au_drinfo_read_k(f
, /*h_ino*/0);
1025 if (IS_ERR_OR_NULL(drinfo
))
1028 AuDbg("oldname %.*s\n", drinfo
->oldnamelen
, drinfo
->oldname
);
1029 *info_dentry
= dget(infopath
.dentry
); /* keep it alive */
1034 dput(infopath
.dentry
);
1037 inode_unlock_shared(h_dir
);
1038 AuTraceErrPtr(drinfo
);
1042 struct au_drinfo_do_load_args
{
1043 struct au_drinfo
**drinfop
;
1044 struct path
*h_ppath
;
1047 struct dentry
**info_dentry
;
1050 static void au_call_drinfo_do_load(void *args
)
1052 struct au_drinfo_do_load_args
*a
= args
;
1054 *a
->drinfop
= au_drinfo_do_load(a
->h_ppath
, a
->whname
, a
->whnamelen
,
1058 struct au_drinfo_load
{
1059 struct path h_ppath
;
1061 unsigned char no_sio
;
1063 aufs_bindex_t ninfo
;
1064 struct au_drinfo
**drinfo
;
1067 static int au_drinfo_load(struct au_drinfo_load
*w
, aufs_bindex_t bindex
,
1068 struct au_branch
*br
)
1070 int err
, wkq_err
, whnamelen
, e
;
1071 char whname
[sizeof(AUFS_WH_DR_INFO_PFX
) + AUFS_DIRREN_ENV_VAL_SZ
]
1072 = AUFS_WH_DR_INFO_PFX
;
1073 struct au_drinfo
*drinfo
;
1074 struct qstr oldname
;
1075 struct inode
*h_dir
, *delegated
;
1076 struct dentry
*info_dentry
;
1077 struct path infopath
;
1079 whnamelen
= sizeof(AUFS_WH_DR_INFO_PFX
) - 1;
1080 whnamelen
+= au_drinfo_name(br
, whname
+ whnamelen
,
1081 sizeof(whname
) - whnamelen
);
1083 drinfo
= au_drinfo_do_load(&w
->h_ppath
, whname
, whnamelen
,
1086 struct au_drinfo_do_load_args args
= {
1088 .h_ppath
= &w
->h_ppath
,
1090 .whnamelen
= whnamelen
,
1091 .info_dentry
= &info_dentry
1093 wkq_err
= au_wkq_wait(au_call_drinfo_do_load
, &args
);
1094 if (unlikely(wkq_err
))
1095 drinfo
= ERR_PTR(wkq_err
);
1097 err
= PTR_ERR(drinfo
);
1098 if (IS_ERR_OR_NULL(drinfo
))
1102 oldname
.len
= drinfo
->oldnamelen
;
1103 oldname
.name
= drinfo
->oldname
;
1104 if (au_qstreq(w
->qname
, &oldname
)) {
1105 /* the name is renamed back */
1109 infopath
.dentry
= info_dentry
;
1110 infopath
.mnt
= w
->h_ppath
.mnt
;
1111 h_dir
= d_inode(w
->h_ppath
.dentry
);
1113 inode_lock_nested(h_dir
, AuLsc_I_PARENT
);
1114 e
= vfsub_unlink(h_dir
, &infopath
, &delegated
, !w
->no_sio
);
1115 inode_unlock(h_dir
);
1117 AuIOErr("ignored %d, %pd2\n", e
, &infopath
.dentry
);
1118 if (unlikely(e
== -EWOULDBLOCK
))
1121 kfree(w
->drinfo
[bindex
]);
1122 w
->drinfo
[bindex
] = drinfo
;
1130 /* ---------------------------------------------------------------------- */
1132 static void au_dr_lkup_free(struct au_drinfo
**drinfo
, int n
)
1134 struct au_drinfo
**p
= drinfo
;
1141 int au_dr_lkup(struct au_do_lookup_args
*lkup
, struct dentry
*dentry
,
1145 struct au_drinfo_load w
;
1146 aufs_bindex_t bindex
, bbot
;
1147 struct au_branch
*br
;
1148 struct inode
*h_dir
;
1149 struct au_dr_hino
*ent
;
1150 struct super_block
*sb
;
1152 AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
1153 AuLNPair(&dentry
->d_name
), AuLNPair(&lkup
->dirren
.dr_name
),
1154 AuLNPair(&lkup
->whname
), btgt
);
1157 bbot
= au_sbbot(sb
);
1159 if (!lkup
->dirren
.drinfo
) {
1160 lkup
->dirren
.drinfo
= kcalloc(w
.ninfo
,
1161 sizeof(*lkup
->dirren
.drinfo
),
1163 if (unlikely(!lkup
->dirren
.drinfo
)) {
1167 lkup
->dirren
.ninfo
= w
.ninfo
;
1169 w
.drinfo
= lkup
->dirren
.drinfo
;
1170 w
.no_sio
= !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID
);
1171 w
.h_ppath
.dentry
= au_h_dptr(dentry
, btgt
);
1172 AuDebugOn(!w
.h_ppath
.dentry
);
1173 w
.h_ppath
.mnt
= au_sbr_mnt(sb
, btgt
);
1174 w
.qname
= &dentry
->d_name
;
1177 for (bindex
= btgt
+ 1; bindex
<= bbot
; bindex
++) {
1178 br
= au_sbr(sb
, bindex
);
1179 err
= au_drinfo_load(&w
, bindex
, br
);
1182 if (w
.drinfo
[bindex
])
1186 br
= au_sbr(sb
, btgt
);
1187 h_dir
= d_inode(w
.h_ppath
.dentry
);
1188 ent
= au_dr_hino_find(&br
->br_dirren
, h_dir
->i_ino
);
1190 au_dr_hino_del(&br
->br_dirren
, ent
);
1193 goto out
; /* success */
1196 au_dr_lkup_free(lkup
->dirren
.drinfo
, lkup
->dirren
.ninfo
);
1197 lkup
->dirren
.ninfo
= 0;
1198 lkup
->dirren
.drinfo
= NULL
;
1204 void au_dr_lkup_fin(struct au_do_lookup_args
*lkup
)
1206 au_dr_lkup_free(lkup
->dirren
.drinfo
, lkup
->dirren
.ninfo
);
1209 int au_dr_lkup_name(struct au_do_lookup_args
*lkup
, aufs_bindex_t btgt
)
1212 struct au_drinfo
*drinfo
;
1215 if (!lkup
->dirren
.drinfo
)
1217 AuDebugOn(lkup
->dirren
.ninfo
< btgt
+ 1);
1218 drinfo
= lkup
->dirren
.drinfo
[btgt
+ 1];
1222 kfree(lkup
->whname
.name
);
1223 lkup
->whname
.name
= NULL
;
1224 lkup
->dirren
.dr_name
.len
= drinfo
->oldnamelen
;
1225 lkup
->dirren
.dr_name
.name
= drinfo
->oldname
;
1226 lkup
->name
= &lkup
->dirren
.dr_name
;
1227 err
= au_wh_name_alloc(&lkup
->whname
, lkup
->name
);
1229 AuDbg("name %.*s, whname %.*s, b%d\n",
1230 AuLNPair(lkup
->name
), AuLNPair(&lkup
->whname
),
1238 int au_dr_lkup_h_ino(struct au_do_lookup_args
*lkup
, aufs_bindex_t bindex
,
1242 struct au_drinfo
*drinfo
;
1245 if (!lkup
->dirren
.drinfo
)
1247 AuDebugOn(lkup
->dirren
.ninfo
< bindex
+ 1);
1248 drinfo
= lkup
->dirren
.drinfo
[bindex
+ 1];
1252 match
= (drinfo
->ino
== h_ino
);
1253 AuDbg("match %d\n", match
);
1259 /* ---------------------------------------------------------------------- */
1261 int au_dr_opt_set(struct super_block
*sb
)
1264 aufs_bindex_t bindex
, bbot
;
1265 struct au_branch
*br
;
1268 bbot
= au_sbbot(sb
);
1269 for (bindex
= 0; !err
&& bindex
<= bbot
; bindex
++) {
1270 br
= au_sbr(sb
, bindex
);
1271 err
= au_dr_hino(sb
, bindex
, /*br*/NULL
, &br
->br_path
);
1277 int au_dr_opt_flush(struct super_block
*sb
)
1280 aufs_bindex_t bindex
, bbot
;
1281 struct au_branch
*br
;
1284 bbot
= au_sbbot(sb
);
1285 for (bindex
= 0; !err
&& bindex
<= bbot
; bindex
++) {
1286 br
= au_sbr(sb
, bindex
);
1287 if (au_br_writable(br
->br_perm
))
1288 err
= au_dr_hino(sb
, bindex
, /*br*/NULL
, /*path*/NULL
);
1294 int au_dr_opt_clr(struct super_block
*sb
, int no_flush
)
1297 aufs_bindex_t bindex
, bbot
;
1298 struct au_branch
*br
;
1302 err
= au_dr_opt_flush(sb
);
1307 bbot
= au_sbbot(sb
);
1308 for (bindex
= 0; bindex
<= bbot
; bindex
++) {
1309 br
= au_sbr(sb
, bindex
);
1310 au_dr_hino_free(&br
->br_dirren
);