Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2#include <linux/fdtable.h>
  3#include <linux/anon_inodes.h>
  4#include <linux/uio.h>
  5#include "internal.h"
  6
  7static int cachefiles_ondemand_fd_release(struct inode *inode,
  8					  struct file *file)
  9{
 10	struct cachefiles_object *object = file->private_data;
 11	struct cachefiles_cache *cache = object->volume->cache;
 12	struct cachefiles_ondemand_info *info = object->ondemand;
 13	int object_id = info->ondemand_id;
 14	struct cachefiles_req *req;
 15	XA_STATE(xas, &cache->reqs, 0);
 16
 17	xa_lock(&cache->reqs);
 18	info->ondemand_id = CACHEFILES_ONDEMAND_ID_CLOSED;
 19	cachefiles_ondemand_set_object_close(object);
 20
 21	/* Only flush CACHEFILES_REQ_NEW marked req to avoid race with daemon_read */
 22	xas_for_each_marked(&xas, req, ULONG_MAX, CACHEFILES_REQ_NEW) {
 
 
 
 23		if (req->msg.object_id == object_id &&
 24		    req->msg.opcode == CACHEFILES_OP_CLOSE) {
 
 25			complete(&req->done);
 26			xas_store(&xas, NULL);
 27		}
 28	}
 29	xa_unlock(&cache->reqs);
 30
 31	xa_erase(&cache->ondemand_ids, object_id);
 32	trace_cachefiles_ondemand_fd_release(object, object_id);
 33	cachefiles_put_object(object, cachefiles_obj_put_ondemand_fd);
 34	cachefiles_put_unbind_pincount(cache);
 35	return 0;
 36}
 37
 38static ssize_t cachefiles_ondemand_fd_write_iter(struct kiocb *kiocb,
 39						 struct iov_iter *iter)
 40{
 41	struct cachefiles_object *object = kiocb->ki_filp->private_data;
 42	struct cachefiles_cache *cache = object->volume->cache;
 43	struct file *file = object->file;
 44	size_t len = iter->count;
 45	loff_t pos = kiocb->ki_pos;
 46	const struct cred *saved_cred;
 47	int ret;
 48
 49	if (!file)
 50		return -ENOBUFS;
 51
 52	cachefiles_begin_secure(cache, &saved_cred);
 53	ret = __cachefiles_prepare_write(object, file, &pos, &len, len, true);
 54	cachefiles_end_secure(cache, saved_cred);
 55	if (ret < 0)
 56		return ret;
 57
 58	trace_cachefiles_ondemand_fd_write(object, file_inode(file), pos, len);
 59	ret = __cachefiles_write(object, file, pos, iter, NULL, NULL);
 60	if (!ret)
 61		ret = len;
 62
 63	return ret;
 64}
 65
 66static loff_t cachefiles_ondemand_fd_llseek(struct file *filp, loff_t pos,
 67					    int whence)
 68{
 69	struct cachefiles_object *object = filp->private_data;
 70	struct file *file = object->file;
 71
 72	if (!file)
 73		return -ENOBUFS;
 74
 75	return vfs_llseek(file, pos, whence);
 76}
 77
 78static long cachefiles_ondemand_fd_ioctl(struct file *filp, unsigned int ioctl,
 79					 unsigned long arg)
 80{
 81	struct cachefiles_object *object = filp->private_data;
 82	struct cachefiles_cache *cache = object->volume->cache;
 83	struct cachefiles_req *req;
 84	unsigned long id;
 85
 86	if (ioctl != CACHEFILES_IOC_READ_COMPLETE)
 87		return -EINVAL;
 88
 89	if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags))
 90		return -EOPNOTSUPP;
 91
 92	id = arg;
 93	req = xa_erase(&cache->reqs, id);
 94	if (!req)
 95		return -EINVAL;
 96
 97	trace_cachefiles_ondemand_cread(object, id);
 98	complete(&req->done);
 99	return 0;
