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