xref: /linux/fs/nfs/fscache.h (revision 64b14a184e83eb62ea0615e31a409956049d40e7)
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_readpage_from_fscache(struct inode *, struct page *);
49 extern void __nfs_read_completion_to_fscache(struct nfs_pgio_header *hdr,
50 					     unsigned long bytes);
51 extern void __nfs_readpage_to_fscache(struct inode *, struct page *);
52 
53 static inline int nfs_fscache_release_page(struct page *page, gfp_t gfp)
54 {
55 	if (PageFsCache(page)) {
56 		if (current_is_kswapd() || !(gfp & __GFP_FS))
57 			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;
64 }
65 
66 /*
67  * Retrieve a page from an inode data storage object.
68  */
69 static inline int nfs_readpage_from_fscache(struct inode *inode,
70 					    struct page *page)
71 {
72 	if (NFS_I(inode)->fscache)
73 		return __nfs_readpage_from_fscache(inode, page);
74 	return -ENOBUFS;
75 }
76 
77 /*
78  * Store a page newly fetched from the server in an inode data storage object
79  * in the cache.
80  */
81 static inline void nfs_readpage_to_fscache(struct inode *inode,
82 					   struct page *page)
83 {
84 	if (NFS_I(inode)->fscache)
85 		__nfs_readpage_to_fscache(inode, page);
86 }
87 
88 static inline void nfs_fscache_update_auxdata(struct nfs_fscache_inode_auxdata *auxdata,
89 					      struct nfs_inode *nfsi)
90 {
91 	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);
99 }
100 
101 /*
102  * Invalidate the contents of fscache for this inode.  This will not sleep.
103  */
104 static inline void nfs_fscache_invalidate(struct inode *inode, int flags)
105 {
106 	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 	}
114 }
115 
116 /*
117  * indicate the client caching state as readable text
118  */
119 static inline const char *nfs_server_fscache_state(struct nfs_server *server)
120 {
121 	if (server->fscache)
122 		return "yes";
123 	return "no ";
124 }
125 
126 #else /* CONFIG_NFS_FSCACHE */
127 static inline void nfs_fscache_release_super_cookie(struct super_block *sb) {}
128 
129 static inline void nfs_fscache_init_inode(struct inode *inode) {}
130 static inline void nfs_fscache_clear_inode(struct inode *inode) {}
131 static inline void nfs_fscache_open_file(struct inode *inode,
132 					 struct file *filp) {}
133 static inline void nfs_fscache_release_file(struct inode *inode, struct file *file) {}
134 
135 static inline int nfs_fscache_release_page(struct page *page, gfp_t gfp)
136 {
137 	return 1; /* True: may release page */
138 }
139 static inline int nfs_readpage_from_fscache(struct inode *inode,
140 					    struct page *page)
141 {
142 	return -ENOBUFS;
143 }
144 static inline void nfs_readpage_to_fscache(struct inode *inode,
145 					   struct page *page) {}
146 
147 
148 static inline void nfs_fscache_invalidate(struct inode *inode, int flags) {}
149 
150 static inline const char *nfs_server_fscache_state(struct nfs_server *server)
151 {
152 	return "no ";
153 }
154 
155 #endif /* CONFIG_NFS_FSCACHE */
156 #endif /* _NFS_FSCACHE_H */
157