100}
101
102static const struct file_operations cachefiles_ondemand_fd_fops = {
103	.owner		= THIS_MODULE,
104	.release	= cachefiles_ondemand_fd_release,
105	.write_iter	= cachefiles_ondemand_fd_write_iter,
106	.llseek		= cachefiles_ondemand_fd_llseek,
107	.unlocked_ioctl	= cachefiles_ondemand_fd_ioctl,
108};
109
110/*
111 * OPEN request Completion (copen)
112 * - command: "copen <id>,<cache_size>"
113 *   <cache_size> indicates the object size if >=0, error code if negative
114 */
115int cachefiles_ondemand_copen(struct cachefiles_cache *cache, char *args)
116{
117	struct cachefiles_req *req;
118	struct fscache_cookie *cookie;
119	char *pid, *psize;
120	unsigned long id;
121	long size;
122	int ret;
123
124	if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags))
125		return -EOPNOTSUPP;
126
127	if (!*args) {
128		pr_err("Empty id specified\n");
129		return -EINVAL;
130	}
131
132	pid = args;
133	psize = strchr(args, ',');
134	if (!psize) {
135		pr_err("Cache size is not specified\n");
136		return -EINVAL;
137	}
138
139	*psize = 0;
140	psize++;
141
142	ret = kstrtoul(pid, 0, &id);
143	if (ret)
144		return ret;
145
146	req = xa_erase(&cache->reqs, id);
147	if (!req)
148		return -EINVAL;
149
150	/* fail OPEN request if copen format is invalid */
151	ret = kstrtol(psize, 0, &size);
152	if (ret) {
153		req->error = ret;
154		goto out;
155	}
156
157	/* fail OPEN request if daemon reports an error */
158	if (size < 0) {
159		if (!IS_ERR_VALUE(size)) {
160			req->error = -EINVAL;
161			ret = -EINVAL;
162		} else {
163			req->error = size;
164			ret = 0;
165		}
166		goto out;
167	}
168
169	cookie = req->object->cookie;
170	cookie->object_size = size;
171	if (size)
172		clear_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags);
173	else
174		set_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags);
175	trace_cachefiles_ondemand_copen(req->object, id, size);
176
177	cachefiles_ondemand_set_object_open(req->object);
178	wake_up_all(&cache->daemon_pollwq);
179
180out:
181	complete(&req->done);
182	return ret;
183}
184
185int cachefiles_ondemand_restore(struct cachefiles_cache *cache, char *args)
186{
187	struct cachefiles_req *req;
188
189	XA_STATE(xas, &cache->reqs, 0);
190
191	if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags))
192		return -EOPNOTSUPP;
193
194	/*
195	 * Reset the requests to CACHEFILES_REQ_NEW state, so that the
196	 * requests have been processed halfway before the crash of the
197	 * user daemon could be reprocessed after the recovery.
198	 */
199	xas_lock(&xas);
200	xas_for_each(&xas, req, ULONG_MAX)
201		xas_set_mark(&xas, CACHEFILES_REQ_NEW);
202	xas_unlock(&xas);
203
204	wake_up_all(&cache->daemon_pollwq);
205	return 0;
206}
207
208static int cachefiles_ondemand_get_fd(struct cachefiles_req *req)
209{
210	struct cachefiles_object *object;
211	struct cachefiles_cache *cache;
212	struct cachefiles_open *load;
213	struct file *file;
214	u32 object_id;
215	int ret, fd;
216
217	object = cachefiles_grab_object(req->object,
218			cachefiles_obj_get_ondemand_fd);
219	cache = object->volume->cache;
220
221	ret = xa_alloc_cyclic(&cache->ondemand_ids, &object_id, NULL,
222			      XA_LIMIT(1, INT_MAX),
223			      &cache->ondemand_id_next, GFP_KERNEL);
224	if (ret < 0)
225		goto err;
226
227	fd = get_unused_fd_flags(O_WRONLY);
228	if (fd < 0) {
229		ret = fd;
230		goto err_free_id;
231	}
232
233	file = anon_inode_getfile("[cachefiles]", &cachefiles_ondemand_fd_fops,
234				  object, O_WRONLY);
235	if (IS_ERR(file)) {
236		ret = PTR_ERR(file);
237		goto err_put_fd;
238	}
239
240	file->f_mode |= FMODE_PWRITE | FMODE_LSEEK;
241	fd_install(fd, file);
242
243	load = (void *)req->msg.data;
244	load->fd = fd;
245	object->ondemand->ondemand_id = object_id;
 
246
247	cachefiles_get_unbind_pincount(cache);
248	trace_cachefiles_ondemand_open(object, &req->msg, load);
249	return 0;
250
251err_put_fd:
252	put_unused_fd(fd);
253err_free_id:
254	xa_erase(&cache->ondemand_ids, object_id);
255err:
256	cachefiles_put_object(object, cachefiles_obj_put_ondemand_fd);
257	return ret;
258}
259
260static void ondemand_object_worker(struct work_struct *work)
261{
262	struct cachefiles_ondemand_info *info =
263		container_of(work, struct cachefiles_ondemand_info, ondemand_work);
264
265	cachefiles_ondemand_init_object(info->object);
266}
267
268/*
269 * If there are any inflight or subsequent READ requests on the
270 * closed object, reopen it.
271 * Skip read requests whose related object is reopening.
272 */
273static struct cachefiles_req *cachefiles_ondemand_select_req(struct xa_state *xas,
274							      unsigned long xa_max)
275{
276	struct cachefiles_req *req;
277	struct cachefiles_object *object;
278	struct cachefiles_ondemand_info *info;
279
280	xas_for_each_marked(xas, req, xa_max, CACHEFILES_REQ_NEW) {
281		if (req->msg.opcode != CACHEFILES_OP_READ)
282			return req;
283		object = req->object;
284		info = object->ondemand;
285		if (cachefiles_ondemand_object_is_close(object)) {
286			cachefiles_ondemand_set_object_reopening(object);
287			queue_work(fscache_wq, &info->ondemand_work);
288			continue;
289		}
290		if (cachefiles_ondemand_object_is_reopening(object))
291			continue;
292		return req;
293	}
294	return NULL;
295}
296
297ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache,
298					char __user *_buffer, size_t buflen)
299{
300	struct cachefiles_req *req;
301	struct cachefiles_msg *msg;
302	unsigned long id = 0;
303	size_t n;
304	int ret = 0;
305	XA_STATE(xas, &cache->reqs, cache->req_id_next);
306
307	xa_lock(&cache->reqs);
308	/*
309	 * Cyclically search for a request that has not ever been processed,
310	 * to prevent requests from being processed repeatedly, and make
311	 * request distribution fair.
312	 */
313	req = cachefiles_ondemand_select_req(&xas, ULONG_MAX);
 
314	if (!req && cache->req_id_next > 0) {
315		xas_set(&xas, 0);
316		req = cachefiles_ondemand_select_req(&xas, cache->req_id_next - 1);
317	}
318	if (!req) {
319		xa_unlock(&cache->reqs);
320		return 0;
321	}
322
323	msg = &req->msg;
324	n = msg->len;
325
326	if (n > buflen) {
327		xa_unlock(&cache->reqs);
328		return -EMSGSIZE;
329	}
330
331	xas_clear_mark(&xas, CACHEFILES_REQ_NEW);
332	cache->req_id_next = xas.xa_index + 1;
333	xa_unlock(&cache->reqs);
334
335	id = xas.xa_index;
 
336
337	if (msg->opcode == CACHEFILES_OP_OPEN) {
338		ret = cachefiles_ondemand_get_fd(req);
339		if (ret) {
340			cachefiles_ondemand_set_object_close(req->object);
341			goto error;
342		}
343	}
344
345	msg->msg_id = id;
346	msg->object_id = req->object->ondemand->ondemand_id;
347
348	if (copy_to_user(_buffer, msg, n) != 0) {
349		ret = -EFAULT;
350		goto err_put_fd;
351	}
352
353	/* CLOSE request has no reply */
354	if (msg->opcode == CACHEFILES_OP_CLOSE) {
355		xa_erase(&cache->reqs, id);
356		complete(&req->done);
357	}
358
359	return n;
360
361err_put_fd:
362	if (msg->opcode == CACHEFILES_OP_OPEN)
363		close_fd(((struct cachefiles_open *)msg->data)->fd);
364error:
365	xa_erase(&cache->reqs, id);
366	req->error = ret;
367	complete(&req->done);
368	return ret;
369}
370
371typedef int (*init_req_fn)(struct cachefiles_req *req, void *private);
372
373static int cachefiles_ondemand_send_req(struct cachefiles_object *object,
374					enum cachefiles_opcode opcode,
375					size_t data_len,
376					init_req_fn init_req,
377					void *private)
378{
379	struct cachefiles_cache *cache = object->volume->cache;
380	struct cachefiles_req *req = NULL;
381	XA_STATE(xas, &cache->reqs, 0);
382	int ret;
383
384	if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags))
385		return 0;
386
387	if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
388		ret = -EIO;
389		goto out;
390	}
391
392	req = kzalloc(sizeof(*req) + data_len, GFP_KERNEL);
393	if (!req) {
394		ret = -ENOMEM;
395		goto out;
396	}
397
398	req->object = object;
399	init_completion(&req->done);
400	req->msg.opcode = opcode;
401	req->msg.len = sizeof(struct cachefiles_msg) + data_len;
402
403	ret = init_req(req, private);
404	if (ret)
405		goto out;
406
407	do {
408		/*
409		 * Stop enqueuing the request when daemon is dying. The
410		 * following two operations need to be atomic as a whole.
411		 *   1) check cache state, and
412		 *   2) enqueue request if cache is alive.
413		 * Otherwise the request may be enqueued after xarray has been
414		 * flushed, leaving the orphan request never being completed.
415		 *
416		 * CPU 1			CPU 2
417		 * =====			=====
418		 *				test CACHEFILES_DEAD bit
419		 * set CACHEFILES_DEAD bit
420		 * flush requests in the xarray
421		 *				enqueue the request
422		 */
423		xas_lock(&xas);
424
425		if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
426			xas_unlock(&xas);
427			ret = -EIO;
428			goto out;
429		}
430
431		/* coupled with the barrier in cachefiles_flush_reqs() */
432		smp_mb();
433
434		if (opcode == CACHEFILES_OP_CLOSE &&
435			!cachefiles_ondemand_object_is_open(object)) {
436			WARN_ON_ONCE(object->ondemand->ondemand_id == 0);
437			xas_unlock(&xas);
438			ret = -EIO;
439			goto out;
440		}
441
442		xas.xa_index = 0;
443		xas_find_marked(&xas, UINT_MAX, XA_FREE_MARK);
444		if (xas.xa_node == XAS_RESTART)
445			xas_set_err(&xas, -EBUSY);
446		xas_store(&xas, req);
447		xas_clear_mark(&xas, XA_FREE_MARK);
448		xas_set_mark(&xas, CACHEFILES_REQ_NEW);
449		xas_unlock(&xas);
450	} while (xas_nomem(&xas, GFP_KERNEL));
451
452	ret = xas_error(&xas);
453	if (ret)
454		goto out;
455
456	wake_up_all(&cache->daemon_pollwq);
457	wait_for_completion(&req->done);
458	ret = req->error;
459	kfree(req);
460	return ret;
461out:
462	/* Reset the object to close state in error handling path.
463	 * If error occurs after creating the anonymous fd,
464	 * cachefiles_ondemand_fd_release() will set object to close.
465	 */
466	if (opcode == CACHEFILES_OP_OPEN)
467		cachefiles_ondemand_set_object_close(object);
468	kfree(req);
469	return ret;
470}
471
472static int cachefiles_ondemand_init_open_req(struct cachefiles_req *req,
473					     void *private)
474{
475	struct cachefiles_object *object = req->object;
476	struct fscache_cookie *cookie = object->cookie;
477	struct fscache_volume *volume = object->volume->vcookie;
478	struct cachefiles_open *load = (void *)req->msg.data;
479	size_t volume_key_size, cookie_key_size;
480	void *volume_key, *cookie_key;
481
482	/*
483	 * Volume key is a NUL-terminated string. key[0] stores strlen() of the
484	 * string, followed by the content of the string (excluding '\0').
485	 */
486	volume_key_size = volume->key[0] + 1;
487	volume_key = volume->key + 1;
488
489	/* Cookie key is binary data, which is netfs specific. */
490	cookie_key_size = cookie->key_len;
491	cookie_key = fscache_get_key(cookie);
492
493	if (!(object->cookie->advice & FSCACHE_ADV_WANT_CACHE_SIZE)) {
494		pr_err("WANT_CACHE_SIZE is needed for on-demand mode\n");
495		return -EINVAL;
496	}
497
498	load->volume_key_size = volume_key_size;
499	load->cookie_key_size = cookie_key_size;
500	memcpy(load->data, volume_key, volume_key_size);
501	memcpy(load->data + volume_key_size, cookie_key, cookie_key_size);
502
503	return 0;
504}
505
506static int cachefiles_ondemand_init_close_req(struct cachefiles_req *req,
507					      void *private)
508{
509	struct cachefiles_object *object = req->object;
 
510
511	if (!cachefiles_ondemand_object_is_open(object))
 
 
 
 
 
 
512		return -ENOENT;
513
 
514	trace_cachefiles_ondemand_close(object, &req->msg);
515	return 0;
516}
517
518struct cachefiles_read_ctx {
519	loff_t off;
520	size_t len;
521};
522
523static int cachefiles_ondemand_init_read_req(struct cachefiles_req *req,
524					     void *private)
525{
526	struct cachefiles_object *object = req->object;
527	struct cachefiles_read *load = (void *)req->msg.data;
528	struct cachefiles_read_ctx *read_ctx = private;
 
529
 
 
 
 
 
 
 
 
530	load->off = read_ctx->off;
531	load->len = read_ctx->len;
532	trace_cachefiles_ondemand_read(object, &req->msg, load);
533	return 0;
534}
535
536int cachefiles_ondemand_init_object(struct cachefiles_object *object)
537{
538	struct fscache_cookie *cookie = object->cookie;
539	struct fscache_volume *volume = object->volume->vcookie;
540	size_t volume_key_size, cookie_key_size, data_len;
541
542	if (!object->ondemand)
543		return 0;
544
545	/*
546	 * CacheFiles will firstly check the cache file under the root cache
547	 * directory. If the coherency check failed, it will fallback to
548	 * creating a new tmpfile as the cache file. Reuse the previously
549	 * allocated object ID if any.
550	 */
551	if (cachefiles_ondemand_object_is_open(object))
552		return 0;
553
554	volume_key_size = volume->key[0] + 1;
555	cookie_key_size = cookie->key_len;
556	data_len = sizeof(struct cachefiles_open) +
557		   volume_key_size + cookie_key_size;
558
559	return cachefiles_ondemand_send_req(object, CACHEFILES_OP_OPEN,
560			data_len, cachefiles_ondemand_init_open_req, NULL);
561}
562
563void cachefiles_ondemand_clean_object(struct cachefiles_object *object)
564{
565	cachefiles_ondemand_send_req(object, CACHEFILES_OP_CLOSE, 0,
566			cachefiles_ondemand_init_close_req, NULL);
567}
568
569int cachefiles_ondemand_init_obj_info(struct cachefiles_object *object,
570				struct cachefiles_volume *volume)
571{
572	if (!cachefiles_in_ondemand_mode(volume->cache))
573		return 0;
574
575	object->ondemand = kzalloc(sizeof(struct cachefiles_ondemand_info),
576					GFP_KERNEL);
577	if (!object->ondemand)
578		return -ENOMEM;
579
580	object->ondemand->object = object;
581	INIT_WORK(&object->ondemand->ondemand_work, ondemand_object_worker);
582	return 0;
583}
584
585void cachefiles_ondemand_deinit_obj_info(struct cachefiles_object *object)
586{
587	kfree(object->ondemand);
588	object->ondemand = NULL;
589}
590
591int cachefiles_ondemand_read(struct cachefiles_object *object,
592			     loff_t pos, size_t len)
593{
594	struct cachefiles_read_ctx read_ctx = {pos, len};
595
596	return cachefiles_ondemand_send_req(object, CACHEFILES_OP_READ,
597			sizeof(struct cachefiles_read),
598			cachefiles_ondemand_init_read_req, &read_ctx);
599}
v6.2
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2#include <linux/fdtable.h>
  3#include <linux/anon_inodes.h>
  4#include <linux/uio.h>
  5#include "internal.h"
  6
  7static int cachefiles_ondemand_fd_release(struct inode *inode,
  8					  struct file *file)
  9{
 10	struct cachefiles_object *object = file->private_data;
 11	struct cachefiles_cache *cache = object->volume->cache;
 12	int object_id = object->ondemand_id;
 
 13	struct cachefiles_req *req;
 14	XA_STATE(xas, &cache->reqs, 0);
 15
 16	xa_lock(&cache->reqs);
 17	object->ondemand_id = CACHEFILES_ONDEMAND_ID_CLOSED;
 
 18
 19	/*
 20	 * Flush all pending READ requests since their completion depends on
 21	 * anon_fd.
 22	 */
 23	xas_for_each(&xas, req, ULONG_MAX) {
 24		if (req->msg.object_id == object_id &&
 25		    req->msg.opcode == CACHEFILES_OP_READ) {
 26			req->error = -EIO;
 27			complete(&req->done);
 28			xas_store(&xas, NULL);
 29		}
 30	}
 31	xa_unlock(&cache->reqs);
 32
 33	xa_erase(&cache->ondemand_ids, object_id);
 34	trace_cachefiles_ondemand_fd_release(object, object_id);
 35	cachefiles_put_object(object, cachefiles_obj_put_ondemand_fd);
 36	cachefiles_put_unbind_pincount(cache);
 37	return 0;
 38}
 39
 40static ssize_t cachefiles_ondemand_fd_write_iter(struct kiocb *kiocb,
 41						 struct iov_iter *iter)
 42{
 43	struct cachefiles_object *object = kiocb->ki_filp->private_data;
 44	struct cachefiles_cache *cache = object->volume->cache;
 45	struct file *file = object->file;
 46	size_t len = iter->count;
 47	loff_t pos = kiocb->ki_pos;
 48	const struct cred *saved_cred;
 49	int ret;
 50
 51	if (!file)
 52		return -ENOBUFS;
 53
 54	cachefiles_begin_secure(cache, &saved_cred);
 55	ret = __cachefiles_prepare_write(object, file, &pos, &len, true);
 56	cachefiles_end_secure(cache, saved_cred);
 57	if (ret < 0)
 58		return ret;
 59
 60	trace_cachefiles_ondemand_fd_write(object, file_inode(file), pos, len);
 61	ret = __cachefiles_write(object, file, pos, iter, NULL, NULL);
 62	if (!ret)
 63		ret = len;
 64
 65	return ret;
 66}
 67
 68static loff_t cachefiles_ondemand_fd_llseek(struct file *filp, loff_t pos,
 69					    int whence)
 70{
 71	struct cachefiles_object *object = filp->private_data;
 72	struct file *file = object->file;
 73
 74	if (!file)
 75		return -ENOBUFS;
 76
 77	return vfs_llseek(file, pos, whence);
 78}
 79
 80static long cachefiles_ondemand_fd_ioctl(struct file *filp, unsigned int ioctl,
 81					 unsigned long arg)
 82{
 83	struct cachefiles_object *object = filp->private_data;
 84	struct cachefiles_cache *cache = object->volume->cache;
 85	struct cachefiles_req *req;
 86	unsigned long id;
 87
 88	if (ioctl != CACHEFILES_IOC_READ_COMPLETE)
 89		return -EINVAL;
 90
 91	if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags))
 92		return -EOPNOTSUPP;
 93
 94	id = arg;
 95	req = xa_erase(&cache->reqs, id);
 96	if (!req)
 97		return -EINVAL;
 98
 99	trace_cachefiles_ondemand_cread(object, id);
