transport.c 40.3 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/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 89 90 91 92 93 94 95 96 97 98 99 100 101
static void _cifs_mid_q_entry_release(struct kref *refcount)
{
	struct mid_q_entry *mid = container_of(refcount, struct mid_q_entry,
					       refcount);

	mempool_free(mid, 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);
}

102
void
L
Linus Torvalds 已提交
103 104
DeleteMidQEntry(struct mid_q_entry *midEntry)
{
S
Steve French 已提交
105
#ifdef CONFIG_CIFS_STATS2
106
	__le16 command = midEntry->server->vals->lock_cmd;
S
Steve French 已提交
107 108
	unsigned long now;
#endif
109
	midEntry->mid_state = MID_FREE;
110
	atomic_dec(&midCount);
111
	if (midEntry->large_buf)
112 113 114
		cifs_buf_release(midEntry->resp_buf);
	else
		cifs_small_buf_release(midEntry->resp_buf);
S
Steve French 已提交
115 116
#ifdef CONFIG_CIFS_STATS2
	now = jiffies;
117 118 119 120 121 122 123 124 125 126 127 128
	/*
	 * 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)) &&
129
	    (midEntry->command != command)) {
130 131 132 133 134
		/*
		 * smb2slowcmd[NUMBER_OF_SMB2_COMMANDS] counts by command
		 * NB: le16_to_cpu returns unsigned so can not be negative below
		 */
		if (le16_to_cpu(midEntry->command) < NUMBER_OF_SMB2_COMMANDS)
135 136
			cifs_stats_inc(&midEntry->server->smb2slowcmd[le16_to_cpu(midEntry->command)]);

137 138 139 140
		trace_smb3_slow_rsp(le16_to_cpu(midEntry->command),
			       midEntry->mid, midEntry->pid,
			       midEntry->when_sent, midEntry->when_received);
		if (cifsFYI & CIFS_TIMER) {
141
			pr_debug(" CIFS slow rsp: cmd %d mid %llu",
S
Steve French 已提交
142
			       midEntry->command, midEntry->mid);
143
			cifs_info(" A: 0x%lx S: 0x%lx R: 0x%lx\n",
S
Steve French 已提交
144 145 146 147 148 149
			       now - midEntry->when_alloc,
			       now - midEntry->when_sent,
			       now - midEntry->when_received);
		}
	}
#endif
150
	cifs_mid_q_entry_release(midEntry);
L
Linus Torvalds 已提交
151 152
}

P
Pavel Shilovsky 已提交
153 154
void
cifs_delete_mid(struct mid_q_entry *mid)
155 156
{
	spin_lock(&GlobalMid_Lock);
157 158
	list_del_init(&mid->qhead);
	mid->mid_flags |= MID_DELETED;
159 160 161 162 163
	spin_unlock(&GlobalMid_Lock);

	DeleteMidQEntry(mid);
}

164 165 166
/*
 * smb_send_kvec - send an array of kvecs to the server
 * @server:	Server to send the data to
167
 * @smb_msg:	Message to send
168 169 170 171 172
 * @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.
 */
173
static int
174 175
smb_send_kvec(struct TCP_Server_Info *server, struct msghdr *smb_msg,
	      size_t *sent)
L
Linus Torvalds 已提交
176 177
{
	int rc = 0;
178
	int retries = 0;
179
	struct socket *ssocket = server->ssocket;
180

181 182
	*sent = 0;

183 184 185 186
	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;
187
	if (server->noblocksnd)
188
		smb_msg->msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL;
189
	else
190
		smb_msg->msg_flags = MSG_NOSIGNAL;
L
Linus Torvalds 已提交
191

192
	while (msg_data_left(smb_msg)) {
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
		/*
		 * 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.
		 */
211
		rc = sock_sendmsg(ssocket, smb_msg);
212
		if (rc == -EAGAIN) {
213 214 215
			retries++;
			if (retries >= 14 ||
			    (!server->noblocksnd && (retries > 2))) {
216 217
				cifs_dbg(VFS, "sends on sock %p stuck for 15 seconds\n",
					 ssocket);
218
				return -EAGAIN;
L
Linus Torvalds 已提交
219
			}
220
			msleep(1 << retries);
L
Linus Torvalds 已提交
221 222
			continue;
		}
223

S
Steve French 已提交
224
		if (rc < 0)
225
			return rc;
226

S
Steve French 已提交
227
		if (rc == 0) {
228 229
			/* should never happen, letting socket clear before
			   retrying is our only obvious option here */
230
			cifs_dbg(VFS, "tcp sent no data\n");
231 232
			msleep(500);
			continue;
233
		}
234

235 236 237
		/* send was at least partially successful */
		*sent += rc;
		retries = 0; /* in case we get ENOSPC on the next send */
L
Linus Torvalds 已提交
238
	}
239
	return 0;
240 241
}

