GNU Linux-libre 4.19.207-gnu1
[releases.git] / fs / 9p / cache.c
1 /*
2  * V9FS cache definitions.
3  *
4  *  Copyright (C) 2009 by Abhishek Kulkarni <adkulkar@umail.iu.edu>
5  *
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.
9  *
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.
14  *
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
20  *
21  */
22
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>
28 #include <linux/fs.h>
29 #include <net/9p/9p.h>
30
31 #include "v9fs.h"
32 #include "cache.h"
33
34 #define CACHETAG_LEN  11
35
36 struct fscache_netfs v9fs_cache_netfs = {
37         .name           = "9p",
38         .version        = 0,
39 };
40
41 /**
42  * v9fs_random_cachetag - Generate a random tag to be associated
43  *                        with a new cache session.
44  *
45  * The value of jiffies is used for a fairly randomly cache tag.
46  */
47
48 static
49 int v9fs_random_cachetag(struct v9fs_session_info *v9ses)
50 {
51         v9ses->cachetag = kmalloc(CACHETAG_LEN, GFP_KERNEL);
52         if (!v9ses->cachetag)
53                 return -ENOMEM;
54
55         return scnprintf(v9ses->cachetag, CACHETAG_LEN, "%lu", jiffies);
56 }
57
58 const struct fscache_cookie_def v9fs_cache_session_index_def = {
59         .name           = "9P.session",
60         .type           = FSCACHE_COOKIE_TYPE_INDEX,
61 };
62
63 void v9fs_cache_session_get_cookie(struct v9fs_session_info *v9ses)
64 {
65         /* If no cache session tag was specified, we generate a random one. */
66         if (!v9ses->cachetag) {
67                 if (v9fs_random_cachetag(v9ses) < 0) {
68                         v9ses->fscache = NULL;
69                         kfree(v9ses->cachetag);
70                         v9ses->cachetag = NULL;
71                         return;
72                 }
73         }
74
75         v9ses->fscache = fscache_acquire_cookie(v9fs_cache_netfs.primary_index,
76                                                 &v9fs_cache_session_index_def,
77                                                 v9ses->cachetag,
78                                                 strlen(v9ses->cachetag),
79                                                 NULL, 0,
80                                                 v9ses, 0, true);
81         p9_debug(P9_DEBUG_FSC, "session %p get cookie %p\n",
82                  v9ses, v9ses->fscache);
83 }
84
85 void v9fs_cache_session_put_cookie(struct v9fs_session_info *v9ses)
86 {
87         p9_debug(P9_DEBUG_FSC, "session %p put cookie %p\n",
88                  v9ses, v9ses->fscache);
89         fscache_relinquish_cookie(v9ses->fscache, NULL, false);
90         v9ses->fscache = NULL;
91 }
92
93 static enum
94 fscache_checkaux v9fs_cache_inode_check_aux(void *cookie_netfs_data,
95                                             const void *buffer,
96                                             uint16_t buflen,
97                                             loff_t object_size)
98 {
99         const struct v9fs_inode *v9inode = cookie_netfs_data;
100
101         if (buflen != sizeof(v9inode->qid.version))
102                 return FSCACHE_CHECKAUX_OBSOLETE;
103
104         if (memcmp(buffer, &v9inode->qid.version,
105                    sizeof(v9inode->qid.version)))
106                 return FSCACHE_CHECKAUX_OBSOLETE;
107
108         return FSCACHE_CHECKAUX_OKAY;
109 }
110
111 const struct fscache_cookie_def v9fs_cache_inode_index_def = {
112         .name           = "9p.inode",
113         .type           = FSCACHE_COOKIE_TYPE_DATAFILE,
114         .check_aux      = v9fs_cache_inode_check_aux,
115 };
116
117 void v9fs_cache_inode_get_cookie(struct inode *inode)
118 {
119         struct v9fs_inode *v9inode;
120         struct v9fs_session_info *v9ses;
121
122         if (!S_ISREG(inode->i_mode))
123                 return;
124
125         v9inode = V9FS_I(inode);
126         if (v9inode->fscache)
127                 return;
128
129         v9ses = v9fs_inode2v9ses(inode);
130         v9inode->fscache = fscache_acquire_cookie(v9ses->fscache,
131                                                   &v9fs_cache_inode_index_def,
132                                                   &v9inode->qid.path,
133                                                   sizeof(v9inode->qid.path),
134                                                   &v9inode->qid.version,
135                                                   sizeof(v9inode->qid.version),
136                                                   v9inode,
137                                                   i_size_read(&v9inode->vfs_inode),
138                                                   true);
139
140         p9_debug(P9_DEBUG_FSC, "inode %p get cookie %p\n",
141                  inode, v9inode->fscache);
142 }
143
144 void v9fs_cache_inode_put_cookie(struct inode *inode)
145 {
146         struct v9fs_inode *v9inode = V9FS_I(inode);
147
148         if (!v9inode->fscache)
149                 return;
150         p9_debug(P9_DEBUG_FSC, "inode %p put cookie %p\n",
151                  inode, v9inode->fscache);
152
153         fscache_relinquish_cookie(v9inode->fscache, &v9inode->qid.version,
154                                   false);
155         v9inode->fscache = NULL;
156 }
157
158 void v9fs_cache_inode_flush_cookie(struct inode *inode)
159 {
160         struct v9fs_inode *v9inode = V9FS_I(inode);
161
162         if (!v9inode->fscache)
163                 return;
164         p9_debug(P9_DEBUG_FSC, "inode %p flush cookie %p\n",
165                  inode, v9inode->fscache);
166
167         fscache_relinquish_cookie(v9inode->fscache, NULL, true);
168         v9inode->fscache = NULL;
169 }
170
171 void v9fs_cache_inode_set_cookie(struct inode *inode, struct file *filp)
172 {
173         struct v9fs_inode *v9inode = V9FS_I(inode);
174
175         if (!v9inode->fscache)
176                 return;
177
178         mutex_lock(&v9inode->fscache_lock);
179
180         if ((filp->f_flags & O_ACCMODE) != O_RDONLY)
181                 v9fs_cache_inode_flush_cookie(inode);
182         else
183                 v9fs_cache_inode_get_cookie(inode);
184
185         mutex_unlock(&v9inode->fscache_lock);
186 }
187
188 void v9fs_cache_inode_reset_cookie(struct inode *inode)
189 {
190         struct v9fs_inode *v9inode = V9FS_I(inode);
191         struct v9fs_session_info *v9ses;
192         struct fscache_cookie *old;
193
194         if (!v9inode->fscache)
195                 return;
196
197         old = v9inode->fscache;
198
199         mutex_lock(&v9inode->fscache_lock);
200         fscache_relinquish_cookie(v9inode->fscache, NULL, true);
201
202         v9ses = v9fs_inode2v9ses(inode);
203         v9inode->fscache = fscache_acquire_cookie(v9ses->fscache,
204                                                   &v9fs_cache_inode_index_def,
205                                                   &v9inode->qid.path,
206                                                   sizeof(v9inode->qid.path),
207                                                   &v9inode->qid.version,
208                                                   sizeof(v9inode->qid.version),
209                                                   v9inode,
210                                                   i_size_read(&v9inode->vfs_inode),
211                                                   true);
212         p9_debug(P9_DEBUG_FSC, "inode %p revalidating cookie old %p new %p\n",
213                  inode, old, v9inode->fscache);
214
215         mutex_unlock(&v9inode->fscache_lock);
216 }
217
218 int __v9fs_fscache_release_page(struct page *page, gfp_t gfp)
219 {
220         struct inode *inode = page->mapping->host;
221         struct v9fs_inode *v9inode = V9FS_I(inode);
222
223         BUG_ON(!v9inode->fscache);
224
225         return fscache_maybe_release_page(v9inode->fscache, page, gfp);
226 }
227
228 void __v9fs_fscache_invalidate_page(struct page *page)
229 {
230         struct inode *inode = page->mapping->host;
231         struct v9fs_inode *v9inode = V9FS_I(inode);
232
233         BUG_ON(!v9inode->fscache);
234
235         if (PageFsCache(page)) {
236                 fscache_wait_on_page_write(v9inode->fscache, page);
237                 BUG_ON(!PageLocked(page));
238                 fscache_uncache_page(v9inode->fscache, page);
239         }
240 }
241
242 static void v9fs_vfs_readpage_complete(struct page *page, void *data,
243                                        int error)
244 {
245         if (!error)
246                 SetPageUptodate(page);
247
248         unlock_page(page);
249 }
250
251 /**
252  * __v9fs_readpage_from_fscache - read a page from cache
253  *
254  * Returns 0 if the pages are in cache and a BIO is submitted,
255  * 1 if the pages are not in cache and -error otherwise.
256  */
257
258 int __v9fs_readpage_from_fscache(struct inode *inode, struct page *page)
259 {
260         int ret;
261         const struct v9fs_inode *v9inode = V9FS_I(inode);
262
263         p9_debug(P9_DEBUG_FSC, "inode %p page %p\n", inode, page);
264         if (!v9inode->fscache)
265                 return -ENOBUFS;
266
267         ret = fscache_read_or_alloc_page(v9inode->fscache,
268                                          page,
269                                          v9fs_vfs_readpage_complete,
270                                          NULL,
271                                          GFP_KERNEL);
272         switch (ret) {
273         case -ENOBUFS:
274         case -ENODATA:
275                 p9_debug(P9_DEBUG_FSC, "page/inode not in cache %d\n", ret);
276                 return 1;
277         case 0:
278                 p9_debug(P9_DEBUG_FSC, "BIO submitted\n");
279                 return ret;
280         default:
281                 p9_debug(P9_DEBUG_FSC, "ret %d\n", ret);
282                 return ret;
283         }
284 }
285
286 /**
287  * __v9fs_readpages_from_fscache - read multiple pages from cache
288  *
289  * Returns 0 if the pages are in cache and a BIO is submitted,
290  * 1 if the pages are not in cache and -error otherwise.
291  */
292
293 int __v9fs_readpages_from_fscache(struct inode *inode,
294                                   struct address_space *mapping,
295                                   struct list_head *pages,
296                                   unsigned *nr_pages)
297 {
298         int ret;
299         const struct v9fs_inode *v9inode = V9FS_I(inode);
300
301         p9_debug(P9_DEBUG_FSC, "inode %p pages %u\n", inode, *nr_pages);
302         if (!v9inode->fscache)
303                 return -ENOBUFS;
304
305         ret = fscache_read_or_alloc_pages(v9inode->fscache,
306                                           mapping, pages, nr_pages,
307                                           v9fs_vfs_readpage_complete,
308                                           NULL,
309                                           mapping_gfp_mask(mapping));
310         switch (ret) {
311         case -ENOBUFS:
312         case -ENODATA:
313                 p9_debug(P9_DEBUG_FSC, "pages/inodes not in cache %d\n", ret);
314                 return 1;
315         case 0:
316                 BUG_ON(!list_empty(pages));
317                 BUG_ON(*nr_pages != 0);
318                 p9_debug(P9_DEBUG_FSC, "BIO submitted\n");
319                 return ret;
320         default:
321                 p9_debug(P9_DEBUG_FSC, "ret %d\n", ret);
322                 return ret;
323         }
324 }
325
326 /**
327  * __v9fs_readpage_to_fscache - write a page to the cache
328  *
329  */
330
331 void __v9fs_readpage_to_fscache(struct inode *inode, struct page *page)
332 {
333         int ret;
334         const struct v9fs_inode *v9inode = V9FS_I(inode);
335
336         p9_debug(P9_DEBUG_FSC, "inode %p page %p\n", inode, page);
337         ret = fscache_write_page(v9inode->fscache, page,
338                                  i_size_read(&v9inode->vfs_inode), GFP_KERNEL);
339         p9_debug(P9_DEBUG_FSC, "ret =  %d\n", ret);
340         if (ret != 0)
341                 v9fs_uncache_page(inode, page);
342 }
343
344 /*
345  * wait for a page to complete writing to the cache
346  */
347 void __v9fs_fscache_wait_on_page_write(struct inode *inode, struct page *page)
348 {
349         const struct v9fs_inode *v9inode = V9FS_I(inode);
350         p9_debug(P9_DEBUG_FSC, "inode %p page %p\n", inode, page);
351         if (PageFsCache(page))
352                 fscache_wait_on_page_write(v9inode->fscache, page);
353 }