]>
git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - fs/aufs/branch.c
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/>.
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
);
37 if (br
->br_xino
.xi_file
)
38 fput(br
->br_xino
.xi_file
);
39 mutex_destroy(&br
->br_xino
.xi_nondir_mtx
);
41 AuDebugOn(au_br_count(br
));
46 for (i
= 0; i
< AuBrWh_Last
; i
++)
48 AuDebugOn(atomic_read(&wbr
->wbr_wh_running
));
49 AuRwDestroy(&wbr
->wbr_wh_rwsem
);
53 au_br_fhsm_fin(br
->br_fhsm
);
54 au_delayed_kfree(br
->br_fhsm
);
58 for (i
= 0; i
< AuBrDynOp
; i
++, key
++)
64 /* recursive lock, s_umount of branch's */
66 path_put(&br
->br_path
);
69 au_delayed_kfree(wbr
);
76 void au_br_free(struct au_sbinfo
*sbinfo
)
79 struct au_branch
**br
;
81 AuRwMustWriteLock(&sbinfo
->si_rwsem
);
83 bmax
= sbinfo
->si_bbot
+ 1;
84 br
= sbinfo
->si_branch
;
90 * find the index of a branch which is specified by @br_id.
92 int au_br_index(struct super_block
*sb
, aufs_bindex_t br_id
)
94 aufs_bindex_t bindex
, bbot
;
97 for (bindex
= 0; bindex
<= bbot
; bindex
++)
98 if (au_sbr_id(sb
, bindex
) == br_id
)
103 /* ---------------------------------------------------------------------- */
109 static int test_overlap(struct super_block
*sb
, struct dentry
*h_adding
,
110 struct dentry
*h_root
)
112 if (unlikely(h_adding
== h_root
113 || au_test_loopback_overlap(sb
, h_adding
)))
115 if (h_adding
->d_sb
!= h_root
->d_sb
)
117 return au_test_subdir(h_adding
, h_root
)
118 || au_test_subdir(h_root
, h_adding
);
122 * returns a newly allocated branch. @new_nbranch is a number of branches
123 * after adding a branch.
125 static struct au_branch
*au_br_alloc(struct super_block
*sb
, int new_nbranch
,
128 struct au_branch
*add_branch
;
135 add_branch
= kzalloc(sizeof(*add_branch
), GFP_NOFS
);
136 if (unlikely(!add_branch
))
139 err
= au_hnotify_init_br(add_branch
, perm
);
143 if (au_br_writable(perm
)) {
144 /* may be freed separately at changing the branch permission */
145 add_branch
->br_wbr
= kzalloc(sizeof(*add_branch
->br_wbr
),
147 if (unlikely(!add_branch
->br_wbr
))
151 if (au_br_fhsm(perm
)) {
152 err
= au_fhsm_br_alloc(add_branch
);
157 err
= au_sbr_realloc(au_sbi(sb
), new_nbranch
, /*may_shrink*/0);
159 err
= au_di_realloc(au_di(root
), new_nbranch
, /*may_shrink*/0);
161 inode
= d_inode(root
);
162 err
= au_hinode_realloc(au_ii(inode
), new_nbranch
, /*may_shrink*/0);
165 return add_branch
; /* success */
168 if (add_branch
->br_wbr
)
169 au_delayed_kfree(add_branch
->br_wbr
);
171 au_hnotify_fin_br(add_branch
);
173 au_delayed_kfree(add_branch
);
179 * test if the branch permission is legal or not.
181 static int test_br(struct inode
*inode
, int brperm
, char *path
)
185 err
= (au_br_writable(brperm
) && IS_RDONLY(inode
));
190 pr_err("write permission for readonly mount or inode, %s\n", path
);
198 * 0: success, the caller will add it
199 * plus: success, it is already unified, the caller should ignore it
202 static int test_add(struct super_block
*sb
, struct au_opt_add
*add
, int remount
)
205 aufs_bindex_t bbot
, bindex
;
206 struct dentry
*root
, *h_dentry
;
207 struct inode
*inode
, *h_inode
;
211 if (unlikely(bbot
>= 0
212 && au_find_dbindex(root
, add
->path
.dentry
) >= 0)) {
216 pr_err("%s duplicated\n", add
->pathname
);
221 err
= -ENOSPC
; /* -E2BIG; */
222 if (unlikely(AUFS_BRANCH_MAX
<= add
->bindex
223 || AUFS_BRANCH_MAX
- 1 <= bbot
)) {
224 pr_err("number of branches exceeded %s\n", add
->pathname
);
229 if (unlikely(add
->bindex
< 0 || bbot
+ 1 < add
->bindex
)) {
230 pr_err("bad index %d\n", add
->bindex
);
234 inode
= d_inode(add
->path
.dentry
);
236 if (unlikely(!inode
->i_nlink
)) {
237 pr_err("no existence %s\n", add
->pathname
);
242 if (unlikely(inode
->i_sb
== sb
)) {
243 pr_err("%s must be outside\n", add
->pathname
);
247 if (unlikely(au_test_fs_unsuppoted(inode
->i_sb
))) {
248 pr_err("unsupported filesystem, %s (%s)\n",
249 add
->pathname
, au_sbtype(inode
->i_sb
));
253 if (unlikely(inode
->i_sb
->s_stack_depth
)) {
254 pr_err("already stacked, %s (%s)\n",
255 add
->pathname
, au_sbtype(inode
->i_sb
));
259 err
= test_br(d_inode(add
->path
.dentry
), add
->perm
, add
->pathname
);
264 return 0; /* success */
267 for (bindex
= 0; bindex
<= bbot
; bindex
++)
268 if (unlikely(test_overlap(sb
, add
->path
.dentry
,
269 au_h_dptr(root
, bindex
)))) {
270 pr_err("%s is overlapped\n", add
->pathname
);
275 if (au_opt_test(au_mntflags(sb
), WARN_PERM
)) {
276 h_dentry
= au_h_dptr(root
, 0);
277 h_inode
= d_inode(h_dentry
);
278 if ((h_inode
->i_mode
& S_IALLUGO
) != (inode
->i_mode
& S_IALLUGO
)
279 || !uid_eq(h_inode
->i_uid
, inode
->i_uid
)
280 || !gid_eq(h_inode
->i_gid
, inode
->i_gid
))
281 pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
283 i_uid_read(inode
), i_gid_read(inode
),
284 (inode
->i_mode
& S_IALLUGO
),
285 i_uid_read(h_inode
), i_gid_read(h_inode
),
286 (h_inode
->i_mode
& S_IALLUGO
));
294 * initialize or clean the whiteouts for an adding branch
296 static int au_br_init_wh(struct super_block
*sb
, struct au_branch
*br
,
300 aufs_bindex_t bindex
;
301 struct inode
*h_inode
;
303 struct au_hinode
*hdir
;
304 struct dentry
*h_dentry
;
306 err
= vfsub_mnt_want_write(au_br_mnt(br
));
311 old_perm
= br
->br_perm
;
312 br
->br_perm
= new_perm
;
315 bindex
= au_br_index(sb
, br
->br_id
);
317 hdir
= au_hi(d_inode(sb
->s_root
), bindex
);
318 au_hn_inode_lock_nested(hdir
, AuLsc_I_PARENT
);
320 h_dentry
= au_br_dentry(br
);
321 h_inode
= d_inode(h_dentry
);
322 inode_lock_nested(h_inode
, AuLsc_I_PARENT
);
325 err
= au_wh_init(br
, sb
);
327 wbr_wh_write_lock(wbr
);
328 err
= au_wh_init(br
, sb
);
329 wbr_wh_write_unlock(wbr
);
332 au_hn_inode_unlock(hdir
);
334 inode_unlock(h_inode
);
335 vfsub_mnt_drop_write(au_br_mnt(br
));
336 br
->br_perm
= old_perm
;
338 if (!err
&& wbr
&& !au_br_writable(new_perm
)) {
339 au_delayed_kfree(wbr
);
347 static int au_wbr_init(struct au_branch
*br
, struct super_block
*sb
,
355 au_rw_init(&wbr
->wbr_wh_rwsem
);
356 atomic_set(&wbr
->wbr_wh_running
, 0);
359 * a limit for rmdir/rename a dir
360 * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
362 err
= vfs_statfs(&br
->br_path
, &kst
);
366 if (kst
.f_namelen
>= NAME_MAX
)
367 err
= au_br_init_wh(sb
, br
, perm
);
369 pr_err("%pd(%s), unsupported namelen %ld\n",
371 au_sbtype(au_br_dentry(br
)->d_sb
), kst
.f_namelen
);
377 /* initialize a new branch */
378 static int au_br_init(struct au_branch
*br
, struct super_block
*sb
,
379 struct au_opt_add
*add
)
382 struct inode
*h_inode
;
385 mutex_init(&br
->br_xino
.xi_nondir_mtx
);
386 br
->br_perm
= add
->perm
;
387 br
->br_path
= add
->path
; /* set first, path_get() later */
388 spin_lock_init(&br
->br_dykey_lock
);
389 au_br_count_init(br
);
390 atomic_set(&br
->br_xino_running
, 0);
391 br
->br_id
= au_new_br_id(sb
);
392 AuDebugOn(br
->br_id
< 0);
394 if (au_br_writable(add
->perm
)) {
395 err
= au_wbr_init(br
, sb
, add
->perm
);
400 if (au_opt_test(au_mntflags(sb
), XINO
)) {
401 h_inode
= d_inode(add
->path
.dentry
);
402 err
= au_xino_br(sb
, br
, h_inode
->i_ino
,
403 au_sbr(sb
, 0)->br_xino
.xi_file
, /*do_test*/1);
405 AuDebugOn(br
->br_xino
.xi_file
);
411 path_get(&br
->br_path
);
412 goto out
; /* success */
415 memset(&br
->br_path
, 0, sizeof(br
->br_path
));
420 static void au_br_do_add_brp(struct au_sbinfo
*sbinfo
, aufs_bindex_t bindex
,
421 struct au_branch
*br
, aufs_bindex_t bbot
,
422 aufs_bindex_t amount
)
424 struct au_branch
**brp
;
426 AuRwMustWriteLock(&sbinfo
->si_rwsem
);
428 brp
= sbinfo
->si_branch
+ bindex
;
429 memmove(brp
+ 1, brp
, sizeof(*brp
) * amount
);
432 if (unlikely(bbot
< 0))
436 static void au_br_do_add_hdp(struct au_dinfo
*dinfo
, aufs_bindex_t bindex
,
437 aufs_bindex_t bbot
, aufs_bindex_t amount
)
439 struct au_hdentry
*hdp
;
441 AuRwMustWriteLock(&dinfo
->di_rwsem
);
443 hdp
= au_hdentry(dinfo
, bindex
);
444 memmove(hdp
+ 1, hdp
, sizeof(*hdp
) * amount
);
445 au_h_dentry_init(hdp
);
447 if (unlikely(bbot
< 0))
451 static void au_br_do_add_hip(struct au_iinfo
*iinfo
, aufs_bindex_t bindex
,
452 aufs_bindex_t bbot
, aufs_bindex_t amount
)
454 struct au_hinode
*hip
;
456 AuRwMustWriteLock(&iinfo
->ii_rwsem
);
458 hip
= au_hinode(iinfo
, bindex
);
459 memmove(hip
+ 1, hip
, sizeof(*hip
) * amount
);
462 if (unlikely(bbot
< 0))
466 static void au_br_do_add(struct super_block
*sb
, struct au_branch
*br
,
467 aufs_bindex_t bindex
)
469 struct dentry
*root
, *h_dentry
;
470 struct inode
*root_inode
, *h_inode
;
471 aufs_bindex_t bbot
, amount
;
474 root_inode
= d_inode(root
);
476 amount
= bbot
+ 1 - bindex
;
477 h_dentry
= au_br_dentry(br
);
479 au_br_do_add_brp(au_sbi(sb
), bindex
, br
, bbot
, amount
);
480 au_br_do_add_hdp(au_di(root
), bindex
, bbot
, amount
);
481 au_br_do_add_hip(au_ii(root_inode
), bindex
, bbot
, amount
);
482 au_set_h_dptr(root
, bindex
, dget(h_dentry
));
483 h_inode
= d_inode(h_dentry
);
484 au_set_h_iptr(root_inode
, bindex
, au_igrab(h_inode
), /*flags*/0);
488 int au_br_add(struct super_block
*sb
, struct au_opt_add
*add
, int remount
)
491 aufs_bindex_t bbot
, add_bindex
;
492 struct dentry
*root
, *h_dentry
;
493 struct inode
*root_inode
;
494 struct au_branch
*add_branch
;
497 root_inode
= d_inode(root
);
498 IMustLock(root_inode
);
499 IiMustWriteLock(root_inode
);
500 err
= test_add(sb
, add
, remount
);
501 if (unlikely(err
< 0))
505 goto out
; /* success */
509 add_branch
= au_br_alloc(sb
, bbot
+ 2, add
->perm
);
510 err
= PTR_ERR(add_branch
);
511 if (IS_ERR(add_branch
))
514 err
= au_br_init(add_branch
, sb
, add
);
516 au_br_do_free(add_branch
);
520 add_bindex
= add
->bindex
;
522 au_br_do_add(sb
, add_branch
, add_bindex
);
524 sysaufs_brs_del(sb
, add_bindex
);
525 au_br_do_add(sb
, add_branch
, add_bindex
);
526 sysaufs_brs_add(sb
, add_bindex
);
529 h_dentry
= add
->path
.dentry
;
531 au_cpup_attr_all(root_inode
, /*force*/1);
532 sb
->s_maxbytes
= h_dentry
->d_sb
->s_maxbytes
;
534 au_add_nlink(root_inode
, d_inode(h_dentry
));
537 * this test/set prevents aufs from handling unnecesary notify events
538 * of xino files, in case of re-adding a writable branch which was
539 * once detached from aufs.
541 if (au_xino_brid(sb
) < 0
542 && au_br_writable(add_branch
->br_perm
)
543 && !au_test_fs_bad_xino(h_dentry
->d_sb
)
544 && add_branch
->br_xino
.xi_file
545 && add_branch
->br_xino
.xi_file
->f_path
.dentry
->d_parent
== h_dentry
)
546 au_xino_brid_set(sb
, add_branch
->br_id
);
552 /* ---------------------------------------------------------------------- */
554 static unsigned long long au_farray_cb(struct super_block
*sb
, void *a
,
555 unsigned long long max __maybe_unused
,
558 unsigned long long n
;
560 struct au_sphlhead
*files
;
561 struct au_finfo
*finfo
;
565 files
= &au_sbi(sb
)->si_files
;
566 spin_lock(&files
->spin
);
567 hlist_for_each_entry(finfo
, &files
->head
, fi_hlist
) {
570 && !special_file(file_inode(f
)->i_mode
)) {
577 spin_unlock(&files
->spin
);
582 static struct file
**au_farray_alloc(struct super_block
*sb
,
583 unsigned long long *max
)
585 *max
= au_nfiles(sb
);
586 return au_array_alloc(max
, au_farray_cb
, sb
, /*arg*/NULL
);
589 static void au_farray_free(struct file
**a
, unsigned long long max
)
591 unsigned long long ull
;
593 for (ull
= 0; ull
< max
; ull
++)
599 /* ---------------------------------------------------------------------- */
605 /* to show the line number, do not make it inlined function */
606 #define AuVerbose(do_info, fmt, ...) do { \
608 pr_info(fmt, ##__VA_ARGS__); \
611 static int au_test_ibusy(struct inode
*inode
, aufs_bindex_t btop
,
614 return (inode
&& !S_ISDIR(inode
->i_mode
)) || btop
== bbot
;
617 static int au_test_dbusy(struct dentry
*dentry
, aufs_bindex_t btop
,
620 return au_test_ibusy(d_inode(dentry
), btop
, bbot
);
624 * test if the branch is deletable or not.
626 static int test_dentry_busy(struct dentry
*root
, aufs_bindex_t bindex
,
627 unsigned int sigen
, const unsigned int verbose
)
629 int err
, i
, j
, ndentry
;
630 aufs_bindex_t btop
, bbot
;
631 struct au_dcsub_pages dpages
;
632 struct au_dpage
*dpage
;
635 err
= au_dpages_init(&dpages
, GFP_NOFS
);
638 err
= au_dcsub_pages(&dpages
, root
, NULL
, NULL
);
642 for (i
= 0; !err
&& i
< dpages
.ndpage
; i
++) {
643 dpage
= dpages
.dpages
+ i
;
644 ndentry
= dpage
->ndentry
;
645 for (j
= 0; !err
&& j
< ndentry
; j
++) {
646 d
= dpage
->dentries
[j
];
647 AuDebugOn(au_dcount(d
) <= 0);
648 if (!au_digen_test(d
, sigen
)) {
649 di_read_lock_child(d
, AuLock_IR
);
650 if (unlikely(au_dbrange_test(d
))) {
651 di_read_unlock(d
, AuLock_IR
);
655 di_write_lock_child(d
);
656 if (unlikely(au_dbrange_test(d
))) {
660 err
= au_reval_dpath(d
, sigen
);
662 di_downgrade_lock(d
, AuLock_IR
);
669 /* AuDbgDentry(d); */
674 && au_h_dptr(d
, bindex
)
675 && au_test_dbusy(d
, btop
, bbot
)) {
677 AuVerbose(verbose
, "busy %pd\n", d
);
680 di_read_unlock(d
, AuLock_IR
);
685 au_dpages_free(&dpages
);
690 static int test_inode_busy(struct super_block
*sb
, aufs_bindex_t bindex
,
691 unsigned int sigen
, const unsigned int verbose
)
694 unsigned long long max
, ull
;
695 struct inode
*i
, **array
;
696 aufs_bindex_t btop
, bbot
;
698 array
= au_iarray_alloc(sb
, &max
);
699 err
= PTR_ERR(array
);
704 AuDbg("b%d\n", bindex
);
705 for (ull
= 0; !err
&& ull
< max
; ull
++) {
709 if (i
->i_ino
== AUFS_ROOT_INO
)
713 if (au_iigen(i
, NULL
) == sigen
)
714 ii_read_lock_child(i
);
716 ii_write_lock_child(i
);
717 err
= au_refresh_hinode_self(i
);
720 ii_downgrade_lock(i
);
731 && au_h_iptr(i
, bindex
)
732 && au_test_ibusy(i
, btop
, bbot
)) {
734 AuVerbose(verbose
, "busy i%lu\n", i
->i_ino
);
739 au_iarray_free(array
, max
);
745 static int test_children_busy(struct dentry
*root
, aufs_bindex_t bindex
,
746 const unsigned int verbose
)
751 sigen
= au_sigen(root
->d_sb
);
752 DiMustNoWaiters(root
);
753 IiMustNoWaiters(d_inode(root
));
754 di_write_unlock(root
);
755 err
= test_dentry_busy(root
, bindex
, sigen
, verbose
);
757 err
= test_inode_busy(root
->d_sb
, bindex
, sigen
, verbose
);
758 di_write_lock_child(root
); /* aufs_write_lock() calls ..._child() */
763 static int test_dir_busy(struct file
*file
, aufs_bindex_t br_id
,
764 struct file
**to_free
, int *idx
)
767 unsigned char matched
, root
;
768 aufs_bindex_t bindex
, bbot
;
769 struct au_fidir
*fidir
;
770 struct au_hfile
*hfile
;
773 root
= IS_ROOT(file
->f_path
.dentry
);
776 to_free
[*idx
] = file
;
782 fidir
= au_fi(file
)->fi_hdir
;
784 bbot
= au_fbbot_dir(file
);
785 for (bindex
= au_fbtop(file
); bindex
<= bbot
; bindex
++) {
786 hfile
= fidir
->fd_hfile
+ bindex
;
790 if (hfile
->hf_br
->br_id
== br_id
) {
802 static int test_file_busy(struct super_block
*sb
, aufs_bindex_t br_id
,
803 struct file
**to_free
, int opened
)
806 unsigned long long ull
, max
;
808 struct file
*file
, **array
;
810 struct au_hfile
*hfile
;
812 array
= au_farray_alloc(sb
, &max
);
813 err
= PTR_ERR(array
);
820 di_write_unlock(root
);
821 for (ull
= 0; ull
< max
; ull
++) {
826 /* AuDbg("%pD\n", file); */
828 btop
= au_fbtop(file
);
829 if (!d_is_dir(file
->f_path
.dentry
)) {
830 hfile
= &au_fi(file
)->fi_htop
;
831 if (hfile
->hf_br
->br_id
== br_id
)
834 err
= test_dir_busy(file
, br_id
, to_free
, &idx
);
835 fi_read_unlock(file
);
839 di_write_lock_child(root
);
840 au_farray_free(array
, max
);
841 AuDebugOn(idx
> opened
);
847 static void br_del_file(struct file
**to_free
, unsigned long long opened
,
850 unsigned long long ull
;
851 aufs_bindex_t bindex
, btop
, bbot
, bfound
;
853 struct au_fidir
*fidir
;
854 struct au_hfile
*hfile
;
856 for (ull
= 0; ull
< opened
; ull
++) {
861 /* AuDbg("%pD\n", file); */
862 AuDebugOn(!d_is_dir(file
->f_path
.dentry
));
864 fidir
= au_fi(file
)->fi_hdir
;
867 btop
= au_fbtop(file
);
868 bbot
= au_fbbot_dir(file
);
869 for (bindex
= btop
; bindex
<= bbot
; bindex
++) {
870 hfile
= fidir
->fd_hfile
+ bindex
;
874 if (hfile
->hf_br
->br_id
== br_id
) {
879 AuDebugOn(bfound
< 0);
880 au_set_h_fptr(file
, bfound
, NULL
);
881 if (bfound
== btop
) {
882 for (btop
++; btop
<= bbot
; btop
++)
883 if (au_hf_dir(file
, btop
)) {
884 au_set_fbtop(file
, btop
);
888 fi_write_unlock(file
);
892 static void au_br_do_del_brp(struct au_sbinfo
*sbinfo
,
893 const aufs_bindex_t bindex
,
894 const aufs_bindex_t bbot
)
896 struct au_branch
**brp
, **p
;
898 AuRwMustWriteLock(&sbinfo
->si_rwsem
);
900 brp
= sbinfo
->si_branch
+ bindex
;
902 memmove(brp
, brp
+ 1, sizeof(*brp
) * (bbot
- bindex
));
903 sbinfo
->si_branch
[0 + bbot
] = NULL
;
906 p
= au_krealloc(sbinfo
->si_branch
, sizeof(*p
) * bbot
, AuGFP_SBILIST
,
909 sbinfo
->si_branch
= p
;
913 static void au_br_do_del_hdp(struct au_dinfo
*dinfo
, const aufs_bindex_t bindex
,
914 const aufs_bindex_t bbot
)
916 struct au_hdentry
*hdp
, *p
;
918 AuRwMustWriteLock(&dinfo
->di_rwsem
);
920 hdp
= au_hdentry(dinfo
, bindex
);
922 memmove(hdp
, hdp
+ 1, sizeof(*hdp
) * (bbot
- bindex
));
923 /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
926 p
= au_krealloc(dinfo
->di_hdentry
, sizeof(*p
) * bbot
, AuGFP_SBILIST
,
929 dinfo
->di_hdentry
= p
;
933 static void au_br_do_del_hip(struct au_iinfo
*iinfo
, const aufs_bindex_t bindex
,
934 const aufs_bindex_t bbot
)
936 struct au_hinode
*hip
, *p
;
938 AuRwMustWriteLock(&iinfo
->ii_rwsem
);
940 hip
= au_hinode(iinfo
, bindex
);
942 memmove(hip
, hip
+ 1, sizeof(*hip
) * (bbot
- bindex
));
943 /* au_hinode_init(au_hinode(iinfo, bbot)); */
946 p
= au_krealloc(iinfo
->ii_hinode
, sizeof(*p
) * bbot
, AuGFP_SBILIST
,
949 iinfo
->ii_hinode
= p
;
953 static void au_br_do_del(struct super_block
*sb
, aufs_bindex_t bindex
,
954 struct au_branch
*br
)
957 struct au_sbinfo
*sbinfo
;
958 struct dentry
*root
, *h_root
;
959 struct inode
*inode
, *h_inode
;
960 struct au_hinode
*hinode
;
965 inode
= d_inode(root
);
967 bbot
= sbinfo
->si_bbot
;
969 h_root
= au_h_dptr(root
, bindex
);
970 hinode
= au_hi(inode
, bindex
);
971 h_inode
= au_igrab(hinode
->hi_inode
);
975 au_br_do_del_brp(sbinfo
, bindex
, bbot
);
976 au_br_do_del_hdp(au_di(root
), bindex
, bbot
);
977 au_br_do_del_hip(au_ii(inode
), bindex
, bbot
);
985 static unsigned long long empty_cb(struct super_block
*sb
, void *array
,
986 unsigned long long max
, void *arg
)
991 int au_br_del(struct super_block
*sb
, struct au_opt_del
*del
, int remount
)
994 unsigned long long opened
;
995 unsigned int mnt_flags
;
996 aufs_bindex_t bindex
, bbot
, br_id
;
997 unsigned char do_wh
, verbose
;
998 struct au_branch
*br
;
1000 struct dentry
*root
;
1001 struct file
**to_free
;
1007 bindex
= au_find_dbindex(root
, del
->h_path
.dentry
);
1010 goto out
; /* success */
1012 pr_err("%s no such branch\n", del
->pathname
);
1015 AuDbg("bindex b%d\n", bindex
);
1018 mnt_flags
= au_mntflags(sb
);
1019 verbose
= !!au_opt_test(mnt_flags
, VERBOSE
);
1020 bbot
= au_sbbot(sb
);
1021 if (unlikely(!bbot
)) {
1022 AuVerbose(verbose
, "no more branches left\n");
1025 br
= au_sbr(sb
, bindex
);
1026 AuDebugOn(!path_equal(&br
->br_path
, &del
->h_path
));
1029 opened
= au_br_count(br
);
1030 if (unlikely(opened
)) {
1031 to_free
= au_array_alloc(&opened
, empty_cb
, sb
, NULL
);
1032 err
= PTR_ERR(to_free
);
1033 if (IS_ERR(to_free
))
1036 err
= test_file_busy(sb
, br_id
, to_free
, opened
);
1037 if (unlikely(err
)) {
1038 AuVerbose(verbose
, "%llu file(s) opened\n", opened
);
1044 do_wh
= wbr
&& (wbr
->wbr_whbase
|| wbr
->wbr_plink
|| wbr
->wbr_orph
);
1046 /* instead of WbrWhMustWriteLock(wbr) */
1047 SiMustWriteLock(sb
);
1048 for (i
= 0; i
< AuBrWh_Last
; i
++) {
1049 dput(wbr
->wbr_wh
[i
]);
1050 wbr
->wbr_wh
[i
] = NULL
;
1054 err
= test_children_busy(root
, bindex
, verbose
);
1055 if (unlikely(err
)) {
1064 * now we confirmed the branch is deletable.
1065 * let's free the remaining opened dirs on the branch.
1067 di_write_unlock(root
);
1068 br_del_file(to_free
, opened
, br_id
);
1069 di_write_lock_child(root
);
1073 au_br_do_del(sb
, bindex
, br
);
1075 sysaufs_brs_del(sb
, bindex
);
1076 au_br_do_del(sb
, bindex
, br
);
1077 sysaufs_brs_add(sb
, bindex
);
1081 au_cpup_attr_all(d_inode(root
), /*force*/1);
1082 sb
->s_maxbytes
= au_sbr_sb(sb
, 0)->s_maxbytes
;
1084 au_sub_nlink(d_inode(root
), d_inode(del
->h_path
.dentry
));
1085 if (au_opt_test(mnt_flags
, PLINK
))
1086 au_plink_half_refresh(sb
, br_id
);
1088 if (au_xino_brid(sb
) == br_id
)
1089 au_xino_brid_set(sb
, -1);
1090 goto out
; /* success */
1094 rerr
= au_br_init_wh(sb
, br
, br
->br_perm
);
1096 pr_warn("failed re-creating base whiteout, %s. (%d)\n",
1097 del
->pathname
, rerr
);
1100 au_farray_free(to_free
, opened
);
1104 /* ---------------------------------------------------------------------- */
1106 static int au_ibusy(struct super_block
*sb
, struct aufs_ibusy __user
*arg
)
1109 aufs_bindex_t btop
, bbot
;
1110 struct aufs_ibusy ibusy
;
1111 struct inode
*inode
, *h_inode
;
1114 if (unlikely(!capable(CAP_SYS_ADMIN
)))
1117 err
= copy_from_user(&ibusy
, arg
, sizeof(ibusy
));
1119 err
= !access_ok(VERIFY_WRITE
, &arg
->h_ino
, sizeof(arg
->h_ino
));
1120 if (unlikely(err
)) {
1127 si_read_lock(sb
, AuLock_FLUSH
);
1128 if (unlikely(ibusy
.bindex
< 0 || ibusy
.bindex
> au_sbbot(sb
)))
1132 ibusy
.h_ino
= 0; /* invalid */
1133 inode
= ilookup(sb
, ibusy
.ino
);
1135 || inode
->i_ino
== AUFS_ROOT_INO
1136 || au_is_bad_inode(inode
))
1139 ii_read_lock_child(inode
);
1140 btop
= au_ibtop(inode
);
1141 bbot
= au_ibbot(inode
);
1142 if (btop
<= ibusy
.bindex
&& ibusy
.bindex
<= bbot
) {
1143 h_inode
= au_h_iptr(inode
, ibusy
.bindex
);
1144 if (h_inode
&& au_test_ibusy(inode
, btop
, bbot
))
1145 ibusy
.h_ino
= h_inode
->i_ino
;
1147 ii_read_unlock(inode
);
1153 err
= __put_user(ibusy
.h_ino
, &arg
->h_ino
);
1154 if (unlikely(err
)) {
1163 long au_ibusy_ioctl(struct file
*file
, unsigned long arg
)
1165 return au_ibusy(file
->f_path
.dentry
->d_sb
, (void __user
*)arg
);
1168 #ifdef CONFIG_COMPAT
1169 long au_ibusy_compat_ioctl(struct file
*file
, unsigned long arg
)
1171 return au_ibusy(file
->f_path
.dentry
->d_sb
, compat_ptr(arg
));
1175 /* ---------------------------------------------------------------------- */
1178 * change a branch permission
1181 static void au_warn_ima(void)
1184 /* since it doesn't support mark_files_ro() */
1185 AuWarn1("RW -> RO makes IMA to produce wrong message\n");
1189 static int do_need_sigen_inc(int a
, int b
)
1191 return au_br_whable(a
) && !au_br_whable(b
);
1194 static int need_sigen_inc(int old
, int new)
1196 return do_need_sigen_inc(old
, new)
1197 || do_need_sigen_inc(new, old
);
1200 static int au_br_mod_files_ro(struct super_block
*sb
, aufs_bindex_t bindex
)
1203 unsigned int mnt_flags
;
1204 unsigned long long ull
, max
;
1205 aufs_bindex_t br_id
;
1206 unsigned char verbose
, writer
;
1207 struct file
*file
, *hf
, **array
;
1208 struct au_hfile
*hfile
;
1210 mnt_flags
= au_mntflags(sb
);
1211 verbose
= !!au_opt_test(mnt_flags
, VERBOSE
);
1213 array
= au_farray_alloc(sb
, &max
);
1214 err
= PTR_ERR(array
);
1219 br_id
= au_sbr_id(sb
, bindex
);
1220 for (ull
= 0; ull
< max
; ull
++) {
1222 if (unlikely(!file
))
1225 /* AuDbg("%pD\n", file); */
1227 if (unlikely(au_test_mmapped(file
))) {
1229 AuVerbose(verbose
, "mmapped %pD\n", file
);
1231 FiMustNoWaiters(file
);
1232 fi_read_unlock(file
);
1236 hfile
= &au_fi(file
)->fi_htop
;
1237 hf
= hfile
->hf_file
;
1238 if (!d_is_reg(file
->f_path
.dentry
)
1239 || !(file
->f_mode
& FMODE_WRITE
)
1240 || hfile
->hf_br
->br_id
!= br_id
1241 || !(hf
->f_mode
& FMODE_WRITE
))
1248 FiMustNoWaiters(file
);
1249 fi_read_unlock(file
);
1257 for (ull
= 0; ull
< max
; ull
++) {
1262 /* todo: already flushed? */
1264 * fs/super.c:mark_files_ro() is gone, but aufs keeps its
1265 * approach which resets f_mode and calls mnt_drop_write() and
1266 * file_release_write() for each file, because the branch
1267 * attribute in aufs world is totally different from the native
1270 /* fi_read_lock(file); */
1271 hfile
= &au_fi(file
)->fi_htop
;
1272 hf
= hfile
->hf_file
;
1273 /* fi_read_unlock(file); */
1274 spin_lock(&hf
->f_lock
);
1275 writer
= !!(hf
->f_mode
& FMODE_WRITER
);
1276 hf
->f_mode
&= ~(FMODE_WRITE
| FMODE_WRITER
);
1277 spin_unlock(&hf
->f_lock
);
1279 put_write_access(file_inode(hf
));
1280 __mnt_drop_write(hf
->f_path
.mnt
);
1285 au_farray_free(array
, max
);
1291 int au_br_mod(struct super_block
*sb
, struct au_opt_mod
*mod
, int remount
,
1295 aufs_bindex_t bindex
;
1296 struct dentry
*root
;
1297 struct au_branch
*br
;
1298 struct au_br_fhsm
*bf
;
1301 bindex
= au_find_dbindex(root
, mod
->h_root
);
1304 return 0; /* success */
1306 pr_err("%s no such branch\n", mod
->path
);
1309 AuDbg("bindex b%d\n", bindex
);
1311 err
= test_br(d_inode(mod
->h_root
), mod
->perm
, mod
->path
);
1315 br
= au_sbr(sb
, bindex
);
1316 AuDebugOn(mod
->h_root
!= au_br_dentry(br
));
1317 if (br
->br_perm
== mod
->perm
)
1318 return 0; /* success */
1320 /* pre-allocate for non-fhsm --> fhsm */
1322 if (!au_br_fhsm(br
->br_perm
) && au_br_fhsm(mod
->perm
)) {
1323 err
= au_fhsm_br_alloc(br
);
1330 if (au_br_writable(br
->br_perm
)) {
1331 /* remove whiteout base */
1332 err
= au_br_init_wh(sb
, br
, mod
->perm
);
1336 if (!au_br_writable(mod
->perm
)) {
1337 /* rw --> ro, file might be mmapped */
1338 DiMustNoWaiters(root
);
1339 IiMustNoWaiters(d_inode(root
));
1340 di_write_unlock(root
);
1341 err
= au_br_mod_files_ro(sb
, bindex
);
1342 /* aufs_write_lock() calls ..._child() */
1343 di_write_lock_child(root
);
1345 if (unlikely(err
)) {
1347 br
->br_wbr
= kzalloc(sizeof(*br
->br_wbr
),
1350 rerr
= au_wbr_init(br
, sb
, br
->br_perm
);
1351 if (unlikely(rerr
)) {
1352 AuIOErr("nested error %d (%d)\n",
1354 br
->br_perm
= mod
->perm
;
1358 } else if (au_br_writable(mod
->perm
)) {
1361 br
->br_wbr
= kzalloc(sizeof(*br
->br_wbr
), GFP_NOFS
);
1363 err
= au_wbr_init(br
, sb
, mod
->perm
);
1364 if (unlikely(err
)) {
1365 au_delayed_kfree(br
->br_wbr
);
1373 if (au_br_fhsm(br
->br_perm
)) {
1374 if (!au_br_fhsm(mod
->perm
)) {
1375 /* fhsm --> non-fhsm */
1376 au_br_fhsm_fin(br
->br_fhsm
);
1377 au_delayed_kfree(br
->br_fhsm
);
1380 } else if (au_br_fhsm(mod
->perm
))
1381 /* non-fhsm --> fhsm */
1384 *do_refresh
|= need_sigen_inc(br
->br_perm
, mod
->perm
);
1385 br
->br_perm
= mod
->perm
;
1386 goto out
; /* success */
1390 au_delayed_kfree(bf
);
1396 /* ---------------------------------------------------------------------- */
1398 int au_br_stfs(struct au_branch
*br
, struct aufs_stfs
*stfs
)
1401 struct kstatfs kstfs
;
1403 err
= vfs_statfs(&br
->br_path
, &kstfs
);
1405 stfs
->f_blocks
= kstfs
.f_blocks
;
1406 stfs
->f_bavail
= kstfs
.f_bavail
;
1407 stfs
->f_files
= kstfs
.f_files
;
1408 stfs
->f_ffree
= kstfs
.f_ffree
;