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