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