1 // SPDX-License-Identifier: GPL-2.0-or-later
2 #include <linux/anon_inodes.h>
6 struct ondemand_anon_file {
11 static inline void cachefiles_req_put(struct cachefiles_req *req)
13 if (refcount_dec_and_test(&req->ref))
17 static int cachefiles_ondemand_fd_release(struct inode *inode,
20 struct cachefiles_object *object = file->private_data;
21 struct cachefiles_cache *cache;
22 struct cachefiles_ondemand_info *info;
24 struct cachefiles_req *req;
25 XA_STATE(xas, NULL, 0);
30 info = object->ondemand;
31 cache = object->volume->cache;
32 xas.xa = &cache->reqs;
34 xa_lock(&cache->reqs);
35 spin_lock(&info->lock);
36 object_id = info->ondemand_id;
37 info->ondemand_id = CACHEFILES_ONDEMAND_ID_CLOSED;
38 cachefiles_ondemand_set_object_close(object);
39 spin_unlock(&info->lock);
41 /* Only flush CACHEFILES_REQ_NEW marked req to avoid race with daemon_read */
42 xas_for_each_marked(&xas, req, ULONG_MAX, CACHEFILES_REQ_NEW) {
43 if (req->msg.object_id == object_id &&
44 req->msg.opcode == CACHEFILES_OP_CLOSE) {
46 xas_store(&xas, NULL);
49 xa_unlock(&cache->reqs);
51 xa_erase(&cache->ondemand_ids, object_id);
52 trace_cachefiles_ondemand_fd_release(object, object_id);
53 cachefiles_put_object(object, cachefiles_obj_put_ondemand_fd);
54 cachefiles_put_unbind_pincount(cache);
58 static ssize_t cachefiles_ondemand_fd_write_iter(struct kiocb *kiocb,
59 struct iov_iter *iter)
61 struct cachefiles_object *object = kiocb->ki_filp->private_data;
62 struct cachefiles_cache *cache = object->volume->cache;
64 size_t len = iter->count, aligned_len = len;
65 loff_t pos = kiocb->ki_pos;
66 const struct cred *saved_cred;
69 spin_lock(&object->lock);
72 spin_unlock(&object->lock);
76 spin_unlock(&object->lock);
78 cachefiles_begin_secure(cache, &saved_cred);
79 ret = __cachefiles_prepare_write(object, file, &pos, &aligned_len, len, true);
80 cachefiles_end_secure(cache, saved_cred);
84 trace_cachefiles_ondemand_fd_write(object, file_inode(file), pos, len);
85 ret = __cachefiles_write(object, file, pos, iter, NULL, NULL);
96 static loff_t cachefiles_ondemand_fd_llseek(struct file *filp, loff_t pos,
99 struct cachefiles_object *object = filp->private_data;
103 spin_lock(&object->lock);
106 spin_unlock(&object->lock);
110 spin_unlock(&object->lock);
112 ret = vfs_llseek(file, pos, whence);
118 static long cachefiles_ondemand_fd_ioctl(struct file *filp, unsigned int ioctl,
121 struct cachefiles_object *object = filp->private_data;
122 struct cachefiles_cache *cache = object->volume->cache;
123 struct cachefiles_req *req;
124 XA_STATE(xas, &cache->reqs, id);
126 if (ioctl != CACHEFILES_IOC_READ_COMPLETE)
129 if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags))
132 xa_lock(&cache->reqs);
133 req = xas_load(&xas);
134 if (!req || req->msg.opcode != CACHEFILES_OP_READ ||
135 req->object != object) {
136 xa_unlock(&cache->reqs);
139 xas_store(&xas, NULL);
140 xa_unlock(&cache->reqs);
142 trace_cachefiles_ondemand_cread(object, id);
143 complete(&req->done);
147 static const struct file_operations cachefiles_ondemand_fd_fops = {
148 .owner = THIS_MODULE,
149 .release = cachefiles_ondemand_fd_release,
150 .write_iter = cachefiles_ondemand_fd_write_iter,
151 .llseek = cachefiles_ondemand_fd_llseek,
152 .unlocked_ioctl = cachefiles_ondemand_fd_ioctl,
156 * OPEN request Completion (copen)
157 * - command: "copen <id>,<cache_size>"
158 * <cache_size> indicates the object size if >=0, error code if negative
160 int cachefiles_ondemand_copen(struct cachefiles_cache *cache, char *args)
162 struct cachefiles_req *req;
163 struct fscache_cookie *cookie;
164 struct cachefiles_ondemand_info *info;
169 XA_STATE(xas, &cache->reqs, 0);
171 if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags))
175 pr_err("Empty id specified\n");
180 psize = strchr(args, ',');
182 pr_err("Cache size is not specified\n");
189 ret = kstrtoul(pid, 0, &id);
193 xa_lock(&cache->reqs);
195 req = xas_load(&xas);
196 if (!req || req->msg.opcode != CACHEFILES_OP_OPEN ||
197 !req->object->ondemand->ondemand_id) {
198 xa_unlock(&cache->reqs);
201 xas_store(&xas, NULL);
202 xa_unlock(&cache->reqs);
204 info = req->object->ondemand;
205 /* fail OPEN request if copen format is invalid */
206 ret = kstrtol(psize, 0, &size);
212 /* fail OPEN request if daemon reports an error */
214 if (!IS_ERR_VALUE(size)) {
215 req->error = -EINVAL;
224 spin_lock(&info->lock);
226 * The anonymous fd was closed before copen ? Fail the request.
229 * ---------------------------------------------------------
230 * cachefiles_ondemand_copen
231 * req = xa_erase(&cache->reqs, id)
232 * // Anon fd is maliciously closed.
233 * cachefiles_ondemand_fd_release
234 * xa_lock(&cache->reqs)
235 * cachefiles_ondemand_set_object_close(object)
236 * xa_unlock(&cache->reqs)
237 * cachefiles_ondemand_set_object_open
238 * // No one will ever close it again.
239 * cachefiles_ondemand_daemon_read
240 * cachefiles_ondemand_select_req
242 * Get a read req but its fd is already closed. The daemon can't
243 * issue a cread ioctl with an closed fd, then hung.
245 if (info->ondemand_id == CACHEFILES_ONDEMAND_ID_CLOSED) {
246 spin_unlock(&info->lock);
247 req->error = -EBADFD;
250 cookie = req->object->cookie;
251 cookie->object_size = size;
253 clear_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags);
255 set_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags);
256 trace_cachefiles_ondemand_copen(req->object, id, size);
258 cachefiles_ondemand_set_object_open(req->object);
259 spin_unlock(&info->lock);
260 wake_up_all(&cache->daemon_pollwq);
263 spin_lock(&info->lock);
264 /* Need to set object close to avoid reopen status continuing */
265 if (info->ondemand_id == CACHEFILES_ONDEMAND_ID_CLOSED)
266 cachefiles_ondemand_set_object_close(req->object);
267 spin_unlock(&info->lock);
268 complete(&req->done);
272 int cachefiles_ondemand_restore(struct cachefiles_cache *cache, char *args)
274 struct cachefiles_req *req;
276 XA_STATE(xas, &cache->reqs, 0);
278 if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags))
282 * Reset the requests to CACHEFILES_REQ_NEW state, so that the
283 * requests have been processed halfway before the crash of the
284 * user daemon could be reprocessed after the recovery.
287 xas_for_each(&xas, req, ULONG_MAX)
288 xas_set_mark(&xas, CACHEFILES_REQ_NEW);
291 wake_up_all(&cache->daemon_pollwq);
295 static int cachefiles_ondemand_get_fd(struct cachefiles_req *req,
296 struct ondemand_anon_file *anon_file)
298 struct cachefiles_object *object;
299 struct cachefiles_cache *cache;
300 struct cachefiles_open *load;
304 object = cachefiles_grab_object(req->object,
305 cachefiles_obj_get_ondemand_fd);
306 cache = object->volume->cache;
308 ret = xa_alloc_cyclic(&cache->ondemand_ids, &object_id, NULL,
309 XA_LIMIT(1, INT_MAX),
310 &cache->ondemand_id_next, GFP_KERNEL);
314 anon_file->fd = get_unused_fd_flags(O_WRONLY);
315 if (anon_file->fd < 0) {
320 anon_file->file = anon_inode_getfile("[cachefiles]",
321 &cachefiles_ondemand_fd_fops, object, O_WRONLY);
322 if (IS_ERR(anon_file->file)) {
323 ret = PTR_ERR(anon_file->file);
327 spin_lock(&object->ondemand->lock);
328 if (object->ondemand->ondemand_id > 0) {
329 spin_unlock(&object->ondemand->lock);
330 /* Pair with check in cachefiles_ondemand_fd_release(). */
331 anon_file->file->private_data = NULL;
336 anon_file->file->f_mode |= FMODE_PWRITE | FMODE_LSEEK;
338 load = (void *)req->msg.data;
339 load->fd = anon_file->fd;
340 object->ondemand->ondemand_id = object_id;
341 spin_unlock(&object->ondemand->lock);
343 cachefiles_get_unbind_pincount(cache);
344 trace_cachefiles_ondemand_open(object, &req->msg, load);
348 fput(anon_file->file);
349 anon_file->file = NULL;
351 put_unused_fd(anon_file->fd);
354 xa_erase(&cache->ondemand_ids, object_id);
356 spin_lock(&object->ondemand->lock);
357 /* Avoid marking an opened object as closed. */
358 if (object->ondemand->ondemand_id <= 0)
359 cachefiles_ondemand_set_object_close(object);
360 spin_unlock(&object->ondemand->lock);
361 cachefiles_put_object(object, cachefiles_obj_put_ondemand_fd);
365 static void ondemand_object_worker(struct work_struct *work)
367 struct cachefiles_ondemand_info *info =
368 container_of(work, struct cachefiles_ondemand_info, ondemand_work);
370 cachefiles_ondemand_init_object(info->object);
374 * If there are any inflight or subsequent READ requests on the
375 * closed object, reopen it.
376 * Skip read requests whose related object is reopening.
378 static struct cachefiles_req *cachefiles_ondemand_select_req(struct xa_state *xas,
379 unsigned long xa_max)
381 struct cachefiles_req *req;
382 struct cachefiles_object *object;
383 struct cachefiles_ondemand_info *info;
385 xas_for_each_marked(xas, req, xa_max, CACHEFILES_REQ_NEW) {
386 if (req->msg.opcode != CACHEFILES_OP_READ)
388 object = req->object;
389 info = object->ondemand;
390 if (cachefiles_ondemand_object_is_close(object)) {
391 cachefiles_ondemand_set_object_reopening(object);
392 queue_work(fscache_wq, &info->ondemand_work);
395 if (cachefiles_ondemand_object_is_reopening(object))
402 static inline bool cachefiles_ondemand_finish_req(struct cachefiles_req *req,
403 struct xa_state *xas, int err)
405 if (unlikely(!xas || !req))
408 if (xa_cmpxchg(xas->xa, xas->xa_index, req, NULL, 0) != req)
412 complete(&req->done);
416 ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache,
417 char __user *_buffer, size_t buflen)
419 struct cachefiles_req *req;
420 struct cachefiles_msg *msg;
423 struct ondemand_anon_file anon_file;
424 XA_STATE(xas, &cache->reqs, cache->req_id_next);
426 xa_lock(&cache->reqs);
428 * Cyclically search for a request that has not ever been processed,
429 * to prevent requests from being processed repeatedly, and make
430 * request distribution fair.
432 req = cachefiles_ondemand_select_req(&xas, ULONG_MAX);
433 if (!req && cache->req_id_next > 0) {
435 req = cachefiles_ondemand_select_req(&xas, cache->req_id_next - 1);
438 xa_unlock(&cache->reqs);
446 xa_unlock(&cache->reqs);
450 xas_clear_mark(&xas, CACHEFILES_REQ_NEW);
451 cache->req_id_next = xas.xa_index + 1;
452 refcount_inc(&req->ref);
453 cachefiles_grab_object(req->object, cachefiles_obj_get_read_req);
454 xa_unlock(&cache->reqs);
456 if (msg->opcode == CACHEFILES_OP_OPEN) {
457 ret = cachefiles_ondemand_get_fd(req, &anon_file);
462 msg->msg_id = xas.xa_index;
463 msg->object_id = req->object->ondemand->ondemand_id;
465 if (copy_to_user(_buffer, msg, n) != 0)
468 if (msg->opcode == CACHEFILES_OP_OPEN) {
470 fput(anon_file.file);
471 put_unused_fd(anon_file.fd);
474 fd_install(anon_file.fd, anon_file.file);
477 cachefiles_put_object(req->object, cachefiles_obj_put_read_req);
478 /* Remove error request and CLOSE request has no reply */
479 if (ret || msg->opcode == CACHEFILES_OP_CLOSE)
480 cachefiles_ondemand_finish_req(req, &xas, ret);
481 cachefiles_req_put(req);
482 return ret ? ret : n;
485 typedef int (*init_req_fn)(struct cachefiles_req *req, void *private);
487 static int cachefiles_ondemand_send_req(struct cachefiles_object *object,
488 enum cachefiles_opcode opcode,
490 init_req_fn init_req,
493 struct cachefiles_cache *cache = object->volume->cache;
494 struct cachefiles_req *req = NULL;
495 XA_STATE(xas, &cache->reqs, 0);
498 if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags))
501 if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
506 req = kzalloc(sizeof(*req) + data_len, GFP_KERNEL);
512 refcount_set(&req->ref, 1);
513 req->object = object;
514 init_completion(&req->done);
515 req->msg.opcode = opcode;
516 req->msg.len = sizeof(struct cachefiles_msg) + data_len;
518 ret = init_req(req, private);
524 * Stop enqueuing the request when daemon is dying. The
525 * following two operations need to be atomic as a whole.
526 * 1) check cache state, and
527 * 2) enqueue request if cache is alive.
528 * Otherwise the request may be enqueued after xarray has been
529 * flushed, leaving the orphan request never being completed.
533 * test CACHEFILES_DEAD bit
534 * set CACHEFILES_DEAD bit
535 * flush requests in the xarray
536 * enqueue the request
540 if (test_bit(CACHEFILES_DEAD, &cache->flags) ||
541 cachefiles_ondemand_object_is_dropping(object)) {
547 /* coupled with the barrier in cachefiles_flush_reqs() */
550 if (opcode == CACHEFILES_OP_CLOSE &&
551 !cachefiles_ondemand_object_is_open(object)) {
552 WARN_ON_ONCE(object->ondemand->ondemand_id == 0);
559 * Cyclically find a free xas to avoid msg_id reuse that would
560 * cause the daemon to successfully copen a stale msg_id.
562 xas.xa_index = cache->msg_id_next;
563 xas_find_marked(&xas, UINT_MAX, XA_FREE_MARK);
564 if (xas.xa_node == XAS_RESTART) {
566 xas_find_marked(&xas, cache->msg_id_next - 1, XA_FREE_MARK);
568 if (xas.xa_node == XAS_RESTART)
569 xas_set_err(&xas, -EBUSY);
571 xas_store(&xas, req);
572 if (xas_valid(&xas)) {
573 cache->msg_id_next = xas.xa_index + 1;
574 xas_clear_mark(&xas, XA_FREE_MARK);
575 xas_set_mark(&xas, CACHEFILES_REQ_NEW);
578 } while (xas_nomem(&xas, GFP_KERNEL));
580 ret = xas_error(&xas);
584 wake_up_all(&cache->daemon_pollwq);
586 ret = wait_for_completion_killable(&req->done);
591 if (!cachefiles_ondemand_finish_req(req, &xas, ret)) {
592 /* Someone will complete it soon. */
597 cachefiles_req_put(req);
600 /* Reset the object to close state in error handling path.
601 * If error occurs after creating the anonymous fd,
602 * cachefiles_ondemand_fd_release() will set object to close.
604 if (opcode == CACHEFILES_OP_OPEN &&
605 !cachefiles_ondemand_object_is_dropping(object))
606 cachefiles_ondemand_set_object_close(object);
611 static int cachefiles_ondemand_init_open_req(struct cachefiles_req *req,
614 struct cachefiles_object *object = req->object;
615 struct fscache_cookie *cookie = object->cookie;
616 struct fscache_volume *volume = object->volume->vcookie;
617 struct cachefiles_open *load = (void *)req->msg.data;
618 size_t volume_key_size, cookie_key_size;
619 void *volume_key, *cookie_key;
622 * Volume key is a NUL-terminated string. key[0] stores strlen() of the
623 * string, followed by the content of the string (excluding '\0').
625 volume_key_size = volume->key[0] + 1;
626 volume_key = volume->key + 1;
628 /* Cookie key is binary data, which is netfs specific. */
629 cookie_key_size = cookie->key_len;
630 cookie_key = fscache_get_key(cookie);
632 if (!(object->cookie->advice & FSCACHE_ADV_WANT_CACHE_SIZE)) {
633 pr_err("WANT_CACHE_SIZE is needed for on-demand mode\n");
637 load->volume_key_size = volume_key_size;
638 load->cookie_key_size = cookie_key_size;
639 memcpy(load->data, volume_key, volume_key_size);
640 memcpy(load->data + volume_key_size, cookie_key, cookie_key_size);
645 static int cachefiles_ondemand_init_close_req(struct cachefiles_req *req,
648 struct cachefiles_object *object = req->object;
650 if (!cachefiles_ondemand_object_is_open(object))
653 trace_cachefiles_ondemand_close(object, &req->msg);
657 struct cachefiles_read_ctx {
662 static int cachefiles_ondemand_init_read_req(struct cachefiles_req *req,
665 struct cachefiles_object *object = req->object;
666 struct cachefiles_read *load = (void *)req->msg.data;
667 struct cachefiles_read_ctx *read_ctx = private;
669 load->off = read_ctx->off;
670 load->len = read_ctx->len;
671 trace_cachefiles_ondemand_read(object, &req->msg, load);
675 int cachefiles_ondemand_init_object(struct cachefiles_object *object)
677 struct fscache_cookie *cookie = object->cookie;
678 struct fscache_volume *volume = object->volume->vcookie;
679 size_t volume_key_size, cookie_key_size, data_len;
681 if (!object->ondemand)
685 * CacheFiles will firstly check the cache file under the root cache
686 * directory. If the coherency check failed, it will fallback to
687 * creating a new tmpfile as the cache file. Reuse the previously
688 * allocated object ID if any.
690 if (cachefiles_ondemand_object_is_open(object))
693 volume_key_size = volume->key[0] + 1;
694 cookie_key_size = cookie->key_len;
695 data_len = sizeof(struct cachefiles_open) +
696 volume_key_size + cookie_key_size;
698 return cachefiles_ondemand_send_req(object, CACHEFILES_OP_OPEN,
699 data_len, cachefiles_ondemand_init_open_req, NULL);
702 void cachefiles_ondemand_clean_object(struct cachefiles_object *object)
705 struct cachefiles_req *req;
706 struct cachefiles_cache *cache;
708 if (!object->ondemand)
711 cachefiles_ondemand_send_req(object, CACHEFILES_OP_CLOSE, 0,
712 cachefiles_ondemand_init_close_req, NULL);
714 if (!object->ondemand->ondemand_id)
717 /* Cancel all requests for the object that is being dropped. */
718 cache = object->volume->cache;
719 xa_lock(&cache->reqs);
720 cachefiles_ondemand_set_object_dropping(object);
721 xa_for_each(&cache->reqs, index, req) {
722 if (req->object == object) {
724 complete(&req->done);
725 __xa_erase(&cache->reqs, index);
728 xa_unlock(&cache->reqs);
730 /* Wait for ondemand_object_worker() to finish to avoid UAF. */
731 cancel_work_sync(&object->ondemand->ondemand_work);
734 int cachefiles_ondemand_init_obj_info(struct cachefiles_object *object,
735 struct cachefiles_volume *volume)
737 if (!cachefiles_in_ondemand_mode(volume->cache))
740 object->ondemand = kzalloc(sizeof(struct cachefiles_ondemand_info),
742 if (!object->ondemand)
745 object->ondemand->object = object;
746 spin_lock_init(&object->ondemand->lock);
747 INIT_WORK(&object->ondemand->ondemand_work, ondemand_object_worker);
751 void cachefiles_ondemand_deinit_obj_info(struct cachefiles_object *object)
753 kfree(object->ondemand);
754 object->ondemand = NULL;
757 int cachefiles_ondemand_read(struct cachefiles_object *object,
758 loff_t pos, size_t len)
760 struct cachefiles_read_ctx read_ctx = {pos, len};
762 return cachefiles_ondemand_send_req(object, CACHEFILES_OP_READ,
763 sizeof(struct cachefiles_read),
764 cachefiles_ondemand_init_read_req, &read_ctx);