cache.c 10 KB

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