2 * Ceph cache definitions.
4 * Copyright (C) 2013 by Adfin Solutions, Inc. All Rights Reserved.
5 * Written by Milosz Tanski (milosz@adfin.com)
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.
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.
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
27 struct ceph_aux_inode {
28 struct timespec mtime;
32 struct fscache_netfs ceph_cache_netfs = {
37 static uint16_t ceph_fscache_session_get_key(const void *cookie_netfs_data,
38 void *buffer, uint16_t maxbuf)
40 const struct ceph_fs_client* fsc = cookie_netfs_data;
43 klen = sizeof(fsc->client->fsid);
47 memcpy(buffer, &fsc->client->fsid, klen);
51 static const struct fscache_cookie_def ceph_fscache_fsid_object_def = {
53 .type = FSCACHE_COOKIE_TYPE_INDEX,
54 .get_key = ceph_fscache_session_get_key,
57 int ceph_fscache_register(void)
59 return fscache_register_netfs(&ceph_cache_netfs);
62 void ceph_fscache_unregister(void)
64 fscache_unregister_netfs(&ceph_cache_netfs);
67 int ceph_fscache_register_fs(struct ceph_fs_client* fsc)
69 fsc->fscache = fscache_acquire_cookie(ceph_cache_netfs.primary_index,
70 &ceph_fscache_fsid_object_def,
73 pr_err("Unable to resgister fsid: %p fscache cookie", fsc);
78 static uint16_t ceph_fscache_inode_get_key(const void *cookie_netfs_data,
79 void *buffer, uint16_t maxbuf)
81 const struct ceph_inode_info* ci = cookie_netfs_data;
84 /* use ceph virtual inode (id + snapshot) */
85 klen = sizeof(ci->i_vino);
89 memcpy(buffer, &ci->i_vino, klen);
93 static uint16_t ceph_fscache_inode_get_aux(const void *cookie_netfs_data,
94 void *buffer, uint16_t bufmax)
96 struct ceph_aux_inode aux;
97 const struct ceph_inode_info* ci = cookie_netfs_data;
98 const struct inode* inode = &ci->vfs_inode;
100 memset(&aux, 0, sizeof(aux));
101 aux.mtime = inode->i_mtime;
102 aux.size = i_size_read(inode);
104 memcpy(buffer, &aux, sizeof(aux));
109 static void ceph_fscache_inode_get_attr(const void *cookie_netfs_data,
112 const struct ceph_inode_info* ci = cookie_netfs_data;
113 *size = i_size_read(&ci->vfs_inode);
116 static enum fscache_checkaux ceph_fscache_inode_check_aux(
117 void *cookie_netfs_data, const void *data, uint16_t dlen)
119 struct ceph_aux_inode aux;
120 struct ceph_inode_info* ci = cookie_netfs_data;
121 struct inode* inode = &ci->vfs_inode;
123 if (dlen != sizeof(aux))
124 return FSCACHE_CHECKAUX_OBSOLETE;
126 memset(&aux, 0, sizeof(aux));
127 aux.mtime = inode->i_mtime;
128 aux.size = i_size_read(inode);
130 if (memcmp(data, &aux, sizeof(aux)) != 0)
131 return FSCACHE_CHECKAUX_OBSOLETE;
133 dout("ceph inode 0x%p cached okay", ci);
134 return FSCACHE_CHECKAUX_OKAY;
137 static void ceph_fscache_inode_now_uncached(void* cookie_netfs_data)
139 struct ceph_inode_info* ci = cookie_netfs_data;
144 pagevec_init(&pvec, 0);
147 dout("ceph inode 0x%p now uncached", ci);
150 nr_pages = pagevec_lookup(&pvec, ci->vfs_inode.i_mapping, first,
151 PAGEVEC_SIZE - pagevec_count(&pvec));
156 for (loop = 0; loop < nr_pages; loop++)
157 ClearPageFsCache(pvec.pages[loop]);
159 first = pvec.pages[nr_pages - 1]->index + 1;
162 pagevec_release(&pvec);
167 static const struct fscache_cookie_def ceph_fscache_inode_object_def = {
168 .name = "CEPH.inode",
169 .type = FSCACHE_COOKIE_TYPE_DATAFILE,
170 .get_key = ceph_fscache_inode_get_key,
171 .get_attr = ceph_fscache_inode_get_attr,
172 .get_aux = ceph_fscache_inode_get_aux,
173 .check_aux = ceph_fscache_inode_check_aux,
174 .now_uncached = ceph_fscache_inode_now_uncached,
177 void ceph_fscache_register_inode_cookie(struct inode *inode)
179 struct ceph_inode_info *ci = ceph_inode(inode);
180 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
182 /* No caching for filesystem */
183 if (fsc->fscache == NULL)
186 /* Only cache for regular files that are read only */
187 if (!S_ISREG(inode->i_mode))
190 inode_lock_nested(inode, I_MUTEX_CHILD);
192 ci->fscache = fscache_acquire_cookie(fsc->fscache,
193 &ceph_fscache_inode_object_def,
199 void ceph_fscache_unregister_inode_cookie(struct ceph_inode_info* ci)
201 struct fscache_cookie* cookie;
203 if ((cookie = ci->fscache) == NULL)
208 fscache_uncache_all_inode_pages(cookie, &ci->vfs_inode);
209 fscache_relinquish_cookie(cookie, 0);
212 static bool ceph_fscache_can_enable(void *data)
214 struct inode *inode = data;
215 return !inode_is_open_for_write(inode);
218 void ceph_fscache_file_set_cookie(struct inode *inode, struct file *filp)
220 struct ceph_inode_info *ci = ceph_inode(inode);
222 if (!fscache_cookie_valid(ci->fscache))
225 if (inode_is_open_for_write(inode)) {
226 dout("fscache_file_set_cookie %p %p disabling cache\n",
228 fscache_disable_cookie(ci->fscache, false);
229 fscache_uncache_all_inode_pages(ci->fscache, inode);
231 fscache_enable_cookie(ci->fscache, ceph_fscache_can_enable,
233 if (fscache_cookie_enabled(ci->fscache)) {
234 dout("fscache_file_set_cookie %p %p enabing cache\n",
240 static void ceph_vfs_readpage_complete(struct page *page, void *data, int error)
243 SetPageUptodate(page);
246 static void ceph_vfs_readpage_complete_unlock(struct page *page, void *data, int error)
249 SetPageUptodate(page);
254 static inline bool cache_valid(struct ceph_inode_info *ci)
256 return ci->i_fscache_gen == ci->i_rdcache_gen;
260 /* Atempt to read from the fscache,
262 * This function is called from the readpage_nounlock context. DO NOT attempt to
263 * unlock the page here (or in the callback).
265 int ceph_readpage_from_fscache(struct inode *inode, struct page *page)
267 struct ceph_inode_info *ci = ceph_inode(inode);
270 if (!cache_valid(ci))
273 ret = fscache_read_or_alloc_page(ci->fscache, page,
274 ceph_vfs_readpage_complete, NULL,
278 case 0: /* Page found */
279 dout("page read submitted\n");
281 case -ENOBUFS: /* Pages were not found, and can't be */
282 case -ENODATA: /* Pages were not found */
283 dout("page/inode not in cache\n");
286 dout("%s: unknown error ret = %i\n", __func__, ret);
291 int ceph_readpages_from_fscache(struct inode *inode,
292 struct address_space *mapping,
293 struct list_head *pages,
296 struct ceph_inode_info *ci = ceph_inode(inode);
299 if (!cache_valid(ci))
302 ret = fscache_read_or_alloc_pages(ci->fscache, mapping, pages, nr_pages,
303 ceph_vfs_readpage_complete_unlock,
304 NULL, mapping_gfp_mask(mapping));
307 case 0: /* All pages found */
308 dout("all-page read submitted\n");
310 case -ENOBUFS: /* Some pages were not found, and can't be */
311 case -ENODATA: /* some pages were not found */
312 dout("page/inode not in cache\n");
315 dout("%s: unknown error ret = %i\n", __func__, ret);
320 void ceph_readpage_to_fscache(struct inode *inode, struct page *page)
322 struct ceph_inode_info *ci = ceph_inode(inode);
325 if (!PageFsCache(page))
328 if (!cache_valid(ci))
331 ret = fscache_write_page(ci->fscache, page, GFP_KERNEL);
333 fscache_uncache_page(ci->fscache, page);
336 void ceph_invalidate_fscache_page(struct inode* inode, struct page *page)
338 struct ceph_inode_info *ci = ceph_inode(inode);
340 if (!PageFsCache(page))
343 fscache_wait_on_page_write(ci->fscache, page);
344 fscache_uncache_page(ci->fscache, page);
347 void ceph_fscache_unregister_fs(struct ceph_fs_client* fsc)
349 fscache_relinquish_cookie(fsc->fscache, 0);
354 * caller should hold CEPH_CAP_FILE_{RD,CACHE}
356 void ceph_fscache_revalidate_cookie(struct ceph_inode_info *ci)
361 /* resue i_truncate_mutex. There should be no pending
362 * truncate while the caller holds CEPH_CAP_FILE_RD */
363 mutex_lock(&ci->i_truncate_mutex);
364 if (!cache_valid(ci)) {
365 if (fscache_check_consistency(ci->fscache))
366 fscache_invalidate(ci->fscache);
367 spin_lock(&ci->i_ceph_lock);
368 ci->i_fscache_gen = ci->i_rdcache_gen;
369 spin_unlock(&ci->i_ceph_lock);
371 mutex_unlock(&ci->i_truncate_mutex);