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
23 #include <linux/exportfs.h>
24 #include <linux/mnt_namespace.h>
25 #include <linux/namei.h>
26 #include <linux/nsproxy.h>
27 #include <linux/random.h>
28 #include <linux/writeback.h>
32 #ifdef CONFIG_AUFS_INO_T_64
40 static ino_t decode_ino(__u32 *a)
44 BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
46 #ifdef CONFIG_AUFS_INO_T_64
52 static void encode_ino(__u32 *a, ino_t ino)
58 #ifdef CONFIG_AUFS_INO_T_64
67 #ifdef CONFIG_AUFS_INO_T_64
68 /* support 64bit inode number */
82 Fh_dir_ino = Fh_dir_ino1
85 static int au_test_anon(struct dentry *dentry)
87 /* note: read d_flags without d_lock */
88 return !!(dentry->d_flags & DCACHE_DISCONNECTED);
91 /* ---------------------------------------------------------------------- */
92 /* inode generation external table */
94 void au_xigen_inc(struct inode *inode)
99 struct super_block *sb;
100 struct au_sbinfo *sbinfo;
103 AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
108 igen = inode->i_generation + 1;
109 sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
111 if (sz == sizeof(igen))
112 return; /* success */
114 if (unlikely(sz >= 0))
115 AuIOErr("xigen error (%zd)\n", sz);
118 int au_xigen_new(struct inode *inode)
123 struct super_block *sb;
124 struct au_sbinfo *sbinfo;
128 /* todo: dirty, at mount time */
129 if (inode->i_ino == AUFS_ROOT_INO)
133 if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
138 if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
139 AuIOErr1("too large i%lld\n", pos);
142 pos *= sizeof(inode->i_generation);
146 file = sbinfo->si_xigen;
149 if (i_size_read(file->f_dentry->d_inode)
150 < pos + sizeof(inode->i_generation)) {
151 inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
152 sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
153 sizeof(inode->i_generation), &pos);
155 sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
156 sizeof(inode->i_generation), &pos);
157 if (sz == sizeof(inode->i_generation))
158 goto out; /* success */
161 if (unlikely(sz >= 0)) {
163 AuIOErr("xigen error (%zd)\n", sz);
170 int au_xigen_set(struct super_block *sb, struct file *base)
173 struct au_sbinfo *sbinfo;
179 file = au_xino_create2(base, sbinfo->si_xigen);
184 if (sbinfo->si_xigen)
185 fput(sbinfo->si_xigen);
186 sbinfo->si_xigen = file;
192 void au_xigen_clr(struct super_block *sb)
194 struct au_sbinfo *sbinfo;
199 if (sbinfo->si_xigen) {
200 fput(sbinfo->si_xigen);
201 sbinfo->si_xigen = NULL;
205 /* ---------------------------------------------------------------------- */
207 static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
210 struct dentry *dentry, *d;
215 inode = ilookup(sb, ino);
219 dentry = ERR_PTR(-ESTALE);
220 sigen = au_sigen(sb);
221 if (unlikely(is_bad_inode(inode)
223 || sigen != au_iigen(inode)))
227 if (!dir_ino || S_ISDIR(inode->i_mode))
228 dentry = d_find_alias(inode);
230 spin_lock(&inode->i_lock);
231 list_for_each_entry(d, &inode->i_dentry, d_alias) {
232 spin_lock(&d->d_lock);
234 && d->d_parent->d_inode->i_ino == dir_ino) {
235 dentry = dget_dlock(d);
236 spin_unlock(&d->d_lock);
239 spin_unlock(&d->d_lock);
241 spin_unlock(&inode->i_lock);
243 if (unlikely(dentry && au_digen_test(dentry, sigen))) {
244 /* need to refresh */
252 AuTraceErrPtr(dentry);
256 /* ---------------------------------------------------------------------- */
259 /* if exportfs_decode_fh() passed vfsmount*, we could be happy */
261 struct au_compare_mnt_args {
263 struct super_block *sb;
266 struct vfsmount *mnt;
269 static int au_compare_mnt(struct vfsmount *mnt, void *arg)
271 struct au_compare_mnt_args *a = arg;
273 if (mnt->mnt_sb != a->sb)
275 a->mnt = mntget(mnt);
279 static struct vfsmount *au_mnt_get(struct super_block *sb)
282 struct au_compare_mnt_args args = {
285 struct mnt_namespace *ns;
287 br_read_lock(vfsmount_lock);
289 AuDebugOn(!current->nsproxy);
290 ns = current->nsproxy->mnt_ns;
292 err = iterate_mounts(au_compare_mnt, &args, ns->root);
293 br_read_unlock(vfsmount_lock);
295 AuDebugOn(!args.mnt);
299 struct au_nfsd_si_lock {
301 aufs_bindex_t bindex, br_id;
302 unsigned char force_lock;
305 static int si_nfsd_read_lock(struct super_block *sb,
306 struct au_nfsd_si_lock *nsi_lock)
309 aufs_bindex_t bindex;
311 si_read_lock(sb, AuLock_FLUSH);
313 /* branch id may be wrapped around */
315 bindex = au_br_index(sb, nsi_lock->br_id);
316 if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
317 goto out; /* success */
321 if (!nsi_lock->force_lock)
325 nsi_lock->bindex = bindex;
329 struct find_name_by_ino {
337 find_name_by_ino(void *arg, const char *name, int namelen, loff_t offset,
338 u64 ino, unsigned int d_type)
340 struct find_name_by_ino *a = arg;
346 memcpy(a->name, name, namelen);
347 a->namelen = namelen;
352 static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
353 struct au_nfsd_si_lock *nsi_lock)
355 struct dentry *dentry, *parent;
358 struct find_name_by_ino arg;
361 parent = path->dentry;
363 si_read_unlock(parent->d_sb);
364 file = vfsub_dentry_open(path, au_dir_roflags);
365 dentry = (void *)file;
369 dentry = ERR_PTR(-ENOMEM);
370 arg.name = __getname_gfp(GFP_NOFS);
371 if (unlikely(!arg.name))
378 err = vfsub_readdir(file, find_name_by_ino, &arg);
379 } while (!err && !arg.found && arg.called);
380 dentry = ERR_PTR(err);
383 dentry = ERR_PTR(-ENOENT);
387 /* do not call au_lkup_one() */
388 dir = parent->d_inode;
389 mutex_lock(&dir->i_mutex);
390 dentry = vfsub_lookup_one_len(arg.name, parent, arg.namelen);
391 mutex_unlock(&dir->i_mutex);
392 AuTraceErrPtr(dentry);
395 AuDebugOn(au_test_anon(dentry));
396 if (unlikely(!dentry->d_inode)) {
398 dentry = ERR_PTR(-ENOENT);
406 if (unlikely(nsi_lock
407 && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
408 if (!IS_ERR(dentry)) {
410 dentry = ERR_PTR(-ESTALE);
412 AuTraceErrPtr(dentry);
416 static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
418 struct au_nfsd_si_lock *nsi_lock)
420 struct dentry *dentry;
423 if (dir_ino != AUFS_ROOT_INO) {
424 path.dentry = decode_by_ino(sb, dir_ino, 0);
425 dentry = path.dentry;
426 if (!path.dentry || IS_ERR(path.dentry))
428 AuDebugOn(au_test_anon(path.dentry));
430 path.dentry = dget(sb->s_root);
432 path.mnt = au_mnt_get(sb);
433 dentry = au_lkup_by_ino(&path, ino, nsi_lock);
437 AuTraceErrPtr(dentry);
441 /* ---------------------------------------------------------------------- */
443 static int h_acceptable(void *expv, struct dentry *dentry)
448 static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
449 char *buf, int len, struct super_block *sb)
455 p = d_path(h_rootpath, buf, len);
460 path.mnt = h_rootpath->mnt;
461 path.dentry = h_parent;
462 p = d_path(&path, buf, len);
468 path.mnt = au_mnt_get(sb);
469 path.dentry = sb->s_root;
470 p = d_path(&path, buf, len - strlen(p));
483 struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
484 int fh_len, struct au_nfsd_si_lock *nsi_lock)
486 struct dentry *dentry, *h_parent, *root;
487 struct super_block *h_sb;
489 struct vfsmount *h_mnt;
490 struct au_branch *br;
494 br = au_sbr(sb, nsi_lock->bindex);
496 h_sb = h_mnt->mnt_sb;
497 /* todo: call lower fh_to_dentry()? fh_to_parent()? */
498 h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
499 fh_len - Fh_tail, fh[Fh_h_type],
500 h_acceptable, /*context*/NULL);
502 if (unlikely(!h_parent || IS_ERR(h_parent))) {
503 AuWarn1("%s decode_fh failed, %ld\n",
504 au_sbtype(h_sb), PTR_ERR(h_parent));
508 if (unlikely(au_test_anon(h_parent))) {
509 AuWarn1("%s decode_fh returned a disconnected dentry\n",
514 dentry = ERR_PTR(-ENOMEM);
515 pathname = (void *)__get_free_page(GFP_NOFS);
516 if (unlikely(!pathname))
521 di_read_lock_parent(root, !AuLock_IR);
522 path.dentry = au_h_dptr(root, nsi_lock->bindex);
523 di_read_unlock(root, !AuLock_IR);
524 p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
530 err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
531 dentry = ERR_PTR(err);
535 dentry = ERR_PTR(-ENOENT);
536 AuDebugOn(au_test_anon(path.dentry));
537 if (unlikely(!path.dentry->d_inode))
540 if (ino != path.dentry->d_inode->i_ino)
541 dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
543 dentry = dget(path.dentry);
548 if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
549 if (!IS_ERR(dentry)) {
551 dentry = ERR_PTR(-ESTALE);
554 free_page((unsigned long)pathname);
558 AuTraceErrPtr(dentry);
562 /* ---------------------------------------------------------------------- */
564 static struct dentry *
565 aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
568 struct dentry *dentry;
569 __u32 *fh = fid->raw;
570 struct au_branch *br;
572 struct au_nfsd_si_lock nsi_lock = {
576 dentry = ERR_PTR(-ESTALE);
577 /* it should never happen, but the file handle is unreliable */
578 if (unlikely(fh_len < Fh_tail))
580 nsi_lock.sigen = fh[Fh_sigen];
581 nsi_lock.br_id = fh[Fh_br_id];
583 /* branch id may be wrapped around */
585 if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
587 nsi_lock.force_lock = 1;
589 /* is this inode still cached? */
590 ino = decode_ino(fh + Fh_ino);
591 /* it should never happen */
592 if (unlikely(ino == AUFS_ROOT_INO))
595 dir_ino = decode_ino(fh + Fh_dir_ino);
596 dentry = decode_by_ino(sb, ino, dir_ino);
602 /* is the parent dir cached? */
603 br = au_sbr(sb, nsi_lock.bindex);
604 atomic_inc(&br->br_count);
605 dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
612 dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
615 if (unlikely(!dentry))
616 /* todo?: make it ESTALE */
620 if (!au_digen_test(dentry, au_sigen(sb))
621 && dentry->d_inode->i_generation == fh[Fh_igen])
622 goto out_unlock; /* success */
625 dentry = ERR_PTR(-ESTALE);
628 atomic_dec(&br->br_count);
631 AuTraceErrPtr(dentry);
635 #if 0 /* reserved for future use */
636 /* support subtreecheck option */
637 static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
638 int fh_len, int fh_type)
640 struct dentry *parent;
641 __u32 *fh = fid->raw;
644 dir_ino = decode_ino(fh + Fh_dir_ino);
645 parent = decode_by_ino(sb, dir_ino, 0);
649 parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
650 dir_ino, fh, fh_len);
653 AuTraceErrPtr(parent);
658 /* ---------------------------------------------------------------------- */
660 static int aufs_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len,
664 aufs_bindex_t bindex, bend;
665 struct super_block *sb, *h_sb;
667 struct dentry *parent, *h_parent;
668 struct au_branch *br;
670 AuDebugOn(au_test_anon(dentry));
674 if (unlikely(*max_len <= Fh_tail)) {
675 AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
680 if (IS_ROOT(dentry)) {
681 AuDebugOn(dentry->d_inode->i_ino != AUFS_ROOT_INO);
686 err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_IR | AuLock_GEN);
690 inode = dentry->d_inode;
693 #ifdef CONFIG_AUFS_DEBUG
694 if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
695 AuWarn1("NFS-exporting requires xino\n");
698 parent = dget_parent(dentry);
699 di_read_lock_parent(parent, !AuLock_IR);
700 bend = au_dbtaildir(parent);
701 for (bindex = au_dbstart(parent); bindex <= bend; bindex++) {
702 h_parent = au_h_dptr(parent, bindex);
708 if (unlikely(!h_parent))
712 br = au_sbr(sb, bindex);
713 h_sb = br->br_mnt->mnt_sb;
714 if (unlikely(!h_sb->s_export_op)) {
715 AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
719 fh[Fh_br_id] = br->br_id;
720 fh[Fh_sigen] = au_sigen(sb);
721 encode_ino(fh + Fh_ino, inode->i_ino);
722 encode_ino(fh + Fh_dir_ino, parent->d_inode->i_ino);
723 fh[Fh_igen] = inode->i_generation;
726 fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
728 /*connectable or subtreecheck*/0);
735 AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
740 di_read_unlock(parent, !AuLock_IR);
742 aufs_read_unlock(dentry, AuLock_IR);
744 if (unlikely(err < 0))
749 /* ---------------------------------------------------------------------- */
751 static int aufs_commit_metadata(struct inode *inode)
754 aufs_bindex_t bindex;
755 struct super_block *sb;
756 struct inode *h_inode;
757 int (*f)(struct inode *inode);
760 si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
761 ii_write_lock_child(inode);
762 bindex = au_ibstart(inode);
763 AuDebugOn(bindex < 0);
764 h_inode = au_h_iptr(inode, bindex);
766 f = h_inode->i_sb->s_export_op->commit_metadata;
770 struct writeback_control wbc = {
771 .sync_mode = WB_SYNC_ALL,
772 .nr_to_write = 0 /* metadata only */
775 err = sync_inode(h_inode, &wbc);
778 au_cpup_attr_timesizes(inode);
779 ii_write_unlock(inode);
784 /* ---------------------------------------------------------------------- */
786 static struct export_operations aufs_export_op = {
787 .fh_to_dentry = aufs_fh_to_dentry,
788 /* .fh_to_parent = aufs_fh_to_parent, */
789 .encode_fh = aufs_encode_fh,
790 .commit_metadata = aufs_commit_metadata
793 void au_export_init(struct super_block *sb)
795 struct au_sbinfo *sbinfo;
798 sb->s_export_op = &aufs_export_op;
800 sbinfo->si_xigen = NULL;
801 get_random_bytes(&u, sizeof(u));
802 BUILD_BUG_ON(sizeof(u) != sizeof(int));
803 atomic_set(&sbinfo->si_xigen_next, u);