xref: /openbmc/linux/fs/nfs/fscache.h (revision 000dbe0b)
1b4d0d230SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
28ec442aeSDavid Howells /* NFS filesystem cache interface definitions
38ec442aeSDavid Howells  *
48ec442aeSDavid Howells  * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved.
58ec442aeSDavid Howells  * Written by David Howells (dhowells@redhat.com)
68ec442aeSDavid Howells  */
78ec442aeSDavid Howells 
88ec442aeSDavid Howells #ifndef _NFS_FSCACHE_H
98ec442aeSDavid Howells #define _NFS_FSCACHE_H
108ec442aeSDavid Howells 
11d7bdba1cSDavid Howells #include <linux/swap.h>
128ec442aeSDavid Howells #include <linux/nfs_fs.h>
138ec442aeSDavid Howells #include <linux/nfs_mount.h>
148ec442aeSDavid Howells #include <linux/nfs4_mount.h>
158ec442aeSDavid Howells #include <linux/fscache.h>
16a6b5a28eSDave Wysochanski #include <linux/iversion.h>
178ec442aeSDavid Howells 
188ec442aeSDavid Howells #ifdef CONFIG_NFS_FSCACHE
198ec442aeSDavid Howells 
208ec442aeSDavid Howells /*
21402cb8ddSDavid Howells  * Definition of the auxiliary data attached to NFS inode storage objects
22402cb8ddSDavid Howells  * within the cache.
23402cb8ddSDavid Howells  *
24402cb8ddSDavid Howells  * The contents of this struct are recorded in the on-disk local cache in the
25402cb8ddSDavid Howells  * auxiliary data attached to the data storage object backing an inode.  This
26402cb8ddSDavid Howells  * permits coherency to be managed when a new inode binds to an already extant
27402cb8ddSDavid Howells  * cache object.
28402cb8ddSDavid Howells  */
29402cb8ddSDavid Howells struct nfs_fscache_inode_auxdata {
306e31ded6SArnd Bergmann 	s64	mtime_sec;
316e31ded6SArnd Bergmann 	s64	mtime_nsec;
326e31ded6SArnd Bergmann 	s64	ctime_sec;
336e31ded6SArnd Bergmann 	s64	ctime_nsec;
34402cb8ddSDavid Howells 	u64	change_attr;
35402cb8ddSDavid Howells };
36402cb8ddSDavid Howells 
37*000dbe0bSDave Wysochanski struct nfs_netfs_io_data {
38*000dbe0bSDave Wysochanski 	/*
39*000dbe0bSDave Wysochanski 	 * NFS may split a netfs_io_subrequest into multiple RPCs, each
40*000dbe0bSDave Wysochanski 	 * with their own read completion.  In netfs, we can only call
41*000dbe0bSDave Wysochanski 	 * netfs_subreq_terminated() once for each subrequest.  Use the
42*000dbe0bSDave Wysochanski 	 * refcount here to double as a marker of the last RPC completion,
43*000dbe0bSDave Wysochanski 	 * and only call netfs via netfs_subreq_terminated() once.
44*000dbe0bSDave Wysochanski 	 */
45*000dbe0bSDave Wysochanski 	refcount_t			refcount;
46*000dbe0bSDave Wysochanski 	struct netfs_io_subrequest	*sreq;
47*000dbe0bSDave Wysochanski 
48*000dbe0bSDave Wysochanski 	/*
49*000dbe0bSDave Wysochanski 	 * Final disposition of the netfs_io_subrequest, sent in
50*000dbe0bSDave Wysochanski 	 * netfs_subreq_terminated()
51*000dbe0bSDave Wysochanski 	 */
52*000dbe0bSDave Wysochanski 	atomic64_t	transferred;
53*000dbe0bSDave Wysochanski 	int		error;
54*000dbe0bSDave Wysochanski };
55*000dbe0bSDave Wysochanski 
56*000dbe0bSDave Wysochanski static inline void nfs_netfs_get(struct nfs_netfs_io_data *netfs)
57*000dbe0bSDave Wysochanski {
58*000dbe0bSDave Wysochanski 	refcount_inc(&netfs->refcount);
59*000dbe0bSDave Wysochanski }
60*000dbe0bSDave Wysochanski 
61*000dbe0bSDave Wysochanski static inline void nfs_netfs_put(struct nfs_netfs_io_data *netfs)
62*000dbe0bSDave Wysochanski {
63*000dbe0bSDave Wysochanski 	ssize_t final_len;
64*000dbe0bSDave Wysochanski 
65*000dbe0bSDave Wysochanski 	/* Only the last RPC completion should call netfs_subreq_terminated() */
66*000dbe0bSDave Wysochanski 	if (!refcount_dec_and_test(&netfs->refcount))
67*000dbe0bSDave Wysochanski 		return;
68*000dbe0bSDave Wysochanski 
69*000dbe0bSDave Wysochanski 	/*
70*000dbe0bSDave Wysochanski 	 * The NFS pageio interface may read a complete page, even when netfs
71*000dbe0bSDave Wysochanski 	 * only asked for a partial page.  Specifically, this may be seen when
72*000dbe0bSDave Wysochanski 	 * one thread is truncating a file while another one is reading the last
73*000dbe0bSDave Wysochanski 	 * page of the file.
74*000dbe0bSDave Wysochanski 	 * Correct the final length here to be no larger than the netfs subrequest
75*000dbe0bSDave Wysochanski 	 * length, and thus avoid netfs's "Subreq overread" warning message.
76*000dbe0bSDave Wysochanski 	 */
77*000dbe0bSDave Wysochanski 	final_len = min_t(s64, netfs->sreq->len, atomic64_read(&netfs->transferred));
78*000dbe0bSDave Wysochanski 	netfs_subreq_terminated(netfs->sreq, netfs->error ?: final_len, false);
79*000dbe0bSDave Wysochanski 	kfree(netfs);
80*000dbe0bSDave Wysochanski }
81*000dbe0bSDave Wysochanski static inline void nfs_netfs_inode_init(struct nfs_inode *nfsi)
82*000dbe0bSDave Wysochanski {
83*000dbe0bSDave Wysochanski 	netfs_inode_init(&nfsi->netfs, &nfs_netfs_ops);
84*000dbe0bSDave Wysochanski }
85*000dbe0bSDave Wysochanski extern void nfs_netfs_initiate_read(struct nfs_pgio_header *hdr);
86*000dbe0bSDave Wysochanski extern void nfs_netfs_read_completion(struct nfs_pgio_header *hdr);
87*000dbe0bSDave Wysochanski extern int nfs_netfs_folio_unlock(struct folio *folio);
88*000dbe0bSDave Wysochanski 
89402cb8ddSDavid Howells /*
9014727281SDavid Howells  * fscache.c
9114727281SDavid Howells  */
92a6b5a28eSDave Wysochanski extern int nfs_fscache_get_super_cookie(struct super_block *, const char *, int);
9308734048SDavid Howells extern void nfs_fscache_release_super_cookie(struct super_block *);
9408734048SDavid Howells 
95f1fe29b4SDavid Howells extern void nfs_fscache_init_inode(struct inode *);
96f1fe29b4SDavid Howells extern void nfs_fscache_clear_inode(struct inode *);
97f1fe29b4SDavid Howells extern void nfs_fscache_open_file(struct inode *, struct file *);
98a6b5a28eSDave Wysochanski extern void nfs_fscache_release_file(struct inode *, struct file *);
99*000dbe0bSDave Wysochanski extern int nfs_netfs_readahead(struct readahead_control *ractl);
100*000dbe0bSDave Wysochanski extern int nfs_netfs_read_folio(struct file *file, struct folio *folio);
1019a9fc1c0SDavid Howells 
1023577da4aSMatthew Wilcox (Oracle) static inline bool nfs_fscache_release_folio(struct folio *folio, gfp_t gfp)
103545db45fSDavid Howells {
1043577da4aSMatthew Wilcox (Oracle) 	if (folio_test_fscache(folio)) {
105d7bdba1cSDavid Howells 		if (current_is_kswapd() || !(gfp & __GFP_FS))
106a6b5a28eSDave Wysochanski 			return false;
1073577da4aSMatthew Wilcox (Oracle) 		folio_wait_fscache(folio);
108545db45fSDavid Howells 	}
109*000dbe0bSDave Wysochanski 	fscache_note_page_release(netfs_i_cookie(netfs_inode(folio->mapping->host)));
110a6b5a28eSDave Wysochanski 	return true;
111545db45fSDavid Howells }
112545db45fSDavid Howells 
113a6b5a28eSDave Wysochanski static inline void nfs_fscache_update_auxdata(struct nfs_fscache_inode_auxdata *auxdata,
11445f3a70bSDave Wysochanski 					      struct inode *inode)
115de242c0bSDavid Howells {
116a6b5a28eSDave Wysochanski 	memset(auxdata, 0, sizeof(*auxdata));
11745f3a70bSDave Wysochanski 	auxdata->mtime_sec  = inode->i_mtime.tv_sec;
11845f3a70bSDave Wysochanski 	auxdata->mtime_nsec = inode->i_mtime.tv_nsec;
11945f3a70bSDave Wysochanski 	auxdata->ctime_sec  = inode->i_ctime.tv_sec;
12045f3a70bSDave Wysochanski 	auxdata->ctime_nsec = inode->i_ctime.tv_nsec;
121a6b5a28eSDave Wysochanski 
12245f3a70bSDave Wysochanski 	if (NFS_SERVER(inode)->nfs_client->rpc_ops->version == 4)
12345f3a70bSDave Wysochanski 		auxdata->change_attr = inode_peek_iversion_raw(inode);
124de242c0bSDavid Howells }
125de242c0bSDavid Howells 
126de242c0bSDavid Howells /*
127a6b5a28eSDave Wysochanski  * Invalidate the contents of fscache for this inode.  This will not sleep.
128de242c0bSDavid Howells  */
129a6b5a28eSDave Wysochanski static inline void nfs_fscache_invalidate(struct inode *inode, int flags)
130de242c0bSDavid Howells {
131a6b5a28eSDave Wysochanski 	struct nfs_fscache_inode_auxdata auxdata;
13288a4d7bdSDave Wysochanski 	struct fscache_cookie *cookie =  netfs_i_cookie(&NFS_I(inode)->netfs);
133a6b5a28eSDave Wysochanski 
13445f3a70bSDave Wysochanski 	nfs_fscache_update_auxdata(&auxdata, inode);
13588a4d7bdSDave Wysochanski 	fscache_invalidate(cookie, &auxdata, i_size_read(inode), flags);
136de242c0bSDavid Howells }
137de242c0bSDavid Howells 
138de242c0bSDavid Howells /*
1395d1acff1SDavid Howells  * indicate the client caching state as readable text
1405d1acff1SDavid Howells  */
1415d1acff1SDavid Howells static inline const char *nfs_server_fscache_state(struct nfs_server *server)
1425d1acff1SDavid Howells {
143dea1bb35STrond Myklebust 	if (server->fscache)
1445d1acff1SDavid Howells 		return "yes";
1455d1acff1SDavid Howells 	return "no ";
1465d1acff1SDavid Howells }
1475d1acff1SDavid Howells 
148*000dbe0bSDave Wysochanski static inline void nfs_netfs_set_pgio_header(struct nfs_pgio_header *hdr,
149*000dbe0bSDave Wysochanski 					     struct nfs_pageio_descriptor *desc)
150*000dbe0bSDave Wysochanski {
151*000dbe0bSDave Wysochanski 	hdr->netfs = desc->pg_netfs;
152*000dbe0bSDave Wysochanski }
153*000dbe0bSDave Wysochanski static inline void nfs_netfs_set_pageio_descriptor(struct nfs_pageio_descriptor *desc,
154*000dbe0bSDave Wysochanski 						   struct nfs_pgio_header *hdr)
155*000dbe0bSDave Wysochanski {
156*000dbe0bSDave Wysochanski 	desc->pg_netfs = hdr->netfs;
157*000dbe0bSDave Wysochanski }
158*000dbe0bSDave Wysochanski static inline void nfs_netfs_reset_pageio_descriptor(struct nfs_pageio_descriptor *desc)
159*000dbe0bSDave Wysochanski {
160*000dbe0bSDave Wysochanski 	desc->pg_netfs = NULL;
161*000dbe0bSDave Wysochanski }
1628ec442aeSDavid Howells #else /* CONFIG_NFS_FSCACHE */
163*000dbe0bSDave Wysochanski static inline void nfs_netfs_inode_init(struct nfs_inode *nfsi) {}
164*000dbe0bSDave Wysochanski static inline void nfs_netfs_initiate_read(struct nfs_pgio_header *hdr) {}
165*000dbe0bSDave Wysochanski static inline void nfs_netfs_read_completion(struct nfs_pgio_header *hdr) {}
166*000dbe0bSDave Wysochanski static inline int nfs_netfs_folio_unlock(struct folio *folio)
167*000dbe0bSDave Wysochanski {
168*000dbe0bSDave Wysochanski 	return 1;
169*000dbe0bSDave Wysochanski }
17008734048SDavid Howells static inline void nfs_fscache_release_super_cookie(struct super_block *sb) {}
17108734048SDavid Howells 
172f1fe29b4SDavid Howells static inline void nfs_fscache_init_inode(struct inode *inode) {}
173f1fe29b4SDavid Howells static inline void nfs_fscache_clear_inode(struct inode *inode) {}
174f1fe29b4SDavid Howells static inline void nfs_fscache_open_file(struct inode *inode,
175ef79c097SDavid Howells 					 struct file *filp) {}
176a6b5a28eSDave Wysochanski static inline void nfs_fscache_release_file(struct inode *inode, struct file *file) {}
177*000dbe0bSDave Wysochanski static inline int nfs_netfs_readahead(struct readahead_control *ractl)
178*000dbe0bSDave Wysochanski {
179*000dbe0bSDave Wysochanski 	return -ENOBUFS;
180*000dbe0bSDave Wysochanski }
181*000dbe0bSDave Wysochanski static inline int nfs_netfs_read_folio(struct file *file, struct folio *folio)
182*000dbe0bSDave Wysochanski {
183*000dbe0bSDave Wysochanski 	return -ENOBUFS;
184*000dbe0bSDave Wysochanski }
185ef79c097SDavid Howells 
1863577da4aSMatthew Wilcox (Oracle) static inline bool nfs_fscache_release_folio(struct folio *folio, gfp_t gfp)
187545db45fSDavid Howells {
1883577da4aSMatthew Wilcox (Oracle) 	return true; /* may release folio */
189545db45fSDavid Howells }
190a6b5a28eSDave Wysochanski static inline void nfs_fscache_invalidate(struct inode *inode, int flags) {}
191de242c0bSDavid Howells 
1925d1acff1SDavid Howells static inline const char *nfs_server_fscache_state(struct nfs_server *server)
1935d1acff1SDavid Howells {
1945d1acff1SDavid Howells 	return "no ";
1955d1acff1SDavid Howells }
196*000dbe0bSDave Wysochanski static inline void nfs_netfs_set_pgio_header(struct nfs_pgio_header *hdr,
197*000dbe0bSDave Wysochanski 					     struct nfs_pageio_descriptor *desc) {}
198*000dbe0bSDave Wysochanski static inline void nfs_netfs_set_pageio_descriptor(struct nfs_pageio_descriptor *desc,
199*000dbe0bSDave Wysochanski 						   struct nfs_pgio_header *hdr) {}
200*000dbe0bSDave Wysochanski static inline void nfs_netfs_reset_pageio_descriptor(struct nfs_pageio_descriptor *desc) {}
2018ec442aeSDavid Howells #endif /* CONFIG_NFS_FSCACHE */
2028ec442aeSDavid Howells #endif /* _NFS_FSCACHE_H */
203