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