242
unsigned long
R
Ronnie Sahlberg 已提交
243
smb_rqst_len(struct TCP_Server_Info *server, struct smb_rqst *rqst)
244 245
{
	unsigned int i;
246 247
	struct kvec *iov;
	int nvec;
248 249
	unsigned long buflen = 0;

R
Ronnie Sahlberg 已提交
250 251
	if (server->vals->header_preamble_size == 0 &&
	    rqst->rq_nvec >= 2 && rqst->rq_iov[0].iov_len == 4) {
252 253 254 255 256 257 258
		iov = &rqst->rq_iov[1];
		nvec = rqst->rq_nvec - 1;
	} else {
		iov = rqst->rq_iov;
		nvec = rqst->rq_nvec;
	}

259
	/* total up iov array first */
260
	for (i = 0; i < nvec; i++)
261 262
		buflen += iov[i].iov_len;

263 264 265 266 267 268
	/*
	 * 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.
	 */
269
	if (rqst->rq_npages) {
270 271 272 273 274 275 276 277 278 279 280
		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;
		}
281 282 283 284 285
	}

	return buflen;
}

286
static int
287 288
__smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
		struct smb_rqst *rqst)
289
{
290 291 292 293 294
	int rc = 0;
	struct kvec *iov;
	int n_vec;
	unsigned int send_length = 0;
	unsigned int i, j;
295
	sigset_t mask, oldmask;
296
	size_t total_len = 0, sent, size;
297
	struct socket *ssocket = server->ssocket;
298
	struct msghdr smb_msg;
299
	int val = 1;
300 301
	__be32 rfc1002_marker;

302
	if (cifs_rdma_enabled(server) && server->smbd_conn) {
R
Ronnie Sahlberg 已提交
303
		rc = smbd_send(server, rqst);
304 305
		goto smbd_done;
	}
306

307
	if (ssocket == NULL)
308
		return -EAGAIN;
309

310 311 312 313 314
	if (signal_pending(current)) {
		cifs_dbg(FYI, "signal is pending before sending any data\n");
		return -EINTR;
	}

315 316 317 318
	/* cork the socket */
	kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
				(char *)&val, sizeof(val));

319
	for (j = 0; j < num_rqst; j++)
R
Ronnie Sahlberg 已提交
320
		send_length += smb_rqst_len(server, &rqst[j]);
321 322
	rfc1002_marker = cpu_to_be32(send_length);

323 324 325 326 327 328 329 330 331 332
	/*
	 * 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);

333 334 335 336 337 338
	/* Generate a rfc1002 marker for SMB2+ */
	if (server->vals->header_preamble_size == 0) {
		struct kvec hiov = {
			.iov_base = &rfc1002_marker,
			.iov_len  = 4
		};
339
		iov_iter_kvec(&smb_msg.msg_iter, WRITE, &hiov, 1, 4);
340 341
		rc = smb_send_kvec(server, &smb_msg, &sent);
		if (rc < 0)
342
			goto unmask;
343 344 345 346 347

		total_len += sent;
		send_length += 4;
	}

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

350 351 352
	for (j = 0; j < num_rqst; j++) {
		iov = rqst[j].rq_iov;
		n_vec = rqst[j].rq_nvec;
353

354
		size = 0;
355 356
		for (i = 0; i < n_vec; i++) {
			dump_smb(iov[i].iov_base, iov[i].iov_len);
357
			size += iov[i].iov_len;
358
		}
359

360
		iov_iter_kvec(&smb_msg.msg_iter, WRITE, iov, n_vec, size);
361

362
		rc = smb_send_kvec(server, &smb_msg, &sent);
363
		if (rc < 0)
364
			goto unmask;
365 366

		total_len += sent;
367 368 369 370 371 372 373 374 375

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

376
			iov_iter_bvec(&smb_msg.msg_iter, WRITE,
377 378 379 380 381 382 383
				      &bvec, 1, bvec.bv_len);
			rc = smb_send_kvec(server, &smb_msg, &sent);
			if (rc < 0)
				break;

			total_len += sent;
		}
