transport.c 40.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 *   fs/cifs/transport.c
 *
S
Steve French 已提交
4
 *   Copyright (C) International Business Machines  Corp., 2002,2008
L
Linus Torvalds 已提交
5
 *   Author(s): Steve French (sfrench@us.ibm.com)
6
 *   Jeremy Allison (jra@samba.org) 2006.
S
Steve French 已提交
7
 *
L
Linus Torvalds 已提交
8 9 10 11 12 13 14 15 16 17 18 19
 *   This library is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation; either version 2.1 of the License, or
 *   (at your option) any later version.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 *   the GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with this library; if not, write to the Free Software
S
Steve French 已提交
20
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
L
Linus Torvalds 已提交
21 22 23 24
 */

#include <linux/fs.h>
#include <linux/list.h>
25
#include <linux/gfp.h>
L
Linus Torvalds 已提交
26 27 28
#include <linux/wait.h>
#include <linux/net.h>
#include <linux/delay.h>
29
#include <linux/freezer.h>
30
#include <linux/tcp.h>
31
#include <linux/bvec.h>
32
#include <linux/highmem.h>
33
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
34 35
#include <asm/processor.h>
#include <linux/mempool.h>
36
#include <linux/sched/signal.h>
L
Linus Torvalds 已提交
37 38 39 40
#include "cifspdu.h"
#include "cifsglob.h"
#include "cifsproto.h"
#include "cifs_debug.h"
41
#include "smb2proto.h"
42
#include "smbdirect.h"
43

44 45 46
/* Max number of iovectors we can use off the stack when sending requests. */
#define CIFS_MAX_IOV_SIZE 8

47 48
void
cifs_wake_up_task(struct mid_q_entry *mid)
49 50 51 52
{
	wake_up_process(mid->callback_data);
}

J
Jeff Layton 已提交
53
struct mid_q_entry *
54
AllocMidQEntry(const struct smb_hdr *smb_buffer, struct TCP_Server_Info *server)
L
Linus Torvalds 已提交
55 56 57
{
	struct mid_q_entry *temp;

58
	if (server == NULL) {
59
		cifs_dbg(VFS, "Null TCP session in AllocMidQEntry\n");
L
Linus Torvalds 已提交
60 61
		return NULL;
	}
62

63
	temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
64
	memset(temp, 0, sizeof(struct mid_q_entry));
65
	kref_init(&temp->refcount);
66 67 68 69
	temp->mid = get_mid(smb_buffer);
	temp->pid = current->pid;
	temp->command = cpu_to_le16(smb_buffer->Command);
	cifs_dbg(FYI, "For smb_command %d\n", smb_buffer->Command);
S
Steve French 已提交
70
	/*	do_gettimeofday(&temp->when_sent);*/ /* easier to use jiffies */
71 72 73
	/* when mid allocated can be before when sent */
	temp->when_alloc = jiffies;
	temp->server = server;
74

75 76 77 78 79 80
	/*
	 * The default is for the mid to be synchronous, so the
	 * default callback just wakes up the current task.
	 */
	temp->callback = cifs_wake_up_task;
	temp->callback_data = current;
L
Linus Torvalds 已提交
81 82

	atomic_inc(&midCount);
83
	temp->mid_state = MID_REQUEST_ALLOCATED;
L
Linus Torvalds 已提交
84 85 86
	return temp;
}

87 88
static void _cifs_mid_q_entry_release(struct kref *refcount)
{
89 90
	struct mid_q_entry *midEntry =
			container_of(refcount, struct mid_q_entry, refcount);
S
Steve French 已提交
91
#ifdef CONFIG_CIFS_STATS2
92
	__le16 command = midEntry->server->vals->lock_cmd;
93
	__u16 smb_cmd = le16_to_cpu(midEntry->command);
S
Steve French 已提交
94
	unsigned long now;
95
	unsigned long roundtrip_time;
S
Steve French 已提交
96
#endif
97 98 99 100 101 102 103
	struct TCP_Server_Info *server = midEntry->server;

	if (midEntry->resp_buf && (midEntry->mid_flags & MID_WAIT_CANCELLED) &&
	    midEntry->mid_state == MID_RESPONSE_RECEIVED &&
	    server->ops->handle_cancelled_mid)
		server->ops->handle_cancelled_mid(midEntry->resp_buf, server);

104
	midEntry->mid_state = MID_FREE;
105
	atomic_dec(&midCount);
106
	if (midEntry->large_buf)
107 108 109
		cifs_buf_release(midEntry->resp_buf);
	else
		cifs_small_buf_release(midEntry->resp_buf);
S
Steve French 已提交
110 111
#ifdef CONFIG_CIFS_STATS2
	now = jiffies;
112
	if (now < midEntry->when_alloc)
113
		cifs_server_dbg(VFS, "invalid mid allocation time\n");
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
	roundtrip_time = now - midEntry->when_alloc;

	if (smb_cmd < NUMBER_OF_SMB2_COMMANDS) {
		if (atomic_read(&server->num_cmds[smb_cmd]) == 0) {
			server->slowest_cmd[smb_cmd] = roundtrip_time;
			server->fastest_cmd[smb_cmd] = roundtrip_time;
		} else {
			if (server->slowest_cmd[smb_cmd] < roundtrip_time)
				server->slowest_cmd[smb_cmd] = roundtrip_time;
			else if (server->fastest_cmd[smb_cmd] > roundtrip_time)
				server->fastest_cmd[smb_cmd] = roundtrip_time;
		}
		cifs_stats_inc(&server->num_cmds[smb_cmd]);
		server->time_per_cmd[smb_cmd] += roundtrip_time;
	}
129 130 131 132 133 134 135 136 137 138 139 140
	/*
	 * commands taking longer than one second (default) can be indications
	 * that something is wrong, unless it is quite a slow link or a very
	 * busy server. Note that this calc is unlikely or impossible to wrap
	 * as long as slow_rsp_threshold is not set way above recommended max
	 * value (32767 ie 9 hours) and is generally harmless even if wrong
	 * since only affects debug counters - so leaving the calc as simple
	 * comparison rather than doing multiple conversions and overflow
	 * checks
	 */
	if ((slow_rsp_threshold != 0) &&
	    time_after(now, midEntry->when_alloc + (slow_rsp_threshold * HZ)) &&
141
	    (midEntry->command != command)) {
142 143 144 145
		/*
		 * smb2slowcmd[NUMBER_OF_SMB2_COMMANDS] counts by command
		 * NB: le16_to_cpu returns unsigned so can not be negative below
		 */
146 147
		if (smb_cmd < NUMBER_OF_SMB2_COMMANDS)
			cifs_stats_inc(&server->smb2slowcmd[smb_cmd]);
148

149
		trace_smb3_slow_rsp(smb_cmd, midEntry->mid, midEntry->pid,
150 151
			       midEntry->when_sent, midEntry->when_received);
		if (cifsFYI & CIFS_TIMER) {
152
			pr_debug(" CIFS slow rsp: cmd %d mid %llu",
S
Steve French 已提交
153
			       midEntry->command, midEntry->mid);
154
			cifs_info(" A: 0x%lx S: 0x%lx R: 0x%lx\n",
S
Steve French 已提交
155 156 157 158 159 160
			       now - midEntry->when_alloc,
			       now - midEntry->when_sent,
			       now - midEntry->when_received);
		}
	}
#endif
161 162 163 164 165 166 167 168 169 170 171 172 173

	mempool_free(midEntry, cifs_mid_poolp);
}

void cifs_mid_q_entry_release(struct mid_q_entry *midEntry)
{
	spin_lock(&GlobalMid_Lock);
	kref_put(&midEntry->refcount, _cifs_mid_q_entry_release);
	spin_unlock(&GlobalMid_Lock);
}

void DeleteMidQEntry(struct mid_q_entry *midEntry)
{
174
	cifs_mid_q_entry_release(midEntry);
L
Linus Torvalds 已提交
175 176
}

