add patch crypto-dont-let-data-integrity-writebacks-fail-with-ENOMEM
[ext4-patch-queue.git] / vfs-add-file-dentry
bloba22e60dee4a6f99eaee9663678cf1eca0ab816f5
1 vfs: add file_dentry()
3 From: Miklos Szeredi <mszeredi@redhat.com>
5 This series fixes bugs in nfs and ext4 due to 4bacc9c9234c ("overlayfs: Make
6 f_path always point to the overlay and f_inode to the underlay").
8 Regular files opened on overlayfs will result in the file being opened on
9 the underlying filesystem, while f_path points to the overlayfs
10 mount/dentry.
12 This confuses filesystems which get the dentry from struct file and assume
13 it's theirs.
15 Add a new helper, file_dentry() [*], to get the filesystem's own dentry
16 from the file.  This simply compares file_inode(file->f_path.dentry) to
17 file_inode(file) and if they are equal returns file->f_path.dentry (this is
18 the common, non-overlayfs case).
20 In the uncommon case (regular file on overlayfs) it will call into
21 overlayfs's ->d_native_dentry() to get the underlying dentry matching
22 file_inode(file).
24 [*] If possible, it's better simply to use file_inode() instead.
26 Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
27 Tested-by: Goldwyn Rodrigues <rgoldwyn@suse.com>
28 Reviewed-by: Trond Myklebust <trond.myklebust@primarydata.com>
29 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
30 Cc: <stable@vger.kernel.org> # v4.2
31 Cc: David Howells <dhowells@redhat.com>
32 Cc: Al Viro <viro@zeniv.linux.org.uk>
33 Cc: Daniel Axtens <dja@axtens.net>
34 ---
35  fs/open.c              | 11 +++++++++++
36  fs/overlayfs/super.c   | 16 ++++++++++++++++
37  include/linux/dcache.h |  1 +
38  include/linux/fs.h     |  2 ++
39  4 files changed, 30 insertions(+)
41 diff --git a/fs/open.c b/fs/open.c
42 index 55bdc75..6326c11 100644
43 --- a/fs/open.c
44 +++ b/fs/open.c
45 @@ -831,6 +831,17 @@ char *file_path(struct file *filp, char *buf, int buflen)
46  }
47  EXPORT_SYMBOL(file_path);
49 +struct dentry *file_dentry(const struct file *file)
51 +       struct dentry *dentry = file->f_path.dentry;
53 +       if (likely(d_inode(dentry) == file_inode(file)))
54 +               return dentry;
55 +       else
56 +               return dentry->d_op->d_native_dentry(dentry, file_inode(file));
58 +EXPORT_SYMBOL(file_dentry);
60  /**
61   * vfs_open - open the file at the given path
62   * @path: path to open
63 diff --git a/fs/overlayfs/super.c b/fs/overlayfs/super.c
64 index 8d826bd..10dbdc7 100644
65 --- a/fs/overlayfs/super.c
66 +++ b/fs/overlayfs/super.c
67 @@ -334,13 +334,29 @@ static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
68         return ret;
69  }
71 +static struct dentry *ovl_d_native_dentry(struct dentry *dentry,
72 +                                        struct inode *inode)
74 +       struct ovl_entry *oe = dentry->d_fsdata;
75 +       struct dentry *realentry = ovl_upperdentry_dereference(oe);
77 +       if (realentry && inode == d_inode(realentry))
78 +               return realentry;
79 +       realentry = __ovl_dentry_lower(oe);
80 +       if (realentry && inode == d_inode(realentry))
81 +               return realentry;
82 +       BUG();
85  static const struct dentry_operations ovl_dentry_operations = {
86         .d_release = ovl_dentry_release,
87         .d_select_inode = ovl_d_select_inode,
88 +       .d_native_dentry = ovl_d_native_dentry,
89  };
91  static const struct dentry_operations ovl_reval_dentry_operations = {
92         .d_release = ovl_dentry_release,
93 +       .d_native_dentry = ovl_d_native_dentry,
94         .d_revalidate = ovl_dentry_revalidate,
95         .d_weak_revalidate = ovl_dentry_weak_revalidate,
96  };
97 diff --git a/include/linux/dcache.h b/include/linux/dcache.h
98 index 7781ce11..957dd36 100644
99 --- a/include/linux/dcache.h
100 +++ b/include/linux/dcache.h
101 @@ -161,6 +161,7 @@ struct dentry_operations {
102         struct vfsmount *(*d_automount)(struct path *);
103         int (*d_manage)(struct dentry *, bool);
104         struct inode *(*d_select_inode)(struct dentry *, unsigned);
105 +       struct dentry *(*d_native_dentry)(struct dentry *, struct inode *);
106  } ____cacheline_aligned;
108  /*
109 diff --git a/include/linux/fs.h b/include/linux/fs.h
110 index 1a20462..e626ab6 100644
111 --- a/include/linux/fs.h
112 +++ b/include/linux/fs.h
113 @@ -1237,6 +1237,8 @@ static inline struct inode *file_inode(const struct file *f)
114         return f->f_inode;
117 +extern struct dentry *file_dentry(const struct file *file);
119  static inline int locks_lock_file_wait(struct file *filp, struct file_lock *fl)
121         return locks_lock_inode_wait(file_inode(filp), fl);