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