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