]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/aufs/xino.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/>.
19 * external inode number translation table and bitmap
22 #include <linux/seq_file.h>
23 #include <linux/statfs.h>
26 static ssize_t
xino_fread_wkq(vfs_readf_t func
, struct file
*file
, void *buf
,
27 size_t size
, loff_t
*pos
);
29 /* todo: unnecessary to support mmap_sem since kernel-space? */
30 ssize_t
xino_fread(vfs_readf_t func
, struct file
*file
, void *kbuf
, size_t size
,
40 const int prevent_endless
= 10;
47 err
= func(file
, buf
.u
, size
, pos
);
50 && fatal_signal_pending(current
)) {
52 err
= xino_fread_wkq(func
, file
, kbuf
, size
, pos
);
53 BUG_ON(err
== -EINTR
);
57 } while (i
++ < prevent_endless
58 && (err
== -EAGAIN
|| err
== -EINTR
));
61 #if 0 /* reserved for future use */
63 fsnotify_access(file
->f_path
.dentry
);
69 struct xino_fread_args
{
78 static void call_xino_fread(void *args
)
80 struct xino_fread_args
*a
= args
;
81 *a
->errp
= xino_fread(a
->func
, a
->file
, a
->buf
, a
->size
, a
->pos
);
84 static ssize_t
xino_fread_wkq(vfs_readf_t func
, struct file
*file
, void *buf
,
85 size_t size
, loff_t
*pos
)
89 struct xino_fread_args args
= {
98 wkq_err
= au_wkq_wait(call_xino_fread
, &args
);
99 if (unlikely(wkq_err
))
105 /* ---------------------------------------------------------------------- */
107 static ssize_t
xino_fwrite_wkq(vfs_writef_t func
, struct file
*file
, void *buf
,
108 size_t size
, loff_t
*pos
);
110 static ssize_t
do_xino_fwrite(vfs_writef_t func
, struct file
*file
, void *kbuf
,
111 size_t size
, loff_t
*pos
)
117 const char __user
*u
;
120 const int prevent_endless
= 10;
127 err
= func(file
, buf
.u
, size
, pos
);
130 && fatal_signal_pending(current
)) {
132 err
= xino_fwrite_wkq(func
, file
, kbuf
, size
, pos
);
133 BUG_ON(err
== -EINTR
);
137 } while (i
++ < prevent_endless
138 && (err
== -EAGAIN
|| err
== -EINTR
));
141 #if 0 /* reserved for future use */
143 fsnotify_modify(file
->f_path
.dentry
);
149 struct do_xino_fwrite_args
{
158 static void call_do_xino_fwrite(void *args
)
160 struct do_xino_fwrite_args
*a
= args
;
161 *a
->errp
= do_xino_fwrite(a
->func
, a
->file
, a
->buf
, a
->size
, a
->pos
);
164 static ssize_t
xino_fwrite_wkq(vfs_writef_t func
, struct file
*file
, void *buf
,
165 size_t size
, loff_t
*pos
)
169 struct do_xino_fwrite_args args
= {
179 * it breaks RLIMIT_FSIZE and normal user's limit,
180 * users should care about quota and real 'filesystem full.'
182 wkq_err
= au_wkq_wait(call_do_xino_fwrite
, &args
);
183 if (unlikely(wkq_err
))
189 ssize_t
xino_fwrite(vfs_writef_t func
, struct file
*file
, void *buf
,
190 size_t size
, loff_t
*pos
)
194 if (rlimit(RLIMIT_FSIZE
) == RLIM_INFINITY
) {
196 err
= do_xino_fwrite(func
, file
, buf
, size
, pos
);
200 err
= xino_fwrite_wkq(func
, file
, buf
, size
, pos
);
207 /* ---------------------------------------------------------------------- */
210 * create a new xinofile at the same place/path as @base_file.
212 struct file
*au_xino_create2(struct file
*base_file
, struct file
*copy_src
)
215 struct dentry
*base
, *parent
;
216 struct inode
*dir
, *delegated
;
221 base
= base_file
->f_path
.dentry
;
222 parent
= base
->d_parent
; /* dir inode is locked */
223 dir
= d_inode(parent
);
226 file
= ERR_PTR(-EINVAL
);
227 name
= &base
->d_name
;
228 path
.dentry
= vfsub_lookup_one_len(name
->name
, parent
, name
->len
);
229 if (IS_ERR(path
.dentry
)) {
230 file
= (void *)path
.dentry
;
231 pr_err("%pd lookup err %ld\n",
232 base
, PTR_ERR(path
.dentry
));
236 /* no need to mnt_want_write() since we call dentry_open() later */
237 err
= vfs_create(dir
, path
.dentry
, S_IRUGO
| S_IWUGO
, NULL
);
240 pr_err("%pd create err %d\n", base
, err
);
244 path
.mnt
= base_file
->f_path
.mnt
;
245 file
= vfsub_dentry_open(&path
,
246 O_RDWR
| O_CREAT
| O_EXCL
| O_LARGEFILE
247 /* | __FMODE_NONOTIFY */);
249 pr_err("%pd open err %ld\n", base
, PTR_ERR(file
));
254 err
= vfsub_unlink(dir
, &file
->f_path
, &delegated
, /*force*/0);
255 if (unlikely(err
== -EWOULDBLOCK
)) {
256 pr_warn("cannot retry for NFSv4 delegation"
257 " for an internal unlink\n");
261 pr_err("%pd unlink err %d\n", base
, err
);
266 /* no one can touch copy_src xino */
267 err
= au_copy_file(file
, copy_src
, vfsub_f_size_read(copy_src
));
269 pr_err("%pd copy err %d\n", base
, err
);
273 goto out_dput
; /* success */
284 struct au_xino_lock_dir
{
285 struct au_hinode
*hdir
;
286 struct dentry
*parent
;
290 static void au_xino_lock_dir(struct super_block
*sb
, struct file
*xino
,
291 struct au_xino_lock_dir
*ldir
)
293 aufs_bindex_t brid
, bindex
;
297 brid
= au_xino_brid(sb
);
299 bindex
= au_br_index(sb
, brid
);
301 ldir
->hdir
= au_hi(d_inode(sb
->s_root
), bindex
);
302 au_hn_inode_lock_nested(ldir
->hdir
, AuLsc_I_PARENT
);
304 ldir
->parent
= dget_parent(xino
->f_path
.dentry
);
305 ldir
->dir
= d_inode(ldir
->parent
);
306 inode_lock_nested(ldir
->dir
, AuLsc_I_PARENT
);
310 static void au_xino_unlock_dir(struct au_xino_lock_dir
*ldir
)
313 au_hn_inode_unlock(ldir
->hdir
);
315 inode_unlock(ldir
->dir
);
320 /* ---------------------------------------------------------------------- */
322 /* trucate xino files asynchronously */
324 int au_xino_trunc(struct super_block
*sb
, aufs_bindex_t bindex
)
329 aufs_bindex_t bi
, bbot
;
331 struct au_branch
*br
;
332 struct file
*new_xino
, *file
;
333 struct super_block
*h_sb
;
334 struct au_xino_lock_dir ldir
;
337 st
= kmalloc(sizeof(*st
), GFP_NOFS
);
343 if (unlikely(bindex
< 0 || bbot
< bindex
))
345 br
= au_sbr(sb
, bindex
);
346 file
= br
->br_xino
.xi_file
;
350 err
= vfs_statfs(&file
->f_path
, st
);
352 AuErr1("statfs err %d, ignored\n", err
);
354 blocks
= file_inode(file
)->i_blocks
;
355 pr_info("begin truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
356 bindex
, (u64
)blocks
, st
->f_bfree
, st
->f_blocks
);
358 au_xino_lock_dir(sb
, file
, &ldir
);
359 /* mnt_want_write() is unnecessary here */
360 new_xino
= au_xino_create2(file
, file
);
361 au_xino_unlock_dir(&ldir
);
362 err
= PTR_ERR(new_xino
);
363 if (IS_ERR(new_xino
)) {
364 pr_err("err %d, ignored\n", err
);
369 br
->br_xino
.xi_file
= new_xino
;
372 for (bi
= 0; bi
<= bbot
; bi
++) {
373 if (unlikely(bi
== bindex
))
376 if (au_br_sb(br
) != h_sb
)
379 fput(br
->br_xino
.xi_file
);
380 br
->br_xino
.xi_file
= new_xino
;
384 err
= vfs_statfs(&new_xino
->f_path
, st
);
386 pr_info("end truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
387 bindex
, (u64
)file_inode(new_xino
)->i_blocks
,
388 st
->f_bfree
, st
->f_blocks
);
389 if (file_inode(new_xino
)->i_blocks
< blocks
)
390 au_sbi(sb
)->si_xino_jiffy
= jiffy
;
392 AuErr1("statfs err %d, ignored\n", err
);
400 struct xino_do_trunc_args
{
401 struct super_block
*sb
;
402 struct au_branch
*br
;
405 static void xino_do_trunc(void *_args
)
407 struct xino_do_trunc_args
*args
= _args
;
408 struct super_block
*sb
;
409 struct au_branch
*br
;
412 aufs_bindex_t bindex
;
416 dir
= d_inode(sb
->s_root
);
419 si_noflush_write_lock(sb
);
420 ii_read_lock_parent(dir
);
421 bindex
= au_br_index(sb
, br
->br_id
);
422 err
= au_xino_trunc(sb
, bindex
);
425 pr_warn("err b%d, (%d)\n", bindex
, err
);
426 atomic_dec(&br
->br_xino_running
);
429 au_nwt_done(&au_sbi(sb
)->si_nowait
);
433 static int xino_trunc_test(struct super_block
*sb
, struct au_branch
*br
)
437 struct au_sbinfo
*sbinfo
;
439 /* todo: si_xino_expire and the ratio should be customizable */
441 if (time_before(jiffies
,
442 sbinfo
->si_xino_jiffy
+ sbinfo
->si_xino_expire
))
445 /* truncation border */
446 err
= vfs_statfs(&br
->br_xino
.xi_file
->f_path
, &st
);
448 AuErr1("statfs err %d, ignored\n", err
);
451 if (div64_u64(st
.f_bfree
* 100, st
.f_blocks
) >= AUFS_XINO_DEF_TRUNC
)
457 static void xino_try_trunc(struct super_block
*sb
, struct au_branch
*br
)
459 struct xino_do_trunc_args
*args
;
462 if (!xino_trunc_test(sb
, br
))
465 if (atomic_inc_return(&br
->br_xino_running
) > 1)
468 /* lock and kfree() will be called in trunc_xino() */
469 args
= kmalloc(sizeof(*args
), GFP_NOFS
);
470 if (unlikely(!args
)) {
471 AuErr1("no memory\n");
478 wkq_err
= au_wkq_nowait(xino_do_trunc
, args
, sb
, /*flags*/0);
480 return; /* success */
482 pr_err("wkq %d\n", wkq_err
);
487 atomic_dec(&br
->br_xino_running
);
490 /* ---------------------------------------------------------------------- */
492 static int au_xino_do_write(vfs_writef_t write
, struct file
*file
,
493 ino_t h_ino
, ino_t ino
)
499 if (unlikely(au_loff_max
/ sizeof(ino
) - 1 < pos
)) {
500 AuIOErr1("too large hi%lu\n", (unsigned long)h_ino
);
504 sz
= xino_fwrite(write
, file
, &ino
, sizeof(ino
), &pos
);
505 if (sz
== sizeof(ino
))
506 return 0; /* success */
508 AuIOErr("write failed (%zd)\n", sz
);
513 * write @ino to the xinofile for the specified branch{@sb, @bindex}
514 * at the position of @h_ino.
515 * even if @ino is zero, it is written to the xinofile and means no entry.
516 * if the size of the xino file on a specific filesystem exceeds the watermark,
519 int au_xino_write(struct super_block
*sb
, aufs_bindex_t bindex
, ino_t h_ino
,
523 unsigned int mnt_flags
;
524 struct au_branch
*br
;
526 BUILD_BUG_ON(sizeof(long long) != sizeof(au_loff_max
)
527 || ((loff_t
)-1) > 0);
530 mnt_flags
= au_mntflags(sb
);
531 if (!au_opt_test(mnt_flags
, XINO
))
534 br
= au_sbr(sb
, bindex
);
535 err
= au_xino_do_write(au_sbi(sb
)->si_xwrite
, br
->br_xino
.xi_file
,
538 if (au_opt_test(mnt_flags
, TRUNC_XINO
)
539 && au_test_fs_trunc_xino(au_br_sb(br
)))
540 xino_try_trunc(sb
, br
);
541 return 0; /* success */
544 AuIOErr("write failed (%d)\n", err
);
548 /* ---------------------------------------------------------------------- */
550 /* aufs inode number bitmap */
552 static const int page_bits
= (int)PAGE_SIZE
* BITS_PER_BYTE
;
553 static ino_t
xib_calc_ino(unsigned long pindex
, int bit
)
557 AuDebugOn(bit
< 0 || page_bits
<= bit
);
558 ino
= AUFS_FIRST_INO
+ pindex
* page_bits
+ bit
;
562 static void xib_calc_bit(ino_t ino
, unsigned long *pindex
, int *bit
)
564 AuDebugOn(ino
< AUFS_FIRST_INO
);
565 ino
-= AUFS_FIRST_INO
;
566 *pindex
= ino
/ page_bits
;
567 *bit
= ino
% page_bits
;
570 static int xib_pindex(struct super_block
*sb
, unsigned long pindex
)
575 struct au_sbinfo
*sbinfo
;
580 MtxMustLock(&sbinfo
->si_xib_mtx
);
581 AuDebugOn(pindex
> ULONG_MAX
/ PAGE_SIZE
582 || !au_opt_test(sbinfo
->si_mntflags
, XINO
));
584 if (pindex
== sbinfo
->si_xib_last_pindex
)
587 xib
= sbinfo
->si_xib
;
588 p
= sbinfo
->si_xib_buf
;
589 pos
= sbinfo
->si_xib_last_pindex
;
591 sz
= xino_fwrite(sbinfo
->si_xwrite
, xib
, p
, PAGE_SIZE
, &pos
);
592 if (unlikely(sz
!= PAGE_SIZE
))
597 if (vfsub_f_size_read(xib
) >= pos
+ PAGE_SIZE
)
598 sz
= xino_fread(sbinfo
->si_xread
, xib
, p
, PAGE_SIZE
, &pos
);
600 memset(p
, 0, PAGE_SIZE
);
601 sz
= xino_fwrite(sbinfo
->si_xwrite
, xib
, p
, PAGE_SIZE
, &pos
);
603 if (sz
== PAGE_SIZE
) {
604 sbinfo
->si_xib_last_pindex
= pindex
;
605 return 0; /* success */
609 AuIOErr1("write failed (%zd)\n", sz
);
616 /* ---------------------------------------------------------------------- */
618 static void au_xib_clear_bit(struct inode
*inode
)
621 unsigned long pindex
;
622 struct super_block
*sb
;
623 struct au_sbinfo
*sbinfo
;
625 AuDebugOn(inode
->i_nlink
);
628 xib_calc_bit(inode
->i_ino
, &pindex
, &bit
);
629 AuDebugOn(page_bits
<= bit
);
631 mutex_lock(&sbinfo
->si_xib_mtx
);
632 err
= xib_pindex(sb
, pindex
);
634 clear_bit(bit
, sbinfo
->si_xib_buf
);
635 sbinfo
->si_xib_next_bit
= bit
;
637 mutex_unlock(&sbinfo
->si_xib_mtx
);
640 /* for s_op->delete_inode() */
641 void au_xino_delete_inode(struct inode
*inode
, const int unlinked
)
644 unsigned int mnt_flags
;
645 aufs_bindex_t bindex
, bbot
, bi
;
646 unsigned char try_trunc
;
647 struct au_iinfo
*iinfo
;
648 struct super_block
*sb
;
649 struct au_hinode
*hi
;
650 struct inode
*h_inode
;
651 struct au_branch
*br
;
654 AuDebugOn(au_is_bad_inode(inode
));
657 mnt_flags
= au_mntflags(sb
);
658 if (!au_opt_test(mnt_flags
, XINO
)
659 || inode
->i_ino
== AUFS_ROOT_INO
)
664 au_xib_clear_bit(inode
);
667 iinfo
= au_ii(inode
);
668 bindex
= iinfo
->ii_btop
;
672 xwrite
= au_sbi(sb
)->si_xwrite
;
673 try_trunc
= !!au_opt_test(mnt_flags
, TRUNC_XINO
);
674 hi
= au_hinode(iinfo
, bindex
);
675 bbot
= iinfo
->ii_bbot
;
676 for (; bindex
<= bbot
; bindex
++, hi
++) {
677 h_inode
= hi
->hi_inode
;
679 || (!unlinked
&& h_inode
->i_nlink
))
682 /* inode may not be revalidated */
683 bi
= au_br_index(sb
, hi
->hi_id
);
688 err
= au_xino_do_write(xwrite
, br
->br_xino
.xi_file
,
689 h_inode
->i_ino
, /*ino*/0);
690 if (!err
&& try_trunc
691 && au_test_fs_trunc_xino(au_br_sb(br
)))
692 xino_try_trunc(sb
, br
);
696 /* get an unused inode number from bitmap */
697 ino_t
au_xino_new_ino(struct super_block
*sb
)
700 unsigned long *p
, pindex
, ul
, pend
;
701 struct au_sbinfo
*sbinfo
;
705 if (!au_opt_test(au_mntflags(sb
), XINO
))
706 return iunique(sb
, AUFS_FIRST_INO
);
709 mutex_lock(&sbinfo
->si_xib_mtx
);
710 p
= sbinfo
->si_xib_buf
;
711 free_bit
= sbinfo
->si_xib_next_bit
;
712 if (free_bit
< page_bits
&& !test_bit(free_bit
, p
))
713 goto out
; /* success */
714 free_bit
= find_first_zero_bit(p
, page_bits
);
715 if (free_bit
< page_bits
)
716 goto out
; /* success */
718 pindex
= sbinfo
->si_xib_last_pindex
;
719 for (ul
= pindex
- 1; ul
< ULONG_MAX
; ul
--) {
720 err
= xib_pindex(sb
, ul
);
723 free_bit
= find_first_zero_bit(p
, page_bits
);
724 if (free_bit
< page_bits
)
725 goto out
; /* success */
728 file
= sbinfo
->si_xib
;
729 pend
= vfsub_f_size_read(file
) / PAGE_SIZE
;
730 for (ul
= pindex
+ 1; ul
<= pend
; ul
++) {
731 err
= xib_pindex(sb
, ul
);
734 free_bit
= find_first_zero_bit(p
, page_bits
);
735 if (free_bit
< page_bits
)
736 goto out
; /* success */
741 set_bit(free_bit
, p
);
742 sbinfo
->si_xib_next_bit
= free_bit
+ 1;
743 pindex
= sbinfo
->si_xib_last_pindex
;
744 mutex_unlock(&sbinfo
->si_xib_mtx
);
745 ino
= xib_calc_ino(pindex
, free_bit
);
746 AuDbg("i%lu\n", (unsigned long)ino
);
749 mutex_unlock(&sbinfo
->si_xib_mtx
);
755 * read @ino from xinofile for the specified branch{@sb, @bindex}
756 * at the position of @h_ino.
757 * if @ino does not exist and @do_new is true, get new one.
759 int au_xino_read(struct super_block
*sb
, aufs_bindex_t bindex
, ino_t h_ino
,
766 struct au_sbinfo
*sbinfo
;
769 if (!au_opt_test(au_mntflags(sb
), XINO
))
770 return 0; /* no xino */
775 if (unlikely(au_loff_max
/ sizeof(*ino
) - 1 < pos
)) {
776 AuIOErr1("too large hi%lu\n", (unsigned long)h_ino
);
781 file
= au_sbr(sb
, bindex
)->br_xino
.xi_file
;
782 if (vfsub_f_size_read(file
) < pos
+ sizeof(*ino
))
783 return 0; /* no ino */
785 sz
= xino_fread(sbinfo
->si_xread
, file
, ino
, sizeof(*ino
), &pos
);
786 if (sz
== sizeof(*ino
))
787 return 0; /* success */
790 if (unlikely(sz
>= 0)) {
792 AuIOErr("xino read error (%zd)\n", sz
);
798 /* ---------------------------------------------------------------------- */
800 /* create and set a new xino file */
802 struct file
*au_xino_create(struct super_block
*sb
, char *fname
, int silent
)
805 struct dentry
*h_parent
, *d
;
806 struct inode
*h_dir
, *inode
;
810 * at mount-time, and the xino file is the default path,
811 * hnotify is disabled so we have no notify events to ignore.
812 * when a user specified the xino, we cannot get au_hdir to be ignored.
814 file
= vfsub_filp_open(fname
, O_RDWR
| O_CREAT
| O_EXCL
| O_LARGEFILE
815 /* | __FMODE_NONOTIFY */,
819 pr_err("open %s(%ld)\n", fname
, PTR_ERR(file
));
823 /* keep file count */
825 inode
= file_inode(file
);
826 h_parent
= dget_parent(file
->f_path
.dentry
);
827 h_dir
= d_inode(h_parent
);
828 inode_lock_nested(h_dir
, AuLsc_I_PARENT
);
829 /* mnt_want_write() is unnecessary here */
830 /* no delegation since it is just created */
832 err
= vfsub_unlink(h_dir
, &file
->f_path
, /*delegated*/NULL
,
838 pr_err("unlink %s(%d)\n", fname
, err
);
843 d
= file
->f_path
.dentry
;
844 if (unlikely(sb
== d
->d_sb
)) {
846 pr_err("%s must be outside\n", fname
);
849 if (unlikely(au_test_fs_bad_xino(d
->d_sb
))) {
851 pr_err("xino doesn't support %s(%s)\n",
852 fname
, au_sbtype(d
->d_sb
));
855 return file
; /* success */
864 * find another branch who is on the same filesystem of the specified
865 * branch{@btgt}. search until @bbot.
867 static int is_sb_shared(struct super_block
*sb
, aufs_bindex_t btgt
,
870 aufs_bindex_t bindex
;
871 struct super_block
*tgt_sb
= au_sbr_sb(sb
, btgt
);
873 for (bindex
= 0; bindex
< btgt
; bindex
++)
874 if (unlikely(tgt_sb
== au_sbr_sb(sb
, bindex
)))
876 for (bindex
++; bindex
<= bbot
; bindex
++)
877 if (unlikely(tgt_sb
== au_sbr_sb(sb
, bindex
)))
882 /* ---------------------------------------------------------------------- */
885 * initialize the xinofile for the specified branch @br
886 * at the place/path where @base_file indicates.
887 * test whether another branch is on the same filesystem or not,
888 * if @do_test is true.
890 int au_xino_br(struct super_block
*sb
, struct au_branch
*br
, ino_t h_ino
,
891 struct file
*base_file
, int do_test
)
895 aufs_bindex_t bbot
, bindex
;
896 struct au_branch
*shared_br
, *b
;
898 struct super_block
*tgt_sb
;
903 tgt_sb
= au_br_sb(br
);
904 for (bindex
= 0; bindex
<= bbot
; bindex
++) {
905 b
= au_sbr(sb
, bindex
);
906 if (tgt_sb
== au_br_sb(b
)) {
913 if (!shared_br
|| !shared_br
->br_xino
.xi_file
) {
914 struct au_xino_lock_dir ldir
;
916 au_xino_lock_dir(sb
, base_file
, &ldir
);
917 /* mnt_want_write() is unnecessary here */
918 file
= au_xino_create2(base_file
, NULL
);
919 au_xino_unlock_dir(&ldir
);
923 br
->br_xino
.xi_file
= file
;
925 br
->br_xino
.xi_file
= shared_br
->br_xino
.xi_file
;
926 get_file(br
->br_xino
.xi_file
);
930 err
= au_xino_do_write(au_sbi(sb
)->si_xwrite
, br
->br_xino
.xi_file
,
933 fput(br
->br_xino
.xi_file
);
934 br
->br_xino
.xi_file
= NULL
;
941 /* ---------------------------------------------------------------------- */
943 /* trucate a xino bitmap file */
946 static int do_xib_restore(struct super_block
*sb
, struct file
*file
, void *page
)
950 unsigned long pindex
;
952 struct au_sbinfo
*sbinfo
;
959 MtxMustLock(&sbinfo
->si_xib_mtx
);
960 p
= sbinfo
->si_xib_buf
;
961 func
= sbinfo
->si_xread
;
962 pend
= vfsub_f_size_read(file
);
965 sz
= xino_fread(func
, file
, page
, PAGE_SIZE
, &pos
);
967 if (unlikely(sz
<= 0))
971 for (ino
= page
; sz
> 0; ino
++, sz
-= sizeof(ino
)) {
972 if (unlikely(*ino
< AUFS_FIRST_INO
))
975 xib_calc_bit(*ino
, &pindex
, &bit
);
976 AuDebugOn(page_bits
<= bit
);
977 err
= xib_pindex(sb
, pindex
);
989 static int xib_restore(struct super_block
*sb
)
992 aufs_bindex_t bindex
, bbot
;
996 page
= (void *)__get_free_page(GFP_NOFS
);
1001 bbot
= au_sbbot(sb
);
1002 for (bindex
= 0; !err
&& bindex
<= bbot
; bindex
++)
1003 if (!bindex
|| is_sb_shared(sb
, bindex
, bindex
- 1) < 0)
1004 err
= do_xib_restore
1005 (sb
, au_sbr(sb
, bindex
)->br_xino
.xi_file
, page
);
1007 AuDbg("b%d\n", bindex
);
1008 free_page((unsigned long)page
);
1014 int au_xib_trunc(struct super_block
*sb
)
1019 struct au_xino_lock_dir ldir
;
1020 struct au_sbinfo
*sbinfo
;
1024 SiMustWriteLock(sb
);
1027 sbinfo
= au_sbi(sb
);
1028 if (!au_opt_test(sbinfo
->si_mntflags
, XINO
))
1031 file
= sbinfo
->si_xib
;
1032 if (vfsub_f_size_read(file
) <= PAGE_SIZE
)
1035 au_xino_lock_dir(sb
, file
, &ldir
);
1036 /* mnt_want_write() is unnecessary here */
1037 file
= au_xino_create2(sbinfo
->si_xib
, NULL
);
1038 au_xino_unlock_dir(&ldir
);
1039 err
= PTR_ERR(file
);
1042 fput(sbinfo
->si_xib
);
1043 sbinfo
->si_xib
= file
;
1045 p
= sbinfo
->si_xib_buf
;
1046 memset(p
, 0, PAGE_SIZE
);
1048 sz
= xino_fwrite(sbinfo
->si_xwrite
, sbinfo
->si_xib
, p
, PAGE_SIZE
, &pos
);
1049 if (unlikely(sz
!= PAGE_SIZE
)) {
1051 AuIOErr("err %d\n", err
);
1057 mutex_lock(&sbinfo
->si_xib_mtx
);
1058 /* mnt_want_write() is unnecessary here */
1059 err
= xib_restore(sb
);
1060 mutex_unlock(&sbinfo
->si_xib_mtx
);
1066 /* ---------------------------------------------------------------------- */
1069 * xino mount option handlers
1073 static void xino_clear_xib(struct super_block
*sb
)
1075 struct au_sbinfo
*sbinfo
;
1077 SiMustWriteLock(sb
);
1079 sbinfo
= au_sbi(sb
);
1080 sbinfo
->si_xread
= NULL
;
1081 sbinfo
->si_xwrite
= NULL
;
1083 fput(sbinfo
->si_xib
);
1084 sbinfo
->si_xib
= NULL
;
1085 if (sbinfo
->si_xib_buf
)
1086 free_page((unsigned long)sbinfo
->si_xib_buf
);
1087 sbinfo
->si_xib_buf
= NULL
;
1090 static int au_xino_set_xib(struct super_block
*sb
, struct file
*base
)
1094 struct au_sbinfo
*sbinfo
;
1097 SiMustWriteLock(sb
);
1099 sbinfo
= au_sbi(sb
);
1100 file
= au_xino_create2(base
, sbinfo
->si_xib
);
1101 err
= PTR_ERR(file
);
1105 fput(sbinfo
->si_xib
);
1106 sbinfo
->si_xib
= file
;
1107 sbinfo
->si_xread
= vfs_readf(file
);
1108 sbinfo
->si_xwrite
= vfs_writef(file
);
1111 if (!sbinfo
->si_xib_buf
)
1112 sbinfo
->si_xib_buf
= (void *)get_zeroed_page(GFP_NOFS
);
1113 if (unlikely(!sbinfo
->si_xib_buf
))
1116 sbinfo
->si_xib_last_pindex
= 0;
1117 sbinfo
->si_xib_next_bit
= 0;
1118 if (vfsub_f_size_read(file
) < PAGE_SIZE
) {
1120 err
= xino_fwrite(sbinfo
->si_xwrite
, file
, sbinfo
->si_xib_buf
,
1122 if (unlikely(err
!= PAGE_SIZE
))
1126 goto out
; /* success */
1129 if (sbinfo
->si_xib_buf
)
1130 free_page((unsigned long)sbinfo
->si_xib_buf
);
1131 sbinfo
->si_xib_buf
= NULL
;
1135 fput(sbinfo
->si_xib
);
1136 sbinfo
->si_xib
= NULL
;
1137 sbinfo
->si_xread
= NULL
;
1138 sbinfo
->si_xwrite
= NULL
;
1143 /* xino for each branch */
1144 static void xino_clear_br(struct super_block
*sb
)
1146 aufs_bindex_t bindex
, bbot
;
1147 struct au_branch
*br
;
1149 bbot
= au_sbbot(sb
);
1150 for (bindex
= 0; bindex
<= bbot
; bindex
++) {
1151 br
= au_sbr(sb
, bindex
);
1152 if (!br
|| !br
->br_xino
.xi_file
)
1155 fput(br
->br_xino
.xi_file
);
1156 br
->br_xino
.xi_file
= NULL
;
1160 static int au_xino_set_br(struct super_block
*sb
, struct file
*base
)
1164 aufs_bindex_t bindex
, bbot
, bshared
;
1166 struct file
*old
, *new;
1168 struct au_branch
*br
;
1169 struct inode
*inode
;
1170 vfs_writef_t writef
;
1172 SiMustWriteLock(sb
);
1175 bbot
= au_sbbot(sb
);
1176 fpair
= kcalloc(bbot
+ 1, sizeof(*fpair
), GFP_NOFS
);
1177 if (unlikely(!fpair
))
1180 inode
= d_inode(sb
->s_root
);
1181 ino
= AUFS_ROOT_INO
;
1182 writef
= au_sbi(sb
)->si_xwrite
;
1183 for (bindex
= 0, p
= fpair
; bindex
<= bbot
; bindex
++, p
++) {
1184 bshared
= is_sb_shared(sb
, bindex
, bindex
- 1);
1187 *p
= fpair
[bshared
];
1193 br
= au_sbr(sb
, bindex
);
1194 p
->old
= br
->br_xino
.xi_file
;
1195 p
->new = au_xino_create2(base
, br
->br_xino
.xi_file
);
1196 err
= PTR_ERR(p
->new);
1197 if (IS_ERR(p
->new)) {
1203 err
= au_xino_do_write(writef
, p
->new,
1204 au_h_iptr(inode
, bindex
)->i_ino
, ino
);
1209 for (bindex
= 0, p
= fpair
; bindex
<= bbot
; bindex
++, p
++) {
1210 br
= au_sbr(sb
, bindex
);
1211 if (br
->br_xino
.xi_file
)
1212 fput(br
->br_xino
.xi_file
);
1214 br
->br_xino
.xi_file
= p
->new;
1218 for (bindex
= 0, p
= fpair
; bindex
<= bbot
; bindex
++, p
++)
1228 void au_xino_clr(struct super_block
*sb
)
1230 struct au_sbinfo
*sbinfo
;
1235 sbinfo
= au_sbi(sb
);
1236 /* lvalue, do not call au_mntflags() */
1237 au_opt_clr(sbinfo
->si_mntflags
, XINO
);
1240 int au_xino_set(struct super_block
*sb
, struct au_opt_xino
*xino
, int remount
)
1243 struct dentry
*parent
, *cur_parent
;
1244 struct qstr
*dname
, *cur_name
;
1245 struct file
*cur_xino
;
1247 struct au_sbinfo
*sbinfo
;
1249 SiMustWriteLock(sb
);
1252 sbinfo
= au_sbi(sb
);
1253 parent
= dget_parent(xino
->file
->f_path
.dentry
);
1256 dname
= &xino
->file
->f_path
.dentry
->d_name
;
1257 cur_xino
= sbinfo
->si_xib
;
1259 cur_parent
= dget_parent(cur_xino
->f_path
.dentry
);
1260 cur_name
= &cur_xino
->f_path
.dentry
->d_name
;
1261 skip
= (cur_parent
== parent
1262 && au_qstreq(dname
, cur_name
));
1269 au_opt_set(sbinfo
->si_mntflags
, XINO
);
1270 dir
= d_inode(parent
);
1271 inode_lock_nested(dir
, AuLsc_I_PARENT
);
1272 /* mnt_want_write() is unnecessary here */
1273 err
= au_xino_set_xib(sb
, xino
->file
);
1275 err
= au_xigen_set(sb
, xino
->file
);
1277 err
= au_xino_set_br(sb
, xino
->file
);
1280 goto out
; /* success */
1283 AuIOErr("failed creating xino(%d).\n", err
);
1292 /* ---------------------------------------------------------------------- */
1295 * create a xinofile at the default place/path.
1297 struct file
*au_xino_def(struct super_block
*sb
)
1301 struct au_branch
*br
;
1302 struct super_block
*h_sb
;
1304 aufs_bindex_t bbot
, bindex
, bwr
;
1307 bbot
= au_sbbot(sb
);
1309 for (bindex
= 0; bindex
<= bbot
; bindex
++) {
1310 br
= au_sbr(sb
, bindex
);
1311 if (au_br_writable(br
->br_perm
)
1312 && !au_test_fs_bad_xino(au_br_sb(br
))) {
1319 file
= ERR_PTR(-ENOMEM
);
1320 page
= (void *)__get_free_page(GFP_NOFS
);
1321 if (unlikely(!page
))
1323 path
.mnt
= au_br_mnt(br
);
1324 path
.dentry
= au_h_dptr(sb
->s_root
, bwr
);
1325 p
= d_path(&path
, page
, PATH_MAX
- sizeof(AUFS_XINO_FNAME
));
1328 strcat(p
, "/" AUFS_XINO_FNAME
);
1330 file
= au_xino_create(sb
, p
, /*silent*/0);
1332 au_xino_brid_set(sb
, br
->br_id
);
1334 free_page((unsigned long)page
);
1336 file
= au_xino_create(sb
, AUFS_XINO_DEFPATH
, /*silent*/0);
1339 h_sb
= file
->f_path
.dentry
->d_sb
;
1340 if (unlikely(au_test_fs_bad_xino(h_sb
))) {
1341 pr_err("xino doesn't support %s(%s)\n",
1342 AUFS_XINO_DEFPATH
, au_sbtype(h_sb
));
1344 file
= ERR_PTR(-EINVAL
);
1347 au_xino_brid_set(sb
, -1);
1354 /* ---------------------------------------------------------------------- */
1356 int au_xino_path(struct seq_file
*seq
, struct file
*file
)
1360 err
= au_seq_path(seq
, &file
->f_path
);
1364 #define Deleted "\\040(deleted)"
1365 seq
->count
-= sizeof(Deleted
) - 1;
1366 AuDebugOn(memcmp(seq
->buf
+ seq
->count
, Deleted
,
1367 sizeof(Deleted
) - 1));
1374 /* ---------------------------------------------------------------------- */
1376 void au_xinondir_leave(struct super_block
*sb
, aufs_bindex_t bindex
,
1377 ino_t h_ino
, int idx
)
1379 struct au_xino_file
*xino
;
1381 AuDebugOn(!au_opt_test(au_mntflags(sb
), XINO
));
1382 xino
= &au_sbr(sb
, bindex
)->br_xino
;
1383 AuDebugOn(idx
< 0 || xino
->xi_nondir
.total
<= idx
);
1385 spin_lock(&xino
->xi_nondir
.spin
);
1386 AuDebugOn(xino
->xi_nondir
.array
[idx
] != h_ino
);
1387 xino
->xi_nondir
.array
[idx
] = 0;
1388 spin_unlock(&xino
->xi_nondir
.spin
);
1389 wake_up_all(&xino
->xi_nondir
.wqh
);
1392 static int au_xinondir_find(struct au_xino_file
*xino
, ino_t h_ino
)
1394 int found
, total
, i
;
1397 total
= xino
->xi_nondir
.total
;
1398 for (i
= 0; i
< total
; i
++) {
1399 if (xino
->xi_nondir
.array
[i
] != h_ino
)
1408 static int au_xinondir_expand(struct au_xino_file
*xino
)
1413 BUILD_BUG_ON(KMALLOC_MAX_SIZE
> INT_MAX
);
1416 sz
= xino
->xi_nondir
.total
* sizeof(ino_t
);
1417 if (unlikely(sz
> KMALLOC_MAX_SIZE
/ 2))
1419 p
= au_kzrealloc(xino
->xi_nondir
.array
, sz
, sz
<< 1, GFP_ATOMIC
,
1422 xino
->xi_nondir
.array
= p
;
1423 xino
->xi_nondir
.total
<<= 1;
1424 AuDbg("xi_nondir.total %d\n", xino
->xi_nondir
.total
);
1432 int au_xinondir_enter(struct super_block
*sb
, aufs_bindex_t bindex
, ino_t h_ino
,
1435 int err
, found
, empty
;
1436 struct au_xino_file
*xino
;
1440 if (!au_opt_test(au_mntflags(sb
), XINO
))
1441 goto out
; /* no xino */
1443 xino
= &au_sbr(sb
, bindex
)->br_xino
;
1446 spin_lock(&xino
->xi_nondir
.spin
);
1447 found
= au_xinondir_find(xino
, h_ino
);
1449 empty
= au_xinondir_find(xino
, /*h_ino*/0);
1451 empty
= xino
->xi_nondir
.total
;
1452 err
= au_xinondir_expand(xino
);
1456 xino
->xi_nondir
.array
[empty
] = h_ino
;
1459 spin_unlock(&xino
->xi_nondir
.spin
);
1460 wait_event(xino
->xi_nondir
.wqh
,
1461 xino
->xi_nondir
.array
[found
] != h_ino
);
1466 spin_unlock(&xino
->xi_nondir
.spin
);