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