transport.c 37.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 36 37 38 39
#include <asm/processor.h>
#include <linux/mempool.h>
#include "cifspdu.h"
#include "cifsglob.h"
#include "cifsproto.h"
#include "cifs_debug.h"
40
#include "smb2proto.h"
41
#include "smbdirect.h"
42

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

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

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

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

62
	temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
63
	memset(temp, 0, sizeof(struct mid_q_entry));
64
	kref_init(&temp->refcount);
65 66 67 68
	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 已提交
69
	/*	do_gettimeofday(&temp->when_sent);*/ /* easier to use jiffies */
70 71 72
	/* when mid allocated can be before when sent */
	temp->when_alloc = jiffies;
	temp->server = server;
73

74 75 76 77 78 79
	/*
	 * 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 已提交
80 81

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

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
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);
}

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

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

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

	DeleteMidQEntry(mid);
}

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

180 181
	*sent = 0;

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

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

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

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

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

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

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

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

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

	return buflen;
}

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

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

307 308 309 310
	/* cork the socket */
	kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
				(char *)&val, sizeof(val));

311
	for (j = 0; j < num_rqst; j++)
R
Ronnie Sahlberg 已提交
312
		send_length += smb_rqst_len(server, &rqst[j]);
313 314
	rfc1002_marker = cpu_to_be32(send_length);

315 316 317 318 319 320
	/* Generate a rfc1002 marker for SMB2+ */
	if (server->vals->header_preamble_size == 0) {
		struct kvec hiov = {
			.iov_base = &rfc1002_marker,
			.iov_len  = 4
		};
321
		iov_iter_kvec(&smb_msg.msg_iter, WRITE, &hiov, 1, 4);
322 323 324 325 326 327 328 329
		rc = smb_send_kvec(server, &smb_msg, &sent);
		if (rc < 0)
			goto uncork;

		total_len += sent;
		send_length += 4;
	}

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

332 333 334
	for (j = 0; j < num_rqst; j++) {
		iov = rqst[j].rq_iov;
		n_vec = rqst[j].rq_nvec;
335

336
		size = 0;
337 338
		for (i = 0; i < n_vec; i++) {
			dump_smb(iov[i].iov_base, iov[i].iov_len);
339
			size += iov[i].iov_len;
340
		}
341

342
		iov_iter_kvec(&smb_msg.msg_iter, WRITE, iov, n_vec, size);
343

344
		rc = smb_send_kvec(server, &smb_msg, &sent);
345
		if (rc < 0)
346
			goto uncork;
347 348

		total_len += sent;
349 350 351 352 353 354 355 356 357

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

358
			iov_iter_bvec(&smb_msg.msg_iter, WRITE,
359 360 361 362 363 364 365
				      &bvec, 1, bvec.bv_len);
			rc = smb_send_kvec(server, &smb_msg, &sent);
			if (rc < 0)
				break;

			total_len += sent;
		}
366
	}
L
Linus Torvalds 已提交
367

368
uncork:
369 370 371 372 373
	/* uncork it */
	val = 0;
	kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
				(char *)&val, sizeof(val));

374
	if ((total_len > 0) && (total_len != send_length)) {
375
		cifs_dbg(FYI, "partial send (wanted=%u sent=%zu): terminating session\n",
376
			 send_length, total_len);
377 378 379 380 381
		/*
		 * 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
		 */
382
		server->tcpStatus = CifsNeedReconnect;
S
Steve French 已提交
383 384
		trace_smb3_partial_send_reconnect(server->CurrentMid,
						  server->hostname);
385
	}
386
smbd_done:
387
	if (rc < 0 && rc != -EINTR)
388 389
		cifs_dbg(VFS, "Error %d sending data on socket to server\n",
			 rc);
390
	else if (rc > 0)
L
Linus Torvalds 已提交
391 392 393 394 395
		rc = 0;

	return rc;
}

396
static int
397 398
smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
	      struct smb_rqst *rqst, int flags)
399
{
400 401 402
	struct kvec iov;
	struct smb2_transform_hdr tr_hdr;
	struct smb_rqst cur_rqst[MAX_COMPOUND];
403 404 405
	int rc;

	if (!(flags & CIFS_TRANSFORM_REQ))
406 407 408 409
		return __smb_send_rqst(server, num_rqst, rqst);

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

411 412 413 414 415 416 417 418 419 420 421 422
	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");
423 424
		return -EIO;
	}
425

426 427
	rc = server->ops->init_transform_rq(server, num_rqst + 1,
					    &cur_rqst[0], rqst);
