Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * NFS internal definitions
0004  */
0005 
0006 #include "nfs4_fs.h"
0007 #include <linux/fs_context.h>
0008 #include <linux/security.h>
0009 #include <linux/crc32.h>
0010 #include <linux/sunrpc/addr.h>
0011 #include <linux/nfs_page.h>
0012 #include <linux/wait_bit.h>
0013 
0014 #define NFS_SB_MASK (SB_RDONLY|SB_NOSUID|SB_NODEV|SB_NOEXEC|SB_SYNCHRONOUS)
0015 
0016 extern const struct export_operations nfs_export_ops;
0017 
0018 struct nfs_string;
0019 struct nfs_pageio_descriptor;
0020 
0021 static inline void nfs_attr_check_mountpoint(struct super_block *parent, struct nfs_fattr *fattr)
0022 {
0023     if (!nfs_fsid_equal(&NFS_SB(parent)->fsid, &fattr->fsid))
0024         fattr->valid |= NFS_ATTR_FATTR_MOUNTPOINT;
0025 }
0026 
0027 static inline int nfs_attr_use_mounted_on_fileid(struct nfs_fattr *fattr)
0028 {
0029     if (((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) == 0) ||
0030         (((fattr->valid & NFS_ATTR_FATTR_MOUNTPOINT) == 0) &&
0031          ((fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) == 0)))
0032         return 0;
0033     return 1;
0034 }
0035 
0036 static inline bool nfs_lookup_is_soft_revalidate(const struct dentry *dentry)
0037 {
0038     if (!(NFS_SB(dentry->d_sb)->flags & NFS_MOUNT_SOFTREVAL))
0039         return false;
0040     if (!d_is_positive(dentry) || !NFS_FH(d_inode(dentry))->size)
0041         return false;
0042     return true;
0043 }
0044 
0045 static inline fmode_t flags_to_mode(int flags)
0046 {
0047     fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
0048     if ((flags & O_ACCMODE) != O_WRONLY)
0049         res |= FMODE_READ;
0050     if ((flags & O_ACCMODE) != O_RDONLY)
0051         res |= FMODE_WRITE;
0052     return res;
0053 }
0054 
0055 /*
0056  * Note: RFC 1813 doesn't limit the number of auth flavors that
0057  * a server can return, so make something up.
0058  */
0059 #define NFS_MAX_SECFLAVORS  (12)
0060 
0061 /*
0062  * Value used if the user did not specify a port value.
0063  */
0064 #define NFS_UNSPEC_PORT     (-1)
0065 
0066 #define NFS_UNSPEC_RETRANS  (UINT_MAX)
0067 #define NFS_UNSPEC_TIMEO    (UINT_MAX)
0068 
0069 struct nfs_client_initdata {
0070     unsigned long init_flags;
0071     const char *hostname;           /* Hostname of the server */
0072     const struct sockaddr *addr;        /* Address of the server */
0073     const char *nodename;           /* Hostname of the client */
0074     const char *ip_addr;            /* IP address of the client */
0075     size_t addrlen;
0076     struct nfs_subversion *nfs_mod;
0077     int proto;
0078     u32 minorversion;
0079     unsigned int nconnect;
0080     unsigned int max_connect;
0081     struct net *net;
0082     const struct rpc_timeout *timeparms;
0083     const struct cred *cred;
0084 };
0085 
0086 /*
0087  * In-kernel mount arguments
0088  */
0089 struct nfs_fs_context {
0090     bool            internal;
0091     bool            skip_reconfig_option_check;
0092     bool            need_mount;
0093     bool            sloppy;
0094     unsigned int        flags;      /* NFS{,4}_MOUNT_* flags */
0095     unsigned int        rsize, wsize;
0096     unsigned int        timeo, retrans;
0097     unsigned int        acregmin, acregmax;
0098     unsigned int        acdirmin, acdirmax;
0099     unsigned int        namlen;
0100     unsigned int        options;
0101     unsigned int        bsize;
0102     struct nfs_auth_info    auth_info;
0103     rpc_authflavor_t    selected_flavor;
0104     char            *client_address;
0105     unsigned int        version;
0106     unsigned int        minorversion;
0107     char            *fscache_uniq;
0108     unsigned short      protofamily;
0109     unsigned short      mountfamily;
0110     bool            has_sec_mnt_opts;
0111 
0112     struct {
0113         union {
0114             struct sockaddr address;
0115             struct sockaddr_storage _address;
0116         };
0117         size_t          addrlen;
0118         char            *hostname;
0119         u32         version;
0120         int         port;
0121         unsigned short      protocol;
0122     } mount_server;
0123 
0124     struct {
0125         union {
0126             struct sockaddr address;
0127             struct sockaddr_storage _address;
0128         };
0129         size_t          addrlen;
0130         char            *hostname;
0131         char            *export_path;
0132         int         port;
0133         unsigned short      protocol;
0134         unsigned short      nconnect;
0135         unsigned short      max_connect;
0136         unsigned short      export_path_len;
0137     } nfs_server;
0138 
0139     struct nfs_fh       *mntfh;
0140     struct nfs_server   *server;
0141     struct nfs_subversion   *nfs_mod;
0142 
0143     /* Information for a cloned mount. */
0144     struct nfs_clone_mount {
0145         struct super_block  *sb;
0146         struct dentry       *dentry;
0147         struct nfs_fattr    *fattr;
0148         unsigned int        inherited_bsize;
0149     } clone_data;
0150 };
0151 
0152 #define nfs_errorf(fc, fmt, ...) ((fc)->log.log ?       \
0153     errorf(fc, fmt, ## __VA_ARGS__) :           \
0154     ({ dprintk(fmt "\n", ## __VA_ARGS__); }))
0155 
0156 #define nfs_ferrorf(fc, fac, fmt, ...) ((fc)->log.log ?     \
0157     errorf(fc, fmt, ## __VA_ARGS__) :           \
0158     ({ dfprintk(fac, fmt "\n", ## __VA_ARGS__); }))
0159 
0160 #define nfs_invalf(fc, fmt, ...) ((fc)->log.log ?       \
0161     invalf(fc, fmt, ## __VA_ARGS__) :           \
0162     ({ dprintk(fmt "\n", ## __VA_ARGS__);  -EINVAL; }))
0163 
0164 #define nfs_finvalf(fc, fac, fmt, ...) ((fc)->log.log ?     \
0165     invalf(fc, fmt, ## __VA_ARGS__) :           \
0166     ({ dfprintk(fac, fmt "\n", ## __VA_ARGS__);  -EINVAL; }))
0167 
0168 #define nfs_warnf(fc, fmt, ...) ((fc)->log.log ?        \
0169     warnf(fc, fmt, ## __VA_ARGS__) :            \
0170     ({ dprintk(fmt "\n", ## __VA_ARGS__); }))
0171 
0172 #define nfs_fwarnf(fc, fac, fmt, ...) ((fc)->log.log ?      \
0173     warnf(fc, fmt, ## __VA_ARGS__) :            \
0174     ({ dfprintk(fac, fmt "\n", ## __VA_ARGS__); }))
0175 
0176 static inline struct nfs_fs_context *nfs_fc2context(const struct fs_context *fc)
0177 {
0178     return fc->fs_private;
0179 }
0180 
0181 /* mount_clnt.c */
0182 struct nfs_mount_request {
0183     struct sockaddr     *sap;
0184     size_t          salen;
0185     char            *hostname;
0186     char            *dirpath;
0187     u32         version;
0188     unsigned short      protocol;
0189     struct nfs_fh       *fh;
0190     int         noresvport;
0191     unsigned int        *auth_flav_len;
0192     rpc_authflavor_t    *auth_flavs;
0193     struct net      *net;
0194 };
0195 
0196 extern int nfs_mount(struct nfs_mount_request *info, int timeo, int retrans);
0197 extern void nfs_umount(const struct nfs_mount_request *info);
0198 
0199 /* client.c */
0200 extern const struct rpc_program nfs_program;
0201 extern void nfs_clients_init(struct net *net);
0202 extern void nfs_clients_exit(struct net *net);
0203 extern struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *);
0204 int nfs_create_rpc_client(struct nfs_client *, const struct nfs_client_initdata *, rpc_authflavor_t);
0205 struct nfs_client *nfs_get_client(const struct nfs_client_initdata *);
0206 int nfs_probe_server(struct nfs_server *, struct nfs_fh *);
0207 void nfs_server_insert_lists(struct nfs_server *);
0208 void nfs_server_remove_lists(struct nfs_server *);
0209 void nfs_init_timeout_values(struct rpc_timeout *to, int proto, int timeo, int retrans);
0210 int nfs_init_server_rpcclient(struct nfs_server *, const struct rpc_timeout *t,
0211         rpc_authflavor_t);
0212 struct nfs_server *nfs_alloc_server(void);
0213 void nfs_server_copy_userdata(struct nfs_server *, struct nfs_server *);
0214 
0215 extern void nfs_put_client(struct nfs_client *);
0216 extern void nfs_free_client(struct nfs_client *);
0217 extern struct nfs_client *nfs4_find_client_ident(struct net *, int);
0218 extern struct nfs_client *
0219 nfs4_find_client_sessionid(struct net *, const struct sockaddr *,
0220                 struct nfs4_sessionid *, u32);
0221 extern struct nfs_server *nfs_create_server(struct fs_context *);
0222 extern void nfs4_server_set_init_caps(struct nfs_server *);
0223 extern struct nfs_server *nfs4_create_server(struct fs_context *);
0224 extern struct nfs_server *nfs4_create_referral_server(struct fs_context *);
0225 extern int nfs4_update_server(struct nfs_server *server, const char *hostname,
0226                     struct sockaddr *sap, size_t salen,
0227                     struct net *net);
0228 extern void nfs_free_server(struct nfs_server *server);
0229 extern struct nfs_server *nfs_clone_server(struct nfs_server *,
0230                        struct nfs_fh *,
0231                        struct nfs_fattr *,
0232                        rpc_authflavor_t);
0233 extern bool nfs_client_init_is_complete(const struct nfs_client *clp);
0234 extern int nfs_client_init_status(const struct nfs_client *clp);
0235 extern int nfs_wait_client_init_complete(const struct nfs_client *clp);
0236 extern void nfs_mark_client_ready(struct nfs_client *clp, int state);
0237 extern struct nfs_client *nfs4_set_ds_client(struct nfs_server *mds_srv,
0238                          const struct sockaddr *ds_addr,
0239                          int ds_addrlen, int ds_proto,
0240                          unsigned int ds_timeo,
0241                          unsigned int ds_retrans,
0242                          u32 minor_version);
0243 extern struct rpc_clnt *nfs4_find_or_create_ds_client(struct nfs_client *,
0244                         struct inode *);
0245 extern struct nfs_client *nfs3_set_ds_client(struct nfs_server *mds_srv,
0246             const struct sockaddr *ds_addr, int ds_addrlen,
0247             int ds_proto, unsigned int ds_timeo,
0248             unsigned int ds_retrans);
0249 #ifdef CONFIG_PROC_FS
0250 extern int __init nfs_fs_proc_init(void);
0251 extern void nfs_fs_proc_exit(void);
0252 extern int nfs_fs_proc_net_init(struct net *net);
0253 extern void nfs_fs_proc_net_exit(struct net *net);
0254 #else
0255 static inline int nfs_fs_proc_net_init(struct net *net)
0256 {
0257     return 0;
0258 }
0259 static inline void nfs_fs_proc_net_exit(struct net *net)
0260 {
0261 }
0262 static inline int nfs_fs_proc_init(void)
0263 {
0264     return 0;
0265 }
0266 static inline void nfs_fs_proc_exit(void)
0267 {
0268 }
0269 #endif
0270 
0271 /* callback_xdr.c */
0272 extern const struct svc_version nfs4_callback_version1;
0273 extern const struct svc_version nfs4_callback_version4;
0274 
0275 /* fs_context.c */
0276 extern struct file_system_type nfs_fs_type;
0277 
0278 /* pagelist.c */
0279 extern int __init nfs_init_nfspagecache(void);
0280 extern void nfs_destroy_nfspagecache(void);
0281 extern int __init nfs_init_readpagecache(void);
0282 extern void nfs_destroy_readpagecache(void);
0283 extern int __init nfs_init_writepagecache(void);
0284 extern void nfs_destroy_writepagecache(void);
0285 
0286 extern int __init nfs_init_directcache(void);
0287 extern void nfs_destroy_directcache(void);
0288 extern void nfs_pgheader_init(struct nfs_pageio_descriptor *desc,
0289                   struct nfs_pgio_header *hdr,
0290                   void (*release)(struct nfs_pgio_header *hdr));
0291 void nfs_set_pgio_error(struct nfs_pgio_header *hdr, int error, loff_t pos);
0292 int nfs_iocounter_wait(struct nfs_lock_context *l_ctx);
0293 
0294 extern const struct nfs_pageio_ops nfs_pgio_rw_ops;
0295 struct nfs_pgio_header *nfs_pgio_header_alloc(const struct nfs_rw_ops *);
0296 void nfs_pgio_header_free(struct nfs_pgio_header *);
0297 int nfs_generic_pgio(struct nfs_pageio_descriptor *, struct nfs_pgio_header *);
0298 int nfs_initiate_pgio(struct rpc_clnt *clnt, struct nfs_pgio_header *hdr,
0299               const struct cred *cred, const struct nfs_rpc_ops *rpc_ops,
0300               const struct rpc_call_ops *call_ops, int how, int flags);
0301 void nfs_free_request(struct nfs_page *req);
0302 struct nfs_pgio_mirror *
0303 nfs_pgio_current_mirror(struct nfs_pageio_descriptor *desc);
0304 
0305 static inline bool nfs_match_open_context(const struct nfs_open_context *ctx1,
0306         const struct nfs_open_context *ctx2)
0307 {
0308     return cred_fscmp(ctx1->cred, ctx2->cred) == 0 && ctx1->state == ctx2->state;
0309 }
0310 
0311 /* nfs2xdr.c */
0312 extern const struct rpc_procinfo nfs_procedures[];
0313 extern int nfs2_decode_dirent(struct xdr_stream *,
0314                 struct nfs_entry *, bool);
0315 
0316 /* nfs3xdr.c */
0317 extern const struct rpc_procinfo nfs3_procedures[];
0318 extern int nfs3_decode_dirent(struct xdr_stream *,
0319                 struct nfs_entry *, bool);
0320 
0321 /* nfs4xdr.c */
0322 #if IS_ENABLED(CONFIG_NFS_V4)
0323 extern int nfs4_decode_dirent(struct xdr_stream *,
0324                 struct nfs_entry *, bool);
0325 #endif
0326 #ifdef CONFIG_NFS_V4_1
0327 extern const u32 nfs41_maxread_overhead;
0328 extern const u32 nfs41_maxwrite_overhead;
0329 extern const u32 nfs41_maxgetdevinfo_overhead;
0330 #endif
0331 
0332 /* nfs4proc.c */
0333 #if IS_ENABLED(CONFIG_NFS_V4)
0334 extern const struct rpc_procinfo nfs4_procedures[];
0335 #endif
0336 
0337 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
0338 extern struct nfs4_label *nfs4_label_alloc(struct nfs_server *server, gfp_t flags);
0339 static inline struct nfs4_label *
0340 nfs4_label_copy(struct nfs4_label *dst, struct nfs4_label *src)
0341 {
0342     if (!dst || !src)
0343         return NULL;
0344 
0345     if (src->len > NFS4_MAXLABELLEN)
0346         return NULL;
0347 
0348     dst->lfs = src->lfs;
0349     dst->pi = src->pi;
0350     dst->len = src->len;
0351     memcpy(dst->label, src->label, src->len);
0352 
0353     return dst;
0354 }
0355 
0356 static inline void nfs_zap_label_cache_locked(struct nfs_inode *nfsi)
0357 {
0358     if (nfs_server_capable(&nfsi->vfs_inode, NFS_CAP_SECURITY_LABEL))
0359         nfsi->cache_validity |= NFS_INO_INVALID_LABEL;
0360 }
0361 #else
0362 static inline struct nfs4_label *nfs4_label_alloc(struct nfs_server *server, gfp_t flags) { return NULL; }
0363 static inline void nfs_zap_label_cache_locked(struct nfs_inode *nfsi)
0364 {
0365 }
0366 static inline struct nfs4_label *
0367 nfs4_label_copy(struct nfs4_label *dst, struct nfs4_label *src)
0368 {
0369     return NULL;
0370 }
0371 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
0372 
0373 /* proc.c */
0374 void nfs_close_context(struct nfs_open_context *ctx, int is_sync);
0375 extern struct nfs_client *nfs_init_client(struct nfs_client *clp,
0376                const struct nfs_client_initdata *);
0377 
0378 /* dir.c */
0379 extern void nfs_readdir_record_entry_cache_hit(struct inode *dir);
0380 extern void nfs_readdir_record_entry_cache_miss(struct inode *dir);
0381 extern unsigned long nfs_access_cache_count(struct shrinker *shrink,
0382                         struct shrink_control *sc);
0383 extern unsigned long nfs_access_cache_scan(struct shrinker *shrink,
0384                        struct shrink_control *sc);
0385 struct dentry *nfs_lookup(struct inode *, struct dentry *, unsigned int);
0386 void nfs_d_prune_case_insensitive_aliases(struct inode *inode);
0387 int nfs_create(struct user_namespace *, struct inode *, struct dentry *,
0388            umode_t, bool);
0389 int nfs_mkdir(struct user_namespace *, struct inode *, struct dentry *,
0390           umode_t);
0391 int nfs_rmdir(struct inode *, struct dentry *);
0392 int nfs_unlink(struct inode *, struct dentry *);
0393 int nfs_symlink(struct user_namespace *, struct inode *, struct dentry *,
0394         const char *);
0395 int nfs_link(struct dentry *, struct inode *, struct dentry *);
0396 int nfs_mknod(struct user_namespace *, struct inode *, struct dentry *, umode_t,
0397           dev_t);
0398 int nfs_rename(struct user_namespace *, struct inode *, struct dentry *,
0399            struct inode *, struct dentry *, unsigned int);
0400 
0401 #ifdef CONFIG_NFS_V4_2
0402 static inline __u32 nfs_access_xattr_mask(const struct nfs_server *server)
0403 {
0404     if (!(server->caps & NFS_CAP_XATTR))
0405         return 0;
0406     return NFS4_ACCESS_XAREAD | NFS4_ACCESS_XAWRITE | NFS4_ACCESS_XALIST;
0407 }
0408 #else
0409 static inline __u32 nfs_access_xattr_mask(const struct nfs_server *server)
0410 {
0411     return 0;
0412 }
0413 #endif
0414 
0415 /* file.c */
0416 int nfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync);
0417 loff_t nfs_file_llseek(struct file *, loff_t, int);
0418 ssize_t nfs_file_read(struct kiocb *, struct iov_iter *);
0419 int nfs_file_mmap(struct file *, struct vm_area_struct *);
0420 ssize_t nfs_file_write(struct kiocb *, struct iov_iter *);
0421 int nfs_file_release(struct inode *, struct file *);
0422 int nfs_lock(struct file *, int, struct file_lock *);
0423 int nfs_flock(struct file *, int, struct file_lock *);
0424 int nfs_check_flags(int);
0425 
0426 /* inode.c */
0427 extern struct workqueue_struct *nfsiod_workqueue;
0428 extern struct inode *nfs_alloc_inode(struct super_block *sb);
0429 extern void nfs_free_inode(struct inode *);
0430 extern int nfs_write_inode(struct inode *, struct writeback_control *);
0431 extern int nfs_drop_inode(struct inode *);
0432 extern void nfs_clear_inode(struct inode *);
0433 extern void nfs_evict_inode(struct inode *);
0434 extern void nfs_zap_acl_cache(struct inode *inode);
0435 extern void nfs_set_cache_invalid(struct inode *inode, unsigned long flags);
0436 extern bool nfs_check_cache_invalid(struct inode *, unsigned long);
0437 extern int nfs_wait_bit_killable(struct wait_bit_key *key, int mode);
0438 extern int nfs_wait_atomic_killable(atomic_t *p, unsigned int mode);
0439 
0440 /* super.c */
0441 extern const struct super_operations nfs_sops;
0442 bool nfs_auth_info_match(const struct nfs_auth_info *, rpc_authflavor_t);
0443 int nfs_try_get_tree(struct fs_context *);
0444 int nfs_get_tree_common(struct fs_context *);
0445 void nfs_kill_super(struct super_block *);
0446 
0447 extern struct rpc_stat nfs_rpcstat;
0448 
0449 extern int __init register_nfs_fs(void);
0450 extern void __exit unregister_nfs_fs(void);
0451 extern bool nfs_sb_active(struct super_block *sb);
0452 extern void nfs_sb_deactive(struct super_block *sb);
0453 extern int nfs_client_for_each_server(struct nfs_client *clp,
0454                       int (*fn)(struct nfs_server *, void *),
0455                       void *data);
0456 /* io.c */
0457 extern void nfs_start_io_read(struct inode *inode);
0458 extern void nfs_end_io_read(struct inode *inode);
0459 extern void nfs_start_io_write(struct inode *inode);
0460 extern void nfs_end_io_write(struct inode *inode);
0461 extern void nfs_start_io_direct(struct inode *inode);
0462 extern void nfs_end_io_direct(struct inode *inode);
0463 
0464 static inline bool nfs_file_io_is_buffered(struct nfs_inode *nfsi)
0465 {
0466     return test_bit(NFS_INO_ODIRECT, &nfsi->flags) == 0;
0467 }
0468 
0469 /* namespace.c */
0470 #define NFS_PATH_CANONICAL 1
0471 extern char *nfs_path(char **p, struct dentry *dentry,
0472               char *buffer, ssize_t buflen, unsigned flags);
0473 extern struct vfsmount *nfs_d_automount(struct path *path);
0474 int nfs_submount(struct fs_context *, struct nfs_server *);
0475 int nfs_do_submount(struct fs_context *);
0476 
0477 /* getroot.c */
0478 extern int nfs_get_root(struct super_block *s, struct fs_context *fc);
0479 #if IS_ENABLED(CONFIG_NFS_V4)
0480 extern int nfs4_get_rootfh(struct nfs_server *server, struct nfs_fh *mntfh, bool);
0481 #endif
0482 
0483 struct nfs_pgio_completion_ops;
0484 /* read.c */
0485 extern void nfs_pageio_init_read(struct nfs_pageio_descriptor *pgio,
0486             struct inode *inode, bool force_mds,
0487             const struct nfs_pgio_completion_ops *compl_ops);
0488 extern void nfs_read_prepare(struct rpc_task *task, void *calldata);
0489 extern void nfs_pageio_reset_read_mds(struct nfs_pageio_descriptor *pgio);
0490 
0491 /* super.c */
0492 void nfs_umount_begin(struct super_block *);
0493 int  nfs_statfs(struct dentry *, struct kstatfs *);
0494 int  nfs_show_options(struct seq_file *, struct dentry *);
0495 int  nfs_show_devname(struct seq_file *, struct dentry *);
0496 int  nfs_show_path(struct seq_file *, struct dentry *);
0497 int  nfs_show_stats(struct seq_file *, struct dentry *);
0498 int  nfs_reconfigure(struct fs_context *);
0499 
0500 /* write.c */
0501 extern void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
0502             struct inode *inode, int ioflags, bool force_mds,
0503             const struct nfs_pgio_completion_ops *compl_ops);
0504 extern void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio);
0505 extern void nfs_commit_free(struct nfs_commit_data *p);
0506 extern void nfs_write_prepare(struct rpc_task *task, void *calldata);
0507 extern void nfs_commit_prepare(struct rpc_task *task, void *calldata);
0508 extern int nfs_initiate_commit(struct rpc_clnt *clnt,
0509                    struct nfs_commit_data *data,
0510                    const struct nfs_rpc_ops *nfs_ops,
0511                    const struct rpc_call_ops *call_ops,
0512                    int how, int flags);
0513 extern void nfs_init_commit(struct nfs_commit_data *data,
0514                 struct list_head *head,
0515                 struct pnfs_layout_segment *lseg,
0516                 struct nfs_commit_info *cinfo);
0517 int nfs_scan_commit_list(struct list_head *src, struct list_head *dst,
0518              struct nfs_commit_info *cinfo, int max);
0519 unsigned long nfs_reqs_to_commit(struct nfs_commit_info *);
0520 int nfs_scan_commit(struct inode *inode, struct list_head *dst,
0521             struct nfs_commit_info *cinfo);
0522 void nfs_mark_request_commit(struct nfs_page *req,
0523                  struct pnfs_layout_segment *lseg,
0524                  struct nfs_commit_info *cinfo,
0525                  u32 ds_commit_idx);
0526 int nfs_write_need_commit(struct nfs_pgio_header *);
0527 void nfs_writeback_update_inode(struct nfs_pgio_header *hdr);
0528 int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
0529                 int how, struct nfs_commit_info *cinfo);
0530 void nfs_retry_commit(struct list_head *page_list,
0531               struct pnfs_layout_segment *lseg,
0532               struct nfs_commit_info *cinfo,
0533               u32 ds_commit_idx);
0534 void nfs_commitdata_release(struct nfs_commit_data *data);
0535 void nfs_request_add_commit_list(struct nfs_page *req,
0536                  struct nfs_commit_info *cinfo);
0537 void nfs_request_add_commit_list_locked(struct nfs_page *req,
0538         struct list_head *dst,
0539         struct nfs_commit_info *cinfo);
0540 void nfs_request_remove_commit_list(struct nfs_page *req,
0541                     struct nfs_commit_info *cinfo);
0542 void nfs_init_cinfo(struct nfs_commit_info *cinfo,
0543             struct inode *inode,
0544             struct nfs_direct_req *dreq);
0545 int nfs_key_timeout_notify(struct file *filp, struct inode *inode);
0546 bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx, struct inode *inode);
0547 void nfs_pageio_stop_mirroring(struct nfs_pageio_descriptor *pgio);
0548 
0549 int nfs_filemap_write_and_wait_range(struct address_space *mapping,
0550         loff_t lstart, loff_t lend);
0551 
0552 #ifdef CONFIG_NFS_V4_1
0553 static inline void
0554 pnfs_bucket_clear_pnfs_ds_commit_verifiers(struct pnfs_commit_bucket *buckets,
0555         unsigned int nbuckets)
0556 {
0557     unsigned int i;
0558 
0559     for (i = 0; i < nbuckets; i++)
0560         buckets[i].direct_verf.committed = NFS_INVALID_STABLE_HOW;
0561 }
0562 static inline
0563 void nfs_clear_pnfs_ds_commit_verifiers(struct pnfs_ds_commit_info *cinfo)
0564 {
0565     struct pnfs_commit_array *array;
0566 
0567     rcu_read_lock();
0568     list_for_each_entry_rcu(array, &cinfo->commits, cinfo_list)
0569         pnfs_bucket_clear_pnfs_ds_commit_verifiers(array->buckets,
0570                 array->nbuckets);
0571     rcu_read_unlock();
0572 }
0573 #else
0574 static inline
0575 void nfs_clear_pnfs_ds_commit_verifiers(struct pnfs_ds_commit_info *cinfo)
0576 {
0577 }
0578 #endif
0579 
0580 #ifdef CONFIG_MIGRATION
0581 int nfs_migrate_folio(struct address_space *, struct folio *dst,
0582         struct folio *src, enum migrate_mode);
0583 #else
0584 #define nfs_migrate_folio NULL
0585 #endif
0586 
0587 static inline int
0588 nfs_write_verifier_cmp(const struct nfs_write_verifier *v1,
0589         const struct nfs_write_verifier *v2)
0590 {
0591     return memcmp(v1->data, v2->data, sizeof(v1->data));
0592 }
0593 
0594 static inline bool
0595 nfs_write_match_verf(const struct nfs_writeverf *verf,
0596         struct nfs_page *req)
0597 {
0598     return verf->committed > NFS_UNSTABLE &&
0599         !nfs_write_verifier_cmp(&req->wb_verf, &verf->verifier);
0600 }
0601 
0602 static inline gfp_t nfs_io_gfp_mask(void)
0603 {
0604     if (current->flags & PF_WQ_WORKER)
0605         return GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN;
0606     return GFP_KERNEL;
0607 }
0608 
0609 /*
0610  * Special version of should_remove_suid() that ignores capabilities.
0611  */
0612 static inline int nfs_should_remove_suid(const struct inode *inode)
0613 {
0614     umode_t mode = inode->i_mode;
0615     int kill = 0;
0616 
0617     /* suid always must be killed */
0618     if (unlikely(mode & S_ISUID))
0619         kill = ATTR_KILL_SUID;
0620 
0621     /*
0622      * sgid without any exec bits is just a mandatory locking mark; leave
0623      * it alone.  If some exec bits are set, it's a real sgid; kill it.
0624      */
0625     if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
0626         kill |= ATTR_KILL_SGID;
0627 
0628     if (unlikely(kill && S_ISREG(mode)))
0629         return kill;
0630 
0631     return 0;
0632 }
0633 
0634 /* unlink.c */
0635 extern struct rpc_task *
0636 nfs_async_rename(struct inode *old_dir, struct inode *new_dir,
0637          struct dentry *old_dentry, struct dentry *new_dentry,
0638          void (*complete)(struct rpc_task *, struct nfs_renamedata *));
0639 extern int nfs_sillyrename(struct inode *dir, struct dentry *dentry);
0640 
0641 /* direct.c */
0642 void nfs_init_cinfo_from_dreq(struct nfs_commit_info *cinfo,
0643                   struct nfs_direct_req *dreq);
0644 extern ssize_t nfs_dreq_bytes_left(struct nfs_direct_req *dreq);
0645 
0646 /* nfs4proc.c */
0647 extern struct nfs_client *nfs4_init_client(struct nfs_client *clp,
0648                 const struct nfs_client_initdata *);
0649 extern int nfs40_walk_client_list(struct nfs_client *clp,
0650                 struct nfs_client **result,
0651                 const struct cred *cred);
0652 extern int nfs41_walk_client_list(struct nfs_client *clp,
0653                 struct nfs_client **result,
0654                 const struct cred *cred);
0655 extern void nfs4_test_session_trunk(struct rpc_clnt *clnt,
0656                 struct rpc_xprt *xprt,
0657                 void *data);
0658 
0659 static inline struct inode *nfs_igrab_and_active(struct inode *inode)
0660 {
0661     struct super_block *sb = inode->i_sb;
0662 
0663     if (sb && nfs_sb_active(sb)) {
0664         if (igrab(inode))
0665             return inode;
0666         nfs_sb_deactive(sb);
0667     }
0668     return NULL;
0669 }
0670 
0671 static inline void nfs_iput_and_deactive(struct inode *inode)
0672 {
0673     if (inode != NULL) {
0674         struct super_block *sb = inode->i_sb;
0675 
0676         iput(inode);
0677         nfs_sb_deactive(sb);
0678     }
0679 }
0680 
0681 /*
0682  * Determine the device name as a string
0683  */
0684 static inline char *nfs_devname(struct dentry *dentry,
0685                 char *buffer, ssize_t buflen)
0686 {
0687     char *dummy;
0688     return nfs_path(&dummy, dentry, buffer, buflen, NFS_PATH_CANONICAL);
0689 }
0690 
0691 /*
0692  * Determine the actual block size (and log2 thereof)
0693  */
0694 static inline
0695 unsigned long nfs_block_bits(unsigned long bsize, unsigned char *nrbitsp)
0696 {
0697     /* make sure blocksize is a power of two */
0698     if ((bsize & (bsize - 1)) || nrbitsp) {
0699         unsigned char   nrbits;
0700 
0701         for (nrbits = 31; nrbits && !(bsize & (1 << nrbits)); nrbits--)
0702             ;
0703         bsize = 1 << nrbits;
0704         if (nrbitsp)
0705             *nrbitsp = nrbits;
0706     }
0707 
0708     return bsize;
0709 }
0710 
0711 /*
0712  * Calculate the number of 512byte blocks used.
0713  */
0714 static inline blkcnt_t nfs_calc_block_size(u64 tsize)
0715 {
0716     blkcnt_t used = (tsize + 511) >> 9;
0717     return (used > ULONG_MAX) ? ULONG_MAX : used;
0718 }
0719 
0720 /*
0721  * Compute and set NFS server blocksize
0722  */
0723 static inline
0724 unsigned long nfs_block_size(unsigned long bsize, unsigned char *nrbitsp)
0725 {
0726     if (bsize < NFS_MIN_FILE_IO_SIZE)
0727         bsize = NFS_DEF_FILE_IO_SIZE;
0728     else if (bsize >= NFS_MAX_FILE_IO_SIZE)
0729         bsize = NFS_MAX_FILE_IO_SIZE;
0730 
0731     return nfs_block_bits(bsize, nrbitsp);
0732 }
0733 
0734 /*
0735  * Compute and set NFS server rsize / wsize
0736  */
0737 static inline
0738 unsigned long nfs_io_size(unsigned long iosize, enum xprt_transports proto)
0739 {
0740     if (iosize < NFS_MIN_FILE_IO_SIZE)
0741         iosize = NFS_DEF_FILE_IO_SIZE;
0742     else if (iosize >= NFS_MAX_FILE_IO_SIZE)
0743         iosize = NFS_MAX_FILE_IO_SIZE;
0744     else
0745         iosize = iosize & PAGE_MASK;
0746 
0747     if (proto == XPRT_TRANSPORT_UDP)
0748         return nfs_block_bits(iosize, NULL);
0749     return iosize;
0750 }
0751 
0752 /*
0753  * Determine the maximum file size for a superblock
0754  */
0755 static inline
0756 void nfs_super_set_maxbytes(struct super_block *sb, __u64 maxfilesize)
0757 {
0758     sb->s_maxbytes = (loff_t)maxfilesize;
0759     if (sb->s_maxbytes > MAX_LFS_FILESIZE || sb->s_maxbytes <= 0)
0760         sb->s_maxbytes = MAX_LFS_FILESIZE;
0761 }
0762 
0763 /*
0764  * Record the page as unstable (an extra writeback period) and mark its
0765  * inode as dirty.
0766  */
0767 static inline
0768 void nfs_mark_page_unstable(struct page *page, struct nfs_commit_info *cinfo)
0769 {
0770     if (!cinfo->dreq) {
0771         struct inode *inode = page_file_mapping(page)->host;
0772 
0773         /* This page is really still in write-back - just that the
0774          * writeback is happening on the server now.
0775          */
0776         inc_node_page_state(page, NR_WRITEBACK);
0777         inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
0778         __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
0779     }
0780 }
0781 
0782 /*
0783  * Determine the number of bytes of data the page contains
0784  */
0785 static inline
0786 unsigned int nfs_page_length(struct page *page)
0787 {
0788     loff_t i_size = i_size_read(page_file_mapping(page)->host);
0789 
0790     if (i_size > 0) {
0791         pgoff_t index = page_index(page);
0792         pgoff_t end_index = (i_size - 1) >> PAGE_SHIFT;
0793         if (index < end_index)
0794             return PAGE_SIZE;
0795         if (index == end_index)
0796             return ((i_size - 1) & ~PAGE_MASK) + 1;
0797     }
0798     return 0;
0799 }
0800 
0801 /*
0802  * Convert a umode to a dirent->d_type
0803  */
0804 static inline
0805 unsigned char nfs_umode_to_dtype(umode_t mode)
0806 {
0807     return (mode >> 12) & 15;
0808 }
0809 
0810 /*
0811  * Determine the number of pages in an array of length 'len' and
0812  * with a base offset of 'base'
0813  */
0814 static inline
0815 unsigned int nfs_page_array_len(unsigned int base, size_t len)
0816 {
0817     return ((unsigned long)len + (unsigned long)base +
0818         PAGE_SIZE - 1) >> PAGE_SHIFT;
0819 }
0820 
0821 /*
0822  * Convert a struct timespec64 into a 64-bit change attribute
0823  *
0824  * This does approximately the same thing as timespec64_to_ns(),
0825  * but for calculation efficiency, we multiply the seconds by
0826  * 1024*1024*1024.
0827  */
0828 static inline
0829 u64 nfs_timespec_to_change_attr(const struct timespec64 *ts)
0830 {
0831     return ((u64)ts->tv_sec << 30) + ts->tv_nsec;
0832 }
0833 
0834 #ifdef CONFIG_CRC32
0835 /**
0836  * nfs_fhandle_hash - calculate the crc32 hash for the filehandle
0837  * @fh - pointer to filehandle
0838  *
0839  * returns a crc32 hash for the filehandle that is compatible with
0840  * the one displayed by "wireshark".
0841  */
0842 static inline u32 nfs_fhandle_hash(const struct nfs_fh *fh)
0843 {
0844     return ~crc32_le(0xFFFFFFFF, &fh->data[0], fh->size);
0845 }
0846 static inline u32 nfs_stateid_hash(const nfs4_stateid *stateid)
0847 {
0848     return ~crc32_le(0xFFFFFFFF, &stateid->other[0],
0849                 NFS4_STATEID_OTHER_SIZE);
0850 }
0851 #else
0852 static inline u32 nfs_fhandle_hash(const struct nfs_fh *fh)
0853 {
0854     return 0;
0855 }
0856 static inline u32 nfs_stateid_hash(nfs4_stateid *stateid)
0857 {
0858     return 0;
0859 }
0860 #endif
0861 
0862 static inline bool nfs_error_is_fatal(int err)
0863 {
0864     switch (err) {
0865     case -ERESTARTSYS:
0866     case -EINTR:
0867     case -EACCES:
0868     case -EDQUOT:
0869     case -EFBIG:
0870     case -EIO:
0871     case -ENOSPC:
0872     case -EROFS:
0873     case -ESTALE:
0874     case -E2BIG:
0875     case -ENOMEM:
0876     case -ETIMEDOUT:
0877         return true;
0878     default:
0879         return false;
0880     }
0881 }
0882 
0883 static inline bool nfs_error_is_fatal_on_server(int err)
0884 {
0885     switch (err) {
0886     case 0:
0887     case -ERESTARTSYS:
0888     case -EINTR:
0889     case -ENOMEM:
0890         return false;
0891     }
0892     return nfs_error_is_fatal(err);
0893 }
0894 
0895 /*
0896  * Select between a default port value and a user-specified port value.
0897  * If a zero value is set, then autobind will be used.
0898  */
0899 static inline void nfs_set_port(struct sockaddr *sap, int *port,
0900                 const unsigned short default_port)
0901 {
0902     if (*port == NFS_UNSPEC_PORT)
0903         *port = default_port;
0904 
0905     rpc_set_port(sap, *port);
0906 }
0907 
0908 struct nfs_direct_req {
0909     struct kref     kref;       /* release manager */
0910 
0911     /* I/O parameters */
0912     struct nfs_open_context *ctx;       /* file open context info */
0913     struct nfs_lock_context *l_ctx;     /* Lock context info */
0914     struct kiocb *      iocb;       /* controlling i/o request */
0915     struct inode *      inode;      /* target file of i/o */
0916 
0917     /* completion state */
0918     atomic_t        io_count;   /* i/os we're waiting for */
0919     spinlock_t      lock;       /* protect completion state */
0920 
0921     loff_t          io_start;   /* Start offset for I/O */
0922     ssize_t         count,      /* bytes actually processed */
0923                 max_count,  /* max expected count */
0924                 bytes_left, /* bytes left to be sent */
0925                 error;      /* any reported error */
0926     struct completion   completion; /* wait for i/o completion */
0927 
0928     /* commit state */
0929     struct nfs_mds_commit_info mds_cinfo;   /* Storage for cinfo */
0930     struct pnfs_ds_commit_info ds_cinfo;    /* Storage for cinfo */
0931     struct work_struct  work;
0932     int         flags;
0933     /* for write */
0934 #define NFS_ODIRECT_DO_COMMIT       (1) /* an unstable reply was received */
0935 #define NFS_ODIRECT_RESCHED_WRITES  (2) /* write verification failed */
0936     /* for read */
0937 #define NFS_ODIRECT_SHOULD_DIRTY    (3) /* dirty user-space page after read */
0938 #define NFS_ODIRECT_DONE        INT_MAX /* write verification failed */
0939 };