384
	}
L
Linus Torvalds 已提交
385

386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
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;
	}

405 406 407 408 409
	/* uncork it */
	val = 0;
	kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
				(char *)&val, sizeof(val));

410
	if ((total_len > 0) && (total_len != send_length)) {
411
		cifs_dbg(FYI, "partial send (wanted=%u sent=%zu): terminating session\n",
412
			 send_length, total_len);
413 414 415 416 417
		/*
		 * 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
		 */
418
		server->tcpStatus = CifsNeedReconnect;
S
Steve French 已提交
419 420
		trace_smb3_partial_send_reconnect(server->CurrentMid,
						  server->hostname);
421
	}
422
smbd_done:
423
	if (rc < 0 && rc != -EINTR)
424 425
		cifs_dbg(VFS, "Error %d sending data on socket to server\n",
			 rc);
426
	else if (rc > 0)
L
Linus Torvalds 已提交
427 428 429 430 431
		rc = 0;

	return rc;
}

432
static int
433 434
smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
	      struct smb_rqst *rqst, int flags)
435
{
436 437 438
	struct kvec iov;
	struct smb2_transform_hdr tr_hdr;
	struct smb_rqst cur_rqst[MAX_COMPOUND];
439 440 441
	int rc;

	if (!(flags & CIFS_TRANSFORM_REQ))
442 443 444 445
		return __smb_send_rqst(server, num_rqst, rqst);

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

447 448 449 450 451 452 453 454 455 456 457 458
	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) {
		cifs_dbg(VFS, "Encryption requested but transform callback "
			 "is missing\n");
459 460
		return -EIO;
	}
461

462 463
	rc = server->ops->init_transform_rq(server, num_rqst + 1,
					    &cur_rqst[0], rqst);
464 465 466
	if (rc)
		return rc;

467 468
	rc = __smb_send_rqst(server, num_rqst + 1, &cur_rqst[0]);
	smb3_free_compound_rqst(num_rqst, &cur_rqst[1]);
469
	return rc;
470 471
}

472 473 474 475
int
smb_send(struct TCP_Server_Info *server, struct smb_hdr *smb_buffer,
	 unsigned int smb_buf_length)
{
476
	struct kvec iov[2];
477 478
	struct smb_rqst rqst = { .rq_iov = iov,
				 .rq_nvec = 2 };
479

480 481 482 483
	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;
484

485
	return __smb_send_rqst(server, 1, &rqst);
486 487
}

P
Pavel Shilovsky 已提交
488
static int
489
wait_for_free_credits(struct TCP_Server_Info *server, const int num_credits,
490 491
		      const int timeout, const int flags,
		      unsigned int *instance)
