From 21d44303a978c64a1ab7522e035e041ec3b45d1b Mon Sep 17 00:00:00 2001 From: jaychao Date: Sat, 16 Dec 2023 12:33:00 +0000 Subject: [PATCH] fix cloud dentry cred Signed-off-by: jaychao --- fs/hmdfs/dentry.c | 29 ++++++++++++++++++++--------- fs/hmdfs/file_cloud.c | 10 +++++++++- fs/hmdfs/hmdfs_dentryfile.c | 11 +++-------- fs/hmdfs/hmdfs_dentryfile.h | 4 ++-- fs/hmdfs/hmdfs_dentryfile_cloud.c | 16 ++++++++++++++-- 5 files changed, 48 insertions(+), 22 deletions(-) diff --git a/fs/hmdfs/dentry.c b/fs/hmdfs/dentry.c index 040d698e1785..2b885a966093 100644 --- a/fs/hmdfs/dentry.c +++ b/fs/hmdfs/dentry.c @@ -8,6 +8,7 @@ #include #include +#include "authority/authentication.h" #include "comm/connection.h" #include "hmdfs_dentryfile.h" #include "hmdfs_device_view.h" @@ -167,9 +168,26 @@ static int hmdfs_d_revalidate(struct dentry *direntry, unsigned int flags) return ret; } -static void hmdfs_dev_d_release(struct dentry *dentry) +static void hmdfs_close_cloud_dents(struct dentry *dentry) { + struct hmdfs_sb_info *sbi = hmdfs_sb(dentry->d_sb); struct clearcache_item *item; + const struct cred *old_cred; + + item = hmdfs_find_cache_item(CLOUD_DEVICE, dentry); + if (item) { + /* cloud dentryfile didn't link to + 'struct cache_file_node', so close file here. + */ + old_cred = hmdfs_override_creds(sbi->cred); + filp_close(item->filp, NULL); + kref_put(&item->ref, release_cache_item); + hmdfs_revert_creds(old_cred); + } +} + +static void hmdfs_dev_d_release(struct dentry *dentry) +{ if (!dentry || !dentry->d_fsdata) return; @@ -188,14 +206,7 @@ static void hmdfs_dev_d_release(struct dentry *dentry) hmdfs_clear_cache_dents(dentry, false); break; case HMDFS_LAYER_SECOND_CLOUD: - item = hmdfs_find_cache_item(CLOUD_DEVICE, dentry); - if (item) { - /* cloud dentryfile didn't link to - 'struct cache_file_node', so close file here. - */ - filp_close(item->filp, NULL); - kref_put(&item->ref, release_cache_item); - } + hmdfs_close_cloud_dents(dentry); hmdfs_clear_cache_dents(dentry, false); break; default: diff --git a/fs/hmdfs/file_cloud.c b/fs/hmdfs/file_cloud.c index b7386be449d4..802fc6a518e7 100644 --- a/fs/hmdfs/file_cloud.c +++ b/fs/hmdfs/file_cloud.c @@ -291,6 +291,14 @@ const struct address_space_operations hmdfs_aops_cloud = { .readpages = hmdfs_readpages_cloud, }; +static int hmdfs_metainfo_read_cloud(struct hmdfs_sb_info *sbi, struct file *filp, + void *buffer, int size, int bidx) +{ + loff_t pos = get_dentry_group_pos(bidx); + + return cache_file_read_cloud(sbi, filp, buffer, (size_t)size, &pos); +} + int analysis_dentry_file_from_cloud(struct hmdfs_sb_info *sbi, struct file *file, struct file *handler, struct dir_context *ctx) @@ -324,7 +332,7 @@ int analysis_dentry_file_from_cloud(struct hmdfs_sb_info *sbi, } for (i = group_id; i < group_num; i++) { - int ret = hmdfs_metainfo_read_nocred(handler, dentry_group, + int ret = hmdfs_metainfo_read_cloud(sbi, handler, dentry_group, sizeof(struct hmdfs_dentry_group_cloud), i); if (ret != sizeof(struct hmdfs_dentry_group_cloud)) { diff --git a/fs/hmdfs/hmdfs_dentryfile.c b/fs/hmdfs/hmdfs_dentryfile.c index 5ec0980b7bf3..c79f29da659e 100644 --- a/fs/hmdfs/hmdfs_dentryfile.c +++ b/fs/hmdfs/hmdfs_dentryfile.c @@ -402,14 +402,6 @@ char *hmdfs_connect_path(const char *path, const char *name) return buf; } -int hmdfs_metainfo_read_nocred(struct file *filp, - void *buffer, int size, int bidx) -{ - loff_t pos = get_dentry_group_pos(bidx); - - return kernel_read(filp, buffer, (size_t)size, &pos); -} - int hmdfs_metainfo_read(struct hmdfs_sb_info *sbi, struct file *filp, void *buffer, int size, int bidx) { @@ -1416,6 +1408,7 @@ int get_cloud_cache_file(struct dentry *dentry, struct hmdfs_sb_info *sbi) char *fullname = NULL; char *cache_file_name = NULL; char *kvalue = NULL; + const struct cred *old_cred; item = hmdfs_find_cache_item(CLOUD_DEVICE, dentry); if (item) { @@ -1461,6 +1454,7 @@ int get_cloud_cache_file(struct dentry *dentry, struct hmdfs_sb_info *sbi) sbi->cache_dir); snprintf(fullname, PATH_MAX, "%s%s", dirname, cache_file_name); + old_cred = hmdfs_override_creds(sbi->cred); filp = filp_open(fullname, O_RDWR | O_LARGEFILE, 0); if (IS_ERR(filp)) { hmdfs_debug("open fail %ld", PTR_ERR(filp)); @@ -1491,6 +1485,7 @@ int get_cloud_cache_file(struct dentry *dentry, struct hmdfs_sb_info *sbi) ret = 0; out: + hmdfs_revert_creds(old_cred); kfree(relative_path); kfree(dirname); kfree(fullname); diff --git a/fs/hmdfs/hmdfs_dentryfile.h b/fs/hmdfs/hmdfs_dentryfile.h index b3907ce1b86e..61e8bcde9f63 100644 --- a/fs/hmdfs/hmdfs_dentryfile.h +++ b/fs/hmdfs/hmdfs_dentryfile.h @@ -287,8 +287,8 @@ ssize_t cache_file_read(struct hmdfs_sb_info *sbi, struct file *filp, void *buf, size_t count, loff_t *pos); ssize_t cache_file_write(struct hmdfs_sb_info *sbi, struct file *filp, const void *buf, size_t count, loff_t *pos); -int hmdfs_metainfo_read_nocred(struct file *filp, - void *buffer, int size, int bidx); +ssize_t cache_file_read_cloud(struct hmdfs_sb_info *sbi, struct file *filp, + void *buf, size_t count, loff_t *pos); int hmdfs_metainfo_read(struct hmdfs_sb_info *sbi, struct file *filp, void *buffer, int buffersize, int bidx); diff --git a/fs/hmdfs/hmdfs_dentryfile_cloud.c b/fs/hmdfs/hmdfs_dentryfile_cloud.c index 4e7dd009913d..b90f90da8b41 100644 --- a/fs/hmdfs/hmdfs_dentryfile_cloud.c +++ b/fs/hmdfs/hmdfs_dentryfile_cloud.c @@ -8,6 +8,7 @@ #include "hmdfs_dentryfile_cloud.h" #include +#include "authority/authentication.h" void hmdfs_init_dcache_lookup_ctx_cloud( struct hmdfs_dcache_lookup_ctx_cloud *ctx, struct hmdfs_sb_info *sbi, @@ -23,6 +24,17 @@ void hmdfs_init_dcache_lookup_ctx_cloud( ctx->insense_page = NULL; } +ssize_t cache_file_read_cloud(struct hmdfs_sb_info *sbi, struct file *filp, + void *buf, size_t count, loff_t *pos) +{ + const struct cred *old_cred = hmdfs_override_creds(sbi->cred); + ssize_t ret = kernel_read(filp, buf, count, pos); + + hmdfs_revert_creds(old_cred); + + return ret; +} + static struct hmdfs_dentry_group_cloud *find_dentry_page(struct hmdfs_sb_info *sbi, pgoff_t index, struct file *filp) { @@ -42,8 +54,8 @@ static struct hmdfs_dentry_group_cloud *find_dentry_page(struct hmdfs_sb_info *s return NULL; } - size = kernel_read(filp, dentry_blk, (size_t)DENTRYGROUP_SIZE, - &pos); + size = cache_file_read_cloud(sbi, filp, dentry_blk, (size_t)DENTRYGROUP_SIZE, + &pos); if (size != DENTRYGROUP_SIZE) { hmdfs_err("read pos %lld failed %d", pos, size); hmdfs_unlock_file(filp, pos, DENTRYGROUP_SIZE); -- Gitee