P
Pavel Shilovsky 已提交
177 178
void
cifs_delete_mid(struct mid_q_entry *mid)
179 180
{
	spin_lock(&GlobalMid_Lock);
181 182 183 184
	if (!(mid->mid_flags & MID_DELETED)) {
		list_del_init(&mid->qhead);
		mid->mid_flags |= MID_DELETED;
	}
185 186 187 188 189
	spin_unlock(&GlobalMid_Lock);

	DeleteMidQEntry(mid);
}

190 191 192
/*
 * smb_send_kvec - send an array of kvecs to the server
 * @server:	Server to send the data to
193
 * @smb_msg:	Message to send
194 195 196 197 198
 * @sent:	amount of data sent on socket is stored here
 *
 * Our basic "send data to server" function. Should be called with srv_mutex
 * held. The caller is responsible for handling the results.
 */
199
static int
200 201
smb_send_kvec(struct TCP_Server_Info *server, struct msghdr *smb_msg,
	      size_t *sent)
L
Linus Torvalds 已提交
202 203
{
	int rc = 0;
204
	int retries = 0;
205
	struct socket *ssocket = server->ssocket;
206

207 208
	*sent = 0;

209 210 211 212
	smb_msg->msg_name = (struct sockaddr *) &server->dstaddr;
	smb_msg->msg_namelen = sizeof(struct sockaddr);
	smb_msg->msg_control = NULL;
	smb_msg->msg_controllen = 0;
213
	if (server->noblocksnd)
214
		smb_msg->msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL;
215
	else
216
		smb_msg->msg_flags = MSG_NOSIGNAL;
L
Linus Torvalds 已提交
217

218
	while (msg_data_left(smb_msg)) {
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
		/*
		 * If blocking send, we try 3 times, since each can block
		 * for 5 seconds. For nonblocking  we have to try more
		 * but wait increasing amounts of time allowing time for
		 * socket to clear.  The overall time we wait in either
		 * case to send on the socket is about 15 seconds.
		 * Similarly we wait for 15 seconds for a response from
		 * the server in SendReceive[2] for the server to send
		 * a response back for most types of requests (except
		 * SMB Write past end of file which can be slow, and
		 * blocking lock operations). NFS waits slightly longer
		 * than CIFS, but this can make it take longer for
		 * nonresponsive servers to be detected and 15 seconds
		 * is more than enough time for modern networks to
		 * send a packet.  In most cases if we fail to send
		 * after the retries we will kill the socket and
		 * reconnect which may clear the network problem.
		 */
237
		rc = sock_sendmsg(ssocket, smb_msg);
238
		if (rc == -EAGAIN) {
239 240 241
			retries++;
			if (retries >= 14 ||
			    (!server->noblocksnd && (retries > 2))) {
242
				cifs_server_dbg(VFS, "sends on sock %p stuck for 15 seconds\n",
243
					 ssocket);
244
				return -EAGAIN;
L
Linus Torvalds 已提交
245
			}
246
			msleep(1 << retries);
L
Linus Torvalds 已提交
247 248
			continue;
		}
249

S
Steve French 已提交
250
		if (rc < 0)
251
			return rc;
252

S
Steve French 已提交
253
		if (rc == 0) {
254 255
			/* should never happen, letting socket clear before
			   retrying is our only obvious option here */
256
			cifs_server_dbg(VFS, "tcp sent no data\n");
257 258
			msleep(500);
			continue;
259
		}
260

261 262 263
		/* send was at least partially successful */
		*sent += rc;
		retries = 0; /* in case we get ENOSPC on the next send */
L
Linus Torvalds 已提交
264
	}
265
	return 0;
266 267
}

268
unsigned long
R
Ronnie Sahlberg 已提交
269
smb_rqst_len(struct TCP_Server_Info *server, struct smb_rqst *rqst)
270 271
{
	unsigned int i;
272 273
	struct kvec *iov;
	int nvec;
274 275
	unsigned long buflen = 0;

R
Ronnie Sahlberg 已提交
276 277
	if (server->vals->header_preamble_size == 0 &&
	    rqst->rq_nvec >= 2 && rqst->rq_iov[0].iov_len == 4) {
278 279 280 281 282 283 284
		iov = &rqst->rq_iov[1];
		nvec = rqst->rq_nvec - 1;
	} else {
		iov = rqst->rq_iov;
		nvec = rqst->rq_nvec;
	}

285
	/* total up iov array first */
286
	for (i = 0; i < nvec; i++)
287 288
		buflen += iov[i].iov_len;

289 290 291 292 293 294
	/*
	 * Add in the page array if there is one. The caller needs to make
	 * sure rq_offset and rq_tailsz are set correctly. If a buffer of
	 * multiple pages ends at page boundary, rq_tailsz needs to be set to
	 * PAGE_SIZE.
	 */
295
	if (rqst->rq_npages) {
296 297 298 299 300 301 302 303 304 305 306
		if (rqst->rq_npages == 1)
			buflen += rqst->rq_tailsz;
		else {
			/*
			 * If there is more than one page, calculate the
			 * buffer length based on rq_offset and rq_tailsz
			 */
			buflen += rqst->rq_pagesz * (rqst->rq_npages - 1) -
					rqst->rq_offset;
			buflen += rqst->rq_tailsz;
		}
307 308 309 310 311
	}

	return buflen;
}

312
static int
313 314
__smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
		struct smb_rqst *rqst)
315
{
316 317 318 319 320
	int rc = 0;
	struct kvec *iov;
	int n_vec;
	unsigned int send_length = 0;
	unsigned int i, j;
321
	sigset_t mask, oldmask;
322
	size_t total_len = 0, sent, size;
323
	struct socket *ssocket = server->ssocket;
324
	struct msghdr smb_msg;
325
	int val = 1;
326 327
	__be32 rfc1002_marker;

328
	if (cifs_rdma_enabled(server) && server->smbd_conn) {
329
		rc = smbd_send(server, num_rqst, rqst);
330 331
		goto smbd_done;
	}
332

333
	if (ssocket == NULL)
334
		return -EAGAIN;
335

336 337 338 339 340
	if (signal_pending(current)) {
		cifs_dbg(FYI, "signal is pending before sending any data\n");
		return -EINTR;
	}

341 342 343 344
	/* cork the socket */
	kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
				(char *)&val, sizeof(val));

345
	for (j = 0; j < num_rqst; j++)
R
Ronnie Sahlberg 已提交
346
		send_length += smb_rqst_len(server, &rqst[j]);
347 348
	rfc1002_marker = cpu_to_be32(send_length);

349 350 351 352 353 354 355 356 357 358
	/*
	 * We should not allow signals to interrupt the network send because
	 * any partial send will cause session reconnects thus increasing
	 * latency of system calls and overload a server with unnecessary
	 * requests.
	 */

	sigfillset(&mask);
	sigprocmask(SIG_BLOCK, &mask, &oldmask);

359 360 361 362 363 364
	/* Generate a rfc1002 marker for SMB2+ */
	if (server->vals->header_preamble_size == 0) {
		struct kvec hiov = {
			.iov_base = &rfc1002_marker,
			.iov_len  = 4
		};
365
		iov_iter_kvec(&smb_msg.msg_iter, WRITE, &hiov, 1, 4);
366 367
		rc = smb_send_kvec(server, &smb_msg, &sent);
		if (rc < 0)
368
			goto unmask;
369 370 371 372 373

		total_len += sent;
		send_length += 4;
	}

374 375
	cifs_dbg(FYI, "Sending smb: smb_len=%u\n", send_length);

376 377 378
	for (j = 0; j < num_rqst; j++) {
		iov = rqst[j].rq_iov;
		n_vec = rqst[j].rq_nvec;
379

380
		size = 0;
381 382
		for (i = 0; i < n_vec; i++) {
			dump_smb(iov[i].iov_base, iov[i].iov_len);
383
			size += iov[i].iov_len;
384
		}
385

386
		iov_iter_kvec(&smb_msg.msg_iter, WRITE, iov, n_vec, size);
387

388
		rc = smb_send_kvec(server, &smb_msg, &sent);
389
		if (rc < 0)
390
			goto unmask;
391 392

		total_len += sent;
393 394 395 396 397 398 399 400 401

		/* now walk the page array and send each page in it */
		for (i = 0; i < rqst[j].rq_npages; i++) {
			struct bio_vec bvec;

			bvec.bv_page = rqst[j].rq_pages[i];
			rqst_page_get_length(&rqst[j], i, &bvec.bv_len,
					     &bvec.bv_offset);

402
			iov_iter_bvec(&smb_msg.msg_iter, WRITE,
403 404 405 406 407 408 409
				      &bvec, 1, bvec.bv_len);
			rc = smb_send_kvec(server, &smb_msg, &sent);
			if (rc < 0)
				break;

			total_len += sent;
		}
410
	}