428 429 430
	if (rc)
		return rc;

431 432
	rc = __smb_send_rqst(server, num_rqst + 1, &cur_rqst[0]);
	smb3_free_compound_rqst(num_rqst, &cur_rqst[1]);
433
	return rc;
434 435
}

436 437 438 439
int
smb_send(struct TCP_Server_Info *server, struct smb_hdr *smb_buffer,
	 unsigned int smb_buf_length)
{
440
	struct kvec iov[2];
441 442
	struct smb_rqst rqst = { .rq_iov = iov,
				 .rq_nvec = 2 };
443

444 445 446 447
	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;
448

449
	return __smb_send_rqst(server, 1, &rqst);
450 451
}

P
Pavel Shilovsky 已提交
452
static int
453
wait_for_free_credits(struct TCP_Server_Info *server, const int timeout,
454
		      int *credits, unsigned int *instance)
L
Linus Torvalds 已提交
455
{
456 457
	int rc;

458 459
	*instance = 0;

P
Pavel Shilovsky 已提交
460
	spin_lock(&server->req_lock);
461
	if (timeout == CIFS_ASYNC_OP) {
L
Linus Torvalds 已提交
462
		/* oplock breaks must not be held up */
P
Pavel Shilovsky 已提交
463
		server->in_flight++;
464
		*credits -= 1;
465
		*instance = server->reconnect_instance;
P
Pavel Shilovsky 已提交
466
		spin_unlock(&server->req_lock);
467 468 469 470
		return 0;
	}

	while (1) {
471
		if (*credits <= 0) {
P
Pavel Shilovsky 已提交
472
			spin_unlock(&server->req_lock);
473
			cifs_num_waiters_inc(server);
474
			rc = wait_event_killable(server->request_q,
475
						 has_credits(server, credits));
476
			cifs_num_waiters_dec(server);
477 478
			if (rc)
				return rc;
P
Pavel Shilovsky 已提交
479
			spin_lock(&server->req_lock);
480
		} else {
481
			if (server->tcpStatus == CifsExiting) {
P
Pavel Shilovsky 已提交
482
				spin_unlock(&server->req_lock);
483
				return -ENOENT;
L
Linus Torvalds 已提交
484
			}
485

486 487 488 489
			/*
			 * Can not count locking commands against total
			 * as they are allowed to block on server.
			 */
490 491

			/* update # of requests on the wire to server */
492
			if (timeout != CIFS_BLOCKING_OP) {
493
				*credits -= 1;
P
Pavel Shilovsky 已提交
494
				server->in_flight++;
495
				*instance = server->reconnect_instance;
496
			}
P
Pavel Shilovsky 已提交
497
			spin_unlock(&server->req_lock);
498
			break;
L
Linus Torvalds 已提交
499 500
		}
	}
J
[CIFS]  
Jeremy Allison 已提交
501 502
	return 0;
}
L
Linus Torvalds 已提交
503

504
static int
505
wait_for_free_request(struct TCP_Server_Info *server, const int timeout,
506
		      const int optype, unsigned int *instance)
507
{
508 509 510 511 512 513
	int *val;

	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;
514
	return wait_for_free_credits(server, timeout, val, instance);
515 516
}

517 518
int
cifs_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
519
		      unsigned int *num, struct cifs_credits *credits)
520 521
{
	*num = size;
522 523
	credits->value = 0;
	credits->instance = server->reconnect_instance;
524 525 526
	return 0;
}

527
static int allocate_mid(struct cifs_ses *ses, struct smb_hdr *in_buf,
J
[CIFS]  
Jeremy Allison 已提交
528 529
			struct mid_q_entry **ppmidQ)
{
L
Linus Torvalds 已提交
530
	if (ses->server->tcpStatus == CifsExiting) {
J
[CIFS]  
Jeremy Allison 已提交
531
		return -ENOENT;
532 533 534
	}

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

539
	if (ses->status == CifsNew) {
S
Steve French 已提交
540
		if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
S
Steve French 已提交
541
			(in_buf->Command != SMB_COM_NEGOTIATE))
J
[CIFS]  
Jeremy Allison 已提交
542
			return -EAGAIN;
S
Steve French 已提交
543
		/* else ok - we are setting up session */
L
Linus Torvalds 已提交
544
	}
545 546 547 548 549 550 551 552

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

553
	*ppmidQ = AllocMidQEntry(in_buf, ses->server);
554
	if (*ppmidQ == NULL)
