[CIFS] cifs: Rename cERROR and cFYI to cifs_dbg
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / cifs / transport.c
CommitLineData
1da177e4
LT
1/*
2 * fs/cifs/transport.c
3 *
ad7a2926 4 * Copyright (C) International Business Machines Corp., 2002,2008
1da177e4 5 * Author(s): Steve French (sfrench@us.ibm.com)
14a441a2 6 * Jeremy Allison (jra@samba.org) 2006.
79a58d1f 7 *
1da177e4
LT
8 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published
10 * by the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
16 * the GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this library; if not, write to the Free Software
79a58d1f 20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1da177e4
LT
21 */
22
23#include <linux/fs.h>
24#include <linux/list.h>
5a0e3ad6 25#include <linux/gfp.h>
1da177e4
LT
26#include <linux/wait.h>
27#include <linux/net.h>
28#include <linux/delay.h>
f06ac72e 29#include <linux/freezer.h>
b8eed283 30#include <linux/tcp.h>
97bc00b3 31#include <linux/highmem.h>
1da177e4
LT
32#include <asm/uaccess.h>
33#include <asm/processor.h>
34#include <linux/mempool.h>
35#include "cifspdu.h"
36#include "cifsglob.h"
37#include "cifsproto.h"
38#include "cifs_debug.h"
50c2f753 39
2dc7e1c0
PS
40void
41cifs_wake_up_task(struct mid_q_entry *mid)
2b84a36c
JL
42{
43 wake_up_process(mid->callback_data);
44}
45
a6827c18 46struct mid_q_entry *
24b9b06b 47AllocMidQEntry(const struct smb_hdr *smb_buffer, struct TCP_Server_Info *server)
1da177e4
LT
48{
49 struct mid_q_entry *temp;
50
24b9b06b 51 if (server == NULL) {
f96637be 52 cifs_dbg(VFS, "Null TCP session in AllocMidQEntry\n");
1da177e4
LT
53 return NULL;
54 }
50c2f753 55
232087cb 56 temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
1da177e4
LT
57 if (temp == NULL)
58 return temp;
59 else {
26f57364 60 memset(temp, 0, sizeof(struct mid_q_entry));
1da177e4
LT
61 temp->mid = smb_buffer->Mid; /* always LE */
62 temp->pid = current->pid;
7c9421e1 63 temp->command = cpu_to_le16(smb_buffer->Command);
f96637be 64 cifs_dbg(FYI, "For smb_command %d\n", smb_buffer->Command);
1047abc1
SF
65 /* do_gettimeofday(&temp->when_sent);*/ /* easier to use jiffies */
66 /* when mid allocated can be before when sent */
67 temp->when_alloc = jiffies;
2dc7e1c0 68 temp->server = server;
2b84a36c
JL
69
70 /*
71 * The default is for the mid to be synchronous, so the
72 * default callback just wakes up the current task.
73 */
2dc7e1c0 74 temp->callback = cifs_wake_up_task;
2b84a36c 75 temp->callback_data = current;
1da177e4
LT
76 }
77
1da177e4 78 atomic_inc(&midCount);
7c9421e1 79 temp->mid_state = MID_REQUEST_ALLOCATED;
1da177e4
LT
80 return temp;
81}
82
766fdbb5 83void
1da177e4
LT
84DeleteMidQEntry(struct mid_q_entry *midEntry)
85{
1047abc1 86#ifdef CONFIG_CIFS_STATS2
2dc7e1c0 87 __le16 command = midEntry->server->vals->lock_cmd;
1047abc1
SF
88 unsigned long now;
89#endif
7c9421e1 90 midEntry->mid_state = MID_FREE;
8097531a 91 atomic_dec(&midCount);
7c9421e1 92 if (midEntry->large_buf)
b8643e1b
SF
93 cifs_buf_release(midEntry->resp_buf);
94 else
95 cifs_small_buf_release(midEntry->resp_buf);
1047abc1
SF
96#ifdef CONFIG_CIFS_STATS2
97 now = jiffies;
98 /* commands taking longer than one second are indications that
99 something is wrong, unless it is quite a slow link or server */
79a58d1f 100 if ((now - midEntry->when_alloc) > HZ) {
2dc7e1c0 101 if ((cifsFYI & CIFS_TIMER) && (midEntry->command != command)) {
7c9421e1 102 printk(KERN_DEBUG " CIFS slow rsp: cmd %d mid %llu",
1047abc1
SF
103 midEntry->command, midEntry->mid);
104 printk(" A: 0x%lx S: 0x%lx R: 0x%lx\n",
105 now - midEntry->when_alloc,
106 now - midEntry->when_sent,
107 now - midEntry->when_received);
108 }
109 }
110#endif
1da177e4
LT
111 mempool_free(midEntry, cifs_mid_poolp);
112}
113
3c1bf7e4
PS
114void
115cifs_delete_mid(struct mid_q_entry *mid)
ddc8cf8f
JL
116{
117 spin_lock(&GlobalMid_Lock);
118 list_del(&mid->qhead);
119 spin_unlock(&GlobalMid_Lock);
120
121 DeleteMidQEntry(mid);
122}
123
6f49f46b
JL
124/*
125 * smb_send_kvec - send an array of kvecs to the server
126 * @server: Server to send the data to
127 * @iov: Pointer to array of kvecs
128 * @n_vec: length of kvec array
129 * @sent: amount of data sent on socket is stored here
130 *
131 * Our basic "send data to server" function. Should be called with srv_mutex
132 * held. The caller is responsible for handling the results.
133 */
d6e04ae6 134static int
6f49f46b
JL
135smb_send_kvec(struct TCP_Server_Info *server, struct kvec *iov, size_t n_vec,
136 size_t *sent)
1da177e4
LT
137{
138 int rc = 0;
139 int i = 0;
140 struct msghdr smb_msg;
6f49f46b
JL
141 unsigned int remaining;
142 size_t first_vec = 0;
edf1ae40 143 struct socket *ssocket = server->ssocket;
50c2f753 144
6f49f46b
JL
145 *sent = 0;
146
a9f1b85e 147 smb_msg.msg_name = (struct sockaddr *) &server->dstaddr;
26f57364 148 smb_msg.msg_namelen = sizeof(struct sockaddr);
1da177e4
LT
149 smb_msg.msg_control = NULL;
150 smb_msg.msg_controllen = 0;
0496e02d 151 if (server->noblocksnd)
edf1ae40
SF
152 smb_msg.msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL;
153 else
154 smb_msg.msg_flags = MSG_NOSIGNAL;
1da177e4 155
6f49f46b 156 remaining = 0;
3e84469d 157 for (i = 0; i < n_vec; i++)
6f49f46b 158 remaining += iov[i].iov_len;
1da177e4 159
17680356 160 i = 0;
6f49f46b
JL
161 while (remaining) {
162 /*
163 * If blocking send, we try 3 times, since each can block
164 * for 5 seconds. For nonblocking we have to try more
165 * but wait increasing amounts of time allowing time for
166 * socket to clear. The overall time we wait in either
167 * case to send on the socket is about 15 seconds.
168 * Similarly we wait for 15 seconds for a response from
169 * the server in SendReceive[2] for the server to send
170 * a response back for most types of requests (except
171 * SMB Write past end of file which can be slow, and
172 * blocking lock operations). NFS waits slightly longer
173 * than CIFS, but this can make it take longer for
174 * nonresponsive servers to be detected and 15 seconds
175 * is more than enough time for modern networks to
176 * send a packet. In most cases if we fail to send
177 * after the retries we will kill the socket and
178 * reconnect which may clear the network problem.
179 */
3e84469d 180 rc = kernel_sendmsg(ssocket, &smb_msg, &iov[first_vec],
6f49f46b
JL
181 n_vec - first_vec, remaining);
182 if (rc == -ENOSPC || rc == -EAGAIN) {
b7a10626
SF
183 /*
184 * Catch if a low level driver returns -ENOSPC. This
185 * WARN_ON will be removed by 3.10 if no one reports
186 * seeing this.
187 */
188 WARN_ON_ONCE(rc == -ENOSPC);
1da177e4 189 i++;
6f49f46b 190 if (i >= 14 || (!server->noblocksnd && (i > 2))) {
f96637be
JP
191 cifs_dbg(VFS, "sends on sock %p stuck for 15 seconds\n",
192 ssocket);
1da177e4
LT
193 rc = -EAGAIN;
194 break;
195 }
68058e75 196 msleep(1 << i);
1da177e4
LT
197 continue;
198 }
6f49f46b 199
79a58d1f 200 if (rc < 0)
1da177e4 201 break;
3e84469d 202
6f49f46b
JL
203 /* send was at least partially successful */
204 *sent += rc;
205
206 if (rc == remaining) {
207 remaining = 0;
61de800d 208 break;
6f49f46b
JL
209 }
210
211 if (rc > remaining) {
f96637be 212 cifs_dbg(VFS, "sent %d requested %d\n", rc, remaining);
3e84469d
SF
213 break;
214 }
6f49f46b 215
79a58d1f 216 if (rc == 0) {
3e84469d
SF
217 /* should never happen, letting socket clear before
218 retrying is our only obvious option here */
f96637be 219 cifs_dbg(VFS, "tcp sent no data\n");
3e84469d
SF
220 msleep(500);
221 continue;
d6e04ae6 222 }
6f49f46b
JL
223
224 remaining -= rc;
225
68058e75 226 /* the line below resets i */
3e84469d
SF
227 for (i = first_vec; i < n_vec; i++) {
228 if (iov[i].iov_len) {
229 if (rc > iov[i].iov_len) {
230 rc -= iov[i].iov_len;
231 iov[i].iov_len = 0;
232 } else {
233 iov[i].iov_base += rc;
234 iov[i].iov_len -= rc;
235 first_vec = i;
236 break;
237 }
238 }
d6e04ae6 239 }
6f49f46b 240
5e1253b5 241 i = 0; /* in case we get ENOSPC on the next send */
6f49f46b 242 rc = 0;
1da177e4 243 }
6f49f46b
JL
244 return rc;
245}
246
97bc00b3
JL
247/**
248 * rqst_page_to_kvec - Turn a slot in the smb_rqst page array into a kvec
249 * @rqst: pointer to smb_rqst
250 * @idx: index into the array of the page
251 * @iov: pointer to struct kvec that will hold the result
252 *
253 * Helper function to convert a slot in the rqst->rq_pages array into a kvec.
254 * The page will be kmapped and the address placed into iov_base. The length
255 * will then be adjusted according to the ptailoff.
256 */
fb308a6f 257void
97bc00b3
JL
258cifs_rqst_page_to_kvec(struct smb_rqst *rqst, unsigned int idx,
259 struct kvec *iov)
260{
261 /*
262 * FIXME: We could avoid this kmap altogether if we used
263 * kernel_sendpage instead of kernel_sendmsg. That will only
264 * work if signing is disabled though as sendpage inlines the
265 * page directly into the fraglist. If userspace modifies the
266 * page after we calculate the signature, then the server will
267 * reject it and may break the connection. kernel_sendmsg does
268 * an extra copy of the data and avoids that issue.
269 */
270 iov->iov_base = kmap(rqst->rq_pages[idx]);
271
272 /* if last page, don't send beyond this offset into page */
273 if (idx == (rqst->rq_npages - 1))
274 iov->iov_len = rqst->rq_tailsz;
275 else
276 iov->iov_len = rqst->rq_pagesz;
277}
278
6f49f46b
JL
279static int
280smb_send_rqst(struct TCP_Server_Info *server, struct smb_rqst *rqst)
281{
282 int rc;
283 struct kvec *iov = rqst->rq_iov;
284 int n_vec = rqst->rq_nvec;
285 unsigned int smb_buf_length = get_rfc1002_length(iov[0].iov_base);
97bc00b3
JL
286 unsigned int i;
287 size_t total_len = 0, sent;
b8eed283
JL
288 struct socket *ssocket = server->ssocket;
289 int val = 1;
6f49f46b 290
ea702b80
JL
291 if (ssocket == NULL)
292 return -ENOTSOCK;
293
f96637be 294 cifs_dbg(FYI, "Sending smb: smb_len=%u\n", smb_buf_length);
6f49f46b
JL
295 dump_smb(iov[0].iov_base, iov[0].iov_len);
296
b8eed283
JL
297 /* cork the socket */
298 kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
299 (char *)&val, sizeof(val));
300
97bc00b3
JL
301 rc = smb_send_kvec(server, iov, n_vec, &sent);
302 if (rc < 0)
303 goto uncork;
304
305 total_len += sent;
306
307 /* now walk the page array and send each page in it */
308 for (i = 0; i < rqst->rq_npages; i++) {
309 struct kvec p_iov;
310
311 cifs_rqst_page_to_kvec(rqst, i, &p_iov);
312 rc = smb_send_kvec(server, &p_iov, 1, &sent);
313 kunmap(rqst->rq_pages[i]);
314 if (rc < 0)
315 break;
316
317 total_len += sent;
318 }
1da177e4 319
97bc00b3 320uncork:
b8eed283
JL
321 /* uncork it */
322 val = 0;
323 kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
324 (char *)&val, sizeof(val));
325
edf1ae40 326 if ((total_len > 0) && (total_len != smb_buf_length + 4)) {
f96637be
JP
327 cifs_dbg(FYI, "partial send (wanted=%u sent=%zu): terminating session\n",
328 smb_buf_length + 4, total_len);
6f49f46b
JL
329 /*
330 * If we have only sent part of an SMB then the next SMB could
331 * be taken as the remainder of this one. We need to kill the
332 * socket so the server throws away the partial SMB
333 */
edf1ae40
SF
334 server->tcpStatus = CifsNeedReconnect;
335 }
336
d804d41d 337 if (rc < 0 && rc != -EINTR)
f96637be
JP
338 cifs_dbg(VFS, "Error %d sending data on socket to server\n",
339 rc);
d804d41d 340 else
1da177e4 341 rc = 0;
1da177e4
LT
342
343 return rc;
344}
345
6f49f46b
JL
346static int
347smb_sendv(struct TCP_Server_Info *server, struct kvec *iov, int n_vec)
348{
349 struct smb_rqst rqst = { .rq_iov = iov,
350 .rq_nvec = n_vec };
351
352 return smb_send_rqst(server, &rqst);
353}
354
0496e02d
JL
355int
356smb_send(struct TCP_Server_Info *server, struct smb_hdr *smb_buffer,
357 unsigned int smb_buf_length)
358{
359 struct kvec iov;
360
361 iov.iov_base = smb_buffer;
362 iov.iov_len = smb_buf_length + 4;
363
364 return smb_sendv(server, &iov, 1);
365}
366
fc40f9cf 367static int
a891f0f8 368wait_for_free_credits(struct TCP_Server_Info *server, const int timeout,
bc205ed1 369 int *credits)
1da177e4 370{
5bc59498
PS
371 int rc;
372
fc40f9cf 373 spin_lock(&server->req_lock);
a891f0f8 374 if (timeout == CIFS_ASYNC_OP) {
1da177e4 375 /* oplock breaks must not be held up */
fc40f9cf 376 server->in_flight++;
bc205ed1 377 *credits -= 1;
fc40f9cf 378 spin_unlock(&server->req_lock);
27a97a61
VL
379 return 0;
380 }
381
27a97a61 382 while (1) {
bc205ed1 383 if (*credits <= 0) {
fc40f9cf 384 spin_unlock(&server->req_lock);
789e6661 385 cifs_num_waiters_inc(server);
5bc59498 386 rc = wait_event_killable(server->request_q,
bc205ed1 387 has_credits(server, credits));
789e6661 388 cifs_num_waiters_dec(server);
5bc59498
PS
389 if (rc)
390 return rc;
fc40f9cf 391 spin_lock(&server->req_lock);
27a97a61 392 } else {
c5797a94 393 if (server->tcpStatus == CifsExiting) {
fc40f9cf 394 spin_unlock(&server->req_lock);
27a97a61 395 return -ENOENT;
1da177e4 396 }
27a97a61 397
2d86dbc9
PS
398 /*
399 * Can not count locking commands against total
400 * as they are allowed to block on server.
401 */
27a97a61
VL
402
403 /* update # of requests on the wire to server */
a891f0f8 404 if (timeout != CIFS_BLOCKING_OP) {
bc205ed1 405 *credits -= 1;
fc40f9cf 406 server->in_flight++;
2d86dbc9 407 }
fc40f9cf 408 spin_unlock(&server->req_lock);
27a97a61 409 break;
1da177e4
LT
410 }
411 }
7ee1af76
JA
412 return 0;
413}
1da177e4 414
bc205ed1 415static int
a891f0f8
PS
416wait_for_free_request(struct TCP_Server_Info *server, const int timeout,
417 const int optype)
bc205ed1 418{
a891f0f8
PS
419 return wait_for_free_credits(server, timeout,
420 server->ops->get_credits_field(server, optype));
bc205ed1
PS
421}
422
96daf2b0 423static int allocate_mid(struct cifs_ses *ses, struct smb_hdr *in_buf,
7ee1af76
JA
424 struct mid_q_entry **ppmidQ)
425{
1da177e4 426 if (ses->server->tcpStatus == CifsExiting) {
7ee1af76 427 return -ENOENT;
8fbbd365
VL
428 }
429
430 if (ses->server->tcpStatus == CifsNeedReconnect) {
f96637be 431 cifs_dbg(FYI, "tcp session dead - return to caller to retry\n");
7ee1af76 432 return -EAGAIN;
8fbbd365
VL
433 }
434
435 if (ses->status != CifsGood) {
1da177e4 436 /* check if SMB session is bad because we are setting it up */
79a58d1f 437 if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
ad7a2926 438 (in_buf->Command != SMB_COM_NEGOTIATE))
7ee1af76 439 return -EAGAIN;
ad7a2926 440 /* else ok - we are setting up session */
1da177e4 441 }
24b9b06b 442 *ppmidQ = AllocMidQEntry(in_buf, ses->server);
26f57364 443 if (*ppmidQ == NULL)
7ee1af76 444 return -ENOMEM;
ddc8cf8f
JL
445 spin_lock(&GlobalMid_Lock);
446 list_add_tail(&(*ppmidQ)->qhead, &ses->server->pending_mid_q);
447 spin_unlock(&GlobalMid_Lock);
7ee1af76
JA
448 return 0;
449}
450
0ade640e
JL
451static int
452wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *midQ)
7ee1af76 453{
0ade640e 454 int error;
7ee1af76 455
f06ac72e 456 error = wait_event_freezekillable(server->response_q,
7c9421e1 457 midQ->mid_state != MID_REQUEST_SUBMITTED);
0ade640e
JL
458 if (error < 0)
459 return -ERESTARTSYS;
7ee1af76 460
0ade640e 461 return 0;
7ee1af76
JA
462}
463
fec344e3
JL
464struct mid_q_entry *
465cifs_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
792af7b0
PS
466{
467 int rc;
fec344e3 468 struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
792af7b0
PS
469 struct mid_q_entry *mid;
470
471 /* enable signing if server requires it */
472 if (server->sec_mode & (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
473 hdr->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
474
475 mid = AllocMidQEntry(hdr, server);
476 if (mid == NULL)
fec344e3 477 return ERR_PTR(-ENOMEM);
792af7b0 478
fec344e3 479 rc = cifs_sign_rqst(rqst, server, &mid->sequence_number);
ffc61ccb
SP
480 if (rc) {
481 DeleteMidQEntry(mid);
fec344e3 482 return ERR_PTR(rc);
ffc61ccb
SP
483 }
484
fec344e3 485 return mid;
792af7b0 486}
133672ef 487
a6827c18
JL
488/*
489 * Send a SMB request and set the callback function in the mid to handle
490 * the result. Caller is responsible for dealing with timeouts.
491 */
492int
fec344e3
JL
493cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst,
494 mid_receive_t *receive, mid_callback_t *callback,
495 void *cbdata, const int flags)
a6827c18 496{
a891f0f8 497 int rc, timeout, optype;
a6827c18
JL
498 struct mid_q_entry *mid;
499
a891f0f8
PS
500 timeout = flags & CIFS_TIMEOUT_MASK;
501 optype = flags & CIFS_OP_MASK;
502
503 rc = wait_for_free_request(server, timeout, optype);
a6827c18
JL
504 if (rc)
505 return rc;
506
507 mutex_lock(&server->srv_mutex);
fec344e3
JL
508 mid = server->ops->setup_async_request(server, rqst);
509 if (IS_ERR(mid)) {
a6827c18 510 mutex_unlock(&server->srv_mutex);
a891f0f8 511 add_credits(server, 1, optype);
0193e072 512 wake_up(&server->request_q);
fec344e3 513 return PTR_ERR(mid);
a6827c18
JL
514 }
515
44d22d84 516 mid->receive = receive;
a6827c18
JL
517 mid->callback = callback;
518 mid->callback_data = cbdata;
7c9421e1 519 mid->mid_state = MID_REQUEST_SUBMITTED;
789e6661 520
ffc61ccb
SP
521 /* put it on the pending_mid_q */
522 spin_lock(&GlobalMid_Lock);
523 list_add_tail(&mid->qhead, &server->pending_mid_q);
524 spin_unlock(&GlobalMid_Lock);
525
526
789e6661 527 cifs_in_send_inc(server);
fec344e3 528 rc = smb_send_rqst(server, rqst);
789e6661
SF
529 cifs_in_send_dec(server);
530 cifs_save_when_sent(mid);
a6827c18 531 mutex_unlock(&server->srv_mutex);
789e6661 532
ffc61ccb
SP
533 if (rc == 0)
534 return 0;
a6827c18 535
3c1bf7e4 536 cifs_delete_mid(mid);
a891f0f8 537 add_credits(server, 1, optype);
a6827c18
JL
538 wake_up(&server->request_q);
539 return rc;
540}
541
133672ef
SF
542/*
543 *
544 * Send an SMB Request. No response info (other than return code)
545 * needs to be parsed.
546 *
547 * flags indicate the type of request buffer and how long to wait
548 * and whether to log NT STATUS code (error) before mapping it to POSIX error
549 *
550 */
551int
96daf2b0 552SendReceiveNoRsp(const unsigned int xid, struct cifs_ses *ses,
792af7b0 553 char *in_buf, int flags)
133672ef
SF
554{
555 int rc;
556 struct kvec iov[1];
557 int resp_buf_type;
558
792af7b0
PS
559 iov[0].iov_base = in_buf;
560 iov[0].iov_len = get_rfc1002_length(in_buf) + 4;
133672ef
SF
561 flags |= CIFS_NO_RESP;
562 rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags);
f96637be 563 cifs_dbg(NOISY, "SendRcvNoRsp flags %d rc %d\n", flags, rc);
90c81e0b 564
133672ef
SF
565 return rc;
566}
567
053d5034 568static int
3c1105df 569cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
053d5034
JL
570{
571 int rc = 0;
572
f96637be
JP
573 cifs_dbg(FYI, "%s: cmd=%d mid=%llu state=%d\n",
574 __func__, le16_to_cpu(mid->command), mid->mid, mid->mid_state);
053d5034 575
74dd92a8 576 spin_lock(&GlobalMid_Lock);
7c9421e1 577 switch (mid->mid_state) {
74dd92a8 578 case MID_RESPONSE_RECEIVED:
053d5034
JL
579 spin_unlock(&GlobalMid_Lock);
580 return rc;
74dd92a8
JL
581 case MID_RETRY_NEEDED:
582 rc = -EAGAIN;
583 break;
71823baf
JL
584 case MID_RESPONSE_MALFORMED:
585 rc = -EIO;
586 break;
3c1105df
JL
587 case MID_SHUTDOWN:
588 rc = -EHOSTDOWN;
589 break;
74dd92a8 590 default:
3c1105df 591 list_del_init(&mid->qhead);
f96637be
JP
592 cifs_dbg(VFS, "%s: invalid mid state mid=%llu state=%d\n",
593 __func__, mid->mid, mid->mid_state);
74dd92a8 594 rc = -EIO;
053d5034
JL
595 }
596 spin_unlock(&GlobalMid_Lock);
597
2b84a36c 598 DeleteMidQEntry(mid);
053d5034
JL
599 return rc;
600}
601
121b046a
JL
602static inline int
603send_cancel(struct TCP_Server_Info *server, void *buf, struct mid_q_entry *mid)
76dcc26f 604{
121b046a
JL
605 return server->ops->send_cancel ?
606 server->ops->send_cancel(server, buf, mid) : 0;
76dcc26f
JL
607}
608
2c8f981d
JL
609int
610cifs_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
611 bool log_error)
612{
792af7b0 613 unsigned int len = get_rfc1002_length(mid->resp_buf) + 4;
826a95e4
JL
614
615 dump_smb(mid->resp_buf, min_t(u32, 92, len));
2c8f981d
JL
616
617 /* convert the length into a more usable form */
96daf2b0 618 if (server->sec_mode & (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED)) {
826a95e4 619 struct kvec iov;
985e4ff0 620 int rc = 0;
bf5ea0e2
JL
621 struct smb_rqst rqst = { .rq_iov = &iov,
622 .rq_nvec = 1 };
826a95e4
JL
623
624 iov.iov_base = mid->resp_buf;
625 iov.iov_len = len;
2c8f981d 626 /* FIXME: add code to kill session */
bf5ea0e2 627 rc = cifs_verify_signature(&rqst, server,
985e4ff0
SF
628 mid->sequence_number + 1);
629 if (rc)
f96637be
JP
630 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
631 rc);
2c8f981d
JL
632 }
633
634 /* BB special case reconnect tid and uid here? */
635 return map_smb_to_linux_error(mid->resp_buf, log_error);
636}
637
fec344e3
JL
638struct mid_q_entry *
639cifs_setup_request(struct cifs_ses *ses, struct smb_rqst *rqst)
792af7b0
PS
640{
641 int rc;
fec344e3 642 struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
792af7b0
PS
643 struct mid_q_entry *mid;
644
645 rc = allocate_mid(ses, hdr, &mid);
646 if (rc)
fec344e3
JL
647 return ERR_PTR(rc);
648 rc = cifs_sign_rqst(rqst, ses->server, &mid->sequence_number);
649 if (rc) {
3c1bf7e4 650 cifs_delete_mid(mid);
fec344e3
JL
651 return ERR_PTR(rc);
652 }
653 return mid;
792af7b0
PS
654}
655
7ee1af76 656int
96daf2b0 657SendReceive2(const unsigned int xid, struct cifs_ses *ses,
a891f0f8 658 struct kvec *iov, int n_vec, int *resp_buf_type /* ret */,
133672ef 659 const int flags)
7ee1af76
JA
660{
661 int rc = 0;
a891f0f8 662 int timeout, optype;
7ee1af76 663 struct mid_q_entry *midQ;
792af7b0 664 char *buf = iov[0].iov_base;
a891f0f8 665 unsigned int credits = 1;
fec344e3
JL
666 struct smb_rqst rqst = { .rq_iov = iov,
667 .rq_nvec = n_vec };
50c2f753 668
a891f0f8
PS
669 timeout = flags & CIFS_TIMEOUT_MASK;
670 optype = flags & CIFS_OP_MASK;
133672ef 671
a891f0f8 672 *resp_buf_type = CIFS_NO_BUFFER; /* no response buf yet */
7ee1af76
JA
673
674 if ((ses == NULL) || (ses->server == NULL)) {
792af7b0 675 cifs_small_buf_release(buf);
f96637be 676 cifs_dbg(VFS, "Null session\n");
7ee1af76
JA
677 return -EIO;
678 }
679
79a58d1f 680 if (ses->server->tcpStatus == CifsExiting) {
792af7b0 681 cifs_small_buf_release(buf);
7ee1af76
JA
682 return -ENOENT;
683 }
684
792af7b0
PS
685 /*
686 * Ensure that we do not send more than 50 overlapping requests
687 * to the same server. We may make this configurable later or
688 * use ses->maxReq.
689 */
7ee1af76 690
a891f0f8 691 rc = wait_for_free_request(ses->server, timeout, optype);
7ee1af76 692 if (rc) {
792af7b0 693 cifs_small_buf_release(buf);
7ee1af76
JA
694 return rc;
695 }
696
792af7b0
PS
697 /*
698 * Make sure that we sign in the same order that we send on this socket
699 * and avoid races inside tcp sendmsg code that could cause corruption
700 * of smb data.
701 */
7ee1af76 702
72ca545b 703 mutex_lock(&ses->server->srv_mutex);
7ee1af76 704
fec344e3
JL
705 midQ = ses->server->ops->setup_request(ses, &rqst);
706 if (IS_ERR(midQ)) {
72ca545b 707 mutex_unlock(&ses->server->srv_mutex);
792af7b0 708 cifs_small_buf_release(buf);
7ee1af76 709 /* Update # of requests on wire to server */
a891f0f8 710 add_credits(ses->server, 1, optype);
fec344e3 711 return PTR_ERR(midQ);
1da177e4 712 }
1da177e4 713
7c9421e1 714 midQ->mid_state = MID_REQUEST_SUBMITTED;
789e6661 715 cifs_in_send_inc(ses->server);
0496e02d 716 rc = smb_sendv(ses->server, iov, n_vec);
789e6661
SF
717 cifs_in_send_dec(ses->server);
718 cifs_save_when_sent(midQ);
7ee1af76 719
72ca545b 720 mutex_unlock(&ses->server->srv_mutex);
7ee1af76 721
2db7c581 722 if (rc < 0) {
792af7b0 723 cifs_small_buf_release(buf);
7ee1af76 724 goto out;
2db7c581 725 }
4b8f930f 726
a891f0f8 727 if (timeout == CIFS_ASYNC_OP) {
792af7b0 728 cifs_small_buf_release(buf);
133672ef 729 goto out;
2db7c581 730 }
d6e04ae6 731
0ade640e 732 rc = wait_for_response(ses->server, midQ);
1be912dd 733 if (rc != 0) {
121b046a 734 send_cancel(ses->server, buf, midQ);
1be912dd 735 spin_lock(&GlobalMid_Lock);
7c9421e1 736 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
1be912dd
JL
737 midQ->callback = DeleteMidQEntry;
738 spin_unlock(&GlobalMid_Lock);
792af7b0 739 cifs_small_buf_release(buf);
a891f0f8 740 add_credits(ses->server, 1, optype);
1be912dd
JL
741 return rc;
742 }
743 spin_unlock(&GlobalMid_Lock);
744 }
d6e04ae6 745
792af7b0 746 cifs_small_buf_release(buf);
2db7c581 747
3c1105df 748 rc = cifs_sync_mid_result(midQ, ses->server);
053d5034 749 if (rc != 0) {
a891f0f8 750 add_credits(ses->server, 1, optype);
d6e04ae6
SF
751 return rc;
752 }
50c2f753 753
7c9421e1 754 if (!midQ->resp_buf || midQ->mid_state != MID_RESPONSE_RECEIVED) {
d6e04ae6 755 rc = -EIO;
f96637be 756 cifs_dbg(FYI, "Bad MID state?\n");
2b2bdfba
SF
757 goto out;
758 }
759
792af7b0
PS
760 buf = (char *)midQ->resp_buf;
761 iov[0].iov_base = buf;
762 iov[0].iov_len = get_rfc1002_length(buf) + 4;
7c9421e1 763 if (midQ->large_buf)
a891f0f8 764 *resp_buf_type = CIFS_LARGE_BUFFER;
2c8f981d 765 else
a891f0f8
PS
766 *resp_buf_type = CIFS_SMALL_BUFFER;
767
768 credits = ses->server->ops->get_credits(midQ);
2b2bdfba 769
082d0642
PS
770 rc = ses->server->ops->check_receive(midQ, ses->server,
771 flags & CIFS_LOG_ERROR);
1da177e4 772
3c1bf7e4 773 /* mark it so buf will not be freed by cifs_delete_mid */
2c8f981d
JL
774 if ((flags & CIFS_NO_RESP) == 0)
775 midQ->resp_buf = NULL;
7ee1af76 776out:
3c1bf7e4 777 cifs_delete_mid(midQ);
a891f0f8 778 add_credits(ses->server, credits, optype);
1da177e4 779
d6e04ae6
SF
780 return rc;
781}
1da177e4
LT
782
783int
96daf2b0 784SendReceive(const unsigned int xid, struct cifs_ses *ses,
1da177e4 785 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
a891f0f8 786 int *pbytes_returned, const int timeout)
1da177e4
LT
787{
788 int rc = 0;
1da177e4
LT
789 struct mid_q_entry *midQ;
790
791 if (ses == NULL) {
f96637be 792 cifs_dbg(VFS, "Null smb session\n");
1da177e4
LT
793 return -EIO;
794 }
79a58d1f 795 if (ses->server == NULL) {
f96637be 796 cifs_dbg(VFS, "Null tcp session\n");
1da177e4
LT
797 return -EIO;
798 }
799
79a58d1f 800 if (ses->server->tcpStatus == CifsExiting)
31ca3bc3
SF
801 return -ENOENT;
802
79a58d1f 803 /* Ensure that we do not send more than 50 overlapping requests
1da177e4
LT
804 to the same server. We may make this configurable later or
805 use ses->maxReq */
1da177e4 806
be8e3b00
SF
807 if (be32_to_cpu(in_buf->smb_buf_length) > CIFSMaxBufSize +
808 MAX_CIFS_HDR_SIZE - 4) {
f96637be
JP
809 cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
810 be32_to_cpu(in_buf->smb_buf_length));
6d9c6d54
VL
811 return -EIO;
812 }
813
a891f0f8 814 rc = wait_for_free_request(ses->server, timeout, 0);
7ee1af76
JA
815 if (rc)
816 return rc;
817
79a58d1f 818 /* make sure that we sign in the same order that we send on this socket
1da177e4
LT
819 and avoid races inside tcp sendmsg code that could cause corruption
820 of smb data */
821
72ca545b 822 mutex_lock(&ses->server->srv_mutex);
1da177e4 823
7ee1af76
JA
824 rc = allocate_mid(ses, in_buf, &midQ);
825 if (rc) {
72ca545b 826 mutex_unlock(&ses->server->srv_mutex);
7ee1af76 827 /* Update # of requests on wire to server */
a891f0f8 828 add_credits(ses->server, 1, 0);
7ee1af76 829 return rc;
1da177e4
LT
830 }
831
ad009ac9 832 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
829049cb
VL
833 if (rc) {
834 mutex_unlock(&ses->server->srv_mutex);
835 goto out;
836 }
1da177e4 837
7c9421e1 838 midQ->mid_state = MID_REQUEST_SUBMITTED;
789e6661
SF
839
840 cifs_in_send_inc(ses->server);
be8e3b00 841 rc = smb_send(ses->server, in_buf, be32_to_cpu(in_buf->smb_buf_length));
789e6661
SF
842 cifs_in_send_dec(ses->server);
843 cifs_save_when_sent(midQ);
72ca545b 844 mutex_unlock(&ses->server->srv_mutex);
7ee1af76 845
79a58d1f 846 if (rc < 0)
7ee1af76
JA
847 goto out;
848
a891f0f8 849 if (timeout == CIFS_ASYNC_OP)
7ee1af76 850 goto out;
1da177e4 851
0ade640e 852 rc = wait_for_response(ses->server, midQ);
1be912dd 853 if (rc != 0) {
121b046a 854 send_cancel(ses->server, in_buf, midQ);
1be912dd 855 spin_lock(&GlobalMid_Lock);
7c9421e1 856 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
1be912dd
JL
857 /* no longer considered to be "in-flight" */
858 midQ->callback = DeleteMidQEntry;
859 spin_unlock(&GlobalMid_Lock);
a891f0f8 860 add_credits(ses->server, 1, 0);
1be912dd
JL
861 return rc;
862 }
863 spin_unlock(&GlobalMid_Lock);
864 }
1da177e4 865
3c1105df 866 rc = cifs_sync_mid_result(midQ, ses->server);
053d5034 867 if (rc != 0) {
a891f0f8 868 add_credits(ses->server, 1, 0);
1da177e4
LT
869 return rc;
870 }
50c2f753 871
2c8f981d 872 if (!midQ->resp_buf || !out_buf ||
7c9421e1 873 midQ->mid_state != MID_RESPONSE_RECEIVED) {
2b2bdfba 874 rc = -EIO;
f96637be 875 cifs_dbg(VFS, "Bad MID state?\n");
2c8f981d 876 goto out;
1da177e4 877 }
7ee1af76 878
d4e4854f 879 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
2c8f981d
JL
880 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
881 rc = cifs_check_receive(midQ, ses->server, 0);
7ee1af76 882out:
3c1bf7e4 883 cifs_delete_mid(midQ);
a891f0f8 884 add_credits(ses->server, 1, 0);
1da177e4 885
7ee1af76
JA
886 return rc;
887}
1da177e4 888
7ee1af76
JA
889/* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
890 blocking lock to return. */
891
892static int
96daf2b0 893send_lock_cancel(const unsigned int xid, struct cifs_tcon *tcon,
7ee1af76
JA
894 struct smb_hdr *in_buf,
895 struct smb_hdr *out_buf)
896{
897 int bytes_returned;
96daf2b0 898 struct cifs_ses *ses = tcon->ses;
7ee1af76
JA
899 LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
900
901 /* We just modify the current in_buf to change
902 the type of lock from LOCKING_ANDX_SHARED_LOCK
903 or LOCKING_ANDX_EXCLUSIVE_LOCK to
904 LOCKING_ANDX_CANCEL_LOCK. */
905
906 pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
907 pSMB->Timeout = 0;
88257360 908 pSMB->hdr.Mid = get_next_mid(ses->server);
7ee1af76
JA
909
910 return SendReceive(xid, ses, in_buf, out_buf,
7749981e 911 &bytes_returned, 0);
7ee1af76
JA
912}
913
914int
96daf2b0 915SendReceiveBlockingLock(const unsigned int xid, struct cifs_tcon *tcon,
7ee1af76
JA
916 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
917 int *pbytes_returned)
918{
919 int rc = 0;
920 int rstart = 0;
7ee1af76 921 struct mid_q_entry *midQ;
96daf2b0 922 struct cifs_ses *ses;
7ee1af76
JA
923
924 if (tcon == NULL || tcon->ses == NULL) {
f96637be 925 cifs_dbg(VFS, "Null smb session\n");
7ee1af76
JA
926 return -EIO;
927 }
928 ses = tcon->ses;
929
79a58d1f 930 if (ses->server == NULL) {
f96637be 931 cifs_dbg(VFS, "Null tcp session\n");
7ee1af76
JA
932 return -EIO;
933 }
934
79a58d1f 935 if (ses->server->tcpStatus == CifsExiting)
7ee1af76
JA
936 return -ENOENT;
937
79a58d1f 938 /* Ensure that we do not send more than 50 overlapping requests
7ee1af76
JA
939 to the same server. We may make this configurable later or
940 use ses->maxReq */
941
be8e3b00
SF
942 if (be32_to_cpu(in_buf->smb_buf_length) > CIFSMaxBufSize +
943 MAX_CIFS_HDR_SIZE - 4) {
f96637be
JP
944 cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
945 be32_to_cpu(in_buf->smb_buf_length));
6d9c6d54
VL
946 return -EIO;
947 }
948
a891f0f8 949 rc = wait_for_free_request(ses->server, CIFS_BLOCKING_OP, 0);
7ee1af76
JA
950 if (rc)
951 return rc;
952
79a58d1f 953 /* make sure that we sign in the same order that we send on this socket
7ee1af76
JA
954 and avoid races inside tcp sendmsg code that could cause corruption
955 of smb data */
956
72ca545b 957 mutex_lock(&ses->server->srv_mutex);
7ee1af76
JA
958
959 rc = allocate_mid(ses, in_buf, &midQ);
960 if (rc) {
72ca545b 961 mutex_unlock(&ses->server->srv_mutex);
7ee1af76
JA
962 return rc;
963 }
964
7ee1af76 965 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
829049cb 966 if (rc) {
3c1bf7e4 967 cifs_delete_mid(midQ);
829049cb
VL
968 mutex_unlock(&ses->server->srv_mutex);
969 return rc;
970 }
1da177e4 971
7c9421e1 972 midQ->mid_state = MID_REQUEST_SUBMITTED;
789e6661 973 cifs_in_send_inc(ses->server);
be8e3b00 974 rc = smb_send(ses->server, in_buf, be32_to_cpu(in_buf->smb_buf_length));
789e6661
SF
975 cifs_in_send_dec(ses->server);
976 cifs_save_when_sent(midQ);
72ca545b 977 mutex_unlock(&ses->server->srv_mutex);
7ee1af76 978
79a58d1f 979 if (rc < 0) {
3c1bf7e4 980 cifs_delete_mid(midQ);
7ee1af76
JA
981 return rc;
982 }
983
984 /* Wait for a reply - allow signals to interrupt. */
985 rc = wait_event_interruptible(ses->server->response_q,
7c9421e1 986 (!(midQ->mid_state == MID_REQUEST_SUBMITTED)) ||
7ee1af76
JA
987 ((ses->server->tcpStatus != CifsGood) &&
988 (ses->server->tcpStatus != CifsNew)));
989
990 /* Were we interrupted by a signal ? */
991 if ((rc == -ERESTARTSYS) &&
7c9421e1 992 (midQ->mid_state == MID_REQUEST_SUBMITTED) &&
7ee1af76
JA
993 ((ses->server->tcpStatus == CifsGood) ||
994 (ses->server->tcpStatus == CifsNew))) {
995
996 if (in_buf->Command == SMB_COM_TRANSACTION2) {
997 /* POSIX lock. We send a NT_CANCEL SMB to cause the
998 blocking lock to return. */
121b046a 999 rc = send_cancel(ses->server, in_buf, midQ);
7ee1af76 1000 if (rc) {
3c1bf7e4 1001 cifs_delete_mid(midQ);
7ee1af76
JA
1002 return rc;
1003 }
1004 } else {
1005 /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
1006 to cause the blocking lock to return. */
1007
1008 rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
1009
1010 /* If we get -ENOLCK back the lock may have
1011 already been removed. Don't exit in this case. */
1012 if (rc && rc != -ENOLCK) {
3c1bf7e4 1013 cifs_delete_mid(midQ);
7ee1af76
JA
1014 return rc;
1015 }
1016 }
1017
1be912dd
JL
1018 rc = wait_for_response(ses->server, midQ);
1019 if (rc) {
121b046a 1020 send_cancel(ses->server, in_buf, midQ);
1be912dd 1021 spin_lock(&GlobalMid_Lock);
7c9421e1 1022 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
1be912dd
JL
1023 /* no longer considered to be "in-flight" */
1024 midQ->callback = DeleteMidQEntry;
1025 spin_unlock(&GlobalMid_Lock);
1026 return rc;
1027 }
1028 spin_unlock(&GlobalMid_Lock);
7ee1af76 1029 }
1be912dd
JL
1030
1031 /* We got the response - restart system call. */
1032 rstart = 1;
7ee1af76
JA
1033 }
1034
3c1105df 1035 rc = cifs_sync_mid_result(midQ, ses->server);
053d5034 1036 if (rc != 0)
7ee1af76 1037 return rc;
50c2f753 1038
17c8bfed 1039 /* rcvd frame is ok */
7c9421e1 1040 if (out_buf == NULL || midQ->mid_state != MID_RESPONSE_RECEIVED) {
698e96a8 1041 rc = -EIO;
f96637be 1042 cifs_dbg(VFS, "Bad MID state?\n");
698e96a8
VL
1043 goto out;
1044 }
1da177e4 1045
d4e4854f 1046 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
2c8f981d
JL
1047 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
1048 rc = cifs_check_receive(midQ, ses->server, 0);
17c8bfed 1049out:
3c1bf7e4 1050 cifs_delete_mid(midQ);
7ee1af76
JA
1051 if (rstart && rc == -EACCES)
1052 return -ERESTARTSYS;
1da177e4
LT
1053 return rc;
1054}