xref: /openbmc/linux/fs/nfs/internal.h (revision c9301cb3)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2f7b422b1SDavid Howells /*
3f7b422b1SDavid Howells  * NFS internal definitions
4f7b422b1SDavid Howells  */
5f7b422b1SDavid Howells 
6eedc020eSAndy Adamson #include "nfs4_fs.h"
7f2aedb71SDavid Howells #include <linux/fs_context.h>
8f9c3a380SEric Paris #include <linux/security.h>
91264a2f0STrond Myklebust #include <linux/crc32.h>
109954bf92SDavid Howells #include <linux/sunrpc/addr.h>
1147af81f2SPeng Tao #include <linux/nfs_page.h>
125dd43ce2SIngo Molnar #include <linux/wait_bit.h>
13f7b422b1SDavid Howells 
1462a55d08SScott Mayhew #define NFS_SB_MASK (SB_RDONLY|SB_NOSUID|SB_NODEV|SB_NOEXEC|SB_SYNCHRONOUS)
1508734048SDavid Howells 
1620fa1902SPeng Tao extern const struct export_operations nfs_export_ops;
1720fa1902SPeng Tao 
1854ceac45SDavid Howells struct nfs_string;
19f2aedb71SDavid Howells struct nfs_pageio_descriptor;
2054ceac45SDavid Howells 
217ebb9315SBryan Schumaker static inline void nfs_attr_check_mountpoint(struct super_block *parent, struct nfs_fattr *fattr)
227ebb9315SBryan Schumaker {
237ebb9315SBryan Schumaker 	if (!nfs_fsid_equal(&NFS_SB(parent)->fsid, &fattr->fsid))
247ebb9315SBryan Schumaker 		fattr->valid |= NFS_ATTR_FATTR_MOUNTPOINT;
257ebb9315SBryan Schumaker }
267ebb9315SBryan Schumaker 
27533eb461SAndy Adamson static inline int nfs_attr_use_mounted_on_fileid(struct nfs_fattr *fattr)
28533eb461SAndy Adamson {
29533eb461SAndy Adamson 	if (((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) == 0) ||
30533eb461SAndy Adamson 	    (((fattr->valid & NFS_ATTR_FATTR_MOUNTPOINT) == 0) &&
31533eb461SAndy Adamson 	     ((fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) == 0)))
32533eb461SAndy Adamson 		return 0;
33533eb461SAndy Adamson 	return 1;
34533eb461SAndy Adamson }
35533eb461SAndy Adamson 
36f7b37b8bSTrond Myklebust static inline bool nfs_lookup_is_soft_revalidate(const struct dentry *dentry)
37f7b37b8bSTrond Myklebust {
38f7b37b8bSTrond Myklebust 	if (!(NFS_SB(dentry->d_sb)->flags & NFS_MOUNT_SOFTREVAL))
39f7b37b8bSTrond Myklebust 		return false;
40f7b37b8bSTrond Myklebust 	if (!d_is_positive(dentry) || !NFS_FH(d_inode(dentry))->size)
41f7b37b8bSTrond Myklebust 		return false;
42f7b37b8bSTrond Myklebust 	return true;
43f7b37b8bSTrond Myklebust }
44f7b37b8bSTrond Myklebust 
456b18eaa0S\"Talpey, Thomas\ /*
46a14017dbSChuck Lever  * Note: RFC 1813 doesn't limit the number of auth flavors that
47a14017dbSChuck Lever  * a server can return, so make something up.
48a14017dbSChuck Lever  */
49a14017dbSChuck Lever #define NFS_MAX_SECFLAVORS	(12)
50a14017dbSChuck Lever 
51a14017dbSChuck Lever /*
524cfd74fcSChuck Lever  * Value used if the user did not specify a port value.
534cfd74fcSChuck Lever  */
544cfd74fcSChuck Lever #define NFS_UNSPEC_PORT		(-1)
554cfd74fcSChuck Lever 
56a956bedaSTrond Myklebust #define NFS_UNSPEC_RETRANS	(UINT_MAX)
57a956bedaSTrond Myklebust #define NFS_UNSPEC_TIMEO	(UINT_MAX)
58a956bedaSTrond Myklebust 
59fcf10398SBryan Schumaker struct nfs_client_initdata {
60fcf10398SBryan Schumaker 	unsigned long init_flags;
615c6e5b60STrond Myklebust 	const char *hostname;			/* Hostname of the server */
625c6e5b60STrond Myklebust 	const struct sockaddr *addr;		/* Address of the server */
635c6e5b60STrond Myklebust 	const char *nodename;			/* Hostname of the client */
645c6e5b60STrond Myklebust 	const char *ip_addr;			/* IP address of the client */
65fcf10398SBryan Schumaker 	size_t addrlen;
66ab7017a3SBryan Schumaker 	struct nfs_subversion *nfs_mod;
67fcf10398SBryan Schumaker 	int proto;
68fcf10398SBryan Schumaker 	u32 minorversion;
696619079dSTrond Myklebust 	unsigned int nconnect;
70fcf10398SBryan Schumaker 	struct net *net;
715c6e5b60STrond Myklebust 	const struct rpc_timeout *timeparms;
721a58e8a0STrond Myklebust 	const struct cred *cred;
73fcf10398SBryan Schumaker };
74fcf10398SBryan Schumaker 
7556e4ebf8SBryan Schumaker /*
766b18eaa0S\"Talpey, Thomas\  * In-kernel mount arguments
776b18eaa0S\"Talpey, Thomas\  */
785eb005caSDavid Howells struct nfs_fs_context {
79f2aedb71SDavid Howells 	bool			internal;
80f2aedb71SDavid Howells 	bool			skip_reconfig_option_check;
81f2aedb71SDavid Howells 	bool			need_mount;
82f2aedb71SDavid Howells 	bool			sloppy;
83e558100fSDavid Howells 	unsigned int		flags;		/* NFS{,4}_MOUNT_* flags */
848cb7f74eSChuck Lever 	unsigned int		rsize, wsize;
858cb7f74eSChuck Lever 	unsigned int		timeo, retrans;
86e558100fSDavid Howells 	unsigned int		acregmin, acregmax;
87e558100fSDavid Howells 	unsigned int		acdirmin, acdirmax;
888cb7f74eSChuck Lever 	unsigned int		namlen;
89b797cac7SDavid Howells 	unsigned int		options;
906b18eaa0S\"Talpey, Thomas\ 	unsigned int		bsize;
91a3f73c27SWeston Andros Adamson 	struct nfs_auth_info	auth_info;
92a3f73c27SWeston Andros Adamson 	rpc_authflavor_t	selected_flavor;
936b18eaa0S\"Talpey, Thomas\ 	char			*client_address;
94764302ccSChuck Lever 	unsigned int		version;
953fd5be9eSMike Sager 	unsigned int		minorversion;
9608734048SDavid Howells 	char			*fscache_uniq;
97f8ee01e3SDavid Howells 	unsigned short		protofamily;
98f8ee01e3SDavid Howells 	unsigned short		mountfamily;
996b18eaa0S\"Talpey, Thomas\ 
1006b18eaa0S\"Talpey, Thomas\ 	struct {
101e558100fSDavid Howells 		union {
102e558100fSDavid Howells 			struct sockaddr	address;
103e558100fSDavid Howells 			struct sockaddr_storage	_address;
104e558100fSDavid Howells 		};
1054c568017SChuck Lever 		size_t			addrlen;
1066b18eaa0S\"Talpey, Thomas\ 		char			*hostname;
10778fa701fSChuck Lever 		u32			version;
1084cfd74fcSChuck Lever 		int			port;
10978fa701fSChuck Lever 		unsigned short		protocol;
1106b18eaa0S\"Talpey, Thomas\ 	} mount_server;
1116b18eaa0S\"Talpey, Thomas\ 
1126b18eaa0S\"Talpey, Thomas\ 	struct {
113e558100fSDavid Howells 		union {
114e558100fSDavid Howells 			struct sockaddr	address;
115e558100fSDavid Howells 			struct sockaddr_storage	_address;
116e558100fSDavid Howells 		};
1174c568017SChuck Lever 		size_t			addrlen;
1186b18eaa0S\"Talpey, Thomas\ 		char			*hostname;
1196b18eaa0S\"Talpey, Thomas\ 		char			*export_path;
1204cfd74fcSChuck Lever 		int			port;
12178fa701fSChuck Lever 		unsigned short		protocol;
12228cc5cd8STrond Myklebust 		unsigned short		nconnect;
123f2aedb71SDavid Howells 		unsigned short		export_path_len;
1246b18eaa0S\"Talpey, Thomas\ 	} nfs_server;
125f9c3a380SEric Paris 
12662a55d08SScott Mayhew 	struct nfs_fh		*mntfh;
12762a55d08SScott Mayhew 	struct nfs_server	*server;
12862a55d08SScott Mayhew 	struct nfs_subversion	*nfs_mod;
12948be8a66SDavid Howells 
13062a55d08SScott Mayhew 	/* Information for a cloned mount. */
13162a55d08SScott Mayhew 	struct nfs_clone_mount {
13262a55d08SScott Mayhew 		struct super_block	*sb;
13362a55d08SScott Mayhew 		struct dentry		*dentry;
13462a55d08SScott Mayhew 		struct nfs_fattr	*fattr;
13562a55d08SScott Mayhew 		unsigned int		inherited_bsize;
13662a55d08SScott Mayhew 	} clone_data;
1376b18eaa0S\"Talpey, Thomas\ };
1386b18eaa0S\"Talpey, Thomas\ 
139c98e9daaSScott Mayhew #define nfs_errorf(fc, fmt, ...) ((fc)->log.log ?		\
140c98e9daaSScott Mayhew 	errorf(fc, fmt, ## __VA_ARGS__) :			\
141c98e9daaSScott Mayhew 	({ dprintk(fmt "\n", ## __VA_ARGS__); }))
142c98e9daaSScott Mayhew 
143c98e9daaSScott Mayhew #define nfs_ferrorf(fc, fac, fmt, ...) ((fc)->log.log ?		\
144c98e9daaSScott Mayhew 	errorf(fc, fmt, ## __VA_ARGS__) :			\
145c98e9daaSScott Mayhew 	({ dfprintk(fac, fmt "\n", ## __VA_ARGS__); }))
146c98e9daaSScott Mayhew 
147c98e9daaSScott Mayhew #define nfs_invalf(fc, fmt, ...) ((fc)->log.log ?		\
148c98e9daaSScott Mayhew 	invalf(fc, fmt, ## __VA_ARGS__) :			\
149c98e9daaSScott Mayhew 	({ dprintk(fmt "\n", ## __VA_ARGS__);  -EINVAL; }))
150c98e9daaSScott Mayhew 
151c98e9daaSScott Mayhew #define nfs_finvalf(fc, fac, fmt, ...) ((fc)->log.log ?		\
152c98e9daaSScott Mayhew 	invalf(fc, fmt, ## __VA_ARGS__) :			\
153c98e9daaSScott Mayhew 	({ dfprintk(fac, fmt "\n", ## __VA_ARGS__);  -EINVAL; }))
154c98e9daaSScott Mayhew 
155c98e9daaSScott Mayhew #define nfs_warnf(fc, fmt, ...) ((fc)->log.log ?		\
156c98e9daaSScott Mayhew 	warnf(fc, fmt, ## __VA_ARGS__) :			\
157c98e9daaSScott Mayhew 	({ dprintk(fmt "\n", ## __VA_ARGS__); }))
158c98e9daaSScott Mayhew 
159c98e9daaSScott Mayhew #define nfs_fwarnf(fc, fac, fmt, ...) ((fc)->log.log ?		\
160c98e9daaSScott Mayhew 	warnf(fc, fmt, ## __VA_ARGS__) :			\
161c98e9daaSScott Mayhew 	({ dfprintk(fac, fmt "\n", ## __VA_ARGS__); }))
162ce8866f0SScott Mayhew 
163f2aedb71SDavid Howells static inline struct nfs_fs_context *nfs_fc2context(const struct fs_context *fc)
164f2aedb71SDavid Howells {
165f2aedb71SDavid Howells 	return fc->fs_private;
166f2aedb71SDavid Howells }
167f2aedb71SDavid Howells 
168146ec944SChuck Lever /* mount_clnt.c */
169c5d120f8SChuck Lever struct nfs_mount_request {
170c5d120f8SChuck Lever 	struct sockaddr		*sap;
171c5d120f8SChuck Lever 	size_t			salen;
172c5d120f8SChuck Lever 	char			*hostname;
173c5d120f8SChuck Lever 	char			*dirpath;
174c5d120f8SChuck Lever 	u32			version;
175c5d120f8SChuck Lever 	unsigned short		protocol;
176c5d120f8SChuck Lever 	struct nfs_fh		*fh;
17750a737f8SChuck Lever 	int			noresvport;
1788e02f6b9SChuck Lever 	unsigned int		*auth_flav_len;
1798e02f6b9SChuck Lever 	rpc_authflavor_t	*auth_flavs;
1806d59b8d5SStanislav Kinsbursky 	struct net		*net;
181c5d120f8SChuck Lever };
182c5d120f8SChuck Lever 
183*c9301cb3SEryu Guan extern int nfs_mount(struct nfs_mount_request *info, int timeo, int retrans);
1840b524123SChuck Lever extern void nfs_umount(const struct nfs_mount_request *info);
185146ec944SChuck Lever 
18624c8dbbbSDavid Howells /* client.c */
187a613fa16STrond Myklebust extern const struct rpc_program nfs_program;
1886b13168bSStanislav Kinsbursky extern void nfs_clients_init(struct net *net);
18910b7a70cSTrond Myklebust extern void nfs_clients_exit(struct net *net);
1906663ee7fSBryan Schumaker extern struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *);
1915c6e5b60STrond Myklebust int nfs_create_rpc_client(struct nfs_client *, const struct nfs_client_initdata *, rpc_authflavor_t);
1927d38de3fSAnna Schumaker struct nfs_client *nfs_get_client(const struct nfs_client_initdata *);
193fcf10398SBryan Schumaker int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *, struct nfs_fattr *);
194fcf10398SBryan Schumaker void nfs_server_insert_lists(struct nfs_server *);
19532e62b7cSChuck Lever void nfs_server_remove_lists(struct nfs_server *);
196a956bedaSTrond Myklebust void nfs_init_timeout_values(struct rpc_timeout *to, int proto, int timeo, int retrans);
197fcf10398SBryan Schumaker int nfs_init_server_rpcclient(struct nfs_server *, const struct rpc_timeout *t,
198fcf10398SBryan Schumaker 		rpc_authflavor_t);
199fcf10398SBryan Schumaker struct nfs_server *nfs_alloc_server(void);
200fcf10398SBryan Schumaker void nfs_server_copy_userdata(struct nfs_server *, struct nfs_server *);
20154ceac45SDavid Howells 
20224c8dbbbSDavid Howells extern void nfs_put_client(struct nfs_client *);
203cdb7ecedSBryan Schumaker extern void nfs_free_client(struct nfs_client *);
20428cd1b3fSStanislav Kinsbursky extern struct nfs_client *nfs4_find_client_ident(struct net *, int);
205c36fca52SAndy Adamson extern struct nfs_client *
206c7add9a9SStanislav Kinsbursky nfs4_find_client_sessionid(struct net *, const struct sockaddr *,
207459de2edSBryan Schumaker 				struct nfs4_sessionid *, u32);
20862a55d08SScott Mayhew extern struct nfs_server *nfs_create_server(struct fs_context *);
20962a55d08SScott Mayhew extern struct nfs_server *nfs4_create_server(struct fs_context *);
21062a55d08SScott Mayhew extern struct nfs_server *nfs4_create_referral_server(struct fs_context *);
21132e62b7cSChuck Lever extern int nfs4_update_server(struct nfs_server *server, const char *hostname,
212292f503cSTrond Myklebust 					struct sockaddr *sap, size_t salen,
213292f503cSTrond Myklebust 					struct net *net);
21454ceac45SDavid Howells extern void nfs_free_server(struct nfs_server *server);
21554ceac45SDavid Howells extern struct nfs_server *nfs_clone_server(struct nfs_server *,
21654ceac45SDavid Howells 					   struct nfs_fh *,
2177e6eb683SBryan Schumaker 					   struct nfs_fattr *,
2187e6eb683SBryan Schumaker 					   rpc_authflavor_t);
219a33e4b03SWeston Andros Adamson extern bool nfs_client_init_is_complete(const struct nfs_client *clp);
220a33e4b03SWeston Andros Adamson extern int nfs_client_init_status(const struct nfs_client *clp);
2214697bd5eSTrond Myklebust extern int nfs_wait_client_init_complete(const struct nfs_client *clp);
22276db6d95SAndy Adamson extern void nfs_mark_client_ready(struct nfs_client *clp, int state);
2233fc75f12STigran Mkrtchyan extern struct nfs_client *nfs4_set_ds_client(struct nfs_server *mds_srv,
224d83217c1SAndy Adamson 					     const struct sockaddr *ds_addr,
22598fc685aSAndy Adamson 					     int ds_addrlen, int ds_proto,
22698fc685aSAndy Adamson 					     unsigned int ds_timeo,
227064172f3SPeng Tao 					     unsigned int ds_retrans,
2287d38de3fSAnna Schumaker 					     u32 minor_version);
2290e20162eSAndy Adamson extern struct rpc_clnt *nfs4_find_or_create_ds_client(struct nfs_client *,
2300e20162eSAndy Adamson 						struct inode *);
231b224f7cbSTigran Mkrtchyan extern struct nfs_client *nfs3_set_ds_client(struct nfs_server *mds_srv,
2321a04c6e1SPeng Tao 			const struct sockaddr *ds_addr, int ds_addrlen,
2331a04c6e1SPeng Tao 			int ds_proto, unsigned int ds_timeo,
2347d38de3fSAnna Schumaker 			unsigned int ds_retrans);
2356aaca566SDavid Howells #ifdef CONFIG_PROC_FS
2366aaca566SDavid Howells extern int __init nfs_fs_proc_init(void);
2376aaca566SDavid Howells extern void nfs_fs_proc_exit(void);
23865b38851SEric W. Biederman extern int nfs_fs_proc_net_init(struct net *net);
23965b38851SEric W. Biederman extern void nfs_fs_proc_net_exit(struct net *net);
2406aaca566SDavid Howells #else
24165b38851SEric W. Biederman static inline int nfs_fs_proc_net_init(struct net *net)
24265b38851SEric W. Biederman {
24365b38851SEric W. Biederman 	return 0;
24465b38851SEric W. Biederman }
24565b38851SEric W. Biederman static inline void nfs_fs_proc_net_exit(struct net *net)
24665b38851SEric W. Biederman {
24765b38851SEric W. Biederman }
2486aaca566SDavid Howells static inline int nfs_fs_proc_init(void)
2496aaca566SDavid Howells {
2506aaca566SDavid Howells 	return 0;
2516aaca566SDavid Howells }
2526aaca566SDavid Howells static inline void nfs_fs_proc_exit(void)
2536aaca566SDavid Howells {
2546aaca566SDavid Howells }
2556aaca566SDavid Howells #endif
25624c8dbbbSDavid Howells 
257f7b422b1SDavid Howells /* callback_xdr.c */
258e9679189SChristoph Hellwig extern const struct svc_version nfs4_callback_version1;
259e9679189SChristoph Hellwig extern const struct svc_version nfs4_callback_version4;
260f7b422b1SDavid Howells 
261f2aedb71SDavid Howells /* fs_context.c */
262f2aedb71SDavid Howells extern struct file_system_type nfs_fs_type;
2639954bf92SDavid Howells 
264f7b422b1SDavid Howells /* pagelist.c */
265f7b422b1SDavid Howells extern int __init nfs_init_nfspagecache(void);
266266bee88SDavid Brownell extern void nfs_destroy_nfspagecache(void);
267f7b422b1SDavid Howells extern int __init nfs_init_readpagecache(void);
268266bee88SDavid Brownell extern void nfs_destroy_readpagecache(void);
269f7b422b1SDavid Howells extern int __init nfs_init_writepagecache(void);
270266bee88SDavid Brownell extern void nfs_destroy_writepagecache(void);
271f7b422b1SDavid Howells 
272f7b422b1SDavid Howells extern int __init nfs_init_directcache(void);
273266bee88SDavid Brownell extern void nfs_destroy_directcache(void);
2744db6e0b7SFred Isaman extern void nfs_pgheader_init(struct nfs_pageio_descriptor *desc,
2754db6e0b7SFred Isaman 			      struct nfs_pgio_header *hdr,
2764db6e0b7SFred Isaman 			      void (*release)(struct nfs_pgio_header *hdr));
2774db6e0b7SFred Isaman void nfs_set_pgio_error(struct nfs_pgio_header *hdr, int error, loff_t pos);
278210c7c17SBenjamin Coddington int nfs_iocounter_wait(struct nfs_lock_context *l_ctx);
279577b4232STrond Myklebust 
28041d8d5b7SAnna Schumaker extern const struct nfs_pageio_ops nfs_pgio_rw_ops;
2811e7f3a48SWeston Andros Adamson struct nfs_pgio_header *nfs_pgio_header_alloc(const struct nfs_rw_ops *);
2821e7f3a48SWeston Andros Adamson void nfs_pgio_header_free(struct nfs_pgio_header *);
283ef2c488cSAnna Schumaker int nfs_generic_pgio(struct nfs_pageio_descriptor *, struct nfs_pgio_header *);
28446a5ab47SPeng Tao int nfs_initiate_pgio(struct rpc_clnt *clnt, struct nfs_pgio_header *hdr,
285a52458b4SNeilBrown 		      const struct cred *cred, const struct nfs_rpc_ops *rpc_ops,
28646a5ab47SPeng Tao 		      const struct rpc_call_ops *call_ops, int how, int flags);
287d4581383SWeston Andros Adamson void nfs_free_request(struct nfs_page *req);
28848d635f1SPeng Tao struct nfs_pgio_mirror *
28948d635f1SPeng Tao nfs_pgio_current_mirror(struct nfs_pageio_descriptor *desc);
29000bfa30aSAnna Schumaker 
291138a2935STrond Myklebust static inline bool nfs_match_open_context(const struct nfs_open_context *ctx1,
292138a2935STrond Myklebust 		const struct nfs_open_context *ctx2)
293138a2935STrond Myklebust {
294a52458b4SNeilBrown 	return cred_fscmp(ctx1->cred, ctx2->cred) == 0 && ctx1->state == ctx2->state;
295138a2935STrond Myklebust }
296138a2935STrond Myklebust 
297f7b422b1SDavid Howells /* nfs2xdr.c */
298499b4988SChristoph Hellwig extern const struct rpc_procinfo nfs_procedures[];
299573c4e1eSChuck Lever extern int nfs2_decode_dirent(struct xdr_stream *,
300a7a3b1e9SBenjamin Coddington 				struct nfs_entry *, bool);
301f7b422b1SDavid Howells 
302f7b422b1SDavid Howells /* nfs3xdr.c */
303499b4988SChristoph Hellwig extern const struct rpc_procinfo nfs3_procedures[];
304573c4e1eSChuck Lever extern int nfs3_decode_dirent(struct xdr_stream *,
305a7a3b1e9SBenjamin Coddington 				struct nfs_entry *, bool);
306f7b422b1SDavid Howells 
307f7b422b1SDavid Howells /* nfs4xdr.c */
30889d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4)
309573c4e1eSChuck Lever extern int nfs4_decode_dirent(struct xdr_stream *,
310a7a3b1e9SBenjamin Coddington 				struct nfs_entry *, bool);
3117d4e2747SDavid Howells #endif
3122449ea2eSAlexandros Batsakis #ifdef CONFIG_NFS_V4_1
3132449ea2eSAlexandros Batsakis extern const u32 nfs41_maxread_overhead;
3142449ea2eSAlexandros Batsakis extern const u32 nfs41_maxwrite_overhead;
315f1c097beSAndy Adamson extern const u32 nfs41_maxgetdevinfo_overhead;
3162449ea2eSAlexandros Batsakis #endif
317f7b422b1SDavid Howells 
318f7b422b1SDavid Howells /* nfs4proc.c */
31989d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4)
320499b4988SChristoph Hellwig extern const struct rpc_procinfo nfs4_procedures[];
321d75d5414SAndrew Morton #endif
322f7b422b1SDavid Howells 
323694e096fSAnna Schumaker #ifdef CONFIG_NFS_V4_SECURITY_LABEL
324694e096fSAnna Schumaker extern struct nfs4_label *nfs4_label_alloc(struct nfs_server *server, gfp_t flags);
325a49c2691SKinglong Mee static inline struct nfs4_label *
326a49c2691SKinglong Mee nfs4_label_copy(struct nfs4_label *dst, struct nfs4_label *src)
327a49c2691SKinglong Mee {
328a49c2691SKinglong Mee 	if (!dst || !src)
329a49c2691SKinglong Mee 		return NULL;
330a49c2691SKinglong Mee 
331a49c2691SKinglong Mee 	if (src->len > NFS4_MAXLABELLEN)
332a49c2691SKinglong Mee 		return NULL;
333a49c2691SKinglong Mee 
334a49c2691SKinglong Mee 	dst->lfs = src->lfs;
335a49c2691SKinglong Mee 	dst->pi = src->pi;
336a49c2691SKinglong Mee 	dst->len = src->len;
337a49c2691SKinglong Mee 	memcpy(dst->label, src->label, src->len);
338a49c2691SKinglong Mee 
339a49c2691SKinglong Mee 	return dst;
340a49c2691SKinglong Mee }
341694e096fSAnna Schumaker static inline void nfs4_label_free(struct nfs4_label *label)
342694e096fSAnna Schumaker {
343694e096fSAnna Schumaker 	if (label) {
344694e096fSAnna Schumaker 		kfree(label->label);
345694e096fSAnna Schumaker 		kfree(label);
346694e096fSAnna Schumaker 	}
347694e096fSAnna Schumaker 	return;
348694e096fSAnna Schumaker }
349fd1defc2STrond Myklebust 
350fd1defc2STrond Myklebust static inline void nfs_zap_label_cache_locked(struct nfs_inode *nfsi)
351fd1defc2STrond Myklebust {
352fd1defc2STrond Myklebust 	if (nfs_server_capable(&nfsi->vfs_inode, NFS_CAP_SECURITY_LABEL))
353fd1defc2STrond Myklebust 		nfsi->cache_validity |= NFS_INO_INVALID_LABEL;
354fd1defc2STrond Myklebust }
355694e096fSAnna Schumaker #else
356694e096fSAnna Schumaker static inline struct nfs4_label *nfs4_label_alloc(struct nfs_server *server, gfp_t flags) { return NULL; }
357694e096fSAnna Schumaker static inline void nfs4_label_free(void *label) {}
358fd1defc2STrond Myklebust static inline void nfs_zap_label_cache_locked(struct nfs_inode *nfsi)
359fd1defc2STrond Myklebust {
360fd1defc2STrond Myklebust }
361a49c2691SKinglong Mee static inline struct nfs4_label *
362a49c2691SKinglong Mee nfs4_label_copy(struct nfs4_label *dst, struct nfs4_label *src)
363a49c2691SKinglong Mee {
364a49c2691SKinglong Mee 	return NULL;
365a49c2691SKinglong Mee }
366694e096fSAnna Schumaker #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
367694e096fSAnna Schumaker 
3687fe5c398STrond Myklebust /* proc.c */
3697fe5c398STrond Myklebust void nfs_close_context(struct nfs_open_context *ctx, int is_sync);
3708cab4c39SChuck Lever extern struct nfs_client *nfs_init_client(struct nfs_client *clp,
3715c6e5b60STrond Myklebust 			   const struct nfs_client_initdata *);
3727fe5c398STrond Myklebust 
373979df72eSTrond Myklebust /* dir.c */
3741bcf4c5cSTrond Myklebust extern void nfs_advise_use_readdirplus(struct inode *dir);
375311324adSTrond Myklebust extern void nfs_force_use_readdirplus(struct inode *dir);
3761ab6c499SDave Chinner extern unsigned long nfs_access_cache_count(struct shrinker *shrink,
3771ab6c499SDave Chinner 					    struct shrink_control *sc);
3781ab6c499SDave Chinner extern unsigned long nfs_access_cache_scan(struct shrinker *shrink,
3791495f230SYing Han 					   struct shrink_control *sc);
380597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *, struct dentry *, unsigned int);
381549c7297SChristian Brauner int nfs_create(struct user_namespace *, struct inode *, struct dentry *,
382549c7297SChristian Brauner 	       umode_t, bool);
383549c7297SChristian Brauner int nfs_mkdir(struct user_namespace *, struct inode *, struct dentry *,
384549c7297SChristian Brauner 	      umode_t);
385597d9289SBryan Schumaker int nfs_rmdir(struct inode *, struct dentry *);
386597d9289SBryan Schumaker int nfs_unlink(struct inode *, struct dentry *);
387549c7297SChristian Brauner int nfs_symlink(struct user_namespace *, struct inode *, struct dentry *,
388549c7297SChristian Brauner 		const char *);
389597d9289SBryan Schumaker int nfs_link(struct dentry *, struct inode *, struct dentry *);
390549c7297SChristian Brauner int nfs_mknod(struct user_namespace *, struct inode *, struct dentry *, umode_t,
391549c7297SChristian Brauner 	      dev_t);
392549c7297SChristian Brauner int nfs_rename(struct user_namespace *, struct inode *, struct dentry *,
3931cd66c93SMiklos Szeredi 	       struct inode *, struct dentry *, unsigned int);
394979df72eSTrond Myklebust 
395ce4ef7c0SBryan Schumaker /* file.c */
3964ff79bc7SChristoph Hellwig int nfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync);
397ce4ef7c0SBryan Schumaker loff_t nfs_file_llseek(struct file *, loff_t, int);
3983aa2d199SAl Viro ssize_t nfs_file_read(struct kiocb *, struct iov_iter *);
399ce4ef7c0SBryan Schumaker int nfs_file_mmap(struct file *, struct vm_area_struct *);
400edaf4369SAl Viro ssize_t nfs_file_write(struct kiocb *, struct iov_iter *);
401ce4ef7c0SBryan Schumaker int nfs_file_release(struct inode *, struct file *);
402ce4ef7c0SBryan Schumaker int nfs_lock(struct file *, int, struct file_lock *);
403ce4ef7c0SBryan Schumaker int nfs_flock(struct file *, int, struct file_lock *);
404ce4ef7c0SBryan Schumaker int nfs_check_flags(int);
405ce4ef7c0SBryan Schumaker 
406f7b422b1SDavid Howells /* inode.c */
4075746006fSTrond Myklebust extern struct workqueue_struct *nfsiod_workqueue;
408f7b422b1SDavid Howells extern struct inode *nfs_alloc_inode(struct super_block *sb);
409ca1a199eSAl Viro extern void nfs_free_inode(struct inode *);
410a9185b41SChristoph Hellwig extern int nfs_write_inode(struct inode *, struct writeback_control *);
411eed99357STrond Myklebust extern int nfs_drop_inode(struct inode *);
41219d87ca3SBryan Schumaker extern void nfs_clear_inode(struct inode *);
413b57922d9SAl Viro extern void nfs_evict_inode(struct inode *);
414fd6d3feeSTrond Myklebust extern void nfs_zap_acl_cache(struct inode *inode);
415fd6d3feeSTrond Myklebust extern void nfs_set_cache_invalid(struct inode *inode, unsigned long flags);
41661540bf6STrond Myklebust extern bool nfs_check_cache_invalid(struct inode *, unsigned long);
417dfd01f02SPeter Zijlstra extern int nfs_wait_bit_killable(struct wait_bit_key *key, int mode);
4185e4def20SDavid Howells extern int nfs_wait_atomic_killable(atomic_t *p, unsigned int mode);
419f7b422b1SDavid Howells 
420f7b422b1SDavid Howells /* super.c */
4216a74490dSBryan Schumaker extern const struct super_operations nfs_sops;
4224d4b69ddSWeston Andros Adamson bool nfs_auth_info_match(const struct nfs_auth_info *, rpc_authflavor_t);
423f2aedb71SDavid Howells int nfs_try_get_tree(struct fs_context *);
424f2aedb71SDavid Howells int nfs_get_tree_common(struct fs_context *);
425fbdefd64SBryan Schumaker void nfs_kill_super(struct super_block *);
4264ebd9ab3SDominik Hackl 
427f7b422b1SDavid Howells extern struct rpc_stat nfs_rpcstat;
4284ebd9ab3SDominik Hackl 
429f7b422b1SDavid Howells extern int __init register_nfs_fs(void);
430f7b422b1SDavid Howells extern void __exit unregister_nfs_fs(void);
431ea7c38feSTrond Myklebust extern bool nfs_sb_active(struct super_block *sb);
4321daef0a8STrond Myklebust extern void nfs_sb_deactive(struct super_block *sb);
4333c9e502bSTrond Myklebust extern int nfs_client_for_each_server(struct nfs_client *clp,
4343c9e502bSTrond Myklebust 				      int (*fn)(struct nfs_server *, void *),
4353c9e502bSTrond Myklebust 				      void *data);
436a5864c99STrond Myklebust /* io.c */
437a5864c99STrond Myklebust extern void nfs_start_io_read(struct inode *inode);
438a5864c99STrond Myklebust extern void nfs_end_io_read(struct inode *inode);
439a5864c99STrond Myklebust extern void nfs_start_io_write(struct inode *inode);
440a5864c99STrond Myklebust extern void nfs_end_io_write(struct inode *inode);
441a5864c99STrond Myklebust extern void nfs_start_io_direct(struct inode *inode);
442a5864c99STrond Myklebust extern void nfs_end_io_direct(struct inode *inode);
443a5864c99STrond Myklebust 
444651b0e70STrond Myklebust static inline bool nfs_file_io_is_buffered(struct nfs_inode *nfsi)
445651b0e70STrond Myklebust {
446651b0e70STrond Myklebust 	return test_bit(NFS_INO_ODIRECT, &nfsi->flags) == 0;
447651b0e70STrond Myklebust }
448651b0e70STrond Myklebust 
449f7b422b1SDavid Howells /* namespace.c */
45097a54868SBen Hutchings #define NFS_PATH_CANONICAL 1
451b514f872SAl Viro extern char *nfs_path(char **p, struct dentry *dentry,
45297a54868SBen Hutchings 		      char *buffer, ssize_t buflen, unsigned flags);
45336d43a43SDavid Howells extern struct vfsmount *nfs_d_automount(struct path *path);
454f2aedb71SDavid Howells int nfs_submount(struct fs_context *, struct nfs_server *);
455f2aedb71SDavid Howells int nfs_do_submount(struct fs_context *);
456f7b422b1SDavid Howells 
45754ceac45SDavid Howells /* getroot.c */
45862a55d08SScott Mayhew extern int nfs_get_root(struct super_block *s, struct fs_context *fc);
45989d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4)
4605e6b1990STrond Myklebust extern int nfs4_get_rootfh(struct nfs_server *server, struct nfs_fh *mntfh, bool);
4617d4e2747SDavid Howells #endif
462f7b422b1SDavid Howells 
463061ae2edSFred Isaman struct nfs_pgio_completion_ops;
464f11c88afSAndy Adamson /* read.c */
465584aa810SFred Isaman extern void nfs_pageio_init_read(struct nfs_pageio_descriptor *pgio,
466fab5fc25SChristoph Hellwig 			struct inode *inode, bool force_mds,
467584aa810SFred Isaman 			const struct nfs_pgio_completion_ops *compl_ops);
468f11c88afSAndy Adamson extern void nfs_read_prepare(struct rpc_task *task, void *calldata);
469493292ddSTrond Myklebust extern void nfs_pageio_reset_read_mds(struct nfs_pageio_descriptor *pgio);
470e885de1aSTrond Myklebust 
471fbdefd64SBryan Schumaker /* super.c */
472fbdefd64SBryan Schumaker void nfs_umount_begin(struct super_block *);
473fbdefd64SBryan Schumaker int  nfs_statfs(struct dentry *, struct kstatfs *);
474fbdefd64SBryan Schumaker int  nfs_show_options(struct seq_file *, struct dentry *);
475fbdefd64SBryan Schumaker int  nfs_show_devname(struct seq_file *, struct dentry *);
476fbdefd64SBryan Schumaker int  nfs_show_path(struct seq_file *, struct dentry *);
477fbdefd64SBryan Schumaker int  nfs_show_stats(struct seq_file *, struct dentry *);
478f2aedb71SDavid Howells int  nfs_reconfigure(struct fs_context *);
479fbdefd64SBryan Schumaker 
480def6ed7eSAndy Adamson /* write.c */
4811763da12SFred Isaman extern void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
482a20c93e3SChristoph Hellwig 			struct inode *inode, int ioflags, bool force_mds,
4831763da12SFred Isaman 			const struct nfs_pgio_completion_ops *compl_ops);
484dce81290STrond Myklebust extern void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio);
4850b7c0153SFred Isaman extern void nfs_commit_free(struct nfs_commit_data *p);
486def6ed7eSAndy Adamson extern void nfs_write_prepare(struct rpc_task *task, void *calldata);
4870b7c0153SFred Isaman extern void nfs_commit_prepare(struct rpc_task *task, void *calldata);
4880b7c0153SFred Isaman extern int nfs_initiate_commit(struct rpc_clnt *clnt,
4890b7c0153SFred Isaman 			       struct nfs_commit_data *data,
490c36aae9aSPeng Tao 			       const struct nfs_rpc_ops *nfs_ops,
491e0c2b380SFred Isaman 			       const struct rpc_call_ops *call_ops,
4929f0ec176SAndy Adamson 			       int how, int flags);
4930b7c0153SFred Isaman extern void nfs_init_commit(struct nfs_commit_data *data,
494e0c2b380SFred Isaman 			    struct list_head *head,
495f453a54aSFred Isaman 			    struct pnfs_layout_segment *lseg,
496f453a54aSFred Isaman 			    struct nfs_commit_info *cinfo);
4971763da12SFred Isaman int nfs_scan_commit_list(struct list_head *src, struct list_head *dst,
4981763da12SFred Isaman 			 struct nfs_commit_info *cinfo, int max);
499ce59515cSAnna Schumaker unsigned long nfs_reqs_to_commit(struct nfs_commit_info *);
5001763da12SFred Isaman int nfs_scan_commit(struct inode *inode, struct list_head *dst,
5011763da12SFred Isaman 		    struct nfs_commit_info *cinfo);
5021763da12SFred Isaman void nfs_mark_request_commit(struct nfs_page *req,
5031763da12SFred Isaman 			     struct pnfs_layout_segment *lseg,
504b57ff130SWeston Andros Adamson 			     struct nfs_commit_info *cinfo,
505b57ff130SWeston Andros Adamson 			     u32 ds_commit_idx);
506c65e6254SWeston Andros Adamson int nfs_write_need_commit(struct nfs_pgio_header *);
507a08a8cd3STrond Myklebust void nfs_writeback_update_inode(struct nfs_pgio_header *hdr);
5081763da12SFred Isaman int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
5091763da12SFred Isaman 			    int how, struct nfs_commit_info *cinfo);
510e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list,
511ea2cf228SFred Isaman 		      struct pnfs_layout_segment *lseg,
512b57ff130SWeston Andros Adamson 		      struct nfs_commit_info *cinfo,
513b57ff130SWeston Andros Adamson 		      u32 ds_commit_idx);
5140b7c0153SFred Isaman void nfs_commitdata_release(struct nfs_commit_data *data);
5156272dcc6SAnna Schumaker void nfs_request_add_commit_list(struct nfs_page *req,
516ea2cf228SFred Isaman 				 struct nfs_commit_info *cinfo);
51786d80f97STrond Myklebust void nfs_request_add_commit_list_locked(struct nfs_page *req,
51886d80f97STrond Myklebust 		struct list_head *dst,
51986d80f97STrond Myklebust 		struct nfs_commit_info *cinfo);
520ea2cf228SFred Isaman void nfs_request_remove_commit_list(struct nfs_page *req,
521ea2cf228SFred Isaman 				    struct nfs_commit_info *cinfo);
522ea2cf228SFred Isaman void nfs_init_cinfo(struct nfs_commit_info *cinfo,
523ea2cf228SFred Isaman 		    struct inode *inode,
524ea2cf228SFred Isaman 		    struct nfs_direct_req *dreq);
525dc24826bSAndy Adamson int nfs_key_timeout_notify(struct file *filp, struct inode *inode);
526ce52914eSScott Mayhew bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx, struct inode *inode);
527a7d42ddbSWeston Andros Adamson void nfs_pageio_stop_mirroring(struct nfs_pageio_descriptor *pgio);
528e0c2b380SFred Isaman 
529837bb1d7STrond Myklebust int nfs_filemap_write_and_wait_range(struct address_space *mapping,
530837bb1d7STrond Myklebust 		loff_t lstart, loff_t lend);
531837bb1d7STrond Myklebust 
532a5314a74STrond Myklebust #ifdef CONFIG_NFS_V4_1
533e3b9f7e6STrond Myklebust static inline void
534e3b9f7e6STrond Myklebust pnfs_bucket_clear_pnfs_ds_commit_verifiers(struct pnfs_commit_bucket *buckets,
535e3b9f7e6STrond Myklebust 		unsigned int nbuckets)
536e3b9f7e6STrond Myklebust {
537e3b9f7e6STrond Myklebust 	unsigned int i;
538e3b9f7e6STrond Myklebust 
539e3b9f7e6STrond Myklebust 	for (i = 0; i < nbuckets; i++)
540e3b9f7e6STrond Myklebust 		buckets[i].direct_verf.committed = NFS_INVALID_STABLE_HOW;
541e3b9f7e6STrond Myklebust }
542a5314a74STrond Myklebust static inline
543a5314a74STrond Myklebust void nfs_clear_pnfs_ds_commit_verifiers(struct pnfs_ds_commit_info *cinfo)
544a5314a74STrond Myklebust {
545e3b9f7e6STrond Myklebust 	struct pnfs_commit_array *array;
546a5314a74STrond Myklebust 
547a9901899STrond Myklebust 	rcu_read_lock();
548a9901899STrond Myklebust 	list_for_each_entry_rcu(array, &cinfo->commits, cinfo_list)
549e3b9f7e6STrond Myklebust 		pnfs_bucket_clear_pnfs_ds_commit_verifiers(array->buckets,
550e3b9f7e6STrond Myklebust 				array->nbuckets);
551a9901899STrond Myklebust 	rcu_read_unlock();
552a5314a74STrond Myklebust }
553a5314a74STrond Myklebust #else
554a5314a74STrond Myklebust static inline
555a5314a74STrond Myklebust void nfs_clear_pnfs_ds_commit_verifiers(struct pnfs_ds_commit_info *cinfo)
556a5314a74STrond Myklebust {
557a5314a74STrond Myklebust }
558a5314a74STrond Myklebust #endif
559a5314a74STrond Myklebust 
560074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION
561074cc1deSTrond Myklebust extern int nfs_migrate_page(struct address_space *,
562a6bc32b8SMel Gorman 		struct page *, struct page *, enum migrate_mode);
563074cc1deSTrond Myklebust #endif
564def6ed7eSAndy Adamson 
5658fc3c386STrond Myklebust static inline int
5668fc3c386STrond Myklebust nfs_write_verifier_cmp(const struct nfs_write_verifier *v1,
5678fc3c386STrond Myklebust 		const struct nfs_write_verifier *v2)
5688fc3c386STrond Myklebust {
5698fc3c386STrond Myklebust 	return memcmp(v1->data, v2->data, sizeof(v1->data));
5708fc3c386STrond Myklebust }
5718fc3c386STrond Myklebust 
5721f28476dSTrond Myklebust static inline bool
5731f28476dSTrond Myklebust nfs_write_match_verf(const struct nfs_writeverf *verf,
5741f28476dSTrond Myklebust 		struct nfs_page *req)
5751f28476dSTrond Myklebust {
5761f28476dSTrond Myklebust 	return verf->committed > NFS_UNSTABLE &&
5771f28476dSTrond Myklebust 		!nfs_write_verifier_cmp(&req->wb_verf, &verf->verifier);
5781f28476dSTrond Myklebust }
5791f28476dSTrond Myklebust 
5800e862a40SJeff Layton /* unlink.c */
5810e862a40SJeff Layton extern struct rpc_task *
5820e862a40SJeff Layton nfs_async_rename(struct inode *old_dir, struct inode *new_dir,
5830e862a40SJeff Layton 		 struct dentry *old_dentry, struct dentry *new_dentry,
5840e862a40SJeff Layton 		 void (*complete)(struct rpc_task *, struct nfs_renamedata *));
5850e862a40SJeff Layton extern int nfs_sillyrename(struct inode *dir, struct dentry *dentry);
5860e862a40SJeff Layton 
5871763da12SFred Isaman /* direct.c */
5881763da12SFred Isaman void nfs_init_cinfo_from_dreq(struct nfs_commit_info *cinfo,
5891763da12SFred Isaman 			      struct nfs_direct_req *dreq);
5906296556fSPeng Tao extern ssize_t nfs_dreq_bytes_left(struct nfs_direct_req *dreq);
5911763da12SFred Isaman 
592cccef3b9SAndy Adamson /* nfs4proc.c */
5938cab4c39SChuck Lever extern struct nfs_client *nfs4_init_client(struct nfs_client *clp,
5945c6e5b60STrond Myklebust 			    const struct nfs_client_initdata *);
59505f4c350SChuck Lever extern int nfs40_walk_client_list(struct nfs_client *clp,
59605f4c350SChuck Lever 				struct nfs_client **result,
597a52458b4SNeilBrown 				const struct cred *cred);
59805f4c350SChuck Lever extern int nfs41_walk_client_list(struct nfs_client *clp,
59905f4c350SChuck Lever 				struct nfs_client **result,
600a52458b4SNeilBrown 				const struct cred *cred);
60110e037d1SSantosh kumar pradhan extern void nfs4_test_session_trunk(struct rpc_clnt *clnt,
60210e037d1SSantosh kumar pradhan 				struct rpc_xprt *xprt,
60310e037d1SSantosh kumar pradhan 				void *data);
604cccef3b9SAndy Adamson 
605ea7c38feSTrond Myklebust static inline struct inode *nfs_igrab_and_active(struct inode *inode)
606ea7c38feSTrond Myklebust {
607896567eeSTrond Myklebust 	struct super_block *sb = inode->i_sb;
608896567eeSTrond Myklebust 
609896567eeSTrond Myklebust 	if (sb && nfs_sb_active(sb)) {
610896567eeSTrond Myklebust 		if (igrab(inode))
611ea7c38feSTrond Myklebust 			return inode;
612896567eeSTrond Myklebust 		nfs_sb_deactive(sb);
613896567eeSTrond Myklebust 	}
614896567eeSTrond Myklebust 	return NULL;
615ea7c38feSTrond Myklebust }
616ea7c38feSTrond Myklebust 
617ea7c38feSTrond Myklebust static inline void nfs_iput_and_deactive(struct inode *inode)
618ea7c38feSTrond Myklebust {
619ea7c38feSTrond Myklebust 	if (inode != NULL) {
620ea7c38feSTrond Myklebust 		struct super_block *sb = inode->i_sb;
621ea7c38feSTrond Myklebust 
622ea7c38feSTrond Myklebust 		iput(inode);
623ea7c38feSTrond Myklebust 		nfs_sb_deactive(sb);
624ea7c38feSTrond Myklebust 	}
625ea7c38feSTrond Myklebust }
626ea7c38feSTrond Myklebust 
627557134a3SAndy Adamson /*
628f7b422b1SDavid Howells  * Determine the device name as a string
629f7b422b1SDavid Howells  */
630b514f872SAl Viro static inline char *nfs_devname(struct dentry *dentry,
631f7b422b1SDavid Howells 				char *buffer, ssize_t buflen)
632f7b422b1SDavid Howells {
633b514f872SAl Viro 	char *dummy;
63497a54868SBen Hutchings 	return nfs_path(&dummy, dentry, buffer, buflen, NFS_PATH_CANONICAL);
635f7b422b1SDavid Howells }
636f7b422b1SDavid Howells 
637f7b422b1SDavid Howells /*
638f7b422b1SDavid Howells  * Determine the actual block size (and log2 thereof)
639f7b422b1SDavid Howells  */
640f7b422b1SDavid Howells static inline
641f7b422b1SDavid Howells unsigned long nfs_block_bits(unsigned long bsize, unsigned char *nrbitsp)
642f7b422b1SDavid Howells {
643f7b422b1SDavid Howells 	/* make sure blocksize is a power of two */
644f7b422b1SDavid Howells 	if ((bsize & (bsize - 1)) || nrbitsp) {
645f7b422b1SDavid Howells 		unsigned char	nrbits;
646f7b422b1SDavid Howells 
647f7b422b1SDavid Howells 		for (nrbits = 31; nrbits && !(bsize & (1 << nrbits)); nrbits--)
648f7b422b1SDavid Howells 			;
649f7b422b1SDavid Howells 		bsize = 1 << nrbits;
650f7b422b1SDavid Howells 		if (nrbitsp)
651f7b422b1SDavid Howells 			*nrbitsp = nrbits;
652f7b422b1SDavid Howells 	}
653f7b422b1SDavid Howells 
654f7b422b1SDavid Howells 	return bsize;
655f7b422b1SDavid Howells }
656f7b422b1SDavid Howells 
657f7b422b1SDavid Howells /*
658f7b422b1SDavid Howells  * Calculate the number of 512byte blocks used.
659f7b422b1SDavid Howells  */
6609eaa67c6SChuck Lever static inline blkcnt_t nfs_calc_block_size(u64 tsize)
661f7b422b1SDavid Howells {
6629eaa67c6SChuck Lever 	blkcnt_t used = (tsize + 511) >> 9;
663f7b422b1SDavid Howells 	return (used > ULONG_MAX) ? ULONG_MAX : used;
664f7b422b1SDavid Howells }
665f7b422b1SDavid Howells 
666f7b422b1SDavid Howells /*
667f7b422b1SDavid Howells  * Compute and set NFS server blocksize
668f7b422b1SDavid Howells  */
669f7b422b1SDavid Howells static inline
670f7b422b1SDavid Howells unsigned long nfs_block_size(unsigned long bsize, unsigned char *nrbitsp)
671f7b422b1SDavid Howells {
672f7b422b1SDavid Howells 	if (bsize < NFS_MIN_FILE_IO_SIZE)
673f7b422b1SDavid Howells 		bsize = NFS_DEF_FILE_IO_SIZE;
674f7b422b1SDavid Howells 	else if (bsize >= NFS_MAX_FILE_IO_SIZE)
675f7b422b1SDavid Howells 		bsize = NFS_MAX_FILE_IO_SIZE;
676f7b422b1SDavid Howells 
677f7b422b1SDavid Howells 	return nfs_block_bits(bsize, nrbitsp);
678f7b422b1SDavid Howells }
679f7b422b1SDavid Howells 
680f7b422b1SDavid Howells /*
681f7b422b1SDavid Howells  * Determine the maximum file size for a superblock
682f7b422b1SDavid Howells  */
683f7b422b1SDavid Howells static inline
684f7b422b1SDavid Howells void nfs_super_set_maxbytes(struct super_block *sb, __u64 maxfilesize)
685f7b422b1SDavid Howells {
686f7b422b1SDavid Howells 	sb->s_maxbytes = (loff_t)maxfilesize;
687f7b422b1SDavid Howells 	if (sb->s_maxbytes > MAX_LFS_FILESIZE || sb->s_maxbytes <= 0)
688f7b422b1SDavid Howells 		sb->s_maxbytes = MAX_LFS_FILESIZE;
689f7b422b1SDavid Howells }
69049a70f27STrond Myklebust 
69149a70f27STrond Myklebust /*
6928d92890bSNeilBrown  * Record the page as unstable (an extra writeback period) and mark its
6938d92890bSNeilBrown  * inode as dirty.
694d15bc38dSTom Haynes  */
695d15bc38dSTom Haynes static inline
69686d80f97STrond Myklebust void nfs_mark_page_unstable(struct page *page, struct nfs_commit_info *cinfo)
697d15bc38dSTom Haynes {
69886d80f97STrond Myklebust 	if (!cinfo->dreq) {
699d15bc38dSTom Haynes 		struct inode *inode = page_file_mapping(page)->host;
700d15bc38dSTom Haynes 
7018d92890bSNeilBrown 		/* This page is really still in write-back - just that the
7028d92890bSNeilBrown 		 * writeback is happening on the server now.
7038d92890bSNeilBrown 		 */
7048d92890bSNeilBrown 		inc_node_page_state(page, NR_WRITEBACK);
7058d92890bSNeilBrown 		inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
706d15bc38dSTom Haynes 		__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
707d15bc38dSTom Haynes 	}
70886d80f97STrond Myklebust }
709d15bc38dSTom Haynes 
710d15bc38dSTom Haynes /*
71149a70f27STrond Myklebust  * Determine the number of bytes of data the page contains
71249a70f27STrond Myklebust  */
71349a70f27STrond Myklebust static inline
71449a70f27STrond Myklebust unsigned int nfs_page_length(struct page *page)
71549a70f27STrond Myklebust {
716d56b4ddfSMel Gorman 	loff_t i_size = i_size_read(page_file_mapping(page)->host);
71749a70f27STrond Myklebust 
71849a70f27STrond Myklebust 	if (i_size > 0) {
7198cd79788SHuang Ying 		pgoff_t index = page_index(page);
72009cbfeafSKirill A. Shutemov 		pgoff_t end_index = (i_size - 1) >> PAGE_SHIFT;
7218cd79788SHuang Ying 		if (index < end_index)
72209cbfeafSKirill A. Shutemov 			return PAGE_SIZE;
7238cd79788SHuang Ying 		if (index == end_index)
72409cbfeafSKirill A. Shutemov 			return ((i_size - 1) & ~PAGE_MASK) + 1;
72549a70f27STrond Myklebust 	}
72649a70f27STrond Myklebust 	return 0;
72749a70f27STrond Myklebust }
7288d5658c9STrond Myklebust 
7298d5658c9STrond Myklebust /*
7300b26a0bfSTrond Myklebust  * Convert a umode to a dirent->d_type
7310b26a0bfSTrond Myklebust  */
7320b26a0bfSTrond Myklebust static inline
7330b26a0bfSTrond Myklebust unsigned char nfs_umode_to_dtype(umode_t mode)
7340b26a0bfSTrond Myklebust {
7350b26a0bfSTrond Myklebust 	return (mode >> 12) & 15;
7360b26a0bfSTrond Myklebust }
7370b26a0bfSTrond Myklebust 
7380b26a0bfSTrond Myklebust /*
7398d5658c9STrond Myklebust  * Determine the number of pages in an array of length 'len' and
7408d5658c9STrond Myklebust  * with a base offset of 'base'
7418d5658c9STrond Myklebust  */
7428d5658c9STrond Myklebust static inline
7438d5658c9STrond Myklebust unsigned int nfs_page_array_len(unsigned int base, size_t len)
7448d5658c9STrond Myklebust {
7458d5658c9STrond Myklebust 	return ((unsigned long)len + (unsigned long)base +
7468d5658c9STrond Myklebust 		PAGE_SIZE - 1) >> PAGE_SHIFT;
7478d5658c9STrond Myklebust }
7480110ee15STrond Myklebust 
7493a1556e8STrond Myklebust /*
750ae08483cSArnd Bergmann  * Convert a struct timespec64 into a 64-bit change attribute
7513a1556e8STrond Myklebust  *
752ae08483cSArnd Bergmann  * This does approximately the same thing as timespec64_to_ns(),
7533a1556e8STrond Myklebust  * but for calculation efficiency, we multiply the seconds by
7543a1556e8STrond Myklebust  * 1024*1024*1024.
7553a1556e8STrond Myklebust  */
7563a1556e8STrond Myklebust static inline
757e86d5a02STrond Myklebust u64 nfs_timespec_to_change_attr(const struct timespec64 *ts)
7583a1556e8STrond Myklebust {
7593a1556e8STrond Myklebust 	return ((u64)ts->tv_sec << 30) + ts->tv_nsec;
7603a1556e8STrond Myklebust }
7611264a2f0STrond Myklebust 
7621264a2f0STrond Myklebust #ifdef CONFIG_CRC32
7631264a2f0STrond Myklebust /**
7641264a2f0STrond Myklebust  * nfs_fhandle_hash - calculate the crc32 hash for the filehandle
7651264a2f0STrond Myklebust  * @fh - pointer to filehandle
7661264a2f0STrond Myklebust  *
7671264a2f0STrond Myklebust  * returns a crc32 hash for the filehandle that is compatible with
7681264a2f0STrond Myklebust  * the one displayed by "wireshark".
7691264a2f0STrond Myklebust  */
7701264a2f0STrond Myklebust static inline u32 nfs_fhandle_hash(const struct nfs_fh *fh)
7711264a2f0STrond Myklebust {
7721264a2f0STrond Myklebust 	return ~crc32_le(0xFFFFFFFF, &fh->data[0], fh->size);
7731264a2f0STrond Myklebust }
77448c9579aSOlga Kornievskaia static inline u32 nfs_stateid_hash(const nfs4_stateid *stateid)
77548c9579aSOlga Kornievskaia {
77648c9579aSOlga Kornievskaia 	return ~crc32_le(0xFFFFFFFF, &stateid->other[0],
77748c9579aSOlga Kornievskaia 				NFS4_STATEID_OTHER_SIZE);
77848c9579aSOlga Kornievskaia }
7791264a2f0STrond Myklebust #else
7801264a2f0STrond Myklebust static inline u32 nfs_fhandle_hash(const struct nfs_fh *fh)
7811264a2f0STrond Myklebust {
7821264a2f0STrond Myklebust 	return 0;
7831264a2f0STrond Myklebust }
78448c9579aSOlga Kornievskaia static inline u32 nfs_stateid_hash(nfs4_stateid *stateid)
78548c9579aSOlga Kornievskaia {
78648c9579aSOlga Kornievskaia 	return 0;
78748c9579aSOlga Kornievskaia }
7881264a2f0STrond Myklebust #endif
7890bcbf039SPeng Tao 
7900bcbf039SPeng Tao static inline bool nfs_error_is_fatal(int err)
7910bcbf039SPeng Tao {
7920bcbf039SPeng Tao 	switch (err) {
7930bcbf039SPeng Tao 	case -ERESTARTSYS:
7947dc58ca5STrond Myklebust 	case -EINTR:
79554551d85STrond Myklebust 	case -EACCES:
79654551d85STrond Myklebust 	case -EDQUOT:
79754551d85STrond Myklebust 	case -EFBIG:
7980bcbf039SPeng Tao 	case -EIO:
7990bcbf039SPeng Tao 	case -ENOSPC:
8000bcbf039SPeng Tao 	case -EROFS:
80154551d85STrond Myklebust 	case -ESTALE:
8020bcbf039SPeng Tao 	case -E2BIG:
8032dc23affSTrond Myklebust 	case -ENOMEM:
80411982a7cSTrond Myklebust 	case -ETIMEDOUT:
8050bcbf039SPeng Tao 		return true;
8060bcbf039SPeng Tao 	default:
8070bcbf039SPeng Tao 		return false;
8080bcbf039SPeng Tao 	}
8090bcbf039SPeng Tao }
810bf4b4905SNeilBrown 
8118f54c7a4STrond Myklebust static inline bool nfs_error_is_fatal_on_server(int err)
8128f54c7a4STrond Myklebust {
8138f54c7a4STrond Myklebust 	switch (err) {
8148f54c7a4STrond Myklebust 	case 0:
8158f54c7a4STrond Myklebust 	case -ERESTARTSYS:
8168f54c7a4STrond Myklebust 	case -EINTR:
8178f54c7a4STrond Myklebust 		return false;
8188f54c7a4STrond Myklebust 	}
8198f54c7a4STrond Myklebust 	return nfs_error_is_fatal(err);
8208f54c7a4STrond Myklebust }
8219954bf92SDavid Howells 
8229954bf92SDavid Howells /*
8239954bf92SDavid Howells  * Select between a default port value and a user-specified port value.
8249954bf92SDavid Howells  * If a zero value is set, then autobind will be used.
8259954bf92SDavid Howells  */
8269954bf92SDavid Howells static inline void nfs_set_port(struct sockaddr *sap, int *port,
8279954bf92SDavid Howells 				const unsigned short default_port)
8289954bf92SDavid Howells {
8299954bf92SDavid Howells 	if (*port == NFS_UNSPEC_PORT)
8309954bf92SDavid Howells 		*port = default_port;
8319954bf92SDavid Howells 
8329954bf92SDavid Howells 	rpc_set_port(sap, *port);
8339954bf92SDavid Howells }
834