L
Linus Torvalds 已提交
411

412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
unmask:
	sigprocmask(SIG_SETMASK, &oldmask, NULL);

	/*
	 * If signal is pending but we have already sent the whole packet to
	 * the server we need to return success status to allow a corresponding
	 * mid entry to be kept in the pending requests queue thus allowing
	 * to handle responses from the server by the client.
	 *
	 * If only part of the packet has been sent there is no need to hide
	 * interrupt because the session will be reconnected anyway, so there
	 * won't be any response from the server to handle.
	 */

	if (signal_pending(current) && (total_len != send_length)) {
		cifs_dbg(FYI, "signal is pending after attempt to send\n");
		rc = -EINTR;
	}

431 432 433 434 435
	/* uncork it */
	val = 0;
	kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
				(char *)&val, sizeof(val));

436
	if ((total_len > 0) && (total_len != send_length)) {
437
		cifs_dbg(FYI, "partial send (wanted=%u sent=%zu): terminating session\n",
438
			 send_length, total_len);
439 440 441 442 443
		/*
		 * If we have only sent part of an SMB then the next SMB could
		 * be taken as the remainder of this one. We need to kill the
		 * socket so the server throws away the partial SMB
		 */
444
		server->tcpStatus = CifsNeedReconnect;
S
Steve French 已提交
445 446
		trace_smb3_partial_send_reconnect(server->CurrentMid,
						  server->hostname);
447
	}
448
smbd_done:
449
	if (rc < 0 && rc != -EINTR)
450
		cifs_server_dbg(VFS, "Error %d sending data on socket to server\n",
451
			 rc);
452
	else if (rc > 0)
L
Linus Torvalds 已提交
453 454 455 456 457
		rc = 0;

	return rc;
}

458
static int
459 460
smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
	      struct smb_rqst *rqst, int flags)
461
{
462 463 464
	struct kvec iov;
	struct smb2_transform_hdr tr_hdr;
	struct smb_rqst cur_rqst[MAX_COMPOUND];
465 466 467
	int rc;

	if (!(flags & CIFS_TRANSFORM_REQ))
468 469 470 471
		return __smb_send_rqst(server, num_rqst, rqst);

	if (num_rqst > MAX_COMPOUND - 1)
		return -ENOMEM;
472

473 474 475 476 477 478 479 480 481 482
	memset(&cur_rqst[0], 0, sizeof(cur_rqst));
	memset(&iov, 0, sizeof(iov));
	memset(&tr_hdr, 0, sizeof(tr_hdr));

	iov.iov_base = &tr_hdr;
	iov.iov_len = sizeof(tr_hdr);
	cur_rqst[0].rq_iov = &iov;
	cur_rqst[0].rq_nvec = 1;

	if (!server->ops->init_transform_rq) {
483 484
		cifs_server_dbg(VFS, "Encryption requested but transform "
				"callback is missing\n");
485 486
		return -EIO;
	}
487

488 489
	rc = server->ops->init_transform_rq(server, num_rqst + 1,
					    &cur_rqst[0], rqst);
490 491 492
	if (rc)
		return rc;

493 494
	rc = __smb_send_rqst(server, num_rqst + 1, &cur_rqst[0]);
	smb3_free_compound_rqst(num_rqst, &cur_rqst[1]);
495
	return rc;
496 497
}

498 499 500 501
int
smb_send(struct TCP_Server_Info *server, struct smb_hdr *smb_buffer,
	 unsigned int smb_buf_length)
{
502
	struct kvec iov[2];
503 504
	struct smb_rqst rqst = { .rq_iov = iov,
				 .rq_nvec = 2 };
505

506 507 508 509
	iov[0].iov_base = smb_buffer;
	iov[0].iov_len = 4;
	iov[1].iov_base = (char *)smb_buffer + 4;
	iov[1].iov_len = smb_buf_length;
510

511
	return __smb_send_rqst(server, 1, &rqst);
512 513
}

P
Pavel Shilovsky 已提交
514
static int
515
wait_for_free_credits(struct TCP_Server_Info *server, const int num_credits,
516 517
		      const int timeout, const int flags,
		      unsigned int *instance)
L
Linus Torvalds 已提交
518
{
519
	int rc;
520 521
	int *credits;
	int optype;
522 523 524 525 526 527
	long int t;

	if (timeout < 0)
		t = MAX_JIFFY_OFFSET;
	else
		t = msecs_to_jiffies(timeout);
528 529

	optype = flags & CIFS_OP_MASK;
530

531 532
	*instance = 0;

533 534 535 536 537
	credits = server->ops->get_credits_field(server, optype);
	/* Since an echo is already inflight, no need to wait to send another */
	if (*credits <= 0 && optype == CIFS_ECHO_OP)
		return -EAGAIN;

P
Pavel Shilovsky 已提交
538
	spin_lock(&server->req_lock);
539
	if ((flags & CIFS_TIMEOUT_MASK) == CIFS_NON_BLOCKING) {
L
Linus Torvalds 已提交
540
		/* oplock breaks must not be held up */
P
Pavel Shilovsky 已提交
541
		server->in_flight++;
542 543
		if (server->in_flight > server->max_in_flight)
			server->max_in_flight = server->in_flight;
544
		*credits -= 1;
545
		*instance = server->reconnect_instance;
P
Pavel Shilovsky 已提交
546
		spin_unlock(&server->req_lock);
547 548 549 550
		return 0;
	}

	while (1) {
551
		if (*credits < num_credits) {
P
Pavel Shilovsky 已提交
552
			spin_unlock(&server->req_lock);
553
			cifs_num_waiters_inc(server);
554 555
			rc = wait_event_killable_timeout(server->request_q,
				has_credits(server, credits, num_credits), t);
556
			cifs_num_waiters_dec(server);
557
			if (!rc) {
558 559
				trace_smb3_credit_timeout(server->CurrentMid,
					server->hostname, num_credits);
560
				cifs_server_dbg(VFS, "wait timed out after %d ms\n",
561 562 563 564 565
					 timeout);
				return -ENOTSUPP;
			}
			if (rc == -ERESTARTSYS)
				return -ERESTARTSYS;
P
Pavel Shilovsky 已提交
566
			spin_lock(&server->req_lock);
567
		} else {
568
			if (server->tcpStatus == CifsExiting) {
P
Pavel Shilovsky 已提交
569
				spin_unlock(&server->req_lock);
570
				return -ENOENT;
L
Linus Torvalds 已提交
571
			}
572

573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
			/*
			 * For normal commands, reserve the last MAX_COMPOUND
			 * credits to compound requests.
			 * Otherwise these compounds could be permanently
			 * starved for credits by single-credit requests.
			 *
			 * To prevent spinning CPU, block this thread until
			 * there are >MAX_COMPOUND credits available.
			 * But only do this is we already have a lot of
			 * credits in flight to avoid triggering this check
			 * for servers that are slow to hand out credits on
			 * new sessions.
			 */
			if (!optype && num_credits == 1 &&
			    server->in_flight > 2 * MAX_COMPOUND &&
			    *credits <= MAX_COMPOUND) {
				spin_unlock(&server->req_lock);
				cifs_num_waiters_inc(server);
591 592
				rc = wait_event_killable_timeout(
					server->request_q,
593
					has_credits(server, credits,
594 595
						    MAX_COMPOUND + 1),
					t);
596
				cifs_num_waiters_dec(server);
597
				if (!rc) {
598 599 600
					trace_smb3_credit_timeout(
						server->CurrentMid,
						server->hostname, num_credits);
601
					cifs_server_dbg(VFS, "wait timed out after %d ms\n",
602 603 604 605 606
						 timeout);
					return -ENOTSUPP;
				}
				if (rc == -ERESTARTSYS)
					return -ERESTARTSYS;
607 608 609 610
				spin_lock(&server->req_lock);
				continue;
			}

611 612 613 614
			/*
			 * Can not count locking commands against total
			 * as they are allowed to block on server.
			 */
615 616

			/* update # of requests on the wire to server */
617
			if ((flags & CIFS_TIMEOUT_MASK) != CIFS_BLOCKING_OP) {
618 619
				*credits -= num_credits;
				server->in_flight += num_credits;
620 621
				if (server->in_flight > server->max_in_flight)
					server->max_in_flight = server->in_flight;
622
				*instance = server->reconnect_instance;
623
			}
P
Pavel Shilovsky 已提交
624
			spin_unlock(&server->req_lock);
625
			break;
L
Linus Torvalds 已提交
626 627
		}
	}
J
[CIFS]  
Jeremy Allison 已提交
628 629
	return 0;
}
L
Linus Torvalds 已提交
630

