cache.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * V9FS cache definitions.
  3. *
  4. * Copyright (C) 2009 by Abhishek Kulkarni <adkulkar@umail.iu.edu>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2
  8. * as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to:
  17. * Free Software Foundation
  18. * 51 Franklin Street, Fifth Floor
  19. * Boston, MA 02111-1301 USA
  20. *
  21. */
  22. #include <linux/jiffies.h>
  23. #include <linux/file.h>
  24. #include <linux/slab.h>
  25. #include <linux/stat.h>
  26. #include <linux/sched.h>
  27. #include <linux/fs.h>
  28. #include <net/9p/9p.h>
  29. #include "v9fs.h"
  30. #include "cache.h"
  31. #define CACHETAG_LEN 11
  32. struct fscache_netfs v9fs_cache_netfs = {
  33. .name = "9p",
  34. .version = 0,
  35. };
  36. /**
  37. * v9fs_random_cachetag - Generate a random tag to be associated
  38. * with a new cache session.
  39. *
  40. * The value of jiffies is used for a fairly randomly cache tag.
  41. */
  42. static
  43. int v9fs_random_cachetag(struct v9fs_session_info *v9ses)
  44. {
  45. v9ses->cachetag = kmalloc(CACHETAG_LEN, GFP_KERNEL);
  46. if (!v9ses->cachetag)
  47. return -ENOMEM;
  48. return scnprintf(v9ses->cachetag, CACHETAG_LEN, "%lu", jiffies);
  49. }
  50. const struct fscache_cookie_def v9fs_cache_session_index_def = {
  51. .name = "9P.session",
  52. .type = FSCACHE_COOKIE_TYPE_INDEX,
  53. };
  54. void v9fs_cache_session_get_cookie(struct v9fs_session_info *v9ses)
  55. {
  56. /* If no cache session tag was specified, we generate a random one. */
  57. if (!v9ses->cachetag) {
  58. if (v9fs_random_cachetag(v9ses) < 0) {
  59. v9ses->fscache = NULL;
  60. kfree(v9ses->cachetag);
  61. v9ses->cachetag = NULL;
  62. return;
  63. }
  64. }
  65. v9ses->fscache = fscache_acquire_cookie(v9fs_cache_netfs.primary_index,
  66. &v9fs_cache_session_index_def,
  67. v9ses->cachetag,
  68. strlen(v9ses->cachetag),
  69. NULL, 0,
  70. v9ses, 0, true);
  71. p9_debug(P9_DEBUG_FSC, "session %p get cookie %p\n",
  72. v9ses, v9ses->fscache);
  73. }
  74. void v9fs_cache_session_put_cookie(struct v9fs_session_info *v9ses)
  75. {
  76. p9_debug(P9_DEBUG_FSC, "session %p put cookie %p\n",
  77. v9ses, v9ses->fscache);
  78. fscache_relinquish_cookie(v9ses->fscache, NULL, false);
  79. v9ses->fscache = NULL;
  80. }
  81. static enum
  82. fscache_checkaux v9fs_cache_inode_check_aux(void *cookie_netfs_data,
  83. const void *buffer,
  84. uint16_t buflen,
  85. loff_t object_size)
  86. {
  87. const struct v9fs_inode *v9inode = cookie_netfs_data;
  88. if (buflen != sizeof(v9inode->qid.version))
  89. return FSCACHE_CHECKAUX_OBSOLETE;
  90. if (memcmp(buffer, &v9inode->qid.version,
  91. sizeof(v9inode->qid.version)))
  92. return FSCACHE_CHECKAUX_OBSOLETE;
  93. return FSCACHE_CHECKAUX_OKAY;
  94. }
  95. const struct fscache_cookie_def v9fs_cache_inode_index_def = {
  96. .name = "9p.inode",
  97. .type = FSCACHE_COOKIE_TYPE_DATAFILE,
  98. .check_aux = v9fs_cache_inode_check_aux,
  99. };
  100. void v9fs_cache_inode_get_cookie(struct inode *inode)
  101. {
  102. struct v9fs_inode *v9inode;
  103. struct v9fs_session_info *v9ses;
  104. if (!S_ISREG(inode->i_mode))
  105. return;
  106. v9inode = V9FS_I(inode);
  107. if (v9inode->fscache)
  108. return;
  109. v9ses = v9fs_inode2v9ses(inode);
  110. v9inode->fscache = fscache_acquire_cookie(v9ses->fscache,
  111. &v9fs_cache_inode_index_def,
  112. &v9inode->qid.path,
  113. sizeof(v9inode->qid.path),
  114. &v9inode->qid.version,
  115. sizeof(v9inode->qid.version),
  116. v9inode,
  117. i_size_read(&v9inode->vfs_inode),
  118. true);
  119. p9_debug(P9_DEBUG_FSC, "inode %p get cookie %p\n",
  120. inode, v9inode->fscache);
  121. }
  122. void v9fs_cache_inode_put_cookie(struct inode *inode)
  123. {
  124. struct v9fs_inode *v9inode = V9FS_I(inode);
  125. if (!v9inode->fscache)
  126. return;
  127. p9_debug(P9_DEBUG_FSC, "inode %p put cookie %p\n",
  128. inode, v9inode->fscache);
  129. fscache_relinquish_cookie(v9inode->fscache, &v9inode->qid.version,
  130. false);
  131. v9inode->fscache = NULL;
  132. }
  133. void v9fs_cache_inode_flush_cookie(struct inode *inode)
  134. {
  135. struct v9fs_inode *v9inode = V9FS_I(inode);
  136. if (!v9inode->fscache)
  137. return;
  138. p9_debug(P9_DEBUG_FSC, "inode %p flush cookie %p\n",
  139. inode, v9inode->fscache);
  140. fscache_relinquish_cookie(v9inode->fscache, NULL, true);
  141. v9inode->fscache = NULL;
  142. }
  143. void v9fs_cache_inode_set_cookie(struct inode *inode, struct file *filp)
  144. {
  145. struct v9fs_inode *v9inode = V9FS_I(inode);
  146. if (!v9inode->fscache)
  147. return;
  148. mutex_lock(&v9inode->fscache_lock);
  149. if ((filp->f_flags & O_ACCMODE) != O_RDONLY)
  150. v9fs_cache_inode_flush_cookie(inode);
  151. else
  152. v9fs_cache_inode_get_cookie(inode);
  153. mutex_unlock(&v9inode->fscache_lock);
  154. }
  155. void v9fs_cache_inode_reset_cookie(struct inode *inode)
  156. {
  157. struct v9fs_inode *v9inode = V9FS_I(inode);
  158. struct v9fs_session_info *v9ses;
  159. struct fscache_cookie *old;
  160. if (!v9inode->fscache)
  161. return;
  162. old = v9inode->fscache;
  163. mutex_lock(&v9inode->fscache_lock);
  164. fscache_relinquish_cookie(v9inode->fscache, NULL, true);
  165. v9ses = v9fs_inode2v9ses(inode);
  166. v9inode->fscache = fscache_acquire_cookie(v9ses->fscache,
  167. &v9fs_cache_inode_index_def,
  168. &v9inode->qid.path,
  169. sizeof(v9inode->qid.path),
  170. &v9inode->qid.version,
  171. sizeof(v9inode->qid.version),
  172. v9inode,
  173. i_size_read(&v9inode->vfs_inode),
  174. true);
  175. p9_debug(P9_DEBUG_FSC, "inode %p revalidating cookie old %p new %p\n",
  176. inode, old, v9inode->fscache);
  177. mutex_unlock(&v9inode->fscache_lock);
  178. }
  179. int __v9fs_fscache_release_page(struct page *page, gfp_t gfp)
  180. {
  181. struct inode *inode = page->mapping->host;
  182. struct v9fs_inode *v9inode = V9FS_I(inode);
  183. BUG_ON(!v9inode->fscache);
  184. return fscache_maybe_release_page(v9inode->fscache, page, gfp);
  185. }
  186. void __v9fs_fscache_invalidate_page(struct page *page)
  187. {
  188. struct inode *inode = page->mapping->host;
  189. struct v9fs_inode *v9inode = V9FS_I(inode);
  190. BUG_ON(!v9inode->fscache);
  191. if (PageFsCache(page)) {
  192. fscache_wait_on_page_write(v9inode->fscache, page);
  193. BUG_ON(!PageLocked(page));
  194. fscache_uncache_page(v9inode->fscache, page);
  195. }
  196. }
  197. static void v9fs_vfs_readpage_complete(struct page *page, void *data,
  198. int error)
  199. {
  200. if (!error)
  201. SetPageUptodate(page);
  202. unlock_page(page);
  203. }
  204. /**
  205. * __v9fs_readpage_from_fscache - read a page from cache
  206. *
  207. * Returns 0 if the pages are in cache and a BIO is submitted,
  208. * 1 if the pages are not in cache and -error otherwise.
  209. */
  210. int __v9fs_readpage_from_fscache(struct inode *inode, struct page *page)
  211. {
  212. int ret;
  213. const struct v9fs_inode *v9inode = V9FS_I(inode);
  214. p9_debug(P9_DEBUG_FSC, "inode %p page %p\n", inode, page);
  215. if (!v9inode->fscache)
  216. return -ENOBUFS;
  217. ret = fscache_read_or_alloc_page(v9inode->fscache,
  218. page,
  219. v9fs_vfs_readpage_complete,
  220. NULL,
  221. GFP_KERNEL);
  222. switch (ret) {
  223. case -ENOBUFS:
  224. case -ENODATA:
  225. p9_debug(P9_DEBUG_FSC, "page/inode not in cache %d\n", ret);
  226. return 1;
  227. case 0:
  228. p9_debug(P9_DEBUG_FSC, "BIO submitted\n");
  229. return ret;
  230. default:
  231. p9_debug(P9_DEBUG_FSC, "ret %d\n", ret);
  232. return ret;
  233. }
  234. }
  235. /**
  236. * __v9fs_readpages_from_fscache - read multiple pages from cache
  237. *
  238. * Returns 0 if the pages are in cache and a BIO is submitted,
  239. * 1 if the pages are not in cache and -error otherwise.
  240. */
  241. int __v9fs_readpages_from_fscache(struct inode *inode,
  242. struct address_space *mapping,
  243. struct list_head *pages,
  244. unsigned *nr_pages)
  245. {
  246. int ret;
  247. const struct v9fs_inode *v9inode = V9FS_I(inode);
  248. p9_debug(P9_DEBUG_FSC, "inode %p pages %u\n", inode, *nr_pages);
  249. if (!v9inode->fscache)
  250. return -ENOBUFS;
  251. ret = fscache_read_or_alloc_pages(v9inode->fscache,
  252. mapping, pages, nr_pages,
  253. v9fs_vfs_readpage_complete,
  254. NULL,
  255. mapping_gfp_mask(mapping));
  256. switch (ret) {
  257. case -ENOBUFS:
  258. case -ENODATA:
  259. p9_debug(P9_DEBUG_FSC, "pages/inodes not in cache %d\n", ret);
  260. return 1;
  261. case 0:
  262. BUG_ON(!list_empty(pages));
  263. BUG_ON(*nr_pages != 0);
  264. p9_debug(P9_DEBUG_FSC, "BIO submitted\n");
  265. return ret;
  266. default:
  267. p9_debug(P9_DEBUG_FSC, "ret %d\n", ret);
  268. return ret;
  269. }
  270. }
  271. /**
  272. * __v9fs_readpage_to_fscache - write a page to the cache
  273. *
  274. */
  275. void __v9fs_readpage_to_fscache(struct inode *inode, struct page *page)
  276. {
  277. int ret;
  278. const struct v9fs_inode *v9inode = V9FS_I(inode);
  279. p9_debug(P9_DEBUG_FSC, "inode %p page %p\n", inode, page);
  280. ret = fscache_write_page(v9inode->fscache, page,
  281. i_size_read(&v9inode->vfs_inode), GFP_KERNEL);
  282. p9_debug(P9_DEBUG_FSC, "ret = %d\n", ret);
  283. if (ret != 0)
  284. v9fs_uncache_page(inode, page);
  285. }
  286. /*
  287. * wait for a page to complete writing to the cache
  288. */
  289. void __v9fs_fscache_wait_on_page_write(struct inode *inode, struct page *page)
  290. {
  291. const struct v9fs_inode *v9inode = V9FS_I(inode);
  292. p9_debug(P9_DEBUG_FSC, "inode %p page %p\n", inode, page);
  293. if (PageFsCache(page))
  294. fscache_wait_on_page_write(v9inode->fscache, page);
  295. }