cache.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * Ceph cache definitions.
  3. *
  4. * Copyright (C) 2013 by Adfin Solutions, Inc. All Rights Reserved.
  5. * Written by Milosz Tanski (milosz@adfin.com)
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to:
  18. * Free Software Foundation
  19. * 51 Franklin Street, Fifth Floor
  20. * Boston, MA 02111-1301 USA
  21. *
  22. */
  23. #include "super.h"
  24. #include "cache.h"
  25. struct ceph_aux_inode {
  26. struct timespec mtime;
  27. loff_t size;
  28. };
  29. struct fscache_netfs ceph_cache_netfs = {
  30. .name = "ceph",
  31. .version = 0,
  32. };
  33. static uint16_t ceph_fscache_session_get_key(const void *cookie_netfs_data,
  34. void *buffer, uint16_t maxbuf)
  35. {
  36. const struct ceph_fs_client* fsc = cookie_netfs_data;
  37. uint16_t klen;
  38. klen = sizeof(fsc->client->fsid);
  39. if (klen > maxbuf)
  40. return 0;
  41. memcpy(buffer, &fsc->client->fsid, klen);
  42. return klen;
  43. }
  44. static const struct fscache_cookie_def ceph_fscache_fsid_object_def = {
  45. .name = "CEPH.fsid",
  46. .type = FSCACHE_COOKIE_TYPE_INDEX,
  47. .get_key = ceph_fscache_session_get_key,
  48. };
  49. int ceph_fscache_register(void)
  50. {
  51. return fscache_register_netfs(&ceph_cache_netfs);
  52. }
  53. void ceph_fscache_unregister(void)
  54. {
  55. fscache_unregister_netfs(&ceph_cache_netfs);
  56. }
  57. int ceph_fscache_register_fs(struct ceph_fs_client* fsc)
  58. {
  59. fsc->fscache = fscache_acquire_cookie(ceph_cache_netfs.primary_index,
  60. &ceph_fscache_fsid_object_def,
  61. fsc, true);
  62. if (fsc->fscache == NULL) {
  63. pr_err("Unable to resgister fsid: %p fscache cookie", fsc);
  64. return 0;
  65. }
  66. fsc->revalidate_wq = alloc_workqueue("ceph-revalidate", 0, 1);
  67. if (fsc->revalidate_wq == NULL)
  68. return -ENOMEM;
  69. return 0;
  70. }
  71. static uint16_t ceph_fscache_inode_get_key(const void *cookie_netfs_data,
  72. void *buffer, uint16_t maxbuf)
  73. {
  74. const struct ceph_inode_info* ci = cookie_netfs_data;
  75. uint16_t klen;
  76. /* use ceph virtual inode (id + snapshot) */
  77. klen = sizeof(ci->i_vino);
  78. if (klen > maxbuf)
  79. return 0;
  80. memcpy(buffer, &ci->i_vino, klen);
  81. return klen;
  82. }
  83. static uint16_t ceph_fscache_inode_get_aux(const void *cookie_netfs_data,
  84. void *buffer, uint16_t bufmax)
  85. {
  86. struct ceph_aux_inode aux;
  87. const struct ceph_inode_info* ci = cookie_netfs_data;
  88. const struct inode* inode = &ci->vfs_inode;
  89. memset(&aux, 0, sizeof(aux));
  90. aux.mtime = inode->i_mtime;
  91. aux.size = inode->i_size;
  92. memcpy(buffer, &aux, sizeof(aux));
  93. return sizeof(aux);
  94. }
  95. static void ceph_fscache_inode_get_attr(const void *cookie_netfs_data,
  96. uint64_t *size)
  97. {
  98. const struct ceph_inode_info* ci = cookie_netfs_data;
  99. const struct inode* inode = &ci->vfs_inode;
  100. *size = inode->i_size;
  101. }
  102. static enum fscache_checkaux ceph_fscache_inode_check_aux(
  103. void *cookie_netfs_data, const void *data, uint16_t dlen)
  104. {
  105. struct ceph_aux_inode aux;
  106. struct ceph_inode_info* ci = cookie_netfs_data;
  107. struct inode* inode = &ci->vfs_inode;
  108. if (dlen != sizeof(aux))
  109. return FSCACHE_CHECKAUX_OBSOLETE;
  110. memset(&aux, 0, sizeof(aux));
  111. aux.mtime = inode->i_mtime;
  112. aux.size = inode->i_size;
  113. if (memcmp(data, &aux, sizeof(aux)) != 0)
  114. return FSCACHE_CHECKAUX_OBSOLETE;
  115. dout("ceph inode 0x%p cached okay", ci);
  116. return FSCACHE_CHECKAUX_OKAY;
  117. }
  118. static void ceph_fscache_inode_now_uncached(void* cookie_netfs_data)
  119. {
  120. struct ceph_inode_info* ci = cookie_netfs_data;
  121. struct pagevec pvec;
  122. pgoff_t first;
  123. int loop, nr_pages;
  124. pagevec_init(&pvec, 0);
  125. first = 0;
  126. dout("ceph inode 0x%p now uncached", ci);
  127. while (1) {
  128. nr_pages = pagevec_lookup(&pvec, ci->vfs_inode.i_mapping, first,
  129. PAGEVEC_SIZE - pagevec_count(&pvec));
  130. if (!nr_pages)
  131. break;
  132. for (loop = 0; loop < nr_pages; loop++)
  133. ClearPageFsCache(pvec.pages[loop]);
  134. first = pvec.pages[nr_pages - 1]->index + 1;
  135. pvec.nr = nr_pages;
  136. pagevec_release(&pvec);
  137. cond_resched();
  138. }
  139. }
  140. static const struct fscache_cookie_def ceph_fscache_inode_object_def = {
  141. .name = "CEPH.inode",
  142. .type = FSCACHE_COOKIE_TYPE_DATAFILE,
  143. .get_key = ceph_fscache_inode_get_key,
  144. .get_attr = ceph_fscache_inode_get_attr,
  145. .get_aux = ceph_fscache_inode_get_aux,
  146. .check_aux = ceph_fscache_inode_check_aux,
  147. .now_uncached = ceph_fscache_inode_now_uncached,
  148. };
  149. void ceph_fscache_register_inode_cookie(struct ceph_fs_client* fsc,
  150. struct ceph_inode_info* ci)
  151. {
  152. struct inode* inode = &ci->vfs_inode;
  153. /* No caching for filesystem */
  154. if (fsc->fscache == NULL)
  155. return;
  156. /* Only cache for regular files that are read only */
  157. if ((ci->vfs_inode.i_mode & S_IFREG) == 0)
  158. return;
  159. /* Avoid multiple racing open requests */
  160. mutex_lock(&inode->i_mutex);
  161. if (ci->fscache)
  162. goto done;
  163. ci->fscache = fscache_acquire_cookie(fsc->fscache,
  164. &ceph_fscache_inode_object_def,
  165. ci, true);
  166. fscache_check_consistency(ci->fscache);
  167. done:
  168. mutex_unlock(&inode->i_mutex);
  169. }
  170. void ceph_fscache_unregister_inode_cookie(struct ceph_inode_info* ci)
  171. {
  172. struct fscache_cookie* cookie;
  173. if ((cookie = ci->fscache) == NULL)
  174. return;
  175. ci->fscache = NULL;
  176. fscache_uncache_all_inode_pages(cookie, &ci->vfs_inode);
  177. fscache_relinquish_cookie(cookie, 0);
  178. }
  179. static void ceph_readpage_from_fscache_complete(struct page *page, void *data, int error)
  180. {
  181. if (!error)
  182. SetPageUptodate(page);
  183. unlock_page(page);
  184. }
  185. static inline int cache_valid(struct ceph_inode_info *ci)
  186. {
  187. return ((ceph_caps_issued(ci) & CEPH_CAP_FILE_CACHE) &&
  188. (ci->i_fscache_gen == ci->i_rdcache_gen));
  189. }
  190. /* Atempt to read from the fscache,
  191. *
  192. * This function is called from the readpage_nounlock context. DO NOT attempt to
  193. * unlock the page here (or in the callback).
  194. */
  195. int ceph_readpage_from_fscache(struct inode *inode, struct page *page)
  196. {
  197. struct ceph_inode_info *ci = ceph_inode(inode);
  198. int ret;
  199. if (!cache_valid(ci))
  200. return -ENOBUFS;
  201. ret = fscache_read_or_alloc_page(ci->fscache, page,
  202. ceph_readpage_from_fscache_complete, NULL,
  203. GFP_KERNEL);
  204. switch (ret) {
  205. case 0: /* Page found */
  206. dout("page read submitted\n");
  207. return 0;
  208. case -ENOBUFS: /* Pages were not found, and can't be */
  209. case -ENODATA: /* Pages were not found */
  210. dout("page/inode not in cache\n");
  211. return ret;
  212. default:
  213. dout("%s: unknown error ret = %i\n", __func__, ret);
  214. return ret;
  215. }
  216. }
  217. int ceph_readpages_from_fscache(struct inode *inode,
  218. struct address_space *mapping,
  219. struct list_head *pages,
  220. unsigned *nr_pages)
  221. {
  222. struct ceph_inode_info *ci = ceph_inode(inode);
  223. int ret;
  224. if (!cache_valid(ci))
  225. return -ENOBUFS;
  226. ret = fscache_read_or_alloc_pages(ci->fscache, mapping, pages, nr_pages,
  227. ceph_readpage_from_fscache_complete,
  228. NULL, mapping_gfp_mask(mapping));
  229. switch (ret) {
  230. case 0: /* All pages found */
  231. dout("all-page read submitted\n");
  232. return 0;
  233. case -ENOBUFS: /* Some pages were not found, and can't be */
  234. case -ENODATA: /* some pages were not found */
  235. dout("page/inode not in cache\n");
  236. return ret;
  237. default:
  238. dout("%s: unknown error ret = %i\n", __func__, ret);
  239. return ret;
  240. }
  241. }
  242. void ceph_readpage_to_fscache(struct inode *inode, struct page *page)
  243. {
  244. struct ceph_inode_info *ci = ceph_inode(inode);
  245. int ret;
  246. if (!PageFsCache(page))
  247. return;
  248. if (!cache_valid(ci))
  249. return;
  250. ret = fscache_write_page(ci->fscache, page, GFP_KERNEL);
  251. if (ret)
  252. fscache_uncache_page(ci->fscache, page);
  253. }
  254. void ceph_invalidate_fscache_page(struct inode* inode, struct page *page)
  255. {
  256. struct ceph_inode_info *ci = ceph_inode(inode);
  257. if (!PageFsCache(page))
  258. return;
  259. fscache_wait_on_page_write(ci->fscache, page);
  260. fscache_uncache_page(ci->fscache, page);
  261. }
  262. void ceph_fscache_unregister_fs(struct ceph_fs_client* fsc)
  263. {
  264. if (fsc->revalidate_wq)
  265. destroy_workqueue(fsc->revalidate_wq);
  266. fscache_relinquish_cookie(fsc->fscache, 0);
  267. fsc->fscache = NULL;
  268. }
  269. static void ceph_revalidate_work(struct work_struct *work)
  270. {
  271. int issued;
  272. u32 orig_gen;
  273. struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
  274. i_revalidate_work);
  275. struct inode *inode = &ci->vfs_inode;
  276. spin_lock(&ci->i_ceph_lock);
  277. issued = __ceph_caps_issued(ci, NULL);
  278. orig_gen = ci->i_rdcache_gen;
  279. spin_unlock(&ci->i_ceph_lock);
  280. if (!(issued & CEPH_CAP_FILE_CACHE)) {
  281. dout("revalidate_work lost cache before validation %p\n",
  282. inode);
  283. goto out;
  284. }
  285. if (!fscache_check_consistency(ci->fscache))
  286. fscache_invalidate(ci->fscache);
  287. spin_lock(&ci->i_ceph_lock);
  288. /* Update the new valid generation (backwards sanity check too) */
  289. if (orig_gen > ci->i_fscache_gen) {
  290. ci->i_fscache_gen = orig_gen;
  291. }
  292. spin_unlock(&ci->i_ceph_lock);
  293. out:
  294. iput(&ci->vfs_inode);
  295. }
  296. void ceph_queue_revalidate(struct inode *inode)
  297. {
  298. struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
  299. struct ceph_inode_info *ci = ceph_inode(inode);
  300. if (fsc->revalidate_wq == NULL || ci->fscache == NULL)
  301. return;
  302. ihold(inode);
  303. if (queue_work(ceph_sb_to_client(inode->i_sb)->revalidate_wq,
  304. &ci->i_revalidate_work)) {
  305. dout("ceph_queue_revalidate %p\n", inode);
  306. } else {
  307. dout("ceph_queue_revalidate %p failed\n)", inode);
  308. iput(inode);
  309. }
  310. }
  311. void ceph_fscache_inode_init(struct ceph_inode_info *ci)
  312. {
  313. ci->fscache = NULL;
  314. /* The first load is verifed cookie open time */
  315. ci->i_fscache_gen = 1;
  316. INIT_WORK(&ci->i_revalidate_work, ceph_revalidate_work);
  317. }