transport.c 24.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 *   fs/cifs/transport.c
 *
S
Steve French 已提交
4
 *   Copyright (C) International Business Machines  Corp., 2002,2008
L
Linus Torvalds 已提交
5
 *   Author(s): Steve French (sfrench@us.ibm.com)
6
 *   Jeremy Allison (jra@samba.org) 2006.
S
Steve French 已提交
7
 *
L
Linus Torvalds 已提交
8 9 10 11 12 13 14 15 16 17 18 19
 *   This library is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation; either version 2.1 of the License, or
 *   (at your option) any later version.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 *   the GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with this library; if not, write to the Free Software
S
Steve French 已提交
20
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
L
Linus Torvalds 已提交
21 22 23 24
 */

#include <linux/fs.h>
#include <linux/list.h>
25
#include <linux/gfp.h>
L
Linus Torvalds 已提交
26 27 28
#include <linux/wait.h>
#include <linux/net.h>
#include <linux/delay.h>
29
#include <linux/freezer.h>
L
Linus Torvalds 已提交
30 31 32 33 34 35 36
#include <asm/uaccess.h>
#include <asm/processor.h>
#include <linux/mempool.h>
#include "cifspdu.h"
#include "cifsglob.h"
#include "cifsproto.h"
#include "cifs_debug.h"
37

38 39
void
cifs_wake_up_task(struct mid_q_entry *mid)
40 41 42 43
{
	wake_up_process(mid->callback_data);
}

J
Jeff Layton 已提交
44
struct mid_q_entry *
45
AllocMidQEntry(const struct smb_hdr *smb_buffer, struct TCP_Server_Info *server)
L
Linus Torvalds 已提交
46 47 48
{
	struct mid_q_entry *temp;

49
	if (server == NULL) {
50
		cERROR(1, "Null TCP session in AllocMidQEntry");
L
Linus Torvalds 已提交
51 52
		return NULL;
	}
53

54
	temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
L
Linus Torvalds 已提交
55 56 57
	if (temp == NULL)
		return temp;
	else {
58
		memset(temp, 0, sizeof(struct mid_q_entry));
L
Linus Torvalds 已提交
59 60
		temp->mid = smb_buffer->Mid;	/* always LE */
		temp->pid = current->pid;
61 62
		temp->command = cpu_to_le16(smb_buffer->Command);
		cFYI(1, "For smb_command %d", smb_buffer->Command);
S
Steve French 已提交
63 64 65
	/*	do_gettimeofday(&temp->when_sent);*/ /* easier to use jiffies */
		/* when mid allocated can be before when sent */
		temp->when_alloc = jiffies;
66
		temp->server = server;
67 68 69 70 71

		/*
		 * The default is for the mid to be synchronous, so the
		 * default callback just wakes up the current task.
		 */
72
		temp->callback = cifs_wake_up_task;
73
		temp->callback_data = current;
L
Linus Torvalds 已提交
74 75 76
	}

	atomic_inc(&midCount);
77
	temp->mid_state = MID_REQUEST_ALLOCATED;
L
Linus Torvalds 已提交
78 79 80
	return temp;
}

81
void
L
Linus Torvalds 已提交
82 83
DeleteMidQEntry(struct mid_q_entry *midEntry)
{
S
Steve French 已提交
84
#ifdef CONFIG_CIFS_STATS2
85
	__le16 command = midEntry->server->vals->lock_cmd;
S
Steve French 已提交
86 87
	unsigned long now;
#endif
88
	midEntry->mid_state = MID_FREE;
89
	atomic_dec(&midCount);
90
	if (midEntry->large_buf)
91 92 93
		cifs_buf_release(midEntry->resp_buf);
	else
		cifs_small_buf_release(midEntry->resp_buf);
S
Steve French 已提交
94 95 96 97
#ifdef CONFIG_CIFS_STATS2
	now = jiffies;
	/* commands taking longer than one second are indications that
	   something is wrong, unless it is quite a slow link or server */
S
Steve French 已提交
98
	if ((now - midEntry->when_alloc) > HZ) {
99
		if ((cifsFYI & CIFS_TIMER) && (midEntry->command != command)) {
100
			printk(KERN_DEBUG " CIFS slow rsp: cmd %d mid %llu",
S
Steve French 已提交
101 102 103 104 105 106 107 108
			       midEntry->command, midEntry->mid);
			printk(" A: 0x%lx S: 0x%lx R: 0x%lx\n",
			       now - midEntry->when_alloc,
			       now - midEntry->when_sent,
			       now - midEntry->when_received);
		}
	}
#endif
L
Linus Torvalds 已提交
109 110 111
	mempool_free(midEntry, cifs_mid_poolp);
}