J
[CIFS]  
Jeremy Allison 已提交
555
		return -ENOMEM;
556 557 558
	spin_lock(&GlobalMid_Lock);
	list_add_tail(&(*ppmidQ)->qhead, &ses->server->pending_mid_q);
	spin_unlock(&GlobalMid_Lock);
J
[CIFS]  
Jeremy Allison 已提交
559 560 561
	return 0;
}

562 563
static int
wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *midQ)
J
[CIFS]  
Jeremy Allison 已提交
564
{
565
	int error;
J
[CIFS]  
Jeremy Allison 已提交
566

567
	error = wait_event_freezekillable_unsafe(server->response_q,
568
				    midQ->mid_state != MID_REQUEST_SUBMITTED);
569 570
	if (error < 0)
		return -ERESTARTSYS;
J
[CIFS]  
Jeremy Allison 已提交
571

572
	return 0;
J
[CIFS]  
Jeremy Allison 已提交
573 574
}

575 576
struct mid_q_entry *
cifs_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
577 578
{
	int rc;
579
	struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
580 581
	struct mid_q_entry *mid;

582 583 584 585
	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);

586
	/* enable signing if server requires it */
587
	if (server->sign)
588 589 590 591
		hdr->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;

	mid = AllocMidQEntry(hdr, server);
	if (mid == NULL)
592
		return ERR_PTR(-ENOMEM);
593

594
	rc = cifs_sign_rqst(rqst, server, &mid->sequence_number);
595 596
	if (rc) {
		DeleteMidQEntry(mid);
597
		return ERR_PTR(rc);
598 599
	}

600
	return mid;
601
}
602

J
Jeff Layton 已提交
603 604 605 606 607
/*
 * Send a SMB request and set the callback function in the mid to handle
 * the result. Caller is responsible for dealing with timeouts.
 */
int
608
cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst,
P
Pavel Shilovsky 已提交
609
		mid_receive_t *receive, mid_callback_t *callback,
610 611
		mid_handle_t *handle, void *cbdata, const int flags,
		const struct cifs_credits *exist_credits)
