xref: /linux/fs/smb/client/cifsglob.h (revision 6c7353836a91b1479e6b81791cdc163fb04b4834)
1 /* SPDX-License-Identifier: LGPL-2.1 */
2 /*
3  *
4  *   Copyright (C) International Business Machines  Corp., 2002,2008
5  *   Author(s): Steve French (sfrench@us.ibm.com)
6  *              Jeremy Allison (jra@samba.org)
7  *
8  */
9 #ifndef _CIFS_GLOB_H
10 #define _CIFS_GLOB_H
11 
12 #include <linux/in.h>
13 #include <linux/in6.h>
14 #include <linux/inet.h>
15 #include <linux/slab.h>
16 #include <linux/scatterlist.h>
17 #include <linux/mm.h>
18 #include <linux/mempool.h>
19 #include <linux/workqueue.h>
20 #include <linux/utsname.h>
21 #include <linux/sched/mm.h>
22 #include <linux/netfs.h>
23 #include "cifs_fs_sb.h"
24 #include "cifsacl.h"
25 #include <crypto/internal/hash.h>
26 #include <uapi/linux/cifs/cifs_mount.h>
27 #include "../common/smb2pdu.h"
28 #include "smb2pdu.h"
29 #include <linux/filelock.h>
30 
31 #define SMB_PATH_MAX 260
32 #define CIFS_PORT 445
33 #define RFC1001_PORT 139
34 
35 /*
36  * The sizes of various internal tables and strings
37  */
38 #define MAX_UID_INFO 16
39 #define MAX_SES_INFO 2
40 #define MAX_TCON_INFO 4
41 
42 #define MAX_TREE_SIZE (2 + CIFS_NI_MAXHOST + 1 + CIFS_MAX_SHARE_LEN + 1)
43 
44 #define CIFS_MIN_RCV_POOL 4
45 
46 #define MAX_REOPEN_ATT	5 /* these many maximum attempts to reopen a file */
47 /*
48  * default attribute cache timeout (jiffies)
49  */
50 #define CIFS_DEF_ACTIMEO (1 * HZ)
51 
52 /*
53  * max attribute cache timeout (jiffies) - 2^30
54  */
55 #define CIFS_MAX_ACTIMEO (1 << 30)
56 
57 /*
58  * Max persistent and resilient handle timeout (milliseconds).
59  * Windows durable max was 960000 (16 minutes)
60  */
61 #define SMB3_MAX_HANDLE_TIMEOUT 960000
62 
63 /*
64  * MAX_REQ is the maximum number of requests that WE will send
65  * on one socket concurrently.
66  */
67 #define CIFS_MAX_REQ 32767
68 
69 #define RFC1001_NAME_LEN 15
70 #define RFC1001_NAME_LEN_WITH_NULL (RFC1001_NAME_LEN + 1)
71 
72 /* maximum length of ip addr as a string (including ipv6 and sctp) */
73 #define SERVER_NAME_LENGTH 80
74 #define SERVER_NAME_LEN_WITH_NULL     (SERVER_NAME_LENGTH + 1)
75 
76 /* echo interval in seconds */
77 #define SMB_ECHO_INTERVAL_MIN 1
78 #define SMB_ECHO_INTERVAL_MAX 600
79 #define SMB_ECHO_INTERVAL_DEFAULT 60
80 
81 /* smb multichannel query server interfaces interval in seconds */
82 #define SMB_INTERFACE_POLL_INTERVAL	600
83 
84 /* maximum number of PDUs in one compound */
85 #define MAX_COMPOUND 5
86 
87 /*
88  * Default number of credits to keep available for SMB3.
89  * This value is chosen somewhat arbitrarily. The Windows client
90  * defaults to 128 credits, the Windows server allows clients up to
91  * 512 credits (or 8K for later versions), and the NetApp server
92  * does not limit clients at all.  Choose a high enough default value
93  * such that the client shouldn't limit performance, but allow mount
94  * to override (until you approach 64K, where we limit credits to 65000
95  * to reduce possibility of seeing more server credit overflow bugs.
96  */
97 #define SMB2_MAX_CREDITS_AVAILABLE 32000
98 
99 #include "cifspdu.h"
100 
101 #ifndef XATTR_DOS_ATTRIB
102 #define XATTR_DOS_ATTRIB "user.DOSATTRIB"
103 #endif
104 
105 #define CIFS_MAX_WORKSTATION_LEN  (__NEW_UTS_LEN + 1)  /* reasonable max for client */
106 
107 #define CIFS_DFS_ROOT_SES(ses) ((ses)->dfs_root_ses ?: (ses))
108 
109 /*
110  * CIFS vfs client Status information (based on what we know.)
111  */
112 
113 /* associated with each connection */
114 enum statusEnum {
115 	CifsNew = 0,
116 	CifsGood,
117 	CifsExiting,
118 	CifsNeedReconnect,
119 	CifsNeedNegotiate,
120 	CifsInNegotiate,
121 };
122 
123 /* associated with each smb session */
124 enum ses_status_enum {
125 	SES_NEW = 0,
126 	SES_GOOD,
127 	SES_EXITING,
128 	SES_NEED_RECON,
129 	SES_IN_SETUP
130 };
131 
132 /* associated with each tree connection to the server */
133 enum tid_status_enum {
134 	TID_NEW = 0,
135 	TID_GOOD,
136 	TID_EXITING,
137 	TID_NEED_RECON,
138 	TID_NEED_TCON,
139 	TID_IN_TCON,
140 	TID_NEED_FILES_INVALIDATE, /* currently unused */
141 	TID_IN_FILES_INVALIDATE
142 };
143 
144 enum securityEnum {
145 	Unspecified = 0,	/* not specified */
146 	NTLMv2,			/* Legacy NTLM auth with NTLMv2 hash */
147 	RawNTLMSSP,		/* NTLMSSP without SPNEGO, NTLMv2 hash */
148 	Kerberos,		/* Kerberos via SPNEGO */
149 };
150 
151 struct session_key {
152 	unsigned int len;
153 	char *response;
154 };
155 
156 /* crypto hashing related structure/fields, not specific to a sec mech */
157 struct cifs_secmech {
158 	struct shash_desc *hmacmd5; /* hmacmd5 hash function, for NTLMv2/CR1 hashes */
159 	struct shash_desc *md5; /* md5 hash function, for CIFS/SMB1 signatures */
160 	struct shash_desc *hmacsha256; /* hmac-sha256 hash function, for SMB2 signatures */
161 	struct shash_desc *sha512; /* sha512 hash function, for SMB3.1.1 preauth hash */
162 	struct shash_desc *aes_cmac; /* block-cipher based MAC function, for SMB3 signatures */
163 
164 	struct crypto_aead *enc; /* smb3 encryption AEAD TFM (AES-CCM and AES-GCM) */
165 	struct crypto_aead *dec; /* smb3 decryption AEAD TFM (AES-CCM and AES-GCM) */
166 };
167 
168 /* per smb session structure/fields */
169 struct ntlmssp_auth {
170 	bool sesskey_per_smbsess; /* whether session key is per smb session */
171 	__u32 client_flags; /* sent by client in type 1 ntlmsssp exchange */
172 	__u32 server_flags; /* sent by server in type 2 ntlmssp exchange */
173 	unsigned char ciphertext[CIFS_CPHTXT_SIZE]; /* sent to server */
174 	char cryptkey[CIFS_CRYPTO_KEY_SIZE]; /* used by ntlmssp */
175 };
176 
177 struct cifs_cred {
178 	int uid;
179 	int gid;
180 	int mode;
181 	int cecount;
182 	struct cifs_sid osid;
183 	struct cifs_sid gsid;
184 	struct cifs_ntace *ntaces;
185 	struct cifs_ace *aces;
186 };
187 
188 struct cifs_open_info_data {
189 	bool adjust_tz;
190 	union {
191 		bool reparse_point;
192 		bool symlink;
193 	};
194 	struct {
195 		/* ioctl response buffer */
196 		struct {
197 			int buftype;
198 			struct kvec iov;
199 		} io;
200 		__u32 tag;
201 		union {
202 			struct reparse_data_buffer *buf;
203 			struct reparse_posix_data *posix;
204 		};
205 	} reparse;
206 	char *symlink_target;
207 	struct cifs_sid posix_owner;
208 	struct cifs_sid posix_group;
209 	union {
210 		struct smb2_file_all_info fi;
211 		struct smb311_posix_qinfo posix_fi;
212 	};
213 };
214 
215 static inline bool cifs_open_data_reparse(struct cifs_open_info_data *data)
216 {
217 	struct smb2_file_all_info *fi = &data->fi;
218 	u32 attrs = le32_to_cpu(fi->Attributes);
219 	bool ret;
220 
221 	ret = data->reparse_point || (attrs & ATTR_REPARSE);
222 	if (ret)
223 		attrs |= ATTR_REPARSE;
224 	fi->Attributes = cpu_to_le32(attrs);
225 	return ret;
226 }
227 
228 /*
229  *****************************************************************
230  * Except the CIFS PDUs themselves all the
231  * globally interesting structs should go here
232  *****************************************************************
233  */
234 
235 /*
236  * A smb_rqst represents a complete request to be issued to a server. It's
237  * formed by a kvec array, followed by an array of pages. Page data is assumed
238  * to start at the beginning of the first page.
239  */
240 struct smb_rqst {
241 	struct kvec	*rq_iov;	/* array of kvecs */
242 	unsigned int	rq_nvec;	/* number of kvecs in array */
243 	size_t		rq_iter_size;	/* Amount of data in ->rq_iter */
244 	struct iov_iter	rq_iter;	/* Data iterator */
245 	struct xarray	rq_buffer;	/* Page buffer for encryption */
246 };
247 
248 struct mid_q_entry;
249 struct TCP_Server_Info;
250 struct cifsFileInfo;
251 struct cifs_ses;
252 struct cifs_tcon;
253 struct dfs_info3_param;
254 struct cifs_fattr;
255 struct smb3_fs_context;
256 struct cifs_fid;
257 struct cifs_readdata;
258 struct cifs_writedata;
259 struct cifs_io_parms;
260 struct cifs_search_info;
261 struct cifsInodeInfo;
262 struct cifs_open_parms;
263 struct cifs_credits;
264 
265 struct smb_version_operations {
266 	int (*send_cancel)(struct TCP_Server_Info *, struct smb_rqst *,
267 			   struct mid_q_entry *);
268 	bool (*compare_fids)(struct cifsFileInfo *, struct cifsFileInfo *);
269 	/* setup request: allocate mid, sign message */
270 	struct mid_q_entry *(*setup_request)(struct cifs_ses *,
271 					     struct TCP_Server_Info *,
272 					     struct smb_rqst *);
273 	/* setup async request: allocate mid, sign message */
274 	struct mid_q_entry *(*setup_async_request)(struct TCP_Server_Info *,
275 						struct smb_rqst *);
276 	/* check response: verify signature, map error */
277 	int (*check_receive)(struct mid_q_entry *, struct TCP_Server_Info *,
278 			     bool);
279 	void (*add_credits)(struct TCP_Server_Info *server,
280 			    const struct cifs_credits *credits,
281 			    const int optype);
282 	void (*set_credits)(struct TCP_Server_Info *, const int);
283 	int * (*get_credits_field)(struct TCP_Server_Info *, const int);
284 	unsigned int (*get_credits)(struct mid_q_entry *);
285 	__u64 (*get_next_mid)(struct TCP_Server_Info *);
286 	void (*revert_current_mid)(struct TCP_Server_Info *server,
287 				   const unsigned int val);
288 	/* data offset from read response message */
289 	unsigned int (*read_data_offset)(char *);
290 	/*
291 	 * Data length from read response message
292 	 * When in_remaining is true, the returned data length is in
293 	 * message field DataRemaining for out-of-band data read (e.g through
294 	 * Memory Registration RDMA write in SMBD).
295 	 * Otherwise, the returned data length is in message field DataLength.
296 	 */
297 	unsigned int (*read_data_length)(char *, bool in_remaining);
298 	/* map smb to linux error */
299 	int (*map_error)(char *, bool);
300 	/* find mid corresponding to the response message */
301 	struct mid_q_entry * (*find_mid)(struct TCP_Server_Info *, char *);
302 	void (*dump_detail)(void *buf, struct TCP_Server_Info *ptcp_info);
303 	void (*clear_stats)(struct cifs_tcon *);
304 	void (*print_stats)(struct seq_file *m, struct cifs_tcon *);
305 	void (*dump_share_caps)(struct seq_file *, struct cifs_tcon *);
306 	/* verify the message */
307 	int (*check_message)(char *, unsigned int, struct TCP_Server_Info *);
308 	bool (*is_oplock_break)(char *, struct TCP_Server_Info *);
309 	int (*handle_cancelled_mid)(struct mid_q_entry *, struct TCP_Server_Info *);
310 	void (*downgrade_oplock)(struct TCP_Server_Info *server,
311 				 struct cifsInodeInfo *cinode, __u32 oplock,
312 				 unsigned int epoch, bool *purge_cache);
313 	/* process transaction2 response */
314 	bool (*check_trans2)(struct mid_q_entry *, struct TCP_Server_Info *,
315 			     char *, int);
316 	/* check if we need to negotiate */
317 	bool (*need_neg)(struct TCP_Server_Info *);
318 	/* negotiate to the server */
319 	int (*negotiate)(const unsigned int xid,
320 			 struct cifs_ses *ses,
321 			 struct TCP_Server_Info *server);
322 	/* set negotiated write size */
323 	unsigned int (*negotiate_wsize)(struct cifs_tcon *tcon, struct smb3_fs_context *ctx);
324 	/* set negotiated read size */
325 	unsigned int (*negotiate_rsize)(struct cifs_tcon *tcon, struct smb3_fs_context *ctx);
326 	/* setup smb sessionn */
327 	int (*sess_setup)(const unsigned int, struct cifs_ses *,
328 			  struct TCP_Server_Info *server,
329 			  const struct nls_table *);
330 	/* close smb session */
331 	int (*logoff)(const unsigned int, struct cifs_ses *);
332 	/* connect to a server share */
333 	int (*tree_connect)(const unsigned int, struct cifs_ses *, const char *,
334 			    struct cifs_tcon *, const struct nls_table *);
335 	/* close tree connecion */
336 	int (*tree_disconnect)(const unsigned int, struct cifs_tcon *);
337 	/* get DFS referrals */
338 	int (*get_dfs_refer)(const unsigned int, struct cifs_ses *,
339 			     const char *, struct dfs_info3_param **,
340 			     unsigned int *, const struct nls_table *, int);
341 	/* informational QFS call */
342 	void (*qfs_tcon)(const unsigned int, struct cifs_tcon *,
343 			 struct cifs_sb_info *);
344 	/* check if a path is accessible or not */
345 	int (*is_path_accessible)(const unsigned int, struct cifs_tcon *,
346 				  struct cifs_sb_info *, const char *);
347 	/* query path data from the server */
348 	int (*query_path_info)(const unsigned int xid,
349 			       struct cifs_tcon *tcon,
350 			       struct cifs_sb_info *cifs_sb,
351 			       const char *full_path,
352 			       struct cifs_open_info_data *data);
353 	/* query file data from the server */
354 	int (*query_file_info)(const unsigned int xid, struct cifs_tcon *tcon,
355 			       struct cifsFileInfo *cfile, struct cifs_open_info_data *data);
356 	/* query reparse point to determine which type of special file */
357 	int (*query_reparse_point)(const unsigned int xid,
358 				   struct cifs_tcon *tcon,
359 				   struct cifs_sb_info *cifs_sb,
360 				   const char *full_path,
361 				   u32 *tag, struct kvec *rsp,
362 				   int *rsp_buftype);
363 	/* get server index number */
364 	int (*get_srv_inum)(const unsigned int xid, struct cifs_tcon *tcon,
365 			    struct cifs_sb_info *cifs_sb, const char *full_path, u64 *uniqueid,
366 			    struct cifs_open_info_data *data);
367 	/* set size by path */
368 	int (*set_path_size)(const unsigned int, struct cifs_tcon *,
369 			     const char *, __u64, struct cifs_sb_info *, bool);
370 	/* set size by file handle */
371 	int (*set_file_size)(const unsigned int, struct cifs_tcon *,
372 			     struct cifsFileInfo *, __u64, bool);
373 	/* set attributes */
374 	int (*set_file_info)(struct inode *, const char *, FILE_BASIC_INFO *,
375 			     const unsigned int);
376 	int (*set_compression)(const unsigned int, struct cifs_tcon *,
377 			       struct cifsFileInfo *);
378 	/* check if we can send an echo or nor */
379 	bool (*can_echo)(struct TCP_Server_Info *);
380 	/* send echo request */
381 	int (*echo)(struct TCP_Server_Info *);
382 	/* create directory */
383 	int (*posix_mkdir)(const unsigned int xid, struct inode *inode,
384 			umode_t mode, struct cifs_tcon *tcon,
385 			const char *full_path,
386 			struct cifs_sb_info *cifs_sb);
387 	int (*mkdir)(const unsigned int xid, struct inode *inode, umode_t mode,
388 		     struct cifs_tcon *tcon, const char *name,
389 		     struct cifs_sb_info *sb);
390 	/* set info on created directory */
391 	void (*mkdir_setinfo)(struct inode *, const char *,
392 			      struct cifs_sb_info *, struct cifs_tcon *,
393 			      const unsigned int);
394 	/* remove directory */
395 	int (*rmdir)(const unsigned int, struct cifs_tcon *, const char *,
396 		     struct cifs_sb_info *);
397 	/* unlink file */
398 	int (*unlink)(const unsigned int, struct cifs_tcon *, const char *,
399 		      struct cifs_sb_info *);
400 	/* open, rename and delete file */
401 	int (*rename_pending_delete)(const char *, struct dentry *,
402 				     const unsigned int);
403 	/* send rename request */
404 	int (*rename)(const unsigned int xid,
405 		      struct cifs_tcon *tcon,
406 		      struct dentry *source_dentry,
407 		      const char *from_name, const char *to_name,
408 		      struct cifs_sb_info *cifs_sb);
409 	/* send create hardlink request */
410 	int (*create_hardlink)(const unsigned int xid,
411 			       struct cifs_tcon *tcon,
412 			       struct dentry *source_dentry,
413 			       const char *from_name, const char *to_name,
414 			       struct cifs_sb_info *cifs_sb);
415 	/* query symlink target */
416 	int (*query_symlink)(const unsigned int xid,
417 			     struct cifs_tcon *tcon,
418 			     struct cifs_sb_info *cifs_sb,
419 			     const char *full_path,
420 			     char **target_path);
421 	/* open a file for non-posix mounts */
422 	int (*open)(const unsigned int xid, struct cifs_open_parms *oparms, __u32 *oplock,
423 		    void *buf);
424 	/* set fid protocol-specific info */
425 	void (*set_fid)(struct cifsFileInfo *, struct cifs_fid *, __u32);
426 	/* close a file */
427 	void (*close)(const unsigned int, struct cifs_tcon *,
428 		      struct cifs_fid *);
429 	/* close a file, returning file attributes and timestamps */
430 	void (*close_getattr)(const unsigned int xid, struct cifs_tcon *tcon,
431 		      struct cifsFileInfo *pfile_info);
432 	/* send a flush request to the server */
433 	int (*flush)(const unsigned int, struct cifs_tcon *, struct cifs_fid *);
434 	/* async read from the server */
435 	int (*async_readv)(struct cifs_readdata *);
436 	/* async write to the server */
437 	int (*async_writev)(struct cifs_writedata *,
438 			    void (*release)(struct kref *));
439 	/* sync read from the server */
440 	int (*sync_read)(const unsigned int, struct cifs_fid *,
441 			 struct cifs_io_parms *, unsigned int *, char **,
442 			 int *);
443 	/* sync write to the server */
444 	int (*sync_write)(const unsigned int, struct cifs_fid *,
445 			  struct cifs_io_parms *, unsigned int *, struct kvec *,
446 			  unsigned long);
447 	/* open dir, start readdir */
448 	int (*query_dir_first)(const unsigned int, struct cifs_tcon *,
449 			       const char *, struct cifs_sb_info *,
450 			       struct cifs_fid *, __u16,
451 			       struct cifs_search_info *);
452 	/* continue readdir */
453 	int (*query_dir_next)(const unsigned int, struct cifs_tcon *,
454 			      struct cifs_fid *,
455 			      __u16, struct cifs_search_info *srch_inf);
456 	/* close dir */
457 	int (*close_dir)(const unsigned int, struct cifs_tcon *,
458 			 struct cifs_fid *);
459 	/* calculate a size of SMB message */
460 	unsigned int (*calc_smb_size)(void *buf);
461 	/* check for STATUS_PENDING and process the response if yes */
462 	bool (*is_status_pending)(char *buf, struct TCP_Server_Info *server);
463 	/* check for STATUS_NETWORK_SESSION_EXPIRED */
464 	bool (*is_session_expired)(char *);
465 	/* send oplock break response */
466 	int (*oplock_response)(struct cifs_tcon *tcon, __u64 persistent_fid, __u64 volatile_fid,
467 			__u16 net_fid, struct cifsInodeInfo *cifs_inode);
468 	/* query remote filesystem */
469 	int (*queryfs)(const unsigned int, struct cifs_tcon *,
470 		       struct cifs_sb_info *, struct kstatfs *);
471 	/* send mandatory brlock to the server */
472 	int (*mand_lock)(const unsigned int, struct cifsFileInfo *, __u64,
473 			 __u64, __u32, int, int, bool);
474 	/* unlock range of mandatory locks */
475 	int (*mand_unlock_range)(struct cifsFileInfo *, struct file_lock *,
476 				 const unsigned int);
477 	/* push brlocks from the cache to the server */
478 	int (*push_mand_locks)(struct cifsFileInfo *);
479 	/* get lease key of the inode */
480 	void (*get_lease_key)(struct inode *, struct cifs_fid *);
481 	/* set lease key of the inode */
482 	void (*set_lease_key)(struct inode *, struct cifs_fid *);
483 	/* generate new lease key */
484 	void (*new_lease_key)(struct cifs_fid *);
485 	int (*generate_signingkey)(struct cifs_ses *ses,
486 				   struct TCP_Server_Info *server);
487 	int (*calc_signature)(struct smb_rqst *, struct TCP_Server_Info *,
488 				bool allocate_crypto);
489 	int (*set_integrity)(const unsigned int, struct cifs_tcon *tcon,
490 			     struct cifsFileInfo *src_file);
491 	int (*enum_snapshots)(const unsigned int xid, struct cifs_tcon *tcon,
492 			     struct cifsFileInfo *src_file, void __user *);
493 	int (*notify)(const unsigned int xid, struct file *pfile,
494 			     void __user *pbuf, bool return_changes);
495 	int (*query_mf_symlink)(unsigned int, struct cifs_tcon *,
496 				struct cifs_sb_info *, const unsigned char *,
497 				char *, unsigned int *);
498 	int (*create_mf_symlink)(unsigned int, struct cifs_tcon *,
499 				 struct cifs_sb_info *, const unsigned char *,
500 				 char *, unsigned int *);
501 	/* if we can do cache read operations */
502 	bool (*is_read_op)(__u32);
503 	/* set oplock level for the inode */
504 	void (*set_oplock_level)(struct cifsInodeInfo *, __u32, unsigned int,
505 				 bool *);
506 	/* create lease context buffer for CREATE request */
507 	char * (*create_lease_buf)(u8 *lease_key, u8 oplock);
508 	/* parse lease context buffer and return oplock/epoch info */
509 	__u8 (*parse_lease_buf)(void *buf, unsigned int *epoch, char *lkey);
510 	ssize_t (*copychunk_range)(const unsigned int,
511 			struct cifsFileInfo *src_file,
512 			struct cifsFileInfo *target_file,
513 			u64 src_off, u64 len, u64 dest_off);
514 	int (*duplicate_extents)(const unsigned int, struct cifsFileInfo *src,
515 			struct cifsFileInfo *target_file, u64 src_off, u64 len,
516 			u64 dest_off);
517 	int (*validate_negotiate)(const unsigned int, struct cifs_tcon *);
518 	ssize_t (*query_all_EAs)(const unsigned int, struct cifs_tcon *,
519 			const unsigned char *, const unsigned char *, char *,
520 			size_t, struct cifs_sb_info *);
521 	int (*set_EA)(const unsigned int, struct cifs_tcon *, const char *,
522 			const char *, const void *, const __u16,
523 			const struct nls_table *, struct cifs_sb_info *);
524 	struct cifs_ntsd * (*get_acl)(struct cifs_sb_info *, struct inode *,
525 			const char *, u32 *, u32);
526 	struct cifs_ntsd * (*get_acl_by_fid)(struct cifs_sb_info *,
527 			const struct cifs_fid *, u32 *, u32);
528 	int (*set_acl)(struct cifs_ntsd *, __u32, struct inode *, const char *,
529 			int);
530 	/* writepages retry size */
531 	unsigned int (*wp_retry_size)(struct inode *);
532 	/* get mtu credits */
533 	int (*wait_mtu_credits)(struct TCP_Server_Info *, unsigned int,
534 				unsigned int *, struct cifs_credits *);
535 	/* adjust previously taken mtu credits to request size */
536 	int (*adjust_credits)(struct TCP_Server_Info *server,
537 			      struct cifs_credits *credits,
538 			      const unsigned int payload_size);
539 	/* check if we need to issue closedir */
540 	bool (*dir_needs_close)(struct cifsFileInfo *);
541 	long (*fallocate)(struct file *, struct cifs_tcon *, int, loff_t,
542 			  loff_t);
543 	/* init transform request - used for encryption for now */
544 	int (*init_transform_rq)(struct TCP_Server_Info *, int num_rqst,
545 				 struct smb_rqst *, struct smb_rqst *);
546 	int (*is_transform_hdr)(void *buf);
547 	int (*receive_transform)(struct TCP_Server_Info *,
548 				 struct mid_q_entry **, char **, int *);
549 	enum securityEnum (*select_sectype)(struct TCP_Server_Info *,
550 			    enum securityEnum);
551 	int (*next_header)(struct TCP_Server_Info *server, char *buf,
552 			   unsigned int *noff);
553 	/* ioctl passthrough for query_info */
554 	int (*ioctl_query_info)(const unsigned int xid,
555 				struct cifs_tcon *tcon,
556 				struct cifs_sb_info *cifs_sb,
557 				__le16 *path, int is_dir,
558 				unsigned long p);
559 	/* make unix special files (block, char, fifo, socket) */
560 	int (*make_node)(unsigned int xid,
561 			 struct inode *inode,
562 			 struct dentry *dentry,
563 			 struct cifs_tcon *tcon,
564 			 const char *full_path,
565 			 umode_t mode,
566 			 dev_t device_number);
567 	/* version specific fiemap implementation */
568 	int (*fiemap)(struct cifs_tcon *tcon, struct cifsFileInfo *,
569 		      struct fiemap_extent_info *, u64, u64);
570 	/* version specific llseek implementation */
571 	loff_t (*llseek)(struct file *, struct cifs_tcon *, loff_t, int);
572 	/* Check for STATUS_IO_TIMEOUT */
573 	bool (*is_status_io_timeout)(char *buf);
574 	/* Check for STATUS_NETWORK_NAME_DELETED */
575 	bool (*is_network_name_deleted)(char *buf, struct TCP_Server_Info *srv);
576 	int (*parse_reparse_point)(struct cifs_sb_info *cifs_sb,
577 				   struct kvec *rsp_iov,
578 				   struct cifs_open_info_data *data);
579 	int (*create_reparse_symlink)(const unsigned int xid,
580 				      struct inode *inode,
581 				      struct dentry *dentry,
582 				      struct cifs_tcon *tcon,
583 				      const char *full_path,
584 				      const char *symname);
585 };
586 
587 struct smb_version_values {
588 	char		*version_string;
589 	__u16		protocol_id;
590 	__u32		req_capabilities;
591 	__u32		large_lock_type;
592 	__u32		exclusive_lock_type;
593 	__u32		shared_lock_type;
594 	__u32		unlock_lock_type;
595 	size_t		header_preamble_size;
596 	size_t		header_size;
597 	size_t		max_header_size;
598 	size_t		read_rsp_size;
599 	__le16		lock_cmd;
600 	unsigned int	cap_unix;
601 	unsigned int	cap_nt_find;
602 	unsigned int	cap_large_files;
603 	__u16		signing_enabled;
604 	__u16		signing_required;
605 	size_t		create_lease_size;
606 };
607 
608 #define HEADER_SIZE(server) (server->vals->header_size)
609 #define MAX_HEADER_SIZE(server) (server->vals->max_header_size)
610 #define HEADER_PREAMBLE_SIZE(server) (server->vals->header_preamble_size)
611 #define MID_HEADER_SIZE(server) (HEADER_SIZE(server) - 1 - HEADER_PREAMBLE_SIZE(server))
612 
613 /**
614  * CIFS superblock mount flags (mnt_cifs_flags) to consider when
615  * trying to reuse existing superblock for a new mount
616  */
617 #define CIFS_MOUNT_MASK (CIFS_MOUNT_NO_PERM | CIFS_MOUNT_SET_UID | \
618 			 CIFS_MOUNT_SERVER_INUM | CIFS_MOUNT_DIRECT_IO | \
619 			 CIFS_MOUNT_NO_XATTR | CIFS_MOUNT_MAP_SPECIAL_CHR | \
620 			 CIFS_MOUNT_MAP_SFM_CHR | \
621 			 CIFS_MOUNT_UNX_EMUL | CIFS_MOUNT_NO_BRL | \
622 			 CIFS_MOUNT_CIFS_ACL | CIFS_MOUNT_OVERR_UID | \
623 			 CIFS_MOUNT_OVERR_GID | CIFS_MOUNT_DYNPERM | \
624 			 CIFS_MOUNT_NOPOSIXBRL | CIFS_MOUNT_NOSSYNC | \
625 			 CIFS_MOUNT_FSCACHE | CIFS_MOUNT_MF_SYMLINKS | \
626 			 CIFS_MOUNT_MULTIUSER | CIFS_MOUNT_STRICT_IO | \
627 			 CIFS_MOUNT_CIFS_BACKUPUID | CIFS_MOUNT_CIFS_BACKUPGID | \
628 			 CIFS_MOUNT_UID_FROM_ACL | CIFS_MOUNT_NO_HANDLE_CACHE | \
629 			 CIFS_MOUNT_NO_DFS | CIFS_MOUNT_MODE_FROM_SID | \
630 			 CIFS_MOUNT_RO_CACHE | CIFS_MOUNT_RW_CACHE)
631 
632 /**
633  * Generic VFS superblock mount flags (s_flags) to consider when
634  * trying to reuse existing superblock for a new mount
635  */
636 #define CIFS_MS_MASK (SB_RDONLY | SB_MANDLOCK | SB_NOEXEC | SB_NOSUID | \
637 		      SB_NODEV | SB_SYNCHRONOUS)
638 
639 struct cifs_mnt_data {
640 	struct cifs_sb_info *cifs_sb;
641 	struct smb3_fs_context *ctx;
642 	int flags;
643 };
644 
645 static inline unsigned int
646 get_rfc1002_length(void *buf)
647 {
648 	return be32_to_cpu(*((__be32 *)buf)) & 0xffffff;
649 }
650 
651 static inline void
652 inc_rfc1001_len(void *buf, int count)
653 {
654 	be32_add_cpu((__be32 *)buf, count);
655 }
656 
657 struct TCP_Server_Info {
658 	struct list_head tcp_ses_list;
659 	struct list_head smb_ses_list;
660 	spinlock_t srv_lock;  /* protect anything here that is not protected */
661 	__u64 conn_id; /* connection identifier (useful for debugging) */
662 	int srv_count; /* reference counter */
663 	/* 15 character server name + 0x20 16th byte indicating type = srv */
664 	char server_RFC1001_name[RFC1001_NAME_LEN_WITH_NULL];
665 	struct smb_version_operations	*ops;
666 	struct smb_version_values	*vals;
667 	/* updates to tcpStatus protected by cifs_tcp_ses_lock */
668 	enum statusEnum tcpStatus; /* what we think the status is */
669 	char *hostname; /* hostname portion of UNC string */
670 	struct socket *ssocket;
671 	struct sockaddr_storage dstaddr;
672 	struct sockaddr_storage srcaddr; /* locally bind to this IP */
673 #ifdef CONFIG_NET_NS
674 	struct net *net;
675 #endif
676 	wait_queue_head_t response_q;
677 	wait_queue_head_t request_q; /* if more than maxmpx to srvr must block*/
678 	spinlock_t mid_lock;  /* protect mid queue and it's entries */
679 	struct list_head pending_mid_q;
680 	bool noblocksnd;		/* use blocking sendmsg */
681 	bool noautotune;		/* do not autotune send buf sizes */
682 	bool nosharesock;
683 	bool tcp_nodelay;
684 	bool terminate;
685 	unsigned int credits;  /* send no more requests at once */
686 	unsigned int max_credits; /* can override large 32000 default at mnt */
687 	unsigned int in_flight;  /* number of requests on the wire to server */
688 	unsigned int max_in_flight; /* max number of requests that were on wire */
689 	spinlock_t req_lock;  /* protect the two values above */
690 	struct mutex _srv_mutex;
691 	unsigned int nofs_flag;
692 	struct task_struct *tsk;
693 	char server_GUID[16];
694 	__u16 sec_mode;
695 	bool sign; /* is signing enabled on this connection? */
696 	bool ignore_signature:1; /* skip validation of signatures in SMB2/3 rsp */
697 	bool session_estab; /* mark when very first sess is established */
698 	int echo_credits;  /* echo reserved slots */
699 	int oplock_credits;  /* oplock break reserved slots */
700 	bool echoes:1; /* enable echoes */
701 	__u8 client_guid[SMB2_CLIENT_GUID_SIZE]; /* Client GUID */
702 	u16 dialect; /* dialect index that server chose */
703 	bool oplocks:1; /* enable oplocks */
704 	unsigned int maxReq;	/* Clients should submit no more */
705 	/* than maxReq distinct unanswered SMBs to the server when using  */
706 	/* multiplexed reads or writes (for SMB1/CIFS only, not SMB2/SMB3) */
707 	unsigned int maxBuf;	/* maxBuf specifies the maximum */
708 	/* message size the server can send or receive for non-raw SMBs */
709 	/* maxBuf is returned by SMB NegotiateProtocol so maxBuf is only 0 */
710 	/* when socket is setup (and during reconnect) before NegProt sent */
711 	unsigned int max_rw;	/* maxRw specifies the maximum */
712 	/* message size the server can send or receive for */
713 	/* SMB_COM_WRITE_RAW or SMB_COM_READ_RAW. */
714 	unsigned int capabilities; /* selective disabling of caps by smb sess */
715 	int timeAdj;  /* Adjust for difference in server time zone in sec */
716 	__u64 CurrentMid;         /* multiplex id - rotating counter, protected by GlobalMid_Lock */
717 	char cryptkey[CIFS_CRYPTO_KEY_SIZE]; /* used by ntlm, ntlmv2 etc */
718 	/* 16th byte of RFC1001 workstation name is always null */
719 	char workstation_RFC1001_name[RFC1001_NAME_LEN_WITH_NULL];
720 	__u32 sequence_number; /* for signing, protected by srv_mutex */
721 	__u32 reconnect_instance; /* incremented on each reconnect */
722 	struct session_key session_key;
723 	unsigned long lstrp; /* when we got last response from this server */
724 	struct cifs_secmech secmech; /* crypto sec mech functs, descriptors */
725 #define	CIFS_NEGFLAVOR_UNENCAP	1	/* wct == 17, but no ext_sec */
726 #define	CIFS_NEGFLAVOR_EXTENDED	2	/* wct == 17, ext_sec bit set */
727 	char	negflavor;	/* NEGOTIATE response flavor */
728 	/* extended security flavors that server supports */
729 	bool	sec_ntlmssp;		/* supports NTLMSSP */
730 	bool	sec_kerberosu2u;	/* supports U2U Kerberos */
731 	bool	sec_kerberos;		/* supports plain Kerberos */
732 	bool	sec_mskerberos;		/* supports legacy MS Kerberos */
733 	bool	large_buf;		/* is current buffer large? */
734 	/* use SMBD connection instead of socket */
735 	bool	rdma;
736 	/* point to the SMBD connection if RDMA is used instead of socket */
737 	struct smbd_connection *smbd_conn;
738 	struct delayed_work	echo; /* echo ping workqueue job */
739 	char	*smallbuf;	/* pointer to current "small" buffer */
740 	char	*bigbuf;	/* pointer to current "big" buffer */
741 	/* Total size of this PDU. Only valid from cifs_demultiplex_thread */
742 	unsigned int pdu_size;
743 	unsigned int total_read; /* total amount of data read in this pass */
744 	atomic_t in_send; /* requests trying to send */
745 	atomic_t num_waiters;   /* blocked waiting to get in sendrecv */
746 #ifdef CONFIG_CIFS_STATS2
747 	atomic_t num_cmds[NUMBER_OF_SMB2_COMMANDS]; /* total requests by cmd */
748 	atomic_t smb2slowcmd[NUMBER_OF_SMB2_COMMANDS]; /* count resps > 1 sec */
749 	__u64 time_per_cmd[NUMBER_OF_SMB2_COMMANDS]; /* total time per cmd */
750 	__u32 slowest_cmd[NUMBER_OF_SMB2_COMMANDS];
751 	__u32 fastest_cmd[NUMBER_OF_SMB2_COMMANDS];
752 #endif /* STATS2 */
753 	unsigned int	max_read;
754 	unsigned int	max_write;
755 	unsigned int	min_offload;
756 	unsigned int	retrans;
757 	__le16	compress_algorithm;
758 	__u16	signing_algorithm;
759 	__le16	cipher_type;
760 	 /* save initital negprot hash */
761 	__u8	preauth_sha_hash[SMB2_PREAUTH_HASH_SIZE];
762 	bool	signing_negotiated; /* true if valid signing context rcvd from server */
763 	bool	posix_ext_supported;
764 	struct delayed_work reconnect; /* reconnect workqueue job */
765 	struct mutex reconnect_mutex; /* prevent simultaneous reconnects */
766 	unsigned long echo_interval;
767 
768 	/*
769 	 * Number of targets available for reconnect. The more targets
770 	 * the more tasks have to wait to let the demultiplex thread
771 	 * reconnect.
772 	 */
773 	int nr_targets;
774 	bool noblockcnt; /* use non-blocking connect() */
775 
776 	/*
777 	 * If this is a session channel,
778 	 * primary_server holds the ref-counted
779 	 * pointer to primary channel connection for the session.
780 	 */
781 #define SERVER_IS_CHAN(server)	(!!(server)->primary_server)
782 	struct TCP_Server_Info *primary_server;
783 	__u16 channel_sequence_num;  /* incremented on primary channel on each chan reconnect */
784 
785 #ifdef CONFIG_CIFS_SWN_UPCALL
786 	bool use_swn_dstaddr;
787 	struct sockaddr_storage swn_dstaddr;
788 #endif
789 	struct mutex refpath_lock; /* protects leaf_fullpath */
790 	/*
791 	 * leaf_fullpath: Canonical DFS referral path related to this
792 	 *                connection.
793 	 *                It is used in DFS cache refresher, reconnect and may
794 	 *                change due to nested DFS links.
795 	 *
796 	 * Protected by @refpath_lock and @srv_lock.  The @refpath_lock is
797 	 * mostly used for not requiring a copy of @leaf_fullpath when getting
798 	 * cached or new DFS referrals (which might also sleep during I/O).
799 	 * While @srv_lock is held for making string and NULL comparions against
800 	 * both fields as in mount(2) and cache refresh.
801 	 *
802 	 * format: \\HOST\SHARE[\OPTIONAL PATH]
803 	 */
804 	char *leaf_fullpath;
805 };
806 
807 static inline bool is_smb1(struct TCP_Server_Info *server)
808 {
809 	return HEADER_PREAMBLE_SIZE(server) != 0;
810 }
811 
812 static inline void cifs_server_lock(struct TCP_Server_Info *server)
813 {
814 	unsigned int nofs_flag = memalloc_nofs_save();
815 
816 	mutex_lock(&server->_srv_mutex);
817 	server->nofs_flag = nofs_flag;
818 }
819 
820 static inline void cifs_server_unlock(struct TCP_Server_Info *server)
821 {
822 	unsigned int nofs_flag = server->nofs_flag;
823 
824 	mutex_unlock(&server->_srv_mutex);
825 	memalloc_nofs_restore(nofs_flag);
826 }
827 
828 struct cifs_credits {
829 	unsigned int value;
830 	unsigned int instance;
831 };
832 
833 static inline unsigned int
834 in_flight(struct TCP_Server_Info *server)
835 {
836 	unsigned int num;
837 
838 	spin_lock(&server->req_lock);
839 	num = server->in_flight;
840 	spin_unlock(&server->req_lock);
841 	return num;
842 }
843 
844 static inline bool
845 has_credits(struct TCP_Server_Info *server, int *credits, int num_credits)
846 {
847 	int num;
848 
849 	spin_lock(&server->req_lock);
850 	num = *credits;
851 	spin_unlock(&server->req_lock);
852 	return num >= num_credits;
853 }
854 
855 static inline void
856 add_credits(struct TCP_Server_Info *server, const struct cifs_credits *credits,
857 	    const int optype)
858 {
859 	server->ops->add_credits(server, credits, optype);
860 }
861 
862 static inline void
863 add_credits_and_wake_if(struct TCP_Server_Info *server,
864 			const struct cifs_credits *credits, const int optype)
865 {
866 	if (credits->value) {
867 		server->ops->add_credits(server, credits, optype);
868 		wake_up(&server->request_q);
869 	}
870 }
871 
872 static inline void
873 set_credits(struct TCP_Server_Info *server, const int val)
874 {
875 	server->ops->set_credits(server, val);
876 }
877 
878 static inline int
879 adjust_credits(struct TCP_Server_Info *server, struct cifs_credits *credits,
880 	       const unsigned int payload_size)
881 {
882 	return server->ops->adjust_credits ?
883 		server->ops->adjust_credits(server, credits, payload_size) : 0;
884 }
885 
886 static inline __le64
887 get_next_mid64(struct TCP_Server_Info *server)
888 {
889 	return cpu_to_le64(server->ops->get_next_mid(server));
890 }
891 
892 static inline __le16
893 get_next_mid(struct TCP_Server_Info *server)
894 {
895 	__u16 mid = server->ops->get_next_mid(server);
896 	/*
897 	 * The value in the SMB header should be little endian for easy
898 	 * on-the-wire decoding.
899 	 */
900 	return cpu_to_le16(mid);
901 }
902 
903 static inline void
904 revert_current_mid(struct TCP_Server_Info *server, const unsigned int val)
905 {
906 	if (server->ops->revert_current_mid)
907 		server->ops->revert_current_mid(server, val);
908 }
909 
910 static inline void
911 revert_current_mid_from_hdr(struct TCP_Server_Info *server,
912 			    const struct smb2_hdr *shdr)
913 {
914 	unsigned int num = le16_to_cpu(shdr->CreditCharge);
915 
916 	return revert_current_mid(server, num > 0 ? num : 1);
917 }
918 
919 static inline __u16
920 get_mid(const struct smb_hdr *smb)
921 {
922 	return le16_to_cpu(smb->Mid);
923 }
924 
925 static inline bool
926 compare_mid(__u16 mid, const struct smb_hdr *smb)
927 {
928 	return mid == le16_to_cpu(smb->Mid);
929 }
930 
931 /*
932  * When the server supports very large reads and writes via POSIX extensions,
933  * we can allow up to 2^24-1, minus the size of a READ/WRITE_AND_X header, not
934  * including the RFC1001 length.
935  *
936  * Note that this might make for "interesting" allocation problems during
937  * writeback however as we have to allocate an array of pointers for the
938  * pages. A 16M write means ~32kb page array with PAGE_SIZE == 4096.
939  *
940  * For reads, there is a similar problem as we need to allocate an array
941  * of kvecs to handle the receive, though that should only need to be done
942  * once.
943  */
944 #define CIFS_MAX_WSIZE ((1<<24) - 1 - sizeof(WRITE_REQ) + 4)
945 #define CIFS_MAX_RSIZE ((1<<24) - sizeof(READ_RSP) + 4)
946 
947 /*
948  * When the server doesn't allow large posix writes, only allow a rsize/wsize
949  * of 2^17-1 minus the size of the call header. That allows for a read or
950  * write up to the maximum size described by RFC1002.
951  */
952 #define CIFS_MAX_RFC1002_WSIZE ((1<<17) - 1 - sizeof(WRITE_REQ) + 4)
953 #define CIFS_MAX_RFC1002_RSIZE ((1<<17) - 1 - sizeof(READ_RSP) + 4)
954 
955 #define CIFS_DEFAULT_IOSIZE (1024 * 1024)
956 
957 /*
958  * Windows only supports a max of 60kb reads and 65535 byte writes. Default to
959  * those values when posix extensions aren't in force. In actuality here, we
960  * use 65536 to allow for a write that is a multiple of 4k. Most servers seem
961  * to be ok with the extra byte even though Windows doesn't send writes that
962  * are that large.
963  *
964  * Citation:
965  *
966  * https://blogs.msdn.com/b/openspecification/archive/2009/04/10/smb-maximum-transmit-buffer-size-and-performance-tuning.aspx
967  */
968 #define CIFS_DEFAULT_NON_POSIX_RSIZE (60 * 1024)
969 #define CIFS_DEFAULT_NON_POSIX_WSIZE (65536)
970 
971 /*
972  * Macros to allow the TCP_Server_Info->net field and related code to drop out
973  * when CONFIG_NET_NS isn't set.
974  */
975 
976 #ifdef CONFIG_NET_NS
977 
978 static inline struct net *cifs_net_ns(struct TCP_Server_Info *srv)
979 {
980 	return srv->net;
981 }
982 
983 static inline void cifs_set_net_ns(struct TCP_Server_Info *srv, struct net *net)
984 {
985 	srv->net = net;
986 }
987 
988 #else
989 
990 static inline struct net *cifs_net_ns(struct TCP_Server_Info *srv)
991 {
992 	return &init_net;
993 }
994 
995 static inline void cifs_set_net_ns(struct TCP_Server_Info *srv, struct net *net)
996 {
997 }
998 
999 #endif
1000 
1001 struct cifs_server_iface {
1002 	struct list_head iface_head;
1003 	struct kref refcount;
1004 	size_t speed;
1005 	size_t weight_fulfilled;
1006 	unsigned int num_channels;
1007 	unsigned int rdma_capable : 1;
1008 	unsigned int rss_capable : 1;
1009 	unsigned int is_active : 1; /* unset if non existent */
1010 	struct sockaddr_storage sockaddr;
1011 };
1012 
1013 /* release iface when last ref is dropped */
1014 static inline void
1015 release_iface(struct kref *ref)
1016 {
1017 	struct cifs_server_iface *iface = container_of(ref,
1018 						       struct cifs_server_iface,
1019 						       refcount);
1020 	kfree(iface);
1021 }
1022 
1023 struct cifs_chan {
1024 	unsigned int in_reconnect : 1; /* if session setup in progress for this channel */
1025 	struct TCP_Server_Info *server;
1026 	struct cifs_server_iface *iface; /* interface in use */
1027 	__u8 signkey[SMB3_SIGN_KEY_SIZE];
1028 };
1029 
1030 /*
1031  * Session structure.  One of these for each uid session with a particular host
1032  */
1033 struct cifs_ses {
1034 	struct list_head smb_ses_list;
1035 	struct list_head rlist; /* reconnect list */
1036 	struct list_head tcon_list;
1037 	struct cifs_tcon *tcon_ipc;
1038 	spinlock_t ses_lock;  /* protect anything here that is not protected */
1039 	struct mutex session_mutex;
1040 	struct TCP_Server_Info *server;	/* pointer to server info */
1041 	int ses_count;		/* reference counter */
1042 	enum ses_status_enum ses_status;  /* updates protected by cifs_tcp_ses_lock */
1043 	unsigned int overrideSecFlg; /* if non-zero override global sec flags */
1044 	char *serverOS;		/* name of operating system underlying server */
1045 	char *serverNOS;	/* name of network operating system of server */
1046 	char *serverDomain;	/* security realm of server */
1047 	__u64 Suid;		/* remote smb uid  */
1048 	kuid_t linux_uid;	/* overriding owner of files on the mount */
1049 	kuid_t cred_uid;	/* owner of credentials */
1050 	unsigned int capabilities;
1051 	char ip_addr[INET6_ADDRSTRLEN + 1]; /* Max ipv6 (or v4) addr string len */
1052 	char *user_name;	/* must not be null except during init of sess
1053 				   and after mount option parsing we fill it */
1054 	char *domainName;
1055 	char *password;
1056 	char workstation_name[CIFS_MAX_WORKSTATION_LEN];
1057 	struct session_key auth_key;
1058 	struct ntlmssp_auth *ntlmssp; /* ciphertext, flags, server challenge */
1059 	enum securityEnum sectype; /* what security flavor was specified? */
1060 	bool sign;		/* is signing required? */
1061 	bool domainAuto:1;
1062 	__u16 session_flags;
1063 	__u8 smb3signingkey[SMB3_SIGN_KEY_SIZE];
1064 	__u8 smb3encryptionkey[SMB3_ENC_DEC_KEY_SIZE];
1065 	__u8 smb3decryptionkey[SMB3_ENC_DEC_KEY_SIZE];
1066 	__u8 preauth_sha_hash[SMB2_PREAUTH_HASH_SIZE];
1067 
1068 	/*
1069 	 * Network interfaces available on the server this session is
1070 	 * connected to.
1071 	 *
1072 	 * Other channels can be opened by connecting and binding this
1073 	 * session to interfaces from this list.
1074 	 *
1075 	 * iface_lock should be taken when accessing any of these fields
1076 	 */
1077 	spinlock_t iface_lock;
1078 	/* ========= begin: protected by iface_lock ======== */
1079 	struct list_head iface_list;
1080 	size_t iface_count;
1081 	unsigned long iface_last_update; /* jiffies */
1082 	/* ========= end: protected by iface_lock ======== */
1083 
1084 	spinlock_t chan_lock;
1085 	/* ========= begin: protected by chan_lock ======== */
1086 #define CIFS_MAX_CHANNELS 16
1087 #define CIFS_INVAL_CHAN_INDEX (-1)
1088 #define CIFS_ALL_CHANNELS_SET(ses)	\
1089 	((1UL << (ses)->chan_count) - 1)
1090 #define CIFS_ALL_CHANS_GOOD(ses)		\
1091 	(!(ses)->chans_need_reconnect)
1092 #define CIFS_ALL_CHANS_NEED_RECONNECT(ses)	\
1093 	((ses)->chans_need_reconnect == CIFS_ALL_CHANNELS_SET(ses))
1094 #define CIFS_SET_ALL_CHANS_NEED_RECONNECT(ses)	\
1095 	((ses)->chans_need_reconnect = CIFS_ALL_CHANNELS_SET(ses))
1096 #define CIFS_CHAN_NEEDS_RECONNECT(ses, index)	\
1097 	test_bit((index), &(ses)->chans_need_reconnect)
1098 #define CIFS_CHAN_IN_RECONNECT(ses, index)	\
1099 	((ses)->chans[(index)].in_reconnect)
1100 
1101 	struct cifs_chan chans[CIFS_MAX_CHANNELS];
1102 	size_t chan_count;
1103 	size_t chan_max;
1104 	atomic_t chan_seq; /* round robin state */
1105 
1106 	/*
1107 	 * chans_need_reconnect is a bitmap indicating which of the channels
1108 	 * under this smb session needs to be reconnected.
1109 	 * If not multichannel session, only one bit will be used.
1110 	 *
1111 	 * We will ask for sess and tcon reconnection only if all the
1112 	 * channels are marked for needing reconnection. This will
1113 	 * enable the sessions on top to continue to live till any
1114 	 * of the channels below are active.
1115 	 */
1116 	unsigned long chans_need_reconnect;
1117 	/* ========= end: protected by chan_lock ======== */
1118 	struct cifs_ses *dfs_root_ses;
1119 	struct nls_table *local_nls;
1120 };
1121 
1122 static inline bool
1123 cap_unix(struct cifs_ses *ses)
1124 {
1125 	return ses->server->vals->cap_unix & ses->capabilities;
1126 }
1127 
1128 /*
1129  * common struct for holding inode info when searching for or updating an
1130  * inode with new info
1131  */
1132 
1133 #define CIFS_FATTR_JUNCTION		0x1
1134 #define CIFS_FATTR_DELETE_PENDING	0x2
1135 #define CIFS_FATTR_NEED_REVAL		0x4
1136 #define CIFS_FATTR_INO_COLLISION	0x8
1137 #define CIFS_FATTR_UNKNOWN_NLINK	0x10
1138 #define CIFS_FATTR_FAKE_ROOT_INO	0x20
1139 
1140 struct cifs_fattr {
1141 	u32		cf_flags;
1142 	u32		cf_cifsattrs;
1143 	u64		cf_uniqueid;
1144 	u64		cf_eof;
1145 	u64		cf_bytes;
1146 	u64		cf_createtime;
1147 	kuid_t		cf_uid;
1148 	kgid_t		cf_gid;
1149 	umode_t		cf_mode;
1150 	dev_t		cf_rdev;
1151 	unsigned int	cf_nlink;
1152 	unsigned int	cf_dtype;
1153 	struct timespec64 cf_atime;
1154 	struct timespec64 cf_mtime;
1155 	struct timespec64 cf_ctime;
1156 	u32             cf_cifstag;
1157 	char            *cf_symlink_target;
1158 };
1159 
1160 /*
1161  * there is one of these for each connection to a resource on a particular
1162  * session
1163  */
1164 struct cifs_tcon {
1165 	struct list_head tcon_list;
1166 	int tc_count;
1167 	struct list_head rlist; /* reconnect list */
1168 	spinlock_t tc_lock;  /* protect anything here that is not protected */
1169 	atomic_t num_local_opens;  /* num of all opens including disconnected */
1170 	atomic_t num_remote_opens; /* num of all network opens on server */
1171 	struct list_head openFileList;
1172 	spinlock_t open_file_lock; /* protects list above */
1173 	struct cifs_ses *ses;	/* pointer to session associated with */
1174 	char tree_name[MAX_TREE_SIZE + 1]; /* UNC name of resource in ASCII */
1175 	char *nativeFileSystem;
1176 	char *password;		/* for share-level security */
1177 	__u32 tid;		/* The 4 byte tree id */
1178 	__u16 Flags;		/* optional support bits */
1179 	enum tid_status_enum status;
1180 	atomic_t num_smbs_sent;
1181 	union {
1182 		struct {
1183 			atomic_t num_writes;
1184 			atomic_t num_reads;
1185 			atomic_t num_flushes;
1186 			atomic_t num_oplock_brks;
1187 			atomic_t num_opens;
1188 			atomic_t num_closes;
1189 			atomic_t num_deletes;
1190 			atomic_t num_mkdirs;
1191 			atomic_t num_posixopens;
1192 			atomic_t num_posixmkdirs;
1193 			atomic_t num_rmdirs;
1194 			atomic_t num_renames;
1195 			atomic_t num_t2renames;
1196 			atomic_t num_ffirst;
1197 			atomic_t num_fnext;
1198 			atomic_t num_fclose;
1199 			atomic_t num_hardlinks;
1200 			atomic_t num_symlinks;
1201 			atomic_t num_locks;
1202 			atomic_t num_acl_get;
1203 			atomic_t num_acl_set;
1204 		} cifs_stats;
1205 		struct {
1206 			atomic_t smb2_com_sent[NUMBER_OF_SMB2_COMMANDS];
1207 			atomic_t smb2_com_failed[NUMBER_OF_SMB2_COMMANDS];
1208 		} smb2_stats;
1209 	} stats;
1210 	__u64    bytes_read;
1211 	__u64    bytes_written;
1212 	spinlock_t stat_lock;  /* protects the two fields above */
1213 	time64_t stats_from_time;
1214 	FILE_SYSTEM_DEVICE_INFO fsDevInfo;
1215 	FILE_SYSTEM_ATTRIBUTE_INFO fsAttrInfo; /* ok if fs name truncated */
1216 	FILE_SYSTEM_UNIX_INFO fsUnixInfo;
1217 	bool ipc:1;   /* set if connection to IPC$ share (always also pipe) */
1218 	bool pipe:1;  /* set if connection to pipe share */
1219 	bool print:1; /* set if connection to printer share */
1220 	bool retry:1;
1221 	bool nocase:1;
1222 	bool nohandlecache:1; /* if strange server resource prob can turn off */
1223 	bool nodelete:1;
1224 	bool seal:1;      /* transport encryption for this mounted share */
1225 	bool unix_ext:1;  /* if false disable Linux extensions to CIFS protocol
1226 				for this mount even if server would support */
1227 	bool posix_extensions; /* if true SMB3.11 posix extensions enabled */
1228 	bool local_lease:1; /* check leases (only) on local system not remote */
1229 	bool broken_posix_open; /* e.g. Samba server versions < 3.3.2, 3.2.9 */
1230 	bool broken_sparse_sup; /* if server or share does not support sparse */
1231 	bool need_reconnect:1; /* connection reset, tid now invalid */
1232 	bool need_reopen_files:1; /* need to reopen tcon file handles */
1233 	bool use_resilient:1; /* use resilient instead of durable handles */
1234 	bool use_persistent:1; /* use persistent instead of durable handles */
1235 	bool no_lease:1;    /* Do not request leases on files or directories */
1236 	bool use_witness:1; /* use witness protocol */
1237 	__le32 capabilities;
1238 	__u32 share_flags;
1239 	__u32 maximal_access;
1240 	__u32 vol_serial_number;
1241 	__le64 vol_create_time;
1242 	__u64 snapshot_time; /* for timewarp tokens - timestamp of snapshot */
1243 	__u32 handle_timeout; /* persistent and durable handle timeout in ms */
1244 	__u32 ss_flags;		/* sector size flags */
1245 	__u32 perf_sector_size; /* best sector size for perf */
1246 	__u32 max_chunks;
1247 	__u32 max_bytes_chunk;
1248 	__u32 max_bytes_copy;
1249 	__u32 max_cached_dirs;
1250 #ifdef CONFIG_CIFS_FSCACHE
1251 	u64 resource_id;		/* server resource id */
1252 	struct fscache_volume *fscache;	/* cookie for share */
1253 #endif
1254 	struct list_head pending_opens;	/* list of incomplete opens */
1255 	struct cached_fids *cfids;
1256 	/* BB add field for back pointer to sb struct(s)? */
1257 #ifdef CONFIG_CIFS_DFS_UPCALL
1258 	struct list_head dfs_ses_list;
1259 	struct delayed_work dfs_cache_work;
1260 #endif
1261 	struct delayed_work	query_interfaces; /* query interfaces workqueue job */
1262 	char *origin_fullpath; /* canonical copy of smb3_fs_context::source */
1263 };
1264 
1265 /*
1266  * This is a refcounted and timestamped container for a tcon pointer. The
1267  * container holds a tcon reference. It is considered safe to free one of
1268  * these when the tl_count goes to 0. The tl_time is the time of the last
1269  * "get" on the container.
1270  */
1271 struct tcon_link {
1272 	struct rb_node		tl_rbnode;
1273 	kuid_t			tl_uid;
1274 	unsigned long		tl_flags;
1275 #define TCON_LINK_MASTER	0
1276 #define TCON_LINK_PENDING	1
1277 #define TCON_LINK_IN_TREE	2
1278 	unsigned long		tl_time;
1279 	atomic_t		tl_count;
1280 	struct cifs_tcon	*tl_tcon;
1281 };
1282 
1283 extern struct tcon_link *cifs_sb_tlink(struct cifs_sb_info *cifs_sb);
1284 extern void smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst);
1285 
1286 static inline struct cifs_tcon *
1287 tlink_tcon(struct tcon_link *tlink)
1288 {
1289 	return tlink->tl_tcon;
1290 }
1291 
1292 static inline struct tcon_link *
1293 cifs_sb_master_tlink(struct cifs_sb_info *cifs_sb)
1294 {
1295 	return cifs_sb->master_tlink;
1296 }
1297 
1298 extern void cifs_put_tlink(struct tcon_link *tlink);
1299 
1300 static inline struct tcon_link *
1301 cifs_get_tlink(struct tcon_link *tlink)
1302 {
1303 	if (tlink && !IS_ERR(tlink))
1304 		atomic_inc(&tlink->tl_count);
1305 	return tlink;
1306 }
1307 
1308 /* This function is always expected to succeed */
1309 extern struct cifs_tcon *cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb);
1310 
1311 #define CIFS_OPLOCK_NO_CHANGE 0xfe
1312 
1313 struct cifs_pending_open {
1314 	struct list_head olist;
1315 	struct tcon_link *tlink;
1316 	__u8 lease_key[16];
1317 	__u32 oplock;
1318 };
1319 
1320 struct cifs_deferred_close {
1321 	struct list_head dlist;
1322 	struct tcon_link *tlink;
1323 	__u16  netfid;
1324 	__u64  persistent_fid;
1325 	__u64  volatile_fid;
1326 };
1327 
1328 /*
1329  * This info hangs off the cifsFileInfo structure, pointed to by llist.
1330  * This is used to track byte stream locks on the file
1331  */
1332 struct cifsLockInfo {
1333 	struct list_head llist;	/* pointer to next cifsLockInfo */
1334 	struct list_head blist; /* pointer to locks blocked on this */
1335 	wait_queue_head_t block_q;
1336 	__u64 offset;
1337 	__u64 length;
1338 	__u32 pid;
1339 	__u16 type;
1340 	__u16 flags;
1341 };
1342 
1343 /*
1344  * One of these for each open instance of a file
1345  */
1346 struct cifs_search_info {
1347 	loff_t index_of_last_entry;
1348 	__u16 entries_in_buffer;
1349 	__u16 info_level;
1350 	__u32 resume_key;
1351 	char *ntwrk_buf_start;
1352 	char *srch_entries_start;
1353 	char *last_entry;
1354 	const char *presume_name;
1355 	unsigned int resume_name_len;
1356 	bool endOfSearch:1;
1357 	bool emptyDir:1;
1358 	bool unicode:1;
1359 	bool smallBuf:1; /* so we know which buf_release function to call */
1360 };
1361 
1362 #define ACL_NO_MODE	((umode_t)(-1))
1363 struct cifs_open_parms {
1364 	struct cifs_tcon *tcon;
1365 	struct cifs_sb_info *cifs_sb;
1366 	int disposition;
1367 	int desired_access;
1368 	int create_options;
1369 	const char *path;
1370 	struct cifs_fid *fid;
1371 	umode_t mode;
1372 	bool reconnect:1;
1373 };
1374 
1375 struct cifs_fid {
1376 	__u16 netfid;
1377 	__u64 persistent_fid;	/* persist file id for smb2 */
1378 	__u64 volatile_fid;	/* volatile file id for smb2 */
1379 	__u8 lease_key[SMB2_LEASE_KEY_SIZE];	/* lease key for smb2 */
1380 	__u8 create_guid[16];
1381 	__u32 access;
1382 	struct cifs_pending_open *pending_open;
1383 	unsigned int epoch;
1384 #ifdef CONFIG_CIFS_DEBUG2
1385 	__u64 mid;
1386 #endif /* CIFS_DEBUG2 */
1387 	bool purge_cache;
1388 };
1389 
1390 struct cifs_fid_locks {
1391 	struct list_head llist;
1392 	struct cifsFileInfo *cfile;	/* fid that owns locks */
1393 	struct list_head locks;		/* locks held by fid above */
1394 };
1395 
1396 struct cifsFileInfo {
1397 	/* following two lists are protected by tcon->open_file_lock */
1398 	struct list_head tlist;	/* pointer to next fid owned by tcon */
1399 	struct list_head flist;	/* next fid (file instance) for this inode */
1400 	/* lock list below protected by cifsi->lock_sem */
1401 	struct cifs_fid_locks *llist;	/* brlocks held by this fid */
1402 	kuid_t uid;		/* allows finding which FileInfo structure */
1403 	__u32 pid;		/* process id who opened file */
1404 	struct cifs_fid fid;	/* file id from remote */
1405 	struct list_head rlist; /* reconnect list */
1406 	/* BB add lock scope info here if needed */
1407 	/* lock scope id (0 if none) */
1408 	struct dentry *dentry;
1409 	struct tcon_link *tlink;
1410 	unsigned int f_flags;
1411 	bool invalidHandle:1;	/* file closed via session abend */
1412 	bool swapfile:1;
1413 	bool oplock_break_cancelled:1;
1414 	unsigned int oplock_epoch; /* epoch from the lease break */
1415 	__u32 oplock_level; /* oplock/lease level from the lease break */
1416 	int count;
1417 	spinlock_t file_info_lock; /* protects four flag/count fields above */
1418 	struct mutex fh_mutex; /* prevents reopen race after dead ses*/
1419 	struct cifs_search_info srch_inf;
1420 	struct work_struct oplock_break; /* work for oplock breaks */
1421 	struct work_struct put; /* work for the final part of _put */
1422 	struct delayed_work deferred;
1423 	bool deferred_close_scheduled; /* Flag to indicate close is scheduled */
1424 	char *symlink_target;
1425 };
1426 
1427 struct cifs_io_parms {
1428 	__u16 netfid;
1429 	__u64 persistent_fid;	/* persist file id for smb2 */
1430 	__u64 volatile_fid;	/* volatile file id for smb2 */
1431 	__u32 pid;
1432 	__u64 offset;
1433 	unsigned int length;
1434 	struct cifs_tcon *tcon;
1435 	struct TCP_Server_Info *server;
1436 };
1437 
1438 struct cifs_aio_ctx {
1439 	struct kref		refcount;
1440 	struct list_head	list;
1441 	struct mutex		aio_mutex;
1442 	struct completion	done;
1443 	struct iov_iter		iter;
1444 	struct kiocb		*iocb;
1445 	struct cifsFileInfo	*cfile;
1446 	struct bio_vec		*bv;
1447 	loff_t			pos;
1448 	unsigned int		nr_pinned_pages;
1449 	ssize_t			rc;
1450 	unsigned int		len;
1451 	unsigned int		total_len;
1452 	unsigned int		bv_need_unpin;	/* If ->bv[] needs unpinning */
1453 	bool			should_dirty;
1454 	/*
1455 	 * Indicates if this aio_ctx is for direct_io,
1456 	 * If yes, iter is a copy of the user passed iov_iter
1457 	 */
1458 	bool			direct_io;
1459 };
1460 
1461 /* asynchronous read support */
1462 struct cifs_readdata {
1463 	struct kref			refcount;
1464 	struct list_head		list;
1465 	struct completion		done;
1466 	struct cifsFileInfo		*cfile;
1467 	struct address_space		*mapping;
1468 	struct cifs_aio_ctx		*ctx;
1469 	__u64				offset;
1470 	ssize_t				got_bytes;
1471 	unsigned int			bytes;
1472 	pid_t				pid;
1473 	int				result;
1474 	struct work_struct		work;
1475 	struct iov_iter			iter;
1476 	struct kvec			iov[2];
1477 	struct TCP_Server_Info		*server;
1478 #ifdef CONFIG_CIFS_SMB_DIRECT
1479 	struct smbd_mr			*mr;
1480 #endif
1481 	struct cifs_credits		credits;
1482 };
1483 
1484 /* asynchronous write support */
1485 struct cifs_writedata {
1486 	struct kref			refcount;
1487 	struct list_head		list;
1488 	struct completion		done;
1489 	enum writeback_sync_modes	sync_mode;
1490 	struct work_struct		work;
1491 	struct cifsFileInfo		*cfile;
1492 	struct cifs_aio_ctx		*ctx;
1493 	struct iov_iter			iter;
1494 	struct bio_vec			*bv;
1495 	__u64				offset;
1496 	pid_t				pid;
1497 	unsigned int			bytes;
1498 	int				result;
1499 	struct TCP_Server_Info		*server;
1500 #ifdef CONFIG_CIFS_SMB_DIRECT
1501 	struct smbd_mr			*mr;
1502 #endif
1503 	struct cifs_credits		credits;
1504 };
1505 
1506 /*
1507  * Take a reference on the file private data. Must be called with
1508  * cfile->file_info_lock held.
1509  */
1510 static inline void
1511 cifsFileInfo_get_locked(struct cifsFileInfo *cifs_file)
1512 {
1513 	++cifs_file->count;
1514 }
1515 
1516 struct cifsFileInfo *cifsFileInfo_get(struct cifsFileInfo *cifs_file);
1517 void _cifsFileInfo_put(struct cifsFileInfo *cifs_file, bool wait_oplock_hdlr,
1518 		       bool offload);
1519 void cifsFileInfo_put(struct cifsFileInfo *cifs_file);
1520 
1521 #define CIFS_CACHE_READ_FLG	1
1522 #define CIFS_CACHE_HANDLE_FLG	2
1523 #define CIFS_CACHE_RH_FLG	(CIFS_CACHE_READ_FLG | CIFS_CACHE_HANDLE_FLG)
1524 #define CIFS_CACHE_WRITE_FLG	4
1525 #define CIFS_CACHE_RW_FLG	(CIFS_CACHE_READ_FLG | CIFS_CACHE_WRITE_FLG)
1526 #define CIFS_CACHE_RHW_FLG	(CIFS_CACHE_RW_FLG | CIFS_CACHE_HANDLE_FLG)
1527 
1528 #define CIFS_CACHE_READ(cinode) ((cinode->oplock & CIFS_CACHE_READ_FLG) || (CIFS_SB(cinode->netfs.inode.i_sb)->mnt_cifs_flags & CIFS_MOUNT_RO_CACHE))
1529 #define CIFS_CACHE_HANDLE(cinode) (cinode->oplock & CIFS_CACHE_HANDLE_FLG)
1530 #define CIFS_CACHE_WRITE(cinode) ((cinode->oplock & CIFS_CACHE_WRITE_FLG) || (CIFS_SB(cinode->netfs.inode.i_sb)->mnt_cifs_flags & CIFS_MOUNT_RW_CACHE))
1531 
1532 /*
1533  * One of these for each file inode
1534  */
1535 
1536 struct cifsInodeInfo {
1537 	struct netfs_inode netfs; /* Netfslib context and vfs inode */
1538 	bool can_cache_brlcks;
1539 	struct list_head llist;	/* locks helb by this inode */
1540 	/*
1541 	 * NOTE: Some code paths call down_read(lock_sem) twice, so
1542 	 * we must always use cifs_down_write() instead of down_write()
1543 	 * for this semaphore to avoid deadlocks.
1544 	 */
1545 	struct rw_semaphore lock_sem;	/* protect the fields above */
1546 	/* BB add in lists for dirty pages i.e. write caching info for oplock */
1547 	struct list_head openFileList;
1548 	spinlock_t	open_file_lock;	/* protects openFileList */
1549 	__u32 cifsAttrs; /* e.g. DOS archive bit, sparse, compressed, system */
1550 	unsigned int oplock;		/* oplock/lease level we have */
1551 	unsigned int epoch;		/* used to track lease state changes */
1552 #define CIFS_INODE_PENDING_OPLOCK_BREAK   (0) /* oplock break in progress */
1553 #define CIFS_INODE_PENDING_WRITERS	  (1) /* Writes in progress */
1554 #define CIFS_INODE_FLAG_UNUSED		  (2) /* Unused flag */
1555 #define CIFS_INO_DELETE_PENDING		  (3) /* delete pending on server */
1556 #define CIFS_INO_INVALID_MAPPING	  (4) /* pagecache is invalid */
1557 #define CIFS_INO_LOCK			  (5) /* lock bit for synchronization */
1558 #define CIFS_INO_MODIFIED_ATTR            (6) /* Indicate change in mtime/ctime */
1559 #define CIFS_INO_CLOSE_ON_LOCK            (7) /* Not to defer the close when lock is set */
1560 	unsigned long flags;
1561 	spinlock_t writers_lock;
1562 	unsigned int writers;		/* Number of writers on this inode */
1563 	unsigned long time;		/* jiffies of last update of inode */
1564 	u64  server_eof;		/* current file size on server -- protected by i_lock */
1565 	u64  uniqueid;			/* server inode number */
1566 	u64  createtime;		/* creation time on server */
1567 	__u8 lease_key[SMB2_LEASE_KEY_SIZE];	/* lease key for this inode */
1568 	struct list_head deferred_closes; /* list of deferred closes */
1569 	spinlock_t deferred_lock; /* protection on deferred list */
1570 	bool lease_granted; /* Flag to indicate whether lease or oplock is granted. */
1571 	char *symlink_target;
1572 	__u32 reparse_tag;
1573 };
1574 
1575 static inline struct cifsInodeInfo *
1576 CIFS_I(struct inode *inode)
1577 {
1578 	return container_of(inode, struct cifsInodeInfo, netfs.inode);
1579 }
1580 
1581 static inline struct cifs_sb_info *
1582 CIFS_SB(struct super_block *sb)
1583 {
1584 	return sb->s_fs_info;
1585 }
1586 
1587 static inline struct cifs_sb_info *
1588 CIFS_FILE_SB(struct file *file)
1589 {
1590 	return CIFS_SB(file_inode(file)->i_sb);
1591 }
1592 
1593 static inline char CIFS_DIR_SEP(const struct cifs_sb_info *cifs_sb)
1594 {
1595 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)
1596 		return '/';
1597 	else
1598 		return '\\';
1599 }
1600 
1601 static inline void
1602 convert_delimiter(char *path, char delim)
1603 {
1604 	char old_delim, *pos;
1605 
1606 	if (delim == '/')
1607 		old_delim = '\\';
1608 	else
1609 		old_delim = '/';
1610 
1611 	pos = path;
1612 	while ((pos = strchr(pos, old_delim)))
1613 		*pos = delim;
1614 }
1615 
1616 #define cifs_stats_inc atomic_inc
1617 
1618 static inline void cifs_stats_bytes_written(struct cifs_tcon *tcon,
1619 					    unsigned int bytes)
1620 {
1621 	if (bytes) {
1622 		spin_lock(&tcon->stat_lock);
1623 		tcon->bytes_written += bytes;
1624 		spin_unlock(&tcon->stat_lock);
1625 	}
1626 }
1627 
1628 static inline void cifs_stats_bytes_read(struct cifs_tcon *tcon,
1629 					 unsigned int bytes)
1630 {
1631 	spin_lock(&tcon->stat_lock);
1632 	tcon->bytes_read += bytes;
1633 	spin_unlock(&tcon->stat_lock);
1634 }
1635 
1636 
1637 /*
1638  * This is the prototype for the mid receive function. This function is for
1639  * receiving the rest of the SMB frame, starting with the WordCount (which is
1640  * just after the MID in struct smb_hdr). Note:
1641  *
1642  * - This will be called by cifsd, with no locks held.
1643  * - The mid will still be on the pending_mid_q.
1644  * - mid->resp_buf will point to the current buffer.
1645  *
1646  * Returns zero on a successful receive, or an error. The receive state in
1647  * the TCP_Server_Info will also be updated.
1648  */
1649 typedef int (mid_receive_t)(struct TCP_Server_Info *server,
1650 			    struct mid_q_entry *mid);
1651 
1652 /*
1653  * This is the prototype for the mid callback function. This is called once the
1654  * mid has been received off of the socket. When creating one, take special
1655  * care to avoid deadlocks. Things to bear in mind:
1656  *
1657  * - it will be called by cifsd, with no locks held
1658  * - the mid will be removed from any lists
1659  */
1660 typedef void (mid_callback_t)(struct mid_q_entry *mid);
1661 
1662 /*
1663  * This is the protopyte for mid handle function. This is called once the mid
1664  * has been recognized after decryption of the message.
1665  */
1666 typedef int (mid_handle_t)(struct TCP_Server_Info *server,
1667 			    struct mid_q_entry *mid);
1668 
1669 /* one of these for every pending CIFS request to the server */
1670 struct mid_q_entry {
1671 	struct list_head qhead;	/* mids waiting on reply from this server */
1672 	struct kref refcount;
1673 	struct TCP_Server_Info *server;	/* server corresponding to this mid */
1674 	__u64 mid;		/* multiplex id */
1675 	__u16 credits;		/* number of credits consumed by this mid */
1676 	__u16 credits_received;	/* number of credits from the response */
1677 	__u32 pid;		/* process id */
1678 	__u32 sequence_number;  /* for CIFS signing */
1679 	unsigned long when_alloc;  /* when mid was created */
1680 #ifdef CONFIG_CIFS_STATS2
1681 	unsigned long when_sent; /* time when smb send finished */
1682 	unsigned long when_received; /* when demux complete (taken off wire) */
1683 #endif
1684 	mid_receive_t *receive; /* call receive callback */
1685 	mid_callback_t *callback; /* call completion callback */
1686 	mid_handle_t *handle; /* call handle mid callback */
1687 	void *callback_data;	  /* general purpose pointer for callback */
1688 	struct task_struct *creator;
1689 	void *resp_buf;		/* pointer to received SMB header */
1690 	unsigned int resp_buf_size;
1691 	int mid_state;	/* wish this were enum but can not pass to wait_event */
1692 	unsigned int mid_flags;
1693 	__le16 command;		/* smb command code */
1694 	unsigned int optype;	/* operation type */
1695 	bool large_buf:1;	/* if valid response, is pointer to large buf */
1696 	bool multiRsp:1;	/* multiple trans2 responses for one request  */
1697 	bool multiEnd:1;	/* both received */
1698 	bool decrypted:1;	/* decrypted entry */
1699 };
1700 
1701 struct close_cancelled_open {
1702 	struct cifs_fid         fid;
1703 	struct cifs_tcon        *tcon;
1704 	struct work_struct      work;
1705 	__u64 mid;
1706 	__u16 cmd;
1707 };
1708 
1709 /*	Make code in transport.c a little cleaner by moving
1710 	update of optional stats into function below */
1711 static inline void cifs_in_send_inc(struct TCP_Server_Info *server)
1712 {
1713 	atomic_inc(&server->in_send);
1714 }
1715 
1716 static inline void cifs_in_send_dec(struct TCP_Server_Info *server)
1717 {
1718 	atomic_dec(&server->in_send);
1719 }
1720 
1721 static inline void cifs_num_waiters_inc(struct TCP_Server_Info *server)
1722 {
1723 	atomic_inc(&server->num_waiters);
1724 }
1725 
1726 static inline void cifs_num_waiters_dec(struct TCP_Server_Info *server)
1727 {
1728 	atomic_dec(&server->num_waiters);
1729 }
1730 
1731 #ifdef CONFIG_CIFS_STATS2
1732 static inline void cifs_save_when_sent(struct mid_q_entry *mid)
1733 {
1734 	mid->when_sent = jiffies;
1735 }
1736 #else
1737 static inline void cifs_save_when_sent(struct mid_q_entry *mid)
1738 {
1739 }
1740 #endif
1741 
1742 /* for pending dnotify requests */
1743 struct dir_notify_req {
1744 	struct list_head lhead;
1745 	__le16 Pid;
1746 	__le16 PidHigh;
1747 	__u16 Mid;
1748 	__u16 Tid;
1749 	__u16 Uid;
1750 	__u16 netfid;
1751 	__u32 filter; /* CompletionFilter (for multishot) */
1752 	int multishot;
1753 	struct file *pfile;
1754 };
1755 
1756 struct dfs_info3_param {
1757 	int flags; /* DFSREF_REFERRAL_SERVER, DFSREF_STORAGE_SERVER*/
1758 	int path_consumed;
1759 	int server_type;
1760 	int ref_flag;
1761 	char *path_name;
1762 	char *node_name;
1763 	int ttl;
1764 };
1765 
1766 struct file_list {
1767 	struct list_head list;
1768 	struct cifsFileInfo *cfile;
1769 };
1770 
1771 struct cifs_mount_ctx {
1772 	struct cifs_sb_info *cifs_sb;
1773 	struct smb3_fs_context *fs_ctx;
1774 	unsigned int xid;
1775 	struct TCP_Server_Info *server;
1776 	struct cifs_ses *ses;
1777 	struct cifs_tcon *tcon;
1778 	struct list_head dfs_ses_list;
1779 };
1780 
1781 static inline void __free_dfs_info_param(struct dfs_info3_param *param)
1782 {
1783 	kfree(param->path_name);
1784 	kfree(param->node_name);
1785 }
1786 
1787 static inline void free_dfs_info_param(struct dfs_info3_param *param)
1788 {
1789 	if (param)
1790 		__free_dfs_info_param(param);
1791 }
1792 
1793 static inline void zfree_dfs_info_param(struct dfs_info3_param *param)
1794 {
1795 	if (param) {
1796 		__free_dfs_info_param(param);
1797 		memset(param, 0, sizeof(*param));
1798 	}
1799 }
1800 
1801 static inline void free_dfs_info_array(struct dfs_info3_param *param,
1802 				       int number_of_items)
1803 {
1804 	int i;
1805 
1806 	if ((number_of_items == 0) || (param == NULL))
1807 		return;
1808 	for (i = 0; i < number_of_items; i++) {
1809 		kfree(param[i].path_name);
1810 		kfree(param[i].node_name);
1811 	}
1812 	kfree(param);
1813 }
1814 
1815 static inline bool is_interrupt_error(int error)
1816 {
1817 	switch (error) {
1818 	case -EINTR:
1819 	case -ERESTARTSYS:
1820 	case -ERESTARTNOHAND:
1821 	case -ERESTARTNOINTR:
1822 		return true;
1823 	}
1824 	return false;
1825 }
1826 
1827 static inline bool is_retryable_error(int error)
1828 {
1829 	if (is_interrupt_error(error) || error == -EAGAIN)
1830 		return true;
1831 	return false;
1832 }
1833 
1834 
1835 /* cifs_get_writable_file() flags */
1836 #define FIND_WR_ANY         0
1837 #define FIND_WR_FSUID_ONLY  1
1838 #define FIND_WR_WITH_DELETE 2
1839 
1840 #define   MID_FREE 0
1841 #define   MID_REQUEST_ALLOCATED 1
1842 #define   MID_REQUEST_SUBMITTED 2
1843 #define   MID_RESPONSE_RECEIVED 4
1844 #define   MID_RETRY_NEEDED      8 /* session closed while this request out */
1845 #define   MID_RESPONSE_MALFORMED 0x10
1846 #define   MID_SHUTDOWN		 0x20
1847 #define   MID_RESPONSE_READY 0x40 /* ready for other process handle the rsp */
1848 
1849 /* Flags */
1850 #define   MID_WAIT_CANCELLED	 1 /* Cancelled while waiting for response */
1851 #define   MID_DELETED            2 /* Mid has been dequeued/deleted */
1852 
1853 /* Types of response buffer returned from SendReceive2 */
1854 #define   CIFS_NO_BUFFER        0    /* Response buffer not returned */
1855 #define   CIFS_SMALL_BUFFER     1
1856 #define   CIFS_LARGE_BUFFER     2
1857 #define   CIFS_IOVEC            4    /* array of response buffers */
1858 
1859 /* Type of Request to SendReceive2 */
1860 #define   CIFS_BLOCKING_OP      1    /* operation can block */
1861 #define   CIFS_NON_BLOCKING     2    /* do not block waiting for credits */
1862 #define   CIFS_TIMEOUT_MASK 0x003    /* only one of above set in req */
1863 #define   CIFS_LOG_ERROR    0x010    /* log NT STATUS if non-zero */
1864 #define   CIFS_LARGE_BUF_OP 0x020    /* large request buffer */
1865 #define   CIFS_NO_RSP_BUF   0x040    /* no response buffer required */
1866 
1867 /* Type of request operation */
1868 #define   CIFS_ECHO_OP            0x080  /* echo request */
1869 #define   CIFS_OBREAK_OP          0x0100 /* oplock break request */
1870 #define   CIFS_NEG_OP             0x0200 /* negotiate request */
1871 #define   CIFS_CP_CREATE_CLOSE_OP 0x0400 /* compound create+close request */
1872 /* Lower bitmask values are reserved by others below. */
1873 #define   CIFS_SESS_OP            0x2000 /* session setup request */
1874 #define   CIFS_OP_MASK            0x2780 /* mask request type */
1875 
1876 #define   CIFS_HAS_CREDITS        0x0400 /* already has credits */
1877 #define   CIFS_TRANSFORM_REQ      0x0800 /* transform request before sending */
1878 #define   CIFS_NO_SRV_RSP         0x1000 /* there is no server response */
1879 
1880 /* Security Flags: indicate type of session setup needed */
1881 #define   CIFSSEC_MAY_SIGN	0x00001
1882 #define   CIFSSEC_MAY_NTLMV2	0x00004
1883 #define   CIFSSEC_MAY_KRB5	0x00008
1884 #define   CIFSSEC_MAY_SEAL	0x00040 /* not supported yet */
1885 #define   CIFSSEC_MAY_NTLMSSP	0x00080 /* raw ntlmssp with ntlmv2 */
1886 
1887 #define   CIFSSEC_MUST_SIGN	0x01001
1888 /* note that only one of the following can be set so the
1889 result of setting MUST flags more than once will be to
1890 require use of the stronger protocol */
1891 #define   CIFSSEC_MUST_NTLMV2	0x04004
1892 #define   CIFSSEC_MUST_KRB5	0x08008
1893 #ifdef CONFIG_CIFS_UPCALL
1894 #define   CIFSSEC_MASK          0x8F08F /* flags supported if no weak allowed */
1895 #else
1896 #define	  CIFSSEC_MASK          0x87087 /* flags supported if no weak allowed */
1897 #endif /* UPCALL */
1898 #define   CIFSSEC_MUST_SEAL	0x40040 /* not supported yet */
1899 #define   CIFSSEC_MUST_NTLMSSP	0x80080 /* raw ntlmssp with ntlmv2 */
1900 
1901 #define   CIFSSEC_DEF (CIFSSEC_MAY_SIGN | CIFSSEC_MAY_NTLMV2 | CIFSSEC_MAY_NTLMSSP)
1902 #define   CIFSSEC_MAX (CIFSSEC_MUST_NTLMV2)
1903 #define   CIFSSEC_AUTH_MASK (CIFSSEC_MAY_NTLMV2 | CIFSSEC_MAY_KRB5 | CIFSSEC_MAY_NTLMSSP)
1904 /*
1905  *****************************************************************
1906  * All constants go here
1907  *****************************************************************
1908  */
1909 
1910 #define UID_HASH (16)
1911 
1912 /*
1913  * Note that ONE module should define _DECLARE_GLOBALS_HERE to cause the
1914  * following to be declared.
1915  */
1916 
1917 /****************************************************************************
1918  * Here are all the locks (spinlock, mutex, semaphore) in cifs.ko, arranged according
1919  * to the locking order. i.e. if two locks are to be held together, the lock that
1920  * appears higher in this list needs to be taken before the other.
1921  *
1922  * If you hold a lock that is lower in this list, and you need to take a higher lock
1923  * (or if you think that one of the functions that you're calling may need to), first
1924  * drop the lock you hold, pick up the higher lock, then the lower one. This will
1925  * ensure that locks are picked up only in one direction in the below table
1926  * (top to bottom).
1927  *
1928  * Also, if you expect a function to be called with a lock held, explicitly document
1929  * this in the comments on top of your function definition.
1930  *
1931  * And also, try to keep the critical sections (lock hold time) to be as minimal as
1932  * possible. Blocking / calling other functions with a lock held always increase
1933  * the risk of a possible deadlock.
1934  *
1935  * Following this rule will avoid unnecessary deadlocks, which can get really hard to
1936  * debug. Also, any new lock that you introduce, please add to this list in the correct
1937  * order.
1938  *
1939  * Please populate this list whenever you introduce new locks in your changes. Or in
1940  * case I've missed some existing locks. Please ensure that it's added in the list
1941  * based on the locking order expected.
1942  *
1943  * =====================================================================================
1944  * Lock				Protects			Initialization fn
1945  * =====================================================================================
1946  * vol_list_lock
1947  * vol_info->ctx_lock		vol_info->ctx
1948  * cifs_sb_info->tlink_tree_lock	cifs_sb_info->tlink_tree	cifs_setup_cifs_sb
1949  * TCP_Server_Info->		TCP_Server_Info			cifs_get_tcp_session
1950  * reconnect_mutex
1951  * TCP_Server_Info->srv_mutex	TCP_Server_Info			cifs_get_tcp_session
1952  * cifs_ses->session_mutex		cifs_ses		sesInfoAlloc
1953  *				cifs_tcon
1954  * cifs_tcon->open_file_lock	cifs_tcon->openFileList		tconInfoAlloc
1955  *				cifs_tcon->pending_opens
1956  * cifs_tcon->stat_lock		cifs_tcon->bytes_read		tconInfoAlloc
1957  *				cifs_tcon->bytes_written
1958  * cifs_tcp_ses_lock		cifs_tcp_ses_list		sesInfoAlloc
1959  * GlobalMid_Lock		GlobalMaxActiveXid		init_cifs
1960  *				GlobalCurrentXid
1961  *				GlobalTotalActiveXid
1962  * TCP_Server_Info->srv_lock	(anything in struct not protected by another lock and can change)
1963  * TCP_Server_Info->mid_lock	TCP_Server_Info->pending_mid_q	cifs_get_tcp_session
1964  *				->CurrentMid
1965  *				(any changes in mid_q_entry fields)
1966  * TCP_Server_Info->req_lock	TCP_Server_Info->in_flight	cifs_get_tcp_session
1967  *				->credits
1968  *				->echo_credits
1969  *				->oplock_credits
1970  *				->reconnect_instance
1971  * cifs_ses->ses_lock		(anything that is not protected by another lock and can change)
1972  * cifs_ses->iface_lock		cifs_ses->iface_list		sesInfoAlloc
1973  *				->iface_count
1974  *				->iface_last_update
1975  * cifs_ses->chan_lock		cifs_ses->chans
1976  *				->chans_need_reconnect
1977  *				->chans_in_reconnect
1978  * cifs_tcon->tc_lock		(anything that is not protected by another lock and can change)
1979  * cifsInodeInfo->open_file_lock	cifsInodeInfo->openFileList	cifs_alloc_inode
1980  * cifsInodeInfo->writers_lock	cifsInodeInfo->writers		cifsInodeInfo_alloc
1981  * cifsInodeInfo->lock_sem	cifsInodeInfo->llist		cifs_init_once
1982  *				->can_cache_brlcks
1983  * cifsInodeInfo->deferred_lock	cifsInodeInfo->deferred_closes	cifsInodeInfo_alloc
1984  * cached_fid->fid_mutex		cifs_tcon->crfid		tcon_info_alloc
1985  * cifsFileInfo->fh_mutex		cifsFileInfo			cifs_new_fileinfo
1986  * cifsFileInfo->file_info_lock	cifsFileInfo->count		cifs_new_fileinfo
1987  *				->invalidHandle			initiate_cifs_search
1988  *				->oplock_break_cancelled
1989  * cifs_aio_ctx->aio_mutex		cifs_aio_ctx			cifs_aio_ctx_alloc
1990  ****************************************************************************/
1991 
1992 #ifdef DECLARE_GLOBALS_HERE
1993 #define GLOBAL_EXTERN
1994 #else
1995 #define GLOBAL_EXTERN extern
1996 #endif
1997 
1998 /*
1999  * the list of TCP_Server_Info structures, ie each of the sockets
2000  * connecting our client to a distinct server (ip address), is
2001  * chained together by cifs_tcp_ses_list. The list of all our SMB
2002  * sessions (and from that the tree connections) can be found
2003  * by iterating over cifs_tcp_ses_list
2004  */
2005 extern struct list_head		cifs_tcp_ses_list;
2006 
2007 /*
2008  * This lock protects the cifs_tcp_ses_list, the list of smb sessions per
2009  * tcp session, and the list of tcon's per smb session. It also protects
2010  * the reference counters for the server, smb session, and tcon.
2011  * generally the locks should be taken in order tcp_ses_lock before
2012  * tcon->open_file_lock and that before file->file_info_lock since the
2013  * structure order is cifs_socket-->cifs_ses-->cifs_tcon-->cifs_file
2014  */
2015 extern spinlock_t		cifs_tcp_ses_lock;
2016 
2017 /*
2018  * Global transaction id (XID) information
2019  */
2020 extern unsigned int GlobalCurrentXid;	/* protected by GlobalMid_Sem */
2021 extern unsigned int GlobalTotalActiveXid; /* prot by GlobalMid_Sem */
2022 extern unsigned int GlobalMaxActiveXid;	/* prot by GlobalMid_Sem */
2023 extern spinlock_t GlobalMid_Lock; /* protects above & list operations on midQ entries */
2024 
2025 /*
2026  *  Global counters, updated atomically
2027  */
2028 extern atomic_t sesInfoAllocCount;
2029 extern atomic_t tconInfoAllocCount;
2030 extern atomic_t tcpSesNextId;
2031 extern atomic_t tcpSesAllocCount;
2032 extern atomic_t tcpSesReconnectCount;
2033 extern atomic_t tconInfoReconnectCount;
2034 
2035 /* Various Debug counters */
2036 extern atomic_t buf_alloc_count;	/* current number allocated  */
2037 extern atomic_t small_buf_alloc_count;
2038 #ifdef CONFIG_CIFS_STATS2
2039 extern atomic_t total_buf_alloc_count; /* total allocated over all time */
2040 extern atomic_t total_small_buf_alloc_count;
2041 extern unsigned int slow_rsp_threshold; /* number of secs before logging */
2042 #endif
2043 
2044 /* Misc globals */
2045 extern bool enable_oplocks; /* enable or disable oplocks */
2046 extern bool lookupCacheEnabled;
2047 extern unsigned int global_secflags;	/* if on, session setup sent
2048 				with more secure ntlmssp2 challenge/resp */
2049 extern unsigned int sign_CIFS_PDUs;  /* enable smb packet signing */
2050 extern bool enable_gcm_256; /* allow optional negotiate of strongest signing (aes-gcm-256) */
2051 extern bool require_gcm_256; /* require use of strongest signing (aes-gcm-256) */
2052 extern bool enable_negotiate_signing; /* request use of faster (GMAC) signing if available */
2053 extern bool linuxExtEnabled;/*enable Linux/Unix CIFS extensions*/
2054 extern unsigned int CIFSMaxBufSize;  /* max size not including hdr */
2055 extern unsigned int cifs_min_rcv;    /* min size of big ntwrk buf pool */
2056 extern unsigned int cifs_min_small;  /* min size of small buf pool */
2057 extern unsigned int cifs_max_pending; /* MAX requests at once to server*/
2058 extern unsigned int dir_cache_timeout; /* max time for directory lease caching of dir */
2059 extern bool disable_legacy_dialects;  /* forbid vers=1.0 and vers=2.0 mounts */
2060 extern atomic_t mid_count;
2061 
2062 void cifs_oplock_break(struct work_struct *work);
2063 void cifs_queue_oplock_break(struct cifsFileInfo *cfile);
2064 void smb2_deferred_work_close(struct work_struct *work);
2065 
2066 extern const struct slow_work_ops cifs_oplock_break_ops;
2067 extern struct workqueue_struct *cifsiod_wq;
2068 extern struct workqueue_struct *decrypt_wq;
2069 extern struct workqueue_struct *fileinfo_put_wq;
2070 extern struct workqueue_struct *cifsoplockd_wq;
2071 extern struct workqueue_struct *deferredclose_wq;
2072 extern __u32 cifs_lock_secret;
2073 
2074 extern mempool_t *cifs_mid_poolp;
2075 
2076 /* Operations for different SMB versions */
2077 #define SMB1_VERSION_STRING	"1.0"
2078 #define SMB20_VERSION_STRING    "2.0"
2079 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2080 extern struct smb_version_operations smb1_operations;
2081 extern struct smb_version_values smb1_values;
2082 extern struct smb_version_operations smb20_operations;
2083 extern struct smb_version_values smb20_values;
2084 #endif /* CIFS_ALLOW_INSECURE_LEGACY */
2085 #define SMB21_VERSION_STRING	"2.1"
2086 extern struct smb_version_operations smb21_operations;
2087 extern struct smb_version_values smb21_values;
2088 #define SMBDEFAULT_VERSION_STRING "default"
2089 extern struct smb_version_values smbdefault_values;
2090 #define SMB3ANY_VERSION_STRING "3"
2091 extern struct smb_version_values smb3any_values;
2092 #define SMB30_VERSION_STRING	"3.0"
2093 extern struct smb_version_operations smb30_operations;
2094 extern struct smb_version_values smb30_values;
2095 #define SMB302_VERSION_STRING	"3.02"
2096 #define ALT_SMB302_VERSION_STRING "3.0.2"
2097 /*extern struct smb_version_operations smb302_operations;*/ /* not needed yet */
2098 extern struct smb_version_values smb302_values;
2099 #define SMB311_VERSION_STRING	"3.1.1"
2100 #define ALT_SMB311_VERSION_STRING "3.11"
2101 extern struct smb_version_operations smb311_operations;
2102 extern struct smb_version_values smb311_values;
2103 
2104 static inline char *get_security_type_str(enum securityEnum sectype)
2105 {
2106 	switch (sectype) {
2107 	case RawNTLMSSP:
2108 		return "RawNTLMSSP";
2109 	case Kerberos:
2110 		return "Kerberos";
2111 	case NTLMv2:
2112 		return "NTLMv2";
2113 	default:
2114 		return "Unknown";
2115 	}
2116 }
2117 
2118 static inline bool is_smb1_server(struct TCP_Server_Info *server)
2119 {
2120 	return strcmp(server->vals->version_string, SMB1_VERSION_STRING) == 0;
2121 }
2122 
2123 static inline bool is_tcon_dfs(struct cifs_tcon *tcon)
2124 {
2125 	/*
2126 	 * For SMB1, see MS-CIFS 2.4.55 SMB_COM_TREE_CONNECT_ANDX (0x75) and MS-CIFS 3.3.4.4 DFS
2127 	 * Subsystem Notifies That a Share Is a DFS Share.
2128 	 *
2129 	 * For SMB2+, see MS-SMB2 2.2.10 SMB2 TREE_CONNECT Response and MS-SMB2 3.3.4.14 Server
2130 	 * Application Updates a Share.
2131 	 */
2132 	if (!tcon || !tcon->ses || !tcon->ses->server)
2133 		return false;
2134 	return is_smb1_server(tcon->ses->server) ? tcon->Flags & SMB_SHARE_IS_IN_DFS :
2135 		tcon->share_flags & (SHI1005_FLAGS_DFS | SHI1005_FLAGS_DFS_ROOT);
2136 }
2137 
2138 static inline bool cifs_is_referral_server(struct cifs_tcon *tcon,
2139 					   const struct dfs_info3_param *ref)
2140 {
2141 	/*
2142 	 * Check if all targets are capable of handling DFS referrals as per
2143 	 * MS-DFSC 2.2.4 RESP_GET_DFS_REFERRAL.
2144 	 */
2145 	return is_tcon_dfs(tcon) || (ref && (ref->flags & DFSREF_REFERRAL_SERVER));
2146 }
2147 
2148 static inline u64 cifs_flock_len(const struct file_lock *fl)
2149 {
2150 	return (u64)fl->fl_end - fl->fl_start + 1;
2151 }
2152 
2153 static inline size_t ntlmssp_workstation_name_size(const struct cifs_ses *ses)
2154 {
2155 	if (WARN_ON_ONCE(!ses || !ses->server))
2156 		return 0;
2157 	/*
2158 	 * Make workstation name no more than 15 chars when using insecure dialects as some legacy
2159 	 * servers do require it during NTLMSSP.
2160 	 */
2161 	if (ses->server->dialect <= SMB20_PROT_ID)
2162 		return min_t(size_t, sizeof(ses->workstation_name), RFC1001_NAME_LEN_WITH_NULL);
2163 	return sizeof(ses->workstation_name);
2164 }
2165 
2166 static inline void move_cifs_info_to_smb2(struct smb2_file_all_info *dst, const FILE_ALL_INFO *src)
2167 {
2168 	memcpy(dst, src, (size_t)((u8 *)&src->AccessFlags - (u8 *)src));
2169 	dst->AccessFlags = src->AccessFlags;
2170 	dst->CurrentByteOffset = src->CurrentByteOffset;
2171 	dst->Mode = src->Mode;
2172 	dst->AlignmentRequirement = src->AlignmentRequirement;
2173 	dst->FileNameLength = src->FileNameLength;
2174 }
2175 
2176 static inline int cifs_get_num_sgs(const struct smb_rqst *rqst,
2177 				   int num_rqst,
2178 				   const u8 *sig)
2179 {
2180 	unsigned int len, skip;
2181 	unsigned int nents = 0;
2182 	unsigned long addr;
2183 	size_t data_size;
2184 	int i, j;
2185 
2186 	/*
2187 	 * The first rqst has a transform header where the first 20 bytes are
2188 	 * not part of the encrypted blob.
2189 	 */
2190 	skip = 20;
2191 
2192 	/* Assumes the first rqst has a transform header as the first iov.
2193 	 * I.e.
2194 	 * rqst[0].rq_iov[0]  is transform header
2195 	 * rqst[0].rq_iov[1+] data to be encrypted/decrypted
2196 	 * rqst[1+].rq_iov[0+] data to be encrypted/decrypted
2197 	 */
2198 	for (i = 0; i < num_rqst; i++) {
2199 		data_size = iov_iter_count(&rqst[i].rq_iter);
2200 
2201 		/* We really don't want a mixture of pinned and unpinned pages
2202 		 * in the sglist.  It's hard to keep track of which is what.
2203 		 * Instead, we convert to a BVEC-type iterator higher up.
2204 		 */
2205 		if (data_size &&
2206 		    WARN_ON_ONCE(user_backed_iter(&rqst[i].rq_iter)))
2207 			return -EIO;
2208 
2209 		/* We also don't want to have any extra refs or pins to clean
2210 		 * up in the sglist.
2211 		 */
2212 		if (data_size &&
2213 		    WARN_ON_ONCE(iov_iter_extract_will_pin(&rqst[i].rq_iter)))
2214 			return -EIO;
2215 
2216 		for (j = 0; j < rqst[i].rq_nvec; j++) {
2217 			struct kvec *iov = &rqst[i].rq_iov[j];
2218 
2219 			addr = (unsigned long)iov->iov_base + skip;
2220 			if (unlikely(is_vmalloc_addr((void *)addr))) {
2221 				len = iov->iov_len - skip;
2222 				nents += DIV_ROUND_UP(offset_in_page(addr) + len,
2223 						      PAGE_SIZE);
2224 			} else {
2225 				nents++;
2226 			}
2227 			skip = 0;
2228 		}
2229 		if (data_size)
2230 			nents += iov_iter_npages(&rqst[i].rq_iter, INT_MAX);
2231 	}
2232 	nents += DIV_ROUND_UP(offset_in_page(sig) + SMB2_SIGNATURE_SIZE, PAGE_SIZE);
2233 	return nents;
2234 }
2235 
2236 /* We can not use the normal sg_set_buf() as we will sometimes pass a
2237  * stack object as buf.
2238  */
2239 static inline void cifs_sg_set_buf(struct sg_table *sgtable,
2240 				   const void *buf,
2241 				   unsigned int buflen)
2242 {
2243 	unsigned long addr = (unsigned long)buf;
2244 	unsigned int off = offset_in_page(addr);
2245 
2246 	addr &= PAGE_MASK;
2247 	if (unlikely(is_vmalloc_addr((void *)addr))) {
2248 		do {
2249 			unsigned int len = min_t(unsigned int, buflen, PAGE_SIZE - off);
2250 
2251 			sg_set_page(&sgtable->sgl[sgtable->nents++],
2252 				    vmalloc_to_page((void *)addr), len, off);
2253 
2254 			off = 0;
2255 			addr += PAGE_SIZE;
2256 			buflen -= len;
2257 		} while (buflen);
2258 	} else {
2259 		sg_set_page(&sgtable->sgl[sgtable->nents++],
2260 			    virt_to_page((void *)addr), buflen, off);
2261 	}
2262 }
2263 
2264 struct smb2_compound_vars {
2265 	struct cifs_open_parms oparms;
2266 	struct kvec rsp_iov[MAX_COMPOUND];
2267 	struct smb_rqst rqst[MAX_COMPOUND];
2268 	struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2269 	struct kvec qi_iov;
2270 	struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
2271 	struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
2272 	struct kvec close_iov;
2273 	struct smb2_file_rename_info rename_info;
2274 	struct smb2_file_link_info link_info;
2275 };
2276 
2277 #endif	/* _CIFS_GLOB_H */
2278