xref: /openbmc/linux/fs/cachefiles/ondemand.c (revision 1519670e4fecc6063fa2f0c10f0666d3331f219b)
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) {
249032b6e8SJeffle Xu 		if (req->msg.opcode == CACHEFILES_OP_READ) {
259032b6e8SJeffle Xu 			req->error = -EIO;
269032b6e8SJeffle Xu 			complete(&req->done);
279032b6e8SJeffle Xu 			xas_store(&xas, NULL);
289032b6e8SJeffle Xu 		}
299032b6e8SJeffle Xu 	}
309032b6e8SJeffle Xu 	xa_unlock(&cache->reqs);
319032b6e8SJeffle Xu 
32c8383054SJeffle Xu 	xa_erase(&cache->ondemand_ids, object_id);
33*1519670eSJeffle Xu 	trace_cachefiles_ondemand_fd_release(object, object_id);
34c8383054SJeffle Xu 	cachefiles_put_object(object, cachefiles_obj_put_ondemand_fd);
35d11b0b04SJeffle Xu 	cachefiles_put_unbind_pincount(cache);
36c8383054SJeffle Xu 	return 0;
37c8383054SJeffle Xu }
38c8383054SJeffle Xu 
39c8383054SJeffle Xu static ssize_t cachefiles_ondemand_fd_write_iter(struct kiocb *kiocb,
40c8383054SJeffle Xu 						 struct iov_iter *iter)
41c8383054SJeffle Xu {
42c8383054SJeffle Xu 	struct cachefiles_object *object = kiocb->ki_filp->private_data;
43c8383054SJeffle Xu 	struct cachefiles_cache *cache = object->volume->cache;
44c8383054SJeffle Xu 	struct file *file = object->file;
45c8383054SJeffle Xu 	size_t len = iter->count;
46c8383054SJeffle Xu 	loff_t pos = kiocb->ki_pos;
47c8383054SJeffle Xu 	const struct cred *saved_cred;
48c8383054SJeffle Xu 	int ret;
49c8383054SJeffle Xu 
50c8383054SJeffle Xu 	if (!file)
51c8383054SJeffle Xu 		return -ENOBUFS;
52c8383054SJeffle Xu 
53c8383054SJeffle Xu 	cachefiles_begin_secure(cache, &saved_cred);
54c8383054SJeffle Xu 	ret = __cachefiles_prepare_write(object, file, &pos, &len, true);
55c8383054SJeffle Xu 	cachefiles_end_secure(cache, saved_cred);
56c8383054SJeffle Xu 	if (ret < 0)
57c8383054SJeffle Xu 		return ret;
58c8383054SJeffle Xu 
59*1519670eSJeffle Xu 	trace_cachefiles_ondemand_fd_write(object, file_inode(file), pos, len);
60c8383054SJeffle Xu 	ret = __cachefiles_write(object, file, pos, iter, NULL, NULL);
61c8383054SJeffle Xu 	if (!ret)
62c8383054SJeffle Xu 		ret = len;
63c8383054SJeffle Xu 
64c8383054SJeffle Xu 	return ret;
65c8383054SJeffle Xu }
66c8383054SJeffle Xu 
67c8383054SJeffle Xu static loff_t cachefiles_ondemand_fd_llseek(struct file *filp, loff_t pos,
68c8383054SJeffle Xu 					    int whence)
69c8383054SJeffle Xu {
70c8383054SJeffle Xu 	struct cachefiles_object *object = filp->private_data;
71c8383054SJeffle Xu 	struct file *file = object->file;
72c8383054SJeffle Xu 
73c8383054SJeffle Xu 	if (!file)
74c8383054SJeffle Xu 		return -ENOBUFS;
75c8383054SJeffle Xu 
76c8383054SJeffle Xu 	return vfs_llseek(file, pos, whence);
77c8383054SJeffle Xu }
78c8383054SJeffle Xu 
799032b6e8SJeffle Xu static long cachefiles_ondemand_fd_ioctl(struct file *filp, unsigned int ioctl,
809032b6e8SJeffle Xu 					 unsigned long arg)
819032b6e8SJeffle Xu {
829032b6e8SJeffle Xu 	struct cachefiles_object *object = filp->private_data;
839032b6e8SJeffle Xu 	struct cachefiles_cache *cache = object->volume->cache;
849032b6e8SJeffle Xu 	struct cachefiles_req *req;
859032b6e8SJeffle Xu 	unsigned long id;
869032b6e8SJeffle Xu 
879032b6e8SJeffle Xu 	if (ioctl != CACHEFILES_IOC_READ_COMPLETE)
889032b6e8SJeffle Xu 		return -EINVAL;
899032b6e8SJeffle Xu 
909032b6e8SJeffle Xu 	if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags))
919032b6e8SJeffle Xu 		return -EOPNOTSUPP;
929032b6e8SJeffle Xu 
939032b6e8SJeffle Xu 	id = arg;
949032b6e8SJeffle Xu 	req = xa_erase(&cache->reqs, id);
959032b6e8SJeffle Xu 	if (!req)
969032b6e8SJeffle Xu 		return -EINVAL;
979032b6e8SJeffle Xu 
98*1519670eSJeffle Xu 	trace_cachefiles_ondemand_cread(object, id);
999032b6e8SJeffle Xu 	complete(&req->done);
1009032b6e8SJeffle Xu 	return 0;
1019032b6e8SJeffle Xu }
1029032b6e8SJeffle Xu 
103c8383054SJeffle Xu static const struct file_operations cachefiles_ondemand_fd_fops = {
104c8383054SJeffle Xu 	.owner		= THIS_MODULE,
105c8383054SJeffle Xu 	.release	= cachefiles_ondemand_fd_release,
106c8383054SJeffle Xu 	.write_iter	= cachefiles_ondemand_fd_write_iter,
107c8383054SJeffle Xu 	.llseek		= cachefiles_ondemand_fd_llseek,
1089032b6e8SJeffle Xu 	.unlocked_ioctl	= cachefiles_ondemand_fd_ioctl,
109c8383054SJeffle Xu };
110c8383054SJeffle Xu 
111c8383054SJeffle Xu /*
112c8383054SJeffle Xu  * OPEN request Completion (copen)
113c8383054SJeffle Xu  * - command: "copen <id>,<cache_size>"
114c8383054SJeffle Xu  *   <cache_size> indicates the object size if >=0, error code if negative
115c8383054SJeffle Xu  */
116c8383054SJeffle Xu int cachefiles_ondemand_copen(struct cachefiles_cache *cache, char *args)
117c8383054SJeffle Xu {
118c8383054SJeffle Xu 	struct cachefiles_req *req;
119c8383054SJeffle Xu 	struct fscache_cookie *cookie;
120c8383054SJeffle Xu 	char *pid, *psize;
121c8383054SJeffle Xu 	unsigned long id;
122c8383054SJeffle Xu 	long size;
123c8383054SJeffle Xu 	int ret;
124c8383054SJeffle Xu 
125c8383054SJeffle Xu 	if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags))
126c8383054SJeffle Xu 		return -EOPNOTSUPP;
127c8383054SJeffle Xu 
128c8383054SJeffle Xu 	if (!*args) {
129c8383054SJeffle Xu 		pr_err("Empty id specified\n");
130c8383054SJeffle Xu 		return -EINVAL;
131c8383054SJeffle Xu 	}
132c8383054SJeffle Xu 
133c8383054SJeffle Xu 	pid = args;
134c8383054SJeffle Xu 	psize = strchr(args, ',');
135c8383054SJeffle Xu 	if (!psize) {
136c8383054SJeffle Xu 		pr_err("Cache size is not specified\n");
137c8383054SJeffle Xu 		return -EINVAL;
138c8383054SJeffle Xu 	}
139c8383054SJeffle Xu 
140c8383054SJeffle Xu 	*psize = 0;
141c8383054SJeffle Xu 	psize++;
142c8383054SJeffle Xu 
143c8383054SJeffle Xu 	ret = kstrtoul(pid, 0, &id);
144c8383054SJeffle Xu 	if (ret)
145c8383054SJeffle Xu 		return ret;
146c8383054SJeffle Xu 
147c8383054SJeffle Xu 	req = xa_erase(&cache->reqs, id);
148c8383054SJeffle Xu 	if (!req)
149c8383054SJeffle Xu 		return -EINVAL;
150c8383054SJeffle Xu 
151c8383054SJeffle Xu 	/* fail OPEN request if copen format is invalid */
152c8383054SJeffle Xu 	ret = kstrtol(psize, 0, &size);
153c8383054SJeffle Xu 	if (ret) {
154c8383054SJeffle Xu 		req->error = ret;
155c8383054SJeffle Xu 		goto out;
156c8383054SJeffle Xu 	}
157c8383054SJeffle Xu 
158c8383054SJeffle Xu 	/* fail OPEN request if daemon reports an error */
159c8383054SJeffle Xu 	if (size < 0) {
160c8383054SJeffle Xu 		if (!IS_ERR_VALUE(size))
161c8383054SJeffle Xu 			size = -EINVAL;
162c8383054SJeffle Xu 		req->error = size;
163c8383054SJeffle Xu 		goto out;
164c8383054SJeffle Xu 	}
165c8383054SJeffle Xu 
166c8383054SJeffle Xu 	cookie = req->object->cookie;
167c8383054SJeffle Xu 	cookie->object_size = size;
168c8383054SJeffle Xu 	if (size)
169c8383054SJeffle Xu 		clear_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags);
170c8383054SJeffle Xu 	else
171c8383054SJeffle Xu 		set_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags);
172*1519670eSJeffle Xu 	trace_cachefiles_ondemand_copen(req->object, id, size);
173c8383054SJeffle Xu 
174c8383054SJeffle Xu out:
175c8383054SJeffle Xu 	complete(&req->done);
176c8383054SJeffle Xu 	return ret;
177c8383054SJeffle Xu }
178c8383054SJeffle Xu 
179c8383054SJeffle Xu static int cachefiles_ondemand_get_fd(struct cachefiles_req *req)
180c8383054SJeffle Xu {
181c8383054SJeffle Xu 	struct cachefiles_object *object;
182c8383054SJeffle Xu 	struct cachefiles_cache *cache;
183c8383054SJeffle Xu 	struct cachefiles_open *load;
184c8383054SJeffle Xu 	struct file *file;
185c8383054SJeffle Xu 	u32 object_id;
186c8383054SJeffle Xu 	int ret, fd;
187c8383054SJeffle Xu 
188c8383054SJeffle Xu 	object = cachefiles_grab_object(req->object,
189c8383054SJeffle Xu 			cachefiles_obj_get_ondemand_fd);
190c8383054SJeffle Xu 	cache = object->volume->cache;
191c8383054SJeffle Xu 
192c8383054SJeffle Xu 	ret = xa_alloc_cyclic(&cache->ondemand_ids, &object_id, NULL,
193c8383054SJeffle Xu 			      XA_LIMIT(1, INT_MAX),
194c8383054SJeffle Xu 			      &cache->ondemand_id_next, GFP_KERNEL);
195c8383054SJeffle Xu 	if (ret < 0)
196c8383054SJeffle Xu 		goto err;
197c8383054SJeffle Xu 
198c8383054SJeffle Xu 	fd = get_unused_fd_flags(O_WRONLY);
199c8383054SJeffle Xu 	if (fd < 0) {
200c8383054SJeffle Xu 		ret = fd;
201c8383054SJeffle Xu 		goto err_free_id;
202c8383054SJeffle Xu 	}
203c8383054SJeffle Xu 
204c8383054SJeffle Xu 	file = anon_inode_getfile("[cachefiles]", &cachefiles_ondemand_fd_fops,
205c8383054SJeffle Xu 				  object, O_WRONLY);
206c8383054SJeffle Xu 	if (IS_ERR(file)) {
207c8383054SJeffle Xu 		ret = PTR_ERR(file);
208c8383054SJeffle Xu 		goto err_put_fd;
209c8383054SJeffle Xu 	}
210c8383054SJeffle Xu 
211c8383054SJeffle Xu 	file->f_mode |= FMODE_PWRITE | FMODE_LSEEK;
212c8383054SJeffle Xu 	fd_install(fd, file);
213c8383054SJeffle Xu 
214c8383054SJeffle Xu 	load = (void *)req->msg.data;
215c8383054SJeffle Xu 	load->fd = fd;
216c8383054SJeffle Xu 	req->msg.object_id = object_id;
217c8383054SJeffle Xu 	object->ondemand_id = object_id;
218d11b0b04SJeffle Xu 
219d11b0b04SJeffle Xu 	cachefiles_get_unbind_pincount(cache);
220*1519670eSJeffle Xu 	trace_cachefiles_ondemand_open(object, &req->msg, load);
221c8383054SJeffle Xu 	return 0;
222c8383054SJeffle Xu 
223c8383054SJeffle Xu err_put_fd:
224c8383054SJeffle Xu 	put_unused_fd(fd);
225c8383054SJeffle Xu err_free_id:
226c8383054SJeffle Xu 	xa_erase(&cache->ondemand_ids, object_id);
227c8383054SJeffle Xu err:
228c8383054SJeffle Xu 	cachefiles_put_object(object, cachefiles_obj_put_ondemand_fd);
229c8383054SJeffle Xu 	return ret;
230c8383054SJeffle Xu }
231c8383054SJeffle Xu 
232c8383054SJeffle Xu ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache,
233c8383054SJeffle Xu 					char __user *_buffer, size_t buflen)
234c8383054SJeffle Xu {
235c8383054SJeffle Xu 	struct cachefiles_req *req;
236c8383054SJeffle Xu 	struct cachefiles_msg *msg;
237c8383054SJeffle Xu 	unsigned long id = 0;
238c8383054SJeffle Xu 	size_t n;
239c8383054SJeffle Xu 	int ret = 0;
240c8383054SJeffle Xu 	XA_STATE(xas, &cache->reqs, 0);
241c8383054SJeffle Xu 
242c8383054SJeffle Xu 	/*
243c8383054SJeffle Xu 	 * Search for a request that has not ever been processed, to prevent
244c8383054SJeffle Xu 	 * requests from being processed repeatedly.
245c8383054SJeffle Xu 	 */
246c8383054SJeffle Xu 	xa_lock(&cache->reqs);
247c8383054SJeffle Xu 	req = xas_find_marked(&xas, UINT_MAX, CACHEFILES_REQ_NEW);
248c8383054SJeffle Xu 	if (!req) {
249c8383054SJeffle Xu 		xa_unlock(&cache->reqs);
250c8383054SJeffle Xu 		return 0;
251c8383054SJeffle Xu 	}
252c8383054SJeffle Xu 
253c8383054SJeffle Xu 	msg = &req->msg;
254c8383054SJeffle Xu 	n = msg->len;
255c8383054SJeffle Xu 
256c8383054SJeffle Xu 	if (n > buflen) {
257c8383054SJeffle Xu 		xa_unlock(&cache->reqs);
258c8383054SJeffle Xu 		return -EMSGSIZE;
259c8383054SJeffle Xu 	}
260c8383054SJeffle Xu 
261c8383054SJeffle Xu 	xas_clear_mark(&xas, CACHEFILES_REQ_NEW);
262c8383054SJeffle Xu 	xa_unlock(&cache->reqs);
263c8383054SJeffle Xu 
264c8383054SJeffle Xu 	id = xas.xa_index;
265c8383054SJeffle Xu 	msg->msg_id = id;
266c8383054SJeffle Xu 
267c8383054SJeffle Xu 	if (msg->opcode == CACHEFILES_OP_OPEN) {
268c8383054SJeffle Xu 		ret = cachefiles_ondemand_get_fd(req);
269c8383054SJeffle Xu 		if (ret)
270c8383054SJeffle Xu 			goto error;
271c8383054SJeffle Xu 	}
272c8383054SJeffle Xu 
273c8383054SJeffle Xu 	if (copy_to_user(_buffer, msg, n) != 0) {
274c8383054SJeffle Xu 		ret = -EFAULT;
275c8383054SJeffle Xu 		goto err_put_fd;
276c8383054SJeffle Xu 	}
277c8383054SJeffle Xu 
278324b954aSJeffle Xu 	/* CLOSE request has no reply */
279324b954aSJeffle Xu 	if (msg->opcode == CACHEFILES_OP_CLOSE) {
280324b954aSJeffle Xu 		xa_erase(&cache->reqs, id);
281324b954aSJeffle Xu 		complete(&req->done);
282324b954aSJeffle Xu 	}
283324b954aSJeffle Xu 
284c8383054SJeffle Xu 	return n;
285c8383054SJeffle Xu 
286c8383054SJeffle Xu err_put_fd:
287c8383054SJeffle Xu 	if (msg->opcode == CACHEFILES_OP_OPEN)
288c8383054SJeffle Xu 		close_fd(((struct cachefiles_open *)msg->data)->fd);
289c8383054SJeffle Xu error:
290c8383054SJeffle Xu 	xa_erase(&cache->reqs, id);
291c8383054SJeffle Xu 	req->error = ret;
292c8383054SJeffle Xu 	complete(&req->done);
293c8383054SJeffle Xu 	return ret;
294c8383054SJeffle Xu }
295c8383054SJeffle Xu 
296c8383054SJeffle Xu typedef int (*init_req_fn)(struct cachefiles_req *req, void *private);
297c8383054SJeffle Xu 
298c8383054SJeffle Xu static int cachefiles_ondemand_send_req(struct cachefiles_object *object,
299c8383054SJeffle Xu 					enum cachefiles_opcode opcode,
300c8383054SJeffle Xu 					size_t data_len,
301c8383054SJeffle Xu 					init_req_fn init_req,
302c8383054SJeffle Xu 					void *private)
303c8383054SJeffle Xu {
304c8383054SJeffle Xu 	struct cachefiles_cache *cache = object->volume->cache;
305c8383054SJeffle Xu 	struct cachefiles_req *req;
306c8383054SJeffle Xu 	XA_STATE(xas, &cache->reqs, 0);
307c8383054SJeffle Xu 	int ret;
308c8383054SJeffle Xu 
309c8383054SJeffle Xu 	if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags))
310c8383054SJeffle Xu 		return 0;
311c8383054SJeffle Xu 
312c8383054SJeffle Xu 	if (test_bit(CACHEFILES_DEAD, &cache->flags))
313c8383054SJeffle Xu 		return -EIO;
314c8383054SJeffle Xu 
315c8383054SJeffle Xu 	req = kzalloc(sizeof(*req) + data_len, GFP_KERNEL);
316c8383054SJeffle Xu 	if (!req)
317c8383054SJeffle Xu 		return -ENOMEM;
318c8383054SJeffle Xu 
319c8383054SJeffle Xu 	req->object = object;
320c8383054SJeffle Xu 	init_completion(&req->done);
321c8383054SJeffle Xu 	req->msg.opcode = opcode;
322c8383054SJeffle Xu 	req->msg.len = sizeof(struct cachefiles_msg) + data_len;
323c8383054SJeffle Xu 
324c8383054SJeffle Xu 	ret = init_req(req, private);
325c8383054SJeffle Xu 	if (ret)
326c8383054SJeffle Xu 		goto out;
327c8383054SJeffle Xu 
328c8383054SJeffle Xu 	do {
329c8383054SJeffle Xu 		/*
330c8383054SJeffle Xu 		 * Stop enqueuing the request when daemon is dying. The
331c8383054SJeffle Xu 		 * following two operations need to be atomic as a whole.
332c8383054SJeffle Xu 		 *   1) check cache state, and
333c8383054SJeffle Xu 		 *   2) enqueue request if cache is alive.
334c8383054SJeffle Xu 		 * Otherwise the request may be enqueued after xarray has been
335c8383054SJeffle Xu 		 * flushed, leaving the orphan request never being completed.
336c8383054SJeffle Xu 		 *
337c8383054SJeffle Xu 		 * CPU 1			CPU 2
338c8383054SJeffle Xu 		 * =====			=====
339c8383054SJeffle Xu 		 *				test CACHEFILES_DEAD bit
340c8383054SJeffle Xu 		 * set CACHEFILES_DEAD bit
341c8383054SJeffle Xu 		 * flush requests in the xarray
342c8383054SJeffle Xu 		 *				enqueue the request
343c8383054SJeffle Xu 		 */
344c8383054SJeffle Xu 		xas_lock(&xas);
345c8383054SJeffle Xu 
346c8383054SJeffle Xu 		if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
347c8383054SJeffle Xu 			xas_unlock(&xas);
348c8383054SJeffle Xu 			ret = -EIO;
349c8383054SJeffle Xu 			goto out;
350c8383054SJeffle Xu 		}
351c8383054SJeffle Xu 
352c8383054SJeffle Xu 		/* coupled with the barrier in cachefiles_flush_reqs() */
353c8383054SJeffle Xu 		smp_mb();
354c8383054SJeffle Xu 
355324b954aSJeffle Xu 		if (opcode != CACHEFILES_OP_OPEN && object->ondemand_id <= 0) {
356324b954aSJeffle Xu 			WARN_ON_ONCE(object->ondemand_id == 0);
357324b954aSJeffle Xu 			xas_unlock(&xas);
358324b954aSJeffle Xu 			ret = -EIO;
359324b954aSJeffle Xu 			goto out;
360324b954aSJeffle Xu 		}
361324b954aSJeffle Xu 
362c8383054SJeffle Xu 		xas.xa_index = 0;
363c8383054SJeffle Xu 		xas_find_marked(&xas, UINT_MAX, XA_FREE_MARK);
364c8383054SJeffle Xu 		if (xas.xa_node == XAS_RESTART)
365c8383054SJeffle Xu 			xas_set_err(&xas, -EBUSY);
366c8383054SJeffle Xu 		xas_store(&xas, req);
367c8383054SJeffle Xu 		xas_clear_mark(&xas, XA_FREE_MARK);
368c8383054SJeffle Xu 		xas_set_mark(&xas, CACHEFILES_REQ_NEW);
369c8383054SJeffle Xu 		xas_unlock(&xas);
370c8383054SJeffle Xu 	} while (xas_nomem(&xas, GFP_KERNEL));
371c8383054SJeffle Xu 
372c8383054SJeffle Xu 	ret = xas_error(&xas);
373c8383054SJeffle Xu 	if (ret)
374c8383054SJeffle Xu 		goto out;
375c8383054SJeffle Xu 
376c8383054SJeffle Xu 	wake_up_all(&cache->daemon_pollwq);
377c8383054SJeffle Xu 	wait_for_completion(&req->done);
378c8383054SJeffle Xu 	ret = req->error;
379c8383054SJeffle Xu out:
380c8383054SJeffle Xu 	kfree(req);
381c8383054SJeffle Xu 	return ret;
382c8383054SJeffle Xu }
383c8383054SJeffle Xu 
384c8383054SJeffle Xu static int cachefiles_ondemand_init_open_req(struct cachefiles_req *req,
385c8383054SJeffle Xu 					     void *private)
386c8383054SJeffle Xu {
387c8383054SJeffle Xu 	struct cachefiles_object *object = req->object;
388c8383054SJeffle Xu 	struct fscache_cookie *cookie = object->cookie;
389c8383054SJeffle Xu 	struct fscache_volume *volume = object->volume->vcookie;
390c8383054SJeffle Xu 	struct cachefiles_open *load = (void *)req->msg.data;
391c8383054SJeffle Xu 	size_t volume_key_size, cookie_key_size;
392c8383054SJeffle Xu 	void *volume_key, *cookie_key;
393c8383054SJeffle Xu 
394c8383054SJeffle Xu 	/*
395c8383054SJeffle Xu 	 * Volume key is a NUL-terminated string. key[0] stores strlen() of the
396c8383054SJeffle Xu 	 * string, followed by the content of the string (excluding '\0').
397c8383054SJeffle Xu 	 */
398c8383054SJeffle Xu 	volume_key_size = volume->key[0] + 1;
399c8383054SJeffle Xu 	volume_key = volume->key + 1;
400c8383054SJeffle Xu 
401c8383054SJeffle Xu 	/* Cookie key is binary data, which is netfs specific. */
402c8383054SJeffle Xu 	cookie_key_size = cookie->key_len;
403c8383054SJeffle Xu 	cookie_key = fscache_get_key(cookie);
404c8383054SJeffle Xu 
405c8383054SJeffle Xu 	if (!(object->cookie->advice & FSCACHE_ADV_WANT_CACHE_SIZE)) {
406c8383054SJeffle Xu 		pr_err("WANT_CACHE_SIZE is needed for on-demand mode\n");
407c8383054SJeffle Xu 		return -EINVAL;
408c8383054SJeffle Xu 	}
409c8383054SJeffle Xu 
410c8383054SJeffle Xu 	load->volume_key_size = volume_key_size;
411c8383054SJeffle Xu 	load->cookie_key_size = cookie_key_size;
412c8383054SJeffle Xu 	memcpy(load->data, volume_key, volume_key_size);
413c8383054SJeffle Xu 	memcpy(load->data + volume_key_size, cookie_key, cookie_key_size);
414c8383054SJeffle Xu 
415c8383054SJeffle Xu 	return 0;
416c8383054SJeffle Xu }
417c8383054SJeffle Xu 
418324b954aSJeffle Xu static int cachefiles_ondemand_init_close_req(struct cachefiles_req *req,
419324b954aSJeffle Xu 					      void *private)
420324b954aSJeffle Xu {
421324b954aSJeffle Xu 	struct cachefiles_object *object = req->object;
422324b954aSJeffle Xu 	int object_id = object->ondemand_id;
423324b954aSJeffle Xu 
424324b954aSJeffle Xu 	/*
425324b954aSJeffle Xu 	 * It's possible that object id is still 0 if the cookie looking up
426324b954aSJeffle Xu 	 * phase failed before OPEN request has ever been sent. Also avoid
427324b954aSJeffle Xu 	 * sending CLOSE request for CACHEFILES_ONDEMAND_ID_CLOSED, which means
428324b954aSJeffle Xu 	 * anon_fd has already been closed.
429324b954aSJeffle Xu 	 */
430324b954aSJeffle Xu 	if (object_id <= 0)
431324b954aSJeffle Xu 		return -ENOENT;
432324b954aSJeffle Xu 
433324b954aSJeffle Xu 	req->msg.object_id = object_id;
434*1519670eSJeffle Xu 	trace_cachefiles_ondemand_close(object, &req->msg);
435324b954aSJeffle Xu 	return 0;
436324b954aSJeffle Xu }
437324b954aSJeffle Xu 
4389032b6e8SJeffle Xu struct cachefiles_read_ctx {
4399032b6e8SJeffle Xu 	loff_t off;
4409032b6e8SJeffle Xu 	size_t len;
4419032b6e8SJeffle Xu };
4429032b6e8SJeffle Xu 
4439032b6e8SJeffle Xu static int cachefiles_ondemand_init_read_req(struct cachefiles_req *req,
4449032b6e8SJeffle Xu 					     void *private)
4459032b6e8SJeffle Xu {
4469032b6e8SJeffle Xu 	struct cachefiles_object *object = req->object;
4479032b6e8SJeffle Xu 	struct cachefiles_read *load = (void *)req->msg.data;
4489032b6e8SJeffle Xu 	struct cachefiles_read_ctx *read_ctx = private;
4499032b6e8SJeffle Xu 	int object_id = object->ondemand_id;
4509032b6e8SJeffle Xu 
4519032b6e8SJeffle Xu 	/* Stop enqueuing requests when daemon has closed anon_fd. */
4529032b6e8SJeffle Xu 	if (object_id <= 0) {
4539032b6e8SJeffle Xu 		WARN_ON_ONCE(object_id == 0);
4549032b6e8SJeffle Xu 		pr_info_once("READ: anonymous fd closed prematurely.\n");
4559032b6e8SJeffle Xu 		return -EIO;
4569032b6e8SJeffle Xu 	}
4579032b6e8SJeffle Xu 
4589032b6e8SJeffle Xu 	req->msg.object_id = object_id;
4599032b6e8SJeffle Xu 	load->off = read_ctx->off;
4609032b6e8SJeffle Xu 	load->len = read_ctx->len;
461*1519670eSJeffle Xu 	trace_cachefiles_ondemand_read(object, &req->msg, load);
4629032b6e8SJeffle Xu 	return 0;
4639032b6e8SJeffle Xu }
4649032b6e8SJeffle Xu 
465c8383054SJeffle Xu int cachefiles_ondemand_init_object(struct cachefiles_object *object)
466c8383054SJeffle Xu {
467c8383054SJeffle Xu 	struct fscache_cookie *cookie = object->cookie;
468c8383054SJeffle Xu 	struct fscache_volume *volume = object->volume->vcookie;
469c8383054SJeffle Xu 	size_t volume_key_size, cookie_key_size, data_len;
470c8383054SJeffle Xu 
471c8383054SJeffle Xu 	/*
472c8383054SJeffle Xu 	 * CacheFiles will firstly check the cache file under the root cache
473c8383054SJeffle Xu 	 * directory. If the coherency check failed, it will fallback to
474c8383054SJeffle Xu 	 * creating a new tmpfile as the cache file. Reuse the previously
475c8383054SJeffle Xu 	 * allocated object ID if any.
476c8383054SJeffle Xu 	 */
477c8383054SJeffle Xu 	if (object->ondemand_id > 0)
478c8383054SJeffle Xu 		return 0;
479c8383054SJeffle Xu 
480c8383054SJeffle Xu 	volume_key_size = volume->key[0] + 1;
481c8383054SJeffle Xu 	cookie_key_size = cookie->key_len;
482c8383054SJeffle Xu 	data_len = sizeof(struct cachefiles_open) +
483c8383054SJeffle Xu 		   volume_key_size + cookie_key_size;
484c8383054SJeffle Xu 
485c8383054SJeffle Xu 	return cachefiles_ondemand_send_req(object, CACHEFILES_OP_OPEN,
486c8383054SJeffle Xu 			data_len, cachefiles_ondemand_init_open_req, NULL);
487c8383054SJeffle Xu }
488324b954aSJeffle Xu 
489324b954aSJeffle Xu void cachefiles_ondemand_clean_object(struct cachefiles_object *object)
490324b954aSJeffle Xu {
491324b954aSJeffle Xu 	cachefiles_ondemand_send_req(object, CACHEFILES_OP_CLOSE, 0,
492324b954aSJeffle Xu 			cachefiles_ondemand_init_close_req, NULL);
493324b954aSJeffle Xu }
4949032b6e8SJeffle Xu 
4959032b6e8SJeffle Xu int cachefiles_ondemand_read(struct cachefiles_object *object,
4969032b6e8SJeffle Xu 			     loff_t pos, size_t len)
4979032b6e8SJeffle Xu {
4989032b6e8SJeffle Xu 	struct cachefiles_read_ctx read_ctx = {pos, len};
4999032b6e8SJeffle Xu 
5009032b6e8SJeffle Xu 	return cachefiles_ondemand_send_req(object, CACHEFILES_OP_READ,
5019032b6e8SJeffle Xu 			sizeof(struct cachefiles_read),
5029032b6e8SJeffle Xu 			cachefiles_ondemand_init_read_req, &read_ctx);
5039032b6e8SJeffle Xu }
504