interface.c 12 KB

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