internal.h 31 KB

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