transport.c 38.7 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 timeout,
490
		      int *credits, unsigned int *instance)
L
Linus Torvalds 已提交
491
{
492 493
	int rc;

494 495
	*instance = 0;

P
Pavel Shilovsky 已提交
496
	spin_lock(&server->req_lock);
497
	if (timeout == CIFS_ASYNC_OP) {
L
Linus Torvalds 已提交
498
		/* oplock breaks must not be held up */
P
Pavel Shilovsky 已提交
499
		server->in_flight++;
500
		*credits -= 1;
501
		*instance = server->reconnect_instance;
P
Pavel Shilovsky 已提交
502
		spin_unlock(&server->req_lock);
503 504 505 506
		return 0;
	}

	while (1) {
507
		if (*credits <= 0) {
P
Pavel Shilovsky 已提交
508
			spin_unlock(&server->req_lock);
509
			cifs_num_waiters_inc(server);
510
			rc = wait_event_killable(server->request_q,
511
						 has_credits(server, credits));
512
			cifs_num_waiters_dec(server);
513 514
			if (rc)
				return rc;
P
Pavel Shilovsky 已提交
515
			spin_lock(&server->req_lock);
516
		} else {
517
			if (server->tcpStatus == CifsExiting) {
P
Pavel Shilovsky 已提交
518
				spin_unlock(&server->req_lock);
519
				return -ENOENT;
L
Linus Torvalds 已提交
520
			}
521

522 523 524 525
			/*
			 * Can not count locking commands against total
			 * as they are allowed to block on server.
			 */
526 527

			/* update # of requests on the wire to server */
528
			if (timeout != CIFS_BLOCKING_OP) {
529
				*credits -= 1;
P
Pavel Shilovsky 已提交
530
				server->in_flight++;
531
				*instance = server->reconnect_instance;
532
			}
P
Pavel Shilovsky 已提交
533
			spin_unlock(&server->req_lock);
534
			break;
L
Linus Torvalds 已提交
535 536
		}
	}
J
[CIFS]  
Jeremy Allison 已提交
537 538
	return 0;
}
L
Linus Torvalds 已提交
539

540
static int
541 542
wait_for_free_request(struct TCP_Server_Info *server, const int flags,
		      unsigned int *instance)
543
{
544
	int *val;
545 546 547 548
	int timeout, optype;

	timeout = flags & CIFS_TIMEOUT_MASK;
	optype = flags & CIFS_OP_MASK;
549 550 551 552 553

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

555
	return wait_for_free_credits(server, timeout, val, instance);
556 557
}

558 559
int
cifs_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
560
		      unsigned int *num, struct cifs_credits *credits)
561 562
{
	*num = size;
563 564
	credits->value = 0;
	credits->instance = server->reconnect_instance;
565 566 567
	return 0;
}

568
static int allocate_mid(struct cifs_ses *ses, struct smb_hdr *in_buf,
J
[CIFS]  
Jeremy Allison 已提交
569 570
			struct mid_q_entry **ppmidQ)
{
L
Linus Torvalds 已提交
571
	if (ses->server->tcpStatus == CifsExiting) {
J
[CIFS]  
Jeremy Allison 已提交
572
		return -ENOENT;
573 574 575
	}

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

580
	if (ses->status == CifsNew) {
S
Steve French 已提交
581
		if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
S
Steve French 已提交
582
			(in_buf->Command != SMB_COM_NEGOTIATE))
J
[CIFS]  
Jeremy Allison 已提交
583
			return -EAGAIN;
S
Steve French 已提交
584
		/* else ok - we are setting up session */
L
Linus Torvalds 已提交
585
	}
586 587 588 589 590 591 592 593

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

594
	*ppmidQ = AllocMidQEntry(in_buf, ses->server);
595
	if (*ppmidQ == NULL)
J
[CIFS]  
Jeremy Allison 已提交
596
		return -ENOMEM;
597 598 599
	spin_lock(&GlobalMid_Lock);
	list_add_tail(&(*ppmidQ)->qhead, &ses->server->pending_mid_q);
	spin_unlock(&GlobalMid_Lock);
J
[CIFS]  
Jeremy Allison 已提交
600 601 602
	return 0;
}

