1 // SPDX-License-Identifier: GPL-2.0-or-later
2 #include <linux/fdtable.h>
3 #include <linux/anon_inodes.h>
7 static int cachefiles_ondemand_fd_release(struct inode *inode,
10 struct cachefiles_object *object = file->private_data;
11 struct cachefiles_cache *cache = object->volume->cache;
12 struct cachefiles_ondemand_info *info = object->ondemand;
13 int object_id = info->ondemand_id;
14 struct cachefiles_req *req;
15 XA_STATE(xas, &cache->reqs, 0);
17 xa_lock(&cache->reqs);
18 info->ondemand_id = CACHEFILES_ONDEMAND_ID_CLOSED;
19 cachefiles_ondemand_set_object_close(object);
21 /* Only flush CACHEFILES_REQ_NEW marked req to avoid race with daemon_read */
22 xas_for_each_marked(&xas, req, ULONG_MAX, CACHEFILES_REQ_NEW) {
23 if (req->msg.object_id == object_id &&
24 req->msg.opcode == CACHEFILES_OP_CLOSE) {
26 xas_store(&xas, NULL);
29 xa_unlock(&cache->reqs);
31 xa_erase(&cache->ondemand_ids, object_id);
32 trace_cachefiles_ondemand_fd_release(object, object_id);
33 cachefiles_put_object(object, cachefiles_obj_put_ondemand_fd);
34 cachefiles_put_unbind_pincount(cache);
38 static ssize_t cachefiles_ondemand_fd_write_iter(struct kiocb *kiocb,
39 struct iov_iter *iter)
41 struct cachefiles_object *object = kiocb->ki_filp->private_data;
42 struct cachefiles_cache *cache = object->volume->cache;
43 struct file *file = object->file;
44 size_t len = iter->count;
45 loff_t pos = kiocb->ki_pos;
46 const struct cred *saved_cred;
52 cachefiles_begin_secure(cache, &saved_cred);
53 ret = __cachefiles_prepare_write(object, file, &pos, &len, len, true);
54 cachefiles_end_secure(cache, saved_cred);
58 trace_cachefiles_ondemand_fd_write(object, file_inode(file), pos, len);
59 ret = __cachefiles_write(object, file, pos, iter, NULL, NULL);
66 static loff_t cachefiles_ondemand_fd_llseek(struct file *filp, loff_t pos,
69 struct cachefiles_object *object = filp->private_data;
70 struct file *file = object->file;
75 return vfs_llseek(file, pos, whence);
78 static long cachefiles_ondemand_fd_ioctl(struct file *filp, unsigned int ioctl,
81 struct cachefiles_object *object = filp->private_data;
82 struct cachefiles_cache *cache = object->volume->cache;
83 struct cachefiles_req *req;
86 if (ioctl != CACHEFILES_IOC_READ_COMPLETE)
89 if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags))
93 req = xa_erase(&cache->reqs, id);
97 trace_cachefiles_ondemand_cread(object, id);
102 static const struct file_operations cachefiles_ondemand_fd_fops = {
103 .owner = THIS_MODULE,
104 .release = cachefiles_ondemand_fd_release,
105 .write_iter = cachefiles_ondemand_fd_write_iter,
106 .llseek = cachefiles_ondemand_fd_llseek,
107 .unlocked_ioctl = cachefiles_ondemand_fd_ioctl,
111 * OPEN request Completion (copen)
112 * - command: "copen <id>,<cache_size>"
113 * <cache_size> indicates the object size if >=0, error code if negative
115 int cachefiles_ondemand_copen(struct cachefiles_cache *cache, char *args)
117 struct cachefiles_req *req;
118 struct fscache_cookie *cookie;
124 if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags))
128 pr_err("Empty id specified\n");
133 psize = strchr(args, ',');
135 pr_err("Cache size is not specified\n");
142 ret = kstrtoul(pid, 0, &id);
146 req = xa_erase(&cache->reqs, id);
150 /* fail OPEN request if copen format is invalid */
151 ret = kstrtol(psize, 0, &size);
157 /* fail OPEN request if daemon reports an error */
159 if (!IS_ERR_VALUE(size)) {
160 req->error = -EINVAL;
169 cookie = req->object->cookie;
170 cookie->object_size = size;
172 clear_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags);
174 set_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags);
175 trace_cachefiles_ondemand_copen(req->object, id, size);
177 cachefiles_ondemand_set_object_open(req->object);
178 wake_up_all(&cache->daemon_pollwq);
181 complete(&req->done);
185 int cachefiles_ondemand_restore(struct cachefiles_cache *cache, char *args)
187 struct cachefiles_req *req;
189 XA_STATE(xas, &cache->reqs, 0);
191 if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags))
195 * Reset the requests to CACHEFILES_REQ_NEW state, so that the
196 * requests have been processed halfway before the crash of the
197 * user daemon could be reprocessed after the recovery.
200 xas_for_each(&xas, req, ULONG_MAX)
201 xas_set_mark(&xas, CACHEFILES_REQ_NEW);
204 wake_up_all(&cache->daemon_pollwq);
208 static int cachefiles_ondemand_get_fd(struct cachefiles_req *req)
210 struct cachefiles_object *object;
211 struct cachefiles_cache *cache;
212 struct cachefiles_open *load;
217 object = cachefiles_grab_object(req->object,
218 cachefiles_obj_get_ondemand_fd);
219 cache = object->volume->cache;
221 ret = xa_alloc_cyclic(&cache->ondemand_ids, &object_id, NULL,
222 XA_LIMIT(1, INT_MAX),
223 &cache->ondemand_id_next, GFP_KERNEL);
227 fd = get_unused_fd_flags(O_WRONLY);
233 file = anon_inode_getfile("[cachefiles]", &cachefiles_ondemand_fd_fops,
240 file->f_mode |= FMODE_PWRITE | FMODE_LSEEK;
241 fd_install(fd, file);
243 load = (void *)req->msg.data;
245 object->ondemand->ondemand_id = object_id;
247 cachefiles_get_unbind_pincount(cache);
248 trace_cachefiles_ondemand_open(object, &req->msg, load);
254 xa_erase(&cache->ondemand_ids, object_id);
256 cachefiles_put_object(object, cachefiles_obj_put_ondemand_fd);
260 static void ondemand_object_worker(struct work_struct *work)
262 struct cachefiles_ondemand_info *info =
263 container_of(work, struct cachefiles_ondemand_info, ondemand_work);
265 cachefiles_ondemand_init_object(info->object);
269 * If there are any inflight or subsequent READ requests on the
270 * closed object, reopen it.
271 * Skip read requests whose related object is reopening.
273 static struct cachefiles_req *cachefiles_ondemand_select_req(struct xa_state *xas,
274 unsigned long xa_max)
276 struct cachefiles_req *req;
277 struct cachefiles_object *object;
278 struct cachefiles_ondemand_info *info;
280 xas_for_each_marked(xas, req, xa_max, CACHEFILES_REQ_NEW) {
281 if (req->msg.opcode != CACHEFILES_OP_READ)
283 object = req->object;
284 info = object->ondemand;
285 if (cachefiles_ondemand_object_is_close(object)) {
286 cachefiles_ondemand_set_object_reopening(object);
287 queue_work(fscache_wq, &info->ondemand_work);
290 if (cachefiles_ondemand_object_is_reopening(object))
297 ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache,
298 char __user *_buffer, size_t buflen)
300 struct cachefiles_req *req;
301 struct cachefiles_msg *msg;
302 unsigned long id = 0;
305 XA_STATE(xas, &cache->reqs, cache->req_id_next);
307 xa_lock(&cache->reqs);
309 * Cyclically search for a request that has not ever been processed,
310 * to prevent requests from being processed repeatedly, and make
311 * request distribution fair.
313 req = cachefiles_ondemand_select_req(&xas, ULONG_MAX);
314 if (!req && cache->req_id_next > 0) {
316 req = cachefiles_ondemand_select_req(&xas, cache->req_id_next - 1);
319 xa_unlock(&cache->reqs);
327 xa_unlock(&cache->reqs);
331 xas_clear_mark(&xas, CACHEFILES_REQ_NEW);
332 cache->req_id_next = xas.xa_index + 1;
333 xa_unlock(&cache->reqs);
337 if (msg->opcode == CACHEFILES_OP_OPEN) {
338 ret = cachefiles_ondemand_get_fd(req);
340 cachefiles_ondemand_set_object_close(req->object);
346 msg->object_id = req->object->ondemand->ondemand_id;
348 if (copy_to_user(_buffer, msg, n) != 0) {
353 /* CLOSE request has no reply */
354 if (msg->opcode == CACHEFILES_OP_CLOSE) {
355 xa_erase(&cache->reqs, id);
356 complete(&req->done);
362 if (msg->opcode == CACHEFILES_OP_OPEN)
363 close_fd(((struct cachefiles_open *)msg->data)->fd);
365 xa_erase(&cache->reqs, id);
367 complete(&req->done);
371 typedef int (*init_req_fn)(struct cachefiles_req *req, void *private);
373 static int cachefiles_ondemand_send_req(struct cachefiles_object *object,
374 enum cachefiles_opcode opcode,
376 init_req_fn init_req,
379 struct cachefiles_cache *cache = object->volume->cache;
380 struct cachefiles_req *req = NULL;
381 XA_STATE(xas, &cache->reqs, 0);
384 if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags))
387 if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
392 req = kzalloc(sizeof(*req) + data_len, GFP_KERNEL);
398 req->object = object;
399 init_completion(&req->done);
400 req->msg.opcode = opcode;
401 req->msg.len = sizeof(struct cachefiles_msg) + data_len;
403 ret = init_req(req, private);
409 * Stop enqueuing the request when daemon is dying. The
410 * following two operations need to be atomic as a whole.
411 * 1) check cache state, and
412 * 2) enqueue request if cache is alive.
413 * Otherwise the request may be enqueued after xarray has been
414 * flushed, leaving the orphan request never being completed.
418 * test CACHEFILES_DEAD bit
419 * set CACHEFILES_DEAD bit
420 * flush requests in the xarray
421 * enqueue the request
425 if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
431 /* coupled with the barrier in cachefiles_flush_reqs() */
434 if (opcode == CACHEFILES_OP_CLOSE &&
435 !cachefiles_ondemand_object_is_open(object)) {
436 WARN_ON_ONCE(object->ondemand->ondemand_id == 0);
443 xas_find_marked(&xas, UINT_MAX, XA_FREE_MARK);
444 if (xas.xa_node == XAS_RESTART)
445 xas_set_err(&xas, -EBUSY);
446 xas_store(&xas, req);
447 xas_clear_mark(&xas, XA_FREE_MARK);
448 xas_set_mark(&xas, CACHEFILES_REQ_NEW);
450 } while (xas_nomem(&xas, GFP_KERNEL));
452 ret = xas_error(&xas);
456 wake_up_all(&cache->daemon_pollwq);
457 wait_for_completion(&req->done);
462 /* Reset the object to close state in error handling path.
463 * If error occurs after creating the anonymous fd,
464 * cachefiles_ondemand_fd_release() will set object to close.
466 if (opcode == CACHEFILES_OP_OPEN)
467 cachefiles_ondemand_set_object_close(object);
472 static int cachefiles_ondemand_init_open_req(struct cachefiles_req *req,
475 struct cachefiles_object *object = req->object;
476 struct fscache_cookie *cookie = object->cookie;
477 struct fscache_volume *volume = object->volume->vcookie;
478 struct cachefiles_open *load = (void *)req->msg.data;
479 size_t volume_key_size, cookie_key_size;
480 void *volume_key, *cookie_key;
483 * Volume key is a NUL-terminated string. key[0] stores strlen() of the
484 * string, followed by the content of the string (excluding '\0').
486 volume_key_size = volume->key[0] + 1;
487 volume_key = volume->key + 1;
489 /* Cookie key is binary data, which is netfs specific. */
490 cookie_key_size = cookie->key_len;
491 cookie_key = fscache_get_key(cookie);
493 if (!(object->cookie->advice & FSCACHE_ADV_WANT_CACHE_SIZE)) {
494 pr_err("WANT_CACHE_SIZE is needed for on-demand mode\n");
498 load->volume_key_size = volume_key_size;
499 load->cookie_key_size = cookie_key_size;
500 memcpy(load->data, volume_key, volume_key_size);
501 memcpy(load->data + volume_key_size, cookie_key, cookie_key_size);
506 static int cachefiles_ondemand_init_close_req(struct cachefiles_req *req,
509 struct cachefiles_object *object = req->object;
511 if (!cachefiles_ondemand_object_is_open(object))
514 trace_cachefiles_ondemand_close(object, &req->msg);
518 struct cachefiles_read_ctx {
523 static int cachefiles_ondemand_init_read_req(struct cachefiles_req *req,
526 struct cachefiles_object *object = req->object;
527 struct cachefiles_read *load = (void *)req->msg.data;
528 struct cachefiles_read_ctx *read_ctx = private;
530 load->off = read_ctx->off;
531 load->len = read_ctx->len;
532 trace_cachefiles_ondemand_read(object, &req->msg, load);
536 int cachefiles_ondemand_init_object(struct cachefiles_object *object)
538 struct fscache_cookie *cookie = object->cookie;
539 struct fscache_volume *volume = object->volume->vcookie;
540 size_t volume_key_size, cookie_key_size, data_len;
542 if (!object->ondemand)
546 * CacheFiles will firstly check the cache file under the root cache
547 * directory. If the coherency check failed, it will fallback to
548 * creating a new tmpfile as the cache file. Reuse the previously
549 * allocated object ID if any.
551 if (cachefiles_ondemand_object_is_open(object))
554 volume_key_size = volume->key[0] + 1;
555 cookie_key_size = cookie->key_len;
556 data_len = sizeof(struct cachefiles_open) +
557 volume_key_size + cookie_key_size;
559 return cachefiles_ondemand_send_req(object, CACHEFILES_OP_OPEN,
560 data_len, cachefiles_ondemand_init_open_req, NULL);
563 void cachefiles_ondemand_clean_object(struct cachefiles_object *object)
565 cachefiles_ondemand_send_req(object, CACHEFILES_OP_CLOSE, 0,
566 cachefiles_ondemand_init_close_req, NULL);
569 int cachefiles_ondemand_init_obj_info(struct cachefiles_object *object,
570 struct cachefiles_volume *volume)
572 if (!cachefiles_in_ondemand_mode(volume->cache))
575 object->ondemand = kzalloc(sizeof(struct cachefiles_ondemand_info),
577 if (!object->ondemand)
580 object->ondemand->object = object;
581 INIT_WORK(&object->ondemand->ondemand_work, ondemand_object_worker);
585 void cachefiles_ondemand_deinit_obj_info(struct cachefiles_object *object)
587 kfree(object->ondemand);
588 object->ondemand = NULL;
591 int cachefiles_ondemand_read(struct cachefiles_object *object,
592 loff_t pos, size_t len)
594 struct cachefiles_read_ctx read_ctx = {pos, len};
596 return cachefiles_ondemand_send_req(object, CACHEFILES_OP_READ,
597 sizeof(struct cachefiles_read),
598 cachefiles_ondemand_init_read_req, &read_ctx);