1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* FS-Cache interface to CacheFiles
4 * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
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>
16 static atomic_t cachefiles_object_debug_id;
19 * Allocate a cache object record.
22 struct cachefiles_object *cachefiles_alloc_object(struct fscache_cookie *cookie)
24 struct fscache_volume *vcookie = cookie->volume;
25 struct cachefiles_volume *volume = vcookie->cache_priv;
26 struct cachefiles_object *object;
28 _enter("{%s},%x,", vcookie->key, cookie->debug_id);
30 object = kmem_cache_zalloc(cachefiles_object_jar, GFP_KERNEL);
34 refcount_set(&object->ref, 1);
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);
42 fscache_count_object(vcookie->cache);
43 trace_cachefiles_ref(object->debug_id, cookie->debug_id, 1,
49 * Note that an object has been seen.
51 void cachefiles_see_object(struct cachefiles_object *object,
52 enum cachefiles_obj_ref_trace why)
54 trace_cachefiles_ref(object->debug_id, object->cookie->debug_id,
55 refcount_read(&object->ref), why);
59 * Increment the usage count on an object;
61 struct cachefiles_object *cachefiles_grab_object(struct cachefiles_object *object,
62 enum cachefiles_obj_ref_trace why)
66 __refcount_inc(&object->ref, &r);
67 trace_cachefiles_ref(object->debug_id, object->cookie->debug_id, r, why);
72 * dispose of a reference to an object
74 void cachefiles_put_object(struct cachefiles_object *object,
75 enum cachefiles_obj_ref_trace why)
77 unsigned int object_debug_id = object->debug_id;
78 unsigned int cookie_debug_id = object->cookie->debug_id;
79 struct fscache_cache *cache;
83 done = __refcount_dec_and_test(&object->ref, &r);
84 trace_cachefiles_ref(object_debug_id, cookie_debug_id, r, why);
86 _debug("- kill object OBJ%x", object_debug_id);
88 ASSERTCMP(object->file, ==, NULL);
90 kfree(object->d_name);
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);
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
109 static int cachefiles_adjust_size(struct cachefiles_object *object)
111 struct iattr newattrs;
112 struct file *file = object->file;
117 ni_size = object->cookie->object_size;
118 ni_size = round_up(ni_size, CACHEFILES_DIO_BLOCK_SIZE);
120 _enter("{OBJ%x},[%llu]",
121 object->debug_id, (unsigned long long) ni_size);
126 oi_size = i_size_read(file_inode(file));
127 if (oi_size == ni_size)
130 inode_lock(file_inode(file));
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
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();
141 ret = notify_change(&nop_mnt_idmap, file->f_path.dentry,
144 goto truncate_failed;
147 newattrs.ia_valid = ATTR_SIZE;
148 newattrs.ia_size = ni_size;
149 ret = cachefiles_inject_write_error();
151 ret = notify_change(&nop_mnt_idmap, file->f_path.dentry,
155 inode_unlock(file_inode(file));
158 trace_cachefiles_io_error(NULL, file_inode(file), ret,
159 cachefiles_trace_notify_change_error);
161 cachefiles_io_error_obj(object, "Size set failed");
165 _leave(" = %d", ret);
170 * Attempt to look up the nominated node in this cache
172 static bool cachefiles_lookup_cookie(struct fscache_cookie *cookie)
174 struct cachefiles_object *object;
175 struct cachefiles_cache *cache = cookie->volume->cache->cache_priv;
176 const struct cred *saved_cred;
179 object = cachefiles_alloc_object(cookie);
183 _enter("{OBJ%x}", object->debug_id);
185 if (!cachefiles_cook_key(object))
188 cookie->cache_priv = object;
190 cachefiles_begin_secure(cache, &saved_cred);
192 success = cachefiles_look_up_object(object);
196 cachefiles_see_object(object, cachefiles_obj_see_lookup_cookie);
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);
203 cachefiles_end_secure(cache, saved_cred);
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.
218 cachefiles_put_object(object, cachefiles_obj_put_alloc_fail);
224 * Shorten the backing object to discard any dirty data and free up
225 * any unused granules.
227 static bool cachefiles_shorten_object(struct cachefiles_object *object,
228 struct file *file, loff_t new_size)
230 struct cachefiles_cache *cache = object->volume->cache;
231 struct inode *inode = file_inode(file);
232 loff_t i_size, dio_size;
235 dio_size = round_up(new_size, CACHEFILES_DIO_BLOCK_SIZE);
236 i_size = i_size_read(inode);
238 trace_cachefiles_trunc(object, inode, i_size, dio_size,
239 cachefiles_trunc_shrink);
240 ret = cachefiles_inject_remove_error();
242 ret = vfs_truncate(&file->f_path, dio_size);
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);
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();
256 ret = vfs_fallocate(file, FALLOC_FL_ZERO_RANGE,
257 new_size, dio_size - new_size);
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);
271 * Resize the backing object.
273 static void cachefiles_resize_cookie(struct netfs_cache_resources *cres,
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;
283 _enter("%llu->%llu", old_size, new_size);
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;
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.
297 cookie->object_size = new_size;
301 * Commit changes to the object as we drop it.
303 static void cachefiles_commit_object(struct cachefiles_object *object,
304 struct cachefiles_cache *cache)
308 if (test_and_clear_bit(FSCACHE_COOKIE_LOCAL_WRITE, &object->cookie->flags))
310 if (test_and_clear_bit(FSCACHE_COOKIE_NEEDS_UPDATE, &object->cookie->flags))
313 cachefiles_set_object_xattr(object);
315 if (test_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags))
316 cachefiles_commit_tmpfile(cache, object);
320 * Finalise and object and close the VFS structs that we have.
322 static void cachefiles_clean_up_object(struct cachefiles_object *object,
323 struct cachefiles_cache *cache)
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);
331 cachefiles_see_object(object, cachefiles_obj_see_clean_drop_tmp);
332 _debug("- inval object OBJ%x tmpfile", object->debug_id);
335 cachefiles_see_object(object, cachefiles_obj_see_clean_commit);
336 cachefiles_commit_object(object, cache);
339 cachefiles_unmark_inode_in_use(object, object->file);
347 * Withdraw caching for a cookie.
349 static void cachefiles_withdraw_cookie(struct fscache_cookie *cookie)
351 struct cachefiles_object *object = cookie->cache_priv;
352 struct cachefiles_cache *cache = object->volume->cache;
353 const struct cred *saved_cred;
355 _enter("o=%x", object->debug_id);
356 cachefiles_see_object(object, cachefiles_obj_see_withdraw_cookie);
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);
365 cachefiles_ondemand_clean_object(object);
368 cachefiles_begin_secure(cache, &saved_cred);
369 cachefiles_clean_up_object(object, cache);
370 cachefiles_end_secure(cache, saved_cred);
373 cookie->cache_priv = NULL;
374 cachefiles_put_object(object, cachefiles_obj_put_detach);
378 * Invalidate the storage associated with a cookie.
380 static bool cachefiles_invalidate_cookie(struct fscache_cookie *cookie)
382 struct cachefiles_object *object = cookie->cache_priv;
383 struct file *new_file, *old_file;
386 _enter("o=%x,[%llu]", object->debug_id, object->cookie->object_size);
388 old_tmpfile = test_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags);
391 fscache_resume_after_invalidation(cookie);
392 _leave(" = t [light]");
396 new_file = cachefiles_create_tmpfile(object);
397 if (IS_ERR(new_file))
400 /* Substitute the VFS target */
402 spin_lock(&object->lock);
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);
410 spin_unlock(&object->lock);
413 /* Allow I/O to take place again */
414 fscache_resume_after_invalidation(cookie);
418 struct cachefiles_volume *volume = object->volume;
419 struct dentry *fan = volume->fanout[(u8)cookie->key_hash];
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);
437 const 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,