L
Linus Torvalds 已提交
492
{
493
	int rc;
494 495
	int *credits;
	int optype;
496 497 498 499 500 501
	long int t;

	if (timeout < 0)
		t = MAX_JIFFY_OFFSET;
	else
		t = msecs_to_jiffies(timeout);
502 503

	optype = flags & CIFS_OP_MASK;
504

505 506
	*instance = 0;

507 508 509 510 511
	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 已提交
512
	spin_lock(&server->req_lock);
513
	if ((flags & CIFS_TIMEOUT_MASK) == CIFS_ASYNC_OP) {
L
Linus Torvalds 已提交
514
		/* oplock breaks must not be held up */
P
Pavel Shilovsky 已提交
515
		server->in_flight++;
516
		*credits -= 1;
517
		*instance = server->reconnect_instance;
P
Pavel Shilovsky 已提交
518
		spin_unlock(&server->req_lock);
519 520 521 522
		return 0;
	}

	while (1) {
523
		if (*credits < num_credits) {
P
Pavel Shilovsky 已提交
524
			spin_unlock(&server->req_lock);
525
			cifs_num_waiters_inc(server);
526 527
			rc = wait_event_killable_timeout(server->request_q,
				has_credits(server, credits, num_credits), t);
528
			cifs_num_waiters_dec(server);
529
			if (!rc) {
530 531
				trace_smb3_credit_timeout(server->CurrentMid,
					server->hostname, num_credits);
532 533 534 535 536 537
				cifs_dbg(VFS, "wait timed out after %d ms\n",
					 timeout);
				return -ENOTSUPP;
			}
			if (rc == -ERESTARTSYS)
				return -ERESTARTSYS;
P
Pavel Shilovsky 已提交
538
			spin_lock(&server->req_lock);
539
		} else {
540
			if (server->tcpStatus == CifsExiting) {
P
Pavel Shilovsky 已提交
541
				spin_unlock(&server->req_lock);
542
				return -ENOENT;
L
Linus Torvalds 已提交
543
			}
544

545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
			/*
			 * 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);
563 564
				rc = wait_event_killable_timeout(
					server->request_q,
565
					has_credits(server, credits,
566 567
						    MAX_COMPOUND + 1),
					t);
568
				cifs_num_waiters_dec(server);
569
				if (!rc) {
570 571 572
					trace_smb3_credit_timeout(
						server->CurrentMid,
						server->hostname, num_credits);
573 574 575 576 577 578
					cifs_dbg(VFS, "wait timed out after %d ms\n",
						 timeout);
					return -ENOTSUPP;
				}
				if (rc == -ERESTARTSYS)
					return -ERESTARTSYS;
579 580 581 582
				spin_lock(&server->req_lock);
				continue;
			}

583 584 585 586
			/*
			 * Can not count locking commands against total
			 * as they are allowed to block on server.
			 */
587 588

			/* update # of requests on the wire to server */
589
			if ((flags & CIFS_TIMEOUT_MASK) != CIFS_BLOCKING_OP) {
590 591
				*credits -= num_credits;
				server->in_flight += num_credits;
592
				*instance = server->reconnect_instance;
593
			}
P
Pavel Shilovsky 已提交
594
			spin_unlock(&server->req_lock);
595
			break;
L
Linus Torvalds 已提交
596 597
		}
	}
J
[CIFS]  
Jeremy Allison 已提交
598 599
	return 0;
}
L
Linus Torvalds 已提交
600

601
static int
602 603
wait_for_free_request(struct TCP_Server_Info *server, const int flags,
		      unsigned int *instance)
604
{
605 606
	return wait_for_free_credits(server, 1, -1, flags,
				     instance);
607 608
}

609 610
int
cifs_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
611
		      unsigned int *num, struct cifs_credits *credits)
612 613
{
	*num = size;
614 615
	credits->value = 0;
	credits->instance = server->reconnect_instance;
616 617 618
	return 0;
}

619
static int allocate_mid(struct cifs_ses *ses, struct smb_hdr *in_buf,
J
[CIFS]  
Jeremy Allison 已提交
620 621
			struct mid_q_entry **ppmidQ)
{
L
Linus Torvalds 已提交
622
	if (ses->server->tcpStatus == CifsExiting) {
J
[CIFS]  
Jeremy Allison 已提交
623
		return -ENOENT;
624 625 626
	}

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

631
	if (ses->status == CifsNew) {
S
Steve French 已提交
632
		if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
S
Steve French 已提交
633
			(in_buf->Command != SMB_COM_NEGOTIATE))
J
[CIFS]  
Jeremy Allison 已提交
634
			return -EAGAIN;
S
Steve French 已提交
635
		/* else ok - we are setting up session */
L
Linus Torvalds 已提交
636
	}
637 638 639 640 641 642 643 644

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

645
	*ppmidQ = AllocMidQEntry(in_buf, ses->server);
646
	if (*ppmidQ == NULL)
J
[CIFS]  
Jeremy Allison 已提交
647
		return -ENOMEM;
648 649 650
	spin_lock(&GlobalMid_Lock);
	list_add_tail(&(*ppmidQ)->qhead, &ses->server->pending_mid_q);
	spin_unlock(&GlobalMid_Lock);
J
[CIFS]  
Jeremy Allison 已提交
651 652 653
	return 0;
}

654 655
static int
wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *midQ)
J
[CIFS]  
Jeremy Allison 已提交
656
{
657
	int error;
J
[CIFS]  
Jeremy Allison 已提交
658

659
	error = wait_event_freezekillable_unsafe(server->response_q,
660
				    midQ->mid_state != MID_REQUEST_SUBMITTED);
661 662
	if (error < 0)
		return -ERESTARTSYS;
J
[CIFS]  
Jeremy Allison 已提交
663

664
	return 0;
J
[CIFS]  
Jeremy Allison 已提交
665 666
}

667 668
struct mid_q_entry *
cifs_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
669 670
{
	int rc;
671
	struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
672 673
	struct mid_q_entry *mid;

674 675 676 677
	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);

