cache.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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_DPRINTK(P9_DEBUG_FSC, "session %p buf %p size %u", v9ses,
  57. 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_DPRINTK(P9_DEBUG_FSC, "cache session tag %s", 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);
  79. P9_DPRINTK(P9_DEBUG_FSC, "session %p get cookie %p", v9ses,
  80. v9ses->fscache);
  81. }
  82. void v9fs_cache_session_put_cookie(struct v9fs_session_info *v9ses)
  83. {
  84. P9_DPRINTK(P9_DEBUG_FSC, "session %p put cookie %p", v9ses,
  85. 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_DPRINTK(P9_DEBUG_FSC, "inode %p get key %llu", &v9inode->vfs_inode,
  95. 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_DPRINTK(P9_DEBUG_FSC, "inode %p get attr %llu", &v9inode->vfs_inode,
  104. *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_DPRINTK(P9_DEBUG_FSC, "inode %p get aux %u", &v9inode->vfs_inode,
  112. 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);
  172. P9_DPRINTK(P9_DEBUG_FSC, "inode %p get cookie %p", inode,
  173. 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_DPRINTK(P9_DEBUG_FSC, "inode %p put cookie %p", inode,
  181. 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_DPRINTK(P9_DEBUG_FSC, "inode %p flush cookie %p", inode,
  191. 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. struct p9_fid *fid;
  199. if (!v9inode->fscache)
  200. return;
  201. spin_lock(&v9inode->fscache_lock);
  202. fid = filp->private_data;
  203. if ((filp->f_flags & O_ACCMODE) != O_RDONLY)
  204. v9fs_cache_inode_flush_cookie(inode);
  205. else
  206. v9fs_cache_inode_get_cookie(inode);
  207. spin_unlock(&v9inode->fscache_lock);
  208. }
  209. void v9fs_cache_inode_reset_cookie(struct inode *inode)
  210. {
  211. struct v9fs_inode *v9inode = V9FS_I(inode);
  212. struct v9fs_session_info *v9ses;
  213. struct fscache_cookie *old;
  214. if (!v9inode->fscache)
  215. return;
  216. old = v9inode->fscache;
  217. spin_lock(&v9inode->fscache_lock);
  218. fscache_relinquish_cookie(v9inode->fscache, 1);
  219. v9ses = v9fs_inode2v9ses(inode);
  220. v9inode->fscache = fscache_acquire_cookie(v9ses->fscache,
  221. &v9fs_cache_inode_index_def,
  222. v9inode);
  223. P9_DPRINTK(P9_DEBUG_FSC, "inode %p revalidating cookie old %p new %p",
  224. inode, old, v9inode->fscache);
  225. spin_unlock(&v9inode->fscache_lock);
  226. }
  227. int __v9fs_fscache_release_page(struct page *page, gfp_t gfp)
  228. {
  229. struct inode *inode = page->mapping->host;
  230. struct v9fs_inode *v9inode = V9FS_I(inode);
  231. BUG_ON(!v9inode->fscache);
  232. return fscache_maybe_release_page(v9inode->fscache, page, gfp);
  233. }
  234. void __v9fs_fscache_invalidate_page(struct page *page)
  235. {
  236. struct inode *inode = page->mapping->host;
  237. struct v9fs_inode *v9inode = V9FS_I(inode);
  238. BUG_ON(!v9inode->fscache);
  239. if (PageFsCache(page)) {
  240. fscache_wait_on_page_write(v9inode->fscache, page);
  241. BUG_ON(!PageLocked(page));
  242. fscache_uncache_page(v9inode->fscache, page);
  243. }
  244. }
  245. static void v9fs_vfs_readpage_complete(struct page *page, void *data,
  246. int error)
  247. {
  248. if (!error)
  249. SetPageUptodate(page);
  250. unlock_page(page);
  251. }
  252. /**
  253. * __v9fs_readpage_from_fscache - read a page from cache
  254. *
  255. * Returns 0 if the pages are in cache and a BIO is submitted,
  256. * 1 if the pages are not in cache and -error otherwise.
  257. */
  258. int __v9fs_readpage_from_fscache(struct inode *inode, struct page *page)
  259. {
  260. int ret;
  261. const struct v9fs_inode *v9inode = V9FS_I(inode);
  262. P9_DPRINTK(P9_DEBUG_FSC, "inode %p page %p", inode, page);
  263. if (!v9inode->fscache)
  264. return -ENOBUFS;
  265. ret = fscache_read_or_alloc_page(v9inode->fscache,
  266. page,
  267. v9fs_vfs_readpage_complete,
  268. NULL,
  269. GFP_KERNEL);
  270. switch (ret) {
  271. case -ENOBUFS:
  272. case -ENODATA:
  273. P9_DPRINTK(P9_DEBUG_FSC, "page/inode not in cache %d", ret);
  274. return 1;
  275. case 0:
  276. P9_DPRINTK(P9_DEBUG_FSC, "BIO submitted");
  277. return ret;
  278. default:
  279. P9_DPRINTK(P9_DEBUG_FSC, "ret %d", ret);
  280. return ret;
  281. }
  282. }
  283. /**
  284. * __v9fs_readpages_from_fscache - read multiple pages from cache
  285. *
  286. * Returns 0 if the pages are in cache and a BIO is submitted,
  287. * 1 if the pages are not in cache and -error otherwise.
  288. */
  289. int __v9fs_readpages_from_fscache(struct inode *inode,
  290. struct address_space *mapping,
  291. struct list_head *pages,
  292. unsigned *nr_pages)
  293. {
  294. int ret;
  295. const struct v9fs_inode *v9inode = V9FS_I(inode);
  296. P9_DPRINTK(P9_DEBUG_FSC, "inode %p pages %u", inode, *nr_pages);
  297. if (!v9inode->fscache)
  298. return -ENOBUFS;
  299. ret = fscache_read_or_alloc_pages(v9inode->fscache,
  300. mapping, pages, nr_pages,
  301. v9fs_vfs_readpage_complete,
  302. NULL,
  303. mapping_gfp_mask(mapping));
  304. switch (ret) {
  305. case -ENOBUFS:
  306. case -ENODATA:
  307. P9_DPRINTK(P9_DEBUG_FSC, "pages/inodes not in cache %d", ret);
  308. return 1;
  309. case 0:
  310. BUG_ON(!list_empty(pages));
  311. BUG_ON(*nr_pages != 0);
  312. P9_DPRINTK(P9_DEBUG_FSC, "BIO submitted");
  313. return ret;
  314. default:
  315. P9_DPRINTK(P9_DEBUG_FSC, "ret %d", ret);
  316. return ret;
  317. }
  318. }
  319. /**
  320. * __v9fs_readpage_to_fscache - write a page to the cache
  321. *
  322. */
  323. void __v9fs_readpage_to_fscache(struct inode *inode, struct page *page)
  324. {
  325. int ret;
  326. const struct v9fs_inode *v9inode = V9FS_I(inode);
  327. P9_DPRINTK(P9_DEBUG_FSC, "inode %p page %p", inode, page);
  328. ret = fscache_write_page(v9inode->fscache, page, GFP_KERNEL);
  329. P9_DPRINTK(P9_DEBUG_FSC, "ret = %d", ret);
  330. if (ret != 0)
  331. v9fs_uncache_page(inode, page);
  332. }
  333. /*
  334. * wait for a page to complete writing to the cache
  335. */
  336. void __v9fs_fscache_wait_on_page_write(struct inode *inode, struct page *page)
  337. {
  338. const struct v9fs_inode *v9inode = V9FS_I(inode);
  339. P9_DPRINTK(P9_DEBUG_FSC, "inode %p page %p", inode, page);
  340. if (PageFsCache(page))
  341. fscache_wait_on_page_write(v9inode->fscache, page);
  342. }