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