cache.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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/stat.h>
  25. #include <linux/sched.h>
  26. #include <linux/fs.h>
  27. #include <net/9p/9p.h>
  28. #include "v9fs.h"
  29. #include "cache.h"
  30. #define CACHETAG_LEN 11
  31. struct kmem_cache *vcookie_cache;
  32. struct fscache_netfs v9fs_cache_netfs = {
  33. .name = "9p",
  34. .version = 0,
  35. };
  36. static void init_once(void *foo)
  37. {
  38. struct v9fs_cookie *vcookie = (struct v9fs_cookie *) foo;
  39. vcookie->fscache = NULL;
  40. vcookie->qid = NULL;
  41. inode_init_once(&vcookie->inode);
  42. }
  43. /**
  44. * v9fs_init_vcookiecache - initialize a cache for vcookies to maintain
  45. * vcookie to inode mapping
  46. *
  47. * Returns 0 on success.
  48. */
  49. static int v9fs_init_vcookiecache(void)
  50. {
  51. vcookie_cache = kmem_cache_create("vcookie_cache",
  52. sizeof(struct v9fs_cookie),
  53. 0, (SLAB_RECLAIM_ACCOUNT|
  54. SLAB_MEM_SPREAD),
  55. init_once);
  56. if (!vcookie_cache)
  57. return -ENOMEM;
  58. return 0;
  59. }
  60. /**
  61. * v9fs_destroy_vcookiecache - destroy the cache of vcookies
  62. *
  63. */
  64. static void v9fs_destroy_vcookiecache(void)
  65. {
  66. kmem_cache_destroy(vcookie_cache);
  67. }
  68. int __v9fs_cache_register(void)
  69. {
  70. int ret;
  71. ret = v9fs_init_vcookiecache();
  72. if (ret < 0)
  73. return ret;
  74. return fscache_register_netfs(&v9fs_cache_netfs);
  75. }
  76. void __v9fs_cache_unregister(void)
  77. {
  78. v9fs_destroy_vcookiecache();
  79. fscache_unregister_netfs(&v9fs_cache_netfs);
  80. }
  81. /**
  82. * v9fs_random_cachetag - Generate a random tag to be associated
  83. * with a new cache session.
  84. *
  85. * The value of jiffies is used for a fairly randomly cache tag.
  86. */
  87. static
  88. int v9fs_random_cachetag(struct v9fs_session_info *v9ses)
  89. {
  90. v9ses->cachetag = kmalloc(CACHETAG_LEN, GFP_KERNEL);
  91. if (!v9ses->cachetag)
  92. return -ENOMEM;
  93. return scnprintf(v9ses->cachetag, CACHETAG_LEN, "%lu", jiffies);
  94. }
  95. static uint16_t v9fs_cache_session_get_key(const void *cookie_netfs_data,
  96. void *buffer, uint16_t bufmax)
  97. {
  98. struct v9fs_session_info *v9ses;
  99. uint16_t klen = 0;
  100. v9ses = (struct v9fs_session_info *)cookie_netfs_data;
  101. P9_DPRINTK(P9_DEBUG_FSC, "session %p buf %p size %u", v9ses,
  102. buffer, bufmax);
  103. if (v9ses->cachetag)
  104. klen = strlen(v9ses->cachetag);
  105. if (klen > bufmax)
  106. return 0;
  107. memcpy(buffer, v9ses->cachetag, klen);
  108. P9_DPRINTK(P9_DEBUG_FSC, "cache session tag %s", v9ses->cachetag);
  109. return klen;
  110. }
  111. const struct fscache_cookie_def v9fs_cache_session_index_def = {
  112. .name = "9P.session",
  113. .type = FSCACHE_COOKIE_TYPE_INDEX,
  114. .get_key = v9fs_cache_session_get_key,
  115. };
  116. void v9fs_cache_session_get_cookie(struct v9fs_session_info *v9ses)
  117. {
  118. /* If no cache session tag was specified, we generate a random one. */
  119. if (!v9ses->cachetag)
  120. v9fs_random_cachetag(v9ses);
  121. v9ses->fscache = fscache_acquire_cookie(v9fs_cache_netfs.primary_index,
  122. &v9fs_cache_session_index_def,
  123. v9ses);
  124. P9_DPRINTK(P9_DEBUG_FSC, "session %p get cookie %p", v9ses,
  125. v9ses->fscache);
  126. }
  127. void v9fs_cache_session_put_cookie(struct v9fs_session_info *v9ses)
  128. {
  129. P9_DPRINTK(P9_DEBUG_FSC, "session %p put cookie %p", v9ses,
  130. v9ses->fscache);
  131. fscache_relinquish_cookie(v9ses->fscache, 0);
  132. v9ses->fscache = NULL;
  133. }
  134. static uint16_t v9fs_cache_inode_get_key(const void *cookie_netfs_data,
  135. void *buffer, uint16_t bufmax)
  136. {
  137. const struct v9fs_cookie *vcookie = cookie_netfs_data;
  138. memcpy(buffer, &vcookie->qid->path, sizeof(vcookie->qid->path));
  139. P9_DPRINTK(P9_DEBUG_FSC, "inode %p get key %llu", &vcookie->inode,
  140. vcookie->qid->path);
  141. return sizeof(vcookie->qid->path);
  142. }
  143. static void v9fs_cache_inode_get_attr(const void *cookie_netfs_data,
  144. uint64_t *size)
  145. {
  146. const struct v9fs_cookie *vcookie = cookie_netfs_data;
  147. *size = i_size_read(&vcookie->inode);
  148. P9_DPRINTK(P9_DEBUG_FSC, "inode %p get attr %llu", &vcookie->inode,
  149. *size);
  150. }
  151. static uint16_t v9fs_cache_inode_get_aux(const void *cookie_netfs_data,
  152. void *buffer, uint16_t buflen)
  153. {
  154. const struct v9fs_cookie *vcookie = cookie_netfs_data;
  155. memcpy(buffer, &vcookie->qid->version, sizeof(vcookie->qid->version));
  156. P9_DPRINTK(P9_DEBUG_FSC, "inode %p get aux %u", &vcookie->inode,
  157. vcookie->qid->version);
  158. return sizeof(vcookie->qid->version);
  159. }
  160. static enum
  161. fscache_checkaux v9fs_cache_inode_check_aux(void *cookie_netfs_data,
  162. const void *buffer,
  163. uint16_t buflen)
  164. {
  165. const struct v9fs_cookie *vcookie = cookie_netfs_data;
  166. if (buflen != sizeof(vcookie->qid->version))
  167. return FSCACHE_CHECKAUX_OBSOLETE;
  168. if (memcmp(buffer, &vcookie->qid->version,
  169. sizeof(vcookie->qid->version)))
  170. return FSCACHE_CHECKAUX_OBSOLETE;
  171. return FSCACHE_CHECKAUX_OKAY;
  172. }
  173. static void v9fs_cache_inode_now_uncached(void *cookie_netfs_data)
  174. {
  175. struct v9fs_cookie *vcookie = cookie_netfs_data;
  176. struct pagevec pvec;
  177. pgoff_t first;
  178. int loop, nr_pages;
  179. pagevec_init(&pvec, 0);
  180. first = 0;
  181. for (;;) {
  182. nr_pages = pagevec_lookup(&pvec, vcookie->inode.i_mapping,
  183. first,
  184. PAGEVEC_SIZE - pagevec_count(&pvec));
  185. if (!nr_pages)
  186. break;
  187. for (loop = 0; loop < nr_pages; loop++)
  188. ClearPageFsCache(pvec.pages[loop]);
  189. first = pvec.pages[nr_pages - 1]->index + 1;
  190. pvec.nr = nr_pages;
  191. pagevec_release(&pvec);
  192. cond_resched();
  193. }
  194. }
  195. const struct fscache_cookie_def v9fs_cache_inode_index_def = {
  196. .name = "9p.inode",
  197. .type = FSCACHE_COOKIE_TYPE_DATAFILE,
  198. .get_key = v9fs_cache_inode_get_key,
  199. .get_attr = v9fs_cache_inode_get_attr,
  200. .get_aux = v9fs_cache_inode_get_aux,
  201. .check_aux = v9fs_cache_inode_check_aux,
  202. .now_uncached = v9fs_cache_inode_now_uncached,
  203. };
  204. void v9fs_cache_inode_get_cookie(struct inode *inode)
  205. {
  206. struct v9fs_cookie *vcookie;
  207. struct v9fs_session_info *v9ses;
  208. if (!S_ISREG(inode->i_mode))
  209. return;
  210. vcookie = v9fs_inode2cookie(inode);
  211. if (vcookie->fscache)
  212. return;
  213. v9ses = v9fs_inode2v9ses(inode);
  214. vcookie->fscache = fscache_acquire_cookie(v9ses->fscache,
  215. &v9fs_cache_inode_index_def,
  216. vcookie);
  217. P9_DPRINTK(P9_DEBUG_FSC, "inode %p get cookie %p", inode,
  218. vcookie->fscache);
  219. }
  220. void v9fs_cache_inode_put_cookie(struct inode *inode)
  221. {
  222. struct v9fs_cookie *vcookie = v9fs_inode2cookie(inode);
  223. if (!vcookie->fscache)
  224. return;
  225. P9_DPRINTK(P9_DEBUG_FSC, "inode %p put cookie %p", inode,
  226. vcookie->fscache);
  227. fscache_relinquish_cookie(vcookie->fscache, 0);
  228. vcookie->fscache = NULL;
  229. }
  230. void v9fs_cache_inode_flush_cookie(struct inode *inode)
  231. {
  232. struct v9fs_cookie *vcookie = v9fs_inode2cookie(inode);
  233. if (!vcookie->fscache)
  234. return;
  235. P9_DPRINTK(P9_DEBUG_FSC, "inode %p flush cookie %p", inode,
  236. vcookie->fscache);
  237. fscache_relinquish_cookie(vcookie->fscache, 1);
  238. vcookie->fscache = NULL;
  239. }
  240. void v9fs_cache_inode_set_cookie(struct inode *inode, struct file *filp)
  241. {
  242. struct v9fs_cookie *vcookie = v9fs_inode2cookie(inode);
  243. struct p9_fid *fid;
  244. if (!vcookie->fscache)
  245. return;
  246. spin_lock(&vcookie->lock);
  247. fid = filp->private_data;
  248. if ((filp->f_flags & O_ACCMODE) != O_RDONLY)
  249. v9fs_cache_inode_flush_cookie(inode);
  250. else
  251. v9fs_cache_inode_get_cookie(inode);
  252. spin_unlock(&vcookie->lock);
  253. }
  254. void v9fs_cache_inode_reset_cookie(struct inode *inode)
  255. {
  256. struct v9fs_cookie *vcookie = v9fs_inode2cookie(inode);
  257. struct v9fs_session_info *v9ses;
  258. struct fscache_cookie *old;
  259. if (!vcookie->fscache)
  260. return;
  261. old = vcookie->fscache;
  262. spin_lock(&vcookie->lock);
  263. fscache_relinquish_cookie(vcookie->fscache, 1);
  264. v9ses = v9fs_inode2v9ses(inode);
  265. vcookie->fscache = fscache_acquire_cookie(v9ses->fscache,
  266. &v9fs_cache_inode_index_def,
  267. vcookie);
  268. P9_DPRINTK(P9_DEBUG_FSC, "inode %p revalidating cookie old %p new %p",
  269. inode, old, vcookie->fscache);
  270. spin_unlock(&vcookie->lock);
  271. }
  272. int __v9fs_fscache_release_page(struct page *page, gfp_t gfp)
  273. {
  274. struct inode *inode = page->mapping->host;
  275. struct v9fs_cookie *vcookie = v9fs_inode2cookie(inode);
  276. BUG_ON(!vcookie->fscache);
  277. return fscache_maybe_release_page(vnode->cache, page, gfp);
  278. }
  279. void __v9fs_fscache_invalidate_page(struct page *page)
  280. {
  281. struct inode *inode = page->mapping->host;
  282. struct v9fs_cookie *vcookie = v9fs_inode2cookie(inode);
  283. BUG_ON(!vcookie->fscache);
  284. if (PageFsCache(page)) {
  285. fscache_wait_on_page_write(vcookie->fscache, page);
  286. BUG_ON(!PageLocked(page));
  287. fscache_uncache_page(vcookie->fscache, page);
  288. }
  289. }
  290. static void v9fs_vfs_readpage_complete(struct page *page, void *data,
  291. int error)
  292. {
  293. if (!error)
  294. SetPageUptodate(page);
  295. unlock_page(page);
  296. }
  297. /**
  298. * __v9fs_readpage_from_fscache - read a page from cache
  299. *
  300. * Returns 0 if the pages are in cache and a BIO is submitted,
  301. * 1 if the pages are not in cache and -error otherwise.
  302. */
  303. int __v9fs_readpage_from_fscache(struct inode *inode, struct page *page)
  304. {
  305. int ret;
  306. const struct v9fs_cookie *vcookie = v9fs_inode2cookie(inode);
  307. P9_DPRINTK(P9_DEBUG_FSC, "inode %p page %p", inode, page);
  308. if (!vcookie->fscache)
  309. return -ENOBUFS;
  310. ret = fscache_read_or_alloc_page(vcookie->fscache,
  311. page,
  312. v9fs_vfs_readpage_complete,
  313. NULL,
  314. GFP_KERNEL);
  315. switch (ret) {
  316. case -ENOBUFS:
  317. case -ENODATA:
  318. P9_DPRINTK(P9_DEBUG_FSC, "page/inode not in cache %d", ret);
  319. return 1;
  320. case 0:
  321. P9_DPRINTK(P9_DEBUG_FSC, "BIO submitted");
  322. return ret;
  323. default:
  324. P9_DPRINTK(P9_DEBUG_FSC, "ret %d", ret);
  325. return ret;
  326. }
  327. }
  328. /**
  329. * __v9fs_readpages_from_fscache - read multiple pages from cache
  330. *
  331. * Returns 0 if the pages are in cache and a BIO is submitted,
  332. * 1 if the pages are not in cache and -error otherwise.
  333. */
  334. int __v9fs_readpages_from_fscache(struct inode *inode,
  335. struct address_space *mapping,
  336. struct list_head *pages,
  337. unsigned *nr_pages)
  338. {
  339. int ret;
  340. const struct v9fs_cookie *vcookie = v9fs_inode2cookie(inode);
  341. P9_DPRINTK(P9_DEBUG_FSC, "inode %p pages %u", inode, *nr_pages);
  342. if (!vcookie->fscache)
  343. return -ENOBUFS;
  344. ret = fscache_read_or_alloc_pages(vcookie->fscache,
  345. mapping, pages, nr_pages,
  346. v9fs_vfs_readpage_complete,
  347. NULL,
  348. mapping_gfp_mask(mapping));
  349. switch (ret) {
  350. case -ENOBUFS:
  351. case -ENODATA:
  352. P9_DPRINTK(P9_DEBUG_FSC, "pages/inodes not in cache %d", ret);
  353. return 1;
  354. case 0:
  355. BUG_ON(!list_empty(pages));
  356. BUG_ON(*nr_pages != 0);
  357. P9_DPRINTK(P9_DEBUG_FSC, "BIO submitted");
  358. return ret;
  359. default:
  360. P9_DPRINTK(P9_DEBUG_FSC, "ret %d", ret);
  361. return ret;
  362. }
  363. }
  364. /**
  365. * __v9fs_readpage_to_fscache - write a page to the cache
  366. *
  367. */
  368. void __v9fs_readpage_to_fscache(struct inode *inode, struct page *page)
  369. {
  370. int ret;
  371. const struct v9fs_cookie *vcookie = v9fs_inode2cookie(inode);
  372. P9_DPRINTK(P9_DEBUG_FSC, "inode %p page %p", inode, page);
  373. ret = fscache_write_page(vcookie->fscache, page, GFP_KERNEL);
  374. P9_DPRINTK(P9_DEBUG_FSC, "ret = %d", ret);
  375. if (ret != 0)
  376. v9fs_uncache_page(inode, page);
  377. }