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