1 /*
2  *  SMB2 version specific operations
3  *
4  *  Copyright (c) 2012, Jeff Layton <jlayton@redhat.com>
5  *
6  *  This library is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License v2 as published
8  *  by the Free Software Foundation.
9  *
10  *  This library is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  *  the GNU Lesser General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Lesser General Public License
16  *  along with this library; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19 
20 #include <linux/pagemap.h>
21 #include <linux/vfs.h>
22 #include <linux/falloc.h>
23 #include <linux/scatterlist.h>
24 #include <linux/uuid.h>
25 #include <crypto/aead.h>
26 #include "cifsglob.h"
27 #include "smb2pdu.h"
28 #include "smb2proto.h"
29 #include "cifsproto.h"
30 #include "cifs_debug.h"
31 #include "cifs_unicode.h"
32 #include "smb2status.h"
33 #include "smb2glob.h"
34 #include "cifs_ioctl.h"
35 #include "smbdirect.h"
36 
37 /* Change credits for different ops and return the total number of credits */
38 static int
change_conf(struct TCP_Server_Info * server)39 change_conf(struct TCP_Server_Info *server)
40 {
41 	server->credits += server->echo_credits + server->oplock_credits;
42 	server->oplock_credits = server->echo_credits = 0;
43 	switch (server->credits) {
44 	case 0:
45 		return 0;
46 	case 1:
47 		server->echoes = false;
48 		server->oplocks = false;
49 		break;
50 	case 2:
51 		server->echoes = true;
52 		server->oplocks = false;
53 		server->echo_credits = 1;
54 		break;
55 	default:
56 		server->echoes = true;
57 		if (enable_oplocks) {
58 			server->oplocks = true;
59 			server->oplock_credits = 1;
60 		} else
61 			server->oplocks = false;
62 
63 		server->echo_credits = 1;
64 	}
65 	server->credits -= server->echo_credits + server->oplock_credits;
66 	return server->credits + server->echo_credits + server->oplock_credits;
67 }
68 
69 static void
smb2_add_credits(struct TCP_Server_Info * server,const unsigned int add,const int optype)70 smb2_add_credits(struct TCP_Server_Info *server, const unsigned int add,
71 		 const int optype)
72 {
73 	int *val, rc = -1;
74 
75 	spin_lock(&server->req_lock);
76 	val = server->ops->get_credits_field(server, optype);
77 	*val += add;
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 	WARN_ON_ONCE(server->in_flight == 0);
83 	server->in_flight--;
84 	if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
85 		rc = change_conf(server);
86 	/*
87 	 * Sometimes server returns 0 credits on oplock break ack - we need to
88 	 * rebalance credits in this case.
89 	 */
90 	else if (server->in_flight > 0 && server->oplock_credits == 0 &&
91 		 server->oplocks) {
92 		if (server->credits > 1) {
93 			server->credits--;
94 			server->oplock_credits++;
95 		}
96 	}
97 	spin_unlock(&server->req_lock);
98 	wake_up(&server->request_q);
99 
100 	if (server->tcpStatus == CifsNeedReconnect)
101 		return;
102 
103 	switch (rc) {
104 	case -1:
105 		/* change_conf hasn't been executed */
106 		break;
107 	case 0:
108 		cifs_dbg(VFS, "Possible client or server bug - zero credits\n");
109 		break;
110 	case 1:
111 		cifs_dbg(VFS, "disabling echoes and oplocks\n");
112 		break;
113 	case 2:
114 		cifs_dbg(FYI, "disabling oplocks\n");
115 		break;
116 	default:
117 		cifs_dbg(FYI, "add %u credits total=%d\n", add, rc);
118 	}
119 }
120 
121 static void
smb2_set_credits(struct TCP_Server_Info * server,const int val)122 smb2_set_credits(struct TCP_Server_Info *server, const int val)
123 {
124 	spin_lock(&server->req_lock);
125 	server->credits = val;
126 	spin_unlock(&server->req_lock);
127 }
128 
129 static int *
smb2_get_credits_field(struct TCP_Server_Info * server,const int optype)130 smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
131 {
132 	switch (optype) {
133 	case CIFS_ECHO_OP:
134 		return &server->echo_credits;
135 	case CIFS_OBREAK_OP:
136 		return &server->oplock_credits;
137 	default:
138 		return &server->credits;
139 	}
140 }
141 
142 static unsigned int
smb2_get_credits(struct mid_q_entry * mid)143 smb2_get_credits(struct mid_q_entry *mid)
144 {
145 	struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)mid->resp_buf;
146 
147 	return le16_to_cpu(shdr->CreditRequest);
148 }
149 
150 static int
smb2_wait_mtu_credits(struct TCP_Server_Info * server,unsigned int size,unsigned int * num,unsigned int * credits)151 smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
152 		      unsigned int *num, unsigned int *credits)
153 {
154 	int rc = 0;
155 	unsigned int scredits;
156 
157 	spin_lock(&server->req_lock);
158 	while (1) {
159 		if (server->credits <= 0) {
160 			spin_unlock(&server->req_lock);
161 			cifs_num_waiters_inc(server);
162 			rc = wait_event_killable(server->request_q,
163 					has_credits(server, &server->credits));
164 			cifs_num_waiters_dec(server);
165 			if (rc)
166 				return rc;
167 			spin_lock(&server->req_lock);
168 		} else {
169 			if (server->tcpStatus == CifsExiting) {
170 				spin_unlock(&server->req_lock);
171 				return -ENOENT;
172 			}
173 
174 			scredits = server->credits;
175 			/* can deadlock with reopen */
176 			if (scredits <= 8) {
177 				*num = SMB2_MAX_BUFFER_SIZE;
178 				*credits = 0;
179 				break;
180 			}
181 
182 			/* leave some credits for reopen and other ops */
183 			scredits -= 8;
184 			*num = min_t(unsigned int, size,
185 				     scredits * SMB2_MAX_BUFFER_SIZE);
186 
187 			*credits = DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
188 			server->credits -= *credits;
189 			server->in_flight++;
190 			break;
191 		}
192 	}
193 	spin_unlock(&server->req_lock);
194 	return rc;
195 }
196 
197 static __u64
smb2_get_next_mid(struct TCP_Server_Info * server)198 smb2_get_next_mid(struct TCP_Server_Info *server)
199 {
200 	__u64 mid;
201 	/* for SMB2 we need the current value */
202 	spin_lock(&GlobalMid_Lock);
203 	mid = server->CurrentMid++;
204 	spin_unlock(&GlobalMid_Lock);
205 	return mid;
206 }
207 
208 static void
smb2_revert_current_mid(struct TCP_Server_Info * server,const unsigned int val)209 smb2_revert_current_mid(struct TCP_Server_Info *server, const unsigned int val)
210 {
211 	spin_lock(&GlobalMid_Lock);
212 	if (server->CurrentMid >= val)
213 		server->CurrentMid -= val;
214 	spin_unlock(&GlobalMid_Lock);
215 }
216 
217 static struct mid_q_entry *
smb2_find_mid(struct TCP_Server_Info * server,char * buf)218 smb2_find_mid(struct TCP_Server_Info *server, char *buf)
219 {
220 	struct mid_q_entry *mid;
221 	struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
222 	__u64 wire_mid = le64_to_cpu(shdr->MessageId);
223 
224 	if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
225 		cifs_dbg(VFS, "encrypted frame parsing not supported yet");
226 		return NULL;
227 	}
228 
229 	spin_lock(&GlobalMid_Lock);
230 	list_for_each_entry(mid, &server->pending_mid_q, qhead) {
231 		if ((mid->mid == wire_mid) &&
232 		    (mid->mid_state == MID_REQUEST_SUBMITTED) &&
233 		    (mid->command == shdr->Command)) {
234 			kref_get(&mid->refcount);
235 			spin_unlock(&GlobalMid_Lock);
236 			return mid;
237 		}
238 	}
239 	spin_unlock(&GlobalMid_Lock);
240 	return NULL;
241 }
242 
243 static void
smb2_dump_detail(void * buf,struct TCP_Server_Info * server)244 smb2_dump_detail(void *buf, struct TCP_Server_Info *server)
245 {
246 #ifdef CONFIG_CIFS_DEBUG2
247 	struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
248 
249 	cifs_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
250 		 shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
251 		 shdr->ProcessId);
252 	cifs_dbg(VFS, "smb buf %p len %u\n", buf,
253 		 server->ops->calc_smb_size(buf, server));
254 #endif
255 }
256 
257 static bool
smb2_need_neg(struct TCP_Server_Info * server)258 smb2_need_neg(struct TCP_Server_Info *server)
259 {
260 	return server->max_read == 0;
261 }
262 
263 static int
smb2_negotiate(const unsigned int xid,struct cifs_ses * ses)264 smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
265 {
266 	int rc;
267 	ses->server->CurrentMid = 0;
268 	rc = SMB2_negotiate(xid, ses);
269 	/* BB we probably don't need to retry with modern servers */
270 	if (rc == -EAGAIN)
271 		rc = -EHOSTDOWN;
272 	return rc;
273 }
274 
275 static unsigned int
smb2_negotiate_wsize(struct cifs_tcon * tcon,struct smb_vol * volume_info)276 smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
277 {
278 	struct TCP_Server_Info *server = tcon->ses->server;
279 	unsigned int wsize;
280 
281 	/* start with specified wsize, or default */
282 	wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
283 	wsize = min_t(unsigned int, wsize, server->max_write);
284 #ifdef CONFIG_CIFS_SMB_DIRECT
285 	if (server->rdma) {
286 		if (server->sign)
287 			wsize = min_t(unsigned int,
288 				wsize, server->smbd_conn->max_fragmented_send_size);
289 		else
290 			wsize = min_t(unsigned int,
291 				wsize, server->smbd_conn->max_readwrite_size);
292 	}
293 #endif
294 	if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
295 		wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
296 
297 	return wsize;
298 }
299 
300 static unsigned int
smb2_negotiate_rsize(struct cifs_tcon * tcon,struct smb_vol * volume_info)301 smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
302 {
303 	struct TCP_Server_Info *server = tcon->ses->server;
304 	unsigned int rsize;
305 
306 	/* start with specified rsize, or default */
307 	rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
308 	rsize = min_t(unsigned int, rsize, server->max_read);
309 #ifdef CONFIG_CIFS_SMB_DIRECT
310 	if (server->rdma) {
311 		if (server->sign)
312 			rsize = min_t(unsigned int,
313 				rsize, server->smbd_conn->max_fragmented_recv_size);
314 		else
315 			rsize = min_t(unsigned int,
316 				rsize, server->smbd_conn->max_readwrite_size);
317 	}
318 #endif
319 
320 	if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
321 		rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
322 
323 	return rsize;
324 }
325 
326 
327 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)328 parse_server_interfaces(struct network_interface_info_ioctl_rsp *buf,
329 			size_t buf_len,
330 			struct cifs_server_iface **iface_list,
331 			size_t *iface_count)
332 {
333 	struct network_interface_info_ioctl_rsp *p;
334 	struct sockaddr_in *addr4;
335 	struct sockaddr_in6 *addr6;
336 	struct iface_info_ipv4 *p4;
337 	struct iface_info_ipv6 *p6;
338 	struct cifs_server_iface *info;
339 	ssize_t bytes_left;
340 	size_t next = 0;
341 	int nb_iface = 0;
342 	int rc = 0;
343 
344 	*iface_list = NULL;
345 	*iface_count = 0;
346 
347 	/*
348 	 * Fist pass: count and sanity check
349 	 */
350 
351 	bytes_left = buf_len;
352 	p = buf;
353 	while (bytes_left >= sizeof(*p)) {
354 		nb_iface++;
355 		next = le32_to_cpu(p->Next);
356 		if (!next) {
357 			bytes_left -= sizeof(*p);
358 			break;
359 		}
360 		p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
361 		bytes_left -= next;
362 	}
363 
364 	if (!nb_iface) {
365 		cifs_dbg(VFS, "%s: malformed interface info\n", __func__);
366 		rc = -EINVAL;
367 		goto out;
368 	}
369 
370 	/* Azure rounds the buffer size up 8, to a 16 byte boundary */
371 	if ((bytes_left > 8) || p->Next)
372 		cifs_dbg(VFS, "%s: incomplete interface info\n", __func__);
373 
374 
375 	/*
376 	 * Second pass: extract info to internal structure
377 	 */
378 
379 	*iface_list = kcalloc(nb_iface, sizeof(**iface_list), GFP_KERNEL);
380 	if (!*iface_list) {
381 		rc = -ENOMEM;
382 		goto out;
383 	}
384 
385 	info = *iface_list;
386 	bytes_left = buf_len;
387 	p = buf;
388 	while (bytes_left >= sizeof(*p)) {
389 		info->speed = le64_to_cpu(p->LinkSpeed);
390 		info->rdma_capable = le32_to_cpu(p->Capability & RDMA_CAPABLE) ? 1 : 0;
391 		info->rss_capable = le32_to_cpu(p->Capability & RSS_CAPABLE) ? 1 : 0;
392 
393 		cifs_dbg(FYI, "%s: adding iface %zu\n", __func__, *iface_count);
394 		cifs_dbg(FYI, "%s: speed %zu bps\n", __func__, info->speed);
395 		cifs_dbg(FYI, "%s: capabilities 0x%08x\n", __func__,
396 			 le32_to_cpu(p->Capability));
397 
398 		switch (p->Family) {
399 		/*
400 		 * The kernel and wire socket structures have the same
401 		 * layout and use network byte order but make the
402 		 * conversion explicit in case either one changes.
403 		 */
404 		case INTERNETWORK:
405 			addr4 = (struct sockaddr_in *)&info->sockaddr;
406 			p4 = (struct iface_info_ipv4 *)p->Buffer;
407 			addr4->sin_family = AF_INET;
408 			memcpy(&addr4->sin_addr, &p4->IPv4Address, 4);
409 
410 			/* [MS-SMB2] 2.2.32.5.1.1 Clients MUST ignore these */
411 			addr4->sin_port = cpu_to_be16(CIFS_PORT);
412 
413 			cifs_dbg(FYI, "%s: ipv4 %pI4\n", __func__,
414 				 &addr4->sin_addr);
415 			break;
416 		case INTERNETWORKV6:
417 			addr6 =	(struct sockaddr_in6 *)&info->sockaddr;
418 			p6 = (struct iface_info_ipv6 *)p->Buffer;
419 			addr6->sin6_family = AF_INET6;
420 			memcpy(&addr6->sin6_addr, &p6->IPv6Address, 16);
421 
422 			/* [MS-SMB2] 2.2.32.5.1.2 Clients MUST ignore these */
423 			addr6->sin6_flowinfo = 0;
424 			addr6->sin6_scope_id = 0;
425 			addr6->sin6_port = cpu_to_be16(CIFS_PORT);
426 
427 			cifs_dbg(FYI, "%s: ipv6 %pI6\n", __func__,
428 				 &addr6->sin6_addr);
429 			break;
430 		default:
431 			cifs_dbg(VFS,
432 				 "%s: skipping unsupported socket family\n",
433 				 __func__);
434 			goto next_iface;
435 		}
436 
437 		(*iface_count)++;
438 		info++;
439 next_iface:
440 		next = le32_to_cpu(p->Next);
441 		if (!next)
442 			break;
443 		p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
444 		bytes_left -= next;
445 	}
446 
447 	if (!*iface_count) {
448 		rc = -EINVAL;
449 		goto out;
450 	}
451 
452 out:
453 	if (rc) {
454 		kfree(*iface_list);
455 		*iface_count = 0;
456 		*iface_list = NULL;
457 	}
458 	return rc;
459 }
460 
461 
462 static int
SMB3_request_interfaces(const unsigned int xid,struct cifs_tcon * tcon)463 SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
464 {
465 	int rc;
466 	unsigned int ret_data_len = 0;
467 	struct network_interface_info_ioctl_rsp *out_buf = NULL;
468 	struct cifs_server_iface *iface_list;
469 	size_t iface_count;
470 	struct cifs_ses *ses = tcon->ses;
471 
472 	rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
473 			FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
474 			NULL /* no data input */, 0 /* no data input */,
475 			(char **)&out_buf, &ret_data_len);
476 	if (rc == -EOPNOTSUPP) {
477 		cifs_dbg(FYI,
478 			 "server does not support query network interfaces\n");
479 		ret_data_len = 0;
480 	} else if (rc != 0) {
481 		cifs_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
482 		goto out;
483 	}
484 
485 	rc = parse_server_interfaces(out_buf, ret_data_len,
486 				     &iface_list, &iface_count);
487 	if (rc)
488 		goto out;
489 
490 	spin_lock(&ses->iface_lock);
491 	kfree(ses->iface_list);
492 	ses->iface_list = iface_list;
493 	ses->iface_count = iface_count;
494 	ses->iface_last_update = jiffies;
495 	spin_unlock(&ses->iface_lock);
496 
497 out:
498 	kfree(out_buf);
499 	return rc;
500 }
501 
502 static void
smb2_close_cached_fid(struct kref * ref)503 smb2_close_cached_fid(struct kref *ref)
504 {
505 	struct cached_fid *cfid = container_of(ref, struct cached_fid,
506 					       refcount);
507 
508 	if (cfid->is_valid) {
509 		cifs_dbg(FYI, "clear cached root file handle\n");
510 		SMB2_close(0, cfid->tcon, cfid->fid->persistent_fid,
511 			   cfid->fid->volatile_fid);
512 		cfid->is_valid = false;
513 	}
514 }
515 
close_shroot(struct cached_fid * cfid)516 void close_shroot(struct cached_fid *cfid)
517 {
518 	mutex_lock(&cfid->fid_mutex);
519 	kref_put(&cfid->refcount, smb2_close_cached_fid);
520 	mutex_unlock(&cfid->fid_mutex);
521 }
522 
523 void
smb2_cached_lease_break(struct work_struct * work)524 smb2_cached_lease_break(struct work_struct *work)
525 {
526 	struct cached_fid *cfid = container_of(work,
527 				struct cached_fid, lease_break);
528 
529 	close_shroot(cfid);
530 }
531 
532 /*
533  * Open the directory at the root of a share
534  */
open_shroot(unsigned int xid,struct cifs_tcon * tcon,struct cifs_fid * pfid)535 int open_shroot(unsigned int xid, struct cifs_tcon *tcon, struct cifs_fid *pfid)
536 {
537 	struct cifs_open_parms oparams;
538 	int rc;
539 	__le16 srch_path = 0; /* Null - since an open of top of share */
540 	u8 oplock = SMB2_OPLOCK_LEVEL_II;
541 
542 	mutex_lock(&tcon->crfid.fid_mutex);
543 	if (tcon->crfid.is_valid) {
544 		cifs_dbg(FYI, "found a cached root file handle\n");
545 		memcpy(pfid, tcon->crfid.fid, sizeof(struct cifs_fid));
546 		kref_get(&tcon->crfid.refcount);
547 		mutex_unlock(&tcon->crfid.fid_mutex);
548 		return 0;
549 	}
550 
551 	oparams.tcon = tcon;
552 	oparams.create_options = 0;
553 	oparams.desired_access = FILE_READ_ATTRIBUTES;
554 	oparams.disposition = FILE_OPEN;
555 	oparams.fid = pfid;
556 	oparams.reconnect = false;
557 
558 	/*
559 	 * We do not hold the lock for the open because in case
560 	 * SMB2_open needs to reconnect, it will end up calling
561 	 * cifs_mark_open_files_invalid() which takes the lock again
562 	 * thus causing a deadlock
563 	 */
564 	mutex_unlock(&tcon->crfid.fid_mutex);
565 	rc = SMB2_open(xid, &oparams, &srch_path, &oplock, NULL, NULL, NULL);
566 	mutex_lock(&tcon->crfid.fid_mutex);
567 
568 	/*
569 	 * Now we need to check again as the cached root might have
570 	 * been successfully re-opened from a concurrent process
571 	 */
572 
573 	if (tcon->crfid.is_valid) {
574 		/* work was already done */
575 
576 		/* stash fids for close() later */
577 		struct cifs_fid fid = {
578 			.persistent_fid = pfid->persistent_fid,
579 			.volatile_fid = pfid->volatile_fid,
580 		};
581 
582 		/*
583 		 * Caller expects this func to set pfid to a valid
584 		 * cached root, so we copy the existing one and get a
585 		 * reference
586 		 */
587 		memcpy(pfid, tcon->crfid.fid, sizeof(*pfid));
588 		kref_get(&tcon->crfid.refcount);
589 
590 		mutex_unlock(&tcon->crfid.fid_mutex);
591 
592 		if (rc == 0) {
593 			/* close extra handle outside of critical section */
594 			SMB2_close(xid, tcon, fid.persistent_fid,
595 				   fid.volatile_fid);
596 		}
597 		return 0;
598 	}
599 
600 	/* Cached root is still invalid, continue normaly */
601 
602 	if (rc == 0) {
603 		memcpy(tcon->crfid.fid, pfid, sizeof(struct cifs_fid));
604 		tcon->crfid.tcon = tcon;
605 		tcon->crfid.is_valid = true;
606 		kref_init(&tcon->crfid.refcount);
607 		kref_get(&tcon->crfid.refcount);
608 	}
609 
610 	mutex_unlock(&tcon->crfid.fid_mutex);
611 	return rc;
612 }
613 
614 static void
smb3_qfs_tcon(const unsigned int xid,struct cifs_tcon * tcon)615 smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
616 {
617 	int rc;
618 	__le16 srch_path = 0; /* Null - open root of share */
619 	u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
620 	struct cifs_open_parms oparms;
621 	struct cifs_fid fid;
622 	bool no_cached_open = tcon->nohandlecache;
623 
624 	oparms.tcon = tcon;
625 	oparms.desired_access = FILE_READ_ATTRIBUTES;
626 	oparms.disposition = FILE_OPEN;
627 	oparms.create_options = 0;
628 	oparms.fid = &fid;
629 	oparms.reconnect = false;
630 
631 	if (no_cached_open)
632 		rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
633 			       NULL);
634 	else
635 		rc = open_shroot(xid, tcon, &fid);
636 
637 	if (rc)
638 		return;
639 
640 	SMB3_request_interfaces(xid, tcon);
641 
642 	SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
643 			FS_ATTRIBUTE_INFORMATION);
644 	SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
645 			FS_DEVICE_INFORMATION);
646 	SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
647 			FS_VOLUME_INFORMATION);
648 	SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
649 			FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
650 	if (no_cached_open)
651 		SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
652 	else
653 		close_shroot(&tcon->crfid);
654 
655 	return;
656 }
657 
658 static void
smb2_qfs_tcon(const unsigned int xid,struct cifs_tcon * tcon)659 smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
660 {
661 	int rc;
662 	__le16 srch_path = 0; /* Null - open root of share */
663 	u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
664 	struct cifs_open_parms oparms;
665 	struct cifs_fid fid;
666 
667 	oparms.tcon = tcon;
668 	oparms.desired_access = FILE_READ_ATTRIBUTES;
669 	oparms.disposition = FILE_OPEN;
670 	oparms.create_options = 0;
671 	oparms.fid = &fid;
672 	oparms.reconnect = false;
673 
674 	rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
675 	if (rc)
676 		return;
677 
678 	SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
679 			FS_ATTRIBUTE_INFORMATION);
680 	SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
681 			FS_DEVICE_INFORMATION);
682 	SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
683 	return;
684 }
685 
686 static int
smb2_is_path_accessible(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb,const char * full_path)687 smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
688 			struct cifs_sb_info *cifs_sb, const char *full_path)
689 {
690 	int rc;
691 	__le16 *utf16_path;
692 	__u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
693 	struct cifs_open_parms oparms;
694 	struct cifs_fid fid;
695 
696 	if ((*full_path == 0) && tcon->crfid.is_valid)
697 		return 0;
698 
699 	utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
700 	if (!utf16_path)
701 		return -ENOMEM;
702 
703 	oparms.tcon = tcon;
704 	oparms.desired_access = FILE_READ_ATTRIBUTES;
705 	oparms.disposition = FILE_OPEN;
706 	if (backup_cred(cifs_sb))
707 		oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
708 	else
709 		oparms.create_options = 0;
710 	oparms.fid = &fid;
711 	oparms.reconnect = false;
712 
713 	rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
714 	if (rc) {
715 		kfree(utf16_path);
716 		return rc;
717 	}
718 
719 	rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
720 	kfree(utf16_path);
721 	return rc;
722 }
723 
724 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)725 smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
726 		  struct cifs_sb_info *cifs_sb, const char *full_path,
727 		  u64 *uniqueid, FILE_ALL_INFO *data)
728 {
729 	*uniqueid = le64_to_cpu(data->IndexNumber);
730 	return 0;
731 }
732 
733 static int
smb2_query_file_info(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_fid * fid,FILE_ALL_INFO * data)734 smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
735 		     struct cifs_fid *fid, FILE_ALL_INFO *data)
736 {
737 	int rc;
738 	struct smb2_file_all_info *smb2_data;
739 
740 	smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
741 			    GFP_KERNEL);
742 	if (smb2_data == NULL)
743 		return -ENOMEM;
744 
745 	rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
746 			     smb2_data);
747 	if (!rc)
748 		move_smb2_info_to_cifs(data, smb2_data);
749 	kfree(smb2_data);
750 	return rc;
751 }
752 
753 #ifdef CONFIG_CIFS_XATTR
754 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)755 move_smb2_ea_to_cifs(char *dst, size_t dst_size,
756 		     struct smb2_file_full_ea_info *src, size_t src_size,
757 		     const unsigned char *ea_name)
758 {
759 	int rc = 0;
760 	unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
761 	char *name, *value;
762 	size_t buf_size = dst_size;
763 	size_t name_len, value_len, user_name_len;
764 
765 	while (src_size > 0) {
766 		name_len = (size_t)src->ea_name_length;
767 		value_len = (size_t)le16_to_cpu(src->ea_value_length);
768 
769 		if (name_len == 0) {
770 			break;
771 		}
772 
773 		if (src_size < 8 + name_len + 1 + value_len) {
774 			cifs_dbg(FYI, "EA entry goes beyond length of list\n");
775 			rc = -EIO;
776 			goto out;
777 		}
778 
779 		name = &src->ea_data[0];
780 		value = &src->ea_data[src->ea_name_length + 1];
781 
782 		if (ea_name) {
783 			if (ea_name_len == name_len &&
784 			    memcmp(ea_name, name, name_len) == 0) {
785 				rc = value_len;
786 				if (dst_size == 0)
787 					goto out;
788 				if (dst_size < value_len) {
789 					rc = -ERANGE;
790 					goto out;
791 				}
792 				memcpy(dst, value, value_len);
793 				goto out;
794 			}
795 		} else {
796 			/* 'user.' plus a terminating null */
797 			user_name_len = 5 + 1 + name_len;
798 
799 			if (buf_size == 0) {
800 				/* skip copy - calc size only */
801 				rc += user_name_len;
802 			} else if (dst_size >= user_name_len) {
803 				dst_size -= user_name_len;
804 				memcpy(dst, "user.", 5);
805 				dst += 5;
806 				memcpy(dst, src->ea_data, name_len);
807 				dst += name_len;
808 				*dst = 0;
809 				++dst;
810 				rc += user_name_len;
811 			} else {
812 				/* stop before overrun buffer */
813 				rc = -ERANGE;
814 				break;
815 			}
816 		}
817 
818 		if (!src->next_entry_offset)
819 			break;
820 
821 		if (src_size < le32_to_cpu(src->next_entry_offset)) {
822 			/* stop before overrun buffer */
823 			rc = -ERANGE;
824 			break;
825 		}
826 		src_size -= le32_to_cpu(src->next_entry_offset);
827 		src = (void *)((char *)src +
828 			       le32_to_cpu(src->next_entry_offset));
829 	}
830 
831 	/* didn't find the named attribute */
832 	if (ea_name)
833 		rc = -ENODATA;
834 
835 out:
836 	return (ssize_t)rc;
837 }
838 
839 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)840 smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
841 	       const unsigned char *path, const unsigned char *ea_name,
842 	       char *ea_data, size_t buf_size,
843 	       struct cifs_sb_info *cifs_sb)
844 {
845 	int rc;
846 	__le16 *utf16_path;
847 	__u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
848 	struct cifs_open_parms oparms;
849 	struct cifs_fid fid;
850 	struct smb2_file_full_ea_info *smb2_data;
851 	int ea_buf_size = SMB2_MIN_EA_BUF;
852 
853 	utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
854 	if (!utf16_path)
855 		return -ENOMEM;
856 
857 	oparms.tcon = tcon;
858 	oparms.desired_access = FILE_READ_EA;
859 	oparms.disposition = FILE_OPEN;
860 	if (backup_cred(cifs_sb))
861 		oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
862 	else
863 		oparms.create_options = 0;
864 	oparms.fid = &fid;
865 	oparms.reconnect = false;
866 
867 	rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
868 	kfree(utf16_path);
869 	if (rc) {
870 		cifs_dbg(FYI, "open failed rc=%d\n", rc);
871 		return rc;
872 	}
873 
874 	while (1) {
875 		smb2_data = kzalloc(ea_buf_size, GFP_KERNEL);
876 		if (smb2_data == NULL) {
877 			SMB2_close(xid, tcon, fid.persistent_fid,
878 				   fid.volatile_fid);
879 			return -ENOMEM;
880 		}
881 
882 		rc = SMB2_query_eas(xid, tcon, fid.persistent_fid,
883 				    fid.volatile_fid,
884 				    ea_buf_size, smb2_data);
885 
886 		if (rc != -E2BIG)
887 			break;
888 
889 		kfree(smb2_data);
890 		ea_buf_size <<= 1;
891 
892 		if (ea_buf_size > SMB2_MAX_EA_BUF) {
893 			cifs_dbg(VFS, "EA size is too large\n");
894 			SMB2_close(xid, tcon, fid.persistent_fid,
895 				   fid.volatile_fid);
896 			return -ENOMEM;
897 		}
898 	}
899 
900 	SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
901 
902 	/*
903 	 * If ea_name is NULL (listxattr) and there are no EAs, return 0 as it's
904 	 * not an error. Otherwise, the specified ea_name was not found.
905 	 */
906 	if (!rc)
907 		rc = move_smb2_ea_to_cifs(ea_data, buf_size, smb2_data,
908 					  SMB2_MAX_EA_BUF, ea_name);
909 	else if (!ea_name && rc == -ENODATA)
910 		rc = 0;
911 
912 	kfree(smb2_data);
913 	return rc;
914 }
915 
916 
917 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)918 smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
919 	    const char *path, const char *ea_name, const void *ea_value,
920 	    const __u16 ea_value_len, const struct nls_table *nls_codepage,
921 	    struct cifs_sb_info *cifs_sb)
922 {
923 	int rc;
924 	__le16 *utf16_path;
925 	__u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
926 	struct cifs_open_parms oparms;
927 	struct cifs_fid fid;
928 	struct smb2_file_full_ea_info *ea;
929 	int ea_name_len = strlen(ea_name);
930 	int len;
931 
932 	if (ea_name_len > 255)
933 		return -EINVAL;
934 
935 	utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
936 	if (!utf16_path)
937 		return -ENOMEM;
938 
939 	oparms.tcon = tcon;
940 	oparms.desired_access = FILE_WRITE_EA;
941 	oparms.disposition = FILE_OPEN;
942 	if (backup_cred(cifs_sb))
943 		oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
944 	else
945 		oparms.create_options = 0;
946 	oparms.fid = &fid;
947 	oparms.reconnect = false;
948 
949 	rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
950 	kfree(utf16_path);
951 	if (rc) {
952 		cifs_dbg(FYI, "open failed rc=%d\n", rc);
953 		return rc;
954 	}
955 
956 	len = sizeof(*ea) + ea_name_len + ea_value_len + 1;
957 	ea = kzalloc(len, GFP_KERNEL);
958 	if (ea == NULL) {
959 		SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
960 		return -ENOMEM;
961 	}
962 
963 	ea->ea_name_length = ea_name_len;
964 	ea->ea_value_length = cpu_to_le16(ea_value_len);
965 	memcpy(ea->ea_data, ea_name, ea_name_len + 1);
966 	memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
967 
968 	rc = SMB2_set_ea(xid, tcon, fid.persistent_fid, fid.volatile_fid, ea,
969 			 len);
970 	kfree(ea);
971 
972 	SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
973 
974 	return rc;
975 }
976 #endif
977 
978 static bool
smb2_can_echo(struct TCP_Server_Info * server)979 smb2_can_echo(struct TCP_Server_Info *server)
980 {
981 	return server->echoes;
982 }
983 
984 static void
smb2_clear_stats(struct cifs_tcon * tcon)985 smb2_clear_stats(struct cifs_tcon *tcon)
986 {
987 	int i;
988 	for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
989 		atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
990 		atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
991 	}
992 }
993 
994 static void
smb2_dump_share_caps(struct seq_file * m,struct cifs_tcon * tcon)995 smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
996 {
997 	seq_puts(m, "\n\tShare Capabilities:");
998 	if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
999 		seq_puts(m, " DFS,");
1000 	if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
1001 		seq_puts(m, " CONTINUOUS AVAILABILITY,");
1002 	if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
1003 		seq_puts(m, " SCALEOUT,");
1004 	if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
1005 		seq_puts(m, " CLUSTER,");
1006 	if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
1007 		seq_puts(m, " ASYMMETRIC,");
1008 	if (tcon->capabilities == 0)
1009 		seq_puts(m, " None");
1010 	if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
1011 		seq_puts(m, " Aligned,");
1012 	if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
1013 		seq_puts(m, " Partition Aligned,");
1014 	if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
1015 		seq_puts(m, " SSD,");
1016 	if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
1017 		seq_puts(m, " TRIM-support,");
1018 
1019 	seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
1020 	seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
1021 	if (tcon->perf_sector_size)
1022 		seq_printf(m, "\tOptimal sector size: 0x%x",
1023 			   tcon->perf_sector_size);
1024 	seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
1025 }
1026 
1027 static void
smb2_print_stats(struct seq_file * m,struct cifs_tcon * tcon)1028 smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
1029 {
1030 	atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
1031 	atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
1032 
1033 	/*
1034 	 *  Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
1035 	 *  totals (requests sent) since those SMBs are per-session not per tcon
1036 	 */
1037 	seq_printf(m, "\nBytes read: %llu  Bytes written: %llu",
1038 		   (long long)(tcon->bytes_read),
1039 		   (long long)(tcon->bytes_written));
1040 	seq_printf(m, "\nTreeConnects: %d total %d failed",
1041 		   atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
1042 		   atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
1043 	seq_printf(m, "\nTreeDisconnects: %d total %d failed",
1044 		   atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
1045 		   atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
1046 	seq_printf(m, "\nCreates: %d total %d failed",
1047 		   atomic_read(&sent[SMB2_CREATE_HE]),
1048 		   atomic_read(&failed[SMB2_CREATE_HE]));
1049 	seq_printf(m, "\nCloses: %d total %d failed",
1050 		   atomic_read(&sent[SMB2_CLOSE_HE]),
1051 		   atomic_read(&failed[SMB2_CLOSE_HE]));
1052 	seq_printf(m, "\nFlushes: %d total %d failed",
1053 		   atomic_read(&sent[SMB2_FLUSH_HE]),
1054 		   atomic_read(&failed[SMB2_FLUSH_HE]));
1055 	seq_printf(m, "\nReads: %d total %d failed",
1056 		   atomic_read(&sent[SMB2_READ_HE]),
1057 		   atomic_read(&failed[SMB2_READ_HE]));
1058 	seq_printf(m, "\nWrites: %d total %d failed",
1059 		   atomic_read(&sent[SMB2_WRITE_HE]),
1060 		   atomic_read(&failed[SMB2_WRITE_HE]));
1061 	seq_printf(m, "\nLocks: %d total %d failed",
1062 		   atomic_read(&sent[SMB2_LOCK_HE]),
1063 		   atomic_read(&failed[SMB2_LOCK_HE]));
1064 	seq_printf(m, "\nIOCTLs: %d total %d failed",
1065 		   atomic_read(&sent[SMB2_IOCTL_HE]),
1066 		   atomic_read(&failed[SMB2_IOCTL_HE]));
1067 	seq_printf(m, "\nQueryDirectories: %d total %d failed",
1068 		   atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
1069 		   atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
1070 	seq_printf(m, "\nChangeNotifies: %d total %d failed",
1071 		   atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
1072 		   atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
1073 	seq_printf(m, "\nQueryInfos: %d total %d failed",
1074 		   atomic_read(&sent[SMB2_QUERY_INFO_HE]),
1075 		   atomic_read(&failed[SMB2_QUERY_INFO_HE]));
1076 	seq_printf(m, "\nSetInfos: %d total %d failed",
1077 		   atomic_read(&sent[SMB2_SET_INFO_HE]),
1078 		   atomic_read(&failed[SMB2_SET_INFO_HE]));
1079 	seq_printf(m, "\nOplockBreaks: %d sent %d failed",
1080 		   atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
1081 		   atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
1082 }
1083 
1084 static void
smb2_set_fid(struct cifsFileInfo * cfile,struct cifs_fid * fid,__u32 oplock)1085 smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
1086 {
1087 	struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1088 	struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1089 
1090 	cfile->fid.persistent_fid = fid->persistent_fid;
1091 	cfile->fid.volatile_fid = fid->volatile_fid;
1092 	server->ops->set_oplock_level(cinode, oplock, fid->epoch,
1093 				      &fid->purge_cache);
1094 	cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
1095 	memcpy(cfile->fid.create_guid, fid->create_guid, 16);
1096 }
1097 
1098 static void
smb2_close_file(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_fid * fid)1099 smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
1100 		struct cifs_fid *fid)
1101 {
1102 	SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1103 }
1104 
1105 static int
SMB2_request_res_key(const unsigned int xid,struct cifs_tcon * tcon,u64 persistent_fid,u64 volatile_fid,struct copychunk_ioctl * pcchunk)1106 SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
1107 		     u64 persistent_fid, u64 volatile_fid,
1108 		     struct copychunk_ioctl *pcchunk)
1109 {
1110 	int rc;
1111 	unsigned int ret_data_len;
1112 	struct resume_key_req *res_key;
1113 
1114 	rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1115 			FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
1116 			NULL, 0 /* no input */,
1117 			(char **)&res_key, &ret_data_len);
1118 
1119 	if (rc) {
1120 		cifs_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
1121 		goto req_res_key_exit;
1122 	}
1123 	if (ret_data_len < sizeof(struct resume_key_req)) {
1124 		cifs_dbg(VFS, "Invalid refcopy resume key length\n");
1125 		rc = -EINVAL;
1126 		goto req_res_key_exit;
1127 	}
1128 	memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
1129 
1130 req_res_key_exit:
1131 	kfree(res_key);
1132 	return rc;
1133 }
1134 
1135 static ssize_t
smb2_copychunk_range(const unsigned int xid,struct cifsFileInfo * srcfile,struct cifsFileInfo * trgtfile,u64 src_off,u64 len,u64 dest_off)1136 smb2_copychunk_range(const unsigned int xid,
1137 			struct cifsFileInfo *srcfile,
1138 			struct cifsFileInfo *trgtfile, u64 src_off,
1139 			u64 len, u64 dest_off)
1140 {
1141 	int rc;
1142 	unsigned int ret_data_len;
1143 	struct copychunk_ioctl *pcchunk;
1144 	struct copychunk_ioctl_rsp *retbuf = NULL;
1145 	struct cifs_tcon *tcon;
1146 	int chunks_copied = 0;
1147 	bool chunk_sizes_updated = false;
1148 	ssize_t bytes_written, total_bytes_written = 0;
1149 	struct inode *inode;
1150 
1151 	pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
1152 
1153 	/*
1154 	 * We need to flush all unwritten data before we can send the
1155 	 * copychunk ioctl to the server.
1156 	 */
1157 	inode = d_inode(trgtfile->dentry);
1158 	filemap_write_and_wait(inode->i_mapping);
1159 
1160 	if (pcchunk == NULL)
1161 		return -ENOMEM;
1162 
1163 	cifs_dbg(FYI, "in smb2_copychunk_range - about to call request res key\n");
1164 	/* Request a key from the server to identify the source of the copy */
1165 	rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
1166 				srcfile->fid.persistent_fid,
1167 				srcfile->fid.volatile_fid, pcchunk);
1168 
1169 	/* Note: request_res_key sets res_key null only if rc !=0 */
1170 	if (rc)
1171 		goto cchunk_out;
1172 
1173 	/* For now array only one chunk long, will make more flexible later */
1174 	pcchunk->ChunkCount = cpu_to_le32(1);
1175 	pcchunk->Reserved = 0;
1176 	pcchunk->Reserved2 = 0;
1177 
1178 	tcon = tlink_tcon(trgtfile->tlink);
1179 
1180 	while (len > 0) {
1181 		pcchunk->SourceOffset = cpu_to_le64(src_off);
1182 		pcchunk->TargetOffset = cpu_to_le64(dest_off);
1183 		pcchunk->Length =
1184 			cpu_to_le32(min_t(u64, len, tcon->max_bytes_chunk));
1185 
1186 		/* Request server copy to target from src identified by key */
1187 		kfree(retbuf);
1188 		retbuf = NULL;
1189 		rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1190 			trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
1191 			true /* is_fsctl */, (char *)pcchunk,
1192 			sizeof(struct copychunk_ioctl),	(char **)&retbuf,
1193 			&ret_data_len);
1194 		if (rc == 0) {
1195 			if (ret_data_len !=
1196 					sizeof(struct copychunk_ioctl_rsp)) {
1197 				cifs_dbg(VFS, "invalid cchunk response size\n");
1198 				rc = -EIO;
1199 				goto cchunk_out;
1200 			}
1201 			if (retbuf->TotalBytesWritten == 0) {
1202 				cifs_dbg(FYI, "no bytes copied\n");
1203 				rc = -EIO;
1204 				goto cchunk_out;
1205 			}
1206 			/*
1207 			 * Check if server claimed to write more than we asked
1208 			 */
1209 			if (le32_to_cpu(retbuf->TotalBytesWritten) >
1210 			    le32_to_cpu(pcchunk->Length)) {
1211 				cifs_dbg(VFS, "invalid copy chunk response\n");
1212 				rc = -EIO;
1213 				goto cchunk_out;
1214 			}
1215 			if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
1216 				cifs_dbg(VFS, "invalid num chunks written\n");
1217 				rc = -EIO;
1218 				goto cchunk_out;
1219 			}
1220 			chunks_copied++;
1221 
1222 			bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
1223 			src_off += bytes_written;
1224 			dest_off += bytes_written;
1225 			len -= bytes_written;
1226 			total_bytes_written += bytes_written;
1227 
1228 			cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
1229 				le32_to_cpu(retbuf->ChunksWritten),
1230 				le32_to_cpu(retbuf->ChunkBytesWritten),
1231 				bytes_written);
1232 		} else if (rc == -EINVAL) {
1233 			if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
1234 				goto cchunk_out;
1235 
1236 			cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
1237 				le32_to_cpu(retbuf->ChunksWritten),
1238 				le32_to_cpu(retbuf->ChunkBytesWritten),
1239 				le32_to_cpu(retbuf->TotalBytesWritten));
1240 
1241 			/*
1242 			 * Check if this is the first request using these sizes,
1243 			 * (ie check if copy succeed once with original sizes
1244 			 * and check if the server gave us different sizes after
1245 			 * we already updated max sizes on previous request).
1246 			 * if not then why is the server returning an error now
1247 			 */
1248 			if ((chunks_copied != 0) || chunk_sizes_updated)
1249 				goto cchunk_out;
1250 
1251 			/* Check that server is not asking us to grow size */
1252 			if (le32_to_cpu(retbuf->ChunkBytesWritten) <
1253 					tcon->max_bytes_chunk)
1254 				tcon->max_bytes_chunk =
1255 					le32_to_cpu(retbuf->ChunkBytesWritten);
1256 			else
1257 				goto cchunk_out; /* server gave us bogus size */
1258 
1259 			/* No need to change MaxChunks since already set to 1 */
1260 			chunk_sizes_updated = true;
1261 		} else
1262 			goto cchunk_out;
1263 	}
1264 
1265 cchunk_out:
1266 	kfree(pcchunk);
1267 	kfree(retbuf);
1268 	if (rc)
1269 		return rc;
1270 	else
1271 		return total_bytes_written;
1272 }
1273 
1274 static int
smb2_flush_file(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_fid * fid)1275 smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1276 		struct cifs_fid *fid)
1277 {
1278 	return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1279 }
1280 
1281 static unsigned int
smb2_read_data_offset(char * buf)1282 smb2_read_data_offset(char *buf)
1283 {
1284 	struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1285 	return rsp->DataOffset;
1286 }
1287 
1288 static unsigned int
smb2_read_data_length(char * buf,bool in_remaining)1289 smb2_read_data_length(char *buf, bool in_remaining)
1290 {
1291 	struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1292 
1293 	if (in_remaining)
1294 		return le32_to_cpu(rsp->DataRemaining);
1295 
1296 	return le32_to_cpu(rsp->DataLength);
1297 }
1298 
1299 
1300 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)1301 smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
1302 	       struct cifs_io_parms *parms, unsigned int *bytes_read,
1303 	       char **buf, int *buf_type)
1304 {
1305 	parms->persistent_fid = pfid->persistent_fid;
1306 	parms->volatile_fid = pfid->volatile_fid;
1307 	return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1308 }
1309 
1310 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)1311 smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
1312 		struct cifs_io_parms *parms, unsigned int *written,
1313 		struct kvec *iov, unsigned long nr_segs)
1314 {
1315 
1316 	parms->persistent_fid = pfid->persistent_fid;
1317 	parms->volatile_fid = pfid->volatile_fid;
1318 	return SMB2_write(xid, parms, written, iov, nr_segs);
1319 }
1320 
1321 /* 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)1322 static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1323 		struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1324 {
1325 	struct cifsInodeInfo *cifsi;
1326 	int rc;
1327 
1328 	cifsi = CIFS_I(inode);
1329 
1330 	/* if file already sparse don't bother setting sparse again */
1331 	if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1332 		return true; /* already sparse */
1333 
1334 	if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1335 		return true; /* already not sparse */
1336 
1337 	/*
1338 	 * Can't check for sparse support on share the usual way via the
1339 	 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1340 	 * since Samba server doesn't set the flag on the share, yet
1341 	 * supports the set sparse FSCTL and returns sparse correctly
1342 	 * in the file attributes. If we fail setting sparse though we
1343 	 * mark that server does not support sparse files for this share
1344 	 * to avoid repeatedly sending the unsupported fsctl to server
1345 	 * if the file is repeatedly extended.
1346 	 */
1347 	if (tcon->broken_sparse_sup)
1348 		return false;
1349 
1350 	rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1351 			cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
1352 			true /* is_fctl */,
1353 			&setsparse, 1, NULL, NULL);
1354 	if (rc) {
1355 		tcon->broken_sparse_sup = true;
1356 		cifs_dbg(FYI, "set sparse rc = %d\n", rc);
1357 		return false;
1358 	}
1359 
1360 	if (setsparse)
1361 		cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
1362 	else
1363 		cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1364 
1365 	return true;
1366 }
1367 
1368 static int
smb2_set_file_size(const unsigned int xid,struct cifs_tcon * tcon,struct cifsFileInfo * cfile,__u64 size,bool set_alloc)1369 smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1370 		   struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1371 {
1372 	__le64 eof = cpu_to_le64(size);
1373 	struct inode *inode;
1374 
1375 	/*
1376 	 * If extending file more than one page make sparse. Many Linux fs
1377 	 * make files sparse by default when extending via ftruncate
1378 	 */
1379 	inode = d_inode(cfile->dentry);
1380 
1381 	if (!set_alloc && (size > inode->i_size + 8192)) {
1382 		__u8 set_sparse = 1;
1383 
1384 		/* whether set sparse succeeds or not, extend the file */
1385 		smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
1386 	}
1387 
1388 	return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
1389 			    cfile->fid.volatile_fid, cfile->pid, &eof, false);
1390 }
1391 
1392 static int
smb2_duplicate_extents(const unsigned int xid,struct cifsFileInfo * srcfile,struct cifsFileInfo * trgtfile,u64 src_off,u64 len,u64 dest_off)1393 smb2_duplicate_extents(const unsigned int xid,
1394 			struct cifsFileInfo *srcfile,
1395 			struct cifsFileInfo *trgtfile, u64 src_off,
1396 			u64 len, u64 dest_off)
1397 {
1398 	int rc;
1399 	unsigned int ret_data_len;
1400 	struct duplicate_extents_to_file dup_ext_buf;
1401 	struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
1402 
1403 	/* server fileays advertise duplicate extent support with this flag */
1404 	if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
1405 	     FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
1406 		return -EOPNOTSUPP;
1407 
1408 	dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
1409 	dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
1410 	dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
1411 	dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
1412 	dup_ext_buf.ByteCount = cpu_to_le64(len);
1413 	cifs_dbg(FYI, "duplicate extents: src off %lld dst off %lld len %lld",
1414 		src_off, dest_off, len);
1415 
1416 	rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
1417 	if (rc)
1418 		goto duplicate_extents_out;
1419 
1420 	rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1421 			trgtfile->fid.volatile_fid,
1422 			FSCTL_DUPLICATE_EXTENTS_TO_FILE,
1423 			true /* is_fsctl */,
1424 			(char *)&dup_ext_buf,
1425 			sizeof(struct duplicate_extents_to_file),
1426 			NULL,
1427 			&ret_data_len);
1428 
1429 	if (ret_data_len > 0)
1430 		cifs_dbg(FYI, "non-zero response length in duplicate extents");
1431 
1432 duplicate_extents_out:
1433 	return rc;
1434 }
1435 
1436 static int
smb2_set_compression(const unsigned int xid,struct cifs_tcon * tcon,struct cifsFileInfo * cfile)1437 smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1438 		   struct cifsFileInfo *cfile)
1439 {
1440 	return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
1441 			    cfile->fid.volatile_fid);
1442 }
1443 
1444 static int
smb3_set_integrity(const unsigned int xid,struct cifs_tcon * tcon,struct cifsFileInfo * cfile)1445 smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
1446 		   struct cifsFileInfo *cfile)
1447 {
1448 	struct fsctl_set_integrity_information_req integr_info;
1449 	unsigned int ret_data_len;
1450 
1451 	integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
1452 	integr_info.Flags = 0;
1453 	integr_info.Reserved = 0;
1454 
1455 	return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1456 			cfile->fid.volatile_fid,
1457 			FSCTL_SET_INTEGRITY_INFORMATION,
1458 			true /* is_fsctl */,
1459 			(char *)&integr_info,
1460 			sizeof(struct fsctl_set_integrity_information_req),
1461 			NULL,
1462 			&ret_data_len);
1463 
1464 }
1465 
1466 /* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
1467 #define GMT_TOKEN_SIZE 50
1468 
1469 /*
1470  * Input buffer contains (empty) struct smb_snapshot array with size filled in
1471  * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
1472  */
1473 static int
smb3_enum_snapshots(const unsigned int xid,struct cifs_tcon * tcon,struct cifsFileInfo * cfile,void __user * ioc_buf)1474 smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
1475 		   struct cifsFileInfo *cfile, void __user *ioc_buf)
1476 {
1477 	char *retbuf = NULL;
1478 	unsigned int ret_data_len = 0;
1479 	int rc;
1480 	struct smb_snapshot_array snapshot_in;
1481 
1482 	rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1483 			cfile->fid.volatile_fid,
1484 			FSCTL_SRV_ENUMERATE_SNAPSHOTS,
1485 			true /* is_fsctl */,
1486 			NULL, 0 /* no input data */,
1487 			(char **)&retbuf,
1488 			&ret_data_len);
1489 	cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
1490 			rc, ret_data_len);
1491 	if (rc)
1492 		return rc;
1493 
1494 	if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
1495 		/* Fixup buffer */
1496 		if (copy_from_user(&snapshot_in, ioc_buf,
1497 		    sizeof(struct smb_snapshot_array))) {
1498 			rc = -EFAULT;
1499 			kfree(retbuf);
1500 			return rc;
1501 		}
1502 
1503 		/*
1504 		 * Check for min size, ie not large enough to fit even one GMT
1505 		 * token (snapshot).  On the first ioctl some users may pass in
1506 		 * smaller size (or zero) to simply get the size of the array
1507 		 * so the user space caller can allocate sufficient memory
1508 		 * and retry the ioctl again with larger array size sufficient
1509 		 * to hold all of the snapshot GMT tokens on the second try.
1510 		 */
1511 		if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
1512 			ret_data_len = sizeof(struct smb_snapshot_array);
1513 
1514 		/*
1515 		 * We return struct SRV_SNAPSHOT_ARRAY, followed by
1516 		 * the snapshot array (of 50 byte GMT tokens) each
1517 		 * representing an available previous version of the data
1518 		 */
1519 		if (ret_data_len > (snapshot_in.snapshot_array_size +
1520 					sizeof(struct smb_snapshot_array)))
1521 			ret_data_len = snapshot_in.snapshot_array_size +
1522 					sizeof(struct smb_snapshot_array);
1523 
1524 		if (copy_to_user(ioc_buf, retbuf, ret_data_len))
1525 			rc = -EFAULT;
1526 	}
1527 
1528 	kfree(retbuf);
1529 	return rc;
1530 }
1531 
1532 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)1533 smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
1534 		     const char *path, struct cifs_sb_info *cifs_sb,
1535 		     struct cifs_fid *fid, __u16 search_flags,
1536 		     struct cifs_search_info *srch_inf)
1537 {
1538 	__le16 *utf16_path;
1539 	int rc;
1540 	__u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1541 	struct cifs_open_parms oparms;
1542 
1543 	utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1544 	if (!utf16_path)
1545 		return -ENOMEM;
1546 
1547 	oparms.tcon = tcon;
1548 	oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
1549 	oparms.disposition = FILE_OPEN;
1550 	if (backup_cred(cifs_sb))
1551 		oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1552 	else
1553 		oparms.create_options = 0;
1554 	oparms.fid = fid;
1555 	oparms.reconnect = false;
1556 
1557 	rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
1558 	kfree(utf16_path);
1559 	if (rc) {
1560 		cifs_dbg(FYI, "open dir failed rc=%d\n", rc);
1561 		return rc;
1562 	}
1563 
1564 	srch_inf->entries_in_buffer = 0;
1565 	srch_inf->index_of_last_entry = 2;
1566 
1567 	rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
1568 				  fid->volatile_fid, 0, srch_inf);
1569 	if (rc) {
1570 		cifs_dbg(FYI, "query directory failed rc=%d\n", rc);
1571 		SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1572 	}
1573 	return rc;
1574 }
1575 
1576 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)1577 smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
1578 		    struct cifs_fid *fid, __u16 search_flags,
1579 		    struct cifs_search_info *srch_inf)
1580 {
1581 	return SMB2_query_directory(xid, tcon, fid->persistent_fid,
1582 				    fid->volatile_fid, 0, srch_inf);
1583 }
1584 
1585 static int
smb2_close_dir(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_fid * fid)1586 smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
1587 	       struct cifs_fid *fid)
1588 {
1589 	return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1590 }
1591 
1592 /*
1593 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
1594 * the number of credits and return true. Otherwise - return false.
1595 */
1596 static bool
smb2_is_status_pending(char * buf,struct TCP_Server_Info * server,int length)1597 smb2_is_status_pending(char *buf, struct TCP_Server_Info *server, int length)
1598 {
1599 	struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
1600 
1601 	if (shdr->Status != STATUS_PENDING)
1602 		return false;
1603 
1604 	if (!length) {
1605 		spin_lock(&server->req_lock);
1606 		server->credits += le16_to_cpu(shdr->CreditRequest);
1607 		spin_unlock(&server->req_lock);
1608 		wake_up(&server->request_q);
1609 	}
1610 
1611 	return true;
1612 }
1613 
1614 static bool
smb2_is_session_expired(char * buf)1615 smb2_is_session_expired(char *buf)
1616 {
1617 	struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
1618 
1619 	if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
1620 	    shdr->Status != STATUS_USER_SESSION_DELETED)
1621 		return false;
1622 
1623 	trace_smb3_ses_expired(shdr->TreeId, shdr->SessionId,
1624 			       le16_to_cpu(shdr->Command),
1625 			       le64_to_cpu(shdr->MessageId));
1626 	cifs_dbg(FYI, "Session expired or deleted\n");
1627 
1628 	return true;
1629 }
1630 
1631 static int
smb2_oplock_response(struct cifs_tcon * tcon,struct cifs_fid * fid,struct cifsInodeInfo * cinode)1632 smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
1633 		     struct cifsInodeInfo *cinode)
1634 {
1635 	if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
1636 		return SMB2_lease_break(0, tcon, cinode->lease_key,
1637 					smb2_get_lease_state(cinode));
1638 
1639 	return SMB2_oplock_break(0, tcon, fid->persistent_fid,
1640 				 fid->volatile_fid,
1641 				 CIFS_CACHE_READ(cinode) ? 1 : 0);
1642 }
1643 
1644 static void
smb2_set_related(struct smb_rqst * rqst)1645 smb2_set_related(struct smb_rqst *rqst)
1646 {
1647 	struct smb2_sync_hdr *shdr;
1648 
1649 	shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
1650 	shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
1651 }
1652 
1653 char smb2_padding[7] = {0, 0, 0, 0, 0, 0, 0};
1654 
1655 static void
smb2_set_next_command(struct TCP_Server_Info * server,struct smb_rqst * rqst)1656 smb2_set_next_command(struct TCP_Server_Info *server, struct smb_rqst *rqst)
1657 {
1658 	struct smb2_sync_hdr *shdr;
1659 	unsigned long len = smb_rqst_len(server, rqst);
1660 
1661 	/* SMB headers in a compound are 8 byte aligned. */
1662 	if (len & 7) {
1663 		rqst->rq_iov[rqst->rq_nvec].iov_base = smb2_padding;
1664 		rqst->rq_iov[rqst->rq_nvec].iov_len = 8 - (len & 7);
1665 		rqst->rq_nvec++;
1666 		len = smb_rqst_len(server, rqst);
1667 	}
1668 
1669 	shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
1670 	shdr->NextCommand = cpu_to_le32(len);
1671 }
1672 
1673 static int
smb2_queryfs(const unsigned int xid,struct cifs_tcon * tcon,struct kstatfs * buf)1674 smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
1675 	     struct kstatfs *buf)
1676 {
1677 	struct smb2_query_info_rsp *rsp;
1678 	struct smb2_fs_full_size_info *info = NULL;
1679 	struct smb_rqst rqst[3];
1680 	int resp_buftype[3];
1681 	struct kvec rsp_iov[3];
1682 	struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1683 	struct kvec qi_iov[1];
1684 	struct kvec close_iov[1];
1685 	struct cifs_ses *ses = tcon->ses;
1686 	struct TCP_Server_Info *server = ses->server;
1687 	__le16 srch_path = 0; /* Null - open root of share */
1688 	u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1689 	struct cifs_open_parms oparms;
1690 	struct cifs_fid fid;
1691 	int flags = 0;
1692 	int rc;
1693 
1694 	if (smb3_encryption_required(tcon))
1695 		flags |= CIFS_TRANSFORM_REQ;
1696 
1697 	memset(rqst, 0, sizeof(rqst));
1698 	memset(resp_buftype, 0, sizeof(resp_buftype));
1699 	memset(rsp_iov, 0, sizeof(rsp_iov));
1700 
1701 	memset(&open_iov, 0, sizeof(open_iov));
1702 	rqst[0].rq_iov = open_iov;
1703 	rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1704 
1705 	oparms.tcon = tcon;
1706 	oparms.desired_access = FILE_READ_ATTRIBUTES;
1707 	oparms.disposition = FILE_OPEN;
1708 	oparms.create_options = 0;
1709 	oparms.fid = &fid;
1710 	oparms.reconnect = false;
1711 
1712 	rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, &srch_path);
1713 	if (rc)
1714 		goto qfs_exit;
1715 	smb2_set_next_command(server, &rqst[0]);
1716 
1717 	memset(&qi_iov, 0, sizeof(qi_iov));
1718 	rqst[1].rq_iov = qi_iov;
1719 	rqst[1].rq_nvec = 1;
1720 
1721 	rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID, COMPOUND_FID,
1722 				  FS_FULL_SIZE_INFORMATION,
1723 				  SMB2_O_INFO_FILESYSTEM, 0,
1724 				  sizeof(struct smb2_fs_full_size_info));
1725 	if (rc)
1726 		goto qfs_exit;
1727 	smb2_set_next_command(server, &rqst[1]);
1728 	smb2_set_related(&rqst[1]);
1729 
1730 	memset(&close_iov, 0, sizeof(close_iov));
1731 	rqst[2].rq_iov = close_iov;
1732 	rqst[2].rq_nvec = 1;
1733 
1734 	rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
1735 	if (rc)
1736 		goto qfs_exit;
1737 	smb2_set_related(&rqst[2]);
1738 
1739 	rc = compound_send_recv(xid, ses, flags, 3, rqst,
1740 				resp_buftype, rsp_iov);
1741 	if (rc)
1742 		goto qfs_exit;
1743 
1744 	rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1745 	buf->f_type = SMB2_MAGIC_NUMBER;
1746 	info = (struct smb2_fs_full_size_info *)(
1747 		le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
1748 	rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
1749 			       le32_to_cpu(rsp->OutputBufferLength),
1750 			       &rsp_iov[1],
1751 			       sizeof(struct smb2_fs_full_size_info));
1752 	if (!rc)
1753 		smb2_copy_fs_info_to_kstatfs(info, buf);
1754 
1755 qfs_exit:
1756 	SMB2_open_free(&rqst[0]);
1757 	SMB2_query_info_free(&rqst[1]);
1758 	SMB2_close_free(&rqst[2]);
1759 	free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1760 	free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1761 	free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1762 	return rc;
1763 }
1764 
1765 static int
smb311_queryfs(const unsigned int xid,struct cifs_tcon * tcon,struct kstatfs * buf)1766 smb311_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
1767 	     struct kstatfs *buf)
1768 {
1769 	int rc;
1770 	__le16 srch_path = 0; /* Null - open root of share */
1771 	u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1772 	struct cifs_open_parms oparms;
1773 	struct cifs_fid fid;
1774 
1775 	if (!tcon->posix_extensions)
1776 		return smb2_queryfs(xid, tcon, buf);
1777 
1778 	oparms.tcon = tcon;
1779 	oparms.desired_access = FILE_READ_ATTRIBUTES;
1780 	oparms.disposition = FILE_OPEN;
1781 	oparms.create_options = 0;
1782 	oparms.fid = &fid;
1783 	oparms.reconnect = false;
1784 
1785 	rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
1786 	if (rc)
1787 		return rc;
1788 
1789 	rc = SMB311_posix_qfs_info(xid, tcon, fid.persistent_fid,
1790 				   fid.volatile_fid, buf);
1791 	buf->f_type = SMB2_MAGIC_NUMBER;
1792 	SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
1793 	return rc;
1794 }
1795 
1796 static bool
smb2_compare_fids(struct cifsFileInfo * ob1,struct cifsFileInfo * ob2)1797 smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
1798 {
1799 	return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
1800 	       ob1->fid.volatile_fid == ob2->fid.volatile_fid;
1801 }
1802 
1803 static int
smb2_mand_lock(const unsigned int xid,struct cifsFileInfo * cfile,__u64 offset,__u64 length,__u32 type,int lock,int unlock,bool wait)1804 smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
1805 	       __u64 length, __u32 type, int lock, int unlock, bool wait)
1806 {
1807 	if (unlock && !lock)
1808 		type = SMB2_LOCKFLAG_UNLOCK;
1809 	return SMB2_lock(xid, tlink_tcon(cfile->tlink),
1810 			 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
1811 			 current->tgid, length, offset, type, wait);
1812 }
1813 
1814 static void
smb2_get_lease_key(struct inode * inode,struct cifs_fid * fid)1815 smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
1816 {
1817 	memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
1818 }
1819 
1820 static void
smb2_set_lease_key(struct inode * inode,struct cifs_fid * fid)1821 smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
1822 {
1823 	memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
1824 }
1825 
1826 static void
smb2_new_lease_key(struct cifs_fid * fid)1827 smb2_new_lease_key(struct cifs_fid *fid)
1828 {
1829 	generate_random_uuid(fid->lease_key);
1830 }
1831 
1832 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)1833 smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
1834 		   const char *search_name,
1835 		   struct dfs_info3_param **target_nodes,
1836 		   unsigned int *num_of_nodes,
1837 		   const struct nls_table *nls_codepage, int remap)
1838 {
1839 	int rc;
1840 	__le16 *utf16_path = NULL;
1841 	int utf16_path_len = 0;
1842 	struct cifs_tcon *tcon;
1843 	struct fsctl_get_dfs_referral_req *dfs_req = NULL;
1844 	struct get_dfs_referral_rsp *dfs_rsp = NULL;
1845 	u32 dfs_req_size = 0, dfs_rsp_size = 0;
1846 
1847 	cifs_dbg(FYI, "smb2_get_dfs_refer path <%s>\n", search_name);
1848 
1849 	/*
1850 	 * Try to use the IPC tcon, otherwise just use any
1851 	 */
1852 	tcon = ses->tcon_ipc;
1853 	if (tcon == NULL) {
1854 		spin_lock(&cifs_tcp_ses_lock);
1855 		tcon = list_first_entry_or_null(&ses->tcon_list,
1856 						struct cifs_tcon,
1857 						tcon_list);
1858 		if (tcon)
1859 			tcon->tc_count++;
1860 		spin_unlock(&cifs_tcp_ses_lock);
1861 	}
1862 
1863 	if (tcon == NULL) {
1864 		cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
1865 			 ses);
1866 		rc = -ENOTCONN;
1867 		goto out;
1868 	}
1869 
1870 	utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
1871 					   &utf16_path_len,
1872 					   nls_codepage, remap);
1873 	if (!utf16_path) {
1874 		rc = -ENOMEM;
1875 		goto out;
1876 	}
1877 
1878 	dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
1879 	dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
1880 	if (!dfs_req) {
1881 		rc = -ENOMEM;
1882 		goto out;
1883 	}
1884 
1885 	/* Highest DFS referral version understood */
1886 	dfs_req->MaxReferralLevel = DFS_VERSION;
1887 
1888 	/* Path to resolve in an UTF-16 null-terminated string */
1889 	memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
1890 
1891 	do {
1892 		rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1893 				FSCTL_DFS_GET_REFERRALS,
1894 				true /* is_fsctl */,
1895 				(char *)dfs_req, dfs_req_size,
1896 				(char **)&dfs_rsp, &dfs_rsp_size);
1897 	} while (rc == -EAGAIN);
1898 
1899 	if (rc) {
1900 		if ((rc != -ENOENT) && (rc != -EOPNOTSUPP))
1901 			cifs_dbg(VFS, "ioctl error in smb2_get_dfs_refer rc=%d\n", rc);
1902 		goto out;
1903 	}
1904 
1905 	rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
1906 				 num_of_nodes, target_nodes,
1907 				 nls_codepage, remap, search_name,
1908 				 true /* is_unicode */);
1909 	if (rc) {
1910 		cifs_dbg(VFS, "parse error in smb2_get_dfs_refer rc=%d\n", rc);
1911 		goto out;
1912 	}
1913 
1914  out:
1915 	if (tcon && !tcon->ipc) {
1916 		/* ipc tcons are not refcounted */
1917 		spin_lock(&cifs_tcp_ses_lock);
1918 		tcon->tc_count--;
1919 		spin_unlock(&cifs_tcp_ses_lock);
1920 	}
1921 	kfree(utf16_path);
1922 	kfree(dfs_req);
1923 	kfree(dfs_rsp);
1924 	return rc;
1925 }
1926 #define SMB2_SYMLINK_STRUCT_SIZE \
1927 	(sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
1928 
1929 static int
smb2_query_symlink(const unsigned int xid,struct cifs_tcon * tcon,const char * full_path,char ** target_path,struct cifs_sb_info * cifs_sb)1930 smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
1931 		   const char *full_path, char **target_path,
1932 		   struct cifs_sb_info *cifs_sb)
1933 {
1934 	int rc;
1935 	__le16 *utf16_path;
1936 	__u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1937 	struct cifs_open_parms oparms;
1938 	struct cifs_fid fid;
1939 	struct kvec err_iov = {NULL, 0};
1940 	struct smb2_err_rsp *err_buf = NULL;
1941 	int resp_buftype;
1942 	struct smb2_symlink_err_rsp *symlink;
1943 	unsigned int sub_len;
1944 	unsigned int sub_offset;
1945 	unsigned int print_len;
1946 	unsigned int print_offset;
1947 
1948 	cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
1949 
1950 	utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
1951 	if (!utf16_path)
1952 		return -ENOMEM;
1953 
1954 	oparms.tcon = tcon;
1955 	oparms.desired_access = FILE_READ_ATTRIBUTES;
1956 	oparms.disposition = FILE_OPEN;
1957 	if (backup_cred(cifs_sb))
1958 		oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1959 	else
1960 		oparms.create_options = 0;
1961 	oparms.fid = &fid;
1962 	oparms.reconnect = false;
1963 
1964 	rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, &err_iov,
1965 		       &resp_buftype);
1966 	if (!rc)
1967 		SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
1968 	if (!rc || !err_iov.iov_base) {
1969 		rc = -ENOENT;
1970 		goto free_path;
1971 	}
1972 
1973 	err_buf = err_iov.iov_base;
1974 	if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
1975 	    err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE) {
1976 		rc = -ENOENT;
1977 		goto querty_exit;
1978 	}
1979 
1980 	/* open must fail on symlink - reset rc */
1981 	rc = 0;
1982 	symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
1983 	sub_len = le16_to_cpu(symlink->SubstituteNameLength);
1984 	sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
1985 	print_len = le16_to_cpu(symlink->PrintNameLength);
1986 	print_offset = le16_to_cpu(symlink->PrintNameOffset);
1987 
1988 	if (err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
1989 		rc = -ENOENT;
1990 		goto querty_exit;
1991 	}
1992 
1993 	if (err_iov.iov_len <
1994 	    SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
1995 		rc = -ENOENT;
1996 		goto querty_exit;
1997 	}
1998 
1999 	*target_path = cifs_strndup_from_utf16(
2000 				(char *)symlink->PathBuffer + sub_offset,
2001 				sub_len, true, cifs_sb->local_nls);
2002 	if (!(*target_path)) {
2003 		rc = -ENOMEM;
2004 		goto querty_exit;
2005 	}
2006 	convert_delimiter(*target_path, '/');
2007 	cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2008 
2009  querty_exit:
2010 	free_rsp_buf(resp_buftype, err_buf);
2011  free_path:
2012 	kfree(utf16_path);
2013 	return rc;
2014 }
2015 
2016 #ifdef CONFIG_CIFS_ACL
2017 static struct cifs_ntsd *
get_smb2_acl_by_fid(struct cifs_sb_info * cifs_sb,const struct cifs_fid * cifsfid,u32 * pacllen)2018 get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
2019 		const struct cifs_fid *cifsfid, u32 *pacllen)
2020 {
2021 	struct cifs_ntsd *pntsd = NULL;
2022 	unsigned int xid;
2023 	int rc = -EOPNOTSUPP;
2024 	struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2025 
2026 	if (IS_ERR(tlink))
2027 		return ERR_CAST(tlink);
2028 
2029 	xid = get_xid();
2030 	cifs_dbg(FYI, "trying to get acl\n");
2031 
2032 	rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
2033 			    cifsfid->volatile_fid, (void **)&pntsd, pacllen);
2034 	free_xid(xid);
2035 
2036 	cifs_put_tlink(tlink);
2037 
2038 	cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
2039 	if (rc)
2040 		return ERR_PTR(rc);
2041 	return pntsd;
2042 
2043 }
2044 
2045 static struct cifs_ntsd *
get_smb2_acl_by_path(struct cifs_sb_info * cifs_sb,const char * path,u32 * pacllen)2046 get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
2047 		const char *path, u32 *pacllen)
2048 {
2049 	struct cifs_ntsd *pntsd = NULL;
2050 	u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2051 	unsigned int xid;
2052 	int rc;
2053 	struct cifs_tcon *tcon;
2054 	struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2055 	struct cifs_fid fid;
2056 	struct cifs_open_parms oparms;
2057 	__le16 *utf16_path;
2058 
2059 	cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
2060 	if (IS_ERR(tlink))
2061 		return ERR_CAST(tlink);
2062 
2063 	tcon = tlink_tcon(tlink);
2064 	xid = get_xid();
2065 
2066 	if (backup_cred(cifs_sb))
2067 		oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2068 	else
2069 		oparms.create_options = 0;
2070 
2071 	utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2072 	if (!utf16_path) {
2073 		rc = -ENOMEM;
2074 		free_xid(xid);
2075 		return ERR_PTR(rc);
2076 	}
2077 
2078 	oparms.tcon = tcon;
2079 	oparms.desired_access = READ_CONTROL;
2080 	oparms.disposition = FILE_OPEN;
2081 	oparms.fid = &fid;
2082 	oparms.reconnect = false;
2083 
2084 	rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
2085 	kfree(utf16_path);
2086 	if (!rc) {
2087 		rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
2088 			    fid.volatile_fid, (void **)&pntsd, pacllen);
2089 		SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2090 	}
2091 
2092 	cifs_put_tlink(tlink);
2093 	free_xid(xid);
2094 
2095 	cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
2096 	if (rc)
2097 		return ERR_PTR(rc);
2098 	return pntsd;
2099 }
2100 
2101 #ifdef CONFIG_CIFS_ACL
2102 static int
set_smb2_acl(struct cifs_ntsd * pnntsd,__u32 acllen,struct inode * inode,const char * path,int aclflag)2103 set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
2104 		struct inode *inode, const char *path, int aclflag)
2105 {
2106 	u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2107 	unsigned int xid;
2108 	int rc, access_flags = 0;
2109 	struct cifs_tcon *tcon;
2110 	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2111 	struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2112 	struct cifs_fid fid;
2113 	struct cifs_open_parms oparms;
2114 	__le16 *utf16_path;
2115 
2116 	cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
2117 	if (IS_ERR(tlink))
2118 		return PTR_ERR(tlink);
2119 
2120 	tcon = tlink_tcon(tlink);
2121 	xid = get_xid();
2122 
2123 	if (backup_cred(cifs_sb))
2124 		oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2125 	else
2126 		oparms.create_options = 0;
2127 
2128 	if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
2129 		access_flags = WRITE_OWNER;
2130 	else
2131 		access_flags = WRITE_DAC;
2132 
2133 	utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2134 	if (!utf16_path) {
2135 		rc = -ENOMEM;
2136 		free_xid(xid);
2137 		return rc;
2138 	}
2139 
2140 	oparms.tcon = tcon;
2141 	oparms.desired_access = access_flags;
2142 	oparms.disposition = FILE_OPEN;
2143 	oparms.path = path;
2144 	oparms.fid = &fid;
2145 	oparms.reconnect = false;
2146 
2147 	rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
2148 	kfree(utf16_path);
2149 	if (!rc) {
2150 		rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
2151 			    fid.volatile_fid, pnntsd, acllen, aclflag);
2152 		SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2153 	}
2154 
2155 	cifs_put_tlink(tlink);
2156 	free_xid(xid);
2157 	return rc;
2158 }
2159 #endif /* CIFS_ACL */
2160 
2161 /* Retrieve an ACL from the server */
2162 static struct cifs_ntsd *
get_smb2_acl(struct cifs_sb_info * cifs_sb,struct inode * inode,const char * path,u32 * pacllen)2163 get_smb2_acl(struct cifs_sb_info *cifs_sb,
2164 				      struct inode *inode, const char *path,
2165 				      u32 *pacllen)
2166 {
2167 	struct cifs_ntsd *pntsd = NULL;
2168 	struct cifsFileInfo *open_file = NULL;
2169 
2170 	if (inode)
2171 		open_file = find_readable_file(CIFS_I(inode), true);
2172 	if (!open_file)
2173 		return get_smb2_acl_by_path(cifs_sb, path, pacllen);
2174 
2175 	pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
2176 	cifsFileInfo_put(open_file);
2177 	return pntsd;
2178 }
2179 #endif
2180 
smb3_zero_range(struct file * file,struct cifs_tcon * tcon,loff_t offset,loff_t len,bool keep_size)2181 static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
2182 			    loff_t offset, loff_t len, bool keep_size)
2183 {
2184 	struct inode *inode;
2185 	struct cifsInodeInfo *cifsi;
2186 	struct cifsFileInfo *cfile = file->private_data;
2187 	struct file_zero_data_information fsctl_buf;
2188 	long rc;
2189 	unsigned int xid;
2190 
2191 	xid = get_xid();
2192 
2193 	inode = d_inode(cfile->dentry);
2194 	cifsi = CIFS_I(inode);
2195 
2196 	/*
2197 	 * We zero the range through ioctl, so we need remove the page caches
2198 	 * first, otherwise the data may be inconsistent with the server.
2199 	 */
2200 	truncate_pagecache_range(inode, offset, offset + len - 1);
2201 
2202 	/* if file not oplocked can't be sure whether asking to extend size */
2203 	if (!CIFS_CACHE_READ(cifsi))
2204 		if (keep_size == false) {
2205 			rc = -EOPNOTSUPP;
2206 			free_xid(xid);
2207 			return rc;
2208 		}
2209 
2210 	/*
2211 	 * Must check if file sparse since fallocate -z (zero range) assumes
2212 	 * non-sparse allocation
2213 	 */
2214 	if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
2215 		rc = -EOPNOTSUPP;
2216 		free_xid(xid);
2217 		return rc;
2218 	}
2219 
2220 	/*
2221 	 * need to make sure we are not asked to extend the file since the SMB3
2222 	 * fsctl does not change the file size. In the future we could change
2223 	 * this to zero the first part of the range then set the file size
2224 	 * which for a non sparse file would zero the newly extended range
2225 	 */
2226 	if (keep_size == false)
2227 		if (i_size_read(inode) < offset + len) {
2228 			rc = -EOPNOTSUPP;
2229 			free_xid(xid);
2230 			return rc;
2231 		}
2232 
2233 	cifs_dbg(FYI, "offset %lld len %lld", offset, len);
2234 
2235 	fsctl_buf.FileOffset = cpu_to_le64(offset);
2236 	fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
2237 
2238 	rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2239 			cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
2240 			true /* is_fctl */, (char *)&fsctl_buf,
2241 			sizeof(struct file_zero_data_information), NULL, NULL);
2242 	free_xid(xid);
2243 	return rc;
2244 }
2245 
smb3_punch_hole(struct file * file,struct cifs_tcon * tcon,loff_t offset,loff_t len)2246 static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
2247 			    loff_t offset, loff_t len)
2248 {
2249 	struct inode *inode;
2250 	struct cifsInodeInfo *cifsi;
2251 	struct cifsFileInfo *cfile = file->private_data;
2252 	struct file_zero_data_information fsctl_buf;
2253 	long rc;
2254 	unsigned int xid;
2255 	__u8 set_sparse = 1;
2256 
2257 	xid = get_xid();
2258 
2259 	inode = d_inode(cfile->dentry);
2260 	cifsi = CIFS_I(inode);
2261 
2262 	/* Need to make file sparse, if not already, before freeing range. */
2263 	/* Consider adding equivalent for compressed since it could also work */
2264 	if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
2265 		rc = -EOPNOTSUPP;
2266 		free_xid(xid);
2267 		return rc;
2268 	}
2269 
2270 	/*
2271 	 * We implement the punch hole through ioctl, so we need remove the page
2272 	 * caches first, otherwise the data may be inconsistent with the server.
2273 	 */
2274 	truncate_pagecache_range(inode, offset, offset + len - 1);
2275 
2276 	cifs_dbg(FYI, "offset %lld len %lld", offset, len);
2277 
2278 	fsctl_buf.FileOffset = cpu_to_le64(offset);
2279 	fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
2280 
2281 	rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2282 			cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
2283 			true /* is_fctl */, (char *)&fsctl_buf,
2284 			sizeof(struct file_zero_data_information), NULL, NULL);
2285 	free_xid(xid);
2286 	return rc;
2287 }
2288 
smb3_simple_falloc(struct file * file,struct cifs_tcon * tcon,loff_t off,loff_t len,bool keep_size)2289 static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
2290 			    loff_t off, loff_t len, bool keep_size)
2291 {
2292 	struct inode *inode;
2293 	struct cifsInodeInfo *cifsi;
2294 	struct cifsFileInfo *cfile = file->private_data;
2295 	long rc = -EOPNOTSUPP;
2296 	unsigned int xid;
2297 
2298 	xid = get_xid();
2299 
2300 	inode = d_inode(cfile->dentry);
2301 	cifsi = CIFS_I(inode);
2302 
2303 	/* if file not oplocked can't be sure whether asking to extend size */
2304 	if (!CIFS_CACHE_READ(cifsi))
2305 		if (keep_size == false) {
2306 			free_xid(xid);
2307 			return rc;
2308 		}
2309 
2310 	/*
2311 	 * Files are non-sparse by default so falloc may be a no-op
2312 	 * Must check if file sparse. If not sparse, and not extending
2313 	 * then no need to do anything since file already allocated
2314 	 */
2315 	if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
2316 		if (keep_size == true)
2317 			rc = 0;
2318 		/* check if extending file */
2319 		else if (i_size_read(inode) >= off + len)
2320 			/* not extending file and already not sparse */
2321 			rc = 0;
2322 		/* BB: in future add else clause to extend file */
2323 		else
2324 			rc = -EOPNOTSUPP;
2325 		free_xid(xid);
2326 		return rc;
2327 	}
2328 
2329 	if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
2330 		/*
2331 		 * Check if falloc starts within first few pages of file
2332 		 * and ends within a few pages of the end of file to
2333 		 * ensure that most of file is being forced to be
2334 		 * fallocated now. If so then setting whole file sparse
2335 		 * ie potentially making a few extra pages at the beginning
2336 		 * or end of the file non-sparse via set_sparse is harmless.
2337 		 */
2338 		if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
2339 			rc = -EOPNOTSUPP;
2340 			free_xid(xid);
2341 			return rc;
2342 		}
2343 
2344 		rc = smb2_set_sparse(xid, tcon, cfile, inode, false);
2345 	}
2346 	/* BB: else ... in future add code to extend file and set sparse */
2347 
2348 
2349 	free_xid(xid);
2350 	return rc;
2351 }
2352 
2353 
smb3_fallocate(struct file * file,struct cifs_tcon * tcon,int mode,loff_t off,loff_t len)2354 static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
2355 			   loff_t off, loff_t len)
2356 {
2357 	/* KEEP_SIZE already checked for by do_fallocate */
2358 	if (mode & FALLOC_FL_PUNCH_HOLE)
2359 		return smb3_punch_hole(file, tcon, off, len);
2360 	else if (mode & FALLOC_FL_ZERO_RANGE) {
2361 		if (mode & FALLOC_FL_KEEP_SIZE)
2362 			return smb3_zero_range(file, tcon, off, len, true);
2363 		return smb3_zero_range(file, tcon, off, len, false);
2364 	} else if (mode == FALLOC_FL_KEEP_SIZE)
2365 		return smb3_simple_falloc(file, tcon, off, len, true);
2366 	else if (mode == 0)
2367 		return smb3_simple_falloc(file, tcon, off, len, false);
2368 
2369 	return -EOPNOTSUPP;
2370 }
2371 
2372 static void
smb2_downgrade_oplock(struct TCP_Server_Info * server,struct cifsInodeInfo * cinode,__u32 oplock,unsigned int epoch,bool * purge_cache)2373 smb2_downgrade_oplock(struct TCP_Server_Info *server,
2374 		      struct cifsInodeInfo *cinode, __u32 oplock,
2375 		      unsigned int epoch, bool *purge_cache)
2376 {
2377 	server->ops->set_oplock_level(cinode, oplock, 0, NULL);
2378 }
2379 
2380 static void
2381 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
2382 		       unsigned int epoch, bool *purge_cache);
2383 
2384 static void
smb3_downgrade_oplock(struct TCP_Server_Info * server,struct cifsInodeInfo * cinode,__u32 oplock,unsigned int epoch,bool * purge_cache)2385 smb3_downgrade_oplock(struct TCP_Server_Info *server,
2386 		       struct cifsInodeInfo *cinode, __u32 oplock,
2387 		       unsigned int epoch, bool *purge_cache)
2388 {
2389 	unsigned int old_state = cinode->oplock;
2390 	unsigned int old_epoch = cinode->epoch;
2391 	unsigned int new_state;
2392 
2393 	if (epoch > old_epoch) {
2394 		smb21_set_oplock_level(cinode, oplock, 0, NULL);
2395 		cinode->epoch = epoch;
2396 	}
2397 
2398 	new_state = cinode->oplock;
2399 	*purge_cache = false;
2400 
2401 	if ((old_state & CIFS_CACHE_READ_FLG) != 0 &&
2402 	    (new_state & CIFS_CACHE_READ_FLG) == 0)
2403 		*purge_cache = true;
2404 	else if (old_state == new_state && (epoch - old_epoch > 1))
2405 		*purge_cache = true;
2406 }
2407 
2408 static void
smb2_set_oplock_level(struct cifsInodeInfo * cinode,__u32 oplock,unsigned int epoch,bool * purge_cache)2409 smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
2410 		      unsigned int epoch, bool *purge_cache)
2411 {
2412 	oplock &= 0xFF;
2413 	if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
2414 		return;
2415 	if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2416 		cinode->oplock = CIFS_CACHE_RHW_FLG;
2417 		cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
2418 			 &cinode->vfs_inode);
2419 	} else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
2420 		cinode->oplock = CIFS_CACHE_RW_FLG;
2421 		cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
2422 			 &cinode->vfs_inode);
2423 	} else if (oplock == SMB2_OPLOCK_LEVEL_II) {
2424 		cinode->oplock = CIFS_CACHE_READ_FLG;
2425 		cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
2426 			 &cinode->vfs_inode);
2427 	} else
2428 		cinode->oplock = 0;
2429 }
2430 
2431 static void
smb21_set_oplock_level(struct cifsInodeInfo * cinode,__u32 oplock,unsigned int epoch,bool * purge_cache)2432 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
2433 		       unsigned int epoch, bool *purge_cache)
2434 {
2435 	char message[5] = {0};
2436 	unsigned int new_oplock = 0;
2437 
2438 	oplock &= 0xFF;
2439 	if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
2440 		return;
2441 
2442 	/* Check if the server granted an oplock rather than a lease */
2443 	if (oplock & SMB2_OPLOCK_LEVEL_EXCLUSIVE)
2444 		return smb2_set_oplock_level(cinode, oplock, epoch,
2445 					     purge_cache);
2446 
2447 	if (oplock & SMB2_LEASE_READ_CACHING_HE) {
2448 		new_oplock |= CIFS_CACHE_READ_FLG;
2449 		strcat(message, "R");
2450 	}
2451 	if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
2452 		new_oplock |= CIFS_CACHE_HANDLE_FLG;
2453 		strcat(message, "H");
2454 	}
2455 	if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
2456 		new_oplock |= CIFS_CACHE_WRITE_FLG;
2457 		strcat(message, "W");
2458 	}
2459 	if (!new_oplock)
2460 		strncpy(message, "None", sizeof(message));
2461 
2462 	cinode->oplock = new_oplock;
2463 	cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
2464 		 &cinode->vfs_inode);
2465 }
2466 
2467 static void
smb3_set_oplock_level(struct cifsInodeInfo * cinode,__u32 oplock,unsigned int epoch,bool * purge_cache)2468 smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
2469 		      unsigned int epoch, bool *purge_cache)
2470 {
2471 	unsigned int old_oplock = cinode->oplock;
2472 
2473 	smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
2474 
2475 	if (purge_cache) {
2476 		*purge_cache = false;
2477 		if (old_oplock == CIFS_CACHE_READ_FLG) {
2478 			if (cinode->oplock == CIFS_CACHE_READ_FLG &&
2479 			    (epoch - cinode->epoch > 0))
2480 				*purge_cache = true;
2481 			else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
2482 				 (epoch - cinode->epoch > 1))
2483 				*purge_cache = true;
2484 			else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
2485 				 (epoch - cinode->epoch > 1))
2486 				*purge_cache = true;
2487 			else if (cinode->oplock == 0 &&
2488 				 (epoch - cinode->epoch > 0))
2489 				*purge_cache = true;
2490 		} else if (old_oplock == CIFS_CACHE_RH_FLG) {
2491 			if (cinode->oplock == CIFS_CACHE_RH_FLG &&
2492 			    (epoch - cinode->epoch > 0))
2493 				*purge_cache = true;
2494 			else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
2495 				 (epoch - cinode->epoch > 1))
2496 				*purge_cache = true;
2497 		}
2498 		cinode->epoch = epoch;
2499 	}
2500 }
2501 
2502 static bool
smb2_is_read_op(__u32 oplock)2503 smb2_is_read_op(__u32 oplock)
2504 {
2505 	return oplock == SMB2_OPLOCK_LEVEL_II;
2506 }
2507 
2508 static bool
smb21_is_read_op(__u32 oplock)2509 smb21_is_read_op(__u32 oplock)
2510 {
2511 	return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
2512 	       !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
2513 }
2514 
2515 static __le32
map_oplock_to_lease(u8 oplock)2516 map_oplock_to_lease(u8 oplock)
2517 {
2518 	if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
2519 		return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
2520 	else if (oplock == SMB2_OPLOCK_LEVEL_II)
2521 		return SMB2_LEASE_READ_CACHING;
2522 	else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
2523 		return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
2524 		       SMB2_LEASE_WRITE_CACHING;
2525 	return 0;
2526 }
2527 
2528 static char *
smb2_create_lease_buf(u8 * lease_key,u8 oplock)2529 smb2_create_lease_buf(u8 *lease_key, u8 oplock)
2530 {
2531 	struct create_lease *buf;
2532 
2533 	buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
2534 	if (!buf)
2535 		return NULL;
2536 
2537 	memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
2538 	buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
2539 
2540 	buf->ccontext.DataOffset = cpu_to_le16(offsetof
2541 					(struct create_lease, lcontext));
2542 	buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
2543 	buf->ccontext.NameOffset = cpu_to_le16(offsetof
2544 				(struct create_lease, Name));
2545 	buf->ccontext.NameLength = cpu_to_le16(4);
2546 	/* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
2547 	buf->Name[0] = 'R';
2548 	buf->Name[1] = 'q';
2549 	buf->Name[2] = 'L';
2550 	buf->Name[3] = 's';
2551 	return (char *)buf;
2552 }
2553 
2554 static char *
smb3_create_lease_buf(u8 * lease_key,u8 oplock)2555 smb3_create_lease_buf(u8 *lease_key, u8 oplock)
2556 {
2557 	struct create_lease_v2 *buf;
2558 
2559 	buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
2560 	if (!buf)
2561 		return NULL;
2562 
2563 	memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
2564 	buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
2565 
2566 	buf->ccontext.DataOffset = cpu_to_le16(offsetof
2567 					(struct create_lease_v2, lcontext));
2568 	buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
2569 	buf->ccontext.NameOffset = cpu_to_le16(offsetof
2570 				(struct create_lease_v2, Name));
2571 	buf->ccontext.NameLength = cpu_to_le16(4);
2572 	/* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
2573 	buf->Name[0] = 'R';
2574 	buf->Name[1] = 'q';
2575 	buf->Name[2] = 'L';
2576 	buf->Name[3] = 's';
2577 	return (char *)buf;
2578 }
2579 
2580 static __u8
smb2_parse_lease_buf(void * buf,unsigned int * epoch,char * lease_key)2581 smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
2582 {
2583 	struct create_lease *lc = (struct create_lease *)buf;
2584 
2585 	*epoch = 0; /* not used */
2586 	if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
2587 		return SMB2_OPLOCK_LEVEL_NOCHANGE;
2588 	return le32_to_cpu(lc->lcontext.LeaseState);
2589 }
2590 
2591 static __u8
smb3_parse_lease_buf(void * buf,unsigned int * epoch,char * lease_key)2592 smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
2593 {
2594 	struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
2595 
2596 	*epoch = le16_to_cpu(lc->lcontext.Epoch);
2597 	if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
2598 		return SMB2_OPLOCK_LEVEL_NOCHANGE;
2599 	if (lease_key)
2600 		memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
2601 	return le32_to_cpu(lc->lcontext.LeaseState);
2602 }
2603 
2604 static unsigned int
smb2_wp_retry_size(struct inode * inode)2605 smb2_wp_retry_size(struct inode *inode)
2606 {
2607 	return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
2608 		     SMB2_MAX_BUFFER_SIZE);
2609 }
2610 
2611 static bool
smb2_dir_needs_close(struct cifsFileInfo * cfile)2612 smb2_dir_needs_close(struct cifsFileInfo *cfile)
2613 {
2614 	return !cfile->invalidHandle;
2615 }
2616 
2617 static void
fill_transform_hdr(struct smb2_transform_hdr * tr_hdr,unsigned int orig_len,struct smb_rqst * old_rq)2618 fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
2619 		   struct smb_rqst *old_rq)
2620 {
2621 	struct smb2_sync_hdr *shdr =
2622 			(struct smb2_sync_hdr *)old_rq->rq_iov[0].iov_base;
2623 
2624 	memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
2625 	tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
2626 	tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
2627 	tr_hdr->Flags = cpu_to_le16(0x01);
2628 	get_random_bytes(&tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
2629 	memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
2630 }
2631 
2632 /* We can not use the normal sg_set_buf() as we will sometimes pass a
2633  * stack object as buf.
2634  */
smb2_sg_set_buf(struct scatterlist * sg,const void * buf,unsigned int buflen)2635 static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
2636 				   unsigned int buflen)
2637 {
2638 	void *addr;
2639 	/*
2640 	 * VMAP_STACK (at least) puts stack into the vmalloc address space
2641 	 */
2642 	if (is_vmalloc_addr(buf))
2643 		addr = vmalloc_to_page(buf);
2644 	else
2645 		addr = virt_to_page(buf);
2646 	sg_set_page(sg, addr, buflen, offset_in_page(buf));
2647 }
2648 
2649 /* Assumes the first rqst has a transform header as the first iov.
2650  * I.e.
2651  * rqst[0].rq_iov[0]  is transform header
2652  * rqst[0].rq_iov[1+] data to be encrypted/decrypted
2653  * rqst[1+].rq_iov[0+] data to be encrypted/decrypted
2654  */
2655 static struct scatterlist *
init_sg(int num_rqst,struct smb_rqst * rqst,u8 * sign)2656 init_sg(int num_rqst, struct smb_rqst *rqst, u8 *sign)
2657 {
2658 	unsigned int sg_len;
2659 	struct scatterlist *sg;
2660 	unsigned int i;
2661 	unsigned int j;
2662 	unsigned int idx = 0;
2663 	int skip;
2664 
2665 	sg_len = 1;
2666 	for (i = 0; i < num_rqst; i++)
2667 		sg_len += rqst[i].rq_nvec + rqst[i].rq_npages;
2668 
2669 	sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
2670 	if (!sg)
2671 		return NULL;
2672 
2673 	sg_init_table(sg, sg_len);
2674 	for (i = 0; i < num_rqst; i++) {
2675 		for (j = 0; j < rqst[i].rq_nvec; j++) {
2676 			/*
2677 			 * The first rqst has a transform header where the
2678 			 * first 20 bytes are not part of the encrypted blob
2679 			 */
2680 			skip = (i == 0) && (j == 0) ? 20 : 0;
2681 			smb2_sg_set_buf(&sg[idx++],
2682 					rqst[i].rq_iov[j].iov_base + skip,
2683 					rqst[i].rq_iov[j].iov_len - skip);
2684 		}
2685 
2686 		for (j = 0; j < rqst[i].rq_npages; j++) {
2687 			unsigned int len, offset;
2688 
2689 			rqst_page_get_length(&rqst[i], j, &len, &offset);
2690 			sg_set_page(&sg[idx++], rqst[i].rq_pages[j], len, offset);
2691 		}
2692 	}
2693 	smb2_sg_set_buf(&sg[idx], sign, SMB2_SIGNATURE_SIZE);
2694 	return sg;
2695 }
2696 
2697 static int
smb2_get_enc_key(struct TCP_Server_Info * server,__u64 ses_id,int enc,u8 * key)2698 smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
2699 {
2700 	struct cifs_ses *ses;
2701 	u8 *ses_enc_key;
2702 
2703 	spin_lock(&cifs_tcp_ses_lock);
2704 	list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2705 		if (ses->Suid != ses_id)
2706 			continue;
2707 		ses_enc_key = enc ? ses->smb3encryptionkey :
2708 							ses->smb3decryptionkey;
2709 		memcpy(key, ses_enc_key, SMB3_SIGN_KEY_SIZE);
2710 		spin_unlock(&cifs_tcp_ses_lock);
2711 		return 0;
2712 	}
2713 	spin_unlock(&cifs_tcp_ses_lock);
2714 
2715 	return -EAGAIN;
2716 }
2717 /*
2718  * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
2719  * iov[0]   - transform header (associate data),
2720  * iov[1-N] - SMB2 header and pages - data to encrypt.
2721  * On success return encrypted data in iov[1-N] and pages, leave iov[0]
2722  * untouched.
2723  */
2724 static int
crypt_message(struct TCP_Server_Info * server,int num_rqst,struct smb_rqst * rqst,int enc)2725 crypt_message(struct TCP_Server_Info *server, int num_rqst,
2726 	      struct smb_rqst *rqst, int enc)
2727 {
2728 	struct smb2_transform_hdr *tr_hdr =
2729 		(struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
2730 	unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
2731 	int rc = 0;
2732 	struct scatterlist *sg;
2733 	u8 sign[SMB2_SIGNATURE_SIZE] = {};
2734 	u8 key[SMB3_SIGN_KEY_SIZE];
2735 	struct aead_request *req;
2736 	char *iv;
2737 	unsigned int iv_len;
2738 	DECLARE_CRYPTO_WAIT(wait);
2739 	struct crypto_aead *tfm;
2740 	unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
2741 
2742 	rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
2743 	if (rc) {
2744 		cifs_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
2745 			 enc ? "en" : "de");
2746 		return rc;
2747 	}
2748 
2749 	rc = smb3_crypto_aead_allocate(server);
2750 	if (rc) {
2751 		cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
2752 		return rc;
2753 	}
2754 
2755 	tfm = enc ? server->secmech.ccmaesencrypt :
2756 						server->secmech.ccmaesdecrypt;
2757 	rc = crypto_aead_setkey(tfm, key, SMB3_SIGN_KEY_SIZE);
2758 	if (rc) {
2759 		cifs_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
2760 		return rc;
2761 	}
2762 
2763 	rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
2764 	if (rc) {
2765 		cifs_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
2766 		return rc;
2767 	}
2768 
2769 	req = aead_request_alloc(tfm, GFP_KERNEL);
2770 	if (!req) {
2771 		cifs_dbg(VFS, "%s: Failed to alloc aead request", __func__);
2772 		return -ENOMEM;
2773 	}
2774 
2775 	if (!enc) {
2776 		memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
2777 		crypt_len += SMB2_SIGNATURE_SIZE;
2778 	}
2779 
2780 	sg = init_sg(num_rqst, rqst, sign);
2781 	if (!sg) {
2782 		cifs_dbg(VFS, "%s: Failed to init sg", __func__);
2783 		rc = -ENOMEM;
2784 		goto free_req;
2785 	}
2786 
2787 	iv_len = crypto_aead_ivsize(tfm);
2788 	iv = kzalloc(iv_len, GFP_KERNEL);
2789 	if (!iv) {
2790 		cifs_dbg(VFS, "%s: Failed to alloc IV", __func__);
2791 		rc = -ENOMEM;
2792 		goto free_sg;
2793 	}
2794 	iv[0] = 3;
2795 	memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
2796 
2797 	aead_request_set_crypt(req, sg, sg, crypt_len, iv);
2798 	aead_request_set_ad(req, assoc_data_len);
2799 
2800 	aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
2801 				  crypto_req_done, &wait);
2802 
2803 	rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
2804 				: crypto_aead_decrypt(req), &wait);
2805 
2806 	if (!rc && enc)
2807 		memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
2808 
2809 	kfree(iv);
2810 free_sg:
2811 	kfree(sg);
2812 free_req:
2813 	kfree(req);
2814 	return rc;
2815 }
2816 
2817 void
smb3_free_compound_rqst(int num_rqst,struct smb_rqst * rqst)2818 smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
2819 {
2820 	int i, j;
2821 
2822 	for (i = 0; i < num_rqst; i++) {
2823 		if (rqst[i].rq_pages) {
2824 			for (j = rqst[i].rq_npages - 1; j >= 0; j--)
2825 				put_page(rqst[i].rq_pages[j]);
2826 			kfree(rqst[i].rq_pages);
2827 		}
2828 	}
2829 }
2830 
2831 /*
2832  * This function will initialize new_rq and encrypt the content.
2833  * The first entry, new_rq[0], only contains a single iov which contains
2834  * a smb2_transform_hdr and is pre-allocated by the caller.
2835  * This function then populates new_rq[1+] with the content from olq_rq[0+].
2836  *
2837  * The end result is an array of smb_rqst structures where the first structure
2838  * only contains a single iov for the transform header which we then can pass
2839  * to crypt_message().
2840  *
2841  * new_rq[0].rq_iov[0] :  smb2_transform_hdr pre-allocated by the caller
2842  * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
2843  */
2844 static int
smb3_init_transform_rq(struct TCP_Server_Info * server,int num_rqst,struct smb_rqst * new_rq,struct smb_rqst * old_rq)2845 smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
2846 		       struct smb_rqst *new_rq, struct smb_rqst *old_rq)
2847 {
2848 	struct page **pages;
2849 	struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
2850 	unsigned int npages;
2851 	unsigned int orig_len = 0;
2852 	int i, j;
2853 	int rc = -ENOMEM;
2854 
2855 	for (i = 1; i < num_rqst; i++) {
2856 		npages = old_rq[i - 1].rq_npages;
2857 		pages = kmalloc_array(npages, sizeof(struct page *),
2858 				      GFP_KERNEL);
2859 		if (!pages)
2860 			goto err_free;
2861 
2862 		new_rq[i].rq_pages = pages;
2863 		new_rq[i].rq_npages = npages;
2864 		new_rq[i].rq_offset = old_rq[i - 1].rq_offset;
2865 		new_rq[i].rq_pagesz = old_rq[i - 1].rq_pagesz;
2866 		new_rq[i].rq_tailsz = old_rq[i - 1].rq_tailsz;
2867 		new_rq[i].rq_iov = old_rq[i - 1].rq_iov;
2868 		new_rq[i].rq_nvec = old_rq[i - 1].rq_nvec;
2869 
2870 		orig_len += smb_rqst_len(server, &old_rq[i - 1]);
2871 
2872 		for (j = 0; j < npages; j++) {
2873 			pages[j] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
2874 			if (!pages[j])
2875 				goto err_free;
2876 		}
2877 
2878 		/* copy pages form the old */
2879 		for (j = 0; j < npages; j++) {
2880 			char *dst, *src;
2881 			unsigned int offset, len;
2882 
2883 			rqst_page_get_length(&new_rq[i], j, &len, &offset);
2884 
2885 			dst = (char *) kmap(new_rq[i].rq_pages[j]) + offset;
2886 			src = (char *) kmap(old_rq[i - 1].rq_pages[j]) + offset;
2887 
2888 			memcpy(dst, src, len);
2889 			kunmap(new_rq[i].rq_pages[j]);
2890 			kunmap(old_rq[i - 1].rq_pages[j]);
2891 		}
2892 	}
2893 
2894 	/* fill the 1st iov with a transform header */
2895 	fill_transform_hdr(tr_hdr, orig_len, old_rq);
2896 
2897 	rc = crypt_message(server, num_rqst, new_rq, 1);
2898 	cifs_dbg(FYI, "encrypt message returned %d", rc);
2899 	if (rc)
2900 		goto err_free;
2901 
2902 	return rc;
2903 
2904 err_free:
2905 	smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
2906 	return rc;
2907 }
2908 
2909 static int
smb3_is_transform_hdr(void * buf)2910 smb3_is_transform_hdr(void *buf)
2911 {
2912 	struct smb2_transform_hdr *trhdr = buf;
2913 
2914 	return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
2915 }
2916 
2917 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)2918 decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
2919 		 unsigned int buf_data_size, struct page **pages,
2920 		 unsigned int npages, unsigned int page_data_size)
2921 {
2922 	struct kvec iov[2];
2923 	struct smb_rqst rqst = {NULL};
2924 	int rc;
2925 
2926 	iov[0].iov_base = buf;
2927 	iov[0].iov_len = sizeof(struct smb2_transform_hdr);
2928 	iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
2929 	iov[1].iov_len = buf_data_size;
2930 
2931 	rqst.rq_iov = iov;
2932 	rqst.rq_nvec = 2;
2933 	rqst.rq_pages = pages;
2934 	rqst.rq_npages = npages;
2935 	rqst.rq_pagesz = PAGE_SIZE;
2936 	rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
2937 
2938 	rc = crypt_message(server, 1, &rqst, 0);
2939 	cifs_dbg(FYI, "decrypt message returned %d\n", rc);
2940 
2941 	if (rc)
2942 		return rc;
2943 
2944 	memmove(buf, iov[1].iov_base, buf_data_size);
2945 
2946 	server->total_read = buf_data_size + page_data_size;
2947 
2948 	return rc;
2949 }
2950 
2951 static int
read_data_into_pages(struct TCP_Server_Info * server,struct page ** pages,unsigned int npages,unsigned int len)2952 read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
2953 		     unsigned int npages, unsigned int len)
2954 {
2955 	int i;
2956 	int length;
2957 
2958 	for (i = 0; i < npages; i++) {
2959 		struct page *page = pages[i];
2960 		size_t n;
2961 
2962 		n = len;
2963 		if (len >= PAGE_SIZE) {
2964 			/* enough data to fill the page */
2965 			n = PAGE_SIZE;
2966 			len -= n;
2967 		} else {
2968 			zero_user(page, len, PAGE_SIZE - len);
2969 			len = 0;
2970 		}
2971 		length = cifs_read_page_from_socket(server, page, 0, n);
2972 		if (length < 0)
2973 			return length;
2974 		server->total_read += length;
2975 	}
2976 
2977 	return 0;
2978 }
2979 
2980 static int
init_read_bvec(struct page ** pages,unsigned int npages,unsigned int data_size,unsigned int cur_off,struct bio_vec ** page_vec)2981 init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
2982 	       unsigned int cur_off, struct bio_vec **page_vec)
2983 {
2984 	struct bio_vec *bvec;
2985 	int i;
2986 
2987 	bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
2988 	if (!bvec)
2989 		return -ENOMEM;
2990 
2991 	for (i = 0; i < npages; i++) {
2992 		bvec[i].bv_page = pages[i];
2993 		bvec[i].bv_offset = (i == 0) ? cur_off : 0;
2994 		bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
2995 		data_size -= bvec[i].bv_len;
2996 	}
2997 
2998 	if (data_size != 0) {
2999 		cifs_dbg(VFS, "%s: something went wrong\n", __func__);
3000 		kfree(bvec);
3001 		return -EIO;
3002 	}
3003 
3004 	*page_vec = bvec;
3005 	return 0;
3006 }
3007 
3008 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)3009 handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
3010 		 char *buf, unsigned int buf_len, struct page **pages,
3011 		 unsigned int npages, unsigned int page_data_size)
3012 {
3013 	unsigned int data_offset;
3014 	unsigned int data_len;
3015 	unsigned int cur_off;
3016 	unsigned int cur_page_idx;
3017 	unsigned int pad_len;
3018 	struct cifs_readdata *rdata = mid->callback_data;
3019 	struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
3020 	struct bio_vec *bvec = NULL;
3021 	struct iov_iter iter;
3022 	struct kvec iov;
3023 	int length;
3024 	bool use_rdma_mr = false;
3025 
3026 	if (shdr->Command != SMB2_READ) {
3027 		cifs_dbg(VFS, "only big read responses are supported\n");
3028 		return -ENOTSUPP;
3029 	}
3030 
3031 	if (server->ops->is_session_expired &&
3032 	    server->ops->is_session_expired(buf)) {
3033 		cifs_reconnect(server);
3034 		wake_up(&server->response_q);
3035 		return -1;
3036 	}
3037 
3038 	if (server->ops->is_status_pending &&
3039 			server->ops->is_status_pending(buf, server, 0))
3040 		return -1;
3041 
3042 	/* set up first two iov to get credits */
3043 	rdata->iov[0].iov_base = buf;
3044 	rdata->iov[0].iov_len = 0;
3045 	rdata->iov[1].iov_base = buf;
3046 	rdata->iov[1].iov_len =
3047 		min_t(unsigned int, buf_len, server->vals->read_rsp_size);
3048 	cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
3049 		 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
3050 	cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
3051 		 rdata->iov[1].iov_base, rdata->iov[1].iov_len);
3052 
3053 	rdata->result = server->ops->map_error(buf, true);
3054 	if (rdata->result != 0) {
3055 		cifs_dbg(FYI, "%s: server returned error %d\n",
3056 			 __func__, rdata->result);
3057 		/* normal error on read response */
3058 		dequeue_mid(mid, false);
3059 		return 0;
3060 	}
3061 
3062 	data_offset = server->ops->read_data_offset(buf);
3063 #ifdef CONFIG_CIFS_SMB_DIRECT
3064 	use_rdma_mr = rdata->mr;
3065 #endif
3066 	data_len = server->ops->read_data_length(buf, use_rdma_mr);
3067 
3068 	if (data_offset < server->vals->read_rsp_size) {
3069 		/*
3070 		 * win2k8 sometimes sends an offset of 0 when the read
3071 		 * is beyond the EOF. Treat it as if the data starts just after
3072 		 * the header.
3073 		 */
3074 		cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
3075 			 __func__, data_offset);
3076 		data_offset = server->vals->read_rsp_size;
3077 	} else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
3078 		/* data_offset is beyond the end of smallbuf */
3079 		cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
3080 			 __func__, data_offset);
3081 		rdata->result = -EIO;
3082 		dequeue_mid(mid, rdata->result);
3083 		return 0;
3084 	}
3085 
3086 	pad_len = data_offset - server->vals->read_rsp_size;
3087 
3088 	if (buf_len <= data_offset) {
3089 		/* read response payload is in pages */
3090 		cur_page_idx = pad_len / PAGE_SIZE;
3091 		cur_off = pad_len % PAGE_SIZE;
3092 
3093 		if (cur_page_idx != 0) {
3094 			/* data offset is beyond the 1st page of response */
3095 			cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
3096 				 __func__, data_offset);
3097 			rdata->result = -EIO;
3098 			dequeue_mid(mid, rdata->result);
3099 			return 0;
3100 		}
3101 
3102 		if (data_len > page_data_size - pad_len) {
3103 			/* data_len is corrupt -- discard frame */
3104 			rdata->result = -EIO;
3105 			dequeue_mid(mid, rdata->result);
3106 			return 0;
3107 		}
3108 
3109 		rdata->result = init_read_bvec(pages, npages, page_data_size,
3110 					       cur_off, &bvec);
3111 		if (rdata->result != 0) {
3112 			dequeue_mid(mid, rdata->result);
3113 			return 0;
3114 		}
3115 
3116 		iov_iter_bvec(&iter, WRITE | ITER_BVEC, bvec, npages, data_len);
3117 	} else if (buf_len >= data_offset + data_len) {
3118 		/* read response payload is in buf */
3119 		WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
3120 		iov.iov_base = buf + data_offset;
3121 		iov.iov_len = data_len;
3122 		iov_iter_kvec(&iter, WRITE | ITER_KVEC, &iov, 1, data_len);
3123 	} else {
3124 		/* read response payload cannot be in both buf and pages */
3125 		WARN_ONCE(1, "buf can not contain only a part of read data");
3126 		rdata->result = -EIO;
3127 		dequeue_mid(mid, rdata->result);
3128 		return 0;
3129 	}
3130 
3131 	length = rdata->copy_into_pages(server, rdata, &iter);
3132 
3133 	kfree(bvec);
3134 
3135 	if (length < 0)
3136 		return length;
3137 
3138 	dequeue_mid(mid, false);
3139 	return length;
3140 }
3141 
3142 static int
receive_encrypted_read(struct TCP_Server_Info * server,struct mid_q_entry ** mid)3143 receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid)
3144 {
3145 	char *buf = server->smallbuf;
3146 	struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
3147 	unsigned int npages;
3148 	struct page **pages;
3149 	unsigned int len;
3150 	unsigned int buflen = server->pdu_size;
3151 	int rc;
3152 	int i = 0;
3153 
3154 	len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
3155 		sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
3156 
3157 	rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
3158 	if (rc < 0)
3159 		return rc;
3160 	server->total_read += rc;
3161 
3162 	len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
3163 		server->vals->read_rsp_size;
3164 	npages = DIV_ROUND_UP(len, PAGE_SIZE);
3165 
3166 	pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
3167 	if (!pages) {
3168 		rc = -ENOMEM;
3169 		goto discard_data;
3170 	}
3171 
3172 	for (; i < npages; i++) {
3173 		pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3174 		if (!pages[i]) {
3175 			rc = -ENOMEM;
3176 			goto discard_data;
3177 		}
3178 	}
3179 
3180 	/* read read data into pages */
3181 	rc = read_data_into_pages(server, pages, npages, len);
3182 	if (rc)
3183 		goto free_pages;
3184 
3185 	rc = cifs_discard_remaining_data(server);
3186 	if (rc)
3187 		goto free_pages;
3188 
3189 	rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
3190 			      pages, npages, len);
3191 	if (rc)
3192 		goto free_pages;
3193 
3194 	*mid = smb2_find_mid(server, buf);
3195 	if (*mid == NULL)
3196 		cifs_dbg(FYI, "mid not found\n");
3197 	else {
3198 		cifs_dbg(FYI, "mid found\n");
3199 		(*mid)->decrypted = true;
3200 		rc = handle_read_data(server, *mid, buf,
3201 				      server->vals->read_rsp_size,
3202 				      pages, npages, len);
3203 	}
3204 
3205 free_pages:
3206 	for (i = i - 1; i >= 0; i--)
3207 		put_page(pages[i]);
3208 	kfree(pages);
3209 	return rc;
3210 discard_data:
3211 	cifs_discard_remaining_data(server);
3212 	goto free_pages;
3213 }
3214 
3215 static int
receive_encrypted_standard(struct TCP_Server_Info * server,struct mid_q_entry ** mids,char ** bufs,int * num_mids)3216 receive_encrypted_standard(struct TCP_Server_Info *server,
3217 			   struct mid_q_entry **mids, char **bufs,
3218 			   int *num_mids)
3219 {
3220 	int ret, length;
3221 	char *buf = server->smallbuf;
3222 	struct smb2_sync_hdr *shdr;
3223 	unsigned int pdu_length = server->pdu_size;
3224 	unsigned int buf_size;
3225 	struct mid_q_entry *mid_entry;
3226 	int next_is_large;
3227 	char *next_buffer = NULL;
3228 
3229 	*num_mids = 0;
3230 
3231 	/* switch to large buffer if too big for a small one */
3232 	if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
3233 		server->large_buf = true;
3234 		memcpy(server->bigbuf, buf, server->total_read);
3235 		buf = server->bigbuf;
3236 	}
3237 
3238 	/* now read the rest */
3239 	length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
3240 				pdu_length - HEADER_SIZE(server) + 1);
3241 	if (length < 0)
3242 		return length;
3243 	server->total_read += length;
3244 
3245 	buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
3246 	length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0);
3247 	if (length)
3248 		return length;
3249 
3250 	next_is_large = server->large_buf;
3251 one_more:
3252 	shdr = (struct smb2_sync_hdr *)buf;
3253 	if (shdr->NextCommand) {
3254 		if (next_is_large)
3255 			next_buffer = (char *)cifs_buf_get();
3256 		else
3257 			next_buffer = (char *)cifs_small_buf_get();
3258 		memcpy(next_buffer,
3259 		       buf + le32_to_cpu(shdr->NextCommand),
3260 		       pdu_length - le32_to_cpu(shdr->NextCommand));
3261 	}
3262 
3263 	mid_entry = smb2_find_mid(server, buf);
3264 	if (mid_entry == NULL)
3265 		cifs_dbg(FYI, "mid not found\n");
3266 	else {
3267 		cifs_dbg(FYI, "mid found\n");
3268 		mid_entry->decrypted = true;
3269 		mid_entry->resp_buf_size = server->pdu_size;
3270 	}
3271 
3272 	if (*num_mids >= MAX_COMPOUND) {
3273 		cifs_dbg(VFS, "too many PDUs in compound\n");
3274 		return -1;
3275 	}
3276 	bufs[*num_mids] = buf;
3277 	mids[(*num_mids)++] = mid_entry;
3278 
3279 	if (mid_entry && mid_entry->handle)
3280 		ret = mid_entry->handle(server, mid_entry);
3281 	else
3282 		ret = cifs_handle_standard(server, mid_entry);
3283 
3284 	if (ret == 0 && shdr->NextCommand) {
3285 		pdu_length -= le32_to_cpu(shdr->NextCommand);
3286 		server->large_buf = next_is_large;
3287 		if (next_is_large)
3288 			server->bigbuf = buf = next_buffer;
3289 		else
3290 			server->smallbuf = buf = next_buffer;
3291 		goto one_more;
3292 	} else if (ret != 0) {
3293 		/*
3294 		 * ret != 0 here means that we didn't get to handle_mid() thus
3295 		 * server->smallbuf and server->bigbuf are still valid. We need
3296 		 * to free next_buffer because it is not going to be used
3297 		 * anywhere.
3298 		 */
3299 		if (next_is_large)
3300 			free_rsp_buf(CIFS_LARGE_BUFFER, next_buffer);
3301 		else
3302 			free_rsp_buf(CIFS_SMALL_BUFFER, next_buffer);
3303 	}
3304 
3305 	return ret;
3306 }
3307 
3308 static int
smb3_receive_transform(struct TCP_Server_Info * server,struct mid_q_entry ** mids,char ** bufs,int * num_mids)3309 smb3_receive_transform(struct TCP_Server_Info *server,
3310 		       struct mid_q_entry **mids, char **bufs, int *num_mids)
3311 {
3312 	char *buf = server->smallbuf;
3313 	unsigned int pdu_length = server->pdu_size;
3314 	struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
3315 	unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
3316 
3317 	if (pdu_length < sizeof(struct smb2_transform_hdr) +
3318 						sizeof(struct smb2_sync_hdr)) {
3319 		cifs_dbg(VFS, "Transform message is too small (%u)\n",
3320 			 pdu_length);
3321 		cifs_reconnect(server);
3322 		wake_up(&server->response_q);
3323 		return -ECONNABORTED;
3324 	}
3325 
3326 	if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
3327 		cifs_dbg(VFS, "Transform message is broken\n");
3328 		cifs_reconnect(server);
3329 		wake_up(&server->response_q);
3330 		return -ECONNABORTED;
3331 	}
3332 
3333 	/* TODO: add support for compounds containing READ. */
3334 	if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server)) {
3335 		*num_mids = 1;
3336 		return receive_encrypted_read(server, &mids[0]);
3337 	}
3338 
3339 	return receive_encrypted_standard(server, mids, bufs, num_mids);
3340 }
3341 
3342 int
smb3_handle_read_data(struct TCP_Server_Info * server,struct mid_q_entry * mid)3343 smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
3344 {
3345 	char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
3346 
3347 	return handle_read_data(server, mid, buf, server->pdu_size,
3348 				NULL, 0, 0);
3349 }
3350 
3351 static int
smb2_next_header(char * buf)3352 smb2_next_header(char *buf)
3353 {
3354 	struct smb2_sync_hdr *hdr = (struct smb2_sync_hdr *)buf;
3355 	struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
3356 
3357 	if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM)
3358 		return sizeof(struct smb2_transform_hdr) +
3359 		  le32_to_cpu(t_hdr->OriginalMessageSize);
3360 
3361 	return le32_to_cpu(hdr->NextCommand);
3362 }
3363 
3364 struct smb_version_operations smb20_operations = {
3365 	.compare_fids = smb2_compare_fids,
3366 	.setup_request = smb2_setup_request,
3367 	.setup_async_request = smb2_setup_async_request,
3368 	.check_receive = smb2_check_receive,
3369 	.add_credits = smb2_add_credits,
3370 	.set_credits = smb2_set_credits,
3371 	.get_credits_field = smb2_get_credits_field,
3372 	.get_credits = smb2_get_credits,
3373 	.wait_mtu_credits = cifs_wait_mtu_credits,
3374 	.get_next_mid = smb2_get_next_mid,
3375 	.revert_current_mid = smb2_revert_current_mid,
3376 	.read_data_offset = smb2_read_data_offset,
3377 	.read_data_length = smb2_read_data_length,
3378 	.map_error = map_smb2_to_linux_error,
3379 	.find_mid = smb2_find_mid,
3380 	.check_message = smb2_check_message,
3381 	.dump_detail = smb2_dump_detail,
3382 	.clear_stats = smb2_clear_stats,
3383 	.print_stats = smb2_print_stats,
3384 	.is_oplock_break = smb2_is_valid_oplock_break,
3385 	.handle_cancelled_mid = smb2_handle_cancelled_mid,
3386 	.downgrade_oplock = smb2_downgrade_oplock,
3387 	.need_neg = smb2_need_neg,
3388 	.negotiate = smb2_negotiate,
3389 	.negotiate_wsize = smb2_negotiate_wsize,
3390 	.negotiate_rsize = smb2_negotiate_rsize,
3391 	.sess_setup = SMB2_sess_setup,
3392 	.logoff = SMB2_logoff,
3393 	.tree_connect = SMB2_tcon,
3394 	.tree_disconnect = SMB2_tdis,
3395 	.qfs_tcon = smb2_qfs_tcon,
3396 	.is_path_accessible = smb2_is_path_accessible,
3397 	.can_echo = smb2_can_echo,
3398 	.echo = SMB2_echo,
3399 	.query_path_info = smb2_query_path_info,
3400 	.get_srv_inum = smb2_get_srv_inum,
3401 	.query_file_info = smb2_query_file_info,
3402 	.set_path_size = smb2_set_path_size,
3403 	.set_file_size = smb2_set_file_size,
3404 	.set_file_info = smb2_set_file_info,
3405 	.set_compression = smb2_set_compression,
3406 	.mkdir = smb2_mkdir,
3407 	.mkdir_setinfo = smb2_mkdir_setinfo,
3408 	.rmdir = smb2_rmdir,
3409 	.unlink = smb2_unlink,
3410 	.rename = smb2_rename_path,
3411 	.create_hardlink = smb2_create_hardlink,
3412 	.query_symlink = smb2_query_symlink,
3413 	.query_mf_symlink = smb3_query_mf_symlink,
3414 	.create_mf_symlink = smb3_create_mf_symlink,
3415 	.open = smb2_open_file,
3416 	.set_fid = smb2_set_fid,
3417 	.close = smb2_close_file,
3418 	.flush = smb2_flush_file,
3419 	.async_readv = smb2_async_readv,
3420 	.async_writev = smb2_async_writev,
3421 	.sync_read = smb2_sync_read,
3422 	.sync_write = smb2_sync_write,
3423 	.query_dir_first = smb2_query_dir_first,
3424 	.query_dir_next = smb2_query_dir_next,
3425 	.close_dir = smb2_close_dir,
3426 	.calc_smb_size = smb2_calc_size,
3427 	.is_status_pending = smb2_is_status_pending,
3428 	.is_session_expired = smb2_is_session_expired,
3429 	.oplock_response = smb2_oplock_response,
3430 	.queryfs = smb2_queryfs,
3431 	.mand_lock = smb2_mand_lock,
3432 	.mand_unlock_range = smb2_unlock_range,
3433 	.push_mand_locks = smb2_push_mandatory_locks,
3434 	.get_lease_key = smb2_get_lease_key,
3435 	.set_lease_key = smb2_set_lease_key,
3436 	.new_lease_key = smb2_new_lease_key,
3437 	.calc_signature = smb2_calc_signature,
3438 	.is_read_op = smb2_is_read_op,
3439 	.set_oplock_level = smb2_set_oplock_level,
3440 	.create_lease_buf = smb2_create_lease_buf,
3441 	.parse_lease_buf = smb2_parse_lease_buf,
3442 	.copychunk_range = smb2_copychunk_range,
3443 	.wp_retry_size = smb2_wp_retry_size,
3444 	.dir_needs_close = smb2_dir_needs_close,
3445 	.get_dfs_refer = smb2_get_dfs_refer,
3446 	.select_sectype = smb2_select_sectype,
3447 #ifdef CONFIG_CIFS_XATTR
3448 	.query_all_EAs = smb2_query_eas,
3449 	.set_EA = smb2_set_ea,
3450 #endif /* CIFS_XATTR */
3451 #ifdef CONFIG_CIFS_ACL
3452 	.get_acl = get_smb2_acl,
3453 	.get_acl_by_fid = get_smb2_acl_by_fid,
3454 	.set_acl = set_smb2_acl,
3455 #endif /* CIFS_ACL */
3456 	.next_header = smb2_next_header,
3457 };
3458 
3459 struct smb_version_operations smb21_operations = {
3460 	.compare_fids = smb2_compare_fids,
3461 	.setup_request = smb2_setup_request,
3462 	.setup_async_request = smb2_setup_async_request,
3463 	.check_receive = smb2_check_receive,
3464 	.add_credits = smb2_add_credits,
3465 	.set_credits = smb2_set_credits,
3466 	.get_credits_field = smb2_get_credits_field,
3467 	.get_credits = smb2_get_credits,
3468 	.wait_mtu_credits = smb2_wait_mtu_credits,
3469 	.get_next_mid = smb2_get_next_mid,
3470 	.revert_current_mid = smb2_revert_current_mid,
3471 	.read_data_offset = smb2_read_data_offset,
3472 	.read_data_length = smb2_read_data_length,
3473 	.map_error = map_smb2_to_linux_error,
3474 	.find_mid = smb2_find_mid,
3475 	.check_message = smb2_check_message,
3476 	.dump_detail = smb2_dump_detail,
3477 	.clear_stats = smb2_clear_stats,
3478 	.print_stats = smb2_print_stats,
3479 	.is_oplock_break = smb2_is_valid_oplock_break,
3480 	.handle_cancelled_mid = smb2_handle_cancelled_mid,
3481 	.downgrade_oplock = smb2_downgrade_oplock,
3482 	.need_neg = smb2_need_neg,
3483 	.negotiate = smb2_negotiate,
3484 	.negotiate_wsize = smb2_negotiate_wsize,
3485 	.negotiate_rsize = smb2_negotiate_rsize,
3486 	.sess_setup = SMB2_sess_setup,
3487 	.logoff = SMB2_logoff,
3488 	.tree_connect = SMB2_tcon,
3489 	.tree_disconnect = SMB2_tdis,
3490 	.qfs_tcon = smb2_qfs_tcon,
3491 	.is_path_accessible = smb2_is_path_accessible,
3492 	.can_echo = smb2_can_echo,
3493 	.echo = SMB2_echo,
3494 	.query_path_info = smb2_query_path_info,
3495 	.get_srv_inum = smb2_get_srv_inum,
3496 	.query_file_info = smb2_query_file_info,
3497 	.set_path_size = smb2_set_path_size,
3498 	.set_file_size = smb2_set_file_size,
3499 	.set_file_info = smb2_set_file_info,
3500 	.set_compression = smb2_set_compression,
3501 	.mkdir = smb2_mkdir,
3502 	.mkdir_setinfo = smb2_mkdir_setinfo,
3503 	.rmdir = smb2_rmdir,
3504 	.unlink = smb2_unlink,
3505 	.rename = smb2_rename_path,
3506 	.create_hardlink = smb2_create_hardlink,
3507 	.query_symlink = smb2_query_symlink,
3508 	.query_mf_symlink = smb3_query_mf_symlink,
3509 	.create_mf_symlink = smb3_create_mf_symlink,
3510 	.open = smb2_open_file,
3511 	.set_fid = smb2_set_fid,
3512 	.close = smb2_close_file,
3513 	.flush = smb2_flush_file,
3514 	.async_readv = smb2_async_readv,
3515 	.async_writev = smb2_async_writev,
3516 	.sync_read = smb2_sync_read,
3517 	.sync_write = smb2_sync_write,
3518 	.query_dir_first = smb2_query_dir_first,
3519 	.query_dir_next = smb2_query_dir_next,
3520 	.close_dir = smb2_close_dir,
3521 	.calc_smb_size = smb2_calc_size,
3522 	.is_status_pending = smb2_is_status_pending,
3523 	.is_session_expired = smb2_is_session_expired,
3524 	.oplock_response = smb2_oplock_response,
3525 	.queryfs = smb2_queryfs,
3526 	.mand_lock = smb2_mand_lock,
3527 	.mand_unlock_range = smb2_unlock_range,
3528 	.push_mand_locks = smb2_push_mandatory_locks,
3529 	.get_lease_key = smb2_get_lease_key,
3530 	.set_lease_key = smb2_set_lease_key,
3531 	.new_lease_key = smb2_new_lease_key,
3532 	.calc_signature = smb2_calc_signature,
3533 	.is_read_op = smb21_is_read_op,
3534 	.set_oplock_level = smb21_set_oplock_level,
3535 	.create_lease_buf = smb2_create_lease_buf,
3536 	.parse_lease_buf = smb2_parse_lease_buf,
3537 	.copychunk_range = smb2_copychunk_range,
3538 	.wp_retry_size = smb2_wp_retry_size,
3539 	.dir_needs_close = smb2_dir_needs_close,
3540 	.enum_snapshots = smb3_enum_snapshots,
3541 	.get_dfs_refer = smb2_get_dfs_refer,
3542 	.select_sectype = smb2_select_sectype,
3543 #ifdef CONFIG_CIFS_XATTR
3544 	.query_all_EAs = smb2_query_eas,
3545 	.set_EA = smb2_set_ea,
3546 #endif /* CIFS_XATTR */
3547 #ifdef CONFIG_CIFS_ACL
3548 	.get_acl = get_smb2_acl,
3549 	.get_acl_by_fid = get_smb2_acl_by_fid,
3550 	.set_acl = set_smb2_acl,
3551 #endif /* CIFS_ACL */
3552 	.next_header = smb2_next_header,
3553 };
3554 
3555 struct smb_version_operations smb30_operations = {
3556 	.compare_fids = smb2_compare_fids,
3557 	.setup_request = smb2_setup_request,
3558 	.setup_async_request = smb2_setup_async_request,
3559 	.check_receive = smb2_check_receive,
3560 	.add_credits = smb2_add_credits,
3561 	.set_credits = smb2_set_credits,
3562 	.get_credits_field = smb2_get_credits_field,
3563 	.get_credits = smb2_get_credits,
3564 	.wait_mtu_credits = smb2_wait_mtu_credits,
3565 	.get_next_mid = smb2_get_next_mid,
3566 	.revert_current_mid = smb2_revert_current_mid,
3567 	.read_data_offset = smb2_read_data_offset,
3568 	.read_data_length = smb2_read_data_length,
3569 	.map_error = map_smb2_to_linux_error,
3570 	.find_mid = smb2_find_mid,
3571 	.check_message = smb2_check_message,
3572 	.dump_detail = smb2_dump_detail,
3573 	.clear_stats = smb2_clear_stats,
3574 	.print_stats = smb2_print_stats,
3575 	.dump_share_caps = smb2_dump_share_caps,
3576 	.is_oplock_break = smb2_is_valid_oplock_break,
3577 	.handle_cancelled_mid = smb2_handle_cancelled_mid,
3578 	.downgrade_oplock = smb3_downgrade_oplock,
3579 	.need_neg = smb2_need_neg,
3580 	.negotiate = smb2_negotiate,
3581 	.negotiate_wsize = smb2_negotiate_wsize,
3582 	.negotiate_rsize = smb2_negotiate_rsize,
3583 	.sess_setup = SMB2_sess_setup,
3584 	.logoff = SMB2_logoff,
3585 	.tree_connect = SMB2_tcon,
3586 	.tree_disconnect = SMB2_tdis,
3587 	.qfs_tcon = smb3_qfs_tcon,
3588 	.is_path_accessible = smb2_is_path_accessible,
3589 	.can_echo = smb2_can_echo,
3590 	.echo = SMB2_echo,
3591 	.query_path_info = smb2_query_path_info,
3592 	.get_srv_inum = smb2_get_srv_inum,
3593 	.query_file_info = smb2_query_file_info,
3594 	.set_path_size = smb2_set_path_size,
3595 	.set_file_size = smb2_set_file_size,
3596 	.set_file_info = smb2_set_file_info,
3597 	.set_compression = smb2_set_compression,
3598 	.mkdir = smb2_mkdir,
3599 	.mkdir_setinfo = smb2_mkdir_setinfo,
3600 	.rmdir = smb2_rmdir,
3601 	.unlink = smb2_unlink,
3602 	.rename = smb2_rename_path,
3603 	.create_hardlink = smb2_create_hardlink,
3604 	.query_symlink = smb2_query_symlink,
3605 	.query_mf_symlink = smb3_query_mf_symlink,
3606 	.create_mf_symlink = smb3_create_mf_symlink,
3607 	.open = smb2_open_file,
3608 	.set_fid = smb2_set_fid,
3609 	.close = smb2_close_file,
3610 	.flush = smb2_flush_file,
3611 	.async_readv = smb2_async_readv,
3612 	.async_writev = smb2_async_writev,
3613 	.sync_read = smb2_sync_read,
3614 	.sync_write = smb2_sync_write,
3615 	.query_dir_first = smb2_query_dir_first,
3616 	.query_dir_next = smb2_query_dir_next,
3617 	.close_dir = smb2_close_dir,
3618 	.calc_smb_size = smb2_calc_size,
3619 	.is_status_pending = smb2_is_status_pending,
3620 	.is_session_expired = smb2_is_session_expired,
3621 	.oplock_response = smb2_oplock_response,
3622 	.queryfs = smb2_queryfs,
3623 	.mand_lock = smb2_mand_lock,
3624 	.mand_unlock_range = smb2_unlock_range,
3625 	.push_mand_locks = smb2_push_mandatory_locks,
3626 	.get_lease_key = smb2_get_lease_key,
3627 	.set_lease_key = smb2_set_lease_key,
3628 	.new_lease_key = smb2_new_lease_key,
3629 	.generate_signingkey = generate_smb30signingkey,
3630 	.calc_signature = smb3_calc_signature,
3631 	.set_integrity  = smb3_set_integrity,
3632 	.is_read_op = smb21_is_read_op,
3633 	.set_oplock_level = smb3_set_oplock_level,
3634 	.create_lease_buf = smb3_create_lease_buf,
3635 	.parse_lease_buf = smb3_parse_lease_buf,
3636 	.copychunk_range = smb2_copychunk_range,
3637 	.duplicate_extents = smb2_duplicate_extents,
3638 	.validate_negotiate = smb3_validate_negotiate,
3639 	.wp_retry_size = smb2_wp_retry_size,
3640 	.dir_needs_close = smb2_dir_needs_close,
3641 	.fallocate = smb3_fallocate,
3642 	.enum_snapshots = smb3_enum_snapshots,
3643 	.init_transform_rq = smb3_init_transform_rq,
3644 	.is_transform_hdr = smb3_is_transform_hdr,
3645 	.receive_transform = smb3_receive_transform,
3646 	.get_dfs_refer = smb2_get_dfs_refer,
3647 	.select_sectype = smb2_select_sectype,
3648 #ifdef CONFIG_CIFS_XATTR
3649 	.query_all_EAs = smb2_query_eas,
3650 	.set_EA = smb2_set_ea,
3651 #endif /* CIFS_XATTR */
3652 #ifdef CONFIG_CIFS_ACL
3653 	.get_acl = get_smb2_acl,
3654 	.get_acl_by_fid = get_smb2_acl_by_fid,
3655 	.set_acl = set_smb2_acl,
3656 #endif /* CIFS_ACL */
3657 	.next_header = smb2_next_header,
3658 };
3659 
3660 struct smb_version_operations smb311_operations = {
3661 	.compare_fids = smb2_compare_fids,
3662 	.setup_request = smb2_setup_request,
3663 	.setup_async_request = smb2_setup_async_request,
3664 	.check_receive = smb2_check_receive,
3665 	.add_credits = smb2_add_credits,
3666 	.set_credits = smb2_set_credits,
3667 	.get_credits_field = smb2_get_credits_field,
3668 	.get_credits = smb2_get_credits,
3669 	.wait_mtu_credits = smb2_wait_mtu_credits,
3670 	.get_next_mid = smb2_get_next_mid,
3671 	.revert_current_mid = smb2_revert_current_mid,
3672 	.read_data_offset = smb2_read_data_offset,
3673 	.read_data_length = smb2_read_data_length,
3674 	.map_error = map_smb2_to_linux_error,
3675 	.find_mid = smb2_find_mid,
3676 	.check_message = smb2_check_message,
3677 	.dump_detail = smb2_dump_detail,
3678 	.clear_stats = smb2_clear_stats,
3679 	.print_stats = smb2_print_stats,
3680 	.dump_share_caps = smb2_dump_share_caps,
3681 	.is_oplock_break = smb2_is_valid_oplock_break,
3682 	.handle_cancelled_mid = smb2_handle_cancelled_mid,
3683 	.downgrade_oplock = smb3_downgrade_oplock,
3684 	.need_neg = smb2_need_neg,
3685 	.negotiate = smb2_negotiate,
3686 	.negotiate_wsize = smb2_negotiate_wsize,
3687 	.negotiate_rsize = smb2_negotiate_rsize,
3688 	.sess_setup = SMB2_sess_setup,
3689 	.logoff = SMB2_logoff,
3690 	.tree_connect = SMB2_tcon,
3691 	.tree_disconnect = SMB2_tdis,
3692 	.qfs_tcon = smb3_qfs_tcon,
3693 	.is_path_accessible = smb2_is_path_accessible,
3694 	.can_echo = smb2_can_echo,
3695 	.echo = SMB2_echo,
3696 	.query_path_info = smb2_query_path_info,
3697 	.get_srv_inum = smb2_get_srv_inum,
3698 	.query_file_info = smb2_query_file_info,
3699 	.set_path_size = smb2_set_path_size,
3700 	.set_file_size = smb2_set_file_size,
3701 	.set_file_info = smb2_set_file_info,
3702 	.set_compression = smb2_set_compression,
3703 	.mkdir = smb2_mkdir,
3704 	.mkdir_setinfo = smb2_mkdir_setinfo,
3705 	.posix_mkdir = smb311_posix_mkdir,
3706 	.rmdir = smb2_rmdir,
3707 	.unlink = smb2_unlink,
3708 	.rename = smb2_rename_path,
3709 	.create_hardlink = smb2_create_hardlink,
3710 	.query_symlink = smb2_query_symlink,
3711 	.query_mf_symlink = smb3_query_mf_symlink,
3712 	.create_mf_symlink = smb3_create_mf_symlink,
3713 	.open = smb2_open_file,
3714 	.set_fid = smb2_set_fid,
3715 	.close = smb2_close_file,
3716 	.flush = smb2_flush_file,
3717 	.async_readv = smb2_async_readv,
3718 	.async_writev = smb2_async_writev,
3719 	.sync_read = smb2_sync_read,
3720 	.sync_write = smb2_sync_write,
3721 	.query_dir_first = smb2_query_dir_first,
3722 	.query_dir_next = smb2_query_dir_next,
3723 	.close_dir = smb2_close_dir,
3724 	.calc_smb_size = smb2_calc_size,
3725 	.is_status_pending = smb2_is_status_pending,
3726 	.is_session_expired = smb2_is_session_expired,
3727 	.oplock_response = smb2_oplock_response,
3728 	.queryfs = smb311_queryfs,
3729 	.mand_lock = smb2_mand_lock,
3730 	.mand_unlock_range = smb2_unlock_range,
3731 	.push_mand_locks = smb2_push_mandatory_locks,
3732 	.get_lease_key = smb2_get_lease_key,
3733 	.set_lease_key = smb2_set_lease_key,
3734 	.new_lease_key = smb2_new_lease_key,
3735 	.generate_signingkey = generate_smb311signingkey,
3736 	.calc_signature = smb3_calc_signature,
3737 	.set_integrity  = smb3_set_integrity,
3738 	.is_read_op = smb21_is_read_op,
3739 	.set_oplock_level = smb3_set_oplock_level,
3740 	.create_lease_buf = smb3_create_lease_buf,
3741 	.parse_lease_buf = smb3_parse_lease_buf,
3742 	.copychunk_range = smb2_copychunk_range,
3743 	.duplicate_extents = smb2_duplicate_extents,
3744 /*	.validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
3745 	.wp_retry_size = smb2_wp_retry_size,
3746 	.dir_needs_close = smb2_dir_needs_close,
3747 	.fallocate = smb3_fallocate,
3748 	.enum_snapshots = smb3_enum_snapshots,
3749 	.init_transform_rq = smb3_init_transform_rq,
3750 	.is_transform_hdr = smb3_is_transform_hdr,
3751 	.receive_transform = smb3_receive_transform,
3752 	.get_dfs_refer = smb2_get_dfs_refer,
3753 	.select_sectype = smb2_select_sectype,
3754 #ifdef CONFIG_CIFS_XATTR
3755 	.query_all_EAs = smb2_query_eas,
3756 	.set_EA = smb2_set_ea,
3757 #endif /* CIFS_XATTR */
3758 #ifdef CONFIG_CIFS_ACL
3759 	.get_acl = get_smb2_acl,
3760 	.get_acl_by_fid = get_smb2_acl_by_fid,
3761 	.set_acl = set_smb2_acl,
3762 #endif /* CIFS_ACL */
3763 	.next_header = smb2_next_header,
3764 };
3765 
3766 struct smb_version_values smb20_values = {
3767 	.version_string = SMB20_VERSION_STRING,
3768 	.protocol_id = SMB20_PROT_ID,
3769 	.req_capabilities = 0, /* MBZ */
3770 	.large_lock_type = 0,
3771 	.exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3772 	.shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3773 	.unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3774 	.header_size = sizeof(struct smb2_sync_hdr),
3775 	.header_preamble_size = 0,
3776 	.max_header_size = MAX_SMB2_HDR_SIZE,
3777 	.read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3778 	.lock_cmd = SMB2_LOCK,
3779 	.cap_unix = 0,
3780 	.cap_nt_find = SMB2_NT_FIND,
3781 	.cap_large_files = SMB2_LARGE_FILES,
3782 	.signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3783 	.signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3784 	.create_lease_size = sizeof(struct create_lease),
3785 };
3786 
3787 struct smb_version_values smb21_values = {
3788 	.version_string = SMB21_VERSION_STRING,
3789 	.protocol_id = SMB21_PROT_ID,
3790 	.req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
3791 	.large_lock_type = 0,
3792 	.exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3793 	.shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3794 	.unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3795 	.header_size = sizeof(struct smb2_sync_hdr),
3796 	.header_preamble_size = 0,
3797 	.max_header_size = MAX_SMB2_HDR_SIZE,
3798 	.read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3799 	.lock_cmd = SMB2_LOCK,
3800 	.cap_unix = 0,
3801 	.cap_nt_find = SMB2_NT_FIND,
3802 	.cap_large_files = SMB2_LARGE_FILES,
3803 	.signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3804 	.signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3805 	.create_lease_size = sizeof(struct create_lease),
3806 };
3807 
3808 struct smb_version_values smb3any_values = {
3809 	.version_string = SMB3ANY_VERSION_STRING,
3810 	.protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
3811 	.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,
3812 	.large_lock_type = 0,
3813 	.exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3814 	.shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3815 	.unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3816 	.header_size = sizeof(struct smb2_sync_hdr),
3817 	.header_preamble_size = 0,
3818 	.max_header_size = MAX_SMB2_HDR_SIZE,
3819 	.read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3820 	.lock_cmd = SMB2_LOCK,
3821 	.cap_unix = 0,
3822 	.cap_nt_find = SMB2_NT_FIND,
3823 	.cap_large_files = SMB2_LARGE_FILES,
3824 	.signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3825 	.signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3826 	.create_lease_size = sizeof(struct create_lease_v2),
3827 };
3828 
3829 struct smb_version_values smbdefault_values = {
3830 	.version_string = SMBDEFAULT_VERSION_STRING,
3831 	.protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
3832 	.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,
3833 	.large_lock_type = 0,
3834 	.exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3835 	.shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3836 	.unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3837 	.header_size = sizeof(struct smb2_sync_hdr),
3838 	.header_preamble_size = 0,
3839 	.max_header_size = MAX_SMB2_HDR_SIZE,
3840 	.read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3841 	.lock_cmd = SMB2_LOCK,
3842 	.cap_unix = 0,
3843 	.cap_nt_find = SMB2_NT_FIND,
3844 	.cap_large_files = SMB2_LARGE_FILES,
3845 	.signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3846 	.signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3847 	.create_lease_size = sizeof(struct create_lease_v2),
3848 };
3849 
3850 struct smb_version_values smb30_values = {
3851 	.version_string = SMB30_VERSION_STRING,
3852 	.protocol_id = SMB30_PROT_ID,
3853 	.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,
3854 	.large_lock_type = 0,
3855 	.exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3856 	.shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3857 	.unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3858 	.header_size = sizeof(struct smb2_sync_hdr),
3859 	.header_preamble_size = 0,
3860 	.max_header_size = MAX_SMB2_HDR_SIZE,
3861 	.read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3862 	.lock_cmd = SMB2_LOCK,
3863 	.cap_unix = 0,
3864 	.cap_nt_find = SMB2_NT_FIND,
3865 	.cap_large_files = SMB2_LARGE_FILES,
3866 	.signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3867 	.signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3868 	.create_lease_size = sizeof(struct create_lease_v2),
3869 };
3870 
3871 struct smb_version_values smb302_values = {
3872 	.version_string = SMB302_VERSION_STRING,
3873 	.protocol_id = SMB302_PROT_ID,
3874 	.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,
3875 	.large_lock_type = 0,
3876 	.exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3877 	.shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3878 	.unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3879 	.header_size = sizeof(struct smb2_sync_hdr),
3880 	.header_preamble_size = 0,
3881 	.max_header_size = MAX_SMB2_HDR_SIZE,
3882 	.read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3883 	.lock_cmd = SMB2_LOCK,
3884 	.cap_unix = 0,
3885 	.cap_nt_find = SMB2_NT_FIND,
3886 	.cap_large_files = SMB2_LARGE_FILES,
3887 	.signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3888 	.signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3889 	.create_lease_size = sizeof(struct create_lease_v2),
3890 };
3891 
3892 struct smb_version_values smb311_values = {
3893 	.version_string = SMB311_VERSION_STRING,
3894 	.protocol_id = SMB311_PROT_ID,
3895 	.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,
3896 	.large_lock_type = 0,
3897 	.exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3898 	.shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3899 	.unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3900 	.header_size = sizeof(struct smb2_sync_hdr),
3901 	.header_preamble_size = 0,
3902 	.max_header_size = MAX_SMB2_HDR_SIZE,
3903 	.read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3904 	.lock_cmd = SMB2_LOCK,
3905 	.cap_unix = 0,
3906 	.cap_nt_find = SMB2_NT_FIND,
3907 	.cap_large_files = SMB2_LARGE_FILES,
3908 	.signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3909 	.signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3910 	.create_lease_size = sizeof(struct create_lease_v2),
3911 };
3912