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