transport.c 40.9 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
	/*
	 * The default is for the mid to be synchronous, so the
	 * default callback just wakes up the current task.
	 */
79 80
	get_task_struct(current);
	temp->creator = current;
81 82
	temp->callback = cifs_wake_up_task;
	temp->callback_data = current;
L
Linus Torvalds 已提交
83 84

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

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

106
	midEntry->mid_state = MID_FREE;
107
	atomic_dec(&midCount);
108
	if (midEntry->large_buf)
109 110 111
		cifs_buf_release(midEntry->resp_buf);
	else
		cifs_small_buf_release(midEntry->resp_buf);
S
Steve French 已提交
112 113
#ifdef CONFIG_CIFS_STATS2
	now = jiffies;
114
	if (now < midEntry->when_alloc)
J
Joe Perches 已提交
115
		cifs_server_dbg(VFS, "Invalid mid allocation time\n");
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
	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;
	}
131 132 133 134 135 136 137 138 139 140 141 142
	/*
	 * 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)) &&
143
	    (midEntry->command != command)) {
144 145 146 147
		/*
		 * smb2slowcmd[NUMBER_OF_SMB2_COMMANDS] counts by command
		 * NB: le16_to_cpu returns unsigned so can not be negative below
		 */
148 149
		if (smb_cmd < NUMBER_OF_SMB2_COMMANDS)
			cifs_stats_inc(&server->smb2slowcmd[smb_cmd]);
150

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

	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)
{
177
	cifs_mid_q_entry_release(midEntry);
L
Linus Torvalds 已提交
178 179
}

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

	DeleteMidQEntry(mid);
}

193 194 195
/*
 * smb_send_kvec - send an array of kvecs to the server
 * @server:	Server to send the data to
196
 * @smb_msg:	Message to send
197 198 199 200 201
 * @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.
 */
202
static int
203 204
smb_send_kvec(struct TCP_Server_Info *server, struct msghdr *smb_msg,
	      size_t *sent)
L
Linus Torvalds 已提交
205 206
{
	int rc = 0;
207
	int retries = 0;
208
	struct socket *ssocket = server->ssocket;
209

210 211
	*sent = 0;

212 213 214 215
	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;
216
	if (server->noblocksnd)
217
		smb_msg->msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL;
218
	else
219
		smb_msg->msg_flags = MSG_NOSIGNAL;
L
Linus Torvalds 已提交
220

221
	while (msg_data_left(smb_msg)) {
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
		/*
		 * 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.
		 */
240
		rc = sock_sendmsg(ssocket, smb_msg);
241
		if (rc == -EAGAIN) {
242 243 244
			retries++;
			if (retries >= 14 ||
			    (!server->noblocksnd && (retries > 2))) {
245
				cifs_server_dbg(VFS, "sends on sock %p stuck for 15 seconds\n",
246
					 ssocket);
247
				return -EAGAIN;
L
Linus Torvalds 已提交
248
			}
249
			msleep(1 << retries);
L
Linus Torvalds 已提交
250 251
			continue;
		}
252

S
Steve French 已提交
253
		if (rc < 0)
254
			return rc;
255

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

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

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

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

288
	/* total up iov array first */
289
	for (i = 0; i < nvec; i++)
290 291
		buflen += iov[i].iov_len;

292 293 294 295 296 297
	/*
	 * 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.
	 */
298
	if (rqst->rq_npages) {
299 300 301 302 303 304 305 306 307 308 309
		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;
		}
310 311 312 313 314
	}

	return buflen;
}

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

330 331 332 333 334
	if (cifs_rdma_enabled(server)) {
		/* return -EAGAIN when connecting or reconnecting */
		rc = -EAGAIN;
		if (server->smbd_conn)
			rc = smbd_send(server, num_rqst, rqst);
335 336
		goto smbd_done;
	}
337

338
	if (ssocket == NULL)
339
		return -EAGAIN;
340

341
	if (signal_pending(current)) {
342 343
		cifs_dbg(FYI, "signal pending before send request\n");
		return -ERESTARTSYS;
344 345
	}

346
	/* cork the socket */
C
Christoph Hellwig 已提交
347
	tcp_sock_set_cork(ssocket->sk, true);
348

349
	for (j = 0; j < num_rqst; j++)
R
Ronnie Sahlberg 已提交
350
		send_length += smb_rqst_len(server, &rqst[j]);
351 352
	rfc1002_marker = cpu_to_be32(send_length);

353 354 355 356 357 358 359 360 361 362
	/*
	 * 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);

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

		total_len += sent;
		send_length += 4;
	}

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

380 381 382
	for (j = 0; j < num_rqst; j++) {
		iov = rqst[j].rq_iov;
		n_vec = rqst[j].rq_nvec;
383

384
		size = 0;
385 386
		for (i = 0; i < n_vec; i++) {
			dump_smb(iov[i].iov_base, iov[i].iov_len);
387
			size += iov[i].iov_len;
388
		}
389

390
		iov_iter_kvec(&smb_msg.msg_iter, WRITE, iov, n_vec, size);
391

392
		rc = smb_send_kvec(server, &smb_msg, &sent);
393
		if (rc < 0)
394
			goto unmask;
395 396

		total_len += sent;
397 398 399 400 401 402 403 404 405

		/* 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);

406
			iov_iter_bvec(&smb_msg.msg_iter, WRITE,
407 408 409 410 411 412 413
				      &bvec, 1, bvec.bv_len);
			rc = smb_send_kvec(server, &smb_msg, &sent);
			if (rc < 0)
				break;

			total_len += sent;
		}
414
	}
L
Linus Torvalds 已提交
415

416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
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;
	}

435
	/* uncork it */
