cache.c 9.1 KB

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