678
	/* enable signing if server requires it */
679
	if (server->sign)
680 681 682 683
		hdr->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;

	mid = AllocMidQEntry(hdr, server);
	if (mid == NULL)
684
		return ERR_PTR(-ENOMEM);
685

686
	rc = cifs_sign_rqst(rqst, server, &mid->sequence_number);
687 688
	if (rc) {
		DeleteMidQEntry(mid);
689
		return ERR_PTR(rc);
690 691
	}

692
	return mid;
693
}
694

J
Jeff Layton 已提交
695 696 697 698 699
/*
 * Send a SMB request and set the callback function in the mid to handle
 * the result. Caller is responsible for dealing with timeouts.
 */
int
700
cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst,
P
Pavel Shilovsky 已提交
701
		mid_receive_t *receive, mid_callback_t *callback,
702 703
		mid_handle_t *handle, void *cbdata, const int flags,
		const struct cifs_credits *exist_credits)
J
Jeff Layton 已提交
704
{
705
	int rc;
J
Jeff Layton 已提交
706
	struct mid_q_entry *mid;
707
	struct cifs_credits credits = { .value = 0, .instance = 0 };
708
	unsigned int instance;
709
	int optype;
J
Jeff Layton 已提交
710

711 712
	optype = flags & CIFS_OP_MASK;

713
	if ((flags & CIFS_HAS_CREDITS) == 0) {
714
		rc = wait_for_free_request(server, flags, &instance);
715 716
		if (rc)
			return rc;
717
		credits.value = 1;
718
		credits.instance = instance;
719 720
	} else
		instance = exist_credits->instance;
J
Jeff Layton 已提交
721 722

	mutex_lock(&server->srv_mutex);
723 724 725 726 727 728 729 730 731 732 733 734

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

735 736
	mid = server->ops->setup_async_request(server, rqst);
	if (IS_ERR(mid)) {
J
Jeff Layton 已提交
737
		mutex_unlock(&server->srv_mutex);
738
		add_credits_and_wake_if(server, &credits, optype);
739
		return PTR_ERR(mid);
J
Jeff Layton 已提交
740 741
	}

742
	mid->receive = receive;
J
Jeff Layton 已提交
743 744
	mid->callback = callback;
	mid->callback_data = cbdata;
P
Pavel Shilovsky 已提交
745
	mid->handle = handle;
746
	mid->mid_state = MID_REQUEST_SUBMITTED;
747

748 749 750 751 752
	/* put it on the pending_mid_q */
	spin_lock(&GlobalMid_Lock);
	list_add_tail(&mid->qhead, &server->pending_mid_q);
	spin_unlock(&GlobalMid_Lock);

753 754 755 756 757
	/*
	 * 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);
758
	cifs_in_send_inc(server);
759
	rc = smb_send_rqst(server, 1, rqst, flags);
760
	cifs_in_send_dec(server);
761

762
	if (rc < 0) {
763
		revert_current_mid(server, mid->credits);
764
		server->sequence_number -= 2;
765 766 767
		cifs_delete_mid(mid);
	}

J
Jeff Layton 已提交
768
	mutex_unlock(&server->srv_mutex);
769

770 771
	if (rc == 0)
		return 0;
J
Jeff Layton 已提交
772

773
	add_credits_and_wake_if(server, &credits, optype);
J
Jeff Layton 已提交
774 775 776
	return rc;
}

777 778 779 780 781 782 783 784 785 786
/*
 *
 * 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
787
SendReceiveNoRsp(const unsigned int xid, struct cifs_ses *ses,
788
		 char *in_buf, int flags)
789 790 791
{
	int rc;
	struct kvec iov[1];
792
	struct kvec rsp_iov;
793 794
	int resp_buf_type;

795 796
	iov[0].iov_base = in_buf;
	iov[0].iov_len = get_rfc1002_length(in_buf) + 4;
797
	flags |= CIFS_NO_RESP;
798
	rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
799
	cifs_dbg(NOISY, "SendRcvNoRsp flags %d rc %d\n", flags, rc);
800

801 802 803
	return rc;
}

804
static int
805
cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
806 807 808
{
	int rc = 0;

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

J
Jeff Layton 已提交
812
	spin_lock(&GlobalMid_Lock);
813
	switch (mid->mid_state) {
J
Jeff Layton 已提交
814
	case MID_RESPONSE_RECEIVED:
815 816
		spin_unlock(&GlobalMid_Lock);
		return rc;
J
Jeff Layton 已提交
817 818 819
	case MID_RETRY_NEEDED:
		rc = -EAGAIN;
		break;
820 821 822
	case MID_RESPONSE_MALFORMED:
		rc = -EIO;
		break;
823 824 825
	case MID_SHUTDOWN:
		rc = -EHOSTDOWN;
		break;
J
Jeff Layton 已提交
826
	default:
827
		list_del_init(&mid->qhead);
828 829
		cifs_dbg(VFS, "%s: invalid mid state mid=%llu state=%d\n",
			 __func__, mid->mid, mid->mid_state);
J
Jeff Layton 已提交
830
		rc = -EIO;
831 832 833
	}
	spin_unlock(&GlobalMid_Lock);

834
	DeleteMidQEntry(mid);
835 836 837
	return rc;
}

838
static inline int
839 840
send_cancel(struct TCP_Server_Info *server, struct smb_rqst *rqst,
	    struct mid_q_entry *mid)
841
{
842
	return server->ops->send_cancel ?
843
				server->ops->send_cancel(server, rqst, mid) : 0;
844 845
}

846 847 848 849
int
cifs_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
		   bool log_error)
{
850
	unsigned int len = get_rfc1002_length(mid->resp_buf) + 4;
851 852

	dump_smb(mid->resp_buf, min_t(u32, 92, len));
853 854

	/* convert the length into a more usable form */
855
	if (server->sign) {
856
		struct kvec iov[2];
857
		int rc = 0;
858 859
		struct smb_rqst rqst = { .rq_iov = iov,
					 .rq_nvec = 2 };
860

861 862 863 864
		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;
865
		/* FIXME: add code to kill session */
866
		rc = cifs_verify_signature(&rqst, server,
867
					   mid->sequence_number);
868
		if (rc)
869 870
			cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
				 rc);
871 872 873 874 875 876
	}

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