603 604
static int
wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *midQ)
J
[CIFS]  
Jeremy Allison 已提交
605
{
606
	int error;
J
[CIFS]  
Jeremy Allison 已提交
607

608
	error = wait_event_freezekillable_unsafe(server->response_q,
609
				    midQ->mid_state != MID_REQUEST_SUBMITTED);
610 611
	if (error < 0)
		return -ERESTARTSYS;
J
[CIFS]  
Jeremy Allison 已提交
612

613
	return 0;
J
[CIFS]  
Jeremy Allison 已提交
614 615
}

616 617
struct mid_q_entry *
cifs_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
618 619
{
	int rc;
620
	struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
621 622
	struct mid_q_entry *mid;

623 624 625 626
	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);

627
	/* enable signing if server requires it */
628
	if (server->sign)
629 630 631 632
		hdr->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;

	mid = AllocMidQEntry(hdr, server);
	if (mid == NULL)
633
		return ERR_PTR(-ENOMEM);
634

635
	rc = cifs_sign_rqst(rqst, server, &mid->sequence_number);
636 637
	if (rc) {
		DeleteMidQEntry(mid);
638
		return ERR_PTR(rc);
639 640
	}

641
	return mid;
642
}
643

J
Jeff Layton 已提交
644 645 646 647 648
/*
 * Send a SMB request and set the callback function in the mid to handle
 * the result. Caller is responsible for dealing with timeouts.
 */
int
649
cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst,
P
Pavel Shilovsky 已提交
650
		mid_receive_t *receive, mid_callback_t *callback,
651 652
		mid_handle_t *handle, void *cbdata, const int flags,
		const struct cifs_credits *exist_credits)
J
Jeff Layton 已提交
653
{
654
	int rc;
J
Jeff Layton 已提交
655
	struct mid_q_entry *mid;
656
	struct cifs_credits credits = { .value = 0, .instance = 0 };
657
	unsigned int instance;
658
	int optype;
J
Jeff Layton 已提交
659

660 661
	optype = flags & CIFS_OP_MASK;

662
	if ((flags & CIFS_HAS_CREDITS) == 0) {
663
		rc = wait_for_free_request(server, flags, &instance);
664 665
		if (rc)
			return rc;
666
		credits.value = 1;
667
		credits.instance = instance;
668 669
	} else
		instance = exist_credits->instance;
J
Jeff Layton 已提交
670 671

	mutex_lock(&server->srv_mutex);
672 673 674 675 676 677 678 679 680 681 682 683

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

684 685
	mid = server->ops->setup_async_request(server, rqst);
	if (IS_ERR(mid)) {
J
Jeff Layton 已提交
686
		mutex_unlock(&server->srv_mutex);
687
		add_credits_and_wake_if(server, &credits, optype);
688
		return PTR_ERR(mid);
J
Jeff Layton 已提交
689 690
	}

691
	mid->receive = receive;
J
Jeff Layton 已提交
692 693
	mid->callback = callback;
	mid->callback_data = cbdata;
P
Pavel Shilovsky 已提交
694
	mid->handle = handle;
695
	mid->mid_state = MID_REQUEST_SUBMITTED;
696

697 698 699 700 701
	/* put it on the pending_mid_q */
	spin_lock(&GlobalMid_Lock);
	list_add_tail(&mid->qhead, &server->pending_mid_q);
	spin_unlock(&GlobalMid_Lock);

702 703 704 705 706
	/*
	 * 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);
707
	cifs_in_send_inc(server);
708
	rc = smb_send_rqst(server, 1, rqst, flags);
709
	cifs_in_send_dec(server);
710

711
	if (rc < 0) {
712
		revert_current_mid(server, mid->credits);
713
		server->sequence_number -= 2;
714 715 716
		cifs_delete_mid(mid);
	}

J
Jeff Layton 已提交
717
	mutex_unlock(&server->srv_mutex);
718

719 720
	if (rc == 0)
		return 0;
J
Jeff Layton 已提交
721

722
	add_credits_and_wake_if(server, &credits, optype);
J
Jeff Layton 已提交
723 724 725
	return rc;
}

726 727 728 729 730 731 732 733 734 735
/*
 *
 * 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
736
SendReceiveNoRsp(const unsigned int xid, struct cifs_ses *ses,
737
		 char *in_buf, int flags)
738 739 740
{
	int rc;
	struct kvec iov[1];
741
	struct kvec rsp_iov;
742 743
	int resp_buf_type;

744 745
	iov[0].iov_base = in_buf;
	iov[0].iov_len = get_rfc1002_length(in_buf) + 4;
746
	flags |= CIFS_NO_RESP;
747
	rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
748
	cifs_dbg(NOISY, "SendRcvNoRsp flags %d rc %d\n", flags, rc);
749

750 751 752
	return rc;
}

753
static int
754
cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
755 756 757
{
	int rc = 0;

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

J
Jeff Layton 已提交
761
	spin_lock(&GlobalMid_Lock);
762
	switch (mid->mid_state) {
J
Jeff Layton 已提交
763
	case MID_RESPONSE_RECEIVED:
764 765
		spin_unlock(&GlobalMid_Lock);
		return rc;
J
Jeff Layton 已提交
766 767 768
	case MID_RETRY_NEEDED:
		rc = -EAGAIN;
		break;
769 770 771
	case MID_RESPONSE_MALFORMED:
		rc = -EIO;
		break;
772 773 774
	case MID_SHUTDOWN:
		rc = -EHOSTDOWN;
		break;
J
Jeff Layton 已提交
775
	default:
776
		list_del_init(&mid->qhead);
777 778
		cifs_dbg(VFS, "%s: invalid mid state mid=%llu state=%d\n",
			 __func__, mid->mid, mid->mid_state);
J
Jeff Layton 已提交
779
		rc = -EIO;
780 781 782
	}
	spin_unlock(&GlobalMid_Lock);

783
	DeleteMidQEntry(mid);
784 785 786
	return rc;
}

787
static inline int
788 789
send_cancel(struct TCP_Server_Info *server, struct smb_rqst *rqst,
	    struct mid_q_entry *mid)
790
{
791
	return server->ops->send_cancel ?
792
				server->ops->send_cancel(server, rqst, mid) : 0;
793 794
}

795 796 797 798
int
cifs_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
		   bool log_error)
{
799
	unsigned int len = get_rfc1002_length(mid->resp_buf) + 4;
800 801

	dump_smb(mid->resp_buf, min_t(u32, 92, len));
802 803

	/* convert the length into a more usable form */
804
	if (server->sign) {
805
		struct kvec iov[2];
806
		int rc = 0;
807 808
		struct smb_rqst rqst = { .rq_iov = iov,
					 .rq_nvec = 2 };
809

810 811 812 813
		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;
814
		/* FIXME: add code to kill session */
815
		rc = cifs_verify_signature(&rqst, server,
816
					   mid->sequence_number);
817
		if (rc)
818 819
			cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
				 rc);
820 821 822 823 824 825
	}

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