P
Pavel Shilovsky 已提交
112 113
void
cifs_delete_mid(struct mid_q_entry *mid)
114 115 116 117 118 119 120 121
{
	spin_lock(&GlobalMid_Lock);
	list_del(&mid->qhead);
	spin_unlock(&GlobalMid_Lock);

	DeleteMidQEntry(mid);
}

122 123 124 125 126 127 128 129 130 131
/*
 * smb_send_kvec - send an array of kvecs to the server
 * @server:	Server to send the data to
 * @iov:	Pointer to array of kvecs
 * @n_vec:	length of kvec array
 * @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.
 */
132
static int
133 134
smb_send_kvec(struct TCP_Server_Info *server, struct kvec *iov, size_t n_vec,
		size_t *sent)
L
Linus Torvalds 已提交
135 136 137 138
{
	int rc = 0;
	int i = 0;
	struct msghdr smb_msg;
139 140
	unsigned int remaining;
	size_t first_vec = 0;
141
	struct socket *ssocket = server->ssocket;
142

143 144
	*sent = 0;

S
Steve French 已提交
145
	if (ssocket == NULL)
L
Linus Torvalds 已提交
146
		return -ENOTSOCK; /* BB eventually add reconnect code here */
147

148
	smb_msg.msg_name = (struct sockaddr *) &server->dstaddr;
149
	smb_msg.msg_namelen = sizeof(struct sockaddr);
L
Linus Torvalds 已提交
150 151
	smb_msg.msg_control = NULL;
	smb_msg.msg_controllen = 0;
152
	if (server->noblocksnd)
153 154 155
		smb_msg.msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL;
	else
		smb_msg.msg_flags = MSG_NOSIGNAL;
L
Linus Torvalds 已提交
156

157
	remaining = 0;
158
	for (i = 0; i < n_vec; i++)
159
		remaining += iov[i].iov_len;
L
Linus Torvalds 已提交
160

161
	i = 0;
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
	while (remaining) {
		/*
		 * 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.
		 */
181
		rc = kernel_sendmsg(ssocket, &smb_msg, &iov[first_vec],
182 183
				    n_vec - first_vec, remaining);
		if (rc == -ENOSPC || rc == -EAGAIN) {
L
Linus Torvalds 已提交
184
			i++;
185 186 187
			if (i >= 14 || (!server->noblocksnd && (i > 2))) {
				cERROR(1, "sends on sock %p stuck for 15 "
					  "seconds", ssocket);
L
Linus Torvalds 已提交
188 189 190
				rc = -EAGAIN;
				break;
			}
191
			msleep(1 << i);
L
Linus Torvalds 已提交
192 193
			continue;
		}
194

S
Steve French 已提交
195
		if (rc < 0)
L
Linus Torvalds 已提交
196
			break;
197

198 199 200 201 202
		/* send was at least partially successful */
		*sent += rc;

		if (rc == remaining) {
			remaining = 0;
S
Steve French 已提交
203
			break;
204 205 206 207
		}

		if (rc > remaining) {
			cERROR(1, "sent %d requested %d", rc, remaining);
208 209
			break;
		}
210

S
Steve French 已提交
211
		if (rc == 0) {
212 213
			/* should never happen, letting socket clear before
			   retrying is our only obvious option here */
214
			cERROR(1, "tcp sent no data");
215 216
			msleep(500);
			continue;
217
		}
218 219 220

		remaining -= rc;

221
		/* the line below resets i */
222 223 224 225 226 227 228 229 230 231 232 233
		for (i = first_vec; i < n_vec; i++) {
			if (iov[i].iov_len) {
				if (rc > iov[i].iov_len) {
					rc -= iov[i].iov_len;
					iov[i].iov_len = 0;
				} else {
					iov[i].iov_base += rc;
					iov[i].iov_len -= rc;
					first_vec = i;
					break;
				}
			}
234
		}
235

236
		i = 0; /* in case we get ENOSPC on the next send */
237
		rc = 0;
L
Linus Torvalds 已提交
238
	}
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
	return rc;
}

static int
smb_send_rqst(struct TCP_Server_Info *server, struct smb_rqst *rqst)
{
	int rc;
	struct kvec *iov = rqst->rq_iov;
	int n_vec = rqst->rq_nvec;
	unsigned int smb_buf_length = get_rfc1002_length(iov[0].iov_base);
	size_t total_len;

	cFYI(1, "Sending smb: smb_len=%u", smb_buf_length);
	dump_smb(iov[0].iov_base, iov[0].iov_len);

	rc = smb_send_kvec(server, iov, n_vec, &total_len);
L
Linus Torvalds 已提交
255

256
	if ((total_len > 0) && (total_len != smb_buf_length + 4)) {
257 258 259 260 261 262 263
		cFYI(1, "partial send (wanted=%u sent=%zu): terminating "
			"session", smb_buf_length + 4, total_len);
		/*
		 * 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
		 */
264 265 266
		server->tcpStatus = CifsNeedReconnect;
	}

267
	if (rc < 0 && rc != -EINTR)
268
		cERROR(1, "Error %d sending data on socket to server", rc);
269
	else
L
Linus Torvalds 已提交
270 271 272 273 274
		rc = 0;

	return rc;
}

