Skip to content

Commit fc812c4

Browse files
author
Al Viro
committed
ecryptfs: get rid of pointless mount references in ecryptfs dentries
->lower_path.mnt has the same value for all dentries on given ecryptfs instance and if somebody goes for mountpoint-crossing variant where that would not be true, we can deal with that when it happens (and _not_ with duplicating these reference into each dentry). As it is, we are better off just sticking a reference into ecryptfs-private part of superblock and keeping it pinned until ->kill_sb(). That way we can stick a reference to underlying dentry right into ->d_fsdata of ecryptfs one, getting rid of indirection through struct ecryptfs_dentry_info, along with the entire struct ecryptfs_dentry_info machinery. [kudos to Dan Carpenter for spotting a bug in ecryptfs_get_tree() part] Reviewed-by: Christian Brauner <brauner@kernel.org> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 75db7fd commit fc812c4

5 files changed

Lines changed: 30 additions & 69 deletions

File tree

fs/ecryptfs/dentry.c

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,6 @@ static int ecryptfs_d_revalidate(struct inode *dir, const struct qstr *name,
5959
return rc;
6060
}
6161

62-
struct kmem_cache *ecryptfs_dentry_info_cache;
63-
64-
static void ecryptfs_dentry_free_rcu(struct rcu_head *head)
65-
{
66-
kmem_cache_free(ecryptfs_dentry_info_cache,
67-
container_of(head, struct ecryptfs_dentry_info, rcu));
68-
}
69-
7062
/**
7163
* ecryptfs_d_release
7264
* @dentry: The ecryptfs dentry
@@ -75,11 +67,7 @@ static void ecryptfs_dentry_free_rcu(struct rcu_head *head)
7567
*/
7668
static void ecryptfs_d_release(struct dentry *dentry)
7769
{
78-
struct ecryptfs_dentry_info *p = dentry->d_fsdata;
79-
if (p) {
80-
path_put(&p->lower_path);
81-
call_rcu(&p->rcu, ecryptfs_dentry_free_rcu);
82-
}
70+
dput(dentry->d_fsdata);
8371
}
8472

