blob: 2126ab185045a1314a4f16d93e948fff847a7e5d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/transport.c
3 *
Steve Frenchad7a2922008-02-07 23:25:02 +00004 * Copyright (C) International Business Machines Corp., 2002,2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Author(s): Steve French (sfrench@us.ibm.com)
Steve French14a441a2b2006-07-16 04:32:51 +00006 * Jeremy Allison (jra@samba.org) 2006.
Steve French79a58d12007-07-06 22:44:50 +00007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * 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
Steve French79a58d12007-07-06 22:44:50 +000020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 */
22
23#include <linux/fs.h>
24#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/wait.h>
27#include <linux/net.h>
28#include <linux/delay.h>
Jeff Laytonf06ac722011-10-19 15:30:40 -040029#include <linux/freezer.h>
Jeff Laytonb8eed282012-09-18 16:20:35 -070030#include <linux/tcp.h>
Jeff Layton97bc00b2012-09-18 16:20:35 -070031#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#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"
Steve French50c2f752007-07-13 00:33:32 +000039
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +040040void
41cifs_wake_up_task(struct mid_q_entry *mid)
Jeff Layton2b84a36c2011-01-11 07:24:21 -050042{
43 wake_up_process(mid->callback_data);
44}
45
Jeff Laytona6827c12011-01-11 07:24:21 -050046struct mid_q_entry *
Jeff Layton24b9b062008-12-01 07:09:34 -050047AllocMidQEntry(const struct smb_hdr *smb_buffer, struct TCP_Server_Info *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 struct mid_q_entry *temp;
50
Jeff Layton24b9b062008-12-01 07:09:34 -050051 if (server == NULL) {
Joe Perchesb6b38f72010-04-21 03:50:45 +000052 cERROR(1, "Null TCP session in AllocMidQEntry");
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 return NULL;
54 }
Steve French50c2f752007-07-13 00:33:32 +000055
Pekka Enberg232087c2008-09-15 13:22:54 +030056 temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 if (temp == NULL)
58 return temp;
59 else {
Steve French26f57362007-08-30 22:09:15 +000060 memset(temp, 0, sizeof(struct mid_q_entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 temp->mid = smb_buffer->Mid; /* always LE */
62 temp->pid = current->pid;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -040063 temp->command = cpu_to_le16(smb_buffer->Command);
64 cFYI(1, "For smb_command %d", smb_buffer->Command);
Steve French1047abc2005-10-11 19:58:06 -070065 /* do_gettimeofday(&temp->when_sent);*/ /* easier to use jiffies */
66 /* when mid allocated can be before when sent */
67 temp->when_alloc = jiffies;
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +040068 temp->server = server;
Jeff Layton2b84a36c2011-01-11 07:24:21 -050069
70 /*
71 * The default is for the mid to be synchronous, so the
72 * default callback just wakes up the current task.
73 */
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +040074 temp->callback = cifs_wake_up_task;
Jeff Layton2b84a36c2011-01-11 07:24:21 -050075 temp->callback_data = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 }
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 atomic_inc(&midCount);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -040079 temp->mid_state = MID_REQUEST_ALLOCATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return temp;
81}
82
Jeff Layton766fdbb2011-01-11 07:24:21 -050083void
Linus Torvalds1da177e2005-04-16 15:20:36 -070084DeleteMidQEntry(struct mid_q_entry *midEntry)
85{
Steve French1047abc2005-10-11 19:58:06 -070086#ifdef CONFIG_CIFS_STATS2
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +040087 __le16 command = midEntry->server->vals->lock_cmd;
Steve French1047abc2005-10-11 19:58:06 -070088 unsigned long now;
89#endif
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -040090 midEntry->mid_state = MID_FREE;
Jeff Layton80975312011-01-11 07:24:02 -050091 atomic_dec(&midCount);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -040092 if (midEntry->large_buf)
Steve Frenchb8643e12005-04-28 22:41:07 -070093 cifs_buf_release(midEntry->resp_buf);
94 else
95 cifs_small_buf_release(midEntry->resp_buf);
Steve French1047abc2005-10-11 19:58:06 -070096#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 */
Steve French79a58d12007-07-06 22:44:50 +0000100 if ((now - midEntry->when_alloc) > HZ) {
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400101 if ((cifsFYI & CIFS_TIMER) && (midEntry->command != command)) {
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400102 printk(KERN_DEBUG " CIFS slow rsp: cmd %d mid %llu",
Steve French1047abc2005-10-11 19:58:06 -0700103 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 mempool_free(midEntry, cifs_mid_poolp);
112}
113
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700114void
115cifs_delete_mid(struct mid_q_entry *mid)
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500116{
117 spin_lock(&GlobalMid_Lock);
118 list_del(&mid->qhead);
119 spin_unlock(&GlobalMid_Lock);
120
121 DeleteMidQEntry(mid);
122}
123
Jeff Layton6f49f462012-09-18 16:20:34 -0700124/*
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 */
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500134static int
Jeff Layton6f49f462012-09-18 16:20:34 -0700135smb_send_kvec(struct TCP_Server_Info *server, struct kvec *iov, size_t n_vec,
136 size_t *sent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 int rc = 0;
139 int i = 0;
140 struct msghdr smb_msg;
Jeff Layton6f49f462012-09-18 16:20:34 -0700141 unsigned int remaining;
142 size_t first_vec = 0;
Steve Frenchedf1ae42008-10-29 00:47:57 +0000143 struct socket *ssocket = server->ssocket;
Steve French50c2f752007-07-13 00:33:32 +0000144
Jeff Layton6f49f462012-09-18 16:20:34 -0700145 *sent = 0;
146
Steve French79a58d12007-07-06 22:44:50 +0000147 if (ssocket == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return -ENOTSOCK; /* BB eventually add reconnect code here */
Steve French3e844692005-10-03 13:37:24 -0700149
Pavel Shilovskya9f1b852010-12-13 19:08:35 +0300150 smb_msg.msg_name = (struct sockaddr *) &server->dstaddr;
Steve French26f57362007-08-30 22:09:15 +0000151 smb_msg.msg_namelen = sizeof(struct sockaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 smb_msg.msg_control = NULL;
153 smb_msg.msg_controllen = 0;
Jeff Layton0496e022008-12-30 12:39:16 -0500154 if (server->noblocksnd)
Steve Frenchedf1ae42008-10-29 00:47:57 +0000155 smb_msg.msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL;
156 else
157 smb_msg.msg_flags = MSG_NOSIGNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Jeff Layton6f49f462012-09-18 16:20:34 -0700159 remaining = 0;
Steve French3e844692005-10-03 13:37:24 -0700160 for (i = 0; i < n_vec; i++)
Jeff Layton6f49f462012-09-18 16:20:34 -0700161 remaining += iov[i].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Shirish Pargaonkar17680352008-07-29 21:26:13 +0000163 i = 0;
Jeff Layton6f49f462012-09-18 16:20:34 -0700164 while (remaining) {
165 /*
166 * If blocking send, we try 3 times, since each can block
167 * for 5 seconds. For nonblocking we have to try more
168 * but wait increasing amounts of time allowing time for
169 * socket to clear. The overall time we wait in either
170 * case to send on the socket is about 15 seconds.
171 * Similarly we wait for 15 seconds for a response from
172 * the server in SendReceive[2] for the server to send
173 * a response back for most types of requests (except
174 * SMB Write past end of file which can be slow, and
175 * blocking lock operations). NFS waits slightly longer
176 * than CIFS, but this can make it take longer for
177 * nonresponsive servers to be detected and 15 seconds
178 * is more than enough time for modern networks to
179 * send a packet. In most cases if we fail to send
180 * after the retries we will kill the socket and
181 * reconnect which may clear the network problem.
182 */
Steve French3e844692005-10-03 13:37:24 -0700183 rc = kernel_sendmsg(ssocket, &smb_msg, &iov[first_vec],
Jeff Layton6f49f462012-09-18 16:20:34 -0700184 n_vec - first_vec, remaining);
185 if (rc == -ENOSPC || rc == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 i++;
Jeff Layton6f49f462012-09-18 16:20:34 -0700187 if (i >= 14 || (!server->noblocksnd && (i > 2))) {
188 cERROR(1, "sends on sock %p stuck for 15 "
189 "seconds", ssocket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 rc = -EAGAIN;
191 break;
192 }
Steve French68058e72005-10-10 10:34:22 -0700193 msleep(1 << i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 continue;
195 }
Jeff Layton6f49f462012-09-18 16:20:34 -0700196
Steve French79a58d12007-07-06 22:44:50 +0000197 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 break;
Steve French3e844692005-10-03 13:37:24 -0700199
Jeff Layton6f49f462012-09-18 16:20:34 -0700200 /* send was at least partially successful */
201 *sent += rc;
202
203 if (rc == remaining) {
204 remaining = 0;
Steve French3e844692005-10-03 13:37:24 -0700205 break;
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500206 }
Jeff Layton6f49f462012-09-18 16:20:34 -0700207
208 if (rc > remaining) {
209 cERROR(1, "sent %d requested %d", rc, remaining);
210 break;
211 }
212
Steve French79a58d12007-07-06 22:44:50 +0000213 if (rc == 0) {
Steve French3e844692005-10-03 13:37:24 -0700214 /* should never happen, letting socket clear before
215 retrying is our only obvious option here */
Joe Perchesb6b38f72010-04-21 03:50:45 +0000216 cERROR(1, "tcp sent no data");
Steve French3e844692005-10-03 13:37:24 -0700217 msleep(500);
218 continue;
219 }
Jeff Layton6f49f462012-09-18 16:20:34 -0700220
221 remaining -= rc;
222
Steve French68058e72005-10-10 10:34:22 -0700223 /* the line below resets i */
Steve French3e844692005-10-03 13:37:24 -0700224 for (i = first_vec; i < n_vec; i++) {
225 if (iov[i].iov_len) {
226 if (rc > iov[i].iov_len) {
227 rc -= iov[i].iov_len;
228 iov[i].iov_len = 0;
229 } else {
230 iov[i].iov_base += rc;
231 iov[i].iov_len -= rc;
232 first_vec = i;
233 break;
234 }
235 }
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500236 }
Jeff Layton6f49f462012-09-18 16:20:34 -0700237
Steve French5e1253b2005-10-10 14:06:37 -0700238 i = 0; /* in case we get ENOSPC on the next send */
Jeff Layton6f49f462012-09-18 16:20:34 -0700239 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
Jeff Layton6f49f462012-09-18 16:20:34 -0700241 return rc;
242}
243
Jeff Layton97bc00b2012-09-18 16:20:35 -0700244/**
245 * rqst_page_to_kvec - Turn a slot in the smb_rqst page array into a kvec
246 * @rqst: pointer to smb_rqst
247 * @idx: index into the array of the page
248 * @iov: pointer to struct kvec that will hold the result
249 *
250 * Helper function to convert a slot in the rqst->rq_pages array into a kvec.
251 * The page will be kmapped and the address placed into iov_base. The length
252 * will then be adjusted according to the ptailoff.
253 */
Jeff Laytonfb308a62012-09-18 16:20:35 -0700254void
Jeff Layton97bc00b2012-09-18 16:20:35 -0700255cifs_rqst_page_to_kvec(struct smb_rqst *rqst, unsigned int idx,
256 struct kvec *iov)
257{
258 /*
259 * FIXME: We could avoid this kmap altogether if we used
260 * kernel_sendpage instead of kernel_sendmsg. That will only
261 * work if signing is disabled though as sendpage inlines the
262 * page directly into the fraglist. If userspace modifies the
263 * page after we calculate the signature, then the server will
264 * reject it and may break the connection. kernel_sendmsg does
265 * an extra copy of the data and avoids that issue.
266 */
267 iov->iov_base = kmap(rqst->rq_pages[idx]);
268
269 /* if last page, don't send beyond this offset into page */
270 if (idx == (rqst->rq_npages - 1))
271 iov->iov_len = rqst->rq_tailsz;
272 else
273 iov->iov_len = rqst->rq_pagesz;
274}
275
Jeff Layton6f49f462012-09-18 16:20:34 -0700276static int
277smb_send_rqst(struct TCP_Server_Info *server, struct smb_rqst *rqst)
278{
279 int rc;
280 struct kvec *iov = rqst->rq_iov;
281 int n_vec = rqst->rq_nvec;
282 unsigned int smb_buf_length = get_rfc1002_length(iov[0].iov_base);
Jeff Layton97bc00b2012-09-18 16:20:35 -0700283 unsigned int i;
284 size_t total_len = 0, sent;
Jeff Laytonb8eed282012-09-18 16:20:35 -0700285 struct socket *ssocket = server->ssocket;
286 int val = 1;
Jeff Layton6f49f462012-09-18 16:20:34 -0700287
288 cFYI(1, "Sending smb: smb_len=%u", smb_buf_length);
289 dump_smb(iov[0].iov_base, iov[0].iov_len);
290
Jeff Laytonb8eed282012-09-18 16:20:35 -0700291 /* cork the socket */
292 kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
293 (char *)&val, sizeof(val));
294
Jeff Layton97bc00b2012-09-18 16:20:35 -0700295 rc = smb_send_kvec(server, iov, n_vec, &sent);
296 if (rc < 0)
297 goto uncork;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Jeff Layton97bc00b2012-09-18 16:20:35 -0700299 total_len += sent;
300
301 /* now walk the page array and send each page in it */
302 for (i = 0; i < rqst->rq_npages; i++) {
303 struct kvec p_iov;
304
305 cifs_rqst_page_to_kvec(rqst, i, &p_iov);
306 rc = smb_send_kvec(server, &p_iov, 1, &sent);
307 kunmap(rqst->rq_pages[i]);
308 if (rc < 0)
309 break;
310
311 total_len += sent;
312 }
313
314uncork:
Jeff Laytonb8eed282012-09-18 16:20:35 -0700315 /* uncork it */
316 val = 0;
317 kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
318 (char *)&val, sizeof(val));
319
Steve Frenchedf1ae42008-10-29 00:47:57 +0000320 if ((total_len > 0) && (total_len != smb_buf_length + 4)) {
Jeff Layton6f49f462012-09-18 16:20:34 -0700321 cFYI(1, "partial send (wanted=%u sent=%zu): terminating "
322 "session", smb_buf_length + 4, total_len);
323 /*
324 * If we have only sent part of an SMB then the next SMB could
325 * be taken as the remainder of this one. We need to kill the
326 * socket so the server throws away the partial SMB
327 */
Steve Frenchedf1ae42008-10-29 00:47:57 +0000328 server->tcpStatus = CifsNeedReconnect;
329 }
330
Jeff Laytond804d412011-01-28 15:05:43 -0500331 if (rc < 0 && rc != -EINTR)
Joe Perchesb6b38f72010-04-21 03:50:45 +0000332 cERROR(1, "Error %d sending data on socket to server", rc);
Jeff Laytond804d412011-01-28 15:05:43 -0500333 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 return rc;
337}
338
Jeff Layton6f49f462012-09-18 16:20:34 -0700339static int
340smb_sendv(struct TCP_Server_Info *server, struct kvec *iov, int n_vec)
341{
342 struct smb_rqst rqst = { .rq_iov = iov,
343 .rq_nvec = n_vec };
344
345 return smb_send_rqst(server, &rqst);
346}
347
Jeff Layton0496e022008-12-30 12:39:16 -0500348int
349smb_send(struct TCP_Server_Info *server, struct smb_hdr *smb_buffer,
350 unsigned int smb_buf_length)
351{
352 struct kvec iov;
353
354 iov.iov_base = smb_buffer;
355 iov.iov_len = smb_buf_length + 4;
356
357 return smb_sendv(server, &iov, 1);
358}
359
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300360static int
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400361wait_for_free_credits(struct TCP_Server_Info *server, const int timeout,
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300362 int *credits)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000363{
Pavel Shilovsky5bc59492012-02-21 19:56:08 +0300364 int rc;
365
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300366 spin_lock(&server->req_lock);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400367 if (timeout == CIFS_ASYNC_OP) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000368 /* oplock breaks must not be held up */
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300369 server->in_flight++;
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300370 *credits -= 1;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300371 spin_unlock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000372 return 0;
373 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000374
Volker Lendecke27a97a62008-12-08 20:59:39 +0000375 while (1) {
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300376 if (*credits <= 0) {
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300377 spin_unlock(&server->req_lock);
Steve French789e6662011-08-09 18:44:44 +0000378 cifs_num_waiters_inc(server);
Pavel Shilovsky5bc59492012-02-21 19:56:08 +0300379 rc = wait_event_killable(server->request_q,
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300380 has_credits(server, credits));
Steve French789e6662011-08-09 18:44:44 +0000381 cifs_num_waiters_dec(server);
Pavel Shilovsky5bc59492012-02-21 19:56:08 +0300382 if (rc)
383 return rc;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300384 spin_lock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000385 } else {
Jeff Laytonc5797a92011-01-11 07:24:01 -0500386 if (server->tcpStatus == CifsExiting) {
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300387 spin_unlock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000388 return -ENOENT;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000389 }
Volker Lendecke27a97a62008-12-08 20:59:39 +0000390
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400391 /*
392 * Can not count locking commands against total
393 * as they are allowed to block on server.
394 */
Volker Lendecke27a97a62008-12-08 20:59:39 +0000395
396 /* update # of requests on the wire to server */
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400397 if (timeout != CIFS_BLOCKING_OP) {
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300398 *credits -= 1;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300399 server->in_flight++;
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400400 }
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300401 spin_unlock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000402 break;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000403 }
404 }
405 return 0;
406}
407
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300408static int
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400409wait_for_free_request(struct TCP_Server_Info *server, const int timeout,
410 const int optype)
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300411{
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400412 return wait_for_free_credits(server, timeout,
413 server->ops->get_credits_field(server, optype));
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300414}
415
Steve French96daf2b2011-05-27 04:34:02 +0000416static int allocate_mid(struct cifs_ses *ses, struct smb_hdr *in_buf,
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000417 struct mid_q_entry **ppmidQ)
418{
419 if (ses->server->tcpStatus == CifsExiting) {
420 return -ENOENT;
Volker Lendecke8fbbd362008-12-06 13:12:34 +0100421 }
422
423 if (ses->server->tcpStatus == CifsNeedReconnect) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000424 cFYI(1, "tcp session dead - return to caller to retry");
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000425 return -EAGAIN;
Volker Lendecke8fbbd362008-12-06 13:12:34 +0100426 }
427
428 if (ses->status != CifsGood) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000429 /* check if SMB session is bad because we are setting it up */
Steve French79a58d12007-07-06 22:44:50 +0000430 if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
Steve Frenchad7a2922008-02-07 23:25:02 +0000431 (in_buf->Command != SMB_COM_NEGOTIATE))
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000432 return -EAGAIN;
Steve Frenchad7a2922008-02-07 23:25:02 +0000433 /* else ok - we are setting up session */
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000434 }
Jeff Layton24b9b062008-12-01 07:09:34 -0500435 *ppmidQ = AllocMidQEntry(in_buf, ses->server);
Steve French26f57362007-08-30 22:09:15 +0000436 if (*ppmidQ == NULL)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000437 return -ENOMEM;
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500438 spin_lock(&GlobalMid_Lock);
439 list_add_tail(&(*ppmidQ)->qhead, &ses->server->pending_mid_q);
440 spin_unlock(&GlobalMid_Lock);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000441 return 0;
442}
443
Jeff Layton0ade6402011-01-11 07:24:02 -0500444static int
445wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *midQ)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000446{
Jeff Layton0ade6402011-01-11 07:24:02 -0500447 int error;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000448
Jeff Laytonf06ac722011-10-19 15:30:40 -0400449 error = wait_event_freezekillable(server->response_q,
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400450 midQ->mid_state != MID_REQUEST_SUBMITTED);
Jeff Layton0ade6402011-01-11 07:24:02 -0500451 if (error < 0)
452 return -ERESTARTSYS;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000453
Jeff Layton0ade6402011-01-11 07:24:02 -0500454 return 0;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000455}
456
Jeff Laytonfec344e2012-09-18 16:20:35 -0700457struct mid_q_entry *
458cifs_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400459{
460 int rc;
Jeff Laytonfec344e2012-09-18 16:20:35 -0700461 struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400462 struct mid_q_entry *mid;
463
464 /* enable signing if server requires it */
465 if (server->sec_mode & (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
466 hdr->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
467
468 mid = AllocMidQEntry(hdr, server);
469 if (mid == NULL)
Jeff Laytonfec344e2012-09-18 16:20:35 -0700470 return ERR_PTR(-ENOMEM);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400471
Jeff Laytonfec344e2012-09-18 16:20:35 -0700472 rc = cifs_sign_rqst(rqst, server, &mid->sequence_number);
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100473 if (rc) {
474 DeleteMidQEntry(mid);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700475 return ERR_PTR(rc);
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100476 }
477
Jeff Laytonfec344e2012-09-18 16:20:35 -0700478 return mid;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400479}
Steve French133672e2007-11-13 22:41:37 +0000480
481/*
Jeff Laytona6827c12011-01-11 07:24:21 -0500482 * Send a SMB request and set the callback function in the mid to handle
483 * the result. Caller is responsible for dealing with timeouts.
484 */
485int
Jeff Laytonfec344e2012-09-18 16:20:35 -0700486cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst,
487 mid_receive_t *receive, mid_callback_t *callback,
488 void *cbdata, const int flags)
Jeff Laytona6827c12011-01-11 07:24:21 -0500489{
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400490 int rc, timeout, optype;
Jeff Laytona6827c12011-01-11 07:24:21 -0500491 struct mid_q_entry *mid;
492
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400493 timeout = flags & CIFS_TIMEOUT_MASK;
494 optype = flags & CIFS_OP_MASK;
495
496 rc = wait_for_free_request(server, timeout, optype);
Jeff Laytona6827c12011-01-11 07:24:21 -0500497 if (rc)
498 return rc;
499
500 mutex_lock(&server->srv_mutex);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700501 mid = server->ops->setup_async_request(server, rqst);
502 if (IS_ERR(mid)) {
Jeff Laytona6827c12011-01-11 07:24:21 -0500503 mutex_unlock(&server->srv_mutex);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400504 add_credits(server, 1, optype);
Pavel Shilovsky0193e072011-08-03 23:12:18 +0400505 wake_up(&server->request_q);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700506 return PTR_ERR(mid);
Jeff Laytona6827c12011-01-11 07:24:21 -0500507 }
508
Jeff Layton44d22d82011-10-19 15:29:49 -0400509 mid->receive = receive;
Jeff Laytona6827c12011-01-11 07:24:21 -0500510 mid->callback = callback;
511 mid->callback_data = cbdata;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400512 mid->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +0000513
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100514 /* put it on the pending_mid_q */
515 spin_lock(&GlobalMid_Lock);
516 list_add_tail(&mid->qhead, &server->pending_mid_q);
517 spin_unlock(&GlobalMid_Lock);
518
519
Steve French789e6662011-08-09 18:44:44 +0000520 cifs_in_send_inc(server);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700521 rc = smb_send_rqst(server, rqst);
Steve French789e6662011-08-09 18:44:44 +0000522 cifs_in_send_dec(server);
523 cifs_save_when_sent(mid);
Jeff Laytona6827c12011-01-11 07:24:21 -0500524 mutex_unlock(&server->srv_mutex);
Steve French789e6662011-08-09 18:44:44 +0000525
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100526 if (rc == 0)
527 return 0;
Jeff Laytona6827c12011-01-11 07:24:21 -0500528
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700529 cifs_delete_mid(mid);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400530 add_credits(server, 1, optype);
Jeff Laytona6827c12011-01-11 07:24:21 -0500531 wake_up(&server->request_q);
532 return rc;
533}
534
535/*
Steve French133672e2007-11-13 22:41:37 +0000536 *
537 * Send an SMB Request. No response info (other than return code)
538 * needs to be parsed.
539 *
540 * flags indicate the type of request buffer and how long to wait
541 * and whether to log NT STATUS code (error) before mapping it to POSIX error
542 *
543 */
544int
Steve French96daf2b2011-05-27 04:34:02 +0000545SendReceiveNoRsp(const unsigned int xid, struct cifs_ses *ses,
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400546 char *in_buf, int flags)
Steve French133672e2007-11-13 22:41:37 +0000547{
548 int rc;
549 struct kvec iov[1];
550 int resp_buf_type;
551
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400552 iov[0].iov_base = in_buf;
553 iov[0].iov_len = get_rfc1002_length(in_buf) + 4;
Steve French133672e2007-11-13 22:41:37 +0000554 flags |= CIFS_NO_RESP;
555 rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags);
Joe Perchesb6b38f72010-04-21 03:50:45 +0000556 cFYI(DBG2, "SendRcvNoRsp flags %d rc %d", flags, rc);
Steve French90c81e02008-02-12 20:32:36 +0000557
Steve French133672e2007-11-13 22:41:37 +0000558 return rc;
559}
560
Jeff Layton053d5032011-01-11 07:24:02 -0500561static int
Jeff Layton3c1105d2011-05-22 07:09:13 -0400562cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
Jeff Layton053d5032011-01-11 07:24:02 -0500563{
564 int rc = 0;
565
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400566 cFYI(1, "%s: cmd=%d mid=%llu state=%d", __func__,
567 le16_to_cpu(mid->command), mid->mid, mid->mid_state);
Jeff Layton053d5032011-01-11 07:24:02 -0500568
Jeff Layton74dd92a2011-01-11 07:24:02 -0500569 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400570 switch (mid->mid_state) {
Jeff Layton74dd92a2011-01-11 07:24:02 -0500571 case MID_RESPONSE_RECEIVED:
Jeff Layton053d5032011-01-11 07:24:02 -0500572 spin_unlock(&GlobalMid_Lock);
573 return rc;
Jeff Layton74dd92a2011-01-11 07:24:02 -0500574 case MID_RETRY_NEEDED:
575 rc = -EAGAIN;
576 break;
Jeff Layton71823ba2011-02-10 08:03:50 -0500577 case MID_RESPONSE_MALFORMED:
578 rc = -EIO;
579 break;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400580 case MID_SHUTDOWN:
581 rc = -EHOSTDOWN;
582 break;
Jeff Layton74dd92a2011-01-11 07:24:02 -0500583 default:
Jeff Layton3c1105d2011-05-22 07:09:13 -0400584 list_del_init(&mid->qhead);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400585 cERROR(1, "%s: invalid mid state mid=%llu state=%d", __func__,
586 mid->mid, mid->mid_state);
Jeff Layton74dd92a2011-01-11 07:24:02 -0500587 rc = -EIO;
Jeff Layton053d5032011-01-11 07:24:02 -0500588 }
589 spin_unlock(&GlobalMid_Lock);
590
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500591 DeleteMidQEntry(mid);
Jeff Layton053d5032011-01-11 07:24:02 -0500592 return rc;
593}
594
Jeff Layton121b0462012-05-15 12:21:10 -0400595static inline int
596send_cancel(struct TCP_Server_Info *server, void *buf, struct mid_q_entry *mid)
Jeff Layton76dcc262011-01-11 07:24:24 -0500597{
Jeff Layton121b0462012-05-15 12:21:10 -0400598 return server->ops->send_cancel ?
599 server->ops->send_cancel(server, buf, mid) : 0;
Jeff Layton76dcc262011-01-11 07:24:24 -0500600}
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602int
Jeff Layton2c8f9812011-05-19 16:22:52 -0400603cifs_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
604 bool log_error)
605{
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400606 unsigned int len = get_rfc1002_length(mid->resp_buf) + 4;
Jeff Layton826a95e2011-10-11 06:41:32 -0400607
608 dump_smb(mid->resp_buf, min_t(u32, 92, len));
Jeff Layton2c8f9812011-05-19 16:22:52 -0400609
610 /* convert the length into a more usable form */
Steve French96daf2b2011-05-27 04:34:02 +0000611 if (server->sec_mode & (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED)) {
Jeff Layton826a95e2011-10-11 06:41:32 -0400612 struct kvec iov;
Steve French985e4ff02012-08-03 09:42:45 -0500613 int rc = 0;
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700614 struct smb_rqst rqst = { .rq_iov = &iov,
615 .rq_nvec = 1 };
Jeff Layton826a95e2011-10-11 06:41:32 -0400616
617 iov.iov_base = mid->resp_buf;
618 iov.iov_len = len;
Jeff Layton2c8f9812011-05-19 16:22:52 -0400619 /* FIXME: add code to kill session */
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700620 rc = cifs_verify_signature(&rqst, server,
Steve French985e4ff02012-08-03 09:42:45 -0500621 mid->sequence_number + 1);
622 if (rc)
623 cERROR(1, "SMB signature verification returned error = "
624 "%d", rc);
Jeff Layton2c8f9812011-05-19 16:22:52 -0400625 }
626
627 /* BB special case reconnect tid and uid here? */
628 return map_smb_to_linux_error(mid->resp_buf, log_error);
629}
630
Jeff Laytonfec344e2012-09-18 16:20:35 -0700631struct mid_q_entry *
632cifs_setup_request(struct cifs_ses *ses, struct smb_rqst *rqst)
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400633{
634 int rc;
Jeff Laytonfec344e2012-09-18 16:20:35 -0700635 struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400636 struct mid_q_entry *mid;
637
638 rc = allocate_mid(ses, hdr, &mid);
639 if (rc)
Jeff Laytonfec344e2012-09-18 16:20:35 -0700640 return ERR_PTR(rc);
641 rc = cifs_sign_rqst(rqst, ses->server, &mid->sequence_number);
642 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700643 cifs_delete_mid(mid);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700644 return ERR_PTR(rc);
645 }
646 return mid;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400647}
648
Jeff Layton2c8f9812011-05-19 16:22:52 -0400649int
Steve French96daf2b2011-05-27 04:34:02 +0000650SendReceive2(const unsigned int xid, struct cifs_ses *ses,
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400651 struct kvec *iov, int n_vec, int *resp_buf_type /* ret */,
Steve French133672e2007-11-13 22:41:37 +0000652 const int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
654 int rc = 0;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400655 int timeout, optype;
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500656 struct mid_q_entry *midQ;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400657 char *buf = iov[0].iov_base;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400658 unsigned int credits = 1;
Jeff Laytonfec344e2012-09-18 16:20:35 -0700659 struct smb_rqst rqst = { .rq_iov = iov,
660 .rq_nvec = n_vec };
Steve French50c2f752007-07-13 00:33:32 +0000661
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400662 timeout = flags & CIFS_TIMEOUT_MASK;
663 optype = flags & CIFS_OP_MASK;
Steve French133672e2007-11-13 22:41:37 +0000664
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400665 *resp_buf_type = CIFS_NO_BUFFER; /* no response buf yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Steve French4b8f9302006-02-26 16:41:18 +0000667 if ((ses == NULL) || (ses->server == NULL)) {
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400668 cifs_small_buf_release(buf);
Joe Perchesb6b38f72010-04-21 03:50:45 +0000669 cERROR(1, "Null session");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return -EIO;
671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Steve French79a58d12007-07-06 22:44:50 +0000673 if (ses->server->tcpStatus == CifsExiting) {
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400674 cifs_small_buf_release(buf);
Steve French31ca3bc2005-04-28 22:41:11 -0700675 return -ENOENT;
Steve French4b8f9302006-02-26 16:41:18 +0000676 }
Steve French31ca3bc2005-04-28 22:41:11 -0700677
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400678 /*
679 * Ensure that we do not send more than 50 overlapping requests
680 * to the same server. We may make this configurable later or
681 * use ses->maxReq.
682 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400684 rc = wait_for_free_request(ses->server, timeout, optype);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000685 if (rc) {
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400686 cifs_small_buf_release(buf);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000687 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000689
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400690 /*
691 * Make sure that we sign in the same order that we send on this socket
692 * and avoid races inside tcp sendmsg code that could cause corruption
693 * of smb data.
694 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Jeff Layton72ca5452008-12-01 07:09:36 -0500696 mutex_lock(&ses->server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Jeff Laytonfec344e2012-09-18 16:20:35 -0700698 midQ = ses->server->ops->setup_request(ses, &rqst);
699 if (IS_ERR(midQ)) {
Jeff Layton72ca5452008-12-01 07:09:36 -0500700 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400701 cifs_small_buf_release(buf);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000702 /* Update # of requests on wire to server */
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400703 add_credits(ses->server, 1, optype);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700704 return PTR_ERR(midQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400707 midQ->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +0000708 cifs_in_send_inc(ses->server);
Jeff Layton0496e022008-12-30 12:39:16 -0500709 rc = smb_sendv(ses->server, iov, n_vec);
Steve French789e6662011-08-09 18:44:44 +0000710 cifs_in_send_dec(ses->server);
711 cifs_save_when_sent(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000712
Jeff Layton72ca5452008-12-01 07:09:36 -0500713 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000714
Jeff Layton2db7c582011-01-28 07:08:28 -0500715 if (rc < 0) {
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400716 cifs_small_buf_release(buf);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000717 goto out;
Jeff Layton2db7c582011-01-28 07:08:28 -0500718 }
Steve French4b8f9302006-02-26 16:41:18 +0000719
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400720 if (timeout == CIFS_ASYNC_OP) {
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400721 cifs_small_buf_release(buf);
Steve French133672e2007-11-13 22:41:37 +0000722 goto out;
Jeff Layton2db7c582011-01-28 07:08:28 -0500723 }
Jeff Layton0ade6402011-01-11 07:24:02 -0500724
725 rc = wait_for_response(ses->server, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -0500726 if (rc != 0) {
Jeff Layton121b0462012-05-15 12:21:10 -0400727 send_cancel(ses->server, buf, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -0500728 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400729 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
Jeff Layton1be912d2011-01-28 07:08:28 -0500730 midQ->callback = DeleteMidQEntry;
731 spin_unlock(&GlobalMid_Lock);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400732 cifs_small_buf_release(buf);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400733 add_credits(ses->server, 1, optype);
Jeff Layton1be912d2011-01-28 07:08:28 -0500734 return rc;
735 }
736 spin_unlock(&GlobalMid_Lock);
737 }
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500738
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400739 cifs_small_buf_release(buf);
Jeff Layton2db7c582011-01-28 07:08:28 -0500740
Jeff Layton3c1105d2011-05-22 07:09:13 -0400741 rc = cifs_sync_mid_result(midQ, ses->server);
Jeff Layton053d5032011-01-11 07:24:02 -0500742 if (rc != 0) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400743 add_credits(ses->server, 1, optype);
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500744 return rc;
745 }
Steve French50c2f752007-07-13 00:33:32 +0000746
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400747 if (!midQ->resp_buf || midQ->mid_state != MID_RESPONSE_RECEIVED) {
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500748 rc = -EIO;
Jeff Layton2c8f9812011-05-19 16:22:52 -0400749 cFYI(1, "Bad MID state?");
Steve French2b2bdfb2008-12-11 17:26:54 +0000750 goto out;
751 }
Steve French84afc292005-12-02 13:32:45 -0800752
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400753 buf = (char *)midQ->resp_buf;
754 iov[0].iov_base = buf;
755 iov[0].iov_len = get_rfc1002_length(buf) + 4;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400756 if (midQ->large_buf)
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400757 *resp_buf_type = CIFS_LARGE_BUFFER;
Jeff Layton2c8f9812011-05-19 16:22:52 -0400758 else
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400759 *resp_buf_type = CIFS_SMALL_BUFFER;
760
761 credits = ses->server->ops->get_credits(midQ);
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500762
Pavel Shilovsky082d0642012-05-17 12:18:21 +0400763 rc = ses->server->ops->check_receive(midQ, ses->server,
764 flags & CIFS_LOG_ERROR);
Steve French2b2bdfb2008-12-11 17:26:54 +0000765
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700766 /* mark it so buf will not be freed by cifs_delete_mid */
Jeff Layton2c8f9812011-05-19 16:22:52 -0400767 if ((flags & CIFS_NO_RESP) == 0)
768 midQ->resp_buf = NULL;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000769out:
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700770 cifs_delete_mid(midQ);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400771 add_credits(ses->server, credits, optype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 return rc;
774}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776int
Steve French96daf2b2011-05-27 04:34:02 +0000777SendReceive(const unsigned int xid, struct cifs_ses *ses,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400779 int *pbytes_returned, const int timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
781 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 struct mid_q_entry *midQ;
783
784 if (ses == NULL) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000785 cERROR(1, "Null smb session");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return -EIO;
787 }
Steve French79a58d12007-07-06 22:44:50 +0000788 if (ses->server == NULL) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000789 cERROR(1, "Null tcp session");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return -EIO;
791 }
792
Steve French79a58d12007-07-06 22:44:50 +0000793 if (ses->server->tcpStatus == CifsExiting)
Steve French31ca3bc2005-04-28 22:41:11 -0700794 return -ENOENT;
795
Steve French79a58d12007-07-06 22:44:50 +0000796 /* Ensure that we do not send more than 50 overlapping requests
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 to the same server. We may make this configurable later or
798 use ses->maxReq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000800 if (be32_to_cpu(in_buf->smb_buf_length) > CIFSMaxBufSize +
801 MAX_CIFS_HDR_SIZE - 4) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000802 cERROR(1, "Illegal length, greater than maximum frame, %d",
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000803 be32_to_cpu(in_buf->smb_buf_length));
Volker Lendecke6d9c6d52008-12-08 20:50:24 +0000804 return -EIO;
805 }
806
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400807 rc = wait_for_free_request(ses->server, timeout, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000808 if (rc)
809 return rc;
810
Steve French79a58d12007-07-06 22:44:50 +0000811 /* make sure that we sign in the same order that we send on this socket
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 and avoid races inside tcp sendmsg code that could cause corruption
813 of smb data */
814
Jeff Layton72ca5452008-12-01 07:09:36 -0500815 mutex_lock(&ses->server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000817 rc = allocate_mid(ses, in_buf, &midQ);
818 if (rc) {
Jeff Layton72ca5452008-12-01 07:09:36 -0500819 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000820 /* Update # of requests on wire to server */
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400821 add_credits(ses->server, 1, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000822 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
824
Steve Frenchad009ac2005-04-28 22:41:05 -0700825 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
Volker Lendecke829049c2008-12-06 16:00:53 +0100826 if (rc) {
827 mutex_unlock(&ses->server->srv_mutex);
828 goto out;
829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400831 midQ->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +0000832
833 cifs_in_send_inc(ses->server);
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000834 rc = smb_send(ses->server, in_buf, be32_to_cpu(in_buf->smb_buf_length));
Steve French789e6662011-08-09 18:44:44 +0000835 cifs_in_send_dec(ses->server);
836 cifs_save_when_sent(midQ);
Jeff Layton72ca5452008-12-01 07:09:36 -0500837 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000838
Steve French79a58d12007-07-06 22:44:50 +0000839 if (rc < 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000840 goto out;
841
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400842 if (timeout == CIFS_ASYNC_OP)
Steve French133672e2007-11-13 22:41:37 +0000843 goto out;
Jeff Layton0ade6402011-01-11 07:24:02 -0500844
845 rc = wait_for_response(ses->server, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -0500846 if (rc != 0) {
Jeff Layton121b0462012-05-15 12:21:10 -0400847 send_cancel(ses->server, in_buf, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -0500848 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400849 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
Jeff Layton1be912d2011-01-28 07:08:28 -0500850 /* no longer considered to be "in-flight" */
851 midQ->callback = DeleteMidQEntry;
852 spin_unlock(&GlobalMid_Lock);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400853 add_credits(ses->server, 1, 0);
Jeff Layton1be912d2011-01-28 07:08:28 -0500854 return rc;
855 }
856 spin_unlock(&GlobalMid_Lock);
857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Jeff Layton3c1105d2011-05-22 07:09:13 -0400859 rc = cifs_sync_mid_result(midQ, ses->server);
Jeff Layton053d5032011-01-11 07:24:02 -0500860 if (rc != 0) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400861 add_credits(ses->server, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return rc;
863 }
Steve French50c2f752007-07-13 00:33:32 +0000864
Jeff Layton2c8f9812011-05-19 16:22:52 -0400865 if (!midQ->resp_buf || !out_buf ||
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400866 midQ->mid_state != MID_RESPONSE_RECEIVED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 rc = -EIO;
Jeff Layton2c8f9812011-05-19 16:22:52 -0400868 cERROR(1, "Bad MID state?");
Steve French2b2bdfb2008-12-11 17:26:54 +0000869 goto out;
870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400872 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
Jeff Layton2c8f9812011-05-19 16:22:52 -0400873 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
874 rc = cifs_check_receive(midQ, ses->server, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000875out:
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700876 cifs_delete_mid(midQ);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400877 add_credits(ses->server, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 return rc;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000880}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000882/* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
883 blocking lock to return. */
884
885static int
Steve French96daf2b2011-05-27 04:34:02 +0000886send_lock_cancel(const unsigned int xid, struct cifs_tcon *tcon,
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000887 struct smb_hdr *in_buf,
888 struct smb_hdr *out_buf)
889{
890 int bytes_returned;
Steve French96daf2b2011-05-27 04:34:02 +0000891 struct cifs_ses *ses = tcon->ses;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000892 LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
893
894 /* We just modify the current in_buf to change
895 the type of lock from LOCKING_ANDX_SHARED_LOCK
896 or LOCKING_ANDX_EXCLUSIVE_LOCK to
897 LOCKING_ANDX_CANCEL_LOCK. */
898
899 pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
900 pSMB->Timeout = 0;
Pavel Shilovsky88257362012-05-23 14:01:59 +0400901 pSMB->hdr.Mid = get_next_mid(ses->server);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000902
903 return SendReceive(xid, ses, in_buf, out_buf,
Jeff Layton77499812011-01-11 07:24:23 -0500904 &bytes_returned, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000905}
906
907int
Steve French96daf2b2011-05-27 04:34:02 +0000908SendReceiveBlockingLock(const unsigned int xid, struct cifs_tcon *tcon,
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000909 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
910 int *pbytes_returned)
911{
912 int rc = 0;
913 int rstart = 0;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000914 struct mid_q_entry *midQ;
Steve French96daf2b2011-05-27 04:34:02 +0000915 struct cifs_ses *ses;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000916
917 if (tcon == NULL || tcon->ses == NULL) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000918 cERROR(1, "Null smb session");
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000919 return -EIO;
920 }
921 ses = tcon->ses;
922
Steve French79a58d12007-07-06 22:44:50 +0000923 if (ses->server == NULL) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000924 cERROR(1, "Null tcp session");
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000925 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 }
927
Steve French79a58d12007-07-06 22:44:50 +0000928 if (ses->server->tcpStatus == CifsExiting)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000929 return -ENOENT;
930
Steve French79a58d12007-07-06 22:44:50 +0000931 /* Ensure that we do not send more than 50 overlapping requests
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000932 to the same server. We may make this configurable later or
933 use ses->maxReq */
934
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000935 if (be32_to_cpu(in_buf->smb_buf_length) > CIFSMaxBufSize +
936 MAX_CIFS_HDR_SIZE - 4) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000937 cERROR(1, "Illegal length, greater than maximum frame, %d",
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000938 be32_to_cpu(in_buf->smb_buf_length));
Volker Lendecke6d9c6d52008-12-08 20:50:24 +0000939 return -EIO;
940 }
941
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400942 rc = wait_for_free_request(ses->server, CIFS_BLOCKING_OP, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000943 if (rc)
944 return rc;
945
Steve French79a58d12007-07-06 22:44:50 +0000946 /* make sure that we sign in the same order that we send on this socket
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000947 and avoid races inside tcp sendmsg code that could cause corruption
948 of smb data */
949
Jeff Layton72ca5452008-12-01 07:09:36 -0500950 mutex_lock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000951
952 rc = allocate_mid(ses, in_buf, &midQ);
953 if (rc) {
Jeff Layton72ca5452008-12-01 07:09:36 -0500954 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000955 return rc;
956 }
957
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000958 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
Volker Lendecke829049c2008-12-06 16:00:53 +0100959 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700960 cifs_delete_mid(midQ);
Volker Lendecke829049c2008-12-06 16:00:53 +0100961 mutex_unlock(&ses->server->srv_mutex);
962 return rc;
963 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000964
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400965 midQ->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +0000966 cifs_in_send_inc(ses->server);
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000967 rc = smb_send(ses->server, in_buf, be32_to_cpu(in_buf->smb_buf_length));
Steve French789e6662011-08-09 18:44:44 +0000968 cifs_in_send_dec(ses->server);
969 cifs_save_when_sent(midQ);
Jeff Layton72ca5452008-12-01 07:09:36 -0500970 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000971
Steve French79a58d12007-07-06 22:44:50 +0000972 if (rc < 0) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700973 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000974 return rc;
975 }
976
977 /* Wait for a reply - allow signals to interrupt. */
978 rc = wait_event_interruptible(ses->server->response_q,
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400979 (!(midQ->mid_state == MID_REQUEST_SUBMITTED)) ||
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000980 ((ses->server->tcpStatus != CifsGood) &&
981 (ses->server->tcpStatus != CifsNew)));
982
983 /* Were we interrupted by a signal ? */
984 if ((rc == -ERESTARTSYS) &&
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400985 (midQ->mid_state == MID_REQUEST_SUBMITTED) &&
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000986 ((ses->server->tcpStatus == CifsGood) ||
987 (ses->server->tcpStatus == CifsNew))) {
988
989 if (in_buf->Command == SMB_COM_TRANSACTION2) {
990 /* POSIX lock. We send a NT_CANCEL SMB to cause the
991 blocking lock to return. */
Jeff Layton121b0462012-05-15 12:21:10 -0400992 rc = send_cancel(ses->server, in_buf, midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000993 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700994 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000995 return rc;
996 }
997 } else {
998 /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
999 to cause the blocking lock to return. */
1000
1001 rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
1002
1003 /* If we get -ENOLCK back the lock may have
1004 already been removed. Don't exit in this case. */
1005 if (rc && rc != -ENOLCK) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001006 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001007 return rc;
1008 }
1009 }
1010
Jeff Layton1be912d2011-01-28 07:08:28 -05001011 rc = wait_for_response(ses->server, midQ);
1012 if (rc) {
Jeff Layton121b0462012-05-15 12:21:10 -04001013 send_cancel(ses->server, in_buf, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -05001014 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001015 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
Jeff Layton1be912d2011-01-28 07:08:28 -05001016 /* no longer considered to be "in-flight" */
1017 midQ->callback = DeleteMidQEntry;
1018 spin_unlock(&GlobalMid_Lock);
1019 return rc;
1020 }
1021 spin_unlock(&GlobalMid_Lock);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001022 }
Jeff Layton1be912d2011-01-28 07:08:28 -05001023
1024 /* We got the response - restart system call. */
1025 rstart = 1;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001026 }
1027
Jeff Layton3c1105d2011-05-22 07:09:13 -04001028 rc = cifs_sync_mid_result(midQ, ses->server);
Jeff Layton053d5032011-01-11 07:24:02 -05001029 if (rc != 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001030 return rc;
Steve French50c2f752007-07-13 00:33:32 +00001031
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001032 /* rcvd frame is ok */
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001033 if (out_buf == NULL || midQ->mid_state != MID_RESPONSE_RECEIVED) {
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001034 rc = -EIO;
Joe Perchesb6b38f72010-04-21 03:50:45 +00001035 cERROR(1, "Bad MID state?");
Volker Lendecke698e96a2008-12-06 16:39:31 +01001036 goto out;
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001037 }
1038
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001039 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
Jeff Layton2c8f9812011-05-19 16:22:52 -04001040 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
1041 rc = cifs_check_receive(midQ, ses->server, 0);
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001042out:
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001043 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001044 if (rstart && rc == -EACCES)
1045 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 return rc;
1047}