cache.c 10 KB

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