631
static int
632 633
wait_for_free_request(struct TCP_Server_Info *server, const int flags,
		      unsigned int *instance)
634
{
635 636
	return wait_for_free_credits(server, 1, -1, flags,
				     instance);
637 638
}

639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
static int
wait_for_compound_request(struct TCP_Server_Info *server, int num,
			  const int flags, unsigned int *instance)
{
	int *credits;

	credits = server->ops->get_credits_field(server, flags & CIFS_OP_MASK);

	spin_lock(&server->req_lock);
	if (*credits < num) {
		/*
		 * Return immediately if not too many requests in flight since
		 * we will likely be stuck on waiting for credits.
		 */
		if (server->in_flight < num - *credits) {
			spin_unlock(&server->req_lock);
			return -ENOTSUPP;
		}
	}
	spin_unlock(&server->req_lock);

	return wait_for_free_credits(server, num, 60000, flags,
				     instance);
}

664 665
int
cifs_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
666
		      unsigned int *num, struct cifs_credits *credits)
667 668
{
	*num = size;
669 670
	credits->value = 0;
	credits->instance = server->reconnect_instance;
671 672 673
	return 0;
}

674
static int allocate_mid(struct cifs_ses *ses, struct smb_hdr *in_buf,
J
[CIFS]  
Jeremy Allison 已提交
675 676
			struct mid_q_entry **ppmidQ)
{
L
Linus Torvalds 已提交
677
	if (ses->server->tcpStatus == CifsExiting) {
J
[CIFS]  
Jeremy Allison 已提交
678
		return -ENOENT;
679 680 681
	}

	if (ses->server->tcpStatus == CifsNeedReconnect) {
682
		cifs_dbg(FYI, "tcp session dead - return to caller to retry\n");
J
[CIFS]  
Jeremy Allison 已提交
683
		return -EAGAIN;
684 685
	}

686
	if (ses->status == CifsNew) {
S
Steve French 已提交
687
		if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
S
Steve French 已提交
688
			(in_buf->Command != SMB_COM_NEGOTIATE))
J
[CIFS]  
Jeremy Allison 已提交
689
			return -EAGAIN;
S
Steve French 已提交
690
		/* else ok - we are setting up session */
L
Linus Torvalds 已提交
691
	}
692 693 694 695 696 697 698 699

	if (ses->status == CifsExiting) {
		/* check if SMB session is bad because we are setting it up */
		if (in_buf->Command != SMB_COM_LOGOFF_ANDX)
			return -EAGAIN;
		/* else ok - we are shutting down session */
	}

700
	*ppmidQ = AllocMidQEntry(in_buf, ses->server);
701
	if (*ppmidQ == NULL)
J
[CIFS]  
Jeremy Allison 已提交
702
		return -ENOMEM;
703 704 705
	spin_lock(&GlobalMid_Lock);
	list_add_tail(&(*ppmidQ)->qhead, &ses->server->pending_mid_q);
	spin_unlock(&GlobalMid_Lock);
J
[CIFS]  
Jeremy Allison 已提交
706 707 708
	return 0;
}

709 710
static int
wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *midQ)
J
[CIFS]  
Jeremy Allison 已提交
711
{
712
	int error;
J
[CIFS]  
Jeremy Allison 已提交
713

714
	error = wait_event_freezekillable_unsafe(server->response_q,
715
				    midQ->mid_state != MID_REQUEST_SUBMITTED);
716 717
	if (error < 0)
		return -ERESTARTSYS;
J
[CIFS]  
Jeremy Allison 已提交
718

719
	return 0;
J
[CIFS]  
Jeremy Allison 已提交
720 721
}

722 723
struct mid_q_entry *
cifs_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
724 725
{
	int rc;
726
	struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
727 728
	struct mid_q_entry *mid;

729 730 731 732
	if (rqst->rq_iov[0].iov_len != 4 ||
	    rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
		return ERR_PTR(-EIO);

733
	/* enable signing if server requires it */
734
	if (server->sign)
735 736 737 738
		hdr->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;

	mid = AllocMidQEntry(hdr, server);
	if (mid == NULL)
739
		return ERR_PTR(-ENOMEM);
740

741
	rc = cifs_sign_rqst(rqst, server, &mid->sequence_number);
742 743
	if (rc) {
		DeleteMidQEntry(mid);
744
		return ERR_PTR(rc);
745 746
	}

747
	return mid;
748
}
749

J
Jeff Layton 已提交
750 751 752 753 754
/*
 * Send a SMB request and set the callback function in the mid to handle
 * the result. Caller is responsible for dealing with timeouts.
 */
int
755
cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst,
P
Pavel Shilovsky 已提交
756
		mid_receive_t *receive, mid_callback_t *callback,
757 758
		mid_handle_t *handle, void *cbdata, const int flags,
		const struct cifs_credits *exist_credits)
J
Jeff Layton 已提交
759
{
760
	int rc;
J
Jeff Layton 已提交
761
	struct mid_q_entry *mid;
762
	struct cifs_credits credits = { .value = 0, .instance = 0 };
763
	unsigned int instance;
764
	int optype;
J
Jeff Layton 已提交
765

766 767
	optype = flags & CIFS_OP_MASK;

768
	if ((flags & CIFS_HAS_CREDITS) == 0) {
769
		rc = wait_for_free_request(server, flags, &instance);
770 771
		if (rc)
			return rc;
772
		credits.value = 1;
773
		credits.instance = instance;
774 775
	} else
		instance = exist_credits->instance;
J
Jeff Layton 已提交
776 777

	mutex_lock(&server->srv_mutex);
778 779 780 781 782 783 784 785 786 787 788 789

	/*
	 * We can't use credits obtained from the previous session to send this
	 * request. Check if there were reconnects after we obtained credits and
	 * return -EAGAIN in such cases to let callers handle it.
	 */
	if (instance != server->reconnect_instance) {
		mutex_unlock(&server->srv_mutex);
		add_credits_and_wake_if(server, &credits, optype);
		return -EAGAIN;
	}

790 791
	mid = server->ops->setup_async_request(server, rqst);
	if (IS_ERR(mid)) {
J
Jeff Layton 已提交
792
		mutex_unlock(&server->srv_mutex);
793
		add_credits_and_wake_if(server, &credits, optype);
794
		return PTR_ERR(mid);
J
Jeff Layton 已提交
795 796
	}

797
	mid->receive = receive;
J
Jeff Layton 已提交
798 799
	mid->callback = callback;
	mid->callback_data = cbdata;
P
Pavel Shilovsky 已提交
800
	mid->handle = handle;
801
	mid->mid_state = MID_REQUEST_SUBMITTED;
802

803 804 805 806 807
	/* put it on the pending_mid_q */
	spin_lock(&GlobalMid_Lock);
	list_add_tail(&mid->qhead, &server->pending_mid_q);
	spin_unlock(&GlobalMid_Lock);

808 809 810 811 812
	/*
	 * Need to store the time in mid before calling I/O. For call_async,
	 * I/O response may come back and free the mid entry on another thread.
	 */
	cifs_save_when_sent(mid);
813
	cifs_in_send_inc(server);
814
	rc = smb_send_rqst(server, 1, rqst, flags);
815
	cifs_in_send_dec(server);
816

817
	if (rc < 0) {
818
		revert_current_mid(server, mid->credits);
819
		server->sequence_number -= 2;
820 821 822
		cifs_delete_mid(mid);
	}

J
Jeff Layton 已提交
823
	mutex_unlock(&server->srv_mutex);
824

825 826
	if (rc == 0)
		return 0;
J
Jeff Layton 已提交
827

828
	add_credits_and_wake_if(server, &credits, optype);
J
Jeff Layton 已提交
829 830 831
	return rc;
}