275 276 277 278 279 280 281 282 283
static int
smb_sendv(struct TCP_Server_Info *server, struct kvec *iov, int n_vec)
{
	struct smb_rqst rqst = { .rq_iov = iov,
				 .rq_nvec = n_vec };

	return smb_send_rqst(server, &rqst);
}

284 285 286 287 288 289 290 291 292 293 294 295
int
smb_send(struct TCP_Server_Info *server, struct smb_hdr *smb_buffer,
	 unsigned int smb_buf_length)
{
	struct kvec iov;

	iov.iov_base = smb_buffer;
	iov.iov_len = smb_buf_length + 4;

	return smb_sendv(server, &iov, 1);
}

P
Pavel Shilovsky 已提交
296
static int
297
wait_for_free_credits(struct TCP_Server_Info *server, const int timeout,
298
		      int *credits)
L
Linus Torvalds 已提交
299
{
300 301
	int rc;

P
Pavel Shilovsky 已提交
302
	spin_lock(&server->req_lock);
303
	if (timeout == CIFS_ASYNC_OP) {
L
Linus Torvalds 已提交
304
		/* oplock breaks must not be held up */
P
Pavel Shilovsky 已提交
305
		server->in_flight++;
306
		*credits -= 1;
P
Pavel Shilovsky 已提交
307
		spin_unlock(&server->req_lock);
308 309 310 311
		return 0;
	}

	while (1) {
312
		if (*credits <= 0) {
P
Pavel Shilovsky 已提交
313
			spin_unlock(&server->req_lock);
314
			cifs_num_waiters_inc(server);
315
			rc = wait_event_killable(server->request_q,
316
						 has_credits(server, credits));
317
			cifs_num_waiters_dec(server);
318 319
			if (rc)
				return rc;
P
Pavel Shilovsky 已提交
320
			spin_lock(&server->req_lock);
321
		} else {
322
			if (server->tcpStatus == CifsExiting) {
P
Pavel Shilovsky 已提交
323
				spin_unlock(&server->req_lock);
324
				return -ENOENT;
L
Linus Torvalds 已提交
325
			}
326

327 328 329 330
			/*
			 * Can not count locking commands against total
			 * as they are allowed to block on server.
			 */
331 332

			/* update # of requests on the wire to server */
333
			if (timeout != CIFS_BLOCKING_OP) {
334
				*credits -= 1;
P
Pavel Shilovsky 已提交
335
				server->in_flight++;
336
			}
P
Pavel Shilovsky 已提交
337
			spin_unlock(&server->req_lock);
338
			break;
L
Linus Torvalds 已提交
339 340
		}
	}
J
[CIFS]  
Jeremy Allison 已提交
341 342
	return 0;
}
L
Linus Torvalds 已提交
343

344
static int
345 346
wait_for_free_request(struct TCP_Server_Info *server, const int timeout,
		      const int optype)
347
{
348 349
	return wait_for_free_credits(server, timeout,
				server->ops->get_credits_field(server, optype));
350 351
}

352
static int allocate_mid(struct cifs_ses *ses, struct smb_hdr *in_buf,
J
[CIFS]  
Jeremy Allison 已提交
353 354
			struct mid_q_entry **ppmidQ)
{
L
Linus Torvalds 已提交
355
	if (ses->server->tcpStatus == CifsExiting) {
J
[CIFS]  
Jeremy Allison 已提交
356
		return -ENOENT;
357 358 359
	}

	if (ses->server->tcpStatus == CifsNeedReconnect) {
360
		cFYI(1, "tcp session dead - return to caller to retry");
J
[CIFS]  
Jeremy Allison 已提交
361
		return -EAGAIN;
362 363 364
	}

	if (ses->status != CifsGood) {
L
Linus Torvalds 已提交
365
		/* check if SMB session is bad because we are setting it up */
S
Steve French 已提交
366
		if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
S
Steve French 已提交
367
			(in_buf->Command != SMB_COM_NEGOTIATE))
J
[CIFS]  
Jeremy Allison 已提交
368
			return -EAGAIN;
S
Steve French 已提交
369
		/* else ok - we are setting up session */
L
Linus Torvalds 已提交
370
	}
371
	*ppmidQ = AllocMidQEntry(in_buf, ses->server);
372
	if (*ppmidQ == NULL)
J
[CIFS]  
Jeremy Allison 已提交
373
		return -ENOMEM;
374 375 376
	spin_lock(&GlobalMid_Lock);
	list_add_tail(&(*ppmidQ)->qhead, &ses->server->pending_mid_q);
	spin_unlock(&GlobalMid_Lock);
