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