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