J
Jeff Layton 已提交
612
{
613
	int rc, timeout, optype;
J
Jeff Layton 已提交
614
	struct mid_q_entry *mid;
615
	struct cifs_credits credits = { .value = 0, .instance = 0 };
616
	unsigned int instance;
J
Jeff Layton 已提交
617

618 619 620
	timeout = flags & CIFS_TIMEOUT_MASK;
	optype = flags & CIFS_OP_MASK;

621
	if ((flags & CIFS_HAS_CREDITS) == 0) {
622
		rc = wait_for_free_request(server, timeout, optype, &instance);
623 624
		if (rc)
			return rc;
625
		credits.value = 1;
626
		credits.instance = instance;
627 628
	} else
		instance = exist_credits->instance;
J
Jeff Layton 已提交
629 630

	mutex_lock(&server->srv_mutex);
631 632 633 634 635 636 637 638 639 640 641 642

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

643 644
	mid = server->ops->setup_async_request(server, rqst);
	if (IS_ERR(mid)) {
J
Jeff Layton 已提交
645
		mutex_unlock(&server->srv_mutex);
646
		add_credits_and_wake_if(server, &credits, optype);
647
		return PTR_ERR(mid);
J
Jeff Layton 已提交
648 649
	}

650
	mid->receive = receive;
J
Jeff Layton 已提交
651 652
	mid->callback = callback;
	mid->callback_data = cbdata;
P
Pavel Shilovsky 已提交
653
	mid->handle = handle;
654
	mid->mid_state = MID_REQUEST_SUBMITTED;
655

656 657 658 659 660
	/* put it on the pending_mid_q */
	spin_lock(&GlobalMid_Lock);
	list_add_tail(&mid->qhead, &server->pending_mid_q);
	spin_unlock(&GlobalMid_Lock);

661 662 663 664 665
	/*
	 * 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);
666
	cifs_in_send_inc(server);
667
	rc = smb_send_rqst(server, 1, rqst, flags);
668
	cifs_in_send_dec(server);
669

670
	if (rc < 0) {
671
		revert_current_mid(server, mid->credits);
672
		server->sequence_number -= 2;
673 674 675
		cifs_delete_mid(mid);
	}

J
Jeff Layton 已提交
676
	mutex_unlock(&server->srv_mutex);
677

678 679
	if (rc == 0)
		return 0;
J
Jeff Layton 已提交
680

681
	add_credits_and_wake_if(server, &credits, optype);
J
Jeff Layton 已提交
682 683 684
	return rc;
}

685 686 687 688 689 690 691 692 693 694
/*
 *
 * 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
695
SendReceiveNoRsp(const unsigned int xid, struct cifs_ses *ses,
696
		 char *in_buf, int flags)
697 698 699
{
	int rc;
	struct kvec iov[1];
700
	struct kvec rsp_iov;
701 702
	int resp_buf_type;

703 704
	iov[0].iov_base = in_buf;
	iov[0].iov_len = get_rfc1002_length(in_buf) + 4;
705
	flags |= CIFS_NO_RESP;
706
	rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
707
	cifs_dbg(NOISY, "SendRcvNoRsp flags %d rc %d\n", flags, rc);
708

709 710 711
	return rc;
}

712
static int
713
cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
714 715 716
{
	int rc = 0;

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

J
Jeff Layton 已提交
720
	spin_lock(&GlobalMid_Lock);
721
	switch (mid->mid_state) {
J
Jeff Layton 已提交
722
	case MID_RESPONSE_RECEIVED:
723 724
		spin_unlock(&GlobalMid_Lock);
		return rc;
J
Jeff Layton 已提交
725 726 727
	case MID_RETRY_NEEDED:
		rc = -EAGAIN;
		break;
728 729 730
	case MID_RESPONSE_MALFORMED:
		rc = -EIO;
		break;
731 732 733
	case MID_SHUTDOWN:
		rc = -EHOSTDOWN;
		break;
J
Jeff Layton 已提交
734
	default:
735
		list_del_init(&mid->qhead);
736 737
		cifs_dbg(VFS, "%s: invalid mid state mid=%llu state=%d\n",
			 __func__, mid->mid, mid->mid_state);
J
Jeff Layton 已提交
738
		rc = -EIO;
739 740 741
	}
	spin_unlock(&GlobalMid_Lock);

742
	DeleteMidQEntry(mid);
743 744 745
	return rc;
}

746
static inline int
747 748
send_cancel(struct TCP_Server_Info *server, struct smb_rqst *rqst,
	    struct mid_q_entry *mid)
749
{
750
	return server->ops->send_cancel ?
751
				server->ops->send_cancel(server, rqst, mid) : 0;
752 753
}

754 755 756 757
int
cifs_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
		   bool log_error)
{
758
	unsigned int len = get_rfc1002_length(mid->resp_buf) + 4;
759 760

	dump_smb(mid->resp_buf, min_t(u32, 92, len));
761 762

	/* convert the length into a more usable form */
763
	if (server->sign) {
764
		struct kvec iov[2];
765
		int rc = 0;
766 767
		struct smb_rqst rqst = { .rq_iov = iov,
					 .rq_nvec = 2 };
768

769 770 771 772
		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;
773
		/* FIXME: add code to kill session */
774
		rc = cifs_verify_signature(&rqst, server,
775
					   mid->sequence_number);
776
		if (rc)
777 778
			cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
				 rc);
779 780 781 782 783 784
	}

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

785 786
struct mid_q_entry *
cifs_setup_request(struct cifs_ses *ses, struct smb_rqst *rqst)
787 788
{
	int rc;
789
	struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
790 791
	struct mid_q_entry *mid;

792 793 794 795
	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);

796 797
	rc = allocate_mid(ses, hdr, &mid);
	if (rc)
798 799 800
		return ERR_PTR(rc);
	rc = cifs_sign_rqst(rqst, ses->server, &mid->sequence_number);
	if (rc) {
P
Pavel Shilovsky 已提交
801
		cifs_delete_mid(mid);
802 803 804
		return ERR_PTR(rc);
	}
	return mid;
805 806
}

807
static void
808
cifs_compound_callback(struct mid_q_entry *mid)
809 810
{
	struct TCP_Server_Info *server = mid->server;
811 812 813 814
	struct cifs_credits credits;

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

816
	add_credits(server, &credits, mid->optype);
817 818
}

819 820 821 822 823 824 825 826 827 828 829 830 831 832
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);
}

833
int
R
Ronnie Sahlberg 已提交
834 835 836
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 已提交
837
{
R
Ronnie Sahlberg 已提交
838
	int i, j, rc = 0;
839
	int timeout, optype;
R
Ronnie Sahlberg 已提交
840
	struct mid_q_entry *midQ[MAX_COMPOUND];
841
	bool cancelled_mid[MAX_COMPOUND] = {false};
842 843 844 845
	struct cifs_credits credits[MAX_COMPOUND] = {
		{ .value = 0, .instance = 0 }
	};
	unsigned int instance;
846
	unsigned int first_instance = 0;
847
	char *buf;
848

849 850
	timeout = flags & CIFS_TIMEOUT_MASK;
	optype = flags & CIFS_OP_MASK;
851

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

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

860
	if (ses->server->tcpStatus == CifsExiting)
J
[CIFS]  
Jeremy Allison 已提交
861 862
		return -ENOENT;

863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
	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;
	}

