This source file includes following definitions.
- nfs_fscache_get_client_cookie
- nfs_fscache_release_client_cookie
- nfs_fscache_get_super_cookie
- nfs_fscache_release_super_cookie
- nfs_fscache_update_auxdata
- nfs_fscache_init_inode
- nfs_fscache_clear_inode
- nfs_fscache_can_enable
- nfs_fscache_open_file
- nfs_fscache_release_page
- __nfs_fscache_invalidate_page
- nfs_readpage_from_fscache_complete
- __nfs_readpage_from_fscache
- __nfs_readpages_from_fscache
- __nfs_readpage_to_fscache
1
2
3
4
5
6
7
8 #include <linux/init.h>
9 #include <linux/kernel.h>
10 #include <linux/sched.h>
11 #include <linux/mm.h>
12 #include <linux/nfs_fs.h>
13 #include <linux/nfs_fs_sb.h>
14 #include <linux/in6.h>
15 #include <linux/seq_file.h>
16 #include <linux/slab.h>
17 #include <linux/iversion.h>
18
19 #include "internal.h"
20 #include "iostat.h"
21 #include "fscache.h"
22
23 #define NFSDBG_FACILITY NFSDBG_FSCACHE
24
25 static struct rb_root nfs_fscache_keys = RB_ROOT;
26 static DEFINE_SPINLOCK(nfs_fscache_keys_lock);
27
28
29
30
31 struct nfs_server_key {
32 struct {
33 uint16_t nfsversion;
34 uint32_t minorversion;
35 uint16_t family;
36 __be16 port;
37 } hdr;
38 union {
39 struct in_addr ipv4_addr;
40 struct in6_addr ipv6_addr;
41 };
42 } __packed;
43
44
45
46
47
48
49
50 void nfs_fscache_get_client_cookie(struct nfs_client *clp)
51 {
52 const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) &clp->cl_addr;
53 const struct sockaddr_in *sin = (struct sockaddr_in *) &clp->cl_addr;
54 struct nfs_server_key key;
55 uint16_t len = sizeof(key.hdr);
56
57 memset(&key, 0, sizeof(key));
58 key.hdr.nfsversion = clp->rpc_ops->version;
59 key.hdr.minorversion = clp->cl_minorversion;
60 key.hdr.family = clp->cl_addr.ss_family;
61
62 switch (clp->cl_addr.ss_family) {
63 case AF_INET:
64 key.hdr.port = sin->sin_port;
65 key.ipv4_addr = sin->sin_addr;
66 len += sizeof(key.ipv4_addr);
67 break;
68
69 case AF_INET6:
70 key.hdr.port = sin6->sin6_port;
71 key.ipv6_addr = sin6->sin6_addr;
72 len += sizeof(key.ipv6_addr);
73 break;
74
75 default:
76 printk(KERN_WARNING "NFS: Unknown network family '%d'\n",
77 clp->cl_addr.ss_family);
78 clp->fscache = NULL;
79 return;
80 }
81
82
83 clp->fscache = fscache_acquire_cookie(nfs_fscache_netfs.primary_index,
84 &nfs_fscache_server_index_def,
85 &key, len,
86 NULL, 0,
87 clp, 0, true);
88 dfprintk(FSCACHE, "NFS: get client cookie (0x%p/0x%p)\n",
89 clp, clp->fscache);
90 }
91
92
93
94
95 void nfs_fscache_release_client_cookie(struct nfs_client *clp)
96 {
97 dfprintk(FSCACHE, "NFS: releasing client cookie (0x%p/0x%p)\n",
98 clp, clp->fscache);
99
100 fscache_relinquish_cookie(clp->fscache, NULL, false);
101 clp->fscache = NULL;
102 }
103
104
105
106
107
108
109
110
111
112 void nfs_fscache_get_super_cookie(struct super_block *sb, const char *uniq, int ulen)
113 {
114 struct nfs_fscache_key *key, *xkey;
115 struct nfs_server *nfss = NFS_SB(sb);
116 struct rb_node **p, *parent;
117 int diff;
118
119 nfss->fscache_key = NULL;
120 nfss->fscache = NULL;
121 if (!(nfss->options & NFS_OPTION_FSCACHE))
122 return;
123 if (!uniq) {
124 uniq = "";
125 ulen = 1;
126 }
127
128 key = kzalloc(sizeof(*key) + ulen, GFP_KERNEL);
129 if (!key)
130 return;
131
132 key->nfs_client = nfss->nfs_client;
133 key->key.super.s_flags = sb->s_flags & NFS_MS_MASK;
134 key->key.nfs_server.flags = nfss->flags;
135 key->key.nfs_server.rsize = nfss->rsize;
136 key->key.nfs_server.wsize = nfss->wsize;
137 key->key.nfs_server.acregmin = nfss->acregmin;
138 key->key.nfs_server.acregmax = nfss->acregmax;
139 key->key.nfs_server.acdirmin = nfss->acdirmin;
140 key->key.nfs_server.acdirmax = nfss->acdirmax;
141 key->key.nfs_server.fsid = nfss->fsid;
142 key->key.rpc_auth.au_flavor = nfss->client->cl_auth->au_flavor;
143
144 key->key.uniq_len = ulen;
145 memcpy(key->key.uniquifier, uniq, ulen);
146
147 spin_lock(&nfs_fscache_keys_lock);
148 p = &nfs_fscache_keys.rb_node;
149 parent = NULL;
150 while (*p) {
151 parent = *p;
152 xkey = rb_entry(parent, struct nfs_fscache_key, node);
153
154 if (key->nfs_client < xkey->nfs_client)
155 goto go_left;
156 if (key->nfs_client > xkey->nfs_client)
157 goto go_right;
158
159 diff = memcmp(&key->key, &xkey->key, sizeof(key->key));
160 if (diff < 0)
161 goto go_left;
162 if (diff > 0)
163 goto go_right;
164
165 if (key->key.uniq_len == 0)
166 goto non_unique;
167 diff = memcmp(key->key.uniquifier,
168 xkey->key.uniquifier,
169 key->key.uniq_len);
170 if (diff < 0)
171 goto go_left;
172 if (diff > 0)
173 goto go_right;
174 goto non_unique;
175
176 go_left:
177 p = &(*p)->rb_left;
178 continue;
179 go_right:
180 p = &(*p)->rb_right;
181 }
182
183 rb_link_node(&key->node, parent, p);
184 rb_insert_color(&key->node, &nfs_fscache_keys);
185 spin_unlock(&nfs_fscache_keys_lock);
186 nfss->fscache_key = key;
187
188
189 nfss->fscache = fscache_acquire_cookie(nfss->nfs_client->fscache,
190 &nfs_fscache_super_index_def,
191 &key->key,
192 sizeof(key->key) + ulen,
193 NULL, 0,
194 nfss, 0, true);
195 dfprintk(FSCACHE, "NFS: get superblock cookie (0x%p/0x%p)\n",
196 nfss, nfss->fscache);
197 return;
198
199 non_unique:
200 spin_unlock(&nfs_fscache_keys_lock);
201 kfree(key);
202 nfss->fscache_key = NULL;
203 nfss->fscache = NULL;
204 printk(KERN_WARNING "NFS:"
205 " Cache request denied due to non-unique superblock keys\n");
206 }
207
208
209
210
211 void nfs_fscache_release_super_cookie(struct super_block *sb)
212 {
213 struct nfs_server *nfss = NFS_SB(sb);
214
215 dfprintk(FSCACHE, "NFS: releasing superblock cookie (0x%p/0x%p)\n",
216 nfss, nfss->fscache);
217
218 fscache_relinquish_cookie(nfss->fscache, NULL, false);
219 nfss->fscache = NULL;
220
221 if (nfss->fscache_key) {
222 spin_lock(&nfs_fscache_keys_lock);
223 rb_erase(&nfss->fscache_key->node, &nfs_fscache_keys);
224 spin_unlock(&nfs_fscache_keys_lock);
225 kfree(nfss->fscache_key);
226 nfss->fscache_key = NULL;
227 }
228 }
229
230 static void nfs_fscache_update_auxdata(struct nfs_fscache_inode_auxdata *auxdata,
231 struct nfs_inode *nfsi)
232 {
233 memset(auxdata, 0, sizeof(*auxdata));
234 auxdata->mtime_sec = nfsi->vfs_inode.i_mtime.tv_sec;
235 auxdata->mtime_nsec = nfsi->vfs_inode.i_mtime.tv_nsec;
236 auxdata->ctime_sec = nfsi->vfs_inode.i_ctime.tv_sec;
237 auxdata->ctime_nsec = nfsi->vfs_inode.i_ctime.tv_nsec;
238
239 if (NFS_SERVER(&nfsi->vfs_inode)->nfs_client->rpc_ops->version == 4)
240 auxdata->change_attr = inode_peek_iversion_raw(&nfsi->vfs_inode);
241 }
242
243
244
245
246 void nfs_fscache_init_inode(struct inode *inode)
247 {
248 struct nfs_fscache_inode_auxdata auxdata;
249 struct nfs_server *nfss = NFS_SERVER(inode);
250 struct nfs_inode *nfsi = NFS_I(inode);
251
252 nfsi->fscache = NULL;
253 if (!(nfss->fscache && S_ISREG(inode->i_mode)))
254 return;
255
256 nfs_fscache_update_auxdata(&auxdata, nfsi);
257
258 nfsi->fscache = fscache_acquire_cookie(NFS_SB(inode->i_sb)->fscache,
259 &nfs_fscache_inode_object_def,
260 nfsi->fh.data, nfsi->fh.size,
261 &auxdata, sizeof(auxdata),
262 nfsi, nfsi->vfs_inode.i_size, false);
263 }
264
265
266
267
268 void nfs_fscache_clear_inode(struct inode *inode)
269 {
270 struct nfs_fscache_inode_auxdata auxdata;
271 struct nfs_inode *nfsi = NFS_I(inode);
272 struct fscache_cookie *cookie = nfs_i_fscache(inode);
273
274 dfprintk(FSCACHE, "NFS: clear cookie (0x%p/0x%p)\n", nfsi, cookie);
275
276 nfs_fscache_update_auxdata(&auxdata, nfsi);
277 fscache_relinquish_cookie(cookie, &auxdata, false);
278 nfsi->fscache = NULL;
279 }
280
281 static bool nfs_fscache_can_enable(void *data)
282 {
283 struct inode *inode = data;
284
285 return !inode_is_open_for_write(inode);
286 }
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307 void nfs_fscache_open_file(struct inode *inode, struct file *filp)
308 {
309 struct nfs_fscache_inode_auxdata auxdata;
310 struct nfs_inode *nfsi = NFS_I(inode);
311 struct fscache_cookie *cookie = nfs_i_fscache(inode);
312
313 if (!fscache_cookie_valid(cookie))
314 return;
315
316 nfs_fscache_update_auxdata(&auxdata, nfsi);
317
318 if (inode_is_open_for_write(inode)) {
319 dfprintk(FSCACHE, "NFS: nfsi 0x%p disabling cache\n", nfsi);
320 clear_bit(NFS_INO_FSCACHE, &nfsi->flags);
321 fscache_disable_cookie(cookie, &auxdata, true);
322 fscache_uncache_all_inode_pages(cookie, inode);
323 } else {
324 dfprintk(FSCACHE, "NFS: nfsi 0x%p enabling cache\n", nfsi);
325 fscache_enable_cookie(cookie, &auxdata, nfsi->vfs_inode.i_size,
326 nfs_fscache_can_enable, inode);
327 if (fscache_cookie_enabled(cookie))
328 set_bit(NFS_INO_FSCACHE, &NFS_I(inode)->flags);
329 }
330 }
331 EXPORT_SYMBOL_GPL(nfs_fscache_open_file);
332
333
334
335
336
337
338 int nfs_fscache_release_page(struct page *page, gfp_t gfp)
339 {
340 if (PageFsCache(page)) {
341 struct fscache_cookie *cookie = nfs_i_fscache(page->mapping->host);
342
343 BUG_ON(!cookie);
344 dfprintk(FSCACHE, "NFS: fscache releasepage (0x%p/0x%p/0x%p)\n",
345 cookie, page, NFS_I(page->mapping->host));
346
347 if (!fscache_maybe_release_page(cookie, page, gfp))
348 return 0;
349
350 nfs_inc_fscache_stats(page->mapping->host,
351 NFSIOS_FSCACHE_PAGES_UNCACHED);
352 }
353
354 return 1;
355 }
356
357
358
359
360
361 void __nfs_fscache_invalidate_page(struct page *page, struct inode *inode)
362 {
363 struct fscache_cookie *cookie = nfs_i_fscache(inode);
364
365 BUG_ON(!cookie);
366
367 dfprintk(FSCACHE, "NFS: fscache invalidatepage (0x%p/0x%p/0x%p)\n",
368 cookie, page, NFS_I(inode));
369
370 fscache_wait_on_page_write(cookie, page);
371
372 BUG_ON(!PageLocked(page));
373 fscache_uncache_page(cookie, page);
374 nfs_inc_fscache_stats(page->mapping->host,
375 NFSIOS_FSCACHE_PAGES_UNCACHED);
376 }
377
378
379
380
381
382 static void nfs_readpage_from_fscache_complete(struct page *page,
383 void *context,
384 int error)
385 {
386 dfprintk(FSCACHE,
387 "NFS: readpage_from_fscache_complete (0x%p/0x%p/%d)\n",
388 page, context, error);
389
390
391
392 if (!error) {
393 SetPageUptodate(page);
394 unlock_page(page);
395 } else {
396 error = nfs_readpage_async(context, page->mapping->host, page);
397 if (error)
398 unlock_page(page);
399 }
400 }
401
402
403
404
405 int __nfs_readpage_from_fscache(struct nfs_open_context *ctx,
406 struct inode *inode, struct page *page)
407 {
408 int ret;
409
410 dfprintk(FSCACHE,
411 "NFS: readpage_from_fscache(fsc:%p/p:%p(i:%lx f:%lx)/0x%p)\n",
412 nfs_i_fscache(inode), page, page->index, page->flags, inode);
413
414 ret = fscache_read_or_alloc_page(nfs_i_fscache(inode),
415 page,
416 nfs_readpage_from_fscache_complete,
417 ctx,
418 GFP_KERNEL);
419
420 switch (ret) {
421 case 0:
422 dfprintk(FSCACHE,
423 "NFS: readpage_from_fscache: BIO submitted\n");
424 nfs_inc_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_READ_OK);
425 return ret;
426
427 case -ENOBUFS:
428 case -ENODATA:
429 nfs_inc_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_READ_FAIL);
430 dfprintk(FSCACHE,
431 "NFS: readpage_from_fscache %d\n", ret);
432 return 1;
433
434 default:
435 dfprintk(FSCACHE, "NFS: readpage_from_fscache %d\n", ret);
436 nfs_inc_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_READ_FAIL);
437 }
438 return ret;
439 }
440
441
442
443
444 int __nfs_readpages_from_fscache(struct nfs_open_context *ctx,
445 struct inode *inode,
446 struct address_space *mapping,
447 struct list_head *pages,
448 unsigned *nr_pages)
449 {
450 unsigned npages = *nr_pages;
451 int ret;
452
453 dfprintk(FSCACHE, "NFS: nfs_getpages_from_fscache (0x%p/%u/0x%p)\n",
454 nfs_i_fscache(inode), npages, inode);
455
456 ret = fscache_read_or_alloc_pages(nfs_i_fscache(inode),
457 mapping, pages, nr_pages,
458 nfs_readpage_from_fscache_complete,
459 ctx,
460 mapping_gfp_mask(mapping));
461 if (*nr_pages < npages)
462 nfs_add_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_READ_OK,
463 npages);
464 if (*nr_pages > 0)
465 nfs_add_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_READ_FAIL,
466 *nr_pages);
467
468 switch (ret) {
469 case 0:
470 BUG_ON(!list_empty(pages));
471 BUG_ON(*nr_pages != 0);
472 dfprintk(FSCACHE,
473 "NFS: nfs_getpages_from_fscache: submitted\n");
474
475 return ret;
476
477 case -ENOBUFS:
478 case -ENODATA:
479 dfprintk(FSCACHE,
480 "NFS: nfs_getpages_from_fscache: no page: %d\n", ret);
481 return 1;
482
483 default:
484 dfprintk(FSCACHE,
485 "NFS: nfs_getpages_from_fscache: ret %d\n", ret);
486 }
487
488 return ret;
489 }
490
491
492
493
494
495 void __nfs_readpage_to_fscache(struct inode *inode, struct page *page, int sync)
496 {
497 int ret;
498
499 dfprintk(FSCACHE,
500 "NFS: readpage_to_fscache(fsc:%p/p:%p(i:%lx f:%lx)/%d)\n",
501 nfs_i_fscache(inode), page, page->index, page->flags, sync);
502
503 ret = fscache_write_page(nfs_i_fscache(inode), page,
504 inode->i_size, GFP_KERNEL);
505 dfprintk(FSCACHE,
506 "NFS: readpage_to_fscache: p:%p(i:%lu f:%lx) ret %d\n",
507 page, page->index, page->flags, ret);
508
509 if (ret != 0) {
510 fscache_uncache_page(nfs_i_fscache(inode), page);
511 nfs_inc_fscache_stats(inode,
512 NFSIOS_FSCACHE_PAGES_WRITTEN_FAIL);
513 nfs_inc_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_UNCACHED);
514 } else {
515 nfs_inc_fscache_stats(inode,
516 NFSIOS_FSCACHE_PAGES_WRITTEN_OK);
517 }
518 }