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 int object_id = object->ondemand_id;
13 struct cachefiles_req *req;
14 XA_STATE(xas, &cache->reqs, 0);
16 xa_lock(&cache->reqs);
17 object->ondemand_id = CACHEFILES_ONDEMAND_ID_CLOSED;
20 * Flush all pending READ requests since their completion depends on
23 xas_for_each(&xas, req, ULONG_MAX) {
24 if (req->msg.opcode == CACHEFILES_OP_READ) {
27 xas_store(&xas, NULL);
30 xa_unlock(&cache->reqs);
32 xa_erase(&cache->ondemand_ids, object_id);
33 trace_cachefiles_ondemand_fd_release(object, object_id);
34 cachefiles_put_object(object, cachefiles_obj_put_ondemand_fd);
35 cachefiles_put_unbind_pincount(cache);
39 static ssize_t cachefiles_ondemand_fd_write_iter(struct kiocb *kiocb,
40 struct iov_iter *iter)
42 struct cachefiles_object *object = kiocb->ki_filp->private_data;
43 struct cachefiles_cache *cache = object->volume->cache;
44 struct file *file = object->file;
45 size_t len = iter->count;
46 loff_t pos = kiocb->ki_pos;
47 const struct cred *saved_cred;
53 cachefiles_begin_secure(cache, &saved_cred);
54 ret = __cachefiles_prepare_write(object, file, &pos, &len, true);
55 cachefiles_end_secure(cache, saved_cred);
59 trace_cachefiles_ondemand_fd_write(object, file_inode(file), pos, len);
60 ret = __cachefiles_write(object, file, pos, iter, NULL, NULL);
67 static loff_t cachefiles_ondemand_fd_llseek(struct file *filp, loff_t pos,
70 struct cachefiles_object *object = filp->private_data;
71 struct file *file = object->file;
76 return vfs_llseek(file, pos, whence);
79 static long cachefiles_ondemand_fd_ioctl(struct file *filp, unsigned int ioctl,
82 struct cachefiles_object *object = filp->private_data;
83 struct cachefiles_cache *cache = object->volume->cache;
84 struct cachefiles_req *req;
87 if (ioctl != CACHEFILES_IOC_READ_COMPLETE)
90 if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags))
94 req = xa_erase(&cache->reqs, id);
98 trace_cachefiles_ondemand_cread(object, id);
103 static const struct file_operations cachefiles_ondemand_fd_fops = {
104 .owner = THIS_MODULE,
105 .release = cachefiles_ondemand_fd_release,
106 .write_iter = cachefiles_ondemand_fd_write_iter,
107 .llseek = cachefiles_ondemand_fd_llseek,
108 .unlocked_ioctl = cachefiles_ondemand_fd_ioctl,
112 * OPEN request Completion (copen)
113 * - command: "copen <id>,<cache_size>"
114 * <cache_size> indicates the object size if >=0, error code if negative
116 int cachefiles_ondemand_copen(struct cachefiles_cache *cache, char *args)
118 struct cachefiles_req *req;
119 struct fscache_cookie *cookie;
125 if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags))
129 pr_err("Empty id specified\n");
134 psize = strchr(args, ',');
136 pr_err("Cache size is not specified\n");
143 ret = kstrtoul(pid, 0, &id);
147 req = xa_erase(&cache->reqs, id);
151 /* fail OPEN request if copen format is invalid */
152 ret = kstrtol(psize, 0, &size);
158 /* fail OPEN request if daemon reports an error */
160 if (!IS_ERR_VALUE(size))
166 cookie = req->object->cookie;
167 cookie->object_size = size;
169 clear_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags);
171 set_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags);
172 trace_cachefiles_ondemand_copen(req->object, id, size);
175 complete(&req->done);
179 static int cachefiles_ondemand_get_fd(struct cachefiles_req *req)
181 struct cachefiles_object *object;
182 struct cachefiles_cache *cache;
183 struct cachefiles_open *load;
188 object = cachefiles_grab_object(req->object,
189 cachefiles_obj_get_ondemand_fd);
190 cache = object->volume->cache;
192 ret = xa_alloc_cyclic(&cache->ondemand_ids, &object_id, NULL,
193 XA_LIMIT(1, INT_MAX),
194 &cache->ondemand_id_next, GFP_KERNEL);
198 fd = get_unused_fd_flags(O_WRONLY);
204 file = anon_inode_getfile("[cachefiles]", &cachefiles_ondemand_fd_fops,
211 file->f_mode |= FMODE_PWRITE | FMODE_LSEEK;
212 fd_install(fd, file);
214 load = (void *)req->msg.data;
216 req->msg.object_id = object_id;
217 object->ondemand_id = object_id;
219 cachefiles_get_unbind_pincount(cache);
220 trace_cachefiles_ondemand_open(object, &req->msg, load);
226 xa_erase(&cache->ondemand_ids, object_id);
228 cachefiles_put_object(object, cachefiles_obj_put_ondemand_fd);
232 ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache,
233 char __user *_buffer, size_t buflen)
235 struct cachefiles_req *req;
236 struct cachefiles_msg *msg;
237 unsigned long id = 0;
240 XA_STATE(xas, &cache->reqs, 0);
243 * Search for a request that has not ever been processed, to prevent
244 * requests from being processed repeatedly.
246 xa_lock(&cache->reqs);
247 req = xas_find_marked(&xas, UINT_MAX, CACHEFILES_REQ_NEW);
249 xa_unlock(&cache->reqs);
257 xa_unlock(&cache->reqs);
261 xas_clear_mark(&xas, CACHEFILES_REQ_NEW);
262 xa_unlock(&cache->reqs);
267 if (msg->opcode == CACHEFILES_OP_OPEN) {
268 ret = cachefiles_ondemand_get_fd(req);
273 if (copy_to_user(_buffer, msg, n) != 0) {
278 /* CLOSE request has no reply */
279 if (msg->opcode == CACHEFILES_OP_CLOSE) {
280 xa_erase(&cache->reqs, id);
281 complete(&req->done);
287 if (msg->opcode == CACHEFILES_OP_OPEN)
288 close_fd(((struct cachefiles_open *)msg->data)->fd);
290 xa_erase(&cache->reqs, id);
292 complete(&req->done);
296 typedef int (*init_req_fn)(struct cachefiles_req *req, void *private);
298 static int cachefiles_ondemand_send_req(struct cachefiles_object *object,
299 enum cachefiles_opcode opcode,
301 init_req_fn init_req,
304 struct cachefiles_cache *cache = object->volume->cache;
305 struct cachefiles_req *req;
306 XA_STATE(xas, &cache->reqs, 0);
309 if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags))
312 if (test_bit(CACHEFILES_DEAD, &cache->flags))
315 req = kzalloc(sizeof(*req) + data_len, GFP_KERNEL);
319 req->object = object;
320 init_completion(&req->done);
321 req->msg.opcode = opcode;
322 req->msg.len = sizeof(struct cachefiles_msg) + data_len;
324 ret = init_req(req, private);
330 * Stop enqueuing the request when daemon is dying. The
331 * following two operations need to be atomic as a whole.
332 * 1) check cache state, and
333 * 2) enqueue request if cache is alive.
334 * Otherwise the request may be enqueued after xarray has been
335 * flushed, leaving the orphan request never being completed.
339 * test CACHEFILES_DEAD bit
340 * set CACHEFILES_DEAD bit
341 * flush requests in the xarray
342 * enqueue the request
346 if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
352 /* coupled with the barrier in cachefiles_flush_reqs() */
355 if (opcode != CACHEFILES_OP_OPEN && object->ondemand_id <= 0) {
356 WARN_ON_ONCE(object->ondemand_id == 0);
363 xas_find_marked(&xas, UINT_MAX, XA_FREE_MARK);
364 if (xas.xa_node == XAS_RESTART)
365 xas_set_err(&xas, -EBUSY);
366 xas_store(&xas, req);
367 xas_clear_mark(&xas, XA_FREE_MARK);
368 xas_set_mark(&xas, CACHEFILES_REQ_NEW);
370 } while (xas_nomem(&xas, GFP_KERNEL));
372 ret = xas_error(&xas);
376 wake_up_all(&cache->daemon_pollwq);
377 wait_for_completion(&req->done);
384 static int cachefiles_ondemand_init_open_req(struct cachefiles_req *req,
387 struct cachefiles_object *object = req->object;
388 struct fscache_cookie *cookie = object->cookie;
389 struct fscache_volume *volume = object->volume->vcookie;
390 struct cachefiles_open *load = (void *)req->msg.data;
391 size_t volume_key_size, cookie_key_size;
392 void *volume_key, *cookie_key;
395 * Volume key is a NUL-terminated string. key[0] stores strlen() of the
396 * string, followed by the content of the string (excluding '\0').
398 volume_key_size = volume->key[0] + 1;
399 volume_key = volume->key + 1;
401 /* Cookie key is binary data, which is netfs specific. */
402 cookie_key_size = cookie->key_len;
403 cookie_key = fscache_get_key(cookie);
405 if (!(object->cookie->advice & FSCACHE_ADV_WANT_CACHE_SIZE)) {
406 pr_err("WANT_CACHE_SIZE is needed for on-demand mode\n");
410 load->volume_key_size = volume_key_size;
411 load->cookie_key_size = cookie_key_size;
412 memcpy(load->data, volume_key, volume_key_size);
413 memcpy(load->data + volume_key_size, cookie_key, cookie_key_size);
418 static int cachefiles_ondemand_init_close_req(struct cachefiles_req *req,
421 struct cachefiles_object *object = req->object;
422 int object_id = object->ondemand_id;
425 * It's possible that object id is still 0 if the cookie looking up
426 * phase failed before OPEN request has ever been sent. Also avoid
427 * sending CLOSE request for CACHEFILES_ONDEMAND_ID_CLOSED, which means
428 * anon_fd has already been closed.
433 req->msg.object_id = object_id;
434 trace_cachefiles_ondemand_close(object, &req->msg);
438 struct cachefiles_read_ctx {
443 static int cachefiles_ondemand_init_read_req(struct cachefiles_req *req,
446 struct cachefiles_object *object = req->object;
447 struct cachefiles_read *load = (void *)req->msg.data;
448 struct cachefiles_read_ctx *read_ctx = private;
449 int object_id = object->ondemand_id;
451 /* Stop enqueuing requests when daemon has closed anon_fd. */
452 if (object_id <= 0) {
453 WARN_ON_ONCE(object_id == 0);
454 pr_info_once("READ: anonymous fd closed prematurely.\n");
458 req->msg.object_id = object_id;
459 load->off = read_ctx->off;
460 load->len = read_ctx->len;
461 trace_cachefiles_ondemand_read(object, &req->msg, load);
465 int cachefiles_ondemand_init_object(struct cachefiles_object *object)
467 struct fscache_cookie *cookie = object->cookie;
468 struct fscache_volume *volume = object->volume->vcookie;
469 size_t volume_key_size, cookie_key_size, data_len;
472 * CacheFiles will firstly check the cache file under the root cache
473 * directory. If the coherency check failed, it will fallback to
474 * creating a new tmpfile as the cache file. Reuse the previously
475 * allocated object ID if any.
477 if (object->ondemand_id > 0)
480 volume_key_size = volume->key[0] + 1;
481 cookie_key_size = cookie->key_len;
482 data_len = sizeof(struct cachefiles_open) +
483 volume_key_size + cookie_key_size;
485 return cachefiles_ondemand_send_req(object, CACHEFILES_OP_OPEN,
486 data_len, cachefiles_ondemand_init_open_req, NULL);
489 void cachefiles_ondemand_clean_object(struct cachefiles_object *object)
491 cachefiles_ondemand_send_req(object, CACHEFILES_OP_CLOSE, 0,
492 cachefiles_ondemand_init_close_req, NULL);
495 int cachefiles_ondemand_read(struct cachefiles_object *object,
496 loff_t pos, size_t len)
498 struct cachefiles_read_ctx read_ctx = {pos, len};
500 return cachefiles_ondemand_send_req(object, CACHEFILES_OP_READ,
501 sizeof(struct cachefiles_read),
502 cachefiles_ondemand_init_read_req, &read_ctx);