C
Christoph Hellwig 已提交
436
	tcp_sock_set_cork(ssocket->sk, false);
437

438
	if ((total_len > 0) && (total_len != send_length)) {
439
		cifs_dbg(FYI, "partial send (wanted=%u sent=%zu): terminating session\n",
440
			 send_length, total_len);
441 442 443 444 445
		/*
		 * 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
		 */
446
		server->tcpStatus = CifsNeedReconnect;
S
Steve French 已提交
447 448
		trace_smb3_partial_send_reconnect(server->CurrentMid,
						  server->hostname);
449
	}
450
smbd_done:
451
	if (rc < 0 && rc != -EINTR)
452
		cifs_server_dbg(VFS, "Error %d sending data on socket to server\n",
453
			 rc);
454
	else if (rc > 0)
L
Linus Torvalds 已提交
455 456 457 458 459
		rc = 0;

	return rc;
}

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

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

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

475
	if (!server->ops->init_transform_rq) {
J
Joe Perches 已提交
476
		cifs_server_dbg(VFS, "Encryption requested but transform callback is missing\n");
477 478
		return -EIO;
	}
479

480 481 482 483 484 485 486 487 488 489 490 491 492
	tr_hdr = kmalloc(sizeof(*tr_hdr), GFP_NOFS);
	if (!tr_hdr)
		return -ENOMEM;

	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;

493 494
	rc = server->ops->init_transform_rq(server, num_rqst + 1,
					    &cur_rqst[0], rqst);
495
	if (rc)
496
		goto out;
497

498 499
	rc = __smb_send_rqst(server, num_rqst + 1, &cur_rqst[0]);
	smb3_free_compound_rqst(num_rqst, &cur_rqst[1]);
500 501
out:
	kfree(tr_hdr);
502
	return rc;
503 504
}

505 506 507 508
int
smb_send(struct TCP_Server_Info *server, struct smb_hdr *smb_buffer,
	 unsigned int smb_buf_length)
{
509
	struct kvec iov[2];
510 511
	struct smb_rqst rqst = { .rq_iov = iov,
				 .rq_nvec = 2 };
512

513 514 515 516
	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;
517

518
	return __smb_send_rqst(server, 1, &rqst);
519 520
}

P
Pavel Shilovsky 已提交
521
static int
522
wait_for_free_credits(struct TCP_Server_Info *server, const int num_credits,
523 524
		      const int timeout, const int flags,
		      unsigned int *instance)
L
Linus Torvalds 已提交
525
{
526
	long rc;
527 528
	int *credits;
	int optype;
529 530 531 532 533 534
	long int t;

	if (timeout < 0)
		t = MAX_JIFFY_OFFSET;
	else
		t = msecs_to_jiffies(timeout);
535 536

	optype = flags & CIFS_OP_MASK;
537

538 539
	*instance = 0;

540 541 542 543 544
	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 已提交
545
	spin_lock(&server->req_lock);
546
	if ((flags & CIFS_TIMEOUT_MASK) == CIFS_NON_BLOCKING) {
L
Linus Torvalds 已提交
547
		/* oplock breaks must not be held up */
P
Pavel Shilovsky 已提交
548
		server->in_flight++;
549 550
		if (server->in_flight > server->max_in_flight)
			server->max_in_flight = server->in_flight;
551
		*credits -= 1;
552
		*instance = server->reconnect_instance;
P
Pavel Shilovsky 已提交
553
		spin_unlock(&server->req_lock);
554 555 556 557
		return 0;
	}

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

580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
			/*
			 * 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);
598 599
				rc = wait_event_killable_timeout(
					server->request_q,
600
					has_credits(server, credits,
601 602
						    MAX_COMPOUND + 1),
					t);
603
				cifs_num_waiters_dec(server);
604
				if (!rc) {
605 606
					trace_smb3_credit_timeout(
						server->CurrentMid,
607 608
						server->hostname, num_credits,
						0);
609
					cifs_server_dbg(VFS, "wait timed out after %d ms\n",
610 611 612 613 614
						 timeout);
					return -ENOTSUPP;
				}
				if (rc == -ERESTARTSYS)
					return -ERESTARTSYS;
615 616 617 618
				spin_lock(&server->req_lock);
				continue;
			}

619 620 621 622
			/*
			 * Can not count locking commands against total
			 * as they are allowed to block on server.
			 */
623 624

			/* update # of requests on the wire to server */
625
			if ((flags & CIFS_TIMEOUT_MASK) != CIFS_BLOCKING_OP) {
626 627
				*credits -= num_credits;
				server->in_flight += num_credits;
628 629
				if (server->in_flight > server->max_in_flight)
					server->max_in_flight = server->in_flight;
630
				*instance = server->reconnect_instance;
631
			}
P
Pavel Shilovsky 已提交
632
			spin_unlock(&server->req_lock);
633
			break;
L
Linus Torvalds 已提交
634 635
		}
	}
