xref: /openbmc/linux/fs/cachefiles/ondemand.c (revision 1122f40072731525c06b1371cfa30112b9b54d27)
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;
189032b6e8SJeffle Xu 
199032b6e8SJeffle Xu 	/*
209032b6e8SJeffle Xu 	 * Flush all pending READ requests since their completion depends on
219032b6e8SJeffle Xu 	 * anon_fd.
229032b6e8SJeffle Xu 	 */
239032b6e8SJeffle Xu 	xas_for_each(&xas, req, ULONG_MAX) {
2465aa5f6fSJia Zhu 		if (req->msg.object_id == object_id &&
2565aa5f6fSJia Zhu 		    req->msg.opcode == CACHEFILES_OP_READ) {
269032b6e8SJeffle Xu 			req->error = -EIO;
279032b6e8SJeffle Xu 			complete(&req->done);
289032b6e8SJeffle Xu 			xas_store(&xas, NULL);
299032b6e8SJeffle Xu 		}
309032b6e8SJeffle Xu 	}
319032b6e8SJeffle Xu 	xa_unlock(&cache->reqs);
329032b6e8SJeffle Xu 
33c8383054SJeffle Xu 	xa_erase(&cache->ondemand_ids, object_id);
341519670eSJeffle Xu 	trace_cachefiles_ondemand_fd_release(object, object_id);
35c8383054SJeffle Xu 	cachefiles_put_object(object, cachefiles_obj_put_ondemand_fd);
36d11b0b04SJeffle Xu 	cachefiles_put_unbind_pincount(cache);
37c8383054SJeffle Xu 	return 0;
38c8383054SJeffle Xu }
39c8383054SJeffle Xu 
40c8383054SJeffle Xu static ssize_t cachefiles_ondemand_fd_write_iter(struct kiocb *kiocb,
41c8383054SJeffle Xu 						 struct iov_iter *iter)
42c8383054SJeffle Xu {
43c8383054SJeffle Xu 	struct cachefiles_object *object = kiocb->ki_filp->private_data;
44c8383054SJeffle Xu 	struct cachefiles_cache *cache = object->volume->cache;
45c8383054SJeffle Xu 	struct file *file = object->file;
46c8383054SJeffle Xu 	size_t len = iter->count;
47c8383054SJeffle Xu 	loff_t pos = kiocb->ki_pos;
48c8383054SJeffle Xu 	const struct cred *saved_cred;
49c8383054SJeffle Xu 	int ret;
50c8383054SJeffle Xu 
51c8383054SJeffle Xu 	if (!file)
52c8383054SJeffle Xu 		return -ENOBUFS;
53c8383054SJeffle Xu 
54c8383054SJeffle Xu 	cachefiles_begin_secure(cache, &saved_cred);
55c8383054SJeffle Xu 	ret = __cachefiles_prepare_write(object, file, &pos, &len, true);
56c8383054SJeffle Xu 	cachefiles_end_secure(cache, saved_cred);
57c8383054SJeffle Xu 	if (ret < 0)
58c8383054SJeffle Xu 		return ret;
59c8383054SJeffle Xu 
601519670eSJeffle Xu 	trace_cachefiles_ondemand_fd_write(object, file_inode(file), pos, len);
61c8383054SJeffle Xu 	ret = __cachefiles_write(object, file, pos, iter, NULL, NULL);
62c8383054SJeffle Xu 	if (!ret)
63c8383054SJeffle Xu 		ret = len;
64c8383054SJeffle Xu 
65c8383054SJeffle Xu 	return ret;
66c8383054SJeffle Xu }
67c8383054SJeffle Xu 
68c8383054SJeffle Xu static loff_t cachefiles_ondemand_fd_llseek(struct file *filp, loff_t pos,
69c8383054SJeffle Xu 					    int whence)
70c8383054SJeffle Xu {
71c8383054SJeffle Xu 	struct cachefiles_object *object = filp->private_data;
72c8383054SJeffle Xu 	struct file *file = object->file;
73c8383054SJeffle Xu 
74c8383054SJeffle Xu 	if (!file)
75c8383054SJeffle Xu 		return -ENOBUFS;
76c8383054SJeffle Xu 
77c8383054SJeffle Xu 	return vfs_llseek(file, pos, whence);
78c8383054SJeffle Xu }
79c8383054SJeffle Xu 
809032b6e8SJeffle Xu static long cachefiles_ondemand_fd_ioctl(struct file *filp, unsigned int ioctl,
819032b6e8SJeffle Xu 					 unsigned long arg)
829032b6e8SJeffle Xu {
839032b6e8SJeffle Xu 	struct cachefiles_object *object = filp->private_data;
849032b6e8SJeffle Xu 	struct cachefiles_cache *cache = object->volume->cache;
859032b6e8SJeffle Xu 	struct cachefiles_req *req;
869032b6e8SJeffle Xu 	unsigned long id;
879032b6e8SJeffle Xu 
889032b6e8SJeffle Xu 	if (ioctl != CACHEFILES_IOC_READ_COMPLETE)
899032b6e8SJeffle Xu 		return -EINVAL;
909032b6e8SJeffle Xu 
919032b6e8SJeffle Xu 	if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags))
929032b6e8SJeffle Xu 		return -EOPNOTSUPP;
939032b6e8SJeffle Xu 
949032b6e8SJeffle Xu 	id = arg;
959032b6e8SJeffle Xu 	req = xa_erase(&cache->reqs, id);
969032b6e8SJeffle Xu 	if (!req)
979032b6e8SJeffle Xu 		return -EINVAL;
989032b6e8SJeffle Xu 
991519670eSJeffle Xu 	trace_cachefiles_ondemand_cread(object, id);
1009032b6e8SJeffle Xu 	complete(&req->done);
1019032b6e8SJeffle Xu 	return 0;
1029032b6e8SJeffle Xu }
1039032b6e8SJeffle Xu 
104c8383054SJeffle Xu static const struct file_operations cachefiles_ondemand_fd_fops = {
105c8383054SJeffle Xu 	.owner		= THIS_MODULE,
106c8383054SJeffle Xu 	.release	= cachefiles_ondemand_fd_release,
107c8383054SJeffle Xu 	.write_iter	= cachefiles_ondemand_fd_write_iter,
108c8383054SJeffle Xu 	.llseek		= cachefiles_ondemand_fd_llseek,
1099032b6e8SJeffle Xu 	.unlocked_ioctl	= cachefiles_ondemand_fd_ioctl,
110c8383054SJeffle Xu };
111c8383054SJeffle Xu 
112c8383054SJeffle Xu /*
113c8383054SJeffle Xu  * OPEN request Completion (copen)
114c8383054SJeffle Xu  * - command: "copen <id>,<cache_size>"
115c8383054SJeffle Xu  *   <cache_size> indicates the object size if >=0, error code if negative
116c8383054SJeffle Xu  */
117c8383054SJeffle Xu int cachefiles_ondemand_copen(struct cachefiles_cache *cache, char *args)
118c8383054SJeffle Xu {
119c8383054SJeffle Xu 	struct cachefiles_req *req;
120c8383054SJeffle Xu 	struct fscache_cookie *cookie;
121c8383054SJeffle Xu 	char *pid, *psize;
122c8383054SJeffle Xu 	unsigned long id;
123c8383054SJeffle Xu 	long size;
124c8383054SJeffle Xu 	int ret;
125c8383054SJeffle Xu 
126c8383054SJeffle Xu 	if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags))
127c8383054SJeffle Xu 		return -EOPNOTSUPP;
128c8383054SJeffle Xu 
129c8383054SJeffle Xu 	if (!*args) {
130c8383054SJeffle Xu 		pr_err("Empty id specified\n");
131c8383054SJeffle Xu 		return -EINVAL;
132c8383054SJeffle Xu 	}
133c8383054SJeffle Xu 
134c8383054SJeffle Xu 	pid = args;
135c8383054SJeffle Xu 	psize = strchr(args, ',');
136c8383054SJeffle Xu 	if (!psize) {
137c8383054SJeffle Xu 		pr_err("Cache size is not specified\n");
138c8383054SJeffle Xu 		return -EINVAL;
139c8383054SJeffle Xu 	}
140c8383054SJeffle Xu 
141c8383054SJeffle Xu 	*psize = 0;
142c8383054SJeffle Xu 	psize++;
143c8383054SJeffle Xu 
144c8383054SJeffle Xu 	ret = kstrtoul(pid, 0, &id);
145c8383054SJeffle Xu 	if (ret)
146c8383054SJeffle Xu 		return ret;
147c8383054SJeffle Xu 
148c8383054SJeffle Xu 	req = xa_erase(&cache->reqs, id);
149c8383054SJeffle Xu 	if (!req)
150c8383054SJeffle Xu 		return -EINVAL;
151c8383054SJeffle Xu 
152c8383054SJeffle Xu 	/* fail OPEN request if copen format is invalid */
153c8383054SJeffle Xu 	ret = kstrtol(psize, 0, &size);
154c8383054SJeffle Xu 	if (ret) {
155c8383054SJeffle Xu 		req->error = ret;
156c8383054SJeffle Xu 		goto out;
157c8383054SJeffle Xu 	}
158c8383054SJeffle Xu 
159c8383054SJeffle Xu 	/* fail OPEN request if daemon reports an error */
160c8383054SJeffle Xu 	if (size < 0) {
161c93ccd63SSun Ke 		if (!IS_ERR_VALUE(size)) {
162c93ccd63SSun Ke 			req->error = -EINVAL;
163c93ccd63SSun Ke 			ret = -EINVAL;
164c93ccd63SSun Ke 		} else {
165c8383054SJeffle Xu 			req->error = size;
166c93ccd63SSun Ke 			ret = 0;
167c93ccd63SSun Ke 		}
168c8383054SJeffle Xu 		goto out;
169c8383054SJeffle Xu 	}
170c8383054SJeffle Xu 
171c8383054SJeffle Xu 	cookie = req->object->cookie;
172c8383054SJeffle Xu 	cookie->object_size = size;
173c8383054SJeffle Xu 	if (size)
174c8383054SJeffle Xu 		clear_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags);
175c8383054SJeffle Xu 	else
176c8383054SJeffle Xu 		set_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags);
1771519670eSJeffle Xu 	trace_cachefiles_ondemand_copen(req->object, id, size);
178c8383054SJeffle Xu 
179c8383054SJeffle Xu out:
180c8383054SJeffle Xu 	complete(&req->done);
181c8383054SJeffle Xu 	return ret;
182c8383054SJeffle Xu }
183c8383054SJeffle Xu 
184c8383054SJeffle Xu static int cachefiles_ondemand_get_fd(struct cachefiles_req *req)
185c8383054SJeffle Xu {
186c8383054SJeffle Xu 	struct cachefiles_object *object;
187c8383054SJeffle Xu 	struct cachefiles_cache *cache;
188c8383054SJeffle Xu 	struct cachefiles_open *load;
189c8383054SJeffle Xu 	struct file *file;
190c8383054SJeffle Xu 	u32 object_id;
191c8383054SJeffle Xu 	int ret, fd;
192c8383054SJeffle Xu 
193c8383054SJeffle Xu 	object = cachefiles_grab_object(req->object,
194c8383054SJeffle Xu 			cachefiles_obj_get_ondemand_fd);
195c8383054SJeffle Xu 	cache = object->volume->cache;
196c8383054SJeffle Xu 
197c8383054SJeffle Xu 	ret = xa_alloc_cyclic(&cache->ondemand_ids, &object_id, NULL,
198c8383054SJeffle Xu 			      XA_LIMIT(1, INT_MAX),
199c8383054SJeffle Xu 			      &cache->ondemand_id_next, GFP_KERNEL);
200c8383054SJeffle Xu 	if (ret < 0)
201c8383054SJeffle Xu 		goto err;
202c8383054SJeffle Xu 
203c8383054SJeffle Xu 	fd = get_unused_fd_flags(O_WRONLY);
204c8383054SJeffle Xu 	if (fd < 0) {
205c8383054SJeffle Xu 		ret = fd;
206c8383054SJeffle Xu 		goto err_free_id;
207c8383054SJeffle Xu 	}
208c8383054SJeffle Xu 
209c8383054SJeffle Xu 	file = anon_inode_getfile("[cachefiles]", &cachefiles_ondemand_fd_fops,
210c8383054SJeffle Xu 				  object, O_WRONLY);
211c8383054SJeffle Xu 	if (IS_ERR(file)) {
212c8383054SJeffle Xu 		ret = PTR_ERR(file);
213c8383054SJeffle Xu 		goto err_put_fd;
214c8383054SJeffle Xu 	}
215c8383054SJeffle Xu 
216c8383054SJeffle Xu 	file->f_mode |= FMODE_PWRITE | FMODE_LSEEK;
217c8383054SJeffle Xu 	fd_install(fd, file);
218c8383054SJeffle Xu 
219c8383054SJeffle Xu 	load = (void *)req->msg.data;
220c8383054SJeffle Xu 	load->fd = fd;
221c8383054SJeffle Xu 	req->msg.object_id = object_id;
222c8383054SJeffle Xu 	object->ondemand_id = object_id;
223d11b0b04SJeffle Xu 
224d11b0b04SJeffle Xu 	cachefiles_get_unbind_pincount(cache);
2251519670eSJeffle Xu 	trace_cachefiles_ondemand_open(object, &req->msg, load);
226c8383054SJeffle Xu 	return 0;
227c8383054SJeffle Xu 
228c8383054SJeffle Xu err_put_fd:
229c8383054SJeffle Xu 	put_unused_fd(fd);
230c8383054SJeffle Xu err_free_id:
231c8383054SJeffle Xu 	xa_erase(&cache->ondemand_ids, object_id);
232c8383054SJeffle Xu err:
233c8383054SJeffle Xu 	cachefiles_put_object(object, cachefiles_obj_put_ondemand_fd);
234c8383054SJeffle Xu 	return ret;
235c8383054SJeffle Xu }
236c8383054SJeffle Xu 
237c8383054SJeffle Xu ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache,
238c8383054SJeffle Xu 					char __user *_buffer, size_t buflen)
239c8383054SJeffle Xu {
240c8383054SJeffle Xu 	struct cachefiles_req *req;
241c8383054SJeffle Xu 	struct cachefiles_msg *msg;
242c8383054SJeffle Xu 	unsigned long id = 0;
243c8383054SJeffle Xu 	size_t n;
244c8383054SJeffle Xu 	int ret = 0;
245*1122f400SXin Yin 	XA_STATE(xas, &cache->reqs, cache->req_id_next);
246c8383054SJeffle Xu 
247c8383054SJeffle Xu 	/*
248*1122f400SXin Yin 	 * Cyclically search for a request that has not ever been processed,
249*1122f400SXin Yin 	 * to prevent requests from being processed repeatedly, and make
250*1122f400SXin Yin 	 * request distribution fair.
251c8383054SJeffle Xu 	 */
252c8383054SJeffle Xu 	xa_lock(&cache->reqs);
253c8383054SJeffle Xu 	req = xas_find_marked(&xas, UINT_MAX, CACHEFILES_REQ_NEW);
254*1122f400SXin Yin 	if (!req && cache->req_id_next > 0) {
255*1122f400SXin Yin 		xas_set(&xas, 0);
256*1122f400SXin Yin 		req = xas_find_marked(&xas, cache->req_id_next - 1, CACHEFILES_REQ_NEW);
257*1122f400SXin Yin 	}
258c8383054SJeffle Xu 	if (!req) {
259c8383054SJeffle Xu 		xa_unlock(&cache->reqs);
260c8383054SJeffle Xu 		return 0;
261c8383054SJeffle Xu 	}
262c8383054SJeffle Xu 
263c8383054SJeffle Xu 	msg = &req->msg;
264c8383054SJeffle Xu 	n = msg->len;
265c8383054SJeffle Xu 
266c8383054SJeffle Xu 	if (n > buflen) {
267c8383054SJeffle Xu 		xa_unlock(&cache->reqs);
268c8383054SJeffle Xu 		return -EMSGSIZE;
269c8383054SJeffle Xu 	}
270c8383054SJeffle Xu 
271c8383054SJeffle Xu 	xas_clear_mark(&xas, CACHEFILES_REQ_NEW);
272*1122f400SXin Yin 	cache->req_id_next = xas.xa_index + 1;
273c8383054SJeffle Xu 	xa_unlock(&cache->reqs);
274c8383054SJeffle Xu 
275c8383054SJeffle Xu 	id = xas.xa_index;
276c8383054SJeffle Xu 	msg->msg_id = id;
277c8383054SJeffle Xu 
278c8383054SJeffle Xu 	if (msg->opcode == CACHEFILES_OP_OPEN) {
279c8383054SJeffle Xu 		ret = cachefiles_ondemand_get_fd(req);
280c8383054SJeffle Xu 		if (ret)
281c8383054SJeffle Xu 			goto error;
282c8383054SJeffle Xu 	}
283c8383054SJeffle Xu 
284c8383054SJeffle Xu 	if (copy_to_user(_buffer, msg, n) != 0) {
285c8383054SJeffle Xu 		ret = -EFAULT;
286c8383054SJeffle Xu 		goto err_put_fd;
287c8383054SJeffle Xu 	}
288c8383054SJeffle Xu 
289324b954aSJeffle Xu 	/* CLOSE request has no reply */
290324b954aSJeffle Xu 	if (msg->opcode == CACHEFILES_OP_CLOSE) {
291324b954aSJeffle Xu 		xa_erase(&cache->reqs, id);
292324b954aSJeffle Xu 		complete(&req->done);
293324b954aSJeffle Xu 	}
294324b954aSJeffle Xu 
295c8383054SJeffle Xu 	return n;
296c8383054SJeffle Xu 
297c8383054SJeffle Xu err_put_fd:
298c8383054SJeffle Xu 	if (msg->opcode == CACHEFILES_OP_OPEN)
299c8383054SJeffle Xu 		close_fd(((struct cachefiles_open *)msg->data)->fd);
300c8383054SJeffle Xu error:
301c8383054SJeffle Xu 	xa_erase(&cache->reqs, id);
302c8383054SJeffle Xu 	req->error = ret;
303c8383054SJeffle Xu 	complete(&req->done);
304c8383054SJeffle Xu 	return ret;
305c8383054SJeffle Xu }
306c8383054SJeffle Xu 
307c8383054SJeffle Xu typedef int (*init_req_fn)(struct cachefiles_req *req, void *private);
308c8383054SJeffle Xu 
309c8383054SJeffle Xu static int cachefiles_ondemand_send_req(struct cachefiles_object *object,
310c8383054SJeffle Xu 					enum cachefiles_opcode opcode,
311c8383054SJeffle Xu 					size_t data_len,
312c8383054SJeffle Xu 					init_req_fn init_req,
313c8383054SJeffle Xu 					void *private)
314c8383054SJeffle Xu {
315c8383054SJeffle Xu 	struct cachefiles_cache *cache = object->volume->cache;
316c8383054SJeffle Xu 	struct cachefiles_req *req;
317c8383054SJeffle Xu 	XA_STATE(xas, &cache->reqs, 0);
318c8383054SJeffle Xu 	int ret;
319c8383054SJeffle Xu 
320c8383054SJeffle Xu 	if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags))
321c8383054SJeffle Xu 		return 0;
322c8383054SJeffle Xu 
323c8383054SJeffle Xu 	if (test_bit(CACHEFILES_DEAD, &cache->flags))
324c8383054SJeffle Xu 		return -EIO;
325c8383054SJeffle Xu 
326c8383054SJeffle Xu 	req = kzalloc(sizeof(*req) + data_len, GFP_KERNEL);
327c8383054SJeffle Xu 	if (!req)
328c8383054SJeffle Xu 		return -ENOMEM;
329c8383054SJeffle Xu 
330c8383054SJeffle Xu 	req->object = object;
331c8383054SJeffle Xu 	init_completion(&req->done);
332c8383054SJeffle Xu 	req->msg.opcode = opcode;
333c8383054SJeffle Xu 	req->msg.len = sizeof(struct cachefiles_msg) + data_len;
334c8383054SJeffle Xu 
335c8383054SJeffle Xu 	ret = init_req(req, private);
336c8383054SJeffle Xu 	if (ret)
337c8383054SJeffle Xu 		goto out;
338c8383054SJeffle Xu 
339c8383054SJeffle Xu 	do {
340c8383054SJeffle Xu 		/*
341c8383054SJeffle Xu 		 * Stop enqueuing the request when daemon is dying. The
342c8383054SJeffle Xu 		 * following two operations need to be atomic as a whole.
343c8383054SJeffle Xu 		 *   1) check cache state, and
344c8383054SJeffle Xu 		 *   2) enqueue request if cache is alive.
345c8383054SJeffle Xu 		 * Otherwise the request may be enqueued after xarray has been
346c8383054SJeffle Xu 		 * flushed, leaving the orphan request never being completed.
347c8383054SJeffle Xu 		 *
348c8383054SJeffle Xu 		 * CPU 1			CPU 2
349c8383054SJeffle Xu 		 * =====			=====
350c8383054SJeffle Xu 		 *				test CACHEFILES_DEAD bit
351c8383054SJeffle Xu 		 * set CACHEFILES_DEAD bit
352c8383054SJeffle Xu 		 * flush requests in the xarray
353c8383054SJeffle Xu 		 *				enqueue the request
354c8383054SJeffle Xu 		 */
355c8383054SJeffle Xu 		xas_lock(&xas);
356c8383054SJeffle Xu 
357c8383054SJeffle Xu 		if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
358c8383054SJeffle Xu 			xas_unlock(&xas);
359c8383054SJeffle Xu 			ret = -EIO;
360c8383054SJeffle Xu 			goto out;
361c8383054SJeffle Xu 		}
362c8383054SJeffle Xu 
363c8383054SJeffle Xu 		/* coupled with the barrier in cachefiles_flush_reqs() */
364c8383054SJeffle Xu 		smp_mb();
365c8383054SJeffle Xu 
366324b954aSJeffle Xu 		if (opcode != CACHEFILES_OP_OPEN && object->ondemand_id <= 0) {
367324b954aSJeffle Xu 			WARN_ON_ONCE(object->ondemand_id == 0);
368324b954aSJeffle Xu 			xas_unlock(&xas);
369324b954aSJeffle Xu 			ret = -EIO;
370324b954aSJeffle Xu 			goto out;
371324b954aSJeffle Xu 		}
372324b954aSJeffle Xu 
373c8383054SJeffle Xu 		xas.xa_index = 0;
374c8383054SJeffle Xu 		xas_find_marked(&xas, UINT_MAX, XA_FREE_MARK);
375c8383054SJeffle Xu 		if (xas.xa_node == XAS_RESTART)
376c8383054SJeffle Xu 			xas_set_err(&xas, -EBUSY);
377c8383054SJeffle Xu 		xas_store(&xas, req);
378c8383054SJeffle Xu 		xas_clear_mark(&xas, XA_FREE_MARK);
379c8383054SJeffle Xu 		xas_set_mark(&xas, CACHEFILES_REQ_NEW);
380c8383054SJeffle Xu 		xas_unlock(&xas);
381c8383054SJeffle Xu 	} while (xas_nomem(&xas, GFP_KERNEL));
382c8383054SJeffle Xu 
383c8383054SJeffle Xu 	ret = xas_error(&xas);
384c8383054SJeffle Xu 	if (ret)
385c8383054SJeffle Xu 		goto out;
386c8383054SJeffle Xu 
387c8383054SJeffle Xu 	wake_up_all(&cache->daemon_pollwq);
388c8383054SJeffle Xu 	wait_for_completion(&req->done);
389c8383054SJeffle Xu 	ret = req->error;
390c8383054SJeffle Xu out:
391c8383054SJeffle Xu 	kfree(req);
392c8383054SJeffle Xu 	return ret;
393c8383054SJeffle Xu }
394c8383054SJeffle Xu 
395c8383054SJeffle Xu static int cachefiles_ondemand_init_open_req(struct cachefiles_req *req,
396c8383054SJeffle Xu 					     void *private)
397c8383054SJeffle Xu {
398c8383054SJeffle Xu 	struct cachefiles_object *object = req->object;
399c8383054SJeffle Xu 	struct fscache_cookie *cookie = object->cookie;
400c8383054SJeffle Xu 	struct fscache_volume *volume = object->volume->vcookie;
401c8383054SJeffle Xu 	struct cachefiles_open *load = (void *)req->msg.data;
402c8383054SJeffle Xu 	size_t volume_key_size, cookie_key_size;
403c8383054SJeffle Xu 	void *volume_key, *cookie_key;
404c8383054SJeffle Xu 
405c8383054SJeffle Xu 	/*
406c8383054SJeffle Xu 	 * Volume key is a NUL-terminated string. key[0] stores strlen() of the
407c8383054SJeffle Xu 	 * string, followed by the content of the string (excluding '\0').
408c8383054SJeffle Xu 	 */
409c8383054SJeffle Xu 	volume_key_size = volume->key[0] + 1;
410c8383054SJeffle Xu 	volume_key = volume->key + 1;
411c8383054SJeffle Xu 
412c8383054SJeffle Xu 	/* Cookie key is binary data, which is netfs specific. */
413c8383054SJeffle Xu 	cookie_key_size = cookie->key_len;
414c8383054SJeffle Xu 	cookie_key = fscache_get_key(cookie);
415c8383054SJeffle Xu 
416c8383054SJeffle Xu 	if (!(object->cookie->advice & FSCACHE_ADV_WANT_CACHE_SIZE)) {
417c8383054SJeffle Xu 		pr_err("WANT_CACHE_SIZE is needed for on-demand mode\n");
418c8383054SJeffle Xu 		return -EINVAL;
419c8383054SJeffle Xu 	}
420c8383054SJeffle Xu 
421c8383054SJeffle Xu 	load->volume_key_size = volume_key_size;
422c8383054SJeffle Xu 	load->cookie_key_size = cookie_key_size;
423c8383054SJeffle Xu 	memcpy(load->data, volume_key, volume_key_size);
424c8383054SJeffle Xu 	memcpy(load->data + volume_key_size, cookie_key, cookie_key_size);
425c8383054SJeffle Xu 
426c8383054SJeffle Xu 	return 0;
427c8383054SJeffle Xu }
428c8383054SJeffle Xu 
429324b954aSJeffle Xu static int cachefiles_ondemand_init_close_req(struct cachefiles_req *req,
430324b954aSJeffle Xu 					      void *private)
431324b954aSJeffle Xu {
432324b954aSJeffle Xu 	struct cachefiles_object *object = req->object;
433324b954aSJeffle Xu 	int object_id = object->ondemand_id;
434324b954aSJeffle Xu 
435324b954aSJeffle Xu 	/*
436324b954aSJeffle Xu 	 * It's possible that object id is still 0 if the cookie looking up
437324b954aSJeffle Xu 	 * phase failed before OPEN request has ever been sent. Also avoid
438324b954aSJeffle Xu 	 * sending CLOSE request for CACHEFILES_ONDEMAND_ID_CLOSED, which means
439324b954aSJeffle Xu 	 * anon_fd has already been closed.
440324b954aSJeffle Xu 	 */
441324b954aSJeffle Xu 	if (object_id <= 0)
442324b954aSJeffle Xu 		return -ENOENT;
443324b954aSJeffle Xu 
444324b954aSJeffle Xu 	req->msg.object_id = object_id;
4451519670eSJeffle Xu 	trace_cachefiles_ondemand_close(object, &req->msg);
446324b954aSJeffle Xu 	return 0;
447324b954aSJeffle Xu }
448324b954aSJeffle Xu 
4499032b6e8SJeffle Xu struct cachefiles_read_ctx {
4509032b6e8SJeffle Xu 	loff_t off;
4519032b6e8SJeffle Xu 	size_t len;
4529032b6e8SJeffle Xu };
4539032b6e8SJeffle Xu 
4549032b6e8SJeffle Xu static int cachefiles_ondemand_init_read_req(struct cachefiles_req *req,
4559032b6e8SJeffle Xu 					     void *private)
4569032b6e8SJeffle Xu {
4579032b6e8SJeffle Xu 	struct cachefiles_object *object = req->object;
4589032b6e8SJeffle Xu 	struct cachefiles_read *load = (void *)req->msg.data;
4599032b6e8SJeffle Xu 	struct cachefiles_read_ctx *read_ctx = private;
4609032b6e8SJeffle Xu 	int object_id = object->ondemand_id;
4619032b6e8SJeffle Xu 
4629032b6e8SJeffle Xu 	/* Stop enqueuing requests when daemon has closed anon_fd. */
4639032b6e8SJeffle Xu 	if (object_id <= 0) {
4649032b6e8SJeffle Xu 		WARN_ON_ONCE(object_id == 0);
4659032b6e8SJeffle Xu 		pr_info_once("READ: anonymous fd closed prematurely.\n");
4669032b6e8SJeffle Xu 		return -EIO;
4679032b6e8SJeffle Xu 	}
4689032b6e8SJeffle Xu 
4699032b6e8SJeffle Xu 	req->msg.object_id = object_id;
4709032b6e8SJeffle Xu 	load->off = read_ctx->off;
4719032b6e8SJeffle Xu 	load->len = read_ctx->len;
4721519670eSJeffle Xu 	trace_cachefiles_ondemand_read(object, &req->msg, load);
4739032b6e8SJeffle Xu 	return 0;
4749032b6e8SJeffle Xu }
4759032b6e8SJeffle Xu 
476c8383054SJeffle Xu int cachefiles_ondemand_init_object(struct cachefiles_object *object)
477c8383054SJeffle Xu {
478c8383054SJeffle Xu 	struct fscache_cookie *cookie = object->cookie;
479c8383054SJeffle Xu 	struct fscache_volume *volume = object->volume->vcookie;
480c8383054SJeffle Xu 	size_t volume_key_size, cookie_key_size, data_len;
481c8383054SJeffle Xu 
482c8383054SJeffle Xu 	/*
483c8383054SJeffle Xu 	 * CacheFiles will firstly check the cache file under the root cache
484c8383054SJeffle Xu 	 * directory. If the coherency check failed, it will fallback to
485c8383054SJeffle Xu 	 * creating a new tmpfile as the cache file. Reuse the previously
486c8383054SJeffle Xu 	 * allocated object ID if any.
487c8383054SJeffle Xu 	 */
488c8383054SJeffle Xu 	if (object->ondemand_id > 0)
489c8383054SJeffle Xu 		return 0;
490c8383054SJeffle Xu 
491c8383054SJeffle Xu 	volume_key_size = volume->key[0] + 1;
492c8383054SJeffle Xu 	cookie_key_size = cookie->key_len;
493c8383054SJeffle Xu 	data_len = sizeof(struct cachefiles_open) +
494c8383054SJeffle Xu 		   volume_key_size + cookie_key_size;
495c8383054SJeffle Xu 
496c8383054SJeffle Xu 	return cachefiles_ondemand_send_req(object, CACHEFILES_OP_OPEN,
497c8383054SJeffle Xu 			data_len, cachefiles_ondemand_init_open_req, NULL);
498c8383054SJeffle Xu }
499324b954aSJeffle Xu 
500324b954aSJeffle Xu void cachefiles_ondemand_clean_object(struct cachefiles_object *object)
501324b954aSJeffle Xu {
502324b954aSJeffle Xu 	cachefiles_ondemand_send_req(object, CACHEFILES_OP_CLOSE, 0,
503324b954aSJeffle Xu 			cachefiles_ondemand_init_close_req, NULL);
504324b954aSJeffle Xu }
5059032b6e8SJeffle Xu 
5069032b6e8SJeffle Xu int cachefiles_ondemand_read(struct cachefiles_object *object,
5079032b6e8SJeffle Xu 			     loff_t pos, size_t len)
5089032b6e8SJeffle Xu {
5099032b6e8SJeffle Xu 	struct cachefiles_read_ctx read_ctx = {pos, len};
5109032b6e8SJeffle Xu 
5119032b6e8SJeffle Xu 	return cachefiles_ondemand_send_req(object, CACHEFILES_OP_READ,
5129032b6e8SJeffle Xu 			sizeof(struct cachefiles_read),
5139032b6e8SJeffle Xu 			cachefiles_ondemand_init_read_req, &read_ctx);
5149032b6e8SJeffle Xu }
515