]>
git.proxmox.com Git - mirror_ubuntu-artful-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 /* todo: unnecessary to support mmap_sem since kernel-space? */
27 ssize_t
xino_fread(vfs_readf_t func
, struct file
*file
, void *kbuf
, size_t size
,
41 /* todo: signal_pending? */
42 err
= func(file
, buf
.u
, size
, pos
);
43 } while (err
== -EAGAIN
|| err
== -EINTR
);
46 #if 0 /* reserved for future use */
48 fsnotify_access(file
->f_path
.dentry
);
54 /* ---------------------------------------------------------------------- */
56 static ssize_t
xino_fwrite_wkq(vfs_writef_t func
, struct file
*file
, void *buf
,
57 size_t size
, loff_t
*pos
);
59 static ssize_t
do_xino_fwrite(vfs_writef_t func
, struct file
*file
, void *kbuf
,
60 size_t size
, loff_t
*pos
)
69 const int prevent_endless
= 10;
76 err
= func(file
, buf
.u
, size
, pos
);
79 && fatal_signal_pending(current
)) {
81 err
= xino_fwrite_wkq(func
, file
, kbuf
, size
, pos
);
82 BUG_ON(err
== -EINTR
);
86 } while (i
++ < prevent_endless
87 && (err
== -EAGAIN
|| err
== -EINTR
));
90 #if 0 /* reserved for future use */
92 fsnotify_modify(file
->f_path
.dentry
);
98 struct do_xino_fwrite_args
{
107 static void call_do_xino_fwrite(void *args
)
109 struct do_xino_fwrite_args
*a
= args
;
110 *a
->errp
= do_xino_fwrite(a
->func
, a
->file
, a
->buf
, a
->size
, a
->pos
);
113 static ssize_t
xino_fwrite_wkq(vfs_writef_t func
, struct file
*file
, void *buf
,
114 size_t size
, loff_t
*pos
)
118 struct do_xino_fwrite_args args
= {
128 * it breaks RLIMIT_FSIZE and normal user's limit,
129 * users should care about quota and real 'filesystem full.'
131 wkq_err
= au_wkq_wait(call_do_xino_fwrite
, &args
);
132 if (unlikely(wkq_err
))
138 ssize_t
xino_fwrite(vfs_writef_t func
, struct file
*file
, void *buf
,
139 size_t size
, loff_t
*pos
)
143 if (rlimit(RLIMIT_FSIZE
) == RLIM_INFINITY
) {
145 err
= do_xino_fwrite(func
, file
, buf
, size
, pos
);
148 err
= xino_fwrite_wkq(func
, file
, buf
, size
, pos
);
153 /* ---------------------------------------------------------------------- */
156 * create a new xinofile at the same place/path as @base_file.
158 struct file
*au_xino_create2(struct file
*base_file
, struct file
*copy_src
)
161 struct dentry
*base
, *parent
;
162 struct inode
*dir
, *delegated
;
167 base
= base_file
->f_path
.dentry
;
168 parent
= base
->d_parent
; /* dir inode is locked */
169 dir
= d_inode(parent
);
172 file
= ERR_PTR(-EINVAL
);
173 name
= &base
->d_name
;
174 path
.dentry
= vfsub_lookup_one_len(name
->name
, parent
, name
->len
);
175 if (IS_ERR(path
.dentry
)) {
176 file
= (void *)path
.dentry
;
177 pr_err("%pd lookup err %ld\n",
178 base
, PTR_ERR(path
.dentry
));
182 /* no need to mnt_want_write() since we call dentry_open() later */
183 err
= vfs_create(dir
, path
.dentry
, S_IRUGO
| S_IWUGO
, NULL
);
186 pr_err("%pd create err %d\n", base
, err
);
190 path
.mnt
= base_file
->f_path
.mnt
;
191 file
= vfsub_dentry_open(&path
,
192 O_RDWR
| O_CREAT
| O_EXCL
| O_LARGEFILE
193 /* | __FMODE_NONOTIFY */);
195 pr_err("%pd open err %ld\n", base
, PTR_ERR(file
));
200 err
= vfsub_unlink(dir
, &file
->f_path
, &delegated
, /*force*/0);
201 if (unlikely(err
== -EWOULDBLOCK
)) {
202 pr_warn("cannot retry for NFSv4 delegation"
203 " for an internal unlink\n");
207 pr_err("%pd unlink err %d\n", base
, err
);
212 /* no one can touch copy_src xino */
213 err
= au_copy_file(file
, copy_src
, vfsub_f_size_read(copy_src
));
215 pr_err("%pd copy err %d\n", base
, err
);
219 goto out_dput
; /* success */
230 struct au_xino_lock_dir
{
231 struct au_hinode
*hdir
;
232 struct dentry
*parent
;
236 static void au_xino_lock_dir(struct super_block
*sb
, struct file
*xino
,
237 struct au_xino_lock_dir
*ldir
)
239 aufs_bindex_t brid
, bindex
;
243 brid
= au_xino_brid(sb
);
245 bindex
= au_br_index(sb
, brid
);
247 ldir
->hdir
= au_hi(d_inode(sb
->s_root
), bindex
);
248 au_hn_inode_lock_nested(ldir
->hdir
, AuLsc_I_PARENT
);
250 ldir
->parent
= dget_parent(xino
->f_path
.dentry
);
251 ldir
->dir
= d_inode(ldir
->parent
);
252 inode_lock_nested(ldir
->dir
, AuLsc_I_PARENT
);
256 static void au_xino_unlock_dir(struct au_xino_lock_dir
*ldir
)
259 au_hn_inode_unlock(ldir
->hdir
);
261 inode_unlock(ldir
->dir
);
266 /* ---------------------------------------------------------------------- */
268 /* trucate xino files asynchronously */
270 int au_xino_trunc(struct super_block
*sb
, aufs_bindex_t bindex
)
275 aufs_bindex_t bi
, bbot
;
277 struct au_branch
*br
;
278 struct file
*new_xino
, *file
;
279 struct super_block
*h_sb
;
280 struct au_xino_lock_dir ldir
;
283 st
= kmalloc(sizeof(*st
), GFP_NOFS
);
289 if (unlikely(bindex
< 0 || bbot
< bindex
))
291 br
= au_sbr(sb
, bindex
);
292 file
= br
->br_xino
.xi_file
;
296 err
= vfs_statfs(&file
->f_path
, st
);
298 AuErr1("statfs err %d, ignored\n", err
);
300 blocks
= file_inode(file
)->i_blocks
;
301 pr_info("begin truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
302 bindex
, (u64
)blocks
, st
->f_bfree
, st
->f_blocks
);
304 au_xino_lock_dir(sb
, file
, &ldir
);
305 /* mnt_want_write() is unnecessary here */
306 new_xino
= au_xino_create2(file
, file
);
307 au_xino_unlock_dir(&ldir
);
308 err
= PTR_ERR(new_xino
);
309 if (IS_ERR(new_xino
)) {
310 pr_err("err %d, ignored\n", err
);
315 br
->br_xino
.xi_file
= new_xino
;
318 for (bi
= 0; bi
<= bbot
; bi
++) {
319 if (unlikely(bi
== bindex
))
322 if (au_br_sb(br
) != h_sb
)
325 fput(br
->br_xino
.xi_file
);
326 br
->br_xino
.xi_file
= new_xino
;
330 err
= vfs_statfs(&new_xino
->f_path
, st
);
332 pr_info("end truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
333 bindex
, (u64
)file_inode(new_xino
)->i_blocks
,
334 st
->f_bfree
, st
->f_blocks
);
335 if (file_inode(new_xino
)->i_blocks
< blocks
)
336 au_sbi(sb
)->si_xino_jiffy
= jiffy
;
338 AuErr1("statfs err %d, ignored\n", err
);
346 struct xino_do_trunc_args
{
347 struct super_block
*sb
;
348 struct au_branch
*br
;
351 static void xino_do_trunc(void *_args
)
353 struct xino_do_trunc_args
*args
= _args
;
354 struct super_block
*sb
;
355 struct au_branch
*br
;
358 aufs_bindex_t bindex
;
362 dir
= d_inode(sb
->s_root
);
365 si_noflush_write_lock(sb
);
366 ii_read_lock_parent(dir
);
367 bindex
= au_br_index(sb
, br
->br_id
);
368 err
= au_xino_trunc(sb
, bindex
);
371 pr_warn("err b%d, (%d)\n", bindex
, err
);
372 atomic_dec(&br
->br_xino_running
);
375 au_nwt_done(&au_sbi(sb
)->si_nowait
);
379 static int xino_trunc_test(struct super_block
*sb
, struct au_branch
*br
)
383 struct au_sbinfo
*sbinfo
;
385 /* todo: si_xino_expire and the ratio should be customizable */
387 if (time_before(jiffies
,
388 sbinfo
->si_xino_jiffy
+ sbinfo
->si_xino_expire
))
391 /* truncation border */
392 err
= vfs_statfs(&br
->br_xino
.xi_file
->f_path
, &st
);
394 AuErr1("statfs err %d, ignored\n", err
);
397 if (div64_u64(st
.f_bfree
* 100, st
.f_blocks
) >= AUFS_XINO_DEF_TRUNC
)
403 static void xino_try_trunc(struct super_block
*sb
, struct au_branch
*br
)
405 struct xino_do_trunc_args
*args
;
408 if (!xino_trunc_test(sb
, br
))
411 if (atomic_inc_return(&br
->br_xino_running
) > 1)
414 /* lock and kfree() will be called in trunc_xino() */
415 args
= kmalloc(sizeof(*args
), GFP_NOFS
);
416 if (unlikely(!args
)) {
417 AuErr1("no memory\n");
424 wkq_err
= au_wkq_nowait(xino_do_trunc
, args
, sb
, /*flags*/0);
426 return; /* success */
428 pr_err("wkq %d\n", wkq_err
);
433 atomic_dec(&br
->br_xino_running
);
436 /* ---------------------------------------------------------------------- */
438 static int au_xino_do_write(vfs_writef_t write
, struct file
*file
,
439 ino_t h_ino
, ino_t ino
)
445 if (unlikely(au_loff_max
/ sizeof(ino
) - 1 < pos
)) {
446 AuIOErr1("too large hi%lu\n", (unsigned long)h_ino
);
450 sz
= xino_fwrite(write
, file
, &ino
, sizeof(ino
), &pos
);
451 if (sz
== sizeof(ino
))
452 return 0; /* success */
454 AuIOErr("write failed (%zd)\n", sz
);
459 * write @ino to the xinofile for the specified branch{@sb, @bindex}
460 * at the position of @h_ino.
461 * even if @ino is zero, it is written to the xinofile and means no entry.
462 * if the size of the xino file on a specific filesystem exceeds the watermark,
465 int au_xino_write(struct super_block
*sb
, aufs_bindex_t bindex
, ino_t h_ino
,
469 unsigned int mnt_flags
;
470 struct au_branch
*br
;
472 BUILD_BUG_ON(sizeof(long long) != sizeof(au_loff_max
)
473 || ((loff_t
)-1) > 0);
476 mnt_flags
= au_mntflags(sb
);
477 if (!au_opt_test(mnt_flags
, XINO
))
480 br
= au_sbr(sb
, bindex
);
481 err
= au_xino_do_write(au_sbi(sb
)->si_xwrite
, br
->br_xino
.xi_file
,
484 if (au_opt_test(mnt_flags
, TRUNC_XINO
)
485 && au_test_fs_trunc_xino(au_br_sb(br
)))
486 xino_try_trunc(sb
, br
);
487 return 0; /* success */
490 AuIOErr("write failed (%d)\n", err
);
494 /* ---------------------------------------------------------------------- */
496 /* aufs inode number bitmap */
498 static const int page_bits
= (int)PAGE_SIZE
* BITS_PER_BYTE
;
499 static ino_t
xib_calc_ino(unsigned long pindex
, int bit
)
503 AuDebugOn(bit
< 0 || page_bits
<= bit
);
504 ino
= AUFS_FIRST_INO
+ pindex
* page_bits
+ bit
;
508 static void xib_calc_bit(ino_t ino
, unsigned long *pindex
, int *bit
)
510 AuDebugOn(ino
< AUFS_FIRST_INO
);
511 ino
-= AUFS_FIRST_INO
;
512 *pindex
= ino
/ page_bits
;
513 *bit
= ino
% page_bits
;
516 static int xib_pindex(struct super_block
*sb
, unsigned long pindex
)
521 struct au_sbinfo
*sbinfo
;
526 MtxMustLock(&sbinfo
->si_xib_mtx
);
527 AuDebugOn(pindex
> ULONG_MAX
/ PAGE_SIZE
528 || !au_opt_test(sbinfo
->si_mntflags
, XINO
));
530 if (pindex
== sbinfo
->si_xib_last_pindex
)
533 xib
= sbinfo
->si_xib
;
534 p
= sbinfo
->si_xib_buf
;
535 pos
= sbinfo
->si_xib_last_pindex
;
537 sz
= xino_fwrite(sbinfo
->si_xwrite
, xib
, p
, PAGE_SIZE
, &pos
);
538 if (unlikely(sz
!= PAGE_SIZE
))
543 if (vfsub_f_size_read(xib
) >= pos
+ PAGE_SIZE
)
544 sz
= xino_fread(sbinfo
->si_xread
, xib
, p
, PAGE_SIZE
, &pos
);
546 memset(p
, 0, PAGE_SIZE
);
547 sz
= xino_fwrite(sbinfo
->si_xwrite
, xib
, p
, PAGE_SIZE
, &pos
);
549 if (sz
== PAGE_SIZE
) {
550 sbinfo
->si_xib_last_pindex
= pindex
;
551 return 0; /* success */
555 AuIOErr1("write failed (%zd)\n", sz
);
562 /* ---------------------------------------------------------------------- */
564 static void au_xib_clear_bit(struct inode
*inode
)
567 unsigned long pindex
;
568 struct super_block
*sb
;
569 struct au_sbinfo
*sbinfo
;
571 AuDebugOn(inode
->i_nlink
);
574 xib_calc_bit(inode
->i_ino
, &pindex
, &bit
);
575 AuDebugOn(page_bits
<= bit
);
577 mutex_lock(&sbinfo
->si_xib_mtx
);
578 err
= xib_pindex(sb
, pindex
);
580 clear_bit(bit
, sbinfo
->si_xib_buf
);
581 sbinfo
->si_xib_next_bit
= bit
;
583 mutex_unlock(&sbinfo
->si_xib_mtx
);
586 /* for s_op->delete_inode() */
587 void au_xino_delete_inode(struct inode
*inode
, const int unlinked
)
590 unsigned int mnt_flags
;
591 aufs_bindex_t bindex
, bbot
, bi
;
592 unsigned char try_trunc
;
593 struct au_iinfo
*iinfo
;
594 struct super_block
*sb
;
595 struct au_hinode
*hi
;
596 struct inode
*h_inode
;
597 struct au_branch
*br
;
600 AuDebugOn(au_is_bad_inode(inode
));
603 mnt_flags
= au_mntflags(sb
);
604 if (!au_opt_test(mnt_flags
, XINO
)
605 || inode
->i_ino
== AUFS_ROOT_INO
)
610 au_xib_clear_bit(inode
);
613 iinfo
= au_ii(inode
);
614 bindex
= iinfo
->ii_btop
;
618 xwrite
= au_sbi(sb
)->si_xwrite
;
619 try_trunc
= !!au_opt_test(mnt_flags
, TRUNC_XINO
);
620 hi
= au_hinode(iinfo
, bindex
);
621 bbot
= iinfo
->ii_bbot
;
622 for (; bindex
<= bbot
; bindex
++, hi
++) {
623 h_inode
= hi
->hi_inode
;
625 || (!unlinked
&& h_inode
->i_nlink
))
628 /* inode may not be revalidated */
629 bi
= au_br_index(sb
, hi
->hi_id
);
634 err
= au_xino_do_write(xwrite
, br
->br_xino
.xi_file
,
635 h_inode
->i_ino
, /*ino*/0);
636 if (!err
&& try_trunc
637 && au_test_fs_trunc_xino(au_br_sb(br
)))
638 xino_try_trunc(sb
, br
);
642 /* get an unused inode number from bitmap */
643 ino_t
au_xino_new_ino(struct super_block
*sb
)
646 unsigned long *p
, pindex
, ul
, pend
;
647 struct au_sbinfo
*sbinfo
;
651 if (!au_opt_test(au_mntflags(sb
), XINO
))
652 return iunique(sb
, AUFS_FIRST_INO
);
655 mutex_lock(&sbinfo
->si_xib_mtx
);
656 p
= sbinfo
->si_xib_buf
;
657 free_bit
= sbinfo
->si_xib_next_bit
;
658 if (free_bit
< page_bits
&& !test_bit(free_bit
, p
))
659 goto out
; /* success */
660 free_bit
= find_first_zero_bit(p
, page_bits
);
661 if (free_bit
< page_bits
)
662 goto out
; /* success */
664 pindex
= sbinfo
->si_xib_last_pindex
;
665 for (ul
= pindex
- 1; ul
< ULONG_MAX
; ul
--) {
666 err
= xib_pindex(sb
, ul
);
669 free_bit
= find_first_zero_bit(p
, page_bits
);
670 if (free_bit
< page_bits
)
671 goto out
; /* success */
674 file
= sbinfo
->si_xib
;
675 pend
= vfsub_f_size_read(file
) / PAGE_SIZE
;
676 for (ul
= pindex
+ 1; ul
<= pend
; ul
++) {
677 err
= xib_pindex(sb
, ul
);
680 free_bit
= find_first_zero_bit(p
, page_bits
);
681 if (free_bit
< page_bits
)
682 goto out
; /* success */
687 set_bit(free_bit
, p
);
688 sbinfo
->si_xib_next_bit
= free_bit
+ 1;
689 pindex
= sbinfo
->si_xib_last_pindex
;
690 mutex_unlock(&sbinfo
->si_xib_mtx
);
691 ino
= xib_calc_ino(pindex
, free_bit
);
692 AuDbg("i%lu\n", (unsigned long)ino
);
695 mutex_unlock(&sbinfo
->si_xib_mtx
);
701 * read @ino from xinofile for the specified branch{@sb, @bindex}
702 * at the position of @h_ino.
703 * if @ino does not exist and @do_new is true, get new one.
705 int au_xino_read(struct super_block
*sb
, aufs_bindex_t bindex
, ino_t h_ino
,
712 struct au_sbinfo
*sbinfo
;
715 if (!au_opt_test(au_mntflags(sb
), XINO
))
716 return 0; /* no xino */
721 if (unlikely(au_loff_max
/ sizeof(*ino
) - 1 < pos
)) {
722 AuIOErr1("too large hi%lu\n", (unsigned long)h_ino
);
727 file
= au_sbr(sb
, bindex
)->br_xino
.xi_file
;
728 if (vfsub_f_size_read(file
) < pos
+ sizeof(*ino
))
729 return 0; /* no ino */
731 sz
= xino_fread(sbinfo
->si_xread
, file
, ino
, sizeof(*ino
), &pos
);
732 if (sz
== sizeof(*ino
))
733 return 0; /* success */
736 if (unlikely(sz
>= 0)) {
738 AuIOErr("xino read error (%zd)\n", sz
);
744 /* ---------------------------------------------------------------------- */
746 /* create and set a new xino file */
748 struct file
*au_xino_create(struct super_block
*sb
, char *fname
, int silent
)
751 struct dentry
*h_parent
, *d
;
752 struct inode
*h_dir
, *inode
;
756 * at mount-time, and the xino file is the default path,
757 * hnotify is disabled so we have no notify events to ignore.
758 * when a user specified the xino, we cannot get au_hdir to be ignored.
760 file
= vfsub_filp_open(fname
, O_RDWR
| O_CREAT
| O_EXCL
| O_LARGEFILE
761 /* | __FMODE_NONOTIFY */,
765 pr_err("open %s(%ld)\n", fname
, PTR_ERR(file
));
769 /* keep file count */
771 inode
= file_inode(file
);
772 h_parent
= dget_parent(file
->f_path
.dentry
);
773 h_dir
= d_inode(h_parent
);
774 inode_lock_nested(h_dir
, AuLsc_I_PARENT
);
775 /* mnt_want_write() is unnecessary here */
776 /* no delegation since it is just created */
778 err
= vfsub_unlink(h_dir
, &file
->f_path
, /*delegated*/NULL
,
784 pr_err("unlink %s(%d)\n", fname
, err
);
789 d
= file
->f_path
.dentry
;
790 if (unlikely(sb
== d
->d_sb
)) {
792 pr_err("%s must be outside\n", fname
);
795 if (unlikely(au_test_fs_bad_xino(d
->d_sb
))) {
797 pr_err("xino doesn't support %s(%s)\n",
798 fname
, au_sbtype(d
->d_sb
));
801 return file
; /* success */
810 * find another branch who is on the same filesystem of the specified
811 * branch{@btgt}. search until @bbot.
813 static int is_sb_shared(struct super_block
*sb
, aufs_bindex_t btgt
,
816 aufs_bindex_t bindex
;
817 struct super_block
*tgt_sb
= au_sbr_sb(sb
, btgt
);
819 for (bindex
= 0; bindex
< btgt
; bindex
++)
820 if (unlikely(tgt_sb
== au_sbr_sb(sb
, bindex
)))
822 for (bindex
++; bindex
<= bbot
; bindex
++)
823 if (unlikely(tgt_sb
== au_sbr_sb(sb
, bindex
)))
828 /* ---------------------------------------------------------------------- */
831 * initialize the xinofile for the specified branch @br
832 * at the place/path where @base_file indicates.
833 * test whether another branch is on the same filesystem or not,
834 * if @do_test is true.
836 int au_xino_br(struct super_block
*sb
, struct au_branch
*br
, ino_t h_ino
,
837 struct file
*base_file
, int do_test
)
841 aufs_bindex_t bbot
, bindex
;
842 struct au_branch
*shared_br
, *b
;
844 struct super_block
*tgt_sb
;
849 tgt_sb
= au_br_sb(br
);
850 for (bindex
= 0; bindex
<= bbot
; bindex
++) {
851 b
= au_sbr(sb
, bindex
);
852 if (tgt_sb
== au_br_sb(b
)) {
859 if (!shared_br
|| !shared_br
->br_xino
.xi_file
) {
860 struct au_xino_lock_dir ldir
;
862 au_xino_lock_dir(sb
, base_file
, &ldir
);
863 /* mnt_want_write() is unnecessary here */
864 file
= au_xino_create2(base_file
, NULL
);
865 au_xino_unlock_dir(&ldir
);
869 br
->br_xino
.xi_file
= file
;
871 br
->br_xino
.xi_file
= shared_br
->br_xino
.xi_file
;
872 get_file(br
->br_xino
.xi_file
);
876 err
= au_xino_do_write(au_sbi(sb
)->si_xwrite
, br
->br_xino
.xi_file
,
879 fput(br
->br_xino
.xi_file
);
880 br
->br_xino
.xi_file
= NULL
;
887 /* ---------------------------------------------------------------------- */
889 /* trucate a xino bitmap file */
892 static int do_xib_restore(struct super_block
*sb
, struct file
*file
, void *page
)
896 unsigned long pindex
;
898 struct au_sbinfo
*sbinfo
;
905 MtxMustLock(&sbinfo
->si_xib_mtx
);
906 p
= sbinfo
->si_xib_buf
;
907 func
= sbinfo
->si_xread
;
908 pend
= vfsub_f_size_read(file
);
911 sz
= xino_fread(func
, file
, page
, PAGE_SIZE
, &pos
);
913 if (unlikely(sz
<= 0))
917 for (ino
= page
; sz
> 0; ino
++, sz
-= sizeof(ino
)) {
918 if (unlikely(*ino
< AUFS_FIRST_INO
))
921 xib_calc_bit(*ino
, &pindex
, &bit
);
922 AuDebugOn(page_bits
<= bit
);
923 err
= xib_pindex(sb
, pindex
);
935 static int xib_restore(struct super_block
*sb
)
938 aufs_bindex_t bindex
, bbot
;
942 page
= (void *)__get_free_page(GFP_NOFS
);
948 for (bindex
= 0; !err
&& bindex
<= bbot
; bindex
++)
949 if (!bindex
|| is_sb_shared(sb
, bindex
, bindex
- 1) < 0)
951 (sb
, au_sbr(sb
, bindex
)->br_xino
.xi_file
, page
);
953 AuDbg("b%d\n", bindex
);
954 free_page((unsigned long)page
);
960 int au_xib_trunc(struct super_block
*sb
)
965 struct au_xino_lock_dir ldir
;
966 struct au_sbinfo
*sbinfo
;
974 if (!au_opt_test(sbinfo
->si_mntflags
, XINO
))
977 file
= sbinfo
->si_xib
;
978 if (vfsub_f_size_read(file
) <= PAGE_SIZE
)
981 au_xino_lock_dir(sb
, file
, &ldir
);
982 /* mnt_want_write() is unnecessary here */
983 file
= au_xino_create2(sbinfo
->si_xib
, NULL
);
984 au_xino_unlock_dir(&ldir
);
988 fput(sbinfo
->si_xib
);
989 sbinfo
->si_xib
= file
;
991 p
= sbinfo
->si_xib_buf
;
992 memset(p
, 0, PAGE_SIZE
);
994 sz
= xino_fwrite(sbinfo
->si_xwrite
, sbinfo
->si_xib
, p
, PAGE_SIZE
, &pos
);
995 if (unlikely(sz
!= PAGE_SIZE
)) {
997 AuIOErr("err %d\n", err
);
1003 mutex_lock(&sbinfo
->si_xib_mtx
);
1004 /* mnt_want_write() is unnecessary here */
1005 err
= xib_restore(sb
);
1006 mutex_unlock(&sbinfo
->si_xib_mtx
);
1012 /* ---------------------------------------------------------------------- */
1015 * xino mount option handlers
1019 static void xino_clear_xib(struct super_block
*sb
)
1021 struct au_sbinfo
*sbinfo
;
1023 SiMustWriteLock(sb
);
1025 sbinfo
= au_sbi(sb
);
1026 sbinfo
->si_xread
= NULL
;
1027 sbinfo
->si_xwrite
= NULL
;
1029 fput(sbinfo
->si_xib
);
1030 sbinfo
->si_xib
= NULL
;
1031 if (sbinfo
->si_xib_buf
)
1032 free_page((unsigned long)sbinfo
->si_xib_buf
);
1033 sbinfo
->si_xib_buf
= NULL
;
1036 static int au_xino_set_xib(struct super_block
*sb
, struct file
*base
)
1040 struct au_sbinfo
*sbinfo
;
1043 SiMustWriteLock(sb
);
1045 sbinfo
= au_sbi(sb
);
1046 file
= au_xino_create2(base
, sbinfo
->si_xib
);
1047 err
= PTR_ERR(file
);
1051 fput(sbinfo
->si_xib
);
1052 sbinfo
->si_xib
= file
;
1053 sbinfo
->si_xread
= vfs_readf(file
);
1054 sbinfo
->si_xwrite
= vfs_writef(file
);
1057 if (!sbinfo
->si_xib_buf
)
1058 sbinfo
->si_xib_buf
= (void *)get_zeroed_page(GFP_NOFS
);
1059 if (unlikely(!sbinfo
->si_xib_buf
))
1062 sbinfo
->si_xib_last_pindex
= 0;
1063 sbinfo
->si_xib_next_bit
= 0;
1064 if (vfsub_f_size_read(file
) < PAGE_SIZE
) {
1066 err
= xino_fwrite(sbinfo
->si_xwrite
, file
, sbinfo
->si_xib_buf
,
1068 if (unlikely(err
!= PAGE_SIZE
))
1072 goto out
; /* success */
1075 if (sbinfo
->si_xib_buf
)
1076 free_page((unsigned long)sbinfo
->si_xib_buf
);
1077 sbinfo
->si_xib_buf
= NULL
;
1081 fput(sbinfo
->si_xib
);
1082 sbinfo
->si_xib
= NULL
;
1083 sbinfo
->si_xread
= NULL
;
1084 sbinfo
->si_xwrite
= NULL
;
1089 /* xino for each branch */
1090 static void xino_clear_br(struct super_block
*sb
)
1092 aufs_bindex_t bindex
, bbot
;
1093 struct au_branch
*br
;
1095 bbot
= au_sbbot(sb
);
1096 for (bindex
= 0; bindex
<= bbot
; bindex
++) {
1097 br
= au_sbr(sb
, bindex
);
1098 if (!br
|| !br
->br_xino
.xi_file
)
1101 fput(br
->br_xino
.xi_file
);
1102 br
->br_xino
.xi_file
= NULL
;
1106 static int au_xino_set_br(struct super_block
*sb
, struct file
*base
)
1110 aufs_bindex_t bindex
, bbot
, bshared
;
1112 struct file
*old
, *new;
1114 struct au_branch
*br
;
1115 struct inode
*inode
;
1116 vfs_writef_t writef
;
1118 SiMustWriteLock(sb
);
1121 bbot
= au_sbbot(sb
);
1122 fpair
= kcalloc(bbot
+ 1, sizeof(*fpair
), GFP_NOFS
);
1123 if (unlikely(!fpair
))
1126 inode
= d_inode(sb
->s_root
);
1127 ino
= AUFS_ROOT_INO
;
1128 writef
= au_sbi(sb
)->si_xwrite
;
1129 for (bindex
= 0, p
= fpair
; bindex
<= bbot
; bindex
++, p
++) {
1130 bshared
= is_sb_shared(sb
, bindex
, bindex
- 1);
1133 *p
= fpair
[bshared
];
1139 br
= au_sbr(sb
, bindex
);
1140 p
->old
= br
->br_xino
.xi_file
;
1141 p
->new = au_xino_create2(base
, br
->br_xino
.xi_file
);
1142 err
= PTR_ERR(p
->new);
1143 if (IS_ERR(p
->new)) {
1149 err
= au_xino_do_write(writef
, p
->new,
1150 au_h_iptr(inode
, bindex
)->i_ino
, ino
);
1155 for (bindex
= 0, p
= fpair
; bindex
<= bbot
; bindex
++, p
++) {
1156 br
= au_sbr(sb
, bindex
);
1157 if (br
->br_xino
.xi_file
)
1158 fput(br
->br_xino
.xi_file
);
1160 br
->br_xino
.xi_file
= p
->new;
1164 for (bindex
= 0, p
= fpair
; bindex
<= bbot
; bindex
++, p
++)
1174 void au_xino_clr(struct super_block
*sb
)
1176 struct au_sbinfo
*sbinfo
;
1181 sbinfo
= au_sbi(sb
);
1182 /* lvalue, do not call au_mntflags() */
1183 au_opt_clr(sbinfo
->si_mntflags
, XINO
);
1186 int au_xino_set(struct super_block
*sb
, struct au_opt_xino
*xino
, int remount
)
1189 struct dentry
*parent
, *cur_parent
;
1190 struct qstr
*dname
, *cur_name
;
1191 struct file
*cur_xino
;
1193 struct au_sbinfo
*sbinfo
;
1195 SiMustWriteLock(sb
);
1198 sbinfo
= au_sbi(sb
);
1199 parent
= dget_parent(xino
->file
->f_path
.dentry
);
1202 dname
= &xino
->file
->f_path
.dentry
->d_name
;
1203 cur_xino
= sbinfo
->si_xib
;
1205 cur_parent
= dget_parent(cur_xino
->f_path
.dentry
);
1206 cur_name
= &cur_xino
->f_path
.dentry
->d_name
;
1207 skip
= (cur_parent
== parent
1208 && au_qstreq(dname
, cur_name
));
1215 au_opt_set(sbinfo
->si_mntflags
, XINO
);
1216 dir
= d_inode(parent
);
1217 inode_lock_nested(dir
, AuLsc_I_PARENT
);
1218 /* mnt_want_write() is unnecessary here */
1219 err
= au_xino_set_xib(sb
, xino
->file
);
1221 err
= au_xigen_set(sb
, xino
->file
);
1223 err
= au_xino_set_br(sb
, xino
->file
);
1226 goto out
; /* success */
1229 AuIOErr("failed creating xino(%d).\n", err
);
1238 /* ---------------------------------------------------------------------- */
1241 * create a xinofile at the default place/path.
1243 struct file
*au_xino_def(struct super_block
*sb
)
1247 struct au_branch
*br
;
1248 struct super_block
*h_sb
;
1250 aufs_bindex_t bbot
, bindex
, bwr
;
1253 bbot
= au_sbbot(sb
);
1255 for (bindex
= 0; bindex
<= bbot
; bindex
++) {
1256 br
= au_sbr(sb
, bindex
);
1257 if (au_br_writable(br
->br_perm
)
1258 && !au_test_fs_bad_xino(au_br_sb(br
))) {
1265 file
= ERR_PTR(-ENOMEM
);
1266 page
= (void *)__get_free_page(GFP_NOFS
);
1267 if (unlikely(!page
))
1269 path
.mnt
= au_br_mnt(br
);
1270 path
.dentry
= au_h_dptr(sb
->s_root
, bwr
);
1271 p
= d_path(&path
, page
, PATH_MAX
- sizeof(AUFS_XINO_FNAME
));
1274 strcat(p
, "/" AUFS_XINO_FNAME
);
1276 file
= au_xino_create(sb
, p
, /*silent*/0);
1278 au_xino_brid_set(sb
, br
->br_id
);
1280 free_page((unsigned long)page
);
1282 file
= au_xino_create(sb
, AUFS_XINO_DEFPATH
, /*silent*/0);
1285 h_sb
= file
->f_path
.dentry
->d_sb
;
1286 if (unlikely(au_test_fs_bad_xino(h_sb
))) {
1287 pr_err("xino doesn't support %s(%s)\n",
1288 AUFS_XINO_DEFPATH
, au_sbtype(h_sb
));
1290 file
= ERR_PTR(-EINVAL
);
1293 au_xino_brid_set(sb
, -1);
1300 /* ---------------------------------------------------------------------- */
1302 int au_xino_path(struct seq_file
*seq
, struct file
*file
)
1306 err
= au_seq_path(seq
, &file
->f_path
);
1310 #define Deleted "\\040(deleted)"
1311 seq
->count
-= sizeof(Deleted
) - 1;
1312 AuDebugOn(memcmp(seq
->buf
+ seq
->count
, Deleted
,
1313 sizeof(Deleted
) - 1));
1320 /* ---------------------------------------------------------------------- */
1322 void au_xinondir_leave(struct super_block
*sb
, aufs_bindex_t bindex
,
1323 ino_t h_ino
, int idx
)
1325 struct au_xino_file
*xino
;
1327 AuDebugOn(!au_opt_test(au_mntflags(sb
), XINO
));
1328 xino
= &au_sbr(sb
, bindex
)->br_xino
;
1329 AuDebugOn(idx
< 0 || xino
->xi_nondir
.total
<= idx
);
1331 spin_lock(&xino
->xi_nondir
.spin
);
1332 AuDebugOn(xino
->xi_nondir
.array
[idx
] != h_ino
);
1333 xino
->xi_nondir
.array
[idx
] = 0;
1334 spin_unlock(&xino
->xi_nondir
.spin
);
1335 wake_up_all(&xino
->xi_nondir
.wqh
);
1338 static int au_xinondir_find(struct au_xino_file
*xino
, ino_t h_ino
)
1340 int found
, total
, i
;
1343 total
= xino
->xi_nondir
.total
;
1344 for (i
= 0; i
< total
; i
++) {
1345 if (xino
->xi_nondir
.array
[i
] != h_ino
)
1354 static int au_xinondir_expand(struct au_xino_file
*xino
)
1359 BUILD_BUG_ON(KMALLOC_MAX_SIZE
> INT_MAX
);
1362 sz
= xino
->xi_nondir
.total
* sizeof(ino_t
);
1363 if (unlikely(sz
> KMALLOC_MAX_SIZE
/ 2))
1365 p
= au_kzrealloc(xino
->xi_nondir
.array
, sz
, sz
<< 1, GFP_ATOMIC
,
1368 xino
->xi_nondir
.array
= p
;
1369 xino
->xi_nondir
.total
<<= 1;
1370 AuDbg("xi_nondir.total %d\n", xino
->xi_nondir
.total
);
1378 int au_xinondir_enter(struct super_block
*sb
, aufs_bindex_t bindex
, ino_t h_ino
,
1381 int err
, found
, empty
;
1382 struct au_xino_file
*xino
;
1386 if (!au_opt_test(au_mntflags(sb
), XINO
))
1387 goto out
; /* no xino */
1389 xino
= &au_sbr(sb
, bindex
)->br_xino
;
1392 spin_lock(&xino
->xi_nondir
.spin
);
1393 found
= au_xinondir_find(xino
, h_ino
);
1395 empty
= au_xinondir_find(xino
, /*h_ino*/0);
1397 empty
= xino
->xi_nondir
.total
;
1398 err
= au_xinondir_expand(xino
);
1402 xino
->xi_nondir
.array
[empty
] = h_ino
;
1405 spin_unlock(&xino
->xi_nondir
.spin
);
1406 wait_event(xino
->xi_nondir
.wqh
,
1407 xino
->xi_nondir
.array
[found
] != h_ino
);
1412 spin_unlock(&xino
->xi_nondir
.spin
);