J
[CIFS]  
Jeremy Allison 已提交
636 637
	return 0;
}
L
Linus Torvalds 已提交
638

639
static int
640 641
wait_for_free_request(struct TCP_Server_Info *server, const int flags,
		      unsigned int *instance)
642
{
643 644
	return wait_for_free_credits(server, 1, -1, flags,
				     instance);
645 646
}

647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
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);
}

672 673
int
cifs_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
674
		      unsigned int *num, struct cifs_credits *credits)
675 676
{
	*num = size;
677 678
	credits->value = 0;
	credits->instance = server->reconnect_instance;
679 680 681
	return 0;
}

682
static int allocate_mid(struct cifs_ses *ses, struct smb_hdr *in_buf,
J
[CIFS]  
Jeremy Allison 已提交
683 684
			struct mid_q_entry **ppmidQ)
{
L
Linus Torvalds 已提交
685
	if (ses->server->tcpStatus == CifsExiting) {
J
[CIFS]  
Jeremy Allison 已提交
686
		return -ENOENT;
687 688 689
	}

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

694
	if (ses->status == CifsNew) {
S
Steve French 已提交
695
		if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
S
Steve French 已提交
696
			(in_buf->Command != SMB_COM_NEGOTIATE))
J
[CIFS]  
Jeremy Allison 已提交
697
			return -EAGAIN;
S
Steve French 已提交
698
		/* else ok - we are setting up session */
L
Linus Torvalds 已提交
699
	}
700 701 702 703 704 705 706 707

	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 */
	}

708
	*ppmidQ = AllocMidQEntry(in_buf, ses->server);
709
	if (*ppmidQ == NULL)
J
[CIFS]  
Jeremy Allison 已提交
710
		return -ENOMEM;
711 712 713
	spin_lock(&GlobalMid_Lock);
	list_add_tail(&(*ppmidQ)->qhead, &ses->server->pending_mid_q);
	spin_unlock(&GlobalMid_Lock);
J
[CIFS]  
Jeremy Allison 已提交
714 715 716
	return 0;
}

717 718
static int
wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *midQ)
J
[CIFS]  
Jeremy Allison 已提交
719
{
720
	int error;
J
[CIFS]  
Jeremy Allison 已提交
721

722
	error = wait_event_freezekillable_unsafe(server->response_q,
723
				    midQ->mid_state != MID_REQUEST_SUBMITTED);
724 725
	if (error < 0)
		return -ERESTARTSYS;
J
[CIFS]  
Jeremy Allison 已提交
726

727
	return 0;
J
[CIFS]  
Jeremy Allison 已提交
728 729
}

730 731
struct mid_q_entry *
cifs_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
732 733
{
	int rc;
734
	struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
735 736
	struct mid_q_entry *mid;

737 738 739 740
	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);

741
	/* enable signing if server requires it */
742
	if (server->sign)
743 744 745 746
		hdr->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;

	mid = AllocMidQEntry(hdr, server);
	if (mid == NULL)
747
		return ERR_PTR(-ENOMEM);
748

749
	rc = cifs_sign_rqst(rqst, server, &mid->sequence_number);
750 751
	if (rc) {
		DeleteMidQEntry(mid);
752
		return ERR_PTR(rc);
753 754
	}

755
	return mid;
756
}
757

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

774 775
	optype = flags & CIFS_OP_MASK;

776
	if ((flags & CIFS_HAS_CREDITS) == 0) {
777
		rc = wait_for_free_request(server, flags, &instance);
778 779
		if (rc)
			return rc;
780
		credits.value = 1;
781
		credits.instance = instance;
782 783
	} else
		instance = exist_credits->instance;
J
Jeff Layton 已提交
784 785

	mutex_lock(&server->srv_mutex);
786 787 788 789 790 791 792 793 794 795 796 797

	/*
	 * 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;
	}

798 799
	mid = server->ops->setup_async_request(server, rqst);
	if (IS_ERR(mid)) {
J
Jeff Layton 已提交
800
		mutex_unlock(&server->srv_mutex);
801
		add_credits_and_wake_if(server, &credits, optype);
802
		return PTR_ERR(mid);
J
Jeff Layton 已提交
803 804
	}

805
	mid->receive = receive;
J
Jeff Layton 已提交
806 807
	mid->callback = callback;
	mid->callback_data = cbdata;
P
Pavel Shilovsky 已提交
808
	mid->handle = handle;
809
	mid->mid_state = MID_REQUEST_SUBMITTED;
810

811 812 813 814 815
	/* put it on the pending_mid_q */
	spin_lock(&GlobalMid_Lock);
	list_add_tail(&mid->qhead, &server->pending_mid_q);
	spin_unlock(&GlobalMid_Lock);

