Linux Audio

Check our new training course

Loading...
  1/* FS-Cache interface to CacheFiles
  2 *
  3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  4 * Written by David Howells (dhowells@redhat.com)
  5 *
  6 * This program is free software; you can redistribute it and/or
  7 * modify it under the terms of the GNU General Public Licence
  8 * as published by the Free Software Foundation; either version
  9 * 2 of the Licence, or (at your option) any later version.
 10 */
 11
 12#include <linux/slab.h>
 13#include <linux/mount.h>
 14#include "internal.h"
 15
 16struct cachefiles_lookup_data {
 17	struct cachefiles_xattr	*auxdata;	/* auxiliary data */
 18	char			*key;		/* key path */
 19};
 20
 21static int cachefiles_attr_changed(struct fscache_object *_object);
 22
 23/*
 24 * allocate an object record for a cookie lookup and prepare the lookup data
 25 */
 26static struct fscache_object *cachefiles_alloc_object(
 27	struct fscache_cache *_cache,
 28	struct fscache_cookie *cookie)
 29{
 30	struct cachefiles_lookup_data *lookup_data;
 31	struct cachefiles_object *object;
 32	struct cachefiles_cache *cache;
 33	struct cachefiles_xattr *auxdata;
 34	unsigned keylen, auxlen;
 35	void *buffer, *p;
 36	char *key;
 37
 38	cache = container_of(_cache, struct cachefiles_cache, cache);
 39
 40	_enter("{%s},%p,", cache->cache.identifier, cookie);
 41
 42	lookup_data = kmalloc(sizeof(*lookup_data), cachefiles_gfp);
 43	if (!lookup_data)
 44		goto nomem_lookup_data;
 45
 46	/* create a new object record and a temporary leaf image */
 47	object = kmem_cache_alloc(cachefiles_object_jar, cachefiles_gfp);
 48	if (!object)
 49		goto nomem_object;
 50
 51	ASSERTCMP(object->backer, ==, NULL);
 52
 53	BUG_ON(test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags));
 54	atomic_set(&object->usage, 1);
 55
 56	fscache_object_init(&object->fscache, cookie, &cache->cache);
 57
 58	object->type = cookie->def->type;
 59
 60	/* get hold of the raw key
 61	 * - stick the length on the front and leave space on the back for the
 62	 *   encoder
 63	 */
 64	buffer = kmalloc((2 + 512) + 3, cachefiles_gfp);
 65	if (!buffer)
 66		goto nomem_buffer;
 67
 68	keylen = cookie->key_len;
 69	if (keylen <= sizeof(cookie->inline_key))
 70		p = cookie->inline_key;
 71	else
 72		p = cookie->key;
 73	memcpy(buffer + 2, p, keylen);
 74
 75	*(uint16_t *)buffer = keylen;
 76	((char *)buffer)[keylen + 2] = 0;
 77	((char *)buffer)[keylen + 3] = 0;
 78	((char *)buffer)[keylen + 4] = 0;
 79
 80	/* turn the raw key into something that can work with as a filename */
 81	key = cachefiles_cook_key(buffer, keylen + 2, object->type);
 82	if (!key)
 83		goto nomem_key;
 84
 85	/* get hold of the auxiliary data and prepend the object type */
 86	auxdata = buffer;
 87	auxlen = cookie->aux_len;
 88	if (auxlen) {
 89		if (auxlen <= sizeof(cookie->inline_aux))
 90			p = cookie->inline_aux;
 91		else
 92			p = cookie->aux;
 93		memcpy(auxdata->data, p, auxlen);
 94	}
 95
 96	auxdata->len = auxlen + 1;
 97	auxdata->type = cookie->type;
 98
 99	lookup_data->auxdata = auxdata;