J
[CIFS]  
Jeremy Allison 已提交
377 378 379
	return 0;
}

380 381
static int
wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *midQ)
J
[CIFS]  
Jeremy Allison 已提交
382
{
383
	int error;
J
[CIFS]  
Jeremy Allison 已提交
384

385
	error = wait_event_freezekillable(server->response_q,
386
				    midQ->mid_state != MID_REQUEST_SUBMITTED);
387 388
	if (error < 0)
		return -ERESTARTSYS;
J
[CIFS]  
Jeremy Allison 已提交
389

390
	return 0;
J
[CIFS]  
Jeremy Allison 已提交
391 392
}

393
int
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
cifs_setup_async_request(struct TCP_Server_Info *server, struct kvec *iov,
			 unsigned int nvec, struct mid_q_entry **ret_mid)
{
	int rc;
	struct smb_hdr *hdr = (struct smb_hdr *)iov[0].iov_base;
	struct mid_q_entry *mid;

	/* enable signing if server requires it */
	if (server->sec_mode & (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
		hdr->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;

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

409
	rc = cifs_sign_smbv(iov, nvec, server, &mid->sequence_number);
410 411 412 413 414
	if (rc) {
		DeleteMidQEntry(mid);
		return rc;
	}

415
	*ret_mid = mid;
416
	return 0;
417
}
418

J
Jeff Layton 已提交
419 420 421 422 423
/*
 * Send a SMB request and set the callback function in the mid to handle
 * the result. Caller is responsible for dealing with timeouts.
 */
int
424
cifs_call_async(struct TCP_Server_Info *server, struct kvec *iov,
425
		unsigned int nvec, mid_receive_t *receive,
426
		mid_callback_t *callback, void *cbdata, const int flags)
J
Jeff Layton 已提交
427
{
428
	int rc, timeout, optype;
J
Jeff Layton 已提交
429 430
	struct mid_q_entry *mid;

431 432 433 434
	timeout = flags & CIFS_TIMEOUT_MASK;
	optype = flags & CIFS_OP_MASK;

	rc = wait_for_free_request(server, timeout, optype);
J
Jeff Layton 已提交
435 436 437 438
	if (rc)
		return rc;

	mutex_lock(&server->srv_mutex);
439
	rc = server->ops->setup_async_request(server, iov, nvec, &mid);
440
	if (rc) {
J
Jeff Layton 已提交
441
		mutex_unlock(&server->srv_mutex);
442
		add_credits(server, 1, optype);
443
		wake_up(&server->request_q);
444
		return rc;
J
Jeff Layton 已提交
445 446
	}

447
	mid->receive = receive;
J
Jeff Layton 已提交
448 449
	mid->callback = callback;
	mid->callback_data = cbdata;
450
	mid->mid_state = MID_REQUEST_SUBMITTED;
451

452 453 454 455 456 457
	/* put it on the pending_mid_q */
	spin_lock(&GlobalMid_Lock);
	list_add_tail(&mid->qhead, &server->pending_mid_q);
	spin_unlock(&GlobalMid_Lock);


458
	cifs_in_send_inc(server);
459
	rc = smb_sendv(server, iov, nvec);
460 461
	cifs_in_send_dec(server);
	cifs_save_when_sent(mid);
J
Jeff Layton 已提交
462
	mutex_unlock(&server->srv_mutex);
463

464 465
	if (rc == 0)
		return 0;
J
Jeff Layton 已提交
466

P
Pavel Shilovsky 已提交
467
	cifs_delete_mid(mid);
468
	add_credits(server, 1, optype);
J
Jeff Layton 已提交
469 470 471 472
	wake_up(&server->request_q);
	return rc;
}

473 474 475 476 477 478 479 480 481 482
/*
 *
 * 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
483
SendReceiveNoRsp(const unsigned int xid, struct cifs_ses *ses,
484
		 char *in_buf, int flags)
485 486 487 488 489
{
	int rc;
	struct kvec iov[1];
	int resp_buf_type;

490 491
	iov[0].iov_base = in_buf;
	iov[0].iov_len = get_rfc1002_length(in_buf) + 4;
492 493
	flags |= CIFS_NO_RESP;
	rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags);
494
	cFYI(DBG2, "SendRcvNoRsp flags %d rc %d", flags, rc);
495

496 497 498
	return rc;
}

499
static int
500
cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
501 502 503
{
	int rc = 0;

504 505
	cFYI(1, "%s: cmd=%d mid=%llu state=%d", __func__,
	     le16_to_cpu(mid->command), mid->mid, mid->mid_state);
506

J
Jeff Layton 已提交
507
	spin_lock(&GlobalMid_Lock);
508
	switch (mid->mid_state) {
J
Jeff Layton 已提交
509
	case MID_RESPONSE_RECEIVED:
510 511
		spin_unlock(&GlobalMid_Lock);
		return rc;
J
Jeff Layton 已提交
512 513 514
	case MID_RETRY_NEEDED:
		rc = -EAGAIN;
		break;
515 516 517
	case MID_RESPONSE_MALFORMED:
		rc = -EIO;
		break;
518 519 520
	case MID_SHUTDOWN:
		rc = -EHOSTDOWN;
		break;
J
Jeff Layton 已提交
521
	default:
522
		list_del_init(&mid->qhead);
523 524
		cERROR(1, "%s: invalid mid state mid=%llu state=%d", __func__,
		       mid->mid, mid->mid_state);
J
Jeff Layton 已提交
525
		rc = -EIO;
526 527 528
	}
	spin_unlock(&GlobalMid_Lock);

529
	DeleteMidQEntry(mid);
530 531 532
	return rc;
}

533 534
static inline int
send_cancel(struct TCP_Server_Info *server, void *buf, struct mid_q_entry *mid)
535
{
536 537
	return server->ops->send_cancel ?
				server->ops->send_cancel(server, buf, mid) : 0;
538 539
}

540 541 542 543
int
cifs_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
		   bool log_error)
{
544
	unsigned int len = get_rfc1002_length(mid->resp_buf) + 4;
545 546

	dump_smb(mid->resp_buf, min_t(u32, 92, len));
547 548

	/* convert the length into a more usable form */
549
	if (server->sec_mode & (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED)) {
550
		struct kvec iov;
551
		int rc = 0;
552 553
		struct smb_rqst rqst = { .rq_iov = &iov,
					 .rq_nvec = 1 };
554 555 556

		iov.iov_base = mid->resp_buf;
		iov.iov_len = len;
557
		/* FIXME: add code to kill session */
558
		rc = cifs_verify_signature(&rqst, server,
559 560 561 562
					   mid->sequence_number + 1);
		if (rc)
			cERROR(1, "SMB signature verification returned error = "
			       "%d", rc);
563 564 565 566 567 568
	}

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

569
int
570 571 572 573 574 575 576 577 578 579
cifs_setup_request(struct cifs_ses *ses, struct kvec *iov,
		   unsigned int nvec, struct mid_q_entry **ret_mid)
{
	int rc;
	struct smb_hdr *hdr = (struct smb_hdr *)iov[0].iov_base;
	struct mid_q_entry *mid;

	rc = allocate_mid(ses, hdr, &mid);
	if (rc)
		return rc;
580
	rc = cifs_sign_smbv(iov, nvec, ses->server, &mid->sequence_number);
581
	if (rc)
P
Pavel Shilovsky 已提交
582
		cifs_delete_mid(mid);
583 584 585 586
	*ret_mid = mid;
	return rc;
}

J
[CIFS]  
Jeremy Allison 已提交
587
int
588
SendReceive2(const unsigned int xid, struct cifs_ses *ses,
589
	     struct kvec *iov, int n_vec, int *resp_buf_type /* ret */,
590
	     const int flags)
J
[CIFS]  
Jeremy Allison 已提交
591 592
{
	int rc = 0;
593
	int timeout, optype;
J
[CIFS]  
Jeremy Allison 已提交
594
	struct mid_q_entry *midQ;
595
	char *buf = iov[0].iov_base;
596
	unsigned int credits = 1;
597

598 599
	timeout = flags & CIFS_TIMEOUT_MASK;
	optype = flags & CIFS_OP_MASK;
600

601
	*resp_buf_type = CIFS_NO_BUFFER;  /* no response buf yet */
J
[CIFS]  
Jeremy Allison 已提交
602 603

	if ((ses == NULL) || (ses->server == NULL)) {
604
		cifs_small_buf_release(buf);
605
		cERROR(1, "Null session");
J
[CIFS]  
Jeremy Allison 已提交
606 607 608
		return -EIO;
	}

S
Steve French 已提交
609
	if (ses->server->tcpStatus == CifsExiting) {
610
		cifs_small_buf_release(buf);
J
[CIFS]  
Jeremy Allison 已提交
611 612 613
		return -ENOENT;
	}

614 615 616 617 618
	/*
	 * Ensure that we do not send more than 50 overlapping requests
	 * to the same server. We may make this configurable later or
	 * use ses->maxReq.
	 */
J
[CIFS]  
Jeremy Allison 已提交
619

620
	rc = wait_for_free_request(ses->server, timeout, optype);
J
[CIFS]  
Jeremy Allison 已提交
621
	if (rc) {
622
		cifs_small_buf_release(buf);
J
[CIFS]  
Jeremy Allison 已提交
623 624 625
		return rc;
	}

626 627 628 629 630
	/*
	 * 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 已提交
631

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

634
	rc = ses->server->ops->setup_request(ses, iov, n_vec, &midQ);
J
[CIFS]  
Jeremy Allison 已提交
635
	if (rc) {
J
Jeff Layton 已提交
636
		mutex_unlock(&ses->server->srv_mutex);
637
		cifs_small_buf_release(buf);
J
[CIFS]  
Jeremy Allison 已提交
638
		/* Update # of requests on wire to server */
639
		add_credits(ses->server, 1, optype);
J
[CIFS]  
Jeremy Allison 已提交
640
		return rc;
L
Linus Torvalds 已提交
641 642
	}

643
	midQ->mid_state = MID_REQUEST_SUBMITTED;
644
	cifs_in_send_inc(ses->server);
645
	rc = smb_sendv(ses->server, iov, n_vec);
646 647
	cifs_in_send_dec(ses->server);
	cifs_save_when_sent(midQ);
J
[CIFS]  
Jeremy Allison 已提交
648

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

651
	if (rc < 0) {
652
		cifs_small_buf_release(buf);
J
[CIFS]  
Jeremy Allison 已提交
653
		goto out;
654
	}
655

656
	if (timeout == CIFS_ASYNC_OP) {
657
		cifs_small_buf_release(buf);
658
		goto out;
659
	}
660

661
	rc = wait_for_response(ses->server, midQ);
662
	if (rc != 0) {
663
		send_cancel(ses->server, buf, midQ);
664
		spin_lock(&GlobalMid_Lock);
665
		if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
666 667
			midQ->callback = DeleteMidQEntry;
			spin_unlock(&GlobalMid_Lock);
668
			cifs_small_buf_release(buf);
669
			add_credits(ses->server, 1, optype);
670 671 672 673
			return rc;
		}
		spin_unlock(&GlobalMid_Lock);
	}
674

675
	cifs_small_buf_release(buf);
676

677
	rc = cifs_sync_mid_result(midQ, ses->server);
678
	if (rc != 0) {
679
		add_credits(ses->server, 1, optype);
680 681
		return rc;
	}
682

683
	if (!midQ->resp_buf || midQ->mid_state != MID_RESPONSE_RECEIVED) {
684
		rc = -EIO;
685
		cFYI(1, "Bad MID state?");
686 687 688
		goto out;
	}

689 690 691
	buf = (char *)midQ->resp_buf;
	iov[0].iov_base = buf;
	iov[0].iov_len = get_rfc1002_length(buf) + 4;
692
	if (midQ->large_buf)
693
		*resp_buf_type = CIFS_LARGE_BUFFER;
694
	else
695 696 697
		*resp_buf_type = CIFS_SMALL_BUFFER;

	credits = ses->server->ops->get_credits(midQ);
698

699 700
	rc = ses->server->ops->check_receive(midQ, ses->server,
					     flags & CIFS_LOG_ERROR);
L
Linus Torvalds 已提交
701

P
Pavel Shilovsky 已提交
702
	/* mark it so buf will not be freed by cifs_delete_mid */
703 704
	if ((flags & CIFS_NO_RESP) == 0)
		midQ->resp_buf = NULL;
J
[CIFS]  
Jeremy Allison 已提交
705
out:
P
Pavel Shilovsky 已提交
706
	cifs_delete_mid(midQ);
707
	add_credits(ses->server, credits, optype);
L
Linus Torvalds 已提交
708

709 710
	return rc;
}
L
Linus Torvalds 已提交
711 712

