]>
git.proxmox.com Git - mirror_ubuntu-artful-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
);
37 if (br
->br_xino
.xi_file
)
38 fput(br
->br_xino
.xi_file
);
39 for (i
= br
->br_xino
.xi_nondir
.total
- 1; i
>= 0; i
--)
40 AuDebugOn(br
->br_xino
.xi_nondir
.array
[i
]);
41 kfree(br
->br_xino
.xi_nondir
.array
);
43 AuDebugOn(au_br_count(br
));
48 for (i
= 0; i
< AuBrWh_Last
; i
++)
50 AuDebugOn(atomic_read(&wbr
->wbr_wh_running
));
51 AuRwDestroy(&wbr
->wbr_wh_rwsem
);
55 au_br_fhsm_fin(br
->br_fhsm
);
60 for (i
= 0; i
< AuBrDynOp
; i
++, key
++)
66 /* recursive lock, s_umount of branch's */
68 path_put(&br
->br_path
);
77 void au_br_free(struct au_sbinfo
*sbinfo
)
80 struct au_branch
**br
;
82 AuRwMustWriteLock(&sbinfo
->si_rwsem
);
84 bmax
= sbinfo
->si_bbot
+ 1;
85 br
= sbinfo
->si_branch
;
91 * find the index of a branch which is specified by @br_id.
93 int au_br_index(struct super_block
*sb
, aufs_bindex_t br_id
)
95 aufs_bindex_t bindex
, bbot
;
98 for (bindex
= 0; bindex
<= bbot
; bindex
++)
99 if (au_sbr_id(sb
, bindex
) == br_id
)
104 /* ---------------------------------------------------------------------- */
110 static int test_overlap(struct super_block
*sb
, struct dentry
*h_adding
,
111 struct dentry
*h_root
)
113 if (unlikely(h_adding
== h_root
114 || au_test_loopback_overlap(sb
, h_adding
)))
116 if (h_adding
->d_sb
!= h_root
->d_sb
)
118 return au_test_subdir(h_adding
, h_root
)
119 || au_test_subdir(h_root
, h_adding
);
123 * returns a newly allocated branch. @new_nbranch is a number of branches
124 * after adding a branch.
126 static struct au_branch
*au_br_alloc(struct super_block
*sb
, int new_nbranch
,
129 struct au_branch
*add_branch
;
135 add_branch
= kzalloc(sizeof(*add_branch
), GFP_NOFS
);
136 if (unlikely(!add_branch
))
138 add_branch
->br_xino
.xi_nondir
.total
= 8; /* initial size */
139 add_branch
->br_xino
.xi_nondir
.array
140 = kcalloc(sizeof(ino_t
), add_branch
->br_xino
.xi_nondir
.total
,
142 if (unlikely(!add_branch
->br_xino
.xi_nondir
.array
))
145 err
= au_hnotify_init_br(add_branch
, perm
);
149 if (au_br_writable(perm
)) {
150 /* may be freed separately at changing the branch permission */
151 add_branch
->br_wbr
= kzalloc(sizeof(*add_branch
->br_wbr
),
153 if (unlikely(!add_branch
->br_wbr
))
157 if (au_br_fhsm(perm
)) {
158 err
= au_fhsm_br_alloc(add_branch
);
164 err
= au_sbr_realloc(au_sbi(sb
), new_nbranch
, /*may_shrink*/0);
166 err
= au_di_realloc(au_di(root
), new_nbranch
, /*may_shrink*/0);
168 inode
= d_inode(root
);
169 err
= au_hinode_realloc(au_ii(inode
), new_nbranch
,
173 return add_branch
; /* success */
176 kfree(add_branch
->br_wbr
);
178 au_hnotify_fin_br(add_branch
);
180 kfree(add_branch
->br_xino
.xi_nondir
.array
);
188 * test if the branch permission is legal or not.
190 static int test_br(struct inode
*inode
, int brperm
, char *path
)
194 err
= (au_br_writable(brperm
) && IS_RDONLY(inode
));
199 pr_err("write permission for readonly mount or inode, %s\n", path
);
207 * 0: success, the caller will add it
208 * plus: success, it is already unified, the caller should ignore it
211 static int test_add(struct super_block
*sb
, struct au_opt_add
*add
, int remount
)
214 aufs_bindex_t bbot
, bindex
;
215 struct dentry
*root
, *h_dentry
;
216 struct inode
*inode
, *h_inode
;
220 if (unlikely(bbot
>= 0
221 && au_find_dbindex(root
, add
->path
.dentry
) >= 0)) {
225 pr_err("%s duplicated\n", add
->pathname
);
230 err
= -ENOSPC
; /* -E2BIG; */
231 if (unlikely(AUFS_BRANCH_MAX
<= add
->bindex
232 || AUFS_BRANCH_MAX
- 1 <= bbot
)) {
233 pr_err("number of branches exceeded %s\n", add
->pathname
);
238 if (unlikely(add
->bindex
< 0 || bbot
+ 1 < add
->bindex
)) {
239 pr_err("bad index %d\n", add
->bindex
);
243 inode
= d_inode(add
->path
.dentry
);
245 if (unlikely(!inode
->i_nlink
)) {
246 pr_err("no existence %s\n", add
->pathname
);
251 if (unlikely(inode
->i_sb
== sb
)) {
252 pr_err("%s must be outside\n", add
->pathname
);
256 if (unlikely(au_test_fs_unsuppoted(inode
->i_sb
))) {
257 pr_err("unsupported filesystem, %s (%s)\n",
258 add
->pathname
, au_sbtype(inode
->i_sb
));
262 if (unlikely(inode
->i_sb
->s_stack_depth
)) {
263 pr_err("already stacked, %s (%s)\n",
264 add
->pathname
, au_sbtype(inode
->i_sb
));
268 err
= test_br(d_inode(add
->path
.dentry
), add
->perm
, add
->pathname
);
273 return 0; /* success */
276 for (bindex
= 0; bindex
<= bbot
; bindex
++)
277 if (unlikely(test_overlap(sb
, add
->path
.dentry
,
278 au_h_dptr(root
, bindex
)))) {
279 pr_err("%s is overlapped\n", add
->pathname
);
284 if (au_opt_test(au_mntflags(sb
), WARN_PERM
)) {
285 h_dentry
= au_h_dptr(root
, 0);
286 h_inode
= d_inode(h_dentry
);
287 if ((h_inode
->i_mode
& S_IALLUGO
) != (inode
->i_mode
& S_IALLUGO
)
288 || !uid_eq(h_inode
->i_uid
, inode
->i_uid
)
289 || !gid_eq(h_inode
->i_gid
, inode
->i_gid
))
290 pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
292 i_uid_read(inode
), i_gid_read(inode
),
293 (inode
->i_mode
& S_IALLUGO
),
294 i_uid_read(h_inode
), i_gid_read(h_inode
),
295 (h_inode
->i_mode
& S_IALLUGO
));
303 * initialize or clean the whiteouts for an adding branch
305 static int au_br_init_wh(struct super_block
*sb
, struct au_branch
*br
,
309 aufs_bindex_t bindex
;
310 struct inode
*h_inode
;
312 struct au_hinode
*hdir
;
313 struct dentry
*h_dentry
;
315 err
= vfsub_mnt_want_write(au_br_mnt(br
));
320 old_perm
= br
->br_perm
;
321 br
->br_perm
= new_perm
;
324 bindex
= au_br_index(sb
, br
->br_id
);
326 hdir
= au_hi(d_inode(sb
->s_root
), bindex
);
327 au_hn_inode_lock_nested(hdir
, AuLsc_I_PARENT
);
329 h_dentry
= au_br_dentry(br
);
330 h_inode
= d_inode(h_dentry
);
331 inode_lock_nested(h_inode
, AuLsc_I_PARENT
);
334 err
= au_wh_init(br
, sb
);
336 wbr_wh_write_lock(wbr
);
337 err
= au_wh_init(br
, sb
);
338 wbr_wh_write_unlock(wbr
);
341 au_hn_inode_unlock(hdir
);
343 inode_unlock(h_inode
);
344 vfsub_mnt_drop_write(au_br_mnt(br
));
345 br
->br_perm
= old_perm
;
347 if (!err
&& wbr
&& !au_br_writable(new_perm
)) {
356 static int au_wbr_init(struct au_branch
*br
, struct super_block
*sb
,
364 au_rw_init(&wbr
->wbr_wh_rwsem
);
365 atomic_set(&wbr
->wbr_wh_running
, 0);
368 * a limit for rmdir/rename a dir
369 * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
371 err
= vfs_statfs(&br
->br_path
, &kst
);
375 if (kst
.f_namelen
>= NAME_MAX
)
376 err
= au_br_init_wh(sb
, br
, perm
);
378 pr_err("%pd(%s), unsupported namelen %ld\n",
380 au_sbtype(au_br_dentry(br
)->d_sb
), kst
.f_namelen
);
386 /* initialize a new branch */
387 static int au_br_init(struct au_branch
*br
, struct super_block
*sb
,
388 struct au_opt_add
*add
)
391 struct inode
*h_inode
;
394 spin_lock_init(&br
->br_xino
.xi_nondir
.spin
);
395 init_waitqueue_head(&br
->br_xino
.xi_nondir
.wqh
);
396 br
->br_perm
= add
->perm
;
397 br
->br_path
= add
->path
; /* set first, path_get() later */
398 spin_lock_init(&br
->br_dykey_lock
);
399 au_br_count_init(br
);
400 atomic_set(&br
->br_xino_running
, 0);
401 br
->br_id
= au_new_br_id(sb
);
402 AuDebugOn(br
->br_id
< 0);
404 if (au_br_writable(add
->perm
)) {
405 err
= au_wbr_init(br
, sb
, add
->perm
);
410 if (au_opt_test(au_mntflags(sb
), XINO
)) {
411 h_inode
= d_inode(add
->path
.dentry
);
412 err
= au_xino_br(sb
, br
, h_inode
->i_ino
,
413 au_sbr(sb
, 0)->br_xino
.xi_file
, /*do_test*/1);
415 AuDebugOn(br
->br_xino
.xi_file
);
421 path_get(&br
->br_path
);
422 goto out
; /* success */
425 memset(&br
->br_path
, 0, sizeof(br
->br_path
));
430 static void au_br_do_add_brp(struct au_sbinfo
*sbinfo
, aufs_bindex_t bindex
,
431 struct au_branch
*br
, aufs_bindex_t bbot
,
432 aufs_bindex_t amount
)
434 struct au_branch
**brp
;
436 AuRwMustWriteLock(&sbinfo
->si_rwsem
);
438 brp
= sbinfo
->si_branch
+ bindex
;
439 memmove(brp
+ 1, brp
, sizeof(*brp
) * amount
);
442 if (unlikely(bbot
< 0))
446 static void au_br_do_add_hdp(struct au_dinfo
*dinfo
, aufs_bindex_t bindex
,
447 aufs_bindex_t bbot
, aufs_bindex_t amount
)
449 struct au_hdentry
*hdp
;
451 AuRwMustWriteLock(&dinfo
->di_rwsem
);
453 hdp
= au_hdentry(dinfo
, bindex
);
454 memmove(hdp
+ 1, hdp
, sizeof(*hdp
) * amount
);
455 au_h_dentry_init(hdp
);
457 if (unlikely(bbot
< 0))
461 static void au_br_do_add_hip(struct au_iinfo
*iinfo
, aufs_bindex_t bindex
,
462 aufs_bindex_t bbot
, aufs_bindex_t amount
)
464 struct au_hinode
*hip
;
466 AuRwMustWriteLock(&iinfo
->ii_rwsem
);
468 hip
= au_hinode(iinfo
, bindex
);
469 memmove(hip
+ 1, hip
, sizeof(*hip
) * amount
);
472 if (unlikely(bbot
< 0))
476 static void au_br_do_add(struct super_block
*sb
, struct au_branch
*br
,
477 aufs_bindex_t bindex
)
479 struct dentry
*root
, *h_dentry
;
480 struct inode
*root_inode
, *h_inode
;
481 aufs_bindex_t bbot
, amount
;
484 root_inode
= d_inode(root
);
486 amount
= bbot
+ 1 - bindex
;
487 h_dentry
= au_br_dentry(br
);
489 au_br_do_add_brp(au_sbi(sb
), bindex
, br
, bbot
, amount
);
490 au_br_do_add_hdp(au_di(root
), bindex
, bbot
, amount
);
491 au_br_do_add_hip(au_ii(root_inode
), bindex
, bbot
, amount
);
492 au_set_h_dptr(root
, bindex
, dget(h_dentry
));
493 h_inode
= d_inode(h_dentry
);
494 au_set_h_iptr(root_inode
, bindex
, au_igrab(h_inode
), /*flags*/0);
498 int au_br_add(struct super_block
*sb
, struct au_opt_add
*add
, int remount
)
501 aufs_bindex_t bbot
, add_bindex
;
502 struct dentry
*root
, *h_dentry
;
503 struct inode
*root_inode
;
504 struct au_branch
*add_branch
;
507 root_inode
= d_inode(root
);
508 IMustLock(root_inode
);
509 IiMustWriteLock(root_inode
);
510 err
= test_add(sb
, add
, remount
);
511 if (unlikely(err
< 0))
515 goto out
; /* success */
519 add_branch
= au_br_alloc(sb
, bbot
+ 2, add
->perm
);
520 err
= PTR_ERR(add_branch
);
521 if (IS_ERR(add_branch
))
524 err
= au_br_init(add_branch
, sb
, add
);
526 au_br_do_free(add_branch
);
530 add_bindex
= add
->bindex
;
532 au_br_do_add(sb
, add_branch
, add_bindex
);
534 sysaufs_brs_del(sb
, add_bindex
);
535 au_br_do_add(sb
, add_branch
, add_bindex
);
536 sysaufs_brs_add(sb
, add_bindex
);
539 h_dentry
= add
->path
.dentry
;
541 au_cpup_attr_all(root_inode
, /*force*/1);
542 sb
->s_maxbytes
= h_dentry
->d_sb
->s_maxbytes
;
544 au_add_nlink(root_inode
, d_inode(h_dentry
));
547 * this test/set prevents aufs from handling unnecesary notify events
548 * of xino files, in case of re-adding a writable branch which was
549 * once detached from aufs.
551 if (au_xino_brid(sb
) < 0
552 && au_br_writable(add_branch
->br_perm
)
553 && !au_test_fs_bad_xino(h_dentry
->d_sb
)
554 && add_branch
->br_xino
.xi_file
555 && add_branch
->br_xino
.xi_file
->f_path
.dentry
->d_parent
== h_dentry
)
556 au_xino_brid_set(sb
, add_branch
->br_id
);
562 /* ---------------------------------------------------------------------- */
564 static unsigned long long au_farray_cb(struct super_block
*sb
, void *a
,
565 unsigned long long max __maybe_unused
,
568 unsigned long long n
;
570 struct au_sphlhead
*files
;
571 struct au_finfo
*finfo
;
575 files
= &au_sbi(sb
)->si_files
;
576 spin_lock(&files
->spin
);
577 hlist_for_each_entry(finfo
, &files
->head
, fi_hlist
) {
580 && !special_file(file_inode(f
)->i_mode
)) {
587 spin_unlock(&files
->spin
);
592 static struct file
**au_farray_alloc(struct super_block
*sb
,
593 unsigned long long *max
)
595 *max
= au_nfiles(sb
);
596 return au_array_alloc(max
, au_farray_cb
, sb
, /*arg*/NULL
);
599 static void au_farray_free(struct file
**a
, unsigned long long max
)
601 unsigned long long ull
;
603 for (ull
= 0; ull
< max
; ull
++)
609 /* ---------------------------------------------------------------------- */
615 /* to show the line number, do not make it inlined function */
616 #define AuVerbose(do_info, fmt, ...) do { \
618 pr_info(fmt, ##__VA_ARGS__); \
621 static int au_test_ibusy(struct inode
*inode
, aufs_bindex_t btop
,
624 return (inode
&& !S_ISDIR(inode
->i_mode
)) || btop
== bbot
;
627 static int au_test_dbusy(struct dentry
*dentry
, aufs_bindex_t btop
,
630 return au_test_ibusy(d_inode(dentry
), btop
, bbot
);
634 * test if the branch is deletable or not.
636 static int test_dentry_busy(struct dentry
*root
, aufs_bindex_t bindex
,
637 unsigned int sigen
, const unsigned int verbose
)
639 int err
, i
, j
, ndentry
;
640 aufs_bindex_t btop
, bbot
;
641 struct au_dcsub_pages dpages
;
642 struct au_dpage
*dpage
;
645 err
= au_dpages_init(&dpages
, GFP_NOFS
);
648 err
= au_dcsub_pages(&dpages
, root
, NULL
, NULL
);
652 for (i
= 0; !err
&& i
< dpages
.ndpage
; i
++) {
653 dpage
= dpages
.dpages
+ i
;
654 ndentry
= dpage
->ndentry
;
655 for (j
= 0; !err
&& j
< ndentry
; j
++) {
656 d
= dpage
->dentries
[j
];
657 AuDebugOn(au_dcount(d
) <= 0);
658 if (!au_digen_test(d
, sigen
)) {
659 di_read_lock_child(d
, AuLock_IR
);
660 if (unlikely(au_dbrange_test(d
))) {
661 di_read_unlock(d
, AuLock_IR
);
665 di_write_lock_child(d
);
666 if (unlikely(au_dbrange_test(d
))) {
670 err
= au_reval_dpath(d
, sigen
);
672 di_downgrade_lock(d
, AuLock_IR
);
679 /* AuDbgDentry(d); */
684 && au_h_dptr(d
, bindex
)
685 && au_test_dbusy(d
, btop
, bbot
)) {
687 AuVerbose(verbose
, "busy %pd\n", d
);
690 di_read_unlock(d
, AuLock_IR
);
695 au_dpages_free(&dpages
);
700 static int test_inode_busy(struct super_block
*sb
, aufs_bindex_t bindex
,
701 unsigned int sigen
, const unsigned int verbose
)
704 unsigned long long max
, ull
;
705 struct inode
*i
, **array
;
706 aufs_bindex_t btop
, bbot
;
708 array
= au_iarray_alloc(sb
, &max
);
709 err
= PTR_ERR(array
);
714 AuDbg("b%d\n", bindex
);
715 for (ull
= 0; !err
&& ull
< max
; ull
++) {
719 if (i
->i_ino
== AUFS_ROOT_INO
)
723 if (au_iigen(i
, NULL
) == sigen
)
724 ii_read_lock_child(i
);
726 ii_write_lock_child(i
);
727 err
= au_refresh_hinode_self(i
);
730 ii_downgrade_lock(i
);
741 && au_h_iptr(i
, bindex
)
742 && au_test_ibusy(i
, btop
, bbot
)) {
744 AuVerbose(verbose
, "busy i%lu\n", i
->i_ino
);
749 au_iarray_free(array
, max
);
755 static int test_children_busy(struct dentry
*root
, aufs_bindex_t bindex
,
756 const unsigned int verbose
)
761 sigen
= au_sigen(root
->d_sb
);
762 DiMustNoWaiters(root
);
763 IiMustNoWaiters(d_inode(root
));
764 di_write_unlock(root
);
765 err
= test_dentry_busy(root
, bindex
, sigen
, verbose
);
767 err
= test_inode_busy(root
->d_sb
, bindex
, sigen
, verbose
);
768 di_write_lock_child(root
); /* aufs_write_lock() calls ..._child() */
773 static int test_dir_busy(struct file
*file
, aufs_bindex_t br_id
,
774 struct file
**to_free
, int *idx
)
777 unsigned char matched
, root
;
778 aufs_bindex_t bindex
, bbot
;
779 struct au_fidir
*fidir
;
780 struct au_hfile
*hfile
;
783 root
= IS_ROOT(file
->f_path
.dentry
);
786 to_free
[*idx
] = file
;
792 fidir
= au_fi(file
)->fi_hdir
;
794 bbot
= au_fbbot_dir(file
);
795 for (bindex
= au_fbtop(file
); bindex
<= bbot
; bindex
++) {
796 hfile
= fidir
->fd_hfile
+ bindex
;
800 if (hfile
->hf_br
->br_id
== br_id
) {
812 static int test_file_busy(struct super_block
*sb
, aufs_bindex_t br_id
,
813 struct file
**to_free
, int opened
)
816 unsigned long long ull
, max
;
818 struct file
*file
, **array
;
820 struct au_hfile
*hfile
;
822 array
= au_farray_alloc(sb
, &max
);
823 err
= PTR_ERR(array
);
830 di_write_unlock(root
);
831 for (ull
= 0; ull
< max
; ull
++) {
836 /* AuDbg("%pD\n", file); */
838 btop
= au_fbtop(file
);
839 if (!d_is_dir(file
->f_path
.dentry
)) {
840 hfile
= &au_fi(file
)->fi_htop
;
841 if (hfile
->hf_br
->br_id
== br_id
)
844 err
= test_dir_busy(file
, br_id
, to_free
, &idx
);
845 fi_read_unlock(file
);
849 di_write_lock_child(root
);
850 au_farray_free(array
, max
);
851 AuDebugOn(idx
> opened
);
857 static void br_del_file(struct file
**to_free
, unsigned long long opened
,
860 unsigned long long ull
;
861 aufs_bindex_t bindex
, btop
, bbot
, bfound
;
863 struct au_fidir
*fidir
;
864 struct au_hfile
*hfile
;
866 for (ull
= 0; ull
< opened
; ull
++) {
871 /* AuDbg("%pD\n", file); */
872 AuDebugOn(!d_is_dir(file
->f_path
.dentry
));
874 fidir
= au_fi(file
)->fi_hdir
;
877 btop
= au_fbtop(file
);
878 bbot
= au_fbbot_dir(file
);
879 for (bindex
= btop
; bindex
<= bbot
; bindex
++) {
880 hfile
= fidir
->fd_hfile
+ bindex
;
884 if (hfile
->hf_br
->br_id
== br_id
) {
889 AuDebugOn(bfound
< 0);
890 au_set_h_fptr(file
, bfound
, NULL
);
891 if (bfound
== btop
) {
892 for (btop
++; btop
<= bbot
; btop
++)
893 if (au_hf_dir(file
, btop
)) {
894 au_set_fbtop(file
, btop
);
898 fi_write_unlock(file
);
902 static void au_br_do_del_brp(struct au_sbinfo
*sbinfo
,
903 const aufs_bindex_t bindex
,
904 const aufs_bindex_t bbot
)
906 struct au_branch
**brp
, **p
;
908 AuRwMustWriteLock(&sbinfo
->si_rwsem
);
910 brp
= sbinfo
->si_branch
+ bindex
;
912 memmove(brp
, brp
+ 1, sizeof(*brp
) * (bbot
- bindex
));
913 sbinfo
->si_branch
[0 + bbot
] = NULL
;
916 p
= au_krealloc(sbinfo
->si_branch
, sizeof(*p
) * bbot
, AuGFP_SBILIST
,
919 sbinfo
->si_branch
= p
;
923 static void au_br_do_del_hdp(struct au_dinfo
*dinfo
, const aufs_bindex_t bindex
,
924 const aufs_bindex_t bbot
)
926 struct au_hdentry
*hdp
, *p
;
928 AuRwMustWriteLock(&dinfo
->di_rwsem
);
930 hdp
= au_hdentry(dinfo
, bindex
);
932 memmove(hdp
, hdp
+ 1, sizeof(*hdp
) * (bbot
- bindex
));
933 /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
936 p
= au_krealloc(dinfo
->di_hdentry
, sizeof(*p
) * bbot
, AuGFP_SBILIST
,
939 dinfo
->di_hdentry
= p
;
943 static void au_br_do_del_hip(struct au_iinfo
*iinfo
, const aufs_bindex_t bindex
,
944 const aufs_bindex_t bbot
)
946 struct au_hinode
*hip
, *p
;
948 AuRwMustWriteLock(&iinfo
->ii_rwsem
);
950 hip
= au_hinode(iinfo
, bindex
);
952 memmove(hip
, hip
+ 1, sizeof(*hip
) * (bbot
- bindex
));
953 /* au_hinode_init(au_hinode(iinfo, bbot)); */
956 p
= au_krealloc(iinfo
->ii_hinode
, sizeof(*p
) * bbot
, AuGFP_SBILIST
,
959 iinfo
->ii_hinode
= p
;
963 static void au_br_do_del(struct super_block
*sb
, aufs_bindex_t bindex
,
964 struct au_branch
*br
)
967 struct au_sbinfo
*sbinfo
;
968 struct dentry
*root
, *h_root
;
969 struct inode
*inode
, *h_inode
;
970 struct au_hinode
*hinode
;
975 inode
= d_inode(root
);
977 bbot
= sbinfo
->si_bbot
;
979 h_root
= au_h_dptr(root
, bindex
);
980 hinode
= au_hi(inode
, bindex
);
981 h_inode
= au_igrab(hinode
->hi_inode
);
985 au_br_do_del_brp(sbinfo
, bindex
, bbot
);
986 au_br_do_del_hdp(au_di(root
), bindex
, bbot
);
987 au_br_do_del_hip(au_ii(inode
), bindex
, bbot
);
995 static unsigned long long empty_cb(struct super_block
*sb
, void *array
,
996 unsigned long long max
, void *arg
)
1001 int au_br_del(struct super_block
*sb
, struct au_opt_del
*del
, int remount
)
1004 unsigned long long opened
;
1005 unsigned int mnt_flags
;
1006 aufs_bindex_t bindex
, bbot
, br_id
;
1007 unsigned char do_wh
, verbose
;
1008 struct au_branch
*br
;
1010 struct dentry
*root
;
1011 struct file
**to_free
;
1017 bindex
= au_find_dbindex(root
, del
->h_path
.dentry
);
1020 goto out
; /* success */
1022 pr_err("%s no such branch\n", del
->pathname
);
1025 AuDbg("bindex b%d\n", bindex
);
1028 mnt_flags
= au_mntflags(sb
);
1029 verbose
= !!au_opt_test(mnt_flags
, VERBOSE
);
1030 bbot
= au_sbbot(sb
);
1031 if (unlikely(!bbot
)) {
1032 AuVerbose(verbose
, "no more branches left\n");
1035 br
= au_sbr(sb
, bindex
);
1036 AuDebugOn(!path_equal(&br
->br_path
, &del
->h_path
));
1039 opened
= au_br_count(br
);
1040 if (unlikely(opened
)) {
1041 to_free
= au_array_alloc(&opened
, empty_cb
, sb
, NULL
);
1042 err
= PTR_ERR(to_free
);
1043 if (IS_ERR(to_free
))
1046 err
= test_file_busy(sb
, br_id
, to_free
, opened
);
1047 if (unlikely(err
)) {
1048 AuVerbose(verbose
, "%llu file(s) opened\n", opened
);
1054 do_wh
= wbr
&& (wbr
->wbr_whbase
|| wbr
->wbr_plink
|| wbr
->wbr_orph
);
1056 /* instead of WbrWhMustWriteLock(wbr) */
1057 SiMustWriteLock(sb
);
1058 for (i
= 0; i
< AuBrWh_Last
; i
++) {
1059 dput(wbr
->wbr_wh
[i
]);
1060 wbr
->wbr_wh
[i
] = NULL
;
1064 err
= test_children_busy(root
, bindex
, verbose
);
1065 if (unlikely(err
)) {
1074 * now we confirmed the branch is deletable.
1075 * let's free the remaining opened dirs on the branch.
1077 di_write_unlock(root
);
1078 br_del_file(to_free
, opened
, br_id
);
1079 di_write_lock_child(root
);
1083 au_br_do_del(sb
, bindex
, br
);
1085 sysaufs_brs_del(sb
, bindex
);
1086 au_br_do_del(sb
, bindex
, br
);
1087 sysaufs_brs_add(sb
, bindex
);
1091 au_cpup_attr_all(d_inode(root
), /*force*/1);
1092 sb
->s_maxbytes
= au_sbr_sb(sb
, 0)->s_maxbytes
;
1094 au_sub_nlink(d_inode(root
), d_inode(del
->h_path
.dentry
));
1095 if (au_opt_test(mnt_flags
, PLINK
))
1096 au_plink_half_refresh(sb
, br_id
);
1098 if (au_xino_brid(sb
) == br_id
)
1099 au_xino_brid_set(sb
, -1);
1100 goto out
; /* success */
1104 rerr
= au_br_init_wh(sb
, br
, br
->br_perm
);
1106 pr_warn("failed re-creating base whiteout, %s. (%d)\n",
1107 del
->pathname
, rerr
);
1110 au_farray_free(to_free
, opened
);
1114 /* ---------------------------------------------------------------------- */
1116 static int au_ibusy(struct super_block
*sb
, struct aufs_ibusy __user
*arg
)
1119 aufs_bindex_t btop
, bbot
;
1120 struct aufs_ibusy ibusy
;
1121 struct inode
*inode
, *h_inode
;
1124 if (unlikely(!capable(CAP_SYS_ADMIN
)))
1127 err
= copy_from_user(&ibusy
, arg
, sizeof(ibusy
));
1129 err
= !access_ok(VERIFY_WRITE
, &arg
->h_ino
, sizeof(arg
->h_ino
));
1130 if (unlikely(err
)) {
1137 si_read_lock(sb
, AuLock_FLUSH
);
1138 if (unlikely(ibusy
.bindex
< 0 || ibusy
.bindex
> au_sbbot(sb
)))
1142 ibusy
.h_ino
= 0; /* invalid */
1143 inode
= ilookup(sb
, ibusy
.ino
);
1145 || inode
->i_ino
== AUFS_ROOT_INO
1146 || au_is_bad_inode(inode
))
1149 ii_read_lock_child(inode
);
1150 btop
= au_ibtop(inode
);
1151 bbot
= au_ibbot(inode
);
1152 if (btop
<= ibusy
.bindex
&& ibusy
.bindex
<= bbot
) {
1153 h_inode
= au_h_iptr(inode
, ibusy
.bindex
);
1154 if (h_inode
&& au_test_ibusy(inode
, btop
, bbot
))
1155 ibusy
.h_ino
= h_inode
->i_ino
;
1157 ii_read_unlock(inode
);
1163 err
= __put_user(ibusy
.h_ino
, &arg
->h_ino
);
1164 if (unlikely(err
)) {
1173 long au_ibusy_ioctl(struct file
*file
, unsigned long arg
)
1175 return au_ibusy(file
->f_path
.dentry
->d_sb
, (void __user
*)arg
);
1178 #ifdef CONFIG_COMPAT
1179 long au_ibusy_compat_ioctl(struct file
*file
, unsigned long arg
)
1181 return au_ibusy(file
->f_path
.dentry
->d_sb
, compat_ptr(arg
));
1185 /* ---------------------------------------------------------------------- */
1188 * change a branch permission
1191 static void au_warn_ima(void)
1194 /* since it doesn't support mark_files_ro() */
1195 AuWarn1("RW -> RO makes IMA to produce wrong message\n");
1199 static int do_need_sigen_inc(int a
, int b
)
1201 return au_br_whable(a
) && !au_br_whable(b
);
1204 static int need_sigen_inc(int old
, int new)
1206 return do_need_sigen_inc(old
, new)
1207 || do_need_sigen_inc(new, old
);
1210 static int au_br_mod_files_ro(struct super_block
*sb
, aufs_bindex_t bindex
)
1213 unsigned int mnt_flags
;
1214 unsigned long long ull
, max
;
1215 aufs_bindex_t br_id
;
1216 unsigned char verbose
, writer
;
1217 struct file
*file
, *hf
, **array
;
1218 struct au_hfile
*hfile
;
1220 mnt_flags
= au_mntflags(sb
);
1221 verbose
= !!au_opt_test(mnt_flags
, VERBOSE
);
1223 array
= au_farray_alloc(sb
, &max
);
1224 err
= PTR_ERR(array
);
1229 br_id
= au_sbr_id(sb
, bindex
);
1230 for (ull
= 0; ull
< max
; ull
++) {
1232 if (unlikely(!file
))
1235 /* AuDbg("%pD\n", file); */
1237 if (unlikely(au_test_mmapped(file
))) {
1239 AuVerbose(verbose
, "mmapped %pD\n", file
);
1241 FiMustNoWaiters(file
);
1242 fi_read_unlock(file
);
1246 hfile
= &au_fi(file
)->fi_htop
;
1247 hf
= hfile
->hf_file
;
1248 if (!d_is_reg(file
->f_path
.dentry
)
1249 || !(file
->f_mode
& FMODE_WRITE
)
1250 || hfile
->hf_br
->br_id
!= br_id
1251 || !(hf
->f_mode
& FMODE_WRITE
))
1258 FiMustNoWaiters(file
);
1259 fi_read_unlock(file
);
1267 for (ull
= 0; ull
< max
; ull
++) {
1272 /* todo: already flushed? */
1274 * fs/super.c:mark_files_ro() is gone, but aufs keeps its
1275 * approach which resets f_mode and calls mnt_drop_write() and
1276 * file_release_write() for each file, because the branch
1277 * attribute in aufs world is totally different from the native
1280 /* fi_read_lock(file); */
1281 hfile
= &au_fi(file
)->fi_htop
;
1282 hf
= hfile
->hf_file
;
1283 /* fi_read_unlock(file); */
1284 spin_lock(&hf
->f_lock
);
1285 writer
= !!(hf
->f_mode
& FMODE_WRITER
);
1286 hf
->f_mode
&= ~(FMODE_WRITE
| FMODE_WRITER
);
1287 spin_unlock(&hf
->f_lock
);
1289 put_write_access(file_inode(hf
));
1290 __mnt_drop_write(hf
->f_path
.mnt
);
1295 au_farray_free(array
, max
);
1301 int au_br_mod(struct super_block
*sb
, struct au_opt_mod
*mod
, int remount
,
1305 aufs_bindex_t bindex
;
1306 struct dentry
*root
;
1307 struct au_branch
*br
;
1308 struct au_br_fhsm
*bf
;
1311 bindex
= au_find_dbindex(root
, mod
->h_root
);
1314 return 0; /* success */
1316 pr_err("%s no such branch\n", mod
->path
);
1319 AuDbg("bindex b%d\n", bindex
);
1321 err
= test_br(d_inode(mod
->h_root
), mod
->perm
, mod
->path
);
1325 br
= au_sbr(sb
, bindex
);
1326 AuDebugOn(mod
->h_root
!= au_br_dentry(br
));
1327 if (br
->br_perm
== mod
->perm
)
1328 return 0; /* success */
1330 /* pre-allocate for non-fhsm --> fhsm */
1332 if (!au_br_fhsm(br
->br_perm
) && au_br_fhsm(mod
->perm
)) {
1333 err
= au_fhsm_br_alloc(br
);
1340 if (au_br_writable(br
->br_perm
)) {
1341 /* remove whiteout base */
1342 err
= au_br_init_wh(sb
, br
, mod
->perm
);
1346 if (!au_br_writable(mod
->perm
)) {
1347 /* rw --> ro, file might be mmapped */
1348 DiMustNoWaiters(root
);
1349 IiMustNoWaiters(d_inode(root
));
1350 di_write_unlock(root
);
1351 err
= au_br_mod_files_ro(sb
, bindex
);
1352 /* aufs_write_lock() calls ..._child() */
1353 di_write_lock_child(root
);
1355 if (unlikely(err
)) {
1357 br
->br_wbr
= kzalloc(sizeof(*br
->br_wbr
),
1360 rerr
= au_wbr_init(br
, sb
, br
->br_perm
);
1361 if (unlikely(rerr
)) {
1362 AuIOErr("nested error %d (%d)\n",
1364 br
->br_perm
= mod
->perm
;
1368 } else if (au_br_writable(mod
->perm
)) {
1371 br
->br_wbr
= kzalloc(sizeof(*br
->br_wbr
), GFP_NOFS
);
1373 err
= au_wbr_init(br
, sb
, mod
->perm
);
1374 if (unlikely(err
)) {
1383 if (au_br_fhsm(br
->br_perm
)) {
1384 if (!au_br_fhsm(mod
->perm
)) {
1385 /* fhsm --> non-fhsm */
1386 au_br_fhsm_fin(br
->br_fhsm
);
1390 } else if (au_br_fhsm(mod
->perm
))
1391 /* non-fhsm --> fhsm */
1394 *do_refresh
|= need_sigen_inc(br
->br_perm
, mod
->perm
);
1395 br
->br_perm
= mod
->perm
;
1396 goto out
; /* success */
1405 /* ---------------------------------------------------------------------- */
1407 int au_br_stfs(struct au_branch
*br
, struct aufs_stfs
*stfs
)
1410 struct kstatfs kstfs
;
1412 err
= vfs_statfs(&br
->br_path
, &kstfs
);
1414 stfs
->f_blocks
= kstfs
.f_blocks
;
1415 stfs
->f_bavail
= kstfs
.f_bavail
;
1416 stfs
->f_files
= kstfs
.f_files
;
1417 stfs
->f_ffree
= kstfs
.f_ffree
;