blob: b6097344cd5bb18fdb0682c35ced87987eb4fda9 [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
Pavel Shilovsky45740842012-06-01 14:26:18 +0400457int
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400458cifs_setup_async_request(struct TCP_Server_Info *server, struct kvec *iov,
459 unsigned int nvec, struct mid_q_entry **ret_mid)
460{
461 int rc;
462 struct smb_hdr *hdr = (struct smb_hdr *)iov[0].iov_base;
463 struct mid_q_entry *mid;
464
465 /* enable signing if server requires it */
466 if (server->sec_mode & (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
467 hdr->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
468
469 mid = AllocMidQEntry(hdr, server);
470 if (mid == NULL)
471 return -ENOMEM;
472
Jeff Layton762a4202012-07-23 13:28:38 -0400473 rc = cifs_sign_smbv(iov, nvec, server, &mid->sequence_number);
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100474 if (rc) {
475 DeleteMidQEntry(mid);
476 return rc;
477 }
478
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400479 *ret_mid = mid;
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100480 return 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400481}
Steve French133672e2007-11-13 22:41:37 +0000482
483/*
Jeff Laytona6827c12011-01-11 07:24:21 -0500484 * Send a SMB request and set the callback function in the mid to handle
485 * the result. Caller is responsible for dealing with timeouts.
486 */
487int
Jeff Laytonfcc31cb2011-05-19 16:22:53 -0400488cifs_call_async(struct TCP_Server_Info *server, struct kvec *iov,
Jeff Layton44d22d82011-10-19 15:29:49 -0400489 unsigned int nvec, mid_receive_t *receive,
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400490 mid_callback_t *callback, void *cbdata, const int flags)
Jeff Laytona6827c12011-01-11 07:24:21 -0500491{
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400492 int rc, timeout, optype;
Jeff Laytona6827c12011-01-11 07:24:21 -0500493 struct mid_q_entry *mid;
494
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400495 timeout = flags & CIFS_TIMEOUT_MASK;
496 optype = flags & CIFS_OP_MASK;
497
498 rc = wait_for_free_request(server, timeout, optype);
Jeff Laytona6827c12011-01-11 07:24:21 -0500499 if (rc)
500 return rc;
501
502 mutex_lock(&server->srv_mutex);
Pavel Shilovsky45740842012-06-01 14:26:18 +0400503 rc = server->ops->setup_async_request(server, iov, nvec, &mid);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400504 if (rc) {
Jeff Laytona6827c12011-01-11 07:24:21 -0500505 mutex_unlock(&server->srv_mutex);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400506 add_credits(server, 1, optype);
Pavel Shilovsky0193e072011-08-03 23:12:18 +0400507 wake_up(&server->request_q);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400508 return rc;
Jeff Laytona6827c12011-01-11 07:24:21 -0500509 }
510
Jeff Layton44d22d82011-10-19 15:29:49 -0400511 mid->receive = receive;
Jeff Laytona6827c12011-01-11 07:24:21 -0500512 mid->callback = callback;
513 mid->callback_data = cbdata;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400514 mid->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +0000515
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100516 /* put it on the pending_mid_q */
517 spin_lock(&GlobalMid_Lock);
518 list_add_tail(&mid->qhead, &server->pending_mid_q);
519 spin_unlock(&GlobalMid_Lock);
520
521
Steve French789e6662011-08-09 18:44:44 +0000522 cifs_in_send_inc(server);
Jeff Laytonfcc31cb2011-05-19 16:22:53 -0400523 rc = smb_sendv(server, iov, nvec);
Steve French789e6662011-08-09 18:44:44 +0000524 cifs_in_send_dec(server);
525 cifs_save_when_sent(mid);
Jeff Laytona6827c12011-01-11 07:24:21 -0500526 mutex_unlock(&server->srv_mutex);
Steve French789e6662011-08-09 18:44:44 +0000527
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100528 if (rc == 0)
529 return 0;
Jeff Laytona6827c12011-01-11 07:24:21 -0500530
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700531 cifs_delete_mid(mid);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400532 add_credits(server, 1, optype);
Jeff Laytona6827c12011-01-11 07:24:21 -0500533 wake_up(&server->request_q);
534 return rc;
535}
536
537/*
Steve French133672e2007-11-13 22:41:37 +0000538 *
539 * Send an SMB Request. No response info (other than return code)
540 * needs to be parsed.
541 *
542 * flags indicate the type of request buffer and how long to wait
543 * and whether to log NT STATUS code (error) before mapping it to POSIX error
544 *
545 */
546int
Steve French96daf2b2011-05-27 04:34:02 +0000547SendReceiveNoRsp(const unsigned int xid, struct cifs_ses *ses,
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400548 char *in_buf, int flags)
Steve French133672e2007-11-13 22:41:37 +0000549{
550 int rc;
551 struct kvec iov[1];
552 int resp_buf_type;
553
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400554 iov[0].iov_base = in_buf;
555 iov[0].iov_len = get_rfc1002_length(in_buf) + 4;
Steve French133672e2007-11-13 22:41:37 +0000556 flags |= CIFS_NO_RESP;
557 rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags);
Joe Perchesb6b38f72010-04-21 03:50:45 +0000558 cFYI(DBG2, "SendRcvNoRsp flags %d rc %d", flags, rc);
Steve French90c81e02008-02-12 20:32:36 +0000559
Steve French133672e2007-11-13 22:41:37 +0000560 return rc;
561}
562
Jeff Layton053d5032011-01-11 07:24:02 -0500563static int
Jeff Layton3c1105d2011-05-22 07:09:13 -0400564cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
Jeff Layton053d5032011-01-11 07:24:02 -0500565{
566 int rc = 0;
567
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400568 cFYI(1, "%s: cmd=%d mid=%llu state=%d", __func__,
569 le16_to_cpu(mid->command), mid->mid, mid->mid_state);
Jeff Layton053d5032011-01-11 07:24:02 -0500570
Jeff Layton74dd92a2011-01-11 07:24:02 -0500571 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400572 switch (mid->mid_state) {
Jeff Layton74dd92a2011-01-11 07:24:02 -0500573 case MID_RESPONSE_RECEIVED:
Jeff Layton053d5032011-01-11 07:24:02 -0500574 spin_unlock(&GlobalMid_Lock);
575 return rc;
Jeff Layton74dd92a2011-01-11 07:24:02 -0500576 case MID_RETRY_NEEDED:
577 rc = -EAGAIN;
578 break;
Jeff Layton71823ba2011-02-10 08:03:50 -0500579 case MID_RESPONSE_MALFORMED:
580 rc = -EIO;
581 break;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400582 case MID_SHUTDOWN:
583 rc = -EHOSTDOWN;
584 break;
Jeff Layton74dd92a2011-01-11 07:24:02 -0500585 default:
Jeff Layton3c1105d2011-05-22 07:09:13 -0400586 list_del_init(&mid->qhead);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400587 cERROR(1, "%s: invalid mid state mid=%llu state=%d", __func__,
588 mid->mid, mid->mid_state);
Jeff Layton74dd92a2011-01-11 07:24:02 -0500589 rc = -EIO;
Jeff Layton053d5032011-01-11 07:24:02 -0500590 }
591 spin_unlock(&GlobalMid_Lock);
592
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500593 DeleteMidQEntry(mid);
Jeff Layton053d5032011-01-11 07:24:02 -0500594 return rc;
595}
596
Jeff Layton121b0462012-05-15 12:21:10 -0400597static inline int
598send_cancel(struct TCP_Server_Info *server, void *buf, struct mid_q_entry *mid)
Jeff Layton76dcc262011-01-11 07:24:24 -0500599{
Jeff Layton121b0462012-05-15 12:21:10 -0400600 return server->ops->send_cancel ?
601 server->ops->send_cancel(server, buf, mid) : 0;
Jeff Layton76dcc262011-01-11 07:24:24 -0500602}
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604int
Jeff Layton2c8f9812011-05-19 16:22:52 -0400605cifs_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
606 bool log_error)
607{
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400608 unsigned int len = get_rfc1002_length(mid->resp_buf) + 4;
Jeff Layton826a95e2011-10-11 06:41:32 -0400609
610 dump_smb(mid->resp_buf, min_t(u32, 92, len));
Jeff Layton2c8f9812011-05-19 16:22:52 -0400611
612 /* convert the length into a more usable form */
Steve French96daf2b2011-05-27 04:34:02 +0000613 if (server->sec_mode & (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED)) {
Jeff Layton826a95e2011-10-11 06:41:32 -0400614 struct kvec iov;
Steve French985e4ff02012-08-03 09:42:45 -0500615 int rc = 0;
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700616 struct smb_rqst rqst = { .rq_iov = &iov,
617 .rq_nvec = 1 };
Jeff Layton826a95e2011-10-11 06:41:32 -0400618
619 iov.iov_base = mid->resp_buf;
620 iov.iov_len = len;
Jeff Layton2c8f9812011-05-19 16:22:52 -0400621 /* FIXME: add code to kill session */
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700622 rc = cifs_verify_signature(&rqst, server,
Steve French985e4ff02012-08-03 09:42:45 -0500623 mid->sequence_number + 1);
624 if (rc)
625 cERROR(1, "SMB signature verification returned error = "
626 "%d", rc);
Jeff Layton2c8f9812011-05-19 16:22:52 -0400627 }
628
629 /* BB special case reconnect tid and uid here? */
630 return map_smb_to_linux_error(mid->resp_buf, log_error);
631}
632
Pavel Shilovsky082d0642012-05-17 12:18:21 +0400633int
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400634cifs_setup_request(struct cifs_ses *ses, struct kvec *iov,
635 unsigned int nvec, struct mid_q_entry **ret_mid)
636{
637 int rc;
638 struct smb_hdr *hdr = (struct smb_hdr *)iov[0].iov_base;
639 struct mid_q_entry *mid;
640
641 rc = allocate_mid(ses, hdr, &mid);
642 if (rc)
643 return rc;
Jeff Layton762a4202012-07-23 13:28:38 -0400644 rc = cifs_sign_smbv(iov, nvec, ses->server, &mid->sequence_number);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400645 if (rc)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700646 cifs_delete_mid(mid);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400647 *ret_mid = mid;
648 return rc;
649}
650
Jeff Layton2c8f9812011-05-19 16:22:52 -0400651int
Steve French96daf2b2011-05-27 04:34:02 +0000652SendReceive2(const unsigned int xid, struct cifs_ses *ses,
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400653 struct kvec *iov, int n_vec, int *resp_buf_type /* ret */,
Steve French133672e2007-11-13 22:41:37 +0000654 const int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
656 int rc = 0;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400657 int timeout, optype;
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500658 struct mid_q_entry *midQ;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400659 char *buf = iov[0].iov_base;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400660 unsigned int credits = 1;
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
Pavel Shilovsky082d0642012-05-17 12:18:21 +0400698 rc = ses->server->ops->setup_request(ses, iov, n_vec, &midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000699 if (rc) {
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);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000704 return rc;
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}