xref: /openbmc/linux/fs/smb/client/smb2ops.c (revision 9144f784f852f9a125cabe9927b986d909bfa439)
1  // SPDX-License-Identifier: GPL-2.0
2  /*
3   *  SMB2 version specific operations
4   *
5   *  Copyright (c) 2012, Jeff Layton <jlayton@redhat.com>
6   */
7  
8  #include <linux/pagemap.h>
9  #include <linux/vfs.h>
10  #include <linux/falloc.h>
11  #include <linux/scatterlist.h>
12  #include <linux/uuid.h>
13  #include <linux/sort.h>
14  #include <crypto/aead.h>
15  #include <linux/fiemap.h>
16  #include <uapi/linux/magic.h>
17  #include "cifsfs.h"
18  #include "cifsglob.h"
19  #include "smb2pdu.h"
20  #include "smb2proto.h"
21  #include "cifsproto.h"
22  #include "cifs_debug.h"
23  #include "cifs_unicode.h"
24  #include "smb2status.h"
25  #include "smb2glob.h"
26  #include "cifs_ioctl.h"
27  #include "smbdirect.h"
28  #include "fscache.h"
29  #include "fs_context.h"
30  #include "cached_dir.h"
31  #include "reparse.h"
32  
33  /* Change credits for different ops and return the total number of credits */
34  static int
35  change_conf(struct TCP_Server_Info *server)
36  {
37  	server->credits += server->echo_credits + server->oplock_credits;
38  	if (server->credits > server->max_credits)
39  		server->credits = server->max_credits;
40  	server->oplock_credits = server->echo_credits = 0;
41  	switch (server->credits) {
42  	case 0:
43  		return 0;
44  	case 1:
45  		server->echoes = false;
46  		server->oplocks = false;
47  		break;
48  	case 2:
49  		server->echoes = true;
50  		server->oplocks = false;
51  		server->echo_credits = 1;
52  		break;
53  	default:
54  		server->echoes = true;
55  		if (enable_oplocks) {
56  			server->oplocks = true;
57  			server->oplock_credits = 1;
58  		} else
59  			server->oplocks = false;
60  
61  		server->echo_credits = 1;
62  	}
63  	server->credits -= server->echo_credits + server->oplock_credits;
64  	return server->credits + server->echo_credits + server->oplock_credits;
65  }
66  
67  static void
68  smb2_add_credits(struct TCP_Server_Info *server,
69  		 const struct cifs_credits *credits, const int optype)
70  {
71  	int *val, rc = -1;
72  	int scredits, in_flight;
73  	unsigned int add = credits->value;
74  	unsigned int instance = credits->instance;
75  	bool reconnect_detected = false;
76  	bool reconnect_with_invalid_credits = false;
77  
78  	spin_lock(&server->req_lock);
79  	val = server->ops->get_credits_field(server, optype);
80  
81  	/* eg found case where write overlapping reconnect messed up credits */
82  	if (((optype & CIFS_OP_MASK) == CIFS_NEG_OP) && (*val != 0))
83  		reconnect_with_invalid_credits = true;
84  
85  	if ((instance == 0) || (instance == server->reconnect_instance))
86  		*val += add;
87  	else
88  		reconnect_detected = true;
89  
90  	if (*val > 65000) {
91  		*val = 65000; /* Don't get near 64K credits, avoid srv bugs */
92  		pr_warn_once("server overflowed SMB3 credits\n");
93  		trace_smb3_overflow_credits(server->CurrentMid,
94  					    server->conn_id, server->hostname, *val,
95  					    add, server->in_flight);
96  	}
97  	WARN_ON_ONCE(server->in_flight == 0);
98  	server->in_flight--;
99  	if (server->in_flight == 0 &&
100  	   ((optype & CIFS_OP_MASK) != CIFS_NEG_OP) &&
101  	   ((optype & CIFS_OP_MASK) != CIFS_SESS_OP))
102  		rc = change_conf(server);
103  	/*
104  	 * Sometimes server returns 0 credits on oplock break ack - we need to
105  	 * rebalance credits in this case.
106  	 */
107  	else if (server->in_flight > 0 && server->oplock_credits == 0 &&
108  		 server->oplocks) {
109  		if (server->credits > 1) {
110  			server->credits--;
111  			server->oplock_credits++;
112  		}
113  	} else if ((server->in_flight > 0) && (server->oplock_credits > 3) &&
114  		   ((optype & CIFS_OP_MASK) == CIFS_OBREAK_OP))
115  		/* if now have too many oplock credits, rebalance so don't starve normal ops */
116  		change_conf(server);
117  
118  	scredits = *val;
119  	in_flight = server->in_flight;
120  	spin_unlock(&server->req_lock);
121  	wake_up(&server->request_q);
122  
123  	if (reconnect_detected) {
124  		trace_smb3_reconnect_detected(server->CurrentMid,
125  			server->conn_id, server->hostname, scredits, add, in_flight);
126  
127  		cifs_dbg(FYI, "trying to put %d credits from the old server instance %d\n",
128  			 add, instance);
129  	}
130  
131  	if (reconnect_with_invalid_credits) {
132  		trace_smb3_reconnect_with_invalid_credits(server->CurrentMid,
133  			server->conn_id, server->hostname, scredits, add, in_flight);
134  		cifs_dbg(FYI, "Negotiate operation when server credits is non-zero. Optype: %d, server credits: %d, credits added: %d\n",
135  			 optype, scredits, add);
136  	}
137  
138  	spin_lock(&server->srv_lock);
139  	if (server->tcpStatus == CifsNeedReconnect
140  	    || server->tcpStatus == CifsExiting) {
141  		spin_unlock(&server->srv_lock);
142  		return;
143  	}
144  	spin_unlock(&server->srv_lock);
145  
146  	switch (rc) {
147  	case -1:
148  		/* change_conf hasn't been executed */
149  		break;
150  	case 0:
151  		cifs_server_dbg(VFS, "Possible client or server bug - zero credits\n");
152  		break;
153  	case 1:
154  		cifs_server_dbg(VFS, "disabling echoes and oplocks\n");
155  		break;
156  	case 2:
157  		cifs_dbg(FYI, "disabling oplocks\n");
158  		break;
159  	default:
160  		/* change_conf rebalanced credits for different types */
161  		break;
162  	}
163  
164  	trace_smb3_add_credits(server->CurrentMid,
165  			server->conn_id, server->hostname, scredits, add, in_flight);
166  	cifs_dbg(FYI, "%s: added %u credits total=%d\n", __func__, add, scredits);
167  }
168  
169  static void
170  smb2_set_credits(struct TCP_Server_Info *server, const int val)
171  {
172  	int scredits, in_flight;
173  
174  	spin_lock(&server->req_lock);
175  	server->credits = val;
176  	if (val == 1) {
177  		server->reconnect_instance++;
178  		/*
179  		 * ChannelSequence updated for all channels in primary channel so that consistent
180  		 * across SMB3 requests sent on any channel. See MS-SMB2 3.2.4.1 and 3.2.7.1
181  		 */
182  		if (SERVER_IS_CHAN(server))
183  			server->primary_server->channel_sequence_num++;
184  		else
185  			server->channel_sequence_num++;
186  	}
187  	scredits = server->credits;
188  	in_flight = server->in_flight;
189  	spin_unlock(&server->req_lock);
190  
191  	trace_smb3_set_credits(server->CurrentMid,
192  			server->conn_id, server->hostname, scredits, val, in_flight);
193  	cifs_dbg(FYI, "%s: set %u credits\n", __func__, val);
194  
195  	/* don't log while holding the lock */
196  	if (val == 1)
197  		cifs_dbg(FYI, "set credits to 1 due to smb2 reconnect\n");
198  }
199  
200  static int *
201  smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
202  {
203  	switch (optype) {
204  	case CIFS_ECHO_OP:
205  		return &server->echo_credits;
206  	case CIFS_OBREAK_OP:
207  		return &server->oplock_credits;
208  	default:
209  		return &server->credits;
210  	}
211  }
212  
213  static unsigned int
214  smb2_get_credits(struct mid_q_entry *mid)
215  {
216  	return mid->credits_received;
217  }
218  
219  static int
220  smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
221  		      unsigned int *num, struct cifs_credits *credits)
222  {
223  	int rc = 0;
224  	unsigned int scredits, in_flight;
225  
226  	spin_lock(&server->req_lock);
227  	while (1) {
228  		spin_unlock(&server->req_lock);
229  
230  		spin_lock(&server->srv_lock);
231  		if (server->tcpStatus == CifsExiting) {
232  			spin_unlock(&server->srv_lock);
233  			return -ENOENT;
234  		}
235  		spin_unlock(&server->srv_lock);
236  
237  		spin_lock(&server->req_lock);
238  		if (server->credits <= 0) {
239  			spin_unlock(&server->req_lock);
240  			cifs_num_waiters_inc(server);
241  			rc = wait_event_killable(server->request_q,
242  				has_credits(server, &server->credits, 1));
243  			cifs_num_waiters_dec(server);
244  			if (rc)
245  				return rc;
246  			spin_lock(&server->req_lock);
247  		} else {
248  			scredits = server->credits;
249  			/* can deadlock with reopen */
250  			if (scredits <= 8) {
251  				*num = SMB2_MAX_BUFFER_SIZE;
252  				credits->value = 0;
253  				credits->instance = 0;
254  				break;
255  			}
256  
257  			/* leave some credits for reopen and other ops */
258  			scredits -= 8;
259  			*num = min_t(unsigned int, size,
260  				     scredits * SMB2_MAX_BUFFER_SIZE);
261  
262  			credits->value =
263  				DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
264  			credits->instance = server->reconnect_instance;
265  			server->credits -= credits->value;
266  			server->in_flight++;
267  			if (server->in_flight > server->max_in_flight)
268  				server->max_in_flight = server->in_flight;
269  			break;
270  		}
271  	}
272  	scredits = server->credits;
273  	in_flight = server->in_flight;
274  	spin_unlock(&server->req_lock);
275  
276  	trace_smb3_wait_credits(server->CurrentMid,
277  			server->conn_id, server->hostname, scredits, -(credits->value), in_flight);
278  	cifs_dbg(FYI, "%s: removed %u credits total=%d\n",
279  			__func__, credits->value, scredits);
280  
281  	return rc;
282  }
283  
284  static int
285  smb2_adjust_credits(struct TCP_Server_Info *server,
286  		    struct cifs_credits *credits,
287  		    const unsigned int payload_size)
288  {
289  	int new_val = DIV_ROUND_UP(payload_size, SMB2_MAX_BUFFER_SIZE);
290  	int scredits, in_flight;
291  
292  	if (!credits->value || credits->value == new_val)
293  		return 0;
294  
295  	if (credits->value < new_val) {
296  		trace_smb3_too_many_credits(server->CurrentMid,
297  				server->conn_id, server->hostname, 0, credits->value - new_val, 0);
298  		cifs_server_dbg(VFS, "request has less credits (%d) than required (%d)",
299  				credits->value, new_val);
300  
301  		return -EOPNOTSUPP;
302  	}
303  
304  	spin_lock(&server->req_lock);
305  
306  	if (server->reconnect_instance != credits->instance) {
307  		scredits = server->credits;
308  		in_flight = server->in_flight;
309  		spin_unlock(&server->req_lock);
310  
311  		trace_smb3_reconnect_detected(server->CurrentMid,
312  			server->conn_id, server->hostname, scredits,
313  			credits->value - new_val, in_flight);
314  		cifs_server_dbg(VFS, "trying to return %d credits to old session\n",
315  			 credits->value - new_val);
316  		return -EAGAIN;
317  	}
318  
319  	server->credits += credits->value - new_val;
320  	scredits = server->credits;
321  	in_flight = server->in_flight;
322  	spin_unlock(&server->req_lock);
323  	wake_up(&server->request_q);
324  
325  	trace_smb3_adj_credits(server->CurrentMid,
326  			server->conn_id, server->hostname, scredits,
327  			credits->value - new_val, in_flight);
328  	cifs_dbg(FYI, "%s: adjust added %u credits total=%d\n",
329  			__func__, credits->value - new_val, scredits);
330  
331  	credits->value = new_val;
332  
333  	return 0;
334  }
335  
336  static __u64
337  smb2_get_next_mid(struct TCP_Server_Info *server)
338  {
339  	__u64 mid;
340  	/* for SMB2 we need the current value */
341  	spin_lock(&server->mid_lock);
342  	mid = server->CurrentMid++;
343  	spin_unlock(&server->mid_lock);
344  	return mid;
345  }
346  
347  static void
348  smb2_revert_current_mid(struct TCP_Server_Info *server, const unsigned int val)
349  {
350  	spin_lock(&server->mid_lock);
351  	if (server->CurrentMid >= val)
352  		server->CurrentMid -= val;
353  	spin_unlock(&server->mid_lock);
354  }
355  
356  static struct mid_q_entry *
357  __smb2_find_mid(struct TCP_Server_Info *server, char *buf, bool dequeue)
358  {
359  	struct mid_q_entry *mid;
360  	struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
361  	__u64 wire_mid = le64_to_cpu(shdr->MessageId);
362  
363  	if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
364  		cifs_server_dbg(VFS, "Encrypted frame parsing not supported yet\n");
365  		return NULL;
366  	}
367  
368  	spin_lock(&server->mid_lock);
369  	list_for_each_entry(mid, &server->pending_mid_q, qhead) {
370  		if ((mid->mid == wire_mid) &&
371  		    (mid->mid_state == MID_REQUEST_SUBMITTED) &&
372  		    (mid->command == shdr->Command)) {
373  			kref_get(&mid->refcount);
374  			if (dequeue) {
375  				list_del_init(&mid->qhead);
376  				mid->mid_flags |= MID_DELETED;
377  			}
378  			spin_unlock(&server->mid_lock);
379  			return mid;
380  		}
381  	}
382  	spin_unlock(&server->mid_lock);
383  	return NULL;
384  }
385  
386  static struct mid_q_entry *
387  smb2_find_mid(struct TCP_Server_Info *server, char *buf)
388  {
389  	return __smb2_find_mid(server, buf, false);
390  }
391  
392  static struct mid_q_entry *
393  smb2_find_dequeue_mid(struct TCP_Server_Info *server, char *buf)
394  {
395  	return __smb2_find_mid(server, buf, true);
396  }
397  
398  static void
399  smb2_dump_detail(void *buf, struct TCP_Server_Info *server)
400  {
401  #ifdef CONFIG_CIFS_DEBUG2
402  	struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
403  
404  	cifs_server_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
405  		 shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
406  		 shdr->Id.SyncId.ProcessId);
407  	if (!server->ops->check_message(buf, server->total_read, server)) {
408  		cifs_server_dbg(VFS, "smb buf %p len %u\n", buf,
409  				server->ops->calc_smb_size(buf));
410  	}
411  #endif
412  }
413  
414  static bool
415  smb2_need_neg(struct TCP_Server_Info *server)
416  {
417  	return server->max_read == 0;
418  }
419  
420  static int
421  smb2_negotiate(const unsigned int xid,
422  	       struct cifs_ses *ses,
423  	       struct TCP_Server_Info *server)
424  {
425  	int rc;
426  
427  	spin_lock(&server->mid_lock);
428  	server->CurrentMid = 0;
429  	spin_unlock(&server->mid_lock);
430  	rc = SMB2_negotiate(xid, ses, server);
431  	/* BB we probably don't need to retry with modern servers */
432  	if (rc == -EAGAIN)
433  		rc = -EHOSTDOWN;
434  	return rc;
435  }
436  
437  static unsigned int
438  smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
439  {
440  	struct TCP_Server_Info *server = tcon->ses->server;
441  	unsigned int wsize;
442  
443  	/* start with specified wsize, or default */
444  	wsize = ctx->wsize ? ctx->wsize : CIFS_DEFAULT_IOSIZE;
445  	wsize = min_t(unsigned int, wsize, server->max_write);
446  	if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
447  		wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
448  
449  	return wsize;
450  }
451  
452  static unsigned int
453  smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
454  {
455  	struct TCP_Server_Info *server = tcon->ses->server;
456  	unsigned int wsize;
457  
458  	/* start with specified wsize, or default */
459  	wsize = ctx->wsize ? ctx->wsize : SMB3_DEFAULT_IOSIZE;
460  	wsize = min_t(unsigned int, wsize, server->max_write);
461  #ifdef CONFIG_CIFS_SMB_DIRECT
462  	if (server->rdma) {
463  		if (server->sign)
464  			/*
465  			 * Account for SMB2 data transfer packet header and
466  			 * possible encryption header
467  			 */
468  			wsize = min_t(unsigned int,
469  				wsize,
470  				server->smbd_conn->max_fragmented_send_size -
471  					SMB2_READWRITE_PDU_HEADER_SIZE -
472  					sizeof(struct smb2_transform_hdr));
473  		else
474  			wsize = min_t(unsigned int,
475  				wsize, server->smbd_conn->max_readwrite_size);
476  	}
477  #endif
478  	if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
479  		wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
480  
481  	return wsize;
482  }
483  
484  static unsigned int
485  smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
486  {
487  	struct TCP_Server_Info *server = tcon->ses->server;
488  	unsigned int rsize;
489  
490  	/* start with specified rsize, or default */
491  	rsize = ctx->rsize ? ctx->rsize : CIFS_DEFAULT_IOSIZE;
492  	rsize = min_t(unsigned int, rsize, server->max_read);
493  
494  	if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
495  		rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
496  
497  	return rsize;
498  }
499  
500  static unsigned int
501  smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
502  {
503  	struct TCP_Server_Info *server = tcon->ses->server;
504  	unsigned int rsize;
505  
506  	/* start with specified rsize, or default */
507  	rsize = ctx->rsize ? ctx->rsize : SMB3_DEFAULT_IOSIZE;
508  	rsize = min_t(unsigned int, rsize, server->max_read);
509  #ifdef CONFIG_CIFS_SMB_DIRECT
510  	if (server->rdma) {
511  		if (server->sign)
512  			/*
513  			 * Account for SMB2 data transfer packet header and
514  			 * possible encryption header
515  			 */
516  			rsize = min_t(unsigned int,
517  				rsize,
518  				server->smbd_conn->max_fragmented_recv_size -
519  					SMB2_READWRITE_PDU_HEADER_SIZE -
520  					sizeof(struct smb2_transform_hdr));
521  		else
522  			rsize = min_t(unsigned int,
523  				rsize, server->smbd_conn->max_readwrite_size);
524  	}
525  #endif
526  
527  	if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
528  		rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
529  
530  	return rsize;
531  }
532  
533  /*
534   * compare two interfaces a and b
535   * return 0 if everything matches.
536   * return 1 if a is rdma capable, or rss capable, or has higher link speed
537   * return -1 otherwise.
538   */
539  static int
540  iface_cmp(struct cifs_server_iface *a, struct cifs_server_iface *b)
541  {
542  	int cmp_ret = 0;
543  
544  	WARN_ON(!a || !b);
545  	if (a->rdma_capable == b->rdma_capable) {
546  		if (a->rss_capable == b->rss_capable) {
547  			if (a->speed == b->speed) {
548  				cmp_ret = cifs_ipaddr_cmp((struct sockaddr *) &a->sockaddr,
549  							  (struct sockaddr *) &b->sockaddr);
550  				if (!cmp_ret)
551  					return 0;
552  				else if (cmp_ret > 0)
553  					return 1;
554  				else
555  					return -1;
556  			} else if (a->speed > b->speed)
557  				return 1;
558  			else
559  				return -1;
560  		} else if (a->rss_capable > b->rss_capable)
561  			return 1;
562  		else
563  			return -1;
564  	} else if (a->rdma_capable > b->rdma_capable)
565  		return 1;
566  	else
567  		return -1;
568  }
569  
570  static int
571  parse_server_interfaces(struct network_interface_info_ioctl_rsp *buf,
572  			size_t buf_len, struct cifs_ses *ses, bool in_mount)
573  {
574  	struct network_interface_info_ioctl_rsp *p;
575  	struct sockaddr_in *addr4;
576  	struct sockaddr_in6 *addr6;
577  	struct iface_info_ipv4 *p4;
578  	struct iface_info_ipv6 *p6;
579  	struct cifs_server_iface *info = NULL, *iface = NULL, *niface = NULL;
580  	struct cifs_server_iface tmp_iface;
581  	ssize_t bytes_left;
582  	size_t next = 0;
583  	int nb_iface = 0;
584  	int rc = 0, ret = 0;
585  
586  	bytes_left = buf_len;
587  	p = buf;
588  
589  	spin_lock(&ses->iface_lock);
590  	/* do not query too frequently, this time with lock held */
591  	if (ses->iface_last_update &&
592  	    time_before(jiffies, ses->iface_last_update +
593  			(SMB_INTERFACE_POLL_INTERVAL * HZ))) {
594  		spin_unlock(&ses->iface_lock);
595  		return 0;
596  	}
597  
598  	/*
599  	 * Go through iface_list and mark them as inactive
600  	 */
601  	list_for_each_entry_safe(iface, niface, &ses->iface_list,
602  				 iface_head)
603  		iface->is_active = 0;
604  
605  	spin_unlock(&ses->iface_lock);
606  
607  	/*
608  	 * Samba server e.g. can return an empty interface list in some cases,
609  	 * which would only be a problem if we were requesting multichannel
610  	 */
611  	if (bytes_left == 0) {
612  		/* avoid spamming logs every 10 minutes, so log only in mount */
613  		if ((ses->chan_max > 1) && in_mount)
614  			cifs_dbg(VFS,
615  				 "multichannel not available\n"
616  				 "Empty network interface list returned by server %s\n",
617  				 ses->server->hostname);
618  		rc = -EOPNOTSUPP;
619  		ses->iface_last_update = jiffies;
620  		goto out;
621  	}
622  
623  	while (bytes_left >= (ssize_t)sizeof(*p)) {
624  		memset(&tmp_iface, 0, sizeof(tmp_iface));
625  		tmp_iface.speed = le64_to_cpu(p->LinkSpeed);
626  		tmp_iface.rdma_capable = le32_to_cpu(p->Capability & RDMA_CAPABLE) ? 1 : 0;
627  		tmp_iface.rss_capable = le32_to_cpu(p->Capability & RSS_CAPABLE) ? 1 : 0;
628  
629  		switch (p->Family) {
630  		/*
631  		 * The kernel and wire socket structures have the same
632  		 * layout and use network byte order but make the
633  		 * conversion explicit in case either one changes.
634  		 */
635  		case INTERNETWORK:
636  			addr4 = (struct sockaddr_in *)&tmp_iface.sockaddr;
637  			p4 = (struct iface_info_ipv4 *)p->Buffer;
638  			addr4->sin_family = AF_INET;
639  			memcpy(&addr4->sin_addr, &p4->IPv4Address, 4);
640  
641  			/* [MS-SMB2] 2.2.32.5.1.1 Clients MUST ignore these */
642  			addr4->sin_port = cpu_to_be16(CIFS_PORT);
643  
644  			cifs_dbg(FYI, "%s: ipv4 %pI4\n", __func__,
645  				 &addr4->sin_addr);
646  			break;
647  		case INTERNETWORKV6:
648  			addr6 =	(struct sockaddr_in6 *)&tmp_iface.sockaddr;
649  			p6 = (struct iface_info_ipv6 *)p->Buffer;
650  			addr6->sin6_family = AF_INET6;
651  			memcpy(&addr6->sin6_addr, &p6->IPv6Address, 16);
652  
653  			/* [MS-SMB2] 2.2.32.5.1.2 Clients MUST ignore these */
654  			addr6->sin6_flowinfo = 0;
655  			addr6->sin6_scope_id = 0;
656  			addr6->sin6_port = cpu_to_be16(CIFS_PORT);
657  
658  			cifs_dbg(FYI, "%s: ipv6 %pI6\n", __func__,
659  				 &addr6->sin6_addr);
660  			break;
661  		default:
662  			cifs_dbg(VFS,
663  				 "%s: skipping unsupported socket family\n",
664  				 __func__);
665  			goto next_iface;
666  		}
667  
668  		/*
669  		 * The iface_list is assumed to be sorted by speed.
670  		 * Check if the new interface exists in that list.
671  		 * NEVER change iface. it could be in use.
672  		 * Add a new one instead
673  		 */
674  		spin_lock(&ses->iface_lock);
675  		list_for_each_entry_safe(iface, niface, &ses->iface_list,
676  					 iface_head) {
677  			ret = iface_cmp(iface, &tmp_iface);
678  			if (!ret) {
679  				iface->is_active = 1;
680  				spin_unlock(&ses->iface_lock);
681  				goto next_iface;
682  			} else if (ret < 0) {
683  				/* all remaining ifaces are slower */
684  				kref_get(&iface->refcount);
685  				break;
686  			}
687  		}
688  		spin_unlock(&ses->iface_lock);
689  
690  		/* no match. insert the entry in the list */
691  		info = kmalloc(sizeof(struct cifs_server_iface),
692  			       GFP_KERNEL);
693  		if (!info) {
694  			rc = -ENOMEM;
695  			goto out;
696  		}
697  		memcpy(info, &tmp_iface, sizeof(tmp_iface));
698  
699  		/* add this new entry to the list */
700  		kref_init(&info->refcount);
701  		info->is_active = 1;
702  
703  		cifs_dbg(FYI, "%s: adding iface %zu\n", __func__, ses->iface_count);
704  		cifs_dbg(FYI, "%s: speed %zu bps\n", __func__, info->speed);
705  		cifs_dbg(FYI, "%s: capabilities 0x%08x\n", __func__,
706  			 le32_to_cpu(p->Capability));
707  
708  		spin_lock(&ses->iface_lock);
709  		if (!list_entry_is_head(iface, &ses->iface_list, iface_head)) {
710  			list_add_tail(&info->iface_head, &iface->iface_head);
711  			kref_put(&iface->refcount, release_iface);
712  		} else
713  			list_add_tail(&info->iface_head, &ses->iface_list);
714  
715  		ses->iface_count++;
716  		spin_unlock(&ses->iface_lock);
717  next_iface:
718  		nb_iface++;
719  		next = le32_to_cpu(p->Next);
720  		if (!next) {
721  			bytes_left -= sizeof(*p);
722  			break;
723  		}
724  		p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
725  		bytes_left -= next;
726  	}
727  
728  	if (!nb_iface) {
729  		cifs_dbg(VFS, "%s: malformed interface info\n", __func__);
730  		rc = -EINVAL;
731  		goto out;
732  	}
733  
734  	/* Azure rounds the buffer size up 8, to a 16 byte boundary */
735  	if ((bytes_left > 8) || p->Next)
736  		cifs_dbg(VFS, "%s: incomplete interface info\n", __func__);
737  
738  	ses->iface_last_update = jiffies;
739  
740  out:
741  	/*
742  	 * Go through the list again and put the inactive entries
743  	 */
744  	spin_lock(&ses->iface_lock);
745  	list_for_each_entry_safe(iface, niface, &ses->iface_list,
746  				 iface_head) {
747  		if (!iface->is_active) {
748  			list_del(&iface->iface_head);
749  			kref_put(&iface->refcount, release_iface);
750  			ses->iface_count--;
751  		}
752  	}
753  	spin_unlock(&ses->iface_lock);
754  
755  	return rc;
756  }
757  
758  int
759  SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon, bool in_mount)
760  {
761  	int rc;
762  	unsigned int ret_data_len = 0;
763  	struct network_interface_info_ioctl_rsp *out_buf = NULL;
764  	struct cifs_ses *ses = tcon->ses;
765  	struct TCP_Server_Info *pserver;
766  
767  	/* do not query too frequently */
768  	if (ses->iface_last_update &&
769  	    time_before(jiffies, ses->iface_last_update +
770  			(SMB_INTERFACE_POLL_INTERVAL * HZ)))
771  		return 0;
772  
773  	rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
774  			FSCTL_QUERY_NETWORK_INTERFACE_INFO,
775  			NULL /* no data input */, 0 /* no data input */,
776  			CIFSMaxBufSize, (char **)&out_buf, &ret_data_len);
777  	if (rc == -EOPNOTSUPP) {
778  		cifs_dbg(FYI,
779  			 "server does not support query network interfaces\n");
780  		ret_data_len = 0;
781  	} else if (rc != 0) {
782  		cifs_tcon_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
783  		goto out;
784  	}
785  
786  	rc = parse_server_interfaces(out_buf, ret_data_len, ses, in_mount);
787  	if (rc)
788  		goto out;
789  
790  	/* check if iface is still active */
791  	spin_lock(&ses->chan_lock);
792  	pserver = ses->chans[0].server;
793  	if (pserver && !cifs_chan_is_iface_active(ses, pserver)) {
794  		spin_unlock(&ses->chan_lock);
795  		cifs_chan_update_iface(ses, pserver);
796  		spin_lock(&ses->chan_lock);
797  	}
798  	spin_unlock(&ses->chan_lock);
799  
800  out:
801  	kfree(out_buf);
802  	return rc;
803  }
804  
805  static void
806  smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
807  	      struct cifs_sb_info *cifs_sb)
808  {
809  	int rc;
810  	__le16 srch_path = 0; /* Null - open root of share */
811  	u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
812  	struct cifs_open_parms oparms;
813  	struct cifs_fid fid;
814  	struct cached_fid *cfid = NULL;
815  
816  	oparms = (struct cifs_open_parms) {
817  		.tcon = tcon,
818  		.path = "",
819  		.desired_access = FILE_READ_ATTRIBUTES,
820  		.disposition = FILE_OPEN,
821  		.create_options = cifs_create_options(cifs_sb, 0),
822  		.fid = &fid,
823  	};
824  
825  	rc = open_cached_dir(xid, tcon, "", cifs_sb, false, &cfid);
826  	if (rc == 0)
827  		memcpy(&fid, &cfid->fid, sizeof(struct cifs_fid));
828  	else
829  		rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
830  			       NULL, NULL);
831  	if (rc)
832  		return;
833  
834  	SMB3_request_interfaces(xid, tcon, true /* called during  mount */);
835  
836  	SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
837  			FS_ATTRIBUTE_INFORMATION);
838  	SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
839  			FS_DEVICE_INFORMATION);
840  	SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
841  			FS_VOLUME_INFORMATION);
842  	SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
843  			FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
844  	if (cfid == NULL)
845  		SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
846  	else
847  		close_cached_dir(cfid);
848  }
849  
850  static void
851  smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
852  	      struct cifs_sb_info *cifs_sb)
853  {
854  	int rc;
855  	__le16 srch_path = 0; /* Null - open root of share */
856  	u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
857  	struct cifs_open_parms oparms;
858  	struct cifs_fid fid;
859  
860  	oparms = (struct cifs_open_parms) {
861  		.tcon = tcon,
862  		.path = "",
863  		.desired_access = FILE_READ_ATTRIBUTES,
864  		.disposition = FILE_OPEN,
865  		.create_options = cifs_create_options(cifs_sb, 0),
866  		.fid = &fid,
867  	};
868  
869  	rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
870  		       NULL, NULL);
871  	if (rc)
872  		return;
873  
874  	SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
875  			FS_ATTRIBUTE_INFORMATION);
876  	SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
877  			FS_DEVICE_INFORMATION);
878  	SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
879  }
880  
881  static int
882  smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
883  			struct cifs_sb_info *cifs_sb, const char *full_path)
884  {
885  	__le16 *utf16_path;
886  	__u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
887  	int err_buftype = CIFS_NO_BUFFER;
888  	struct cifs_open_parms oparms;
889  	struct kvec err_iov = {};
890  	struct cifs_fid fid;
891  	struct cached_fid *cfid;
892  	bool islink;
893  	int rc, rc2;
894  
895  	rc = open_cached_dir(xid, tcon, full_path, cifs_sb, true, &cfid);
896  	if (!rc) {
897  		if (cfid->has_lease) {
898  			close_cached_dir(cfid);
899  			return 0;
900  		}
901  		close_cached_dir(cfid);
902  	}
903  
904  	utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
905  	if (!utf16_path)
906  		return -ENOMEM;
907  
908  	oparms = (struct cifs_open_parms) {
909  		.tcon = tcon,
910  		.path = full_path,
911  		.desired_access = FILE_READ_ATTRIBUTES,
912  		.disposition = FILE_OPEN,
913  		.create_options = cifs_create_options(cifs_sb, 0),
914  		.fid = &fid,
915  	};
916  
917  	rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL,
918  		       &err_iov, &err_buftype);
919  	if (rc) {
920  		struct smb2_hdr *hdr = err_iov.iov_base;
921  
922  		if (unlikely(!hdr || err_buftype == CIFS_NO_BUFFER))
923  			goto out;
924  
925  		if (rc != -EREMOTE && hdr->Status == STATUS_OBJECT_NAME_INVALID) {
926  			rc2 = cifs_inval_name_dfs_link_error(xid, tcon, cifs_sb,
927  							     full_path, &islink);
928  			if (rc2) {
929  				rc = rc2;
930  				goto out;
931  			}
932  			if (islink)
933  				rc = -EREMOTE;
934  		}
935  		if (rc == -EREMOTE && IS_ENABLED(CONFIG_CIFS_DFS_UPCALL) && cifs_sb &&
936  		    (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS))
937  			rc = -EOPNOTSUPP;
938  		goto out;
939  	}
940  
941  	rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
942  
943  out:
944  	free_rsp_buf(err_buftype, err_iov.iov_base);
945  	kfree(utf16_path);
946  	return rc;
947  }
948  
949  static int smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
950  			     struct cifs_sb_info *cifs_sb, const char *full_path,
951  			     u64 *uniqueid, struct cifs_open_info_data *data)
952  {
953  	*uniqueid = le64_to_cpu(data->fi.IndexNumber);
954  	return 0;
955  }
956  
957  static int smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
958  				struct cifsFileInfo *cfile, struct cifs_open_info_data *data)
959  {
960  	struct cifs_fid *fid = &cfile->fid;
961  
962  	if (cfile->symlink_target) {
963  		data->symlink_target = kstrdup(cfile->symlink_target, GFP_KERNEL);
964  		if (!data->symlink_target)
965  			return -ENOMEM;
966  	}
967  	return SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid, &data->fi);
968  }
969  
970  #ifdef CONFIG_CIFS_XATTR
971  static ssize_t
972  move_smb2_ea_to_cifs(char *dst, size_t dst_size,
973  		     struct smb2_file_full_ea_info *src, size_t src_size,
974  		     const unsigned char *ea_name)
975  {
976  	int rc = 0;
977  	unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
978  	char *name, *value;
979  	size_t buf_size = dst_size;
980  	size_t name_len, value_len, user_name_len;
981  
982  	while (src_size > 0) {
983  		name_len = (size_t)src->ea_name_length;
984  		value_len = (size_t)le16_to_cpu(src->ea_value_length);
985  
986  		if (name_len == 0)
987  			break;
988  
989  		if (src_size < 8 + name_len + 1 + value_len) {
990  			cifs_dbg(FYI, "EA entry goes beyond length of list\n");
991  			rc = -EIO;
992  			goto out;
993  		}
994  
995  		name = &src->ea_data[0];
996  		value = &src->ea_data[src->ea_name_length + 1];
997  
998  		if (ea_name) {
999  			if (ea_name_len == name_len &&
1000  			    memcmp(ea_name, name, name_len) == 0) {
1001  				rc = value_len;
1002  				if (dst_size == 0)
1003  					goto out;
1004  				if (dst_size < value_len) {
1005  					rc = -ERANGE;
1006  					goto out;
1007  				}
1008  				memcpy(dst, value, value_len);
1009  				goto out;
1010  			}
1011  		} else {
1012  			/* 'user.' plus a terminating null */
1013  			user_name_len = 5 + 1 + name_len;
1014  
1015  			if (buf_size == 0) {
1016  				/* skip copy - calc size only */
1017  				rc += user_name_len;
1018  			} else if (dst_size >= user_name_len) {
1019  				dst_size -= user_name_len;
1020  				memcpy(dst, "user.", 5);
1021  				dst += 5;
1022  				memcpy(dst, src->ea_data, name_len);
1023  				dst += name_len;
1024  				*dst = 0;
1025  				++dst;
1026  				rc += user_name_len;
1027  			} else {
1028  				/* stop before overrun buffer */
1029  				rc = -ERANGE;
1030  				break;
1031  			}
1032  		}
1033  
1034  		if (!src->next_entry_offset)
1035  			break;
1036  
1037  		if (src_size < le32_to_cpu(src->next_entry_offset)) {
1038  			/* stop before overrun buffer */
1039  			rc = -ERANGE;
1040  			break;
1041  		}
1042  		src_size -= le32_to_cpu(src->next_entry_offset);
1043  		src = (void *)((char *)src +
1044  			       le32_to_cpu(src->next_entry_offset));
1045  	}
1046  
1047  	/* didn't find the named attribute */
1048  	if (ea_name)
1049  		rc = -ENODATA;
1050  
1051  out:
1052  	return (ssize_t)rc;
1053  }
1054  
1055  static ssize_t
1056  smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
1057  	       const unsigned char *path, const unsigned char *ea_name,
1058  	       char *ea_data, size_t buf_size,
1059  	       struct cifs_sb_info *cifs_sb)
1060  {
1061  	int rc;
1062  	struct kvec rsp_iov = {NULL, 0};
1063  	int buftype = CIFS_NO_BUFFER;
1064  	struct smb2_query_info_rsp *rsp;
1065  	struct smb2_file_full_ea_info *info = NULL;
1066  
1067  	rc = smb2_query_info_compound(xid, tcon, path,
1068  				      FILE_READ_EA,
1069  				      FILE_FULL_EA_INFORMATION,
1070  				      SMB2_O_INFO_FILE,
1071  				      CIFSMaxBufSize -
1072  				      MAX_SMB2_CREATE_RESPONSE_SIZE -
1073  				      MAX_SMB2_CLOSE_RESPONSE_SIZE,
1074  				      &rsp_iov, &buftype, cifs_sb);
1075  	if (rc) {
1076  		/*
1077  		 * If ea_name is NULL (listxattr) and there are no EAs,
1078  		 * return 0 as it's not an error. Otherwise, the specified
1079  		 * ea_name was not found.
1080  		 */
1081  		if (!ea_name && rc == -ENODATA)
1082  			rc = 0;
1083  		goto qeas_exit;
1084  	}
1085  
1086  	rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
1087  	rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
1088  			       le32_to_cpu(rsp->OutputBufferLength),
1089  			       &rsp_iov,
1090  			       sizeof(struct smb2_file_full_ea_info));
1091  	if (rc)
1092  		goto qeas_exit;
1093  
1094  	info = (struct smb2_file_full_ea_info *)(
1095  			le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
1096  	rc = move_smb2_ea_to_cifs(ea_data, buf_size, info,
1097  			le32_to_cpu(rsp->OutputBufferLength), ea_name);
1098  
1099   qeas_exit:
1100  	free_rsp_buf(buftype, rsp_iov.iov_base);
1101  	return rc;
1102  }
1103  
1104  static int
1105  smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
1106  	    const char *path, const char *ea_name, const void *ea_value,
1107  	    const __u16 ea_value_len, const struct nls_table *nls_codepage,
1108  	    struct cifs_sb_info *cifs_sb)
1109  {
1110  	struct smb2_compound_vars *vars;
1111  	struct cifs_ses *ses = tcon->ses;
1112  	struct TCP_Server_Info *server;
1113  	struct smb_rqst *rqst;
1114  	struct kvec *rsp_iov;
1115  	__le16 *utf16_path = NULL;
1116  	int ea_name_len = strlen(ea_name);
1117  	int flags = CIFS_CP_CREATE_CLOSE_OP;
1118  	int len;
1119  	int resp_buftype[3];
1120  	struct cifs_open_parms oparms;
1121  	__u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1122  	struct cifs_fid fid;
1123  	unsigned int size[1];
1124  	void *data[1];
1125  	struct smb2_file_full_ea_info *ea;
1126  	struct smb2_query_info_rsp *rsp;
1127  	int rc, used_len = 0;
1128  	int retries = 0, cur_sleep = 1;
1129  
1130  replay_again:
1131  	/* reinitialize for possible replay */
1132  	flags = CIFS_CP_CREATE_CLOSE_OP;
1133  	oplock = SMB2_OPLOCK_LEVEL_NONE;
1134  	server = cifs_pick_channel(ses);
1135  
1136  	if (smb3_encryption_required(tcon))
1137  		flags |= CIFS_TRANSFORM_REQ;
1138  
1139  	if (ea_name_len > 255)
1140  		return -EINVAL;
1141  
1142  	utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1143  	if (!utf16_path)
1144  		return -ENOMEM;
1145  
1146  	ea = NULL;
1147  	resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1148  	vars = kzalloc(sizeof(*vars), GFP_KERNEL);
1149  	if (!vars) {
1150  		rc = -ENOMEM;
1151  		goto out_free_path;
1152  	}
1153  	rqst = vars->rqst;
1154  	rsp_iov = vars->rsp_iov;
1155  
1156  	if (ses->server->ops->query_all_EAs) {
1157  		if (!ea_value) {
1158  			rc = ses->server->ops->query_all_EAs(xid, tcon, path,
1159  							     ea_name, NULL, 0,
1160  							     cifs_sb);
1161  			if (rc == -ENODATA)
1162  				goto sea_exit;
1163  		} else {
1164  			/* If we are adding a attribute we should first check
1165  			 * if there will be enough space available to store
1166  			 * the new EA. If not we should not add it since we
1167  			 * would not be able to even read the EAs back.
1168  			 */
1169  			rc = smb2_query_info_compound(xid, tcon, path,
1170  				      FILE_READ_EA,
1171  				      FILE_FULL_EA_INFORMATION,
1172  				      SMB2_O_INFO_FILE,
1173  				      CIFSMaxBufSize -
1174  				      MAX_SMB2_CREATE_RESPONSE_SIZE -
1175  				      MAX_SMB2_CLOSE_RESPONSE_SIZE,
1176  				      &rsp_iov[1], &resp_buftype[1], cifs_sb);
1177  			if (rc == 0) {
1178  				rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1179  				used_len = le32_to_cpu(rsp->OutputBufferLength);
1180  			}
1181  			free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1182  			resp_buftype[1] = CIFS_NO_BUFFER;
1183  			memset(&rsp_iov[1], 0, sizeof(rsp_iov[1]));
1184  			rc = 0;
1185  
1186  			/* Use a fudge factor of 256 bytes in case we collide
1187  			 * with a different set_EAs command.
1188  			 */
1189  			if (CIFSMaxBufSize - MAX_SMB2_CREATE_RESPONSE_SIZE -
1190  			   MAX_SMB2_CLOSE_RESPONSE_SIZE - 256 <
1191  			   used_len + ea_name_len + ea_value_len + 1) {
1192  				rc = -ENOSPC;
1193  				goto sea_exit;
1194  			}
1195  		}
1196  	}
1197  
1198  	/* Open */
1199  	rqst[0].rq_iov = vars->open_iov;
1200  	rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1201  
1202  	oparms = (struct cifs_open_parms) {
1203  		.tcon = tcon,
1204  		.path = path,
1205  		.desired_access = FILE_WRITE_EA,
1206  		.disposition = FILE_OPEN,
1207  		.create_options = cifs_create_options(cifs_sb, 0),
1208  		.fid = &fid,
1209  		.replay = !!(retries),
1210  	};
1211  
1212  	rc = SMB2_open_init(tcon, server,
1213  			    &rqst[0], &oplock, &oparms, utf16_path);
1214  	if (rc)
1215  		goto sea_exit;
1216  	smb2_set_next_command(tcon, &rqst[0]);
1217  
1218  
1219  	/* Set Info */
1220  	rqst[1].rq_iov = vars->si_iov;
1221  	rqst[1].rq_nvec = 1;
1222  
1223  	len = sizeof(*ea) + ea_name_len + ea_value_len + 1;
1224  	ea = kzalloc(len, GFP_KERNEL);
1225  	if (ea == NULL) {
1226  		rc = -ENOMEM;
1227  		goto sea_exit;
1228  	}
1229  
1230  	ea->ea_name_length = ea_name_len;
1231  	ea->ea_value_length = cpu_to_le16(ea_value_len);
1232  	memcpy(ea->ea_data, ea_name, ea_name_len + 1);
1233  	memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
1234  
1235  	size[0] = len;
1236  	data[0] = ea;
1237  
1238  	rc = SMB2_set_info_init(tcon, server,
1239  				&rqst[1], COMPOUND_FID,
1240  				COMPOUND_FID, current->tgid,
1241  				FILE_FULL_EA_INFORMATION,
1242  				SMB2_O_INFO_FILE, 0, data, size);
1243  	if (rc)
1244  		goto sea_exit;
1245  	smb2_set_next_command(tcon, &rqst[1]);
1246  	smb2_set_related(&rqst[1]);
1247  
1248  	/* Close */
1249  	rqst[2].rq_iov = &vars->close_iov;
1250  	rqst[2].rq_nvec = 1;
1251  	rc = SMB2_close_init(tcon, server,
1252  			     &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
1253  	if (rc)
1254  		goto sea_exit;
1255  	smb2_set_related(&rqst[2]);
1256  
1257  	if (retries) {
1258  		smb2_set_replay(server, &rqst[0]);
1259  		smb2_set_replay(server, &rqst[1]);
1260  		smb2_set_replay(server, &rqst[2]);
1261  	}
1262  
1263  	rc = compound_send_recv(xid, ses, server,
1264  				flags, 3, rqst,
1265  				resp_buftype, rsp_iov);
1266  	/* no need to bump num_remote_opens because handle immediately closed */
1267  
1268   sea_exit:
1269  	kfree(ea);
1270  	SMB2_open_free(&rqst[0]);
1271  	SMB2_set_info_free(&rqst[1]);
1272  	SMB2_close_free(&rqst[2]);
1273  	free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1274  	free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1275  	free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1276  	kfree(vars);
1277  out_free_path:
1278  	kfree(utf16_path);
1279  
1280  	if (is_replayable_error(rc) &&
1281  	    smb2_should_replay(tcon, &retries, &cur_sleep))
1282  		goto replay_again;
1283  
1284  	return rc;
1285  }
1286  #endif
1287  
1288  static bool
1289  smb2_can_echo(struct TCP_Server_Info *server)
1290  {
1291  	return server->echoes;
1292  }
1293  
1294  static void
1295  smb2_clear_stats(struct cifs_tcon *tcon)
1296  {
1297  	int i;
1298  
1299  	for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
1300  		atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
1301  		atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
1302  	}
1303  }
1304  
1305  static void
1306  smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
1307  {
1308  	seq_puts(m, "\n\tShare Capabilities:");
1309  	if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
1310  		seq_puts(m, " DFS,");
1311  	if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
1312  		seq_puts(m, " CONTINUOUS AVAILABILITY,");
1313  	if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
1314  		seq_puts(m, " SCALEOUT,");
1315  	if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
1316  		seq_puts(m, " CLUSTER,");
1317  	if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
1318  		seq_puts(m, " ASYMMETRIC,");
1319  	if (tcon->capabilities == 0)
1320  		seq_puts(m, " None");
1321  	if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
1322  		seq_puts(m, " Aligned,");
1323  	if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
1324  		seq_puts(m, " Partition Aligned,");
1325  	if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
1326  		seq_puts(m, " SSD,");
1327  	if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
1328  		seq_puts(m, " TRIM-support,");
1329  
1330  	seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
1331  	seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
1332  	if (tcon->perf_sector_size)
1333  		seq_printf(m, "\tOptimal sector size: 0x%x",
1334  			   tcon->perf_sector_size);
1335  	seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
1336  }
1337  
1338  static void
1339  smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
1340  {
1341  	atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
1342  	atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
1343  
1344  	/*
1345  	 *  Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
1346  	 *  totals (requests sent) since those SMBs are per-session not per tcon
1347  	 */
1348  	seq_printf(m, "\nBytes read: %llu  Bytes written: %llu",
1349  		   (long long)(tcon->bytes_read),
1350  		   (long long)(tcon->bytes_written));
1351  	seq_printf(m, "\nOpen files: %d total (local), %d open on server",
1352  		   atomic_read(&tcon->num_local_opens),
1353  		   atomic_read(&tcon->num_remote_opens));
1354  	seq_printf(m, "\nTreeConnects: %d total %d failed",
1355  		   atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
1356  		   atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
1357  	seq_printf(m, "\nTreeDisconnects: %d total %d failed",
1358  		   atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
1359  		   atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
1360  	seq_printf(m, "\nCreates: %d total %d failed",
1361  		   atomic_read(&sent[SMB2_CREATE_HE]),
1362  		   atomic_read(&failed[SMB2_CREATE_HE]));
1363  	seq_printf(m, "\nCloses: %d total %d failed",
1364  		   atomic_read(&sent[SMB2_CLOSE_HE]),
1365  		   atomic_read(&failed[SMB2_CLOSE_HE]));
1366  	seq_printf(m, "\nFlushes: %d total %d failed",
1367  		   atomic_read(&sent[SMB2_FLUSH_HE]),
1368  		   atomic_read(&failed[SMB2_FLUSH_HE]));
1369  	seq_printf(m, "\nReads: %d total %d failed",
1370  		   atomic_read(&sent[SMB2_READ_HE]),
1371  		   atomic_read(&failed[SMB2_READ_HE]));
1372  	seq_printf(m, "\nWrites: %d total %d failed",
1373  		   atomic_read(&sent[SMB2_WRITE_HE]),
1374  		   atomic_read(&failed[SMB2_WRITE_HE]));
1375  	seq_printf(m, "\nLocks: %d total %d failed",
1376  		   atomic_read(&sent[SMB2_LOCK_HE]),
1377  		   atomic_read(&failed[SMB2_LOCK_HE]));
1378  	seq_printf(m, "\nIOCTLs: %d total %d failed",
1379  		   atomic_read(&sent[SMB2_IOCTL_HE]),
1380  		   atomic_read(&failed[SMB2_IOCTL_HE]));
1381  	seq_printf(m, "\nQueryDirectories: %d total %d failed",
1382  		   atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
1383  		   atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
1384  	seq_printf(m, "\nChangeNotifies: %d total %d failed",
1385  		   atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
1386  		   atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
1387  	seq_printf(m, "\nQueryInfos: %d total %d failed",
1388  		   atomic_read(&sent[SMB2_QUERY_INFO_HE]),
1389  		   atomic_read(&failed[SMB2_QUERY_INFO_HE]));
1390  	seq_printf(m, "\nSetInfos: %d total %d failed",
1391  		   atomic_read(&sent[SMB2_SET_INFO_HE]),
1392  		   atomic_read(&failed[SMB2_SET_INFO_HE]));
1393  	seq_printf(m, "\nOplockBreaks: %d sent %d failed",
1394  		   atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
1395  		   atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
1396  }
1397  
1398  static void
1399  smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
1400  {
1401  	struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1402  	struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1403  
1404  	cfile->fid.persistent_fid = fid->persistent_fid;
1405  	cfile->fid.volatile_fid = fid->volatile_fid;
1406  	cfile->fid.access = fid->access;
1407  #ifdef CONFIG_CIFS_DEBUG2
1408  	cfile->fid.mid = fid->mid;
1409  #endif /* CIFS_DEBUG2 */
1410  	server->ops->set_oplock_level(cinode, oplock, fid->epoch,
1411  				      &fid->purge_cache);
1412  	cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
1413  	memcpy(cfile->fid.create_guid, fid->create_guid, 16);
1414  }
1415  
1416  static int
1417  smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
1418  		struct cifs_fid *fid)
1419  {
1420  	return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1421  }
1422  
1423  static int
1424  smb2_close_getattr(const unsigned int xid, struct cifs_tcon *tcon,
1425  		   struct cifsFileInfo *cfile)
1426  {
1427  	struct smb2_file_network_open_info file_inf;
1428  	struct inode *inode;
1429  	int rc;
1430  
1431  	rc = __SMB2_close(xid, tcon, cfile->fid.persistent_fid,
1432  		   cfile->fid.volatile_fid, &file_inf);
1433  	if (rc)
1434  		return rc;
1435  
1436  	inode = d_inode(cfile->dentry);
1437  
1438  	spin_lock(&inode->i_lock);
1439  	CIFS_I(inode)->time = jiffies;
1440  
1441  	/* Creation time should not need to be updated on close */
1442  	if (file_inf.LastWriteTime)
1443  		inode_set_mtime_to_ts(inode,
1444  				      cifs_NTtimeToUnix(file_inf.LastWriteTime));
1445  	if (file_inf.ChangeTime)
1446  		inode_set_ctime_to_ts(inode,
1447  				      cifs_NTtimeToUnix(file_inf.ChangeTime));
1448  	if (file_inf.LastAccessTime)
1449  		inode_set_atime_to_ts(inode,
1450  				      cifs_NTtimeToUnix(file_inf.LastAccessTime));
1451  
1452  	/*
1453  	 * i_blocks is not related to (i_size / i_blksize),
1454  	 * but instead 512 byte (2**9) size is required for
1455  	 * calculating num blocks.
1456  	 */
1457  	if (le64_to_cpu(file_inf.AllocationSize) > 4096)
1458  		inode->i_blocks =
1459  			(512 - 1 + le64_to_cpu(file_inf.AllocationSize)) >> 9;
1460  
1461  	/* End of file and Attributes should not have to be updated on close */
1462  	spin_unlock(&inode->i_lock);
1463  	return rc;
1464  }
1465  
1466  static int
1467  SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
1468  		     u64 persistent_fid, u64 volatile_fid,
1469  		     struct copychunk_ioctl *pcchunk)
1470  {
1471  	int rc;
1472  	unsigned int ret_data_len;
1473  	struct resume_key_req *res_key;
1474  
1475  	rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1476  			FSCTL_SRV_REQUEST_RESUME_KEY, NULL, 0 /* no input */,
1477  			CIFSMaxBufSize, (char **)&res_key, &ret_data_len);
1478  
1479  	if (rc == -EOPNOTSUPP) {
1480  		pr_warn_once("Server share %s does not support copy range\n", tcon->tree_name);
1481  		goto req_res_key_exit;
1482  	} else if (rc) {
1483  		cifs_tcon_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
1484  		goto req_res_key_exit;
1485  	}
1486  	if (ret_data_len < sizeof(struct resume_key_req)) {
1487  		cifs_tcon_dbg(VFS, "Invalid refcopy resume key length\n");
1488  		rc = -EINVAL;
1489  		goto req_res_key_exit;
1490  	}
1491  	memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
1492  
1493  req_res_key_exit:
1494  	kfree(res_key);
1495  	return rc;
1496  }
1497  
1498  static int
1499  smb2_ioctl_query_info(const unsigned int xid,
1500  		      struct cifs_tcon *tcon,
1501  		      struct cifs_sb_info *cifs_sb,
1502  		      __le16 *path, int is_dir,
1503  		      unsigned long p)
1504  {
1505  	struct smb2_compound_vars *vars;
1506  	struct smb_rqst *rqst;
1507  	struct kvec *rsp_iov;
1508  	struct cifs_ses *ses = tcon->ses;
1509  	struct TCP_Server_Info *server;
1510  	char __user *arg = (char __user *)p;
1511  	struct smb_query_info qi;
1512  	struct smb_query_info __user *pqi;
1513  	int rc = 0;
1514  	int flags = CIFS_CP_CREATE_CLOSE_OP;
1515  	struct smb2_query_info_rsp *qi_rsp = NULL;
1516  	struct smb2_ioctl_rsp *io_rsp = NULL;
1517  	void *buffer = NULL;
1518  	int resp_buftype[3];
1519  	struct cifs_open_parms oparms;
1520  	u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1521  	struct cifs_fid fid;
1522  	unsigned int size[2];
1523  	void *data[2];
1524  	int create_options = is_dir ? CREATE_NOT_FILE : CREATE_NOT_DIR;
1525  	void (*free_req1_func)(struct smb_rqst *r);
1526  	int retries = 0, cur_sleep = 1;
1527  
1528  replay_again:
1529  	/* reinitialize for possible replay */
1530  	flags = CIFS_CP_CREATE_CLOSE_OP;
1531  	oplock = SMB2_OPLOCK_LEVEL_NONE;
1532  	server = cifs_pick_channel(ses);
1533  
1534  	vars = kzalloc(sizeof(*vars), GFP_ATOMIC);
1535  	if (vars == NULL)
1536  		return -ENOMEM;
1537  	rqst = &vars->rqst[0];
1538  	rsp_iov = &vars->rsp_iov[0];
1539  
1540  	resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1541  
1542  	if (copy_from_user(&qi, arg, sizeof(struct smb_query_info))) {
1543  		rc = -EFAULT;
1544  		goto free_vars;
1545  	}
1546  	if (qi.output_buffer_length > 1024) {
1547  		rc = -EINVAL;
1548  		goto free_vars;
1549  	}
1550  
1551  	if (!ses || !server) {
1552  		rc = -EIO;
1553  		goto free_vars;
1554  	}
1555  
1556  	if (smb3_encryption_required(tcon))
1557  		flags |= CIFS_TRANSFORM_REQ;
1558  
1559  	if (qi.output_buffer_length) {
1560  		buffer = memdup_user(arg + sizeof(struct smb_query_info), qi.output_buffer_length);
1561  		if (IS_ERR(buffer)) {
1562  			rc = PTR_ERR(buffer);
1563  			goto free_vars;
1564  		}
1565  	}
1566  
1567  	/* Open */
1568  	rqst[0].rq_iov = &vars->open_iov[0];
1569  	rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1570  
1571  	oparms = (struct cifs_open_parms) {
1572  		.tcon = tcon,
1573  		.disposition = FILE_OPEN,
1574  		.create_options = cifs_create_options(cifs_sb, create_options),
1575  		.fid = &fid,
1576  		.replay = !!(retries),
1577  	};
1578  
1579  	if (qi.flags & PASSTHRU_FSCTL) {
1580  		switch (qi.info_type & FSCTL_DEVICE_ACCESS_MASK) {
1581  		case FSCTL_DEVICE_ACCESS_FILE_READ_WRITE_ACCESS:
1582  			oparms.desired_access = FILE_READ_DATA | FILE_WRITE_DATA | FILE_READ_ATTRIBUTES | SYNCHRONIZE;
1583  			break;
1584  		case FSCTL_DEVICE_ACCESS_FILE_ANY_ACCESS:
1585  			oparms.desired_access = GENERIC_ALL;
1586  			break;
1587  		case FSCTL_DEVICE_ACCESS_FILE_READ_ACCESS:
1588  			oparms.desired_access = GENERIC_READ;
1589  			break;
1590  		case FSCTL_DEVICE_ACCESS_FILE_WRITE_ACCESS:
1591  			oparms.desired_access = GENERIC_WRITE;
1592  			break;
1593  		}
1594  	} else if (qi.flags & PASSTHRU_SET_INFO) {
1595  		oparms.desired_access = GENERIC_WRITE;
1596  	} else {
1597  		oparms.desired_access = FILE_READ_ATTRIBUTES | READ_CONTROL;
1598  	}
1599  
1600  	rc = SMB2_open_init(tcon, server,
1601  			    &rqst[0], &oplock, &oparms, path);
1602  	if (rc)
1603  		goto free_output_buffer;
1604  	smb2_set_next_command(tcon, &rqst[0]);
1605  
1606  	/* Query */
1607  	if (qi.flags & PASSTHRU_FSCTL) {
1608  		/* Can eventually relax perm check since server enforces too */
1609  		if (!capable(CAP_SYS_ADMIN)) {
1610  			rc = -EPERM;
1611  			goto free_open_req;
1612  		}
1613  		rqst[1].rq_iov = &vars->io_iov[0];
1614  		rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
1615  
1616  		rc = SMB2_ioctl_init(tcon, server, &rqst[1], COMPOUND_FID, COMPOUND_FID,
1617  				     qi.info_type, buffer, qi.output_buffer_length,
1618  				     CIFSMaxBufSize - MAX_SMB2_CREATE_RESPONSE_SIZE -
1619  				     MAX_SMB2_CLOSE_RESPONSE_SIZE);
1620  		free_req1_func = SMB2_ioctl_free;
1621  	} else if (qi.flags == PASSTHRU_SET_INFO) {
1622  		/* Can eventually relax perm check since server enforces too */
1623  		if (!capable(CAP_SYS_ADMIN)) {
1624  			rc = -EPERM;
1625  			goto free_open_req;
1626  		}
1627  		if (qi.output_buffer_length < 8) {
1628  			rc = -EINVAL;
1629  			goto free_open_req;
1630  		}
1631  		rqst[1].rq_iov = vars->si_iov;
1632  		rqst[1].rq_nvec = 1;
1633  
1634  		/* MS-FSCC 2.4.13 FileEndOfFileInformation */
1635  		size[0] = 8;
1636  		data[0] = buffer;
1637  
1638  		rc = SMB2_set_info_init(tcon, server, &rqst[1], COMPOUND_FID, COMPOUND_FID,
1639  					current->tgid, FILE_END_OF_FILE_INFORMATION,
1640  					SMB2_O_INFO_FILE, 0, data, size);
1641  		free_req1_func = SMB2_set_info_free;
1642  	} else if (qi.flags == PASSTHRU_QUERY_INFO) {
1643  		rqst[1].rq_iov = &vars->qi_iov;
1644  		rqst[1].rq_nvec = 1;
1645  
1646  		rc = SMB2_query_info_init(tcon, server,
1647  				  &rqst[1], COMPOUND_FID,
1648  				  COMPOUND_FID, qi.file_info_class,
1649  				  qi.info_type, qi.additional_information,
1650  				  qi.input_buffer_length,
1651  				  qi.output_buffer_length, buffer);
1652  		free_req1_func = SMB2_query_info_free;
1653  	} else { /* unknown flags */
1654  		cifs_tcon_dbg(VFS, "Invalid passthru query flags: 0x%x\n",
1655  			      qi.flags);
1656  		rc = -EINVAL;
1657  	}
1658  
1659  	if (rc)
1660  		goto free_open_req;
1661  	smb2_set_next_command(tcon, &rqst[1]);
1662  	smb2_set_related(&rqst[1]);
1663  
1664  	/* Close */
1665  	rqst[2].rq_iov = &vars->close_iov;
1666  	rqst[2].rq_nvec = 1;
1667  
1668  	rc = SMB2_close_init(tcon, server,
1669  			     &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
1670  	if (rc)
1671  		goto free_req_1;
1672  	smb2_set_related(&rqst[2]);
1673  
1674  	if (retries) {
1675  		smb2_set_replay(server, &rqst[0]);
1676  		smb2_set_replay(server, &rqst[1]);
1677  		smb2_set_replay(server, &rqst[2]);
1678  	}
1679  
1680  	rc = compound_send_recv(xid, ses, server,
1681  				flags, 3, rqst,
1682  				resp_buftype, rsp_iov);
1683  	if (rc)
1684  		goto out;
1685  
1686  	/* No need to bump num_remote_opens since handle immediately closed */
1687  	if (qi.flags & PASSTHRU_FSCTL) {
1688  		pqi = (struct smb_query_info __user *)arg;
1689  		io_rsp = (struct smb2_ioctl_rsp *)rsp_iov[1].iov_base;
1690  		if (le32_to_cpu(io_rsp->OutputCount) < qi.input_buffer_length)
1691  			qi.input_buffer_length = le32_to_cpu(io_rsp->OutputCount);
1692  		if (qi.input_buffer_length > 0 &&
1693  		    le32_to_cpu(io_rsp->OutputOffset) + qi.input_buffer_length
1694  		    > rsp_iov[1].iov_len) {
1695  			rc = -EFAULT;
1696  			goto out;
1697  		}
1698  
1699  		if (copy_to_user(&pqi->input_buffer_length,
1700  				 &qi.input_buffer_length,
1701  				 sizeof(qi.input_buffer_length))) {
1702  			rc = -EFAULT;
1703  			goto out;
1704  		}
1705  
1706  		if (copy_to_user((void __user *)pqi + sizeof(struct smb_query_info),
1707  				 (const void *)io_rsp + le32_to_cpu(io_rsp->OutputOffset),
1708  				 qi.input_buffer_length))
1709  			rc = -EFAULT;
1710  	} else {
1711  		pqi = (struct smb_query_info __user *)arg;
1712  		qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1713  		if (le32_to_cpu(qi_rsp->OutputBufferLength) < qi.input_buffer_length)
1714  			qi.input_buffer_length = le32_to_cpu(qi_rsp->OutputBufferLength);
1715  		if (copy_to_user(&pqi->input_buffer_length,
1716  				 &qi.input_buffer_length,
1717  				 sizeof(qi.input_buffer_length))) {
1718  			rc = -EFAULT;
1719  			goto out;
1720  		}
1721  
1722  		if (copy_to_user(pqi + 1, qi_rsp->Buffer,
1723  				 qi.input_buffer_length))
1724  			rc = -EFAULT;
1725  	}
1726  
1727  out:
1728  	free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1729  	free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1730  	free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1731  	SMB2_close_free(&rqst[2]);
1732  free_req_1:
1733  	free_req1_func(&rqst[1]);
1734  free_open_req:
1735  	SMB2_open_free(&rqst[0]);
1736  free_output_buffer:
1737  	kfree(buffer);
1738  free_vars:
1739  	kfree(vars);
1740  
1741  	if (is_replayable_error(rc) &&
1742  	    smb2_should_replay(tcon, &retries, &cur_sleep))
1743  		goto replay_again;
1744  
1745  	return rc;
1746  }
1747  
1748  static ssize_t
1749  smb2_copychunk_range(const unsigned int xid,
1750  			struct cifsFileInfo *srcfile,
1751  			struct cifsFileInfo *trgtfile, u64 src_off,
1752  			u64 len, u64 dest_off)
1753  {
1754  	int rc;
1755  	unsigned int ret_data_len;
1756  	struct copychunk_ioctl *pcchunk;
1757  	struct copychunk_ioctl_rsp *retbuf = NULL;
1758  	struct cifs_tcon *tcon;
1759  	int chunks_copied = 0;
1760  	bool chunk_sizes_updated = false;
1761  	ssize_t bytes_written, total_bytes_written = 0;
1762  
1763  	pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
1764  	if (pcchunk == NULL)
1765  		return -ENOMEM;
1766  
1767  	cifs_dbg(FYI, "%s: about to call request res key\n", __func__);
1768  	/* Request a key from the server to identify the source of the copy */
1769  	rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
1770  				srcfile->fid.persistent_fid,
1771  				srcfile->fid.volatile_fid, pcchunk);
1772  
1773  	/* Note: request_res_key sets res_key null only if rc !=0 */
1774  	if (rc)
1775  		goto cchunk_out;
1776  
1777  	/* For now array only one chunk long, will make more flexible later */
1778  	pcchunk->ChunkCount = cpu_to_le32(1);
1779  	pcchunk->Reserved = 0;
1780  	pcchunk->Reserved2 = 0;
1781  
1782  	tcon = tlink_tcon(trgtfile->tlink);
1783  
1784  	while (len > 0) {
1785  		pcchunk->SourceOffset = cpu_to_le64(src_off);
1786  		pcchunk->TargetOffset = cpu_to_le64(dest_off);
1787  		pcchunk->Length =
1788  			cpu_to_le32(min_t(u64, len, tcon->max_bytes_chunk));
1789  
1790  		/* Request server copy to target from src identified by key */
1791  		kfree(retbuf);
1792  		retbuf = NULL;
1793  		rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1794  			trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
1795  			(char *)pcchunk, sizeof(struct copychunk_ioctl),
1796  			CIFSMaxBufSize, (char **)&retbuf, &ret_data_len);
1797  		if (rc == 0) {
1798  			if (ret_data_len !=
1799  					sizeof(struct copychunk_ioctl_rsp)) {
1800  				cifs_tcon_dbg(VFS, "Invalid cchunk response size\n");
1801  				rc = -EIO;
1802  				goto cchunk_out;
1803  			}
1804  			if (retbuf->TotalBytesWritten == 0) {
1805  				cifs_dbg(FYI, "no bytes copied\n");
1806  				rc = -EIO;
1807  				goto cchunk_out;
1808  			}
1809  			/*
1810  			 * Check if server claimed to write more than we asked
1811  			 */
1812  			if (le32_to_cpu(retbuf->TotalBytesWritten) >
1813  			    le32_to_cpu(pcchunk->Length)) {
1814  				cifs_tcon_dbg(VFS, "Invalid copy chunk response\n");
1815  				rc = -EIO;
1816  				goto cchunk_out;
1817  			}
1818  			if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
1819  				cifs_tcon_dbg(VFS, "Invalid num chunks written\n");
1820  				rc = -EIO;
1821  				goto cchunk_out;
1822  			}
1823  			chunks_copied++;
1824  
1825  			bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
1826  			src_off += bytes_written;
1827  			dest_off += bytes_written;
1828  			len -= bytes_written;
1829  			total_bytes_written += bytes_written;
1830  
1831  			cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
1832  				le32_to_cpu(retbuf->ChunksWritten),
1833  				le32_to_cpu(retbuf->ChunkBytesWritten),
1834  				bytes_written);
1835  		} else if (rc == -EINVAL) {
1836  			if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
1837  				goto cchunk_out;
1838  
1839  			cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
1840  				le32_to_cpu(retbuf->ChunksWritten),
1841  				le32_to_cpu(retbuf->ChunkBytesWritten),
1842  				le32_to_cpu(retbuf->TotalBytesWritten));
1843  
1844  			/*
1845  			 * Check if this is the first request using these sizes,
1846  			 * (ie check if copy succeed once with original sizes
1847  			 * and check if the server gave us different sizes after
1848  			 * we already updated max sizes on previous request).
1849  			 * if not then why is the server returning an error now
1850  			 */
1851  			if ((chunks_copied != 0) || chunk_sizes_updated)
1852  				goto cchunk_out;
1853  
1854  			/* Check that server is not asking us to grow size */
1855  			if (le32_to_cpu(retbuf->ChunkBytesWritten) <
1856  					tcon->max_bytes_chunk)
1857  				tcon->max_bytes_chunk =
1858  					le32_to_cpu(retbuf->ChunkBytesWritten);
1859  			else
1860  				goto cchunk_out; /* server gave us bogus size */
1861  
1862  			/* No need to change MaxChunks since already set to 1 */
1863  			chunk_sizes_updated = true;
1864  		} else
1865  			goto cchunk_out;
1866  	}
1867  
1868  cchunk_out:
1869  	kfree(pcchunk);
1870  	kfree(retbuf);
1871  	if (rc)
1872  		return rc;
1873  	else
1874  		return total_bytes_written;
1875  }
1876  
1877  static int
1878  smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1879  		struct cifs_fid *fid)
1880  {
1881  	return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1882  }
1883  
1884  static unsigned int
1885  smb2_read_data_offset(char *buf)
1886  {
1887  	struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1888  
1889  	return rsp->DataOffset;
1890  }
1891  
1892  static unsigned int
1893  smb2_read_data_length(char *buf, bool in_remaining)
1894  {
1895  	struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1896  
1897  	if (in_remaining)
1898  		return le32_to_cpu(rsp->DataRemaining);
1899  
1900  	return le32_to_cpu(rsp->DataLength);
1901  }
1902  
1903  
1904  static int
1905  smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
1906  	       struct cifs_io_parms *parms, unsigned int *bytes_read,
1907  	       char **buf, int *buf_type)
1908  {
1909  	parms->persistent_fid = pfid->persistent_fid;
1910  	parms->volatile_fid = pfid->volatile_fid;
1911  	return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1912  }
1913  
1914  static int
1915  smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
1916  		struct cifs_io_parms *parms, unsigned int *written,
1917  		struct kvec *iov, unsigned long nr_segs)
1918  {
1919  
1920  	parms->persistent_fid = pfid->persistent_fid;
1921  	parms->volatile_fid = pfid->volatile_fid;
1922  	return SMB2_write(xid, parms, written, iov, nr_segs);
1923  }
1924  
1925  /* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1926  static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1927  		struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1928  {
1929  	struct cifsInodeInfo *cifsi;
1930  	int rc;
1931  
1932  	cifsi = CIFS_I(inode);
1933  
1934  	/* if file already sparse don't bother setting sparse again */
1935  	if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1936  		return true; /* already sparse */
1937  
1938  	if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1939  		return true; /* already not sparse */
1940  
1941  	/*
1942  	 * Can't check for sparse support on share the usual way via the
1943  	 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1944  	 * since Samba server doesn't set the flag on the share, yet
1945  	 * supports the set sparse FSCTL and returns sparse correctly
1946  	 * in the file attributes. If we fail setting sparse though we
1947  	 * mark that server does not support sparse files for this share
1948  	 * to avoid repeatedly sending the unsupported fsctl to server
1949  	 * if the file is repeatedly extended.
1950  	 */
1951  	if (tcon->broken_sparse_sup)
1952  		return false;
1953  
1954  	rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1955  			cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
1956  			&setsparse, 1, CIFSMaxBufSize, NULL, NULL);
1957  	if (rc) {
1958  		tcon->broken_sparse_sup = true;
1959  		cifs_dbg(FYI, "set sparse rc = %d\n", rc);
1960  		return false;
1961  	}
1962  
1963  	if (setsparse)
1964  		cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
1965  	else
1966  		cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1967  
1968  	return true;
1969  }
1970  
1971  static int
1972  smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1973  		   struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1974  {
1975  	struct inode *inode;
1976  
1977  	/*
1978  	 * If extending file more than one page make sparse. Many Linux fs
1979  	 * make files sparse by default when extending via ftruncate
1980  	 */
1981  	inode = d_inode(cfile->dentry);
1982  
1983  	if (!set_alloc && (size > inode->i_size + 8192)) {
1984  		__u8 set_sparse = 1;
1985  
1986  		/* whether set sparse succeeds or not, extend the file */
1987  		smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
1988  	}
1989  
1990  	return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
1991  			    cfile->fid.volatile_fid, cfile->pid, size);
1992  }
1993  
1994  static int
1995  smb2_duplicate_extents(const unsigned int xid,
1996  			struct cifsFileInfo *srcfile,
1997  			struct cifsFileInfo *trgtfile, u64 src_off,
1998  			u64 len, u64 dest_off)
1999  {
2000  	int rc;
2001  	unsigned int ret_data_len;
2002  	struct inode *inode;
2003  	struct duplicate_extents_to_file dup_ext_buf;
2004  	struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
2005  
2006  	/* server fileays advertise duplicate extent support with this flag */
2007  	if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
2008  	     FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
2009  		return -EOPNOTSUPP;
2010  
2011  	dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
2012  	dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
2013  	dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
2014  	dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
2015  	dup_ext_buf.ByteCount = cpu_to_le64(len);
2016  	cifs_dbg(FYI, "Duplicate extents: src off %lld dst off %lld len %lld\n",
2017  		src_off, dest_off, len);
2018  
2019  	inode = d_inode(trgtfile->dentry);
2020  	if (inode->i_size < dest_off + len) {
2021  		rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
2022  		if (rc)
2023  			goto duplicate_extents_out;
2024  
2025  		/*
2026  		 * Although also could set plausible allocation size (i_blocks)
2027  		 * here in addition to setting the file size, in reflink
2028  		 * it is likely that the target file is sparse. Its allocation
2029  		 * size will be queried on next revalidate, but it is important
2030  		 * to make sure that file's cached size is updated immediately
2031  		 */
2032  		cifs_setsize(inode, dest_off + len);
2033  	}
2034  	rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
2035  			trgtfile->fid.volatile_fid,
2036  			FSCTL_DUPLICATE_EXTENTS_TO_FILE,
2037  			(char *)&dup_ext_buf,
2038  			sizeof(struct duplicate_extents_to_file),
2039  			CIFSMaxBufSize, NULL,
2040  			&ret_data_len);
2041  
2042  	if (ret_data_len > 0)
2043  		cifs_dbg(FYI, "Non-zero response length in duplicate extents\n");
2044  
2045  duplicate_extents_out:
2046  	return rc;
2047  }
2048  
2049  static int
2050  smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2051  		   struct cifsFileInfo *cfile)
2052  {
2053  	return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
2054  			    cfile->fid.volatile_fid);
2055  }
2056  
2057  static int
2058  smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
2059  		   struct cifsFileInfo *cfile)
2060  {
2061  	struct fsctl_set_integrity_information_req integr_info;
2062  	unsigned int ret_data_len;
2063  
2064  	integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
2065  	integr_info.Flags = 0;
2066  	integr_info.Reserved = 0;
2067  
2068  	return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2069  			cfile->fid.volatile_fid,
2070  			FSCTL_SET_INTEGRITY_INFORMATION,
2071  			(char *)&integr_info,
2072  			sizeof(struct fsctl_set_integrity_information_req),
2073  			CIFSMaxBufSize, NULL,
2074  			&ret_data_len);
2075  
2076  }
2077  
2078  /* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
2079  #define GMT_TOKEN_SIZE 50
2080  
2081  #define MIN_SNAPSHOT_ARRAY_SIZE 16 /* See MS-SMB2 section 3.3.5.15.1 */
2082  
2083  /*
2084   * Input buffer contains (empty) struct smb_snapshot array with size filled in
2085   * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
2086   */
2087  static int
2088  smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
2089  		   struct cifsFileInfo *cfile, void __user *ioc_buf)
2090  {
2091  	char *retbuf = NULL;
2092  	unsigned int ret_data_len = 0;
2093  	int rc;
2094  	u32 max_response_size;
2095  	struct smb_snapshot_array snapshot_in;
2096  
2097  	/*
2098  	 * On the first query to enumerate the list of snapshots available
2099  	 * for this volume the buffer begins with 0 (number of snapshots
2100  	 * which can be returned is zero since at that point we do not know
2101  	 * how big the buffer needs to be). On the second query,
2102  	 * it (ret_data_len) is set to number of snapshots so we can
2103  	 * know to set the maximum response size larger (see below).
2104  	 */
2105  	if (get_user(ret_data_len, (unsigned int __user *)ioc_buf))
2106  		return -EFAULT;
2107  
2108  	/*
2109  	 * Note that for snapshot queries that servers like Azure expect that
2110  	 * the first query be minimal size (and just used to get the number/size
2111  	 * of previous versions) so response size must be specified as EXACTLY
2112  	 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2113  	 * of eight bytes.
2114  	 */
2115  	if (ret_data_len == 0)
2116  		max_response_size = MIN_SNAPSHOT_ARRAY_SIZE;
2117  	else
2118  		max_response_size = CIFSMaxBufSize;
2119  
2120  	rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2121  			cfile->fid.volatile_fid,
2122  			FSCTL_SRV_ENUMERATE_SNAPSHOTS,
2123  			NULL, 0 /* no input data */, max_response_size,
2124  			(char **)&retbuf,
2125  			&ret_data_len);
2126  	cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
2127  			rc, ret_data_len);
2128  	if (rc)
2129  		return rc;
2130  
2131  	if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
2132  		/* Fixup buffer */
2133  		if (copy_from_user(&snapshot_in, ioc_buf,
2134  		    sizeof(struct smb_snapshot_array))) {
2135  			rc = -EFAULT;
2136  			kfree(retbuf);
2137  			return rc;
2138  		}
2139  
2140  		/*
2141  		 * Check for min size, ie not large enough to fit even one GMT
2142  		 * token (snapshot).  On the first ioctl some users may pass in
2143  		 * smaller size (or zero) to simply get the size of the array
2144  		 * so the user space caller can allocate sufficient memory
2145  		 * and retry the ioctl again with larger array size sufficient
2146  		 * to hold all of the snapshot GMT tokens on the second try.
2147  		 */
2148  		if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
2149  			ret_data_len = sizeof(struct smb_snapshot_array);
2150  
2151  		/*
2152  		 * We return struct SRV_SNAPSHOT_ARRAY, followed by
2153  		 * the snapshot array (of 50 byte GMT tokens) each
2154  		 * representing an available previous version of the data
2155  		 */
2156  		if (ret_data_len > (snapshot_in.snapshot_array_size +
2157  					sizeof(struct smb_snapshot_array)))
2158  			ret_data_len = snapshot_in.snapshot_array_size +
2159  					sizeof(struct smb_snapshot_array);
2160  
2161  		if (copy_to_user(ioc_buf, retbuf, ret_data_len))
2162  			rc = -EFAULT;
2163  	}
2164  
2165  	kfree(retbuf);
2166  	return rc;
2167  }
2168  
2169  
2170  
2171  static int
2172  smb3_notify(const unsigned int xid, struct file *pfile,
2173  	    void __user *ioc_buf, bool return_changes)
2174  {
2175  	struct smb3_notify_info notify;
2176  	struct smb3_notify_info __user *pnotify_buf;
2177  	struct dentry *dentry = pfile->f_path.dentry;
2178  	struct inode *inode = file_inode(pfile);
2179  	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2180  	struct cifs_open_parms oparms;
2181  	struct cifs_fid fid;
2182  	struct cifs_tcon *tcon;
2183  	const unsigned char *path;
2184  	char *returned_ioctl_info = NULL;
2185  	void *page = alloc_dentry_path();
2186  	__le16 *utf16_path = NULL;
2187  	u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2188  	int rc = 0;
2189  	__u32 ret_len = 0;
2190  
2191  	path = build_path_from_dentry(dentry, page);
2192  	if (IS_ERR(path)) {
2193  		rc = PTR_ERR(path);
2194  		goto notify_exit;
2195  	}
2196  
2197  	utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2198  	if (utf16_path == NULL) {
2199  		rc = -ENOMEM;
2200  		goto notify_exit;
2201  	}
2202  
2203  	if (return_changes) {
2204  		if (copy_from_user(&notify, ioc_buf, sizeof(struct smb3_notify_info))) {
2205  			rc = -EFAULT;
2206  			goto notify_exit;
2207  		}
2208  	} else {
2209  		if (copy_from_user(&notify, ioc_buf, sizeof(struct smb3_notify))) {
2210  			rc = -EFAULT;
2211  			goto notify_exit;
2212  		}
2213  		notify.data_len = 0;
2214  	}
2215  
2216  	tcon = cifs_sb_master_tcon(cifs_sb);
2217  	oparms = (struct cifs_open_parms) {
2218  		.tcon = tcon,
2219  		.path = path,
2220  		.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA,
2221  		.disposition = FILE_OPEN,
2222  		.create_options = cifs_create_options(cifs_sb, 0),
2223  		.fid = &fid,
2224  	};
2225  
2226  	rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
2227  		       NULL);
2228  	if (rc)
2229  		goto notify_exit;
2230  
2231  	rc = SMB2_change_notify(xid, tcon, fid.persistent_fid, fid.volatile_fid,
2232  				notify.watch_tree, notify.completion_filter,
2233  				notify.data_len, &returned_ioctl_info, &ret_len);
2234  
2235  	SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2236  
2237  	cifs_dbg(FYI, "change notify for path %s rc %d\n", path, rc);
2238  	if (return_changes && (ret_len > 0) && (notify.data_len > 0)) {
2239  		if (ret_len > notify.data_len)
2240  			ret_len = notify.data_len;
2241  		pnotify_buf = (struct smb3_notify_info __user *)ioc_buf;
2242  		if (copy_to_user(pnotify_buf->notify_data, returned_ioctl_info, ret_len))
2243  			rc = -EFAULT;
2244  		else if (copy_to_user(&pnotify_buf->data_len, &ret_len, sizeof(ret_len)))
2245  			rc = -EFAULT;
2246  	}
2247  	kfree(returned_ioctl_info);
2248  notify_exit:
2249  	free_dentry_path(page);
2250  	kfree(utf16_path);
2251  	return rc;
2252  }
2253  
2254  static int
2255  smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
2256  		     const char *path, struct cifs_sb_info *cifs_sb,
2257  		     struct cifs_fid *fid, __u16 search_flags,
2258  		     struct cifs_search_info *srch_inf)
2259  {
2260  	__le16 *utf16_path;
2261  	struct smb_rqst rqst[2];
2262  	struct kvec rsp_iov[2];
2263  	int resp_buftype[2];
2264  	struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2265  	struct kvec qd_iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
2266  	int rc, flags = 0;
2267  	u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2268  	struct cifs_open_parms oparms;
2269  	struct smb2_query_directory_rsp *qd_rsp = NULL;
2270  	struct smb2_create_rsp *op_rsp = NULL;
2271  	struct TCP_Server_Info *server;
2272  	int retries = 0, cur_sleep = 1;
2273  
2274  replay_again:
2275  	/* reinitialize for possible replay */
2276  	flags = 0;
2277  	oplock = SMB2_OPLOCK_LEVEL_NONE;
2278  	server = cifs_pick_channel(tcon->ses);
2279  
2280  	utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2281  	if (!utf16_path)
2282  		return -ENOMEM;
2283  
2284  	if (smb3_encryption_required(tcon))
2285  		flags |= CIFS_TRANSFORM_REQ;
2286  
2287  	memset(rqst, 0, sizeof(rqst));
2288  	resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
2289  	memset(rsp_iov, 0, sizeof(rsp_iov));
2290  
2291  	/* Open */
2292  	memset(&open_iov, 0, sizeof(open_iov));
2293  	rqst[0].rq_iov = open_iov;
2294  	rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2295  
2296  	oparms = (struct cifs_open_parms) {
2297  		.tcon = tcon,
2298  		.path = path,
2299  		.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA,
2300  		.disposition = FILE_OPEN,
2301  		.create_options = cifs_create_options(cifs_sb, 0),
2302  		.fid = fid,
2303  		.replay = !!(retries),
2304  	};
2305  
2306  	rc = SMB2_open_init(tcon, server,
2307  			    &rqst[0], &oplock, &oparms, utf16_path);
2308  	if (rc)
2309  		goto qdf_free;
2310  	smb2_set_next_command(tcon, &rqst[0]);
2311  
2312  	/* Query directory */
2313  	srch_inf->entries_in_buffer = 0;
2314  	srch_inf->index_of_last_entry = 2;
2315  
2316  	memset(&qd_iov, 0, sizeof(qd_iov));
2317  	rqst[1].rq_iov = qd_iov;
2318  	rqst[1].rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
2319  
2320  	rc = SMB2_query_directory_init(xid, tcon, server,
2321  				       &rqst[1],
2322  				       COMPOUND_FID, COMPOUND_FID,
2323  				       0, srch_inf->info_level);
2324  	if (rc)
2325  		goto qdf_free;
2326  
2327  	smb2_set_related(&rqst[1]);
2328  
2329  	if (retries) {
2330  		smb2_set_replay(server, &rqst[0]);
2331  		smb2_set_replay(server, &rqst[1]);
2332  	}
2333  
2334  	rc = compound_send_recv(xid, tcon->ses, server,
2335  				flags, 2, rqst,
2336  				resp_buftype, rsp_iov);
2337  
2338  	/* If the open failed there is nothing to do */
2339  	op_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
2340  	if (op_rsp == NULL || op_rsp->hdr.Status != STATUS_SUCCESS) {
2341  		cifs_dbg(FYI, "query_dir_first: open failed rc=%d\n", rc);
2342  		goto qdf_free;
2343  	}
2344  	fid->persistent_fid = op_rsp->PersistentFileId;
2345  	fid->volatile_fid = op_rsp->VolatileFileId;
2346  
2347  	/* Anything else than ENODATA means a genuine error */
2348  	if (rc && rc != -ENODATA) {
2349  		SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2350  		cifs_dbg(FYI, "query_dir_first: query directory failed rc=%d\n", rc);
2351  		trace_smb3_query_dir_err(xid, fid->persistent_fid,
2352  					 tcon->tid, tcon->ses->Suid, 0, 0, rc);
2353  		goto qdf_free;
2354  	}
2355  
2356  	atomic_inc(&tcon->num_remote_opens);
2357  
2358  	qd_rsp = (struct smb2_query_directory_rsp *)rsp_iov[1].iov_base;
2359  	if (qd_rsp->hdr.Status == STATUS_NO_MORE_FILES) {
2360  		trace_smb3_query_dir_done(xid, fid->persistent_fid,
2361  					  tcon->tid, tcon->ses->Suid, 0, 0);
2362  		srch_inf->endOfSearch = true;
2363  		rc = 0;
2364  		goto qdf_free;
2365  	}
2366  
2367  	rc = smb2_parse_query_directory(tcon, &rsp_iov[1], resp_buftype[1],
2368  					srch_inf);
2369  	if (rc) {
2370  		trace_smb3_query_dir_err(xid, fid->persistent_fid, tcon->tid,
2371  			tcon->ses->Suid, 0, 0, rc);
2372  		goto qdf_free;
2373  	}
2374  	resp_buftype[1] = CIFS_NO_BUFFER;
2375  
2376  	trace_smb3_query_dir_done(xid, fid->persistent_fid, tcon->tid,
2377  			tcon->ses->Suid, 0, srch_inf->entries_in_buffer);
2378  
2379   qdf_free:
2380  	kfree(utf16_path);
2381  	SMB2_open_free(&rqst[0]);
2382  	SMB2_query_directory_free(&rqst[1]);
2383  	free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2384  	free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2385  
2386  	if (is_replayable_error(rc) &&
2387  	    smb2_should_replay(tcon, &retries, &cur_sleep))
2388  		goto replay_again;
2389  
2390  	return rc;
2391  }
2392  
2393  static int
2394  smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
2395  		    struct cifs_fid *fid, __u16 search_flags,
2396  		    struct cifs_search_info *srch_inf)
2397  {
2398  	return SMB2_query_directory(xid, tcon, fid->persistent_fid,
2399  				    fid->volatile_fid, 0, srch_inf);
2400  }
2401  
2402  static int
2403  smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
2404  	       struct cifs_fid *fid)
2405  {
2406  	return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2407  }
2408  
2409  /*
2410   * If we negotiate SMB2 protocol and get STATUS_PENDING - update
2411   * the number of credits and return true. Otherwise - return false.
2412   */
2413  static bool
2414  smb2_is_status_pending(char *buf, struct TCP_Server_Info *server)
2415  {
2416  	struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
2417  	int scredits, in_flight;
2418  
2419  	if (shdr->Status != STATUS_PENDING)
2420  		return false;
2421  
2422  	if (shdr->CreditRequest) {
2423  		spin_lock(&server->req_lock);
2424  		server->credits += le16_to_cpu(shdr->CreditRequest);
2425  		scredits = server->credits;
2426  		in_flight = server->in_flight;
2427  		spin_unlock(&server->req_lock);
2428  		wake_up(&server->request_q);
2429  
2430  		trace_smb3_pend_credits(server->CurrentMid,
2431  				server->conn_id, server->hostname, scredits,
2432  				le16_to_cpu(shdr->CreditRequest), in_flight);
2433  		cifs_dbg(FYI, "%s: status pending add %u credits total=%d\n",
2434  				__func__, le16_to_cpu(shdr->CreditRequest), scredits);
2435  	}
2436  
2437  	return true;
2438  }
2439  
2440  static bool
2441  smb2_is_session_expired(char *buf)
2442  {
2443  	struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
2444  
2445  	if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
2446  	    shdr->Status != STATUS_USER_SESSION_DELETED)
2447  		return false;
2448  
2449  	trace_smb3_ses_expired(le32_to_cpu(shdr->Id.SyncId.TreeId),
2450  			       le64_to_cpu(shdr->SessionId),
2451  			       le16_to_cpu(shdr->Command),
2452  			       le64_to_cpu(shdr->MessageId));
2453  	cifs_dbg(FYI, "Session expired or deleted\n");
2454  
2455  	return true;
2456  }
2457  
2458  static bool
2459  smb2_is_status_io_timeout(char *buf)
2460  {
2461  	struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
2462  
2463  	if (shdr->Status == STATUS_IO_TIMEOUT)
2464  		return true;
2465  	else
2466  		return false;
2467  }
2468  
2469  static bool
2470  smb2_is_network_name_deleted(char *buf, struct TCP_Server_Info *server)
2471  {
2472  	struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
2473  	struct TCP_Server_Info *pserver;
2474  	struct cifs_ses *ses;
2475  	struct cifs_tcon *tcon;
2476  
2477  	if (shdr->Status != STATUS_NETWORK_NAME_DELETED)
2478  		return false;
2479  
2480  	/* If server is a channel, select the primary channel */
2481  	pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
2482  
2483  	spin_lock(&cifs_tcp_ses_lock);
2484  	list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
2485  		if (cifs_ses_exiting(ses))
2486  			continue;
2487  		list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
2488  			if (tcon->tid == le32_to_cpu(shdr->Id.SyncId.TreeId)) {
2489  				spin_lock(&tcon->tc_lock);
2490  				tcon->need_reconnect = true;
2491  				spin_unlock(&tcon->tc_lock);
2492  				spin_unlock(&cifs_tcp_ses_lock);
2493  				pr_warn_once("Server share %s deleted.\n",
2494  					     tcon->tree_name);
2495  				return true;
2496  			}
2497  		}
2498  	}
2499  	spin_unlock(&cifs_tcp_ses_lock);
2500  
2501  	return false;
2502  }
2503  
2504  static int
2505  smb2_oplock_response(struct cifs_tcon *tcon, __u64 persistent_fid,
2506  		__u64 volatile_fid, __u16 net_fid, struct cifsInodeInfo *cinode)
2507  {
2508  	if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
2509  		return SMB2_lease_break(0, tcon, cinode->lease_key,
2510  					smb2_get_lease_state(cinode));
2511  
2512  	return SMB2_oplock_break(0, tcon, persistent_fid, volatile_fid,
2513  				 CIFS_CACHE_READ(cinode) ? 1 : 0);
2514  }
2515  
2516  void
2517  smb2_set_replay(struct TCP_Server_Info *server, struct smb_rqst *rqst)
2518  {
2519  	struct smb2_hdr *shdr;
2520  
2521  	if (server->dialect < SMB30_PROT_ID)
2522  		return;
2523  
2524  	shdr = (struct smb2_hdr *)(rqst->rq_iov[0].iov_base);
2525  	if (shdr == NULL) {
2526  		cifs_dbg(FYI, "shdr NULL in smb2_set_related\n");
2527  		return;
2528  	}
2529  	shdr->Flags |= SMB2_FLAGS_REPLAY_OPERATION;
2530  }
2531  
2532  void
2533  smb2_set_related(struct smb_rqst *rqst)
2534  {
2535  	struct smb2_hdr *shdr;
2536  
2537  	shdr = (struct smb2_hdr *)(rqst->rq_iov[0].iov_base);
2538  	if (shdr == NULL) {
2539  		cifs_dbg(FYI, "shdr NULL in smb2_set_related\n");
2540  		return;
2541  	}
2542  	shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2543  }
2544  
2545  char smb2_padding[7] = {0, 0, 0, 0, 0, 0, 0};
2546  
2547  void
2548  smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst)
2549  {
2550  	struct smb2_hdr *shdr;
2551  	struct cifs_ses *ses = tcon->ses;
2552  	struct TCP_Server_Info *server = ses->server;
2553  	unsigned long len = smb_rqst_len(server, rqst);
2554  	int i, num_padding;
2555  
2556  	shdr = (struct smb2_hdr *)(rqst->rq_iov[0].iov_base);
2557  	if (shdr == NULL) {
2558  		cifs_dbg(FYI, "shdr NULL in smb2_set_next_command\n");
2559  		return;
2560  	}
2561  
2562  	/* SMB headers in a compound are 8 byte aligned. */
2563  
2564  	/* No padding needed */
2565  	if (!(len & 7))
2566  		goto finished;
2567  
2568  	num_padding = 8 - (len & 7);
2569  	if (!smb3_encryption_required(tcon)) {
2570  		/*
2571  		 * If we do not have encryption then we can just add an extra
2572  		 * iov for the padding.
2573  		 */
2574  		rqst->rq_iov[rqst->rq_nvec].iov_base = smb2_padding;
2575  		rqst->rq_iov[rqst->rq_nvec].iov_len = num_padding;
2576  		rqst->rq_nvec++;
2577  		len += num_padding;
2578  	} else {
2579  		/*
2580  		 * We can not add a small padding iov for the encryption case
2581  		 * because the encryption framework can not handle the padding
2582  		 * iovs.
2583  		 * We have to flatten this into a single buffer and add
2584  		 * the padding to it.
2585  		 */
2586  		for (i = 1; i < rqst->rq_nvec; i++) {
2587  			memcpy(rqst->rq_iov[0].iov_base +
2588  			       rqst->rq_iov[0].iov_len,
2589  			       rqst->rq_iov[i].iov_base,
2590  			       rqst->rq_iov[i].iov_len);
2591  			rqst->rq_iov[0].iov_len += rqst->rq_iov[i].iov_len;
2592  		}
2593  		memset(rqst->rq_iov[0].iov_base + rqst->rq_iov[0].iov_len,
2594  		       0, num_padding);
2595  		rqst->rq_iov[0].iov_len += num_padding;
2596  		len += num_padding;
2597  		rqst->rq_nvec = 1;
2598  	}
2599  
2600   finished:
2601  	shdr->NextCommand = cpu_to_le32(len);
2602  }
2603  
2604  /*
2605   * helper function for exponential backoff and check if replayable
2606   */
2607  bool smb2_should_replay(struct cifs_tcon *tcon,
2608  				int *pretries,
2609  				int *pcur_sleep)
2610  {
2611  	if (!pretries || !pcur_sleep)
2612  		return false;
2613  
2614  	if (tcon->retry || (*pretries)++ < tcon->ses->server->retrans) {
2615  		msleep(*pcur_sleep);
2616  		(*pcur_sleep) = ((*pcur_sleep) << 1);
2617  		if ((*pcur_sleep) > CIFS_MAX_SLEEP)
2618  			(*pcur_sleep) = CIFS_MAX_SLEEP;
2619  		return true;
2620  	}
2621  
2622  	return false;
2623  }
2624  
2625  /*
2626   * Passes the query info response back to the caller on success.
2627   * Caller need to free this with free_rsp_buf().
2628   */
2629  int
2630  smb2_query_info_compound(const unsigned int xid, struct cifs_tcon *tcon,
2631  			 const char *path, u32 desired_access,
2632  			 u32 class, u32 type, u32 output_len,
2633  			 struct kvec *rsp, int *buftype,
2634  			 struct cifs_sb_info *cifs_sb)
2635  {
2636  	struct smb2_compound_vars *vars;
2637  	struct cifs_ses *ses = tcon->ses;
2638  	struct TCP_Server_Info *server;
2639  	int flags = CIFS_CP_CREATE_CLOSE_OP;
2640  	struct smb_rqst *rqst;
2641  	int resp_buftype[3];
2642  	struct kvec *rsp_iov;
2643  	u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2644  	struct cifs_open_parms oparms;
2645  	struct cifs_fid fid;
2646  	int rc;
2647  	__le16 *utf16_path;
2648  	struct cached_fid *cfid = NULL;
2649  	int retries = 0, cur_sleep = 1;
2650  
2651  replay_again:
2652  	/* reinitialize for possible replay */
2653  	flags = CIFS_CP_CREATE_CLOSE_OP;
2654  	oplock = SMB2_OPLOCK_LEVEL_NONE;
2655  	server = cifs_pick_channel(ses);
2656  
2657  	if (!path)
2658  		path = "";
2659  	utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2660  	if (!utf16_path)
2661  		return -ENOMEM;
2662  
2663  	if (smb3_encryption_required(tcon))
2664  		flags |= CIFS_TRANSFORM_REQ;
2665  
2666  	resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2667  	vars = kzalloc(sizeof(*vars), GFP_KERNEL);
2668  	if (!vars) {
2669  		rc = -ENOMEM;
2670  		goto out_free_path;
2671  	}
2672  	rqst = vars->rqst;
2673  	rsp_iov = vars->rsp_iov;
2674  
2675  	/*
2676  	 * We can only call this for things we know are directories.
2677  	 */
2678  	if (!strcmp(path, ""))
2679  		open_cached_dir(xid, tcon, path, cifs_sb, false,
2680  				&cfid); /* cfid null if open dir failed */
2681  
2682  	rqst[0].rq_iov = vars->open_iov;
2683  	rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2684  
2685  	oparms = (struct cifs_open_parms) {
2686  		.tcon = tcon,
2687  		.path = path,
2688  		.desired_access = desired_access,
2689  		.disposition = FILE_OPEN,
2690  		.create_options = cifs_create_options(cifs_sb, 0),
2691  		.fid = &fid,
2692  		.replay = !!(retries),
2693  	};
2694  
2695  	rc = SMB2_open_init(tcon, server,
2696  			    &rqst[0], &oplock, &oparms, utf16_path);
2697  	if (rc)
2698  		goto qic_exit;
2699  	smb2_set_next_command(tcon, &rqst[0]);
2700  
2701  	rqst[1].rq_iov = &vars->qi_iov;
2702  	rqst[1].rq_nvec = 1;
2703  
2704  	if (cfid) {
2705  		rc = SMB2_query_info_init(tcon, server,
2706  					  &rqst[1],
2707  					  cfid->fid.persistent_fid,
2708  					  cfid->fid.volatile_fid,
2709  					  class, type, 0,
2710  					  output_len, 0,
2711  					  NULL);
2712  	} else {
2713  		rc = SMB2_query_info_init(tcon, server,
2714  					  &rqst[1],
2715  					  COMPOUND_FID,
2716  					  COMPOUND_FID,
2717  					  class, type, 0,
2718  					  output_len, 0,
2719  					  NULL);
2720  	}
2721  	if (rc)
2722  		goto qic_exit;
2723  	if (!cfid) {
2724  		smb2_set_next_command(tcon, &rqst[1]);
2725  		smb2_set_related(&rqst[1]);
2726  	}
2727  
2728  	rqst[2].rq_iov = &vars->close_iov;
2729  	rqst[2].rq_nvec = 1;
2730  
2731  	rc = SMB2_close_init(tcon, server,
2732  			     &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
2733  	if (rc)
2734  		goto qic_exit;
2735  	smb2_set_related(&rqst[2]);
2736  
2737  	if (retries) {
2738  		if (!cfid) {
2739  			smb2_set_replay(server, &rqst[0]);
2740  			smb2_set_replay(server, &rqst[2]);
2741  		}
2742  		smb2_set_replay(server, &rqst[1]);
2743  	}
2744  
2745  	if (cfid) {
2746  		rc = compound_send_recv(xid, ses, server,
2747  					flags, 1, &rqst[1],
2748  					&resp_buftype[1], &rsp_iov[1]);
2749  	} else {
2750  		rc = compound_send_recv(xid, ses, server,
2751  					flags, 3, rqst,
2752  					resp_buftype, rsp_iov);
2753  	}
2754  	if (rc) {
2755  		free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2756  		if (rc == -EREMCHG) {
2757  			tcon->need_reconnect = true;
2758  			pr_warn_once("server share %s deleted\n",
2759  				     tcon->tree_name);
2760  		}
2761  		goto qic_exit;
2762  	}
2763  	*rsp = rsp_iov[1];
2764  	*buftype = resp_buftype[1];
2765  
2766   qic_exit:
2767  	SMB2_open_free(&rqst[0]);
2768  	SMB2_query_info_free(&rqst[1]);
2769  	SMB2_close_free(&rqst[2]);
2770  	free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2771  	free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2772  	if (cfid)
2773  		close_cached_dir(cfid);
2774  	kfree(vars);
2775  out_free_path:
2776  	kfree(utf16_path);
2777  
2778  	if (is_replayable_error(rc) &&
2779  	    smb2_should_replay(tcon, &retries, &cur_sleep))
2780  		goto replay_again;
2781  
2782  	return rc;
2783  }
2784  
2785  static int
2786  smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2787  	     const char *path, struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
2788  {
2789  	struct smb2_query_info_rsp *rsp;
2790  	struct smb2_fs_full_size_info *info = NULL;
2791  	struct kvec rsp_iov = {NULL, 0};
2792  	int buftype = CIFS_NO_BUFFER;
2793  	int rc;
2794  
2795  
2796  	rc = smb2_query_info_compound(xid, tcon, path,
2797  				      FILE_READ_ATTRIBUTES,
2798  				      FS_FULL_SIZE_INFORMATION,
2799  				      SMB2_O_INFO_FILESYSTEM,
2800  				      sizeof(struct smb2_fs_full_size_info),
2801  				      &rsp_iov, &buftype, cifs_sb);
2802  	if (rc)
2803  		goto qfs_exit;
2804  
2805  	rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
2806  	buf->f_type = SMB2_SUPER_MAGIC;
2807  	info = (struct smb2_fs_full_size_info *)(
2808  		le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
2809  	rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
2810  			       le32_to_cpu(rsp->OutputBufferLength),
2811  			       &rsp_iov,
2812  			       sizeof(struct smb2_fs_full_size_info));
2813  	if (!rc)
2814  		smb2_copy_fs_info_to_kstatfs(info, buf);
2815  
2816  qfs_exit:
2817  	trace_smb3_qfs_done(xid, tcon->tid, tcon->ses->Suid, tcon->tree_name, rc);
2818  	free_rsp_buf(buftype, rsp_iov.iov_base);
2819  	return rc;
2820  }
2821  
2822  static int
2823  smb311_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2824  	       const char *path, struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
2825  {
2826  	int rc;
2827  	__le16 *utf16_path = NULL;
2828  	u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2829  	struct cifs_open_parms oparms;
2830  	struct cifs_fid fid;
2831  
2832  	if (!tcon->posix_extensions)
2833  		return smb2_queryfs(xid, tcon, path, cifs_sb, buf);
2834  
2835  	oparms = (struct cifs_open_parms) {
2836  		.tcon = tcon,
2837  		.path = path,
2838  		.desired_access = FILE_READ_ATTRIBUTES,
2839  		.disposition = FILE_OPEN,
2840  		.create_options = cifs_create_options(cifs_sb, 0),
2841  		.fid = &fid,
2842  	};
2843  
2844  	utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2845  	if (utf16_path == NULL)
2846  		return -ENOMEM;
2847  
2848  	rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL,
2849  		       NULL, NULL);
2850  	kfree(utf16_path);
2851  	if (rc)
2852  		return rc;
2853  
2854  	rc = SMB311_posix_qfs_info(xid, tcon, fid.persistent_fid,
2855  				   fid.volatile_fid, buf);
2856  	buf->f_type = SMB2_SUPER_MAGIC;
2857  	SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2858  	return rc;
2859  }
2860  
2861  static bool
2862  smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
2863  {
2864  	return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
2865  	       ob1->fid.volatile_fid == ob2->fid.volatile_fid;
2866  }
2867  
2868  static int
2869  smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
2870  	       __u64 length, __u32 type, int lock, int unlock, bool wait)
2871  {
2872  	if (unlock && !lock)
2873  		type = SMB2_LOCKFLAG_UNLOCK;
2874  	return SMB2_lock(xid, tlink_tcon(cfile->tlink),
2875  			 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
2876  			 current->tgid, length, offset, type, wait);
2877  }
2878  
2879  static void
2880  smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
2881  {
2882  	memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
2883  }
2884  
2885  static void
2886  smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
2887  {
2888  	memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
2889  }
2890  
2891  static void
2892  smb2_new_lease_key(struct cifs_fid *fid)
2893  {
2894  	generate_random_uuid(fid->lease_key);
2895  }
2896  
2897  static int
2898  smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
2899  		   const char *search_name,
2900  		   struct dfs_info3_param **target_nodes,
2901  		   unsigned int *num_of_nodes,
2902  		   const struct nls_table *nls_codepage, int remap)
2903  {
2904  	int rc;
2905  	__le16 *utf16_path = NULL;
2906  	int utf16_path_len = 0;
2907  	struct cifs_tcon *tcon;
2908  	struct fsctl_get_dfs_referral_req *dfs_req = NULL;
2909  	struct get_dfs_referral_rsp *dfs_rsp = NULL;
2910  	u32 dfs_req_size = 0, dfs_rsp_size = 0;
2911  	int retry_count = 0;
2912  
2913  	cifs_dbg(FYI, "%s: path: %s\n", __func__, search_name);
2914  
2915  	/*
2916  	 * Try to use the IPC tcon, otherwise just use any
2917  	 */
2918  	tcon = ses->tcon_ipc;
2919  	if (tcon == NULL) {
2920  		spin_lock(&cifs_tcp_ses_lock);
2921  		tcon = list_first_entry_or_null(&ses->tcon_list,
2922  						struct cifs_tcon,
2923  						tcon_list);
2924  		if (tcon) {
2925  			tcon->tc_count++;
2926  			trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count,
2927  					    netfs_trace_tcon_ref_get_dfs_refer);
2928  		}
2929  		spin_unlock(&cifs_tcp_ses_lock);
2930  	}
2931  
2932  	if (tcon == NULL) {
2933  		cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
2934  			 ses);
2935  		rc = -ENOTCONN;
2936  		goto out;
2937  	}
2938  
2939  	utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
2940  					   &utf16_path_len,
2941  					   nls_codepage, remap);
2942  	if (!utf16_path) {
2943  		rc = -ENOMEM;
2944  		goto out;
2945  	}
2946  
2947  	dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
2948  	dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
2949  	if (!dfs_req) {
2950  		rc = -ENOMEM;
2951  		goto out;
2952  	}
2953  
2954  	/* Highest DFS referral version understood */
2955  	dfs_req->MaxReferralLevel = DFS_VERSION;
2956  
2957  	/* Path to resolve in an UTF-16 null-terminated string */
2958  	memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
2959  
2960  	do {
2961  		rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
2962  				FSCTL_DFS_GET_REFERRALS,
2963  				(char *)dfs_req, dfs_req_size, CIFSMaxBufSize,
2964  				(char **)&dfs_rsp, &dfs_rsp_size);
2965  		if (!is_retryable_error(rc))
2966  			break;
2967  		usleep_range(512, 2048);
2968  	} while (++retry_count < 5);
2969  
2970  	if (!rc && !dfs_rsp)
2971  		rc = -EIO;
2972  	if (rc) {
2973  		if (!is_retryable_error(rc) && rc != -ENOENT && rc != -EOPNOTSUPP)
2974  			cifs_tcon_dbg(VFS, "%s: ioctl error: rc=%d\n", __func__, rc);
2975  		goto out;
2976  	}
2977  
2978  	rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
2979  				 num_of_nodes, target_nodes,
2980  				 nls_codepage, remap, search_name,
2981  				 true /* is_unicode */);
2982  	if (rc) {
2983  		cifs_tcon_dbg(VFS, "parse error in %s rc=%d\n", __func__, rc);
2984  		goto out;
2985  	}
2986  
2987   out:
2988  	if (tcon && !tcon->ipc) {
2989  		/* ipc tcons are not refcounted */
2990  		spin_lock(&cifs_tcp_ses_lock);
2991  		tcon->tc_count--;
2992  		trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count,
2993  				    netfs_trace_tcon_ref_dec_dfs_refer);
2994  		/* tc_count can never go negative */
2995  		WARN_ON(tcon->tc_count < 0);
2996  		spin_unlock(&cifs_tcp_ses_lock);
2997  	}
2998  	kfree(utf16_path);
2999  	kfree(dfs_req);
3000  	kfree(dfs_rsp);
3001  	return rc;
3002  }
3003  
3004  static struct smb_ntsd *
3005  get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
3006  		    const struct cifs_fid *cifsfid, u32 *pacllen, u32 info)
3007  {
3008  	struct smb_ntsd *pntsd = NULL;
3009  	unsigned int xid;
3010  	int rc = -EOPNOTSUPP;
3011  	struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3012  
3013  	if (IS_ERR(tlink))
3014  		return ERR_CAST(tlink);
3015  
3016  	xid = get_xid();
3017  	cifs_dbg(FYI, "trying to get acl\n");
3018  
3019  	rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
3020  			    cifsfid->volatile_fid, (void **)&pntsd, pacllen,
3021  			    info);
3022  	free_xid(xid);
3023  
3024  	cifs_put_tlink(tlink);
3025  
3026  	cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
3027  	if (rc)
3028  		return ERR_PTR(rc);
3029  	return pntsd;
3030  
3031  }
3032  
3033  static struct smb_ntsd *
3034  get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
3035  		     const char *path, u32 *pacllen, u32 info)
3036  {
3037  	struct smb_ntsd *pntsd = NULL;
3038  	u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3039  	unsigned int xid;
3040  	int rc;
3041  	struct cifs_tcon *tcon;
3042  	struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3043  	struct cifs_fid fid;
3044  	struct cifs_open_parms oparms;
3045  	__le16 *utf16_path;
3046  
3047  	cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
3048  	if (IS_ERR(tlink))
3049  		return ERR_CAST(tlink);
3050  
3051  	tcon = tlink_tcon(tlink);
3052  	xid = get_xid();
3053  
3054  	utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
3055  	if (!utf16_path) {
3056  		rc = -ENOMEM;
3057  		free_xid(xid);
3058  		return ERR_PTR(rc);
3059  	}
3060  
3061  	oparms = (struct cifs_open_parms) {
3062  		.tcon = tcon,
3063  		.path = path,
3064  		.desired_access = READ_CONTROL,
3065  		.disposition = FILE_OPEN,
3066  		/*
3067  		 * When querying an ACL, even if the file is a symlink
3068  		 * we want to open the source not the target, and so
3069  		 * the protocol requires that the client specify this
3070  		 * flag when opening a reparse point
3071  		 */
3072  		.create_options = cifs_create_options(cifs_sb, 0) |
3073  				  OPEN_REPARSE_POINT,
3074  		.fid = &fid,
3075  	};
3076  
3077  	if (info & SACL_SECINFO)
3078  		oparms.desired_access |= SYSTEM_SECURITY;
3079  
3080  	rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
3081  		       NULL);
3082  	kfree(utf16_path);
3083  	if (!rc) {
3084  		rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3085  				    fid.volatile_fid, (void **)&pntsd, pacllen,
3086  				    info);
3087  		SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3088  	}
3089  
3090  	cifs_put_tlink(tlink);
3091  	free_xid(xid);
3092  
3093  	cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
3094  	if (rc)
3095  		return ERR_PTR(rc);
3096  	return pntsd;
3097  }
3098  
3099  static int
3100  set_smb2_acl(struct smb_ntsd *pnntsd, __u32 acllen,
3101  		struct inode *inode, const char *path, int aclflag)
3102  {
3103  	u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3104  	unsigned int xid;
3105  	int rc, access_flags = 0;
3106  	struct cifs_tcon *tcon;
3107  	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3108  	struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3109  	struct cifs_fid fid;
3110  	struct cifs_open_parms oparms;
3111  	__le16 *utf16_path;
3112  
3113  	cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
3114  	if (IS_ERR(tlink))
3115  		return PTR_ERR(tlink);
3116  
3117  	tcon = tlink_tcon(tlink);
3118  	xid = get_xid();
3119  
3120  	if (aclflag & CIFS_ACL_OWNER || aclflag & CIFS_ACL_GROUP)
3121  		access_flags |= WRITE_OWNER;
3122  	if (aclflag & CIFS_ACL_SACL)
3123  		access_flags |= SYSTEM_SECURITY;
3124  	if (aclflag & CIFS_ACL_DACL)
3125  		access_flags |= WRITE_DAC;
3126  
3127  	utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
3128  	if (!utf16_path) {
3129  		rc = -ENOMEM;
3130  		free_xid(xid);
3131  		return rc;
3132  	}
3133  
3134  	oparms = (struct cifs_open_parms) {
3135  		.tcon = tcon,
3136  		.desired_access = access_flags,
3137  		.create_options = cifs_create_options(cifs_sb, 0),
3138  		.disposition = FILE_OPEN,
3139  		.path = path,
3140  		.fid = &fid,
3141  	};
3142  
3143  	rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL,
3144  		       NULL, NULL);
3145  	kfree(utf16_path);
3146  	if (!rc) {
3147  		rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3148  			    fid.volatile_fid, pnntsd, acllen, aclflag);
3149  		SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3150  	}
3151  
3152  	cifs_put_tlink(tlink);
3153  	free_xid(xid);
3154  	return rc;
3155  }
3156  
3157  /* Retrieve an ACL from the server */
3158  static struct smb_ntsd *
3159  get_smb2_acl(struct cifs_sb_info *cifs_sb,
3160  	     struct inode *inode, const char *path,
3161  	     u32 *pacllen, u32 info)
3162  {
3163  	struct smb_ntsd *pntsd = NULL;
3164  	struct cifsFileInfo *open_file = NULL;
3165  
3166  	if (inode && !(info & SACL_SECINFO))
3167  		open_file = find_readable_file(CIFS_I(inode), true);
3168  	if (!open_file || (info & SACL_SECINFO))
3169  		return get_smb2_acl_by_path(cifs_sb, path, pacllen, info);
3170  
3171  	pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen, info);
3172  	cifsFileInfo_put(open_file);
3173  	return pntsd;
3174  }
3175  
3176  static long smb3_zero_data(struct file *file, struct cifs_tcon *tcon,
3177  			     loff_t offset, loff_t len, unsigned int xid)
3178  {
3179  	struct cifsFileInfo *cfile = file->private_data;
3180  	struct file_zero_data_information fsctl_buf;
3181  
3182  	cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
3183  
3184  	fsctl_buf.FileOffset = cpu_to_le64(offset);
3185  	fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3186  
3187  	return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3188  			  cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
3189  			  (char *)&fsctl_buf,
3190  			  sizeof(struct file_zero_data_information),
3191  			  0, NULL, NULL);
3192  }
3193  
3194  static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
3195  			    unsigned long long offset, unsigned long long len,
3196  			    bool keep_size)
3197  {
3198  	struct cifs_ses *ses = tcon->ses;
3199  	struct inode *inode = file_inode(file);
3200  	struct cifsInodeInfo *cifsi = CIFS_I(inode);
3201  	struct cifsFileInfo *cfile = file->private_data;
3202  	struct netfs_inode *ictx = netfs_inode(inode);
3203  	unsigned long long i_size, new_size, remote_size;
3204  	long rc;
3205  	unsigned int xid;
3206  
3207  	xid = get_xid();
3208  
3209  	trace_smb3_zero_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3210  			      ses->Suid, offset, len);
3211  
3212  	inode_lock(inode);
3213  	filemap_invalidate_lock(inode->i_mapping);
3214  
3215  	i_size = i_size_read(inode);
3216  	remote_size = ictx->remote_i_size;
3217  	if (offset + len >= remote_size && offset < i_size) {
3218  		unsigned long long top = umin(offset + len, i_size);
3219  
3220  		rc = filemap_write_and_wait_range(inode->i_mapping, offset, top - 1);
3221  		if (rc < 0)
3222  			goto zero_range_exit;
3223  	}
3224  
3225  	/*
3226  	 * We zero the range through ioctl, so we need remove the page caches
3227  	 * first, otherwise the data may be inconsistent with the server.
3228  	 */
3229  	truncate_pagecache_range(inode, offset, offset + len - 1);
3230  
3231  	/* if file not oplocked can't be sure whether asking to extend size */
3232  	rc = -EOPNOTSUPP;
3233  	if (keep_size == false && !CIFS_CACHE_READ(cifsi))
3234  		goto zero_range_exit;
3235  
3236  	rc = smb3_zero_data(file, tcon, offset, len, xid);
3237  	if (rc < 0)
3238  		goto zero_range_exit;
3239  
3240  	/*
3241  	 * do we also need to change the size of the file?
3242  	 */
3243  	new_size = offset + len;
3244  	if (keep_size == false && (unsigned long long)i_size_read(inode) < new_size) {
3245  		rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3246  				  cfile->fid.volatile_fid, cfile->pid, new_size);
3247  		if (rc >= 0) {
3248  			truncate_setsize(inode, new_size);
3249  			fscache_resize_cookie(cifs_inode_cookie(inode), new_size);
3250  		}
3251  	}
3252  
3253   zero_range_exit:
3254  	filemap_invalidate_unlock(inode->i_mapping);
3255  	inode_unlock(inode);
3256  	free_xid(xid);
3257  	if (rc)
3258  		trace_smb3_zero_err(xid, cfile->fid.persistent_fid, tcon->tid,
3259  			      ses->Suid, offset, len, rc);
3260  	else
3261  		trace_smb3_zero_done(xid, cfile->fid.persistent_fid, tcon->tid,
3262  			      ses->Suid, offset, len);
3263  	return rc;
3264  }
3265  
3266  static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
3267  			    loff_t offset, loff_t len)
3268  {
3269  	struct inode *inode = file_inode(file);
3270  	struct cifsFileInfo *cfile = file->private_data;
3271  	struct file_zero_data_information fsctl_buf;
3272  	unsigned long long end = offset + len, i_size, remote_i_size;
3273  	long rc;
3274  	unsigned int xid;
3275  	__u8 set_sparse = 1;
3276  
3277  	xid = get_xid();
3278  
3279  	inode_lock(inode);
3280  	/* Need to make file sparse, if not already, before freeing range. */
3281  	/* Consider adding equivalent for compressed since it could also work */
3282  	if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
3283  		rc = -EOPNOTSUPP;
3284  		goto out;
3285  	}
3286  
3287  	filemap_invalidate_lock(inode->i_mapping);
3288  	/*
3289  	 * We implement the punch hole through ioctl, so we need remove the page
3290  	 * caches first, otherwise the data may be inconsistent with the server.
3291  	 */
3292  	truncate_pagecache_range(inode, offset, offset + len - 1);
3293  
3294  	cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
3295  
3296  	fsctl_buf.FileOffset = cpu_to_le64(offset);
3297  	fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3298  
3299  	rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3300  			cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
3301  			(char *)&fsctl_buf,
3302  			sizeof(struct file_zero_data_information),
3303  			CIFSMaxBufSize, NULL, NULL);
3304  
3305  	if (rc)
3306  		goto unlock;
3307  
3308  	/* If there's dirty data in the buffer that would extend the EOF if it
3309  	 * were written, then we need to move the EOF marker over to the lower
3310  	 * of the high end of the hole and the proposed EOF.  The problem is
3311  	 * that we locally hole-punch the tail of the dirty data, the proposed
3312  	 * EOF update will end up in the wrong place.
3313  	 */
3314  	i_size = i_size_read(inode);
3315  	remote_i_size = netfs_inode(inode)->remote_i_size;
3316  	if (end > remote_i_size && i_size > remote_i_size) {
3317  		unsigned long long extend_to = umin(end, i_size);
3318  		rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3319  				  cfile->fid.volatile_fid, cfile->pid, extend_to);
3320  		if (rc >= 0)
3321  			netfs_inode(inode)->remote_i_size = extend_to;
3322  	}
3323  
3324  unlock:
3325  	filemap_invalidate_unlock(inode->i_mapping);
3326  out:
3327  	inode_unlock(inode);
3328  	free_xid(xid);
3329  	return rc;
3330  }
3331  
3332  static int smb3_simple_fallocate_write_range(unsigned int xid,
3333  					     struct cifs_tcon *tcon,
3334  					     struct cifsFileInfo *cfile,
3335  					     loff_t off, loff_t len,
3336  					     char *buf)
3337  {
3338  	struct cifs_io_parms io_parms = {0};
3339  	int nbytes;
3340  	int rc = 0;
3341  	struct kvec iov[2];
3342  
3343  	io_parms.netfid = cfile->fid.netfid;
3344  	io_parms.pid = current->tgid;
3345  	io_parms.tcon = tcon;
3346  	io_parms.persistent_fid = cfile->fid.persistent_fid;
3347  	io_parms.volatile_fid = cfile->fid.volatile_fid;
3348  
3349  	while (len) {
3350  		io_parms.offset = off;
3351  		io_parms.length = len;
3352  		if (io_parms.length > SMB2_MAX_BUFFER_SIZE)
3353  			io_parms.length = SMB2_MAX_BUFFER_SIZE;
3354  		/* iov[0] is reserved for smb header */
3355  		iov[1].iov_base = buf;
3356  		iov[1].iov_len = io_parms.length;
3357  		rc = SMB2_write(xid, &io_parms, &nbytes, iov, 1);
3358  		if (rc)
3359  			break;
3360  		if (nbytes > len)
3361  			return -EINVAL;
3362  		buf += nbytes;
3363  		off += nbytes;
3364  		len -= nbytes;
3365  	}
3366  	return rc;
3367  }
3368  
3369  static int smb3_simple_fallocate_range(unsigned int xid,
3370  				       struct cifs_tcon *tcon,
3371  				       struct cifsFileInfo *cfile,
3372  				       loff_t off, loff_t len)
3373  {
3374  	struct file_allocated_range_buffer in_data, *out_data = NULL, *tmp_data;
3375  	u32 out_data_len;
3376  	char *buf = NULL;
3377  	loff_t l;
3378  	int rc;
3379  
3380  	in_data.file_offset = cpu_to_le64(off);
3381  	in_data.length = cpu_to_le64(len);
3382  	rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3383  			cfile->fid.volatile_fid,
3384  			FSCTL_QUERY_ALLOCATED_RANGES,
3385  			(char *)&in_data, sizeof(in_data),
3386  			1024 * sizeof(struct file_allocated_range_buffer),
3387  			(char **)&out_data, &out_data_len);
3388  	if (rc)
3389  		goto out;
3390  
3391  	buf = kzalloc(1024 * 1024, GFP_KERNEL);
3392  	if (buf == NULL) {
3393  		rc = -ENOMEM;
3394  		goto out;
3395  	}
3396  
3397  	tmp_data = out_data;
3398  	while (len) {
3399  		/*
3400  		 * The rest of the region is unmapped so write it all.
3401  		 */
3402  		if (out_data_len == 0) {
3403  			rc = smb3_simple_fallocate_write_range(xid, tcon,
3404  					       cfile, off, len, buf);
3405  			goto out;
3406  		}
3407  
3408  		if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3409  			rc = -EINVAL;
3410  			goto out;
3411  		}
3412  
3413  		if (off < le64_to_cpu(tmp_data->file_offset)) {
3414  			/*
3415  			 * We are at a hole. Write until the end of the region
3416  			 * or until the next allocated data,
3417  			 * whichever comes next.
3418  			 */
3419  			l = le64_to_cpu(tmp_data->file_offset) - off;
3420  			if (len < l)
3421  				l = len;
3422  			rc = smb3_simple_fallocate_write_range(xid, tcon,
3423  					       cfile, off, l, buf);
3424  			if (rc)
3425  				goto out;
3426  			off = off + l;
3427  			len = len - l;
3428  			if (len == 0)
3429  				goto out;
3430  		}
3431  		/*
3432  		 * We are at a section of allocated data, just skip forward
3433  		 * until the end of the data or the end of the region
3434  		 * we are supposed to fallocate, whichever comes first.
3435  		 */
3436  		l = le64_to_cpu(tmp_data->length);
3437  		if (len < l)
3438  			l = len;
3439  		off += l;
3440  		len -= l;
3441  
3442  		tmp_data = &tmp_data[1];
3443  		out_data_len -= sizeof(struct file_allocated_range_buffer);
3444  	}
3445  
3446   out:
3447  	kfree(out_data);
3448  	kfree(buf);
3449  	return rc;
3450  }
3451  
3452  
3453  static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
3454  			    loff_t off, loff_t len, bool keep_size)
3455  {
3456  	struct inode *inode;
3457  	struct cifsInodeInfo *cifsi;
3458  	struct cifsFileInfo *cfile = file->private_data;
3459  	long rc = -EOPNOTSUPP;
3460  	unsigned int xid;
3461  	loff_t new_eof;
3462  
3463  	xid = get_xid();
3464  
3465  	inode = d_inode(cfile->dentry);
3466  	cifsi = CIFS_I(inode);
3467  
3468  	trace_smb3_falloc_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3469  				tcon->ses->Suid, off, len);
3470  	/* if file not oplocked can't be sure whether asking to extend size */
3471  	if (!CIFS_CACHE_READ(cifsi))
3472  		if (keep_size == false) {
3473  			trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
3474  				tcon->tid, tcon->ses->Suid, off, len, rc);
3475  			free_xid(xid);
3476  			return rc;
3477  		}
3478  
3479  	/*
3480  	 * Extending the file
3481  	 */
3482  	if ((keep_size == false) && i_size_read(inode) < off + len) {
3483  		rc = inode_newsize_ok(inode, off + len);
3484  		if (rc)
3485  			goto out;
3486  
3487  		if (cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)
3488  			smb2_set_sparse(xid, tcon, cfile, inode, false);
3489  
3490  		new_eof = off + len;
3491  		rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3492  				  cfile->fid.volatile_fid, cfile->pid, new_eof);
3493  		if (rc == 0) {
3494  			cifsi->server_eof = new_eof;
3495  			cifs_setsize(inode, new_eof);
3496  			cifs_truncate_page(inode->i_mapping, inode->i_size);
3497  			truncate_setsize(inode, new_eof);
3498  		}
3499  		goto out;
3500  	}
3501  
3502  	/*
3503  	 * Files are non-sparse by default so falloc may be a no-op
3504  	 * Must check if file sparse. If not sparse, and since we are not
3505  	 * extending then no need to do anything since file already allocated
3506  	 */
3507  	if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
3508  		rc = 0;
3509  		goto out;
3510  	}
3511  
3512  	if (keep_size == true) {
3513  		/*
3514  		 * We can not preallocate pages beyond the end of the file
3515  		 * in SMB2
3516  		 */
3517  		if (off >= i_size_read(inode)) {
3518  			rc = 0;
3519  			goto out;
3520  		}
3521  		/*
3522  		 * For fallocates that are partially beyond the end of file,
3523  		 * clamp len so we only fallocate up to the end of file.
3524  		 */
3525  		if (off + len > i_size_read(inode)) {
3526  			len = i_size_read(inode) - off;
3527  		}
3528  	}
3529  
3530  	if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
3531  		/*
3532  		 * At this point, we are trying to fallocate an internal
3533  		 * regions of a sparse file. Since smb2 does not have a
3534  		 * fallocate command we have two otions on how to emulate this.
3535  		 * We can either turn the entire file to become non-sparse
3536  		 * which we only do if the fallocate is for virtually
3537  		 * the whole file,  or we can overwrite the region with zeroes
3538  		 * using SMB2_write, which could be prohibitevly expensive
3539  		 * if len is large.
3540  		 */
3541  		/*
3542  		 * We are only trying to fallocate a small region so
3543  		 * just write it with zero.
3544  		 */
3545  		if (len <= 1024 * 1024) {
3546  			rc = smb3_simple_fallocate_range(xid, tcon, cfile,
3547  							 off, len);
3548  			goto out;
3549  		}
3550  
3551  		/*
3552  		 * Check if falloc starts within first few pages of file
3553  		 * and ends within a few pages of the end of file to
3554  		 * ensure that most of file is being forced to be
3555  		 * fallocated now. If so then setting whole file sparse
3556  		 * ie potentially making a few extra pages at the beginning
3557  		 * or end of the file non-sparse via set_sparse is harmless.
3558  		 */
3559  		if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
3560  			rc = -EOPNOTSUPP;
3561  			goto out;
3562  		}
3563  	}
3564  
3565  	smb2_set_sparse(xid, tcon, cfile, inode, false);
3566  	rc = 0;
3567  
3568  out:
3569  	if (rc)
3570  		trace_smb3_falloc_err(xid, cfile->fid.persistent_fid, tcon->tid,
3571  				tcon->ses->Suid, off, len, rc);
3572  	else
3573  		trace_smb3_falloc_done(xid, cfile->fid.persistent_fid, tcon->tid,
3574  				tcon->ses->Suid, off, len);
3575  
3576  	free_xid(xid);
3577  	return rc;
3578  }
3579  
3580  static long smb3_collapse_range(struct file *file, struct cifs_tcon *tcon,
3581  			    loff_t off, loff_t len)
3582  {
3583  	int rc;
3584  	unsigned int xid;
3585  	struct inode *inode = file_inode(file);
3586  	struct cifsFileInfo *cfile = file->private_data;
3587  	struct cifsInodeInfo *cifsi = CIFS_I(inode);
3588  	loff_t old_eof, new_eof;
3589  
3590  	xid = get_xid();
3591  
3592  	inode_lock(inode);
3593  
3594  	old_eof = i_size_read(inode);
3595  	if ((off >= old_eof) ||
3596  	    off + len >= old_eof) {
3597  		rc = -EINVAL;
3598  		goto out;
3599  	}
3600  
3601  	filemap_invalidate_lock(inode->i_mapping);
3602  	rc = filemap_write_and_wait_range(inode->i_mapping, off, old_eof - 1);
3603  	if (rc < 0)
3604  		goto out_2;
3605  
3606  	truncate_pagecache_range(inode, off, old_eof);
3607  
3608  	rc = smb2_copychunk_range(xid, cfile, cfile, off + len,
3609  				  old_eof - off - len, off);
3610  	if (rc < 0)
3611  		goto out_2;
3612  
3613  	new_eof = old_eof - len;
3614  	rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3615  			  cfile->fid.volatile_fid, cfile->pid, new_eof);
3616  	if (rc < 0)
3617  		goto out_2;
3618  
3619  	rc = 0;
3620  
3621  	cifsi->server_eof = i_size_read(inode) - len;
3622  	truncate_setsize(inode, cifsi->server_eof);
3623  	fscache_resize_cookie(cifs_inode_cookie(inode), cifsi->server_eof);
3624  out_2:
3625  	filemap_invalidate_unlock(inode->i_mapping);
3626   out:
3627  	inode_unlock(inode);
3628  	free_xid(xid);
3629  	return rc;
3630  }
3631  
3632  static long smb3_insert_range(struct file *file, struct cifs_tcon *tcon,
3633  			      loff_t off, loff_t len)
3634  {
3635  	int rc;
3636  	unsigned int xid;
3637  	struct cifsFileInfo *cfile = file->private_data;
3638  	struct inode *inode = file_inode(file);
3639  	__u64 count, old_eof, new_eof;
3640  
3641  	xid = get_xid();
3642  
3643  	inode_lock(inode);
3644  
3645  	old_eof = i_size_read(inode);
3646  	if (off >= old_eof) {
3647  		rc = -EINVAL;
3648  		goto out;
3649  	}
3650  
3651  	count = old_eof - off;
3652  	new_eof = old_eof + len;
3653  
3654  	filemap_invalidate_lock(inode->i_mapping);
3655  	rc = filemap_write_and_wait_range(inode->i_mapping, off, new_eof - 1);
3656  	if (rc < 0)
3657  		goto out_2;
3658  	truncate_pagecache_range(inode, off, old_eof);
3659  
3660  	rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3661  			  cfile->fid.volatile_fid, cfile->pid, new_eof);
3662  	if (rc < 0)
3663  		goto out_2;
3664  
3665  	truncate_setsize(inode, new_eof);
3666  	fscache_resize_cookie(cifs_inode_cookie(inode), i_size_read(inode));
3667  
3668  	rc = smb2_copychunk_range(xid, cfile, cfile, off, count, off + len);
3669  	if (rc < 0)
3670  		goto out_2;
3671  
3672  	rc = smb3_zero_data(file, tcon, off, len, xid);
3673  	if (rc < 0)
3674  		goto out_2;
3675  
3676  	rc = 0;
3677  out_2:
3678  	filemap_invalidate_unlock(inode->i_mapping);
3679   out:
3680  	inode_unlock(inode);
3681  	free_xid(xid);
3682  	return rc;
3683  }
3684  
3685  static loff_t smb3_llseek(struct file *file, struct cifs_tcon *tcon, loff_t offset, int whence)
3686  {
3687  	struct cifsFileInfo *wrcfile, *cfile = file->private_data;
3688  	struct cifsInodeInfo *cifsi;
3689  	struct inode *inode;
3690  	int rc = 0;
3691  	struct file_allocated_range_buffer in_data, *out_data = NULL;
3692  	u32 out_data_len;
3693  	unsigned int xid;
3694  
3695  	if (whence != SEEK_HOLE && whence != SEEK_DATA)
3696  		return generic_file_llseek(file, offset, whence);
3697  
3698  	inode = d_inode(cfile->dentry);
3699  	cifsi = CIFS_I(inode);
3700  
3701  	if (offset < 0 || offset >= i_size_read(inode))
3702  		return -ENXIO;
3703  
3704  	xid = get_xid();
3705  	/*
3706  	 * We need to be sure that all dirty pages are written as they
3707  	 * might fill holes on the server.
3708  	 * Note that we also MUST flush any written pages since at least
3709  	 * some servers (Windows2016) will not reflect recent writes in
3710  	 * QUERY_ALLOCATED_RANGES until SMB2_flush is called.
3711  	 */
3712  	wrcfile = find_writable_file(cifsi, FIND_WR_ANY);
3713  	if (wrcfile) {
3714  		filemap_write_and_wait(inode->i_mapping);
3715  		smb2_flush_file(xid, tcon, &wrcfile->fid);
3716  		cifsFileInfo_put(wrcfile);
3717  	}
3718  
3719  	if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
3720  		if (whence == SEEK_HOLE)
3721  			offset = i_size_read(inode);
3722  		goto lseek_exit;
3723  	}
3724  
3725  	in_data.file_offset = cpu_to_le64(offset);
3726  	in_data.length = cpu_to_le64(i_size_read(inode));
3727  
3728  	rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3729  			cfile->fid.volatile_fid,
3730  			FSCTL_QUERY_ALLOCATED_RANGES,
3731  			(char *)&in_data, sizeof(in_data),
3732  			sizeof(struct file_allocated_range_buffer),
3733  			(char **)&out_data, &out_data_len);
3734  	if (rc == -E2BIG)
3735  		rc = 0;
3736  	if (rc)
3737  		goto lseek_exit;
3738  
3739  	if (whence == SEEK_HOLE && out_data_len == 0)
3740  		goto lseek_exit;
3741  
3742  	if (whence == SEEK_DATA && out_data_len == 0) {
3743  		rc = -ENXIO;
3744  		goto lseek_exit;
3745  	}
3746  
3747  	if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3748  		rc = -EINVAL;
3749  		goto lseek_exit;
3750  	}
3751  	if (whence == SEEK_DATA) {
3752  		offset = le64_to_cpu(out_data->file_offset);
3753  		goto lseek_exit;
3754  	}
3755  	if (offset < le64_to_cpu(out_data->file_offset))
3756  		goto lseek_exit;
3757  
3758  	offset = le64_to_cpu(out_data->file_offset) + le64_to_cpu(out_data->length);
3759  
3760   lseek_exit:
3761  	free_xid(xid);
3762  	kfree(out_data);
3763  	if (!rc)
3764  		return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
3765  	else
3766  		return rc;
3767  }
3768  
3769  static int smb3_fiemap(struct cifs_tcon *tcon,
3770  		       struct cifsFileInfo *cfile,
3771  		       struct fiemap_extent_info *fei, u64 start, u64 len)
3772  {
3773  	unsigned int xid;
3774  	struct file_allocated_range_buffer in_data, *out_data;
3775  	u32 out_data_len;
3776  	int i, num, rc, flags, last_blob;
3777  	u64 next;
3778  
3779  	rc = fiemap_prep(d_inode(cfile->dentry), fei, start, &len, 0);
3780  	if (rc)
3781  		return rc;
3782  
3783  	xid = get_xid();
3784   again:
3785  	in_data.file_offset = cpu_to_le64(start);
3786  	in_data.length = cpu_to_le64(len);
3787  
3788  	rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3789  			cfile->fid.volatile_fid,
3790  			FSCTL_QUERY_ALLOCATED_RANGES,
3791  			(char *)&in_data, sizeof(in_data),
3792  			1024 * sizeof(struct file_allocated_range_buffer),
3793  			(char **)&out_data, &out_data_len);
3794  	if (rc == -E2BIG) {
3795  		last_blob = 0;
3796  		rc = 0;
3797  	} else
3798  		last_blob = 1;
3799  	if (rc)
3800  		goto out;
3801  
3802  	if (out_data_len && out_data_len < sizeof(struct file_allocated_range_buffer)) {
3803  		rc = -EINVAL;
3804  		goto out;
3805  	}
3806  	if (out_data_len % sizeof(struct file_allocated_range_buffer)) {
3807  		rc = -EINVAL;
3808  		goto out;
3809  	}
3810  
3811  	num = out_data_len / sizeof(struct file_allocated_range_buffer);
3812  	for (i = 0; i < num; i++) {
3813  		flags = 0;
3814  		if (i == num - 1 && last_blob)
3815  			flags |= FIEMAP_EXTENT_LAST;
3816  
3817  		rc = fiemap_fill_next_extent(fei,
3818  				le64_to_cpu(out_data[i].file_offset),
3819  				le64_to_cpu(out_data[i].file_offset),
3820  				le64_to_cpu(out_data[i].length),
3821  				flags);
3822  		if (rc < 0)
3823  			goto out;
3824  		if (rc == 1) {
3825  			rc = 0;
3826  			goto out;
3827  		}
3828  	}
3829  
3830  	if (!last_blob) {
3831  		next = le64_to_cpu(out_data[num - 1].file_offset) +
3832  		  le64_to_cpu(out_data[num - 1].length);
3833  		len = len - (next - start);
3834  		start = next;
3835  		goto again;
3836  	}
3837  
3838   out:
3839  	free_xid(xid);
3840  	kfree(out_data);
3841  	return rc;
3842  }
3843  
3844  static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
3845  			   loff_t off, loff_t len)
3846  {
3847  	/* KEEP_SIZE already checked for by do_fallocate */
3848  	if (mode & FALLOC_FL_PUNCH_HOLE)
3849  		return smb3_punch_hole(file, tcon, off, len);
3850  	else if (mode & FALLOC_FL_ZERO_RANGE) {
3851  		if (mode & FALLOC_FL_KEEP_SIZE)
3852  			return smb3_zero_range(file, tcon, off, len, true);
3853  		return smb3_zero_range(file, tcon, off, len, false);
3854  	} else if (mode == FALLOC_FL_KEEP_SIZE)
3855  		return smb3_simple_falloc(file, tcon, off, len, true);
3856  	else if (mode == FALLOC_FL_COLLAPSE_RANGE)
3857  		return smb3_collapse_range(file, tcon, off, len);
3858  	else if (mode == FALLOC_FL_INSERT_RANGE)
3859  		return smb3_insert_range(file, tcon, off, len);
3860  	else if (mode == 0)
3861  		return smb3_simple_falloc(file, tcon, off, len, false);
3862  
3863  	return -EOPNOTSUPP;
3864  }
3865  
3866  static void
3867  smb2_downgrade_oplock(struct TCP_Server_Info *server,
3868  		      struct cifsInodeInfo *cinode, __u32 oplock,
3869  		      unsigned int epoch, bool *purge_cache)
3870  {
3871  	server->ops->set_oplock_level(cinode, oplock, 0, NULL);
3872  }
3873  
3874  static void
3875  smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3876  		       unsigned int epoch, bool *purge_cache);
3877  
3878  static void
3879  smb3_downgrade_oplock(struct TCP_Server_Info *server,
3880  		       struct cifsInodeInfo *cinode, __u32 oplock,
3881  		       unsigned int epoch, bool *purge_cache)
3882  {
3883  	unsigned int old_state = cinode->oplock;
3884  	unsigned int old_epoch = cinode->epoch;
3885  	unsigned int new_state;
3886  
3887  	if (epoch > old_epoch) {
3888  		smb21_set_oplock_level(cinode, oplock, 0, NULL);
3889  		cinode->epoch = epoch;
3890  	}
3891  
3892  	new_state = cinode->oplock;
3893  	*purge_cache = false;
3894  
3895  	if ((old_state & CIFS_CACHE_READ_FLG) != 0 &&
3896  	    (new_state & CIFS_CACHE_READ_FLG) == 0)
3897  		*purge_cache = true;
3898  	else if (old_state == new_state && (epoch - old_epoch > 1))
3899  		*purge_cache = true;
3900  }
3901  
3902  static void
3903  smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3904  		      unsigned int epoch, bool *purge_cache)
3905  {
3906  	oplock &= 0xFF;
3907  	cinode->lease_granted = false;
3908  	if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3909  		return;
3910  	if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
3911  		cinode->oplock = CIFS_CACHE_RHW_FLG;
3912  		cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
3913  			 &cinode->netfs.inode);
3914  	} else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
3915  		cinode->oplock = CIFS_CACHE_RW_FLG;
3916  		cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
3917  			 &cinode->netfs.inode);
3918  	} else if (oplock == SMB2_OPLOCK_LEVEL_II) {
3919  		cinode->oplock = CIFS_CACHE_READ_FLG;
3920  		cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
3921  			 &cinode->netfs.inode);
3922  	} else
3923  		cinode->oplock = 0;
3924  }
3925  
3926  static void
3927  smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3928  		       unsigned int epoch, bool *purge_cache)
3929  {
3930  	char message[5] = {0};
3931  	unsigned int new_oplock = 0;
3932  
3933  	oplock &= 0xFF;
3934  	cinode->lease_granted = true;
3935  	if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3936  		return;
3937  
3938  	/* Check if the server granted an oplock rather than a lease */
3939  	if (oplock & SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3940  		return smb2_set_oplock_level(cinode, oplock, epoch,
3941  					     purge_cache);
3942  
3943  	if (oplock & SMB2_LEASE_READ_CACHING_HE) {
3944  		new_oplock |= CIFS_CACHE_READ_FLG;
3945  		strcat(message, "R");
3946  	}
3947  	if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
3948  		new_oplock |= CIFS_CACHE_HANDLE_FLG;
3949  		strcat(message, "H");
3950  	}
3951  	if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
3952  		new_oplock |= CIFS_CACHE_WRITE_FLG;
3953  		strcat(message, "W");
3954  	}
3955  	if (!new_oplock)
3956  		strncpy(message, "None", sizeof(message));
3957  
3958  	cinode->oplock = new_oplock;
3959  	cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
3960  		 &cinode->netfs.inode);
3961  }
3962  
3963  static void
3964  smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3965  		      unsigned int epoch, bool *purge_cache)
3966  {
3967  	unsigned int old_oplock = cinode->oplock;
3968  
3969  	smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
3970  
3971  	if (purge_cache) {
3972  		*purge_cache = false;
3973  		if (old_oplock == CIFS_CACHE_READ_FLG) {
3974  			if (cinode->oplock == CIFS_CACHE_READ_FLG &&
3975  			    (epoch - cinode->epoch > 0))
3976  				*purge_cache = true;
3977  			else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3978  				 (epoch - cinode->epoch > 1))
3979  				*purge_cache = true;
3980  			else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3981  				 (epoch - cinode->epoch > 1))
3982  				*purge_cache = true;
3983  			else if (cinode->oplock == 0 &&
3984  				 (epoch - cinode->epoch > 0))
3985  				*purge_cache = true;
3986  		} else if (old_oplock == CIFS_CACHE_RH_FLG) {
3987  			if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3988  			    (epoch - cinode->epoch > 0))
3989  				*purge_cache = true;
3990  			else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3991  				 (epoch - cinode->epoch > 1))
3992  				*purge_cache = true;
3993  		}
3994  		cinode->epoch = epoch;
3995  	}
3996  }
3997  
3998  #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3999  static bool
4000  smb2_is_read_op(__u32 oplock)
4001  {
4002  	return oplock == SMB2_OPLOCK_LEVEL_II;
4003  }
4004  #endif /* CIFS_ALLOW_INSECURE_LEGACY */
4005  
4006  static bool
4007  smb21_is_read_op(__u32 oplock)
4008  {
4009  	return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
4010  	       !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
4011  }
4012  
4013  static __le32
4014  map_oplock_to_lease(u8 oplock)
4015  {
4016  	if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
4017  		return SMB2_LEASE_WRITE_CACHING_LE | SMB2_LEASE_READ_CACHING_LE;
4018  	else if (oplock == SMB2_OPLOCK_LEVEL_II)
4019  		return SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE;
4020  	else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
4021  		return SMB2_LEASE_HANDLE_CACHING_LE | SMB2_LEASE_READ_CACHING_LE |
4022  		       SMB2_LEASE_WRITE_CACHING_LE;
4023  	return 0;
4024  }
4025  
4026  static char *
4027  smb2_create_lease_buf(u8 *lease_key, u8 oplock)
4028  {
4029  	struct create_lease *buf;
4030  
4031  	buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
4032  	if (!buf)
4033  		return NULL;
4034  
4035  	memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
4036  	buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
4037  
4038  	buf->ccontext.DataOffset = cpu_to_le16(offsetof
4039  					(struct create_lease, lcontext));
4040  	buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
4041  	buf->ccontext.NameOffset = cpu_to_le16(offsetof
4042  				(struct create_lease, Name));
4043  	buf->ccontext.NameLength = cpu_to_le16(4);
4044  	/* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
4045  	buf->Name[0] = 'R';
4046  	buf->Name[1] = 'q';
4047  	buf->Name[2] = 'L';
4048  	buf->Name[3] = 's';
4049  	return (char *)buf;
4050  }
4051  
4052  static char *
4053  smb3_create_lease_buf(u8 *lease_key, u8 oplock)
4054  {
4055  	struct create_lease_v2 *buf;
4056  
4057  	buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
4058  	if (!buf)
4059  		return NULL;
4060  
4061  	memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
4062  	buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
4063  
4064  	buf->ccontext.DataOffset = cpu_to_le16(offsetof
4065  					(struct create_lease_v2, lcontext));
4066  	buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
4067  	buf->ccontext.NameOffset = cpu_to_le16(offsetof
4068  				(struct create_lease_v2, Name));
4069  	buf->ccontext.NameLength = cpu_to_le16(4);
4070  	/* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
4071  	buf->Name[0] = 'R';
4072  	buf->Name[1] = 'q';
4073  	buf->Name[2] = 'L';
4074  	buf->Name[3] = 's';
4075  	return (char *)buf;
4076  }
4077  
4078  static __u8
4079  smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
4080  {
4081  	struct create_lease *lc = (struct create_lease *)buf;
4082  
4083  	*epoch = 0; /* not used */
4084  	if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS_LE)
4085  		return SMB2_OPLOCK_LEVEL_NOCHANGE;
4086  	return le32_to_cpu(lc->lcontext.LeaseState);
4087  }
4088  
4089  static __u8
4090  smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
4091  {
4092  	struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
4093  
4094  	*epoch = le16_to_cpu(lc->lcontext.Epoch);
4095  	if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS_LE)
4096  		return SMB2_OPLOCK_LEVEL_NOCHANGE;
4097  	if (lease_key)
4098  		memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
4099  	return le32_to_cpu(lc->lcontext.LeaseState);
4100  }
4101  
4102  static unsigned int
4103  smb2_wp_retry_size(struct inode *inode)
4104  {
4105  	return min_t(unsigned int, CIFS_SB(inode->i_sb)->ctx->wsize,
4106  		     SMB2_MAX_BUFFER_SIZE);
4107  }
4108  
4109  static bool
4110  smb2_dir_needs_close(struct cifsFileInfo *cfile)
4111  {
4112  	return !cfile->invalidHandle;
4113  }
4114  
4115  static void
4116  fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
4117  		   struct smb_rqst *old_rq, __le16 cipher_type)
4118  {
4119  	struct smb2_hdr *shdr =
4120  			(struct smb2_hdr *)old_rq->rq_iov[0].iov_base;
4121  
4122  	memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
4123  	tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
4124  	tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
4125  	tr_hdr->Flags = cpu_to_le16(0x01);
4126  	if ((cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
4127  	    (cipher_type == SMB2_ENCRYPTION_AES256_GCM))
4128  		get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
4129  	else
4130  		get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
4131  	memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
4132  }
4133  
4134  static void *smb2_aead_req_alloc(struct crypto_aead *tfm, const struct smb_rqst *rqst,
4135  				 int num_rqst, const u8 *sig, u8 **iv,
4136  				 struct aead_request **req, struct sg_table *sgt,
4137  				 unsigned int *num_sgs, size_t *sensitive_size)
4138  {
4139  	unsigned int req_size = sizeof(**req) + crypto_aead_reqsize(tfm);
4140  	unsigned int iv_size = crypto_aead_ivsize(tfm);
4141  	unsigned int len;
4142  	u8 *p;
4143  
4144  	*num_sgs = cifs_get_num_sgs(rqst, num_rqst, sig);
4145  	if (IS_ERR_VALUE((long)(int)*num_sgs))
4146  		return ERR_PTR(*num_sgs);
4147  
4148  	len = iv_size;
4149  	len += crypto_aead_alignmask(tfm) & ~(crypto_tfm_ctx_alignment() - 1);
4150  	len = ALIGN(len, crypto_tfm_ctx_alignment());
4151  	len += req_size;
4152  	len = ALIGN(len, __alignof__(struct scatterlist));
4153  	len += array_size(*num_sgs, sizeof(struct scatterlist));
4154  	*sensitive_size = len;
4155  
4156  	p = kvzalloc(len, GFP_NOFS);
4157  	if (!p)
4158  		return ERR_PTR(-ENOMEM);
4159  
4160  	*iv = (u8 *)PTR_ALIGN(p, crypto_aead_alignmask(tfm) + 1);
4161  	*req = (struct aead_request *)PTR_ALIGN(*iv + iv_size,
4162  						crypto_tfm_ctx_alignment());
4163  	sgt->sgl = (struct scatterlist *)PTR_ALIGN((u8 *)*req + req_size,
4164  						   __alignof__(struct scatterlist));
4165  	return p;
4166  }
4167  
4168  static void *smb2_get_aead_req(struct crypto_aead *tfm, struct smb_rqst *rqst,
4169  			       int num_rqst, const u8 *sig, u8 **iv,
4170  			       struct aead_request **req, struct scatterlist **sgl,
4171  			       size_t *sensitive_size)
4172  {
4173  	struct sg_table sgtable = {};
4174  	unsigned int skip, num_sgs, i, j;
4175  	ssize_t rc;
4176  	void *p;
4177  
4178  	p = smb2_aead_req_alloc(tfm, rqst, num_rqst, sig, iv, req, &sgtable,
4179  				&num_sgs, sensitive_size);
4180  	if (IS_ERR(p))
4181  		return ERR_CAST(p);
4182  
4183  	sg_init_marker(sgtable.sgl, num_sgs);
4184  
4185  	/*
4186  	 * The first rqst has a transform header where the
4187  	 * first 20 bytes are not part of the encrypted blob.
4188  	 */
4189  	skip = 20;
4190  
4191  	for (i = 0; i < num_rqst; i++) {
4192  		struct iov_iter *iter = &rqst[i].rq_iter;
4193  		size_t count = iov_iter_count(iter);
4194  
4195  		for (j = 0; j < rqst[i].rq_nvec; j++) {
4196  			cifs_sg_set_buf(&sgtable,
4197  					rqst[i].rq_iov[j].iov_base + skip,
4198  					rqst[i].rq_iov[j].iov_len - skip);
4199  
4200  			/* See the above comment on the 'skip' assignment */
4201  			skip = 0;
4202  		}
4203  		sgtable.orig_nents = sgtable.nents;
4204  
4205  		rc = extract_iter_to_sg(iter, count, &sgtable,
4206  					num_sgs - sgtable.nents, 0);
4207  		iov_iter_revert(iter, rc);
4208  		sgtable.orig_nents = sgtable.nents;
4209  	}
4210  
4211  	cifs_sg_set_buf(&sgtable, sig, SMB2_SIGNATURE_SIZE);
4212  	sg_mark_end(&sgtable.sgl[sgtable.nents - 1]);
4213  	*sgl = sgtable.sgl;
4214  	return p;
4215  }
4216  
4217  static int
4218  smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
4219  {
4220  	struct TCP_Server_Info *pserver;
4221  	struct cifs_ses *ses;
4222  	u8 *ses_enc_key;
4223  
4224  	/* If server is a channel, select the primary channel */
4225  	pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
4226  
4227  	spin_lock(&cifs_tcp_ses_lock);
4228  	list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
4229  		if (ses->Suid == ses_id) {
4230  			spin_lock(&ses->ses_lock);
4231  			ses_enc_key = enc ? ses->smb3encryptionkey :
4232  				ses->smb3decryptionkey;
4233  			memcpy(key, ses_enc_key, SMB3_ENC_DEC_KEY_SIZE);
4234  			spin_unlock(&ses->ses_lock);
4235  			spin_unlock(&cifs_tcp_ses_lock);
4236  			return 0;
4237  		}
4238  	}
4239  	spin_unlock(&cifs_tcp_ses_lock);
4240  
4241  	trace_smb3_ses_not_found(ses_id);
4242  
4243  	return -EAGAIN;
4244  }
4245  /*
4246   * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
4247   * iov[0]   - transform header (associate data),
4248   * iov[1-N] - SMB2 header and pages - data to encrypt.
4249   * On success return encrypted data in iov[1-N] and pages, leave iov[0]
4250   * untouched.
4251   */
4252  static int
4253  crypt_message(struct TCP_Server_Info *server, int num_rqst,
4254  	      struct smb_rqst *rqst, int enc, struct crypto_aead *tfm)
4255  {
4256  	struct smb2_transform_hdr *tr_hdr =
4257  		(struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
4258  	unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
4259  	int rc = 0;
4260  	struct scatterlist *sg;
4261  	u8 sign[SMB2_SIGNATURE_SIZE] = {};
4262  	u8 key[SMB3_ENC_DEC_KEY_SIZE];
4263  	struct aead_request *req;
4264  	u8 *iv;
4265  	unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
4266  	void *creq;
4267  	size_t sensitive_size;
4268  
4269  	rc = smb2_get_enc_key(server, le64_to_cpu(tr_hdr->SessionId), enc, key);
4270  	if (rc) {
4271  		cifs_server_dbg(FYI, "%s: Could not get %scryption key. sid: 0x%llx\n", __func__,
4272  			 enc ? "en" : "de", le64_to_cpu(tr_hdr->SessionId));
4273  		return rc;
4274  	}
4275  
4276  	if ((server->cipher_type == SMB2_ENCRYPTION_AES256_CCM) ||
4277  		(server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
4278  		rc = crypto_aead_setkey(tfm, key, SMB3_GCM256_CRYPTKEY_SIZE);
4279  	else
4280  		rc = crypto_aead_setkey(tfm, key, SMB3_GCM128_CRYPTKEY_SIZE);
4281  
4282  	if (rc) {
4283  		cifs_server_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
4284  		return rc;
4285  	}
4286  
4287  	rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
4288  	if (rc) {
4289  		cifs_server_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
4290  		return rc;
4291  	}
4292  
4293  	creq = smb2_get_aead_req(tfm, rqst, num_rqst, sign, &iv, &req, &sg,
4294  				 &sensitive_size);
4295  	if (IS_ERR(creq))
4296  		return PTR_ERR(creq);
4297  
4298  	if (!enc) {
4299  		memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
4300  		crypt_len += SMB2_SIGNATURE_SIZE;
4301  	}
4302  
4303  	if ((server->cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
4304  	    (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
4305  		memcpy(iv, (char *)tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
4306  	else {
4307  		iv[0] = 3;
4308  		memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
4309  	}
4310  
4311  	aead_request_set_tfm(req, tfm);
4312  	aead_request_set_crypt(req, sg, sg, crypt_len, iv);
4313  	aead_request_set_ad(req, assoc_data_len);
4314  
4315  	rc = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
4316  
4317  	if (!rc && enc)
4318  		memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
4319  
4320  	kvfree_sensitive(creq, sensitive_size);
4321  	return rc;
4322  }
4323  
4324  /*
4325   * Clear a read buffer, discarding the folios which have XA_MARK_0 set.
4326   */
4327  static void cifs_clear_xarray_buffer(struct xarray *buffer)
4328  {
4329  	struct folio *folio;
4330  
4331  	XA_STATE(xas, buffer, 0);
4332  
4333  	rcu_read_lock();
4334  	xas_for_each_marked(&xas, folio, ULONG_MAX, XA_MARK_0) {
4335  		folio_put(folio);
4336  	}
4337  	rcu_read_unlock();
4338  	xa_destroy(buffer);
4339  }
4340  
4341  void
4342  smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
4343  {
4344  	int i;
4345  
4346  	for (i = 0; i < num_rqst; i++)
4347  		if (!xa_empty(&rqst[i].rq_buffer))
4348  			cifs_clear_xarray_buffer(&rqst[i].rq_buffer);
4349  }
4350  
4351  /*
4352   * This function will initialize new_rq and encrypt the content.
4353   * The first entry, new_rq[0], only contains a single iov which contains
4354   * a smb2_transform_hdr and is pre-allocated by the caller.
4355   * This function then populates new_rq[1+] with the content from olq_rq[0+].
4356   *
4357   * The end result is an array of smb_rqst structures where the first structure
4358   * only contains a single iov for the transform header which we then can pass
4359   * to crypt_message().
4360   *
4361   * new_rq[0].rq_iov[0] :  smb2_transform_hdr pre-allocated by the caller
4362   * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
4363   */
4364  static int
4365  smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
4366  		       struct smb_rqst *new_rq, struct smb_rqst *old_rq)
4367  {
4368  	struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
4369  	struct page *page;
4370  	unsigned int orig_len = 0;
4371  	int i, j;
4372  	int rc = -ENOMEM;
4373  
4374  	for (i = 1; i < num_rqst; i++) {
4375  		struct smb_rqst *old = &old_rq[i - 1];
4376  		struct smb_rqst *new = &new_rq[i];
4377  		struct xarray *buffer = &new->rq_buffer;
4378  		size_t size = iov_iter_count(&old->rq_iter), seg, copied = 0;
4379  
4380  		orig_len += smb_rqst_len(server, old);
4381  		new->rq_iov = old->rq_iov;
4382  		new->rq_nvec = old->rq_nvec;
4383  
4384  		xa_init(buffer);
4385  
4386  		if (size > 0) {
4387  			unsigned int npages = DIV_ROUND_UP(size, PAGE_SIZE);
4388  
4389  			for (j = 0; j < npages; j++) {
4390  				void *o;
4391  
4392  				rc = -ENOMEM;
4393  				page = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
4394  				if (!page)
4395  					goto err_free;
4396  				page->index = j;
4397  				o = xa_store(buffer, j, page, GFP_KERNEL);
4398  				if (xa_is_err(o)) {
4399  					rc = xa_err(o);
4400  					put_page(page);
4401  					goto err_free;
4402  				}
4403  
4404  				xa_set_mark(buffer, j, XA_MARK_0);
4405  
4406  				seg = min_t(size_t, size - copied, PAGE_SIZE);
4407  				if (copy_page_from_iter(page, 0, seg, &old->rq_iter) != seg) {
4408  					rc = -EFAULT;
4409  					goto err_free;
4410  				}
4411  				copied += seg;
4412  			}
4413  			iov_iter_xarray(&new->rq_iter, ITER_SOURCE,
4414  					buffer, 0, size);
4415  			new->rq_iter_size = size;
4416  		}
4417  	}
4418  
4419  	/* fill the 1st iov with a transform header */
4420  	fill_transform_hdr(tr_hdr, orig_len, old_rq, server->cipher_type);
4421  
4422  	rc = crypt_message(server, num_rqst, new_rq, 1, server->secmech.enc);
4423  	cifs_dbg(FYI, "Encrypt message returned %d\n", rc);
4424  	if (rc)
4425  		goto err_free;
4426  
4427  	return rc;
4428  
4429  err_free:
4430  	smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
4431  	return rc;
4432  }
4433  
4434  static int
4435  smb3_is_transform_hdr(void *buf)
4436  {
4437  	struct smb2_transform_hdr *trhdr = buf;
4438  
4439  	return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
4440  }
4441  
4442  static int
4443  decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
4444  		 unsigned int buf_data_size, struct iov_iter *iter,
4445  		 bool is_offloaded)
4446  {
4447  	struct crypto_aead *tfm;
4448  	struct smb_rqst rqst = {NULL};
4449  	struct kvec iov[2];
4450  	size_t iter_size = 0;
4451  	int rc;
4452  
4453  	iov[0].iov_base = buf;
4454  	iov[0].iov_len = sizeof(struct smb2_transform_hdr);
4455  	iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
4456  	iov[1].iov_len = buf_data_size;
4457  
4458  	rqst.rq_iov = iov;
4459  	rqst.rq_nvec = 2;
4460  	if (iter) {
4461  		rqst.rq_iter = *iter;
4462  		rqst.rq_iter_size = iov_iter_count(iter);
4463  		iter_size = iov_iter_count(iter);
4464  	}
4465  
4466  	if (is_offloaded) {
4467  		if ((server->cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
4468  		    (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
4469  			tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
4470  		else
4471  			tfm = crypto_alloc_aead("ccm(aes)", 0, 0);
4472  		if (IS_ERR(tfm)) {
4473  			rc = PTR_ERR(tfm);
4474  			cifs_server_dbg(VFS, "%s: Failed alloc decrypt TFM, rc=%d\n", __func__, rc);
4475  
4476  			return rc;
4477  		}
4478  	} else {
4479  		if (unlikely(!server->secmech.dec))
4480  			return -EIO;
4481  
4482  		tfm = server->secmech.dec;
4483  	}
4484  
4485  	rc = crypt_message(server, 1, &rqst, 0, tfm);
4486  	cifs_dbg(FYI, "Decrypt message returned %d\n", rc);
4487  
4488  	if (is_offloaded)
4489  		crypto_free_aead(tfm);
4490  
4491  	if (rc)
4492  		return rc;
4493  
4494  	memmove(buf, iov[1].iov_base, buf_data_size);
4495  
4496  	if (!is_offloaded)
4497  		server->total_read = buf_data_size + iter_size;
4498  
4499  	return rc;
4500  }
4501  
4502  static int
4503  cifs_copy_pages_to_iter(struct xarray *pages, unsigned int data_size,
4504  			unsigned int skip, struct iov_iter *iter)
4505  {
4506  	struct page *page;
4507  	unsigned long index;
4508  
4509  	xa_for_each(pages, index, page) {
4510  		size_t n, len = min_t(unsigned int, PAGE_SIZE - skip, data_size);
4511  
4512  		n = copy_page_to_iter(page, skip, len, iter);
4513  		if (n != len) {
4514  			cifs_dbg(VFS, "%s: something went wrong\n", __func__);
4515  			return -EIO;
4516  		}
4517  		data_size -= n;
4518  		skip = 0;
4519  	}
4520  
4521  	return 0;
4522  }
4523  
4524  static int
4525  handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
4526  		 char *buf, unsigned int buf_len, struct xarray *pages,
4527  		 unsigned int pages_len, bool is_offloaded)
4528  {
4529  	unsigned int data_offset;
4530  	unsigned int data_len;
4531  	unsigned int cur_off;
4532  	unsigned int cur_page_idx;
4533  	unsigned int pad_len;
4534  	struct cifs_readdata *rdata = mid->callback_data;
4535  	struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
4536  	int length;
4537  	bool use_rdma_mr = false;
4538  
4539  	if (shdr->Command != SMB2_READ) {
4540  		cifs_server_dbg(VFS, "only big read responses are supported\n");
4541  		return -EOPNOTSUPP;
4542  	}
4543  
4544  	if (server->ops->is_session_expired &&
4545  	    server->ops->is_session_expired(buf)) {
4546  		if (!is_offloaded)
4547  			cifs_reconnect(server, true);
4548  		return -1;
4549  	}
4550  
4551  	if (server->ops->is_status_pending &&
4552  			server->ops->is_status_pending(buf, server))
4553  		return -1;
4554  
4555  	/* set up first two iov to get credits */
4556  	rdata->iov[0].iov_base = buf;
4557  	rdata->iov[0].iov_len = 0;
4558  	rdata->iov[1].iov_base = buf;
4559  	rdata->iov[1].iov_len =
4560  		min_t(unsigned int, buf_len, server->vals->read_rsp_size);
4561  	cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
4562  		 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
4563  	cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
4564  		 rdata->iov[1].iov_base, rdata->iov[1].iov_len);
4565  
4566  	rdata->result = server->ops->map_error(buf, true);
4567  	if (rdata->result != 0) {
4568  		cifs_dbg(FYI, "%s: server returned error %d\n",
4569  			 __func__, rdata->result);
4570  		/* normal error on read response */
4571  		if (is_offloaded)
4572  			mid->mid_state = MID_RESPONSE_RECEIVED;
4573  		else
4574  			dequeue_mid(mid, false);
4575  		return 0;
4576  	}
4577  
4578  	data_offset = server->ops->read_data_offset(buf);
4579  #ifdef CONFIG_CIFS_SMB_DIRECT
4580  	use_rdma_mr = rdata->mr;
4581  #endif
4582  	data_len = server->ops->read_data_length(buf, use_rdma_mr);
4583  
4584  	if (data_offset < server->vals->read_rsp_size) {
4585  		/*
4586  		 * win2k8 sometimes sends an offset of 0 when the read
4587  		 * is beyond the EOF. Treat it as if the data starts just after
4588  		 * the header.
4589  		 */
4590  		cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
4591  			 __func__, data_offset);
4592  		data_offset = server->vals->read_rsp_size;
4593  	} else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
4594  		/* data_offset is beyond the end of smallbuf */
4595  		cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
4596  			 __func__, data_offset);
4597  		rdata->result = -EIO;
4598  		if (is_offloaded)
4599  			mid->mid_state = MID_RESPONSE_MALFORMED;
4600  		else
4601  			dequeue_mid(mid, rdata->result);
4602  		return 0;
4603  	}
4604  
4605  	pad_len = data_offset - server->vals->read_rsp_size;
4606  
4607  	if (buf_len <= data_offset) {
4608  		/* read response payload is in pages */
4609  		cur_page_idx = pad_len / PAGE_SIZE;
4610  		cur_off = pad_len % PAGE_SIZE;
4611  
4612  		if (cur_page_idx != 0) {
4613  			/* data offset is beyond the 1st page of response */
4614  			cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
4615  				 __func__, data_offset);
4616  			rdata->result = -EIO;
4617  			if (is_offloaded)
4618  				mid->mid_state = MID_RESPONSE_MALFORMED;
4619  			else
4620  				dequeue_mid(mid, rdata->result);
4621  			return 0;
4622  		}
4623  
4624  		if (data_len > pages_len - pad_len) {
4625  			/* data_len is corrupt -- discard frame */
4626  			rdata->result = -EIO;
4627  			if (is_offloaded)
4628  				mid->mid_state = MID_RESPONSE_MALFORMED;
4629  			else
4630  				dequeue_mid(mid, rdata->result);
4631  			return 0;
4632  		}
4633  
4634  		/* Copy the data to the output I/O iterator. */
4635  		rdata->result = cifs_copy_pages_to_iter(pages, pages_len,
4636  							cur_off, &rdata->iter);
4637  		if (rdata->result != 0) {
4638  			if (is_offloaded)
4639  				mid->mid_state = MID_RESPONSE_MALFORMED;
4640  			else
4641  				dequeue_mid(mid, rdata->result);
4642  			return 0;
4643  		}
4644  		rdata->got_bytes = pages_len;
4645  
4646  	} else if (buf_len >= data_offset + data_len) {
4647  		/* read response payload is in buf */
4648  		WARN_ONCE(pages && !xa_empty(pages),
4649  			  "read data can be either in buf or in pages");
4650  		length = copy_to_iter(buf + data_offset, data_len, &rdata->iter);
4651  		if (length < 0)
4652  			return length;
4653  		rdata->got_bytes = data_len;
4654  	} else {
4655  		/* read response payload cannot be in both buf and pages */
4656  		WARN_ONCE(1, "buf can not contain only a part of read data");
4657  		rdata->result = -EIO;
4658  		if (is_offloaded)
4659  			mid->mid_state = MID_RESPONSE_MALFORMED;
4660  		else
4661  			dequeue_mid(mid, rdata->result);
4662  		return 0;
4663  	}
4664  
4665  	if (is_offloaded)
4666  		mid->mid_state = MID_RESPONSE_RECEIVED;
4667  	else
4668  		dequeue_mid(mid, false);
4669  	return 0;
4670  }
4671  
4672  struct smb2_decrypt_work {
4673  	struct work_struct decrypt;
4674  	struct TCP_Server_Info *server;
4675  	struct xarray buffer;
4676  	char *buf;
4677  	unsigned int len;
4678  };
4679  
4680  
4681  static void smb2_decrypt_offload(struct work_struct *work)
4682  {
4683  	struct smb2_decrypt_work *dw = container_of(work,
4684  				struct smb2_decrypt_work, decrypt);
4685  	int rc;
4686  	struct mid_q_entry *mid;
4687  	struct iov_iter iter;
4688  
4689  	iov_iter_xarray(&iter, ITER_DEST, &dw->buffer, 0, dw->len);
4690  	rc = decrypt_raw_data(dw->server, dw->buf, dw->server->vals->read_rsp_size,
4691  			      &iter, true);
4692  	if (rc) {
4693  		cifs_dbg(VFS, "error decrypting rc=%d\n", rc);
4694  		goto free_pages;
4695  	}
4696  
4697  	dw->server->lstrp = jiffies;
4698  	mid = smb2_find_dequeue_mid(dw->server, dw->buf);
4699  	if (mid == NULL)
4700  		cifs_dbg(FYI, "mid not found\n");
4701  	else {
4702  		mid->decrypted = true;
4703  		rc = handle_read_data(dw->server, mid, dw->buf,
4704  				      dw->server->vals->read_rsp_size,
4705  				      &dw->buffer, dw->len,
4706  				      true);
4707  		if (rc >= 0) {
4708  #ifdef CONFIG_CIFS_STATS2
4709  			mid->when_received = jiffies;
4710  #endif
4711  			if (dw->server->ops->is_network_name_deleted)
4712  				dw->server->ops->is_network_name_deleted(dw->buf,
4713  									 dw->server);
4714  
4715  			mid->callback(mid);
4716  		} else {
4717  			spin_lock(&dw->server->srv_lock);
4718  			if (dw->server->tcpStatus == CifsNeedReconnect) {
4719  				spin_lock(&dw->server->mid_lock);
4720  				mid->mid_state = MID_RETRY_NEEDED;
4721  				spin_unlock(&dw->server->mid_lock);
4722  				spin_unlock(&dw->server->srv_lock);
4723  				mid->callback(mid);
4724  			} else {
4725  				spin_lock(&dw->server->mid_lock);
4726  				mid->mid_state = MID_REQUEST_SUBMITTED;
4727  				mid->mid_flags &= ~(MID_DELETED);
4728  				list_add_tail(&mid->qhead,
4729  					&dw->server->pending_mid_q);
4730  				spin_unlock(&dw->server->mid_lock);
4731  				spin_unlock(&dw->server->srv_lock);
4732  			}
4733  		}
4734  		release_mid(mid);
4735  	}
4736  
4737  free_pages:
4738  	cifs_clear_xarray_buffer(&dw->buffer);
4739  	cifs_small_buf_release(dw->buf);
4740  	kfree(dw);
4741  }
4742  
4743  
4744  static int
4745  receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid,
4746  		       int *num_mids)
4747  {
4748  	struct page *page;
4749  	char *buf = server->smallbuf;
4750  	struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4751  	struct iov_iter iter;
4752  	unsigned int len, npages;
4753  	unsigned int buflen = server->pdu_size;
4754  	int rc;
4755  	int i = 0;
4756  	struct smb2_decrypt_work *dw;
4757  
4758  	dw = kzalloc(sizeof(struct smb2_decrypt_work), GFP_KERNEL);
4759  	if (!dw)
4760  		return -ENOMEM;
4761  	xa_init(&dw->buffer);
4762  	INIT_WORK(&dw->decrypt, smb2_decrypt_offload);
4763  	dw->server = server;
4764  
4765  	*num_mids = 1;
4766  	len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
4767  		sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
4768  
4769  	rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
4770  	if (rc < 0)
4771  		goto free_dw;
4772  	server->total_read += rc;
4773  
4774  	len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
4775  		server->vals->read_rsp_size;
4776  	dw->len = len;
4777  	npages = DIV_ROUND_UP(len, PAGE_SIZE);
4778  
4779  	rc = -ENOMEM;
4780  	for (; i < npages; i++) {
4781  		void *old;
4782  
4783  		page = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
4784  		if (!page)
4785  			goto discard_data;
4786  		page->index = i;
4787  		old = xa_store(&dw->buffer, i, page, GFP_KERNEL);
4788  		if (xa_is_err(old)) {
4789  			rc = xa_err(old);
4790  			put_page(page);
4791  			goto discard_data;
4792  		}
4793  		xa_set_mark(&dw->buffer, i, XA_MARK_0);
4794  	}
4795  
4796  	iov_iter_xarray(&iter, ITER_DEST, &dw->buffer, 0, npages * PAGE_SIZE);
4797  
4798  	/* Read the data into the buffer and clear excess bufferage. */
4799  	rc = cifs_read_iter_from_socket(server, &iter, dw->len);
4800  	if (rc < 0)
4801  		goto discard_data;
4802  
4803  	server->total_read += rc;
4804  	if (rc < npages * PAGE_SIZE)
4805  		iov_iter_zero(npages * PAGE_SIZE - rc, &iter);
4806  	iov_iter_revert(&iter, npages * PAGE_SIZE);
4807  	iov_iter_truncate(&iter, dw->len);
4808  
4809  	rc = cifs_discard_remaining_data(server);
4810  	if (rc)
4811  		goto free_pages;
4812  
4813  	/*
4814  	 * For large reads, offload to different thread for better performance,
4815  	 * use more cores decrypting which can be expensive
4816  	 */
4817  
4818  	if ((server->min_offload) && (server->in_flight > 1) &&
4819  	    (server->pdu_size >= server->min_offload)) {
4820  		dw->buf = server->smallbuf;
4821  		server->smallbuf = (char *)cifs_small_buf_get();
4822  
4823  		queue_work(decrypt_wq, &dw->decrypt);
4824  		*num_mids = 0; /* worker thread takes care of finding mid */
4825  		return -1;
4826  	}
4827  
4828  	rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
4829  			      &iter, false);
4830  	if (rc)
4831  		goto free_pages;
4832  
4833  	*mid = smb2_find_mid(server, buf);
4834  	if (*mid == NULL) {
4835  		cifs_dbg(FYI, "mid not found\n");
4836  	} else {
4837  		cifs_dbg(FYI, "mid found\n");
4838  		(*mid)->decrypted = true;
4839  		rc = handle_read_data(server, *mid, buf,
4840  				      server->vals->read_rsp_size,
4841  				      &dw->buffer, dw->len, false);
4842  		if (rc >= 0) {
4843  			if (server->ops->is_network_name_deleted) {
4844  				server->ops->is_network_name_deleted(buf,
4845  								server);
4846  			}
4847  		}
4848  	}
4849  
4850  free_pages:
4851  	cifs_clear_xarray_buffer(&dw->buffer);
4852  free_dw:
4853  	kfree(dw);
4854  	return rc;
4855  discard_data:
4856  	cifs_discard_remaining_data(server);
4857  	goto free_pages;
4858  }
4859  
4860  static int
4861  receive_encrypted_standard(struct TCP_Server_Info *server,
4862  			   struct mid_q_entry **mids, char **bufs,
4863  			   int *num_mids)
4864  {
4865  	int ret, length;
4866  	char *buf = server->smallbuf;
4867  	struct smb2_hdr *shdr;
4868  	unsigned int pdu_length = server->pdu_size;
4869  	unsigned int buf_size;
4870  	unsigned int next_cmd;
4871  	struct mid_q_entry *mid_entry;
4872  	int next_is_large;
4873  	char *next_buffer = NULL;
4874  
4875  	*num_mids = 0;
4876  
4877  	/* switch to large buffer if too big for a small one */
4878  	if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
4879  		server->large_buf = true;
4880  		memcpy(server->bigbuf, buf, server->total_read);
4881  		buf = server->bigbuf;
4882  	}
4883  
4884  	/* now read the rest */
4885  	length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
4886  				pdu_length - HEADER_SIZE(server) + 1);
4887  	if (length < 0)
4888  		return length;
4889  	server->total_read += length;
4890  
4891  	buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
4892  	length = decrypt_raw_data(server, buf, buf_size, NULL, false);
4893  	if (length)
4894  		return length;
4895  
4896  	next_is_large = server->large_buf;
4897  one_more:
4898  	shdr = (struct smb2_hdr *)buf;
4899  	next_cmd = le32_to_cpu(shdr->NextCommand);
4900  	if (next_cmd) {
4901  		if (WARN_ON_ONCE(next_cmd > pdu_length))
4902  			return -1;
4903  		if (next_is_large)
4904  			next_buffer = (char *)cifs_buf_get();
4905  		else
4906  			next_buffer = (char *)cifs_small_buf_get();
4907  		memcpy(next_buffer, buf + next_cmd, pdu_length - next_cmd);
4908  	}
4909  
4910  	mid_entry = smb2_find_mid(server, buf);
4911  	if (mid_entry == NULL)
4912  		cifs_dbg(FYI, "mid not found\n");
4913  	else {
4914  		cifs_dbg(FYI, "mid found\n");
4915  		mid_entry->decrypted = true;
4916  		mid_entry->resp_buf_size = server->pdu_size;
4917  	}
4918  
4919  	if (*num_mids >= MAX_COMPOUND) {
4920  		cifs_server_dbg(VFS, "too many PDUs in compound\n");
4921  		return -1;
4922  	}
4923  	bufs[*num_mids] = buf;
4924  	mids[(*num_mids)++] = mid_entry;
4925  
4926  	if (mid_entry && mid_entry->handle)
4927  		ret = mid_entry->handle(server, mid_entry);
4928  	else
4929  		ret = cifs_handle_standard(server, mid_entry);
4930  
4931  	if (ret == 0 && next_cmd) {
4932  		pdu_length -= next_cmd;
4933  		server->large_buf = next_is_large;
4934  		if (next_is_large)
4935  			server->bigbuf = buf = next_buffer;
4936  		else
4937  			server->smallbuf = buf = next_buffer;
4938  		goto one_more;
4939  	} else if (ret != 0) {
4940  		/*
4941  		 * ret != 0 here means that we didn't get to handle_mid() thus
4942  		 * server->smallbuf and server->bigbuf are still valid. We need
4943  		 * to free next_buffer because it is not going to be used
4944  		 * anywhere.
4945  		 */
4946  		if (next_is_large)
4947  			free_rsp_buf(CIFS_LARGE_BUFFER, next_buffer);
4948  		else
4949  			free_rsp_buf(CIFS_SMALL_BUFFER, next_buffer);
4950  	}
4951  
4952  	return ret;
4953  }
4954  
4955  static int
4956  smb3_receive_transform(struct TCP_Server_Info *server,
4957  		       struct mid_q_entry **mids, char **bufs, int *num_mids)
4958  {
4959  	char *buf = server->smallbuf;
4960  	unsigned int pdu_length = server->pdu_size;
4961  	struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4962  	unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
4963  
4964  	if (pdu_length < sizeof(struct smb2_transform_hdr) +
4965  						sizeof(struct smb2_hdr)) {
4966  		cifs_server_dbg(VFS, "Transform message is too small (%u)\n",
4967  			 pdu_length);
4968  		cifs_reconnect(server, true);
4969  		return -ECONNABORTED;
4970  	}
4971  
4972  	if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
4973  		cifs_server_dbg(VFS, "Transform message is broken\n");
4974  		cifs_reconnect(server, true);
4975  		return -ECONNABORTED;
4976  	}
4977  
4978  	/* TODO: add support for compounds containing READ. */
4979  	if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server)) {
4980  		return receive_encrypted_read(server, &mids[0], num_mids);
4981  	}
4982  
4983  	return receive_encrypted_standard(server, mids, bufs, num_mids);
4984  }
4985  
4986  int
4987  smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
4988  {
4989  	char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
4990  
4991  	return handle_read_data(server, mid, buf, server->pdu_size,
4992  				NULL, 0, false);
4993  }
4994  
4995  static int smb2_next_header(struct TCP_Server_Info *server, char *buf,
4996  			    unsigned int *noff)
4997  {
4998  	struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
4999  	struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
5000  
5001  	if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
5002  		*noff = le32_to_cpu(t_hdr->OriginalMessageSize);
5003  		if (unlikely(check_add_overflow(*noff, sizeof(*t_hdr), noff)))
5004  			return -EINVAL;
5005  	} else {
5006  		*noff = le32_to_cpu(hdr->NextCommand);
5007  	}
5008  	if (unlikely(*noff && *noff < MID_HEADER_SIZE(server)))
5009  		return -EINVAL;
5010  	return 0;
5011  }
5012  
5013  static int __cifs_sfu_make_node(unsigned int xid, struct inode *inode,
5014  				struct dentry *dentry, struct cifs_tcon *tcon,
5015  				const char *full_path, umode_t mode, dev_t dev)
5016  {
5017  	struct TCP_Server_Info *server = tcon->ses->server;
5018  	struct cifs_open_parms oparms;
5019  	struct cifs_io_parms io_parms = {};
5020  	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
5021  	struct cifs_fid fid;
5022  	unsigned int bytes_written;
5023  	struct win_dev pdev = {};
5024  	struct kvec iov[2];
5025  	__u32 oplock = server->oplocks ? REQ_OPLOCK : 0;
5026  	int rc;
5027  
5028  	switch (mode & S_IFMT) {
5029  	case S_IFCHR:
5030  		strscpy(pdev.type, "IntxCHR", strlen("IntxChr"));
5031  		pdev.major = cpu_to_le64(MAJOR(dev));
5032  		pdev.minor = cpu_to_le64(MINOR(dev));
5033  		break;
5034  	case S_IFBLK:
5035  		strscpy(pdev.type, "IntxBLK", strlen("IntxBLK"));
5036  		pdev.major = cpu_to_le64(MAJOR(dev));
5037  		pdev.minor = cpu_to_le64(MINOR(dev));
5038  		break;
5039  	case S_IFIFO:
5040  		strscpy(pdev.type, "LnxFIFO", strlen("LnxFIFO"));
5041  		break;
5042  	default:
5043  		return -EPERM;
5044  	}
5045  
5046  	oparms = CIFS_OPARMS(cifs_sb, tcon, full_path, GENERIC_WRITE,
5047  			     FILE_CREATE, CREATE_NOT_DIR |
5048  			     CREATE_OPTION_SPECIAL, ACL_NO_MODE);
5049  	oparms.fid = &fid;
5050  
5051  	rc = server->ops->open(xid, &oparms, &oplock, NULL);
5052  	if (rc)
5053  		return rc;
5054  
5055  	io_parms.pid = current->tgid;
5056  	io_parms.tcon = tcon;
5057  	io_parms.length = sizeof(pdev);
5058  	iov[1].iov_base = &pdev;
5059  	iov[1].iov_len = sizeof(pdev);
5060  
5061  	rc = server->ops->sync_write(xid, &fid, &io_parms,
5062  				     &bytes_written, iov, 1);
5063  	server->ops->close(xid, tcon, &fid);
5064  	return rc;
5065  }
5066  
5067  int cifs_sfu_make_node(unsigned int xid, struct inode *inode,
5068  		       struct dentry *dentry, struct cifs_tcon *tcon,
5069  		       const char *full_path, umode_t mode, dev_t dev)
5070  {
5071  	struct inode *new = NULL;
5072  	int rc;
5073  
5074  	rc = __cifs_sfu_make_node(xid, inode, dentry, tcon,
5075  				  full_path, mode, dev);
5076  	if (rc)
5077  		return rc;
5078  
5079  	if (tcon->posix_extensions) {
5080  		rc = smb311_posix_get_inode_info(&new, full_path, NULL,
5081  						 inode->i_sb, xid);
5082  	} else if (tcon->unix_ext) {
5083  		rc = cifs_get_inode_info_unix(&new, full_path,
5084  					      inode->i_sb, xid);
5085  	} else {
5086  		rc = cifs_get_inode_info(&new, full_path, NULL,
5087  					 inode->i_sb, xid, NULL);
5088  	}
5089  	if (!rc)
5090  		d_instantiate(dentry, new);
5091  	return rc;
5092  }
5093  
5094  static int smb2_make_node(unsigned int xid, struct inode *inode,
5095  			  struct dentry *dentry, struct cifs_tcon *tcon,
5096  			  const char *full_path, umode_t mode, dev_t dev)
5097  {
5098  	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
5099  	int rc;
5100  
5101  	/*
5102  	 * Check if mounted with mount parm 'sfu' mount parm.
5103  	 * SFU emulation should work with all servers, but only
5104  	 * supports block and char device (no socket & fifo),
5105  	 * and was used by default in earlier versions of Windows
5106  	 */
5107  	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
5108  		rc = cifs_sfu_make_node(xid, inode, dentry, tcon,
5109  					full_path, mode, dev);
5110  	} else {
5111  		rc = smb2_mknod_reparse(xid, inode, dentry, tcon,
5112  					full_path, mode, dev);
5113  	}
5114  	return rc;
5115  }
5116  
5117  #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
5118  struct smb_version_operations smb20_operations = {
5119  	.compare_fids = smb2_compare_fids,
5120  	.setup_request = smb2_setup_request,
5121  	.setup_async_request = smb2_setup_async_request,
5122  	.check_receive = smb2_check_receive,
5123  	.add_credits = smb2_add_credits,
5124  	.set_credits = smb2_set_credits,
5125  	.get_credits_field = smb2_get_credits_field,
5126  	.get_credits = smb2_get_credits,
5127  	.wait_mtu_credits = cifs_wait_mtu_credits,
5128  	.get_next_mid = smb2_get_next_mid,
5129  	.revert_current_mid = smb2_revert_current_mid,
5130  	.read_data_offset = smb2_read_data_offset,
5131  	.read_data_length = smb2_read_data_length,
5132  	.map_error = map_smb2_to_linux_error,
5133  	.find_mid = smb2_find_mid,
5134  	.check_message = smb2_check_message,
5135  	.dump_detail = smb2_dump_detail,
5136  	.clear_stats = smb2_clear_stats,
5137  	.print_stats = smb2_print_stats,
5138  	.is_oplock_break = smb2_is_valid_oplock_break,
5139  	.handle_cancelled_mid = smb2_handle_cancelled_mid,
5140  	.downgrade_oplock = smb2_downgrade_oplock,
5141  	.need_neg = smb2_need_neg,
5142  	.negotiate = smb2_negotiate,
5143  	.negotiate_wsize = smb2_negotiate_wsize,
5144  	.negotiate_rsize = smb2_negotiate_rsize,
5145  	.sess_setup = SMB2_sess_setup,
5146  	.logoff = SMB2_logoff,
5147  	.tree_connect = SMB2_tcon,
5148  	.tree_disconnect = SMB2_tdis,
5149  	.qfs_tcon = smb2_qfs_tcon,
5150  	.is_path_accessible = smb2_is_path_accessible,
5151  	.can_echo = smb2_can_echo,
5152  	.echo = SMB2_echo,
5153  	.query_path_info = smb2_query_path_info,
5154  	.query_reparse_point = smb2_query_reparse_point,
5155  	.get_srv_inum = smb2_get_srv_inum,
5156  	.query_file_info = smb2_query_file_info,
5157  	.set_path_size = smb2_set_path_size,
5158  	.set_file_size = smb2_set_file_size,
5159  	.set_file_info = smb2_set_file_info,
5160  	.set_compression = smb2_set_compression,
5161  	.mkdir = smb2_mkdir,
5162  	.mkdir_setinfo = smb2_mkdir_setinfo,
5163  	.rmdir = smb2_rmdir,
5164  	.unlink = smb2_unlink,
5165  	.rename = smb2_rename_path,
5166  	.create_hardlink = smb2_create_hardlink,
5167  	.parse_reparse_point = smb2_parse_reparse_point,
5168  	.query_mf_symlink = smb3_query_mf_symlink,
5169  	.create_mf_symlink = smb3_create_mf_symlink,
5170  	.create_reparse_symlink = smb2_create_reparse_symlink,
5171  	.open = smb2_open_file,
5172  	.set_fid = smb2_set_fid,
5173  	.close = smb2_close_file,
5174  	.flush = smb2_flush_file,
5175  	.async_readv = smb2_async_readv,
5176  	.async_writev = smb2_async_writev,
5177  	.sync_read = smb2_sync_read,
5178  	.sync_write = smb2_sync_write,
5179  	.query_dir_first = smb2_query_dir_first,
5180  	.query_dir_next = smb2_query_dir_next,
5181  	.close_dir = smb2_close_dir,
5182  	.calc_smb_size = smb2_calc_size,
5183  	.is_status_pending = smb2_is_status_pending,
5184  	.is_session_expired = smb2_is_session_expired,
5185  	.oplock_response = smb2_oplock_response,
5186  	.queryfs = smb2_queryfs,
5187  	.mand_lock = smb2_mand_lock,
5188  	.mand_unlock_range = smb2_unlock_range,
5189  	.push_mand_locks = smb2_push_mandatory_locks,
5190  	.get_lease_key = smb2_get_lease_key,
5191  	.set_lease_key = smb2_set_lease_key,
5192  	.new_lease_key = smb2_new_lease_key,
5193  	.calc_signature = smb2_calc_signature,
5194  	.is_read_op = smb2_is_read_op,
5195  	.set_oplock_level = smb2_set_oplock_level,
5196  	.create_lease_buf = smb2_create_lease_buf,
5197  	.parse_lease_buf = smb2_parse_lease_buf,
5198  	.copychunk_range = smb2_copychunk_range,
5199  	.wp_retry_size = smb2_wp_retry_size,
5200  	.dir_needs_close = smb2_dir_needs_close,
5201  	.get_dfs_refer = smb2_get_dfs_refer,
5202  	.select_sectype = smb2_select_sectype,
5203  #ifdef CONFIG_CIFS_XATTR
5204  	.query_all_EAs = smb2_query_eas,
5205  	.set_EA = smb2_set_ea,
5206  #endif /* CIFS_XATTR */
5207  	.get_acl = get_smb2_acl,
5208  	.get_acl_by_fid = get_smb2_acl_by_fid,
5209  	.set_acl = set_smb2_acl,
5210  	.next_header = smb2_next_header,
5211  	.ioctl_query_info = smb2_ioctl_query_info,
5212  	.make_node = smb2_make_node,
5213  	.fiemap = smb3_fiemap,
5214  	.llseek = smb3_llseek,
5215  	.is_status_io_timeout = smb2_is_status_io_timeout,
5216  	.is_network_name_deleted = smb2_is_network_name_deleted,
5217  };
5218  #endif /* CIFS_ALLOW_INSECURE_LEGACY */
5219  
5220  struct smb_version_operations smb21_operations = {
5221  	.compare_fids = smb2_compare_fids,
5222  	.setup_request = smb2_setup_request,
5223  	.setup_async_request = smb2_setup_async_request,
5224  	.check_receive = smb2_check_receive,
5225  	.add_credits = smb2_add_credits,
5226  	.set_credits = smb2_set_credits,
5227  	.get_credits_field = smb2_get_credits_field,
5228  	.get_credits = smb2_get_credits,
5229  	.wait_mtu_credits = smb2_wait_mtu_credits,
5230  	.adjust_credits = smb2_adjust_credits,
5231  	.get_next_mid = smb2_get_next_mid,
5232  	.revert_current_mid = smb2_revert_current_mid,
5233  	.read_data_offset = smb2_read_data_offset,
5234  	.read_data_length = smb2_read_data_length,
5235  	.map_error = map_smb2_to_linux_error,
5236  	.find_mid = smb2_find_mid,
5237  	.check_message = smb2_check_message,
5238  	.dump_detail = smb2_dump_detail,
5239  	.clear_stats = smb2_clear_stats,
5240  	.print_stats = smb2_print_stats,
5241  	.is_oplock_break = smb2_is_valid_oplock_break,
5242  	.handle_cancelled_mid = smb2_handle_cancelled_mid,
5243  	.downgrade_oplock = smb2_downgrade_oplock,
5244  	.need_neg = smb2_need_neg,
5245  	.negotiate = smb2_negotiate,
5246  	.negotiate_wsize = smb2_negotiate_wsize,
5247  	.negotiate_rsize = smb2_negotiate_rsize,
5248  	.sess_setup = SMB2_sess_setup,
5249  	.logoff = SMB2_logoff,
5250  	.tree_connect = SMB2_tcon,
5251  	.tree_disconnect = SMB2_tdis,
5252  	.qfs_tcon = smb2_qfs_tcon,
5253  	.is_path_accessible = smb2_is_path_accessible,
5254  	.can_echo = smb2_can_echo,
5255  	.echo = SMB2_echo,
5256  	.query_path_info = smb2_query_path_info,
5257  	.query_reparse_point = smb2_query_reparse_point,
5258  	.get_srv_inum = smb2_get_srv_inum,
5259  	.query_file_info = smb2_query_file_info,
5260  	.set_path_size = smb2_set_path_size,
5261  	.set_file_size = smb2_set_file_size,
5262  	.set_file_info = smb2_set_file_info,
5263  	.set_compression = smb2_set_compression,
5264  	.mkdir = smb2_mkdir,
5265  	.mkdir_setinfo = smb2_mkdir_setinfo,
5266  	.rmdir = smb2_rmdir,
5267  	.unlink = smb2_unlink,
5268  	.rename = smb2_rename_path,
5269  	.create_hardlink = smb2_create_hardlink,
5270  	.parse_reparse_point = smb2_parse_reparse_point,
5271  	.query_mf_symlink = smb3_query_mf_symlink,
5272  	.create_mf_symlink = smb3_create_mf_symlink,
5273  	.create_reparse_symlink = smb2_create_reparse_symlink,
5274  	.open = smb2_open_file,
5275  	.set_fid = smb2_set_fid,
5276  	.close = smb2_close_file,
5277  	.flush = smb2_flush_file,
5278  	.async_readv = smb2_async_readv,
5279  	.async_writev = smb2_async_writev,
5280  	.sync_read = smb2_sync_read,
5281  	.sync_write = smb2_sync_write,
5282  	.query_dir_first = smb2_query_dir_first,
5283  	.query_dir_next = smb2_query_dir_next,
5284  	.close_dir = smb2_close_dir,
5285  	.calc_smb_size = smb2_calc_size,
5286  	.is_status_pending = smb2_is_status_pending,
5287  	.is_session_expired = smb2_is_session_expired,
5288  	.oplock_response = smb2_oplock_response,
5289  	.queryfs = smb2_queryfs,
5290  	.mand_lock = smb2_mand_lock,
5291  	.mand_unlock_range = smb2_unlock_range,
5292  	.push_mand_locks = smb2_push_mandatory_locks,
5293  	.get_lease_key = smb2_get_lease_key,
5294  	.set_lease_key = smb2_set_lease_key,
5295  	.new_lease_key = smb2_new_lease_key,
5296  	.calc_signature = smb2_calc_signature,
5297  	.is_read_op = smb21_is_read_op,
5298  	.set_oplock_level = smb21_set_oplock_level,
5299  	.create_lease_buf = smb2_create_lease_buf,
5300  	.parse_lease_buf = smb2_parse_lease_buf,
5301  	.copychunk_range = smb2_copychunk_range,
5302  	.wp_retry_size = smb2_wp_retry_size,
5303  	.dir_needs_close = smb2_dir_needs_close,
5304  	.enum_snapshots = smb3_enum_snapshots,
5305  	.notify = smb3_notify,
5306  	.get_dfs_refer = smb2_get_dfs_refer,
5307  	.select_sectype = smb2_select_sectype,
5308  #ifdef CONFIG_CIFS_XATTR
5309  	.query_all_EAs = smb2_query_eas,
5310  	.set_EA = smb2_set_ea,
5311  #endif /* CIFS_XATTR */
5312  	.get_acl = get_smb2_acl,
5313  	.get_acl_by_fid = get_smb2_acl_by_fid,
5314  	.set_acl = set_smb2_acl,
5315  	.next_header = smb2_next_header,
5316  	.ioctl_query_info = smb2_ioctl_query_info,
5317  	.make_node = smb2_make_node,
5318  	.fiemap = smb3_fiemap,
5319  	.llseek = smb3_llseek,
5320  	.is_status_io_timeout = smb2_is_status_io_timeout,
5321  	.is_network_name_deleted = smb2_is_network_name_deleted,
5322  };
5323  
5324  struct smb_version_operations smb30_operations = {
5325  	.compare_fids = smb2_compare_fids,
5326  	.setup_request = smb2_setup_request,
5327  	.setup_async_request = smb2_setup_async_request,
5328  	.check_receive = smb2_check_receive,
5329  	.add_credits = smb2_add_credits,
5330  	.set_credits = smb2_set_credits,
5331  	.get_credits_field = smb2_get_credits_field,
5332  	.get_credits = smb2_get_credits,
5333  	.wait_mtu_credits = smb2_wait_mtu_credits,
5334  	.adjust_credits = smb2_adjust_credits,
5335  	.get_next_mid = smb2_get_next_mid,
5336  	.revert_current_mid = smb2_revert_current_mid,
5337  	.read_data_offset = smb2_read_data_offset,
5338  	.read_data_length = smb2_read_data_length,
5339  	.map_error = map_smb2_to_linux_error,
5340  	.find_mid = smb2_find_mid,
5341  	.check_message = smb2_check_message,
5342  	.dump_detail = smb2_dump_detail,
5343  	.clear_stats = smb2_clear_stats,
5344  	.print_stats = smb2_print_stats,
5345  	.dump_share_caps = smb2_dump_share_caps,
5346  	.is_oplock_break = smb2_is_valid_oplock_break,
5347  	.handle_cancelled_mid = smb2_handle_cancelled_mid,
5348  	.downgrade_oplock = smb3_downgrade_oplock,
5349  	.need_neg = smb2_need_neg,
5350  	.negotiate = smb2_negotiate,
5351  	.negotiate_wsize = smb3_negotiate_wsize,
5352  	.negotiate_rsize = smb3_negotiate_rsize,
5353  	.sess_setup = SMB2_sess_setup,
5354  	.logoff = SMB2_logoff,
5355  	.tree_connect = SMB2_tcon,
5356  	.tree_disconnect = SMB2_tdis,
5357  	.qfs_tcon = smb3_qfs_tcon,
5358  	.query_server_interfaces = SMB3_request_interfaces,
5359  	.is_path_accessible = smb2_is_path_accessible,
5360  	.can_echo = smb2_can_echo,
5361  	.echo = SMB2_echo,
5362  	.query_path_info = smb2_query_path_info,
5363  	/* WSL tags introduced long after smb2.1, enable for SMB3, 3.11 only */
5364  	.query_reparse_point = smb2_query_reparse_point,
5365  	.get_srv_inum = smb2_get_srv_inum,
5366  	.query_file_info = smb2_query_file_info,
5367  	.set_path_size = smb2_set_path_size,
5368  	.set_file_size = smb2_set_file_size,
5369  	.set_file_info = smb2_set_file_info,
5370  	.set_compression = smb2_set_compression,
5371  	.mkdir = smb2_mkdir,
5372  	.mkdir_setinfo = smb2_mkdir_setinfo,
5373  	.rmdir = smb2_rmdir,
5374  	.unlink = smb2_unlink,
5375  	.rename = smb2_rename_path,
5376  	.create_hardlink = smb2_create_hardlink,
5377  	.parse_reparse_point = smb2_parse_reparse_point,
5378  	.query_mf_symlink = smb3_query_mf_symlink,
5379  	.create_mf_symlink = smb3_create_mf_symlink,
5380  	.create_reparse_symlink = smb2_create_reparse_symlink,
5381  	.open = smb2_open_file,
5382  	.set_fid = smb2_set_fid,
5383  	.close = smb2_close_file,
5384  	.close_getattr = smb2_close_getattr,
5385  	.flush = smb2_flush_file,
5386  	.async_readv = smb2_async_readv,
5387  	.async_writev = smb2_async_writev,
5388  	.sync_read = smb2_sync_read,
5389  	.sync_write = smb2_sync_write,
5390  	.query_dir_first = smb2_query_dir_first,
5391  	.query_dir_next = smb2_query_dir_next,
5392  	.close_dir = smb2_close_dir,
5393  	.calc_smb_size = smb2_calc_size,
5394  	.is_status_pending = smb2_is_status_pending,
5395  	.is_session_expired = smb2_is_session_expired,
5396  	.oplock_response = smb2_oplock_response,
5397  	.queryfs = smb2_queryfs,
5398  	.mand_lock = smb2_mand_lock,
5399  	.mand_unlock_range = smb2_unlock_range,
5400  	.push_mand_locks = smb2_push_mandatory_locks,
5401  	.get_lease_key = smb2_get_lease_key,
5402  	.set_lease_key = smb2_set_lease_key,
5403  	.new_lease_key = smb2_new_lease_key,
5404  	.generate_signingkey = generate_smb30signingkey,
5405  	.calc_signature = smb3_calc_signature,
5406  	.set_integrity  = smb3_set_integrity,
5407  	.is_read_op = smb21_is_read_op,
5408  	.set_oplock_level = smb3_set_oplock_level,
5409  	.create_lease_buf = smb3_create_lease_buf,
5410  	.parse_lease_buf = smb3_parse_lease_buf,
5411  	.copychunk_range = smb2_copychunk_range,
5412  	.duplicate_extents = smb2_duplicate_extents,
5413  	.validate_negotiate = smb3_validate_negotiate,
5414  	.wp_retry_size = smb2_wp_retry_size,
5415  	.dir_needs_close = smb2_dir_needs_close,
5416  	.fallocate = smb3_fallocate,
5417  	.enum_snapshots = smb3_enum_snapshots,
5418  	.notify = smb3_notify,
5419  	.init_transform_rq = smb3_init_transform_rq,
5420  	.is_transform_hdr = smb3_is_transform_hdr,
5421  	.receive_transform = smb3_receive_transform,
5422  	.get_dfs_refer = smb2_get_dfs_refer,
5423  	.select_sectype = smb2_select_sectype,
5424  #ifdef CONFIG_CIFS_XATTR
5425  	.query_all_EAs = smb2_query_eas,
5426  	.set_EA = smb2_set_ea,
5427  #endif /* CIFS_XATTR */
5428  	.get_acl = get_smb2_acl,
5429  	.get_acl_by_fid = get_smb2_acl_by_fid,
5430  	.set_acl = set_smb2_acl,
5431  	.next_header = smb2_next_header,
5432  	.ioctl_query_info = smb2_ioctl_query_info,
5433  	.make_node = smb2_make_node,
5434  	.fiemap = smb3_fiemap,
5435  	.llseek = smb3_llseek,
5436  	.is_status_io_timeout = smb2_is_status_io_timeout,
5437  	.is_network_name_deleted = smb2_is_network_name_deleted,
5438  };
5439  
5440  struct smb_version_operations smb311_operations = {
5441  	.compare_fids = smb2_compare_fids,
5442  	.setup_request = smb2_setup_request,
5443  	.setup_async_request = smb2_setup_async_request,
5444  	.check_receive = smb2_check_receive,
5445  	.add_credits = smb2_add_credits,
5446  	.set_credits = smb2_set_credits,
5447  	.get_credits_field = smb2_get_credits_field,
5448  	.get_credits = smb2_get_credits,
5449  	.wait_mtu_credits = smb2_wait_mtu_credits,
5450  	.adjust_credits = smb2_adjust_credits,
5451  	.get_next_mid = smb2_get_next_mid,
5452  	.revert_current_mid = smb2_revert_current_mid,
5453  	.read_data_offset = smb2_read_data_offset,
5454  	.read_data_length = smb2_read_data_length,
5455  	.map_error = map_smb2_to_linux_error,
5456  	.find_mid = smb2_find_mid,
5457  	.check_message = smb2_check_message,
5458  	.dump_detail = smb2_dump_detail,
5459  	.clear_stats = smb2_clear_stats,
5460  	.print_stats = smb2_print_stats,
5461  	.dump_share_caps = smb2_dump_share_caps,
5462  	.is_oplock_break = smb2_is_valid_oplock_break,
5463  	.handle_cancelled_mid = smb2_handle_cancelled_mid,
5464  	.downgrade_oplock = smb3_downgrade_oplock,
5465  	.need_neg = smb2_need_neg,
5466  	.negotiate = smb2_negotiate,
5467  	.negotiate_wsize = smb3_negotiate_wsize,
5468  	.negotiate_rsize = smb3_negotiate_rsize,
5469  	.sess_setup = SMB2_sess_setup,
5470  	.logoff = SMB2_logoff,
5471  	.tree_connect = SMB2_tcon,
5472  	.tree_disconnect = SMB2_tdis,
5473  	.qfs_tcon = smb3_qfs_tcon,
5474  	.query_server_interfaces = SMB3_request_interfaces,
5475  	.is_path_accessible = smb2_is_path_accessible,
5476  	.can_echo = smb2_can_echo,
5477  	.echo = SMB2_echo,
5478  	.query_path_info = smb2_query_path_info,
5479  	.query_reparse_point = smb2_query_reparse_point,
5480  	.get_srv_inum = smb2_get_srv_inum,
5481  	.query_file_info = smb2_query_file_info,
5482  	.set_path_size = smb2_set_path_size,
5483  	.set_file_size = smb2_set_file_size,
5484  	.set_file_info = smb2_set_file_info,
5485  	.set_compression = smb2_set_compression,
5486  	.mkdir = smb2_mkdir,
5487  	.mkdir_setinfo = smb2_mkdir_setinfo,
5488  	.posix_mkdir = smb311_posix_mkdir,
5489  	.rmdir = smb2_rmdir,
5490  	.unlink = smb2_unlink,
5491  	.rename = smb2_rename_path,
5492  	.create_hardlink = smb2_create_hardlink,
5493  	.parse_reparse_point = smb2_parse_reparse_point,
5494  	.query_mf_symlink = smb3_query_mf_symlink,
5495  	.create_mf_symlink = smb3_create_mf_symlink,
5496  	.create_reparse_symlink = smb2_create_reparse_symlink,
5497  	.open = smb2_open_file,
5498  	.set_fid = smb2_set_fid,
5499  	.close = smb2_close_file,
5500  	.close_getattr = smb2_close_getattr,
5501  	.flush = smb2_flush_file,
5502  	.async_readv = smb2_async_readv,
5503  	.async_writev = smb2_async_writev,
5504  	.sync_read = smb2_sync_read,
5505  	.sync_write = smb2_sync_write,
5506  	.query_dir_first = smb2_query_dir_first,
5507  	.query_dir_next = smb2_query_dir_next,
5508  	.close_dir = smb2_close_dir,
5509  	.calc_smb_size = smb2_calc_size,
5510  	.is_status_pending = smb2_is_status_pending,
5511  	.is_session_expired = smb2_is_session_expired,
5512  	.oplock_response = smb2_oplock_response,
5513  	.queryfs = smb311_queryfs,
5514  	.mand_lock = smb2_mand_lock,
5515  	.mand_unlock_range = smb2_unlock_range,
5516  	.push_mand_locks = smb2_push_mandatory_locks,
5517  	.get_lease_key = smb2_get_lease_key,
5518  	.set_lease_key = smb2_set_lease_key,
5519  	.new_lease_key = smb2_new_lease_key,
5520  	.generate_signingkey = generate_smb311signingkey,
5521  	.calc_signature = smb3_calc_signature,
5522  	.set_integrity  = smb3_set_integrity,
5523  	.is_read_op = smb21_is_read_op,
5524  	.set_oplock_level = smb3_set_oplock_level,
5525  	.create_lease_buf = smb3_create_lease_buf,
5526  	.parse_lease_buf = smb3_parse_lease_buf,
5527  	.copychunk_range = smb2_copychunk_range,
5528  	.duplicate_extents = smb2_duplicate_extents,
5529  /*	.validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
5530  	.wp_retry_size = smb2_wp_retry_size,
5531  	.dir_needs_close = smb2_dir_needs_close,
5532  	.fallocate = smb3_fallocate,
5533  	.enum_snapshots = smb3_enum_snapshots,
5534  	.notify = smb3_notify,
5535  	.init_transform_rq = smb3_init_transform_rq,
5536  	.is_transform_hdr = smb3_is_transform_hdr,
5537  	.receive_transform = smb3_receive_transform,
5538  	.get_dfs_refer = smb2_get_dfs_refer,
5539  	.select_sectype = smb2_select_sectype,
5540  #ifdef CONFIG_CIFS_XATTR
5541  	.query_all_EAs = smb2_query_eas,
5542  	.set_EA = smb2_set_ea,
5543  #endif /* CIFS_XATTR */
5544  	.get_acl = get_smb2_acl,
5545  	.get_acl_by_fid = get_smb2_acl_by_fid,
5546  	.set_acl = set_smb2_acl,
5547  	.next_header = smb2_next_header,
5548  	.ioctl_query_info = smb2_ioctl_query_info,
5549  	.make_node = smb2_make_node,
5550  	.fiemap = smb3_fiemap,
5551  	.llseek = smb3_llseek,
5552  	.is_status_io_timeout = smb2_is_status_io_timeout,
5553  	.is_network_name_deleted = smb2_is_network_name_deleted,
5554  };
5555  
5556  #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
5557  struct smb_version_values smb20_values = {
5558  	.version_string = SMB20_VERSION_STRING,
5559  	.protocol_id = SMB20_PROT_ID,
5560  	.req_capabilities = 0, /* MBZ */
5561  	.large_lock_type = 0,
5562  	.exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
5563  	.shared_lock_type = SMB2_LOCKFLAG_SHARED,
5564  	.unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5565  	.header_size = sizeof(struct smb2_hdr),
5566  	.header_preamble_size = 0,
5567  	.max_header_size = MAX_SMB2_HDR_SIZE,
5568  	.read_rsp_size = sizeof(struct smb2_read_rsp),
5569  	.lock_cmd = SMB2_LOCK,
5570  	.cap_unix = 0,
5571  	.cap_nt_find = SMB2_NT_FIND,
5572  	.cap_large_files = SMB2_LARGE_FILES,
5573  	.signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5574  	.signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5575  	.create_lease_size = sizeof(struct create_lease),
5576  };
5577  #endif /* ALLOW_INSECURE_LEGACY */
5578  
5579  struct smb_version_values smb21_values = {
5580  	.version_string = SMB21_VERSION_STRING,
5581  	.protocol_id = SMB21_PROT_ID,
5582  	.req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
5583  	.large_lock_type = 0,
5584  	.exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
5585  	.shared_lock_type = SMB2_LOCKFLAG_SHARED,
5586  	.unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5587  	.header_size = sizeof(struct smb2_hdr),
5588  	.header_preamble_size = 0,
5589  	.max_header_size = MAX_SMB2_HDR_SIZE,
5590  	.read_rsp_size = sizeof(struct smb2_read_rsp),
5591  	.lock_cmd = SMB2_LOCK,
5592  	.cap_unix = 0,
5593  	.cap_nt_find = SMB2_NT_FIND,
5594  	.cap_large_files = SMB2_LARGE_FILES,
5595  	.signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5596  	.signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5597  	.create_lease_size = sizeof(struct create_lease),
5598  };
5599  
5600  struct smb_version_values smb3any_values = {
5601  	.version_string = SMB3ANY_VERSION_STRING,
5602  	.protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5603  	.req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
5604  	.large_lock_type = 0,
5605  	.exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
5606  	.shared_lock_type = SMB2_LOCKFLAG_SHARED,
5607  	.unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5608  	.header_size = sizeof(struct smb2_hdr),
5609  	.header_preamble_size = 0,
5610  	.max_header_size = MAX_SMB2_HDR_SIZE,
5611  	.read_rsp_size = sizeof(struct smb2_read_rsp),
5612  	.lock_cmd = SMB2_LOCK,
5613  	.cap_unix = 0,
5614  	.cap_nt_find = SMB2_NT_FIND,
5615  	.cap_large_files = SMB2_LARGE_FILES,
5616  	.signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5617  	.signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5618  	.create_lease_size = sizeof(struct create_lease_v2),
5619  };
5620  
5621  struct smb_version_values smbdefault_values = {
5622  	.version_string = SMBDEFAULT_VERSION_STRING,
5623  	.protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5624  	.req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
5625  	.large_lock_type = 0,
5626  	.exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
5627  	.shared_lock_type = SMB2_LOCKFLAG_SHARED,
5628  	.unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5629  	.header_size = sizeof(struct smb2_hdr),
5630  	.header_preamble_size = 0,
5631  	.max_header_size = MAX_SMB2_HDR_SIZE,
5632  	.read_rsp_size = sizeof(struct smb2_read_rsp),
5633  	.lock_cmd = SMB2_LOCK,
5634  	.cap_unix = 0,
5635  	.cap_nt_find = SMB2_NT_FIND,
5636  	.cap_large_files = SMB2_LARGE_FILES,
5637  	.signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5638  	.signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5639  	.create_lease_size = sizeof(struct create_lease_v2),
5640  };
5641  
5642  struct smb_version_values smb30_values = {
5643  	.version_string = SMB30_VERSION_STRING,
5644  	.protocol_id = SMB30_PROT_ID,
5645  	.req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
5646  	.large_lock_type = 0,
5647  	.exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
5648  	.shared_lock_type = SMB2_LOCKFLAG_SHARED,
5649  	.unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5650  	.header_size = sizeof(struct smb2_hdr),
5651  	.header_preamble_size = 0,
5652  	.max_header_size = MAX_SMB2_HDR_SIZE,
5653  	.read_rsp_size = sizeof(struct smb2_read_rsp),
5654  	.lock_cmd = SMB2_LOCK,
5655  	.cap_unix = 0,
5656  	.cap_nt_find = SMB2_NT_FIND,
5657  	.cap_large_files = SMB2_LARGE_FILES,
5658  	.signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5659  	.signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5660  	.create_lease_size = sizeof(struct create_lease_v2),
5661  };
5662  
5663  struct smb_version_values smb302_values = {
5664  	.version_string = SMB302_VERSION_STRING,
5665  	.protocol_id = SMB302_PROT_ID,
5666  	.req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
5667  	.large_lock_type = 0,
5668  	.exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
5669  	.shared_lock_type = SMB2_LOCKFLAG_SHARED,
5670  	.unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5671  	.header_size = sizeof(struct smb2_hdr),
5672  	.header_preamble_size = 0,
5673  	.max_header_size = MAX_SMB2_HDR_SIZE,
5674  	.read_rsp_size = sizeof(struct smb2_read_rsp),
5675  	.lock_cmd = SMB2_LOCK,
5676  	.cap_unix = 0,
5677  	.cap_nt_find = SMB2_NT_FIND,
5678  	.cap_large_files = SMB2_LARGE_FILES,
5679  	.signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5680  	.signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5681  	.create_lease_size = sizeof(struct create_lease_v2),
5682  };
5683  
5684  struct smb_version_values smb311_values = {
5685  	.version_string = SMB311_VERSION_STRING,
5686  	.protocol_id = SMB311_PROT_ID,
5687  	.req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
5688  	.large_lock_type = 0,
5689  	.exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
5690  	.shared_lock_type = SMB2_LOCKFLAG_SHARED,
5691  	.unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5692  	.header_size = sizeof(struct smb2_hdr),
5693  	.header_preamble_size = 0,
5694  	.max_header_size = MAX_SMB2_HDR_SIZE,
5695  	.read_rsp_size = sizeof(struct smb2_read_rsp),
5696  	.lock_cmd = SMB2_LOCK,
5697  	.cap_unix = 0,
5698  	.cap_nt_find = SMB2_NT_FIND,
5699  	.cap_large_files = SMB2_LARGE_FILES,
5700  	.signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5701  	.signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5702  	.create_lease_size = sizeof(struct create_lease_v2),
5703  };
5704