fs: store real path instead of fake path in backing file f_path
authorAmir Goldstein <amir73il@gmail.com>
Mon, 9 Oct 2023 15:37:12 +0000 (18:37 +0300)
committerChristian Brauner <brauner@kernel.org>
Thu, 19 Oct 2023 09:03:15 +0000 (11:03 +0200)
A backing file struct stores two path's, one "real" path that is referring
to f_inode and one "fake" path, which should be displayed to users in
/proc/<pid>/maps.

There is a lot more potential code that needs to know the "real" path, then
code that needs to know the "fake" path.

Instead of code having to request the "real" path with file_real_path(),
store the "real" path in f_path and require code that needs to know the
"fake" path request it with file_user_path().
Replace the file_real_path() helper with a simple const accessor f_path().

After this change, file_dentry() is not expected to observe any files
with overlayfs f_path and real f_inode, so the call to ->d_real() should
not be needed.  Leave the ->d_real() call for now and add an assertion
in ovl_d_real() to catch if we made wrong assumptions.

Suggested-by: Miklos Szeredi <miklos@szeredi.hu>
Link: https://lore.kernel.org/r/CAJfpegtt48eXhhjDFA1ojcHPNKj3Go6joryCPtEFAKpocyBsnw@mail.gmail.com/
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Link: https://lore.kernel.org/r/20231009153712.1566422-4-amir73il@gmail.com
Signed-off-by: Christian Brauner <brauner@kernel.org>
fs/file_table.c
fs/internal.h
fs/open.c
fs/overlayfs/super.c
include/linux/fs.h
include/linux/fsnotify.h

index 08fd1dd6d863693ae85044e91f101596f2ad6b70..fa92743ba6a91691f3621e0d8d050460c24649ef 100644 (file)
@@ -44,10 +44,10 @@ static struct kmem_cache *filp_cachep __read_mostly;
 
 static struct percpu_counter nr_files __cacheline_aligned_in_smp;
 
-/* Container for backing file with optional real path */
+/* Container for backing file with optional user path */
 struct backing_file {
        struct file file;
-       struct path real_path;
+       struct path user_path;
 };
 
 static inline struct backing_file *backing_file(struct file *f)
@@ -55,11 +55,11 @@ static inline struct backing_file *backing_file(struct file *f)
        return container_of(f, struct backing_file, file);
 }
 
-struct path *backing_file_real_path(struct file *f)
+struct path *backing_file_user_path(struct file *f)
 {
-       return &backing_file(f)->real_path;
+       return &backing_file(f)->user_path;
 }