826 827
struct mid_q_entry *
cifs_setup_request(struct cifs_ses *ses, struct smb_rqst *rqst)
828 829
{
	int rc;
830
	struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
831 832
	struct mid_q_entry *mid;

833 834 835 836
	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);

837 838
	rc = allocate_mid(ses, hdr, &mid);
	if (rc)
839 840 841
		return ERR_PTR(rc);
	rc = cifs_sign_rqst(rqst, ses->server, &mid->sequence_number);
	if (rc) {
P
Pavel Shilovsky 已提交
842
		cifs_delete_mid(mid);
843 844 845
		return ERR_PTR(rc);
	}
	return mid;
846 847
}

848
static void
849
cifs_compound_callback(struct mid_q_entry *mid)
850 851
{
	struct TCP_Server_Info *server = mid->server;
852 853 854 855
	struct cifs_credits credits;

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

857
	add_credits(server, &credits, mid->optype);
858 859
}

860 861 862 863 864 865 866 867 868 869 870 871 872 873
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);
}

874
int
R
Ronnie Sahlberg 已提交
875 876 877
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 已提交
878
{
879
	int i, j, optype, rc = 0;
R
Ronnie Sahlberg 已提交
880
	struct mid_q_entry *midQ[MAX_COMPOUND];
881
	bool cancelled_mid[MAX_COMPOUND] = {false};
882 883 884 885
	struct cifs_credits credits[MAX_COMPOUND] = {
		{ .value = 0, .instance = 0 }
	};
	unsigned int instance;
886
	unsigned int first_instance = 0;
887
	char *buf;
888

889
	optype = flags & CIFS_OP_MASK;
890

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

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

899
	if (ses->server->tcpStatus == CifsExiting)
J
[CIFS]  
Jeremy Allison 已提交
900 901
		return -ENOENT;

902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
	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;
	}

929
	/*
930 931 932 933 934 935
	 * 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.
936
	 * Ensure we obtain 1 credit per request in the compound chain.
937
	 */
