]> git.ipfire.org Git - thirdparty/kernel/linux.git/blob - fs/backing-file.c
fs: prepare for stackable filesystems backing file helpers
[thirdparty/kernel/linux.git] / fs / backing-file.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Common helpers for stackable filesystems and backing files.
4 *
5 * Copyright (C) 2023 CTERA Networks.
6 */
7
8 #include <linux/fs.h>
9 #include <linux/backing-file.h>
10
11 #include "internal.h"
12
13 /**
14 * backing_file_open - open a backing file for kernel internal use
15 * @user_path: path that the user reuqested to open
16 * @flags: open flags
17 * @real_path: path of the backing file
18 * @cred: credentials for open
19 *
20 * Open a backing file for a stackable filesystem (e.g., overlayfs).
21 * @user_path may be on the stackable filesystem and @real_path on the
22 * underlying filesystem. In this case, we want to be able to return the
23 * @user_path of the stackable filesystem. This is done by embedding the
24 * returned file into a container structure that also stores the stacked
25 * file's path, which can be retrieved using backing_file_user_path().
26 */
27 struct file *backing_file_open(const struct path *user_path, int flags,
28 const struct path *real_path,
29 const struct cred *cred)
30 {
31 struct file *f;
32 int error;
33
34 f = alloc_empty_backing_file(flags, cred);
35 if (IS_ERR(f))
36 return f;
37
38 path_get(user_path);
39 *backing_file_user_path(f) = *user_path;
40 error = vfs_open(real_path, f);
41 if (error) {
42 fput(f);
43 f = ERR_PTR(error);
44 }
45
46 return f;
47 }
48 EXPORT_SYMBOL_GPL(backing_file_open);