890
	/*
891 892 893 894 895 896
	 * 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.
897
	 * Ensure we obtain 1 credit per request in the compound chain.
898
	 */
899
	for (i = 0; i < num_rqst; i++) {
900 901
		rc = wait_for_free_request(ses->server, timeout, optype,
					   &instance);
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920

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

921 922 923 924 925 926 927 928 929 930 931
		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++)
932
				add_credits(ses->server, &credits[j], optype);
933 934 935
			return rc;
		}
	}
J
[CIFS]  
Jeremy Allison 已提交
936

937
setup_rqsts:
938 939 940 941 942
	/*
	 * 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 已提交
943

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

946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
	/*
	 * 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 已提交
962 963 964
	for (i = 0; i < num_rqst; i++) {
		midQ[i] = ses->server->ops->setup_request(ses, &rqst[i]);
		if (IS_ERR(midQ[i])) {
965
			revert_current_mid(ses->server, i);
R
Ronnie Sahlberg 已提交
966 967 968
			for (j = 0; j < i; j++)
				cifs_delete_mid(midQ[j]);
			mutex_unlock(&ses->server->srv_mutex);
969

R
Ronnie Sahlberg 已提交
970
			/* Update # of requests on wire to server */
971
			for (j = 0; j < num_rqst; j++)
972
				add_credits(ses->server, &credits[j], optype);
R
Ronnie Sahlberg 已提交
973 974 975 976
			return PTR_ERR(midQ[i]);
		}

		midQ[i]->mid_state = MID_REQUEST_SUBMITTED;
977
		midQ[i]->optype = optype;
978
		/*
979 980 981
		 * Invoke callback for every part of the compound chain
		 * to calculate credits properly. Wake up this thread only when
		 * the last element is received.
982 983
		 */
		if (i < num_rqst - 1)
984 985 986
			midQ[i]->callback = cifs_compound_callback;
		else
			midQ[i]->callback = cifs_compound_last_callback;
L
Linus Torvalds 已提交
987
	}
988
	cifs_in_send_inc(ses->server);
R
Ronnie Sahlberg 已提交
989
	rc = smb_send_rqst(ses->server, num_rqst, rqst, flags);
990
	cifs_in_send_dec(ses->server);
R
Ronnie Sahlberg 已提交
991 992 993

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

995 996
	if (rc < 0) {
		revert_current_mid(ses->server, num_rqst);
997
		ses->server->sequence_number -= 2;
998
	}
R
Ronnie Sahlberg 已提交
999

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

1002 1003 1004
	if (rc < 0) {
		/* Sending failed for some reason - return credits back */
		for (i = 0; i < num_rqst; i++)
1005
			add_credits(ses->server, &credits[i], optype);
1006
		goto out;
1007 1008 1009 1010 1011 1012 1013 1014 1015
	}

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

1017 1018 1019 1020 1021 1022
	/*
	 * 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 已提交
1023

1024 1025
	if (timeout == CIFS_ASYNC_OP)
		goto out;
R
Ronnie Sahlberg 已提交
1026

1027
	for (i = 0; i < num_rqst; i++) {
R
Ronnie Sahlberg 已提交
1028
		rc = wait_for_response(ses->server, midQ[i]);
1029 1030 1031 1032 1033
		if (rc != 0)
			break;
	}
	if (rc != 0) {
		for (; i < num_rqst; i++) {
1034 1035
			cifs_dbg(VFS, "Cancelling wait for mid %llu cmd: %d\n",
				 midQ[i]->mid, le16_to_cpu(midQ[i]->command));
R
Ronnie Sahlberg 已提交
1036 1037 1038 1039
			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;
1040
				midQ[i]->callback = cifs_cancelled_callback;
1041
				cancelled_mid[i] = true;
1042
				credits[i].value = 0;
R
Ronnie Sahlberg 已提交
1043
			}
1044
			spin_unlock(&GlobalMid_Lock);
R
Ronnie Sahlberg 已提交
1045
		}
1046 1047 1048 1049 1050
	}

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

		rc = cifs_sync_mid_result(midQ[i], ses->server);
		if (rc != 0) {
1054 1055 1056
			/* mark this mid as cancelled to not free it below */
			cancelled_mid[i] = true;
			goto out;