832 833 834 835 836 837 838 839 840 841
/*
 *
 * Send an SMB Request.  No response info (other than return code)
 * needs to be parsed.
 *
 * flags indicate the type of request buffer and how long to wait
 * and whether to log NT STATUS code (error) before mapping it to POSIX error
 *
 */
int
842
SendReceiveNoRsp(const unsigned int xid, struct cifs_ses *ses,
843
		 char *in_buf, int flags)
844 845 846
{
	int rc;
	struct kvec iov[1];
847
	struct kvec rsp_iov;
848 849
	int resp_buf_type;

850 851
	iov[0].iov_base = in_buf;
	iov[0].iov_len = get_rfc1002_length(in_buf) + 4;
852
	flags |= CIFS_NO_RSP_BUF;
853
	rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
854
	cifs_dbg(NOISY, "SendRcvNoRsp flags %d rc %d\n", flags, rc);
855

856 857 858
	return rc;
}

859
static int
860
cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
861 862 863
{
	int rc = 0;

864 865
	cifs_dbg(FYI, "%s: cmd=%d mid=%llu state=%d\n",
		 __func__, le16_to_cpu(mid->command), mid->mid, mid->mid_state);
866

J
Jeff Layton 已提交
867
	spin_lock(&GlobalMid_Lock);
868
	switch (mid->mid_state) {
J
Jeff Layton 已提交
869
	case MID_RESPONSE_RECEIVED:
870 871
		spin_unlock(&GlobalMid_Lock);
		return rc;
J
Jeff Layton 已提交
872 873 874
	case MID_RETRY_NEEDED:
		rc = -EAGAIN;
		break;
875 876 877
	case MID_RESPONSE_MALFORMED:
		rc = -EIO;
		break;
878 879 880
	case MID_SHUTDOWN:
		rc = -EHOSTDOWN;
		break;
J
Jeff Layton 已提交
881
	default:
882 883 884 885
		if (!(mid->mid_flags & MID_DELETED)) {
			list_del_init(&mid->qhead);
			mid->mid_flags |= MID_DELETED;
		}
886
		cifs_server_dbg(VFS, "%s: invalid mid state mid=%llu state=%d\n",
887
			 __func__, mid->mid, mid->mid_state);
J
Jeff Layton 已提交
888
		rc = -EIO;
889 890 891
	}
	spin_unlock(&GlobalMid_Lock);

892
	DeleteMidQEntry(mid);
893 894 895
	return rc;
}

896
static inline int
897 898
send_cancel(struct TCP_Server_Info *server, struct smb_rqst *rqst,
	    struct mid_q_entry *mid)
899
{
900
	return server->ops->send_cancel ?
901
				server->ops->send_cancel(server, rqst, mid) : 0;
902 903
}

904 905 906 907
int
cifs_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
		   bool log_error)
{
908
	unsigned int len = get_rfc1002_length(mid->resp_buf) + 4;
909 910

	dump_smb(mid->resp_buf, min_t(u32, 92, len));
911 912

	/* convert the length into a more usable form */
913
	if (server->sign) {
914
		struct kvec iov[2];
915
		int rc = 0;
916 917
		struct smb_rqst rqst = { .rq_iov = iov,
					 .rq_nvec = 2 };
918

919 920 921 922
		iov[0].iov_base = mid->resp_buf;
		iov[0].iov_len = 4;
		iov[1].iov_base = (char *)mid->resp_buf + 4;
		iov[1].iov_len = len - 4;
923
		/* FIXME: add code to kill session */
924
		rc = cifs_verify_signature(&rqst, server,
925
					   mid->sequence_number);
926
		if (rc)
927
			cifs_server_dbg(VFS, "SMB signature verification returned error = %d\n",
928
				 rc);
929 930 931 932 933 934
	}

	/* BB special case reconnect tid and uid here? */
	return map_smb_to_linux_error(mid->resp_buf, log_error);
}