938
	for (i = 0; i < num_rqst; i++) {
939
		rc = wait_for_free_request(ses->server, flags, &instance);
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958

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

959 960 961 962 963 964 965 966 967 968 969
		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++)
970
				add_credits(ses->server, &credits[j], optype);
971 972 973
			return rc;
		}
	}
J
[CIFS]  
Jeremy Allison 已提交
974

975
setup_rqsts:
976 977 978 979 980
	/*
	 * 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 已提交
981

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

984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
	/*
	 * 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 已提交
1000 1001 1002
	for (i = 0; i < num_rqst; i++) {
		midQ[i] = ses->server->ops->setup_request(ses, &rqst[i]);
		if (IS_ERR(midQ[i])) {
1003
			revert_current_mid(ses->server, i);
R
Ronnie Sahlberg 已提交
1004 1005 1006
			for (j = 0; j < i; j++)
				cifs_delete_mid(midQ[j]);
			mutex_unlock(&ses->server->srv_mutex);
1007

R
Ronnie Sahlberg 已提交
1008
			/* Update # of requests on wire to server */
1009
			for (j = 0; j < num_rqst; j++)
1010
				add_credits(ses->server, &credits[j], optype);
R
Ronnie Sahlberg 已提交
1011 1012 1013 1014
			return PTR_ERR(midQ[i]);
		}

		midQ[i]->mid_state = MID_REQUEST_SUBMITTED;
1015
		midQ[i]->optype = optype;
1016
		/*
1017 1018 1019
		 * Invoke callback for every part of the compound chain
		 * to calculate credits properly. Wake up this thread only when
		 * the last element is received.
1020 1021
		 */
		if (i < num_rqst - 1)
1022 1023 1024
			midQ[i]->callback = cifs_compound_callback;
		else
			midQ[i]->callback = cifs_compound_last_callback;
L
Linus Torvalds 已提交
1025
	}
1026
	cifs_in_send_inc(ses->server);
R
Ronnie Sahlberg 已提交
1027
	rc = smb_send_rqst(ses->server, num_rqst, rqst, flags);
1028
	cifs_in_send_dec(ses->server);
R
Ronnie Sahlberg 已提交
1029 1030 1031

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

1033 1034
	if (rc < 0) {
		revert_current_mid(ses->server, num_rqst);
1035
		ses->server->sequence_number -= 2;
1036
	}
R
Ronnie Sahlberg 已提交
1037

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

1040 1041 1042
	if (rc < 0) {
		/* Sending failed for some reason - return credits back */
		for (i = 0; i < num_rqst; i++)
1043
			add_credits(ses->server, &credits[i], optype);
1044
		goto out;
1045 1046 1047 1048 1049 1050 1051 1052 1053
	}

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

1055 1056 1057 1058 1059 1060
	/*
	 * 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 已提交
1061

1062
	if ((flags & CIFS_TIMEOUT_MASK) == CIFS_ASYNC_OP)
1063
		goto out;
R
Ronnie Sahlberg 已提交
1064

1065
	for (i = 0; i < num_rqst; i++) {
R
Ronnie Sahlberg 已提交
1066
		rc = wait_for_response(ses->server, midQ[i]);
1067 1068 1069 1070 1071
		if (rc != 0)
			break;
	}
	if (rc != 0) {
		for (; i < num_rqst; i++) {
1072 1073
			cifs_dbg(VFS, "Cancelling wait for mid %llu cmd: %d\n",
				 midQ[i]->mid, le16_to_cpu(midQ[i]->command));
R
Ronnie Sahlberg 已提交
1074 1075 1076 1077
			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;
1078
				midQ[i]->callback = cifs_cancelled_callback;
1079
				cancelled_mid[i] = true;
1080
				credits[i].value = 0;
R
Ronnie Sahlberg 已提交
1081
			}
1082
			spin_unlock(&GlobalMid_Lock);
R
Ronnie Sahlberg 已提交
1083
		}
1084 1085 1086 1087 1088
	}

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

		rc = cifs_sync_mid_result(midQ[i], ses->server);
		if (rc != 0) {
1092 1093 1094
			/* mark this mid as cancelled to not free it below */
			cancelled_mid[i] = true;
			goto out;
1095
		}
1096