816 817 818 819 820
	/*
	 * 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);
821
	cifs_in_send_inc(server);
822
	rc = smb_send_rqst(server, 1, rqst, flags);
823
	cifs_in_send_dec(server);
824

825
	if (rc < 0) {
826
		revert_current_mid(server, mid->credits);
827
		server->sequence_number -= 2;
828 829 830
		cifs_delete_mid(mid);
	}

J
Jeff Layton 已提交
831
	mutex_unlock(&server->srv_mutex);
832

833 834
	if (rc == 0)
		return 0;
J
Jeff Layton 已提交
835

836
	add_credits_and_wake_if(server, &credits, optype);
J
Jeff Layton 已提交
837 838 839
	return rc;
}

840 841 842 843 844 845 846 847 848 849
/*
 *
 * 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
850
SendReceiveNoRsp(const unsigned int xid, struct cifs_ses *ses,
851
		 char *in_buf, int flags)
852 853 854
{
	int rc;
	struct kvec iov[1];
855
	struct kvec rsp_iov;
856 857
	int resp_buf_type;

858 859
	iov[0].iov_base = in_buf;
	iov[0].iov_len = get_rfc1002_length(in_buf) + 4;
860
	flags |= CIFS_NO_RSP_BUF;
861
	rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
862
	cifs_dbg(NOISY, "SendRcvNoRsp flags %d rc %d\n", flags, rc);
863

864 865 866
	return rc;
}

867
static int
868
cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
869 870 871
{
	int rc = 0;

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

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

900
	DeleteMidQEntry(mid);
901 902 903
	return rc;
}

904
static inline int
905 906
send_cancel(struct TCP_Server_Info *server, struct smb_rqst *rqst,
	    struct mid_q_entry *mid)
907
{
908
	return server->ops->send_cancel ?
909
				server->ops->send_cancel(server, rqst, mid) : 0;
910 911
}

912 913 914 915
int
cifs_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
		   bool log_error)
{
916
	unsigned int len = get_rfc1002_length(mid->resp_buf) + 4;
917 918

	dump_smb(mid->resp_buf, min_t(u32, 92, len));
919 920

	/* convert the length into a more usable form */
921
	if (server->sign) {
922
		struct kvec iov[2];
923
		int rc = 0;
924 925
		struct smb_rqst rqst = { .rq_iov = iov,
					 .rq_nvec = 2 };
926

927 928 929 930
		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;
931
		/* FIXME: add code to kill session */
932
		rc = cifs_verify_signature(&rqst, server,
933
					   mid->sequence_number);
934
		if (rc)
935
			cifs_server_dbg(VFS, "SMB signature verification returned error = %d\n",
936
				 rc);
937 938 939
	}

	/* BB special case reconnect tid and uid here? */
940
	return map_and_check_smb_error(mid, log_error);
941 942
}

943
struct mid_q_entry *
A
Aurelien Aptel 已提交
944 945
cifs_setup_request(struct cifs_ses *ses, struct TCP_Server_Info *ignored,
		   struct smb_rqst *rqst)
946 947
{
	int rc;
948
	struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
949 950
	struct mid_q_entry *mid;

951 952 953 954
	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);

955 956
	rc = allocate_mid(ses, hdr, &mid);
	if (rc)
957 958 959
		return ERR_PTR(rc);
	rc = cifs_sign_rqst(rqst, ses->server, &mid->sequence_number);
	if (rc) {
P
Pavel Shilovsky 已提交
960
		cifs_delete_mid(mid);
961 962 963
		return ERR_PTR(rc);
	}
	return mid;
964 965
}

966
static void
967
cifs_compound_callback(struct mid_q_entry *mid)
968 969
{
	struct TCP_Server_Info *server = mid->server;
970 971 972 973
	struct cifs_credits credits;

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

975
	add_credits(server, &credits, mid->optype);
976 977
}

978 979 980 981 982 983 984 985 986 987 988 989 990 991
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);
}

992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
/*
 * Return a channel (master if none) of @ses that can be used to send
 * regular requests.
 *
 * If we are currently binding a new channel (negprot/sess.setup),
 * return the new incomplete channel.
 */
struct TCP_Server_Info *cifs_pick_channel(struct cifs_ses *ses)
{
	uint index = 0;

	if (!ses)
		return NULL;

	if (!ses->binding) {
		/* round robin */
		if (ses->chan_count > 1) {
			index = (uint)atomic_inc_return(&ses->chan_seq);
			index %= ses->chan_count;
		}
		return ses->chans[index].server;
	} else {
		return cifs_ses_server(ses);
	}
}

1018
int
R
Ronnie Sahlberg 已提交
1019
compound_send_recv(const unsigned int xid, struct cifs_ses *ses,
1020
		   struct TCP_Server_Info *server,
R
Ronnie Sahlberg 已提交
1021 1022
		   const int flags, const int num_rqst, struct smb_rqst *rqst,
		   int *resp_buf_type, struct kvec *resp_iov)