1057
		}
1058

R
Ronnie Sahlberg 已提交
1059 1060 1061 1062 1063 1064
		if (!midQ[i]->resp_buf ||
		    midQ[i]->mid_state != MID_RESPONSE_RECEIVED) {
			rc = -EIO;
			cifs_dbg(FYI, "Bad MID state?\n");
			goto out;
		}
1065

R
Ronnie Sahlberg 已提交
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
		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 已提交
1078

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

R
Ronnie Sahlberg 已提交
1083
	}
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095

	/*
	 * 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 已提交
1096
out:
1097 1098 1099 1100 1101 1102
	/*
	 * 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.
	 */
1103 1104 1105 1106
	for (i = 0; i < num_rqst; i++) {
		if (!cancelled_mid[i])
			cifs_delete_mid(midQ[i]);
	}
L
Linus Torvalds 已提交
1107

1108 1109
	return rc;
}
L
Linus Torvalds 已提交
1110

R
Ronnie Sahlberg 已提交
1111 1112 1113 1114 1115 1116 1117 1118 1119
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);
}

1120 1121 1122 1123 1124 1125
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;
1126
	struct kvec s_iov[CIFS_MAX_IOV_SIZE], *new_iov;
1127 1128
	int rc;

1129
	if (n_vec + 1 > CIFS_MAX_IOV_SIZE) {
1130 1131
		new_iov = kmalloc_array(n_vec + 1, sizeof(struct kvec),
					GFP_KERNEL);
1132 1133 1134
		if (!new_iov) {
			/* otherwise cifs_send_recv below sets resp_buf_type */
			*resp_buf_type = CIFS_NO_BUFFER;
1135
			return -ENOMEM;
1136
		}
1137 1138
	} else
		new_iov = s_iov;
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152

	/* 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);
1153 1154
	if (n_vec + 1 > CIFS_MAX_IOV_SIZE)
		kfree(new_iov);
1155 1156 1157
	return rc;
}

L
Linus Torvalds 已提交
1158
int
1159
SendReceive(const unsigned int xid, struct cifs_ses *ses,
L
Linus Torvalds 已提交
1160
	    struct smb_hdr *in_buf, struct smb_hdr *out_buf,
1161
	    int *pbytes_returned, const int timeout)
L
Linus Torvalds 已提交
1162 1163 1164
{
	int rc = 0;
	struct mid_q_entry *midQ;
1165 1166 1167
	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 };
1168
	struct cifs_credits credits = { .value = 1, .instance = 0 };
L
Linus Torvalds 已提交
1169 1170

	if (ses == NULL) {
1171
		cifs_dbg(VFS, "Null smb session\n");
L
Linus Torvalds 已提交
1172 1173
		return -EIO;
	}
S
Steve French 已提交
1174
	if (ses->server == NULL) {
1175
		cifs_dbg(VFS, "Null tcp session\n");
L
Linus Torvalds 已提交
1176 1177 1178
		return -EIO;
	}

S
Steve French 已提交
1179
	if (ses->server->tcpStatus == CifsExiting)
1180 1181
		return -ENOENT;

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

1186
	if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
1187
		cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
1188
			 len);
1189 1190 1191
		return -EIO;
	}

1192
	rc = wait_for_free_request(ses->server, timeout, 0, &credits.instance);
J
[CIFS]  
Jeremy Allison 已提交
1193 1194 1195
	if (rc)
		return rc;

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

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

J
[CIFS]  
Jeremy Allison 已提交
1202 1203
	rc = allocate_mid(ses, in_buf, &midQ);
	if (rc) {
J
Jeff Layton 已提交
1204
		mutex_unlock(&ses->server->srv_mutex);
J
[CIFS]  
Jeremy Allison 已提交
1205
		/* Update # of requests on wire to server */
1206
		add_credits(ses->server, &credits, 0);
J
[CIFS]  
Jeremy Allison 已提交
1207
		return rc;
L
Linus Torvalds 已提交
1208 1209
	}

1210
	rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
1211 1212 1213 1214
	if (rc) {
		mutex_unlock(&ses->server->srv_mutex);
		goto out;
	}
L
Linus Torvalds 已提交
1215

1216
	midQ->mid_state = MID_REQUEST_SUBMITTED;
1217 1218

	cifs_in_send_inc(ses->server);
