internal.h 28 KB

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