interface.c 13 KB

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