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