100	complete(&req->done);
101	return 0;
102}
103
104static const struct file_operations cachefiles_ondemand_fd_fops = {
105	.owner		= THIS_MODULE,
106	.release	= cachefiles_ondemand_fd_release,
107	.write_iter	= cachefiles_ondemand_fd_write_iter,
108	.llseek		= cachefiles_ondemand_fd_llseek,
109	.unlocked_ioctl	= cachefiles_ondemand_fd_ioctl,
110};
111
112/*
113 * OPEN request Completion (copen)
114 * - command: "copen <id>,<cache_size>"
115 *   <cache_size> indicates the object size if >=0, error code if negative
116 */
117int cachefiles_ondemand_copen(struct cachefiles_cache *cache, char *args)
118{
119	struct cachefiles_req *req;
120	struct fscache_cookie *cookie;
121	char *pid, *psize;
122	unsigned long id;
123	long size;
124	int ret;
125
126	if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags))
127		return -EOPNOTSUPP;
128
129	if (!*args) {
130		pr_err("Empty id specified\n");
131		return -EINVAL;
132	}
133
134	pid = args;
135	psize = strchr(args, ',');
136	if (!psize) {
137		pr_err("Cache size is not specified\n");
138		return -EINVAL;
139	}
140
141	*psize = 0;
142	psize++;
143
144	ret = kstrtoul(pid, 0, &id);
145	if (ret)
146		return ret;
147
148	req = xa_erase(&cache->reqs, id);
149	if (!req)
150		return -EINVAL;
151
152	/* fail OPEN request if copen format is invalid */
153	ret = kstrtol(psize, 0, &size);
154	if (ret) {
155		req->error = ret;
156		goto out;
157	}
158
159	/* fail OPEN request if daemon reports an error */
160	if (size < 0) {
161		if (!IS_ERR_VALUE(size)) {
162			req->error = -EINVAL;
163			ret = -EINVAL;
164		} else {
165			req->error = size;
166			ret = 0;
167		}
168		goto out;
169	}
170
171	cookie = req->object->cookie;
172	cookie->object_size = size;
173	if (size)
174		clear_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags);
175	else
176		set_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags);
177	trace_cachefiles_ondemand_copen(req->object, id, size);
178
 
 
 
