2 * Copyright (C) 2005-2012 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>
26 /* todo: unnecessary to support mmap_sem since kernel-space? */
27 ssize_t xino_fread(au_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_dentry);
54 /* ---------------------------------------------------------------------- */
56 static ssize_t do_xino_fwrite(au_writef_t func, struct file *file, void *kbuf,
57 size_t size, loff_t *pos)
70 /* todo: signal_pending? */
71 err = func(file, buf.u, size, pos);
72 } while (err == -EAGAIN || err == -EINTR);
75 #if 0 /* reserved for future use */
77 fsnotify_modify(file->f_dentry);
83 struct do_xino_fwrite_args {
92 static void call_do_xino_fwrite(void *args)
94 struct do_xino_fwrite_args *a = args;
95 *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
98 ssize_t xino_fwrite(au_writef_t func, struct file *file, void *buf, size_t size,
103 /* todo: signal block and no wkq? */
104 if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
106 err = do_xino_fwrite(func, file, buf, size, pos);
110 * it breaks RLIMIT_FSIZE and normal user's limit,
111 * users should care about quota and real 'filesystem full.'
114 struct do_xino_fwrite_args args = {
123 wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
124 if (unlikely(wkq_err))
131 /* ---------------------------------------------------------------------- */
134 * create a new xinofile at the same place/path as @base_file.
136 struct file *au_xino_create2(struct file *base_file, struct file *copy_src)
139 struct dentry *base, *parent;
145 base = base_file->f_dentry;
146 parent = base->d_parent; /* dir inode is locked */
147 dir = parent->d_inode;
150 file = ERR_PTR(-EINVAL);
151 name = &base->d_name;
152 path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
153 if (IS_ERR(path.dentry)) {
154 file = (void *)path.dentry;
155 pr_err("%.*s lookup err %ld\n",
156 AuLNPair(name), PTR_ERR(path.dentry));
160 /* no need to mnt_want_write() since we call dentry_open() later */
161 err = vfs_create(dir, path.dentry, S_IRUGO | S_IWUGO, NULL);
164 pr_err("%.*s create err %d\n", AuLNPair(name), err);
168 path.mnt = base_file->f_vfsmnt;
169 file = vfsub_dentry_open(&path,
170 O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
171 /* | __FMODE_NONOTIFY */);
173 pr_err("%.*s open err %ld\n", AuLNPair(name), PTR_ERR(file));
177 err = vfsub_unlink(dir, &file->f_path, /*force*/0);
179 pr_err("%.*s unlink err %d\n", AuLNPair(name), err);
184 /* no one can touch copy_src xino */
185 err = au_copy_file(file, copy_src,
186 i_size_read(copy_src->f_dentry->d_inode));
188 pr_err("%.*s copy err %d\n", AuLNPair(name), err);
192 goto out_dput; /* success */
203 struct au_xino_lock_dir {
204 struct au_hinode *hdir;
205 struct dentry *parent;
209 static void au_xino_lock_dir(struct super_block *sb, struct file *xino,
210 struct au_xino_lock_dir *ldir)
212 aufs_bindex_t brid, bindex;
216 brid = au_xino_brid(sb);
218 bindex = au_br_index(sb, brid);
220 ldir->hdir = au_hi(sb->s_root->d_inode, bindex);
221 au_hn_imtx_lock_nested(ldir->hdir, AuLsc_I_PARENT);
223 ldir->parent = dget_parent(xino->f_dentry);
224 ldir->mtx = &ldir->parent->d_inode->i_mutex;
225 mutex_lock_nested(ldir->mtx, AuLsc_I_PARENT);
229 static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
232 au_hn_imtx_unlock(ldir->hdir);
234 mutex_unlock(ldir->mtx);
239 /* ---------------------------------------------------------------------- */
241 /* trucate xino files asynchronously */
243 int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex)
246 aufs_bindex_t bi, bend;
247 struct au_branch *br;
248 struct file *new_xino, *file;
249 struct super_block *h_sb;
250 struct au_xino_lock_dir ldir;
254 if (unlikely(bindex < 0 || bend < bindex))
256 br = au_sbr(sb, bindex);
257 file = br->br_xino.xi_file;
261 au_xino_lock_dir(sb, file, &ldir);
262 /* mnt_want_write() is unnecessary here */
263 new_xino = au_xino_create2(file, file);
264 au_xino_unlock_dir(&ldir);
265 err = PTR_ERR(new_xino);
266 if (IS_ERR(new_xino))
270 br->br_xino.xi_file = new_xino;
272 h_sb = br->br_mnt->mnt_sb;
273 for (bi = 0; bi <= bend; bi++) {
274 if (unlikely(bi == bindex))
277 if (br->br_mnt->mnt_sb != h_sb)
280 fput(br->br_xino.xi_file);
281 br->br_xino.xi_file = new_xino;
289 struct xino_do_trunc_args {
290 struct super_block *sb;
291 struct au_branch *br;
294 static void xino_do_trunc(void *_args)
296 struct xino_do_trunc_args *args = _args;
297 struct super_block *sb;
298 struct au_branch *br;
301 aufs_bindex_t bindex;
305 dir = sb->s_root->d_inode;
308 si_noflush_write_lock(sb);
309 ii_read_lock_parent(dir);
310 bindex = au_br_index(sb, br->br_id);
311 err = au_xino_trunc(sb, bindex);
313 && br->br_xino.xi_file->f_dentry->d_inode->i_blocks
314 >= br->br_xino_upper)
315 br->br_xino_upper += AUFS_XINO_TRUNC_STEP;
319 pr_warning("err b%d, (%d)\n", bindex, err);
320 atomic_dec(&br->br_xino_running);
321 atomic_dec(&br->br_count);
323 au_nwt_done(&au_sbi(sb)->si_nowait);
327 static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
329 struct xino_do_trunc_args *args;
332 if (br->br_xino.xi_file->f_dentry->d_inode->i_blocks
336 if (atomic_inc_return(&br->br_xino_running) > 1)
339 /* lock and kfree() will be called in trunc_xino() */
340 args = kmalloc(sizeof(*args), GFP_NOFS);
341 if (unlikely(!args)) {
342 AuErr1("no memory\n");
346 atomic_inc(&br->br_count);
349 wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
351 return; /* success */
353 pr_err("wkq %d\n", wkq_err);
354 atomic_dec(&br->br_count);
359 atomic_dec(&br->br_xino_running);
362 /* ---------------------------------------------------------------------- */
364 static int au_xino_do_write(au_writef_t write, struct file *file,
365 ino_t h_ino, ino_t ino)
371 if (unlikely(au_loff_max / sizeof(ino) - 1 < pos)) {
372 AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
376 sz = xino_fwrite(write, file, &ino, sizeof(ino), &pos);
377 if (sz == sizeof(ino))
378 return 0; /* success */
380 AuIOErr("write failed (%zd)\n", sz);
385 * write @ino to the xinofile for the specified branch{@sb, @bindex}
386 * at the position of @h_ino.
387 * even if @ino is zero, it is written to the xinofile and means no entry.
388 * if the size of the xino file on a specific filesystem exceeds the watermark,
391 int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
395 unsigned int mnt_flags;
396 struct au_branch *br;
398 BUILD_BUG_ON(sizeof(long long) != sizeof(au_loff_max)
399 || ((loff_t)-1) > 0);
402 mnt_flags = au_mntflags(sb);
403 if (!au_opt_test(mnt_flags, XINO))
406 br = au_sbr(sb, bindex);
407 err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
410 if (au_opt_test(mnt_flags, TRUNC_XINO)
411 && au_test_fs_trunc_xino(br->br_mnt->mnt_sb))
412 xino_try_trunc(sb, br);
413 return 0; /* success */
416 AuIOErr("write failed (%d)\n", err);
420 /* ---------------------------------------------------------------------- */
422 /* aufs inode number bitmap */
424 static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
425 static ino_t xib_calc_ino(unsigned long pindex, int bit)
429 AuDebugOn(bit < 0 || page_bits <= bit);
430 ino = AUFS_FIRST_INO + pindex * page_bits + bit;
434 static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
436 AuDebugOn(ino < AUFS_FIRST_INO);
437 ino -= AUFS_FIRST_INO;
438 *pindex = ino / page_bits;
439 *bit = ino % page_bits;
442 static int xib_pindex(struct super_block *sb, unsigned long pindex)
447 struct au_sbinfo *sbinfo;
452 MtxMustLock(&sbinfo->si_xib_mtx);
453 AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
454 || !au_opt_test(sbinfo->si_mntflags, XINO));
456 if (pindex == sbinfo->si_xib_last_pindex)
459 xib = sbinfo->si_xib;
460 p = sbinfo->si_xib_buf;
461 pos = sbinfo->si_xib_last_pindex;
463 sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
464 if (unlikely(sz != PAGE_SIZE))
469 if (i_size_read(xib->f_dentry->d_inode) >= pos + PAGE_SIZE)
470 sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
472 memset(p, 0, PAGE_SIZE);
473 sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
475 if (sz == PAGE_SIZE) {
476 sbinfo->si_xib_last_pindex = pindex;
477 return 0; /* success */
481 AuIOErr1("write failed (%zd)\n", sz);
488 /* ---------------------------------------------------------------------- */
490 static void au_xib_clear_bit(struct inode *inode)
493 unsigned long pindex;
494 struct super_block *sb;
495 struct au_sbinfo *sbinfo;
497 AuDebugOn(inode->i_nlink);
500 xib_calc_bit(inode->i_ino, &pindex, &bit);
501 AuDebugOn(page_bits <= bit);
503 mutex_lock(&sbinfo->si_xib_mtx);
504 err = xib_pindex(sb, pindex);
506 clear_bit(bit, sbinfo->si_xib_buf);
507 sbinfo->si_xib_next_bit = bit;
509 mutex_unlock(&sbinfo->si_xib_mtx);
512 /* for s_op->delete_inode() */
513 void au_xino_delete_inode(struct inode *inode, const int unlinked)
516 unsigned int mnt_flags;
517 aufs_bindex_t bindex, bend, bi;
518 unsigned char try_trunc;
519 struct au_iinfo *iinfo;
520 struct super_block *sb;
521 struct au_hinode *hi;
522 struct inode *h_inode;
523 struct au_branch *br;
527 mnt_flags = au_mntflags(sb);
528 if (!au_opt_test(mnt_flags, XINO)
529 || inode->i_ino == AUFS_ROOT_INO)
534 au_xib_clear_bit(inode);
537 iinfo = au_ii(inode);
541 bindex = iinfo->ii_bstart;
545 xwrite = au_sbi(sb)->si_xwrite;
546 try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
547 hi = iinfo->ii_hinode + bindex;
548 bend = iinfo->ii_bend;
549 for (; bindex <= bend; bindex++, hi++) {
550 h_inode = hi->hi_inode;
552 || (!unlinked && h_inode->i_nlink))
555 /* inode may not be revalidated */
556 bi = au_br_index(sb, hi->hi_id);
561 err = au_xino_do_write(xwrite, br->br_xino.xi_file,
562 h_inode->i_ino, /*ino*/0);
563 if (!err && try_trunc
564 && au_test_fs_trunc_xino(br->br_mnt->mnt_sb))
565 xino_try_trunc(sb, br);
569 /* get an unused inode number from bitmap */
570 ino_t au_xino_new_ino(struct super_block *sb)
573 unsigned long *p, pindex, ul, pend;
574 struct au_sbinfo *sbinfo;
578 if (!au_opt_test(au_mntflags(sb), XINO))
579 return iunique(sb, AUFS_FIRST_INO);
582 mutex_lock(&sbinfo->si_xib_mtx);
583 p = sbinfo->si_xib_buf;
584 free_bit = sbinfo->si_xib_next_bit;
585 if (free_bit < page_bits && !test_bit(free_bit, p))
586 goto out; /* success */
587 free_bit = find_first_zero_bit(p, page_bits);
588 if (free_bit < page_bits)
589 goto out; /* success */
591 pindex = sbinfo->si_xib_last_pindex;
592 for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
593 err = xib_pindex(sb, ul);
596 free_bit = find_first_zero_bit(p, page_bits);
597 if (free_bit < page_bits)
598 goto out; /* success */
601 file = sbinfo->si_xib;
602 pend = i_size_read(file->f_dentry->d_inode) / PAGE_SIZE;
603 for (ul = pindex + 1; ul <= pend; ul++) {
604 err = xib_pindex(sb, ul);
607 free_bit = find_first_zero_bit(p, page_bits);
608 if (free_bit < page_bits)
609 goto out; /* success */
614 set_bit(free_bit, p);
615 sbinfo->si_xib_next_bit = free_bit + 1;
616 pindex = sbinfo->si_xib_last_pindex;
617 mutex_unlock(&sbinfo->si_xib_mtx);
618 ino = xib_calc_ino(pindex, free_bit);
619 AuDbg("i%lu\n", (unsigned long)ino);
622 mutex_unlock(&sbinfo->si_xib_mtx);
628 * read @ino from xinofile for the specified branch{@sb, @bindex}
629 * at the position of @h_ino.
630 * if @ino does not exist and @do_new is true, get new one.
632 int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
639 struct au_sbinfo *sbinfo;
642 if (!au_opt_test(au_mntflags(sb), XINO))
643 return 0; /* no xino */
648 if (unlikely(au_loff_max / sizeof(*ino) - 1 < pos)) {
649 AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
654 file = au_sbr(sb, bindex)->br_xino.xi_file;
655 if (i_size_read(file->f_dentry->d_inode) < pos + sizeof(*ino))
656 return 0; /* no ino */
658 sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &pos);
659 if (sz == sizeof(*ino))
660 return 0; /* success */
663 if (unlikely(sz >= 0)) {
665 AuIOErr("xino read error (%zd)\n", sz);
671 /* ---------------------------------------------------------------------- */
673 /* create and set a new xino file */
675 struct file *au_xino_create(struct super_block *sb, char *fname, int silent)
678 struct dentry *h_parent, *d;
683 * at mount-time, and the xino file is the default path,
684 * hnotify is disabled so we have no notify events to ignore.
685 * when a user specified the xino, we cannot get au_hdir to be ignored.
687 file = vfsub_filp_open(fname, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
688 /* | __FMODE_NONOTIFY */,
692 pr_err("open %s(%ld)\n", fname, PTR_ERR(file));
696 /* keep file count */
697 h_parent = dget_parent(file->f_dentry);
698 h_dir = h_parent->d_inode;
699 mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
700 /* mnt_want_write() is unnecessary here */
701 err = vfsub_unlink(h_dir, &file->f_path, /*force*/0);
702 mutex_unlock(&h_dir->i_mutex);
706 pr_err("unlink %s(%d)\n", fname, err);
712 if (unlikely(sb == d->d_sb)) {
714 pr_err("%s must be outside\n", fname);
717 if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
719 pr_err("xino doesn't support %s(%s)\n",
720 fname, au_sbtype(d->d_sb));
723 return file; /* success */
732 * find another branch who is on the same filesystem of the specified
733 * branch{@btgt}. search until @bend.
735 static int is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
738 aufs_bindex_t bindex;
739 struct super_block *tgt_sb = au_sbr_sb(sb, btgt);
741 for (bindex = 0; bindex < btgt; bindex++)
742 if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
744 for (bindex++; bindex <= bend; bindex++)
745 if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
750 /* ---------------------------------------------------------------------- */
753 * initialize the xinofile for the specified branch @br
754 * at the place/path where @base_file indicates.
755 * test whether another branch is on the same filesystem or not,
756 * if @do_test is true.
758 int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
759 struct file *base_file, int do_test)
763 aufs_bindex_t bend, bindex;
764 struct au_branch *shared_br, *b;
766 struct super_block *tgt_sb;
771 tgt_sb = br->br_mnt->mnt_sb;
772 for (bindex = 0; bindex <= bend; bindex++) {
773 b = au_sbr(sb, bindex);
774 if (tgt_sb == b->br_mnt->mnt_sb) {
781 if (!shared_br || !shared_br->br_xino.xi_file) {
782 struct au_xino_lock_dir ldir;
784 au_xino_lock_dir(sb, base_file, &ldir);
785 /* mnt_want_write() is unnecessary here */
786 file = au_xino_create2(base_file, NULL);
787 au_xino_unlock_dir(&ldir);
791 br->br_xino.xi_file = file;
793 br->br_xino.xi_file = shared_br->br_xino.xi_file;
794 get_file(br->br_xino.xi_file);
798 err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
801 fput(br->br_xino.xi_file);
802 br->br_xino.xi_file = NULL;
809 /* ---------------------------------------------------------------------- */
811 /* trucate a xino bitmap file */
814 static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
818 unsigned long pindex;
820 struct au_sbinfo *sbinfo;
827 MtxMustLock(&sbinfo->si_xib_mtx);
828 p = sbinfo->si_xib_buf;
829 func = sbinfo->si_xread;
830 pend = i_size_read(file->f_dentry->d_inode);
833 sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
835 if (unlikely(sz <= 0))
839 for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
840 if (unlikely(*ino < AUFS_FIRST_INO))
843 xib_calc_bit(*ino, &pindex, &bit);
844 AuDebugOn(page_bits <= bit);
845 err = xib_pindex(sb, pindex);
857 static int xib_restore(struct super_block *sb)
860 aufs_bindex_t bindex, bend;
864 page = (void *)__get_free_page(GFP_NOFS);
870 for (bindex = 0; !err && bindex <= bend; bindex++)
871 if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0)
873 (sb, au_sbr(sb, bindex)->br_xino.xi_file, page);
875 AuDbg("b%d\n", bindex);
876 free_page((unsigned long)page);
882 int au_xib_trunc(struct super_block *sb)
887 struct au_xino_lock_dir ldir;
888 struct au_sbinfo *sbinfo;
896 if (!au_opt_test(sbinfo->si_mntflags, XINO))
899 file = sbinfo->si_xib;
900 if (i_size_read(file->f_dentry->d_inode) <= PAGE_SIZE)
903 au_xino_lock_dir(sb, file, &ldir);
904 /* mnt_want_write() is unnecessary here */
905 file = au_xino_create2(sbinfo->si_xib, NULL);
906 au_xino_unlock_dir(&ldir);
910 fput(sbinfo->si_xib);
911 sbinfo->si_xib = file;
913 p = sbinfo->si_xib_buf;
914 memset(p, 0, PAGE_SIZE);
916 sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
917 if (unlikely(sz != PAGE_SIZE)) {
919 AuIOErr("err %d\n", err);
925 mutex_lock(&sbinfo->si_xib_mtx);
926 /* mnt_want_write() is unnecessary here */
927 err = xib_restore(sb);
928 mutex_unlock(&sbinfo->si_xib_mtx);
934 /* ---------------------------------------------------------------------- */
937 * xino mount option handlers
939 static au_readf_t find_readf(struct file *h_file)
941 const struct file_operations *fop = h_file->f_op;
949 return ERR_PTR(-ENOSYS);
952 static au_writef_t find_writef(struct file *h_file)
954 const struct file_operations *fop = h_file->f_op;
960 return do_sync_write;
962 return ERR_PTR(-ENOSYS);
966 static void xino_clear_xib(struct super_block *sb)
968 struct au_sbinfo *sbinfo;
973 sbinfo->si_xread = NULL;
974 sbinfo->si_xwrite = NULL;
976 fput(sbinfo->si_xib);
977 sbinfo->si_xib = NULL;
978 free_page((unsigned long)sbinfo->si_xib_buf);
979 sbinfo->si_xib_buf = NULL;
982 static int au_xino_set_xib(struct super_block *sb, struct file *base)
986 struct au_sbinfo *sbinfo;
992 file = au_xino_create2(base, sbinfo->si_xib);
997 fput(sbinfo->si_xib);
998 sbinfo->si_xib = file;
999 sbinfo->si_xread = find_readf(file);
1000 sbinfo->si_xwrite = find_writef(file);
1003 if (!sbinfo->si_xib_buf)
1004 sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
1005 if (unlikely(!sbinfo->si_xib_buf))
1008 sbinfo->si_xib_last_pindex = 0;
1009 sbinfo->si_xib_next_bit = 0;
1010 if (i_size_read(file->f_dentry->d_inode) < PAGE_SIZE) {
1012 err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
1014 if (unlikely(err != PAGE_SIZE))
1018 goto out; /* success */
1021 free_page((unsigned long)sbinfo->si_xib_buf);
1022 sbinfo->si_xib_buf = NULL;
1026 fput(sbinfo->si_xib);
1027 sbinfo->si_xib = NULL;
1028 sbinfo->si_xread = NULL;
1029 sbinfo->si_xwrite = NULL;
1034 /* xino for each branch */
1035 static void xino_clear_br(struct super_block *sb)
1037 aufs_bindex_t bindex, bend;
1038 struct au_branch *br;
1040 bend = au_sbend(sb);
1041 for (bindex = 0; bindex <= bend; bindex++) {
1042 br = au_sbr(sb, bindex);
1043 if (!br || !br->br_xino.xi_file)
1046 fput(br->br_xino.xi_file);
1047 br->br_xino.xi_file = NULL;
1051 static int au_xino_set_br(struct super_block *sb, struct file *base)
1055 aufs_bindex_t bindex, bend, bshared;
1057 struct file *old, *new;
1059 struct au_branch *br;
1060 struct inode *inode;
1063 SiMustWriteLock(sb);
1066 bend = au_sbend(sb);
1067 fpair = kcalloc(bend + 1, sizeof(*fpair), GFP_NOFS);
1068 if (unlikely(!fpair))
1071 inode = sb->s_root->d_inode;
1072 ino = AUFS_ROOT_INO;
1073 writef = au_sbi(sb)->si_xwrite;
1074 for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++) {
1075 br = au_sbr(sb, bindex);
1076 bshared = is_sb_shared(sb, bindex, bindex - 1);
1079 *p = fpair[bshared];
1085 p->old = br->br_xino.xi_file;
1086 p->new = au_xino_create2(base, br->br_xino.xi_file);
1087 err = PTR_ERR(p->new);
1088 if (IS_ERR(p->new)) {
1094 err = au_xino_do_write(writef, p->new,
1095 au_h_iptr(inode, bindex)->i_ino, ino);
1100 for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++) {
1101 br = au_sbr(sb, bindex);
1102 if (br->br_xino.xi_file)
1103 fput(br->br_xino.xi_file);
1105 br->br_xino.xi_file = p->new;
1109 for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++)
1119 void au_xino_clr(struct super_block *sb)
1121 struct au_sbinfo *sbinfo;
1126 sbinfo = au_sbi(sb);
1127 /* lvalue, do not call au_mntflags() */
1128 au_opt_clr(sbinfo->si_mntflags, XINO);
1131 int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount)
1134 struct dentry *parent, *cur_parent;
1135 struct qstr *dname, *cur_name;
1136 struct file *cur_xino;
1138 struct au_sbinfo *sbinfo;
1140 SiMustWriteLock(sb);
1143 sbinfo = au_sbi(sb);
1144 parent = dget_parent(xino->file->f_dentry);
1147 dname = &xino->file->f_dentry->d_name;
1148 cur_xino = sbinfo->si_xib;
1150 cur_parent = dget_parent(cur_xino->f_dentry);
1151 cur_name = &cur_xino->f_dentry->d_name;
1152 skip = (cur_parent == parent
1153 && dname->len == cur_name->len
1154 && !memcmp(dname->name, cur_name->name,
1162 au_opt_set(sbinfo->si_mntflags, XINO);
1163 dir = parent->d_inode;
1164 mutex_lock_nested(&dir->i_mutex, AuLsc_I_PARENT);
1165 /* mnt_want_write() is unnecessary here */
1166 err = au_xino_set_xib(sb, xino->file);
1168 err = au_xigen_set(sb, xino->file);
1170 err = au_xino_set_br(sb, xino->file);
1171 mutex_unlock(&dir->i_mutex);
1173 goto out; /* success */
1176 AuIOErr("failed creating xino(%d).\n", err);
1183 /* ---------------------------------------------------------------------- */
1186 * create a xinofile at the default place/path.
1188 struct file *au_xino_def(struct super_block *sb)
1192 struct au_branch *br;
1193 struct super_block *h_sb;
1195 aufs_bindex_t bend, bindex, bwr;
1198 bend = au_sbend(sb);
1200 for (bindex = 0; bindex <= bend; bindex++) {
1201 br = au_sbr(sb, bindex);
1202 if (au_br_writable(br->br_perm)
1203 && !au_test_fs_bad_xino(br->br_mnt->mnt_sb)) {
1210 file = ERR_PTR(-ENOMEM);
1211 page = __getname_gfp(GFP_NOFS);
1212 if (unlikely(!page))
1214 path.mnt = br->br_mnt;
1215 path.dentry = au_h_dptr(sb->s_root, bwr);
1216 p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
1219 strcat(p, "/" AUFS_XINO_FNAME);
1221 file = au_xino_create(sb, p, /*silent*/0);
1223 au_xino_brid_set(sb, br->br_id);
1227 file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0);
1230 h_sb = file->f_dentry->d_sb;
1231 if (unlikely(au_test_fs_bad_xino(h_sb))) {
1232 pr_err("xino doesn't support %s(%s)\n",
1233 AUFS_XINO_DEFPATH, au_sbtype(h_sb));
1235 file = ERR_PTR(-EINVAL);
1238 au_xino_brid_set(sb, -1);
1245 /* ---------------------------------------------------------------------- */
1247 int au_xino_path(struct seq_file *seq, struct file *file)
1251 err = au_seq_path(seq, &file->f_path);
1252 if (unlikely(err < 0))
1256 #define Deleted "\\040(deleted)"
1257 seq->count -= sizeof(Deleted) - 1;
1258 AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
1259 sizeof(Deleted) - 1));