1219
	rc = smb_send(ses->server, in_buf, len);
1220 1221
	cifs_in_send_dec(ses->server);
	cifs_save_when_sent(midQ);
1222 1223 1224 1225

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

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

S
Steve French 已提交
1228
	if (rc < 0)
J
[CIFS]  
Jeremy Allison 已提交
1229 1230
		goto out;

1231
	if (timeout == CIFS_ASYNC_OP)
J
[CIFS]  
Jeremy Allison 已提交
1232
		goto out;
L
Linus Torvalds 已提交
1233

1234
	rc = wait_for_response(ses->server, midQ);
1235
	if (rc != 0) {
1236
		send_cancel(ses->server, &rqst, midQ);
1237
		spin_lock(&GlobalMid_Lock);
1238
		if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
1239 1240 1241
			/* no longer considered to be "in-flight" */
			midQ->callback = DeleteMidQEntry;
			spin_unlock(&GlobalMid_Lock);
1242
			add_credits(ses->server, &credits, 0);
1243 1244 1245 1246
			return rc;
		}
		spin_unlock(&GlobalMid_Lock);
	}
L
Linus Torvalds 已提交
1247

1248
	rc = cifs_sync_mid_result(midQ, ses->server);
1249
	if (rc != 0) {
1250
		add_credits(ses->server, &credits, 0);
L
Linus Torvalds 已提交
1251 1252
		return rc;
	}
1253

1254
	if (!midQ->resp_buf || !out_buf ||
1255
	    midQ->mid_state != MID_RESPONSE_RECEIVED) {
1256
		rc = -EIO;
1257
		cifs_dbg(VFS, "Bad MID state?\n");
1258
		goto out;
L
Linus Torvalds 已提交
1259
	}
J
[CIFS]  
Jeremy Allison 已提交
1260

1261
	*pbytes_returned = get_rfc1002_length(midQ->resp_buf);
1262 1263
	memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
	rc = cifs_check_receive(midQ, ses->server, 0);
J
[CIFS]  
Jeremy Allison 已提交
1264
out:
P
Pavel Shilovsky 已提交
1265
	cifs_delete_mid(midQ);
1266
	add_credits(ses->server, &credits, 0);
L
Linus Torvalds 已提交
1267

J
[CIFS]  
Jeremy Allison 已提交
1268 1269
	return rc;
}
L
Linus Torvalds 已提交
1270

J
[CIFS]  
Jeremy Allison 已提交
1271 1272 1273 1274
/* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
   blocking lock to return. */

static int
1275
send_lock_cancel(const unsigned int xid, struct cifs_tcon *tcon,
J
[CIFS]  
Jeremy Allison 已提交
1276 1277 1278 1279
			struct smb_hdr *in_buf,
			struct smb_hdr *out_buf)
{
	int bytes_returned;
1280
	struct cifs_ses *ses = tcon->ses;
J
[CIFS]  
Jeremy Allison 已提交
1281 1282 1283 1284 1285 1286 1287 1288 1289
	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;
1290
	pSMB->hdr.Mid = get_next_mid(ses->server);
J
[CIFS]  
Jeremy Allison 已提交
1291 1292

	return SendReceive(xid, ses, in_buf, out_buf,
1293
			&bytes_returned, 0);
J
[CIFS]  
Jeremy Allison 已提交
1294 1295 1296
}

int
1297
SendReceiveBlockingLock(const unsigned int xid, struct cifs_tcon *tcon,
J
[CIFS]  
Jeremy Allison 已提交
1298 1299 1300 1301 1302 1303
	    struct smb_hdr *in_buf, struct smb_hdr *out_buf,
	    int *pbytes_returned)
{
	int rc = 0;
	int rstart = 0;
	struct mid_q_entry *midQ;
1304
	struct cifs_ses *ses;
1305 1306 1307
	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 };
1308
	unsigned int instance;
J
[CIFS]  
Jeremy Allison 已提交
1309 1310

	if (tcon == NULL || tcon->ses == NULL) {
1311
		cifs_dbg(VFS, "Null smb session\n");
J
[CIFS]  
Jeremy Allison 已提交
1312 1313 1314 1315
		return -EIO;
	}
	ses = tcon->ses;

S
Steve French 已提交
1316
	if (ses->server == NULL) {
1317
		cifs_dbg(VFS, "Null tcp session\n");
J
[CIFS]  
Jeremy Allison 已提交
1318 1319 1320
		return -EIO;
	}

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

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

1328
	if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
1329
		cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
1330
			 len);
