Linux Audio

Check our new training course

Embedded Linux training

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