877 878
struct mid_q_entry *
cifs_setup_request(struct cifs_ses *ses, struct smb_rqst *rqst)
879 880
{
	int rc;
881
	struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
882 883
	struct mid_q_entry *mid;

884 885 886 887
	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);

888 889
	rc = allocate_mid(ses, hdr, &mid);
	if (rc)
890 891 892
		return ERR_PTR(rc);
	rc = cifs_sign_rqst(rqst, ses->server, &mid->sequence_number);
	if (rc) {
P
Pavel Shilovsky 已提交
893
		cifs_delete_mid(mid);
894 895 896
		return ERR_PTR(rc);
	}
	return mid;
897 898
}

899
static void
900
cifs_compound_callback(struct mid_q_entry *mid)
901 902
{
	struct TCP_Server_Info *server = mid->server;
903 904 905 906
	struct cifs_credits credits;

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

908
	add_credits(server, &credits, mid->optype);
909 910
}

911 912 913 914 915 916 917 918 919 920 921 922 923 924
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);
}

925
int
R
Ronnie Sahlberg 已提交
926 927 928
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 已提交
929
{
930
	int i, j, optype, rc = 0;
R
Ronnie Sahlberg 已提交
931
	struct mid_q_entry *midQ[MAX_COMPOUND];
932
	bool cancelled_mid[MAX_COMPOUND] = {false};
933 934 935 936
	struct cifs_credits credits[MAX_COMPOUND] = {
		{ .value = 0, .instance = 0 }
	};
	unsigned int instance;
937
	unsigned int first_instance = 0;
938
	char *buf;
939

940
	optype = flags & CIFS_OP_MASK;
941

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

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

950
	if (ses->server->tcpStatus == CifsExiting)
J
[CIFS]  
Jeremy Allison 已提交
951 952
		return -ENOENT;

953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
	spin_lock(&ses->server->req_lock);
	if (ses->server->credits < num_rqst) {
		/*
		 * Return immediately if not too many requests in flight since
		 * we will likely be stuck on waiting for credits.
		 */
		if (ses->server->in_flight < num_rqst - ses->server->credits) {
			spin_unlock(&ses->server->req_lock);
			return -ENOTSUPP;
		}
	} else {
		/* enough credits to send the whole compounded request */
		ses->server->credits -= num_rqst;
		ses->server->in_flight += num_rqst;
		first_instance = ses->server->reconnect_instance;
	}
	spin_unlock(&ses->server->req_lock);

	if (first_instance) {
		cifs_dbg(FYI, "Acquired %d credits at once\n", num_rqst);
		for (i = 0; i < num_rqst; i++) {
			credits[i].value = 1;
			credits[i].instance = first_instance;
		}
		goto setup_rqsts;
	}

980
	/*
981 982 983 984 985 986
	 * There are not enough credits to send the whole compound request but
	 * there are requests in flight that may bring credits from the server.
	 * This approach still leaves the possibility to be stuck waiting for
	 * credits if the server doesn't grant credits to the outstanding
	 * requests. This should be fixed by returning immediately and letting
	 * a caller fallback to sequential commands instead of compounding.
987
	 * Ensure we obtain 1 credit per request in the compound chain.
988
	 */
989
	for (i = 0; i < num_rqst; i++) {
990
		rc = wait_for_free_request(ses->server, flags, &instance);
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009

		if (rc == 0) {
			credits[i].value = 1;
			credits[i].instance = instance;
			/*
			 * All parts of the compound chain must get credits from
			 * the same session, otherwise we may end up using more
			 * credits than the server granted. If there were
			 * reconnects in between, return -EAGAIN and let callers
			 * handle it.
			 */
			if (i == 0)
				first_instance = instance;
			else if (first_instance != instance) {
				i++;
				rc = -EAGAIN;
			}
		}

1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
		if (rc) {
			/*
			 * We haven't sent an SMB packet to the server yet but
			 * we already obtained credits for i requests in the
			 * compound chain - need to return those credits back
			 * for future use. Note that we need to call add_credits
			 * multiple times to match the way we obtained credits
			 * in the first place and to account for in flight
			 * requests correctly.
			 */
			for (j = 0; j < i; j++)
1021
				add_credits(ses->server, &credits[j], optype);
1022 1023 1024
			return rc;
		}
	}
J
[CIFS]  
Jeremy Allison 已提交
1025

1026
setup_rqsts:
1027 1028 1029 1030 1031
	/*
	 * 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 已提交
1032

J
Jeff Layton 已提交
1033
	mutex_lock(&ses->server->srv_mutex);
J
[CIFS]  
Jeremy Allison 已提交
1034

1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
	/*
	 * All the parts of the compound chain belong obtained credits from the
	 * same session (see the appropriate checks above). In the same time
	 * there might be reconnects after those checks but before we acquired
	 * the srv_mutex. We can not 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 (first_instance != ses->server->reconnect_instance) {
		mutex_unlock(&ses->server->srv_mutex);
		for (j = 0; j < num_rqst; j++)
			add_credits(ses->server, &credits[j], optype);
		return -EAGAIN;
	}

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

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

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

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

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

J
Jeff Layton 已提交
1089
	mutex_unlock(&ses->server->srv_mutex);
J
[CIFS]  
Jeremy Allison 已提交
1090

1091 1092 1093
	if (rc < 0) {
		/* Sending failed for some reason - return credits back */
		for (i = 0; i < num_rqst; i++)
1094
			add_credits(ses->server, &credits[i], optype);
1095
		goto out;
1096 1097 1098 1099 1100 1101 1102 1103 1104
	}

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