1331 1332 1333
		return -EIO;
	}

1334 1335
	rc = wait_for_free_request(ses->server, CIFS_BLOCKING_OP, 0,
				   &instance);
J
[CIFS]  
Jeremy Allison 已提交
1336 1337 1338
	if (rc)
		return rc;

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

J
Jeff Layton 已提交
1343
	mutex_lock(&ses->server->srv_mutex);
J
[CIFS]  
Jeremy Allison 已提交
1344 1345 1346

	rc = allocate_mid(ses, in_buf, &midQ);
	if (rc) {
J
Jeff Layton 已提交
1347
		mutex_unlock(&ses->server->srv_mutex);
J
[CIFS]  
Jeremy Allison 已提交
1348 1349 1350 1351
		return rc;
	}

	rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
1352
	if (rc) {
P
Pavel Shilovsky 已提交
1353
		cifs_delete_mid(midQ);
1354 1355 1356
		mutex_unlock(&ses->server->srv_mutex);
		return rc;
	}
L
Linus Torvalds 已提交
1357

1358
	midQ->mid_state = MID_REQUEST_SUBMITTED;
1359
	cifs_in_send_inc(ses->server);
1360
	rc = smb_send(ses->server, in_buf, len);
1361 1362
	cifs_in_send_dec(ses->server);
	cifs_save_when_sent(midQ);
1363 1364 1365 1366

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

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

S
Steve French 已提交
1369
	if (rc < 0) {
P
Pavel Shilovsky 已提交
1370
		cifs_delete_mid(midQ);
J
[CIFS]  
Jeremy Allison 已提交
1371 1372 1373 1374 1375
		return rc;
	}

	/* Wait for a reply - allow signals to interrupt. */
	rc = wait_event_interruptible(ses->server->response_q,
1376
		(!(midQ->mid_state == MID_REQUEST_SUBMITTED)) ||
J
[CIFS]  
Jeremy Allison 已提交
1377 1378 1379 1380 1381
		((ses->server->tcpStatus != CifsGood) &&
		 (ses->server->tcpStatus != CifsNew)));

	/* Were we interrupted by a signal ? */
	if ((rc == -ERESTARTSYS) &&
1382
		(midQ->mid_state == MID_REQUEST_SUBMITTED) &&
J
[CIFS]  
Jeremy Allison 已提交
1383 1384 1385 1386 1387 1388
		((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. */
1389
			rc = send_cancel(ses->server, &rqst, midQ);
J
[CIFS]  
Jeremy Allison 已提交
1390
			if (rc) {
P
Pavel Shilovsky 已提交
1391
				cifs_delete_mid(midQ);
J
[CIFS]  
Jeremy Allison 已提交
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
				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 已提交
1403
				cifs_delete_mid(midQ);
J
[CIFS]  
Jeremy Allison 已提交
1404 1405 1406 1407
				return rc;
			}
		}

1408 1409
		rc = wait_for_response(ses->server, midQ);
		if (rc) {
1410
			send_cancel(ses->server, &rqst, midQ);
1411
			spin_lock(&GlobalMid_Lock);
1412
			if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
1413 1414 1415 1416 1417 1418
				/* no longer considered to be "in-flight" */
				midQ->callback = DeleteMidQEntry;
				spin_unlock(&GlobalMid_Lock);
				return rc;
			}
			spin_unlock(&GlobalMid_Lock);
J
[CIFS]  
Jeremy Allison 已提交
1419
		}
1420 1421 1422

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

1425
	rc = cifs_sync_mid_result(midQ, ses->server);
1426
	if (rc != 0)
J
[CIFS]  
Jeremy Allison 已提交
1427
		return rc;
1428

1429
	/* rcvd frame is ok */
1430
	if (out_buf == NULL || midQ->mid_state != MID_RESPONSE_RECEIVED) {
1431
		rc = -EIO;
1432
		cifs_dbg(VFS, "Bad MID state?\n");
1433 1434
		goto out;
	}
L
Linus Torvalds 已提交
1435

1436
	*pbytes_returned = get_rfc1002_length(midQ->resp_buf);
1437 1438
	memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
	rc = cifs_check_receive(midQ, ses->server, 0);
1439
out:
P
Pavel Shilovsky 已提交
1440
	cifs_delete_mid(midQ);
J
[CIFS]  
Jeremy Allison 已提交
1441 1442
	if (rstart && rc == -EACCES)
		return -ERESTARTSYS;
L
Linus Torvalds 已提交
1443 1444
	return rc;
}