blob: c4d7825dfc0a31c7d91132cb3e9cc87f5ff48155 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/uaccess.h>
31#include <asm/processor.h>
32#include <linux/mempool.h>
33#include "cifspdu.h"
34#include "cifsglob.h"
35#include "cifsproto.h"
36#include "cifs_debug.h"
Steve French50c2f752007-07-13 00:33:32 +000037
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +040038void
39cifs_wake_up_task(struct mid_q_entry *mid)
Jeff Layton2b84a36c2011-01-11 07:24:21 -050040{
41 wake_up_process(mid->callback_data);
42}
43
Jeff Laytona6827c12011-01-11 07:24:21 -050044struct mid_q_entry *
Jeff Layton24b9b062008-12-01 07:09:34 -050045AllocMidQEntry(const struct smb_hdr *smb_buffer, struct TCP_Server_Info *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
47 struct mid_q_entry *temp;
48
Jeff Layton24b9b062008-12-01 07:09:34 -050049 if (server == NULL) {
Joe Perchesb6b38f72010-04-21 03:50:45 +000050 cERROR(1, "Null TCP session in AllocMidQEntry");
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 return NULL;
52 }
Steve French50c2f752007-07-13 00:33:32 +000053
Pekka Enberg232087c2008-09-15 13:22:54 +030054 temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 if (temp == NULL)
56 return temp;
57 else {
Steve French26f57362007-08-30 22:09:15 +000058 memset(temp, 0, sizeof(struct mid_q_entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 temp->mid = smb_buffer->Mid; /* always LE */
60 temp->pid = current->pid;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -040061 temp->command = cpu_to_le16(smb_buffer->Command);
62 cFYI(1, "For smb_command %d", smb_buffer->Command);
Steve French1047abc2005-10-11 19:58:06 -070063 /* do_gettimeofday(&temp->when_sent);*/ /* easier to use jiffies */
64 /* when mid allocated can be before when sent */
65 temp->when_alloc = jiffies;
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +040066 temp->server = server;
Jeff Layton2b84a36c2011-01-11 07:24:21 -050067
68 /*
69 * The default is for the mid to be synchronous, so the
70 * default callback just wakes up the current task.
71 */
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +040072 temp->callback = cifs_wake_up_task;
Jeff Layton2b84a36c2011-01-11 07:24:21 -050073 temp->callback_data = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 }
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 atomic_inc(&midCount);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -040077 temp->mid_state = MID_REQUEST_ALLOCATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return temp;
79}
80
Jeff Layton766fdbb2011-01-11 07:24:21 -050081void
Linus Torvalds1da177e2005-04-16 15:20:36 -070082DeleteMidQEntry(struct mid_q_entry *midEntry)
83{
Steve French1047abc2005-10-11 19:58:06 -070084#ifdef CONFIG_CIFS_STATS2
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +040085 __le16 command = midEntry->server->vals->lock_cmd;
Steve French1047abc2005-10-11 19:58:06 -070086 unsigned long now;
87#endif
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -040088 midEntry->mid_state = MID_FREE;
Jeff Layton80975312011-01-11 07:24:02 -050089 atomic_dec(&midCount);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -040090 if (midEntry->large_buf)
Steve Frenchb8643e12005-04-28 22:41:07 -070091 cifs_buf_release(midEntry->resp_buf);
92 else
93 cifs_small_buf_release(midEntry->resp_buf);
Steve French1047abc2005-10-11 19:58:06 -070094#ifdef CONFIG_CIFS_STATS2
95 now = jiffies;
96 /* commands taking longer than one second are indications that
97 something is wrong, unless it is quite a slow link or server */
Steve French79a58d12007-07-06 22:44:50 +000098 if ((now - midEntry->when_alloc) > HZ) {
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +040099 if ((cifsFYI & CIFS_TIMER) && (midEntry->command != command)) {
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400100 printk(KERN_DEBUG " CIFS slow rsp: cmd %d mid %llu",
Steve French1047abc2005-10-11 19:58:06 -0700101 midEntry->command, midEntry->mid);
102 printk(" A: 0x%lx S: 0x%lx R: 0x%lx\n",
103 now - midEntry->when_alloc,
104 now - midEntry->when_sent,
105 now - midEntry->when_received);
106 }
107 }
108#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 mempool_free(midEntry, cifs_mid_poolp);
110}
111
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700112void
113cifs_delete_mid(struct mid_q_entry *mid)
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500114{
115 spin_lock(&GlobalMid_Lock);
116 list_del(&mid->qhead);
117 spin_unlock(&GlobalMid_Lock);
118
119 DeleteMidQEntry(mid);
120}
121
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500122static int
Jeff Layton0496e022008-12-30 12:39:16 -0500123smb_sendv(struct TCP_Server_Info *server, struct kvec *iov, int n_vec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 int rc = 0;
126 int i = 0;
127 struct msghdr smb_msg;
Steve French3e844692005-10-03 13:37:24 -0700128 unsigned int len = iov[0].iov_len;
129 unsigned int total_len;
130 int first_vec = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400131 unsigned int smb_buf_length = get_rfc1002_length(iov[0].iov_base);
Steve Frenchedf1ae42008-10-29 00:47:57 +0000132 struct socket *ssocket = server->ssocket;
Steve French50c2f752007-07-13 00:33:32 +0000133
Steve French79a58d12007-07-06 22:44:50 +0000134 if (ssocket == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return -ENOTSOCK; /* BB eventually add reconnect code here */
Steve French3e844692005-10-03 13:37:24 -0700136
Pavel Shilovskya9f1b852010-12-13 19:08:35 +0300137 smb_msg.msg_name = (struct sockaddr *) &server->dstaddr;
Steve French26f57362007-08-30 22:09:15 +0000138 smb_msg.msg_namelen = sizeof(struct sockaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 smb_msg.msg_control = NULL;
140 smb_msg.msg_controllen = 0;
Jeff Layton0496e022008-12-30 12:39:16 -0500141 if (server->noblocksnd)
Steve Frenchedf1ae42008-10-29 00:47:57 +0000142 smb_msg.msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL;
143 else
144 smb_msg.msg_flags = MSG_NOSIGNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Steve French3e844692005-10-03 13:37:24 -0700146 total_len = 0;
147 for (i = 0; i < n_vec; i++)
148 total_len += iov[i].iov_len;
149
Joe Perchesb6b38f72010-04-21 03:50:45 +0000150 cFYI(1, "Sending smb: total_len %d", total_len);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400151 dump_smb(iov[0].iov_base, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Shirish Pargaonkar17680352008-07-29 21:26:13 +0000153 i = 0;
Steve French3e844692005-10-03 13:37:24 -0700154 while (total_len) {
155 rc = kernel_sendmsg(ssocket, &smb_msg, &iov[first_vec],
156 n_vec - first_vec, total_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 if ((rc == -ENOSPC) || (rc == -EAGAIN)) {
158 i++;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400159 /*
160 * If blocking send we try 3 times, since each can block
161 * for 5 seconds. For nonblocking we have to try more
162 * but wait increasing amounts of time allowing time for
163 * socket to clear. The overall time we wait in either
164 * case to send on the socket is about 15 seconds.
165 * Similarly we wait for 15 seconds for a response from
166 * the server in SendReceive[2] for the server to send
167 * a response back for most types of requests (except
168 * SMB Write past end of file which can be slow, and
169 * blocking lock operations). NFS waits slightly longer
170 * than CIFS, but this can make it take longer for
171 * nonresponsive servers to be detected and 15 seconds
172 * is more than enough time for modern networks to
173 * send a packet. In most cases if we fail to send
174 * after the retries we will kill the socket and
175 * reconnect which may clear the network problem.
176 */
Steve Frenchda505c32009-01-19 03:49:35 +0000177 if ((i >= 14) || (!server->noblocksnd && (i > 2))) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000178 cERROR(1, "sends on sock %p stuck for 15 seconds",
179 ssocket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 rc = -EAGAIN;
181 break;
182 }
Steve French68058e72005-10-10 10:34:22 -0700183 msleep(1 << i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 continue;
185 }
Steve French79a58d12007-07-06 22:44:50 +0000186 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 break;
Steve French3e844692005-10-03 13:37:24 -0700188
Steve French61de8002008-10-30 20:15:22 +0000189 if (rc == total_len) {
190 total_len = 0;
191 break;
192 } else if (rc > total_len) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000193 cERROR(1, "sent %d requested %d", rc, total_len);
Steve French3e844692005-10-03 13:37:24 -0700194 break;
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500195 }
Steve French79a58d12007-07-06 22:44:50 +0000196 if (rc == 0) {
Steve French3e844692005-10-03 13:37:24 -0700197 /* should never happen, letting socket clear before
198 retrying is our only obvious option here */
Joe Perchesb6b38f72010-04-21 03:50:45 +0000199 cERROR(1, "tcp sent no data");
Steve French3e844692005-10-03 13:37:24 -0700200 msleep(500);
201 continue;
202 }
203 total_len -= rc;
Steve French68058e72005-10-10 10:34:22 -0700204 /* the line below resets i */
Steve French3e844692005-10-03 13:37:24 -0700205 for (i = first_vec; i < n_vec; i++) {
206 if (iov[i].iov_len) {
207 if (rc > iov[i].iov_len) {
208 rc -= iov[i].iov_len;
209 iov[i].iov_len = 0;
210 } else {
211 iov[i].iov_base += rc;
212 iov[i].iov_len -= rc;
213 first_vec = i;
214 break;
215 }
216 }
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500217 }
Steve French5e1253b2005-10-10 14:06:37 -0700218 i = 0; /* in case we get ENOSPC on the next send */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
220
Steve Frenchedf1ae42008-10-29 00:47:57 +0000221 if ((total_len > 0) && (total_len != smb_buf_length + 4)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000222 cFYI(1, "partial send (%d remaining), terminating session",
223 total_len);
Steve Frenchedf1ae42008-10-29 00:47:57 +0000224 /* If we have only sent part of an SMB then the next SMB
225 could be taken as the remainder of this one. We need
226 to kill the socket so the server throws away the partial
227 SMB */
228 server->tcpStatus = CifsNeedReconnect;
229 }
230
Jeff Laytond804d412011-01-28 15:05:43 -0500231 if (rc < 0 && rc != -EINTR)
Joe Perchesb6b38f72010-04-21 03:50:45 +0000232 cERROR(1, "Error %d sending data on socket to server", rc);
Jeff Laytond804d412011-01-28 15:05:43 -0500233 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 return rc;
237}
238
Jeff Layton0496e022008-12-30 12:39:16 -0500239int
240smb_send(struct TCP_Server_Info *server, struct smb_hdr *smb_buffer,
241 unsigned int smb_buf_length)
242{
243 struct kvec iov;
244
245 iov.iov_base = smb_buffer;
246 iov.iov_len = smb_buf_length + 4;
247
248 return smb_sendv(server, &iov, 1);
249}
250
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300251static int
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400252wait_for_free_credits(struct TCP_Server_Info *server, const int timeout,
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300253 int *credits)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000254{
Pavel Shilovsky5bc59492012-02-21 19:56:08 +0300255 int rc;
256
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300257 spin_lock(&server->req_lock);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400258 if (timeout == CIFS_ASYNC_OP) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000259 /* oplock breaks must not be held up */
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300260 server->in_flight++;
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300261 *credits -= 1;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300262 spin_unlock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000263 return 0;
264 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000265
Volker Lendecke27a97a62008-12-08 20:59:39 +0000266 while (1) {
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300267 if (*credits <= 0) {
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300268 spin_unlock(&server->req_lock);
Steve French789e6662011-08-09 18:44:44 +0000269 cifs_num_waiters_inc(server);
Pavel Shilovsky5bc59492012-02-21 19:56:08 +0300270 rc = wait_event_killable(server->request_q,
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300271 has_credits(server, credits));
Steve French789e6662011-08-09 18:44:44 +0000272 cifs_num_waiters_dec(server);
Pavel Shilovsky5bc59492012-02-21 19:56:08 +0300273 if (rc)
274 return rc;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300275 spin_lock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000276 } else {
Jeff Laytonc5797a92011-01-11 07:24:01 -0500277 if (server->tcpStatus == CifsExiting) {
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300278 spin_unlock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000279 return -ENOENT;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000280 }
Volker Lendecke27a97a62008-12-08 20:59:39 +0000281
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400282 /*
283 * Can not count locking commands against total
284 * as they are allowed to block on server.
285 */
Volker Lendecke27a97a62008-12-08 20:59:39 +0000286
287 /* update # of requests on the wire to server */
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400288 if (timeout != CIFS_BLOCKING_OP) {
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300289 *credits -= 1;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300290 server->in_flight++;
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400291 }
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300292 spin_unlock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000293 break;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000294 }
295 }
296 return 0;
297}
298
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300299static int
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400300wait_for_free_request(struct TCP_Server_Info *server, const int timeout,
301 const int optype)
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300302{
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400303 return wait_for_free_credits(server, timeout,
304 server->ops->get_credits_field(server, optype));
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300305}
306
Steve French96daf2b2011-05-27 04:34:02 +0000307static int allocate_mid(struct cifs_ses *ses, struct smb_hdr *in_buf,
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000308 struct mid_q_entry **ppmidQ)
309{
310 if (ses->server->tcpStatus == CifsExiting) {
311 return -ENOENT;
Volker Lendecke8fbbd362008-12-06 13:12:34 +0100312 }
313
314 if (ses->server->tcpStatus == CifsNeedReconnect) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000315 cFYI(1, "tcp session dead - return to caller to retry");
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000316 return -EAGAIN;
Volker Lendecke8fbbd362008-12-06 13:12:34 +0100317 }
318
319 if (ses->status != CifsGood) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000320 /* check if SMB session is bad because we are setting it up */
Steve French79a58d12007-07-06 22:44:50 +0000321 if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
Steve Frenchad7a2922008-02-07 23:25:02 +0000322 (in_buf->Command != SMB_COM_NEGOTIATE))
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000323 return -EAGAIN;
Steve Frenchad7a2922008-02-07 23:25:02 +0000324 /* else ok - we are setting up session */
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000325 }
Jeff Layton24b9b062008-12-01 07:09:34 -0500326 *ppmidQ = AllocMidQEntry(in_buf, ses->server);
Steve French26f57362007-08-30 22:09:15 +0000327 if (*ppmidQ == NULL)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000328 return -ENOMEM;
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500329 spin_lock(&GlobalMid_Lock);
330 list_add_tail(&(*ppmidQ)->qhead, &ses->server->pending_mid_q);
331 spin_unlock(&GlobalMid_Lock);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000332 return 0;
333}
334
Jeff Layton0ade6402011-01-11 07:24:02 -0500335static int
336wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *midQ)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000337{
Jeff Layton0ade6402011-01-11 07:24:02 -0500338 int error;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000339
Jeff Laytonf06ac722011-10-19 15:30:40 -0400340 error = wait_event_freezekillable(server->response_q,
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400341 midQ->mid_state != MID_REQUEST_SUBMITTED);
Jeff Layton0ade6402011-01-11 07:24:02 -0500342 if (error < 0)
343 return -ERESTARTSYS;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000344
Jeff Layton0ade6402011-01-11 07:24:02 -0500345 return 0;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000346}
347
Pavel Shilovsky45740842012-06-01 14:26:18 +0400348int
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400349cifs_setup_async_request(struct TCP_Server_Info *server, struct kvec *iov,
350 unsigned int nvec, struct mid_q_entry **ret_mid)
351{
352 int rc;
353 struct smb_hdr *hdr = (struct smb_hdr *)iov[0].iov_base;
354 struct mid_q_entry *mid;
355
356 /* enable signing if server requires it */
357 if (server->sec_mode & (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
358 hdr->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
359
360 mid = AllocMidQEntry(hdr, server);
361 if (mid == NULL)
362 return -ENOMEM;
363
Jeff Layton762a4202012-07-23 13:28:38 -0400364 rc = cifs_sign_smbv(iov, nvec, server, &mid->sequence_number);
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100365 if (rc) {
366 DeleteMidQEntry(mid);
367 return rc;
368 }
369
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400370 *ret_mid = mid;
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100371 return 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400372}
Steve French133672e2007-11-13 22:41:37 +0000373
374/*
Jeff Laytona6827c12011-01-11 07:24:21 -0500375 * Send a SMB request and set the callback function in the mid to handle
376 * the result. Caller is responsible for dealing with timeouts.
377 */
378int
Jeff Laytonfcc31cb2011-05-19 16:22:53 -0400379cifs_call_async(struct TCP_Server_Info *server, struct kvec *iov,
Jeff Layton44d22d82011-10-19 15:29:49 -0400380 unsigned int nvec, mid_receive_t *receive,
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400381 mid_callback_t *callback, void *cbdata, const int flags)
Jeff Laytona6827c12011-01-11 07:24:21 -0500382{
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400383 int rc, timeout, optype;
Jeff Laytona6827c12011-01-11 07:24:21 -0500384 struct mid_q_entry *mid;
385
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400386 timeout = flags & CIFS_TIMEOUT_MASK;
387 optype = flags & CIFS_OP_MASK;
388
389 rc = wait_for_free_request(server, timeout, optype);
Jeff Laytona6827c12011-01-11 07:24:21 -0500390 if (rc)
391 return rc;
392
393 mutex_lock(&server->srv_mutex);
Pavel Shilovsky45740842012-06-01 14:26:18 +0400394 rc = server->ops->setup_async_request(server, iov, nvec, &mid);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400395 if (rc) {
Jeff Laytona6827c12011-01-11 07:24:21 -0500396 mutex_unlock(&server->srv_mutex);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400397 add_credits(server, 1, optype);
Pavel Shilovsky0193e072011-08-03 23:12:18 +0400398 wake_up(&server->request_q);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400399 return rc;
Jeff Laytona6827c12011-01-11 07:24:21 -0500400 }
401
Jeff Layton44d22d82011-10-19 15:29:49 -0400402 mid->receive = receive;
Jeff Laytona6827c12011-01-11 07:24:21 -0500403 mid->callback = callback;
404 mid->callback_data = cbdata;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400405 mid->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +0000406
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100407 /* put it on the pending_mid_q */
408 spin_lock(&GlobalMid_Lock);
409 list_add_tail(&mid->qhead, &server->pending_mid_q);
410 spin_unlock(&GlobalMid_Lock);
411
412
Steve French789e6662011-08-09 18:44:44 +0000413 cifs_in_send_inc(server);
Jeff Laytonfcc31cb2011-05-19 16:22:53 -0400414 rc = smb_sendv(server, iov, nvec);
Steve French789e6662011-08-09 18:44:44 +0000415 cifs_in_send_dec(server);
416 cifs_save_when_sent(mid);
Jeff Laytona6827c12011-01-11 07:24:21 -0500417 mutex_unlock(&server->srv_mutex);
Steve French789e6662011-08-09 18:44:44 +0000418
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100419 if (rc == 0)
420 return 0;
Jeff Laytona6827c12011-01-11 07:24:21 -0500421
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700422 cifs_delete_mid(mid);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400423 add_credits(server, 1, optype);
Jeff Laytona6827c12011-01-11 07:24:21 -0500424 wake_up(&server->request_q);
425 return rc;
426}
427
428/*
Steve French133672e2007-11-13 22:41:37 +0000429 *
430 * Send an SMB Request. No response info (other than return code)
431 * needs to be parsed.
432 *
433 * flags indicate the type of request buffer and how long to wait
434 * and whether to log NT STATUS code (error) before mapping it to POSIX error
435 *
436 */
437int
Steve French96daf2b2011-05-27 04:34:02 +0000438SendReceiveNoRsp(const unsigned int xid, struct cifs_ses *ses,
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400439 char *in_buf, int flags)
Steve French133672e2007-11-13 22:41:37 +0000440{
441 int rc;
442 struct kvec iov[1];
443 int resp_buf_type;
444
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400445 iov[0].iov_base = in_buf;
446 iov[0].iov_len = get_rfc1002_length(in_buf) + 4;
Steve French133672e2007-11-13 22:41:37 +0000447 flags |= CIFS_NO_RESP;
448 rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags);
Joe Perchesb6b38f72010-04-21 03:50:45 +0000449 cFYI(DBG2, "SendRcvNoRsp flags %d rc %d", flags, rc);
Steve French90c81e02008-02-12 20:32:36 +0000450
Steve French133672e2007-11-13 22:41:37 +0000451 return rc;
452}
453
Jeff Layton053d5032011-01-11 07:24:02 -0500454static int
Jeff Layton3c1105d2011-05-22 07:09:13 -0400455cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
Jeff Layton053d5032011-01-11 07:24:02 -0500456{
457 int rc = 0;
458
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400459 cFYI(1, "%s: cmd=%d mid=%llu state=%d", __func__,
460 le16_to_cpu(mid->command), mid->mid, mid->mid_state);
Jeff Layton053d5032011-01-11 07:24:02 -0500461
Jeff Layton74dd92a2011-01-11 07:24:02 -0500462 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400463 switch (mid->mid_state) {
Jeff Layton74dd92a2011-01-11 07:24:02 -0500464 case MID_RESPONSE_RECEIVED:
Jeff Layton053d5032011-01-11 07:24:02 -0500465 spin_unlock(&GlobalMid_Lock);
466 return rc;
Jeff Layton74dd92a2011-01-11 07:24:02 -0500467 case MID_RETRY_NEEDED:
468 rc = -EAGAIN;
469 break;
Jeff Layton71823ba2011-02-10 08:03:50 -0500470 case MID_RESPONSE_MALFORMED:
471 rc = -EIO;
472 break;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400473 case MID_SHUTDOWN:
474 rc = -EHOSTDOWN;
475 break;
Jeff Layton74dd92a2011-01-11 07:24:02 -0500476 default:
Jeff Layton3c1105d2011-05-22 07:09:13 -0400477 list_del_init(&mid->qhead);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400478 cERROR(1, "%s: invalid mid state mid=%llu state=%d", __func__,
479 mid->mid, mid->mid_state);
Jeff Layton74dd92a2011-01-11 07:24:02 -0500480 rc = -EIO;
Jeff Layton053d5032011-01-11 07:24:02 -0500481 }
482 spin_unlock(&GlobalMid_Lock);
483
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500484 DeleteMidQEntry(mid);
Jeff Layton053d5032011-01-11 07:24:02 -0500485 return rc;
486}
487
Jeff Layton121b0462012-05-15 12:21:10 -0400488static inline int
489send_cancel(struct TCP_Server_Info *server, void *buf, struct mid_q_entry *mid)
Jeff Layton76dcc262011-01-11 07:24:24 -0500490{
Jeff Layton121b0462012-05-15 12:21:10 -0400491 return server->ops->send_cancel ?
492 server->ops->send_cancel(server, buf, mid) : 0;
Jeff Layton76dcc262011-01-11 07:24:24 -0500493}
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495int
Jeff Layton2c8f9812011-05-19 16:22:52 -0400496cifs_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
497 bool log_error)
498{
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400499 unsigned int len = get_rfc1002_length(mid->resp_buf) + 4;
Jeff Layton826a95e2011-10-11 06:41:32 -0400500
501 dump_smb(mid->resp_buf, min_t(u32, 92, len));
Jeff Layton2c8f9812011-05-19 16:22:52 -0400502
503 /* convert the length into a more usable form */
Steve French96daf2b2011-05-27 04:34:02 +0000504 if (server->sec_mode & (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED)) {
Jeff Layton826a95e2011-10-11 06:41:32 -0400505 struct kvec iov;
Steve French985e4ff02012-08-03 09:42:45 -0500506 int rc = 0;
Jeff Layton826a95e2011-10-11 06:41:32 -0400507
508 iov.iov_base = mid->resp_buf;
509 iov.iov_len = len;
Jeff Layton2c8f9812011-05-19 16:22:52 -0400510 /* FIXME: add code to kill session */
Steve French985e4ff02012-08-03 09:42:45 -0500511 rc = cifs_verify_signature(&iov, 1, server,
512 mid->sequence_number + 1);
513 if (rc)
514 cERROR(1, "SMB signature verification returned error = "
515 "%d", rc);
Jeff Layton2c8f9812011-05-19 16:22:52 -0400516 }
517
518 /* BB special case reconnect tid and uid here? */
519 return map_smb_to_linux_error(mid->resp_buf, log_error);
520}
521
Pavel Shilovsky082d0642012-05-17 12:18:21 +0400522int
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400523cifs_setup_request(struct cifs_ses *ses, struct kvec *iov,
524 unsigned int nvec, struct mid_q_entry **ret_mid)
525{
526 int rc;
527 struct smb_hdr *hdr = (struct smb_hdr *)iov[0].iov_base;
528 struct mid_q_entry *mid;
529
530 rc = allocate_mid(ses, hdr, &mid);
531 if (rc)
532 return rc;
Jeff Layton762a4202012-07-23 13:28:38 -0400533 rc = cifs_sign_smbv(iov, nvec, ses->server, &mid->sequence_number);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400534 if (rc)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700535 cifs_delete_mid(mid);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400536 *ret_mid = mid;
537 return rc;
538}
539
Jeff Layton2c8f9812011-05-19 16:22:52 -0400540int
Steve French96daf2b2011-05-27 04:34:02 +0000541SendReceive2(const unsigned int xid, struct cifs_ses *ses,
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400542 struct kvec *iov, int n_vec, int *resp_buf_type /* ret */,
Steve French133672e2007-11-13 22:41:37 +0000543 const int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 int rc = 0;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400546 int timeout, optype;
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500547 struct mid_q_entry *midQ;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400548 char *buf = iov[0].iov_base;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400549 unsigned int credits = 1;
Steve French50c2f752007-07-13 00:33:32 +0000550
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400551 timeout = flags & CIFS_TIMEOUT_MASK;
552 optype = flags & CIFS_OP_MASK;
Steve French133672e2007-11-13 22:41:37 +0000553
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400554 *resp_buf_type = CIFS_NO_BUFFER; /* no response buf yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Steve French4b8f9302006-02-26 16:41:18 +0000556 if ((ses == NULL) || (ses->server == NULL)) {
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400557 cifs_small_buf_release(buf);
Joe Perchesb6b38f72010-04-21 03:50:45 +0000558 cERROR(1, "Null session");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return -EIO;
560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Steve French79a58d12007-07-06 22:44:50 +0000562 if (ses->server->tcpStatus == CifsExiting) {
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400563 cifs_small_buf_release(buf);
Steve French31ca3bc2005-04-28 22:41:11 -0700564 return -ENOENT;
Steve French4b8f9302006-02-26 16:41:18 +0000565 }
Steve French31ca3bc2005-04-28 22:41:11 -0700566
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400567 /*
568 * Ensure that we do not send more than 50 overlapping requests
569 * to the same server. We may make this configurable later or
570 * use ses->maxReq.
571 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400573 rc = wait_for_free_request(ses->server, timeout, optype);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000574 if (rc) {
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400575 cifs_small_buf_release(buf);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000576 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000578
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400579 /*
580 * Make sure that we sign in the same order that we send on this socket
581 * and avoid races inside tcp sendmsg code that could cause corruption
582 * of smb data.
583 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Jeff Layton72ca5452008-12-01 07:09:36 -0500585 mutex_lock(&ses->server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Pavel Shilovsky082d0642012-05-17 12:18:21 +0400587 rc = ses->server->ops->setup_request(ses, iov, n_vec, &midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000588 if (rc) {
Jeff Layton72ca5452008-12-01 07:09:36 -0500589 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400590 cifs_small_buf_release(buf);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000591 /* Update # of requests on wire to server */
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400592 add_credits(ses->server, 1, optype);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000593 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400596 midQ->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +0000597 cifs_in_send_inc(ses->server);
Jeff Layton0496e022008-12-30 12:39:16 -0500598 rc = smb_sendv(ses->server, iov, n_vec);
Steve French789e6662011-08-09 18:44:44 +0000599 cifs_in_send_dec(ses->server);
600 cifs_save_when_sent(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000601
Jeff Layton72ca5452008-12-01 07:09:36 -0500602 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000603
Jeff Layton2db7c582011-01-28 07:08:28 -0500604 if (rc < 0) {
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400605 cifs_small_buf_release(buf);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000606 goto out;
Jeff Layton2db7c582011-01-28 07:08:28 -0500607 }
Steve French4b8f9302006-02-26 16:41:18 +0000608
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400609 if (timeout == CIFS_ASYNC_OP) {
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400610 cifs_small_buf_release(buf);
Steve French133672e2007-11-13 22:41:37 +0000611 goto out;
Jeff Layton2db7c582011-01-28 07:08:28 -0500612 }
Jeff Layton0ade6402011-01-11 07:24:02 -0500613
614 rc = wait_for_response(ses->server, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -0500615 if (rc != 0) {
Jeff Layton121b0462012-05-15 12:21:10 -0400616 send_cancel(ses->server, buf, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -0500617 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400618 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
Jeff Layton1be912d2011-01-28 07:08:28 -0500619 midQ->callback = DeleteMidQEntry;
620 spin_unlock(&GlobalMid_Lock);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400621 cifs_small_buf_release(buf);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400622 add_credits(ses->server, 1, optype);
Jeff Layton1be912d2011-01-28 07:08:28 -0500623 return rc;
624 }
625 spin_unlock(&GlobalMid_Lock);
626 }
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500627
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400628 cifs_small_buf_release(buf);
Jeff Layton2db7c582011-01-28 07:08:28 -0500629
Jeff Layton3c1105d2011-05-22 07:09:13 -0400630 rc = cifs_sync_mid_result(midQ, ses->server);
Jeff Layton053d5032011-01-11 07:24:02 -0500631 if (rc != 0) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400632 add_credits(ses->server, 1, optype);
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500633 return rc;
634 }
Steve French50c2f752007-07-13 00:33:32 +0000635
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400636 if (!midQ->resp_buf || midQ->mid_state != MID_RESPONSE_RECEIVED) {
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500637 rc = -EIO;
Jeff Layton2c8f9812011-05-19 16:22:52 -0400638 cFYI(1, "Bad MID state?");
Steve French2b2bdfb2008-12-11 17:26:54 +0000639 goto out;
640 }
Steve French84afc292005-12-02 13:32:45 -0800641
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400642 buf = (char *)midQ->resp_buf;
643 iov[0].iov_base = buf;
644 iov[0].iov_len = get_rfc1002_length(buf) + 4;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400645 if (midQ->large_buf)
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400646 *resp_buf_type = CIFS_LARGE_BUFFER;
Jeff Layton2c8f9812011-05-19 16:22:52 -0400647 else
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400648 *resp_buf_type = CIFS_SMALL_BUFFER;
649
650 credits = ses->server->ops->get_credits(midQ);
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500651
Pavel Shilovsky082d0642012-05-17 12:18:21 +0400652 rc = ses->server->ops->check_receive(midQ, ses->server,
653 flags & CIFS_LOG_ERROR);
Steve French2b2bdfb2008-12-11 17:26:54 +0000654
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700655 /* mark it so buf will not be freed by cifs_delete_mid */
Jeff Layton2c8f9812011-05-19 16:22:52 -0400656 if ((flags & CIFS_NO_RESP) == 0)
657 midQ->resp_buf = NULL;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000658out:
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700659 cifs_delete_mid(midQ);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400660 add_credits(ses->server, credits, optype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 return rc;
663}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665int
Steve French96daf2b2011-05-27 04:34:02 +0000666SendReceive(const unsigned int xid, struct cifs_ses *ses,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400668 int *pbytes_returned, const int timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
670 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 struct mid_q_entry *midQ;
672
673 if (ses == NULL) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000674 cERROR(1, "Null smb session");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return -EIO;
676 }
Steve French79a58d12007-07-06 22:44:50 +0000677 if (ses->server == NULL) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000678 cERROR(1, "Null tcp session");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 return -EIO;
680 }
681
Steve French79a58d12007-07-06 22:44:50 +0000682 if (ses->server->tcpStatus == CifsExiting)
Steve French31ca3bc2005-04-28 22:41:11 -0700683 return -ENOENT;
684
Steve French79a58d12007-07-06 22:44:50 +0000685 /* Ensure that we do not send more than 50 overlapping requests
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 to the same server. We may make this configurable later or
687 use ses->maxReq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000689 if (be32_to_cpu(in_buf->smb_buf_length) > CIFSMaxBufSize +
690 MAX_CIFS_HDR_SIZE - 4) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000691 cERROR(1, "Illegal length, greater than maximum frame, %d",
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000692 be32_to_cpu(in_buf->smb_buf_length));
Volker Lendecke6d9c6d52008-12-08 20:50:24 +0000693 return -EIO;
694 }
695
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400696 rc = wait_for_free_request(ses->server, timeout, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000697 if (rc)
698 return rc;
699
Steve French79a58d12007-07-06 22:44:50 +0000700 /* make sure that we sign in the same order that we send on this socket
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 and avoid races inside tcp sendmsg code that could cause corruption
702 of smb data */
703
Jeff Layton72ca5452008-12-01 07:09:36 -0500704 mutex_lock(&ses->server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000706 rc = allocate_mid(ses, in_buf, &midQ);
707 if (rc) {
Jeff Layton72ca5452008-12-01 07:09:36 -0500708 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000709 /* Update # of requests on wire to server */
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400710 add_credits(ses->server, 1, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000711 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
713
Steve Frenchad009ac2005-04-28 22:41:05 -0700714 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
Volker Lendecke829049c2008-12-06 16:00:53 +0100715 if (rc) {
716 mutex_unlock(&ses->server->srv_mutex);
717 goto out;
718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400720 midQ->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +0000721
722 cifs_in_send_inc(ses->server);
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000723 rc = smb_send(ses->server, in_buf, be32_to_cpu(in_buf->smb_buf_length));
Steve French789e6662011-08-09 18:44:44 +0000724 cifs_in_send_dec(ses->server);
725 cifs_save_when_sent(midQ);
Jeff Layton72ca5452008-12-01 07:09:36 -0500726 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000727
Steve French79a58d12007-07-06 22:44:50 +0000728 if (rc < 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000729 goto out;
730
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400731 if (timeout == CIFS_ASYNC_OP)
Steve French133672e2007-11-13 22:41:37 +0000732 goto out;
Jeff Layton0ade6402011-01-11 07:24:02 -0500733
734 rc = wait_for_response(ses->server, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -0500735 if (rc != 0) {
Jeff Layton121b0462012-05-15 12:21:10 -0400736 send_cancel(ses->server, in_buf, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -0500737 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400738 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
Jeff Layton1be912d2011-01-28 07:08:28 -0500739 /* no longer considered to be "in-flight" */
740 midQ->callback = DeleteMidQEntry;
741 spin_unlock(&GlobalMid_Lock);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400742 add_credits(ses->server, 1, 0);
Jeff Layton1be912d2011-01-28 07:08:28 -0500743 return rc;
744 }
745 spin_unlock(&GlobalMid_Lock);
746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Jeff Layton3c1105d2011-05-22 07:09:13 -0400748 rc = cifs_sync_mid_result(midQ, ses->server);
Jeff Layton053d5032011-01-11 07:24:02 -0500749 if (rc != 0) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400750 add_credits(ses->server, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 return rc;
752 }
Steve French50c2f752007-07-13 00:33:32 +0000753
Jeff Layton2c8f9812011-05-19 16:22:52 -0400754 if (!midQ->resp_buf || !out_buf ||
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400755 midQ->mid_state != MID_RESPONSE_RECEIVED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 rc = -EIO;
Jeff Layton2c8f9812011-05-19 16:22:52 -0400757 cERROR(1, "Bad MID state?");
Steve French2b2bdfb2008-12-11 17:26:54 +0000758 goto out;
759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400761 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
Jeff Layton2c8f9812011-05-19 16:22:52 -0400762 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
763 rc = cifs_check_receive(midQ, ses->server, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000764out:
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700765 cifs_delete_mid(midQ);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400766 add_credits(ses->server, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 return rc;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000769}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000771/* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
772 blocking lock to return. */
773
774static int
Steve French96daf2b2011-05-27 04:34:02 +0000775send_lock_cancel(const unsigned int xid, struct cifs_tcon *tcon,
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000776 struct smb_hdr *in_buf,
777 struct smb_hdr *out_buf)
778{
779 int bytes_returned;
Steve French96daf2b2011-05-27 04:34:02 +0000780 struct cifs_ses *ses = tcon->ses;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000781 LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
782
783 /* We just modify the current in_buf to change
784 the type of lock from LOCKING_ANDX_SHARED_LOCK
785 or LOCKING_ANDX_EXCLUSIVE_LOCK to
786 LOCKING_ANDX_CANCEL_LOCK. */
787
788 pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
789 pSMB->Timeout = 0;
Pavel Shilovsky88257362012-05-23 14:01:59 +0400790 pSMB->hdr.Mid = get_next_mid(ses->server);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000791
792 return SendReceive(xid, ses, in_buf, out_buf,
Jeff Layton77499812011-01-11 07:24:23 -0500793 &bytes_returned, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000794}
795
796int
Steve French96daf2b2011-05-27 04:34:02 +0000797SendReceiveBlockingLock(const unsigned int xid, struct cifs_tcon *tcon,
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000798 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
799 int *pbytes_returned)
800{
801 int rc = 0;
802 int rstart = 0;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000803 struct mid_q_entry *midQ;
Steve French96daf2b2011-05-27 04:34:02 +0000804 struct cifs_ses *ses;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000805
806 if (tcon == NULL || tcon->ses == NULL) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000807 cERROR(1, "Null smb session");
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000808 return -EIO;
809 }
810 ses = tcon->ses;
811
Steve French79a58d12007-07-06 22:44:50 +0000812 if (ses->server == NULL) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000813 cERROR(1, "Null tcp session");
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000814 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
816
Steve French79a58d12007-07-06 22:44:50 +0000817 if (ses->server->tcpStatus == CifsExiting)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000818 return -ENOENT;
819
Steve French79a58d12007-07-06 22:44:50 +0000820 /* Ensure that we do not send more than 50 overlapping requests
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000821 to the same server. We may make this configurable later or
822 use ses->maxReq */
823
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000824 if (be32_to_cpu(in_buf->smb_buf_length) > CIFSMaxBufSize +
825 MAX_CIFS_HDR_SIZE - 4) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000826 cERROR(1, "Illegal length, greater than maximum frame, %d",
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000827 be32_to_cpu(in_buf->smb_buf_length));
Volker Lendecke6d9c6d52008-12-08 20:50:24 +0000828 return -EIO;
829 }
830
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400831 rc = wait_for_free_request(ses->server, CIFS_BLOCKING_OP, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000832 if (rc)
833 return rc;
834
Steve French79a58d12007-07-06 22:44:50 +0000835 /* make sure that we sign in the same order that we send on this socket
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000836 and avoid races inside tcp sendmsg code that could cause corruption
837 of smb data */
838
Jeff Layton72ca5452008-12-01 07:09:36 -0500839 mutex_lock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000840
841 rc = allocate_mid(ses, in_buf, &midQ);
842 if (rc) {
Jeff Layton72ca5452008-12-01 07:09:36 -0500843 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000844 return rc;
845 }
846
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000847 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
Volker Lendecke829049c2008-12-06 16:00:53 +0100848 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700849 cifs_delete_mid(midQ);
Volker Lendecke829049c2008-12-06 16:00:53 +0100850 mutex_unlock(&ses->server->srv_mutex);
851 return rc;
852 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000853
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400854 midQ->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +0000855 cifs_in_send_inc(ses->server);
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000856 rc = smb_send(ses->server, in_buf, be32_to_cpu(in_buf->smb_buf_length));
Steve French789e6662011-08-09 18:44:44 +0000857 cifs_in_send_dec(ses->server);
858 cifs_save_when_sent(midQ);
Jeff Layton72ca5452008-12-01 07:09:36 -0500859 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000860
Steve French79a58d12007-07-06 22:44:50 +0000861 if (rc < 0) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700862 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000863 return rc;
864 }
865
866 /* Wait for a reply - allow signals to interrupt. */
867 rc = wait_event_interruptible(ses->server->response_q,
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400868 (!(midQ->mid_state == MID_REQUEST_SUBMITTED)) ||
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000869 ((ses->server->tcpStatus != CifsGood) &&
870 (ses->server->tcpStatus != CifsNew)));
871
872 /* Were we interrupted by a signal ? */
873 if ((rc == -ERESTARTSYS) &&
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400874 (midQ->mid_state == MID_REQUEST_SUBMITTED) &&
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000875 ((ses->server->tcpStatus == CifsGood) ||
876 (ses->server->tcpStatus == CifsNew))) {
877
878 if (in_buf->Command == SMB_COM_TRANSACTION2) {
879 /* POSIX lock. We send a NT_CANCEL SMB to cause the
880 blocking lock to return. */
Jeff Layton121b0462012-05-15 12:21:10 -0400881 rc = send_cancel(ses->server, in_buf, midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000882 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700883 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000884 return rc;
885 }
886 } else {
887 /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
888 to cause the blocking lock to return. */
889
890 rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
891
892 /* If we get -ENOLCK back the lock may have
893 already been removed. Don't exit in this case. */
894 if (rc && rc != -ENOLCK) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700895 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000896 return rc;
897 }
898 }
899
Jeff Layton1be912d2011-01-28 07:08:28 -0500900 rc = wait_for_response(ses->server, midQ);
901 if (rc) {
Jeff Layton121b0462012-05-15 12:21:10 -0400902 send_cancel(ses->server, in_buf, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -0500903 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400904 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
Jeff Layton1be912d2011-01-28 07:08:28 -0500905 /* no longer considered to be "in-flight" */
906 midQ->callback = DeleteMidQEntry;
907 spin_unlock(&GlobalMid_Lock);
908 return rc;
909 }
910 spin_unlock(&GlobalMid_Lock);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000911 }
Jeff Layton1be912d2011-01-28 07:08:28 -0500912
913 /* We got the response - restart system call. */
914 rstart = 1;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000915 }
916
Jeff Layton3c1105d2011-05-22 07:09:13 -0400917 rc = cifs_sync_mid_result(midQ, ses->server);
Jeff Layton053d5032011-01-11 07:24:02 -0500918 if (rc != 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000919 return rc;
Steve French50c2f752007-07-13 00:33:32 +0000920
Volker Lendecke17c8bfe2008-12-06 16:38:19 +0100921 /* rcvd frame is ok */
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400922 if (out_buf == NULL || midQ->mid_state != MID_RESPONSE_RECEIVED) {
Volker Lendecke17c8bfe2008-12-06 16:38:19 +0100923 rc = -EIO;
Joe Perchesb6b38f72010-04-21 03:50:45 +0000924 cERROR(1, "Bad MID state?");
Volker Lendecke698e96a2008-12-06 16:39:31 +0100925 goto out;
Volker Lendecke17c8bfe2008-12-06 16:38:19 +0100926 }
927
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400928 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
Jeff Layton2c8f9812011-05-19 16:22:52 -0400929 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
930 rc = cifs_check_receive(midQ, ses->server, 0);
Volker Lendecke17c8bfe2008-12-06 16:38:19 +0100931out:
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700932 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000933 if (rstart && rc == -EACCES)
934 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return rc;
936}