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
37000dbe0bSDave Wysochanski struct nfs_netfs_io_data {
38000dbe0bSDave Wysochanski /*
39000dbe0bSDave Wysochanski * NFS may split a netfs_io_subrequest into multiple RPCs, each
40000dbe0bSDave Wysochanski * with their own read completion. In netfs, we can only call
41000dbe0bSDave Wysochanski * netfs_subreq_terminated() once for each subrequest. Use the
42000dbe0bSDave Wysochanski * refcount here to double as a marker of the last RPC completion,
43000dbe0bSDave Wysochanski * and only call netfs via netfs_subreq_terminated() once.
44000dbe0bSDave Wysochanski */
45000dbe0bSDave Wysochanski refcount_t refcount;
46000dbe0bSDave Wysochanski struct netfs_io_subrequest *sreq;
47000dbe0bSDave Wysochanski
48000dbe0bSDave Wysochanski /*
49000dbe0bSDave Wysochanski * Final disposition of the netfs_io_subrequest, sent in
50000dbe0bSDave Wysochanski * netfs_subreq_terminated()
51000dbe0bSDave Wysochanski */
52000dbe0bSDave Wysochanski atomic64_t transferred;
53000dbe0bSDave Wysochanski int error;
54000dbe0bSDave Wysochanski };
55000dbe0bSDave Wysochanski
nfs_netfs_get(struct nfs_netfs_io_data * netfs)56000dbe0bSDave Wysochanski static inline void nfs_netfs_get(struct nfs_netfs_io_data *netfs)
57000dbe0bSDave Wysochanski {
58000dbe0bSDave Wysochanski refcount_inc(&netfs->refcount);
59000dbe0bSDave Wysochanski }
60000dbe0bSDave Wysochanski
nfs_netfs_put(struct nfs_netfs_io_data * netfs)61000dbe0bSDave Wysochanski static inline void nfs_netfs_put(struct nfs_netfs_io_data *netfs)
62000dbe0bSDave Wysochanski {
63000dbe0bSDave Wysochanski ssize_t final_len;
64000dbe0bSDave Wysochanski
65000dbe0bSDave Wysochanski /* Only the last RPC completion should call netfs_subreq_terminated() */
66000dbe0bSDave Wysochanski if (!refcount_dec_and_test(&netfs->refcount))
67000dbe0bSDave Wysochanski return;
68000dbe0bSDave Wysochanski
69000dbe0bSDave Wysochanski /*
70000dbe0bSDave Wysochanski * The NFS pageio interface may read a complete page, even when netfs
71000dbe0bSDave Wysochanski * only asked for a partial page. Specifically, this may be seen when
72000dbe0bSDave Wysochanski * one thread is truncating a file while another one is reading the last
73000dbe0bSDave Wysochanski * page of the file.
74000dbe0bSDave Wysochanski * Correct the final length here to be no larger than the netfs subrequest
75000dbe0bSDave Wysochanski * length, and thus avoid netfs's "Subreq overread" warning message.
76000dbe0bSDave Wysochanski */
77000dbe0bSDave Wysochanski final_len = min_t(s64, netfs->sreq->len, atomic64_read(&netfs->transferred));
78000dbe0bSDave Wysochanski netfs_subreq_terminated(netfs->sreq, netfs->error ?: final_len, false);
79000dbe0bSDave Wysochanski kfree(netfs);
80000dbe0bSDave Wysochanski }
nfs_netfs_inode_init(struct nfs_inode * nfsi)81000dbe0bSDave Wysochanski static inline void nfs_netfs_inode_init(struct nfs_inode *nfsi)
82000dbe0bSDave Wysochanski {
83000dbe0bSDave Wysochanski netfs_inode_init(&nfsi->netfs, &nfs_netfs_ops);
84000dbe0bSDave Wysochanski }
85000dbe0bSDave Wysochanski extern void nfs_netfs_initiate_read(struct nfs_pgio_header *hdr);
86000dbe0bSDave Wysochanski extern void nfs_netfs_read_completion(struct nfs_pgio_header *hdr);
87000dbe0bSDave Wysochanski extern int nfs_netfs_folio_unlock(struct folio *folio);
88000dbe0bSDave 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 *);
99000dbe0bSDave Wysochanski extern int nfs_netfs_readahead(struct readahead_control *ractl);
100000dbe0bSDave Wysochanski extern int nfs_netfs_read_folio(struct file *file, struct folio *folio);
1019a9fc1c0SDavid Howells
nfs_fscache_release_folio(struct folio * folio,gfp_t gfp)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 }
109000dbe0bSDave Wysochanski fscache_note_page_release(netfs_i_cookie(netfs_inode(folio->mapping->host)));
110a6b5a28eSDave Wysochanski return true;
111545db45fSDavid Howells }
112545db45fSDavid Howells
nfs_fscache_update_auxdata(struct nfs_fscache_inode_auxdata * auxdata,struct inode * inode)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;
119*55e04e9cSJeff Layton auxdata->ctime_sec = inode_get_ctime(inode).tv_sec;
120*55e04e9cSJeff Layton auxdata->ctime_nsec = inode_get_ctime(inode).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 */
nfs_fscache_invalidate(struct inode * inode,int flags)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 */
nfs_server_fscache_state(struct nfs_server * server)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
nfs_netfs_set_pgio_header(struct nfs_pgio_header * hdr,struct nfs_pageio_descriptor * desc)148000dbe0bSDave Wysochanski static inline void nfs_netfs_set_pgio_header(struct nfs_pgio_header *hdr,
149000dbe0bSDave Wysochanski struct nfs_pageio_descriptor *desc)
150000dbe0bSDave Wysochanski {
151000dbe0bSDave Wysochanski hdr->netfs = desc->pg_netfs;
152000dbe0bSDave Wysochanski }
nfs_netfs_set_pageio_descriptor(struct nfs_pageio_descriptor * desc,struct nfs_pgio_header * hdr)153000dbe0bSDave Wysochanski static inline void nfs_netfs_set_pageio_descriptor(struct nfs_pageio_descriptor *desc,
154000dbe0bSDave Wysochanski struct nfs_pgio_header *hdr)
155000dbe0bSDave Wysochanski {
156000dbe0bSDave Wysochanski desc->pg_netfs = hdr->netfs;
157000dbe0bSDave Wysochanski }
nfs_netfs_reset_pageio_descriptor(struct nfs_pageio_descriptor * desc)158000dbe0bSDave Wysochanski static inline void nfs_netfs_reset_pageio_descriptor(struct nfs_pageio_descriptor *desc)
159000dbe0bSDave Wysochanski {
160000dbe0bSDave Wysochanski desc->pg_netfs = NULL;
161000dbe0bSDave Wysochanski }
1628ec442aeSDavid Howells #else /* CONFIG_NFS_FSCACHE */
nfs_netfs_inode_init(struct nfs_inode * nfsi)163000dbe0bSDave Wysochanski static inline void nfs_netfs_inode_init(struct nfs_inode *nfsi) {}
nfs_netfs_initiate_read(struct nfs_pgio_header * hdr)164000dbe0bSDave Wysochanski static inline void nfs_netfs_initiate_read(struct nfs_pgio_header *hdr) {}
nfs_netfs_read_completion(struct nfs_pgio_header * hdr)165000dbe0bSDave Wysochanski static inline void nfs_netfs_read_completion(struct nfs_pgio_header *hdr) {}
nfs_netfs_folio_unlock(struct folio * folio)166000dbe0bSDave Wysochanski static inline int nfs_netfs_folio_unlock(struct folio *folio)
167000dbe0bSDave Wysochanski {
168000dbe0bSDave Wysochanski return 1;
169000dbe0bSDave Wysochanski }
nfs_fscache_release_super_cookie(struct super_block * sb)17008734048SDavid Howells static inline void nfs_fscache_release_super_cookie(struct super_block *sb) {}
17108734048SDavid Howells
nfs_fscache_init_inode(struct inode * inode)172f1fe29b4SDavid Howells static inline void nfs_fscache_init_inode(struct inode *inode) {}
nfs_fscache_clear_inode(struct inode * inode)173f1fe29b4SDavid Howells static inline void nfs_fscache_clear_inode(struct inode *inode) {}
nfs_fscache_open_file(struct inode * inode,struct file * filp)174f1fe29b4SDavid Howells static inline void nfs_fscache_open_file(struct inode *inode,
175ef79c097SDavid Howells struct file *filp) {}
nfs_fscache_release_file(struct inode * inode,struct file * file)176a6b5a28eSDave Wysochanski static inline void nfs_fscache_release_file(struct inode *inode, struct file *file) {}
nfs_netfs_readahead(struct readahead_control * ractl)177000dbe0bSDave Wysochanski static inline int nfs_netfs_readahead(struct readahead_control *ractl)
178000dbe0bSDave Wysochanski {
179000dbe0bSDave Wysochanski return -ENOBUFS;
180000dbe0bSDave Wysochanski }
nfs_netfs_read_folio(struct file * file,struct folio * folio)181000dbe0bSDave Wysochanski static inline int nfs_netfs_read_folio(struct file *file, struct folio *folio)
182000dbe0bSDave Wysochanski {
183000dbe0bSDave Wysochanski return -ENOBUFS;
184000dbe0bSDave Wysochanski }
185ef79c097SDavid Howells
nfs_fscache_release_folio(struct folio * folio,gfp_t gfp)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 }
nfs_fscache_invalidate(struct inode * inode,int flags)190a6b5a28eSDave Wysochanski static inline void nfs_fscache_invalidate(struct inode *inode, int flags) {}
191de242c0bSDavid Howells
nfs_server_fscache_state(struct nfs_server * server)1925d1acff1SDavid Howells static inline const char *nfs_server_fscache_state(struct nfs_server *server)
1935d1acff1SDavid Howells {
1945d1acff1SDavid Howells return "no ";
1955d1acff1SDavid Howells }
nfs_netfs_set_pgio_header(struct nfs_pgio_header * hdr,struct nfs_pageio_descriptor * desc)196000dbe0bSDave Wysochanski static inline void nfs_netfs_set_pgio_header(struct nfs_pgio_header *hdr,
197000dbe0bSDave Wysochanski struct nfs_pageio_descriptor *desc) {}
nfs_netfs_set_pageio_descriptor(struct nfs_pageio_descriptor * desc,struct nfs_pgio_header * hdr)198000dbe0bSDave Wysochanski static inline void nfs_netfs_set_pageio_descriptor(struct nfs_pageio_descriptor *desc,
199000dbe0bSDave Wysochanski struct nfs_pgio_header *hdr) {}
nfs_netfs_reset_pageio_descriptor(struct nfs_pageio_descriptor * desc)200000dbe0bSDave 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