internal.h 25 KB

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