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