935 936
struct mid_q_entry *
cifs_setup_request(struct cifs_ses *ses, struct smb_rqst *rqst)
937 938
{
	int rc;
939
	struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
940 941
	struct mid_q_entry *mid;

942 943 944 945
	if (rqst->rq_iov[0].iov_len != 4 ||
	    rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
		return ERR_PTR(-EIO);

946 947
	rc = allocate_mid(ses, hdr, &mid);
	if (rc)
948 949 950
		return ERR_PTR(rc);
	rc = cifs_sign_rqst(rqst, ses->server, &mid->sequence_number);
	if (rc) {
P
Pavel Shilovsky 已提交
951
		cifs_delete_mid(mid);
952 953 954
		return ERR_PTR(rc);
	}
	return mid;
955 956
}

957
static void
958
cifs_compound_callback(struct mid_q_entry *mid)
959 960
{
	struct TCP_Server_Info *server = mid->server;
961 962 963 964
	struct cifs_credits credits;

	credits.value = server->ops->get_credits(mid);
	credits.instance = server->reconnect_instance;
965

966
	add_credits(server, &credits, mid->optype);
967 968
}

969 970 971 972 973 974 975 976 977 978 979 980 981 982
static void
cifs_compound_last_callback(struct mid_q_entry *mid)
{
	cifs_compound_callback(mid);
	cifs_wake_up_task(mid);
}

static void
cifs_cancelled_callback(struct mid_q_entry *mid)
{
	cifs_compound_callback(mid);
	DeleteMidQEntry(mid);
}

983
int
R
Ronnie Sahlberg 已提交
984 985 986
compound_send_recv(const unsigned int xid, struct cifs_ses *ses,
		   const int flags, const int num_rqst, struct smb_rqst *rqst,
		   int *resp_buf_type, struct kvec *resp_iov)
J
[CIFS]  
Jeremy Allison 已提交
987
{
988
	int i, j, optype, rc = 0;
R
Ronnie Sahlberg 已提交
989
	struct mid_q_entry *midQ[MAX_COMPOUND];
990
	bool cancelled_mid[MAX_COMPOUND] = {false};
991 992 993 994
	struct cifs_credits credits[MAX_COMPOUND] = {
		{ .value = 0, .instance = 0 }
	};
	unsigned int instance;
995
	char *buf;
996
	struct TCP_Server_Info *server;
997

998
	optype = flags & CIFS_OP_MASK;
999

R
Ronnie Sahlberg 已提交
1000 1001
	for (i = 0; i < num_rqst; i++)
		resp_buf_type[i] = CIFS_NO_BUFFER;  /* no response buf yet */
J
[CIFS]  
Jeremy Allison 已提交
1002 1003

	if ((ses == NULL) || (ses->server == NULL)) {
1004
		cifs_dbg(VFS, "Null session\n");
J
[CIFS]  
Jeremy Allison 已提交
1005 1006 1007
		return -EIO;
	}

1008 1009
	server = ses->server;
	if (server->tcpStatus == CifsExiting)
J
[CIFS]  
Jeremy Allison 已提交
1010 1011
		return -ENOENT;

1012
	/*
1013
	 * Wait for all the requests to become available.
1014 1015
	 * This approach still leaves the possibility to be stuck waiting for
	 * credits if the server doesn't grant credits to the outstanding
1016 1017 1018
	 * requests and if the client is completely idle, not generating any
	 * other requests.
	 * This can be handled by the eventual session reconnect.
1019
	 */
1020
	rc = wait_for_compound_request(server, num_rqst, flags,
1021 1022 1023
				       &instance);
	if (rc)
		return rc;
1024

1025 1026 1027
	for (i = 0; i < num_rqst; i++) {
		credits[i].value = 1;
		credits[i].instance = instance;
1028
	}
J
[CIFS]  
Jeremy Allison 已提交
1029

1030 1031 1032 1033 1034
	/*
	 * Make sure that we sign in the same order that we send on this socket
	 * and avoid races inside tcp sendmsg code that could cause corruption
	 * of smb data.
	 */
J
[CIFS]  
Jeremy Allison 已提交
1035

1036
	mutex_lock(&server->srv_mutex);
J
[CIFS]  
Jeremy Allison 已提交
1037

1038 1039
	/*
	 * All the parts of the compound chain belong obtained credits from the
1040
	 * same session. We can not use credits obtained from the previous
1041 1042 1043 1044
	 * session to send this request. Check if there were reconnects after
	 * we obtained credits and return -EAGAIN in such cases to let callers
	 * handle it.
	 */
1045 1046
	if (instance != server->reconnect_instance) {
		mutex_unlock(&server->srv_mutex);
1047
		for (j = 0; j < num_rqst; j++)
1048
			add_credits(server, &credits[j], optype);
1049 1050 1051
		return -EAGAIN;
	}

R
Ronnie Sahlberg 已提交
1052
	for (i = 0; i < num_rqst; i++) {
1053
		midQ[i] = server->ops->setup_request(ses, &rqst[i]);
R
Ronnie Sahlberg 已提交
1054
		if (IS_ERR(midQ[i])) {
1055
			revert_current_mid(server, i);
R
Ronnie Sahlberg 已提交
1056 1057
			for (j = 0; j < i; j++)
				cifs_delete_mid(midQ[j]);
1058
			mutex_unlock(&server->srv_mutex);
1059

R
Ronnie Sahlberg 已提交
1060
			/* Update # of requests on wire to server */
1061
			for (j = 0; j < num_rqst; j++)
1062
				add_credits(server, &credits[j], optype);
R
Ronnie Sahlberg 已提交
1063 1064 1065 1066
			return PTR_ERR(midQ[i]);
		}

		midQ[i]->mid_state = MID_REQUEST_SUBMITTED;
1067
		midQ[i]->optype = optype;
1068
		/*
1069 1070 1071
		 * Invoke callback for every part of the compound chain
		 * to calculate credits properly. Wake up this thread only when
		 * the last element is received.
1072 1073
		 */
		if (i < num_rqst - 1)
1074 1075 1076
			midQ[i]->callback = cifs_compound_callback;
		else
			midQ[i]->callback = cifs_compound_last_callback;
L
Linus Torvalds 已提交
1077
	}
1078 1079 1080
	cifs_in_send_inc(server);
	rc = smb_send_rqst(server, num_rqst, rqst, flags);
	cifs_in_send_dec(server);
R
Ronnie Sahlberg 已提交
1081 1082 1083

	for (i = 0; i < num_rqst; i++)
		cifs_save_when_sent(midQ[i]);
J
[CIFS]  
Jeremy Allison 已提交
1084

1085
	if (rc < 0) {
1086 1087
		revert_current_mid(server, num_rqst);
		server->sequence_number -= 2;
1088
	}
R
Ronnie Sahlberg 已提交
1089

1090
	mutex_unlock(&server->srv_mutex);
J
[CIFS]  
Jeremy Allison 已提交
1091

1092 1093 1094 1095 1096
	/*
	 * If sending failed for some reason or it is an oplock break that we
	 * will not receive a response to - return credits back
	 */
	if (rc < 0 || (flags & CIFS_NO_SRV_RSP)) {
1097
		for (i = 0; i < num_rqst; i++)
1098
			add_credits(server, &credits[i], optype);
1099
		goto out;
1100 1101 1102 1103 1104 1105 1106 1107 1108
	}

	/*
	 * At this point the request is passed to the network stack - we assume
	 * that any credits taken from the server structure on the client have
	 * been spent and we can't return them back. Once we receive responses
	 * we will collect credits granted by the server in the mid callbacks
	 * and add those credits to the server structure.
	 */
R
Ronnie Sahlberg 已提交
1109

1110 1111 1112 1113 1114 1115
	/*
	 * Compounding is never used during session establish.
	 */
	if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP))
		smb311_update_preauth_hash(ses, rqst[0].rq_iov,
					   rqst[0].rq_nvec);
R
Ronnie Sahlberg 已提交
1116

1117
	for (i = 0; i < num_rqst; i++) {
1118
		rc = wait_for_response(server, midQ[i]);
1119 1120 1121 1122 1123
		if (rc != 0)
			break;
	}
	if (rc != 0) {
		for (; i < num_rqst; i++) {
1124
			cifs_server_dbg(VFS, "Cancelling wait for mid %llu cmd: %d\n",
1125
				 midQ[i]->mid, le16_to_cpu(midQ[i]->command));
1126
			send_cancel(server, &rqst[i], midQ[i]);
R
Ronnie Sahlberg 已提交
1127
			spin_lock(&GlobalMid_Lock);
1128
			midQ[i]->mid_flags |= MID_WAIT_CANCELLED;
R
Ronnie Sahlberg 已提交
1129
			if (midQ[i]->mid_state == MID_REQUEST_SUBMITTED) {
1130
				midQ[i]->callback = cifs_cancelled_callback;
1131
				cancelled_mid[i] = true;
1132
				credits[i].value = 0;
R
Ronnie Sahlberg 已提交
1133
			}
1134
			spin_unlock(&GlobalMid_Lock);
R
Ronnie Sahlberg 已提交
1135
		}
1136 1137 1138 1139 1140
	}

	for (i = 0; i < num_rqst; i++) {
		if (rc < 0)
			goto out;
R
Ronnie Sahlberg 已提交
1141

1142
		rc = cifs_sync_mid_result(midQ[i], server);
R
Ronnie Sahlberg 已提交
1143
		if (rc != 0) {
1144 1145 1146
			/* mark this mid as cancelled to not free it below */
			cancelled_mid[i] = true;
			goto out;
1147
		}
1148

R
Ronnie Sahlberg 已提交
1149 1150 1151 1152 1153 1154
		if (!midQ[i]->resp_buf ||
		    midQ[i]->mid_state != MID_RESPONSE_RECEIVED) {
			rc = -EIO;
			cifs_dbg(FYI, "Bad MID state?\n");
			goto out;
		}
1155

R
Ronnie Sahlberg 已提交
1156 1157 1158
		buf = (char *)midQ[i]->resp_buf;
		resp_iov[i].iov_base = buf;
		resp_iov[i].iov_len = midQ[i]->resp_buf_size +
1159
			server->vals->header_preamble_size;
R
Ronnie Sahlberg 已提交
1160 1161 1162 1163 1164 1165

		if (midQ[i]->large_buf)
			resp_buf_type[i] = CIFS_LARGE_BUFFER;
		else
			resp_buf_type[i] = CIFS_SMALL_BUFFER;

1166
		rc = server->ops->check_receive(midQ[i], server,
R
Ronnie Sahlberg 已提交
1167
						     flags & CIFS_LOG_ERROR);
L
Linus Torvalds 已提交
1168

R
Ronnie Sahlberg 已提交
1169
		/* mark it so buf will not be freed by cifs_delete_mid */
1170
		if ((flags & CIFS_NO_RSP_BUF) == 0)
R
Ronnie Sahlberg 已提交
1171
			midQ[i]->resp_buf = NULL;
1172

R
Ronnie Sahlberg 已提交
1173
	}
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185

	/*
	 * Compounding is never used during session establish.
	 */
	if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP)) {
		struct kvec iov = {
			.iov_base = resp_iov[0].iov_base,
			.iov_len = resp_iov[0].iov_len
		};
		smb311_update_preauth_hash(ses, &iov, 1);
	}

J
[CIFS]  
Jeremy Allison 已提交
1186
out:
1187 1188 1189 1190 1191 1192
	/*
	 * This will dequeue all mids. After this it is important that the
	 * demultiplex_thread will not process any of these mids any futher.
	 * This is prevented above by using a noop callback that will not
	 * wake this thread except for the very last PDU.
	 */
