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