]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/aufs/branch.c
2 * Copyright (C) 2005-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/>.
22 #include <linux/compat.h>
23 #include <linux/statfs.h>
27 * free a single branch
29 static void au_br_do_free(struct au_branch
*br
)
33 struct au_dykey
**key
;
35 au_hnotify_fin_br(br
);
36 /* always, regardless the mount option */
37 au_dr_hino_free(&br
->br_dirren
);
39 if (br
->br_xino
.xi_file
)
40 fput(br
->br_xino
.xi_file
);
41 for (i
= br
->br_xino
.xi_nondir
.total
- 1; i
>= 0; i
--)
42 AuDebugOn(br
->br_xino
.xi_nondir
.array
[i
]);
43 kfree(br
->br_xino
.xi_nondir
.array
);
45 AuDebugOn(au_br_count(br
));
50 for (i
= 0; i
< AuBrWh_Last
; i
++)
52 AuDebugOn(atomic_read(&wbr
->wbr_wh_running
));
53 AuRwDestroy(&wbr
->wbr_wh_rwsem
);
57 au_br_fhsm_fin(br
->br_fhsm
);
62 for (i
= 0; i
< AuBrDynOp
; i
++, key
++)
68 /* recursive lock, s_umount of branch's */
70 path_put(&br
->br_path
);
79 void au_br_free(struct au_sbinfo
*sbinfo
)
82 struct au_branch
**br
;
84 AuRwMustWriteLock(&sbinfo
->si_rwsem
);
86 bmax
= sbinfo
->si_bbot
+ 1;
87 br
= sbinfo
->si_branch
;
93 * find the index of a branch which is specified by @br_id.
95 int au_br_index(struct super_block
*sb
, aufs_bindex_t br_id
)
97 aufs_bindex_t bindex
, bbot
;
100 for (bindex
= 0; bindex
<= bbot
; bindex
++)
101 if (au_sbr_id(sb
, bindex
) == br_id
)
106 /* ---------------------------------------------------------------------- */
112 static int test_overlap(struct super_block
*sb
, struct dentry
*h_adding
,
113 struct dentry
*h_root
)
115 if (unlikely(h_adding
== h_root
116 || au_test_loopback_overlap(sb
, h_adding
)))
118 if (h_adding
->d_sb
!= h_root
->d_sb
)
120 return au_test_subdir(h_adding
, h_root
)
121 || au_test_subdir(h_root
, h_adding
);
125 * returns a newly allocated branch. @new_nbranch is a number of branches
126 * after adding a branch.
128 static struct au_branch
*au_br_alloc(struct super_block
*sb
, int new_nbranch
,
131 struct au_branch
*add_branch
;
137 add_branch
= kzalloc(sizeof(*add_branch
), GFP_NOFS
);
138 if (unlikely(!add_branch
))
140 add_branch
->br_xino
.xi_nondir
.total
= 8; /* initial size */
141 add_branch
->br_xino
.xi_nondir
.array
142 = kcalloc(add_branch
->br_xino
.xi_nondir
.total
, sizeof(ino_t
),
144 if (unlikely(!add_branch
->br_xino
.xi_nondir
.array
))
147 err
= au_hnotify_init_br(add_branch
, perm
);
151 if (au_br_writable(perm
)) {
152 /* may be freed separately at changing the branch permission */
153 add_branch
->br_wbr
= kzalloc(sizeof(*add_branch
->br_wbr
),
155 if (unlikely(!add_branch
->br_wbr
))
159 if (au_br_fhsm(perm
)) {
160 err
= au_fhsm_br_alloc(add_branch
);
166 err
= au_sbr_realloc(au_sbi(sb
), new_nbranch
, /*may_shrink*/0);
168 err
= au_di_realloc(au_di(root
), new_nbranch
, /*may_shrink*/0);
170 inode
= d_inode(root
);
171 err
= au_hinode_realloc(au_ii(inode
), new_nbranch
,
175 return add_branch
; /* success */
178 kfree(add_branch
->br_wbr
);
180 au_hnotify_fin_br(add_branch
);
182 kfree(add_branch
->br_xino
.xi_nondir
.array
);
190 * test if the branch permission is legal or not.
192 static int test_br(struct inode
*inode
, int brperm
, char *path
)
196 err
= (au_br_writable(brperm
) && IS_RDONLY(inode
));
201 pr_err("write permission for readonly mount or inode, %s\n", path
);
209 * 0: success, the caller will add it
210 * plus: success, it is already unified, the caller should ignore it
213 static int test_add(struct super_block
*sb
, struct au_opt_add
*add
, int remount
)
216 aufs_bindex_t bbot
, bindex
;
217 struct dentry
*root
, *h_dentry
;
218 struct inode
*inode
, *h_inode
;
222 if (unlikely(bbot
>= 0
223 && au_find_dbindex(root
, add
->path
.dentry
) >= 0)) {
227 pr_err("%s duplicated\n", add
->pathname
);
232 err
= -ENOSPC
; /* -E2BIG; */
233 if (unlikely(AUFS_BRANCH_MAX
<= add
->bindex
234 || AUFS_BRANCH_MAX
- 1 <= bbot
)) {
235 pr_err("number of branches exceeded %s\n", add
->pathname
);
240 if (unlikely(add
->bindex
< 0 || bbot
+ 1 < add
->bindex
)) {
241 pr_err("bad index %d\n", add
->bindex
);
245 inode
= d_inode(add
->path
.dentry
);
247 if (unlikely(!inode
->i_nlink
)) {
248 pr_err("no existence %s\n", add
->pathname
);
253 if (unlikely(inode
->i_sb
== sb
)) {
254 pr_err("%s must be outside\n", add
->pathname
);
258 if (unlikely(au_test_fs_unsuppoted(inode
->i_sb
))) {
259 pr_err("unsupported filesystem, %s (%s)\n",
260 add
->pathname
, au_sbtype(inode
->i_sb
));
264 if (unlikely(inode
->i_sb
->s_stack_depth
)) {
265 pr_err("already stacked, %s (%s)\n",
266 add
->pathname
, au_sbtype(inode
->i_sb
));
270 err
= test_br(d_inode(add
->path
.dentry
), add
->perm
, add
->pathname
);
275 return 0; /* success */
278 for (bindex
= 0; bindex
<= bbot
; bindex
++)
279 if (unlikely(test_overlap(sb
, add
->path
.dentry
,
280 au_h_dptr(root
, bindex
)))) {
281 pr_err("%s is overlapped\n", add
->pathname
);
286 if (au_opt_test(au_mntflags(sb
), WARN_PERM
)) {
287 h_dentry
= au_h_dptr(root
, 0);
288 h_inode
= d_inode(h_dentry
);
289 if ((h_inode
->i_mode
& S_IALLUGO
) != (inode
->i_mode
& S_IALLUGO
)
290 || !uid_eq(h_inode
->i_uid
, inode
->i_uid
)
291 || !gid_eq(h_inode
->i_gid
, inode
->i_gid
))
292 pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
294 i_uid_read(inode
), i_gid_read(inode
),
295 (inode
->i_mode
& S_IALLUGO
),
296 i_uid_read(h_inode
), i_gid_read(h_inode
),
297 (h_inode
->i_mode
& S_IALLUGO
));
305 * initialize or clean the whiteouts for an adding branch
307 static int au_br_init_wh(struct super_block
*sb
, struct au_branch
*br
,
311 aufs_bindex_t bindex
;
312 struct inode
*h_inode
;
314 struct au_hinode
*hdir
;
315 struct dentry
*h_dentry
;
317 err
= vfsub_mnt_want_write(au_br_mnt(br
));
322 old_perm
= br
->br_perm
;
323 br
->br_perm
= new_perm
;
326 bindex
= au_br_index(sb
, br
->br_id
);
328 hdir
= au_hi(d_inode(sb
->s_root
), bindex
);
329 au_hn_inode_lock_nested(hdir
, AuLsc_I_PARENT
);
331 h_dentry
= au_br_dentry(br
);
332 h_inode
= d_inode(h_dentry
);
333 inode_lock_nested(h_inode
, AuLsc_I_PARENT
);
336 err
= au_wh_init(br
, sb
);
338 wbr_wh_write_lock(wbr
);
339 err
= au_wh_init(br
, sb
);
340 wbr_wh_write_unlock(wbr
);
343 au_hn_inode_unlock(hdir
);
345 inode_unlock(h_inode
);
346 vfsub_mnt_drop_write(au_br_mnt(br
));
347 br
->br_perm
= old_perm
;
349 if (!err
&& wbr
&& !au_br_writable(new_perm
)) {
358 static int au_wbr_init(struct au_branch
*br
, struct super_block
*sb
,
366 au_rw_init(&wbr
->wbr_wh_rwsem
);
367 atomic_set(&wbr
->wbr_wh_running
, 0);
370 * a limit for rmdir/rename a dir
371 * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
373 err
= vfs_statfs(&br
->br_path
, &kst
);
377 if (kst
.f_namelen
>= NAME_MAX
)
378 err
= au_br_init_wh(sb
, br
, perm
);
380 pr_err("%pd(%s), unsupported namelen %ld\n",
382 au_sbtype(au_br_dentry(br
)->d_sb
), kst
.f_namelen
);
388 /* initialize a new branch */
389 static int au_br_init(struct au_branch
*br
, struct super_block
*sb
,
390 struct au_opt_add
*add
)
393 struct inode
*h_inode
;
396 spin_lock_init(&br
->br_xino
.xi_nondir
.spin
);
397 init_waitqueue_head(&br
->br_xino
.xi_nondir
.wqh
);
398 br
->br_perm
= add
->perm
;
399 br
->br_path
= add
->path
; /* set first, path_get() later */
400 spin_lock_init(&br
->br_dykey_lock
);
401 au_br_count_init(br
);
402 atomic_set(&br
->br_xino_running
, 0);
403 br
->br_id
= au_new_br_id(sb
);
404 AuDebugOn(br
->br_id
< 0);
406 /* always, regardless the given option */
407 err
= au_dr_br_init(sb
, br
, &add
->path
);
411 if (au_br_writable(add
->perm
)) {
412 err
= au_wbr_init(br
, sb
, add
->perm
);
417 if (au_opt_test(au_mntflags(sb
), XINO
)) {
418 h_inode
= d_inode(add
->path
.dentry
);
419 err
= au_xino_br(sb
, br
, h_inode
->i_ino
,
420 au_sbr(sb
, 0)->br_xino
.xi_file
, /*do_test*/1);
422 AuDebugOn(br
->br_xino
.xi_file
);
428 path_get(&br
->br_path
);
429 goto out
; /* success */
432 memset(&br
->br_path
, 0, sizeof(br
->br_path
));
437 static void au_br_do_add_brp(struct au_sbinfo
*sbinfo
, aufs_bindex_t bindex
,
438 struct au_branch
*br
, aufs_bindex_t bbot
,
439 aufs_bindex_t amount
)
441 struct au_branch
**brp
;
443 AuRwMustWriteLock(&sbinfo
->si_rwsem
);
445 brp
= sbinfo
->si_branch
+ bindex
;
446 memmove(brp
+ 1, brp
, sizeof(*brp
) * amount
);
449 if (unlikely(bbot
< 0))
453 static void au_br_do_add_hdp(struct au_dinfo
*dinfo
, aufs_bindex_t bindex
,
454 aufs_bindex_t bbot
, aufs_bindex_t amount
)
456 struct au_hdentry
*hdp
;
458 AuRwMustWriteLock(&dinfo
->di_rwsem
);
460 hdp
= au_hdentry(dinfo
, bindex
);
461 memmove(hdp
+ 1, hdp
, sizeof(*hdp
) * amount
);
462 au_h_dentry_init(hdp
);
464 if (unlikely(bbot
< 0))
468 static void au_br_do_add_hip(struct au_iinfo
*iinfo
, aufs_bindex_t bindex
,
469 aufs_bindex_t bbot
, aufs_bindex_t amount
)
471 struct au_hinode
*hip
;
473 AuRwMustWriteLock(&iinfo
->ii_rwsem
);
475 hip
= au_hinode(iinfo
, bindex
);
476 memmove(hip
+ 1, hip
, sizeof(*hip
) * amount
);
479 if (unlikely(bbot
< 0))
483 static void au_br_do_add(struct super_block
*sb
, struct au_branch
*br
,
484 aufs_bindex_t bindex
)
486 struct dentry
*root
, *h_dentry
;
487 struct inode
*root_inode
, *h_inode
;
488 aufs_bindex_t bbot
, amount
;
491 root_inode
= d_inode(root
);
493 amount
= bbot
+ 1 - bindex
;
494 h_dentry
= au_br_dentry(br
);
496 au_br_do_add_brp(au_sbi(sb
), bindex
, br
, bbot
, amount
);
497 au_br_do_add_hdp(au_di(root
), bindex
, bbot
, amount
);
498 au_br_do_add_hip(au_ii(root_inode
), bindex
, bbot
, amount
);
499 au_set_h_dptr(root
, bindex
, dget(h_dentry
));
500 h_inode
= d_inode(h_dentry
);
501 au_set_h_iptr(root_inode
, bindex
, au_igrab(h_inode
), /*flags*/0);
505 int au_br_add(struct super_block
*sb
, struct au_opt_add
*add
, int remount
)
508 aufs_bindex_t bbot
, add_bindex
;
509 struct dentry
*root
, *h_dentry
;
510 struct inode
*root_inode
;
511 struct au_branch
*add_branch
;
514 root_inode
= d_inode(root
);
515 IMustLock(root_inode
);
516 IiMustWriteLock(root_inode
);
517 err
= test_add(sb
, add
, remount
);
518 if (unlikely(err
< 0))
522 goto out
; /* success */
526 add_branch
= au_br_alloc(sb
, bbot
+ 2, add
->perm
);
527 err
= PTR_ERR(add_branch
);
528 if (IS_ERR(add_branch
))
531 err
= au_br_init(add_branch
, sb
, add
);
533 au_br_do_free(add_branch
);
537 add_bindex
= add
->bindex
;
539 au_br_do_add(sb
, add_branch
, add_bindex
);
541 sysaufs_brs_del(sb
, add_bindex
);
542 au_br_do_add(sb
, add_branch
, add_bindex
);
543 sysaufs_brs_add(sb
, add_bindex
);
546 h_dentry
= add
->path
.dentry
;
548 au_cpup_attr_all(root_inode
, /*force*/1);
549 sb
->s_maxbytes
= h_dentry
->d_sb
->s_maxbytes
;
551 au_add_nlink(root_inode
, d_inode(h_dentry
));
554 * this test/set prevents aufs from handling unnecesary notify events
555 * of xino files, in case of re-adding a writable branch which was
556 * once detached from aufs.
558 if (au_xino_brid(sb
) < 0
559 && au_br_writable(add_branch
->br_perm
)
560 && !au_test_fs_bad_xino(h_dentry
->d_sb
)
561 && add_branch
->br_xino
.xi_file
562 && add_branch
->br_xino
.xi_file
->f_path
.dentry
->d_parent
== h_dentry
)
563 au_xino_brid_set(sb
, add_branch
->br_id
);
569 /* ---------------------------------------------------------------------- */
571 static unsigned long long au_farray_cb(struct super_block
*sb
, void *a
,
572 unsigned long long max __maybe_unused
,
575 unsigned long long n
;
577 struct hlist_bl_head
*files
;
578 struct hlist_bl_node
*pos
;
579 struct au_finfo
*finfo
;
583 files
= &au_sbi(sb
)->si_files
;
584 hlist_bl_lock(files
);
585 hlist_bl_for_each_entry(finfo
, pos
, files
, fi_hlist
) {
588 && !special_file(file_inode(f
)->i_mode
)) {
595 hlist_bl_unlock(files
);
600 static struct file
**au_farray_alloc(struct super_block
*sb
,
601 unsigned long long *max
)
603 *max
= au_nfiles(sb
);
604 return au_array_alloc(max
, au_farray_cb
, sb
, /*arg*/NULL
);
607 static void au_farray_free(struct file
**a
, unsigned long long max
)
609 unsigned long long ull
;
611 for (ull
= 0; ull
< max
; ull
++)
617 /* ---------------------------------------------------------------------- */
623 /* to show the line number, do not make it inlined function */
624 #define AuVerbose(do_info, fmt, ...) do { \
626 pr_info(fmt, ##__VA_ARGS__); \
629 static int au_test_ibusy(struct inode
*inode
, aufs_bindex_t btop
,
632 return (inode
&& !S_ISDIR(inode
->i_mode
)) || btop
== bbot
;
635 static int au_test_dbusy(struct dentry
*dentry
, aufs_bindex_t btop
,
638 return au_test_ibusy(d_inode(dentry
), btop
, bbot
);
642 * test if the branch is deletable or not.
644 static int test_dentry_busy(struct dentry
*root
, aufs_bindex_t bindex
,
645 unsigned int sigen
, const unsigned int verbose
)
647 int err
, i
, j
, ndentry
;
648 aufs_bindex_t btop
, bbot
;
649 struct au_dcsub_pages dpages
;
650 struct au_dpage
*dpage
;
653 err
= au_dpages_init(&dpages
, GFP_NOFS
);
656 err
= au_dcsub_pages(&dpages
, root
, NULL
, NULL
);
660 for (i
= 0; !err
&& i
< dpages
.ndpage
; i
++) {
661 dpage
= dpages
.dpages
+ i
;
662 ndentry
= dpage
->ndentry
;
663 for (j
= 0; !err
&& j
< ndentry
; j
++) {
664 d
= dpage
->dentries
[j
];
665 AuDebugOn(au_dcount(d
) <= 0);
666 if (!au_digen_test(d
, sigen
)) {
667 di_read_lock_child(d
, AuLock_IR
);
668 if (unlikely(au_dbrange_test(d
))) {
669 di_read_unlock(d
, AuLock_IR
);
673 di_write_lock_child(d
);
674 if (unlikely(au_dbrange_test(d
))) {
678 err
= au_reval_dpath(d
, sigen
);
680 di_downgrade_lock(d
, AuLock_IR
);
687 /* AuDbgDentry(d); */
692 && au_h_dptr(d
, bindex
)
693 && au_test_dbusy(d
, btop
, bbot
)) {
695 AuVerbose(verbose
, "busy %pd\n", d
);
698 di_read_unlock(d
, AuLock_IR
);
703 au_dpages_free(&dpages
);
708 static int test_inode_busy(struct super_block
*sb
, aufs_bindex_t bindex
,
709 unsigned int sigen
, const unsigned int verbose
)
712 unsigned long long max
, ull
;
713 struct inode
*i
, **array
;
714 aufs_bindex_t btop
, bbot
;
716 array
= au_iarray_alloc(sb
, &max
);
717 err
= PTR_ERR(array
);
722 AuDbg("b%d\n", bindex
);
723 for (ull
= 0; !err
&& ull
< max
; ull
++) {
727 if (i
->i_ino
== AUFS_ROOT_INO
)
731 if (au_iigen(i
, NULL
) == sigen
)
732 ii_read_lock_child(i
);
734 ii_write_lock_child(i
);
735 err
= au_refresh_hinode_self(i
);
738 ii_downgrade_lock(i
);
749 && au_h_iptr(i
, bindex
)
750 && au_test_ibusy(i
, btop
, bbot
)) {
752 AuVerbose(verbose
, "busy i%lu\n", i
->i_ino
);
757 au_iarray_free(array
, max
);
763 static int test_children_busy(struct dentry
*root
, aufs_bindex_t bindex
,
764 const unsigned int verbose
)
769 sigen
= au_sigen(root
->d_sb
);
770 DiMustNoWaiters(root
);
771 IiMustNoWaiters(d_inode(root
));
772 di_write_unlock(root
);
773 err
= test_dentry_busy(root
, bindex
, sigen
, verbose
);
775 err
= test_inode_busy(root
->d_sb
, bindex
, sigen
, verbose
);
776 di_write_lock_child(root
); /* aufs_write_lock() calls ..._child() */
781 static int test_dir_busy(struct file
*file
, aufs_bindex_t br_id
,
782 struct file
**to_free
, int *idx
)
785 unsigned char matched
, root
;
786 aufs_bindex_t bindex
, bbot
;
787 struct au_fidir
*fidir
;
788 struct au_hfile
*hfile
;
791 root
= IS_ROOT(file
->f_path
.dentry
);
794 to_free
[*idx
] = file
;
800 fidir
= au_fi(file
)->fi_hdir
;
802 bbot
= au_fbbot_dir(file
);
803 for (bindex
= au_fbtop(file
); bindex
<= bbot
; bindex
++) {
804 hfile
= fidir
->fd_hfile
+ bindex
;
808 if (hfile
->hf_br
->br_id
== br_id
) {
820 static int test_file_busy(struct super_block
*sb
, aufs_bindex_t br_id
,
821 struct file
**to_free
, int opened
)
824 unsigned long long ull
, max
;
826 struct file
*file
, **array
;
828 struct au_hfile
*hfile
;
830 array
= au_farray_alloc(sb
, &max
);
831 err
= PTR_ERR(array
);
838 di_write_unlock(root
);
839 for (ull
= 0; ull
< max
; ull
++) {
844 /* AuDbg("%pD\n", file); */
846 btop
= au_fbtop(file
);
847 if (!d_is_dir(file
->f_path
.dentry
)) {
848 hfile
= &au_fi(file
)->fi_htop
;
849 if (hfile
->hf_br
->br_id
== br_id
)
852 err
= test_dir_busy(file
, br_id
, to_free
, &idx
);
853 fi_read_unlock(file
);
857 di_write_lock_child(root
);
858 au_farray_free(array
, max
);
859 AuDebugOn(idx
> opened
);
865 static void br_del_file(struct file
**to_free
, unsigned long long opened
,
868 unsigned long long ull
;
869 aufs_bindex_t bindex
, btop
, bbot
, bfound
;
871 struct au_fidir
*fidir
;
872 struct au_hfile
*hfile
;
874 for (ull
= 0; ull
< opened
; ull
++) {
879 /* AuDbg("%pD\n", file); */
880 AuDebugOn(!d_is_dir(file
->f_path
.dentry
));
882 fidir
= au_fi(file
)->fi_hdir
;
885 btop
= au_fbtop(file
);
886 bbot
= au_fbbot_dir(file
);
887 for (bindex
= btop
; bindex
<= bbot
; bindex
++) {
888 hfile
= fidir
->fd_hfile
+ bindex
;
892 if (hfile
->hf_br
->br_id
== br_id
) {
897 AuDebugOn(bfound
< 0);
898 au_set_h_fptr(file
, bfound
, NULL
);
899 if (bfound
== btop
) {
900 for (btop
++; btop
<= bbot
; btop
++)
901 if (au_hf_dir(file
, btop
)) {
902 au_set_fbtop(file
, btop
);
906 fi_write_unlock(file
);
910 static void au_br_do_del_brp(struct au_sbinfo
*sbinfo
,
911 const aufs_bindex_t bindex
,
912 const aufs_bindex_t bbot
)
914 struct au_branch
**brp
, **p
;
916 AuRwMustWriteLock(&sbinfo
->si_rwsem
);
918 brp
= sbinfo
->si_branch
+ bindex
;
920 memmove(brp
, brp
+ 1, sizeof(*brp
) * (bbot
- bindex
));
921 sbinfo
->si_branch
[0 + bbot
] = NULL
;
924 p
= au_krealloc(sbinfo
->si_branch
, sizeof(*p
) * bbot
, AuGFP_SBILIST
,
927 sbinfo
->si_branch
= p
;
931 static void au_br_do_del_hdp(struct au_dinfo
*dinfo
, const aufs_bindex_t bindex
,
932 const aufs_bindex_t bbot
)
934 struct au_hdentry
*hdp
, *p
;
936 AuRwMustWriteLock(&dinfo
->di_rwsem
);
938 hdp
= au_hdentry(dinfo
, bindex
);
940 memmove(hdp
, hdp
+ 1, sizeof(*hdp
) * (bbot
- bindex
));
941 /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
944 p
= au_krealloc(dinfo
->di_hdentry
, sizeof(*p
) * bbot
, AuGFP_SBILIST
,
947 dinfo
->di_hdentry
= p
;
951 static void au_br_do_del_hip(struct au_iinfo
*iinfo
, const aufs_bindex_t bindex
,
952 const aufs_bindex_t bbot
)
954 struct au_hinode
*hip
, *p
;
956 AuRwMustWriteLock(&iinfo
->ii_rwsem
);
958 hip
= au_hinode(iinfo
, bindex
);
960 memmove(hip
, hip
+ 1, sizeof(*hip
) * (bbot
- bindex
));
961 /* au_hinode_init(au_hinode(iinfo, bbot)); */
964 p
= au_krealloc(iinfo
->ii_hinode
, sizeof(*p
) * bbot
, AuGFP_SBILIST
,
967 iinfo
->ii_hinode
= p
;
971 static void au_br_do_del(struct super_block
*sb
, aufs_bindex_t bindex
,
972 struct au_branch
*br
)
975 struct au_sbinfo
*sbinfo
;
976 struct dentry
*root
, *h_root
;
977 struct inode
*inode
, *h_inode
;
978 struct au_hinode
*hinode
;
983 inode
= d_inode(root
);
985 bbot
= sbinfo
->si_bbot
;
987 h_root
= au_h_dptr(root
, bindex
);
988 hinode
= au_hi(inode
, bindex
);
989 h_inode
= au_igrab(hinode
->hi_inode
);
993 au_br_do_del_brp(sbinfo
, bindex
, bbot
);
994 au_br_do_del_hdp(au_di(root
), bindex
, bbot
);
995 au_br_do_del_hip(au_ii(inode
), bindex
, bbot
);
998 /* ignore an error */
999 au_dr_br_fin(sb
, br
); /* always, regardless the mount option */
1006 static unsigned long long empty_cb(struct super_block
*sb
, void *array
,
1007 unsigned long long max
, void *arg
)
1012 int au_br_del(struct super_block
*sb
, struct au_opt_del
*del
, int remount
)
1015 unsigned long long opened
;
1016 unsigned int mnt_flags
;
1017 aufs_bindex_t bindex
, bbot
, br_id
;
1018 unsigned char do_wh
, verbose
;
1019 struct au_branch
*br
;
1021 struct dentry
*root
;
1022 struct file
**to_free
;
1028 bindex
= au_find_dbindex(root
, del
->h_path
.dentry
);
1031 goto out
; /* success */
1033 pr_err("%s no such branch\n", del
->pathname
);
1036 AuDbg("bindex b%d\n", bindex
);
1039 mnt_flags
= au_mntflags(sb
);
1040 verbose
= !!au_opt_test(mnt_flags
, VERBOSE
);
1041 bbot
= au_sbbot(sb
);
1042 if (unlikely(!bbot
)) {
1043 AuVerbose(verbose
, "no more branches left\n");
1046 br
= au_sbr(sb
, bindex
);
1047 AuDebugOn(!path_equal(&br
->br_path
, &del
->h_path
));
1050 opened
= au_br_count(br
);
1051 if (unlikely(opened
)) {
1052 to_free
= au_array_alloc(&opened
, empty_cb
, sb
, NULL
);
1053 err
= PTR_ERR(to_free
);
1054 if (IS_ERR(to_free
))
1057 err
= test_file_busy(sb
, br_id
, to_free
, opened
);
1058 if (unlikely(err
)) {
1059 AuVerbose(verbose
, "%llu file(s) opened\n", opened
);
1065 do_wh
= wbr
&& (wbr
->wbr_whbase
|| wbr
->wbr_plink
|| wbr
->wbr_orph
);
1067 /* instead of WbrWhMustWriteLock(wbr) */
1068 SiMustWriteLock(sb
);
1069 for (i
= 0; i
< AuBrWh_Last
; i
++) {
1070 dput(wbr
->wbr_wh
[i
]);
1071 wbr
->wbr_wh
[i
] = NULL
;
1075 err
= test_children_busy(root
, bindex
, verbose
);
1076 if (unlikely(err
)) {
1085 * now we confirmed the branch is deletable.
1086 * let's free the remaining opened dirs on the branch.
1088 di_write_unlock(root
);
1089 br_del_file(to_free
, opened
, br_id
);
1090 di_write_lock_child(root
);
1094 au_br_do_del(sb
, bindex
, br
);
1096 sysaufs_brs_del(sb
, bindex
);
1097 au_br_do_del(sb
, bindex
, br
);
1098 sysaufs_brs_add(sb
, bindex
);
1102 au_cpup_attr_all(d_inode(root
), /*force*/1);
1103 sb
->s_maxbytes
= au_sbr_sb(sb
, 0)->s_maxbytes
;
1105 au_sub_nlink(d_inode(root
), d_inode(del
->h_path
.dentry
));
1106 if (au_opt_test(mnt_flags
, PLINK
))
1107 au_plink_half_refresh(sb
, br_id
);
1109 if (au_xino_brid(sb
) == br_id
)
1110 au_xino_brid_set(sb
, -1);
1111 goto out
; /* success */
1115 rerr
= au_br_init_wh(sb
, br
, br
->br_perm
);
1117 pr_warn("failed re-creating base whiteout, %s. (%d)\n",
1118 del
->pathname
, rerr
);
1121 au_farray_free(to_free
, opened
);
1125 /* ---------------------------------------------------------------------- */
1127 static int au_ibusy(struct super_block
*sb
, struct aufs_ibusy __user
*arg
)
1130 aufs_bindex_t btop
, bbot
;
1131 struct aufs_ibusy ibusy
;
1132 struct inode
*inode
, *h_inode
;
1135 if (unlikely(!capable(CAP_SYS_ADMIN
)))
1138 err
= copy_from_user(&ibusy
, arg
, sizeof(ibusy
));
1140 err
= !access_ok(VERIFY_WRITE
, &arg
->h_ino
, sizeof(arg
->h_ino
));
1141 if (unlikely(err
)) {
1148 si_read_lock(sb
, AuLock_FLUSH
);
1149 if (unlikely(ibusy
.bindex
< 0 || ibusy
.bindex
> au_sbbot(sb
)))
1153 ibusy
.h_ino
= 0; /* invalid */
1154 inode
= ilookup(sb
, ibusy
.ino
);
1156 || inode
->i_ino
== AUFS_ROOT_INO
1157 || au_is_bad_inode(inode
))
1160 ii_read_lock_child(inode
);
1161 btop
= au_ibtop(inode
);
1162 bbot
= au_ibbot(inode
);
1163 if (btop
<= ibusy
.bindex
&& ibusy
.bindex
<= bbot
) {
1164 h_inode
= au_h_iptr(inode
, ibusy
.bindex
);
1165 if (h_inode
&& au_test_ibusy(inode
, btop
, bbot
))
1166 ibusy
.h_ino
= h_inode
->i_ino
;
1168 ii_read_unlock(inode
);
1174 err
= __put_user(ibusy
.h_ino
, &arg
->h_ino
);
1175 if (unlikely(err
)) {
1184 long au_ibusy_ioctl(struct file
*file
, unsigned long arg
)
1186 return au_ibusy(file
->f_path
.dentry
->d_sb
, (void __user
*)arg
);
1189 #ifdef CONFIG_COMPAT
1190 long au_ibusy_compat_ioctl(struct file
*file
, unsigned long arg
)
1192 return au_ibusy(file
->f_path
.dentry
->d_sb
, compat_ptr(arg
));
1196 /* ---------------------------------------------------------------------- */
1199 * change a branch permission
1202 static void au_warn_ima(void)
1205 /* since it doesn't support mark_files_ro() */
1206 AuWarn1("RW -> RO makes IMA to produce wrong message\n");
1210 static int do_need_sigen_inc(int a
, int b
)
1212 return au_br_whable(a
) && !au_br_whable(b
);
1215 static int need_sigen_inc(int old
, int new)
1217 return do_need_sigen_inc(old
, new)
1218 || do_need_sigen_inc(new, old
);
1221 static int au_br_mod_files_ro(struct super_block
*sb
, aufs_bindex_t bindex
)
1224 unsigned int mnt_flags
;
1225 unsigned long long ull
, max
;
1226 aufs_bindex_t br_id
;
1227 unsigned char verbose
, writer
;
1228 struct file
*file
, *hf
, **array
;
1229 struct au_hfile
*hfile
;
1231 mnt_flags
= au_mntflags(sb
);
1232 verbose
= !!au_opt_test(mnt_flags
, VERBOSE
);
1234 array
= au_farray_alloc(sb
, &max
);
1235 err
= PTR_ERR(array
);
1240 br_id
= au_sbr_id(sb
, bindex
);
1241 for (ull
= 0; ull
< max
; ull
++) {
1243 if (unlikely(!file
))
1246 /* AuDbg("%pD\n", file); */
1248 if (unlikely(au_test_mmapped(file
))) {
1250 AuVerbose(verbose
, "mmapped %pD\n", file
);
1252 FiMustNoWaiters(file
);
1253 fi_read_unlock(file
);
1257 hfile
= &au_fi(file
)->fi_htop
;
1258 hf
= hfile
->hf_file
;
1259 if (!d_is_reg(file
->f_path
.dentry
)
1260 || !(file
->f_mode
& FMODE_WRITE
)
1261 || hfile
->hf_br
->br_id
!= br_id
1262 || !(hf
->f_mode
& FMODE_WRITE
))
1269 FiMustNoWaiters(file
);
1270 fi_read_unlock(file
);
1278 for (ull
= 0; ull
< max
; ull
++) {
1283 /* todo: already flushed? */
1285 * fs/super.c:mark_files_ro() is gone, but aufs keeps its
1286 * approach which resets f_mode and calls mnt_drop_write() and
1287 * file_release_write() for each file, because the branch
1288 * attribute in aufs world is totally different from the native
1291 /* fi_read_lock(file); */
1292 hfile
= &au_fi(file
)->fi_htop
;
1293 hf
= hfile
->hf_file
;
1294 /* fi_read_unlock(file); */
1295 spin_lock(&hf
->f_lock
);
1296 writer
= !!(hf
->f_mode
& FMODE_WRITER
);
1297 hf
->f_mode
&= ~(FMODE_WRITE
| FMODE_WRITER
);
1298 spin_unlock(&hf
->f_lock
);
1300 put_write_access(file_inode(hf
));
1301 __mnt_drop_write(hf
->f_path
.mnt
);
1306 au_farray_free(array
, max
);
1312 int au_br_mod(struct super_block
*sb
, struct au_opt_mod
*mod
, int remount
,
1316 aufs_bindex_t bindex
;
1317 struct dentry
*root
;
1318 struct au_branch
*br
;
1319 struct au_br_fhsm
*bf
;
1322 bindex
= au_find_dbindex(root
, mod
->h_root
);
1325 return 0; /* success */
1327 pr_err("%s no such branch\n", mod
->path
);
1330 AuDbg("bindex b%d\n", bindex
);
1332 err
= test_br(d_inode(mod
->h_root
), mod
->perm
, mod
->path
);
1336 br
= au_sbr(sb
, bindex
);
1337 AuDebugOn(mod
->h_root
!= au_br_dentry(br
));
1338 if (br
->br_perm
== mod
->perm
)
1339 return 0; /* success */
1341 /* pre-allocate for non-fhsm --> fhsm */
1343 if (!au_br_fhsm(br
->br_perm
) && au_br_fhsm(mod
->perm
)) {
1344 err
= au_fhsm_br_alloc(br
);
1351 if (au_br_writable(br
->br_perm
)) {
1352 /* remove whiteout base */
1353 err
= au_br_init_wh(sb
, br
, mod
->perm
);
1357 if (!au_br_writable(mod
->perm
)) {
1358 /* rw --> ro, file might be mmapped */
1359 DiMustNoWaiters(root
);
1360 IiMustNoWaiters(d_inode(root
));
1361 di_write_unlock(root
);
1362 err
= au_br_mod_files_ro(sb
, bindex
);
1363 /* aufs_write_lock() calls ..._child() */
1364 di_write_lock_child(root
);
1366 if (unlikely(err
)) {
1368 br
->br_wbr
= kzalloc(sizeof(*br
->br_wbr
),
1371 rerr
= au_wbr_init(br
, sb
, br
->br_perm
);
1372 if (unlikely(rerr
)) {
1373 AuIOErr("nested error %d (%d)\n",
1375 br
->br_perm
= mod
->perm
;
1379 } else if (au_br_writable(mod
->perm
)) {
1382 br
->br_wbr
= kzalloc(sizeof(*br
->br_wbr
), GFP_NOFS
);
1384 err
= au_wbr_init(br
, sb
, mod
->perm
);
1385 if (unlikely(err
)) {
1394 if (au_br_fhsm(br
->br_perm
)) {
1395 if (!au_br_fhsm(mod
->perm
)) {
1396 /* fhsm --> non-fhsm */
1397 au_br_fhsm_fin(br
->br_fhsm
);
1401 } else if (au_br_fhsm(mod
->perm
))
1402 /* non-fhsm --> fhsm */
1405 *do_refresh
|= need_sigen_inc(br
->br_perm
, mod
->perm
);
1406 br
->br_perm
= mod
->perm
;
1407 goto out
; /* success */
1416 /* ---------------------------------------------------------------------- */
1418 int au_br_stfs(struct au_branch
*br
, struct aufs_stfs
*stfs
)
1421 struct kstatfs kstfs
;
1423 err
= vfs_statfs(&br
->br_path
, &kstfs
);
1425 stfs
->f_blocks
= kstfs
.f_blocks
;
1426 stfs
->f_bavail
= kstfs
.f_bavail
;
1427 stfs
->f_files
= kstfs
.f_files
;
1428 stfs
->f_ffree
= kstfs
.f_ffree
;