J
[CIFS]  
Jeremy Allison 已提交
1023
{
1024
	int i, j, optype, rc = 0;
R
Ronnie Sahlberg 已提交
1025
	struct mid_q_entry *midQ[MAX_COMPOUND];
1026
	bool cancelled_mid[MAX_COMPOUND] = {false};
1027 1028 1029 1030
	struct cifs_credits credits[MAX_COMPOUND] = {
		{ .value = 0, .instance = 0 }
	};
	unsigned int instance;
1031
	char *buf;
1032

1033
	optype = flags & CIFS_OP_MASK;
1034

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

1038
	if (!ses || !ses->server || !server) {
1039
		cifs_dbg(VFS, "Null session\n");
J
[CIFS]  
Jeremy Allison 已提交
1040 1041 1042
		return -EIO;
	}

1043
	if (server->tcpStatus == CifsExiting)
J
[CIFS]  
Jeremy Allison 已提交
1044 1045
		return -ENOENT;

1046
	/*
1047
	 * Wait for all the requests to become available.
1048 1049
	 * This approach still leaves the possibility to be stuck waiting for
	 * credits if the server doesn't grant credits to the outstanding
1050 1051 1052
	 * requests and if the client is completely idle, not generating any
	 * other requests.
	 * This can be handled by the eventual session reconnect.
1053
	 */
1054
	rc = wait_for_compound_request(server, num_rqst, flags,
1055 1056 1057
				       &instance);
	if (rc)
		return rc;
1058

1059 1060 1061
	for (i = 0; i < num_rqst; i++) {
		credits[i].value = 1;
		credits[i].instance = instance;
1062
	}
J
[CIFS]  
Jeremy Allison 已提交
1063

1064 1065 1066 1067 1068
	/*
	 * 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 已提交
1069

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

1072 1073
	/*
	 * All the parts of the compound chain belong obtained credits from the
1074
	 * same session. We can not use credits obtained from the previous
1075 1076 1077 1078
	 * 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.
	 */
1079 1080
	if (instance != server->reconnect_instance) {
		mutex_unlock(&server->srv_mutex);
1081
		for (j = 0; j < num_rqst; j++)
1082
			add_credits(server, &credits[j], optype);
1083 1084 1085
		return -EAGAIN;
	}

R
Ronnie Sahlberg 已提交
1086
	for (i = 0; i < num_rqst; i++) {
A
Aurelien Aptel 已提交
1087
		midQ[i] = server->ops->setup_request(ses, server, &rqst[i]);
R
Ronnie Sahlberg 已提交
1088
		if (IS_ERR(midQ[i])) {
1089
			revert_current_mid(server, i);
R
Ronnie Sahlberg 已提交
1090 1091
			for (j = 0; j < i; j++)
				cifs_delete_mid(midQ[j]);
1092
			mutex_unlock(&server->srv_mutex);
1093

R
Ronnie Sahlberg 已提交
1094
			/* Update # of requests on wire to server */
1095
			for (j = 0; j < num_rqst; j++)
1096
				add_credits(server, &credits[j], optype);
R
Ronnie Sahlberg 已提交
1097 1098 1099 1100
			return PTR_ERR(midQ[i]);
		}

		midQ[i]->mid_state = MID_REQUEST_SUBMITTED;
1101
		midQ[i]->optype = optype;
1102
		/*
1103 1104 1105
		 * Invoke callback for every part of the compound chain
		 * to calculate credits properly. Wake up this thread only when
		 * the last element is received.
1106 1107
		 */
		if (i < num_rqst - 1)
1108 1109 1110
			midQ[i]->callback = cifs_compound_callback;
		else
			midQ[i]->callback = cifs_compound_last_callback;
L
Linus Torvalds 已提交
1111
	}
1112 1113 1114
	cifs_in_send_inc(server);
	rc = smb_send_rqst(server, num_rqst, rqst, flags);
	cifs_in_send_dec(server);
R
Ronnie Sahlberg 已提交
1115 1116 1117

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

1119
	if (rc < 0) {
1120 1121
		revert_current_mid(server, num_rqst);
		server->sequence_number -= 2;
1122
	}
R
Ronnie Sahlberg 已提交
1123

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

1126 1127 1128 1129 1130
	/*
	 * 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)) {
1131
		for (i = 0; i < num_rqst; i++)
1132
			add_credits(server, &credits[i], optype);
1133
		goto out;
1134 1135 1136 1137 1138 1139 1140 1141 1142
	}

	/*
	 * 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 已提交
1143

1144 1145 1146 1147 1148 1149
	/*
	 * 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 已提交
1150

1151
	for (i = 0; i < num_rqst; i++) {
1152
		rc = wait_for_response(server, midQ[i]);
1153 1154 1155 1156 1157
		if (rc != 0)
			break;
	}
	if (rc != 0) {
		for (; i < num_rqst; i++) {
1158
			cifs_server_dbg(VFS, "Cancelling wait for mid %llu cmd: %d\n",
1159
				 midQ[i]->mid, le16_to_cpu(midQ[i]->command));
1160
			send_cancel(server, &rqst[i], midQ[i]);
R
Ronnie Sahlberg 已提交
1161
			spin_lock(&GlobalMid_Lock);
1162
			midQ[i]->mid_flags |= MID_WAIT_CANCELLED;
R
Ronnie Sahlberg 已提交
1163
			if (midQ[i]->mid_state == MID_REQUEST_SUBMITTED) {
1164
				midQ[i]->callback = cifs_cancelled_callback;
1165
				cancelled_mid[i] = true;
1166
				credits[i].value = 0;
R
Ronnie Sahlberg 已提交
1167
			}
1168
			spin_unlock(&GlobalMid_Lock);
R
Ronnie Sahlberg 已提交
1169
		}
1170 1171 1172 1173 1174
	}

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

1176
		rc = cifs_sync_mid_result(midQ[i], server);
R
Ronnie Sahlberg 已提交
1177
		if (rc != 0) {
1178 1179 1180
			/* mark this mid as cancelled to not free it below */
			cancelled_mid[i] = true;
			goto out;
1181
		}
