fs: use backing_file container for internal files with "fake" f_path

Overlayfs uses open_with_fake_path() to allocate internal kernel files,
with a "fake" path - whose f_path is not on the same fs as f_inode.

Allocate a container struct backing_file for those internal files, that
is used to hold the "fake" ovl path along with the real path.

backing_file_real_path() can be used to access the stored real path.

Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Message-Id: <20230615112229.2143178-5-amir73il@gmail.com>
Signed-off-by: Christian Brauner <brauner@kernel.org>
This commit is contained in:
Amir Goldstein 2023-06-15 14:22:28 +03:00 committed by Christian Brauner
parent 8a05a8c31d
commit 62d53c4a1d
No known key found for this signature in database
GPG key ID: 91C61BC06578DCA2
5 changed files with 114 additions and 23 deletions

View file

@ -1149,23 +1149,44 @@ struct file *kernel_file_open(const struct path *path, int flags,
}
EXPORT_SYMBOL_GPL(kernel_file_open);
struct file *open_with_fake_path(const struct path *path, int flags,
struct inode *inode, const struct cred *cred)
/**
* backing_file_open - open a backing file for kernel internal use
* @path: path of the file to open
* @flags: open flags
* @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().
*/
struct file *backing_file_open(const struct path *path, int flags,
const struct path *real_path,
const struct cred *cred)
{
struct file *f = alloc_empty_file_noaccount(flags, cred);
if (!IS_ERR(f)) {
int error;
struct file *f;
int error;
f->f_path = *path;
error = do_dentry_open(f, inode, NULL);
if (error) {
fput(f);
f = ERR_PTR(error);
}
f = alloc_empty_backing_file(flags, cred);
if (IS_ERR(f))
return f;
f->f_path = *path;
path_get(real_path);
*backing_file_real_path(f) = *real_path;
error = do_dentry_open(f, d_inode(real_path->dentry), NULL);
if (error) {
fput(f);
f = ERR_PTR(error);
}
return f;
}
EXPORT_SYMBOL(open_with_fake_path);
EXPORT_SYMBOL_GPL(backing_file_open);
#define WILL_CREATE(flags) (flags & (O_CREAT | __O_TMPFILE))
#define O_PATH_FLAGS (O_DIRECTORY | O_NOFOLLOW | O_PATH | O_CLOEXEC)