179out:
180	complete(&req->done);
181	return ret;
182}
183
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184static int cachefiles_ondemand_get_fd(struct cachefiles_req *req)
185{
186	struct cachefiles_object *object;
187	struct cachefiles_cache *cache;
188	struct cachefiles_open *load;
189	struct file *file;
190	u32 object_id;
191	int ret, fd;
192
193	object = cachefiles_grab_object(req->object,
194			cachefiles_obj_get_ondemand_fd);
195	cache = object->volume->cache;
196
197	ret = xa_alloc_cyclic(&cache->ondemand_ids, &object_id, NULL,
198			      XA_LIMIT(1, INT_MAX),
199			      &cache->ondemand_id_next, GFP_KERNEL);
200	if (ret < 0)
201		goto err;
202
203	fd = get_unused_fd_flags(O_WRONLY);
204	if (fd < 0) {
205		ret = fd;
206		goto err_free_id;
207	}
208
209	file = anon_inode_getfile("[cachefiles]", &cachefiles_ondemand_fd_fops,
210				  object, O_WRONLY);
211	if (IS_ERR(file)) {
212		ret = PTR_ERR(file);
213		goto err_put_fd;
214	}
215
216	file->f_mode |= FMODE_PWRITE | FMODE_LSEEK;
217	fd_install(fd, file);
218
219	load = (void *)req->msg.data;
220	load->fd = fd;
221	req->msg.object_id = object_id;
222	object->ondemand_id = object_id;
223
224	cachefiles_get_unbind_pincount(cache);
225	trace_cachefiles_ondemand_open(object, &req->msg, load);
226	return 0;
227
228err_put_fd:
229	put_unused_fd(fd);
230err_free_id:
231	xa_erase(&cache->ondemand_ids, object_id);
232err:
233	cachefiles_put_object(object, cachefiles_obj_put_ondemand_fd);
234	return ret;
235}
236
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
237ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache,
238					char __user *_buffer, size_t buflen)
239{
240	struct cachefiles_req *req;
241	struct cachefiles_msg *msg;
242	unsigned long id = 0;
243	size_t n;
244	int ret = 0;
245	XA_STATE(xas, &cache->reqs, cache->req_id_next);
246
 
247	/*
248	 * Cyclically search for a request that has not ever been processed,
249	 * to prevent requests from being processed repeatedly, and make
250	 * request distribution fair.
251	 */
252	xa_lock(&cache->reqs);
253	req = xas_find_marked(&xas, UINT_MAX, CACHEFILES_REQ_NEW);
254	if (!req && cache->req_id_next > 0) {
255		xas_set(&xas, 0);
256		req = xas_find_marked(&xas, cache->req_id_next - 1, CACHEFILES_REQ_NEW);
257	}
258	if (!req) {
259		xa_unlock(&cache->reqs);
260		return 0;
261	}
262
263	msg = &req->msg;
264	n = msg->len;
265
266	if (n > buflen) {
267		xa_unlock(&cache->reqs);
268		return -EMSGSIZE;
269	}
270
271	xas_clear_mark(&xas, CACHEFILES_REQ_NEW);
272	cache->req_id_next = xas.xa_index + 1;
273	xa_unlock(&cache->reqs);
274
275	id = xas.xa_index;
276	msg->msg_id = id;
277
278	if (msg->opcode == CACHEFILES_OP_OPEN) {
279		ret = cachefiles_ondemand_get_fd(req);
280		if (ret)
 
281			goto error;
 
282	}
283
 
 
 
284	if (copy_to_user(_buffer, msg, n) != 0) {
285		ret = -EFAULT;
286		goto err_put_fd;
287	}
288
289	/* CLOSE request has no reply */
290	if (msg->opcode == CACHEFILES_OP_CLOSE) {
291		xa_erase(&cache->reqs, id);
292		complete(&req->done);
293	}
294
295	return n;
296
297err_put_fd:
298	if (msg->opcode == CACHEFILES_OP_OPEN)
299		close_fd(((struct cachefiles_open *)msg->data)->fd);
300error:
301	xa_erase(&cache->reqs, id);
302	req->error = ret;
303	complete(&req->done);
304	return ret;
305}
306
307typedef int (*init_req_fn)(struct cachefiles_req *req, void *private);
308
309static int cachefiles_ondemand_send_req(struct cachefiles_object *object,
310					enum cachefiles_opcode opcode,
311					size_t data_len,
312					init_req_fn init_req,
313					void *private)
314{
315	struct cachefiles_cache *cache = object->volume->cache;
316	struct cachefiles_req *req;
317	XA_STATE(xas, &cache->reqs, 0);
318	int ret;
319
320	if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags))
321		return 0;
322
323	if (test_bit(CACHEFILES_DEAD, &cache->flags))
324		return -EIO;
 
 
325
326	req = kzalloc(sizeof(*req) + data_len, GFP_KERNEL);
327	if (!req)
328		return -ENOMEM;
 
 
329
330	req->object = object;
331	init_completion(&req->done);
332	req->msg.opcode = opcode;
333	req->msg.len = sizeof(struct cachefiles_msg) + data_len;
334
335	ret = init_req(req, private);
336	if (ret)
337		goto out;
338
339	do {
340		/*
341		 * Stop enqueuing the request when daemon is dying. The
342		 * following two operations need to be atomic as a whole.
343		 *   1) check cache state, and
344		 *   2) enqueue request if cache is alive.
345		 * Otherwise the request may be enqueued after xarray has been
346		 * flushed, leaving the orphan request never being completed.
347		 *
348		 * CPU 1			CPU 2
349		 * =====			=====
350		 *				test CACHEFILES_DEAD bit
351		 * set CACHEFILES_DEAD bit
352		 * flush requests in the xarray
353		 *				enqueue the request
354		 */
355		xas_lock(&xas);
356
357		if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
358			xas_unlock(&xas);
359			ret = -EIO;
360			goto out;
361		}
362
363		/* coupled with the barrier in cachefiles_flush_reqs() */
364		smp_mb();
365
366		if (opcode != CACHEFILES_OP_OPEN && object->ondemand_id <= 0) {
367			WARN_ON_ONCE(object->ondemand_id == 0);
 
368			xas_unlock(&xas);
369			ret = -EIO;
370			goto out;
371		}
372
373		xas.xa_index = 0;
374		xas_find_marked(&xas, UINT_MAX, XA_FREE_MARK);
375		if (xas.xa_node == XAS_RESTART)
376			xas_set_err(&xas, -EBUSY);
377		xas_store(&xas, req);
378		xas_clear_mark(&xas, XA_FREE_MARK);
379		xas_set_mark(&xas, CACHEFILES_REQ_NEW);
380		xas_unlock(&xas);
381	} while (xas_nomem(&xas, GFP_KERNEL));
382
383	ret = xas_error(&xas);
384	if (ret)
385		goto out;
386
387	wake_up_all(&cache->daemon_pollwq);
388	wait_for_completion(&req->done);
389	ret = req->error;
 
 
390out:
 
 
 
 
 
 
391	kfree(req);
392	return ret;
393}
394
395static int cachefiles_ondemand_init_open_req(struct cachefiles_req *req,
396					     void *private)
397{
398	struct cachefiles_object *object = req->object;
399	struct fscache_cookie *cookie = object->cookie;
400	struct fscache_volume *volume = object->volume->vcookie;
401	struct cachefiles_open *load = (void *)req->msg.data;
402	size_t volume_key_size, cookie_key_size;
403	void *volume_key, *cookie_key;
404
405	/*
406	 * Volume key is a NUL-terminated string. key[0] stores strlen() of the
407	 * string, followed by the content of the string (excluding '\0').
408	 */
409	volume_key_size = volume->key[0] + 1;
410	volume_key = volume->key + 1;
411
412	/* Cookie key is binary data, which is netfs specific. */
413	cookie_key_size = cookie->key_len;
414	cookie_key = fscache_get_key(cookie);
415
416	if (!(object->cookie->advice & FSCACHE_ADV_WANT_CACHE_SIZE)) {
417		pr_err("WANT_CACHE_SIZE is needed for on-demand mode\n");
418		return -EINVAL;
419	}
420
421	load->volume_key_size = volume_key_size;
422	load->cookie_key_size = cookie_key_size;
423	memcpy(load->data, volume_key, volume_key_size);
424	memcpy(load->data + volume_key_size, cookie_key, cookie_key_size);
425
426	return 0;
427}
428
429static int cachefiles_ondemand_init_close_req(struct cachefiles_req *req,
430					      void *private)
431{
432	struct cachefiles_object *object = req->object;
433	int object_id = object->ondemand_id;
434
435	/*
436	 * It's possible that object id is still 0 if the cookie looking up
437	 * phase failed before OPEN request has ever been sent. Also avoid
438	 * sending CLOSE request for CACHEFILES_ONDEMAND_ID_CLOSED, which means
439	 * anon_fd has already been closed.
440	 */
441	if (object_id <= 0)
442		return -ENOENT;
443
444	req->msg.object_id = object_id;
445	trace_cachefiles_ondemand_close(object, &req->msg);
446	return 0;
447}
448
449struct cachefiles_read_ctx {
450	loff_t off;
451	size_t len;
452};
453
454static int cachefiles_ondemand_init_read_req(struct cachefiles_req *req,
455					     void *private)
456{
457	struct cachefiles_object *object = req->object;
458	struct cachefiles_read *load = (void *)req->msg.data;
459	struct cachefiles_read_ctx *read_ctx = private;
460	int object_id = object->ondemand_id;
461
462	/* Stop enqueuing requests when daemon has closed anon_fd. */
463	if (object_id <= 0) {
464		WARN_ON_ONCE(object_id == 0);
465		pr_info_once("READ: anonymous fd closed prematurely.\n");
466		return -EIO;
467	}
468
469	req->msg.object_id = object_id;
470	load->off = read_ctx->off;
471	load->len = read_ctx->len;
472	trace_cachefiles_ondemand_read(object, &req->msg, load);
473	return 0;
474}
475
476int cachefiles_ondemand_init_object(struct cachefiles_object *object)
477{
478	struct fscache_cookie *cookie = object->cookie;
479	struct fscache_volume *volume = object->volume->vcookie;
480	size_t volume_key_size, cookie_key_size, data_len;
481
 
 
 
482	/*
483	 * CacheFiles will firstly check the cache file under the root cache
484	 * directory. If the coherency check failed, it will fallback to
485	 * creating a new tmpfile as the cache file. Reuse the previously
486	 * allocated object ID if any.
487	 */
488	if (object->ondemand_id > 0)
489		return 0;
490
491	volume_key_size = volume->key[0] + 1;
492	cookie_key_size = cookie->key_len;
493	data_len = sizeof(struct cachefiles_open) +
494		   volume_key_size + cookie_key_size;
495
496	return cachefiles_ondemand_send_req(object, CACHEFILES_OP_OPEN,
497			data_len, cachefiles_ondemand_init_open_req, NULL);
498}
499
500void cachefiles_ondemand_clean_object(struct cachefiles_object *object)
501{
502	cachefiles_ondemand_send_req(object, CACHEFILES_OP_CLOSE, 0,
503			cachefiles_ondemand_init_close_req, NULL);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
504}
505
506int cachefiles_ondemand_read(struct cachefiles_object *object,
507			     loff_t pos, size_t len)
508{
509	struct cachefiles_read_ctx read_ctx = {pos, len};
510
511	return cachefiles_ondemand_send_req(object, CACHEFILES_OP_READ,
512			sizeof(struct cachefiles_read),
513			cachefiles_ondemand_init_read_req, &read_ctx);
514}