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