cleancache.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 dentry *, __u32 *fh, int *, int);
  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. struct dentry d;
  82. d.d_inode = inode;
  83. len = (*fhfn)(&d, &key->u.fh[0], &maxlen, 0);
  84. if (len <= 0 || len == 255)
  85. return -1;
  86. if (maxlen > CLEANCACHE_KEY_MAX)
  87. return -1;
  88. }
  89. }
  90. return 0;
  91. }
  92. /*
  93. * "Get" data from cleancache associated with the poolid/inode/index
  94. * that were specified when the data was put to cleanache and, if
  95. * successful, use it to fill the specified page with data and return 0.
  96. * The pageframe is unchanged and returns -1 if the get fails.
  97. * Page must be locked by caller.
  98. */
  99. int __cleancache_get_page(struct page *page)
  100. {
  101. int ret = -1;
  102. int pool_id;
  103. struct cleancache_filekey key = { .u.key = { 0 } };
  104. VM_BUG_ON(!PageLocked(page));
  105. pool_id = page->mapping->host->i_sb->cleancache_poolid;
  106. if (pool_id < 0)
  107. goto out;
  108. if (cleancache_get_key(page->mapping->host, &key) < 0)
  109. goto out;
  110. ret = (*cleancache_ops.get_page)(pool_id, key, page->index, page);
  111. if (ret == 0)
  112. cleancache_succ_gets++;
  113. else
  114. cleancache_failed_gets++;
  115. out:
  116. return ret;
  117. }
  118. EXPORT_SYMBOL(__cleancache_get_page);
  119. /*
  120. * "Put" data from a page to cleancache and associate it with the
  121. * (previously-obtained per-filesystem) poolid and the page's,
  122. * inode and page index. Page must be locked. Note that a put_page
  123. * always "succeeds", though a subsequent get_page may succeed or fail.
  124. */
  125. void __cleancache_put_page(struct page *page)
  126. {
  127. int pool_id;
  128. struct cleancache_filekey key = { .u.key = { 0 } };
  129. VM_BUG_ON(!PageLocked(page));
  130. pool_id = page->mapping->host->i_sb->cleancache_poolid;
  131. if (pool_id >= 0 &&
  132. cleancache_get_key(page->mapping->host, &key) >= 0) {
  133. (*cleancache_ops.put_page)(pool_id, key, page->index, page);
  134. cleancache_puts++;
  135. }
  136. }
  137. EXPORT_SYMBOL(__cleancache_put_page);
  138. /*
  139. * Invalidate any data from cleancache associated with the poolid and the
  140. * page's inode and page index so that a subsequent "get" will fail.
  141. */
  142. void __cleancache_invalidate_page(struct address_space *mapping,
  143. struct page *page)
  144. {
  145. /* careful... page->mapping is NULL sometimes when this is called */
  146. int pool_id = mapping->host->i_sb->cleancache_poolid;
  147. struct cleancache_filekey key = { .u.key = { 0 } };
  148. if (pool_id >= 0) {
  149. VM_BUG_ON(!PageLocked(page));
  150. if (cleancache_get_key(mapping->host, &key) >= 0) {
  151. (*cleancache_ops.invalidate_page)(pool_id,
  152. key, page->index);
  153. cleancache_invalidates++;
  154. }
  155. }
  156. }
  157. EXPORT_SYMBOL(__cleancache_invalidate_page);
  158. /*
  159. * Invalidate all data from cleancache associated with the poolid and the
  160. * mappings's inode so that all subsequent gets to this poolid/inode
  161. * will fail.
  162. */
  163. void __cleancache_invalidate_inode(struct address_space *mapping)
  164. {
  165. int pool_id = mapping->host->i_sb->cleancache_poolid;
  166. struct cleancache_filekey key = { .u.key = { 0 } };
  167. if (pool_id >= 0 && cleancache_get_key(mapping->host, &key) >= 0)
  168. (*cleancache_ops.invalidate_inode)(pool_id, key);
  169. }
  170. EXPORT_SYMBOL(__cleancache_invalidate_inode);
  171. /*
  172. * Called by any cleancache-enabled filesystem at time of unmount;
  173. * note that pool_id is surrendered and may be reutrned by a subsequent
  174. * cleancache_init_fs or cleancache_init_shared_fs
  175. */
  176. void __cleancache_invalidate_fs(struct super_block *sb)
  177. {
  178. if (sb->cleancache_poolid >= 0) {
  179. int old_poolid = sb->cleancache_poolid;
  180. sb->cleancache_poolid = -1;
  181. (*cleancache_ops.invalidate_fs)(old_poolid);
  182. }
  183. }
  184. EXPORT_SYMBOL(__cleancache_invalidate_fs);
  185. static int __init init_cleancache(void)
  186. {
  187. #ifdef CONFIG_DEBUG_FS
  188. struct dentry *root = debugfs_create_dir("cleancache", NULL);
  189. if (root == NULL)
  190. return -ENXIO;
  191. debugfs_create_u64("succ_gets", S_IRUGO, root, &cleancache_succ_gets);
  192. debugfs_create_u64("failed_gets", S_IRUGO,
  193. root, &cleancache_failed_gets);
  194. debugfs_create_u64("puts", S_IRUGO, root, &cleancache_puts);
  195. debugfs_create_u64("invalidates", S_IRUGO,
  196. root, &cleancache_invalidates);
  197. #endif
  198. return 0;
  199. }
  200. module_init(init_cleancache)