cache.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 <linux/fscache.h>
  24. #include "super.h"
  25. #include "cache.h"
  26. struct ceph_aux_inode {
  27. struct timespec mtime;
  28. loff_t size;
  29. };
  30. struct fscache_netfs ceph_cache_netfs = {
  31. .name = "ceph",
  32. .version = 0,
  33. };
  34. static uint16_t ceph_fscache_session_get_key(const void *cookie_netfs_data,
  35. void *buffer, uint16_t maxbuf)
  36. {
  37. const struct ceph_fs_client* fsc = cookie_netfs_data;
  38. uint16_t klen;
  39. klen = sizeof(fsc->client->fsid);
  40. if (klen > maxbuf)
  41. return 0;
  42. memcpy(buffer, &fsc->client->fsid, klen);
  43. return klen;
  44. }
  45. static const struct fscache_cookie_def ceph_fscache_fsid_object_def = {
  46. .name = "CEPH.fsid",
  47. .type = FSCACHE_COOKIE_TYPE_INDEX,
  48. .get_key = ceph_fscache_session_get_key,
  49. };
  50. int ceph_fscache_register()
  51. {
  52. return fscache_register_netfs(&ceph_cache_netfs);
  53. }
  54. void ceph_fscache_unregister()
  55. {
  56. fscache_unregister_netfs(&ceph_cache_netfs);
  57. }
  58. int ceph_fscache_register_fs(struct ceph_fs_client* fsc)
  59. {
  60. fsc->fscache = fscache_acquire_cookie(ceph_cache_netfs.primary_index,
  61. &ceph_fscache_fsid_object_def,
  62. fsc);
  63. if (fsc->fscache == NULL) {
  64. pr_err("Unable to resgister fsid: %p fscache cookie", fsc);
  65. return 0;
  66. }
  67. fsc->revalidate_wq = alloc_workqueue("ceph-revalidate", 0, 1);
  68. if (fsc->revalidate_wq == NULL)
  69. return -ENOMEM;
  70. return 0;
  71. }
  72. static uint16_t ceph_fscache_inode_get_key(const void *cookie_netfs_data,
  73. void *buffer, uint16_t maxbuf)
  74. {
  75. const struct ceph_inode_info* ci = cookie_netfs_data;
  76. uint16_t klen;
  77. /* use ceph virtual inode (id + snaphot) */
  78. klen = sizeof(ci->i_vino);
  79. if (klen > maxbuf)
  80. return 0;
  81. memcpy(buffer, &ci->i_vino, klen);
  82. return klen;
  83. }
  84. static uint16_t ceph_fscache_inode_get_aux(const void *cookie_netfs_data,
  85. void *buffer, uint16_t bufmax)
  86. {
  87. struct ceph_aux_inode aux;
  88. const struct ceph_inode_info* ci = cookie_netfs_data;
  89. const struct inode* inode = &ci->vfs_inode;
  90. memset(&aux, 0, sizeof(aux));
  91. aux.mtime = inode->i_mtime;
  92. aux.size = inode->i_size;
  93. memcpy(buffer, &aux, sizeof(aux));
  94. return sizeof(aux);
  95. }
  96. static void ceph_fscache_inode_get_attr(const void *cookie_netfs_data,
  97. uint64_t *size)
  98. {
  99. const struct ceph_inode_info* ci = cookie_netfs_data;
  100. const struct inode* inode = &ci->vfs_inode;
  101. *size = inode->i_size;
  102. }
  103. static enum fscache_checkaux ceph_fscache_inode_check_aux(
  104. void *cookie_netfs_data, const void *data, uint16_t dlen)
  105. {
  106. struct ceph_aux_inode aux;
  107. struct ceph_inode_info* ci = cookie_netfs_data;
  108. struct inode* inode = &ci->vfs_inode;
  109. if (dlen != sizeof(aux))
  110. return FSCACHE_CHECKAUX_OBSOLETE;
  111. memset(&aux, 0, sizeof(aux));
  112. aux.mtime = inode->i_mtime;
  113. aux.size = inode->i_size;
  114. if (memcmp(data, &aux, sizeof(aux)) != 0)
  115. return FSCACHE_CHECKAUX_OBSOLETE;
  116. dout("ceph inode 0x%p cached okay", ci);
  117. return FSCACHE_CHECKAUX_OKAY;
  118. }
  119. static void ceph_fscache_inode_now_uncached(void* cookie_netfs_data)
  120. {
  121. struct ceph_inode_info* ci = cookie_netfs_data;
  122. struct pagevec pvec;
  123. pgoff_t first;
  124. int loop, nr_pages;
  125. pagevec_init(&pvec, 0);
  126. first = 0;
  127. dout("ceph inode 0x%p now uncached", ci);
  128. while (1) {
  129. nr_pages = pagevec_lookup(&pvec, ci->vfs_inode.i_mapping, first,
  130. PAGEVEC_SIZE - pagevec_count(&pvec));
  131. if (!nr_pages)
  132. break;
  133. for (loop = 0; loop < nr_pages; loop++)
  134. ClearPageFsCache(pvec.pages[loop]);
  135. first = pvec.pages[nr_pages - 1]->index + 1;
  136. pvec.nr = nr_pages;
  137. pagevec_release(&pvec);
  138. cond_resched();
  139. }
  140. }
  141. static const struct fscache_cookie_def ceph_fscache_inode_object_def = {
  142. .name = "CEPH.inode",
  143. .type = FSCACHE_COOKIE_TYPE_DATAFILE,
  144. .get_key = ceph_fscache_inode_get_key,
  145. .get_attr = ceph_fscache_inode_get_attr,
  146. .get_aux = ceph_fscache_inode_get_aux,
  147. .check_aux = ceph_fscache_inode_check_aux,
  148. .now_uncached = ceph_fscache_inode_now_uncached,
  149. };
  150. void ceph_fscache_register_inode_cookie(struct ceph_fs_client* fsc,
  151. struct ceph_inode_info* ci)
  152. {
  153. struct inode* inode = &ci->vfs_inode;
  154. /* No caching for filesystem */
  155. if (fsc->fscache == NULL)
  156. return;
  157. /* Only cache for regular files that are read only */
  158. if ((ci->vfs_inode.i_mode & S_IFREG) == 0)
  159. return;
  160. /* Avoid multiple racing open requests */
  161. mutex_lock(&inode->i_mutex);
  162. if (ci->fscache)
  163. goto done;
  164. ci->fscache = fscache_acquire_cookie(fsc->fscache,
  165. &ceph_fscache_inode_object_def,
  166. ci);
  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 (!cache_valid(ci))
  252. return;
  253. ret = fscache_write_page(ci->fscache, page, GFP_KERNEL);
  254. if (ret)
  255. fscache_uncache_page(ci->fscache, page);
  256. }
  257. void ceph_invalidate_fscache_page(struct inode* inode, struct page *page)
  258. {
  259. struct ceph_inode_info *ci = ceph_inode(inode);
  260. fscache_wait_on_page_write(ci->fscache, page);
  261. fscache_uncache_page(ci->fscache, page);
  262. }
  263. void ceph_fscache_unregister_fs(struct ceph_fs_client* fsc)
  264. {
  265. if (fsc->revalidate_wq)
  266. destroy_workqueue(fsc->revalidate_wq);
  267. fscache_relinquish_cookie(fsc->fscache, 0);
  268. fsc->fscache = NULL;
  269. }
  270. static void ceph_revalidate_work(struct work_struct *work)
  271. {
  272. int issued;
  273. u32 orig_gen;
  274. struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
  275. i_revalidate_work);
  276. struct inode *inode = &ci->vfs_inode;
  277. spin_lock(&ci->i_ceph_lock);
  278. issued = __ceph_caps_issued(ci, NULL);
  279. orig_gen = ci->i_rdcache_gen;
  280. spin_unlock(&ci->i_ceph_lock);
  281. if (!(issued & CEPH_CAP_FILE_CACHE)) {
  282. dout("revalidate_work lost cache before validation %p\n",
  283. inode);
  284. goto out;
  285. }
  286. if (!fscache_check_consistency(ci->fscache))
  287. fscache_invalidate(ci->fscache);
  288. spin_lock(&ci->i_ceph_lock);
  289. /* Update the new valid generation (backwards sanity check too) */
  290. if (orig_gen > ci->i_fscache_gen) {
  291. ci->i_fscache_gen = orig_gen;
  292. }
  293. spin_unlock(&ci->i_ceph_lock);
  294. out:
  295. iput(&ci->vfs_inode);
  296. }
  297. void ceph_queue_revalidate(struct inode *inode)
  298. {
  299. struct ceph_inode_info *ci = ceph_inode(inode);
  300. ihold(inode);
  301. if (queue_work(ceph_sb_to_client(inode->i_sb)->revalidate_wq,
  302. &ci->i_revalidate_work)) {
  303. dout("ceph_queue_revalidate %p\n", inode);
  304. } else {
  305. dout("ceph_queue_revalidate %p failed\n)", inode);
  306. iput(inode);
  307. }
  308. }
  309. void ceph_fscache_inode_init(struct ceph_inode_info *ci)
  310. {
  311. ci->fscache = NULL;
  312. /* The first load is verifed cookie open time */
  313. ci->i_fscache_gen = 1;
  314. INIT_WORK(&ci->i_revalidate_work, ceph_revalidate_work);
  315. }