100	lookup_data->key = key;
101	object->lookup_data = lookup_data;
102
103	_leave(" = %p [%p]", &object->fscache, lookup_data);
104	return &object->fscache;
105
106nomem_key:
107	kfree(buffer);
108nomem_buffer:
109	BUG_ON(test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags));
110	kmem_cache_free(cachefiles_object_jar, object);
111	fscache_object_destroyed(&cache->cache);
112nomem_object:
113	kfree(lookup_data);
114nomem_lookup_data:
115	_leave(" = -ENOMEM");
116	return ERR_PTR(-ENOMEM);
117}
118
119/*
120 * attempt to look up the nominated node in this cache
121 * - return -ETIMEDOUT to be scheduled again
122 */
123static int cachefiles_lookup_object(struct fscache_object *_object)
124{
125	struct cachefiles_lookup_data *lookup_data;
126	struct cachefiles_object *parent, *object;
127	struct cachefiles_cache *cache;
128	const struct cred *saved_cred;
129	int ret;
130
131	_enter("{OBJ%x}", _object->debug_id);
132
133	cache = container_of(_object->cache, struct cachefiles_cache, cache);
134	parent = container_of(_object->parent,
135			      struct cachefiles_object, fscache);
136	object = container_of(_object, struct cachefiles_object, fscache);
137	lookup_data = object->lookup_data;
138
139	ASSERTCMP(lookup_data, !=, NULL);
140
141	/* look up the key, creating any missing bits */
142	cachefiles_begin_secure(cache, &saved_cred);
143	ret = cachefiles_walk_to_object(parent, object,
144					lookup_data->key,
145					lookup_data->auxdata);
146	cachefiles_end_secure(cache, saved_cred);
147
148	/* polish off by setting the attributes of non-index files */
149	if (ret == 0 &&
150	    object->fscache.cookie->def->type != FSCACHE_COOKIE_TYPE_INDEX)
151		cachefiles_attr_changed(&object->fscache);
152
153	if (ret < 0 && ret != -ETIMEDOUT) {
154		if (ret != -ENOBUFS)
155			pr_warn("Lookup failed error %d\n", ret);
156		fscache_object_lookup_error(&object->fscache);
157	}
158
159	_leave(" [%d]", ret);
160	return ret;
161}
162
163/*
164 * indication of lookup completion
165 */
166static void cachefiles_lookup_complete(struct fscache_object *_object)
167{
168	struct cachefiles_object *object;
169
170	object = container_of(_object, struct cachefiles_object, fscache);
171
172	_enter("{OBJ%x,%p}", object->fscache.debug_id, object->lookup_data);
173
174	if (object->lookup_data) {
175		kfree(object->lookup_data->key);
176		kfree(object->lookup_data->auxdata);
177		kfree(object->lookup_data);
178		object->lookup_data = NULL;
179	}
180}
181
182/*
183 * increment the usage count on an inode object (may fail if unmounting)
184 */
185static
186struct fscache_object *cachefiles_grab_object(struct fscache_object *_object,
187					      enum fscache_obj_ref_trace why)
188{
189	struct cachefiles_object *object =
190		container_of(_object, struct cachefiles_object, fscache);
191	int u;
192
193	_enter("{OBJ%x,%d}", _object->debug_id, atomic_read(&object->usage));
194
195#ifdef CACHEFILES_DEBUG_SLAB
196	ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
197#endif
198
199	u = atomic_inc_return(&object->usage);
200	trace_cachefiles_ref(object, _object->cookie,
201			     (enum cachefiles_obj_ref_trace)why, u);
202	return &object->fscache;
203}
204
205/*
206 * update the auxiliary data for an object object on disk
207 */
208static void cachefiles_update_object(struct fscache_object *_object)
209{
210	struct cachefiles_object *object;
211	struct cachefiles_xattr *auxdata;
212	struct cachefiles_cache *cache;
213	struct fscache_cookie *cookie;
214	const struct cred *saved_cred;
215	const void *aux;
216	unsigned auxlen;
217
218	_enter("{OBJ%x}", _object->debug_id);
219
220	object = container_of(_object, struct cachefiles_object, fscache);
221	cache = container_of(object->fscache.cache, struct cachefiles_cache,
222			     cache);
223
224	if (!fscache_use_cookie(_object)) {
225		_leave(" [relinq]");
226		return;
227	}
228
229	cookie = object->fscache.cookie;
230	auxlen = cookie->aux_len;
231
232	if (!auxlen) {
233		fscache_unuse_cookie(_object);
234		_leave(" [no aux]");
235		return;
236	}
237
238	auxdata = kmalloc(2 + auxlen + 3, cachefiles_gfp);
239	if (!auxdata) {
240		fscache_unuse_cookie(_object);
241		_leave(" [nomem]");
242		return;
243	}
244
245	aux = (auxlen <= sizeof(cookie->inline_aux)) ?
246		cookie->inline_aux : cookie->aux;
247
248	memcpy(auxdata->data, aux, auxlen);
249	fscache_unuse_cookie(_object);
250
251	auxdata->len = auxlen + 1;
252	auxdata->type = cookie->type;
253
254	cachefiles_begin_secure(cache, &saved_cred);
255	cachefiles_update_object_xattr(object, auxdata);
256	cachefiles_end_secure(cache, saved_cred);
257	kfree(auxdata);
258	_leave("");
259}
260
261/*
262 * discard the resources pinned by an object and effect retirement if
263 * requested
264 */
265static void cachefiles_drop_object(struct fscache_object *_object)
266{
267	struct cachefiles_object *object;
268	struct cachefiles_cache *cache;
269	const struct cred *saved_cred;
270	struct inode *inode;
271	blkcnt_t i_blocks = 0;
272
273	ASSERT(_object);
274
275	object = container_of(_object, struct cachefiles_object, fscache);
276
277	_enter("{OBJ%x,%d}",
278	       object->fscache.debug_id, atomic_read(&object->usage));
279
280	cache = container_of(object->fscache.cache,
281			     struct cachefiles_cache, cache);
282
283#ifdef CACHEFILES_DEBUG_SLAB
284	ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
285#endif
286
287	/* We need to tidy the object up if we did in fact manage to open it.
288	 * It's possible for us to get here before the object is fully
289	 * initialised if the parent goes away or the object gets retired
290	 * before we set it up.
291	 */
292	if (object->dentry) {
293		/* delete retired objects */
294		if (test_bit(FSCACHE_OBJECT_RETIRED, &object->fscache.flags) &&
295		    _object != cache->cache.fsdef
296		    ) {
297			_debug("- retire object OBJ%x", object->fscache.debug_id);
298			inode = d_backing_inode(object->dentry);
299			if (inode)
300				i_blocks = inode->i_blocks;
301
302			cachefiles_begin_secure(cache, &saved_cred);
303			cachefiles_delete_object(cache, object);
304			cachefiles_end_secure(cache, saved_cred);
305		}
306
307		/* close the filesystem stuff attached to the object */
308		if (object->backer != object->dentry)
309			dput(object->backer);
310		object->backer = NULL;
311	}
312
313	/* note that the object is now inactive */
314	if (test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags))
315		cachefiles_mark_object_inactive(cache, object, i_blocks);
316
317	dput(object->dentry);
318	object->dentry = NULL;
319
320	_leave("");
321}
322
323/*
324 * dispose of a reference to an object
325 */
326static void cachefiles_put_object(struct fscache_object *_object,
327				  enum fscache_obj_ref_trace why)
328{
329	struct cachefiles_object *object;
330	struct fscache_cache *cache;
331	int u;
332
333	ASSERT(_object);
334
335	object = container_of(_object, struct cachefiles_object, fscache);
336
337	_enter("{OBJ%x,%d}",
338	       object->fscache.debug_id, atomic_read(&object->usage));
339
340#ifdef CACHEFILES_DEBUG_SLAB
341	ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
342#endif
343
344	ASSERTIFCMP(object->fscache.parent,
345		    object->fscache.parent->n_children, >, 0);
346
347	u = atomic_dec_return(&object->usage);
348	trace_cachefiles_ref(object, _object->cookie,
349			     (enum cachefiles_obj_ref_trace)why, u);
350	ASSERTCMP(u, !=, -1);
351	if (u == 0) {
352		_debug("- kill object OBJ%x", object->fscache.debug_id);
353
354		ASSERT(!test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags));
355		ASSERTCMP(object->fscache.parent, ==, NULL);
356		ASSERTCMP(object->backer, ==, NULL);
357		ASSERTCMP(object->dentry, ==, NULL);
358		ASSERTCMP(object->fscache.n_ops, ==, 0);
359		ASSERTCMP(object->fscache.n_children, ==, 0);
360
361		if (object->lookup_data) {
362			kfree(object->lookup_data->key);
363			kfree(object->lookup_data->auxdata);
364			kfree(object->lookup_data);
365			object->lookup_data = NULL;
366		}
367
368		cache = object->fscache.cache;
369		fscache_object_destroy(&object->fscache);
370		kmem_cache_free(cachefiles_object_jar, object);
371		fscache_object_destroyed(cache);
372	}
373
374	_leave("");
375}
376
377/*
378 * sync a cache
379 */
380static void cachefiles_sync_cache(struct fscache_cache *_cache)
381{
382	struct cachefiles_cache *cache;
383	const struct cred *saved_cred;
384	int ret;
385
386	_enter("%p", _cache);
387
388	cache = container_of(_cache, struct cachefiles_cache, cache);
389
390	/* make sure all pages pinned by operations on behalf of the netfs are
391	 * written to disc */
392	cachefiles_begin_secure(cache, &saved_cred);
393	down_read(&cache->mnt->mnt_sb->s_umount);
394	ret = sync_filesystem(cache->mnt->mnt_sb);
395	up_read(&cache->mnt->mnt_sb->s_umount);
396	cachefiles_end_secure(cache, saved_cred);
397
398	if (ret == -EIO)
399		cachefiles_io_error(cache,
400				    "Attempt to sync backing fs superblock"
401				    " returned error %d",
402				    ret);
403}
404
405/*
406 * check if the backing cache is updated to FS-Cache
407 * - called by FS-Cache when evaluates if need to invalidate the cache
408 */
409static int cachefiles_check_consistency(struct fscache_operation *op)
410{
411	struct cachefiles_object *object;
412	struct cachefiles_cache *cache;
413	const struct cred *saved_cred;
414	int ret;
415
416	_enter("{OBJ%x}", op->object->debug_id);
417
418	object = container_of(op->object, struct cachefiles_object, fscache);
419	cache = container_of(object->fscache.cache,
420			     struct cachefiles_cache, cache);
421
422	cachefiles_begin_secure(cache, &saved_cred);
423	ret = cachefiles_check_auxdata(object);
424	cachefiles_end_secure(cache, saved_cred);
425
426	_leave(" = %d", ret);
427	return ret;
428}
429
430/*
431 * notification the attributes on an object have changed
432 * - called with reads/writes excluded by FS-Cache
433 */
434static int cachefiles_attr_changed(struct fscache_object *_object)
435{
436	struct cachefiles_object *object;
437	struct cachefiles_cache *cache;
438	const struct cred *saved_cred;
439	struct iattr newattrs;
440	uint64_t ni_size;
441	loff_t oi_size;
442	int ret;
443
444	ni_size = _object->store_limit_l;
445
446	_enter("{OBJ%x},[%llu]",
447	       _object->debug_id, (unsigned long long) ni_size);
448
449	object = container_of(_object, struct cachefiles_object, fscache);
450	cache = container_of(object->fscache.cache,
451			     struct cachefiles_cache, cache);
452
453	if (ni_size == object->i_size)
454		return 0;
455
456	if (!object->backer)
457		return -ENOBUFS;
458
459	ASSERT(d_is_reg(object->backer));
460
461	fscache_set_store_limit(&object->fscache, ni_size);
462
463	oi_size = i_size_read(d_backing_inode(object->backer));
464	if (oi_size == ni_size)
465		return 0;
466
467	cachefiles_begin_secure(cache, &saved_cred);
468	inode_lock(d_inode(object->backer));
469
470	/* if there's an extension to a partial page at the end of the backing
471	 * file, we need to discard the partial page so that we pick up new
472	 * data after it */
473	if (oi_size & ~PAGE_MASK && ni_size > oi_size) {
474		_debug("discard tail %llx", oi_size);
475		newattrs.ia_valid = ATTR_SIZE;
476		newattrs.ia_size = oi_size & PAGE_MASK;
477		ret = notify_change(object->backer, &newattrs, NULL);
478		if (ret < 0)
479			goto truncate_failed;
480	}
481
482	newattrs.ia_valid = ATTR_SIZE;
483	newattrs.ia_size = ni_size;
484	ret = notify_change(object->backer, &newattrs, NULL);
485
486truncate_failed:
487	inode_unlock(d_inode(object->backer));
488	cachefiles_end_secure(cache, saved_cred);
489
490	if (ret == -EIO) {
491		fscache_set_store_limit(&object->fscache, 0);
492		cachefiles_io_error_obj(object, "Size set failed");
493		ret = -ENOBUFS;
494	}
495
496	_leave(" = %d", ret);
497	return ret;
498}
499
500/*
501 * Invalidate an object
502 */
503static void cachefiles_invalidate_object(struct fscache_operation *op)
504{
505	struct cachefiles_object *object;
506	struct cachefiles_cache *cache;
507	const struct cred *saved_cred;
508	struct path path;
509	uint64_t ni_size;
510	int ret;
511
512	object = container_of(op->object, struct cachefiles_object, fscache);
513	cache = container_of(object->fscache.cache,
514			     struct cachefiles_cache, cache);
515
516	ni_size = op->object->store_limit_l;
517
518	_enter("{OBJ%x},[%llu]",
519	       op->object->debug_id, (unsigned long long)ni_size);
520
521	if (object->backer) {
522		ASSERT(d_is_reg(object->backer));
523
524		fscache_set_store_limit(&object->fscache, ni_size);
525
526		path.dentry = object->backer;
527		path.mnt = cache->mnt;
528
529		cachefiles_begin_secure(cache, &saved_cred);
530		ret = vfs_truncate(&path, 0);
531		if (ret == 0)
532			ret = vfs_truncate(&path, ni_size);
533		cachefiles_end_secure(cache, saved_cred);
534
535		if (ret != 0) {
536			fscache_set_store_limit(&object->fscache, 0);
537			if (ret == -EIO)
538				cachefiles_io_error_obj(object,
539							"Invalidate failed");
540		}
541	}
542
543	fscache_op_complete(op, true);
544	_leave("");
545}
546
547/*
548 * dissociate a cache from all the pages it was backing
549 */
550static void cachefiles_dissociate_pages(struct fscache_cache *cache)
551{
552	_enter("");
553}
554
555const struct fscache_cache_ops cachefiles_cache_ops = {
556	.name			= "cachefiles",
557	.alloc_object		= cachefiles_alloc_object,
558	.lookup_object		= cachefiles_lookup_object,
559	.lookup_complete	= cachefiles_lookup_complete,
560	.grab_object		= cachefiles_grab_object,
561	.update_object		= cachefiles_update_object,
562	.invalidate_object	= cachefiles_invalidate_object,
563	.drop_object		= cachefiles_drop_object,
564	.put_object		= cachefiles_put_object,
565	.sync_cache		= cachefiles_sync_cache,
566	.attr_changed		= cachefiles_attr_changed,
567	.read_or_alloc_page	= cachefiles_read_or_alloc_page,
568	.read_or_alloc_pages	= cachefiles_read_or_alloc_pages,
569	.allocate_page		= cachefiles_allocate_page,
570	.allocate_pages		= cachefiles_allocate_pages,
571	.write_page		= cachefiles_write_page,
572	.uncache_page		= cachefiles_uncache_page,
573	.dissociate_pages	= cachefiles_dissociate_pages,
574	.check_consistency	= cachefiles_check_consistency,
575};
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/* FS-Cache interface to CacheFiles
  3 *
  4 * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
  5 * Written by David Howells (dhowells@redhat.com)
  6 */
  7
  8#include <linux/slab.h>
  9#include <linux/mount.h>
 10#include <linux/xattr.h>
 11#include <linux/file.h>
 12#include <linux/falloc.h>
 13#include <trace/events/fscache.h>
 14#include "internal.h"
 15
 16static atomic_t cachefiles_object_debug_id;
 17
 18/*
 19 * Allocate a cache object record.
 20 */
 21static
 22struct cachefiles_object *cachefiles_alloc_object(struct fscache_cookie *cookie)
 23{
 24	struct fscache_volume *vcookie = cookie->volume;
 25	struct cachefiles_volume *volume = vcookie->cache_priv;
 26	struct cachefiles_object *object;
 27
 28	_enter("{%s},%x,", vcookie->key, cookie->debug_id);
 29
 30	object = kmem_cache_zalloc(cachefiles_object_jar, GFP_KERNEL);
 31	if (!object)
 32		return NULL;
 33
 34	refcount_set(&object->ref, 1);
 35
 36	spin_lock_init(&object->lock);
 37	INIT_LIST_HEAD(&object->cache_link);
 38	object->volume = volume;
 39	object->debug_id = atomic_inc_return(&cachefiles_object_debug_id);
 40	object->cookie = fscache_get_cookie(cookie, fscache_cookie_get_attach_object);
 41
 42	fscache_count_object(vcookie->cache);
 43	trace_cachefiles_ref(object->debug_id, cookie->debug_id, 1,
 44			     cachefiles_obj_new);
 45	return object;
 46}
 47
 48/*
 49 * Note that an object has been seen.
 50 */
 51void cachefiles_see_object(struct cachefiles_object *object,
 52			   enum cachefiles_obj_ref_trace why)
 53{
 54	trace_cachefiles_ref(object->debug_id, object->cookie->debug_id,
 55			     refcount_read(&object->ref), why);
 56}
 57
 58/*
 59 * Increment the usage count on an object;
 60 */
 61struct cachefiles_object *cachefiles_grab_object(struct cachefiles_object *object,
 62						 enum cachefiles_obj_ref_trace why)
 63{
 64	int r;
 65
 66	__refcount_inc(&object->ref, &r);
 67	trace_cachefiles_ref(object->debug_id, object->cookie->debug_id, r, why);
 68	return object;
 69}
 70
 71/*
 72 * dispose of a reference to an object
 73 */
 74void cachefiles_put_object(struct cachefiles_object *object,
 75			   enum cachefiles_obj_ref_trace why)
 76{
 77	unsigned int object_debug_id = object->debug_id;
 78	unsigned int cookie_debug_id = object->cookie->debug_id;
 79	struct fscache_cache *cache;
 80	bool done;
 81	int r;
 82
 83	done = __refcount_dec_and_test(&object->ref, &r);
 84	trace_cachefiles_ref(object_debug_id, cookie_debug_id, r, why);
 85	if (done) {
 86		_debug("- kill object OBJ%x", object_debug_id);
 87
 88		ASSERTCMP(object->file, ==, NULL);
 89
 90		kfree(object->d_name);
 91
 92		cache = object->volume->cache->cache;
 93		fscache_put_cookie(object->cookie, fscache_cookie_put_object);
 94		object->cookie = NULL;
 95		kmem_cache_free(cachefiles_object_jar, object);
 96		fscache_uncount_object(cache);
 97	}
 98
 99	_leave("");
100}
101
102/*
103 * Adjust the size of a cache file if necessary to match the DIO size.  We keep
104 * the EOF marker a multiple of DIO blocks so that we don't fall back to doing
105 * non-DIO for a partial block straddling the EOF, but we also have to be
106 * careful of someone expanding the file and accidentally accreting the
107 * padding.
108 */
109static int cachefiles_adjust_size(struct cachefiles_object *object)
110{
111	struct iattr newattrs;
112	struct file *file = object->file;
113	uint64_t ni_size;
114	loff_t oi_size;
115	int ret;
116
117	ni_size = object->cookie->object_size;
118	ni_size = round_up(ni_size, CACHEFILES_DIO_BLOCK_SIZE);
119
120	_enter("{OBJ%x},[%llu]",
121	       object->debug_id, (unsigned long long) ni_size);
122
123	if (!file)
124		return -ENOBUFS;
125
126	oi_size = i_size_read(file_inode(file));
127	if (oi_size == ni_size)
128		return 0;
129
130	inode_lock(file_inode(file));
131
132	/* if there's an extension to a partial page at the end of the backing
133	 * file, we need to discard the partial page so that we pick up new
134	 * data after it */
135	if (oi_size & ~PAGE_MASK && ni_size > oi_size) {
136		_debug("discard tail %llx", oi_size);
137		newattrs.ia_valid = ATTR_SIZE;
138		newattrs.ia_size = oi_size & PAGE_MASK;
139		ret = cachefiles_inject_remove_error();
140		if (ret == 0)
141			ret = notify_change(&init_user_ns, file->f_path.dentry,
142					    &newattrs, NULL);
143		if (ret < 0)
144			goto truncate_failed;
145	}
146
147	newattrs.ia_valid = ATTR_SIZE;
148	newattrs.ia_size = ni_size;
149	ret = cachefiles_inject_write_error();
150	if (ret == 0)
151		ret = notify_change(&init_user_ns, file->f_path.dentry,
152				    &newattrs, NULL);
153
154truncate_failed:
155	inode_unlock(file_inode(file));
156
157	if (ret < 0)
158		trace_cachefiles_io_error(NULL, file_inode(file), ret,
159					  cachefiles_trace_notify_change_error);
160	if (ret == -EIO) {
161		cachefiles_io_error_obj(object, "Size set failed");
162		ret = -ENOBUFS;
163	}
164
165	_leave(" = %d", ret);
166	return ret;
167}
168
169/*
170 * Attempt to look up the nominated node in this cache
171 */
172static bool cachefiles_lookup_cookie(struct fscache_cookie *cookie)
173{
174	struct cachefiles_object *object;
175	struct cachefiles_cache *cache = cookie->volume->cache->cache_priv;
176	const struct cred *saved_cred;
177	bool success;
178
179	object = cachefiles_alloc_object(cookie);
180	if (!object)
181		goto fail;
182
183	_enter("{OBJ%x}", object->debug_id);
184
185	if (!cachefiles_cook_key(object))
186		goto fail_put;
187
188	cookie->cache_priv = object;
189
190	cachefiles_begin_secure(cache, &saved_cred);
191
192	success = cachefiles_look_up_object(object);
193	if (!success)
194		goto fail_withdraw;
195
196	cachefiles_see_object(object, cachefiles_obj_see_lookup_cookie);
197
198	spin_lock(&cache->object_list_lock);
199	list_add(&object->cache_link, &cache->object_list);
200	spin_unlock(&cache->object_list_lock);
201	cachefiles_adjust_size(object);
202
203	cachefiles_end_secure(cache, saved_cred);
204	_leave(" = t");
205	return true;
206
207fail_withdraw:
208	cachefiles_end_secure(cache, saved_cred);
209	cachefiles_see_object(object, cachefiles_obj_see_lookup_failed);
210	fscache_caching_failed(cookie);
211	_debug("failed c=%08x o=%08x", cookie->debug_id, object->debug_id);
212	/* The caller holds an access count on the cookie, so we need them to
213	 * drop it before we can withdraw the object.
214	 */
215	return false;
216
217fail_put:
218	cachefiles_put_object(object, cachefiles_obj_put_alloc_fail);
219fail:
220	return false;
221}
222
223/*
224 * Shorten the backing object to discard any dirty data and free up
225 * any unused granules.
226 */
227static bool cachefiles_shorten_object(struct cachefiles_object *object,
228				      struct file *file, loff_t new_size)
229{
230	struct cachefiles_cache *cache = object->volume->cache;
231	struct inode *inode = file_inode(file);
232	loff_t i_size, dio_size;
233	int ret;
234
235	dio_size = round_up(new_size, CACHEFILES_DIO_BLOCK_SIZE);
236	i_size = i_size_read(inode);
237
238	trace_cachefiles_trunc(object, inode, i_size, dio_size,
239			       cachefiles_trunc_shrink);
240	ret = cachefiles_inject_remove_error();
241	if (ret == 0)
242		ret = vfs_truncate(&file->f_path, dio_size);
243	if (ret < 0) {
244		trace_cachefiles_io_error(object, file_inode(file), ret,
245					  cachefiles_trace_trunc_error);
246		cachefiles_io_error_obj(object, "Trunc-to-size failed %d", ret);
247		cachefiles_remove_object_xattr(cache, object, file->f_path.dentry);
248		return false;
249	}
250
251	if (new_size < dio_size) {
252		trace_cachefiles_trunc(object, inode, dio_size, new_size,
253				       cachefiles_trunc_dio_adjust);
254		ret = cachefiles_inject_write_error();
255		if (ret == 0)
256			ret = vfs_fallocate(file, FALLOC_FL_ZERO_RANGE,
257					    new_size, dio_size - new_size);
258		if (ret < 0) {
259			trace_cachefiles_io_error(object, file_inode(file), ret,
260						  cachefiles_trace_fallocate_error);
261			cachefiles_io_error_obj(object, "Trunc-to-dio-size failed %d", ret);
262			cachefiles_remove_object_xattr(cache, object, file->f_path.dentry);
263			return false;
264		}
265	}
266
267	return true;
268}
269
270/*
271 * Resize the backing object.
272 */
273static void cachefiles_resize_cookie(struct netfs_cache_resources *cres,
274				     loff_t new_size)
275{
276	struct cachefiles_object *object = cachefiles_cres_object(cres);
277	struct cachefiles_cache *cache = object->volume->cache;
278	struct fscache_cookie *cookie = object->cookie;
279	const struct cred *saved_cred;
280	struct file *file = cachefiles_cres_file(cres);
281	loff_t old_size = cookie->object_size;
282
283	_enter("%llu->%llu", old_size, new_size);
284
285	if (new_size < old_size) {
286		cachefiles_begin_secure(cache, &saved_cred);
287		cachefiles_shorten_object(object, file, new_size);
288		cachefiles_end_secure(cache, saved_cred);
289		object->cookie->object_size = new_size;
290		return;
291	}
292
293	/* The file is being expanded.  We don't need to do anything
294	 * particularly.  cookie->initial_size doesn't change and so the point
295	 * at which we have to download before doesn't change.
296	 */
297	cookie->object_size = new_size;
298}
299
300/*
301 * Commit changes to the object as we drop it.
302 */
303static void cachefiles_commit_object(struct cachefiles_object *object,
304				     struct cachefiles_cache *cache)
305{
306	bool update = false;
307
308	if (test_and_clear_bit(FSCACHE_COOKIE_LOCAL_WRITE, &object->cookie->flags))
309		update = true;
310	if (test_and_clear_bit(FSCACHE_COOKIE_NEEDS_UPDATE, &object->cookie->flags))
311		update = true;
312	if (update)
313		cachefiles_set_object_xattr(object);
314
315	if (test_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags))
316		cachefiles_commit_tmpfile(cache, object);
317}
318
319/*
320 * Finalise and object and close the VFS structs that we have.
321 */
322static void cachefiles_clean_up_object(struct cachefiles_object *object,
323				       struct cachefiles_cache *cache)
324{
325	if (test_bit(FSCACHE_COOKIE_RETIRED, &object->cookie->flags)) {
326		if (!test_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags)) {
327			cachefiles_see_object(object, cachefiles_obj_see_clean_delete);
328			_debug("- inval object OBJ%x", object->debug_id);
329			cachefiles_delete_object(object, FSCACHE_OBJECT_WAS_RETIRED);
330		} else {
331			cachefiles_see_object(object, cachefiles_obj_see_clean_drop_tmp);
332			_debug("- inval object OBJ%x tmpfile", object->debug_id);
333		}
334	} else {
335		cachefiles_see_object(object, cachefiles_obj_see_clean_commit);
336		cachefiles_commit_object(object, cache);
337	}
338
339	cachefiles_unmark_inode_in_use(object, object->file);
340	if (object->file) {
341		fput(object->file);
342		object->file = NULL;
343	}
344}
345
346/*
347 * Withdraw caching for a cookie.
348 */
349static void cachefiles_withdraw_cookie(struct fscache_cookie *cookie)
350{
351	struct cachefiles_object *object = cookie->cache_priv;
352	struct cachefiles_cache *cache = object->volume->cache;
353	const struct cred *saved_cred;
354
355	_enter("o=%x", object->debug_id);
356	cachefiles_see_object(object, cachefiles_obj_see_withdraw_cookie);
357
358	if (!list_empty(&object->cache_link)) {
359		spin_lock(&cache->object_list_lock);
360		cachefiles_see_object(object, cachefiles_obj_see_withdrawal);
361		list_del_init(&object->cache_link);
362		spin_unlock(&cache->object_list_lock);
363	}
364
365	cachefiles_ondemand_clean_object(object);
366
367	if (object->file) {
368		cachefiles_begin_secure(cache, &saved_cred);
369		cachefiles_clean_up_object(object, cache);
370		cachefiles_end_secure(cache, saved_cred);
371	}
372
373	cookie->cache_priv = NULL;
374	cachefiles_put_object(object, cachefiles_obj_put_detach);
375}
376
377/*
378 * Invalidate the storage associated with a cookie.
379 */
380static bool cachefiles_invalidate_cookie(struct fscache_cookie *cookie)
381{
382	struct cachefiles_object *object = cookie->cache_priv;
383	struct file *new_file, *old_file;
384	bool old_tmpfile;
385
386	_enter("o=%x,[%llu]", object->debug_id, object->cookie->object_size);
387
388	old_tmpfile = test_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags);
389
390	if (!object->file) {
391		fscache_resume_after_invalidation(cookie);
392		_leave(" = t [light]");
393		return true;
394	}
395
396	new_file = cachefiles_create_tmpfile(object);
397	if (IS_ERR(new_file))
398		goto failed;
399
400	/* Substitute the VFS target */
401	_debug("sub");
402	spin_lock(&object->lock);
403
404	old_file = object->file;
405	object->file = new_file;
406	object->content_info = CACHEFILES_CONTENT_NO_DATA;
407	set_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags);
408	set_bit(FSCACHE_COOKIE_NEEDS_UPDATE, &object->cookie->flags);
409
410	spin_unlock(&object->lock);
411	_debug("subbed");
412
413	/* Allow I/O to take place again */
414	fscache_resume_after_invalidation(cookie);
415
416	if (old_file) {
417		if (!old_tmpfile) {
418			struct cachefiles_volume *volume = object->volume;
419			struct dentry *fan = volume->fanout[(u8)cookie->key_hash];
420
421			inode_lock_nested(d_inode(fan), I_MUTEX_PARENT);
422			cachefiles_bury_object(volume->cache, object, fan,
423					       old_file->f_path.dentry,
424					       FSCACHE_OBJECT_INVALIDATED);
425		}
426		fput(old_file);
427	}
428
429	_leave(" = t");
430	return true;
431
432failed:
433	_leave(" = f");
434	return false;
435}
436
437const struct fscache_cache_ops cachefiles_cache_ops = {
438	.name			= "cachefiles",
439	.acquire_volume		= cachefiles_acquire_volume,
440	.free_volume		= cachefiles_free_volume,
441	.lookup_cookie		= cachefiles_lookup_cookie,
442	.withdraw_cookie	= cachefiles_withdraw_cookie,
443	.invalidate_cookie	= cachefiles_invalidate_cookie,
444	.begin_operation	= cachefiles_begin_operation,
445	.resize_cookie		= cachefiles_resize_cookie,
446	.prepare_to_write	= cachefiles_prepare_to_write,
447};