R
Ronnie Sahlberg 已提交
1097 1098 1099 1100 1101 1102
		if (!midQ[i]->resp_buf ||
		    midQ[i]->mid_state != MID_RESPONSE_RECEIVED) {
			rc = -EIO;
			cifs_dbg(FYI, "Bad MID state?\n");
			goto out;
		}
1103

R
Ronnie Sahlberg 已提交
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
		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 已提交
1116

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

R
Ronnie Sahlberg 已提交
1121
	}
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133

	/*
	 * 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 已提交
1134
out:
1135 1136 1137 1138 1139 1140
	/*
	 * 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.
	 */
1141 1142 1143 1144
	for (i = 0; i < num_rqst; i++) {
		if (!cancelled_mid[i])
			cifs_delete_mid(midQ[i]);
	}
L
Linus Torvalds 已提交
1145

1146 1147
	return rc;
}
L
Linus Torvalds 已提交
1148

R
Ronnie Sahlberg 已提交
1149 1150 1151 1152 1153 1154 1155 1156 1157
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);
}

1158 1159 1160 1161 1162 1163
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;
1164
	struct kvec s_iov[CIFS_MAX_IOV_SIZE], *new_iov;
1165 1166
	int rc;

1167
	if (n_vec + 1 > CIFS_MAX_IOV_SIZE) {
1168 1169
		new_iov = kmalloc_array(n_vec + 1, sizeof(struct kvec),
					GFP_KERNEL);
1170 1171 1172
		if (!new_iov) {
			/* otherwise cifs_send_recv below sets resp_buf_type */
			*resp_buf_type = CIFS_NO_BUFFER;
1173
			return -ENOMEM;
1174
		}
1175 1176
	} else
		new_iov = s_iov;
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190

	/* 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);
1191 1192
	if (n_vec + 1 > CIFS_MAX_IOV_SIZE)
		kfree(new_iov);
1193 1194 1195
	return rc;
}

L
Linus Torvalds 已提交
1196
int
1197
SendReceive(const unsigned int xid, struct cifs_ses *ses,
L
Linus Torvalds 已提交
1198
	    struct smb_hdr *in_buf, struct smb_hdr *out_buf,
1199
	    int *pbytes_returned, const int flags)
L
Linus Torvalds 已提交
1200 1201 1202
{
	int rc = 0;
	struct mid_q_entry *midQ;
1203 1204 1205
	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 };
1206
	struct cifs_credits credits = { .value = 1, .instance = 0 };
L
Linus Torvalds 已提交
1207 1208

	if (ses == NULL) {
1209
		cifs_dbg(VFS, "Null smb session\n");
L
Linus Torvalds 已提交
1210 1211
		return -EIO;
	}
S
Steve French 已提交
1212
	if (ses->server == NULL) {
1213
		cifs_dbg(VFS, "Null tcp session\n");
L
Linus Torvalds 已提交
1214 1215 1216
		return -EIO;
	}

S
Steve French 已提交
1217
	if (ses->server->tcpStatus == CifsExiting)
1218 1219
		return -ENOENT;

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

1224
	if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
1225
		cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
1226
			 len);
1227 1228 1229
		return -EIO;
	}

1230
	rc = wait_for_free_request(ses->server, flags, &credits.instance);
J
[CIFS]  
Jeremy Allison 已提交
1231 1232 1233
	if (rc)
		return rc;

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

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

J
[CIFS]  
Jeremy Allison 已提交
1240 1241
	rc = allocate_mid(ses, in_buf, &midQ);
	if (rc) {
J
Jeff Layton 已提交
1242
		mutex_unlock(&ses->server->srv_mutex);
J
[CIFS]  
Jeremy Allison 已提交
1243
		/* Update # of requests on wire to server */
1244
		add_credits(ses->server, &credits, 0);
J
[CIFS]  
Jeremy Allison 已提交
1245
		return rc;
L
Linus Torvalds 已提交
1246 1247
	}

1248
	rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
1249 1250 1251 1252
	if (rc) {
		mutex_unlock(&ses->server->srv_mutex);
		goto out;
	}
L
Linus Torvalds 已提交
1253

1254
	midQ->mid_state = MID_REQUEST_SUBMITTED;
1255 1256

	cifs_in_send_inc(ses->server);
1257
	rc = smb_send(ses->server, in_buf, len);
1258 1259
	cifs_in_send_dec(ses->server);
	cifs_save_when_sent(midQ);
1260 1261 1262 1263

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

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

S
Steve French 已提交
1266
	if (rc < 0)
