cleancache.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Cleancache frontend
  3. *
  4. * This code provides the generic "frontend" layer to call a matching
  5. * "backend" driver implementation of cleancache. See
  6. * Documentation/vm/cleancache.txt for more information.
  7. *
  8. * Copyright (C) 2009-2010 Oracle Corp. All rights reserved.
  9. * Author: Dan Magenheimer
  10. *
  11. * This work is licensed under the terms of the GNU GPL, version 2.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/fs.h>
  15. #include <linux/exportfs.h>
  16. #include <linux/mm.h>
  17. #include <linux/debugfs.h>
  18. #include <linux/cleancache.h>
  19. /*
  20. * This global enablement flag may be read thousands of times per second
  21. * by cleancache_get/put/invalidate even on systems where cleancache_ops
  22. * is not claimed (e.g. cleancache is config'ed on but remains
  23. * disabled), so is preferred to the slower alternative: a function
  24. * call that checks a non-global.
  25. */
  26. int cleancache_enabled __read_mostly;
  27. EXPORT_SYMBOL(cleancache_enabled);
  28. /*
  29. * cleancache_ops is set by cleancache_ops_register to contain the pointers
  30. * to the cleancache "backend" implementation functions.
  31. */
  32. static struct cleancache_ops cleancache_ops __read_mostly;
  33. /*
  34. * Counters available via /sys/kernel/debug/frontswap (if debugfs is
  35. * properly configured. These are for information only so are not protected
  36. * against increment races.
  37. */
  38. static u64 cleancache_succ_gets;
  39. static u64 cleancache_failed_gets;
  40. static u64 cleancache_puts;
  41. static u64 cleancache_invalidates;
  42. /*
  43. * register operations for cleancache, returning previous thus allowing
  44. * detection of multiple backends and possible nesting
  45. */
  46. struct cleancache_ops cleancache_register_ops(struct cleancache_ops *ops)
  47. {
  48. struct cleancache_ops old = cleancache_ops;
  49. cleancache_ops = *ops;
  50. cleancache_enabled = 1;
  51. return old;
  52. }
  53. EXPORT_SYMBOL(cleancache_register_ops);
  54. /* Called by a cleancache-enabled filesystem at time of mount */
  55. void __cleancache_init_fs(struct super_block *sb)
  56. {
  57. sb->cleancache_poolid = (*cleancache_ops.init_fs)(PAGE_SIZE);
  58. }
  59. EXPORT_SYMBOL(__cleancache_init_fs);
  60. /* Called by a cleancache-enabled clustered filesystem at time of mount */
  61. void __cleancache_init_shared_fs(char *uuid, struct super_block *sb)
  62. {
  63. sb->cleancache_poolid =
  64. (*cleancache_ops.init_shared_fs)(uuid, PAGE_SIZE);
  65. }
  66. EXPORT_SYMBOL(__cleancache_init_shared_fs);
  67. /*
  68. * If the filesystem uses exportable filehandles, use the filehandle as
  69. * the key, else use the inode number.
  70. */
  71. static int cleancache_get_key(struct inode *inode,
  72. struct cleancache_filekey *key)
  73. {
  74. int (*fhfn)(struct inode *, __u32 *fh, int *, struct inode *);
  75. int len = 0, maxlen = CLEANCACHE_KEY_MAX;
  76. struct super_block *sb = inode->i_sb;
  77. key->u.ino = inode->i_ino;
  78. if (sb->s_export_op != NULL) {
  79. fhfn = sb->s_export_op->encode_fh;
  80. if (fhfn) {
  81. len = (*fhfn)(inode, &key->u.fh[0], &maxlen, NULL);
  82. if (len <= 0 || len == 255)
  83. return -1;
  84. if (maxlen > CLEANCACHE_KEY_MAX)
  85. return -1;
  86. }
  87. }
  88. return 0;
  89. }
  90. /*
  91. * "Get" data from cleancache associated with the poolid/inode/index
  92. * that were specified when the data was put to cleanache and, if
  93. * successful, use it to fill the specified page with data and return 0.
  94. * The pageframe is unchanged and returns -1 if the get fails.
  95. * Page must be locked by caller.
  96. */
  97. int __cleancache_get_page(struct page *page)
  98. {
  99. int ret = -1;
  100. int pool_id;
  101. struct cleancache_filekey key = { .u.key = { 0 } };
  102. VM_BUG_ON(!PageLocked(page));
  103. pool_id = page->mapping->host->i_sb->cleancache_poolid;
  104. if (pool_id < 0)
  105. goto out;
  106. if (cleancache_get_key(page->mapping->host, &key) < 0)
  107. goto out;
  108. ret = (*cleancache_ops.get_page)(pool_id, key, page->index, page);
  109. if (ret == 0)
  110. cleancache_succ_gets++;
  111. else
  112. cleancache_failed_gets++;
  113. out:
  114. return ret;
  115. }
  116. EXPORT_SYMBOL(__cleancache_get_page);
  117. /*
  118. * "Put" data from a page to cleancache and associate it with the
  119. * (previously-obtained per-filesystem) poolid and the page's,
  120. * inode and page index. Page must be locked. Note that a put_page
  121. * always "succeeds", though a subsequent get_page may succeed or fail.
  122. */
  123. void __cleancache_put_page(struct page *page)
  124. {
  125. int pool_id;
  126. struct cleancache_filekey key = { .u.key = { 0 } };
  127. VM_BUG_ON(!PageLocked(page));
  128. pool_id = page->mapping->host->i_sb->cleancache_poolid;
  129. if (pool_id >= 0 &&
  130. cleancache_get_key(page->mapping->host, &key) >= 0) {
  131. (*cleancache_ops.put_page)(pool_id, key, page->index, page);
  132. cleancache_puts++;
  133. }
  134. }
  135. EXPORT_SYMBOL(__cleancache_put_page);
  136. /*
  137. * Invalidate any data from cleancache associated with the poolid and the
  138. * page's inode and page index so that a subsequent "get" will fail.
  139. */
  140. void __cleancache_invalidate_page(struct address_space *mapping,
  141. struct page *page)
  142. {
  143. /* careful... page->mapping is NULL sometimes when this is called */
  144. int pool_id = mapping->host->i_sb->cleancache_poolid;
  145. struct cleancache_filekey key = { .u.key = { 0 } };
  146. if (pool_id >= 0) {
  147. VM_BUG_ON(!PageLocked(page));
  148. if (cleancache_get_key(mapping->host, &key) >= 0) {
  149. (*cleancache_ops.invalidate_page)(pool_id,
  150. key, page->index);
  151. cleancache_invalidates++;
  152. }
  153. }
  154. }
  155. EXPORT_SYMBOL(__cleancache_invalidate_page);
  156. /*
  157. * Invalidate all data from cleancache associated with the poolid and the
  158. * mappings's inode so that all subsequent gets to this poolid/inode
  159. * will fail.
  160. */
  161. void __cleancache_invalidate_inode(struct address_space *mapping)
  162. {
  163. int pool_id = mapping->host->i_sb->cleancache_poolid;
  164. struct cleancache_filekey key = { .u.key = { 0 } };
  165. if (pool_id >= 0 && cleancache_get_key(mapping->host, &key) >= 0)
  166. (*cleancache_ops.invalidate_inode)(pool_id, key);
  167. }
  168. EXPORT_SYMBOL(__cleancache_invalidate_inode);
  169. /*
  170. * Called by any cleancache-enabled filesystem at time of unmount;
  171. * note that pool_id is surrendered and may be reutrned by a subsequent
  172. * cleancache_init_fs or cleancache_init_shared_fs
  173. */
  174. void __cleancache_invalidate_fs(struct super_block *sb)
  175. {
  176. if (sb->cleancache_poolid >= 0) {
  177. int old_poolid = sb->cleancache_poolid;
  178. sb->cleancache_poolid = -1;
  179. (*cleancache_ops.invalidate_fs)(old_poolid);
  180. }
  181. }
  182. EXPORT_SYMBOL(__cleancache_invalidate_fs);
  183. static int __init init_cleancache(void)
  184. {
  185. #ifdef CONFIG_DEBUG_FS
  186. struct dentry *root = debugfs_create_dir("cleancache", NULL);
  187. if (root == NULL)
  188. return -ENXIO;
  189. debugfs_create_u64("succ_gets", S_IRUGO, root, &cleancache_succ_gets);
  190. debugfs_create_u64("failed_gets", S_IRUGO,
  191. root, &cleancache_failed_gets);
  192. debugfs_create_u64("puts", S_IRUGO, root, &cleancache_puts);
  193. debugfs_create_u64("invalidates", S_IRUGO,
  194. root, &cleancache_invalidates);
  195. #endif
  196. return 0;
  197. }
  198. module_init(init_cleancache)