int
713
SendReceive(const unsigned int xid, struct cifs_ses *ses,
L
Linus Torvalds 已提交
714
	    struct smb_hdr *in_buf, struct smb_hdr *out_buf,
715
	    int *pbytes_returned, const int timeout)
L
Linus Torvalds 已提交
716 717 718 719 720
{
	int rc = 0;
	struct mid_q_entry *midQ;

	if (ses == NULL) {
721
		cERROR(1, "Null smb session");
L
Linus Torvalds 已提交
722 723
		return -EIO;
	}
S
Steve French 已提交
724
	if (ses->server == NULL) {
725
		cERROR(1, "Null tcp session");
L
Linus Torvalds 已提交
726 727 728
		return -EIO;
	}

S
Steve French 已提交
729
	if (ses->server->tcpStatus == CifsExiting)
730 731
		return -ENOENT;

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

736 737
	if (be32_to_cpu(in_buf->smb_buf_length) > CIFSMaxBufSize +
			MAX_CIFS_HDR_SIZE - 4) {
738
		cERROR(1, "Illegal length, greater than maximum frame, %d",
739
			   be32_to_cpu(in_buf->smb_buf_length));
740 741 742
		return -EIO;
	}

743
	rc = wait_for_free_request(ses->server, timeout, 0);
J
[CIFS]  
Jeremy Allison 已提交
744 745 746
	if (rc)
		return rc;

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

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

J
[CIFS]  
Jeremy Allison 已提交
753 754
	rc = allocate_mid(ses, in_buf, &midQ);
	if (rc) {
J
Jeff Layton 已提交
755
		mutex_unlock(&ses->server->srv_mutex);
J
[CIFS]  
Jeremy Allison 已提交
756
		/* Update # of requests on wire to server */
757
		add_credits(ses->server, 1, 0);
J
[CIFS]  
Jeremy Allison 已提交
758
		return rc;
L
Linus Torvalds 已提交
759 760
	}

761
	rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
762 763 764 765
	if (rc) {
		mutex_unlock(&ses->server->srv_mutex);
		goto out;
	}
L
Linus Torvalds 已提交
766

767
	midQ->mid_state = MID_REQUEST_SUBMITTED;
768 769

	cifs_in_send_inc(ses->server);
770
	rc = smb_send(ses->server, in_buf, be32_to_cpu(in_buf->smb_buf_length));
771 772
	cifs_in_send_dec(ses->server);
	cifs_save_when_sent(midQ);
J
Jeff Layton 已提交
773
	mutex_unlock(&ses->server->srv_mutex);
J
[CIFS]  
Jeremy Allison 已提交
774

S
Steve French 已提交
775
	if (rc < 0)
J
[CIFS]  
Jeremy Allison 已提交
776 777
		goto out;

778
	if (timeout == CIFS_ASYNC_OP)
J
[CIFS]  
Jeremy Allison 已提交
779
		goto out;
L
Linus Torvalds 已提交
780

781
	rc = wait_for_response(ses->server, midQ);
782
	if (rc != 0) {
783
		send_cancel(ses->server, in_buf, midQ);
784
		spin_lock(&GlobalMid_Lock);
785
		if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
786 787 788
			/* no longer considered to be "in-flight" */
			midQ->callback = DeleteMidQEntry;
			spin_unlock(&GlobalMid_Lock);
789
			add_credits(ses->server, 1, 0);
790 791 792 793
			return rc;
		}
		spin_unlock(&GlobalMid_Lock);
	}