J
[CIFS]  
Jeremy Allison 已提交
1267 1268
		goto out;

1269
	if ((flags & CIFS_TIMEOUT_MASK) == CIFS_ASYNC_OP)
J
[CIFS]  
Jeremy Allison 已提交
1270
		goto out;
L
Linus Torvalds 已提交
1271

1272
	rc = wait_for_response(ses->server, midQ);
1273
	if (rc != 0) {
1274
		send_cancel(ses->server, &rqst, midQ);
1275
		spin_lock(&GlobalMid_Lock);
1276
		if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
1277 1278 1279
			/* no longer considered to be "in-flight" */
			midQ->callback = DeleteMidQEntry;
			spin_unlock(&GlobalMid_Lock);
1280
			add_credits(ses->server, &credits, 0);
1281 1282 1283 1284
			return rc;
		}
		spin_unlock(&GlobalMid_Lock);
	}
L
Linus Torvalds 已提交
1285

1286
	rc = cifs_sync_mid_result(midQ, ses->server);
1287
	if (rc != 0) {
1288
		add_credits(ses->server, &credits, 0);
L
Linus Torvalds 已提交
1289 1290
		return rc;
	}
1291

1292
	if (!midQ->resp_buf || !out_buf ||
1293
	    midQ->mid_state != MID_RESPONSE_RECEIVED) {
1294
		rc = -EIO;
1295
		cifs_dbg(VFS, "Bad MID state?\n");
1296
		goto out;
L
Linus Torvalds 已提交
1297
	}
J
[CIFS]  
Jeremy Allison 已提交
1298

1299
	*pbytes_returned = get_rfc1002_length(midQ->resp_buf);
1300 1301
	memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
	rc = cifs_check_receive(midQ, ses->server, 0);
J
[CIFS]  
Jeremy Allison 已提交
1302
out:
P
Pavel Shilovsky 已提交
1303
	cifs_delete_mid(midQ);
1304
	add_credits(ses->server, &credits, 0);
L
Linus Torvalds 已提交
1305

J
[CIFS]  
Jeremy Allison 已提交
1306 1307
	return rc;
}
L
Linus Torvalds 已提交
1308

J
[CIFS]  
Jeremy Allison 已提交
1309 1310 1311 1312
/* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
   blocking lock to return. */

static int
1313
send_lock_cancel(const unsigned int xid, struct cifs_tcon *tcon,
J
[CIFS]  
Jeremy Allison 已提交
1314 1315 1316 1317
			struct smb_hdr *in_buf,
			struct smb_hdr *out_buf)
{
	int bytes_returned;
1318
	struct cifs_ses *ses = tcon->ses;
J
[CIFS]  
Jeremy Allison 已提交
1319 1320 1321 1322 1323 1324 1325 1326 1327
	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;
1328
	pSMB->hdr.Mid = get_next_mid(ses->server);
J
[CIFS]  
Jeremy Allison 已提交
1329 1330

	return SendReceive(xid, ses, in_buf, out_buf,
1331
			&bytes_returned, 0);
J
[CIFS]  
Jeremy Allison 已提交
1332 1333 1334
}

int
1335
SendReceiveBlockingLock(const unsigned int xid, struct cifs_tcon *tcon,
J
[CIFS]  
Jeremy Allison 已提交
1336 1337 1338 1339 1340 1341
	    struct smb_hdr *in_buf, struct smb_hdr *out_buf,
	    int *pbytes_returned)
{
	int rc = 0;
	int rstart = 0;
	struct mid_q_entry *midQ;
1342
	struct cifs_ses *ses;
1343 1344 1345
	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 };
1346
	unsigned int instance;
J
[CIFS]  
Jeremy Allison 已提交
1347 1348

	if (tcon == NULL || tcon->ses == NULL) {
1349
		cifs_dbg(VFS, "Null smb session\n");
J
[CIFS]  
Jeremy Allison 已提交
1350 1351 1352 1353
		return -EIO;
	}
	ses = tcon->ses;

S
Steve French 已提交
1354
	if (ses->server == NULL) {
1355
		cifs_dbg(VFS, "Null tcp session\n");
J
[CIFS]  
Jeremy Allison 已提交
1356 1357 1358
		return -EIO;
	}

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

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

1366
	if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
1367
		cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
1368
			 len);
1369 1370 1371
		return -EIO;
	}

