cache.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. u64 version;
  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(void)
  51. {
  52. return fscache_register_netfs(&ceph_cache_netfs);
  53. }
  54. void ceph_fscache_unregister(void)
  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, true);
  63. if (!fsc->fscache)
  64. pr_err("Unable to register fsid: %p fscache cookie\n", fsc);
  65. return 0;
  66. }
  67. static uint16_t ceph_fscache_inode_get_key(const void *cookie_netfs_data,
  68. void *buffer, uint16_t maxbuf)
  69. {
  70. const struct ceph_inode_info* ci = cookie_netfs_data;
  71. uint16_t klen;
  72. /* use ceph virtual inode (id + snapshot) */
  73. klen = sizeof(ci->i_vino);
  74. if (klen > maxbuf)
  75. return 0;
  76. memcpy(buffer, &ci->i_vino, klen);
  77. return klen;
  78. }
  79. static uint16_t ceph_fscache_inode_get_aux(const void *cookie_netfs_data,
  80. void *buffer, uint16_t bufmax)
  81. {
  82. struct ceph_aux_inode aux;
  83. const struct ceph_inode_info* ci = cookie_netfs_data;
  84. const struct inode* inode = &ci->vfs_inode;
  85. memset(&aux, 0, sizeof(aux));
  86. aux.version = ci->i_version;
  87. aux.mtime = inode->i_mtime;
  88. aux.size = i_size_read(inode);
  89. memcpy(buffer, &aux, sizeof(aux));
  90. return sizeof(aux);
  91. }
  92. static void ceph_fscache_inode_get_attr(const void *cookie_netfs_data,
  93. uint64_t *size)
  94. {
  95. const struct ceph_inode_info* ci = cookie_netfs_data;
  96. *size = i_size_read(&ci->vfs_inode);
  97. }
  98. static enum fscache_checkaux ceph_fscache_inode_check_aux(
  99. void *cookie_netfs_data, const void *data, uint16_t dlen)
  100. {
  101. struct ceph_aux_inode aux;
  102. struct ceph_inode_info* ci = cookie_netfs_data;
  103. struct inode* inode = &ci->vfs_inode;
  104. if (dlen != sizeof(aux))
  105. return FSCACHE_CHECKAUX_OBSOLETE;
  106. memset(&aux, 0, sizeof(aux));
  107. aux.version = ci->i_version;
  108. aux.mtime = inode->i_mtime;
  109. aux.size = i_size_read(inode);
  110. if (memcmp(data, &aux, sizeof(aux)) != 0)
  111. return FSCACHE_CHECKAUX_OBSOLETE;
  112. dout("ceph inode 0x%p cached okay", ci);
  113. return FSCACHE_CHECKAUX_OKAY;
  114. }
  115. static void ceph_fscache_inode_now_uncached(void* cookie_netfs_data)
  116. {
  117. struct ceph_inode_info* ci = cookie_netfs_data;
  118. struct pagevec pvec;
  119. pgoff_t first;
  120. int loop, nr_pages;
  121. pagevec_init(&pvec, 0);
  122. first = 0;
  123. dout("ceph inode 0x%p now uncached", ci);
  124. while (1) {
  125. nr_pages = pagevec_lookup(&pvec, ci->vfs_inode.i_mapping, first,
  126. PAGEVEC_SIZE - pagevec_count(&pvec));
  127. if (!nr_pages)
  128. break;
  129. for (loop = 0; loop < nr_pages; loop++)
  130. ClearPageFsCache(pvec.pages[loop]);
  131. first = pvec.pages[nr_pages - 1]->index + 1;
  132. pvec.nr = nr_pages;
  133. pagevec_release(&pvec);
  134. cond_resched();
  135. }
  136. }
  137. static const struct fscache_cookie_def ceph_fscache_inode_object_def = {
  138. .name = "CEPH.inode",
  139. .type = FSCACHE_COOKIE_TYPE_DATAFILE,
  140. .get_key = ceph_fscache_inode_get_key,
  141. .get_attr = ceph_fscache_inode_get_attr,
  142. .get_aux = ceph_fscache_inode_get_aux,
  143. .check_aux = ceph_fscache_inode_check_aux,
  144. .now_uncached = ceph_fscache_inode_now_uncached,
  145. };
  146. void ceph_fscache_register_inode_cookie(struct inode *inode)
  147. {
  148. struct ceph_inode_info *ci = ceph_inode(inode);
  149. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  150. /* No caching for filesystem */
  151. if (fsc->fscache == NULL)
  152. return;
  153. /* Only cache for regular files that are read only */
  154. if (!S_ISREG(inode->i_mode))
  155. return;
  156. inode_lock_nested(inode, I_MUTEX_CHILD);
  157. if (!ci->fscache) {
  158. ci->fscache = fscache_acquire_cookie(fsc->fscache,
  159. &ceph_fscache_inode_object_def,
  160. ci, false);
  161. }
  162. inode_unlock(inode);
  163. }
  164. void ceph_fscache_unregister_inode_cookie(struct ceph_inode_info* ci)
  165. {
  166. struct fscache_cookie* cookie;
  167. if ((cookie = ci->fscache) == NULL)
  168. return;
  169. ci->fscache = NULL;
  170. fscache_uncache_all_inode_pages(cookie, &ci->vfs_inode);
  171. fscache_relinquish_cookie(cookie, 0);
  172. }
  173. static bool ceph_fscache_can_enable(void *data)
  174. {
  175. struct inode *inode = data;
  176. return !inode_is_open_for_write(inode);
  177. }
  178. void ceph_fscache_file_set_cookie(struct inode *inode, struct file *filp)
  179. {
  180. struct ceph_inode_info *ci = ceph_inode(inode);
  181. if (!fscache_cookie_valid(ci->fscache))
  182. return;
  183. if (inode_is_open_for_write(inode)) {
  184. dout("fscache_file_set_cookie %p %p disabling cache\n",
  185. inode, filp);
  186. fscache_disable_cookie(ci->fscache, false);
  187. fscache_uncache_all_inode_pages(ci->fscache, inode);
  188. } else {
  189. fscache_enable_cookie(ci->fscache, ceph_fscache_can_enable,
  190. inode);
  191. if (fscache_cookie_enabled(ci->fscache)) {
  192. dout("fscache_file_set_cookie %p %p enabing cache\n",
  193. inode, filp);
  194. }
  195. }
  196. }
  197. static void ceph_readpage_from_fscache_complete(struct page *page, void *data, int error)
  198. {
  199. if (!error)
  200. SetPageUptodate(page);
  201. unlock_page(page);
  202. }
  203. static inline bool cache_valid(struct ceph_inode_info *ci)
  204. {
  205. return ci->i_fscache_gen == ci->i_rdcache_gen;
  206. }
  207. /* Atempt to read from the fscache,
  208. *
  209. * This function is called from the readpage_nounlock context. DO NOT attempt to
  210. * unlock the page here (or in the callback).
  211. */
  212. int ceph_readpage_from_fscache(struct inode *inode, struct page *page)
  213. {
  214. struct ceph_inode_info *ci = ceph_inode(inode);
  215. int ret;
  216. if (!cache_valid(ci))
  217. return -ENOBUFS;
  218. ret = fscache_read_or_alloc_page(ci->fscache, page,
  219. ceph_readpage_from_fscache_complete, NULL,
  220. GFP_KERNEL);
  221. switch (ret) {
  222. case 0: /* Page found */
  223. dout("page read submitted\n");
  224. return 0;
  225. case -ENOBUFS: /* Pages were not found, and can't be */
  226. case -ENODATA: /* Pages were not found */
  227. dout("page/inode not in cache\n");
  228. return ret;
  229. default:
  230. dout("%s: unknown error ret = %i\n", __func__, ret);
  231. return ret;
  232. }
  233. }
  234. int ceph_readpages_from_fscache(struct inode *inode,
  235. struct address_space *mapping,
  236. struct list_head *pages,
  237. unsigned *nr_pages)
  238. {
  239. struct ceph_inode_info *ci = ceph_inode(inode);
  240. int ret;
  241. if (!cache_valid(ci))
  242. return -ENOBUFS;
  243. ret = fscache_read_or_alloc_pages(ci->fscache, mapping, pages, nr_pages,
  244. ceph_readpage_from_fscache_complete,
  245. NULL, mapping_gfp_mask(mapping));
  246. switch (ret) {
  247. case 0: /* All pages found */
  248. dout("all-page read submitted\n");
  249. return 0;
  250. case -ENOBUFS: /* Some pages were not found, and can't be */
  251. case -ENODATA: /* some pages were not found */
  252. dout("page/inode not in cache\n");
  253. return ret;
  254. default:
  255. dout("%s: unknown error ret = %i\n", __func__, ret);
  256. return ret;
  257. }
  258. }
  259. void ceph_readpage_to_fscache(struct inode *inode, struct page *page)
  260. {
  261. struct ceph_inode_info *ci = ceph_inode(inode);
  262. int ret;
  263. if (!PageFsCache(page))
  264. return;
  265. if (!cache_valid(ci))
  266. return;
  267. ret = fscache_write_page(ci->fscache, page, GFP_KERNEL);
  268. if (ret)
  269. fscache_uncache_page(ci->fscache, page);
  270. }
  271. void ceph_invalidate_fscache_page(struct inode* inode, struct page *page)
  272. {
  273. struct ceph_inode_info *ci = ceph_inode(inode);
  274. if (!PageFsCache(page))
  275. return;
  276. fscache_wait_on_page_write(ci->fscache, page);
  277. fscache_uncache_page(ci->fscache, page);
  278. }
  279. void ceph_fscache_unregister_fs(struct ceph_fs_client* fsc)
  280. {
  281. fscache_relinquish_cookie(fsc->fscache, 0);
  282. fsc->fscache = NULL;
  283. }
  284. /*
  285. * caller should hold CEPH_CAP_FILE_{RD,CACHE}
  286. */
  287. void ceph_fscache_revalidate_cookie(struct ceph_inode_info *ci)
  288. {
  289. if (cache_valid(ci))
  290. return;
  291. /* resue i_truncate_mutex. There should be no pending
  292. * truncate while the caller holds CEPH_CAP_FILE_RD */
  293. mutex_lock(&ci->i_truncate_mutex);
  294. if (!cache_valid(ci)) {
  295. if (fscache_check_consistency(ci->fscache))
  296. fscache_invalidate(ci->fscache);
  297. spin_lock(&ci->i_ceph_lock);
  298. ci->i_fscache_gen = ci->i_rdcache_gen;
  299. spin_unlock(&ci->i_ceph_lock);
  300. }
  301. mutex_unlock(&ci->i_truncate_mutex);
  302. }