File Index
Files and directories
Sign in
FILE

cifsglob.h

File preview with syntax highlighting.

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