Skip to content

Commit cbb0b9d

Browse files
amir73ilbrauner
authored andcommitted
fs: use a helper for opening kernel internal files
cachefiles uses kernel_open_tmpfile() to open kernel internal tmpfile without accounting for nr_files. cachefiles uses open_with_fake_path() for the same reason without the need for a fake path. Fork open_with_fake_path() to kernel_file_open() which only does the noaccount part and use it in cachefiles. Signed-off-by: Amir Goldstein <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Message-Id: <[email protected]> Signed-off-by: Christian Brauner <[email protected]>
1 parent d56e0dd commit cbb0b9d

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

fs/cachefiles/namei.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,8 +560,8 @@ static bool cachefiles_open_file(struct cachefiles_object *object,
560560
*/
561561
path.mnt = cache->mnt;
562562
path.dentry = dentry;
563-
file = open_with_fake_path(&path, O_RDWR | O_LARGEFILE | O_DIRECT,
564-
d_backing_inode(dentry), cache->cache_cred);
563+
file = kernel_file_open(&path, O_RDWR | O_LARGEFILE | O_DIRECT,
564+
d_backing_inode(dentry), cache->cache_cred);
565565
if (IS_ERR(file)) {
566566
trace_cachefiles_vfs_error(object, d_backing_inode(dentry),
567567
PTR_ERR(file),

fs/open.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,39 @@ struct file *dentry_create(const struct path *path, int flags, umode_t mode,
11161116
}
11171117
EXPORT_SYMBOL(dentry_create);
11181118

1119+
/**
1120+
* kernel_file_open - open a file for kernel internal use
1121+
* @path: path of the file to open
1122+
* @flags: open flags
1123+
* @inode: the inode
1124+
* @cred: credentials for open
1125+
*
1126+
* Open a file for use by in-kernel consumers. The file is not accounted
1127+
* against nr_files and must not be installed into the file descriptor
1128+
* table.
1129+
*
1130+
* Return: Opened file on success, an error pointer on failure.
1131+
*/
1132+
struct file *kernel_file_open(const struct path *path, int flags,
1133+
struct inode *inode, const struct cred *cred)
1134+
{
1135+
struct file *f;
1136+
int error;
1137+
1138+
f = alloc_empty_file_noaccount(flags, cred);
1139+
if (IS_ERR(f))
1140+
return f;
1141+
1142+
f->f_path = *path;
1143+
error = do_dentry_open(f, inode, NULL);
1144+
if (error) {
1145+
fput(f);
1146+
f = ERR_PTR(error);
1147+
}
1148+
return f;
1149+
}
1150+
EXPORT_SYMBOL_GPL(kernel_file_open);
1151+
11191152
struct file *open_with_fake_path(const struct path *path, int flags,
11201153
struct inode *inode, const struct cred *cred)
11211154
{

include/linux/fs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,6 +1676,8 @@ struct file *kernel_tmpfile_open(struct mnt_idmap *idmap,
16761676
const struct path *parentpath,
16771677
umode_t mode, int open_flag,
16781678
const struct cred *cred);
1679+
struct file *kernel_file_open(const struct path *path, int flags,
1680+
struct inode *inode, const struct cred *cred);
16791681

16801682
int vfs_mkobj(struct dentry *, umode_t,
16811683
int (*f)(struct dentry *, umode_t, void *),

0 commit comments

Comments
 (0)