Skip to content

Commit 9032b6e

Browse files
lostjefflehsiangkao
authored andcommitted
cachefiles: implement on-demand read
Implement the data plane of on-demand read mode. The early implementation [1] place the entry to cachefiles_ondemand_read() in fscache_read(). However, fscache_read() can only detect if the requested file range is fully cache miss, whilst we need to notify the user daemon as long as there's a hole inside the requested file range. Thus the entry is now placed in cachefiles_prepare_read(). When working in on-demand read mode, once a hole detected, the read routine will send a READ request to the user daemon. The user daemon needs to fetch the data and write it to the cache file. After sending the READ request, the read routine will hang there, until the READ request is handled by the user daemon. Then it will retry to read from the same file range. If no progress encountered, the read routine will fail then. A new NETFS_SREQ_ONDEMAND flag is introduced to indicate that on-demand read should be done when a cache miss encountered. [1] https://lore.kernel.org/all/[email protected]/ #v8 Signed-off-by: Jeffle Xu <[email protected]> Acked-by: David Howells <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Gao Xiang <[email protected]>
1 parent 324b954 commit 9032b6e

File tree

5 files changed

+117
-2
lines changed

5 files changed

+117
-2
lines changed

fs/cachefiles/internal.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,9 @@ extern int cachefiles_ondemand_copen(struct cachefiles_cache *cache,
292292
extern int cachefiles_ondemand_init_object(struct cachefiles_object *object);
293293
extern void cachefiles_ondemand_clean_object(struct cachefiles_object *object);
294294

295+
extern int cachefiles_ondemand_read(struct cachefiles_object *object,
296+
loff_t pos, size_t len);
297+
295298
#else
296299
static inline ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache,
297300
char __user *_buffer, size_t buflen)
@@ -307,6 +310,12 @@ static inline int cachefiles_ondemand_init_object(struct cachefiles_object *obje
307310
static inline void cachefiles_ondemand_clean_object(struct cachefiles_object *object)
308311
{
309312
}
313+
314+
static inline int cachefiles_ondemand_read(struct cachefiles_object *object,
315+
loff_t pos, size_t len)
316+
{
317+
return -EOPNOTSUPP;
318+
}
310319
#endif
311320

312321
/*

fs/cachefiles/io.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ static enum netfs_io_source cachefiles_prepare_read(struct netfs_io_subrequest *
403403
enum netfs_io_source ret = NETFS_DOWNLOAD_FROM_SERVER;
404404
loff_t off, to;
405405
ino_t ino = file ? file_inode(file)->i_ino : 0;
406+
int rc;
406407

407408
_enter("%zx @%llx/%llx", subreq->len, subreq->start, i_size);
408409

@@ -415,7 +416,8 @@ static enum netfs_io_source cachefiles_prepare_read(struct netfs_io_subrequest *
415416
if (test_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags)) {
416417
__set_bit(NETFS_SREQ_COPY_TO_CACHE, &subreq->flags);
417418
why = cachefiles_trace_read_no_data;
418-
goto out_no_object;
419+
if (!test_bit(NETFS_SREQ_ONDEMAND, &subreq->flags))
420+
goto out_no_object;
419421
}
420422

421423
/* The object and the file may be being created in the background. */
@@ -432,7 +434,7 @@ static enum netfs_io_source cachefiles_prepare_read(struct netfs_io_subrequest *
432434
object = cachefiles_cres_object(cres);
433435
cache = object->volume->cache;
434436
cachefiles_begin_secure(cache, &saved_cred);
435-
437+
retry:
436438
off = cachefiles_inject_read_error();
437439
if (off == 0)
438440
off = vfs_llseek(file, subreq->start, SEEK_DATA);
@@ -483,6 +485,15 @@ static enum netfs_io_source cachefiles_prepare_read(struct netfs_io_subrequest *
483485

484486
download_and_store:
485487
__set_bit(NETFS_SREQ_COPY_TO_CACHE, &subreq->flags);
488+
if (test_bit(NETFS_SREQ_ONDEMAND, &subreq->flags)) {
489+
rc = cachefiles_ondemand_read(object, subreq->start,
490+
subreq->len);
491+
if (!rc) {
492+
__clear_bit(NETFS_SREQ_ONDEMAND, &subreq->flags);
493+
goto retry;
494+
}
495+
ret = NETFS_INVALID_READ;
496+
}
486497
out:
487498
cachefiles_end_secure(cache, saved_cred);
488499
out_no_object:

fs/cachefiles/ondemand.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,25 @@ static int cachefiles_ondemand_fd_release(struct inode *inode,
1010
struct cachefiles_object *object = file->private_data;
1111
struct cachefiles_cache *cache = object->volume->cache;
1212
int object_id = object->ondemand_id;
13+
struct cachefiles_req *req;
14+
XA_STATE(xas, &cache->reqs, 0);
1315

16+
xa_lock(&cache->reqs);
1417
object->ondemand_id = CACHEFILES_ONDEMAND_ID_CLOSED;
18+
19+
/*
20+
* Flush all pending READ requests since their completion depends on
21+
* anon_fd.
22+
*/
23+
xas_for_each(&xas, req, ULONG_MAX) {
24+
if (req->msg.opcode == CACHEFILES_OP_READ) {
25+
req->error = -EIO;
26+
complete(&req->done);
27+
xas_store(&xas, NULL);
28+
}
29+
}
30+
xa_unlock(&cache->reqs);
31+
1532
xa_erase(&cache->ondemand_ids, object_id);
1633
cachefiles_put_object(object, cachefiles_obj_put_ondemand_fd);
1734
cachefiles_put_unbind_pincount(cache);
@@ -57,11 +74,35 @@ static loff_t cachefiles_ondemand_fd_llseek(struct file *filp, loff_t pos,
5774
return vfs_llseek(file, pos, whence);
5875
}
5976

77+
static long cachefiles_ondemand_fd_ioctl(struct file *filp, unsigned int ioctl,
78+
unsigned long arg)
79+
{
80+
struct cachefiles_object *object = filp->private_data;
81+
struct cachefiles_cache *cache = object->volume->cache;
82+
struct cachefiles_req *req;
83+
unsigned long id;
84+
85+
if (ioctl != CACHEFILES_IOC_READ_COMPLETE)
86+
return -EINVAL;
87+
88+
if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags))
89+
return -EOPNOTSUPP;
90+
91+
id = arg;
92+
req = xa_erase(&cache->reqs, id);
93+
if (!req)
94+
return -EINVAL;
95+
96+
complete(&req->done);
97+
return 0;
98+
}
99+
60100
static const struct file_operations cachefiles_ondemand_fd_fops = {
61101
.owner = THIS_MODULE,
62102
.release = cachefiles_ondemand_fd_release,
63103
.write_iter = cachefiles_ondemand_fd_write_iter,
64104
.llseek = cachefiles_ondemand_fd_llseek,
105+
.unlocked_ioctl = cachefiles_ondemand_fd_ioctl,
65106
};
66107

67108
/*
@@ -388,6 +429,32 @@ static int cachefiles_ondemand_init_close_req(struct cachefiles_req *req,
388429
return 0;
389430
}
390431

432+
struct cachefiles_read_ctx {
433+
loff_t off;
434+
size_t len;
435+
};
436+
437+
static int cachefiles_ondemand_init_read_req(struct cachefiles_req *req,
438+
void *private)
439+
{
440+
struct cachefiles_object *object = req->object;
441+
struct cachefiles_read *load = (void *)req->msg.data;
442+
struct cachefiles_read_ctx *read_ctx = private;
443+
int object_id = object->ondemand_id;
444+
445+
/* Stop enqueuing requests when daemon has closed anon_fd. */
446+
if (object_id <= 0) {
447+
WARN_ON_ONCE(object_id == 0);
448+
pr_info_once("READ: anonymous fd closed prematurely.\n");
449+
return -EIO;
450+
}
451+
452+
req->msg.object_id = object_id;
453+
load->off = read_ctx->off;
454+
load->len = read_ctx->len;
455+
return 0;
456+
}
457+
391458
int cachefiles_ondemand_init_object(struct cachefiles_object *object)
392459
{
393460
struct fscache_cookie *cookie = object->cookie;
@@ -417,3 +484,13 @@ void cachefiles_ondemand_clean_object(struct cachefiles_object *object)
417484
cachefiles_ondemand_send_req(object, CACHEFILES_OP_CLOSE, 0,
418485
cachefiles_ondemand_init_close_req, NULL);
419486
}
487+
488+
int cachefiles_ondemand_read(struct cachefiles_object *object,
489+
loff_t pos, size_t len)
490+
{
491+
struct cachefiles_read_ctx read_ctx = {pos, len};
492+
493+
return cachefiles_ondemand_send_req(object, CACHEFILES_OP_READ,
494+
sizeof(struct cachefiles_read),
495+
cachefiles_ondemand_init_read_req, &read_ctx);
496+
}

include/linux/netfs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ struct netfs_io_subrequest {
159159
#define NETFS_SREQ_SHORT_IO 2 /* Set if the I/O was short */
160160
#define NETFS_SREQ_SEEK_DATA_READ 3 /* Set if ->read() should SEEK_DATA first */
161161
#define NETFS_SREQ_NO_PROGRESS 4 /* Set if we didn't manage to read any data */
162+
#define NETFS_SREQ_ONDEMAND 5 /* Set if it's from on-demand read mode */
162163
};
163164

164165
enum netfs_io_origin {

include/uapi/linux/cachefiles.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#define _LINUX_CACHEFILES_H
44

55
#include <linux/types.h>
6+
#include <linux/ioctl.h>
67

78
/*
89
* Fscache ensures that the maximum length of cookie key is 255. The volume key
@@ -13,6 +14,7 @@
1314
enum cachefiles_opcode {
1415
CACHEFILES_OP_OPEN,
1516
CACHEFILES_OP_CLOSE,
17+
CACHEFILES_OP_READ,
1618
};
1719

1820
/*
@@ -48,4 +50,19 @@ struct cachefiles_open {
4850
__u8 data[];
4951
};
5052

53+
/*
54+
* @off indicates the starting offset of the requested file range
55+
* @len indicates the length of the requested file range
56+
*/
57+
struct cachefiles_read {
58+
__u64 off;
59+
__u64 len;
60+
};
61+
62+
/*
63+
* Reply for READ request
64+
* @arg for this ioctl is the @id field of READ request.
65+
*/
66+
#define CACHEFILES_IOC_READ_COMPLETE _IOW(0x98, 1, int)
67+
5168
#endif

0 commit comments

Comments
 (0)