-EXPORT_SYMBOL_GPL(backing_file_real_path);
+EXPORT_SYMBOL_GPL(backing_file_user_path);
 
 static inline void file_free(struct file *f)
 {
@@ -68,7 +68,7 @@ static inline void file_free(struct file *f)
                percpu_counter_dec(&nr_files);
        put_cred(f->f_cred);
        if (unlikely(f->f_mode & FMODE_BACKING)) {
-               path_put(backing_file_real_path(f));
+               path_put(backing_file_user_path(f));
                kfree(backing_file(f));
        } else {
                kmem_cache_free(filp_cachep, f);
index 4e93a685bdaa6e88e4f8d6f0e4b2b5d082a5148c..58e43341aebf07d92245f789fd1cdf8d5415cebd 100644 (file)
@@ -101,7 +101,7 @@ static inline void file_put_write_access(struct file *file)
        put_write_access(file->f_inode);
        mnt_put_write_access(file->f_path.mnt);
        if (unlikely(file->f_mode & FMODE_BACKING))
-               mnt_put_write_access(backing_file_real_path(file)->mnt);
+               mnt_put_write_access(backing_file_user_path(file)->mnt);
 }
 
 static inline void put_file_access(struct file *file)
index fe63e236da2225e1ac4579d8b5b41dea06cd51a3..02dc608d40d81f90c9427695763c85d0fd111e93 100644 (file)
--- a/fs/open.c
+++ b/fs/open.c
@@ -881,7 +881,7 @@ static inline int file_get_write_access(struct file *f)
        if (unlikely(error))
                goto cleanup_inode;
        if (unlikely(f->f_mode & FMODE_BACKING)) {
-               error = mnt_get_write_access(backing_file_real_path(f)->mnt);
+               error = mnt_get_write_access(backing_file_user_path(f)->mnt);
                if (unlikely(error))
                        goto cleanup_mnt;
        }
@@ -1182,20 +1182,19 @@ EXPORT_SYMBOL_GPL(kernel_file_open);
 
 /**
  * backing_file_open - open a backing file for kernel internal use
- * @path:      path of the file to open
+ * @user_path: path that the user reuqested to open
  * @flags:     open flags
  * @real_path: path of the backing file
  * @cred:      credentials for open
  *
  * Open a backing file for a stackable filesystem (e.g., overlayfs).
- * @path may be on the stackable filesystem and backing inode on the
- * underlying filesystem. In this case, we want to be able to return
- * the @real_path of the backing inode. This is done by embedding the
- * returned file into a container structure that also stores the path of
- * the backing inode on the underlying filesystem, which can be
- * retrieved using backing_file_real_path().
+ * @user_path may be on the stackable filesystem and @real_path on the
+ * underlying filesystem.  In this case, we want to be able to return the
+ * @user_path of the stackable filesystem. This is done by embedding the
+ * returned file into a container structure that also stores the stacked
+ * file's path, which can be retrieved using backing_file_user_path().
  */
-struct file *backing_file_open(const struct path *path, int flags,
+struct file *backing_file_open(const struct path *user_path, int flags,
                               const struct path *real_path,
                               const struct cred *cred)
 {
@@ -1206,9 +1205,9 @@ struct file *backing_file_open(const struct path *path, int flags,
        if (IS_ERR(f))
                return f;
 
-       f->f_path = *path;
-       path_get(real_path);
-       *backing_file_real_path(f) = *real_path;
+       path_get(user_path);
+       *backing_file_user_path(f) = *user_path;
+       f->f_path = *real_path;
        error = do_dentry_open(f, d_inode(real_path->dentry), NULL);
        if (error) {
                fput(f);
index def266b5e2a33b0aa630662286827227f952e513..9f43f0d303ad786f0f5d43b19995d2990450019e 100644 (file)
@@ -34,14 +34,22 @@ static struct dentry *ovl_d_real(struct dentry *dentry,
        struct dentry *real = NULL, *lower;
        int err;
 
-       /* It's an overlay file */
+       /*
+        * vfs is only expected to call d_real() with NULL from d_real_inode()
+        * and with overlay inode from file_dentry() on an overlay file.
+        *
+        * TODO: remove @inode argument from d_real() API, remove code in this
+        * function that deals with non-NULL @inode and remove d_real() call
+        * from file_dentry().
+        */
        if (inode && d_inode(dentry) == inode)
                return dentry;
+       else if (inode)
+               goto bug;
 
        if (!d_is_reg(dentry)) {
-               if (!inode || inode == d_inode(dentry))
-                       return dentry;
-               goto bug;
+               /* d_real_inode() is only relevant for regular files */
+               return dentry;
        }
 
        real = ovl_dentry_upper(dentry);
index 057a3bbd4d275b8019ba217d3fc35e14751dbaf4..b37dccfb45346b4971c3cbbcfecd0467dde2bd28 100644 (file)
@@ -2492,26 +2492,10 @@ struct file *dentry_open(const struct path *path, int flags,
                         const struct cred *creds);
 struct file *dentry_create(const struct path *path, int flags, umode_t mode,
                           const struct cred *cred);
-struct file *backing_file_open(const struct path *path, int flags,
+struct file *backing_file_open(const struct path *user_path, int flags,
                               const struct path *real_path,
                               const struct cred *cred);
-struct path *backing_file_real_path(struct file *f);
-
-/*
- * file_real_path - get the path corresponding to f_inode
- *
- * When opening a backing file for a stackable filesystem (e.g.,
- * overlayfs) f_path may be on the stackable filesystem and f_inode on
- * the underlying filesystem.  When the path associated with f_inode is
- * needed, this helper should be used instead of accessing f_path
- * directly.
-*/
-static inline const struct path *file_real_path(struct file *f)
-{
-       if (unlikely(f->f_mode & FMODE_BACKING))
-               return backing_file_real_path(f);
-       return &f->f_path;
-}
+struct path *backing_file_user_path(struct file *f);
 
 /*
  * file_user_path - get the path to display for memory mapped file
@@ -2524,6 +2508,8 @@ static inline const struct path *file_real_path(struct file *f)
  */
 static inline const struct path *file_user_path(struct file *f)
 {
+       if (unlikely(f->f_mode & FMODE_BACKING))
+               return backing_file_user_path(f);
        return &f->f_path;
 }
 
index ed48e4f1e755fbb63b88c3c90d9bedea5a64906c..bcb6609b54b30af1ffaaac647e98500149890048 100644 (file)
@@ -96,8 +96,7 @@ static inline int fsnotify_file(struct file *file, __u32 mask)
        if (file->f_mode & FMODE_NONOTIFY)
                return 0;
 
-       /* Overlayfs internal files have fake f_path */
-       path = file_real_path(file);
+       path = &file->f_path;
        return fsnotify_parent(path->dentry, mask, path, FSNOTIFY_EVENT_PATH);
 }