cache.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. };
  29. struct fscache_netfs ceph_cache_netfs = {
  30. .name = "ceph",
  31. .version = 0,
  32. };
  33. static DEFINE_MUTEX(ceph_fscache_lock);
  34. static LIST_HEAD(ceph_fscache_list);
  35. struct ceph_fscache_entry {
  36. struct list_head list;
  37. struct fscache_cookie *fscache;
  38. size_t uniq_len;
  39. /* The following members must be last */
  40. struct ceph_fsid fsid;
  41. char uniquifier[0];
  42. };
  43. static const struct fscache_cookie_def ceph_fscache_fsid_object_def = {
  44. .name = "CEPH.fsid",
  45. .type = FSCACHE_COOKIE_TYPE_INDEX,
  46. };
  47. int __init ceph_fscache_register(void)
  48. {
  49. return fscache_register_netfs(&ceph_cache_netfs);
  50. }
  51. void ceph_fscache_unregister(void)
  52. {
  53. fscache_unregister_netfs(&ceph_cache_netfs);
  54. }
  55. int ceph_fscache_register_fs(struct ceph_fs_client* fsc)
  56. {
  57. const struct ceph_fsid *fsid = &fsc->client->fsid;
  58. const char *fscache_uniq = fsc->mount_options->fscache_uniq;
  59. size_t uniq_len = fscache_uniq ? strlen(fscache_uniq) : 0;
  60. struct ceph_fscache_entry *ent;
  61. int err = 0;
  62. mutex_lock(&ceph_fscache_lock);
  63. list_for_each_entry(ent, &ceph_fscache_list, list) {
  64. if (memcmp(&ent->fsid, fsid, sizeof(*fsid)))
  65. continue;
  66. if (ent->uniq_len != uniq_len)
  67. continue;
  68. if (uniq_len && memcmp(ent->uniquifier, fscache_uniq, uniq_len))
  69. continue;
  70. pr_err("fscache cookie already registered for fsid %pU\n", fsid);
  71. pr_err(" use fsc=%%s mount option to specify a uniquifier\n");
  72. err = -EBUSY;
  73. goto out_unlock;
  74. }
  75. ent = kzalloc(sizeof(*ent) + uniq_len, GFP_KERNEL);
  76. if (!ent) {
  77. err = -ENOMEM;
  78. goto out_unlock;
  79. }
  80. memcpy(&ent->fsid, fsid, sizeof(*fsid));
  81. if (uniq_len > 0) {
  82. memcpy(&ent->uniquifier, fscache_uniq, uniq_len);
  83. ent->uniq_len = uniq_len;
  84. }
  85. fsc->fscache = fscache_acquire_cookie(ceph_cache_netfs.primary_index,
  86. &ceph_fscache_fsid_object_def,
  87. &ent->fsid, sizeof(ent->fsid) + uniq_len,
  88. NULL, 0,
  89. fsc, 0, true);
  90. if (fsc->fscache) {
  91. ent->fscache = fsc->fscache;
  92. list_add_tail(&ent->list, &ceph_fscache_list);
  93. } else {
  94. kfree(ent);
  95. pr_err("unable to register fscache cookie for fsid %pU\n",
  96. fsid);
  97. /* all other fs ignore this error */
  98. }
  99. out_unlock:
  100. mutex_unlock(&ceph_fscache_lock);
  101. return err;
  102. }
  103. static enum fscache_checkaux ceph_fscache_inode_check_aux(
  104. void *cookie_netfs_data, const void *data, uint16_t dlen,
  105. loff_t object_size)
  106. {
  107. struct ceph_aux_inode aux;
  108. struct ceph_inode_info* ci = cookie_netfs_data;
  109. struct inode* inode = &ci->vfs_inode;
  110. if (dlen != sizeof(aux) ||
  111. i_size_read(inode) != object_size)
  112. return FSCACHE_CHECKAUX_OBSOLETE;
  113. memset(&aux, 0, sizeof(aux));
  114. aux.version = ci->i_version;
  115. aux.mtime = timespec64_to_timespec(inode->i_mtime);
  116. if (memcmp(data, &aux, sizeof(aux)) != 0)
  117. return FSCACHE_CHECKAUX_OBSOLETE;
  118. dout("ceph inode 0x%p cached okay\n", ci);
  119. return FSCACHE_CHECKAUX_OKAY;
  120. }
  121. static const struct fscache_cookie_def ceph_fscache_inode_object_def = {
  122. .name = "CEPH.inode",
  123. .type = FSCACHE_COOKIE_TYPE_DATAFILE,
  124. .check_aux = ceph_fscache_inode_check_aux,
  125. };
  126. void ceph_fscache_register_inode_cookie(struct inode *inode)
  127. {
  128. struct ceph_inode_info *ci = ceph_inode(inode);
  129. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  130. struct ceph_aux_inode aux;
  131. /* No caching for filesystem */
  132. if (!fsc->fscache)
  133. return;
  134. /* Only cache for regular files that are read only */
  135. if (!S_ISREG(inode->i_mode))
  136. return;
  137. inode_lock_nested(inode, I_MUTEX_CHILD);
  138. if (!ci->fscache) {
  139. memset(&aux, 0, sizeof(aux));
  140. aux.version = ci->i_version;
  141. aux.mtime = timespec64_to_timespec(inode->i_mtime);
  142. ci->fscache = fscache_acquire_cookie(fsc->fscache,
  143. &ceph_fscache_inode_object_def,
  144. &ci->i_vino, sizeof(ci->i_vino),
  145. &aux, sizeof(aux),
  146. ci, i_size_read(inode), false);
  147. }
  148. inode_unlock(inode);
  149. }
  150. void ceph_fscache_unregister_inode_cookie(struct ceph_inode_info* ci)
  151. {
  152. struct fscache_cookie* cookie;
  153. if ((cookie = ci->fscache) == NULL)
  154. return;
  155. ci->fscache = NULL;
  156. fscache_uncache_all_inode_pages(cookie, &ci->vfs_inode);
  157. fscache_relinquish_cookie(cookie, &ci->i_vino, false);
  158. }
  159. static bool ceph_fscache_can_enable(void *data)
  160. {
  161. struct inode *inode = data;
  162. return !inode_is_open_for_write(inode);
  163. }
  164. void ceph_fscache_file_set_cookie(struct inode *inode, struct file *filp)
  165. {
  166. struct ceph_inode_info *ci = ceph_inode(inode);
  167. if (!fscache_cookie_valid(ci->fscache))
  168. return;
  169. if (inode_is_open_for_write(inode)) {
  170. dout("fscache_file_set_cookie %p %p disabling cache\n",
  171. inode, filp);
  172. fscache_disable_cookie(ci->fscache, &ci->i_vino, false);
  173. fscache_uncache_all_inode_pages(ci->fscache, inode);
  174. } else {
  175. fscache_enable_cookie(ci->fscache, &ci->i_vino, i_size_read(inode),
  176. ceph_fscache_can_enable, inode);
  177. if (fscache_cookie_enabled(ci->fscache)) {
  178. dout("fscache_file_set_cookie %p %p enabling cache\n",
  179. inode, filp);
  180. }
  181. }
  182. }
  183. static void ceph_readpage_from_fscache_complete(struct page *page, void *data, int error)
  184. {
  185. if (!error)
  186. SetPageUptodate(page);
  187. unlock_page(page);
  188. }
  189. static inline bool cache_valid(struct ceph_inode_info *ci)
  190. {
  191. return ci->i_fscache_gen == ci->i_rdcache_gen;
  192. }
  193. /* Atempt to read from the fscache,
  194. *
  195. * This function is called from the readpage_nounlock context. DO NOT attempt to
  196. * unlock the page here (or in the callback).
  197. */
  198. int ceph_readpage_from_fscache(struct inode *inode, struct page *page)
  199. {
  200. struct ceph_inode_info *ci = ceph_inode(inode);
  201. int ret;
  202. if (!cache_valid(ci))
  203. return -ENOBUFS;
  204. ret = fscache_read_or_alloc_page(ci->fscache, page,
  205. ceph_readpage_from_fscache_complete, NULL,
  206. GFP_KERNEL);
  207. switch (ret) {
  208. case 0: /* Page found */
  209. dout("page read submitted\n");
  210. return 0;
  211. case -ENOBUFS: /* Pages were not found, and can't be */
  212. case -ENODATA: /* Pages were not found */
  213. dout("page/inode not in cache\n");
  214. return ret;
  215. default:
  216. dout("%s: unknown error ret = %i\n", __func__, ret);
  217. return ret;
  218. }
  219. }
  220. int ceph_readpages_from_fscache(struct inode *inode,
  221. struct address_space *mapping,
  222. struct list_head *pages,
  223. unsigned *nr_pages)
  224. {
  225. struct ceph_inode_info *ci = ceph_inode(inode);
  226. int ret;
  227. if (!cache_valid(ci))
  228. return -ENOBUFS;
  229. ret = fscache_read_or_alloc_pages(ci->fscache, mapping, pages, nr_pages,
  230. ceph_readpage_from_fscache_complete,
  231. NULL, mapping_gfp_mask(mapping));
  232. switch (ret) {
  233. case 0: /* All pages found */
  234. dout("all-page read submitted\n");
  235. return 0;
  236. case -ENOBUFS: /* Some pages were not found, and can't be */
  237. case -ENODATA: /* some pages were not found */
  238. dout("page/inode not in cache\n");
  239. return ret;
  240. default:
  241. dout("%s: unknown error ret = %i\n", __func__, ret);
  242. return ret;
  243. }
  244. }
  245. void ceph_readpage_to_fscache(struct inode *inode, struct page *page)
  246. {
  247. struct ceph_inode_info *ci = ceph_inode(inode);
  248. int ret;
  249. if (!PageFsCache(page))
  250. return;
  251. if (!cache_valid(ci))
  252. return;
  253. ret = fscache_write_page(ci->fscache, page, i_size_read(inode),
  254. 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. if (!PageFsCache(page))
  262. return;
  263. fscache_wait_on_page_write(ci->fscache, page);
  264. fscache_uncache_page(ci->fscache, page);
  265. }
  266. void ceph_fscache_unregister_fs(struct ceph_fs_client* fsc)
  267. {
  268. if (fscache_cookie_valid(fsc->fscache)) {
  269. struct ceph_fscache_entry *ent;
  270. bool found = false;
  271. mutex_lock(&ceph_fscache_lock);
  272. list_for_each_entry(ent, &ceph_fscache_list, list) {
  273. if (ent->fscache == fsc->fscache) {
  274. list_del(&ent->list);
  275. kfree(ent);
  276. found = true;
  277. break;
  278. }
  279. }
  280. WARN_ON_ONCE(!found);
  281. mutex_unlock(&ceph_fscache_lock);
  282. __fscache_relinquish_cookie(fsc->fscache, NULL, false);
  283. }
  284. fsc->fscache = NULL;
  285. }
  286. /*
  287. * caller should hold CEPH_CAP_FILE_{RD,CACHE}
  288. */
  289. void ceph_fscache_revalidate_cookie(struct ceph_inode_info *ci)
  290. {
  291. if (cache_valid(ci))
  292. return;
  293. /* resue i_truncate_mutex. There should be no pending
  294. * truncate while the caller holds CEPH_CAP_FILE_RD */
  295. mutex_lock(&ci->i_truncate_mutex);
  296. if (!cache_valid(ci)) {
  297. if (fscache_check_consistency(ci->fscache, &ci->i_vino))
  298. fscache_invalidate(ci->fscache);
  299. spin_lock(&ci->i_ceph_lock);
  300. ci->i_fscache_gen = ci->i_rdcache_gen;
  301. spin_unlock(&ci->i_ceph_lock);
  302. }
  303. mutex_unlock(&ci->i_truncate_mutex);
  304. }