L
Linus Torvalds 已提交
794

795
	rc = cifs_sync_mid_result(midQ, ses->server);
796
	if (rc != 0) {
797
		add_credits(ses->server, 1, 0);
L
Linus Torvalds 已提交
798 799
		return rc;
	}
800

801
	if (!midQ->resp_buf || !out_buf ||
802
	    midQ->mid_state != MID_RESPONSE_RECEIVED) {
803
		rc = -EIO;
804
		cERROR(1, "Bad MID state?");
805
		goto out;
L
Linus Torvalds 已提交
806
	}
J
[CIFS]  
Jeremy Allison 已提交
807

808
	*pbytes_returned = get_rfc1002_length(midQ->resp_buf);
809 810
	memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
	rc = cifs_check_receive(midQ, ses->server, 0);
J
[CIFS]  
Jeremy Allison 已提交
811
out:
P
Pavel Shilovsky 已提交
812
	cifs_delete_mid(midQ);
813
	add_credits(ses->server, 1, 0);
L
Linus Torvalds 已提交
814

J
[CIFS]  
Jeremy Allison 已提交
815 816
	return rc;
}
L
Linus Torvalds 已提交
817

J
[CIFS]  
Jeremy Allison 已提交
818 819 820 821
/* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
   blocking lock to return. */

