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 if (fsc
->fscache
== NULL
) {
74 pr_err("Unable to resgister fsid: %p fscache cookie", fsc
);
78 fsc
->revalidate_wq
= alloc_workqueue("ceph-revalidate", 0, 1);
79 if (fsc
->revalidate_wq
== NULL
)
85 static uint16_t ceph_fscache_inode_get_key(const void *cookie_netfs_data
,
86 void *buffer
, uint16_t maxbuf
)
88 const struct ceph_inode_info
* ci
= cookie_netfs_data
;
91 /* use ceph virtual inode (id + snaphot) */
92 klen
= sizeof(ci
->i_vino
);
96 memcpy(buffer
, &ci
->i_vino
, klen
);
100 static uint16_t ceph_fscache_inode_get_aux(const void *cookie_netfs_data
,
101 void *buffer
, uint16_t bufmax
)
103 struct ceph_aux_inode aux
;
104 const struct ceph_inode_info
* ci
= cookie_netfs_data
;
105 const struct inode
* inode
= &ci
->vfs_inode
;
107 memset(&aux
, 0, sizeof(aux
));
108 aux
.mtime
= inode
->i_mtime
;
109 aux
.size
= inode
->i_size
;
111 memcpy(buffer
, &aux
, sizeof(aux
));
116 static void ceph_fscache_inode_get_attr(const void *cookie_netfs_data
,
119 const struct ceph_inode_info
* ci
= cookie_netfs_data
;
120 const struct inode
* inode
= &ci
->vfs_inode
;
122 *size
= inode
->i_size
;
125 static enum fscache_checkaux
ceph_fscache_inode_check_aux(
126 void *cookie_netfs_data
, const void *data
, uint16_t dlen
)
128 struct ceph_aux_inode aux
;
129 struct ceph_inode_info
* ci
= cookie_netfs_data
;
130 struct inode
* inode
= &ci
->vfs_inode
;
132 if (dlen
!= sizeof(aux
))
133 return FSCACHE_CHECKAUX_OBSOLETE
;
135 memset(&aux
, 0, sizeof(aux
));
136 aux
.mtime
= inode
->i_mtime
;
137 aux
.size
= inode
->i_size
;
139 if (memcmp(data
, &aux
, sizeof(aux
)) != 0)
140 return FSCACHE_CHECKAUX_OBSOLETE
;
142 dout("ceph inode 0x%p cached okay", ci
);
143 return FSCACHE_CHECKAUX_OKAY
;
146 static void ceph_fscache_inode_now_uncached(void* cookie_netfs_data
)
148 struct ceph_inode_info
* ci
= cookie_netfs_data
;
153 pagevec_init(&pvec
, 0);
156 dout("ceph inode 0x%p now uncached", ci
);
159 nr_pages
= pagevec_lookup(&pvec
, ci
->vfs_inode
.i_mapping
, first
,
160 PAGEVEC_SIZE
- pagevec_count(&pvec
));
165 for (loop
= 0; loop
< nr_pages
; loop
++)
166 ClearPageFsCache(pvec
.pages
[loop
]);
168 first
= pvec
.pages
[nr_pages
- 1]->index
+ 1;
171 pagevec_release(&pvec
);
176 static const struct fscache_cookie_def ceph_fscache_inode_object_def
= {
177 .name
= "CEPH.inode",
178 .type
= FSCACHE_COOKIE_TYPE_DATAFILE
,
179 .get_key
= ceph_fscache_inode_get_key
,
180 .get_attr
= ceph_fscache_inode_get_attr
,
181 .get_aux
= ceph_fscache_inode_get_aux
,
182 .check_aux
= ceph_fscache_inode_check_aux
,
183 .now_uncached
= ceph_fscache_inode_now_uncached
,
186 void ceph_fscache_register_inode_cookie(struct ceph_fs_client
* fsc
,
187 struct ceph_inode_info
* ci
)
189 struct inode
* inode
= &ci
->vfs_inode
;
191 /* No caching for filesystem */
192 if (fsc
->fscache
== NULL
)
195 /* Only cache for regular files that are read only */
196 if ((ci
->vfs_inode
.i_mode
& S_IFREG
) == 0)
199 /* Avoid multiple racing open requests */
200 mutex_lock(&inode
->i_mutex
);
205 ci
->fscache
= fscache_acquire_cookie(fsc
->fscache
,
206 &ceph_fscache_inode_object_def
,
209 mutex_unlock(&inode
->i_mutex
);
213 void ceph_fscache_unregister_inode_cookie(struct ceph_inode_info
* ci
)
215 struct fscache_cookie
* cookie
;
217 if ((cookie
= ci
->fscache
) == NULL
)
222 fscache_uncache_all_inode_pages(cookie
, &ci
->vfs_inode
);
223 fscache_relinquish_cookie(cookie
, 0);
226 static void ceph_vfs_readpage_complete(struct page
*page
, void *data
, int error
)
229 SetPageUptodate(page
);
232 static void ceph_vfs_readpage_complete_unlock(struct page
*page
, void *data
, int error
)
235 SetPageUptodate(page
);
240 static inline int cache_valid(struct ceph_inode_info
*ci
)
242 return ((ceph_caps_issued(ci
) & CEPH_CAP_FILE_CACHE
) &&
243 (ci
->i_fscache_gen
== ci
->i_rdcache_gen
));
247 /* Atempt to read from the fscache,
249 * This function is called from the readpage_nounlock context. DO NOT attempt to
250 * unlock the page here (or in the callback).
252 int ceph_readpage_from_fscache(struct inode
*inode
, struct page
*page
)
254 struct ceph_inode_info
*ci
= ceph_inode(inode
);
257 if (!cache_valid(ci
))
260 ret
= fscache_read_or_alloc_page(ci
->fscache
, page
,
261 ceph_vfs_readpage_complete
, NULL
,
265 case 0: /* Page found */
266 dout("page read submitted\n");
268 case -ENOBUFS
: /* Pages were not found, and can't be */
269 case -ENODATA
: /* Pages were not found */
270 dout("page/inode not in cache\n");
273 dout("%s: unknown error ret = %i\n", __func__
, ret
);
278 int ceph_readpages_from_fscache(struct inode
*inode
,
279 struct address_space
*mapping
,
280 struct list_head
*pages
,
283 struct ceph_inode_info
*ci
= ceph_inode(inode
);
286 if (!cache_valid(ci
))
289 ret
= fscache_read_or_alloc_pages(ci
->fscache
, mapping
, pages
, nr_pages
,
290 ceph_vfs_readpage_complete_unlock
,
291 NULL
, mapping_gfp_mask(mapping
));
294 case 0: /* All pages found */
295 dout("all-page read submitted\n");
297 case -ENOBUFS
: /* Some pages were not found, and can't be */
298 case -ENODATA
: /* some pages were not found */
299 dout("page/inode not in cache\n");
302 dout("%s: unknown error ret = %i\n", __func__
, ret
);
307 void ceph_readpage_to_fscache(struct inode
*inode
, struct page
*page
)
309 struct ceph_inode_info
*ci
= ceph_inode(inode
);
312 if (!PageFsCache(page
))
315 if (!cache_valid(ci
))
318 ret
= fscache_write_page(ci
->fscache
, page
, GFP_KERNEL
);
320 fscache_uncache_page(ci
->fscache
, page
);
323 void ceph_invalidate_fscache_page(struct inode
* inode
, struct page
*page
)
325 struct ceph_inode_info
*ci
= ceph_inode(inode
);
327 fscache_wait_on_page_write(ci
->fscache
, page
);
328 fscache_uncache_page(ci
->fscache
, page
);
331 void ceph_fscache_unregister_fs(struct ceph_fs_client
* fsc
)
333 if (fsc
->revalidate_wq
)
334 destroy_workqueue(fsc
->revalidate_wq
);
336 fscache_relinquish_cookie(fsc
->fscache
, 0);
340 static void ceph_revalidate_work(struct work_struct
*work
)
344 struct ceph_inode_info
*ci
= container_of(work
, struct ceph_inode_info
,
346 struct inode
*inode
= &ci
->vfs_inode
;
348 spin_lock(&ci
->i_ceph_lock
);
349 issued
= __ceph_caps_issued(ci
, NULL
);
350 orig_gen
= ci
->i_rdcache_gen
;
351 spin_unlock(&ci
->i_ceph_lock
);
353 if (!(issued
& CEPH_CAP_FILE_CACHE
)) {
354 dout("revalidate_work lost cache before validation %p\n",
359 if (!fscache_check_consistency(ci
->fscache
))
360 fscache_invalidate(ci
->fscache
);
362 spin_lock(&ci
->i_ceph_lock
);
363 /* Update the new valid generation (backwards sanity check too) */
364 if (orig_gen
> ci
->i_fscache_gen
) {
365 ci
->i_fscache_gen
= orig_gen
;
367 spin_unlock(&ci
->i_ceph_lock
);
370 iput(&ci
->vfs_inode
);
373 void ceph_queue_revalidate(struct inode
*inode
)
375 struct ceph_fs_client
*fsc
= ceph_sb_to_client(inode
->i_sb
);
376 struct ceph_inode_info
*ci
= ceph_inode(inode
);
378 if (fsc
->revalidate_wq
== NULL
|| ci
->fscache
== NULL
)
383 if (queue_work(ceph_sb_to_client(inode
->i_sb
)->revalidate_wq
,
384 &ci
->i_revalidate_work
)) {
385 dout("ceph_queue_revalidate %p\n", inode
);
387 dout("ceph_queue_revalidate %p failed\n)", inode
);
392 void ceph_fscache_inode_init(struct ceph_inode_info
*ci
)
395 /* The first load is verifed cookie open time */
396 ci
->i_fscache_gen
= 1;
397 INIT_WORK(&ci
->i_revalidate_work
, ceph_revalidate_work
);