xref: /openbmc/linux/fs/nfs/fscache.h (revision 1aaba11d)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /* NFS filesystem cache interface definitions
3  *
4  * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7 
8 #ifndef _NFS_FSCACHE_H
9 #define _NFS_FSCACHE_H
10 
11 #include <linux/swap.h>
12 #include <linux/nfs_fs.h>
13 #include <linux/nfs_mount.h>
14 #include <linux/nfs4_mount.h>
15 #include <linux/fscache.h>
16 #include <linux/iversion.h>
17 
18 #ifdef CONFIG_NFS_FSCACHE
19 
20 /*
21  * 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  */
29 struct nfs_fscache_inode_auxdata {
30 	s64	mtime_sec;
31 	s64	mtime_nsec;
32 	s64	ctime_sec;
33 	s64	ctime_nsec;
34 	u64	change_attr;
35 };
36 
37 /*
38  * fscache.c
39  */
40 extern int nfs_fscache_get_super_cookie(struct super_block *, const char *, int);
41 extern void nfs_fscache_release_super_cookie(struct super_block *);
42 
43 extern void nfs_fscache_init_inode(struct inode *);
44 extern void nfs_fscache_clear_inode(struct inode *);
45 extern void nfs_fscache_open_file(struct inode *, struct file *);
46 extern void nfs_fscache_release_file(struct inode *, struct file *);
47 
48 extern int __nfs_fscache_read_page(struct inode *, struct page *);
49 extern void __nfs_fscache_write_page(struct inode *, struct page *);
50 
51 static inline bool nfs_fscache_release_folio(struct folio *folio, gfp_t gfp)
52 {
53 	if (folio_test_fscache(folio)) {
54 		if (current_is_kswapd() || !(gfp & __GFP_FS))
55 			return false;
56 		folio_wait_fscache(folio);
57 		fscache_note_page_release(nfs_i_fscache(folio->mapping->host));
58 		nfs_inc_fscache_stats(folio->mapping->host,
59 				      NFSIOS_FSCACHE_PAGES_UNCACHED);
60 	}
61 	return true;
62 }
63 
64 /*
65  * Retrieve a page from an inode data storage object.
66  */
67 static inline int nfs_fscache_read_page(struct inode *inode, struct page *page)
68 {
69 	if (nfs_i_fscache(inode))
70 		return __nfs_fscache_read_page(inode, page);
71 	return -ENOBUFS;
72 }
73 
74 /*
75  * Store a page newly fetched from the server in an inode data storage object
76  * in the cache.
77  */
78 static inline void nfs_fscache_write_page(struct inode *inode,
79 					   struct page *page)
80 {
81 	if (nfs_i_fscache(inode))
82 		__nfs_fscache_write_page(inode, page);
83 }
84 
85 static inline void nfs_fscache_update_auxdata(struct nfs_fscache_inode_auxdata *auxdata,
86 					      struct inode *inode)
87 {
88 	memset(auxdata, 0, sizeof(*auxdata));
89 	auxdata->mtime_sec  = inode->i_mtime.tv_sec;
90 	auxdata->mtime_nsec = inode->i_mtime.tv_nsec;
91 	auxdata->ctime_sec  = inode->i_ctime.tv_sec;
92 	auxdata->ctime_nsec = inode->i_ctime.tv_nsec;
93 
94 	if (NFS_SERVER(inode)->nfs_client->rpc_ops->version == 4)
95 		auxdata->change_attr = inode_peek_iversion_raw(inode);
96 }
97 
98 /*
99  * Invalidate the contents of fscache for this inode.  This will not sleep.
100  */
101 static inline void nfs_fscache_invalidate(struct inode *inode, int flags)
102 {
103 	struct nfs_fscache_inode_auxdata auxdata;
104 	struct nfs_inode *nfsi = NFS_I(inode);
105 
106 	if (nfsi->fscache) {
107 		nfs_fscache_update_auxdata(&auxdata, inode);
108 		fscache_invalidate(nfsi->fscache, &auxdata,
109 				   i_size_read(inode), flags);
110 	}
111 }
112 
113 /*
114  * indicate the client caching state as readable text
115  */
116 static inline const char *nfs_server_fscache_state(struct nfs_server *server)
117 {
118 	if (server->fscache)
119 		return "yes";
120 	return "no ";
121 }
122 
123 #else /* CONFIG_NFS_FSCACHE */
124 static inline void nfs_fscache_release_super_cookie(struct super_block *sb) {}
125 
126 static inline void nfs_fscache_init_inode(struct inode *inode) {}
127 static inline void nfs_fscache_clear_inode(struct inode *inode) {}
128 static inline void nfs_fscache_open_file(struct inode *inode,
129 					 struct file *filp) {}
130 static inline void nfs_fscache_release_file(struct inode *inode, struct file *file) {}
131 
132 static inline bool nfs_fscache_release_folio(struct folio *folio, gfp_t gfp)
133 {
134 	return true; /* may release folio */
135 }
136 static inline int nfs_fscache_read_page(struct inode *inode, struct page *page)
137 {
138 	return -ENOBUFS;
139 }
140 static inline void nfs_fscache_write_page(struct inode *inode, struct page *page) {}
141 static inline void nfs_fscache_invalidate(struct inode *inode, int flags) {}
142 
143 static inline const char *nfs_server_fscache_state(struct nfs_server *server)
144 {
145 	return "no ";
146 }
147 
148 #endif /* CONFIG_NFS_FSCACHE */
149 #endif /* _NFS_FSCACHE_H */
150