1182

R
Ronnie Sahlberg 已提交
1183 1184 1185 1186 1187 1188
		if (!midQ[i]->resp_buf ||
		    midQ[i]->mid_state != MID_RESPONSE_RECEIVED) {
			rc = -EIO;
			cifs_dbg(FYI, "Bad MID state?\n");
			goto out;
		}
1189

R
Ronnie Sahlberg 已提交
1190 1191 1192
		buf = (char *)midQ[i]->resp_buf;
		resp_iov[i].iov_base = buf;
		resp_iov[i].iov_len = midQ[i]->resp_buf_size +
1193
			server->vals->header_preamble_size;
R
Ronnie Sahlberg 已提交
1194 1195 1196 1197 1198 1199

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

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

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

R
Ronnie Sahlberg 已提交
1207
	}
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219

	/*
	 * 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 已提交
1220
out:
1221 1222 1223 1224 1225 1226
	/*
	 * 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.
	 */
1227 1228 1229 1230
	for (i = 0; i < num_rqst; i++) {
		if (!cancelled_mid[i])
			cifs_delete_mid(midQ[i]);
	}
L
Linus Torvalds 已提交
1231

1232 1233
	return rc;
}
L
Linus Torvalds 已提交
1234

R
Ronnie Sahlberg 已提交
1235 1236
int
cifs_send_recv(const unsigned int xid, struct cifs_ses *ses,
1237
	       struct TCP_Server_Info *server,
R
Ronnie Sahlberg 已提交
1238 1239 1240
	       struct smb_rqst *rqst, int *resp_buf_type, const int flags,
	       struct kvec *resp_iov)
{
1241 1242
	return compound_send_recv(xid, ses, server, flags, 1,
				  rqst, resp_buf_type, resp_iov);
R
Ronnie Sahlberg 已提交
1243 1244
}

1245 1246 1247 1248 1249 1250
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;
1251
	struct kvec s_iov[CIFS_MAX_IOV_SIZE], *new_iov;
1252 1253
	int rc;

1254
	if (n_vec + 1 > CIFS_MAX_IOV_SIZE) {
1255 1256
		new_iov = kmalloc_array(n_vec + 1, sizeof(struct kvec),
					GFP_KERNEL);
1257 1258 1259
		if (!new_iov) {
			/* otherwise cifs_send_recv below sets resp_buf_type */
			*resp_buf_type = CIFS_NO_BUFFER;
1260
			return -ENOMEM;
1261
		}
1262 1263
	} else
		new_iov = s_iov;
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276

	/* 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;

1277 1278
	rc = cifs_send_recv(xid, ses, ses->server,
			    &rqst, resp_buf_type, flags, resp_iov);
1279 1280
	if (n_vec + 1 > CIFS_MAX_IOV_SIZE)
		kfree(new_iov);
1281 1282 1283
	return rc;
}

L
Linus Torvalds 已提交
1284
int
1285
SendReceive(const unsigned int xid, struct cifs_ses *ses,
L
Linus Torvalds 已提交
1286
	    struct smb_hdr *in_buf, struct smb_hdr *out_buf,
1287
	    int *pbytes_returned, const int flags)
L
Linus Torvalds 已提交
1288 1289 1290
{
	int rc = 0;
	struct mid_q_entry *midQ;
1291 1292 1293
	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 };
1294
	struct cifs_credits credits = { .value = 1, .instance = 0 };
1295
	struct TCP_Server_Info *server;
L
Linus Torvalds 已提交
1296 1297

	if (ses == NULL) {
1298
		cifs_dbg(VFS, "Null smb session\n");
L
Linus Torvalds 已提交
1299 1300
		return -EIO;
	}
1301
	server = ses->server;
1302
	if (server == NULL) {
1303
		cifs_dbg(VFS, "Null tcp session\n");
L
Linus Torvalds 已提交
1304 1305 1306
		return -EIO;
	}

1307
	if (server->tcpStatus == CifsExiting)
1308 1309
		return -ENOENT;

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

1314
	if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
J
Joe Perches 已提交
1315 1316
		cifs_server_dbg(VFS, "Invalid length, greater than maximum frame, %d\n",
				len);
1317 1318 1319
		return -EIO;
	}

1320
	rc = wait_for_free_request(server, flags, &credits.instance);
J
[CIFS]  
Jeremy Allison 已提交
1321 1322 1323
	if (rc)
		return rc;

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

1328
	mutex_lock(&server->srv_mutex);
L
Linus Torvalds 已提交
1329

J
[CIFS]  
Jeremy Allison 已提交
1330 1331
	rc = allocate_mid(ses, in_buf, &midQ);
	if (rc) {
1332
		mutex_unlock(&server->srv_mutex);
J
[CIFS]  
Jeremy Allison 已提交
1333
		/* Update # of requests on wire to server */
