17f082442804a0bbf19da2e5a7d7c0ff711d2400
[sfrench/cifs-2.6.git] / fs / nfs / internal.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * NFS internal definitions
4  */
5
6 #include "nfs4_fs.h"
7 #include <linux/fs_context.h>
8 #include <linux/security.h>
9 #include <linux/crc32.h>
10 #include <linux/sunrpc/addr.h>
11 #include <linux/nfs_page.h>
12 #include <linux/wait_bit.h>
13
14 #define NFS_SB_MASK (SB_RDONLY|SB_NOSUID|SB_NODEV|SB_NOEXEC|SB_SYNCHRONOUS)
15
16 extern const struct export_operations nfs_export_ops;
17
18 struct nfs_string;
19 struct nfs_pageio_descriptor;
20
21 static inline void nfs_attr_check_mountpoint(struct super_block *parent, struct nfs_fattr *fattr)
22 {
23         if (!nfs_fsid_equal(&NFS_SB(parent)->fsid, &fattr->fsid))
24                 fattr->valid |= NFS_ATTR_FATTR_MOUNTPOINT;
25 }
26
27 static inline int nfs_attr_use_mounted_on_fileid(struct nfs_fattr *fattr)
28 {
29         if (((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) == 0) ||
30             (((fattr->valid & NFS_ATTR_FATTR_MOUNTPOINT) == 0) &&
31              ((fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) == 0)))
32                 return 0;
33         return 1;
34 }
35
36 /*
37  * Note: RFC 1813 doesn't limit the number of auth flavors that
38  * a server can return, so make something up.
39  */
40 #define NFS_MAX_SECFLAVORS      (12)
41
42 /*
43  * Value used if the user did not specify a port value.
44  */
45 #define NFS_UNSPEC_PORT         (-1)
46
47 #define NFS_UNSPEC_RETRANS      (UINT_MAX)
48 #define NFS_UNSPEC_TIMEO        (UINT_MAX)
49
50 /*
51  * Maximum number of pages that readdir can use for creating
52  * a vmapped array of pages.
53  */
54 #define NFS_MAX_READDIR_PAGES 8
55
56 struct nfs_client_initdata {
57         unsigned long init_flags;
58         const char *hostname;                   /* Hostname of the server */
59         const struct sockaddr *addr;            /* Address of the server */
60         const char *nodename;                   /* Hostname of the client */
61         const char *ip_addr;                    /* IP address of the client */
62         size_t addrlen;
63         struct nfs_subversion *nfs_mod;
64         int proto;
65         u32 minorversion;
66         unsigned int nconnect;
67         struct net *net;
68         const struct rpc_timeout *timeparms;
69         const struct cred *cred;
70 };
71
72 /*
73  * In-kernel mount arguments
74  */
75 struct nfs_fs_context {
76         bool                    internal;
77         bool                    skip_reconfig_option_check;
78         bool                    need_mount;
79         bool                    sloppy;
80         unsigned int            flags;          /* NFS{,4}_MOUNT_* flags */
81         unsigned int            rsize, wsize;
82         unsigned int            timeo, retrans;
83         unsigned int            acregmin, acregmax;
84         unsigned int            acdirmin, acdirmax;
85         unsigned int            namlen;
86         unsigned int            options;
87         unsigned int            bsize;
88         struct nfs_auth_info    auth_info;
89         rpc_authflavor_t        selected_flavor;
90         char                    *client_address;
91         unsigned int            version;
92         unsigned int            minorversion;
93         char                    *fscache_uniq;
94         unsigned short          protofamily;
95         unsigned short          mountfamily;
96
97         struct {
98                 union {
99                         struct sockaddr address;
100                         struct sockaddr_storage _address;
101                 };
102                 size_t                  addrlen;
103                 char                    *hostname;
104                 u32                     version;
105                 int                     port;
106                 unsigned short          protocol;
107         } mount_server;
108
109         struct {
110                 union {
111                         struct sockaddr address;
112                         struct sockaddr_storage _address;
113                 };
114                 size_t                  addrlen;
115                 char                    *hostname;
116                 char                    *export_path;
117                 int                     port;
118                 unsigned short          protocol;
119                 unsigned short          nconnect;
120                 unsigned short          export_path_len;
121         } nfs_server;
122
123         struct nfs_fh           *mntfh;
124         struct nfs_server       *server;
125         struct nfs_subversion   *nfs_mod;
126
127         /* Information for a cloned mount. */
128         struct nfs_clone_mount {
129                 struct super_block      *sb;
130                 struct dentry           *dentry;
131                 struct nfs_fattr        *fattr;
132                 unsigned int            inherited_bsize;
133         } clone_data;
134 };
135
136 #define nfs_errorf(fc, fmt, ...) errorf(fc, fmt, ## __VA_ARGS__)
137 #define nfs_invalf(fc, fmt, ...) invalf(fc, fmt, ## __VA_ARGS__)
138 #define nfs_warnf(fc, fmt, ...) warnf(fc, fmt, ## __VA_ARGS__)
139
140 static inline struct nfs_fs_context *nfs_fc2context(const struct fs_context *fc)
141 {
142         return fc->fs_private;
143 }
144
145 /* mount_clnt.c */
146 struct nfs_mount_request {
147         struct sockaddr         *sap;
148         size_t                  salen;
149         char                    *hostname;
150         char                    *dirpath;
151         u32                     version;
152         unsigned short          protocol;
153         struct nfs_fh           *fh;
154         int                     noresvport;
155         unsigned int            *auth_flav_len;
156         rpc_authflavor_t        *auth_flavs;
157         struct net              *net;
158 };
159
160 extern int nfs_mount(struct nfs_mount_request *info);
161 extern void nfs_umount(const struct nfs_mount_request *info);
162
163 /* client.c */
164 extern const struct rpc_program nfs_program;
165 extern void nfs_clients_init(struct net *net);
166 extern void nfs_clients_exit(struct net *net);
167 extern struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *);
168 int nfs_create_rpc_client(struct nfs_client *, const struct nfs_client_initdata *, rpc_authflavor_t);
169 struct nfs_client *nfs_get_client(const struct nfs_client_initdata *);
170 int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *, struct nfs_fattr *);
171 void nfs_server_insert_lists(struct nfs_server *);
172 void nfs_server_remove_lists(struct nfs_server *);
173 void nfs_init_timeout_values(struct rpc_timeout *to, int proto, int timeo, int retrans);
174 int nfs_init_server_rpcclient(struct nfs_server *, const struct rpc_timeout *t,
175                 rpc_authflavor_t);
176 struct nfs_server *nfs_alloc_server(void);
177 void nfs_server_copy_userdata(struct nfs_server *, struct nfs_server *);
178
179 extern void nfs_put_client(struct nfs_client *);
180 extern void nfs_free_client(struct nfs_client *);
181 extern struct nfs_client *nfs4_find_client_ident(struct net *, int);
182 extern struct nfs_client *
183 nfs4_find_client_sessionid(struct net *, const struct sockaddr *,
184                                 struct nfs4_sessionid *, u32);
185 extern struct nfs_server *nfs_create_server(struct fs_context *);
186 extern struct nfs_server *nfs4_create_server(struct fs_context *);
187 extern struct nfs_server *nfs4_create_referral_server(struct fs_context *);
188 extern int nfs4_update_server(struct nfs_server *server, const char *hostname,
189                                         struct sockaddr *sap, size_t salen,
190                                         struct net *net);
191 extern void nfs_free_server(struct nfs_server *server);
192 extern struct nfs_server *nfs_clone_server(struct nfs_server *,
193                                            struct nfs_fh *,
194                                            struct nfs_fattr *,
195                                            rpc_authflavor_t);
196 extern bool nfs_client_init_is_complete(const struct nfs_client *clp);
197 extern int nfs_client_init_status(const struct nfs_client *clp);
198 extern int nfs_wait_client_init_complete(const struct nfs_client *clp);
199 extern void nfs_mark_client_ready(struct nfs_client *clp, int state);
200 extern struct nfs_client *nfs4_set_ds_client(struct nfs_server *mds_srv,
201                                              const struct sockaddr *ds_addr,
202                                              int ds_addrlen, int ds_proto,
203                                              unsigned int ds_timeo,
204                                              unsigned int ds_retrans,
205                                              u32 minor_version);
206 extern struct rpc_clnt *nfs4_find_or_create_ds_client(struct nfs_client *,
207                                                 struct inode *);
208 extern struct nfs_client *nfs3_set_ds_client(struct nfs_server *mds_srv,
209                         const struct sockaddr *ds_addr, int ds_addrlen,
210                         int ds_proto, unsigned int ds_timeo,
211                         unsigned int ds_retrans);
212 #ifdef CONFIG_PROC_FS
213 extern int __init nfs_fs_proc_init(void);
214 extern void nfs_fs_proc_exit(void);
215 extern int nfs_fs_proc_net_init(struct net *net);
216 extern void nfs_fs_proc_net_exit(struct net *net);
217 #else
218 static inline int nfs_fs_proc_net_init(struct net *net)
219 {
220         return 0;
221 }
222 static inline void nfs_fs_proc_net_exit(struct net *net)
223 {
224 }
225 static inline int nfs_fs_proc_init(void)
226 {
227         return 0;
228 }
229 static inline void nfs_fs_proc_exit(void)
230 {
231 }
232 #endif
233
234 /* callback_xdr.c */
235 extern const struct svc_version nfs4_callback_version1;
236 extern const struct svc_version nfs4_callback_version4;
237
238 /* fs_context.c */
239 extern struct file_system_type nfs_fs_type;
240
241 /* pagelist.c */
242 extern int __init nfs_init_nfspagecache(void);
243 extern void nfs_destroy_nfspagecache(void);
244 extern int __init nfs_init_readpagecache(void);
245 extern void nfs_destroy_readpagecache(void);
246 extern int __init nfs_init_writepagecache(void);
247 extern void nfs_destroy_writepagecache(void);
248
249 extern int __init nfs_init_directcache(void);
250 extern void nfs_destroy_directcache(void);
251 extern void nfs_pgheader_init(struct nfs_pageio_descriptor *desc,
252                               struct nfs_pgio_header *hdr,
253                               void (*release)(struct nfs_pgio_header *hdr));
254 void nfs_set_pgio_error(struct nfs_pgio_header *hdr, int error, loff_t pos);
255 int nfs_iocounter_wait(struct nfs_lock_context *l_ctx);
256
257 extern const struct nfs_pageio_ops nfs_pgio_rw_ops;
258 struct nfs_pgio_header *nfs_pgio_header_alloc(const struct nfs_rw_ops *);
259 void nfs_pgio_header_free(struct nfs_pgio_header *);
260 int nfs_generic_pgio(struct nfs_pageio_descriptor *, struct nfs_pgio_header *);
261 int nfs_initiate_pgio(struct rpc_clnt *clnt, struct nfs_pgio_header *hdr,
262                       const struct cred *cred, const struct nfs_rpc_ops *rpc_ops,
263                       const struct rpc_call_ops *call_ops, int how, int flags);
264 void nfs_free_request(struct nfs_page *req);
265 struct nfs_pgio_mirror *
266 nfs_pgio_current_mirror(struct nfs_pageio_descriptor *desc);
267
268 static inline bool nfs_pgio_has_mirroring(struct nfs_pageio_descriptor *desc)
269 {
270         WARN_ON_ONCE(desc->pg_mirror_count < 1);
271         return desc->pg_mirror_count > 1;
272 }
273
274 static inline bool nfs_match_open_context(const struct nfs_open_context *ctx1,
275                 const struct nfs_open_context *ctx2)
276 {
277         return cred_fscmp(ctx1->cred, ctx2->cred) == 0 && ctx1->state == ctx2->state;
278 }
279
280 /* nfs2xdr.c */
281 extern const struct rpc_procinfo nfs_procedures[];
282 extern int nfs2_decode_dirent(struct xdr_stream *,
283                                 struct nfs_entry *, bool);
284
285 /* nfs3xdr.c */
286 extern const struct rpc_procinfo nfs3_procedures[];
287 extern int nfs3_decode_dirent(struct xdr_stream *,
288                                 struct nfs_entry *, bool);
289
290 /* nfs4xdr.c */
291 #if IS_ENABLED(CONFIG_NFS_V4)
292 extern int nfs4_decode_dirent(struct xdr_stream *,
293                                 struct nfs_entry *, bool);
294 #endif
295 #ifdef CONFIG_NFS_V4_1
296 extern const u32 nfs41_maxread_overhead;
297 extern const u32 nfs41_maxwrite_overhead;
298 extern const u32 nfs41_maxgetdevinfo_overhead;
299 #endif
300
301 /* nfs4proc.c */
302 #if IS_ENABLED(CONFIG_NFS_V4)
303 extern const struct rpc_procinfo nfs4_procedures[];
304 #endif
305
306 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
307 extern struct nfs4_label *nfs4_label_alloc(struct nfs_server *server, gfp_t flags);
308 static inline struct nfs4_label *
309 nfs4_label_copy(struct nfs4_label *dst, struct nfs4_label *src)
310 {
311         if (!dst || !src)
312                 return NULL;
313
314         if (src->len > NFS4_MAXLABELLEN)
315                 return NULL;
316
317         dst->lfs = src->lfs;
318         dst->pi = src->pi;
319         dst->len = src->len;
320         memcpy(dst->label, src->label, src->len);
321
322         return dst;
323 }
324 static inline void nfs4_label_free(struct nfs4_label *label)
325 {
326         if (label) {
327                 kfree(label->label);
328                 kfree(label);
329         }
330         return;
331 }
332
333 static inline void nfs_zap_label_cache_locked(struct nfs_inode *nfsi)
334 {
335         if (nfs_server_capable(&nfsi->vfs_inode, NFS_CAP_SECURITY_LABEL))
336                 nfsi->cache_validity |= NFS_INO_INVALID_LABEL;
337 }
338 #else
339 static inline struct nfs4_label *nfs4_label_alloc(struct nfs_server *server, gfp_t flags) { return NULL; }
340 static inline void nfs4_label_free(void *label) {}
341 static inline void nfs_zap_label_cache_locked(struct nfs_inode *nfsi)
342 {
343 }
344 static inline struct nfs4_label *
345 nfs4_label_copy(struct nfs4_label *dst, struct nfs4_label *src)
346 {
347         return NULL;
348 }
349 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
350
351 /* proc.c */
352 void nfs_close_context(struct nfs_open_context *ctx, int is_sync);
353 extern struct nfs_client *nfs_init_client(struct nfs_client *clp,
354                            const struct nfs_client_initdata *);
355
356 /* dir.c */
357 extern void nfs_advise_use_readdirplus(struct inode *dir);
358 extern void nfs_force_use_readdirplus(struct inode *dir);
359 extern unsigned long nfs_access_cache_count(struct shrinker *shrink,
360                                             struct shrink_control *sc);
361 extern unsigned long nfs_access_cache_scan(struct shrinker *shrink,
362                                            struct shrink_control *sc);
363 struct dentry *nfs_lookup(struct inode *, struct dentry *, unsigned int);
364 int nfs_create(struct inode *, struct dentry *, umode_t, bool);
365 int nfs_mkdir(struct inode *, struct dentry *, umode_t);
366 int nfs_rmdir(struct inode *, struct dentry *);
367 int nfs_unlink(struct inode *, struct dentry *);
368 int nfs_symlink(struct inode *, struct dentry *, const char *);
369 int nfs_link(struct dentry *, struct inode *, struct dentry *);
370 int nfs_mknod(struct inode *, struct dentry *, umode_t, dev_t);
371 int nfs_rename(struct inode *, struct dentry *,
372                struct inode *, struct dentry *, unsigned int);
373
374 /* file.c */
375 int nfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync);
376 loff_t nfs_file_llseek(struct file *, loff_t, int);
377 ssize_t nfs_file_read(struct kiocb *, struct iov_iter *);
378 int nfs_file_mmap(struct file *, struct vm_area_struct *);
379 ssize_t nfs_file_write(struct kiocb *, struct iov_iter *);
380 int nfs_file_release(struct inode *, struct file *);
381 int nfs_lock(struct file *, int, struct file_lock *);
382 int nfs_flock(struct file *, int, struct file_lock *);
383 int nfs_check_flags(int);
384
385 /* inode.c */
386 extern struct workqueue_struct *nfsiod_workqueue;
387 extern struct inode *nfs_alloc_inode(struct super_block *sb);
388 extern void nfs_free_inode(struct inode *);
389 extern int nfs_write_inode(struct inode *, struct writeback_control *);
390 extern int nfs_drop_inode(struct inode *);
391 extern void nfs_clear_inode(struct inode *);
392 extern void nfs_evict_inode(struct inode *);
393 void nfs_zap_acl_cache(struct inode *inode);
394 extern bool nfs_check_cache_invalid(struct inode *, unsigned long);
395 extern int nfs_wait_bit_killable(struct wait_bit_key *key, int mode);
396 extern int nfs_wait_atomic_killable(atomic_t *p, unsigned int mode);
397
398 /* super.c */
399 extern const struct super_operations nfs_sops;
400 bool nfs_auth_info_match(const struct nfs_auth_info *, rpc_authflavor_t);
401 int nfs_try_get_tree(struct fs_context *);
402 int nfs_get_tree_common(struct fs_context *);
403 void nfs_kill_super(struct super_block *);
404
405 extern struct rpc_stat nfs_rpcstat;
406
407 extern int __init register_nfs_fs(void);
408 extern void __exit unregister_nfs_fs(void);
409 extern bool nfs_sb_active(struct super_block *sb);
410 extern void nfs_sb_deactive(struct super_block *sb);
411
412 /* io.c */
413 extern void nfs_start_io_read(struct inode *inode);
414 extern void nfs_end_io_read(struct inode *inode);
415 extern void nfs_start_io_write(struct inode *inode);
416 extern void nfs_end_io_write(struct inode *inode);
417 extern void nfs_start_io_direct(struct inode *inode);
418 extern void nfs_end_io_direct(struct inode *inode);
419
420 static inline bool nfs_file_io_is_buffered(struct nfs_inode *nfsi)
421 {
422         return test_bit(NFS_INO_ODIRECT, &nfsi->flags) == 0;
423 }
424
425 /* namespace.c */
426 #define NFS_PATH_CANONICAL 1
427 extern char *nfs_path(char **p, struct dentry *dentry,
428                       char *buffer, ssize_t buflen, unsigned flags);
429 extern struct vfsmount *nfs_d_automount(struct path *path);
430 int nfs_submount(struct fs_context *, struct nfs_server *);
431 int nfs_do_submount(struct fs_context *);
432
433 /* getroot.c */
434 extern int nfs_get_root(struct super_block *s, struct fs_context *fc);
435 #if IS_ENABLED(CONFIG_NFS_V4)
436 extern int nfs4_get_rootfh(struct nfs_server *server, struct nfs_fh *mntfh, bool);
437 #endif
438
439 struct nfs_pgio_completion_ops;
440 /* read.c */
441 extern void nfs_pageio_init_read(struct nfs_pageio_descriptor *pgio,
442                         struct inode *inode, bool force_mds,
443                         const struct nfs_pgio_completion_ops *compl_ops);
444 extern void nfs_read_prepare(struct rpc_task *task, void *calldata);
445 extern void nfs_pageio_reset_read_mds(struct nfs_pageio_descriptor *pgio);
446
447 /* super.c */
448 void nfs_umount_begin(struct super_block *);
449 int  nfs_statfs(struct dentry *, struct kstatfs *);
450 int  nfs_show_options(struct seq_file *, struct dentry *);
451 int  nfs_show_devname(struct seq_file *, struct dentry *);
452 int  nfs_show_path(struct seq_file *, struct dentry *);
453 int  nfs_show_stats(struct seq_file *, struct dentry *);
454 int  nfs_reconfigure(struct fs_context *);
455
456 /* write.c */
457 extern void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
458                         struct inode *inode, int ioflags, bool force_mds,
459                         const struct nfs_pgio_completion_ops *compl_ops);
460 extern void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio);
461 extern void nfs_commit_free(struct nfs_commit_data *p);
462 extern void nfs_write_prepare(struct rpc_task *task, void *calldata);
463 extern void nfs_commit_prepare(struct rpc_task *task, void *calldata);
464 extern int nfs_initiate_commit(struct rpc_clnt *clnt,
465                                struct nfs_commit_data *data,
466                                const struct nfs_rpc_ops *nfs_ops,
467                                const struct rpc_call_ops *call_ops,
468                                int how, int flags);
469 extern void nfs_init_commit(struct nfs_commit_data *data,
470                             struct list_head *head,
471                             struct pnfs_layout_segment *lseg,
472                             struct nfs_commit_info *cinfo);
473 int nfs_scan_commit_list(struct list_head *src, struct list_head *dst,
474                          struct nfs_commit_info *cinfo, int max);
475 unsigned long nfs_reqs_to_commit(struct nfs_commit_info *);
476 int nfs_scan_commit(struct inode *inode, struct list_head *dst,
477                     struct nfs_commit_info *cinfo);
478 void nfs_mark_request_commit(struct nfs_page *req,
479                              struct pnfs_layout_segment *lseg,
480                              struct nfs_commit_info *cinfo,
481                              u32 ds_commit_idx);
482 int nfs_write_need_commit(struct nfs_pgio_header *);
483 void nfs_writeback_update_inode(struct nfs_pgio_header *hdr);
484 int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
485                             int how, struct nfs_commit_info *cinfo);
486 void nfs_retry_commit(struct list_head *page_list,
487                       struct pnfs_layout_segment *lseg,
488                       struct nfs_commit_info *cinfo,
489                       u32 ds_commit_idx);
490 void nfs_commitdata_release(struct nfs_commit_data *data);
491 void nfs_request_add_commit_list(struct nfs_page *req,
492                                  struct nfs_commit_info *cinfo);
493 void nfs_request_add_commit_list_locked(struct nfs_page *req,
494                 struct list_head *dst,
495                 struct nfs_commit_info *cinfo);
496 void nfs_request_remove_commit_list(struct nfs_page *req,
497                                     struct nfs_commit_info *cinfo);
498 void nfs_init_cinfo(struct nfs_commit_info *cinfo,
499                     struct inode *inode,
500                     struct nfs_direct_req *dreq);
501 int nfs_key_timeout_notify(struct file *filp, struct inode *inode);
502 bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx, struct inode *inode);
503 void nfs_pageio_stop_mirroring(struct nfs_pageio_descriptor *pgio);
504
505 int nfs_filemap_write_and_wait_range(struct address_space *mapping,
506                 loff_t lstart, loff_t lend);
507
508 #ifdef CONFIG_NFS_V4_1
509 static inline
510 void nfs_clear_pnfs_ds_commit_verifiers(struct pnfs_ds_commit_info *cinfo)
511 {
512         int i;
513
514         for (i = 0; i < cinfo->nbuckets; i++)
515                 cinfo->buckets[i].direct_verf.committed = NFS_INVALID_STABLE_HOW;
516 }
517 #else
518 static inline
519 void nfs_clear_pnfs_ds_commit_verifiers(struct pnfs_ds_commit_info *cinfo)
520 {
521 }
522 #endif
523
524 #ifdef CONFIG_MIGRATION
525 extern int nfs_migrate_page(struct address_space *,
526                 struct page *, struct page *, enum migrate_mode);
527 #endif
528
529 static inline int
530 nfs_write_verifier_cmp(const struct nfs_write_verifier *v1,
531                 const struct nfs_write_verifier *v2)
532 {
533         return memcmp(v1->data, v2->data, sizeof(v1->data));
534 }
535
536 /* unlink.c */
537 extern struct rpc_task *
538 nfs_async_rename(struct inode *old_dir, struct inode *new_dir,
539                  struct dentry *old_dentry, struct dentry *new_dentry,
540                  void (*complete)(struct rpc_task *, struct nfs_renamedata *));
541 extern int nfs_sillyrename(struct inode *dir, struct dentry *dentry);
542
543 /* direct.c */
544 void nfs_init_cinfo_from_dreq(struct nfs_commit_info *cinfo,
545                               struct nfs_direct_req *dreq);
546 extern ssize_t nfs_dreq_bytes_left(struct nfs_direct_req *dreq);
547
548 /* nfs4proc.c */
549 extern struct nfs_client *nfs4_init_client(struct nfs_client *clp,
550                             const struct nfs_client_initdata *);
551 extern int nfs40_walk_client_list(struct nfs_client *clp,
552                                 struct nfs_client **result,
553                                 const struct cred *cred);
554 extern int nfs41_walk_client_list(struct nfs_client *clp,
555                                 struct nfs_client **result,
556                                 const struct cred *cred);
557 extern void nfs4_test_session_trunk(struct rpc_clnt *clnt,
558                                 struct rpc_xprt *xprt,
559                                 void *data);
560
561 static inline struct inode *nfs_igrab_and_active(struct inode *inode)
562 {
563         inode = igrab(inode);
564         if (inode != NULL && !nfs_sb_active(inode->i_sb)) {
565                 iput(inode);
566                 inode = NULL;
567         }
568         return inode;
569 }
570
571 static inline void nfs_iput_and_deactive(struct inode *inode)
572 {
573         if (inode != NULL) {
574                 struct super_block *sb = inode->i_sb;
575
576                 iput(inode);
577                 nfs_sb_deactive(sb);
578         }
579 }
580
581 /*
582  * Determine the device name as a string
583  */
584 static inline char *nfs_devname(struct dentry *dentry,
585                                 char *buffer, ssize_t buflen)
586 {
587         char *dummy;
588         return nfs_path(&dummy, dentry, buffer, buflen, NFS_PATH_CANONICAL);
589 }
590
591 /*
592  * Determine the actual block size (and log2 thereof)
593  */
594 static inline
595 unsigned long nfs_block_bits(unsigned long bsize, unsigned char *nrbitsp)
596 {
597         /* make sure blocksize is a power of two */
598         if ((bsize & (bsize - 1)) || nrbitsp) {
599                 unsigned char   nrbits;
600
601                 for (nrbits = 31; nrbits && !(bsize & (1 << nrbits)); nrbits--)
602                         ;
603                 bsize = 1 << nrbits;
604                 if (nrbitsp)
605                         *nrbitsp = nrbits;
606         }
607
608         return bsize;
609 }
610
611 /*
612  * Calculate the number of 512byte blocks used.
613  */
614 static inline blkcnt_t nfs_calc_block_size(u64 tsize)
615 {
616         blkcnt_t used = (tsize + 511) >> 9;
617         return (used > ULONG_MAX) ? ULONG_MAX : used;
618 }
619
620 /*
621  * Compute and set NFS server blocksize
622  */
623 static inline
624 unsigned long nfs_block_size(unsigned long bsize, unsigned char *nrbitsp)
625 {
626         if (bsize < NFS_MIN_FILE_IO_SIZE)
627                 bsize = NFS_DEF_FILE_IO_SIZE;
628         else if (bsize >= NFS_MAX_FILE_IO_SIZE)
629                 bsize = NFS_MAX_FILE_IO_SIZE;
630
631         return nfs_block_bits(bsize, nrbitsp);
632 }
633
634 /*
635  * Determine the maximum file size for a superblock
636  */
637 static inline
638 void nfs_super_set_maxbytes(struct super_block *sb, __u64 maxfilesize)
639 {
640         sb->s_maxbytes = (loff_t)maxfilesize;
641         if (sb->s_maxbytes > MAX_LFS_FILESIZE || sb->s_maxbytes <= 0)
642                 sb->s_maxbytes = MAX_LFS_FILESIZE;
643 }
644
645 /*
646  * Record the page as unstable and mark its inode as dirty.
647  */
648 static inline
649 void nfs_mark_page_unstable(struct page *page, struct nfs_commit_info *cinfo)
650 {
651         if (!cinfo->dreq) {
652                 struct inode *inode = page_file_mapping(page)->host;
653
654                 inc_node_page_state(page, NR_UNSTABLE_NFS);
655                 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_RECLAIMABLE);
656                 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
657         }
658 }
659
660 /*
661  * Determine the number of bytes of data the page contains
662  */
663 static inline
664 unsigned int nfs_page_length(struct page *page)
665 {
666         loff_t i_size = i_size_read(page_file_mapping(page)->host);
667
668         if (i_size > 0) {
669                 pgoff_t index = page_index(page);
670                 pgoff_t end_index = (i_size - 1) >> PAGE_SHIFT;
671                 if (index < end_index)
672                         return PAGE_SIZE;
673                 if (index == end_index)
674                         return ((i_size - 1) & ~PAGE_MASK) + 1;
675         }
676         return 0;
677 }
678
679 /*
680  * Convert a umode to a dirent->d_type
681  */
682 static inline
683 unsigned char nfs_umode_to_dtype(umode_t mode)
684 {
685         return (mode >> 12) & 15;
686 }
687
688 /*
689  * Determine the number of pages in an array of length 'len' and
690  * with a base offset of 'base'
691  */
692 static inline
693 unsigned int nfs_page_array_len(unsigned int base, size_t len)
694 {
695         return ((unsigned long)len + (unsigned long)base +
696                 PAGE_SIZE - 1) >> PAGE_SHIFT;
697 }
698
699 /*
700  * Convert a struct timespec64 into a 64-bit change attribute
701  *
702  * This does approximately the same thing as timespec64_to_ns(),
703  * but for calculation efficiency, we multiply the seconds by
704  * 1024*1024*1024.
705  */
706 static inline
707 u64 nfs_timespec_to_change_attr(const struct timespec64 *ts)
708 {
709         return ((u64)ts->tv_sec << 30) + ts->tv_nsec;
710 }
711
712 #ifdef CONFIG_CRC32
713 /**
714  * nfs_fhandle_hash - calculate the crc32 hash for the filehandle
715  * @fh - pointer to filehandle
716  *
717  * returns a crc32 hash for the filehandle that is compatible with
718  * the one displayed by "wireshark".
719  */
720 static inline u32 nfs_fhandle_hash(const struct nfs_fh *fh)
721 {
722         return ~crc32_le(0xFFFFFFFF, &fh->data[0], fh->size);
723 }
724 static inline u32 nfs_stateid_hash(const nfs4_stateid *stateid)
725 {
726         return ~crc32_le(0xFFFFFFFF, &stateid->other[0],
727                                 NFS4_STATEID_OTHER_SIZE);
728 }
729 #else
730 static inline u32 nfs_fhandle_hash(const struct nfs_fh *fh)
731 {
732         return 0;
733 }
734 static inline u32 nfs_stateid_hash(nfs4_stateid *stateid)
735 {
736         return 0;
737 }
738 #endif
739
740 static inline bool nfs_error_is_fatal(int err)
741 {
742         switch (err) {
743         case -ERESTARTSYS:
744         case -EINTR:
745         case -EACCES:
746         case -EDQUOT:
747         case -EFBIG:
748         case -EIO:
749         case -ENOSPC:
750         case -EROFS:
751         case -ESTALE:
752         case -E2BIG:
753         case -ENOMEM:
754         case -ETIMEDOUT:
755                 return true;
756         default:
757                 return false;
758         }
759 }
760
761 static inline bool nfs_error_is_fatal_on_server(int err)
762 {
763         switch (err) {
764         case 0:
765         case -ERESTARTSYS:
766         case -EINTR:
767                 return false;
768         }
769         return nfs_error_is_fatal(err);
770 }
771
772 /*
773  * Select between a default port value and a user-specified port value.
774  * If a zero value is set, then autobind will be used.
775  */
776 static inline void nfs_set_port(struct sockaddr *sap, int *port,
777                                 const unsigned short default_port)
778 {
779         if (*port == NFS_UNSPEC_PORT)
780                 *port = default_port;
781
782         rpc_set_port(sap, *port);
783 }