1 // SPDX-License-Identifier: GPL-2.0-only
3 * V9FS cache definitions.
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;
58 v9ses->fscache = fscache_acquire_cookie(v9fs_cache_netfs.primary_index,
59 &v9fs_cache_session_index_def,
61 strlen(v9ses->cachetag),
64 p9_debug(P9_DEBUG_FSC, "session %p get cookie %p\n",
65 v9ses, v9ses->fscache);
68 void v9fs_cache_session_put_cookie(struct v9fs_session_info *v9ses)
70 p9_debug(P9_DEBUG_FSC, "session %p put cookie %p\n",
71 v9ses, v9ses->fscache);
72 fscache_relinquish_cookie(v9ses->fscache, NULL, false);
73 v9ses->fscache = NULL;
77 fscache_checkaux v9fs_cache_inode_check_aux(void *cookie_netfs_data,
82 const struct v9fs_inode *v9inode = cookie_netfs_data;
84 if (buflen != sizeof(v9inode->qid.version))
85 return FSCACHE_CHECKAUX_OBSOLETE;
87 if (memcmp(buffer, &v9inode->qid.version,
88 sizeof(v9inode->qid.version)))
89 return FSCACHE_CHECKAUX_OBSOLETE;
91 return FSCACHE_CHECKAUX_OKAY;
94 const struct fscache_cookie_def v9fs_cache_inode_index_def = {
96 .type = FSCACHE_COOKIE_TYPE_DATAFILE,
97 .check_aux = v9fs_cache_inode_check_aux,
100 void v9fs_cache_inode_get_cookie(struct inode *inode)
102 struct v9fs_inode *v9inode;
103 struct v9fs_session_info *v9ses;
105 if (!S_ISREG(inode->i_mode))
108 v9inode = V9FS_I(inode);
109 if (v9inode->fscache)
112 v9ses = v9fs_inode2v9ses(inode);
113 v9inode->fscache = fscache_acquire_cookie(v9ses->fscache,
114 &v9fs_cache_inode_index_def,
116 sizeof(v9inode->qid.path),
117 &v9inode->qid.version,
118 sizeof(v9inode->qid.version),
120 i_size_read(&v9inode->vfs_inode),
123 p9_debug(P9_DEBUG_FSC, "inode %p get cookie %p\n",
124 inode, v9inode->fscache);
127 void v9fs_cache_inode_put_cookie(struct inode *inode)
129 struct v9fs_inode *v9inode = V9FS_I(inode);
131 if (!v9inode->fscache)
133 p9_debug(P9_DEBUG_FSC, "inode %p put cookie %p\n",
134 inode, v9inode->fscache);
136 fscache_relinquish_cookie(v9inode->fscache, &v9inode->qid.version,
138 v9inode->fscache = NULL;
141 void v9fs_cache_inode_flush_cookie(struct inode *inode)
143 struct v9fs_inode *v9inode = V9FS_I(inode);
145 if (!v9inode->fscache)
147 p9_debug(P9_DEBUG_FSC, "inode %p flush cookie %p\n",
148 inode, v9inode->fscache);
150 fscache_relinquish_cookie(v9inode->fscache, NULL, true);
151 v9inode->fscache = NULL;
154 void v9fs_cache_inode_set_cookie(struct inode *inode, struct file *filp)
156 struct v9fs_inode *v9inode = V9FS_I(inode);
158 if (!v9inode->fscache)
161 mutex_lock(&v9inode->fscache_lock);
163 if ((filp->f_flags & O_ACCMODE) != O_RDONLY)
164 v9fs_cache_inode_flush_cookie(inode);
166 v9fs_cache_inode_get_cookie(inode);
168 mutex_unlock(&v9inode->fscache_lock);
171 void v9fs_cache_inode_reset_cookie(struct inode *inode)
173 struct v9fs_inode *v9inode = V9FS_I(inode);
174 struct v9fs_session_info *v9ses;
175 struct fscache_cookie *old;
177 if (!v9inode->fscache)
180 old = v9inode->fscache;
182 mutex_lock(&v9inode->fscache_lock);
183 fscache_relinquish_cookie(v9inode->fscache, NULL, true);
185 v9ses = v9fs_inode2v9ses(inode);
186 v9inode->fscache = fscache_acquire_cookie(v9ses->fscache,
187 &v9fs_cache_inode_index_def,
189 sizeof(v9inode->qid.path),
190 &v9inode->qid.version,
191 sizeof(v9inode->qid.version),
193 i_size_read(&v9inode->vfs_inode),
195 p9_debug(P9_DEBUG_FSC, "inode %p revalidating cookie old %p new %p\n",
196 inode, old, v9inode->fscache);
198 mutex_unlock(&v9inode->fscache_lock);
201 int __v9fs_fscache_release_page(struct page *page, gfp_t gfp)
203 struct inode *inode = page->mapping->host;
204 struct v9fs_inode *v9inode = V9FS_I(inode);
206 BUG_ON(!v9inode->fscache);
208 return fscache_maybe_release_page(v9inode->fscache, page, gfp);
211 void __v9fs_fscache_invalidate_page(struct page *page)
213 struct inode *inode = page->mapping->host;
214 struct v9fs_inode *v9inode = V9FS_I(inode);
216 BUG_ON(!v9inode->fscache);
218 if (PageFsCache(page)) {
219 fscache_wait_on_page_write(v9inode->fscache, page);
220 BUG_ON(!PageLocked(page));
221 fscache_uncache_page(v9inode->fscache, page);
225 static void v9fs_vfs_readpage_complete(struct page *page, void *data,
229 SetPageUptodate(page);
235 * __v9fs_readpage_from_fscache - read a page from cache
237 * Returns 0 if the pages are in cache and a BIO is submitted,
238 * 1 if the pages are not in cache and -error otherwise.
241 int __v9fs_readpage_from_fscache(struct inode *inode, struct page *page)
244 const struct v9fs_inode *v9inode = V9FS_I(inode);
246 p9_debug(P9_DEBUG_FSC, "inode %p page %p\n", inode, page);
247 if (!v9inode->fscache)
250 ret = fscache_read_or_alloc_page(v9inode->fscache,
252 v9fs_vfs_readpage_complete,
258 p9_debug(P9_DEBUG_FSC, "page/inode not in cache %d\n", ret);
261 p9_debug(P9_DEBUG_FSC, "BIO submitted\n");
264 p9_debug(P9_DEBUG_FSC, "ret %d\n", ret);
270 * __v9fs_readpages_from_fscache - read multiple pages from cache
272 * Returns 0 if the pages are in cache and a BIO is submitted,
273 * 1 if the pages are not in cache and -error otherwise.
276 int __v9fs_readpages_from_fscache(struct inode *inode,
277 struct address_space *mapping,
278 struct list_head *pages,
282 const struct v9fs_inode *v9inode = V9FS_I(inode);
284 p9_debug(P9_DEBUG_FSC, "inode %p pages %u\n", inode, *nr_pages);
285 if (!v9inode->fscache)
288 ret = fscache_read_or_alloc_pages(v9inode->fscache,
289 mapping, pages, nr_pages,
290 v9fs_vfs_readpage_complete,
292 mapping_gfp_mask(mapping));
296 p9_debug(P9_DEBUG_FSC, "pages/inodes not in cache %d\n", ret);
299 BUG_ON(!list_empty(pages));
300 BUG_ON(*nr_pages != 0);
301 p9_debug(P9_DEBUG_FSC, "BIO submitted\n");
304 p9_debug(P9_DEBUG_FSC, "ret %d\n", ret);
310 * __v9fs_readpage_to_fscache - write a page to the cache
314 void __v9fs_readpage_to_fscache(struct inode *inode, struct page *page)
317 const struct v9fs_inode *v9inode = V9FS_I(inode);
319 p9_debug(P9_DEBUG_FSC, "inode %p page %p\n", inode, page);
320 ret = fscache_write_page(v9inode->fscache, page,
321 i_size_read(&v9inode->vfs_inode), GFP_KERNEL);
322 p9_debug(P9_DEBUG_FSC, "ret = %d\n", ret);
324 v9fs_uncache_page(inode, page);
328 * wait for a page to complete writing to the cache
330 void __v9fs_fscache_wait_on_page_write(struct inode *inode, struct page *page)
332 const struct v9fs_inode *v9inode = V9FS_I(inode);
333 p9_debug(P9_DEBUG_FSC, "inode %p page %p\n", inode, page);
334 if (PageFsCache(page))
335 fscache_wait_on_page_write(v9inode->fscache, page);