1 // SPDX-License-Identifier: GPL-2.0-only
3 * V9FS cache definitions.
5 * Copyright (C) 2009 by Abhishek Kulkarni <adkulkar@umail.iu.edu>
8 #include <linux/jiffies.h>
9 #include <linux/file.h>
10 #include <linux/slab.h>
11 #include <linux/stat.h>
12 #include <linux/sched.h>
14 #include <net/9p/9p.h>
19 #define CACHETAG_LEN 11
21 struct fscache_netfs v9fs_cache_netfs
= {
27 * v9fs_random_cachetag - Generate a random tag to be associated
28 * with a new cache session.
30 * The value of jiffies is used for a fairly randomly cache tag.
34 int v9fs_random_cachetag(struct v9fs_session_info
*v9ses
)
36 v9ses
->cachetag
= kmalloc(CACHETAG_LEN
, GFP_KERNEL
);
40 return scnprintf(v9ses
->cachetag
, CACHETAG_LEN
, "%lu", jiffies
);
43 const struct fscache_cookie_def v9fs_cache_session_index_def
= {
45 .type
= FSCACHE_COOKIE_TYPE_INDEX
,
48 void v9fs_cache_session_get_cookie(struct v9fs_session_info
*v9ses
)
50 /* If no cache session tag was specified, we generate a random one. */
51 if (!v9ses
->cachetag
) {
52 if (v9fs_random_cachetag(v9ses
) < 0) {
53 v9ses
->fscache
= NULL
;
54 kfree(v9ses
->cachetag
);
55 v9ses
->cachetag
= NULL
;
60 v9ses
->fscache
= fscache_acquire_cookie(v9fs_cache_netfs
.primary_index
,
61 &v9fs_cache_session_index_def
,
63 strlen(v9ses
->cachetag
),
66 p9_debug(P9_DEBUG_FSC
, "session %p get cookie %p\n",
67 v9ses
, v9ses
->fscache
);
70 void v9fs_cache_session_put_cookie(struct v9fs_session_info
*v9ses
)
72 p9_debug(P9_DEBUG_FSC
, "session %p put cookie %p\n",
73 v9ses
, v9ses
->fscache
);
74 fscache_relinquish_cookie(v9ses
->fscache
, NULL
, false);
75 v9ses
->fscache
= NULL
;
79 fscache_checkaux
v9fs_cache_inode_check_aux(void *cookie_netfs_data
,
84 const struct v9fs_inode
*v9inode
= cookie_netfs_data
;
86 if (buflen
!= sizeof(v9inode
->qid
.version
))
87 return FSCACHE_CHECKAUX_OBSOLETE
;
89 if (memcmp(buffer
, &v9inode
->qid
.version
,
90 sizeof(v9inode
->qid
.version
)))
91 return FSCACHE_CHECKAUX_OBSOLETE
;
93 return FSCACHE_CHECKAUX_OKAY
;
96 const struct fscache_cookie_def v9fs_cache_inode_index_def
= {
98 .type
= FSCACHE_COOKIE_TYPE_DATAFILE
,
99 .check_aux
= v9fs_cache_inode_check_aux
,
102 void v9fs_cache_inode_get_cookie(struct inode
*inode
)
104 struct v9fs_inode
*v9inode
;
105 struct v9fs_session_info
*v9ses
;
107 if (!S_ISREG(inode
->i_mode
))
110 v9inode
= V9FS_I(inode
);
111 if (v9inode
->fscache
)
114 v9ses
= v9fs_inode2v9ses(inode
);
115 v9inode
->fscache
= fscache_acquire_cookie(v9ses
->fscache
,
116 &v9fs_cache_inode_index_def
,
118 sizeof(v9inode
->qid
.path
),
119 &v9inode
->qid
.version
,
120 sizeof(v9inode
->qid
.version
),
122 i_size_read(&v9inode
->vfs_inode
),
125 p9_debug(P9_DEBUG_FSC
, "inode %p get cookie %p\n",
126 inode
, v9inode
->fscache
);
129 void v9fs_cache_inode_put_cookie(struct inode
*inode
)
131 struct v9fs_inode
*v9inode
= V9FS_I(inode
);
133 if (!v9inode
->fscache
)
135 p9_debug(P9_DEBUG_FSC
, "inode %p put cookie %p\n",
136 inode
, v9inode
->fscache
);
138 fscache_relinquish_cookie(v9inode
->fscache
, &v9inode
->qid
.version
,
140 v9inode
->fscache
= NULL
;
143 void v9fs_cache_inode_flush_cookie(struct inode
*inode
)
145 struct v9fs_inode
*v9inode
= V9FS_I(inode
);
147 if (!v9inode
->fscache
)
149 p9_debug(P9_DEBUG_FSC
, "inode %p flush cookie %p\n",
150 inode
, v9inode
->fscache
);
152 fscache_relinquish_cookie(v9inode
->fscache
, NULL
, true);
153 v9inode
->fscache
= NULL
;
156 void v9fs_cache_inode_set_cookie(struct inode
*inode
, struct file
*filp
)
158 struct v9fs_inode
*v9inode
= V9FS_I(inode
);
160 if (!v9inode
->fscache
)
163 mutex_lock(&v9inode
->fscache_lock
);
165 if ((filp
->f_flags
& O_ACCMODE
) != O_RDONLY
)
166 v9fs_cache_inode_flush_cookie(inode
);
168 v9fs_cache_inode_get_cookie(inode
);
170 mutex_unlock(&v9inode
->fscache_lock
);
173 void v9fs_cache_inode_reset_cookie(struct inode
*inode
)
175 struct v9fs_inode
*v9inode
= V9FS_I(inode
);
176 struct v9fs_session_info
*v9ses
;
177 struct fscache_cookie
*old
;
179 if (!v9inode
->fscache
)
182 old
= v9inode
->fscache
;
184 mutex_lock(&v9inode
->fscache_lock
);
185 fscache_relinquish_cookie(v9inode
->fscache
, NULL
, true);
187 v9ses
= v9fs_inode2v9ses(inode
);
188 v9inode
->fscache
= fscache_acquire_cookie(v9ses
->fscache
,
189 &v9fs_cache_inode_index_def
,
191 sizeof(v9inode
->qid
.path
),
192 &v9inode
->qid
.version
,
193 sizeof(v9inode
->qid
.version
),
195 i_size_read(&v9inode
->vfs_inode
),
197 p9_debug(P9_DEBUG_FSC
, "inode %p revalidating cookie old %p new %p\n",
198 inode
, old
, v9inode
->fscache
);
200 mutex_unlock(&v9inode
->fscache_lock
);
203 int __v9fs_fscache_release_page(struct page
*page
, gfp_t gfp
)
205 struct inode
*inode
= page
->mapping
->host
;
206 struct v9fs_inode
*v9inode
= V9FS_I(inode
);
208 BUG_ON(!v9inode
->fscache
);
210 return fscache_maybe_release_page(v9inode
->fscache
, page
, gfp
);
213 void __v9fs_fscache_invalidate_page(struct page
*page
)
215 struct inode
*inode
= page
->mapping
->host
;
216 struct v9fs_inode
*v9inode
= V9FS_I(inode
);
218 BUG_ON(!v9inode
->fscache
);
220 if (PageFsCache(page
)) {
221 fscache_wait_on_page_write(v9inode
->fscache
, page
);
222 BUG_ON(!PageLocked(page
));
223 fscache_uncache_page(v9inode
->fscache
, page
);
227 static void v9fs_vfs_readpage_complete(struct page
*page
, void *data
,
231 SetPageUptodate(page
);
237 * __v9fs_readpage_from_fscache - read a page from cache
239 * Returns 0 if the pages are in cache and a BIO is submitted,
240 * 1 if the pages are not in cache and -error otherwise.
243 int __v9fs_readpage_from_fscache(struct inode
*inode
, struct page
*page
)
246 const struct v9fs_inode
*v9inode
= V9FS_I(inode
);
248 p9_debug(P9_DEBUG_FSC
, "inode %p page %p\n", inode
, page
);
249 if (!v9inode
->fscache
)
252 ret
= fscache_read_or_alloc_page(v9inode
->fscache
,
254 v9fs_vfs_readpage_complete
,
260 p9_debug(P9_DEBUG_FSC
, "page/inode not in cache %d\n", ret
);
263 p9_debug(P9_DEBUG_FSC
, "BIO submitted\n");
266 p9_debug(P9_DEBUG_FSC
, "ret %d\n", ret
);
272 * __v9fs_readpages_from_fscache - read multiple pages from cache
274 * Returns 0 if the pages are in cache and a BIO is submitted,
275 * 1 if the pages are not in cache and -error otherwise.
278 int __v9fs_readpages_from_fscache(struct inode
*inode
,
279 struct address_space
*mapping
,
280 struct list_head
*pages
,
284 const struct v9fs_inode
*v9inode
= V9FS_I(inode
);
286 p9_debug(P9_DEBUG_FSC
, "inode %p pages %u\n", inode
, *nr_pages
);
287 if (!v9inode
->fscache
)
290 ret
= fscache_read_or_alloc_pages(v9inode
->fscache
,
291 mapping
, pages
, nr_pages
,
292 v9fs_vfs_readpage_complete
,
294 mapping_gfp_mask(mapping
));
298 p9_debug(P9_DEBUG_FSC
, "pages/inodes not in cache %d\n", ret
);
301 BUG_ON(!list_empty(pages
));
302 BUG_ON(*nr_pages
!= 0);
303 p9_debug(P9_DEBUG_FSC
, "BIO submitted\n");
306 p9_debug(P9_DEBUG_FSC
, "ret %d\n", ret
);
312 * __v9fs_readpage_to_fscache - write a page to the cache
316 void __v9fs_readpage_to_fscache(struct inode
*inode
, struct page
*page
)
319 const struct v9fs_inode
*v9inode
= V9FS_I(inode
);
321 p9_debug(P9_DEBUG_FSC
, "inode %p page %p\n", inode
, page
);
322 ret
= fscache_write_page(v9inode
->fscache
, page
,
323 i_size_read(&v9inode
->vfs_inode
), GFP_KERNEL
);
324 p9_debug(P9_DEBUG_FSC
, "ret = %d\n", ret
);
326 v9fs_uncache_page(inode
, page
);
330 * wait for a page to complete writing to the cache
332 void __v9fs_fscache_wait_on_page_write(struct inode
*inode
, struct page
*page
)
334 const struct v9fs_inode
*v9inode
= V9FS_I(inode
);
335 p9_debug(P9_DEBUG_FSC
, "inode %p page %p\n", inode
, page
);
336 if (PageFsCache(page
))
337 fscache_wait_on_page_write(v9inode
->fscache
, page
);