cleancache.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Cleancache frontend
  4. *
  5. * This code provides the generic "frontend" layer to call a matching
  6. * "backend" driver implementation of cleancache. See
  7. * Documentation/vm/cleancache.rst for more information.
  8. *
  9. * Copyright (C) 2009-2010 Oracle Corp. All rights reserved.
  10. * Author: Dan Magenheimer
  11. */
  12. #include <linux/module.h>
  13. #include <linux/fs.h>
  14. #include <linux/exportfs.h>
  15. #include <linux/mm.h>
  16. #include <linux/debugfs.h>
  17. #include <linux/cleancache.h>
  18. /*
  19. * cleancache_ops is set by cleancache_register_ops to contain the pointers
  20. * to the cleancache "backend" implementation functions.
  21. */
  22. static const struct cleancache_ops *cleancache_ops __read_mostly;
  23. /*
  24. * Counters available via /sys/kernel/debug/cleancache (if debugfs is
  25. * properly configured. These are for information only so are not protected
  26. * against increment races.
  27. */
  28. static u64 cleancache_succ_gets;
  29. static u64 cleancache_failed_gets;
  30. static u64 cleancache_puts;
  31. static u64 cleancache_invalidates;
  32. static void cleancache_register_ops_sb(struct super_block *sb, void *unused)
  33. {
  34. switch (sb->cleancache_poolid) {
  35. case CLEANCACHE_NO_BACKEND:
  36. __cleancache_init_fs(sb);
  37. break;
  38. case CLEANCACHE_NO_BACKEND_SHARED:
  39. __cleancache_init_shared_fs(sb);
  40. break;
  41. }
  42. }
  43. /*
  44. * Register operations for cleancache. Returns 0 on success.
  45. */
  46. int cleancache_register_ops(const struct cleancache_ops *ops)
  47. {
  48. if (cmpxchg(&cleancache_ops, NULL, ops))
  49. return -EBUSY;
  50. /*
  51. * A cleancache backend can be built as a module and hence loaded after
  52. * a cleancache enabled filesystem has called cleancache_init_fs. To
  53. * handle such a scenario, here we call ->init_fs or ->init_shared_fs
  54. * for each active super block. To differentiate between local and
  55. * shared filesystems, we temporarily initialize sb->cleancache_poolid
  56. * to CLEANCACHE_NO_BACKEND or CLEANCACHE_NO_BACKEND_SHARED
  57. * respectively in case there is no backend registered at the time
  58. * cleancache_init_fs or cleancache_init_shared_fs is called.
  59. *
  60. * Since filesystems can be mounted concurrently with cleancache
  61. * backend registration, we have to be careful to guarantee that all
  62. * cleancache enabled filesystems that has been mounted by the time
  63. * cleancache_register_ops is called has got and all mounted later will
  64. * get cleancache_poolid. This is assured by the following statements
  65. * tied together:
  66. *
  67. * a) iterate_supers skips only those super blocks that has started
  68. * ->kill_sb
  69. *
  70. * b) if iterate_supers encounters a super block that has not finished
  71. * ->mount yet, it waits until it is finished
  72. *
  73. * c) cleancache_init_fs is called from ->mount and
  74. * cleancache_invalidate_fs is called from ->kill_sb
  75. *
  76. * d) we call iterate_supers after cleancache_ops has been set
  77. *
  78. * From a) it follows that if iterate_supers skips a super block, then
  79. * either the super block is already dead, in which case we do not need
  80. * to bother initializing cleancache for it, or it was mounted after we
  81. * initiated iterate_supers. In the latter case, it must have seen
  82. * cleancache_ops set according to d) and initialized cleancache from
  83. * ->mount by itself according to c). This proves that we call
  84. * ->init_fs at least once for each active super block.
  85. *
  86. * From b) and c) it follows that if iterate_supers encounters a super
  87. * block that has already started ->init_fs, it will wait until ->mount
  88. * and hence ->init_fs has finished, then check cleancache_poolid, see
  89. * that it has already been set and therefore do nothing. This proves
  90. * that we call ->init_fs no more than once for each super block.
  91. *
  92. * Combined together, the last two paragraphs prove the function
  93. * correctness.
  94. *
  95. * Note that various cleancache callbacks may proceed before this
  96. * function is called or even concurrently with it, but since
  97. * CLEANCACHE_NO_BACKEND is negative, they will all result in a noop
  98. * until the corresponding ->init_fs has been actually called and
  99. * cleancache_ops has been set.
  100. */
  101. iterate_supers(cleancache_register_ops_sb, NULL);
  102. return 0;
  103. }
  104. EXPORT_SYMBOL(cleancache_register_ops);
  105. /* Called by a cleancache-enabled filesystem at time of mount */
  106. void __cleancache_init_fs(struct super_block *sb)
  107. {
  108. int pool_id = CLEANCACHE_NO_BACKEND;
  109. if (cleancache_ops) {
  110. pool_id = cleancache_ops->init_fs(PAGE_SIZE);
  111. if (pool_id < 0)
  112. pool_id = CLEANCACHE_NO_POOL;
  113. }
  114. sb->cleancache_poolid = pool_id;
  115. }
  116. EXPORT_SYMBOL(__cleancache_init_fs);
  117. /* Called by a cleancache-enabled clustered filesystem at time of mount */
  118. void __cleancache_init_shared_fs(struct super_block *sb)
  119. {
  120. int pool_id = CLEANCACHE_NO_BACKEND_SHARED;
  121. if (cleancache_ops) {
  122. pool_id = cleancache_ops->init_shared_fs(&sb->s_uuid, PAGE_SIZE);
  123. if (pool_id < 0)
  124. pool_id = CLEANCACHE_NO_POOL;
  125. }
  126. sb->cleancache_poolid = pool_id;
  127. }
  128. EXPORT_SYMBOL(__cleancache_init_shared_fs);
  129. /*
  130. * If the filesystem uses exportable filehandles, use the filehandle as
  131. * the key, else use the inode number.
  132. */
  133. static int cleancache_get_key(struct inode *inode,
  134. struct cleancache_filekey *key)
  135. {
  136. int (*fhfn)(struct inode *, __u32 *fh, int *, struct inode *);
  137. int len = 0, maxlen = CLEANCACHE_KEY_MAX;
  138. struct super_block *sb = inode->i_sb;
  139. key->u.ino = inode->i_ino;
  140. if (sb->s_export_op != NULL) {
  141. fhfn = sb->s_export_op->encode_fh;
  142. if (fhfn) {
  143. len = (*fhfn)(inode, &key->u.fh[0], &maxlen, NULL);
  144. if (len <= FILEID_ROOT || len == FILEID_INVALID)
  145. return -1;
  146. if (maxlen > CLEANCACHE_KEY_MAX)
  147. return -1;
  148. }
  149. }
  150. return 0;
  151. }
  152. /*
  153. * "Get" data from cleancache associated with the poolid/inode/index
  154. * that were specified when the data was put to cleanache and, if
  155. * successful, use it to fill the specified page with data and return 0.
  156. * The pageframe is unchanged and returns -1 if the get fails.
  157. * Page must be locked by caller.
  158. *
  159. * The function has two checks before any action is taken - whether
  160. * a backend is registered and whether the sb->cleancache_poolid
  161. * is correct.
  162. */
  163. int __cleancache_get_page(struct page *page)
  164. {
  165. int ret = -1;
  166. int pool_id;
  167. struct cleancache_filekey key = { .u.key = { 0 } };
  168. if (!cleancache_ops) {
  169. cleancache_failed_gets++;
  170. goto out;
  171. }
  172. VM_BUG_ON_PAGE(!PageLocked(page), page);
  173. pool_id = page->mapping->host->i_sb->cleancache_poolid;
  174. if (pool_id < 0)
  175. goto out;
  176. if (cleancache_get_key(page->mapping->host, &key) < 0)
  177. goto out;
  178. ret = cleancache_ops->get_page(pool_id, key, page->index, page);
  179. if (ret == 0)
  180. cleancache_succ_gets++;
  181. else
  182. cleancache_failed_gets++;
  183. out:
  184. return ret;
  185. }
  186. EXPORT_SYMBOL(__cleancache_get_page);
  187. /*
  188. * "Put" data from a page to cleancache and associate it with the
  189. * (previously-obtained per-filesystem) poolid and the page's,
  190. * inode and page index. Page must be locked. Note that a put_page
  191. * always "succeeds", though a subsequent get_page may succeed or fail.
  192. *
  193. * The function has two checks before any action is taken - whether
  194. * a backend is registered and whether the sb->cleancache_poolid
  195. * is correct.
  196. */
  197. void __cleancache_put_page(struct page *page)
  198. {
  199. int pool_id;
  200. struct cleancache_filekey key = { .u.key = { 0 } };
  201. if (!cleancache_ops) {
  202. cleancache_puts++;
  203. return;
  204. }
  205. VM_BUG_ON_PAGE(!PageLocked(page), page);
  206. pool_id = page->mapping->host->i_sb->cleancache_poolid;
  207. if (pool_id >= 0 &&
  208. cleancache_get_key(page->mapping->host, &key) >= 0) {
  209. cleancache_ops->put_page(pool_id, key, page->index, page);
  210. cleancache_puts++;
  211. }
  212. }
  213. EXPORT_SYMBOL(__cleancache_put_page);
  214. /*
  215. * Invalidate any data from cleancache associated with the poolid and the
  216. * page's inode and page index so that a subsequent "get" will fail.
  217. *
  218. * The function has two checks before any action is taken - whether
  219. * a backend is registered and whether the sb->cleancache_poolid
  220. * is correct.
  221. */
  222. void __cleancache_invalidate_page(struct address_space *mapping,
  223. struct page *page)
  224. {
  225. /* careful... page->mapping is NULL sometimes when this is called */
  226. int pool_id = mapping->host->i_sb->cleancache_poolid;
  227. struct cleancache_filekey key = { .u.key = { 0 } };
  228. if (!cleancache_ops)
  229. return;
  230. if (pool_id >= 0) {
  231. VM_BUG_ON_PAGE(!PageLocked(page), page);
  232. if (cleancache_get_key(mapping->host, &key) >= 0) {
  233. cleancache_ops->invalidate_page(pool_id,
  234. key, page->index);
  235. cleancache_invalidates++;
  236. }
  237. }
  238. }
  239. EXPORT_SYMBOL(__cleancache_invalidate_page);
  240. /*
  241. * Invalidate all data from cleancache associated with the poolid and the
  242. * mappings's inode so that all subsequent gets to this poolid/inode
  243. * will fail.
  244. *
  245. * The function has two checks before any action is taken - whether
  246. * a backend is registered and whether the sb->cleancache_poolid
  247. * is correct.
  248. */
  249. void __cleancache_invalidate_inode(struct address_space *mapping)
  250. {
  251. int pool_id = mapping->host->i_sb->cleancache_poolid;
  252. struct cleancache_filekey key = { .u.key = { 0 } };
  253. if (!cleancache_ops)
  254. return;
  255. if (pool_id >= 0 && cleancache_get_key(mapping->host, &key) >= 0)
  256. cleancache_ops->invalidate_inode(pool_id, key);
  257. }
  258. EXPORT_SYMBOL(__cleancache_invalidate_inode);
  259. /*
  260. * Called by any cleancache-enabled filesystem at time of unmount;
  261. * note that pool_id is surrendered and may be returned by a subsequent
  262. * cleancache_init_fs or cleancache_init_shared_fs.
  263. */
  264. void __cleancache_invalidate_fs(struct super_block *sb)
  265. {
  266. int pool_id;
  267. pool_id = sb->cleancache_poolid;
  268. sb->cleancache_poolid = CLEANCACHE_NO_POOL;
  269. if (cleancache_ops && pool_id >= 0)
  270. cleancache_ops->invalidate_fs(pool_id);
  271. }
  272. EXPORT_SYMBOL(__cleancache_invalidate_fs);
  273. static int __init init_cleancache(void)
  274. {
  275. #ifdef CONFIG_DEBUG_FS
  276. struct dentry *root = debugfs_create_dir("cleancache", NULL);
  277. debugfs_create_u64("succ_gets", 0444, root, &cleancache_succ_gets);
  278. debugfs_create_u64("failed_gets", 0444, root, &cleancache_failed_gets);
  279. debugfs_create_u64("puts", 0444, root, &cleancache_puts);
  280. debugfs_create_u64("invalidates", 0444, root, &cleancache_invalidates);
  281. #endif
  282. return 0;
  283. }
  284. module_init(init_cleancache)