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 490
wait_for_free_credits(struct TCP_Server_Info *server, const int flags,
		      unsigned int *instance)
L
Linus Torvalds 已提交
491
{
492
	int rc;
493 494 495 496
	int *credits;
	int optype;

	optype = flags & CIFS_OP_MASK;
497

498 499
	*instance = 0;

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

	while (1) {
516
		if (*credits <= 0) {
P
Pavel Shilovsky 已提交
517
			spin_unlock(&server->req_lock);
518
			cifs_num_waiters_inc(server);
519
			rc = wait_event_killable(server->request_q,
520
						 has_credits(server, credits));
521
			cifs_num_waiters_dec(server);
522 523
			if (rc)
				return rc;
P
Pavel Shilovsky 已提交
524
			spin_lock(&server->req_lock);
525
		} else {
526
			if (server->tcpStatus == CifsExiting) {
P
Pavel Shilovsky 已提交
527
				spin_unlock(&server->req_lock);
528
				return -ENOENT;
L
Linus Torvalds 已提交
529
			}
530

531 532 533 534
			/*
			 * Can not count locking commands against total
			 * as they are allowed to block on server.
			 */
535 536

			/* update # of requests on the wire to server */
537
			if ((flags & CIFS_TIMEOUT_MASK) != CIFS_BLOCKING_OP) {
538
				*credits -= 1;
P
Pavel Shilovsky 已提交
539
				server->in_flight++;
540
				*instance = server->reconnect_instance;
541
			}
P
Pavel Shilovsky 已提交
542
			spin_unlock(&server->req_lock);
543
			break;
L
Linus Torvalds 已提交
544 545
		}
	}
J
[CIFS]  
Jeremy Allison 已提交
546 547
	return 0;
}
L
Linus Torvalds 已提交
548

549
static int
550 551
wait_for_free_request(struct TCP_Server_Info *server, const int flags,
		      unsigned int *instance)
552
{
553
	return wait_for_free_credits(server, flags, instance);
554 555
}

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

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

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

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

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

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

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

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

611
	return 0;
J
[CIFS]  
Jeremy Allison 已提交
612 613
}

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

621 622 623 624
	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);

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

	mid = AllocMidQEntry(hdr, server);
	if (mid == NULL)
631
		return ERR_PTR(-ENOMEM);
632

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

639
	return mid;
640
}
641

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

658 659
	optype = flags & CIFS_OP_MASK;

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

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

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

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

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

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

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

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

J
Jeff Layton 已提交
715
	mutex_unlock(&server->srv_mutex);
716

717 718
	if (rc == 0)
		return 0;
J
Jeff Layton 已提交
719

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

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

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

748 749 750
	return rc;
}

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

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

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

781
	DeleteMidQEntry(mid);
782 783 784
	return rc;
}

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

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

	dump_smb(mid->resp_buf, min_t(u32, 92, len));
800 801

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

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

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

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

831 832 833 834
	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);

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

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

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

855
	add_credits(server, &credits, mid->optype);
856 857
}

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

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

887
	optype = flags & CIFS_OP_MASK;
888

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1144 1145
	return rc;
}
L
Linus Torvalds 已提交
1146

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1252
	midQ->mid_state = MID_REQUEST_SUBMITTED;
1253 1254

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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