2 * Copyright (C) 2005-2013 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, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * external inode number translation table and bitmap
23 #include <linux/seq_file.h>
24 #include <linux/statfs.h>
27 /* todo: unnecessary to support mmap_sem since kernel-space? */
28 ssize_t xino_fread(au_readf_t func, struct file *file, void *kbuf, size_t size,
42 /* todo: signal_pending? */
43 err = func(file, buf.u, size, pos);
44 } while (err == -EAGAIN || err == -EINTR);
47 #if 0 /* reserved for future use */
49 fsnotify_access(file->f_dentry);
55 /* ---------------------------------------------------------------------- */
57 static ssize_t do_xino_fwrite(au_writef_t func, struct file *file, void *kbuf,
58 size_t size, loff_t *pos)
71 /* todo: signal_pending? */
72 err = func(file, buf.u, size, pos);
73 } while (err == -EAGAIN || err == -EINTR);
76 #if 0 /* reserved for future use */
78 fsnotify_modify(file->f_dentry);
84 struct do_xino_fwrite_args {
93 static void call_do_xino_fwrite(void *args)
95 struct do_xino_fwrite_args *a = args;
96 *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
99 ssize_t xino_fwrite(au_writef_t func, struct file *file, void *buf, size_t size,
104 /* todo: signal block and no wkq? */
105 if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
107 err = do_xino_fwrite(func, file, buf, size, pos);
111 * it breaks RLIMIT_FSIZE and normal user's limit,
112 * users should care about quota and real 'filesystem full.'
115 struct do_xino_fwrite_args args = {
124 wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
125 if (unlikely(wkq_err))
132 /* ---------------------------------------------------------------------- */
135 * create a new xinofile at the same place/path as @base_file.
137 struct file *au_xino_create2(struct file *base_file, struct file *copy_src)
140 struct dentry *base, *parent;
146 base = base_file->f_dentry;
147 parent = base->d_parent; /* dir inode is locked */
148 dir = parent->d_inode;
151 file = ERR_PTR(-EINVAL);
152 name = &base->d_name;
153 path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
154 if (IS_ERR(path.dentry)) {
155 file = (void *)path.dentry;
156 pr_err("%.*s lookup err %ld\n",
157 AuLNPair(name), PTR_ERR(path.dentry));
161 /* no need to mnt_want_write() since we call dentry_open() later */
162 err = vfs_create(dir, path.dentry, S_IRUGO | S_IWUGO, NULL);
165 pr_err("%.*s create err %d\n", AuLNPair(name), err);
169 path.mnt = base_file->f_vfsmnt;
170 file = vfsub_dentry_open(&path,
171 O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
172 /* | __FMODE_NONOTIFY */);
174 pr_err("%.*s open err %ld\n", AuLNPair(name), PTR_ERR(file));
178 err = vfsub_unlink(dir, &file->f_path, /*force*/0);
180 pr_err("%.*s unlink err %d\n", AuLNPair(name), err);
185 /* no one can touch copy_src xino */
186 err = au_copy_file(file, copy_src,
187 i_size_read(copy_src->f_dentry->d_inode));
189 pr_err("%.*s copy err %d\n", AuLNPair(name), err);
193 goto out_dput; /* success */
204 struct au_xino_lock_dir {
205 struct au_hinode *hdir;
206 struct dentry *parent;
210 static void au_xino_lock_dir(struct super_block *sb, struct file *xino,
211 struct au_xino_lock_dir *ldir)
213 aufs_bindex_t brid, bindex;
217 brid = au_xino_brid(sb);
219 bindex = au_br_index(sb, brid);
221 ldir->hdir = au_hi(sb->s_root->d_inode, bindex);
222 au_hn_imtx_lock_nested(ldir->hdir, AuLsc_I_PARENT);
224 ldir->parent = dget_parent(xino->f_dentry);
225 ldir->mtx = &ldir->parent->d_inode->i_mutex;
226 mutex_lock_nested(ldir->mtx, AuLsc_I_PARENT);
230 static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
233 au_hn_imtx_unlock(ldir->hdir);
235 mutex_unlock(ldir->mtx);
240 /* ---------------------------------------------------------------------- */
242 /* trucate xino files asynchronously */
244 int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex)
249 aufs_bindex_t bi, bend;
251 struct au_branch *br;
252 struct file *new_xino, *file;
253 struct super_block *h_sb;
254 struct au_xino_lock_dir ldir;
257 st = kzalloc(sizeof(*st), GFP_NOFS);
263 if (unlikely(bindex < 0 || bend < bindex))
265 br = au_sbr(sb, bindex);
266 file = br->br_xino.xi_file;
270 err = vfs_statfs(&file->f_path, st);
272 AuErr1("statfs err %d, ignored\n", err);
274 blocks = file->f_dentry->d_inode->i_blocks;
275 pr_info("begin truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
276 bindex, (u64)blocks, st->f_bfree, st->f_blocks);
278 au_xino_lock_dir(sb, file, &ldir);
279 /* mnt_want_write() is unnecessary here */
280 new_xino = au_xino_create2(file, file);
281 au_xino_unlock_dir(&ldir);
282 err = PTR_ERR(new_xino);
283 if (IS_ERR(new_xino)) {
284 pr_err("err %d, ignored\n", err);
289 br->br_xino.xi_file = new_xino;
292 for (bi = 0; bi <= bend; bi++) {
293 if (unlikely(bi == bindex))
296 if (au_br_sb(br) != h_sb)
299 fput(br->br_xino.xi_file);
300 br->br_xino.xi_file = new_xino;
304 err = vfs_statfs(&new_xino->f_path, st);
306 pr_info("end truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
307 bindex, (u64)new_xino->f_dentry->d_inode->i_blocks,
308 st->f_bfree, st->f_blocks);
309 if (new_xino->f_dentry->d_inode->i_blocks < blocks)
310 au_sbi(sb)->si_xino_jiffy = jiffy;
312 AuErr1("statfs err %d, ignored\n", err);
320 struct xino_do_trunc_args {
321 struct super_block *sb;
322 struct au_branch *br;
325 static void xino_do_trunc(void *_args)
327 struct xino_do_trunc_args *args = _args;
328 struct super_block *sb;
329 struct au_branch *br;
332 aufs_bindex_t bindex;
336 dir = sb->s_root->d_inode;
339 si_noflush_write_lock(sb);
340 ii_read_lock_parent(dir);
341 bindex = au_br_index(sb, br->br_id);
342 err = au_xino_trunc(sb, bindex);
345 pr_warn("err b%d, (%d)\n", bindex, err);
346 atomic_dec(&br->br_xino_running);
347 atomic_dec(&br->br_count);
349 au_nwt_done(&au_sbi(sb)->si_nowait);
353 static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
357 struct au_sbinfo *sbinfo;
359 /* todo: si_xino_expire and the ratio should be customizable */
361 if (time_before(jiffies,
362 sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
365 /* truncation border */
366 err = vfs_statfs(&br->br_xino.xi_file->f_path, &st);
368 AuErr1("statfs err %d, ignored\n", err);
371 if (div64_u64(st.f_bfree * 100, st.f_blocks) >= AUFS_XINO_DEF_TRUNC)
377 static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
379 struct xino_do_trunc_args *args;
382 if (!xino_trunc_test(sb, br))
385 if (atomic_inc_return(&br->br_xino_running) > 1)
388 /* lock and kfree() will be called in trunc_xino() */
389 args = kmalloc(sizeof(*args), GFP_NOFS);
390 if (unlikely(!args)) {
391 AuErr1("no memory\n");
395 atomic_inc(&br->br_count);
398 wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
400 return; /* success */
402 pr_err("wkq %d\n", wkq_err);
403 atomic_dec(&br->br_count);
408 atomic_dec(&br->br_xino_running);
411 /* ---------------------------------------------------------------------- */
413 static int au_xino_do_write(au_writef_t write, struct file *file,
414 ino_t h_ino, ino_t ino)
420 if (unlikely(au_loff_max / sizeof(ino) - 1 < pos)) {
421 AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
425 sz = xino_fwrite(write, file, &ino, sizeof(ino), &pos);
426 if (sz == sizeof(ino))
427 return 0; /* success */
429 AuIOErr("write failed (%zd)\n", sz);
434 * write @ino to the xinofile for the specified branch{@sb, @bindex}
435 * at the position of @h_ino.
436 * even if @ino is zero, it is written to the xinofile and means no entry.
437 * if the size of the xino file on a specific filesystem exceeds the watermark,
440 int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
444 unsigned int mnt_flags;
445 struct au_branch *br;
447 BUILD_BUG_ON(sizeof(long long) != sizeof(au_loff_max)
448 || ((loff_t)-1) > 0);
451 mnt_flags = au_mntflags(sb);
452 if (!au_opt_test(mnt_flags, XINO))
455 br = au_sbr(sb, bindex);
456 err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
459 if (au_opt_test(mnt_flags, TRUNC_XINO)
460 && au_test_fs_trunc_xino(au_br_sb(br)))
461 xino_try_trunc(sb, br);
462 return 0; /* success */
465 AuIOErr("write failed (%d)\n", err);
469 /* ---------------------------------------------------------------------- */
471 /* aufs inode number bitmap */
473 static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
474 static ino_t xib_calc_ino(unsigned long pindex, int bit)
478 AuDebugOn(bit < 0 || page_bits <= bit);
479 ino = AUFS_FIRST_INO + pindex * page_bits + bit;
483 static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
485 AuDebugOn(ino < AUFS_FIRST_INO);
486 ino -= AUFS_FIRST_INO;
487 *pindex = ino / page_bits;
488 *bit = ino % page_bits;
491 static int xib_pindex(struct super_block *sb, unsigned long pindex)
496 struct au_sbinfo *sbinfo;
501 MtxMustLock(&sbinfo->si_xib_mtx);
502 AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
503 || !au_opt_test(sbinfo->si_mntflags, XINO));
505 if (pindex == sbinfo->si_xib_last_pindex)
508 xib = sbinfo->si_xib;
509 p = sbinfo->si_xib_buf;
510 pos = sbinfo->si_xib_last_pindex;
512 sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
513 if (unlikely(sz != PAGE_SIZE))
518 if (i_size_read(xib->f_dentry->d_inode) >= pos + PAGE_SIZE)
519 sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
521 memset(p, 0, PAGE_SIZE);
522 sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
524 if (sz == PAGE_SIZE) {
525 sbinfo->si_xib_last_pindex = pindex;
526 return 0; /* success */
530 AuIOErr1("write failed (%zd)\n", sz);
537 /* ---------------------------------------------------------------------- */
539 static void au_xib_clear_bit(struct inode *inode)
542 unsigned long pindex;
543 struct super_block *sb;
544 struct au_sbinfo *sbinfo;
546 AuDebugOn(inode->i_nlink);
549 xib_calc_bit(inode->i_ino, &pindex, &bit);
550 AuDebugOn(page_bits <= bit);
552 mutex_lock(&sbinfo->si_xib_mtx);
553 err = xib_pindex(sb, pindex);
555 clear_bit(bit, sbinfo->si_xib_buf);
556 sbinfo->si_xib_next_bit = bit;
558 mutex_unlock(&sbinfo->si_xib_mtx);
561 /* for s_op->delete_inode() */
562 void au_xino_delete_inode(struct inode *inode, const int unlinked)
565 unsigned int mnt_flags;
566 aufs_bindex_t bindex, bend, bi;
567 unsigned char try_trunc;
568 struct au_iinfo *iinfo;
569 struct super_block *sb;
570 struct au_hinode *hi;
571 struct inode *h_inode;
572 struct au_branch *br;
576 mnt_flags = au_mntflags(sb);
577 if (!au_opt_test(mnt_flags, XINO)
578 || inode->i_ino == AUFS_ROOT_INO)
583 au_xib_clear_bit(inode);
586 iinfo = au_ii(inode);
590 bindex = iinfo->ii_bstart;
594 xwrite = au_sbi(sb)->si_xwrite;
595 try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
596 hi = iinfo->ii_hinode + bindex;
597 bend = iinfo->ii_bend;
598 for (; bindex <= bend; bindex++, hi++) {
599 h_inode = hi->hi_inode;
601 || (!unlinked && h_inode->i_nlink))
604 /* inode may not be revalidated */
605 bi = au_br_index(sb, hi->hi_id);
610 err = au_xino_do_write(xwrite, br->br_xino.xi_file,
611 h_inode->i_ino, /*ino*/0);
612 if (!err && try_trunc
613 && au_test_fs_trunc_xino(au_br_sb(br)))
614 xino_try_trunc(sb, br);
618 /* get an unused inode number from bitmap */
619 ino_t au_xino_new_ino(struct super_block *sb)
622 unsigned long *p, pindex, ul, pend;
623 struct au_sbinfo *sbinfo;
627 if (!au_opt_test(au_mntflags(sb), XINO))
628 return iunique(sb, AUFS_FIRST_INO);
631 mutex_lock(&sbinfo->si_xib_mtx);
632 p = sbinfo->si_xib_buf;
633 free_bit = sbinfo->si_xib_next_bit;
634 if (free_bit < page_bits && !test_bit(free_bit, p))
635 goto out; /* success */
636 free_bit = find_first_zero_bit(p, page_bits);
637 if (free_bit < page_bits)
638 goto out; /* success */
640 pindex = sbinfo->si_xib_last_pindex;
641 for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
642 err = xib_pindex(sb, ul);
645 free_bit = find_first_zero_bit(p, page_bits);
646 if (free_bit < page_bits)
647 goto out; /* success */
650 file = sbinfo->si_xib;
651 pend = i_size_read(file->f_dentry->d_inode) / PAGE_SIZE;
652 for (ul = pindex + 1; ul <= pend; ul++) {
653 err = xib_pindex(sb, ul);
656 free_bit = find_first_zero_bit(p, page_bits);
657 if (free_bit < page_bits)
658 goto out; /* success */
663 set_bit(free_bit, p);
664 sbinfo->si_xib_next_bit = free_bit + 1;
665 pindex = sbinfo->si_xib_last_pindex;
666 mutex_unlock(&sbinfo->si_xib_mtx);
667 ino = xib_calc_ino(pindex, free_bit);
668 AuDbg("i%lu\n", (unsigned long)ino);
671 mutex_unlock(&sbinfo->si_xib_mtx);
677 * read @ino from xinofile for the specified branch{@sb, @bindex}
678 * at the position of @h_ino.
679 * if @ino does not exist and @do_new is true, get new one.
681 int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
688 struct au_sbinfo *sbinfo;
691 if (!au_opt_test(au_mntflags(sb), XINO))
692 return 0; /* no xino */
697 if (unlikely(au_loff_max / sizeof(*ino) - 1 < pos)) {
698 AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
703 file = au_sbr(sb, bindex)->br_xino.xi_file;
704 if (i_size_read(file->f_dentry->d_inode) < pos + sizeof(*ino))
705 return 0; /* no ino */
707 sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &pos);
708 if (sz == sizeof(*ino))
709 return 0; /* success */
712 if (unlikely(sz >= 0)) {
714 AuIOErr("xino read error (%zd)\n", sz);
720 /* ---------------------------------------------------------------------- */
722 /* create and set a new xino file */
724 struct file *au_xino_create(struct super_block *sb, char *fname, int silent)
727 struct dentry *h_parent, *d;
732 * at mount-time, and the xino file is the default path,
733 * hnotify is disabled so we have no notify events to ignore.
734 * when a user specified the xino, we cannot get au_hdir to be ignored.
736 file = vfsub_filp_open(fname, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
737 /* | __FMODE_NONOTIFY */,
741 pr_err("open %s(%ld)\n", fname, PTR_ERR(file));
745 /* keep file count */
746 h_parent = dget_parent(file->f_dentry);
747 h_dir = h_parent->d_inode;
748 mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
749 /* mnt_want_write() is unnecessary here */
750 err = vfsub_unlink(h_dir, &file->f_path, /*force*/0);
751 mutex_unlock(&h_dir->i_mutex);
755 pr_err("unlink %s(%d)\n", fname, err);
761 if (unlikely(sb == d->d_sb)) {
763 pr_err("%s must be outside\n", fname);
766 if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
768 pr_err("xino doesn't support %s(%s)\n",
769 fname, au_sbtype(d->d_sb));
772 return file; /* success */
781 * find another branch who is on the same filesystem of the specified
782 * branch{@btgt}. search until @bend.
784 static int is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
787 aufs_bindex_t bindex;
788 struct super_block *tgt_sb = au_sbr_sb(sb, btgt);
790 for (bindex = 0; bindex < btgt; bindex++)
791 if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
793 for (bindex++; bindex <= bend; bindex++)
794 if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
799 /* ---------------------------------------------------------------------- */
802 * initialize the xinofile for the specified branch @br
803 * at the place/path where @base_file indicates.
804 * test whether another branch is on the same filesystem or not,
805 * if @do_test is true.
807 int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
808 struct file *base_file, int do_test)
812 aufs_bindex_t bend, bindex;
813 struct au_branch *shared_br, *b;
815 struct super_block *tgt_sb;
820 tgt_sb = au_br_sb(br);
821 for (bindex = 0; bindex <= bend; bindex++) {
822 b = au_sbr(sb, bindex);
823 if (tgt_sb == au_br_sb(b)) {
830 if (!shared_br || !shared_br->br_xino.xi_file) {
831 struct au_xino_lock_dir ldir;
833 au_xino_lock_dir(sb, base_file, &ldir);
834 /* mnt_want_write() is unnecessary here */
835 file = au_xino_create2(base_file, NULL);
836 au_xino_unlock_dir(&ldir);
840 br->br_xino.xi_file = file;
842 br->br_xino.xi_file = shared_br->br_xino.xi_file;
843 get_file(br->br_xino.xi_file);
847 err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
850 fput(br->br_xino.xi_file);
851 br->br_xino.xi_file = NULL;
858 /* ---------------------------------------------------------------------- */
860 /* trucate a xino bitmap file */
863 static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
867 unsigned long pindex;
869 struct au_sbinfo *sbinfo;
876 MtxMustLock(&sbinfo->si_xib_mtx);
877 p = sbinfo->si_xib_buf;
878 func = sbinfo->si_xread;
879 pend = i_size_read(file->f_dentry->d_inode);
882 sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
884 if (unlikely(sz <= 0))
888 for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
889 if (unlikely(*ino < AUFS_FIRST_INO))
892 xib_calc_bit(*ino, &pindex, &bit);
893 AuDebugOn(page_bits <= bit);
894 err = xib_pindex(sb, pindex);
906 static int xib_restore(struct super_block *sb)
909 aufs_bindex_t bindex, bend;
913 page = (void *)__get_free_page(GFP_NOFS);
919 for (bindex = 0; !err && bindex <= bend; bindex++)
920 if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0)
922 (sb, au_sbr(sb, bindex)->br_xino.xi_file, page);
924 AuDbg("b%d\n", bindex);
925 free_page((unsigned long)page);
931 int au_xib_trunc(struct super_block *sb)
936 struct au_xino_lock_dir ldir;
937 struct au_sbinfo *sbinfo;
945 if (!au_opt_test(sbinfo->si_mntflags, XINO))
948 file = sbinfo->si_xib;
949 if (i_size_read(file->f_dentry->d_inode) <= PAGE_SIZE)
952 au_xino_lock_dir(sb, file, &ldir);
953 /* mnt_want_write() is unnecessary here */
954 file = au_xino_create2(sbinfo->si_xib, NULL);
955 au_xino_unlock_dir(&ldir);
959 fput(sbinfo->si_xib);
960 sbinfo->si_xib = file;
962 p = sbinfo->si_xib_buf;
963 memset(p, 0, PAGE_SIZE);
965 sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
966 if (unlikely(sz != PAGE_SIZE)) {
968 AuIOErr("err %d\n", err);
974 mutex_lock(&sbinfo->si_xib_mtx);
975 /* mnt_want_write() is unnecessary here */
976 err = xib_restore(sb);
977 mutex_unlock(&sbinfo->si_xib_mtx);
983 /* ---------------------------------------------------------------------- */
986 * xino mount option handlers
988 static au_readf_t find_readf(struct file *h_file)
990 const struct file_operations *fop = h_file->f_op;
998 return ERR_PTR(-ENOSYS);
1001 static au_writef_t find_writef(struct file *h_file)
1003 const struct file_operations *fop = h_file->f_op;
1009 return do_sync_write;
1011 return ERR_PTR(-ENOSYS);
1015 static void xino_clear_xib(struct super_block *sb)
1017 struct au_sbinfo *sbinfo;
1019 SiMustWriteLock(sb);
1021 sbinfo = au_sbi(sb);
1022 sbinfo->si_xread = NULL;
1023 sbinfo->si_xwrite = NULL;
1025 fput(sbinfo->si_xib);
1026 sbinfo->si_xib = NULL;
1027 free_page((unsigned long)sbinfo->si_xib_buf);
1028 sbinfo->si_xib_buf = NULL;
1031 static int au_xino_set_xib(struct super_block *sb, struct file *base)
1035 struct au_sbinfo *sbinfo;
1038 SiMustWriteLock(sb);
1040 sbinfo = au_sbi(sb);
1041 file = au_xino_create2(base, sbinfo->si_xib);
1042 err = PTR_ERR(file);
1046 fput(sbinfo->si_xib);
1047 sbinfo->si_xib = file;
1048 sbinfo->si_xread = find_readf(file);
1049 sbinfo->si_xwrite = find_writef(file);
1052 if (!sbinfo->si_xib_buf)
1053 sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
1054 if (unlikely(!sbinfo->si_xib_buf))
1057 sbinfo->si_xib_last_pindex = 0;
1058 sbinfo->si_xib_next_bit = 0;
1059 if (i_size_read(file->f_dentry->d_inode) < PAGE_SIZE) {
1061 err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
1063 if (unlikely(err != PAGE_SIZE))
1067 goto out; /* success */
1070 free_page((unsigned long)sbinfo->si_xib_buf);
1071 sbinfo->si_xib_buf = NULL;
1075 fput(sbinfo->si_xib);
1076 sbinfo->si_xib = NULL;
1077 sbinfo->si_xread = NULL;
1078 sbinfo->si_xwrite = NULL;
1083 /* xino for each branch */
1084 static void xino_clear_br(struct super_block *sb)
1086 aufs_bindex_t bindex, bend;
1087 struct au_branch *br;
1089 bend = au_sbend(sb);
1090 for (bindex = 0; bindex <= bend; bindex++) {
1091 br = au_sbr(sb, bindex);
1092 if (!br || !br->br_xino.xi_file)
1095 fput(br->br_xino.xi_file);
1096 br->br_xino.xi_file = NULL;
1100 static int au_xino_set_br(struct super_block *sb, struct file *base)
1104 aufs_bindex_t bindex, bend, bshared;
1106 struct file *old, *new;
1108 struct au_branch *br;
1109 struct inode *inode;
1112 SiMustWriteLock(sb);
1115 bend = au_sbend(sb);
1116 fpair = kcalloc(bend + 1, sizeof(*fpair), GFP_NOFS);
1117 if (unlikely(!fpair))
1120 inode = sb->s_root->d_inode;
1121 ino = AUFS_ROOT_INO;
1122 writef = au_sbi(sb)->si_xwrite;
1123 for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++) {
1124 br = au_sbr(sb, bindex);
1125 bshared = is_sb_shared(sb, bindex, bindex - 1);
1128 *p = fpair[bshared];
1134 p->old = br->br_xino.xi_file;
1135 p->new = au_xino_create2(base, br->br_xino.xi_file);
1136 err = PTR_ERR(p->new);
1137 if (IS_ERR(p->new)) {
1143 err = au_xino_do_write(writef, p->new,
1144 au_h_iptr(inode, bindex)->i_ino, ino);
1149 for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++) {
1150 br = au_sbr(sb, bindex);
1151 if (br->br_xino.xi_file)
1152 fput(br->br_xino.xi_file);
1154 br->br_xino.xi_file = p->new;
1158 for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++)
1168 void au_xino_clr(struct super_block *sb)
1170 struct au_sbinfo *sbinfo;
1175 sbinfo = au_sbi(sb);
1176 /* lvalue, do not call au_mntflags() */
1177 au_opt_clr(sbinfo->si_mntflags, XINO);
1180 int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount)
1183 struct dentry *parent, *cur_parent;
1184 struct qstr *dname, *cur_name;
1185 struct file *cur_xino;
1187 struct au_sbinfo *sbinfo;
1189 SiMustWriteLock(sb);
1192 sbinfo = au_sbi(sb);
1193 parent = dget_parent(xino->file->f_dentry);
1196 dname = &xino->file->f_dentry->d_name;
1197 cur_xino = sbinfo->si_xib;
1199 cur_parent = dget_parent(cur_xino->f_dentry);
1200 cur_name = &cur_xino->f_dentry->d_name;
1201 skip = (cur_parent == parent
1202 && dname->len == cur_name->len
1203 && !memcmp(dname->name, cur_name->name,
1211 au_opt_set(sbinfo->si_mntflags, XINO);
1212 dir = parent->d_inode;
1213 mutex_lock_nested(&dir->i_mutex, AuLsc_I_PARENT);
1214 /* mnt_want_write() is unnecessary here */
1215 err = au_xino_set_xib(sb, xino->file);
1217 err = au_xigen_set(sb, xino->file);
1219 err = au_xino_set_br(sb, xino->file);
1220 mutex_unlock(&dir->i_mutex);
1222 goto out; /* success */
1225 AuIOErr("failed creating xino(%d).\n", err);
1232 /* ---------------------------------------------------------------------- */
1235 * create a xinofile at the default place/path.
1237 struct file *au_xino_def(struct super_block *sb)
1241 struct au_branch *br;
1242 struct super_block *h_sb;
1244 aufs_bindex_t bend, bindex, bwr;
1247 bend = au_sbend(sb);
1249 for (bindex = 0; bindex <= bend; bindex++) {
1250 br = au_sbr(sb, bindex);
1251 if (au_br_writable(br->br_perm)
1252 && !au_test_fs_bad_xino(au_br_sb(br))) {
1259 file = ERR_PTR(-ENOMEM);
1260 page = __getname_gfp(GFP_NOFS);
1261 if (unlikely(!page))
1263 path.mnt = au_br_mnt(br);
1264 path.dentry = au_h_dptr(sb->s_root, bwr);
1265 p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
1268 strcat(p, "/" AUFS_XINO_FNAME);
1270 file = au_xino_create(sb, p, /*silent*/0);
1272 au_xino_brid_set(sb, br->br_id);
1276 file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0);
1279 h_sb = file->f_dentry->d_sb;
1280 if (unlikely(au_test_fs_bad_xino(h_sb))) {
1281 pr_err("xino doesn't support %s(%s)\n",
1282 AUFS_XINO_DEFPATH, au_sbtype(h_sb));
1284 file = ERR_PTR(-EINVAL);
1287 au_xino_brid_set(sb, -1);
1294 /* ---------------------------------------------------------------------- */
1296 int au_xino_path(struct seq_file *seq, struct file *file)
1300 err = au_seq_path(seq, &file->f_path);
1301 if (unlikely(err < 0))
1305 #define Deleted "\\040(deleted)"
1306 seq->count -= sizeof(Deleted) - 1;
1307 AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
1308 sizeof(Deleted) - 1));