cache.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 + snaphot) */
  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. done:
  167. mutex_unlock(&inode->i_mutex);
  168. }
  169. void ceph_fscache_unregister_inode_cookie(struct ceph_inode_info* ci)
  170. {
  171. struct fscache_cookie* cookie;
  172. if ((cookie = ci->fscache) == NULL)
  173. return;
  174. ci->fscache = NULL;
  175. fscache_uncache_all_inode_pages(cookie, &ci->vfs_inode);
  176. fscache_relinquish_cookie(cookie, 0);
  177. }
  178. static void ceph_vfs_readpage_complete(struct page *page, void *data, int error)
  179. {
  180. if (!error)
  181. SetPageUptodate(page);
  182. }
  183. static void ceph_vfs_readpage_complete_unlock(struct page *page, void *data, int error)
  184. {
  185. if (!error)
  186. SetPageUptodate(page);
  187. unlock_page(page);
  188. }
  189. static inline int cache_valid(struct ceph_inode_info *ci)
  190. {
  191. return ((ceph_caps_issued(ci) & CEPH_CAP_FILE_CACHE) &&
  192. (ci->i_fscache_gen == ci->i_rdcache_gen));
  193. }
  194. /* Atempt to read from the fscache,
  195. *
  196. * This function is called from the readpage_nounlock context. DO NOT attempt to
  197. * unlock the page here (or in the callback).
  198. */
  199. int ceph_readpage_from_fscache(struct inode *inode, struct page *page)
  200. {
  201. struct ceph_inode_info *ci = ceph_inode(inode);
  202. int ret;
  203. if (!cache_valid(ci))
  204. return -ENOBUFS;
  205. ret = fscache_read_or_alloc_page(ci->fscache, page,
  206. ceph_vfs_readpage_complete, NULL,
  207. GFP_KERNEL);
  208. switch (ret) {
  209. case 0: /* Page found */
  210. dout("page read submitted\n");
  211. return 0;
  212. case -ENOBUFS: /* Pages were not found, and can't be */
  213. case -ENODATA: /* Pages were not found */
  214. dout("page/inode not in cache\n");
  215. return ret;
  216. default:
  217. dout("%s: unknown error ret = %i\n", __func__, ret);
  218. return ret;
  219. }
  220. }
  221. int ceph_readpages_from_fscache(struct inode *inode,
  222. struct address_space *mapping,
  223. struct list_head *pages,
  224. unsigned *nr_pages)
  225. {
  226. struct ceph_inode_info *ci = ceph_inode(inode);
  227. int ret;
  228. if (!cache_valid(ci))
  229. return -ENOBUFS;
  230. ret = fscache_read_or_alloc_pages(ci->fscache, mapping, pages, nr_pages,
  231. ceph_vfs_readpage_complete_unlock,
  232. NULL, mapping_gfp_mask(mapping));
  233. switch (ret) {
  234. case 0: /* All pages found */
  235. dout("all-page read submitted\n");
  236. return 0;
  237. case -ENOBUFS: /* Some pages were not found, and can't be */
  238. case -ENODATA: /* some pages were not found */
  239. dout("page/inode not in cache\n");
  240. return ret;
  241. default:
  242. dout("%s: unknown error ret = %i\n", __func__, ret);
  243. return ret;
  244. }
  245. }
  246. void ceph_readpage_to_fscache(struct inode *inode, struct page *page)
  247. {
  248. struct ceph_inode_info *ci = ceph_inode(inode);
  249. int ret;
  250. if (!PageFsCache(page))
  251. return;
  252. if (!cache_valid(ci))
  253. return;
  254. ret = fscache_write_page(ci->fscache, page, GFP_KERNEL);
  255. if (ret)
  256. fscache_uncache_page(ci->fscache, page);
  257. }
  258. void ceph_invalidate_fscache_page(struct inode* inode, struct page *page)
  259. {
  260. struct ceph_inode_info *ci = ceph_inode(inode);
  261. fscache_wait_on_page_write(ci->fscache, page);
  262. fscache_uncache_page(ci->fscache, page);
  263. }
  264. void ceph_fscache_unregister_fs(struct ceph_fs_client* fsc)
  265. {
  266. if (fsc->revalidate_wq)
  267. destroy_workqueue(fsc->revalidate_wq);
  268. fscache_relinquish_cookie(fsc->fscache, 0);
  269. fsc->fscache = NULL;
  270. }
  271. static void ceph_revalidate_work(struct work_struct *work)
  272. {
  273. int issued;
  274. u32 orig_gen;
  275. struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
  276. i_revalidate_work);
  277. struct inode *inode = &ci->vfs_inode;
  278. spin_lock(&ci->i_ceph_lock);
  279. issued = __ceph_caps_issued(ci, NULL);
  280. orig_gen = ci->i_rdcache_gen;
  281. spin_unlock(&ci->i_ceph_lock);
  282. if (!(issued & CEPH_CAP_FILE_CACHE)) {
  283. dout("revalidate_work lost cache before validation %p\n",
  284. inode);
  285. goto out;
  286. }
  287. if (!fscache_check_consistency(ci->fscache))
  288. fscache_invalidate(ci->fscache);
  289. spin_lock(&ci->i_ceph_lock);
  290. /* Update the new valid generation (backwards sanity check too) */
  291. if (orig_gen > ci->i_fscache_gen) {
  292. ci->i_fscache_gen = orig_gen;
  293. }
  294. spin_unlock(&ci->i_ceph_lock);
  295. out:
  296. iput(&ci->vfs_inode);
  297. }
  298. void ceph_queue_revalidate(struct inode *inode)
  299. {
  300. struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
  301. struct ceph_inode_info *ci = ceph_inode(inode);
  302. if (fsc->revalidate_wq == NULL || ci->fscache == NULL)
  303. return;
  304. ihold(inode);
  305. if (queue_work(ceph_sb_to_client(inode->i_sb)->revalidate_wq,
  306. &ci->i_revalidate_work)) {
  307. dout("ceph_queue_revalidate %p\n", inode);
  308. } else {
  309. dout("ceph_queue_revalidate %p failed\n)", inode);
  310. iput(inode);
  311. }
  312. }
  313. void ceph_fscache_inode_init(struct ceph_inode_info *ci)
  314. {
  315. ci->fscache = NULL;
  316. /* The first load is verifed cookie open time */
  317. ci->i_fscache_gen = 1;
  318. INIT_WORK(&ci->i_revalidate_work, ceph_revalidate_work);
  319. }