8573
const struct dentry_operations ecryptfs_dops = {

fs/ecryptfs/ecryptfs_kernel.h

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,6 @@ struct ecryptfs_inode_info {
258258
struct ecryptfs_crypt_stat crypt_stat;
259259
};
260260

261-
/* dentry private data. Each dentry must keep track of a lower
262-
* vfsmount too. */
263-
struct ecryptfs_dentry_info {
264-
struct path lower_path;
265-
struct rcu_head rcu;
266-
};
267-
268261
/**
269262
* ecryptfs_global_auth_tok - A key used to encrypt all new files under the mountpoint
270263
* @flags: Status flags
@@ -348,6 +341,7 @@ struct ecryptfs_mount_crypt_stat {
348341
/* superblock private data. */
349342
struct ecryptfs_sb_info {
350343
struct super_block *wsi_sb;
344+
struct vfsmount *lower_mnt;
351345
struct ecryptfs_mount_crypt_stat mount_crypt_stat;
352346
};
353347

@@ -494,22 +488,25 @@ ecryptfs_set_superblock_lower(struct super_block *sb,
494488
}
495489

496490
static inline void
497-
ecryptfs_set_dentry_private(struct dentry *dentry,
498-
struct ecryptfs_dentry_info *dentry_info)
491+
ecryptfs_set_dentry_lower(struct dentry *dentry,
492+
struct dentry *lower_dentry)
499493
{
500-
dentry->d_fsdata = dentry_info;
494+
dentry->d_fsdata = lower_dentry;
501495
}
502496

503497
static inline struct dentry *
504498
ecryptfs_dentry_to_lower(struct dentry *dentry)
505499
{
506-
return ((struct ecryptfs_dentry_info *)dentry->d_fsdata)->lower_path.dentry;
500+
return dentry->d_fsdata;
507501
}
508502

509-
static inline const struct path *
510-
ecryptfs_dentry_to_lower_path(struct dentry *dentry)
503+
static inline struct path
504+
ecryptfs_lower_path(struct dentry *dentry)
511505
{
512-
return &((struct ecryptfs_dentry_info *)dentry->d_fsdata)->lower_path;
506+
return (struct path){
507+
.mnt = ecryptfs_superblock_to_private(dentry->d_sb)->lower_mnt,
508+
.dentry = ecryptfs_dentry_to_lower(dentry)
509+
};
513510
}
514511

515512
#define ecryptfs_printk(type, fmt, arg...) \
@@ -532,7 +529,6 @@ extern unsigned int ecryptfs_number_of_users;
532529

533530
extern struct kmem_cache *ecryptfs_auth_tok_list_item_cache;
534531
extern struct kmem_cache *ecryptfs_file_info_cache;
535-
extern struct kmem_cache *ecryptfs_dentry_info_cache;
536532
extern struct kmem_cache *ecryptfs_inode_info_cache;
537533
extern struct kmem_cache *ecryptfs_sb_info_cache;
538534
extern struct kmem_cache *ecryptfs_header_cache;
@@ -557,7 +553,6 @@ int ecryptfs_encrypt_and_encode_filename(
557553
size_t *encoded_name_size,
558554
struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
559555
const char *name, size_t name_size);
560-
struct dentry *ecryptfs_lower_dentry(struct dentry *this_dentry);
561556
void ecryptfs_dump_hex(char *data, int bytes);
562557
int virt_to_scatterlist(const void *addr, int size, struct scatterlist *sg,
563558
int sg_size);

fs/ecryptfs/file.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,12 @@ static ssize_t ecryptfs_read_update_atime(struct kiocb *iocb,
3333
struct iov_iter *to)
3434
{
3535
ssize_t rc;
36-
const struct path *path;
3736
struct file *file = iocb->ki_filp;
3837

3938
rc = generic_file_read_iter(iocb, to);
4039
if (rc >= 0) {
41-
path = ecryptfs_dentry_to_lower_path(file->f_path.dentry);
42-
touch_atime(path);
40+
struct path path = ecryptfs_lower_path(file->f_path.dentry);
41+
touch_atime(&path);
4342
}
4443
return rc;
4544
}
@@ -59,12 +58,11 @@ static ssize_t ecryptfs_splice_read_update_atime(struct file *in, loff_t *ppos,
5958
size_t len, unsigned int flags)
6059
{
6160
ssize_t rc;
62-
const struct path *path;
6361

6462
rc = filemap_splice_read(in, ppos, pipe, len, flags);
6563
if (rc >= 0) {
66-
path = ecryptfs_dentry_to_lower_path(in->f_path.dentry);
67-
touch_atime(path);
64+
struct path path = ecryptfs_lower_path(in->f_path.dentry);
65+
touch_atime(&path);
6866
}
6967
return rc;
7068
}
@@ -283,6 +281,7 @@ static int ecryptfs_dir_open(struct inode *inode, struct file *file)
283281
* ecryptfs_lookup() */
284282
struct ecryptfs_file_info *file_info;
285283
struct file *lower_file;
284+
struct path path;
286285

287286
/* Released in ecryptfs_release or end of function if failure */
288287
file_info = kmem_cache_zalloc(ecryptfs_file_info_cache, GFP_KERNEL);
@@ -292,8 +291,8 @@ static int ecryptfs_dir_open(struct inode *inode, struct file *file)
292291
"Error attempting to allocate memory\n");
293292
return -ENOMEM;
294293
}
295-
lower_file = dentry_open(ecryptfs_dentry_to_lower_path(ecryptfs_dentry),
296-
file->f_flags, current_cred());
294+
path = ecryptfs_lower_path(ecryptfs_dentry);
295+
lower_file = dentry_open(&path, file->f_flags, current_cred());
297296
if (IS_ERR(lower_file)) {
298297
printk(KERN_ERR "%s: Error attempting to initialize "
299298
"the lower file for the dentry with name "

fs/ecryptfs/inode.c

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -327,24 +327,15 @@ static int ecryptfs_i_size_read(struct dentry *dentry, struct inode *inode)
327327
static struct dentry *ecryptfs_lookup_interpose(struct dentry *dentry,
328328
struct dentry *lower_dentry)
329329
{
330-
const struct path *path = ecryptfs_dentry_to_lower_path(dentry->d_parent);
330+
struct dentry *lower_parent = ecryptfs_dentry_to_lower(dentry->d_parent);
331331
struct inode *inode, *lower_inode;
332-
struct ecryptfs_dentry_info *dentry_info;
333332
int rc = 0;
334333

335-
dentry_info = kmem_cache_alloc(ecryptfs_dentry_info_cache, GFP_KERNEL);
336-
if (!dentry_info) {
337-
dput(lower_dentry);
338-
return ERR_PTR(-ENOMEM);
339-
}
340-
341334
fsstack_copy_attr_atime(d_inode(dentry->d_parent),
342-
d_inode(path->dentry));
335+
d_inode(lower_parent));
343336
BUG_ON(!d_count(lower_dentry));
344337

345-
ecryptfs_set_dentry_private(dentry, dentry_info);
346-
dentry_info->lower_path.mnt = mntget(path->mnt);
347-
dentry_info->lower_path.dentry = lower_dentry;
338+
ecryptfs_set_dentry_lower(dentry, lower_dentry);
348339

349340
/*
350341
* negative dentry can go positive under us here - its parent is not
@@ -1022,10 +1013,10 @@ static int ecryptfs_getattr(struct mnt_idmap *idmap,
10221013
{
10231014
struct dentry *dentry = path->dentry;
10241015
struct kstat lower_stat;
1016+
struct path lower_path = ecryptfs_lower_path(dentry);
10251017
int rc;
10261018

1027-
rc = vfs_getattr_nosec(ecryptfs_dentry_to_lower_path(dentry),
1028-
&lower_stat, request_mask, flags);
1019+
rc = vfs_getattr_nosec(&lower_path, &lower_stat, request_mask, flags);
10291020
if (!rc) {
10301021
fsstack_copy_attr_all(d_inode(dentry),
10311022
ecryptfs_inode_to_lower(d_inode(dentry)));

fs/ecryptfs/main.c

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,14 @@ static int ecryptfs_init_lower_file(struct dentry *dentry,
106106
struct file **lower_file)
107107
{
108108
const struct cred *cred = current_cred();
109-
const struct path *path = ecryptfs_dentry_to_lower_path(dentry);
109+
struct path path = ecryptfs_lower_path(dentry);
110110
int rc;
111111

112-
rc = ecryptfs_privileged_open(lower_file, path->dentry, path->mnt,
113-
cred);
112+
rc = ecryptfs_privileged_open(lower_file, path.dentry, path.mnt, cred);
114113
if (rc) {
115114
printk(KERN_ERR "Error opening lower file "
116115
"for lower_dentry [0x%p] and lower_mnt [0x%p]; "
117-
"rc = [%d]\n", path->dentry, path->mnt, rc);
116+
"rc = [%d]\n", path.dentry, path.mnt, rc);
118117
(*lower_file) = NULL;
119118
}
120119
return rc;
@@ -437,7 +436,6 @@ static int ecryptfs_get_tree(struct fs_context *fc)
437436
struct ecryptfs_fs_context *ctx = fc->fs_private;
438437
struct ecryptfs_sb_info *sbi = fc->s_fs_info;
439438
struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
440-
struct ecryptfs_dentry_info *root_info;
441439
const char *err = "Getting sb failed";
442440
struct inode *inode;
443441
struct path path;
@@ -543,14 +541,8 @@ static int ecryptfs_get_tree(struct fs_context *fc)
543541
goto out_free;
544542
}
545543

546-
rc = -ENOMEM;
547-
root_info = kmem_cache_zalloc(ecryptfs_dentry_info_cache, GFP_KERNEL);
548-
if (!root_info)
549-
goto out_free;
550-
551-
/* ->kill_sb() will take care of root_info */
552-
ecryptfs_set_dentry_private(s->s_root, root_info);
553-
root_info->lower_path = path;
544+
ecryptfs_set_dentry_lower(s->s_root, path.dentry);
545+
ecryptfs_superblock_to_private(s)->lower_mnt = path.mnt;
554546

555547
s->s_flags |= SB_ACTIVE;
556548
fc->root = dget(s->s_root);
@@ -580,6 +572,7 @@ static void ecryptfs_kill_block_super(struct super_block *sb)
580572
kill_anon_super(sb);
581573
if (!sb_info)
582574
return;
575+
mntput(sb_info->lower_mnt);
583576
ecryptfs_destroy_mount_crypt_stat(&sb_info->mount_crypt_stat);
584577
kmem_cache_free(ecryptfs_sb_info_cache, sb_info);
585578
}
@@ -667,11 +660,6 @@ static struct ecryptfs_cache_info {
667660
.name = "ecryptfs_file_cache",
668661
.size = sizeof(struct ecryptfs_file_info),
669662
},
670-
{
671-
.cache = &ecryptfs_dentry_info_cache,
672-
.name = "ecryptfs_dentry_info_cache",
673-
.size = sizeof(struct ecryptfs_dentry_info),
674-
},
675663
{
676664
.cache = &ecryptfs_inode_info_cache,
677665
.name = "ecryptfs_inode_cache",

0 commit comments

Comments
 (0)