From: Jeffle Xu jefflexu@linux.alibaba.com
anolis inclusion category: feature bugzilla: https://gitee.com/openeuler/kernel/issues/IB5UKT
Reference: https://gitee.com/anolis/cloud-kernel/commit/d665b925541c
--------------------------------
ANBZ: #1666
commit 324b954ac80cff0d11ddb6bde9b6631e45e98620 upstream.
Notify the user daemon that cookie is going to be withdrawn, providing a hint that the associated anonymous fd can be closed.
Be noted that this is only a hint. The user daemon may close the associated anonymous fd when receiving the CLOSE request, then it will receive another anonymous fd when the cookie gets looked up. Or it may ignore the CLOSE request, and keep writing data through the anonymous fd. However the next time the cookie gets looked up, the user daemon will still receive another new anonymous fd.
Signed-off-by: Jeffle Xu jefflexu@linux.alibaba.com Acked-by: David Howells dhowells@redhat.com Link: https://lore.kernel.org/r/20220425122143.56815-5-jefflexu@linux.alibaba.com Signed-off-by: Gao Xiang hsiangkao@linux.alibaba.com Signed-off-by: Huang Jianan jnhuang@linux.alibaba.com Reviewed-by: Gao Xiang hsiangkao@linux.alibaba.com Reviewed-by: Jeffle Xu jefflexu@linux.alibaba.com Signed-off-by: Baokun Li libaokun1@huawei.com --- fs/cachefiles/interface.c | 2 ++ fs/cachefiles/internal.h | 5 +++++ fs/cachefiles/ondemand.c | 40 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+)
diff --git a/fs/cachefiles/interface.c b/fs/cachefiles/interface.c index 4cea5fbf695e..99f50bc59b3c 100644 --- a/fs/cachefiles/interface.c +++ b/fs/cachefiles/interface.c @@ -280,6 +280,8 @@ static void cachefiles_drop_object(struct fscache_object *_object) ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000); #endif
+ cachefiles_ondemand_clean_object(object); + /* We need to tidy the object up if we did in fact manage to open it. * It's possible for us to get here before the object is fully * initialised if the parent goes away or the object gets retired diff --git a/fs/cachefiles/internal.h b/fs/cachefiles/internal.h index 52188b42081f..c09a405ef42b 100644 --- a/fs/cachefiles/internal.h +++ b/fs/cachefiles/internal.h @@ -257,6 +257,7 @@ extern int cachefiles_ondemand_copen(struct cachefiles_cache *cache, char *args);
extern int cachefiles_ondemand_init_object(struct cachefiles_object *object); +extern void cachefiles_ondemand_clean_object(struct cachefiles_object *object);
#else static inline ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache, @@ -269,6 +270,10 @@ static inline int cachefiles_ondemand_init_object(struct cachefiles_object *obje { return 0; } + +static inline void cachefiles_ondemand_clean_object(struct cachefiles_object *object) +{ +} #endif
/* diff --git a/fs/cachefiles/ondemand.c b/fs/cachefiles/ondemand.c index d134f22e3818..26bdcbba92c5 100644 --- a/fs/cachefiles/ondemand.c +++ b/fs/cachefiles/ondemand.c @@ -241,6 +241,14 @@ ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache, ret = -EFAULT; goto err_put_fd; } + + /* CLOSE request has no reply */ + if (msg->opcode == CACHEFILES_OP_CLOSE) { + xa_lock(&cache->reqs); + radix_tree_delete(&cache->reqs, id); + xa_unlock(&cache->reqs); + complete(&req->done); + } return n; } xa_unlock(&cache->reqs); @@ -321,6 +329,13 @@ static int cachefiles_ondemand_send_req(struct cachefiles_object *object, /* coupled with the barrier in cachefiles_flush_reqs() */ smp_mb();
+ if (opcode != CACHEFILES_OP_OPEN && object->ondemand_id <= 0) { + WARN_ON_ONCE(object->ondemand_id == 0); + xa_unlock(&cache->reqs); + ret = -EIO; + goto out; + } + while (radix_tree_insert(&cache->reqs, id = atomic64_read(&global_index), req)) atomic64_inc(&global_index); @@ -368,6 +383,25 @@ static int cachefiles_ondemand_init_open_req(struct cachefiles_req *req, return 0; }
+static int cachefiles_ondemand_init_close_req(struct cachefiles_req *req, + void *private) +{ + struct cachefiles_object *object = req->object; + int object_id = object->ondemand_id; + + /* + * It's possible that object id is still 0 if the cookie looking up + * phase failed before OPEN request has ever been sent. Also avoid + * sending CLOSE request for CACHEFILES_ONDEMAND_ID_CLOSED, which means + * anon_fd has already been closed. + */ + if (object_id <= 0) + return -ENOENT; + + req->msg.object_id = object_id; + return 0; +} + int cachefiles_ondemand_init_object(struct cachefiles_object *object) { struct fscache_cookie *cookie = object->fscache.cookie; @@ -389,3 +423,9 @@ int cachefiles_ondemand_init_object(struct cachefiles_object *object) return cachefiles_ondemand_send_req(object, CACHEFILES_OP_OPEN, data_len, cachefiles_ondemand_init_open_req, NULL); } + +void cachefiles_ondemand_clean_object(struct cachefiles_object *object) +{ + cachefiles_ondemand_send_req(object, CACHEFILES_OP_CLOSE, 0, + cachefiles_ondemand_init_close_req, NULL); +}