Skip to content

Commit 24a8175

Browse files
author
Miklos Szeredi
committed
cachefiles: use vfs_tmpfile_open() helper
Use the vfs_tmpfile_open() helper instead of doing tmpfile creation and opening separately. The only minor difference is that previously no permission checking was done, while vfs_tmpfile_open() will call may_open() with zero access mask (i.e. no access is checked). Even if this would make a difference with callers caps (don't see how it could, even in the LSM codepaths) cachfiles raises caps before performing the tmpfile creation, so this extra permission check will not result in any regression. Reviewed-by: Christian Brauner (Microsoft) <[email protected]> Signed-off-by: Miklos Szeredi <[email protected]>
1 parent 08d7a6f commit 24a8175

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

fs/cachefiles/namei.c

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -442,18 +442,19 @@ struct file *cachefiles_create_tmpfile(struct cachefiles_object *object)
442442
const struct cred *saved_cred;
443443
struct dentry *fan = volume->fanout[(u8)object->cookie->key_hash];
444444
struct file *file;
445-
struct path path;
445+
const struct path parentpath = { .mnt = cache->mnt, .dentry = fan };
446446
uint64_t ni_size;
447447
long ret;
448448

449449

450450
cachefiles_begin_secure(cache, &saved_cred);
451451

452-
path.mnt = cache->mnt;
453452
ret = cachefiles_inject_write_error();
454453
if (ret == 0) {
455-
path.dentry = vfs_tmpfile(&init_user_ns, fan, S_IFREG, O_RDWR);
456-
ret = PTR_ERR_OR_ZERO(path.dentry);
454+
file = vfs_tmpfile_open(&init_user_ns, &parentpath, S_IFREG,
455+
O_RDWR | O_LARGEFILE | O_DIRECT,
456+
cache->cache_cred);
457+
ret = PTR_ERR_OR_ZERO(file);
457458
}
458459
if (ret) {
459460
trace_cachefiles_vfs_error(object, d_inode(fan), ret,
@@ -463,10 +464,10 @@ struct file *cachefiles_create_tmpfile(struct cachefiles_object *object)
463464
goto err;
464465
}
465466

466-
trace_cachefiles_tmpfile(object, d_backing_inode(path.dentry));
467+
trace_cachefiles_tmpfile(object, file_inode(file));
467468

468469
/* This is a newly created file with no other possible user */
469-
if (!cachefiles_mark_inode_in_use(object, d_inode(path.dentry)))
470+
if (!cachefiles_mark_inode_in_use(object, file_inode(file)))
470471
WARN_ON(1);
471472

472473
ret = cachefiles_ondemand_init_object(object);
@@ -477,42 +478,33 @@ struct file *cachefiles_create_tmpfile(struct cachefiles_object *object)
477478
ni_size = round_up(ni_size, CACHEFILES_DIO_BLOCK_SIZE);
478479

479480
if (ni_size > 0) {
480-
trace_cachefiles_trunc(object, d_backing_inode(path.dentry), 0, ni_size,
481+
trace_cachefiles_trunc(object, file_inode(file), 0, ni_size,
481482
cachefiles_trunc_expand_tmpfile);
482483
ret = cachefiles_inject_write_error();
483484
if (ret == 0)
484-
ret = vfs_truncate(&path, ni_size);
485+
ret = vfs_truncate(&file->f_path, ni_size);
485486
if (ret < 0) {
486487
trace_cachefiles_vfs_error(
487-
object, d_backing_inode(path.dentry), ret,
488+
object, file_inode(file), ret,
488489
cachefiles_trace_trunc_error);
489490
goto err_unuse;
490491
}
491492
}
492493

493-
file = open_with_fake_path(&path, O_RDWR | O_LARGEFILE | O_DIRECT,
494-
d_backing_inode(path.dentry), cache->cache_cred);
495-
ret = PTR_ERR(file);
496-
if (IS_ERR(file)) {
497-
trace_cachefiles_vfs_error(object, d_backing_inode(path.dentry),
498-
ret, cachefiles_trace_open_error);
499-
goto err_unuse;
500-
}
501494
ret = -EINVAL;
502495
if (unlikely(!file->f_op->read_iter) ||
503496
unlikely(!file->f_op->write_iter)) {
504497
fput(file);
505498
pr_notice("Cache does not support read_iter and write_iter\n");
506499
goto err_unuse;
507500
}
508-
dput(path.dentry);
509501
out:
510502
cachefiles_end_secure(cache, saved_cred);
511503
return file;
512504

513505
err_unuse:
514-
cachefiles_do_unmark_inode_in_use(object, d_inode(path.dentry));
515-
dput(path.dentry);
506+
cachefiles_do_unmark_inode_in_use(object, file_inode(file));
507+
fput(file);
516508
err:
517509
file = ERR_PTR(ret);
518510
goto out;

0 commit comments

Comments
 (0)