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