blob: 25a5c0f82392108a3dc82d41cd3c7490db682437 [file] [log] [blame]
Thomas Gleixnerb4d0d232019-05-20 19:08:01 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
David Howells8ec442a2009-04-03 16:42:42 +01002/* NFS filesystem cache interface definitions
3 *
4 * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
David Howells8ec442a2009-04-03 16:42:42 +01006 */
7
8#ifndef _NFS_FSCACHE_H
9#define _NFS_FSCACHE_H
10
David Howellsd7bdba12021-12-22 17:21:04 +000011#include <linux/swap.h>
David Howells8ec442a2009-04-03 16:42:42 +010012#include <linux/nfs_fs.h>
13#include <linux/nfs_mount.h>
14#include <linux/nfs4_mount.h>
15#include <linux/fscache.h>
Dave Wysochanskia6b5a282020-11-14 13:43:54 -050016#include <linux/iversion.h>
David Howells8ec442a2009-04-03 16:42:42 +010017
18#ifdef CONFIG_NFS_FSCACHE
19
20/*
David Howells402cb8d2018-04-04 13:41:28 +010021 * Definition of the auxiliary data attached to NFS inode storage objects
22 * within the cache.
23 *
24 * The contents of this struct are recorded in the on-disk local cache in the
25 * auxiliary data attached to the data storage object backing an inode. This
26 * permits coherency to be managed when a new inode binds to an already extant
27 * cache object.
28 */
29struct nfs_fscache_inode_auxdata {
Arnd Bergmann6e31ded2019-10-28 11:41:42 +010030 s64 mtime_sec;
31 s64 mtime_nsec;
32 s64 ctime_sec;
33 s64 ctime_nsec;
34 u64 change_attr;
David Howells402cb8d2018-04-04 13:41:28 +010035};
36
37/*
David Howells14727282009-04-03 16:42:42 +010038 * fscache.c
39 */
Dave Wysochanskia6b5a282020-11-14 13:43:54 -050040extern int nfs_fscache_get_super_cookie(struct super_block *, const char *, int);
David Howells08734042009-04-03 16:42:42 +010041extern void nfs_fscache_release_super_cookie(struct super_block *);
42
David Howellsf1fe29b2013-09-27 11:20:03 +010043extern void nfs_fscache_init_inode(struct inode *);
44extern void nfs_fscache_clear_inode(struct inode *);
45extern void nfs_fscache_open_file(struct inode *, struct file *);
Dave Wysochanskia6b5a282020-11-14 13:43:54 -050046extern void nfs_fscache_release_file(struct inode *, struct file *);
David Howellsef79c092009-04-03 16:42:43 +010047
David Howells16f2f4e2021-08-27 15:19:34 +010048extern int __nfs_readpage_from_fscache(struct inode *, struct page *);
49extern void __nfs_read_completion_to_fscache(struct nfs_pgio_header *hdr,
50 unsigned long bytes);
51extern void __nfs_readpage_to_fscache(struct inode *, struct page *);
David Howells9a9fc1c2009-04-03 16:42:44 +010052
Dave Wysochanskia6b5a282020-11-14 13:43:54 -050053static inline int nfs_fscache_release_page(struct page *page, gfp_t gfp)
David Howells545db452009-04-03 16:42:44 +010054{
Dave Wysochanskia6b5a282020-11-14 13:43:54 -050055 if (PageFsCache(page)) {
David Howellsd7bdba12021-12-22 17:21:04 +000056 if (current_is_kswapd() || !(gfp & __GFP_FS))
Dave Wysochanskia6b5a282020-11-14 13:43:54 -050057 return false;
58 wait_on_page_fscache(page);
59 fscache_note_page_release(nfs_i_fscache(page->mapping->host));
60 nfs_inc_fscache_stats(page->mapping->host,
61 NFSIOS_FSCACHE_PAGES_UNCACHED);
62 }
63 return true;
David Howells545db452009-04-03 16:42:44 +010064}
65
David Howells9a9fc1c2009-04-03 16:42:44 +010066/*
67 * Retrieve a page from an inode data storage object.
68 */
David Howells16f2f4e2021-08-27 15:19:34 +010069static inline int nfs_readpage_from_fscache(struct inode *inode,
David Howells9a9fc1c2009-04-03 16:42:44 +010070 struct page *page)
71{
72 if (NFS_I(inode)->fscache)
David Howells16f2f4e2021-08-27 15:19:34 +010073 return __nfs_readpage_from_fscache(inode, page);
David Howells9a9fc1c2009-04-03 16:42:44 +010074 return -ENOBUFS;
75}
David Howells545db452009-04-03 16:42:44 +010076
David Howells7f8e05f2009-04-03 16:42:45 +010077/*
78 * Store a page newly fetched from the server in an inode data storage object
79 * in the cache.
80 */
81static inline void nfs_readpage_to_fscache(struct inode *inode,
David Howells16f2f4e2021-08-27 15:19:34 +010082 struct page *page)
David Howells7f8e05f2009-04-03 16:42:45 +010083{
David Howells16f2f4e2021-08-27 15:19:34 +010084 if (NFS_I(inode)->fscache)
85 __nfs_readpage_to_fscache(inode, page);
David Howells7f8e05f2009-04-03 16:42:45 +010086}
87
Dave Wysochanskia6b5a282020-11-14 13:43:54 -050088static inline void nfs_fscache_update_auxdata(struct nfs_fscache_inode_auxdata *auxdata,
89 struct nfs_inode *nfsi)
David Howellsde242c02012-12-20 21:52:38 +000090{
Dave Wysochanskia6b5a282020-11-14 13:43:54 -050091 memset(auxdata, 0, sizeof(*auxdata));
92 auxdata->mtime_sec = nfsi->vfs_inode.i_mtime.tv_sec;
93 auxdata->mtime_nsec = nfsi->vfs_inode.i_mtime.tv_nsec;
94 auxdata->ctime_sec = nfsi->vfs_inode.i_ctime.tv_sec;
95 auxdata->ctime_nsec = nfsi->vfs_inode.i_ctime.tv_nsec;
96
97 if (NFS_SERVER(&nfsi->vfs_inode)->nfs_client->rpc_ops->version == 4)
98 auxdata->change_attr = inode_peek_iversion_raw(&nfsi->vfs_inode);
David Howellsde242c02012-12-20 21:52:38 +000099}
100
101/*
Dave Wysochanskia6b5a282020-11-14 13:43:54 -0500102 * Invalidate the contents of fscache for this inode. This will not sleep.
David Howellsde242c02012-12-20 21:52:38 +0000103 */
Dave Wysochanskia6b5a282020-11-14 13:43:54 -0500104static inline void nfs_fscache_invalidate(struct inode *inode, int flags)
David Howellsde242c02012-12-20 21:52:38 +0000105{
Dave Wysochanskia6b5a282020-11-14 13:43:54 -0500106 struct nfs_fscache_inode_auxdata auxdata;
107 struct nfs_inode *nfsi = NFS_I(inode);
108
109 if (nfsi->fscache) {
110 nfs_fscache_update_auxdata(&auxdata, nfsi);
111 fscache_invalidate(nfsi->fscache, &auxdata,
112 i_size_read(&nfsi->vfs_inode), flags);
113 }
David Howellsde242c02012-12-20 21:52:38 +0000114}
115
116/*
David Howells5d1acff2009-04-03 16:42:47 +0100117 * indicate the client caching state as readable text
118 */
119static inline const char *nfs_server_fscache_state(struct nfs_server *server)
120{
Trond Myklebustdea1bb32019-08-03 13:39:24 -0400121 if (server->fscache)
David Howells5d1acff2009-04-03 16:42:47 +0100122 return "yes";
123 return "no ";
124}
125
David Howells8ec442a2009-04-03 16:42:42 +0100126#else /* CONFIG_NFS_FSCACHE */
David Howells08734042009-04-03 16:42:42 +0100127static inline void nfs_fscache_release_super_cookie(struct super_block *sb) {}
128
David Howellsf1fe29b2013-09-27 11:20:03 +0100129static inline void nfs_fscache_init_inode(struct inode *inode) {}
130static inline void nfs_fscache_clear_inode(struct inode *inode) {}
131static inline void nfs_fscache_open_file(struct inode *inode,
132 struct file *filp) {}
Dave Wysochanskia6b5a282020-11-14 13:43:54 -0500133static inline void nfs_fscache_release_file(struct inode *inode, struct file *file) {}
David Howellsef79c092009-04-03 16:42:43 +0100134
David Howells545db452009-04-03 16:42:44 +0100135static inline int nfs_fscache_release_page(struct page *page, gfp_t gfp)
136{
137 return 1; /* True: may release page */
138}
David Howells16f2f4e2021-08-27 15:19:34 +0100139static inline int nfs_readpage_from_fscache(struct inode *inode,
David Howells9a9fc1c2009-04-03 16:42:44 +0100140 struct page *page)
141{
142 return -ENOBUFS;
143}
David Howells7f8e05f2009-04-03 16:42:45 +0100144static inline void nfs_readpage_to_fscache(struct inode *inode,
David Howells16f2f4e2021-08-27 15:19:34 +0100145 struct page *page) {}
David Howells9a9fc1c2009-04-03 16:42:44 +0100146
David Howellsde242c02012-12-20 21:52:38 +0000147
Dave Wysochanskia6b5a282020-11-14 13:43:54 -0500148static inline void nfs_fscache_invalidate(struct inode *inode, int flags) {}
David Howellsde242c02012-12-20 21:52:38 +0000149
David Howells5d1acff2009-04-03 16:42:47 +0100150static inline const char *nfs_server_fscache_state(struct nfs_server *server)
151{
152 return "no ";
153}
154
David Howells8ec442a2009-04-03 16:42:42 +0100155#endif /* CONFIG_NFS_FSCACHE */
156#endif /* _NFS_FSCACHE_H */