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/namei.h> 13 #include <linux/falloc.h> 14 #include <trace/events/fscache.h> 15 #include "internal.h" 16 17 static atomic_t cachefiles_object_debug_id; 18 19 /* 20 * Allocate a cache object record. 21 */ 22 static 23 struct cachefiles_object *cachefiles_alloc_object(struct fscache_cookie *cookie) 24 { 25 struct fscache_volume *vcookie = cookie->volume; 26 struct cachefiles_volume *volume = vcookie->cache_priv; 27 struct cachefiles_object *object; 28 29 _enter("{%s},%x,", vcookie->key, cookie->debug_id); 30 31 object = kmem_cache_zalloc(cachefiles_object_jar, GFP_KERNEL); 32 if (!object) 33 return NULL; 34 35 refcount_set(&object->ref, 1); 36 37 spin_lock_init(&object->lock); 38 INIT_LIST_HEAD(&object->cache_link); 39 object->volume = volume; 40 object->debug_id = atomic_inc_return(&cachefiles_object_debug_id); 41 object->cookie = fscache_get_cookie(cookie, fscache_cookie_get_attach_object); 42 43 fscache_count_object(vcookie->cache); 44 trace_cachefiles_ref(object->debug_id, cookie->debug_id, 1, 45 cachefiles_obj_new); 46 return object; 47 } 48 49 /* 50 * Note that an object has been seen. 51 */ 52 void cachefiles_see_object(struct cachefiles_object *object, 53 enum cachefiles_obj_ref_trace why) 54 { 55 trace_cachefiles_ref(object->debug_id, object->cookie->debug_id, 56 refcount_read(&object->ref), why); 57 } 58 59 /* 60 * Increment the usage count on an object; 61 */ 62 struct cachefiles_object *cachefiles_grab_object(struct cachefiles_object *object, 63 enum cachefiles_obj_ref_trace why) 64 { 65 int r; 66 67 __refcount_inc(&object->ref, &r); 68 trace_cachefiles_ref(object->debug_id, object->cookie->debug_id, r, why); 69 return object; 70 } 71 72 /* 73 * dispose of a reference to an object 74 */ 75 void cachefiles_put_object(struct cachefiles_object *object, 76 enum cachefiles_obj_ref_trace why) 77 { 78 unsigned int object_debug_id = object->debug_id; 79 unsigned int cookie_debug_id = object->cookie->debug_id; 80 struct fscache_cache *cache; 81 bool done; 82 int r; 83 84 done = __refcount_dec_and_test(&object->ref, &r); 85 trace_cachefiles_ref(object_debug_id, cookie_debug_id, r, why); 86 if (done) { 87 _debug("- kill object OBJ%x", object_debug_id); 88 89 ASSERTCMP(object->file, ==, NULL); 90 91 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); 97 } 98 99 _leave(""); 100 } 101 102 /* 103 * Attempt to look up the nominated node in this cache 104 */ 105 static bool cachefiles_lookup_cookie(struct fscache_cookie *cookie) 106 { 107 struct cachefiles_object *object; 108 struct cachefiles_cache *cache = cookie->volume->cache->cache_priv; 109 const struct cred *saved_cred; 110 bool success; 111 112 object = cachefiles_alloc_object(cookie); 113 if (!object) 114 goto fail; 115 116 _enter("{OBJ%x}", object->debug_id); 117 118 if (!cachefiles_cook_key(object)) 119 goto fail_put; 120 121 cookie->cache_priv = object; 122 123 cachefiles_begin_secure(cache, &saved_cred); 124 125 success = cachefiles_look_up_object(object); 126 if (!success) 127 goto fail_withdraw; 128 129 cachefiles_see_object(object, cachefiles_obj_see_lookup_cookie); 130 131 spin_lock(&cache->object_list_lock); 132 list_add(&object->cache_link, &cache->object_list); 133 spin_unlock(&cache->object_list_lock); 134 135 cachefiles_end_secure(cache, saved_cred); 136 _leave(" = t"); 137 return true; 138 139 fail_withdraw: 140 cachefiles_end_secure(cache, saved_cred); 141 cachefiles_see_object(object, cachefiles_obj_see_lookup_failed); 142 fscache_caching_failed(cookie); 143 _debug("failed c=%08x o=%08x", cookie->debug_id, object->debug_id); 144 /* The caller holds an access count on the cookie, so we need them to 145 * drop it before we can withdraw the object. 146 */ 147 return false; 148 149 fail_put: 150 cachefiles_put_object(object, cachefiles_obj_put_alloc_fail); 151 fail: 152 return false; 153 } 154 155 /* 156 * Shorten the backing object to discard any dirty data and free up 157 * any unused granules. 158 */ 159 static bool cachefiles_shorten_object(struct cachefiles_object *object, 160 struct file *file, uoff_t new_size) 161 { 162 struct cachefiles_cache *cache = object->volume->cache; 163 struct inode *inode = file_inode(file); 164 uoff_t i_size, dio_size; 165 int ret; 166 167 dio_size = round_up(new_size, cache->bsize); 168 i_size = i_size_read(inode); 169 170 trace_cachefiles_trunc(object, inode, i_size, dio_size, 171 cachefiles_trunc_shrink); 172 ret = cachefiles_inject_remove_error(); 173 if (ret == 0) 174 ret = vfs_truncate(&file->f_path, dio_size); 175 if (ret < 0) { 176 trace_cachefiles_io_error(object, file_inode(file), ret, 177 cachefiles_trace_trunc_error); 178 cachefiles_io_error_obj(object, "Trunc-to-size failed %d", ret); 179 cachefiles_remove_object_xattr(cache, object, file->f_path.dentry); 180 return false; 181 } 182 183 if (new_size < dio_size) { 184 trace_cachefiles_trunc(object, inode, dio_size, new_size, 185 cachefiles_trunc_dio_adjust); 186 ret = cachefiles_inject_write_error(); 187 if (ret == 0) 188 ret = vfs_fallocate(file, FALLOC_FL_ZERO_RANGE, 189 new_size, dio_size - new_size); 190 if (ret < 0) { 191 trace_cachefiles_io_error(object, file_inode(file), ret, 192 cachefiles_trace_fallocate_error); 193 cachefiles_io_error_obj(object, "Trunc-to-dio-size failed %d", ret); 194 cachefiles_remove_object_xattr(cache, object, file->f_path.dentry); 195 return false; 196 } 197 } 198 199 object->object_size = new_size; 200 return true; 201 } 202 203 /* 204 * Resize the backing object. 205 */ 206 static void cachefiles_resize_cookie(struct netfs_cache_resources *cres, 207 uoff_t new_size, bool shrank) 208 { 209 struct cachefiles_object *object = cachefiles_cres_object(cres); 210 struct cachefiles_cache *cache = object->volume->cache; 211 struct fscache_cookie *cookie = object->cookie; 212 const struct cred *saved_cred; 213 struct file *file = cachefiles_cres_file(cres); 214 uoff_t i_size = i_size_read(file_inode(file)); 215 216 _enter("%llu->%llu", object->object_size, new_size); 217 218 /* If the file shrank, we need to downsize the backing file and clear 219 * the end of the final block. 220 */ 221 if (shrank) { 222 if (new_size >= i_size) 223 goto out; 224 cachefiles_begin_secure(cache, &saved_cred); 225 cachefiles_shorten_object(object, file, new_size); 226 cachefiles_end_secure(cache, saved_cred); 227 object->cookie->object_size = new_size; 228 if (new_size == 0) 229 object->content_info = CACHEFILES_CONTENT_NO_DATA; 230 return; 231 } 232 233 /* The file is being expanded. We don't need to do anything 234 * particularly. The tail of the last block should have been cleared 235 * both when it is written and when it is shrunk. 236 */ 237 out: 238 object->object_size = new_size; 239 cookie->object_size = new_size; 240 } 241 242 /* 243 * Commit changes to the object as we drop it. 244 */ 245 static void cachefiles_commit_object(struct cachefiles_object *object, 246 struct cachefiles_cache *cache) 247 { 248 bool update = false; 249 250 if (test_and_clear_bit(FSCACHE_COOKIE_LOCAL_WRITE, &object->cookie->flags)) 251 update = true; 252 if (test_and_clear_bit(FSCACHE_COOKIE_NEEDS_UPDATE, &object->cookie->flags)) 253 update = true; 254 if (update) 255 cachefiles_set_object_xattr(object); 256 257 if (test_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags)) 258 cachefiles_commit_tmpfile(cache, object); 259 } 260 261 /* 262 * Finalise and object and close the VFS structs that we have. 263 */ 264 static void cachefiles_clean_up_object(struct cachefiles_object *object, 265 struct cachefiles_cache *cache) 266 { 267 struct file *file; 268 269 if (test_bit(FSCACHE_COOKIE_RETIRED, &object->cookie->flags)) { 270 if (!test_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags)) { 271 cachefiles_see_object(object, cachefiles_obj_see_clean_delete); 272 _debug("- inval object OBJ%x", object->debug_id); 273 cachefiles_delete_object(object, FSCACHE_OBJECT_WAS_RETIRED); 274 } else { 275 cachefiles_see_object(object, cachefiles_obj_see_clean_drop_tmp); 276 _debug("- inval object OBJ%x tmpfile", object->debug_id); 277 } 278 } else { 279 cachefiles_see_object(object, cachefiles_obj_see_clean_commit); 280 cachefiles_commit_object(object, cache); 281 } 282 283 cachefiles_unmark_inode_in_use(object, object->file); 284 285 spin_lock(&object->lock); 286 file = object->file; 287 object->file = NULL; 288 spin_unlock(&object->lock); 289 290 if (file) 291 fput(file); 292 } 293 294 /* 295 * Withdraw caching for a cookie. 296 */ 297 static void cachefiles_withdraw_cookie(struct fscache_cookie *cookie) 298 { 299 struct cachefiles_object *object = cookie->cache_priv; 300 struct cachefiles_cache *cache = object->volume->cache; 301 const struct cred *saved_cred; 302 303 _enter("o=%x", object->debug_id); 304 cachefiles_see_object(object, cachefiles_obj_see_withdraw_cookie); 305 306 if (!list_empty(&object->cache_link)) { 307 spin_lock(&cache->object_list_lock); 308 cachefiles_see_object(object, cachefiles_obj_see_withdrawal); 309 list_del_init(&object->cache_link); 310 spin_unlock(&cache->object_list_lock); 311 } 312 313 if (object->file) { 314 cachefiles_begin_secure(cache, &saved_cred); 315 cachefiles_clean_up_object(object, cache); 316 cachefiles_end_secure(cache, saved_cred); 317 } 318 319 cookie->cache_priv = NULL; 320 cachefiles_put_object(object, cachefiles_obj_put_detach); 321 } 322 323 /* 324 * Invalidate the storage associated with a cookie. 325 */ 326 static bool cachefiles_invalidate_cookie(struct fscache_cookie *cookie) 327 { 328 struct cachefiles_object *object = cookie->cache_priv; 329 struct file *new_file, *old_file; 330 bool old_tmpfile; 331 332 _enter("o=%x,[%llu]", object->debug_id, object->cookie->object_size); 333 334 old_tmpfile = test_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags); 335 336 if (!object->file) { 337 fscache_resume_after_invalidation(cookie); 338 _leave(" = t [light]"); 339 return true; 340 } 341 342 new_file = cachefiles_create_tmpfile(object); 343 if (IS_ERR(new_file)) 344 goto failed; 345 346 /* Substitute the VFS target */ 347 _debug("sub"); 348 spin_lock(&object->lock); 349 350 old_file = object->file; 351 object->file = new_file; 352 object->content_info = CACHEFILES_CONTENT_NO_DATA; 353 set_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags); 354 set_bit(FSCACHE_COOKIE_NEEDS_UPDATE, &object->cookie->flags); 355 356 spin_unlock(&object->lock); 357 _debug("subbed"); 358 359 /* Allow I/O to take place again */ 360 fscache_resume_after_invalidation(cookie); 361 362 if (old_file) { 363 if (!old_tmpfile) { 364 struct cachefiles_volume *volume = object->volume; 365 struct dentry *fan = volume->fanout[(u8)cookie->key_hash]; 366 struct dentry *obj; 367 368 obj = start_removing_dentry(fan, old_file->f_path.dentry); 369 if (!IS_ERR(obj)) 370 cachefiles_bury_object(volume->cache, object, 371 fan, obj, 372 FSCACHE_OBJECT_INVALIDATED); 373 } 374 fput(old_file); 375 } 376 377 _leave(" = t"); 378 return true; 379 380 failed: 381 _leave(" = f"); 382 return false; 383 } 384 385 const struct fscache_cache_ops cachefiles_cache_ops = { 386 .name = "cachefiles", 387 .acquire_volume = cachefiles_acquire_volume, 388 .free_volume = cachefiles_free_volume, 389 .lookup_cookie = cachefiles_lookup_cookie, 390 .withdraw_cookie = cachefiles_withdraw_cookie, 391 .invalidate_cookie = cachefiles_invalidate_cookie, 392 .begin_operation = cachefiles_begin_operation, 393 .resize_cookie = cachefiles_resize_cookie, 394 .prepare_to_write = cachefiles_prepare_to_write, 395 }; 396