1c8383054SJeffle Xu // SPDX-License-Identifier: GPL-2.0-or-later 2c8383054SJeffle Xu #include <linux/fdtable.h> 3c8383054SJeffle Xu #include <linux/anon_inodes.h> 4c8383054SJeffle Xu #include <linux/uio.h> 5c8383054SJeffle Xu #include "internal.h" 6c8383054SJeffle Xu 7c8383054SJeffle Xu static int cachefiles_ondemand_fd_release(struct inode *inode, 8c8383054SJeffle Xu struct file *file) 9c8383054SJeffle Xu { 10c8383054SJeffle Xu struct cachefiles_object *object = file->private_data; 11c8383054SJeffle Xu struct cachefiles_cache *cache = object->volume->cache; 1233d21f06SJia Zhu struct cachefiles_ondemand_info *info = object->ondemand; 13e564e48cSBaokun Li int object_id; 149032b6e8SJeffle Xu struct cachefiles_req *req; 159032b6e8SJeffle Xu XA_STATE(xas, &cache->reqs, 0); 16c8383054SJeffle Xu 179032b6e8SJeffle Xu xa_lock(&cache->reqs); 18e564e48cSBaokun Li spin_lock(&info->lock); 19e564e48cSBaokun Li object_id = info->ondemand_id; 2033d21f06SJia Zhu info->ondemand_id = CACHEFILES_ONDEMAND_ID_CLOSED; 21955190e1SJia Zhu cachefiles_ondemand_set_object_close(object); 22e564e48cSBaokun Li spin_unlock(&info->lock); 239032b6e8SJeffle Xu 24f740fd94SJia Zhu /* Only flush CACHEFILES_REQ_NEW marked req to avoid race with daemon_read */ 25f740fd94SJia Zhu xas_for_each_marked(&xas, req, ULONG_MAX, CACHEFILES_REQ_NEW) { 2665aa5f6fSJia Zhu if (req->msg.object_id == object_id && 27f740fd94SJia Zhu req->msg.opcode == CACHEFILES_OP_CLOSE) { 289032b6e8SJeffle Xu complete(&req->done); 299032b6e8SJeffle Xu xas_store(&xas, NULL); 309032b6e8SJeffle Xu } 319032b6e8SJeffle Xu } 329032b6e8SJeffle Xu xa_unlock(&cache->reqs); 339032b6e8SJeffle Xu 34c8383054SJeffle Xu xa_erase(&cache->ondemand_ids, object_id); 351519670eSJeffle Xu trace_cachefiles_ondemand_fd_release(object, object_id); 36c8383054SJeffle Xu cachefiles_put_object(object, cachefiles_obj_put_ondemand_fd); 37d11b0b04SJeffle Xu cachefiles_put_unbind_pincount(cache); 38c8383054SJeffle Xu return 0; 39c8383054SJeffle Xu } 40c8383054SJeffle Xu 41c8383054SJeffle Xu static ssize_t cachefiles_ondemand_fd_write_iter(struct kiocb *kiocb, 42c8383054SJeffle Xu struct iov_iter *iter) 43c8383054SJeffle Xu { 44c8383054SJeffle Xu struct cachefiles_object *object = kiocb->ki_filp->private_data; 45c8383054SJeffle Xu struct cachefiles_cache *cache = object->volume->cache; 46c8383054SJeffle Xu struct file *file = object->file; 47c8383054SJeffle Xu size_t len = iter->count; 48c8383054SJeffle Xu loff_t pos = kiocb->ki_pos; 49c8383054SJeffle Xu const struct cred *saved_cred; 50c8383054SJeffle Xu int ret; 51c8383054SJeffle Xu 52c8383054SJeffle Xu if (!file) 53c8383054SJeffle Xu return -ENOBUFS; 54c8383054SJeffle Xu 55c8383054SJeffle Xu cachefiles_begin_secure(cache, &saved_cred); 56c8383054SJeffle Xu ret = __cachefiles_prepare_write(object, file, &pos, &len, true); 57c8383054SJeffle Xu cachefiles_end_secure(cache, saved_cred); 58c8383054SJeffle Xu if (ret < 0) 59c8383054SJeffle Xu return ret; 60c8383054SJeffle Xu 611519670eSJeffle Xu trace_cachefiles_ondemand_fd_write(object, file_inode(file), pos, len); 62c8383054SJeffle Xu ret = __cachefiles_write(object, file, pos, iter, NULL, NULL); 63c8383054SJeffle Xu if (!ret) 64c8383054SJeffle Xu ret = len; 65c8383054SJeffle Xu 66c8383054SJeffle Xu return ret; 67c8383054SJeffle Xu } 68c8383054SJeffle Xu 69c8383054SJeffle Xu static loff_t cachefiles_ondemand_fd_llseek(struct file *filp, loff_t pos, 70c8383054SJeffle Xu int whence) 71c8383054SJeffle Xu { 72c8383054SJeffle Xu struct cachefiles_object *object = filp->private_data; 73c8383054SJeffle Xu struct file *file = object->file; 74c8383054SJeffle Xu 75c8383054SJeffle Xu if (!file) 76c8383054SJeffle Xu return -ENOBUFS; 77c8383054SJeffle Xu 78c8383054SJeffle Xu return vfs_llseek(file, pos, whence); 79c8383054SJeffle Xu } 80c8383054SJeffle Xu 819032b6e8SJeffle Xu static long cachefiles_ondemand_fd_ioctl(struct file *filp, unsigned int ioctl, 829032b6e8SJeffle Xu unsigned long arg) 839032b6e8SJeffle Xu { 849032b6e8SJeffle Xu struct cachefiles_object *object = filp->private_data; 859032b6e8SJeffle Xu struct cachefiles_cache *cache = object->volume->cache; 869032b6e8SJeffle Xu struct cachefiles_req *req; 879032b6e8SJeffle Xu unsigned long id; 889032b6e8SJeffle Xu 899032b6e8SJeffle Xu if (ioctl != CACHEFILES_IOC_READ_COMPLETE) 909032b6e8SJeffle Xu return -EINVAL; 919032b6e8SJeffle Xu 929032b6e8SJeffle Xu if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags)) 939032b6e8SJeffle Xu return -EOPNOTSUPP; 949032b6e8SJeffle Xu 959032b6e8SJeffle Xu id = arg; 969032b6e8SJeffle Xu req = xa_erase(&cache->reqs, id); 979032b6e8SJeffle Xu if (!req) 989032b6e8SJeffle Xu return -EINVAL; 999032b6e8SJeffle Xu 1001519670eSJeffle Xu trace_cachefiles_ondemand_cread(object, id); 1019032b6e8SJeffle Xu complete(&req->done); 1029032b6e8SJeffle Xu return 0; 1039032b6e8SJeffle Xu } 1049032b6e8SJeffle Xu 105c8383054SJeffle Xu static const struct file_operations cachefiles_ondemand_fd_fops = { 106c8383054SJeffle Xu .owner = THIS_MODULE, 107c8383054SJeffle Xu .release = cachefiles_ondemand_fd_release, 108c8383054SJeffle Xu .write_iter = cachefiles_ondemand_fd_write_iter, 109c8383054SJeffle Xu .llseek = cachefiles_ondemand_fd_llseek, 1109032b6e8SJeffle Xu .unlocked_ioctl = cachefiles_ondemand_fd_ioctl, 111c8383054SJeffle Xu }; 112c8383054SJeffle Xu 113c8383054SJeffle Xu /* 114c8383054SJeffle Xu * OPEN request Completion (copen) 115c8383054SJeffle Xu * - command: "copen <id>,<cache_size>" 116c8383054SJeffle Xu * <cache_size> indicates the object size if >=0, error code if negative 117c8383054SJeffle Xu */ 118c8383054SJeffle Xu int cachefiles_ondemand_copen(struct cachefiles_cache *cache, char *args) 119c8383054SJeffle Xu { 120c8383054SJeffle Xu struct cachefiles_req *req; 121c8383054SJeffle Xu struct fscache_cookie *cookie; 122e564e48cSBaokun Li struct cachefiles_ondemand_info *info; 123c8383054SJeffle Xu char *pid, *psize; 124c8383054SJeffle Xu unsigned long id; 125c8383054SJeffle Xu long size; 126c8383054SJeffle Xu int ret; 127c8383054SJeffle Xu 128c8383054SJeffle Xu if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags)) 129c8383054SJeffle Xu return -EOPNOTSUPP; 130c8383054SJeffle Xu 131c8383054SJeffle Xu if (!*args) { 132c8383054SJeffle Xu pr_err("Empty id specified\n"); 133c8383054SJeffle Xu return -EINVAL; 134c8383054SJeffle Xu } 135c8383054SJeffle Xu 136c8383054SJeffle Xu pid = args; 137c8383054SJeffle Xu psize = strchr(args, ','); 138c8383054SJeffle Xu if (!psize) { 139c8383054SJeffle Xu pr_err("Cache size is not specified\n"); 140c8383054SJeffle Xu return -EINVAL; 141c8383054SJeffle Xu } 142c8383054SJeffle Xu 143c8383054SJeffle Xu *psize = 0; 144c8383054SJeffle Xu psize++; 145c8383054SJeffle Xu 146c8383054SJeffle Xu ret = kstrtoul(pid, 0, &id); 147c8383054SJeffle Xu if (ret) 148c8383054SJeffle Xu return ret; 149c8383054SJeffle Xu 150c8383054SJeffle Xu req = xa_erase(&cache->reqs, id); 151c8383054SJeffle Xu if (!req) 152c8383054SJeffle Xu return -EINVAL; 153c8383054SJeffle Xu 154c8383054SJeffle Xu /* fail OPEN request if copen format is invalid */ 155c8383054SJeffle Xu ret = kstrtol(psize, 0, &size); 156c8383054SJeffle Xu if (ret) { 157c8383054SJeffle Xu req->error = ret; 158c8383054SJeffle Xu goto out; 159c8383054SJeffle Xu } 160c8383054SJeffle Xu 161c8383054SJeffle Xu /* fail OPEN request if daemon reports an error */ 162c8383054SJeffle Xu if (size < 0) { 163c93ccd63SSun Ke if (!IS_ERR_VALUE(size)) { 164c93ccd63SSun Ke req->error = -EINVAL; 165c93ccd63SSun Ke ret = -EINVAL; 166c93ccd63SSun Ke } else { 167c8383054SJeffle Xu req->error = size; 168c93ccd63SSun Ke ret = 0; 169c93ccd63SSun Ke } 170c8383054SJeffle Xu goto out; 171c8383054SJeffle Xu } 172c8383054SJeffle Xu 173e564e48cSBaokun Li info = req->object->ondemand; 174e564e48cSBaokun Li spin_lock(&info->lock); 175e564e48cSBaokun Li /* 176e564e48cSBaokun Li * The anonymous fd was closed before copen ? Fail the request. 177e564e48cSBaokun Li * 178e564e48cSBaokun Li * t1 | t2 179e564e48cSBaokun Li * --------------------------------------------------------- 180e564e48cSBaokun Li * cachefiles_ondemand_copen 181e564e48cSBaokun Li * req = xa_erase(&cache->reqs, id) 182e564e48cSBaokun Li * // Anon fd is maliciously closed. 183e564e48cSBaokun Li * cachefiles_ondemand_fd_release 184e564e48cSBaokun Li * xa_lock(&cache->reqs) 185e564e48cSBaokun Li * cachefiles_ondemand_set_object_close(object) 186e564e48cSBaokun Li * xa_unlock(&cache->reqs) 187e564e48cSBaokun Li * cachefiles_ondemand_set_object_open 188e564e48cSBaokun Li * // No one will ever close it again. 189e564e48cSBaokun Li * cachefiles_ondemand_daemon_read 190e564e48cSBaokun Li * cachefiles_ondemand_select_req 191e564e48cSBaokun Li * 192e564e48cSBaokun Li * Get a read req but its fd is already closed. The daemon can't 193e564e48cSBaokun Li * issue a cread ioctl with an closed fd, then hung. 194e564e48cSBaokun Li */ 195e564e48cSBaokun Li if (info->ondemand_id == CACHEFILES_ONDEMAND_ID_CLOSED) { 196e564e48cSBaokun Li spin_unlock(&info->lock); 197e564e48cSBaokun Li req->error = -EBADFD; 198e564e48cSBaokun Li goto out; 199e564e48cSBaokun Li } 200c8383054SJeffle Xu cookie = req->object->cookie; 201c8383054SJeffle Xu cookie->object_size = size; 202c8383054SJeffle Xu if (size) 203c8383054SJeffle Xu clear_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags); 204c8383054SJeffle Xu else 205c8383054SJeffle Xu set_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags); 2061519670eSJeffle Xu trace_cachefiles_ondemand_copen(req->object, id, size); 207c8383054SJeffle Xu 208955190e1SJia Zhu cachefiles_ondemand_set_object_open(req->object); 209e564e48cSBaokun Li spin_unlock(&info->lock); 210f740fd94SJia Zhu wake_up_all(&cache->daemon_pollwq); 211955190e1SJia Zhu 212c8383054SJeffle Xu out: 213c8383054SJeffle Xu complete(&req->done); 214c8383054SJeffle Xu return ret; 215c8383054SJeffle Xu } 216c8383054SJeffle Xu 217*9f5fa40fSJia Zhu int cachefiles_ondemand_restore(struct cachefiles_cache *cache, char *args) 218*9f5fa40fSJia Zhu { 219*9f5fa40fSJia Zhu struct cachefiles_req *req; 220*9f5fa40fSJia Zhu 221*9f5fa40fSJia Zhu XA_STATE(xas, &cache->reqs, 0); 222*9f5fa40fSJia Zhu 223*9f5fa40fSJia Zhu if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags)) 224*9f5fa40fSJia Zhu return -EOPNOTSUPP; 225*9f5fa40fSJia Zhu 226*9f5fa40fSJia Zhu /* 227*9f5fa40fSJia Zhu * Reset the requests to CACHEFILES_REQ_NEW state, so that the 228*9f5fa40fSJia Zhu * requests have been processed halfway before the crash of the 229*9f5fa40fSJia Zhu * user daemon could be reprocessed after the recovery. 230*9f5fa40fSJia Zhu */ 231*9f5fa40fSJia Zhu xas_lock(&xas); 232*9f5fa40fSJia Zhu xas_for_each(&xas, req, ULONG_MAX) 233*9f5fa40fSJia Zhu xas_set_mark(&xas, CACHEFILES_REQ_NEW); 234*9f5fa40fSJia Zhu xas_unlock(&xas); 235*9f5fa40fSJia Zhu 236*9f5fa40fSJia Zhu wake_up_all(&cache->daemon_pollwq); 237*9f5fa40fSJia Zhu return 0; 238*9f5fa40fSJia Zhu } 239*9f5fa40fSJia Zhu 240c8383054SJeffle Xu static int cachefiles_ondemand_get_fd(struct cachefiles_req *req) 241c8383054SJeffle Xu { 242c8383054SJeffle Xu struct cachefiles_object *object; 243c8383054SJeffle Xu struct cachefiles_cache *cache; 244c8383054SJeffle Xu struct cachefiles_open *load; 245c8383054SJeffle Xu struct file *file; 246c8383054SJeffle Xu u32 object_id; 247c8383054SJeffle Xu int ret, fd; 248c8383054SJeffle Xu 249c8383054SJeffle Xu object = cachefiles_grab_object(req->object, 250c8383054SJeffle Xu cachefiles_obj_get_ondemand_fd); 251c8383054SJeffle Xu cache = object->volume->cache; 252c8383054SJeffle Xu 253c8383054SJeffle Xu ret = xa_alloc_cyclic(&cache->ondemand_ids, &object_id, NULL, 254c8383054SJeffle Xu XA_LIMIT(1, INT_MAX), 255c8383054SJeffle Xu &cache->ondemand_id_next, GFP_KERNEL); 256c8383054SJeffle Xu if (ret < 0) 257c8383054SJeffle Xu goto err; 258c8383054SJeffle Xu 259c8383054SJeffle Xu fd = get_unused_fd_flags(O_WRONLY); 260c8383054SJeffle Xu if (fd < 0) { 261c8383054SJeffle Xu ret = fd; 262c8383054SJeffle Xu goto err_free_id; 263c8383054SJeffle Xu } 264c8383054SJeffle Xu 265c8383054SJeffle Xu file = anon_inode_getfile("[cachefiles]", &cachefiles_ondemand_fd_fops, 266c8383054SJeffle Xu object, O_WRONLY); 267c8383054SJeffle Xu if (IS_ERR(file)) { 268c8383054SJeffle Xu ret = PTR_ERR(file); 269c8383054SJeffle Xu goto err_put_fd; 270c8383054SJeffle Xu } 271c8383054SJeffle Xu 272c8383054SJeffle Xu file->f_mode |= FMODE_PWRITE | FMODE_LSEEK; 273c8383054SJeffle Xu fd_install(fd, file); 274c8383054SJeffle Xu 275c8383054SJeffle Xu load = (void *)req->msg.data; 276c8383054SJeffle Xu load->fd = fd; 27733d21f06SJia Zhu object->ondemand->ondemand_id = object_id; 278d11b0b04SJeffle Xu 279d11b0b04SJeffle Xu cachefiles_get_unbind_pincount(cache); 2801519670eSJeffle Xu trace_cachefiles_ondemand_open(object, &req->msg, load); 281c8383054SJeffle Xu return 0; 282c8383054SJeffle Xu 283c8383054SJeffle Xu err_put_fd: 284c8383054SJeffle Xu put_unused_fd(fd); 285c8383054SJeffle Xu err_free_id: 286c8383054SJeffle Xu xa_erase(&cache->ondemand_ids, object_id); 287c8383054SJeffle Xu err: 288c8383054SJeffle Xu cachefiles_put_object(object, cachefiles_obj_put_ondemand_fd); 289c8383054SJeffle Xu return ret; 290c8383054SJeffle Xu } 291c8383054SJeffle Xu 292f740fd94SJia Zhu static void ondemand_object_worker(struct work_struct *work) 293f740fd94SJia Zhu { 294f740fd94SJia Zhu struct cachefiles_ondemand_info *info = 295f740fd94SJia Zhu container_of(work, struct cachefiles_ondemand_info, ondemand_work); 296f740fd94SJia Zhu 297f740fd94SJia Zhu cachefiles_ondemand_init_object(info->object); 298f740fd94SJia Zhu } 299f740fd94SJia Zhu 300f740fd94SJia Zhu /* 301f740fd94SJia Zhu * If there are any inflight or subsequent READ requests on the 302f740fd94SJia Zhu * closed object, reopen it. 303f740fd94SJia Zhu * Skip read requests whose related object is reopening. 304f740fd94SJia Zhu */ 305f740fd94SJia Zhu static struct cachefiles_req *cachefiles_ondemand_select_req(struct xa_state *xas, 306f740fd94SJia Zhu unsigned long xa_max) 307f740fd94SJia Zhu { 308f740fd94SJia Zhu struct cachefiles_req *req; 309f740fd94SJia Zhu struct cachefiles_object *object; 310f740fd94SJia Zhu struct cachefiles_ondemand_info *info; 311f740fd94SJia Zhu 312f740fd94SJia Zhu xas_for_each_marked(xas, req, xa_max, CACHEFILES_REQ_NEW) { 313f740fd94SJia Zhu if (req->msg.opcode != CACHEFILES_OP_READ) 314f740fd94SJia Zhu return req; 315f740fd94SJia Zhu object = req->object; 316f740fd94SJia Zhu info = object->ondemand; 317f740fd94SJia Zhu if (cachefiles_ondemand_object_is_close(object)) { 318f740fd94SJia Zhu cachefiles_ondemand_set_object_reopening(object); 319f740fd94SJia Zhu queue_work(fscache_wq, &info->ondemand_work); 320f740fd94SJia Zhu continue; 321f740fd94SJia Zhu } 322f740fd94SJia Zhu if (cachefiles_ondemand_object_is_reopening(object)) 323f740fd94SJia Zhu continue; 324f740fd94SJia Zhu return req; 325f740fd94SJia Zhu } 326f740fd94SJia Zhu return NULL; 327f740fd94SJia Zhu } 328f740fd94SJia Zhu 329c8383054SJeffle Xu ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache, 330c8383054SJeffle Xu char __user *_buffer, size_t buflen) 331c8383054SJeffle Xu { 332c8383054SJeffle Xu struct cachefiles_req *req; 333c8383054SJeffle Xu struct cachefiles_msg *msg; 334c8383054SJeffle Xu unsigned long id = 0; 335c8383054SJeffle Xu size_t n; 336c8383054SJeffle Xu int ret = 0; 3371122f400SXin Yin XA_STATE(xas, &cache->reqs, cache->req_id_next); 338c8383054SJeffle Xu 339f740fd94SJia Zhu xa_lock(&cache->reqs); 340c8383054SJeffle Xu /* 3411122f400SXin Yin * Cyclically search for a request that has not ever been processed, 3421122f400SXin Yin * to prevent requests from being processed repeatedly, and make 3431122f400SXin Yin * request distribution fair. 344c8383054SJeffle Xu */ 345f740fd94SJia Zhu req = cachefiles_ondemand_select_req(&xas, ULONG_MAX); 3461122f400SXin Yin if (!req && cache->req_id_next > 0) { 3471122f400SXin Yin xas_set(&xas, 0); 348f740fd94SJia Zhu req = cachefiles_ondemand_select_req(&xas, cache->req_id_next - 1); 3491122f400SXin Yin } 350c8383054SJeffle Xu if (!req) { 351c8383054SJeffle Xu xa_unlock(&cache->reqs); 352c8383054SJeffle Xu return 0; 353c8383054SJeffle Xu } 354c8383054SJeffle Xu 355c8383054SJeffle Xu msg = &req->msg; 356c8383054SJeffle Xu n = msg->len; 357c8383054SJeffle Xu 358c8383054SJeffle Xu if (n > buflen) { 359c8383054SJeffle Xu xa_unlock(&cache->reqs); 360c8383054SJeffle Xu return -EMSGSIZE; 361c8383054SJeffle Xu } 362c8383054SJeffle Xu 363c8383054SJeffle Xu xas_clear_mark(&xas, CACHEFILES_REQ_NEW); 3641122f400SXin Yin cache->req_id_next = xas.xa_index + 1; 365c8383054SJeffle Xu xa_unlock(&cache->reqs); 366c8383054SJeffle Xu 367c8383054SJeffle Xu id = xas.xa_index; 368c8383054SJeffle Xu 369c8383054SJeffle Xu if (msg->opcode == CACHEFILES_OP_OPEN) { 370c8383054SJeffle Xu ret = cachefiles_ondemand_get_fd(req); 371f740fd94SJia Zhu if (ret) { 372f740fd94SJia Zhu cachefiles_ondemand_set_object_close(req->object); 373c8383054SJeffle Xu goto error; 374c8383054SJeffle Xu } 375f740fd94SJia Zhu } 376f740fd94SJia Zhu 377f740fd94SJia Zhu msg->msg_id = id; 378f740fd94SJia Zhu msg->object_id = req->object->ondemand->ondemand_id; 379c8383054SJeffle Xu 380c8383054SJeffle Xu if (copy_to_user(_buffer, msg, n) != 0) { 381c8383054SJeffle Xu ret = -EFAULT; 382c8383054SJeffle Xu goto err_put_fd; 383c8383054SJeffle Xu } 384c8383054SJeffle Xu 385324b954aSJeffle Xu /* CLOSE request has no reply */ 386324b954aSJeffle Xu if (msg->opcode == CACHEFILES_OP_CLOSE) { 387324b954aSJeffle Xu xa_erase(&cache->reqs, id); 388324b954aSJeffle Xu complete(&req->done); 389324b954aSJeffle Xu } 390324b954aSJeffle Xu 391c8383054SJeffle Xu return n; 392c8383054SJeffle Xu 393c8383054SJeffle Xu err_put_fd: 394c8383054SJeffle Xu if (msg->opcode == CACHEFILES_OP_OPEN) 395c8383054SJeffle Xu close_fd(((struct cachefiles_open *)msg->data)->fd); 396c8383054SJeffle Xu error: 397c8383054SJeffle Xu xa_erase(&cache->reqs, id); 398c8383054SJeffle Xu req->error = ret; 399c8383054SJeffle Xu complete(&req->done); 400c8383054SJeffle Xu return ret; 401c8383054SJeffle Xu } 402c8383054SJeffle Xu 403c8383054SJeffle Xu typedef int (*init_req_fn)(struct cachefiles_req *req, void *private); 404c8383054SJeffle Xu 405c8383054SJeffle Xu static int cachefiles_ondemand_send_req(struct cachefiles_object *object, 406c8383054SJeffle Xu enum cachefiles_opcode opcode, 407c8383054SJeffle Xu size_t data_len, 408c8383054SJeffle Xu init_req_fn init_req, 409c8383054SJeffle Xu void *private) 410c8383054SJeffle Xu { 411c8383054SJeffle Xu struct cachefiles_cache *cache = object->volume->cache; 412f740fd94SJia Zhu struct cachefiles_req *req = NULL; 413c8383054SJeffle Xu XA_STATE(xas, &cache->reqs, 0); 414c8383054SJeffle Xu int ret; 415c8383054SJeffle Xu 416c8383054SJeffle Xu if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags)) 417c8383054SJeffle Xu return 0; 418c8383054SJeffle Xu 419f740fd94SJia Zhu if (test_bit(CACHEFILES_DEAD, &cache->flags)) { 420f740fd94SJia Zhu ret = -EIO; 421f740fd94SJia Zhu goto out; 422f740fd94SJia Zhu } 423c8383054SJeffle Xu 424c8383054SJeffle Xu req = kzalloc(sizeof(*req) + data_len, GFP_KERNEL); 425f740fd94SJia Zhu if (!req) { 426f740fd94SJia Zhu ret = -ENOMEM; 427f740fd94SJia Zhu goto out; 428f740fd94SJia Zhu } 429c8383054SJeffle Xu 430c8383054SJeffle Xu req->object = object; 431c8383054SJeffle Xu init_completion(&req->done); 432c8383054SJeffle Xu req->msg.opcode = opcode; 433c8383054SJeffle Xu req->msg.len = sizeof(struct cachefiles_msg) + data_len; 434c8383054SJeffle Xu 435c8383054SJeffle Xu ret = init_req(req, private); 436c8383054SJeffle Xu if (ret) 437c8383054SJeffle Xu goto out; 438c8383054SJeffle Xu 439c8383054SJeffle Xu do { 440c8383054SJeffle Xu /* 441c8383054SJeffle Xu * Stop enqueuing the request when daemon is dying. The 442c8383054SJeffle Xu * following two operations need to be atomic as a whole. 443c8383054SJeffle Xu * 1) check cache state, and 444c8383054SJeffle Xu * 2) enqueue request if cache is alive. 445c8383054SJeffle Xu * Otherwise the request may be enqueued after xarray has been 446c8383054SJeffle Xu * flushed, leaving the orphan request never being completed. 447c8383054SJeffle Xu * 448c8383054SJeffle Xu * CPU 1 CPU 2 449c8383054SJeffle Xu * ===== ===== 450c8383054SJeffle Xu * test CACHEFILES_DEAD bit 451c8383054SJeffle Xu * set CACHEFILES_DEAD bit 452c8383054SJeffle Xu * flush requests in the xarray 453c8383054SJeffle Xu * enqueue the request 454c8383054SJeffle Xu */ 455c8383054SJeffle Xu xas_lock(&xas); 456c8383054SJeffle Xu 457c8383054SJeffle Xu if (test_bit(CACHEFILES_DEAD, &cache->flags)) { 458c8383054SJeffle Xu xas_unlock(&xas); 459c8383054SJeffle Xu ret = -EIO; 460c8383054SJeffle Xu goto out; 461c8383054SJeffle Xu } 462c8383054SJeffle Xu 463c8383054SJeffle Xu /* coupled with the barrier in cachefiles_flush_reqs() */ 464c8383054SJeffle Xu smp_mb(); 465c8383054SJeffle Xu 466f740fd94SJia Zhu if (opcode == CACHEFILES_OP_CLOSE && 467955190e1SJia Zhu !cachefiles_ondemand_object_is_open(object)) { 46833d21f06SJia Zhu WARN_ON_ONCE(object->ondemand->ondemand_id == 0); 469324b954aSJeffle Xu xas_unlock(&xas); 470324b954aSJeffle Xu ret = -EIO; 471324b954aSJeffle Xu goto out; 472324b954aSJeffle Xu } 473324b954aSJeffle Xu 474c8383054SJeffle Xu xas.xa_index = 0; 475c8383054SJeffle Xu xas_find_marked(&xas, UINT_MAX, XA_FREE_MARK); 476c8383054SJeffle Xu if (xas.xa_node == XAS_RESTART) 477c8383054SJeffle Xu xas_set_err(&xas, -EBUSY); 478c8383054SJeffle Xu xas_store(&xas, req); 479c8383054SJeffle Xu xas_clear_mark(&xas, XA_FREE_MARK); 480c8383054SJeffle Xu xas_set_mark(&xas, CACHEFILES_REQ_NEW); 481c8383054SJeffle Xu xas_unlock(&xas); 482c8383054SJeffle Xu } while (xas_nomem(&xas, GFP_KERNEL)); 483c8383054SJeffle Xu 484c8383054SJeffle Xu ret = xas_error(&xas); 485c8383054SJeffle Xu if (ret) 486c8383054SJeffle Xu goto out; 487c8383054SJeffle Xu 488c8383054SJeffle Xu wake_up_all(&cache->daemon_pollwq); 489c8383054SJeffle Xu wait_for_completion(&req->done); 490c8383054SJeffle Xu ret = req->error; 491f740fd94SJia Zhu kfree(req); 492f740fd94SJia Zhu return ret; 493c8383054SJeffle Xu out: 494f740fd94SJia Zhu /* Reset the object to close state in error handling path. 495f740fd94SJia Zhu * If error occurs after creating the anonymous fd, 496f740fd94SJia Zhu * cachefiles_ondemand_fd_release() will set object to close. 497f740fd94SJia Zhu */ 498f740fd94SJia Zhu if (opcode == CACHEFILES_OP_OPEN) 499f740fd94SJia Zhu cachefiles_ondemand_set_object_close(object); 500c8383054SJeffle Xu kfree(req); 501c8383054SJeffle Xu return ret; 502c8383054SJeffle Xu } 503c8383054SJeffle Xu 504c8383054SJeffle Xu static int cachefiles_ondemand_init_open_req(struct cachefiles_req *req, 505c8383054SJeffle Xu void *private) 506c8383054SJeffle Xu { 507c8383054SJeffle Xu struct cachefiles_object *object = req->object; 508c8383054SJeffle Xu struct fscache_cookie *cookie = object->cookie; 509c8383054SJeffle Xu struct fscache_volume *volume = object->volume->vcookie; 510c8383054SJeffle Xu struct cachefiles_open *load = (void *)req->msg.data; 511c8383054SJeffle Xu size_t volume_key_size, cookie_key_size; 512c8383054SJeffle Xu void *volume_key, *cookie_key; 513c8383054SJeffle Xu 514c8383054SJeffle Xu /* 515c8383054SJeffle Xu * Volume key is a NUL-terminated string. key[0] stores strlen() of the 516c8383054SJeffle Xu * string, followed by the content of the string (excluding '\0'). 517c8383054SJeffle Xu */ 518c8383054SJeffle Xu volume_key_size = volume->key[0] + 1; 519c8383054SJeffle Xu volume_key = volume->key + 1; 520c8383054SJeffle Xu 521c8383054SJeffle Xu /* Cookie key is binary data, which is netfs specific. */ 522c8383054SJeffle Xu cookie_key_size = cookie->key_len; 523c8383054SJeffle Xu cookie_key = fscache_get_key(cookie); 524c8383054SJeffle Xu 525c8383054SJeffle Xu if (!(object->cookie->advice & FSCACHE_ADV_WANT_CACHE_SIZE)) { 526c8383054SJeffle Xu pr_err("WANT_CACHE_SIZE is needed for on-demand mode\n"); 527c8383054SJeffle Xu return -EINVAL; 528c8383054SJeffle Xu } 529c8383054SJeffle Xu 530c8383054SJeffle Xu load->volume_key_size = volume_key_size; 531c8383054SJeffle Xu load->cookie_key_size = cookie_key_size; 532c8383054SJeffle Xu memcpy(load->data, volume_key, volume_key_size); 533c8383054SJeffle Xu memcpy(load->data + volume_key_size, cookie_key, cookie_key_size); 534c8383054SJeffle Xu 535c8383054SJeffle Xu return 0; 536c8383054SJeffle Xu } 537c8383054SJeffle Xu 538324b954aSJeffle Xu static int cachefiles_ondemand_init_close_req(struct cachefiles_req *req, 539324b954aSJeffle Xu void *private) 540324b954aSJeffle Xu { 541324b954aSJeffle Xu struct cachefiles_object *object = req->object; 542324b954aSJeffle Xu 543955190e1SJia Zhu if (!cachefiles_ondemand_object_is_open(object)) 544324b954aSJeffle Xu return -ENOENT; 545324b954aSJeffle Xu 5461519670eSJeffle Xu trace_cachefiles_ondemand_close(object, &req->msg); 547324b954aSJeffle Xu return 0; 548324b954aSJeffle Xu } 549324b954aSJeffle Xu 5509032b6e8SJeffle Xu struct cachefiles_read_ctx { 5519032b6e8SJeffle Xu loff_t off; 5529032b6e8SJeffle Xu size_t len; 5539032b6e8SJeffle Xu }; 5549032b6e8SJeffle Xu 5559032b6e8SJeffle Xu static int cachefiles_ondemand_init_read_req(struct cachefiles_req *req, 5569032b6e8SJeffle Xu void *private) 5579032b6e8SJeffle Xu { 5589032b6e8SJeffle Xu struct cachefiles_object *object = req->object; 5599032b6e8SJeffle Xu struct cachefiles_read *load = (void *)req->msg.data; 5609032b6e8SJeffle Xu struct cachefiles_read_ctx *read_ctx = private; 5619032b6e8SJeffle Xu 5629032b6e8SJeffle Xu load->off = read_ctx->off; 5639032b6e8SJeffle Xu load->len = read_ctx->len; 5641519670eSJeffle Xu trace_cachefiles_ondemand_read(object, &req->msg, load); 5659032b6e8SJeffle Xu return 0; 5669032b6e8SJeffle Xu } 5679032b6e8SJeffle Xu 568c8383054SJeffle Xu int cachefiles_ondemand_init_object(struct cachefiles_object *object) 569c8383054SJeffle Xu { 570c8383054SJeffle Xu struct fscache_cookie *cookie = object->cookie; 571c8383054SJeffle Xu struct fscache_volume *volume = object->volume->vcookie; 572c8383054SJeffle Xu size_t volume_key_size, cookie_key_size, data_len; 573c8383054SJeffle Xu 574c8383054SJeffle Xu /* 575c8383054SJeffle Xu * CacheFiles will firstly check the cache file under the root cache 576c8383054SJeffle Xu * directory. If the coherency check failed, it will fallback to 577c8383054SJeffle Xu * creating a new tmpfile as the cache file. Reuse the previously 578c8383054SJeffle Xu * allocated object ID if any. 579c8383054SJeffle Xu */ 580955190e1SJia Zhu if (cachefiles_ondemand_object_is_open(object)) 581c8383054SJeffle Xu return 0; 582c8383054SJeffle Xu 583c8383054SJeffle Xu volume_key_size = volume->key[0] + 1; 584c8383054SJeffle Xu cookie_key_size = cookie->key_len; 585c8383054SJeffle Xu data_len = sizeof(struct cachefiles_open) + 586c8383054SJeffle Xu volume_key_size + cookie_key_size; 587c8383054SJeffle Xu 588c8383054SJeffle Xu return cachefiles_ondemand_send_req(object, CACHEFILES_OP_OPEN, 589c8383054SJeffle Xu data_len, cachefiles_ondemand_init_open_req, NULL); 590c8383054SJeffle Xu } 591324b954aSJeffle Xu 592324b954aSJeffle Xu void cachefiles_ondemand_clean_object(struct cachefiles_object *object) 593324b954aSJeffle Xu { 594324b954aSJeffle Xu cachefiles_ondemand_send_req(object, CACHEFILES_OP_CLOSE, 0, 595324b954aSJeffle Xu cachefiles_ondemand_init_close_req, NULL); 596324b954aSJeffle Xu } 5979032b6e8SJeffle Xu 59833d21f06SJia Zhu int cachefiles_ondemand_init_obj_info(struct cachefiles_object *object, 59933d21f06SJia Zhu struct cachefiles_volume *volume) 60033d21f06SJia Zhu { 60133d21f06SJia Zhu if (!cachefiles_in_ondemand_mode(volume->cache)) 60233d21f06SJia Zhu return 0; 60333d21f06SJia Zhu 60433d21f06SJia Zhu object->ondemand = kzalloc(sizeof(struct cachefiles_ondemand_info), 60533d21f06SJia Zhu GFP_KERNEL); 60633d21f06SJia Zhu if (!object->ondemand) 60733d21f06SJia Zhu return -ENOMEM; 60833d21f06SJia Zhu 60933d21f06SJia Zhu object->ondemand->object = object; 610e564e48cSBaokun Li spin_lock_init(&object->ondemand->lock); 611f740fd94SJia Zhu INIT_WORK(&object->ondemand->ondemand_work, ondemand_object_worker); 61233d21f06SJia Zhu return 0; 61333d21f06SJia Zhu } 61433d21f06SJia Zhu 61533d21f06SJia Zhu void cachefiles_ondemand_deinit_obj_info(struct cachefiles_object *object) 61633d21f06SJia Zhu { 61733d21f06SJia Zhu kfree(object->ondemand); 61833d21f06SJia Zhu object->ondemand = NULL; 61933d21f06SJia Zhu } 62033d21f06SJia Zhu 6219032b6e8SJeffle Xu int cachefiles_ondemand_read(struct cachefiles_object *object, 6229032b6e8SJeffle Xu loff_t pos, size_t len) 6239032b6e8SJeffle Xu { 6249032b6e8SJeffle Xu struct cachefiles_read_ctx read_ctx = {pos, len}; 6259032b6e8SJeffle Xu 6269032b6e8SJeffle Xu return cachefiles_ondemand_send_req(object, CACHEFILES_OP_READ, 6279032b6e8SJeffle Xu sizeof(struct cachefiles_read), 6289032b6e8SJeffle Xu cachefiles_ondemand_init_read_req, &read_ctx); 6299032b6e8SJeffle Xu } 630