cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

cifsglob.h (71313B)


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