cache.c 8.6 KB

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