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