2 * V9FS cache definitions.
4 * Copyright (C) 2009 by Abhishek Kulkarni <adkulkar@umail.iu.edu>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to:
17 * Free Software Foundation
18 * 51 Franklin Street, Fifth Floor
19 * Boston, MA 02111-1301 USA
23 #include <linux/jiffies.h>
24 #include <linux/file.h>
25 #include <linux/slab.h>
26 #include <linux/stat.h>
27 #include <linux/sched.h>
29 #include <net/9p/9p.h>
34 #define CACHETAG_LEN 11
36 struct kmem_cache
*vcookie_cache
;
38 struct fscache_netfs v9fs_cache_netfs
= {
43 static void init_once(void *foo
)
45 struct v9fs_cookie
*vcookie
= (struct v9fs_cookie
*) foo
;
46 vcookie
->fscache
= NULL
;
48 inode_init_once(&vcookie
->inode
);
52 * v9fs_init_vcookiecache - initialize a cache for vcookies to maintain
53 * vcookie to inode mapping
55 * Returns 0 on success.
58 static int v9fs_init_vcookiecache(void)
60 vcookie_cache
= kmem_cache_create("vcookie_cache",
61 sizeof(struct v9fs_cookie
),
62 0, (SLAB_RECLAIM_ACCOUNT
|
72 * v9fs_destroy_vcookiecache - destroy the cache of vcookies
76 static void v9fs_destroy_vcookiecache(void)
78 kmem_cache_destroy(vcookie_cache
);
81 int __v9fs_cache_register(void)
84 ret
= v9fs_init_vcookiecache();
88 return fscache_register_netfs(&v9fs_cache_netfs
);
91 void __v9fs_cache_unregister(void)
93 v9fs_destroy_vcookiecache();
94 fscache_unregister_netfs(&v9fs_cache_netfs
);
98 * v9fs_random_cachetag - Generate a random tag to be associated
99 * with a new cache session.
101 * The value of jiffies is used for a fairly randomly cache tag.
105 int v9fs_random_cachetag(struct v9fs_session_info
*v9ses
)
107 v9ses
->cachetag
= kmalloc(CACHETAG_LEN
, GFP_KERNEL
);
108 if (!v9ses
->cachetag
)
111 return scnprintf(v9ses
->cachetag
, CACHETAG_LEN
, "%lu", jiffies
);
114 static uint16_t v9fs_cache_session_get_key(const void *cookie_netfs_data
,
115 void *buffer
, uint16_t bufmax
)
117 struct v9fs_session_info
*v9ses
;
120 v9ses
= (struct v9fs_session_info
*)cookie_netfs_data
;
121 P9_DPRINTK(P9_DEBUG_FSC
, "session %p buf %p size %u", v9ses
,
125 klen
= strlen(v9ses
->cachetag
);
130 memcpy(buffer
, v9ses
->cachetag
, klen
);
131 P9_DPRINTK(P9_DEBUG_FSC
, "cache session tag %s", v9ses
->cachetag
);
135 const struct fscache_cookie_def v9fs_cache_session_index_def
= {
136 .name
= "9P.session",
137 .type
= FSCACHE_COOKIE_TYPE_INDEX
,
138 .get_key
= v9fs_cache_session_get_key
,
141 void v9fs_cache_session_get_cookie(struct v9fs_session_info
*v9ses
)
143 /* If no cache session tag was specified, we generate a random one. */
144 if (!v9ses
->cachetag
)
145 v9fs_random_cachetag(v9ses
);
147 v9ses
->fscache
= fscache_acquire_cookie(v9fs_cache_netfs
.primary_index
,
148 &v9fs_cache_session_index_def
,
150 P9_DPRINTK(P9_DEBUG_FSC
, "session %p get cookie %p", v9ses
,
154 void v9fs_cache_session_put_cookie(struct v9fs_session_info
*v9ses
)
156 P9_DPRINTK(P9_DEBUG_FSC
, "session %p put cookie %p", v9ses
,
158 fscache_relinquish_cookie(v9ses
->fscache
, 0);
159 v9ses
->fscache
= NULL
;
163 static uint16_t v9fs_cache_inode_get_key(const void *cookie_netfs_data
,
164 void *buffer
, uint16_t bufmax
)
166 const struct v9fs_cookie
*vcookie
= cookie_netfs_data
;
167 memcpy(buffer
, &vcookie
->qid
->path
, sizeof(vcookie
->qid
->path
));
169 P9_DPRINTK(P9_DEBUG_FSC
, "inode %p get key %llu", &vcookie
->inode
,
171 return sizeof(vcookie
->qid
->path
);
174 static void v9fs_cache_inode_get_attr(const void *cookie_netfs_data
,
177 const struct v9fs_cookie
*vcookie
= cookie_netfs_data
;
178 *size
= i_size_read(&vcookie
->inode
);
180 P9_DPRINTK(P9_DEBUG_FSC
, "inode %p get attr %llu", &vcookie
->inode
,
184 static uint16_t v9fs_cache_inode_get_aux(const void *cookie_netfs_data
,
185 void *buffer
, uint16_t buflen
)
187 const struct v9fs_cookie
*vcookie
= cookie_netfs_data
;
188 memcpy(buffer
, &vcookie
->qid
->version
, sizeof(vcookie
->qid
->version
));
190 P9_DPRINTK(P9_DEBUG_FSC
, "inode %p get aux %u", &vcookie
->inode
,
191 vcookie
->qid
->version
);
192 return sizeof(vcookie
->qid
->version
);
196 fscache_checkaux
v9fs_cache_inode_check_aux(void *cookie_netfs_data
,
200 const struct v9fs_cookie
*vcookie
= cookie_netfs_data
;
202 if (buflen
!= sizeof(vcookie
->qid
->version
))
203 return FSCACHE_CHECKAUX_OBSOLETE
;
205 if (memcmp(buffer
, &vcookie
->qid
->version
,
206 sizeof(vcookie
->qid
->version
)))
207 return FSCACHE_CHECKAUX_OBSOLETE
;
209 return FSCACHE_CHECKAUX_OKAY
;
212 static void v9fs_cache_inode_now_uncached(void *cookie_netfs_data
)
214 struct v9fs_cookie
*vcookie
= cookie_netfs_data
;
219 pagevec_init(&pvec
, 0);
223 nr_pages
= pagevec_lookup(&pvec
, vcookie
->inode
.i_mapping
,
225 PAGEVEC_SIZE
- pagevec_count(&pvec
));
229 for (loop
= 0; loop
< nr_pages
; loop
++)
230 ClearPageFsCache(pvec
.pages
[loop
]);
232 first
= pvec
.pages
[nr_pages
- 1]->index
+ 1;
235 pagevec_release(&pvec
);
240 const struct fscache_cookie_def v9fs_cache_inode_index_def
= {
242 .type
= FSCACHE_COOKIE_TYPE_DATAFILE
,
243 .get_key
= v9fs_cache_inode_get_key
,
244 .get_attr
= v9fs_cache_inode_get_attr
,
245 .get_aux
= v9fs_cache_inode_get_aux
,
246 .check_aux
= v9fs_cache_inode_check_aux
,
247 .now_uncached
= v9fs_cache_inode_now_uncached
,
250 void v9fs_cache_inode_get_cookie(struct inode
*inode
)
252 struct v9fs_cookie
*vcookie
;
253 struct v9fs_session_info
*v9ses
;
255 if (!S_ISREG(inode
->i_mode
))
258 vcookie
= v9fs_inode2cookie(inode
);
259 if (vcookie
->fscache
)
262 v9ses
= v9fs_inode2v9ses(inode
);
263 vcookie
->fscache
= fscache_acquire_cookie(v9ses
->fscache
,
264 &v9fs_cache_inode_index_def
,
267 P9_DPRINTK(P9_DEBUG_FSC
, "inode %p get cookie %p", inode
,
271 void v9fs_cache_inode_put_cookie(struct inode
*inode
)
273 struct v9fs_cookie
*vcookie
= v9fs_inode2cookie(inode
);
275 if (!vcookie
->fscache
)
277 P9_DPRINTK(P9_DEBUG_FSC
, "inode %p put cookie %p", inode
,
280 fscache_relinquish_cookie(vcookie
->fscache
, 0);
281 vcookie
->fscache
= NULL
;
284 void v9fs_cache_inode_flush_cookie(struct inode
*inode
)
286 struct v9fs_cookie
*vcookie
= v9fs_inode2cookie(inode
);
288 if (!vcookie
->fscache
)
290 P9_DPRINTK(P9_DEBUG_FSC
, "inode %p flush cookie %p", inode
,
293 fscache_relinquish_cookie(vcookie
->fscache
, 1);
294 vcookie
->fscache
= NULL
;
297 void v9fs_cache_inode_set_cookie(struct inode
*inode
, struct file
*filp
)
299 struct v9fs_cookie
*vcookie
= v9fs_inode2cookie(inode
);
302 if (!vcookie
->fscache
)
305 spin_lock(&vcookie
->lock
);
306 fid
= filp
->private_data
;
307 if ((filp
->f_flags
& O_ACCMODE
) != O_RDONLY
)
308 v9fs_cache_inode_flush_cookie(inode
);
310 v9fs_cache_inode_get_cookie(inode
);
312 spin_unlock(&vcookie
->lock
);
315 void v9fs_cache_inode_reset_cookie(struct inode
*inode
)
317 struct v9fs_cookie
*vcookie
= v9fs_inode2cookie(inode
);
318 struct v9fs_session_info
*v9ses
;
319 struct fscache_cookie
*old
;
321 if (!vcookie
->fscache
)
324 old
= vcookie
->fscache
;
326 spin_lock(&vcookie
->lock
);
327 fscache_relinquish_cookie(vcookie
->fscache
, 1);
329 v9ses
= v9fs_inode2v9ses(inode
);
330 vcookie
->fscache
= fscache_acquire_cookie(v9ses
->fscache
,
331 &v9fs_cache_inode_index_def
,
334 P9_DPRINTK(P9_DEBUG_FSC
, "inode %p revalidating cookie old %p new %p",
335 inode
, old
, vcookie
->fscache
);
337 spin_unlock(&vcookie
->lock
);
340 int __v9fs_fscache_release_page(struct page
*page
, gfp_t gfp
)
342 struct inode
*inode
= page
->mapping
->host
;
343 struct v9fs_cookie
*vcookie
= v9fs_inode2cookie(inode
);
345 BUG_ON(!vcookie
->fscache
);
347 return fscache_maybe_release_page(vcookie
->fscache
, page
, gfp
);
350 void __v9fs_fscache_invalidate_page(struct page
*page
)
352 struct inode
*inode
= page
->mapping
->host
;
353 struct v9fs_cookie
*vcookie
= v9fs_inode2cookie(inode
);
355 BUG_ON(!vcookie
->fscache
);
357 if (PageFsCache(page
)) {
358 fscache_wait_on_page_write(vcookie
->fscache
, page
);
359 BUG_ON(!PageLocked(page
));
360 fscache_uncache_page(vcookie
->fscache
, page
);
364 static void v9fs_vfs_readpage_complete(struct page
*page
, void *data
,
368 SetPageUptodate(page
);
374 * __v9fs_readpage_from_fscache - read a page from cache
376 * Returns 0 if the pages are in cache and a BIO is submitted,
377 * 1 if the pages are not in cache and -error otherwise.
380 int __v9fs_readpage_from_fscache(struct inode
*inode
, struct page
*page
)
383 const struct v9fs_cookie
*vcookie
= v9fs_inode2cookie(inode
);
385 P9_DPRINTK(P9_DEBUG_FSC
, "inode %p page %p", inode
, page
);
386 if (!vcookie
->fscache
)
389 ret
= fscache_read_or_alloc_page(vcookie
->fscache
,
391 v9fs_vfs_readpage_complete
,
397 P9_DPRINTK(P9_DEBUG_FSC
, "page/inode not in cache %d", ret
);
400 P9_DPRINTK(P9_DEBUG_FSC
, "BIO submitted");
403 P9_DPRINTK(P9_DEBUG_FSC
, "ret %d", ret
);
409 * __v9fs_readpages_from_fscache - read multiple pages from cache
411 * Returns 0 if the pages are in cache and a BIO is submitted,
412 * 1 if the pages are not in cache and -error otherwise.
415 int __v9fs_readpages_from_fscache(struct inode
*inode
,
416 struct address_space
*mapping
,
417 struct list_head
*pages
,
421 const struct v9fs_cookie
*vcookie
= v9fs_inode2cookie(inode
);
423 P9_DPRINTK(P9_DEBUG_FSC
, "inode %p pages %u", inode
, *nr_pages
);
424 if (!vcookie
->fscache
)
427 ret
= fscache_read_or_alloc_pages(vcookie
->fscache
,
428 mapping
, pages
, nr_pages
,
429 v9fs_vfs_readpage_complete
,
431 mapping_gfp_mask(mapping
));
435 P9_DPRINTK(P9_DEBUG_FSC
, "pages/inodes not in cache %d", ret
);
438 BUG_ON(!list_empty(pages
));
439 BUG_ON(*nr_pages
!= 0);
440 P9_DPRINTK(P9_DEBUG_FSC
, "BIO submitted");
443 P9_DPRINTK(P9_DEBUG_FSC
, "ret %d", ret
);
449 * __v9fs_readpage_to_fscache - write a page to the cache
453 void __v9fs_readpage_to_fscache(struct inode
*inode
, struct page
*page
)
456 const struct v9fs_cookie
*vcookie
= v9fs_inode2cookie(inode
);
458 P9_DPRINTK(P9_DEBUG_FSC
, "inode %p page %p", inode
, page
);
459 ret
= fscache_write_page(vcookie
->fscache
, page
, GFP_KERNEL
);
460 P9_DPRINTK(P9_DEBUG_FSC
, "ret = %d", ret
);
462 v9fs_uncache_page(inode
, page
);