Skip to content

Commit 3c5ecfe

Browse files
Jia Zhubrauner
authored andcommitted
cachefiles: extract ondemand info field from cachefiles_object
We'll introduce a @work_struct field for @object in subsequent patches, it will enlarge the size of @object. As the result of that, this commit extracts ondemand info field from @object. Signed-off-by: Jia Zhu <zhujia.zj@bytedance.com> Link: https://lore.kernel.org/r/20231120041422.75170-3-zhujia.zj@bytedance.com Reviewed-by: Jingbo Xu <jefflexu@linux.alibaba.com> Reviewed-by: David Howells <dhowells@redhat.com> Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent 357a18d commit 3c5ecfe

3 files changed

Lines changed: 56 additions & 11 deletions

File tree

fs/cachefiles/interface.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ struct cachefiles_object *cachefiles_alloc_object(struct fscache_cookie *cookie)
3131
if (!object)
3232
return NULL;
3333

34+
if (cachefiles_ondemand_init_obj_info(object, volume)) {
35+
kmem_cache_free(cachefiles_object_jar, object);
36+
return NULL;
37+
}
38+
3439
refcount_set(&object->ref, 1);
3540

3641
spin_lock_init(&object->lock);
@@ -88,7 +93,7 @@ void cachefiles_put_object(struct cachefiles_object *object,
8893
ASSERTCMP(object->file, ==, NULL);
8994

9095
kfree(object->d_name);
91-
96+
cachefiles_ondemand_deinit_obj_info(object);
9297
cache = object->volume->cache->cache;
9398
fscache_put_cookie(object->cookie, fscache_cookie_put_object);
9499
object->cookie = NULL;

fs/cachefiles/internal.h

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ enum cachefiles_object_state {
4949
CACHEFILES_ONDEMAND_OBJSTATE_OPEN, /* Anonymous fd associated with object is available */
5050
};
5151

52+
struct cachefiles_ondemand_info {
53+
int ondemand_id;
54+
enum cachefiles_object_state state;
55+
struct cachefiles_object *object;
56+
};
57+
5258
/*
5359
* Backing file state.
5460
*/
@@ -66,8 +72,7 @@ struct cachefiles_object {
6672
unsigned long flags;
6773
#define CACHEFILES_OBJECT_USING_TMPFILE 0 /* Have an unlinked tmpfile */
6874
#ifdef CONFIG_CACHEFILES_ONDEMAND
69-
int ondemand_id;
70-
enum cachefiles_object_state state;
75+
struct cachefiles_ondemand_info *ondemand;
7176
#endif
7277
};
7378

@@ -302,17 +307,21 @@ extern void cachefiles_ondemand_clean_object(struct cachefiles_object *object);
302307
extern int cachefiles_ondemand_read(struct cachefiles_object *object,
303308
loff_t pos, size_t len);
304309

310+
extern int cachefiles_ondemand_init_obj_info(struct cachefiles_object *obj,
311+
struct cachefiles_volume *volume);
312+
extern void cachefiles_ondemand_deinit_obj_info(struct cachefiles_object *obj);
313+
305314
#define CACHEFILES_OBJECT_STATE_FUNCS(_state, _STATE) \
306315
static inline bool \
307316
cachefiles_ondemand_object_is_##_state(const struct cachefiles_object *object) \
308317
{ \
309-
return object->state == CACHEFILES_ONDEMAND_OBJSTATE_##_STATE; \
318+
return object->ondemand->state == CACHEFILES_ONDEMAND_OBJSTATE_##_STATE; \
310319
} \
311320
\
312321
static inline void \
313322
cachefiles_ondemand_set_object_##_state(struct cachefiles_object *object) \
314323
{ \
315-
object->state = CACHEFILES_ONDEMAND_OBJSTATE_##_STATE; \
324+
object->ondemand->state = CACHEFILES_ONDEMAND_OBJSTATE_##_STATE; \
316325
}
317326

318327
CACHEFILES_OBJECT_STATE_FUNCS(open, OPEN);
@@ -338,6 +347,15 @@ static inline int cachefiles_ondemand_read(struct cachefiles_object *object,
338347
{
339348
return -EOPNOTSUPP;
340349
}
350+
351+
static inline int cachefiles_ondemand_init_obj_info(struct cachefiles_object *obj,
352+
struct cachefiles_volume *volume)
353+
{
354+
return 0;
355+
}
356+
static inline void cachefiles_ondemand_deinit_obj_info(struct cachefiles_object *obj)
357+
{
358+
}
341359
#endif
342360

343361
/*

fs/cachefiles/ondemand.c

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ static int cachefiles_ondemand_fd_release(struct inode *inode,
99
{
1010
struct cachefiles_object *object = file->private_data;
1111
struct cachefiles_cache *cache = object->volume->cache;
12-
int object_id = object->ondemand_id;
12+
struct cachefiles_ondemand_info *info = object->ondemand;
13+
int object_id = info->ondemand_id;
1314
struct cachefiles_req *req;
1415
XA_STATE(xas, &cache->reqs, 0);
1516

1617
xa_lock(&cache->reqs);
17-
object->ondemand_id = CACHEFILES_ONDEMAND_ID_CLOSED;
18+
info->ondemand_id = CACHEFILES_ONDEMAND_ID_CLOSED;
1819
cachefiles_ondemand_set_object_close(object);
1920

2021
/*
@@ -222,7 +223,7 @@ static int cachefiles_ondemand_get_fd(struct cachefiles_req *req)
222223
load = (void *)req->msg.data;
223224
load->fd = fd;
224225
req->msg.object_id = object_id;
225-
object->ondemand_id = object_id;
226+
object->ondemand->ondemand_id = object_id;
226227

227228
cachefiles_get_unbind_pincount(cache);
228229
trace_cachefiles_ondemand_open(object, &req->msg, load);
@@ -368,7 +369,7 @@ static int cachefiles_ondemand_send_req(struct cachefiles_object *object,
368369

369370
if (opcode != CACHEFILES_OP_OPEN &&
370371
!cachefiles_ondemand_object_is_open(object)) {
371-
WARN_ON_ONCE(object->ondemand_id == 0);
372+
WARN_ON_ONCE(object->ondemand->ondemand_id == 0);
372373
xas_unlock(&xas);
373374
ret = -EIO;
374375
goto out;
@@ -438,7 +439,7 @@ static int cachefiles_ondemand_init_close_req(struct cachefiles_req *req,
438439
if (!cachefiles_ondemand_object_is_open(object))
439440
return -ENOENT;
440441

441-
req->msg.object_id = object->ondemand_id;
442+
req->msg.object_id = object->ondemand->ondemand_id;
442443
trace_cachefiles_ondemand_close(object, &req->msg);
443444
return 0;
444445
}
@@ -454,7 +455,7 @@ static int cachefiles_ondemand_init_read_req(struct cachefiles_req *req,
454455
struct cachefiles_object *object = req->object;
455456
struct cachefiles_read *load = (void *)req->msg.data;
456457
struct cachefiles_read_ctx *read_ctx = private;
457-
int object_id = object->ondemand_id;
458+
int object_id = object->ondemand->ondemand_id;
458459

459460
/* Stop enqueuing requests when daemon has closed anon_fd. */
460461
if (!cachefiles_ondemand_object_is_open(object)) {
@@ -500,6 +501,27 @@ void cachefiles_ondemand_clean_object(struct cachefiles_object *object)
500501
cachefiles_ondemand_init_close_req, NULL);
501502
}
502503

504+
int cachefiles_ondemand_init_obj_info(struct cachefiles_object *object,
505+
struct cachefiles_volume *volume)
506+
{
507+
if (!cachefiles_in_ondemand_mode(volume->cache))
508+
return 0;
509+
510+
object->ondemand = kzalloc(sizeof(struct cachefiles_ondemand_info),
511+
GFP_KERNEL);
512+
if (!object->ondemand)
513+
return -ENOMEM;
514+
515+
object->ondemand->object = object;
516+
return 0;
517+
}
518+
519+
void cachefiles_ondemand_deinit_obj_info(struct cachefiles_object *object)
520+
{
521+
kfree(object->ondemand);
522+
object->ondemand = NULL;
523+
}
524+
503525
int cachefiles_ondemand_read(struct cachefiles_object *object,
504526
loff_t pos, size_t len)
505527
{

0 commit comments

Comments
 (0)