static int
822
send_lock_cancel(const unsigned int xid, struct cifs_tcon *tcon,
J
[CIFS]  
Jeremy Allison 已提交
823 824 825 826
			struct smb_hdr *in_buf,
			struct smb_hdr *out_buf)
{
	int bytes_returned;
827
	struct cifs_ses *ses = tcon->ses;
J
[CIFS]  
Jeremy Allison 已提交
828 829 830 831 832 833 834 835 836
	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;
837
	pSMB->hdr.Mid = get_next_mid(ses->server);
J
[CIFS]  
Jeremy Allison 已提交
838 839

	return SendReceive(xid, ses, in_buf, out_buf,
840
			&bytes_returned, 0);
J
[CIFS]  
Jeremy Allison 已提交
841 842 843
}

int
844
SendReceiveBlockingLock(const unsigned int xid, struct cifs_tcon *tcon,
J
[CIFS]  
Jeremy Allison 已提交
845 846 847 848 849 850
	    struct smb_hdr *in_buf, struct smb_hdr *out_buf,
	    int *pbytes_returned)
{
	int rc = 0;
	int rstart = 0;
	struct mid_q_entry *midQ;
851
	struct cifs_ses *ses;
J
[CIFS]  
Jeremy Allison 已提交
852 853

	if (tcon == NULL || tcon->ses == NULL) {
854
		cERROR(1, "Null smb session");
J
[CIFS]  
Jeremy Allison 已提交
855 856 857 858
		return -EIO;
	}
	ses = tcon->ses;

S
Steve French 已提交
859
	if (ses->server == NULL) {
860
		cERROR(1, "Null tcp session");
J
[CIFS]  
Jeremy Allison 已提交
861 862 863
		return -EIO;
	}

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

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

871 872
	if (be32_to_cpu(in_buf->smb_buf_length) > CIFSMaxBufSize +
			MAX_CIFS_HDR_SIZE - 4) {
873
		cERROR(1, "Illegal length, greater than maximum frame, %d",
874
			   be32_to_cpu(in_buf->smb_buf_length));
875 876 877
		return -EIO;
	}

878
	rc = wait_for_free_request(ses->server, CIFS_BLOCKING_OP, 0);
J
[CIFS]  
Jeremy Allison 已提交
879 880 881
	if (rc)
		return rc;

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

J
Jeff Layton 已提交
886
	mutex_lock(&ses->server->srv_mutex);
J
[CIFS]  
Jeremy Allison 已提交
887 888 889

	rc = allocate_mid(ses, in_buf, &midQ);
	if (rc) {
J
Jeff Layton 已提交
890
		mutex_unlock(&ses->server->srv_mutex);
J
[CIFS]  
Jeremy Allison 已提交
891 892 893 894
		return rc;
	}

	rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
895
	if (rc) {
P
Pavel Shilovsky 已提交
896
		cifs_delete_mid(midQ);
897 898 899
		mutex_unlock(&ses->server->srv_mutex);
		return rc;
	}
L
Linus Torvalds 已提交
900

901
	midQ->mid_state = MID_REQUEST_SUBMITTED;
902
	cifs_in_send_inc(ses->server);
903
	rc = smb_send(ses->server, in_buf, be32_to_cpu(in_buf->smb_buf_length));
904 905
	cifs_in_send_dec(ses->server);
	cifs_save_when_sent(midQ);
J
Jeff Layton 已提交
906
	mutex_unlock(&ses->server->srv_mutex);
J
[CIFS]  
Jeremy Allison 已提交
907

S
Steve French 已提交
908
	if (rc < 0) {
P
Pavel Shilovsky 已提交
909
		cifs_delete_mid(midQ);
J
[CIFS]  
Jeremy Allison 已提交
910 911 912 913 914
		return rc;
	}

	/* Wait for a reply - allow signals to interrupt. */
	rc = wait_event_interruptible(ses->server->response_q,
915
		(!(midQ->mid_state == MID_REQUEST_SUBMITTED)) ||
J
[CIFS]  
Jeremy Allison 已提交
916 917 918 919 920
		((ses->server->tcpStatus != CifsGood) &&
		 (ses->server->tcpStatus != CifsNew)));

	/* Were we interrupted by a signal ? */
	if ((rc == -ERESTARTSYS) &&
921
		(midQ->mid_state == MID_REQUEST_SUBMITTED) &&
J
[CIFS]  
Jeremy Allison 已提交
922 923 924 925 926 927
		((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. */
928
			rc = send_cancel(ses->server, in_buf, midQ);
J
[CIFS]  
Jeremy Allison 已提交
929
			if (rc) {
P
Pavel Shilovsky 已提交
930
				cifs_delete_mid(midQ);
J
[CIFS]  
Jeremy Allison 已提交
931 932 933 934 935 936 937 938 939 940 941
				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 已提交
942
				cifs_delete_mid(midQ);
J
[CIFS]  
Jeremy Allison 已提交
943 944 945 946
				return rc;
			}
		}

947 948
		rc = wait_for_response(ses->server, midQ);
		if (rc) {
949
			send_cancel(ses->server, in_buf, midQ);
950
			spin_lock(&GlobalMid_Lock);
951
			if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
952 953 954 955 956 957
				/* no longer considered to be "in-flight" */
				midQ->callback = DeleteMidQEntry;
				spin_unlock(&GlobalMid_Lock);
				return rc;
			}
			spin_unlock(&GlobalMid_Lock);
J
[CIFS]  
Jeremy Allison 已提交
958
		}
959 960 961

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

964
	rc = cifs_sync_mid_result(midQ, ses->server);
965
	if (rc != 0)
J
[CIFS]  
Jeremy Allison 已提交
966
		return rc;
967

968
	/* rcvd frame is ok */
969
	if (out_buf == NULL || midQ->mid_state != MID_RESPONSE_RECEIVED) {
970
		rc = -EIO;
971
		cERROR(1, "Bad MID state?");
972 973
		goto out;
	}
L
Linus Torvalds 已提交
974

975
	*pbytes_returned = get_rfc1002_length(midQ->resp_buf);
976 977
	memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
	rc = cifs_check_receive(midQ, ses->server, 0);
978
out:
P
Pavel Shilovsky 已提交
979
	cifs_delete_mid(midQ);
J
[CIFS]  
Jeremy Allison 已提交
980 981
	if (rstart && rc == -EACCES)
		return -ERESTARTSYS;
L
Linus Torvalds 已提交
982 983
	return rc;
}