cache.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. static void v9fs_cache_inode_now_uncached(void *cookie_netfs_data)
  129. {
  130. struct v9fs_inode *v9inode = cookie_netfs_data;
  131. struct pagevec pvec;
  132. pgoff_t first;
  133. int loop, nr_pages;
  134. pagevec_init(&pvec, 0);
  135. first = 0;
  136. for (;;) {
  137. nr_pages = pagevec_lookup(&pvec, v9inode->vfs_inode.i_mapping,
  138. first,
  139. PAGEVEC_SIZE - pagevec_count(&pvec));
  140. if (!nr_pages)
  141. break;
  142. for (loop = 0; loop < nr_pages; loop++)
  143. ClearPageFsCache(pvec.pages[loop]);
  144. first = pvec.pages[nr_pages - 1]->index + 1;
  145. pvec.nr = nr_pages;
  146. pagevec_release(&pvec);
  147. cond_resched();
  148. }
  149. }
  150. const struct fscache_cookie_def v9fs_cache_inode_index_def = {
  151. .name = "9p.inode",
  152. .type = FSCACHE_COOKIE_TYPE_DATAFILE,
  153. .get_key = v9fs_cache_inode_get_key,
  154. .get_attr = v9fs_cache_inode_get_attr,
  155. .get_aux = v9fs_cache_inode_get_aux,
  156. .check_aux = v9fs_cache_inode_check_aux,
  157. .now_uncached = v9fs_cache_inode_now_uncached,
  158. };
  159. void v9fs_cache_inode_get_cookie(struct inode *inode)
  160. {
  161. struct v9fs_inode *v9inode;
  162. struct v9fs_session_info *v9ses;
  163. if (!S_ISREG(inode->i_mode))
  164. return;
  165. v9inode = V9FS_I(inode);
  166. if (v9inode->fscache)
  167. return;
  168. v9ses = v9fs_inode2v9ses(inode);
  169. v9inode->fscache = fscache_acquire_cookie(v9ses->fscache,
  170. &v9fs_cache_inode_index_def,
  171. v9inode, true);
  172. p9_debug(P9_DEBUG_FSC, "inode %p get cookie %p\n",
  173. inode, v9inode->fscache);
  174. }
  175. void v9fs_cache_inode_put_cookie(struct inode *inode)
  176. {
  177. struct v9fs_inode *v9inode = V9FS_I(inode);
  178. if (!v9inode->fscache)
  179. return;
  180. p9_debug(P9_DEBUG_FSC, "inode %p put cookie %p\n",
  181. inode, v9inode->fscache);
  182. fscache_relinquish_cookie(v9inode->fscache, 0);
  183. v9inode->fscache = NULL;
  184. }
  185. void v9fs_cache_inode_flush_cookie(struct inode *inode)
  186. {
  187. struct v9fs_inode *v9inode = V9FS_I(inode);
  188. if (!v9inode->fscache)
  189. return;
  190. p9_debug(P9_DEBUG_FSC, "inode %p flush cookie %p\n",
  191. inode, v9inode->fscache);
  192. fscache_relinquish_cookie(v9inode->fscache, 1);
  193. v9inode->fscache = NULL;
  194. }
  195. void v9fs_cache_inode_set_cookie(struct inode *inode, struct file *filp)
  196. {
  197. struct v9fs_inode *v9inode = V9FS_I(inode);
  198. if (!v9inode->fscache)
  199. return;
  200. mutex_lock(&v9inode->fscache_lock);
  201. if ((filp->f_flags & O_ACCMODE) != O_RDONLY)
  202. v9fs_cache_inode_flush_cookie(inode);
  203. else
  204. v9fs_cache_inode_get_cookie(inode);
  205. mutex_unlock(&v9inode->fscache_lock);
  206. }
  207. void v9fs_cache_inode_reset_cookie(struct inode *inode)
  208. {
  209. struct v9fs_inode *v9inode = V9FS_I(inode);
  210. struct v9fs_session_info *v9ses;
  211. struct fscache_cookie *old;
  212. if (!v9inode->fscache)
  213. return;
  214. old = v9inode->fscache;
  215. mutex_lock(&v9inode->fscache_lock);
  216. fscache_relinquish_cookie(v9inode->fscache, 1);
  217. v9ses = v9fs_inode2v9ses(inode);
  218. v9inode->fscache = fscache_acquire_cookie(v9ses->fscache,
  219. &v9fs_cache_inode_index_def,
  220. v9inode, true);
  221. p9_debug(P9_DEBUG_FSC, "inode %p revalidating cookie old %p new %p\n",
  222. inode, old, v9inode->fscache);
  223. mutex_unlock(&v9inode->fscache_lock);
  224. }
  225. int __v9fs_fscache_release_page(struct page *page, gfp_t gfp)
  226. {
  227. struct inode *inode = page->mapping->host;
  228. struct v9fs_inode *v9inode = V9FS_I(inode);
  229. BUG_ON(!v9inode->fscache);
  230. return fscache_maybe_release_page(v9inode->fscache, page, gfp);
  231. }
  232. void __v9fs_fscache_invalidate_page(struct page *page)
  233. {
  234. struct inode *inode = page->mapping->host;
  235. struct v9fs_inode *v9inode = V9FS_I(inode);
  236. BUG_ON(!v9inode->fscache);
  237. if (PageFsCache(page)) {
  238. fscache_wait_on_page_write(v9inode->fscache, page);
  239. BUG_ON(!PageLocked(page));
  240. fscache_uncache_page(v9inode->fscache, page);
  241. }
  242. }
  243. static void v9fs_vfs_readpage_complete(struct page *page, void *data,
  244. int error)
  245. {
  246. if (!error)
  247. SetPageUptodate(page);
  248. unlock_page(page);
  249. }
  250. /**
  251. * __v9fs_readpage_from_fscache - read a page from cache
  252. *
  253. * Returns 0 if the pages are in cache and a BIO is submitted,
  254. * 1 if the pages are not in cache and -error otherwise.
  255. */
  256. int __v9fs_readpage_from_fscache(struct inode *inode, struct page *page)
  257. {
  258. int ret;
  259. const struct v9fs_inode *v9inode = V9FS_I(inode);
  260. p9_debug(P9_DEBUG_FSC, "inode %p page %p\n", inode, page);
  261. if (!v9inode->fscache)
  262. return -ENOBUFS;
  263. ret = fscache_read_or_alloc_page(v9inode->fscache,
  264. page,
  265. v9fs_vfs_readpage_complete,
  266. NULL,
  267. GFP_KERNEL);
  268. switch (ret) {
  269. case -ENOBUFS:
  270. case -ENODATA:
  271. p9_debug(P9_DEBUG_FSC, "page/inode not in cache %d\n", ret);
  272. return 1;
  273. case 0:
  274. p9_debug(P9_DEBUG_FSC, "BIO submitted\n");
  275. return ret;
  276. default:
  277. p9_debug(P9_DEBUG_FSC, "ret %d\n", ret);
  278. return ret;
  279. }
  280. }
  281. /**
  282. * __v9fs_readpages_from_fscache - read multiple pages from cache
  283. *
  284. * Returns 0 if the pages are in cache and a BIO is submitted,
  285. * 1 if the pages are not in cache and -error otherwise.
  286. */
  287. int __v9fs_readpages_from_fscache(struct inode *inode,
  288. struct address_space *mapping,
  289. struct list_head *pages,
  290. unsigned *nr_pages)
  291. {
  292. int ret;
  293. const struct v9fs_inode *v9inode = V9FS_I(inode);
  294. p9_debug(P9_DEBUG_FSC, "inode %p pages %u\n", inode, *nr_pages);
  295. if (!v9inode->fscache)
  296. return -ENOBUFS;
  297. ret = fscache_read_or_alloc_pages(v9inode->fscache,
  298. mapping, pages, nr_pages,
  299. v9fs_vfs_readpage_complete,
  300. NULL,
  301. mapping_gfp_mask(mapping));
  302. switch (ret) {
  303. case -ENOBUFS:
  304. case -ENODATA:
  305. p9_debug(P9_DEBUG_FSC, "pages/inodes not in cache %d\n", ret);
  306. return 1;
  307. case 0:
  308. BUG_ON(!list_empty(pages));
  309. BUG_ON(*nr_pages != 0);
  310. p9_debug(P9_DEBUG_FSC, "BIO submitted\n");
  311. return ret;
  312. default:
  313. p9_debug(P9_DEBUG_FSC, "ret %d\n", ret);
  314. return ret;
  315. }
  316. }
  317. /**
  318. * __v9fs_readpage_to_fscache - write a page to the cache
  319. *
  320. */
  321. void __v9fs_readpage_to_fscache(struct inode *inode, struct page *page)
  322. {
  323. int ret;
  324. const struct v9fs_inode *v9inode = V9FS_I(inode);
  325. p9_debug(P9_DEBUG_FSC, "inode %p page %p\n", inode, page);
  326. ret = fscache_write_page(v9inode->fscache, page, GFP_KERNEL);
  327. p9_debug(P9_DEBUG_FSC, "ret = %d\n", ret);
  328. if (ret != 0)
  329. v9fs_uncache_page(inode, page);
  330. }
  331. /*
  332. * wait for a page to complete writing to the cache
  333. */
  334. void __v9fs_fscache_wait_on_page_write(struct inode *inode, struct page *page)
  335. {
  336. const struct v9fs_inode *v9inode = V9FS_I(inode);
  337. p9_debug(P9_DEBUG_FSC, "inode %p page %p\n", inode, page);
  338. if (PageFsCache(page))
  339. fscache_wait_on_page_write(v9inode->fscache, page);
  340. }