internal.h 30 KB

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