1334
		add_credits(server, &credits, 0);
J
[CIFS]  
Jeremy Allison 已提交
1335
		return rc;
L
Linus Torvalds 已提交
1336 1337
	}

1338
	rc = cifs_sign_smb(in_buf, server, &midQ->sequence_number);
1339
	if (rc) {
1340
		mutex_unlock(&server->srv_mutex);
1341 1342
		goto out;
	}
L
Linus Torvalds 已提交
1343

1344
	midQ->mid_state = MID_REQUEST_SUBMITTED;
1345

1346 1347 1348
	cifs_in_send_inc(server);
	rc = smb_send(server, in_buf, len);
	cifs_in_send_dec(server);
1349
	cifs_save_when_sent(midQ);
1350 1351

	if (rc < 0)
1352
		server->sequence_number -= 2;
1353

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

S
Steve French 已提交
1356
	if (rc < 0)
J
[CIFS]  
Jeremy Allison 已提交
1357 1358
		goto out;

1359
	rc = wait_for_response(server, midQ);
1360
	if (rc != 0) {
1361
		send_cancel(server, &rqst, midQ);
1362
		spin_lock(&GlobalMid_Lock);
1363
		if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
1364 1365 1366
			/* no longer considered to be "in-flight" */
			midQ->callback = DeleteMidQEntry;
			spin_unlock(&GlobalMid_Lock);
1367
			add_credits(server, &credits, 0);
1368 1369 1370 1371
			return rc;
		}
		spin_unlock(&GlobalMid_Lock);
	}
L
Linus Torvalds 已提交
1372

1373
	rc = cifs_sync_mid_result(midQ, server);
1374
	if (rc != 0) {
1375
		add_credits(server, &credits, 0);
L
Linus Torvalds 已提交
1376 1377
		return rc;
	}
1378

1379
	if (!midQ->resp_buf || !out_buf ||
1380
	    midQ->mid_state != MID_RESPONSE_RECEIVED) {
1381
		rc = -EIO;
1382
		cifs_server_dbg(VFS, "Bad MID state?\n");
1383
		goto out;
L
Linus Torvalds 已提交
1384
	}
J
[CIFS]  
Jeremy Allison 已提交
1385

1386
	*pbytes_returned = get_rfc1002_length(midQ->resp_buf);
1387
	memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
1388
	rc = cifs_check_receive(midQ, server, 0);
J
[CIFS]  
Jeremy Allison 已提交
1389
out:
P
Pavel Shilovsky 已提交
1390
	cifs_delete_mid(midQ);
1391
	add_credits(server, &credits, 0);
L
Linus Torvalds 已提交
1392

J
[CIFS]  
Jeremy Allison 已提交
1393 1394
	return rc;
}
L
Linus Torvalds 已提交
1395

J
[CIFS]  
Jeremy Allison 已提交
1396 1397 1398 1399
/* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
   blocking lock to return. */

static int
1400
send_lock_cancel(const unsigned int xid, struct cifs_tcon *tcon,
J
[CIFS]  
Jeremy Allison 已提交
1401 1402 1403 1404
			struct smb_hdr *in_buf,
			struct smb_hdr *out_buf)
{
	int bytes_returned;
1405
	struct cifs_ses *ses = tcon->ses;
J
[CIFS]  
Jeremy Allison 已提交
1406 1407 1408 1409 1410 1411 1412 1413 1414
	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;
1415
	pSMB->hdr.Mid = get_next_mid(ses->server);
J
[CIFS]  
Jeremy Allison 已提交
1416 1417

	return SendReceive(xid, ses, in_buf, out_buf,
1418
			&bytes_returned, 0);
J
[CIFS]  
Jeremy Allison 已提交
1419 1420 1421
}

int
1422
SendReceiveBlockingLock(const unsigned int xid, struct cifs_tcon *tcon,
J
[CIFS]  
Jeremy Allison 已提交
1423 1424 1425 1426 1427 1428
	    struct smb_hdr *in_buf, struct smb_hdr *out_buf,
	    int *pbytes_returned)
{
	int rc = 0;
	int rstart = 0;
	struct mid_q_entry *midQ;
1429
	struct cifs_ses *ses;
1430 1431 1432
	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 };
1433
	unsigned int instance;
1434
	struct TCP_Server_Info *server;
J
[CIFS]  
Jeremy Allison 已提交
1435 1436

	if (tcon == NULL || tcon->ses == NULL) {
1437
		cifs_dbg(VFS, "Null smb session\n");
J
[CIFS]  
Jeremy Allison 已提交
1438 1439 1440
		return -EIO;
	}
	ses = tcon->ses;
1441
	server = ses->server;
J
[CIFS]  
Jeremy Allison 已提交
1442

