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