xref: /openbmc/linux/fs/cachefiles/ondemand.c (revision 8a73c08e00fe9fdb4a29e63d530709c64af74cae)
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 
7d2d3eb37SBaokun Li struct ondemand_anon_file {
8d2d3eb37SBaokun Li 	struct file *file;
9d2d3eb37SBaokun Li 	int fd;
10d2d3eb37SBaokun Li };
11d2d3eb37SBaokun Li 
12a6de8276SBaokun Li static inline void cachefiles_req_put(struct cachefiles_req *req)
13a6de8276SBaokun Li {
14a6de8276SBaokun Li 	if (refcount_dec_and_test(&req->ref))
15a6de8276SBaokun Li 		kfree(req);
16a6de8276SBaokun Li }
17a6de8276SBaokun Li 
18c8383054SJeffle Xu static int cachefiles_ondemand_fd_release(struct inode *inode,
19c8383054SJeffle Xu 					  struct file *file)
20c8383054SJeffle Xu {
21c8383054SJeffle Xu 	struct cachefiles_object *object = file->private_data;
22527db1cbSBaokun Li 	struct cachefiles_cache *cache;
23527db1cbSBaokun Li 	struct cachefiles_ondemand_info *info;
24e564e48cSBaokun Li 	int object_id;
259032b6e8SJeffle Xu 	struct cachefiles_req *req;
26527db1cbSBaokun Li 	XA_STATE(xas, NULL, 0);
27527db1cbSBaokun Li 
28527db1cbSBaokun Li 	if (!object)
29527db1cbSBaokun Li 		return 0;
30527db1cbSBaokun Li 
31527db1cbSBaokun Li 	info = object->ondemand;
32527db1cbSBaokun Li 	cache = object->volume->cache;
33527db1cbSBaokun Li 	xas.xa = &cache->reqs;
34c8383054SJeffle Xu 
359032b6e8SJeffle Xu 	xa_lock(&cache->reqs);
36e564e48cSBaokun Li 	spin_lock(&info->lock);
37e564e48cSBaokun Li 	object_id = info->ondemand_id;
3833d21f06SJia Zhu 	info->ondemand_id = CACHEFILES_ONDEMAND_ID_CLOSED;
39955190e1SJia Zhu 	cachefiles_ondemand_set_object_close(object);
40e564e48cSBaokun Li 	spin_unlock(&info->lock);
419032b6e8SJeffle Xu 
42f740fd94SJia Zhu 	/* Only flush CACHEFILES_REQ_NEW marked req to avoid race with daemon_read */
43f740fd94SJia Zhu 	xas_for_each_marked(&xas, req, ULONG_MAX, CACHEFILES_REQ_NEW) {
4465aa5f6fSJia Zhu 		if (req->msg.object_id == object_id &&
45f740fd94SJia Zhu 		    req->msg.opcode == CACHEFILES_OP_CLOSE) {
469032b6e8SJeffle Xu 			complete(&req->done);
479032b6e8SJeffle Xu 			xas_store(&xas, NULL);
489032b6e8SJeffle Xu 		}
499032b6e8SJeffle Xu 	}
509032b6e8SJeffle Xu 	xa_unlock(&cache->reqs);
519032b6e8SJeffle Xu 
52c8383054SJeffle Xu 	xa_erase(&cache->ondemand_ids, object_id);
531519670eSJeffle Xu 	trace_cachefiles_ondemand_fd_release(object, object_id);
54c8383054SJeffle Xu 	cachefiles_put_object(object, cachefiles_obj_put_ondemand_fd);
55d11b0b04SJeffle Xu 	cachefiles_put_unbind_pincount(cache);
56c8383054SJeffle Xu 	return 0;
57c8383054SJeffle Xu }
58c8383054SJeffle Xu 
59c8383054SJeffle Xu static ssize_t cachefiles_ondemand_fd_write_iter(struct kiocb *kiocb,
60c8383054SJeffle Xu 						 struct iov_iter *iter)
61c8383054SJeffle Xu {
62c8383054SJeffle Xu 	struct cachefiles_object *object = kiocb->ki_filp->private_data;
63c8383054SJeffle Xu 	struct cachefiles_cache *cache = object->volume->cache;
64c8383054SJeffle Xu 	struct file *file = object->file;
65c8383054SJeffle Xu 	size_t len = iter->count;
66c8383054SJeffle Xu 	loff_t pos = kiocb->ki_pos;
67c8383054SJeffle Xu 	const struct cred *saved_cred;
68c8383054SJeffle Xu 	int ret;
69c8383054SJeffle Xu 
70c8383054SJeffle Xu 	if (!file)
71c8383054SJeffle Xu 		return -ENOBUFS;
72c8383054SJeffle Xu 
73c8383054SJeffle Xu 	cachefiles_begin_secure(cache, &saved_cred);
74c8383054SJeffle Xu 	ret = __cachefiles_prepare_write(object, file, &pos, &len, true);
75c8383054SJeffle Xu 	cachefiles_end_secure(cache, saved_cred);
76c8383054SJeffle Xu 	if (ret < 0)
77c8383054SJeffle Xu 		return ret;
78c8383054SJeffle Xu 
791519670eSJeffle Xu 	trace_cachefiles_ondemand_fd_write(object, file_inode(file), pos, len);
80c8383054SJeffle Xu 	ret = __cachefiles_write(object, file, pos, iter, NULL, NULL);
81c8383054SJeffle Xu 	if (!ret)
82c8383054SJeffle Xu 		ret = len;
83c8383054SJeffle Xu 
84c8383054SJeffle Xu 	return ret;
85c8383054SJeffle Xu }
86c8383054SJeffle Xu 
87c8383054SJeffle Xu static loff_t cachefiles_ondemand_fd_llseek(struct file *filp, loff_t pos,
88c8383054SJeffle Xu 					    int whence)
89c8383054SJeffle Xu {
90c8383054SJeffle Xu 	struct cachefiles_object *object = filp->private_data;
91c8383054SJeffle Xu 	struct file *file = object->file;
92c8383054SJeffle Xu 
93c8383054SJeffle Xu 	if (!file)
94c8383054SJeffle Xu 		return -ENOBUFS;
95c8383054SJeffle Xu 
96c8383054SJeffle Xu 	return vfs_llseek(file, pos, whence);
97c8383054SJeffle Xu }
98c8383054SJeffle Xu 
999032b6e8SJeffle Xu static long cachefiles_ondemand_fd_ioctl(struct file *filp, unsigned int ioctl,
1009032b6e8SJeffle Xu 					 unsigned long arg)
1019032b6e8SJeffle Xu {
1029032b6e8SJeffle Xu 	struct cachefiles_object *object = filp->private_data;
1039032b6e8SJeffle Xu 	struct cachefiles_cache *cache = object->volume->cache;
1049032b6e8SJeffle Xu 	struct cachefiles_req *req;
1059032b6e8SJeffle Xu 	unsigned long id;
1069032b6e8SJeffle Xu 
1079032b6e8SJeffle Xu 	if (ioctl != CACHEFILES_IOC_READ_COMPLETE)
1089032b6e8SJeffle Xu 		return -EINVAL;
1099032b6e8SJeffle Xu 
1109032b6e8SJeffle Xu 	if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags))
1119032b6e8SJeffle Xu 		return -EOPNOTSUPP;
1129032b6e8SJeffle Xu 
1139032b6e8SJeffle Xu 	id = arg;
1149032b6e8SJeffle Xu 	req = xa_erase(&cache->reqs, id);
1159032b6e8SJeffle Xu 	if (!req)
1169032b6e8SJeffle Xu 		return -EINVAL;
1179032b6e8SJeffle Xu 
1181519670eSJeffle Xu 	trace_cachefiles_ondemand_cread(object, id);
1199032b6e8SJeffle Xu 	complete(&req->done);
1209032b6e8SJeffle Xu 	return 0;
1219032b6e8SJeffle Xu }
1229032b6e8SJeffle Xu 
123c8383054SJeffle Xu static const struct file_operations cachefiles_ondemand_fd_fops = {
124c8383054SJeffle Xu 	.owner		= THIS_MODULE,
125c8383054SJeffle Xu 	.release	= cachefiles_ondemand_fd_release,
126c8383054SJeffle Xu 	.write_iter	= cachefiles_ondemand_fd_write_iter,
127c8383054SJeffle Xu 	.llseek		= cachefiles_ondemand_fd_llseek,
1289032b6e8SJeffle Xu 	.unlocked_ioctl	= cachefiles_ondemand_fd_ioctl,
129c8383054SJeffle Xu };
130c8383054SJeffle Xu 
131c8383054SJeffle Xu /*
132c8383054SJeffle Xu  * OPEN request Completion (copen)
133c8383054SJeffle Xu  * - command: "copen <id>,<cache_size>"
134c8383054SJeffle Xu  *   <cache_size> indicates the object size if >=0, error code if negative
135c8383054SJeffle Xu  */
136c8383054SJeffle Xu int cachefiles_ondemand_copen(struct cachefiles_cache *cache, char *args)
137c8383054SJeffle Xu {
138c8383054SJeffle Xu 	struct cachefiles_req *req;
139c8383054SJeffle Xu 	struct fscache_cookie *cookie;
140e564e48cSBaokun Li 	struct cachefiles_ondemand_info *info;
141c8383054SJeffle Xu 	char *pid, *psize;
142c8383054SJeffle Xu 	unsigned long id;
143c8383054SJeffle Xu 	long size;
144c8383054SJeffle Xu 	int ret;
145c8383054SJeffle Xu 
146c8383054SJeffle Xu 	if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags))
147c8383054SJeffle Xu 		return -EOPNOTSUPP;
148c8383054SJeffle Xu 
149c8383054SJeffle Xu 	if (!*args) {
150c8383054SJeffle Xu 		pr_err("Empty id specified\n");
151c8383054SJeffle Xu 		return -EINVAL;
152c8383054SJeffle Xu 	}
153c8383054SJeffle Xu 
154c8383054SJeffle Xu 	pid = args;
155c8383054SJeffle Xu 	psize = strchr(args, ',');
156c8383054SJeffle Xu 	if (!psize) {
157c8383054SJeffle Xu 		pr_err("Cache size is not specified\n");
158c8383054SJeffle Xu 		return -EINVAL;
159c8383054SJeffle Xu 	}
160c8383054SJeffle Xu 
161c8383054SJeffle Xu 	*psize = 0;
162c8383054SJeffle Xu 	psize++;
163c8383054SJeffle Xu 
164c8383054SJeffle Xu 	ret = kstrtoul(pid, 0, &id);
165c8383054SJeffle Xu 	if (ret)
166c8383054SJeffle Xu 		return ret;
167c8383054SJeffle Xu 
168c8383054SJeffle Xu 	req = xa_erase(&cache->reqs, id);
169c8383054SJeffle Xu 	if (!req)
170c8383054SJeffle Xu 		return -EINVAL;
171c8383054SJeffle Xu 
172c8383054SJeffle Xu 	/* fail OPEN request if copen format is invalid */
173c8383054SJeffle Xu 	ret = kstrtol(psize, 0, &size);
174c8383054SJeffle Xu 	if (ret) {
175c8383054SJeffle Xu 		req->error = ret;
176c8383054SJeffle Xu 		goto out;
177c8383054SJeffle Xu 	}
178c8383054SJeffle Xu 
179c8383054SJeffle Xu 	/* fail OPEN request if daemon reports an error */
180c8383054SJeffle Xu 	if (size < 0) {
181c93ccd63SSun Ke 		if (!IS_ERR_VALUE(size)) {
182c93ccd63SSun Ke 			req->error = -EINVAL;
183c93ccd63SSun Ke 			ret = -EINVAL;
184c93ccd63SSun Ke 		} else {
185c8383054SJeffle Xu 			req->error = size;
186c93ccd63SSun Ke 			ret = 0;
187c93ccd63SSun Ke 		}
188c8383054SJeffle Xu 		goto out;
189c8383054SJeffle Xu 	}
190c8383054SJeffle Xu 
191e564e48cSBaokun Li 	info = req->object->ondemand;
192e564e48cSBaokun Li 	spin_lock(&info->lock);
193e564e48cSBaokun Li 	/*
194e564e48cSBaokun Li 	 * The anonymous fd was closed before copen ? Fail the request.
195e564e48cSBaokun Li 	 *
196e564e48cSBaokun Li 	 *             t1             |             t2
197e564e48cSBaokun Li 	 * ---------------------------------------------------------
198e564e48cSBaokun Li 	 *                             cachefiles_ondemand_copen
199e564e48cSBaokun Li 	 *                             req = xa_erase(&cache->reqs, id)
200e564e48cSBaokun Li 	 * // Anon fd is maliciously closed.
201e564e48cSBaokun Li 	 * cachefiles_ondemand_fd_release
202e564e48cSBaokun Li 	 * xa_lock(&cache->reqs)
203e564e48cSBaokun Li 	 * cachefiles_ondemand_set_object_close(object)
204e564e48cSBaokun Li 	 * xa_unlock(&cache->reqs)
205e564e48cSBaokun Li 	 *                             cachefiles_ondemand_set_object_open
206e564e48cSBaokun Li 	 *                             // No one will ever close it again.
207e564e48cSBaokun Li 	 * cachefiles_ondemand_daemon_read
208e564e48cSBaokun Li 	 * cachefiles_ondemand_select_req
209e564e48cSBaokun Li 	 *
210e564e48cSBaokun Li 	 * Get a read req but its fd is already closed. The daemon can't
211e564e48cSBaokun Li 	 * issue a cread ioctl with an closed fd, then hung.
212e564e48cSBaokun Li 	 */
213e564e48cSBaokun Li 	if (info->ondemand_id == CACHEFILES_ONDEMAND_ID_CLOSED) {
214e564e48cSBaokun Li 		spin_unlock(&info->lock);
215e564e48cSBaokun Li 		req->error = -EBADFD;
216e564e48cSBaokun Li 		goto out;
217e564e48cSBaokun Li 	}
218c8383054SJeffle Xu 	cookie = req->object->cookie;
219c8383054SJeffle Xu 	cookie->object_size = size;
220c8383054SJeffle Xu 	if (size)
221c8383054SJeffle Xu 		clear_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags);
222c8383054SJeffle Xu 	else
223c8383054SJeffle Xu 		set_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags);
2241519670eSJeffle Xu 	trace_cachefiles_ondemand_copen(req->object, id, size);
225c8383054SJeffle Xu 
226955190e1SJia Zhu 	cachefiles_ondemand_set_object_open(req->object);
227e564e48cSBaokun Li 	spin_unlock(&info->lock);
228f740fd94SJia Zhu 	wake_up_all(&cache->daemon_pollwq);
229955190e1SJia Zhu 
230c8383054SJeffle Xu out:
231c8383054SJeffle Xu 	complete(&req->done);
232c8383054SJeffle Xu 	return ret;
233c8383054SJeffle Xu }
234c8383054SJeffle Xu 
2359f5fa40fSJia Zhu int cachefiles_ondemand_restore(struct cachefiles_cache *cache, char *args)
2369f5fa40fSJia Zhu {
2379f5fa40fSJia Zhu 	struct cachefiles_req *req;
2389f5fa40fSJia Zhu 
2399f5fa40fSJia Zhu 	XA_STATE(xas, &cache->reqs, 0);
2409f5fa40fSJia Zhu 
2419f5fa40fSJia Zhu 	if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags))
2429f5fa40fSJia Zhu 		return -EOPNOTSUPP;
2439f5fa40fSJia Zhu 
2449f5fa40fSJia Zhu 	/*
2459f5fa40fSJia Zhu 	 * Reset the requests to CACHEFILES_REQ_NEW state, so that the
2469f5fa40fSJia Zhu 	 * requests have been processed halfway before the crash of the
2479f5fa40fSJia Zhu 	 * user daemon could be reprocessed after the recovery.
2489f5fa40fSJia Zhu 	 */
2499f5fa40fSJia Zhu 	xas_lock(&xas);
2509f5fa40fSJia Zhu 	xas_for_each(&xas, req, ULONG_MAX)
2519f5fa40fSJia Zhu 		xas_set_mark(&xas, CACHEFILES_REQ_NEW);
2529f5fa40fSJia Zhu 	xas_unlock(&xas);
2539f5fa40fSJia Zhu 
2549f5fa40fSJia Zhu 	wake_up_all(&cache->daemon_pollwq);
2559f5fa40fSJia Zhu 	return 0;
2569f5fa40fSJia Zhu }
2579f5fa40fSJia Zhu 
258d2d3eb37SBaokun Li static int cachefiles_ondemand_get_fd(struct cachefiles_req *req,
259d2d3eb37SBaokun Li 				      struct ondemand_anon_file *anon_file)
260c8383054SJeffle Xu {
261c8383054SJeffle Xu 	struct cachefiles_object *object;
262c8383054SJeffle Xu 	struct cachefiles_cache *cache;
263c8383054SJeffle Xu 	struct cachefiles_open *load;
264c8383054SJeffle Xu 	u32 object_id;
265d2d3eb37SBaokun Li 	int ret;
266c8383054SJeffle Xu 
267c8383054SJeffle Xu 	object = cachefiles_grab_object(req->object,
268c8383054SJeffle Xu 			cachefiles_obj_get_ondemand_fd);
269c8383054SJeffle Xu 	cache = object->volume->cache;
270c8383054SJeffle Xu 
271c8383054SJeffle Xu 	ret = xa_alloc_cyclic(&cache->ondemand_ids, &object_id, NULL,
272c8383054SJeffle Xu 			      XA_LIMIT(1, INT_MAX),
273c8383054SJeffle Xu 			      &cache->ondemand_id_next, GFP_KERNEL);
274c8383054SJeffle Xu 	if (ret < 0)
275c8383054SJeffle Xu 		goto err;
276c8383054SJeffle Xu 
277d2d3eb37SBaokun Li 	anon_file->fd = get_unused_fd_flags(O_WRONLY);
278d2d3eb37SBaokun Li 	if (anon_file->fd < 0) {
279d2d3eb37SBaokun Li 		ret = anon_file->fd;
280c8383054SJeffle Xu 		goto err_free_id;
281c8383054SJeffle Xu 	}
282c8383054SJeffle Xu 
283d2d3eb37SBaokun Li 	anon_file->file = anon_inode_getfile("[cachefiles]",
284d2d3eb37SBaokun Li 				&cachefiles_ondemand_fd_fops, object, O_WRONLY);
285d2d3eb37SBaokun Li 	if (IS_ERR(anon_file->file)) {
286d2d3eb37SBaokun Li 		ret = PTR_ERR(anon_file->file);
287c8383054SJeffle Xu 		goto err_put_fd;
288c8383054SJeffle Xu 	}
289c8383054SJeffle Xu 
290527db1cbSBaokun Li 	spin_lock(&object->ondemand->lock);
291527db1cbSBaokun Li 	if (object->ondemand->ondemand_id > 0) {
292527db1cbSBaokun Li 		spin_unlock(&object->ondemand->lock);
293527db1cbSBaokun Li 		/* Pair with check in cachefiles_ondemand_fd_release(). */
294d2d3eb37SBaokun Li 		anon_file->file->private_data = NULL;
295527db1cbSBaokun Li 		ret = -EEXIST;
296527db1cbSBaokun Li 		goto err_put_file;
297527db1cbSBaokun Li 	}
298527db1cbSBaokun Li 
299d2d3eb37SBaokun Li 	anon_file->file->f_mode |= FMODE_PWRITE | FMODE_LSEEK;
300c8383054SJeffle Xu 
301c8383054SJeffle Xu 	load = (void *)req->msg.data;
302d2d3eb37SBaokun Li 	load->fd = anon_file->fd;
30333d21f06SJia Zhu 	object->ondemand->ondemand_id = object_id;
304527db1cbSBaokun Li 	spin_unlock(&object->ondemand->lock);
305d11b0b04SJeffle Xu 
306d11b0b04SJeffle Xu 	cachefiles_get_unbind_pincount(cache);
3071519670eSJeffle Xu 	trace_cachefiles_ondemand_open(object, &req->msg, load);
308c8383054SJeffle Xu 	return 0;
309c8383054SJeffle Xu 
310527db1cbSBaokun Li err_put_file:
311d2d3eb37SBaokun Li 	fput(anon_file->file);
312d2d3eb37SBaokun Li 	anon_file->file = NULL;
313c8383054SJeffle Xu err_put_fd:
314d2d3eb37SBaokun Li 	put_unused_fd(anon_file->fd);
315d2d3eb37SBaokun Li 	anon_file->fd = ret;
316c8383054SJeffle Xu err_free_id:
317c8383054SJeffle Xu 	xa_erase(&cache->ondemand_ids, object_id);
318c8383054SJeffle Xu err:
319527db1cbSBaokun Li 	spin_lock(&object->ondemand->lock);
320527db1cbSBaokun Li 	/* Avoid marking an opened object as closed. */
321527db1cbSBaokun Li 	if (object->ondemand->ondemand_id <= 0)
322527db1cbSBaokun Li 		cachefiles_ondemand_set_object_close(object);
323527db1cbSBaokun Li 	spin_unlock(&object->ondemand->lock);
324c8383054SJeffle Xu 	cachefiles_put_object(object, cachefiles_obj_put_ondemand_fd);
325c8383054SJeffle Xu 	return ret;
326c8383054SJeffle Xu }
327c8383054SJeffle Xu 
328f740fd94SJia Zhu static void ondemand_object_worker(struct work_struct *work)
329f740fd94SJia Zhu {
330f740fd94SJia Zhu 	struct cachefiles_ondemand_info *info =
331f740fd94SJia Zhu 		container_of(work, struct cachefiles_ondemand_info, ondemand_work);
332f740fd94SJia Zhu 
333f740fd94SJia Zhu 	cachefiles_ondemand_init_object(info->object);
334f740fd94SJia Zhu }
335f740fd94SJia Zhu 
336f740fd94SJia Zhu /*
337f740fd94SJia Zhu  * If there are any inflight or subsequent READ requests on the
338f740fd94SJia Zhu  * closed object, reopen it.
339f740fd94SJia Zhu  * Skip read requests whose related object is reopening.
340f740fd94SJia Zhu  */
341f740fd94SJia Zhu static struct cachefiles_req *cachefiles_ondemand_select_req(struct xa_state *xas,
342f740fd94SJia Zhu 							      unsigned long xa_max)
343f740fd94SJia Zhu {
344f740fd94SJia Zhu 	struct cachefiles_req *req;
345f740fd94SJia Zhu 	struct cachefiles_object *object;
346f740fd94SJia Zhu 	struct cachefiles_ondemand_info *info;
347f740fd94SJia Zhu 
348f740fd94SJia Zhu 	xas_for_each_marked(xas, req, xa_max, CACHEFILES_REQ_NEW) {
349f740fd94SJia Zhu 		if (req->msg.opcode != CACHEFILES_OP_READ)
350f740fd94SJia Zhu 			return req;
351f740fd94SJia Zhu 		object = req->object;
352f740fd94SJia Zhu 		info = object->ondemand;
353f740fd94SJia Zhu 		if (cachefiles_ondemand_object_is_close(object)) {
354f740fd94SJia Zhu 			cachefiles_ondemand_set_object_reopening(object);
355f740fd94SJia Zhu 			queue_work(fscache_wq, &info->ondemand_work);
356f740fd94SJia Zhu 			continue;
357f740fd94SJia Zhu 		}
358f740fd94SJia Zhu 		if (cachefiles_ondemand_object_is_reopening(object))
359f740fd94SJia Zhu 			continue;
360f740fd94SJia Zhu 		return req;
361f740fd94SJia Zhu 	}
362f740fd94SJia Zhu 	return NULL;
363f740fd94SJia Zhu }
364f740fd94SJia Zhu 
365c8383054SJeffle Xu ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache,
366c8383054SJeffle Xu 					char __user *_buffer, size_t buflen)
367c8383054SJeffle Xu {
368c8383054SJeffle Xu 	struct cachefiles_req *req;
369c8383054SJeffle Xu 	struct cachefiles_msg *msg;
370c8383054SJeffle Xu 	size_t n;
371c8383054SJeffle Xu 	int ret = 0;
372d2d3eb37SBaokun Li 	struct ondemand_anon_file anon_file;
3731122f400SXin Yin 	XA_STATE(xas, &cache->reqs, cache->req_id_next);
374c8383054SJeffle Xu 
375f740fd94SJia Zhu 	xa_lock(&cache->reqs);
376c8383054SJeffle Xu 	/*
3771122f400SXin Yin 	 * Cyclically search for a request that has not ever been processed,
3781122f400SXin Yin 	 * to prevent requests from being processed repeatedly, and make
3791122f400SXin Yin 	 * request distribution fair.
380c8383054SJeffle Xu 	 */
381f740fd94SJia Zhu 	req = cachefiles_ondemand_select_req(&xas, ULONG_MAX);
3821122f400SXin Yin 	if (!req && cache->req_id_next > 0) {
3831122f400SXin Yin 		xas_set(&xas, 0);
384f740fd94SJia Zhu 		req = cachefiles_ondemand_select_req(&xas, cache->req_id_next - 1);
3851122f400SXin Yin 	}
386c8383054SJeffle Xu 	if (!req) {
387c8383054SJeffle Xu 		xa_unlock(&cache->reqs);
388c8383054SJeffle Xu 		return 0;
389c8383054SJeffle Xu 	}
390c8383054SJeffle Xu 
391c8383054SJeffle Xu 	msg = &req->msg;
392c8383054SJeffle Xu 	n = msg->len;
393c8383054SJeffle Xu 
394c8383054SJeffle Xu 	if (n > buflen) {
395c8383054SJeffle Xu 		xa_unlock(&cache->reqs);
396c8383054SJeffle Xu 		return -EMSGSIZE;
397c8383054SJeffle Xu 	}
398c8383054SJeffle Xu 
399c8383054SJeffle Xu 	xas_clear_mark(&xas, CACHEFILES_REQ_NEW);
4001122f400SXin Yin 	cache->req_id_next = xas.xa_index + 1;
401a6de8276SBaokun Li 	refcount_inc(&req->ref);
4023958679cSBaokun Li 	cachefiles_grab_object(req->object, cachefiles_obj_get_read_req);
403c8383054SJeffle Xu 	xa_unlock(&cache->reqs);
404c8383054SJeffle Xu 
405c8383054SJeffle Xu 	if (msg->opcode == CACHEFILES_OP_OPEN) {
406d2d3eb37SBaokun Li 		ret = cachefiles_ondemand_get_fd(req, &anon_file);
407527db1cbSBaokun Li 		if (ret)
4081d95e501SBaokun Li 			goto out;
409c8383054SJeffle Xu 	}
410f740fd94SJia Zhu 
4111d95e501SBaokun Li 	msg->msg_id = xas.xa_index;
412f740fd94SJia Zhu 	msg->object_id = req->object->ondemand->ondemand_id;
413c8383054SJeffle Xu 
414d2d3eb37SBaokun Li 	if (copy_to_user(_buffer, msg, n) != 0)
415c8383054SJeffle Xu 		ret = -EFAULT;
416d2d3eb37SBaokun Li 
417d2d3eb37SBaokun Li 	if (msg->opcode == CACHEFILES_OP_OPEN) {
418d2d3eb37SBaokun Li 		if (ret < 0) {
419d2d3eb37SBaokun Li 			fput(anon_file.file);
420d2d3eb37SBaokun Li 			put_unused_fd(anon_file.fd);
421d2d3eb37SBaokun Li 			goto out;
422d2d3eb37SBaokun Li 		}
423d2d3eb37SBaokun Li 		fd_install(anon_file.fd, anon_file.file);
4241d95e501SBaokun Li 	}
4251d95e501SBaokun Li out:
4263958679cSBaokun Li 	cachefiles_put_object(req->object, cachefiles_obj_put_read_req);
4271d95e501SBaokun Li 	/* Remove error request and CLOSE request has no reply */
4281d95e501SBaokun Li 	if (ret || msg->opcode == CACHEFILES_OP_CLOSE) {
429a6de8276SBaokun Li 		xas_reset(&xas);
430a6de8276SBaokun Li 		xas_lock(&xas);
431a6de8276SBaokun Li 		if (xas_load(&xas) == req) {
432c8383054SJeffle Xu 			req->error = ret;
433c8383054SJeffle Xu 			complete(&req->done);
434a6de8276SBaokun Li 			xas_store(&xas, NULL);
435a6de8276SBaokun Li 		}
436a6de8276SBaokun Li 		xas_unlock(&xas);
4371d95e501SBaokun Li 	}
438a6de8276SBaokun Li 	cachefiles_req_put(req);
4391d95e501SBaokun Li 	return ret ? ret : n;
440c8383054SJeffle Xu }
441c8383054SJeffle Xu 
442c8383054SJeffle Xu typedef int (*init_req_fn)(struct cachefiles_req *req, void *private);
443c8383054SJeffle Xu 
444c8383054SJeffle Xu static int cachefiles_ondemand_send_req(struct cachefiles_object *object,
445c8383054SJeffle Xu 					enum cachefiles_opcode opcode,
446c8383054SJeffle Xu 					size_t data_len,
447c8383054SJeffle Xu 					init_req_fn init_req,
448c8383054SJeffle Xu 					void *private)
449c8383054SJeffle Xu {
450c8383054SJeffle Xu 	struct cachefiles_cache *cache = object->volume->cache;
451f740fd94SJia Zhu 	struct cachefiles_req *req = NULL;
452c8383054SJeffle Xu 	XA_STATE(xas, &cache->reqs, 0);
453c8383054SJeffle Xu 	int ret;
454c8383054SJeffle Xu 
455c8383054SJeffle Xu 	if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags))
456c8383054SJeffle Xu 		return 0;
457c8383054SJeffle Xu 
458f740fd94SJia Zhu 	if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
459f740fd94SJia Zhu 		ret = -EIO;
460f740fd94SJia Zhu 		goto out;
461f740fd94SJia Zhu 	}
462c8383054SJeffle Xu 
463c8383054SJeffle Xu 	req = kzalloc(sizeof(*req) + data_len, GFP_KERNEL);
464f740fd94SJia Zhu 	if (!req) {
465f740fd94SJia Zhu 		ret = -ENOMEM;
466f740fd94SJia Zhu 		goto out;
467f740fd94SJia Zhu 	}
468c8383054SJeffle Xu 
469a6de8276SBaokun Li 	refcount_set(&req->ref, 1);
470c8383054SJeffle Xu 	req->object = object;
471c8383054SJeffle Xu 	init_completion(&req->done);
472c8383054SJeffle Xu 	req->msg.opcode = opcode;
473c8383054SJeffle Xu 	req->msg.len = sizeof(struct cachefiles_msg) + data_len;
474c8383054SJeffle Xu 
475c8383054SJeffle Xu 	ret = init_req(req, private);
476c8383054SJeffle Xu 	if (ret)
477c8383054SJeffle Xu 		goto out;
478c8383054SJeffle Xu 
479c8383054SJeffle Xu 	do {
480c8383054SJeffle Xu 		/*
481c8383054SJeffle Xu 		 * Stop enqueuing the request when daemon is dying. The
482c8383054SJeffle Xu 		 * following two operations need to be atomic as a whole.
483c8383054SJeffle Xu 		 *   1) check cache state, and
484c8383054SJeffle Xu 		 *   2) enqueue request if cache is alive.
485c8383054SJeffle Xu 		 * Otherwise the request may be enqueued after xarray has been
486c8383054SJeffle Xu 		 * flushed, leaving the orphan request never being completed.
487c8383054SJeffle Xu 		 *
488c8383054SJeffle Xu 		 * CPU 1			CPU 2
489c8383054SJeffle Xu 		 * =====			=====
490c8383054SJeffle Xu 		 *				test CACHEFILES_DEAD bit
491c8383054SJeffle Xu 		 * set CACHEFILES_DEAD bit
492c8383054SJeffle Xu 		 * flush requests in the xarray
493c8383054SJeffle Xu 		 *				enqueue the request
494c8383054SJeffle Xu 		 */
495c8383054SJeffle Xu 		xas_lock(&xas);
496c8383054SJeffle Xu 
497c8383054SJeffle Xu 		if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
498c8383054SJeffle Xu 			xas_unlock(&xas);
499c8383054SJeffle Xu 			ret = -EIO;
500c8383054SJeffle Xu 			goto out;
501c8383054SJeffle Xu 		}
502c8383054SJeffle Xu 
503c8383054SJeffle Xu 		/* coupled with the barrier in cachefiles_flush_reqs() */
504c8383054SJeffle Xu 		smp_mb();
505c8383054SJeffle Xu 
506f740fd94SJia Zhu 		if (opcode == CACHEFILES_OP_CLOSE &&
507955190e1SJia Zhu 			!cachefiles_ondemand_object_is_open(object)) {
50833d21f06SJia Zhu 			WARN_ON_ONCE(object->ondemand->ondemand_id == 0);
509324b954aSJeffle Xu 			xas_unlock(&xas);
510324b954aSJeffle Xu 			ret = -EIO;
511324b954aSJeffle Xu 			goto out;
512324b954aSJeffle Xu 		}
513324b954aSJeffle Xu 
514c8383054SJeffle Xu 		xas.xa_index = 0;
515c8383054SJeffle Xu 		xas_find_marked(&xas, UINT_MAX, XA_FREE_MARK);
516c8383054SJeffle Xu 		if (xas.xa_node == XAS_RESTART)
517c8383054SJeffle Xu 			xas_set_err(&xas, -EBUSY);
518c8383054SJeffle Xu 		xas_store(&xas, req);
519c8383054SJeffle Xu 		xas_clear_mark(&xas, XA_FREE_MARK);
520c8383054SJeffle Xu 		xas_set_mark(&xas, CACHEFILES_REQ_NEW);
521c8383054SJeffle Xu 		xas_unlock(&xas);
522c8383054SJeffle Xu 	} while (xas_nomem(&xas, GFP_KERNEL));
523c8383054SJeffle Xu 
524c8383054SJeffle Xu 	ret = xas_error(&xas);
525c8383054SJeffle Xu 	if (ret)
526c8383054SJeffle Xu 		goto out;
527c8383054SJeffle Xu 
528c8383054SJeffle Xu 	wake_up_all(&cache->daemon_pollwq);
529c8383054SJeffle Xu 	wait_for_completion(&req->done);
530c8383054SJeffle Xu 	ret = req->error;
531a6de8276SBaokun Li 	cachefiles_req_put(req);
532f740fd94SJia Zhu 	return ret;
533c8383054SJeffle Xu out:
534f740fd94SJia Zhu 	/* Reset the object to close state in error handling path.
535f740fd94SJia Zhu 	 * If error occurs after creating the anonymous fd,
536f740fd94SJia Zhu 	 * cachefiles_ondemand_fd_release() will set object to close.
537f740fd94SJia Zhu 	 */
538f740fd94SJia Zhu 	if (opcode == CACHEFILES_OP_OPEN)
539f740fd94SJia Zhu 		cachefiles_ondemand_set_object_close(object);
540c8383054SJeffle Xu 	kfree(req);
541c8383054SJeffle Xu 	return ret;
542c8383054SJeffle Xu }
543c8383054SJeffle Xu 
544c8383054SJeffle Xu static int cachefiles_ondemand_init_open_req(struct cachefiles_req *req,
545c8383054SJeffle Xu 					     void *private)
546c8383054SJeffle Xu {
547c8383054SJeffle Xu 	struct cachefiles_object *object = req->object;
548c8383054SJeffle Xu 	struct fscache_cookie *cookie = object->cookie;
549c8383054SJeffle Xu 	struct fscache_volume *volume = object->volume->vcookie;
550c8383054SJeffle Xu 	struct cachefiles_open *load = (void *)req->msg.data;
551c8383054SJeffle Xu 	size_t volume_key_size, cookie_key_size;
552c8383054SJeffle Xu 	void *volume_key, *cookie_key;
553c8383054SJeffle Xu 
554c8383054SJeffle Xu 	/*
555c8383054SJeffle Xu 	 * Volume key is a NUL-terminated string. key[0] stores strlen() of the
556c8383054SJeffle Xu 	 * string, followed by the content of the string (excluding '\0').
557c8383054SJeffle Xu 	 */
558c8383054SJeffle Xu 	volume_key_size = volume->key[0] + 1;
559c8383054SJeffle Xu 	volume_key = volume->key + 1;
560c8383054SJeffle Xu 
561c8383054SJeffle Xu 	/* Cookie key is binary data, which is netfs specific. */
562c8383054SJeffle Xu 	cookie_key_size = cookie->key_len;
563c8383054SJeffle Xu 	cookie_key = fscache_get_key(cookie);
564c8383054SJeffle Xu 
565c8383054SJeffle Xu 	if (!(object->cookie->advice & FSCACHE_ADV_WANT_CACHE_SIZE)) {
566c8383054SJeffle Xu 		pr_err("WANT_CACHE_SIZE is needed for on-demand mode\n");
567c8383054SJeffle Xu 		return -EINVAL;
568c8383054SJeffle Xu 	}
569c8383054SJeffle Xu 
570c8383054SJeffle Xu 	load->volume_key_size = volume_key_size;
571c8383054SJeffle Xu 	load->cookie_key_size = cookie_key_size;
572c8383054SJeffle Xu 	memcpy(load->data, volume_key, volume_key_size);
573c8383054SJeffle Xu 	memcpy(load->data + volume_key_size, cookie_key, cookie_key_size);
574c8383054SJeffle Xu 
575c8383054SJeffle Xu 	return 0;
576c8383054SJeffle Xu }
577c8383054SJeffle Xu 
578324b954aSJeffle Xu static int cachefiles_ondemand_init_close_req(struct cachefiles_req *req,
579324b954aSJeffle Xu 					      void *private)
580324b954aSJeffle Xu {
581324b954aSJeffle Xu 	struct cachefiles_object *object = req->object;
582324b954aSJeffle Xu 
583955190e1SJia Zhu 	if (!cachefiles_ondemand_object_is_open(object))
584324b954aSJeffle Xu 		return -ENOENT;
585324b954aSJeffle Xu 
5861519670eSJeffle Xu 	trace_cachefiles_ondemand_close(object, &req->msg);
587324b954aSJeffle Xu 	return 0;
588324b954aSJeffle Xu }
589324b954aSJeffle Xu 
5909032b6e8SJeffle Xu struct cachefiles_read_ctx {
5919032b6e8SJeffle Xu 	loff_t off;
5929032b6e8SJeffle Xu 	size_t len;
5939032b6e8SJeffle Xu };
5949032b6e8SJeffle Xu 
5959032b6e8SJeffle Xu static int cachefiles_ondemand_init_read_req(struct cachefiles_req *req,
5969032b6e8SJeffle Xu 					     void *private)
5979032b6e8SJeffle Xu {
5989032b6e8SJeffle Xu 	struct cachefiles_object *object = req->object;
5999032b6e8SJeffle Xu 	struct cachefiles_read *load = (void *)req->msg.data;
6009032b6e8SJeffle Xu 	struct cachefiles_read_ctx *read_ctx = private;
6019032b6e8SJeffle Xu 
6029032b6e8SJeffle Xu 	load->off = read_ctx->off;
6039032b6e8SJeffle Xu 	load->len = read_ctx->len;
6041519670eSJeffle Xu 	trace_cachefiles_ondemand_read(object, &req->msg, load);
6059032b6e8SJeffle Xu 	return 0;
6069032b6e8SJeffle Xu }
6079032b6e8SJeffle Xu 
608c8383054SJeffle Xu int cachefiles_ondemand_init_object(struct cachefiles_object *object)
609c8383054SJeffle Xu {
610c8383054SJeffle Xu 	struct fscache_cookie *cookie = object->cookie;
611c8383054SJeffle Xu 	struct fscache_volume *volume = object->volume->vcookie;
612c8383054SJeffle Xu 	size_t volume_key_size, cookie_key_size, data_len;
613c8383054SJeffle Xu 
614*8a73c08eSDavid Howells 	if (!object->ondemand)
615*8a73c08eSDavid Howells 		return 0;
616*8a73c08eSDavid Howells 
617c8383054SJeffle Xu 	/*
618c8383054SJeffle Xu 	 * CacheFiles will firstly check the cache file under the root cache
619c8383054SJeffle Xu 	 * directory. If the coherency check failed, it will fallback to
620c8383054SJeffle Xu 	 * creating a new tmpfile as the cache file. Reuse the previously
621c8383054SJeffle Xu 	 * allocated object ID if any.
622c8383054SJeffle Xu 	 */
623955190e1SJia Zhu 	if (cachefiles_ondemand_object_is_open(object))
624c8383054SJeffle Xu 		return 0;
625c8383054SJeffle Xu 
626c8383054SJeffle Xu 	volume_key_size = volume->key[0] + 1;
627c8383054SJeffle Xu 	cookie_key_size = cookie->key_len;
628c8383054SJeffle Xu 	data_len = sizeof(struct cachefiles_open) +
629c8383054SJeffle Xu 		   volume_key_size + cookie_key_size;
630c8383054SJeffle Xu 
631c8383054SJeffle Xu 	return cachefiles_ondemand_send_req(object, CACHEFILES_OP_OPEN,
632c8383054SJeffle Xu 			data_len, cachefiles_ondemand_init_open_req, NULL);
633c8383054SJeffle Xu }
634324b954aSJeffle Xu 
635324b954aSJeffle Xu void cachefiles_ondemand_clean_object(struct cachefiles_object *object)
636324b954aSJeffle Xu {
637324b954aSJeffle Xu 	cachefiles_ondemand_send_req(object, CACHEFILES_OP_CLOSE, 0,
638324b954aSJeffle Xu 			cachefiles_ondemand_init_close_req, NULL);
639324b954aSJeffle Xu }
6409032b6e8SJeffle Xu 
64133d21f06SJia Zhu int cachefiles_ondemand_init_obj_info(struct cachefiles_object *object,
64233d21f06SJia Zhu 				struct cachefiles_volume *volume)
64333d21f06SJia Zhu {
64433d21f06SJia Zhu 	if (!cachefiles_in_ondemand_mode(volume->cache))
64533d21f06SJia Zhu 		return 0;
64633d21f06SJia Zhu 
64733d21f06SJia Zhu 	object->ondemand = kzalloc(sizeof(struct cachefiles_ondemand_info),
64833d21f06SJia Zhu 					GFP_KERNEL);
64933d21f06SJia Zhu 	if (!object->ondemand)
65033d21f06SJia Zhu 		return -ENOMEM;
65133d21f06SJia Zhu 
65233d21f06SJia Zhu 	object->ondemand->object = object;
653e564e48cSBaokun Li 	spin_lock_init(&object->ondemand->lock);
654f740fd94SJia Zhu 	INIT_WORK(&object->ondemand->ondemand_work, ondemand_object_worker);
65533d21f06SJia Zhu 	return 0;
65633d21f06SJia Zhu }
65733d21f06SJia Zhu 
65833d21f06SJia Zhu void cachefiles_ondemand_deinit_obj_info(struct cachefiles_object *object)
65933d21f06SJia Zhu {
66033d21f06SJia Zhu 	kfree(object->ondemand);
66133d21f06SJia Zhu 	object->ondemand = NULL;
66233d21f06SJia Zhu }
66333d21f06SJia Zhu 
6649032b6e8SJeffle Xu int cachefiles_ondemand_read(struct cachefiles_object *object,
6659032b6e8SJeffle Xu 			     loff_t pos, size_t len)
6669032b6e8SJeffle Xu {
6679032b6e8SJeffle Xu 	struct cachefiles_read_ctx read_ctx = {pos, len};
6689032b6e8SJeffle Xu 
6699032b6e8SJeffle Xu 	return cachefiles_ondemand_send_req(object, CACHEFILES_OP_READ,
6709032b6e8SJeffle Xu 			sizeof(struct cachefiles_read),
6719032b6e8SJeffle Xu 			cachefiles_ondemand_init_read_req, &read_ctx);
6729032b6e8SJeffle Xu }
673