xref: /linux-6.15/fs/cachefiles/interface.c (revision a18feb55)
1 /* FS-Cache interface to CacheFiles
2  *
3  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells ([email protected])
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 
16 struct cachefiles_lookup_data {
17 	struct cachefiles_xattr	*auxdata;	/* auxiliary data */
18 	char			*key;		/* key path */
19 };
20 
21 static 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  */
26 static 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;
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->def->get_key(cookie->netfs_data, buffer + 2, 512);
69 	ASSERTCMP(keylen, <, 512);
70 
71 	*(uint16_t *)buffer = keylen;
72 	((char *)buffer)[keylen + 2] = 0;
73 	((char *)buffer)[keylen + 3] = 0;
74 	((char *)buffer)[keylen + 4] = 0;
75 
76 	/* turn the raw key into something that can work with as a filename */
77 	key = cachefiles_cook_key(buffer, keylen + 2, object->type);
78 	if (!key)
79 		goto nomem_key;
80 
81 	/* get hold of the auxiliary data and prepend the object type */
82 	auxdata = buffer;
83 	auxlen = 0;
84 	if (cookie->def->get_aux) {
85 		auxlen = cookie->def->get_aux(cookie->netfs_data,
86 					      auxdata->data, 511);
87 		ASSERTCMP(auxlen, <, 511);
88 	}
89 
90 	auxdata->len = auxlen + 1;
91 	auxdata->type = cookie->def->type;
92 
93 	lookup_data->auxdata = auxdata;
94 	lookup_data->key = key;
95 	object->lookup_data = lookup_data;
96 
97 	_leave(" = %p [%p]", &object->fscache, lookup_data);
98 	return &object->fscache;
99 
100 nomem_key:
101 	kfree(buffer);
102 nomem_buffer:
103 	BUG_ON(test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags));
104 	kmem_cache_free(cachefiles_object_jar, object);
105 	fscache_object_destroyed(&cache->cache);
106 nomem_object:
107 	kfree(lookup_data);
108 nomem_lookup_data:
109 	_leave(" = -ENOMEM");
110 	return ERR_PTR(-ENOMEM);
111 }
112 
113 /*
114  * attempt to look up the nominated node in this cache
115  * - return -ETIMEDOUT to be scheduled again
116  */
117 static int cachefiles_lookup_object(struct fscache_object *_object)
118 {
119 	struct cachefiles_lookup_data *lookup_data;
120 	struct cachefiles_object *parent, *object;
121 	struct cachefiles_cache *cache;
122 	const struct cred *saved_cred;
123 	int ret;
124 
125 	_enter("{OBJ%x}", _object->debug_id);
126 
127 	cache = container_of(_object->cache, struct cachefiles_cache, cache);
128 	parent = container_of(_object->parent,
129 			      struct cachefiles_object, fscache);
130 	object = container_of(_object, struct cachefiles_object, fscache);
131 	lookup_data = object->lookup_data;
132 
133 	ASSERTCMP(lookup_data, !=, NULL);
134 
135 	/* look up the key, creating any missing bits */
136 	cachefiles_begin_secure(cache, &saved_cred);
137 	ret = cachefiles_walk_to_object(parent, object,
138 					lookup_data->key,
139 					lookup_data->auxdata);
140 	cachefiles_end_secure(cache, saved_cred);
141 
142 	/* polish off by setting the attributes of non-index files */
143 	if (ret == 0 &&
144 	    object->fscache.cookie->def->type != FSCACHE_COOKIE_TYPE_INDEX)
145 		cachefiles_attr_changed(&object->fscache);
146 
147 	if (ret < 0 && ret != -ETIMEDOUT) {
148 		if (ret != -ENOBUFS)
149 			pr_warn("Lookup failed error %d\n", ret);
150 		fscache_object_lookup_error(&object->fscache);
151 	}
152 
153 	_leave(" [%d]", ret);
154 	return ret;
155 }
156 
157 /*
158  * indication of lookup completion
159  */
160 static void cachefiles_lookup_complete(struct fscache_object *_object)
161 {
162 	struct cachefiles_object *object;
163 
164 	object = container_of(_object, struct cachefiles_object, fscache);
165 
166 	_enter("{OBJ%x,%p}", object->fscache.debug_id, object->lookup_data);
167 
168 	if (object->lookup_data) {
169 		kfree(object->lookup_data->key);
170 		kfree(object->lookup_data->auxdata);
171 		kfree(object->lookup_data);
172 		object->lookup_data = NULL;
173 	}
174 }
175 
176 /*
177  * increment the usage count on an inode object (may fail if unmounting)
178  */
179 static
180 struct fscache_object *cachefiles_grab_object(struct fscache_object *_object,
181 					      enum fscache_obj_ref_trace why)
182 {
183 	struct cachefiles_object *object =
184 		container_of(_object, struct cachefiles_object, fscache);
185 	int u;
186 
187 	_enter("{OBJ%x,%d}", _object->debug_id, atomic_read(&object->usage));
188 
189 #ifdef CACHEFILES_DEBUG_SLAB
190 	ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
191 #endif
192 
193 	u = atomic_inc_return(&object->usage);
194 	trace_cachefiles_ref(object, _object->cookie,
195 			     (enum cachefiles_obj_ref_trace)why, u);
196 	return &object->fscache;
197 }
198 
199 /*
200  * update the auxiliary data for an object object on disk
201  */
202 static void cachefiles_update_object(struct fscache_object *_object)
203 {
204 	struct cachefiles_object *object;
205 	struct cachefiles_xattr *auxdata;
206 	struct cachefiles_cache *cache;
207 	struct fscache_cookie *cookie;
208 	const struct cred *saved_cred;
209 	unsigned auxlen;
210 
211 	_enter("{OBJ%x}", _object->debug_id);
212 
213 	object = container_of(_object, struct cachefiles_object, fscache);
214 	cache = container_of(object->fscache.cache, struct cachefiles_cache,
215 			     cache);
216 
217 	if (!fscache_use_cookie(_object)) {
218 		_leave(" [relinq]");
219 		return;
220 	}
221 
222 	cookie = object->fscache.cookie;
223 
224 	if (!cookie->def->get_aux) {
225 		fscache_unuse_cookie(_object);
226 		_leave(" [no aux]");
227 		return;
228 	}
229 
230 	auxdata = kmalloc(2 + 512 + 3, cachefiles_gfp);
231 	if (!auxdata) {
232 		fscache_unuse_cookie(_object);
233 		_leave(" [nomem]");
234 		return;
235 	}
236 
237 	auxlen = cookie->def->get_aux(cookie->netfs_data, auxdata->data, 511);
238 	fscache_unuse_cookie(_object);
239 	ASSERTCMP(auxlen, <, 511);
240 
241 	auxdata->len = auxlen + 1;
242 	auxdata->type = cookie->def->type;
243 
244 	cachefiles_begin_secure(cache, &saved_cred);
245 	cachefiles_update_object_xattr(object, auxdata);
246 	cachefiles_end_secure(cache, saved_cred);
247 	kfree(auxdata);
248 	_leave("");
249 }
250 
251 /*
252  * discard the resources pinned by an object and effect retirement if
253  * requested
254  */
255 static void cachefiles_drop_object(struct fscache_object *_object)
256 {
257 	struct cachefiles_object *object;
258 	struct cachefiles_cache *cache;
259 	const struct cred *saved_cred;
260 	struct inode *inode;
261 	blkcnt_t i_blocks = 0;
262 
263 	ASSERT(_object);
264 
265 	object = container_of(_object, struct cachefiles_object, fscache);
266 
267 	_enter("{OBJ%x,%d}",
268 	       object->fscache.debug_id, atomic_read(&object->usage));
269 
270 	cache = container_of(object->fscache.cache,
271 			     struct cachefiles_cache, cache);
272 
273 #ifdef CACHEFILES_DEBUG_SLAB
274 	ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
275 #endif
276 
277 	/* We need to tidy the object up if we did in fact manage to open it.
278 	 * It's possible for us to get here before the object is fully
279 	 * initialised if the parent goes away or the object gets retired
280 	 * before we set it up.
281 	 */
282 	if (object->dentry) {
283 		/* delete retired objects */
284 		if (test_bit(FSCACHE_OBJECT_RETIRED, &object->fscache.flags) &&
285 		    _object != cache->cache.fsdef
286 		    ) {
287 			_debug("- retire object OBJ%x", object->fscache.debug_id);
288 			inode = d_backing_inode(object->dentry);
289 			if (inode)
290 				i_blocks = inode->i_blocks;
291 
292 			cachefiles_begin_secure(cache, &saved_cred);
293 			cachefiles_delete_object(cache, object);
294 			cachefiles_end_secure(cache, saved_cred);
295 		}
296 
297 		/* close the filesystem stuff attached to the object */
298 		if (object->backer != object->dentry)
299 			dput(object->backer);
300 		object->backer = NULL;
301 	}
302 
303 	/* note that the object is now inactive */
304 	if (test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags))
305 		cachefiles_mark_object_inactive(cache, object, i_blocks);
306 
307 	dput(object->dentry);
308 	object->dentry = NULL;
309 
310 	_leave("");
311 }
312 
313 /*
314  * dispose of a reference to an object
315  */
316 static void cachefiles_put_object(struct fscache_object *_object,
317 				  enum fscache_obj_ref_trace why)
318 {
319 	struct cachefiles_object *object;
320 	struct fscache_cache *cache;
321 	int u;
322 
323 	ASSERT(_object);
324 
325 	object = container_of(_object, struct cachefiles_object, fscache);
326 
327 	_enter("{OBJ%x,%d}",
328 	       object->fscache.debug_id, atomic_read(&object->usage));
329 
330 #ifdef CACHEFILES_DEBUG_SLAB
331 	ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
332 #endif
333 
334 	ASSERTIFCMP(object->fscache.parent,
335 		    object->fscache.parent->n_children, >, 0);
336 
337 	u = atomic_dec_return(&object->usage);
338 	trace_cachefiles_ref(object, _object->cookie,
339 			     (enum cachefiles_obj_ref_trace)why, u);
340 	ASSERTCMP(u, !=, -1);
341 	if (u == 0) {
342 		_debug("- kill object OBJ%x", object->fscache.debug_id);
343 
344 		ASSERT(!test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags));
345 		ASSERTCMP(object->fscache.parent, ==, NULL);
346 		ASSERTCMP(object->backer, ==, NULL);
347 		ASSERTCMP(object->dentry, ==, NULL);
348 		ASSERTCMP(object->fscache.n_ops, ==, 0);
349 		ASSERTCMP(object->fscache.n_children, ==, 0);
350 
351 		if (object->lookup_data) {
352 			kfree(object->lookup_data->key);
353 			kfree(object->lookup_data->auxdata);
354 			kfree(object->lookup_data);
355 			object->lookup_data = NULL;
356 		}
357 
358 		cache = object->fscache.cache;
359 		fscache_object_destroy(&object->fscache);
360 		kmem_cache_free(cachefiles_object_jar, object);
361 		fscache_object_destroyed(cache);
362 	}
363 
364 	_leave("");
365 }
366 
367 /*
368  * sync a cache
369  */
370 static void cachefiles_sync_cache(struct fscache_cache *_cache)
371 {
372 	struct cachefiles_cache *cache;
373 	const struct cred *saved_cred;
374 	int ret;
375 
376 	_enter("%p", _cache);
377 
378 	cache = container_of(_cache, struct cachefiles_cache, cache);
379 
380 	/* make sure all pages pinned by operations on behalf of the netfs are
381 	 * written to disc */
382 	cachefiles_begin_secure(cache, &saved_cred);
383 	down_read(&cache->mnt->mnt_sb->s_umount);
384 	ret = sync_filesystem(cache->mnt->mnt_sb);
385 	up_read(&cache->mnt->mnt_sb->s_umount);
386 	cachefiles_end_secure(cache, saved_cred);
387 
388 	if (ret == -EIO)
389 		cachefiles_io_error(cache,
390 				    "Attempt to sync backing fs superblock"
391 				    " returned error %d",
392 				    ret);
393 }
394 
395 /*
396  * check if the backing cache is updated to FS-Cache
397  * - called by FS-Cache when evaluates if need to invalidate the cache
398  */
399 static int cachefiles_check_consistency(struct fscache_operation *op)
400 {
401 	struct cachefiles_object *object;
402 	struct cachefiles_cache *cache;
403 	const struct cred *saved_cred;
404 	int ret;
405 
406 	_enter("{OBJ%x}", op->object->debug_id);
407 
408 	object = container_of(op->object, struct cachefiles_object, fscache);
409 	cache = container_of(object->fscache.cache,
410 			     struct cachefiles_cache, cache);
411 
412 	cachefiles_begin_secure(cache, &saved_cred);
413 	ret = cachefiles_check_auxdata(object);
414 	cachefiles_end_secure(cache, saved_cred);
415 
416 	_leave(" = %d", ret);
417 	return ret;
418 }
419 
420 /*
421  * notification the attributes on an object have changed
422  * - called with reads/writes excluded by FS-Cache
423  */
424 static int cachefiles_attr_changed(struct fscache_object *_object)
425 {
426 	struct cachefiles_object *object;
427 	struct cachefiles_cache *cache;
428 	const struct cred *saved_cred;
429 	struct iattr newattrs;
430 	uint64_t ni_size;
431 	loff_t oi_size;
432 	int ret;
433 
434 	_object->cookie->def->get_attr(_object->cookie->netfs_data, &ni_size);
435 
436 	_enter("{OBJ%x},[%llu]",
437 	       _object->debug_id, (unsigned long long) ni_size);
438 
439 	object = container_of(_object, struct cachefiles_object, fscache);
440 	cache = container_of(object->fscache.cache,
441 			     struct cachefiles_cache, cache);
442 
443 	if (ni_size == object->i_size)
444 		return 0;
445 
446 	if (!object->backer)
447 		return -ENOBUFS;
448 
449 	ASSERT(d_is_reg(object->backer));
450 
451 	fscache_set_store_limit(&object->fscache, ni_size);
452 
453 	oi_size = i_size_read(d_backing_inode(object->backer));
454 	if (oi_size == ni_size)
455 		return 0;
456 
457 	cachefiles_begin_secure(cache, &saved_cred);
458 	inode_lock(d_inode(object->backer));
459 
460 	/* if there's an extension to a partial page at the end of the backing
461 	 * file, we need to discard the partial page so that we pick up new
462 	 * data after it */
463 	if (oi_size & ~PAGE_MASK && ni_size > oi_size) {
464 		_debug("discard tail %llx", oi_size);
465 		newattrs.ia_valid = ATTR_SIZE;
466 		newattrs.ia_size = oi_size & PAGE_MASK;
467 		ret = notify_change(object->backer, &newattrs, NULL);
468 		if (ret < 0)
469 			goto truncate_failed;
470 	}
471 
472 	newattrs.ia_valid = ATTR_SIZE;
473 	newattrs.ia_size = ni_size;
474 	ret = notify_change(object->backer, &newattrs, NULL);
475 
476 truncate_failed:
477 	inode_unlock(d_inode(object->backer));
478 	cachefiles_end_secure(cache, saved_cred);
479 
480 	if (ret == -EIO) {
481 		fscache_set_store_limit(&object->fscache, 0);
482 		cachefiles_io_error_obj(object, "Size set failed");
483 		ret = -ENOBUFS;
484 	}
485 
486 	_leave(" = %d", ret);
487 	return ret;
488 }
489 
490 /*
491  * Invalidate an object
492  */
493 static void cachefiles_invalidate_object(struct fscache_operation *op)
494 {
495 	struct cachefiles_object *object;
496 	struct cachefiles_cache *cache;
497 	const struct cred *saved_cred;
498 	struct path path;
499 	uint64_t ni_size;
500 	int ret;
501 
502 	object = container_of(op->object, struct cachefiles_object, fscache);
503 	cache = container_of(object->fscache.cache,
504 			     struct cachefiles_cache, cache);
505 
506 	op->object->cookie->def->get_attr(op->object->cookie->netfs_data,
507 					  &ni_size);
508 
509 	_enter("{OBJ%x},[%llu]",
510 	       op->object->debug_id, (unsigned long long)ni_size);
511 
512 	if (object->backer) {
513 		ASSERT(d_is_reg(object->backer));
514 
515 		fscache_set_store_limit(&object->fscache, ni_size);
516 
517 		path.dentry = object->backer;
518 		path.mnt = cache->mnt;
519 
520 		cachefiles_begin_secure(cache, &saved_cred);
521 		ret = vfs_truncate(&path, 0);
522 		if (ret == 0)
523 			ret = vfs_truncate(&path, ni_size);
524 		cachefiles_end_secure(cache, saved_cred);
525 
526 		if (ret != 0) {
527 			fscache_set_store_limit(&object->fscache, 0);
528 			if (ret == -EIO)
529 				cachefiles_io_error_obj(object,
530 							"Invalidate failed");
531 		}
532 	}
533 
534 	fscache_op_complete(op, true);
535 	_leave("");
536 }
537 
538 /*
539  * dissociate a cache from all the pages it was backing
540  */
541 static void cachefiles_dissociate_pages(struct fscache_cache *cache)
542 {
543 	_enter("");
544 }
545 
546 const struct fscache_cache_ops cachefiles_cache_ops = {
547 	.name			= "cachefiles",
548 	.alloc_object		= cachefiles_alloc_object,
549 	.lookup_object		= cachefiles_lookup_object,
550 	.lookup_complete	= cachefiles_lookup_complete,
551 	.grab_object		= cachefiles_grab_object,
552 	.update_object		= cachefiles_update_object,
553 	.invalidate_object	= cachefiles_invalidate_object,
554 	.drop_object		= cachefiles_drop_object,
555 	.put_object		= cachefiles_put_object,
556 	.sync_cache		= cachefiles_sync_cache,
557 	.attr_changed		= cachefiles_attr_changed,
558 	.read_or_alloc_page	= cachefiles_read_or_alloc_page,
559 	.read_or_alloc_pages	= cachefiles_read_or_alloc_pages,
560 	.allocate_page		= cachefiles_allocate_page,
561 	.allocate_pages		= cachefiles_allocate_pages,
562 	.write_page		= cachefiles_write_page,
563 	.uncache_page		= cachefiles_uncache_page,
564 	.dissociate_pages	= cachefiles_dissociate_pages,
565 	.check_consistency	= cachefiles_check_consistency,
566 };
567