1193 1194 1195 1196
	for (i = 0; i < num_rqst; i++) {
		if (!cancelled_mid[i])
			cifs_delete_mid(midQ[i]);
	}
L
Linus Torvalds 已提交
1197

1198 1199
	return rc;
}
L
Linus Torvalds 已提交
1200

R
Ronnie Sahlberg 已提交
1201 1202 1203 1204 1205 1206 1207 1208 1209
int
cifs_send_recv(const unsigned int xid, struct cifs_ses *ses,
	       struct smb_rqst *rqst, int *resp_buf_type, const int flags,
	       struct kvec *resp_iov)
{
	return compound_send_recv(xid, ses, flags, 1, rqst, resp_buf_type,
				  resp_iov);
}

1210 1211 1212 1213 1214 1215
int
SendReceive2(const unsigned int xid, struct cifs_ses *ses,
	     struct kvec *iov, int n_vec, int *resp_buf_type /* ret */,
	     const int flags, struct kvec *resp_iov)
{
	struct smb_rqst rqst;
1216
	struct kvec s_iov[CIFS_MAX_IOV_SIZE], *new_iov;
1217 1218
	int rc;

1219
	if (n_vec + 1 > CIFS_MAX_IOV_SIZE) {
1220 1221
		new_iov = kmalloc_array(n_vec + 1, sizeof(struct kvec),
					GFP_KERNEL);
1222 1223 1224
		if (!new_iov) {
			/* otherwise cifs_send_recv below sets resp_buf_type */
			*resp_buf_type = CIFS_NO_BUFFER;
1225
			return -ENOMEM;
1226
		}
1227 1228
	} else
		new_iov = s_iov;
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242

	/* 1st iov is a RFC1001 length followed by the rest of the packet */
	memcpy(new_iov + 1, iov, (sizeof(struct kvec) * n_vec));

	new_iov[0].iov_base = new_iov[1].iov_base;
	new_iov[0].iov_len = 4;
	new_iov[1].iov_base += 4;
	new_iov[1].iov_len -= 4;

	memset(&rqst, 0, sizeof(struct smb_rqst));
	rqst.rq_iov = new_iov;
	rqst.rq_nvec = n_vec + 1;

	rc = cifs_send_recv(xid, ses, &rqst, resp_buf_type, flags, resp_iov);
1243 1244
	if (n_vec + 1 > CIFS_MAX_IOV_SIZE)
		kfree(new_iov);
1245 1246 1247
	return rc;
}

L
Linus Torvalds 已提交
1248
int
1249
SendReceive(const unsigned int xid, struct cifs_ses *ses,
L
Linus Torvalds 已提交
1250
	    struct smb_hdr *in_buf, struct smb_hdr *out_buf,
1251
	    int *pbytes_returned, const int flags)
L
Linus Torvalds 已提交
1252 1253 1254
{
	int rc = 0;
	struct mid_q_entry *midQ;
1255 1256 1257
	unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
	struct kvec iov = { .iov_base = in_buf, .iov_len = len };
	struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
1258
	struct cifs_credits credits = { .value = 1, .instance = 0 };
1259
	struct TCP_Server_Info *server;
L
Linus Torvalds 已提交
1260 1261

	if (ses == NULL) {
1262
		cifs_dbg(VFS, "Null smb session\n");
L
Linus Torvalds 已提交
1263 1264
		return -EIO;
	}
1265
	server = ses->server;
1266
	if (server == NULL) {
1267
		cifs_dbg(VFS, "Null tcp session\n");
L
Linus Torvalds 已提交
1268 1269 1270
		return -EIO;
	}

1271
	if (server->tcpStatus == CifsExiting)
1272 1273
		return -ENOENT;

S
Steve French 已提交
1274
	/* Ensure that we do not send more than 50 overlapping requests
L
Linus Torvalds 已提交
1275 1276 1277
	   to the same server. We may make this configurable later or
	   use ses->maxReq */

1278
	if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
1279
		cifs_server_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
1280
			 len);
1281 1282 1283
		return -EIO;
	}

1284
	rc = wait_for_free_request(server, flags, &credits.instance);
J
[CIFS]  
Jeremy Allison 已提交
1285 1286 1287
	if (rc)
		return rc;

S
Steve French 已提交
1288
	/* make sure that we sign in the same order that we send on this socket
L
Linus Torvalds 已提交
1289 1290 1291
	   and avoid races inside tcp sendmsg code that could cause corruption
	   of smb data */

1292
	mutex_lock(&server->srv_mutex);
L
Linus Torvalds 已提交
1293

J
[CIFS]  
Jeremy Allison 已提交
1294 1295
	rc = allocate_mid(ses, in_buf, &midQ);
	if (rc) {
1296
		mutex_unlock(&server->srv_mutex);
J
[CIFS]  
Jeremy Allison 已提交
1297
		/* Update # of requests on wire to server */
1298
		add_credits(server, &credits, 0);
J
[CIFS]  
Jeremy Allison 已提交
1299
		return rc;
L
Linus Torvalds 已提交
1300 1301
	}

1302
	rc = cifs_sign_smb(in_buf, server, &midQ->sequence_number);
1303
	if (rc) {
1304
		mutex_unlock(&server->srv_mutex);
1305 1306
		goto out;
	}
L
Linus Torvalds 已提交
1307

1308
	midQ->mid_state = MID_REQUEST_SUBMITTED;
1309

1310 1311 1312
	cifs_in_send_inc(server);
	rc = smb_send(server, in_buf, len);
	cifs_in_send_dec(server);
1313
	cifs_save_when_sent(midQ);
1314 1315

	if (rc < 0)
1316
		server->sequence_number -= 2;
1317

1318
	mutex_unlock(&server->srv_mutex);
J
[CIFS]  
Jeremy Allison 已提交
1319

S
Steve French 已提交
1320
	if (rc < 0)
J
[CIFS]  
Jeremy Allison 已提交
1321 1322
		goto out;

1323
	rc = wait_for_response(server, midQ);
1324
	if (rc != 0) {
1325
		send_cancel(server, &rqst, midQ);
1326
		spin_lock(&GlobalMid_Lock);
1327
		if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
1328 1329 1330
			/* no longer considered to be "in-flight" */
			midQ->callback = DeleteMidQEntry;
			spin_unlock(&GlobalMid_Lock);
1331
			add_credits(server, &credits, 0);
1332 1333 1334 1335
			return rc;
		}
		spin_unlock(&GlobalMid_Lock);
	}
L
Linus Torvalds 已提交
1336

1337
	rc = cifs_sync_mid_result(midQ, server);
1338
	if (rc != 0) {
1339
		add_credits(server, &credits, 0);
L
Linus Torvalds 已提交
1340 1341
		return rc;
	}
1342

1343
	if (!midQ->resp_buf || !out_buf ||
1344
	    midQ->mid_state != MID_RESPONSE_RECEIVED) {
1345
		rc = -EIO;
1346
		cifs_server_dbg(VFS, "Bad MID state?\n");
1347
		goto out;
L
Linus Torvalds 已提交
1348
	}
J
[CIFS]  
Jeremy Allison 已提交
1349

1350
	*pbytes_returned = get_rfc1002_length(midQ->resp_buf);
1351
	memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
1352
	rc = cifs_check_receive(midQ, server, 0);
J
[CIFS]  
Jeremy Allison 已提交
1353
out:
P
Pavel Shilovsky 已提交
1354
	cifs_delete_mid(midQ);
1355
	add_credits(server, &credits, 0);
L
Linus Torvalds 已提交
1356

J
[CIFS]  
Jeremy Allison 已提交
1357 1358
	return rc;
}
L
Linus Torvalds 已提交
1359

J
[CIFS]  
Jeremy Allison 已提交
1360 1361 1362 1363
/* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
   blocking lock to return. */

static int
1364
send_lock_cancel(const unsigned int xid, struct cifs_tcon *tcon,
J
[CIFS]  
Jeremy Allison 已提交
1365 1366 1367 1368
			struct smb_hdr *in_buf,
			struct smb_hdr *out_buf)
{
	int bytes_returned;
1369
	struct cifs_ses *ses = tcon->ses;
J
[CIFS]  
Jeremy Allison 已提交
1370 1371 1372 1373 1374 1375 1376 1377 1378
	LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;

