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