cache.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. static uint16_t v9fs_cache_session_get_key(const void *cookie_netfs_data,
  51. void *buffer, uint16_t bufmax)
  52. {
  53. struct v9fs_session_info *v9ses;
  54. uint16_t klen = 0;
  55. v9ses = (struct v9fs_session_info *)cookie_netfs_data;
  56. p9_debug(P9_DEBUG_FSC, "session %p buf %p size %u\n",
  57. v9ses, buffer, bufmax);
  58. if (v9ses->cachetag)
  59. klen = strlen(v9ses->cachetag);
  60. if (klen > bufmax)
  61. return 0;
  62. memcpy(buffer, v9ses->cachetag, klen);
  63. p9_debug(P9_DEBUG_FSC, "cache session tag %s\n", v9ses->cachetag);
  64. return klen;
  65. }
  66. const struct fscache_cookie_def v9fs_cache_session_index_def = {
  67. .name = "9P.session",
  68. .type = FSCACHE_COOKIE_TYPE_INDEX,
  69. .get_key = v9fs_cache_session_get_key,
  70. };
  71. void v9fs_cache_session_get_cookie(struct v9fs_session_info *v9ses)
  72. {
  73. /* If no cache session tag was specified, we generate a random one. */
  74. if (!v9ses->cachetag)
  75. v9fs_random_cachetag(v9ses);
  76. v9ses->fscache = fscache_acquire_cookie(v9fs_cache_netfs.primary_index,
  77. &v9fs_cache_session_index_def,
  78. v9ses, true);
  79. p9_debug(P9_DEBUG_FSC, "session %p get cookie %p\n",
  80. v9ses, v9ses->fscache);
  81. }
  82. void v9fs_cache_session_put_cookie(struct v9fs_session_info *v9ses)
  83. {
  84. p9_debug(P9_DEBUG_FSC, "session %p put cookie %p\n",
  85. v9ses, v9ses->fscache);
  86. fscache_relinquish_cookie(v9ses->fscache, 0);
  87. v9ses->fscache = NULL;
  88. }
  89. static uint16_t v9fs_cache_inode_get_key(const void *cookie_netfs_data,
  90. void *buffer, uint16_t bufmax)
  91. {
  92. const struct v9fs_inode *v9inode = cookie_netfs_data;
  93. memcpy(buffer, &v9inode->qid.path, sizeof(v9inode->qid.path));
  94. p9_debug(P9_DEBUG_FSC, "inode %p get key %llu\n",
  95. &v9inode->vfs_inode, v9inode->qid.path);
  96. return sizeof(v9inode->qid.path);
  97. }
  98. static void v9fs_cache_inode_get_attr(const void *cookie_netfs_data,
  99. uint64_t *size)
  100. {
  101. const struct v9fs_inode *v9inode = cookie_netfs_data;
  102. *size = i_size_read(&v9inode->vfs_inode);
  103. p9_debug(P9_DEBUG_FSC, "inode %p get attr %llu\n",
  104. &v9inode->vfs_inode, *size);
  105. }
  106. static uint16_t v9fs_cache_inode_get_aux(const void *cookie_netfs_data,
  107. void *buffer, uint16_t buflen)
  108. {
  109. const struct v9fs_inode *v9inode = cookie_netfs_data;
  110. memcpy(buffer, &v9inode->qid.version, sizeof(v9inode->qid.version));
  111. p9_debug(P9_DEBUG_FSC, "inode %p get aux %u\n",
  112. &v9inode->vfs_inode, v9inode->qid.version);
  113. return sizeof(v9inode->qid.version);
  114. }
  115. static enum
  116. fscache_checkaux v9fs_cache_inode_check_aux(void *cookie_netfs_data,
  117. const void *buffer,
  118. uint16_t buflen)
  119. {
  120. const struct v9fs_inode *v9inode = cookie_netfs_data;
  121. if (buflen != sizeof(v9inode->qid.version))
  122. return FSCACHE_CHECKAUX_OBSOLETE;
  123. if (memcmp(buffer, &v9inode->qid.version,
  124. sizeof(v9inode->qid.version)))
  125. return FSCACHE_CHECKAUX_OBSOLETE;
  126. return FSCACHE_CHECKAUX_OKAY;
  127. }
  128. const struct fscache_cookie_def v9fs_cache_inode_index_def = {
  129. .name = "9p.inode",
  130. .type = FSCACHE_COOKIE_TYPE_DATAFILE,
  131. .get_key = v9fs_cache_inode_get_key,
  132. .get_attr = v9fs_cache_inode_get_attr,
  133. .get_aux = v9fs_cache_inode_get_aux,
  134. .check_aux = v9fs_cache_inode_check_aux,
  135. };
  136. void v9fs_cache_inode_get_cookie(struct inode *inode)
  137. {
  138. struct v9fs_inode *v9inode;
  139. struct v9fs_session_info *v9ses;
  140. if (!S_ISREG(inode->i_mode))
  141. return;
  142. v9inode = V9FS_I(inode);
  143. if (v9inode->fscache)
  144. return;
  145. v9ses = v9fs_inode2v9ses(inode);
  146. v9inode->fscache = fscache_acquire_cookie(v9ses->fscache,
  147. &v9fs_cache_inode_index_def,
  148. v9inode, true);
  149. p9_debug(P9_DEBUG_FSC, "inode %p get cookie %p\n",
  150. inode, v9inode->fscache);
  151. }
  152. void v9fs_cache_inode_put_cookie(struct inode *inode)
  153. {
  154. struct v9fs_inode *v9inode = V9FS_I(inode);
  155. if (!v9inode->fscache)
  156. return;
  157. p9_debug(P9_DEBUG_FSC, "inode %p put cookie %p\n",
  158. inode, v9inode->fscache);
  159. fscache_relinquish_cookie(v9inode->fscache, 0);
  160. v9inode->fscache = NULL;
  161. }
  162. void v9fs_cache_inode_flush_cookie(struct inode *inode)
  163. {
  164. struct v9fs_inode *v9inode = V9FS_I(inode);
  165. if (!v9inode->fscache)
  166. return;
  167. p9_debug(P9_DEBUG_FSC, "inode %p flush cookie %p\n",
  168. inode, v9inode->fscache);
  169. fscache_relinquish_cookie(v9inode->fscache, 1);
  170. v9inode->fscache = NULL;
  171. }
  172. void v9fs_cache_inode_set_cookie(struct inode *inode, struct file *filp)
  173. {
  174. struct v9fs_inode *v9inode = V9FS_I(inode);
  175. if (!v9inode->fscache)
  176. return;
  177. mutex_lock(&v9inode->fscache_lock);
  178. if ((filp->f_flags & O_ACCMODE) != O_RDONLY)
  179. v9fs_cache_inode_flush_cookie(inode);
  180. else
  181. v9fs_cache_inode_get_cookie(inode);
  182. mutex_unlock(&v9inode->fscache_lock);
  183. }
  184. void v9fs_cache_inode_reset_cookie(struct inode *inode)
  185. {
  186. struct v9fs_inode *v9inode = V9FS_I(inode);
  187. struct v9fs_session_info *v9ses;
  188. struct fscache_cookie *old;
  189. if (!v9inode->fscache)
  190. return;
  191. old = v9inode->fscache;
  192. mutex_lock(&v9inode->fscache_lock);
  193. fscache_relinquish_cookie(v9inode->fscache, 1);
  194. v9ses = v9fs_inode2v9ses(inode);
  195. v9inode->fscache = fscache_acquire_cookie(v9ses->fscache,
  196. &v9fs_cache_inode_index_def,
  197. v9inode, true);
  198. p9_debug(P9_DEBUG_FSC, "inode %p revalidating cookie old %p new %p\n",
  199. inode, old, v9inode->fscache);
  200. mutex_unlock(&v9inode->fscache_lock);
  201. }
  202. int __v9fs_fscache_release_page(struct page *page, gfp_t gfp)
  203. {
  204. struct inode *inode = page->mapping->host;
  205. struct v9fs_inode *v9inode = V9FS_I(inode);
  206. BUG_ON(!v9inode->fscache);
  207. return fscache_maybe_release_page(v9inode->fscache, page, gfp);
  208. }
  209. void __v9fs_fscache_invalidate_page(struct page *page)
  210. {
  211. struct inode *inode = page->mapping->host;
  212. struct v9fs_inode *v9inode = V9FS_I(inode);
  213. BUG_ON(!v9inode->fscache);
  214. if (PageFsCache(page)) {
  215. fscache_wait_on_page_write(v9inode->fscache, page);
  216. BUG_ON(!PageLocked(page));
  217. fscache_uncache_page(v9inode->fscache, page);
  218. }
  219. }
  220. static void v9fs_vfs_readpage_complete(struct page *page, void *data,
  221. int error)
  222. {
  223. if (!error)
  224. SetPageUptodate(page);
  225. unlock_page(page);
  226. }
  227. /**
  228. * __v9fs_readpage_from_fscache - read a page from cache
  229. *
  230. * Returns 0 if the pages are in cache and a BIO is submitted,
  231. * 1 if the pages are not in cache and -error otherwise.
  232. */
  233. int __v9fs_readpage_from_fscache(struct inode *inode, struct page *page)
  234. {
  235. int ret;
  236. const struct v9fs_inode *v9inode = V9FS_I(inode);
  237. p9_debug(P9_DEBUG_FSC, "inode %p page %p\n", inode, page);
  238. if (!v9inode->fscache)
  239. return -ENOBUFS;
  240. ret = fscache_read_or_alloc_page(v9inode->fscache,
  241. page,
  242. v9fs_vfs_readpage_complete,
  243. NULL,
  244. GFP_KERNEL);
  245. switch (ret) {
  246. case -ENOBUFS:
  247. case -ENODATA:
  248. p9_debug(P9_DEBUG_FSC, "page/inode not in cache %d\n", ret);
  249. return 1;
  250. case 0:
  251. p9_debug(P9_DEBUG_FSC, "BIO submitted\n");
  252. return ret;
  253. default:
  254. p9_debug(P9_DEBUG_FSC, "ret %d\n", ret);
  255. return ret;
  256. }
  257. }
  258. /**
  259. * __v9fs_readpages_from_fscache - read multiple pages from cache
  260. *
  261. * Returns 0 if the pages are in cache and a BIO is submitted,
  262. * 1 if the pages are not in cache and -error otherwise.
  263. */
  264. int __v9fs_readpages_from_fscache(struct inode *inode,
  265. struct address_space *mapping,
  266. struct list_head *pages,
  267. unsigned *nr_pages)
  268. {
  269. int ret;
  270. const struct v9fs_inode *v9inode = V9FS_I(inode);
  271. p9_debug(P9_DEBUG_FSC, "inode %p pages %u\n", inode, *nr_pages);
  272. if (!v9inode->fscache)
  273. return -ENOBUFS;
  274. ret = fscache_read_or_alloc_pages(v9inode->fscache,
  275. mapping, pages, nr_pages,
  276. v9fs_vfs_readpage_complete,
  277. NULL,
  278. mapping_gfp_mask(mapping));
  279. switch (ret) {
  280. case -ENOBUFS:
  281. case -ENODATA:
  282. p9_debug(P9_DEBUG_FSC, "pages/inodes not in cache %d\n", ret);
  283. return 1;
  284. case 0:
  285. BUG_ON(!list_empty(pages));
  286. BUG_ON(*nr_pages != 0);
  287. p9_debug(P9_DEBUG_FSC, "BIO submitted\n");
  288. return ret;
  289. default:
  290. p9_debug(P9_DEBUG_FSC, "ret %d\n", ret);
  291. return ret;
  292. }
  293. }
  294. /**
  295. * __v9fs_readpage_to_fscache - write a page to the cache
  296. *
  297. */
  298. void __v9fs_readpage_to_fscache(struct inode *inode, struct page *page)
  299. {
  300. int ret;
  301. const struct v9fs_inode *v9inode = V9FS_I(inode);
  302. p9_debug(P9_DEBUG_FSC, "inode %p page %p\n", inode, page);
  303. ret = fscache_write_page(v9inode->fscache, page, GFP_KERNEL);
  304. p9_debug(P9_DEBUG_FSC, "ret = %d\n", ret);
  305. if (ret != 0)
  306. v9fs_uncache_page(inode, page);
  307. }
  308. /*
  309. * wait for a page to complete writing to the cache
  310. */
  311. void __v9fs_fscache_wait_on_page_write(struct inode *inode, struct page *page)
  312. {
  313. const struct v9fs_inode *v9inode = V9FS_I(inode);
  314. p9_debug(P9_DEBUG_FSC, "inode %p page %p\n", inode, page);
  315. if (PageFsCache(page))
  316. fscache_wait_on_page_write(v9inode->fscache, page);
  317. }