	/* We just modify the current in_buf to change
	   the type of lock from LOCKING_ANDX_SHARED_LOCK
	   or LOCKING_ANDX_EXCLUSIVE_LOCK to
	   LOCKING_ANDX_CANCEL_LOCK. */

	pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
	pSMB->Timeout = 0;
1379
	pSMB->hdr.Mid = get_next_mid(ses->server);
J
[CIFS]  
Jeremy Allison 已提交
1380 1381

	return SendReceive(xid, ses, in_buf, out_buf,
1382
			&bytes_returned, 0);
J
[CIFS]  
Jeremy Allison 已提交
1383 1384 1385
}

int
1386
SendReceiveBlockingLock(const unsigned int xid, struct cifs_tcon *tcon,
J
[CIFS]  
Jeremy Allison 已提交
1387 1388 1389 1390 1391 1392
	    struct smb_hdr *in_buf, struct smb_hdr *out_buf,
	    int *pbytes_returned)
{
	int rc = 0;
	int rstart = 0;
	struct mid_q_entry *midQ;
1393
	struct cifs_ses *ses;
1394 1395 1396
	unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
	struct kvec iov = { .iov_base = in_buf, .iov_len = len };
	struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
1397
	unsigned int instance;
1398
	struct TCP_Server_Info *server;
J
[CIFS]  
Jeremy Allison 已提交
1399 1400

	if (tcon == NULL || tcon->ses == NULL) {
1401
		cifs_dbg(VFS, "Null smb session\n");
J
[CIFS]  
Jeremy Allison 已提交
1402 1403 1404
		return -EIO;
	}
	ses = tcon->ses;
1405
	server = ses->server;
J
[CIFS]  
Jeremy Allison 已提交
1406

1407
	if (server == NULL) {
1408
		cifs_dbg(VFS, "Null tcp session\n");
J
[CIFS]  
Jeremy Allison 已提交
1409 1410 1411
		return -EIO;
	}

1412
	if (server->tcpStatus == CifsExiting)
J
[CIFS]  
Jeremy Allison 已提交
1413 1414
		return -ENOENT;

S
Steve French 已提交
1415
	/* Ensure that we do not send more than 50 overlapping requests
J
[CIFS]  
Jeremy Allison 已提交
1416 1417 1418
	   to the same server. We may make this configurable later or
	   use ses->maxReq */

1419
	if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
1420
		cifs_tcon_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
1421
			 len);
1422 1423 1424
		return -EIO;
	}

1425
	rc = wait_for_free_request(server, CIFS_BLOCKING_OP, &instance);
J
[CIFS]  
Jeremy Allison 已提交
1426 1427 1428
	if (rc)
		return rc;

S
Steve French 已提交
1429
	/* make sure that we sign in the same order that we send on this socket
J
[CIFS]  
Jeremy Allison 已提交
1430 1431 1432
	   and avoid races inside tcp sendmsg code that could cause corruption
	   of smb data */

1433
	mutex_lock(&server->srv_mutex);
J
[CIFS]  
Jeremy Allison 已提交
1434 1435 1436

	rc = allocate_mid(ses, in_buf, &midQ);
	if (rc) {
1437
		mutex_unlock(&server->srv_mutex);
J
[CIFS]  
Jeremy Allison 已提交
1438 1439 1440
		return rc;
	}

1441
	rc = cifs_sign_smb(in_buf, server, &midQ->sequence_number);
1442
	if (rc) {
P
Pavel Shilovsky 已提交
1443
		cifs_delete_mid(midQ);
1444
		mutex_unlock(&server->srv_mutex);
1445 1446
		return rc;
	}
L
Linus Torvalds 已提交
1447

1448
	midQ->mid_state = MID_REQUEST_SUBMITTED;
1449 1450 1451
	cifs_in_send_inc(server);
	rc = smb_send(server, in_buf, len);
	cifs_in_send_dec(server);
1452
	cifs_save_when_sent(midQ);
1453 1454

	if (rc < 0)
1455
		server->sequence_number -= 2;
1456

1457
	mutex_unlock(&server->srv_mutex);
J
[CIFS]  
Jeremy Allison 已提交
1458

S
Steve French 已提交
1459
	if (rc < 0) {
P
Pavel Shilovsky 已提交
1460
		cifs_delete_mid(midQ);
J
[CIFS]  
Jeremy Allison 已提交
1461 1462 1463 1464
		return rc;
	}

	/* Wait for a reply - allow signals to interrupt. */
1465
	rc = wait_event_interruptible(server->response_q,
1466
		(!(midQ->mid_state == MID_REQUEST_SUBMITTED)) ||
1467 1468
		((server->tcpStatus != CifsGood) &&
		 (server->tcpStatus != CifsNew)));
J
[CIFS]  
Jeremy Allison 已提交
1469 1470 1471

	/* Were we interrupted by a signal ? */
	if ((rc == -ERESTARTSYS) &&
1472
		(midQ->mid_state == MID_REQUEST_SUBMITTED) &&
1473 1474
		((server->tcpStatus == CifsGood) ||
		 (server->tcpStatus == CifsNew))) {
J
[CIFS]  
Jeremy Allison 已提交
1475 1476 1477 1478

		if (in_buf->Command == SMB_COM_TRANSACTION2) {
			/* POSIX lock. We send a NT_CANCEL SMB to cause the
			   blocking lock to return. */
1479
			rc = send_cancel(server, &rqst, midQ);
J
[CIFS]  
Jeremy Allison 已提交
1480
			if (rc) {
P
Pavel Shilovsky 已提交
1481
				cifs_delete_mid(midQ);
J
[CIFS]  
Jeremy Allison 已提交
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492
				return rc;
			}
		} else {
			/* Windows lock. We send a LOCKINGX_CANCEL_LOCK
			   to cause the blocking lock to return. */

			rc = send_lock_cancel(xid, tcon, in_buf, out_buf);

			/* If we get -ENOLCK back the lock may have
			   already been removed. Don't exit in this case. */
			if (rc && rc != -ENOLCK) {
P
Pavel Shilovsky 已提交
1493
				cifs_delete_mid(midQ);
J
[CIFS]  
Jeremy Allison 已提交
1494 1495 1496 1497
				return rc;
			}
		}

1498
		rc = wait_for_response(server, midQ);
1499
		if (rc) {
1500
			send_cancel(server, &rqst, midQ);
1501
			spin_lock(&GlobalMid_Lock);
1502
			if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
1503 1504 1505 1506 1507 1508
				/* no longer considered to be "in-flight" */
				midQ->callback = DeleteMidQEntry;
				spin_unlock(&GlobalMid_Lock);
				return rc;
			}
			spin_unlock(&GlobalMid_Lock);
J
[CIFS]  
Jeremy Allison 已提交
1509
		}
1510 1511 1512

		/* We got the response - restart system call. */
		rstart = 1;
J
[CIFS]  
Jeremy Allison 已提交
1513 1514
	}

1515
	rc = cifs_sync_mid_result(midQ, server);
1516
	if (rc != 0)
J
[CIFS]  
Jeremy Allison 已提交
1517
		return rc;
1518

1519
	/* rcvd frame is ok */
1520
	if (out_buf == NULL || midQ->mid_state != MID_RESPONSE_RECEIVED) {
1521
		rc = -EIO;
1522
		cifs_tcon_dbg(VFS, "Bad MID state?\n");
1523 1524
		goto out;
	}
L
Linus Torvalds 已提交
1525

1526
	*pbytes_returned = get_rfc1002_length(midQ->resp_buf);
1527
	memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
1528
	rc = cifs_check_receive(midQ, server, 0);
1529
out:
P
Pavel Shilovsky 已提交
1530
	cifs_delete_mid(midQ);
J
[CIFS]  
Jeremy Allison 已提交
1531 1532
	if (rstart && rc == -EACCES)
		return -ERESTARTSYS;
L
Linus Torvalds 已提交
1533 1534
	return rc;
}