1372
	rc = wait_for_free_request(ses->server, CIFS_BLOCKING_OP, &instance);
J
[CIFS]  
Jeremy Allison 已提交
1373 1374 1375
	if (rc)
		return rc;

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

J
Jeff Layton 已提交
1380
	mutex_lock(&ses->server->srv_mutex);
J
[CIFS]  
Jeremy Allison 已提交
1381 1382 1383

	rc = allocate_mid(ses, in_buf, &midQ);
	if (rc) {
J
Jeff Layton 已提交
1384
		mutex_unlock(&ses->server->srv_mutex);
J
[CIFS]  
Jeremy Allison 已提交
1385 1386 1387 1388
		return rc;
	}

	rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
1389
	if (rc) {
P
Pavel Shilovsky 已提交
1390
		cifs_delete_mid(midQ);
1391 1392 1393
		mutex_unlock(&ses->server->srv_mutex);
		return rc;
	}
L
Linus Torvalds 已提交
1394

1395
	midQ->mid_state = MID_REQUEST_SUBMITTED;
1396
	cifs_in_send_inc(ses->server);
1397
	rc = smb_send(ses->server, in_buf, len);
1398 1399
	cifs_in_send_dec(ses->server);
	cifs_save_when_sent(midQ);
1400 1401 1402 1403

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

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

S
Steve French 已提交
1406
	if (rc < 0) {
P
Pavel Shilovsky 已提交
1407
		cifs_delete_mid(midQ);
J
[CIFS]  
Jeremy Allison 已提交
1408 1409 1410 1411 1412
		return rc;
	}

	/* Wait for a reply - allow signals to interrupt. */
	rc = wait_event_interruptible(ses->server->response_q,
1413
		(!(midQ->mid_state == MID_REQUEST_SUBMITTED)) ||
J
[CIFS]  
Jeremy Allison 已提交
1414 1415 1416 1417 1418
		((ses->server->tcpStatus != CifsGood) &&
		 (ses->server->tcpStatus != CifsNew)));

	/* Were we interrupted by a signal ? */
	if ((rc == -ERESTARTSYS) &&
1419
		(midQ->mid_state == MID_REQUEST_SUBMITTED) &&
J
[CIFS]  
Jeremy Allison 已提交
1420 1421 1422 1423 1424 1425
		((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. */
1426
			rc = send_cancel(ses->server, &rqst, midQ);
J
[CIFS]  
Jeremy Allison 已提交
1427
			if (rc) {
P
Pavel Shilovsky 已提交
1428
				cifs_delete_mid(midQ);
J
[CIFS]  
Jeremy Allison 已提交
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439
				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 已提交
1440
				cifs_delete_mid(midQ);
J
[CIFS]  
Jeremy Allison 已提交
1441 1442 1443 1444
				return rc;
			}
		}

1445 1446
		rc = wait_for_response(ses->server, midQ);
		if (rc) {
1447
			send_cancel(ses->server, &rqst, midQ);
1448
			spin_lock(&GlobalMid_Lock);
1449
			if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
1450 1451 1452 1453 1454 1455
				/* no longer considered to be "in-flight" */
				midQ->callback = DeleteMidQEntry;
				spin_unlock(&GlobalMid_Lock);
				return rc;
			}
			spin_unlock(&GlobalMid_Lock);
J
[CIFS]  
Jeremy Allison 已提交
1456
		}
1457 1458 1459

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

1462
	rc = cifs_sync_mid_result(midQ, ses->server);
1463
	if (rc != 0)
J
[CIFS]  
Jeremy Allison 已提交
1464
		return rc;
1465

1466
	/* rcvd frame is ok */
1467
	if (out_buf == NULL || midQ->mid_state != MID_RESPONSE_RECEIVED) {
1468
		rc = -EIO;
1469
		cifs_dbg(VFS, "Bad MID state?\n");
1470 1471
		goto out;
	}
L
Linus Torvalds 已提交
1472

1473
	*pbytes_returned = get_rfc1002_length(midQ->resp_buf);
1474 1475
	memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
	rc = cifs_check_receive(midQ, ses->server, 0);
1476
out:
P
Pavel Shilovsky 已提交
1477
	cifs_delete_mid(midQ);
J
[CIFS]  
Jeremy Allison 已提交
1478 1479
	if (rstart && rc == -EACCES)
		return -ERESTARTSYS;
L
Linus Torvalds 已提交
1480 1481
	return rc;
}