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 {
29 struct timespec mtime;
33 struct fscache_netfs ceph_cache_netfs = {
38 static DEFINE_MUTEX(ceph_fscache_lock);
39 static LIST_HEAD(ceph_fscache_list);
41 struct ceph_fscache_entry {
42 struct list_head list;
43 struct fscache_cookie *fscache;
44 struct ceph_fsid fsid;
49 static uint16_t ceph_fscache_session_get_key(const void *cookie_netfs_data,
50 void *buffer, uint16_t maxbuf)
52 const struct ceph_fs_client* fsc = cookie_netfs_data;
53 const char *fscache_uniq = fsc->mount_options->fscache_uniq;
54 uint16_t fsid_len, uniq_len;
56 fsid_len = sizeof(fsc->client->fsid);
57 uniq_len = fscache_uniq ? strlen(fscache_uniq) : 0;
58 if (fsid_len + uniq_len > maxbuf)
61 memcpy(buffer, &fsc->client->fsid, fsid_len);
63 memcpy(buffer + fsid_len, fscache_uniq, uniq_len);
65 return fsid_len + uniq_len;
68 static const struct fscache_cookie_def ceph_fscache_fsid_object_def = {
70 .type = FSCACHE_COOKIE_TYPE_INDEX,
71 .get_key = ceph_fscache_session_get_key,
74 int ceph_fscache_register(void)
76 return fscache_register_netfs(&ceph_cache_netfs);
79 void ceph_fscache_unregister(void)
81 fscache_unregister_netfs(&ceph_cache_netfs);
84 int ceph_fscache_register_fs(struct ceph_fs_client* fsc)
86 const struct ceph_fsid *fsid = &fsc->client->fsid;
87 const char *fscache_uniq = fsc->mount_options->fscache_uniq;
88 size_t uniq_len = fscache_uniq ? strlen(fscache_uniq) : 0;
89 struct ceph_fscache_entry *ent;
92 mutex_lock(&ceph_fscache_lock);
93 list_for_each_entry(ent, &ceph_fscache_list, list) {
94 if (memcmp(&ent->fsid, fsid, sizeof(*fsid)))
96 if (ent->uniq_len != uniq_len)
98 if (uniq_len && memcmp(ent->uniquifier, fscache_uniq, uniq_len))
101 pr_err("fscache cookie already registered for fsid %pU\n", fsid);
102 pr_err(" use fsc=%%s mount option to specify a uniquifier\n");
107 ent = kzalloc(sizeof(*ent) + uniq_len, GFP_KERNEL);
113 fsc->fscache = fscache_acquire_cookie(ceph_cache_netfs.primary_index,
114 &ceph_fscache_fsid_object_def,
118 memcpy(&ent->fsid, fsid, sizeof(*fsid));
120 memcpy(&ent->uniquifier, fscache_uniq, uniq_len);
121 ent->uniq_len = uniq_len;
123 ent->fscache = fsc->fscache;
124 list_add_tail(&ent->list, &ceph_fscache_list);
127 pr_err("unable to register fscache cookie for fsid %pU\n",
129 /* all other fs ignore this error */
132 mutex_unlock(&ceph_fscache_lock);
136 static uint16_t ceph_fscache_inode_get_key(const void *cookie_netfs_data,
137 void *buffer, uint16_t maxbuf)
139 const struct ceph_inode_info* ci = cookie_netfs_data;
142 /* use ceph virtual inode (id + snapshot) */
143 klen = sizeof(ci->i_vino);
147 memcpy(buffer, &ci->i_vino, klen);
151 static uint16_t ceph_fscache_inode_get_aux(const void *cookie_netfs_data,
152 void *buffer, uint16_t bufmax)
154 struct ceph_aux_inode aux;
155 const struct ceph_inode_info* ci = cookie_netfs_data;
156 const struct inode* inode = &ci->vfs_inode;
158 memset(&aux, 0, sizeof(aux));
159 aux.version = ci->i_version;
160 aux.mtime = inode->i_mtime;
161 aux.size = i_size_read(inode);
163 memcpy(buffer, &aux, sizeof(aux));
168 static void ceph_fscache_inode_get_attr(const void *cookie_netfs_data,
171 const struct ceph_inode_info* ci = cookie_netfs_data;
172 *size = i_size_read(&ci->vfs_inode);
175 static enum fscache_checkaux ceph_fscache_inode_check_aux(
176 void *cookie_netfs_data, const void *data, uint16_t dlen)
178 struct ceph_aux_inode aux;
179 struct ceph_inode_info* ci = cookie_netfs_data;
180 struct inode* inode = &ci->vfs_inode;
182 if (dlen != sizeof(aux))
183 return FSCACHE_CHECKAUX_OBSOLETE;
185 memset(&aux, 0, sizeof(aux));
186 aux.version = ci->i_version;
187 aux.mtime = inode->i_mtime;
188 aux.size = i_size_read(inode);
190 if (memcmp(data, &aux, sizeof(aux)) != 0)
191 return FSCACHE_CHECKAUX_OBSOLETE;
193 dout("ceph inode 0x%p cached okay", ci);
194 return FSCACHE_CHECKAUX_OKAY;
197 static void ceph_fscache_inode_now_uncached(void* cookie_netfs_data)
199 struct ceph_inode_info* ci = cookie_netfs_data;
204 pagevec_init(&pvec, 0);
207 dout("ceph inode 0x%p now uncached", ci);
210 nr_pages = pagevec_lookup(&pvec, ci->vfs_inode.i_mapping, first,
211 PAGEVEC_SIZE - pagevec_count(&pvec));
216 for (loop = 0; loop < nr_pages; loop++)
217 ClearPageFsCache(pvec.pages[loop]);
219 first = pvec.pages[nr_pages - 1]->index + 1;
222 pagevec_release(&pvec);
227 static const struct fscache_cookie_def ceph_fscache_inode_object_def = {
228 .name = "CEPH.inode",
229 .type = FSCACHE_COOKIE_TYPE_DATAFILE,
230 .get_key = ceph_fscache_inode_get_key,
231 .get_attr = ceph_fscache_inode_get_attr,
232 .get_aux = ceph_fscache_inode_get_aux,
233 .check_aux = ceph_fscache_inode_check_aux,
234 .now_uncached = ceph_fscache_inode_now_uncached,
237 void ceph_fscache_register_inode_cookie(struct inode *inode)
239 struct ceph_inode_info *ci = ceph_inode(inode);
240 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
242 /* No caching for filesystem */
243 if (fsc->fscache == NULL)
246 /* Only cache for regular files that are read only */
247 if (!S_ISREG(inode->i_mode))
250 inode_lock_nested(inode, I_MUTEX_CHILD);
252 ci->fscache = fscache_acquire_cookie(fsc->fscache,
253 &ceph_fscache_inode_object_def,
259 void ceph_fscache_unregister_inode_cookie(struct ceph_inode_info* ci)
261 struct fscache_cookie* cookie;
263 if ((cookie = ci->fscache) == NULL)
268 fscache_uncache_all_inode_pages(cookie, &ci->vfs_inode);
269 fscache_relinquish_cookie(cookie, 0);
272 static bool ceph_fscache_can_enable(void *data)
274 struct inode *inode = data;
275 return !inode_is_open_for_write(inode);
278 void ceph_fscache_file_set_cookie(struct inode *inode, struct file *filp)
280 struct ceph_inode_info *ci = ceph_inode(inode);
282 if (!fscache_cookie_valid(ci->fscache))
285 if (inode_is_open_for_write(inode)) {
286 dout("fscache_file_set_cookie %p %p disabling cache\n",
288 fscache_disable_cookie(ci->fscache, false);
289 fscache_uncache_all_inode_pages(ci->fscache, inode);
291 fscache_enable_cookie(ci->fscache, ceph_fscache_can_enable,
293 if (fscache_cookie_enabled(ci->fscache)) {
294 dout("fscache_file_set_cookie %p %p enabling cache\n",
300 static void ceph_readpage_from_fscache_complete(struct page *page, void *data, int error)
303 SetPageUptodate(page);
308 static inline bool cache_valid(struct ceph_inode_info *ci)
310 return ci->i_fscache_gen == ci->i_rdcache_gen;
314 /* Atempt to read from the fscache,
316 * This function is called from the readpage_nounlock context. DO NOT attempt to
317 * unlock the page here (or in the callback).
319 int ceph_readpage_from_fscache(struct inode *inode, struct page *page)
321 struct ceph_inode_info *ci = ceph_inode(inode);
324 if (!cache_valid(ci))
327 ret = fscache_read_or_alloc_page(ci->fscache, page,
328 ceph_readpage_from_fscache_complete, NULL,
332 case 0: /* Page found */
333 dout("page read submitted\n");
335 case -ENOBUFS: /* Pages were not found, and can't be */
336 case -ENODATA: /* Pages were not found */
337 dout("page/inode not in cache\n");
340 dout("%s: unknown error ret = %i\n", __func__, ret);
345 int ceph_readpages_from_fscache(struct inode *inode,
346 struct address_space *mapping,
347 struct list_head *pages,
350 struct ceph_inode_info *ci = ceph_inode(inode);
353 if (!cache_valid(ci))
356 ret = fscache_read_or_alloc_pages(ci->fscache, mapping, pages, nr_pages,
357 ceph_readpage_from_fscache_complete,
358 NULL, mapping_gfp_mask(mapping));
361 case 0: /* All pages found */
362 dout("all-page read submitted\n");
364 case -ENOBUFS: /* Some pages were not found, and can't be */
365 case -ENODATA: /* some pages were not found */
366 dout("page/inode not in cache\n");
369 dout("%s: unknown error ret = %i\n", __func__, ret);
374 void ceph_readpage_to_fscache(struct inode *inode, struct page *page)
376 struct ceph_inode_info *ci = ceph_inode(inode);
379 if (!PageFsCache(page))
382 if (!cache_valid(ci))
385 ret = fscache_write_page(ci->fscache, page, GFP_KERNEL);
387 fscache_uncache_page(ci->fscache, page);
390 void ceph_invalidate_fscache_page(struct inode* inode, struct page *page)
392 struct ceph_inode_info *ci = ceph_inode(inode);
394 if (!PageFsCache(page))
397 fscache_wait_on_page_write(ci->fscache, page);
398 fscache_uncache_page(ci->fscache, page);
401 void ceph_fscache_unregister_fs(struct ceph_fs_client* fsc)
403 if (fscache_cookie_valid(fsc->fscache)) {
404 struct ceph_fscache_entry *ent;
407 mutex_lock(&ceph_fscache_lock);
408 list_for_each_entry(ent, &ceph_fscache_list, list) {
409 if (ent->fscache == fsc->fscache) {
410 list_del(&ent->list);
416 WARN_ON_ONCE(!found);
417 mutex_unlock(&ceph_fscache_lock);
419 __fscache_relinquish_cookie(fsc->fscache, 0);
425 * caller should hold CEPH_CAP_FILE_{RD,CACHE}
427 void ceph_fscache_revalidate_cookie(struct ceph_inode_info *ci)
432 /* resue i_truncate_mutex. There should be no pending
433 * truncate while the caller holds CEPH_CAP_FILE_RD */
434 mutex_lock(&ci->i_truncate_mutex);
435 if (!cache_valid(ci)) {
436 if (fscache_check_consistency(ci->fscache))
437 fscache_invalidate(ci->fscache);
438 spin_lock(&ci->i_ceph_lock);
439 ci->i_fscache_gen = ci->i_rdcache_gen;
440 spin_unlock(&ci->i_ceph_lock);
442 mutex_unlock(&ci->i_truncate_mutex);