drm/i915/ibx: Ensure the HW is powered during PLL HW readout
[linux/fpc-iii.git] / fs / ceph / cache.c
bloba351480dbabc95891e4b61f83fb92485f8ea7b18
1 /*
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
24 #include "super.h"
25 #include "cache.h"
27 struct ceph_aux_inode {
28 struct timespec mtime;
29 loff_t size;
32 struct fscache_netfs ceph_cache_netfs = {
33 .name = "ceph",
34 .version = 0,
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;
41 uint16_t klen;
43 klen = sizeof(fsc->client->fsid);
44 if (klen > maxbuf)
45 return 0;
47 memcpy(buffer, &fsc->client->fsid, klen);
48 return klen;
51 static const struct fscache_cookie_def ceph_fscache_fsid_object_def = {
52 .name = "CEPH.fsid",
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,
71 fsc, true);
73 if (fsc->fscache == NULL) {
74 pr_err("Unable to resgister fsid: %p fscache cookie", fsc);
75 return 0;
78 fsc->revalidate_wq = alloc_workqueue("ceph-revalidate", 0, 1);
79 if (fsc->revalidate_wq == NULL)
80 return -ENOMEM;
82 return 0;
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;
89 uint16_t klen;
91 /* use ceph virtual inode (id + snapshot) */
92 klen = sizeof(ci->i_vino);
93 if (klen > maxbuf)
94 return 0;
96 memcpy(buffer, &ci->i_vino, klen);
97 return 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 = i_size_read(inode);
111 memcpy(buffer, &aux, sizeof(aux));
113 return sizeof(aux);
116 static void ceph_fscache_inode_get_attr(const void *cookie_netfs_data,
117 uint64_t *size)
119 const struct ceph_inode_info* ci = cookie_netfs_data;
120 *size = i_size_read(&ci->vfs_inode);
123 static enum fscache_checkaux ceph_fscache_inode_check_aux(
124 void *cookie_netfs_data, const void *data, uint16_t dlen)
126 struct ceph_aux_inode aux;
127 struct ceph_inode_info* ci = cookie_netfs_data;
128 struct inode* inode = &ci->vfs_inode;
130 if (dlen != sizeof(aux))
131 return FSCACHE_CHECKAUX_OBSOLETE;
133 memset(&aux, 0, sizeof(aux));
134 aux.mtime = inode->i_mtime;
135 aux.size = i_size_read(inode);
137 if (memcmp(data, &aux, sizeof(aux)) != 0)
138 return FSCACHE_CHECKAUX_OBSOLETE;
140 dout("ceph inode 0x%p cached okay", ci);
141 return FSCACHE_CHECKAUX_OKAY;
144 static void ceph_fscache_inode_now_uncached(void* cookie_netfs_data)
146 struct ceph_inode_info* ci = cookie_netfs_data;
147 struct pagevec pvec;
148 pgoff_t first;
149 int loop, nr_pages;
151 pagevec_init(&pvec, 0);
152 first = 0;
154 dout("ceph inode 0x%p now uncached", ci);
156 while (1) {
157 nr_pages = pagevec_lookup(&pvec, ci->vfs_inode.i_mapping, first,
158 PAGEVEC_SIZE - pagevec_count(&pvec));
160 if (!nr_pages)
161 break;
163 for (loop = 0; loop < nr_pages; loop++)
164 ClearPageFsCache(pvec.pages[loop]);
166 first = pvec.pages[nr_pages - 1]->index + 1;
168 pvec.nr = nr_pages;
169 pagevec_release(&pvec);
170 cond_resched();
174 static const struct fscache_cookie_def ceph_fscache_inode_object_def = {
175 .name = "CEPH.inode",
176 .type = FSCACHE_COOKIE_TYPE_DATAFILE,
177 .get_key = ceph_fscache_inode_get_key,
178 .get_attr = ceph_fscache_inode_get_attr,
179 .get_aux = ceph_fscache_inode_get_aux,
180 .check_aux = ceph_fscache_inode_check_aux,
181 .now_uncached = ceph_fscache_inode_now_uncached,
184 void ceph_fscache_register_inode_cookie(struct ceph_fs_client* fsc,
185 struct ceph_inode_info* ci)
187 struct inode* inode = &ci->vfs_inode;
189 /* No caching for filesystem */
190 if (fsc->fscache == NULL)
191 return;
193 /* Only cache for regular files that are read only */
194 if ((ci->vfs_inode.i_mode & S_IFREG) == 0)
195 return;
197 /* Avoid multiple racing open requests */
198 inode_lock(inode);
200 if (ci->fscache)
201 goto done;
203 ci->fscache = fscache_acquire_cookie(fsc->fscache,
204 &ceph_fscache_inode_object_def,
205 ci, true);
206 fscache_check_consistency(ci->fscache);
207 done:
208 inode_unlock(inode);
212 void ceph_fscache_unregister_inode_cookie(struct ceph_inode_info* ci)
214 struct fscache_cookie* cookie;
216 if ((cookie = ci->fscache) == NULL)
217 return;
219 ci->fscache = NULL;
221 fscache_uncache_all_inode_pages(cookie, &ci->vfs_inode);
222 fscache_relinquish_cookie(cookie, 0);
225 static void ceph_vfs_readpage_complete(struct page *page, void *data, int error)
227 if (!error)
228 SetPageUptodate(page);
231 static void ceph_vfs_readpage_complete_unlock(struct page *page, void *data, int error)
233 if (!error)
234 SetPageUptodate(page);
236 unlock_page(page);
239 static inline int cache_valid(struct ceph_inode_info *ci)
241 return ((ceph_caps_issued(ci) & CEPH_CAP_FILE_CACHE) &&
242 (ci->i_fscache_gen == ci->i_rdcache_gen));
246 /* Atempt to read from the fscache,
248 * This function is called from the readpage_nounlock context. DO NOT attempt to
249 * unlock the page here (or in the callback).
251 int ceph_readpage_from_fscache(struct inode *inode, struct page *page)
253 struct ceph_inode_info *ci = ceph_inode(inode);
254 int ret;
256 if (!cache_valid(ci))
257 return -ENOBUFS;
259 ret = fscache_read_or_alloc_page(ci->fscache, page,
260 ceph_vfs_readpage_complete, NULL,
261 GFP_KERNEL);
263 switch (ret) {
264 case 0: /* Page found */
265 dout("page read submitted\n");
266 return 0;
267 case -ENOBUFS: /* Pages were not found, and can't be */
268 case -ENODATA: /* Pages were not found */
269 dout("page/inode not in cache\n");
270 return ret;
271 default:
272 dout("%s: unknown error ret = %i\n", __func__, ret);
273 return ret;
277 int ceph_readpages_from_fscache(struct inode *inode,
278 struct address_space *mapping,
279 struct list_head *pages,
280 unsigned *nr_pages)
282 struct ceph_inode_info *ci = ceph_inode(inode);
283 int ret;
285 if (!cache_valid(ci))
286 return -ENOBUFS;
288 ret = fscache_read_or_alloc_pages(ci->fscache, mapping, pages, nr_pages,
289 ceph_vfs_readpage_complete_unlock,
290 NULL, mapping_gfp_mask(mapping));
292 switch (ret) {
293 case 0: /* All pages found */
294 dout("all-page read submitted\n");
295 return 0;
296 case -ENOBUFS: /* Some pages were not found, and can't be */
297 case -ENODATA: /* some pages were not found */
298 dout("page/inode not in cache\n");
299 return ret;
300 default:
301 dout("%s: unknown error ret = %i\n", __func__, ret);
302 return ret;
306 void ceph_readpage_to_fscache(struct inode *inode, struct page *page)
308 struct ceph_inode_info *ci = ceph_inode(inode);
309 int ret;
311 if (!PageFsCache(page))
312 return;
314 if (!cache_valid(ci))
315 return;
317 ret = fscache_write_page(ci->fscache, page, GFP_KERNEL);
318 if (ret)
319 fscache_uncache_page(ci->fscache, page);
322 void ceph_invalidate_fscache_page(struct inode* inode, struct page *page)
324 struct ceph_inode_info *ci = ceph_inode(inode);
326 if (!PageFsCache(page))
327 return;
329 fscache_wait_on_page_write(ci->fscache, page);
330 fscache_uncache_page(ci->fscache, page);
333 void ceph_fscache_unregister_fs(struct ceph_fs_client* fsc)
335 if (fsc->revalidate_wq)
336 destroy_workqueue(fsc->revalidate_wq);
338 fscache_relinquish_cookie(fsc->fscache, 0);
339 fsc->fscache = NULL;
342 static void ceph_revalidate_work(struct work_struct *work)
344 int issued;
345 u32 orig_gen;
346 struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
347 i_revalidate_work);
348 struct inode *inode = &ci->vfs_inode;
350 spin_lock(&ci->i_ceph_lock);
351 issued = __ceph_caps_issued(ci, NULL);
352 orig_gen = ci->i_rdcache_gen;
353 spin_unlock(&ci->i_ceph_lock);
355 if (!(issued & CEPH_CAP_FILE_CACHE)) {
356 dout("revalidate_work lost cache before validation %p\n",
357 inode);
358 goto out;
361 if (!fscache_check_consistency(ci->fscache))
362 fscache_invalidate(ci->fscache);
364 spin_lock(&ci->i_ceph_lock);
365 /* Update the new valid generation (backwards sanity check too) */
366 if (orig_gen > ci->i_fscache_gen) {
367 ci->i_fscache_gen = orig_gen;
369 spin_unlock(&ci->i_ceph_lock);
371 out:
372 iput(&ci->vfs_inode);
375 void ceph_queue_revalidate(struct inode *inode)
377 struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
378 struct ceph_inode_info *ci = ceph_inode(inode);
380 if (fsc->revalidate_wq == NULL || ci->fscache == NULL)
381 return;
383 ihold(inode);
385 if (queue_work(ceph_sb_to_client(inode->i_sb)->revalidate_wq,
386 &ci->i_revalidate_work)) {
387 dout("ceph_queue_revalidate %p\n", inode);
388 } else {
389 dout("ceph_queue_revalidate %p failed\n)", inode);
390 iput(inode);
394 void ceph_fscache_inode_init(struct ceph_inode_info *ci)
396 ci->fscache = NULL;
397 /* The first load is verifed cookie open time */
398 ci->i_fscache_gen = 1;
399 INIT_WORK(&ci->i_revalidate_work, ceph_revalidate_work);