xref: /openbmc/linux/fs/cachefiles/ondemand.c (revision 955190e1851afb386309e6affcf1a127a9ea0204)
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;
12c8383054SJeffle Xu 	int object_id = object->ondemand_id;
139032b6e8SJeffle Xu 	struct cachefiles_req *req;
149032b6e8SJeffle Xu 	XA_STATE(xas, &cache->reqs, 0);
15c8383054SJeffle Xu 
169032b6e8SJeffle Xu 	xa_lock(&cache->reqs);
17c8383054SJeffle Xu 	object->ondemand_id = CACHEFILES_ONDEMAND_ID_CLOSED;
18*955190e1SJia Zhu 	cachefiles_ondemand_set_object_close(object);
199032b6e8SJeffle Xu 
209032b6e8SJeffle Xu 	/*
219032b6e8SJeffle Xu 	 * Flush all pending READ requests since their completion depends on
229032b6e8SJeffle Xu 	 * anon_fd.
239032b6e8SJeffle Xu 	 */
249032b6e8SJeffle Xu 	xas_for_each(&xas, req, ULONG_MAX) {
2565aa5f6fSJia Zhu 		if (req->msg.object_id == object_id &&
2665aa5f6fSJia Zhu 		    req->msg.opcode == CACHEFILES_OP_READ) {
279032b6e8SJeffle Xu 			req->error = -EIO;
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;
122c8383054SJeffle Xu 	char *pid, *psize;
123c8383054SJeffle Xu 	unsigned long id;
124c8383054SJeffle Xu 	long size;
125c8383054SJeffle Xu 	int ret;
126c8383054SJeffle Xu 
127c8383054SJeffle Xu 	if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags))
128c8383054SJeffle Xu 		return -EOPNOTSUPP;
129c8383054SJeffle Xu 
130c8383054SJeffle Xu 	if (!*args) {
131c8383054SJeffle Xu 		pr_err("Empty id specified\n");
132c8383054SJeffle Xu 		return -EINVAL;
133c8383054SJeffle Xu 	}
134c8383054SJeffle Xu 
135c8383054SJeffle Xu 	pid = args;
136c8383054SJeffle Xu 	psize = strchr(args, ',');
137c8383054SJeffle Xu 	if (!psize) {
138c8383054SJeffle Xu 		pr_err("Cache size is not specified\n");
139c8383054SJeffle Xu 		return -EINVAL;
140c8383054SJeffle Xu 	}
141c8383054SJeffle Xu 
142c8383054SJeffle Xu 	*psize = 0;
143c8383054SJeffle Xu 	psize++;
144c8383054SJeffle Xu 
145c8383054SJeffle Xu 	ret = kstrtoul(pid, 0, &id);
146c8383054SJeffle Xu 	if (ret)
147c8383054SJeffle Xu 		return ret;
148c8383054SJeffle Xu 
149c8383054SJeffle Xu 	req = xa_erase(&cache->reqs, id);
150c8383054SJeffle Xu 	if (!req)
151c8383054SJeffle Xu 		return -EINVAL;
152c8383054SJeffle Xu 
153c8383054SJeffle Xu 	/* fail OPEN request if copen format is invalid */
154c8383054SJeffle Xu 	ret = kstrtol(psize, 0, &size);
155c8383054SJeffle Xu 	if (ret) {
156c8383054SJeffle Xu 		req->error = ret;
157c8383054SJeffle Xu 		goto out;
158c8383054SJeffle Xu 	}
159c8383054SJeffle Xu 
160c8383054SJeffle Xu 	/* fail OPEN request if daemon reports an error */
161c8383054SJeffle Xu 	if (size < 0) {
162c93ccd63SSun Ke 		if (!IS_ERR_VALUE(size)) {
163c93ccd63SSun Ke 			req->error = -EINVAL;
164c93ccd63SSun Ke 			ret = -EINVAL;
165c93ccd63SSun Ke 		} else {
166c8383054SJeffle Xu 			req->error = size;
167c93ccd63SSun Ke 			ret = 0;
168c93ccd63SSun Ke 		}
169c8383054SJeffle Xu 		goto out;
170c8383054SJeffle Xu 	}
171c8383054SJeffle Xu 
172c8383054SJeffle Xu 	cookie = req->object->cookie;
173c8383054SJeffle Xu 	cookie->object_size = size;
174c8383054SJeffle Xu 	if (size)
175c8383054SJeffle Xu 		clear_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags);
176c8383054SJeffle Xu 	else
177c8383054SJeffle Xu 		set_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags);
1781519670eSJeffle Xu 	trace_cachefiles_ondemand_copen(req->object, id, size);
179c8383054SJeffle Xu 
180*955190e1SJia Zhu 	cachefiles_ondemand_set_object_open(req->object);
181*955190e1SJia Zhu 
182c8383054SJeffle Xu out:
183c8383054SJeffle Xu 	complete(&req->done);
184c8383054SJeffle Xu 	return ret;
185c8383054SJeffle Xu }
186c8383054SJeffle Xu 
187c8383054SJeffle Xu static int cachefiles_ondemand_get_fd(struct cachefiles_req *req)
188c8383054SJeffle Xu {
189c8383054SJeffle Xu 	struct cachefiles_object *object;
190c8383054SJeffle Xu 	struct cachefiles_cache *cache;
191c8383054SJeffle Xu 	struct cachefiles_open *load;
192c8383054SJeffle Xu 	struct file *file;
193c8383054SJeffle Xu 	u32 object_id;
194c8383054SJeffle Xu 	int ret, fd;
195c8383054SJeffle Xu 
196c8383054SJeffle Xu 	object = cachefiles_grab_object(req->object,
197c8383054SJeffle Xu 			cachefiles_obj_get_ondemand_fd);
198c8383054SJeffle Xu 	cache = object->volume->cache;
199c8383054SJeffle Xu 
200c8383054SJeffle Xu 	ret = xa_alloc_cyclic(&cache->ondemand_ids, &object_id, NULL,
201c8383054SJeffle Xu 			      XA_LIMIT(1, INT_MAX),
202c8383054SJeffle Xu 			      &cache->ondemand_id_next, GFP_KERNEL);
203c8383054SJeffle Xu 	if (ret < 0)
204c8383054SJeffle Xu 		goto err;
205c8383054SJeffle Xu 
206c8383054SJeffle Xu 	fd = get_unused_fd_flags(O_WRONLY);
207c8383054SJeffle Xu 	if (fd < 0) {
208c8383054SJeffle Xu 		ret = fd;
209c8383054SJeffle Xu 		goto err_free_id;
210c8383054SJeffle Xu 	}
211c8383054SJeffle Xu 
212c8383054SJeffle Xu 	file = anon_inode_getfile("[cachefiles]", &cachefiles_ondemand_fd_fops,
213c8383054SJeffle Xu 				  object, O_WRONLY);
214c8383054SJeffle Xu 	if (IS_ERR(file)) {
215c8383054SJeffle Xu 		ret = PTR_ERR(file);
216c8383054SJeffle Xu 		goto err_put_fd;
217c8383054SJeffle Xu 	}
218c8383054SJeffle Xu 
219c8383054SJeffle Xu 	file->f_mode |= FMODE_PWRITE | FMODE_LSEEK;
220c8383054SJeffle Xu 	fd_install(fd, file);
221c8383054SJeffle Xu 
222c8383054SJeffle Xu 	load = (void *)req->msg.data;
223c8383054SJeffle Xu 	load->fd = fd;
224c8383054SJeffle Xu 	req->msg.object_id = object_id;
225c8383054SJeffle Xu 	object->ondemand_id = object_id;
226d11b0b04SJeffle Xu 
227d11b0b04SJeffle Xu 	cachefiles_get_unbind_pincount(cache);
2281519670eSJeffle Xu 	trace_cachefiles_ondemand_open(object, &req->msg, load);
229c8383054SJeffle Xu 	return 0;
230c8383054SJeffle Xu 
231c8383054SJeffle Xu err_put_fd:
232c8383054SJeffle Xu 	put_unused_fd(fd);
233c8383054SJeffle Xu err_free_id:
234c8383054SJeffle Xu 	xa_erase(&cache->ondemand_ids, object_id);
235c8383054SJeffle Xu err:
236c8383054SJeffle Xu 	cachefiles_put_object(object, cachefiles_obj_put_ondemand_fd);
237c8383054SJeffle Xu 	return ret;
238c8383054SJeffle Xu }
239c8383054SJeffle Xu 
240c8383054SJeffle Xu ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache,
241c8383054SJeffle Xu 					char __user *_buffer, size_t buflen)
242c8383054SJeffle Xu {
243c8383054SJeffle Xu 	struct cachefiles_req *req;
244c8383054SJeffle Xu 	struct cachefiles_msg *msg;
245c8383054SJeffle Xu 	unsigned long id = 0;
246c8383054SJeffle Xu 	size_t n;
247c8383054SJeffle Xu 	int ret = 0;
2481122f400SXin Yin 	XA_STATE(xas, &cache->reqs, cache->req_id_next);
249c8383054SJeffle Xu 
250c8383054SJeffle Xu 	/*
2511122f400SXin Yin 	 * Cyclically search for a request that has not ever been processed,
2521122f400SXin Yin 	 * to prevent requests from being processed repeatedly, and make
2531122f400SXin Yin 	 * request distribution fair.
254c8383054SJeffle Xu 	 */
255c8383054SJeffle Xu 	xa_lock(&cache->reqs);
256c8383054SJeffle Xu 	req = xas_find_marked(&xas, UINT_MAX, CACHEFILES_REQ_NEW);
2571122f400SXin Yin 	if (!req && cache->req_id_next > 0) {
2581122f400SXin Yin 		xas_set(&xas, 0);
2591122f400SXin Yin 		req = xas_find_marked(&xas, cache->req_id_next - 1, CACHEFILES_REQ_NEW);
2601122f400SXin Yin 	}
261c8383054SJeffle Xu 	if (!req) {
262c8383054SJeffle Xu 		xa_unlock(&cache->reqs);
263c8383054SJeffle Xu 		return 0;
264c8383054SJeffle Xu 	}
265c8383054SJeffle Xu 
266c8383054SJeffle Xu 	msg = &req->msg;
267c8383054SJeffle Xu 	n = msg->len;
268c8383054SJeffle Xu 
269c8383054SJeffle Xu 	if (n > buflen) {
270c8383054SJeffle Xu 		xa_unlock(&cache->reqs);
271c8383054SJeffle Xu 		return -EMSGSIZE;
272c8383054SJeffle Xu 	}
273c8383054SJeffle Xu 
274c8383054SJeffle Xu 	xas_clear_mark(&xas, CACHEFILES_REQ_NEW);
2751122f400SXin Yin 	cache->req_id_next = xas.xa_index + 1;
276c8383054SJeffle Xu 	xa_unlock(&cache->reqs);
277c8383054SJeffle Xu 
278c8383054SJeffle Xu 	id = xas.xa_index;
279c8383054SJeffle Xu 	msg->msg_id = id;
280c8383054SJeffle Xu 
281c8383054SJeffle Xu 	if (msg->opcode == CACHEFILES_OP_OPEN) {
282c8383054SJeffle Xu 		ret = cachefiles_ondemand_get_fd(req);
283c8383054SJeffle Xu 		if (ret)
284c8383054SJeffle Xu 			goto error;
285c8383054SJeffle Xu 	}
286c8383054SJeffle Xu 
287c8383054SJeffle Xu 	if (copy_to_user(_buffer, msg, n) != 0) {
288c8383054SJeffle Xu 		ret = -EFAULT;
289c8383054SJeffle Xu 		goto err_put_fd;
290c8383054SJeffle Xu 	}
291c8383054SJeffle Xu 
292324b954aSJeffle Xu 	/* CLOSE request has no reply */
293324b954aSJeffle Xu 	if (msg->opcode == CACHEFILES_OP_CLOSE) {
294324b954aSJeffle Xu 		xa_erase(&cache->reqs, id);
295324b954aSJeffle Xu 		complete(&req->done);
296324b954aSJeffle Xu 	}
297324b954aSJeffle Xu 
298c8383054SJeffle Xu 	return n;
299c8383054SJeffle Xu 
300c8383054SJeffle Xu err_put_fd:
301c8383054SJeffle Xu 	if (msg->opcode == CACHEFILES_OP_OPEN)
302c8383054SJeffle Xu 		close_fd(((struct cachefiles_open *)msg->data)->fd);
303c8383054SJeffle Xu error:
304c8383054SJeffle Xu 	xa_erase(&cache->reqs, id);
305c8383054SJeffle Xu 	req->error = ret;
306c8383054SJeffle Xu 	complete(&req->done);
307c8383054SJeffle Xu 	return ret;
308c8383054SJeffle Xu }
309c8383054SJeffle Xu 
310c8383054SJeffle Xu typedef int (*init_req_fn)(struct cachefiles_req *req, void *private);
311c8383054SJeffle Xu 
312c8383054SJeffle Xu static int cachefiles_ondemand_send_req(struct cachefiles_object *object,
313c8383054SJeffle Xu 					enum cachefiles_opcode opcode,
314c8383054SJeffle Xu 					size_t data_len,
315c8383054SJeffle Xu 					init_req_fn init_req,
316c8383054SJeffle Xu 					void *private)
317c8383054SJeffle Xu {
318c8383054SJeffle Xu 	struct cachefiles_cache *cache = object->volume->cache;
319c8383054SJeffle Xu 	struct cachefiles_req *req;
320c8383054SJeffle Xu 	XA_STATE(xas, &cache->reqs, 0);
321c8383054SJeffle Xu 	int ret;
322c8383054SJeffle Xu 
323c8383054SJeffle Xu 	if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags))
324c8383054SJeffle Xu 		return 0;
325c8383054SJeffle Xu 
326c8383054SJeffle Xu 	if (test_bit(CACHEFILES_DEAD, &cache->flags))
327c8383054SJeffle Xu 		return -EIO;
328c8383054SJeffle Xu 
329c8383054SJeffle Xu 	req = kzalloc(sizeof(*req) + data_len, GFP_KERNEL);
330c8383054SJeffle Xu 	if (!req)
331c8383054SJeffle Xu 		return -ENOMEM;
332c8383054SJeffle Xu 
333c8383054SJeffle Xu 	req->object = object;
334c8383054SJeffle Xu 	init_completion(&req->done);
335c8383054SJeffle Xu 	req->msg.opcode = opcode;
336c8383054SJeffle Xu 	req->msg.len = sizeof(struct cachefiles_msg) + data_len;
337c8383054SJeffle Xu 
338c8383054SJeffle Xu 	ret = init_req(req, private);
339c8383054SJeffle Xu 	if (ret)
340c8383054SJeffle Xu 		goto out;
341c8383054SJeffle Xu 
342c8383054SJeffle Xu 	do {
343c8383054SJeffle Xu 		/*
344c8383054SJeffle Xu 		 * Stop enqueuing the request when daemon is dying. The
345c8383054SJeffle Xu 		 * following two operations need to be atomic as a whole.
346c8383054SJeffle Xu 		 *   1) check cache state, and
347c8383054SJeffle Xu 		 *   2) enqueue request if cache is alive.
348c8383054SJeffle Xu 		 * Otherwise the request may be enqueued after xarray has been
349c8383054SJeffle Xu 		 * flushed, leaving the orphan request never being completed.
350c8383054SJeffle Xu 		 *
351c8383054SJeffle Xu 		 * CPU 1			CPU 2
352c8383054SJeffle Xu 		 * =====			=====
353c8383054SJeffle Xu 		 *				test CACHEFILES_DEAD bit
354c8383054SJeffle Xu 		 * set CACHEFILES_DEAD bit
355c8383054SJeffle Xu 		 * flush requests in the xarray
356c8383054SJeffle Xu 		 *				enqueue the request
357c8383054SJeffle Xu 		 */
358c8383054SJeffle Xu 		xas_lock(&xas);
359c8383054SJeffle Xu 
360c8383054SJeffle Xu 		if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
361c8383054SJeffle Xu 			xas_unlock(&xas);
362c8383054SJeffle Xu 			ret = -EIO;
363c8383054SJeffle Xu 			goto out;
364c8383054SJeffle Xu 		}
365c8383054SJeffle Xu 
366c8383054SJeffle Xu 		/* coupled with the barrier in cachefiles_flush_reqs() */
367c8383054SJeffle Xu 		smp_mb();
368c8383054SJeffle Xu 
369*955190e1SJia Zhu 		if (opcode != CACHEFILES_OP_OPEN &&
370*955190e1SJia Zhu 			!cachefiles_ondemand_object_is_open(object)) {
371324b954aSJeffle Xu 			WARN_ON_ONCE(object->ondemand_id == 0);
372324b954aSJeffle Xu 			xas_unlock(&xas);
373324b954aSJeffle Xu 			ret = -EIO;
374324b954aSJeffle Xu 			goto out;
375324b954aSJeffle Xu 		}
376324b954aSJeffle Xu 
377c8383054SJeffle Xu 		xas.xa_index = 0;
378c8383054SJeffle Xu 		xas_find_marked(&xas, UINT_MAX, XA_FREE_MARK);
379c8383054SJeffle Xu 		if (xas.xa_node == XAS_RESTART)
380c8383054SJeffle Xu 			xas_set_err(&xas, -EBUSY);
381c8383054SJeffle Xu 		xas_store(&xas, req);
382c8383054SJeffle Xu 		xas_clear_mark(&xas, XA_FREE_MARK);
383c8383054SJeffle Xu 		xas_set_mark(&xas, CACHEFILES_REQ_NEW);
384c8383054SJeffle Xu 		xas_unlock(&xas);
385c8383054SJeffle Xu 	} while (xas_nomem(&xas, GFP_KERNEL));
386c8383054SJeffle Xu 
387c8383054SJeffle Xu 	ret = xas_error(&xas);
388c8383054SJeffle Xu 	if (ret)
389c8383054SJeffle Xu 		goto out;
390c8383054SJeffle Xu 
391c8383054SJeffle Xu 	wake_up_all(&cache->daemon_pollwq);
392c8383054SJeffle Xu 	wait_for_completion(&req->done);
393c8383054SJeffle Xu 	ret = req->error;
394c8383054SJeffle Xu out:
395c8383054SJeffle Xu 	kfree(req);
396c8383054SJeffle Xu 	return ret;
397c8383054SJeffle Xu }
398c8383054SJeffle Xu 
399c8383054SJeffle Xu static int cachefiles_ondemand_init_open_req(struct cachefiles_req *req,
400c8383054SJeffle Xu 					     void *private)
401c8383054SJeffle Xu {
402c8383054SJeffle Xu 	struct cachefiles_object *object = req->object;
403c8383054SJeffle Xu 	struct fscache_cookie *cookie = object->cookie;
404c8383054SJeffle Xu 	struct fscache_volume *volume = object->volume->vcookie;
405c8383054SJeffle Xu 	struct cachefiles_open *load = (void *)req->msg.data;
406c8383054SJeffle Xu 	size_t volume_key_size, cookie_key_size;
407c8383054SJeffle Xu 	void *volume_key, *cookie_key;
408c8383054SJeffle Xu 
409c8383054SJeffle Xu 	/*
410c8383054SJeffle Xu 	 * Volume key is a NUL-terminated string. key[0] stores strlen() of the
411c8383054SJeffle Xu 	 * string, followed by the content of the string (excluding '\0').
412c8383054SJeffle Xu 	 */
413c8383054SJeffle Xu 	volume_key_size = volume->key[0] + 1;
414c8383054SJeffle Xu 	volume_key = volume->key + 1;
415c8383054SJeffle Xu 
416c8383054SJeffle Xu 	/* Cookie key is binary data, which is netfs specific. */
417c8383054SJeffle Xu 	cookie_key_size = cookie->key_len;
418c8383054SJeffle Xu 	cookie_key = fscache_get_key(cookie);
419c8383054SJeffle Xu 
420c8383054SJeffle Xu 	if (!(object->cookie->advice & FSCACHE_ADV_WANT_CACHE_SIZE)) {
421c8383054SJeffle Xu 		pr_err("WANT_CACHE_SIZE is needed for on-demand mode\n");
422c8383054SJeffle Xu 		return -EINVAL;
423c8383054SJeffle Xu 	}
424c8383054SJeffle Xu 
425c8383054SJeffle Xu 	load->volume_key_size = volume_key_size;
426c8383054SJeffle Xu 	load->cookie_key_size = cookie_key_size;
427c8383054SJeffle Xu 	memcpy(load->data, volume_key, volume_key_size);
428c8383054SJeffle Xu 	memcpy(load->data + volume_key_size, cookie_key, cookie_key_size);
429c8383054SJeffle Xu 
430c8383054SJeffle Xu 	return 0;
431c8383054SJeffle Xu }
432c8383054SJeffle Xu 
433324b954aSJeffle Xu static int cachefiles_ondemand_init_close_req(struct cachefiles_req *req,
434324b954aSJeffle Xu 					      void *private)
435324b954aSJeffle Xu {
436324b954aSJeffle Xu 	struct cachefiles_object *object = req->object;
437324b954aSJeffle Xu 
438*955190e1SJia Zhu 	if (!cachefiles_ondemand_object_is_open(object))
439324b954aSJeffle Xu 		return -ENOENT;
440324b954aSJeffle Xu 
441*955190e1SJia Zhu 	req->msg.object_id = object->ondemand_id;
4421519670eSJeffle Xu 	trace_cachefiles_ondemand_close(object, &req->msg);
443324b954aSJeffle Xu 	return 0;
444324b954aSJeffle Xu }
445324b954aSJeffle Xu 
4469032b6e8SJeffle Xu struct cachefiles_read_ctx {
4479032b6e8SJeffle Xu 	loff_t off;
4489032b6e8SJeffle Xu 	size_t len;
4499032b6e8SJeffle Xu };
4509032b6e8SJeffle Xu 
4519032b6e8SJeffle Xu static int cachefiles_ondemand_init_read_req(struct cachefiles_req *req,
4529032b6e8SJeffle Xu 					     void *private)
4539032b6e8SJeffle Xu {
4549032b6e8SJeffle Xu 	struct cachefiles_object *object = req->object;
4559032b6e8SJeffle Xu 	struct cachefiles_read *load = (void *)req->msg.data;
4569032b6e8SJeffle Xu 	struct cachefiles_read_ctx *read_ctx = private;
4579032b6e8SJeffle Xu 	int object_id = object->ondemand_id;
4589032b6e8SJeffle Xu 
4599032b6e8SJeffle Xu 	/* Stop enqueuing requests when daemon has closed anon_fd. */
460*955190e1SJia Zhu 	if (!cachefiles_ondemand_object_is_open(object)) {
4619032b6e8SJeffle Xu 		WARN_ON_ONCE(object_id == 0);
4629032b6e8SJeffle Xu 		pr_info_once("READ: anonymous fd closed prematurely.\n");
4639032b6e8SJeffle Xu 		return -EIO;
4649032b6e8SJeffle Xu 	}
4659032b6e8SJeffle Xu 
4669032b6e8SJeffle Xu 	req->msg.object_id = object_id;
4679032b6e8SJeffle Xu 	load->off = read_ctx->off;
4689032b6e8SJeffle Xu 	load->len = read_ctx->len;
4691519670eSJeffle Xu 	trace_cachefiles_ondemand_read(object, &req->msg, load);
4709032b6e8SJeffle Xu 	return 0;
4719032b6e8SJeffle Xu }
4729032b6e8SJeffle Xu 
473c8383054SJeffle Xu int cachefiles_ondemand_init_object(struct cachefiles_object *object)
474c8383054SJeffle Xu {
475c8383054SJeffle Xu 	struct fscache_cookie *cookie = object->cookie;
476c8383054SJeffle Xu 	struct fscache_volume *volume = object->volume->vcookie;
477c8383054SJeffle Xu 	size_t volume_key_size, cookie_key_size, data_len;
478c8383054SJeffle Xu 
479c8383054SJeffle Xu 	/*
480c8383054SJeffle Xu 	 * CacheFiles will firstly check the cache file under the root cache
481c8383054SJeffle Xu 	 * directory. If the coherency check failed, it will fallback to
482c8383054SJeffle Xu 	 * creating a new tmpfile as the cache file. Reuse the previously
483c8383054SJeffle Xu 	 * allocated object ID if any.
484c8383054SJeffle Xu 	 */
485*955190e1SJia Zhu 	if (cachefiles_ondemand_object_is_open(object))
486c8383054SJeffle Xu 		return 0;
487c8383054SJeffle Xu 
488c8383054SJeffle Xu 	volume_key_size = volume->key[0] + 1;
489c8383054SJeffle Xu 	cookie_key_size = cookie->key_len;
490c8383054SJeffle Xu 	data_len = sizeof(struct cachefiles_open) +
491c8383054SJeffle Xu 		   volume_key_size + cookie_key_size;
492c8383054SJeffle Xu 
493c8383054SJeffle Xu 	return cachefiles_ondemand_send_req(object, CACHEFILES_OP_OPEN,
494c8383054SJeffle Xu 			data_len, cachefiles_ondemand_init_open_req, NULL);
495c8383054SJeffle Xu }
496324b954aSJeffle Xu 
497324b954aSJeffle Xu void cachefiles_ondemand_clean_object(struct cachefiles_object *object)
498324b954aSJeffle Xu {
499324b954aSJeffle Xu 	cachefiles_ondemand_send_req(object, CACHEFILES_OP_CLOSE, 0,
500324b954aSJeffle Xu 			cachefiles_ondemand_init_close_req, NULL);
501324b954aSJeffle Xu }
5029032b6e8SJeffle Xu 
5039032b6e8SJeffle Xu int cachefiles_ondemand_read(struct cachefiles_object *object,
5049032b6e8SJeffle Xu 			     loff_t pos, size_t len)
5059032b6e8SJeffle Xu {
5069032b6e8SJeffle Xu 	struct cachefiles_read_ctx read_ctx = {pos, len};
5079032b6e8SJeffle Xu 
5089032b6e8SJeffle Xu 	return cachefiles_ondemand_send_req(object, CACHEFILES_OP_READ,
5099032b6e8SJeffle Xu 			sizeof(struct cachefiles_read),
5109032b6e8SJeffle Xu 			cachefiles_ondemand_init_read_req, &read_ctx);
5119032b6e8SJeffle Xu }
512