cache.c 9.5 KB

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