cache.c 11 KB

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