1443
	if (server == NULL) {
1444
		cifs_dbg(VFS, "Null tcp session\n");
J
[CIFS]  
Jeremy Allison 已提交
1445 1446 1447
		return -EIO;
	}

1448
	if (server->tcpStatus == CifsExiting)
J
[CIFS]  
Jeremy Allison 已提交
1449 1450
		return -ENOENT;

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

1455
	if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
J
Joe Perches 已提交
1456 1457
		cifs_tcon_dbg(VFS, "Invalid length, greater than maximum frame, %d\n",
			      len);
1458 1459 1460
		return -EIO;
	}

1461
	rc = wait_for_free_request(server, CIFS_BLOCKING_OP, &instance);
J
[CIFS]  
Jeremy Allison 已提交
1462 1463 1464
	if (rc)
		return rc;

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

1469
	mutex_lock(&server->srv_mutex);
J
[CIFS]  
Jeremy Allison 已提交
1470 1471 1472

	rc = allocate_mid(ses, in_buf, &midQ);
	if (rc) {
1473
		mutex_unlock(&server->srv_mutex);
J
[CIFS]  
Jeremy Allison 已提交
1474 1475 1476
		return rc;
	}

1477
	rc = cifs_sign_smb(in_buf, server, &midQ->sequence_number);
1478
	if (rc) {
P
Pavel Shilovsky 已提交
1479
		cifs_delete_mid(midQ);
1480
		mutex_unlock(&server->srv_mutex);
1481 1482
		return rc;
	}
L
Linus Torvalds 已提交
1483

1484
	midQ->mid_state = MID_REQUEST_SUBMITTED;
1485 1486 1487
	cifs_in_send_inc(server);
	rc = smb_send(server, in_buf, len);
	cifs_in_send_dec(server);
1488
	cifs_save_when_sent(midQ);
1489 1490

	if (rc < 0)
1491
		server->sequence_number -= 2;
1492

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

S
Steve French 已提交
1495
	if (rc < 0) {
P
Pavel Shilovsky 已提交
1496
		cifs_delete_mid(midQ);
J
[CIFS]  
Jeremy Allison 已提交
1497 1498 1499 1500
		return rc;
	}

	/* Wait for a reply - allow signals to interrupt. */
1501
	rc = wait_event_interruptible(server->response_q,
1502
		(!(midQ->mid_state == MID_REQUEST_SUBMITTED)) ||
1503 1504
		((server->tcpStatus != CifsGood) &&
		 (server->tcpStatus != CifsNew)));
J
[CIFS]  
Jeremy Allison 已提交
1505 1506 1507

	/* Were we interrupted by a signal ? */
	if ((rc == -ERESTARTSYS) &&
1508
		(midQ->mid_state == MID_REQUEST_SUBMITTED) &&
1509 1510
		((server->tcpStatus == CifsGood) ||
		 (server->tcpStatus == CifsNew))) {
J
[CIFS]  
Jeremy Allison 已提交
1511 1512 1513 1514

		if (in_buf->Command == SMB_COM_TRANSACTION2) {
			/* POSIX lock. We send a NT_CANCEL SMB to cause the
			   blocking lock to return. */
1515
			rc = send_cancel(server, &rqst, midQ);
J
[CIFS]  
Jeremy Allison 已提交
1516
			if (rc) {
P
Pavel Shilovsky 已提交
1517
				cifs_delete_mid(midQ);
J
[CIFS]  
Jeremy Allison 已提交
1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528
				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 已提交
1529
				cifs_delete_mid(midQ);
J
[CIFS]  
Jeremy Allison 已提交
1530 1531 1532 1533
				return rc;
			}
		}

1534
		rc = wait_for_response(server, midQ);
1535
		if (rc) {
1536
			send_cancel(server, &rqst, midQ);
1537
			spin_lock(&GlobalMid_Lock);
1538
			if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
1539 1540 1541 1542 1543 1544
				/* no longer considered to be "in-flight" */
				midQ->callback = DeleteMidQEntry;
				spin_unlock(&GlobalMid_Lock);
				return rc;
			}
			spin_unlock(&GlobalMid_Lock);
J
[CIFS]  
Jeremy Allison 已提交
1545
		}
1546 1547 1548

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

1551
	rc = cifs_sync_mid_result(midQ, server);
1552
	if (rc != 0)
J
[CIFS]  
Jeremy Allison 已提交
1553
		return rc;
1554

1555
	/* rcvd frame is ok */
1556
	if (out_buf == NULL || midQ->mid_state != MID_RESPONSE_RECEIVED) {
1557
		rc = -EIO;
1558
		cifs_tcon_dbg(VFS, "Bad MID state?\n");
1559 1560
		goto out;
	}
L
Linus Torvalds 已提交
1561

1562
	*pbytes_returned = get_rfc1002_length(midQ->resp_buf);
1563
	memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
1564
	rc = cifs_check_receive(midQ, server, 0);
1565
out:
P
Pavel Shilovsky 已提交
1566
	cifs_delete_mid(midQ);
J
[CIFS]  
Jeremy Allison 已提交
1567 1568
	if (rstart && rc == -EACCES)
		return -ERESTARTSYS;
L
Linus Torvalds 已提交
1569 1570
	return rc;
}