1106 1107 1108 1109 1110 1111
	/*
	 * 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 已提交
1112

1113
	if ((flags & CIFS_TIMEOUT_MASK) == CIFS_ASYNC_OP)
1114
		goto out;
R
Ronnie Sahlberg 已提交
1115

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

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

		rc = cifs_sync_mid_result(midQ[i], ses->server);
		if (rc != 0) {
1143 1144 1145
			/* mark this mid as cancelled to not free it below */
			cancelled_mid[i] = true;
			goto out;
1146
		}
1147

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

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

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

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

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

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

	/*
	 * 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 已提交
1185
out:
1186 1187 1188 1189 1190 1191
	/*
	 * 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.
	 */
1192 1193 1194 1195
	for (i = 0; i < num_rqst; i++) {
		if (!cancelled_mid[i])
			cifs_delete_mid(midQ[i]);
	}
L
Linus Torvalds 已提交
1196

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

R
Ronnie Sahlberg 已提交
1200 1201 1202 1203 1204 1205 1206 1207 1208
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);
}

1209 1210 1211 1212 1213 1214
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;
1215
	struct kvec s_iov[CIFS_MAX_IOV_SIZE], *new_iov;
1216 1217
	int rc;

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

	/* 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);
1242 1243
	if (n_vec + 1 > CIFS_MAX_IOV_SIZE)
		kfree(new_iov);
1244 1245 1246
	return rc;
}

L
Linus Torvalds 已提交
1247
int
1248
SendReceive(const unsigned int xid, struct cifs_ses *ses,
L
Linus Torvalds 已提交
1249
	    struct smb_hdr *in_buf, struct smb_hdr *out_buf,
1250
	    int *pbytes_returned, const int flags)
L
Linus Torvalds 已提交
1251 1252 1253
{
	int rc = 0;
	struct mid_q_entry *midQ;
1254 1255 1256
	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 };
1257
	struct cifs_credits credits = { .value = 1, .instance = 0 };
L
Linus Torvalds 已提交
1258 1259

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

S
Steve French 已提交
1268
	if (ses->server->tcpStatus == CifsExiting)
1269 1270
		return -ENOENT;

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

1275
	if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
1276
		cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
1277
			 len);
1278 1279 1280
		return -EIO;
	}

1281
	rc = wait_for_free_request(ses->server, flags, &credits.instance);
J
[CIFS]  
Jeremy Allison 已提交
1282 1283 1284
	if (rc)
		return rc;

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

J
Jeff Layton 已提交
1289
	mutex_lock(&ses->server->srv_mutex);
L
Linus Torvalds 已提交
1290

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

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

1305
	midQ->mid_state = MID_REQUEST_SUBMITTED;
1306 1307

	cifs_in_send_inc(ses->server);
1308
	rc = smb_send(ses->server, in_buf, len);
1309 1310
	cifs_in_send_dec(ses->server);
	cifs_save_when_sent(midQ);
1311 1312 1313 1314

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

J
Jeff Layton 已提交
1315
	mutex_unlock(&ses->server->srv_mutex);
J
[CIFS]  
Jeremy Allison 已提交
1316

S
Steve French 已提交
1317
	if (rc < 0)
J
[CIFS]  
Jeremy Allison 已提交
1318 1319
		goto out;

1320
	if ((flags & CIFS_TIMEOUT_MASK) == CIFS_ASYNC_OP)
J
[CIFS]  
Jeremy Allison 已提交
1321
		goto out;
L
Linus Torvalds 已提交
1322

1323
	rc = wait_for_response(ses->server, midQ);
1324
	if (rc != 0) {
1325
		send_cancel(ses->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(ses->server, &credits, 0);
1332 1333 1334 1335
			return rc;
		}
		spin_unlock(&GlobalMid_Lock);
	}
L
Linus Torvalds 已提交
1336

1337
	rc = cifs_sync_mid_result(midQ, ses->server);
1338
	if (rc != 0) {
1339
		add_credits(ses->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_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 1352
	memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
	rc = cifs_check_receive(midQ, ses->server, 0);
J
[CIFS]  
Jeremy Allison 已提交
1353
out:
P
Pavel Shilovsky 已提交
1354
	cifs_delete_mid(midQ);
1355
	add_credits(ses->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;
J
[CIFS]  
Jeremy Allison 已提交
1398 1399

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

S
Steve French 已提交
1405
	if (ses->server == NULL) {
1406
		cifs_dbg(VFS, "Null tcp session\n");
J
[CIFS]  
Jeremy Allison 已提交
1407 1408 1409
		return -EIO;
	}

S
Steve French 已提交
1410
	if (ses->server->tcpStatus == CifsExiting)
J
[CIFS]  
Jeremy Allison 已提交
1411 1412
		return -ENOENT;

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

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

1423
	rc = wait_for_free_request(ses->server, CIFS_BLOCKING_OP, &instance);
J
[CIFS]  
Jeremy Allison 已提交
1424 1425 1426
	if (rc)
		return rc;

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

J
Jeff Layton 已提交
1431
	mutex_lock(&ses->server->srv_mutex);
J
[CIFS]  
Jeremy Allison 已提交
1432 1433 1434

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

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

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

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

J
Jeff Layton 已提交
1455
	mutex_unlock(&ses->server->srv_mutex);
J
[CIFS]  
Jeremy Allison 已提交
1456

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

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

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

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

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

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

1513
	rc = cifs_sync_mid_result(midQ, ses->server);
1514
	if (rc != 0)
J
[CIFS]  
Jeremy Allison 已提交
1515
		return rc;
1516

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

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