2 * Copyright (C) 2005-2016 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/>.
22 #include <linux/exportfs.h>
23 #include <linux/fs_struct.h>
24 #include <linux/namei.h>
25 #include <linux/nsproxy.h>
26 #include <linux/random.h>
27 #include <linux/writeback.h>
31 #ifdef CONFIG_AUFS_INO_T_64
39 static ino_t
decode_ino(__u32
*a
)
43 BUILD_BUG_ON(sizeof(u
.ino
) != sizeof(u
.a
));
45 #ifdef CONFIG_AUFS_INO_T_64
51 static void encode_ino(__u32
*a
, ino_t ino
)
57 #ifdef CONFIG_AUFS_INO_T_64
66 #ifdef CONFIG_AUFS_INO_T_64
67 /* support 64bit inode number */
81 Fh_dir_ino
= Fh_dir_ino1
84 static int au_test_anon(struct dentry
*dentry
)
86 /* note: read d_flags without d_lock */
87 return !!(dentry
->d_flags
& DCACHE_DISCONNECTED
);
90 int au_test_nfsd(void)
93 struct task_struct
*tsk
= current
;
94 char comm
[sizeof(tsk
->comm
)];
97 if (tsk
->flags
& PF_KTHREAD
) {
98 get_task_comm(comm
, tsk
);
99 ret
= !strcmp(comm
, "nfsd");
105 /* ---------------------------------------------------------------------- */
106 /* inode generation external table */
108 void au_xigen_inc(struct inode
*inode
)
113 struct super_block
*sb
;
114 struct au_sbinfo
*sbinfo
;
117 AuDebugOn(!au_opt_test(au_mntflags(sb
), XINO
));
122 igen
= inode
->i_generation
+ 1;
123 sz
= xino_fwrite(sbinfo
->si_xwrite
, sbinfo
->si_xigen
, &igen
,
125 if (sz
== sizeof(igen
))
126 return; /* success */
128 if (unlikely(sz
>= 0))
129 AuIOErr("xigen error (%zd)\n", sz
);
132 int au_xigen_new(struct inode
*inode
)
137 struct super_block
*sb
;
138 struct au_sbinfo
*sbinfo
;
142 /* todo: dirty, at mount time */
143 if (inode
->i_ino
== AUFS_ROOT_INO
)
147 if (unlikely(!au_opt_test(au_mntflags(sb
), XINO
)))
152 if (unlikely(au_loff_max
/ sizeof(inode
->i_generation
) - 1 < pos
)) {
153 AuIOErr1("too large i%lld\n", pos
);
156 pos
*= sizeof(inode
->i_generation
);
160 file
= sbinfo
->si_xigen
;
163 if (vfsub_f_size_read(file
)
164 < pos
+ sizeof(inode
->i_generation
)) {
165 inode
->i_generation
= atomic_inc_return(&sbinfo
->si_xigen_next
);
166 sz
= xino_fwrite(sbinfo
->si_xwrite
, file
, &inode
->i_generation
,
167 sizeof(inode
->i_generation
), &pos
);
169 sz
= xino_fread(sbinfo
->si_xread
, file
, &inode
->i_generation
,
170 sizeof(inode
->i_generation
), &pos
);
171 if (sz
== sizeof(inode
->i_generation
))
172 goto out
; /* success */
175 if (unlikely(sz
>= 0)) {
177 AuIOErr("xigen error (%zd)\n", sz
);
184 int au_xigen_set(struct super_block
*sb
, struct file
*base
)
187 struct au_sbinfo
*sbinfo
;
193 file
= au_xino_create2(base
, sbinfo
->si_xigen
);
198 if (sbinfo
->si_xigen
)
199 fput(sbinfo
->si_xigen
);
200 sbinfo
->si_xigen
= file
;
206 void au_xigen_clr(struct super_block
*sb
)
208 struct au_sbinfo
*sbinfo
;
213 if (sbinfo
->si_xigen
) {
214 fput(sbinfo
->si_xigen
);
215 sbinfo
->si_xigen
= NULL
;
219 /* ---------------------------------------------------------------------- */
221 static struct dentry
*decode_by_ino(struct super_block
*sb
, ino_t ino
,
224 struct dentry
*dentry
, *d
;
229 inode
= ilookup(sb
, ino
);
233 dentry
= ERR_PTR(-ESTALE
);
234 sigen
= au_sigen(sb
);
235 if (unlikely(au_is_bad_inode(inode
)
237 || sigen
!= au_iigen(inode
, NULL
)))
241 if (!dir_ino
|| S_ISDIR(inode
->i_mode
))
242 dentry
= d_find_alias(inode
);
244 spin_lock(&inode
->i_lock
);
245 hlist_for_each_entry(d
, &inode
->i_dentry
, d_u
.d_alias
) {
246 spin_lock(&d
->d_lock
);
248 && d_inode(d
->d_parent
)->i_ino
== dir_ino
) {
249 dentry
= dget_dlock(d
);
250 spin_unlock(&d
->d_lock
);
253 spin_unlock(&d
->d_lock
);
255 spin_unlock(&inode
->i_lock
);
257 if (unlikely(dentry
&& au_digen_test(dentry
, sigen
))) {
258 /* need to refresh */
266 AuTraceErrPtr(dentry
);
270 /* ---------------------------------------------------------------------- */
273 /* if exportfs_decode_fh() passed vfsmount*, we could be happy */
275 struct au_compare_mnt_args
{
277 struct super_block
*sb
;
280 struct vfsmount
*mnt
;
283 static int au_compare_mnt(struct vfsmount
*mnt
, void *arg
)
285 struct au_compare_mnt_args
*a
= arg
;
287 if (mnt
->mnt_sb
!= a
->sb
)
289 a
->mnt
= mntget(mnt
);
293 static struct vfsmount
*au_mnt_get(struct super_block
*sb
)
297 struct au_compare_mnt_args args
= {
301 get_fs_root(current
->fs
, &root
);
303 err
= iterate_mounts(au_compare_mnt
, &args
, root
.mnt
);
307 AuDebugOn(!args
.mnt
);
311 struct au_nfsd_si_lock
{
313 aufs_bindex_t bindex
, br_id
;
314 unsigned char force_lock
;
317 static int si_nfsd_read_lock(struct super_block
*sb
,
318 struct au_nfsd_si_lock
*nsi_lock
)
321 aufs_bindex_t bindex
;
323 si_read_lock(sb
, AuLock_FLUSH
);
325 /* branch id may be wrapped around */
327 bindex
= au_br_index(sb
, nsi_lock
->br_id
);
328 if (bindex
>= 0 && nsi_lock
->sigen
+ AUFS_BRANCH_MAX
> au_sigen(sb
))
329 goto out
; /* success */
333 if (!nsi_lock
->force_lock
)
337 nsi_lock
->bindex
= bindex
;
341 struct find_name_by_ino
{
342 struct dir_context ctx
;
350 find_name_by_ino(struct dir_context
*ctx
, const char *name
, int namelen
,
351 loff_t offset
, u64 ino
, unsigned int d_type
)
353 struct find_name_by_ino
*a
= container_of(ctx
, struct find_name_by_ino
,
360 memcpy(a
->name
, name
, namelen
);
361 a
->namelen
= namelen
;
366 static struct dentry
*au_lkup_by_ino(struct path
*path
, ino_t ino
,
367 struct au_nfsd_si_lock
*nsi_lock
)
369 struct dentry
*dentry
, *parent
;
372 struct find_name_by_ino arg
= {
374 .actor
= find_name_by_ino
379 parent
= path
->dentry
;
381 si_read_unlock(parent
->d_sb
);
382 file
= vfsub_dentry_open(path
, au_dir_roflags
);
383 dentry
= (void *)file
;
387 dentry
= ERR_PTR(-ENOMEM
);
388 arg
.name
= (void *)__get_free_page(GFP_NOFS
);
389 if (unlikely(!arg
.name
))
396 err
= vfsub_iterate_dir(file
, &arg
.ctx
);
397 } while (!err
&& !arg
.found
&& arg
.called
);
398 dentry
= ERR_PTR(err
);
401 /* instead of ENOENT */
402 dentry
= ERR_PTR(-ESTALE
);
406 /* do not call vfsub_lkup_one() */
407 dir
= d_inode(parent
);
408 dentry
= vfsub_lookup_one_len_unlocked(arg
.name
, parent
, arg
.namelen
);
409 AuTraceErrPtr(dentry
);
412 AuDebugOn(au_test_anon(dentry
));
413 if (unlikely(d_really_is_negative(dentry
))) {
415 dentry
= ERR_PTR(-ENOENT
);
419 au_delayed_free_page((unsigned long)arg
.name
);
423 if (unlikely(nsi_lock
424 && si_nfsd_read_lock(parent
->d_sb
, nsi_lock
) < 0))
425 if (!IS_ERR(dentry
)) {
427 dentry
= ERR_PTR(-ESTALE
);
429 AuTraceErrPtr(dentry
);
433 static struct dentry
*decode_by_dir_ino(struct super_block
*sb
, ino_t ino
,
435 struct au_nfsd_si_lock
*nsi_lock
)
437 struct dentry
*dentry
;
440 if (dir_ino
!= AUFS_ROOT_INO
) {
441 path
.dentry
= decode_by_ino(sb
, dir_ino
, 0);
442 dentry
= path
.dentry
;
443 if (!path
.dentry
|| IS_ERR(path
.dentry
))
445 AuDebugOn(au_test_anon(path
.dentry
));
447 path
.dentry
= dget(sb
->s_root
);
449 path
.mnt
= au_mnt_get(sb
);
450 dentry
= au_lkup_by_ino(&path
, ino
, nsi_lock
);
454 AuTraceErrPtr(dentry
);
458 /* ---------------------------------------------------------------------- */
460 static int h_acceptable(void *expv
, struct dentry
*dentry
)
465 static char *au_build_path(struct dentry
*h_parent
, struct path
*h_rootpath
,
466 char *buf
, int len
, struct super_block
*sb
)
472 p
= d_path(h_rootpath
, buf
, len
);
477 path
.mnt
= h_rootpath
->mnt
;
478 path
.dentry
= h_parent
;
479 p
= d_path(&path
, buf
, len
);
485 path
.mnt
= au_mnt_get(sb
);
486 path
.dentry
= sb
->s_root
;
487 p
= d_path(&path
, buf
, len
- strlen(p
));
500 struct dentry
*decode_by_path(struct super_block
*sb
, ino_t ino
, __u32
*fh
,
501 int fh_len
, struct au_nfsd_si_lock
*nsi_lock
)
503 struct dentry
*dentry
, *h_parent
, *root
;
504 struct super_block
*h_sb
;
506 struct vfsmount
*h_mnt
;
507 struct au_branch
*br
;
511 br
= au_sbr(sb
, nsi_lock
->bindex
);
512 h_mnt
= au_br_mnt(br
);
513 h_sb
= h_mnt
->mnt_sb
;
514 /* todo: call lower fh_to_dentry()? fh_to_parent()? */
516 h_parent
= exportfs_decode_fh(h_mnt
, (void *)(fh
+ Fh_tail
),
517 fh_len
- Fh_tail
, fh
[Fh_h_type
],
518 h_acceptable
, /*context*/NULL
);
521 if (unlikely(!h_parent
|| IS_ERR(h_parent
))) {
522 AuWarn1("%s decode_fh failed, %ld\n",
523 au_sbtype(h_sb
), PTR_ERR(h_parent
));
527 if (unlikely(au_test_anon(h_parent
))) {
528 AuWarn1("%s decode_fh returned a disconnected dentry\n",
533 dentry
= ERR_PTR(-ENOMEM
);
534 pathname
= (void *)__get_free_page(GFP_NOFS
);
535 if (unlikely(!pathname
))
540 di_read_lock_parent(root
, !AuLock_IR
);
541 path
.dentry
= au_h_dptr(root
, nsi_lock
->bindex
);
542 di_read_unlock(root
, !AuLock_IR
);
543 p
= au_build_path(h_parent
, &path
, pathname
, PAGE_SIZE
, sb
);
549 err
= vfsub_kern_path(p
, LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
, &path
);
550 dentry
= ERR_PTR(err
);
554 dentry
= ERR_PTR(-ENOENT
);
555 AuDebugOn(au_test_anon(path
.dentry
));
556 if (unlikely(d_really_is_negative(path
.dentry
)))
559 if (ino
!= d_inode(path
.dentry
)->i_ino
)
560 dentry
= au_lkup_by_ino(&path
, ino
, /*nsi_lock*/NULL
);
562 dentry
= dget(path
.dentry
);
567 if (unlikely(si_nfsd_read_lock(sb
, nsi_lock
) < 0))
568 if (!IS_ERR(dentry
)) {
570 dentry
= ERR_PTR(-ESTALE
);
573 au_delayed_free_page((unsigned long)pathname
);
577 AuTraceErrPtr(dentry
);
581 /* ---------------------------------------------------------------------- */
583 static struct dentry
*
584 aufs_fh_to_dentry(struct super_block
*sb
, struct fid
*fid
, int fh_len
,
587 struct dentry
*dentry
;
588 __u32
*fh
= fid
->raw
;
589 struct au_branch
*br
;
591 struct au_nfsd_si_lock nsi_lock
= {
595 dentry
= ERR_PTR(-ESTALE
);
596 /* it should never happen, but the file handle is unreliable */
597 if (unlikely(fh_len
< Fh_tail
))
599 nsi_lock
.sigen
= fh
[Fh_sigen
];
600 nsi_lock
.br_id
= fh
[Fh_br_id
];
602 /* branch id may be wrapped around */
604 if (unlikely(si_nfsd_read_lock(sb
, &nsi_lock
)))
606 nsi_lock
.force_lock
= 1;
608 /* is this inode still cached? */
609 ino
= decode_ino(fh
+ Fh_ino
);
610 /* it should never happen */
611 if (unlikely(ino
== AUFS_ROOT_INO
))
614 dir_ino
= decode_ino(fh
+ Fh_dir_ino
);
615 dentry
= decode_by_ino(sb
, ino
, dir_ino
);
621 /* is the parent dir cached? */
622 br
= au_sbr(sb
, nsi_lock
.bindex
);
624 dentry
= decode_by_dir_ino(sb
, ino
, dir_ino
, &nsi_lock
);
631 dentry
= decode_by_path(sb
, ino
, fh
, fh_len
, &nsi_lock
);
634 if (unlikely(!dentry
))
635 /* todo?: make it ESTALE */
639 if (!au_digen_test(dentry
, au_sigen(sb
))
640 && d_inode(dentry
)->i_generation
== fh
[Fh_igen
])
641 goto out_unlock
; /* success */
644 dentry
= ERR_PTR(-ESTALE
);
650 AuTraceErrPtr(dentry
);
654 #if 0 /* reserved for future use */
655 /* support subtreecheck option */
656 static struct dentry
*aufs_fh_to_parent(struct super_block
*sb
, struct fid
*fid
,
657 int fh_len
, int fh_type
)
659 struct dentry
*parent
;
660 __u32
*fh
= fid
->raw
;
663 dir_ino
= decode_ino(fh
+ Fh_dir_ino
);
664 parent
= decode_by_ino(sb
, dir_ino
, 0);
668 parent
= decode_by_path(sb
, au_br_index(sb
, fh
[Fh_br_id
]),
669 dir_ino
, fh
, fh_len
);
672 AuTraceErrPtr(parent
);
677 /* ---------------------------------------------------------------------- */
679 static int aufs_encode_fh(struct inode
*inode
, __u32
*fh
, int *max_len
,
683 aufs_bindex_t bindex
;
684 struct super_block
*sb
, *h_sb
;
685 struct dentry
*dentry
, *parent
, *h_parent
;
687 struct au_branch
*br
;
690 if (unlikely(*max_len
<= Fh_tail
)) {
691 AuWarn1("NFSv2 client (max_len %d)?\n", *max_len
);
696 if (inode
->i_ino
== AUFS_ROOT_INO
) {
697 AuDebugOn(inode
->i_ino
!= AUFS_ROOT_INO
);
703 err
= si_read_lock(sb
, AuLock_FLUSH
);
707 #ifdef CONFIG_AUFS_DEBUG
708 if (unlikely(!au_opt_test(au_mntflags(sb
), XINO
)))
709 AuWarn1("NFS-exporting requires xino\n");
713 ii_read_lock_child(inode
);
714 bindex
= au_ibtop(inode
);
716 dentry
= d_find_any_alias(inode
);
717 if (unlikely(!dentry
))
719 AuDebugOn(au_test_anon(dentry
));
720 parent
= dget_parent(dentry
);
722 if (unlikely(!parent
))
724 if (d_really_is_positive(parent
))
725 dir
= d_inode(parent
);
728 ii_read_lock_parent(dir
);
729 h_dir
= au_h_iptr(dir
, bindex
);
731 if (unlikely(!h_dir
))
733 h_parent
= d_find_any_alias(h_dir
);
734 if (unlikely(!h_parent
))
738 br
= au_sbr(sb
, bindex
);
740 if (unlikely(!h_sb
->s_export_op
)) {
741 AuErr1("%s branch is not exportable\n", au_sbtype(h_sb
));
745 fh
[Fh_br_id
] = br
->br_id
;
746 fh
[Fh_sigen
] = au_sigen(sb
);
747 encode_ino(fh
+ Fh_ino
, inode
->i_ino
);
748 encode_ino(fh
+ Fh_dir_ino
, dir
->i_ino
);
749 fh
[Fh_igen
] = inode
->i_generation
;
752 fh
[Fh_h_type
] = exportfs_encode_fh(h_parent
, (void *)(fh
+ Fh_tail
),
754 /*connectable or subtreecheck*/0);
758 if (err
!= FILEID_INVALID
)
761 AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb
));
768 ii_read_unlock(inode
);
771 if (unlikely(err
< 0))
772 err
= FILEID_INVALID
;
776 /* ---------------------------------------------------------------------- */
778 static int aufs_commit_metadata(struct inode
*inode
)
781 aufs_bindex_t bindex
;
782 struct super_block
*sb
;
783 struct inode
*h_inode
;
784 int (*f
)(struct inode
*inode
);
787 si_read_lock(sb
, AuLock_FLUSH
| AuLock_NOPLMW
);
788 ii_write_lock_child(inode
);
789 bindex
= au_ibtop(inode
);
790 AuDebugOn(bindex
< 0);
791 h_inode
= au_h_iptr(inode
, bindex
);
793 f
= h_inode
->i_sb
->s_export_op
->commit_metadata
;
797 struct writeback_control wbc
= {
798 .sync_mode
= WB_SYNC_ALL
,
799 .nr_to_write
= 0 /* metadata only */
802 err
= sync_inode(h_inode
, &wbc
);
805 au_cpup_attr_timesizes(inode
);
806 ii_write_unlock(inode
);
811 /* ---------------------------------------------------------------------- */
813 static struct export_operations aufs_export_op
= {
814 .fh_to_dentry
= aufs_fh_to_dentry
,
815 /* .fh_to_parent = aufs_fh_to_parent, */
816 .encode_fh
= aufs_encode_fh
,
817 .commit_metadata
= aufs_commit_metadata
820 void au_export_init(struct super_block
*sb
)
822 struct au_sbinfo
*sbinfo
;
825 BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS
)
826 && IS_MODULE(CONFIG_EXPORTFS
),
827 AUFS_NAME
": unsupported configuration "
828 "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
830 sb
->s_export_op
= &aufs_export_op
;
832 sbinfo
->si_xigen
= NULL
;
833 get_random_bytes(&u
, sizeof(u
));
834 BUILD_BUG_ON(sizeof(u
) != sizeof(int));
835 atomic_set(&sbinfo
->si_xigen_next
, u
);