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