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/stat.h>
26 #include <linux/sched.h>
28 #include <net/9p/9p.h>
33 #define CACHETAG_LEN 11
35 struct kmem_cache
*vcookie_cache
;
37 struct fscache_netfs v9fs_cache_netfs
= {
42 static void init_once(void *foo
)
44 struct v9fs_cookie
*vcookie
= (struct v9fs_cookie
*) foo
;
45 vcookie
->fscache
= NULL
;
47 inode_init_once(&vcookie
->inode
);
51 * v9fs_init_vcookiecache - initialize a cache for vcookies to maintain
52 * vcookie to inode mapping
54 * Returns 0 on success.
57 static int v9fs_init_vcookiecache(void)
59 vcookie_cache
= kmem_cache_create("vcookie_cache",
60 sizeof(struct v9fs_cookie
),
61 0, (SLAB_RECLAIM_ACCOUNT
|
71 * v9fs_destroy_vcookiecache - destroy the cache of vcookies
75 static void v9fs_destroy_vcookiecache(void)
77 kmem_cache_destroy(vcookie_cache
);
80 int __v9fs_cache_register(void)
83 ret
= v9fs_init_vcookiecache();
87 return fscache_register_netfs(&v9fs_cache_netfs
);
90 void __v9fs_cache_unregister(void)
92 v9fs_destroy_vcookiecache();
93 fscache_unregister_netfs(&v9fs_cache_netfs
);
97 * v9fs_random_cachetag - Generate a random tag to be associated
98 * with a new cache session.
100 * The value of jiffies is used for a fairly randomly cache tag.
104 int v9fs_random_cachetag(struct v9fs_session_info
*v9ses
)
106 v9ses
->cachetag
= kmalloc(CACHETAG_LEN
, GFP_KERNEL
);
107 if (!v9ses
->cachetag
)
110 return scnprintf(v9ses
->cachetag
, CACHETAG_LEN
, "%lu", jiffies
);
113 static uint16_t v9fs_cache_session_get_key(const void *cookie_netfs_data
,
114 void *buffer
, uint16_t bufmax
)
116 struct v9fs_session_info
*v9ses
;
119 v9ses
= (struct v9fs_session_info
*)cookie_netfs_data
;
120 P9_DPRINTK(P9_DEBUG_FSC
, "session %p buf %p size %u", v9ses
,
124 klen
= strlen(v9ses
->cachetag
);
129 memcpy(buffer
, v9ses
->cachetag
, klen
);
130 P9_DPRINTK(P9_DEBUG_FSC
, "cache session tag %s", v9ses
->cachetag
);
134 const struct fscache_cookie_def v9fs_cache_session_index_def
= {
135 .name
= "9P.session",
136 .type
= FSCACHE_COOKIE_TYPE_INDEX
,
137 .get_key
= v9fs_cache_session_get_key
,
140 void v9fs_cache_session_get_cookie(struct v9fs_session_info
*v9ses
)
142 /* If no cache session tag was specified, we generate a random one. */
143 if (!v9ses
->cachetag
)
144 v9fs_random_cachetag(v9ses
);
146 v9ses
->fscache
= fscache_acquire_cookie(v9fs_cache_netfs
.primary_index
,
147 &v9fs_cache_session_index_def
,
149 P9_DPRINTK(P9_DEBUG_FSC
, "session %p get cookie %p", v9ses
,
153 void v9fs_cache_session_put_cookie(struct v9fs_session_info
*v9ses
)
155 P9_DPRINTK(P9_DEBUG_FSC
, "session %p put cookie %p", v9ses
,
157 fscache_relinquish_cookie(v9ses
->fscache
, 0);
158 v9ses
->fscache
= NULL
;
162 static uint16_t v9fs_cache_inode_get_key(const void *cookie_netfs_data
,
163 void *buffer
, uint16_t bufmax
)
165 const struct v9fs_cookie
*vcookie
= cookie_netfs_data
;
166 memcpy(buffer
, &vcookie
->qid
->path
, sizeof(vcookie
->qid
->path
));
168 P9_DPRINTK(P9_DEBUG_FSC
, "inode %p get key %llu", &vcookie
->inode
,
170 return sizeof(vcookie
->qid
->path
);
173 static void v9fs_cache_inode_get_attr(const void *cookie_netfs_data
,
176 const struct v9fs_cookie
*vcookie
= cookie_netfs_data
;
177 *size
= i_size_read(&vcookie
->inode
);
179 P9_DPRINTK(P9_DEBUG_FSC
, "inode %p get attr %llu", &vcookie
->inode
,
183 static uint16_t v9fs_cache_inode_get_aux(const void *cookie_netfs_data
,
184 void *buffer
, uint16_t buflen
)
186 const struct v9fs_cookie
*vcookie
= cookie_netfs_data
;
187 memcpy(buffer
, &vcookie
->qid
->version
, sizeof(vcookie
->qid
->version
));
189 P9_DPRINTK(P9_DEBUG_FSC
, "inode %p get aux %u", &vcookie
->inode
,
190 vcookie
->qid
->version
);
191 return sizeof(vcookie
->qid
->version
);
195 fscache_checkaux
v9fs_cache_inode_check_aux(void *cookie_netfs_data
,
199 const struct v9fs_cookie
*vcookie
= cookie_netfs_data
;
201 if (buflen
!= sizeof(vcookie
->qid
->version
))
202 return FSCACHE_CHECKAUX_OBSOLETE
;
204 if (memcmp(buffer
, &vcookie
->qid
->version
,
205 sizeof(vcookie
->qid
->version
)))
206 return FSCACHE_CHECKAUX_OBSOLETE
;
208 return FSCACHE_CHECKAUX_OKAY
;
211 static void v9fs_cache_inode_now_uncached(void *cookie_netfs_data
)
213 struct v9fs_cookie
*vcookie
= cookie_netfs_data
;
218 pagevec_init(&pvec
, 0);
222 nr_pages
= pagevec_lookup(&pvec
, vcookie
->inode
.i_mapping
,
224 PAGEVEC_SIZE
- pagevec_count(&pvec
));
228 for (loop
= 0; loop
< nr_pages
; loop
++)
229 ClearPageFsCache(pvec
.pages
[loop
]);
231 first
= pvec
.pages
[nr_pages
- 1]->index
+ 1;
234 pagevec_release(&pvec
);
239 const struct fscache_cookie_def v9fs_cache_inode_index_def
= {
241 .type
= FSCACHE_COOKIE_TYPE_DATAFILE
,
242 .get_key
= v9fs_cache_inode_get_key
,
243 .get_attr
= v9fs_cache_inode_get_attr
,
244 .get_aux
= v9fs_cache_inode_get_aux
,
245 .check_aux
= v9fs_cache_inode_check_aux
,
246 .now_uncached
= v9fs_cache_inode_now_uncached
,
249 void v9fs_cache_inode_get_cookie(struct inode
*inode
)
251 struct v9fs_cookie
*vcookie
;
252 struct v9fs_session_info
*v9ses
;
254 if (!S_ISREG(inode
->i_mode
))
257 vcookie
= v9fs_inode2cookie(inode
);
258 if (vcookie
->fscache
)
261 v9ses
= v9fs_inode2v9ses(inode
);
262 vcookie
->fscache
= fscache_acquire_cookie(v9ses
->fscache
,
263 &v9fs_cache_inode_index_def
,
266 P9_DPRINTK(P9_DEBUG_FSC
, "inode %p get cookie %p", inode
,
270 void v9fs_cache_inode_put_cookie(struct inode
*inode
)
272 struct v9fs_cookie
*vcookie
= v9fs_inode2cookie(inode
);
274 if (!vcookie
->fscache
)
276 P9_DPRINTK(P9_DEBUG_FSC
, "inode %p put cookie %p", inode
,
279 fscache_relinquish_cookie(vcookie
->fscache
, 0);
280 vcookie
->fscache
= NULL
;
283 void v9fs_cache_inode_flush_cookie(struct inode
*inode
)
285 struct v9fs_cookie
*vcookie
= v9fs_inode2cookie(inode
);
287 if (!vcookie
->fscache
)
289 P9_DPRINTK(P9_DEBUG_FSC
, "inode %p flush cookie %p", inode
,
292 fscache_relinquish_cookie(vcookie
->fscache
, 1);
293 vcookie
->fscache
= NULL
;
296 void v9fs_cache_inode_set_cookie(struct inode
*inode
, struct file
*filp
)
298 struct v9fs_cookie
*vcookie
= v9fs_inode2cookie(inode
);
301 if (!vcookie
->fscache
)
304 spin_lock(&vcookie
->lock
);
305 fid
= filp
->private_data
;
306 if ((filp
->f_flags
& O_ACCMODE
) != O_RDONLY
)
307 v9fs_cache_inode_flush_cookie(inode
);
309 v9fs_cache_inode_get_cookie(inode
);
311 spin_unlock(&vcookie
->lock
);
314 void v9fs_cache_inode_reset_cookie(struct inode
*inode
)
316 struct v9fs_cookie
*vcookie
= v9fs_inode2cookie(inode
);
317 struct v9fs_session_info
*v9ses
;
318 struct fscache_cookie
*old
;
320 if (!vcookie
->fscache
)
323 old
= vcookie
->fscache
;
325 spin_lock(&vcookie
->lock
);
326 fscache_relinquish_cookie(vcookie
->fscache
, 1);
328 v9ses
= v9fs_inode2v9ses(inode
);
329 vcookie
->fscache
= fscache_acquire_cookie(v9ses
->fscache
,
330 &v9fs_cache_inode_index_def
,
333 P9_DPRINTK(P9_DEBUG_FSC
, "inode %p revalidating cookie old %p new %p",
334 inode
, old
, vcookie
->fscache
);
336 spin_unlock(&vcookie
->lock
);
339 int __v9fs_fscache_release_page(struct page
*page
, gfp_t gfp
)
341 struct inode
*inode
= page
->mapping
->host
;
342 struct v9fs_cookie
*vcookie
= v9fs_inode2cookie(inode
);
344 BUG_ON(!vcookie
->fscache
);
346 return fscache_maybe_release_page(vcookie
->fscache
, page
, gfp
);
349 void __v9fs_fscache_invalidate_page(struct page
*page
)
351 struct inode
*inode
= page
->mapping
->host
;
352 struct v9fs_cookie
*vcookie
= v9fs_inode2cookie(inode
);
354 BUG_ON(!vcookie
->fscache
);
356 if (PageFsCache(page
)) {
357 fscache_wait_on_page_write(vcookie
->fscache
, page
);
358 BUG_ON(!PageLocked(page
));
359 fscache_uncache_page(vcookie
->fscache
, page
);
363 static void v9fs_vfs_readpage_complete(struct page
*page
, void *data
,
367 SetPageUptodate(page
);
373 * __v9fs_readpage_from_fscache - read a page from cache
375 * Returns 0 if the pages are in cache and a BIO is submitted,
376 * 1 if the pages are not in cache and -error otherwise.
379 int __v9fs_readpage_from_fscache(struct inode
*inode
, struct page
*page
)
382 const struct v9fs_cookie
*vcookie
= v9fs_inode2cookie(inode
);
384 P9_DPRINTK(P9_DEBUG_FSC
, "inode %p page %p", inode
, page
);
385 if (!vcookie
->fscache
)
388 ret
= fscache_read_or_alloc_page(vcookie
->fscache
,
390 v9fs_vfs_readpage_complete
,
396 P9_DPRINTK(P9_DEBUG_FSC
, "page/inode not in cache %d", ret
);
399 P9_DPRINTK(P9_DEBUG_FSC
, "BIO submitted");
402 P9_DPRINTK(P9_DEBUG_FSC
, "ret %d", ret
);
408 * __v9fs_readpages_from_fscache - read multiple pages from cache
410 * Returns 0 if the pages are in cache and a BIO is submitted,
411 * 1 if the pages are not in cache and -error otherwise.
414 int __v9fs_readpages_from_fscache(struct inode
*inode
,
415 struct address_space
*mapping
,
416 struct list_head
*pages
,
420 const struct v9fs_cookie
*vcookie
= v9fs_inode2cookie(inode
);
422 P9_DPRINTK(P9_DEBUG_FSC
, "inode %p pages %u", inode
, *nr_pages
);
423 if (!vcookie
->fscache
)
426 ret
= fscache_read_or_alloc_pages(vcookie
->fscache
,
427 mapping
, pages
, nr_pages
,
428 v9fs_vfs_readpage_complete
,
430 mapping_gfp_mask(mapping
));
434 P9_DPRINTK(P9_DEBUG_FSC
, "pages/inodes not in cache %d", ret
);
437 BUG_ON(!list_empty(pages
));
438 BUG_ON(*nr_pages
!= 0);
439 P9_DPRINTK(P9_DEBUG_FSC
, "BIO submitted");
442 P9_DPRINTK(P9_DEBUG_FSC
, "ret %d", ret
);
448 * __v9fs_readpage_to_fscache - write a page to the cache
452 void __v9fs_readpage_to_fscache(struct inode
*inode
, struct page
*page
)
455 const struct v9fs_cookie
*vcookie
= v9fs_inode2cookie(inode
);
457 P9_DPRINTK(P9_DEBUG_FSC
, "inode %p page %p", inode
, page
);
458 ret
= fscache_write_page(vcookie
->fscache
, page
, GFP_KERNEL
);
459 P9_DPRINTK(P9_DEBUG_FSC
, "ret = %d", ret
);
461 v9fs_uncache_page(inode
, page
);