blob: bc9ccddad9377876e4736740dfb0c407d9371b73 [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 Laytonbf5ea0e2012-09-18 16:20:34 -0700507 struct smb_rqst rqst = { .rq_iov = &iov,
508 .rq_nvec = 1 };
Jeff Layton826a95e2011-10-11 06:41:32 -0400509
510 iov.iov_base = mid->resp_buf;
511 iov.iov_len = len;
Jeff Layton2c8f9812011-05-19 16:22:52 -0400512 /* FIXME: add code to kill session */
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700513 rc = cifs_verify_signature(&rqst, server,
Steve French985e4ff02012-08-03 09:42:45 -0500514 mid->sequence_number + 1);
515 if (rc)
516 cERROR(1, "SMB signature verification returned error = "
517 "%d", rc);
Jeff Layton2c8f9812011-05-19 16:22:52 -0400518 }
519
520 /* BB special case reconnect tid and uid here? */
521 return map_smb_to_linux_error(mid->resp_buf, log_error);
522}
523
Pavel Shilovsky082d0642012-05-17 12:18:21 +0400524int
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400525cifs_setup_request(struct cifs_ses *ses, struct kvec *iov,
526 unsigned int nvec, struct mid_q_entry **ret_mid)
527{
528 int rc;
529 struct smb_hdr *hdr = (struct smb_hdr *)iov[0].iov_base;
530 struct mid_q_entry *mid;
531
532 rc = allocate_mid(ses, hdr, &mid);
533 if (rc)
534 return rc;
Jeff Layton762a4202012-07-23 13:28:38 -0400535 rc = cifs_sign_smbv(iov, nvec, ses->server, &mid->sequence_number);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400536 if (rc)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700537 cifs_delete_mid(mid);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400538 *ret_mid = mid;
539 return rc;
540}
541
Jeff Layton2c8f9812011-05-19 16:22:52 -0400542int
Steve French96daf2b2011-05-27 04:34:02 +0000543SendReceive2(const unsigned int xid, struct cifs_ses *ses,
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400544 struct kvec *iov, int n_vec, int *resp_buf_type /* ret */,
Steve French133672e2007-11-13 22:41:37 +0000545 const int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
547 int rc = 0;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400548 int timeout, optype;
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500549 struct mid_q_entry *midQ;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400550 char *buf = iov[0].iov_base;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400551 unsigned int credits = 1;
Steve French50c2f752007-07-13 00:33:32 +0000552
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400553 timeout = flags & CIFS_TIMEOUT_MASK;
554 optype = flags & CIFS_OP_MASK;
Steve French133672e2007-11-13 22:41:37 +0000555
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400556 *resp_buf_type = CIFS_NO_BUFFER; /* no response buf yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Steve French4b8f9302006-02-26 16:41:18 +0000558 if ((ses == NULL) || (ses->server == NULL)) {
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400559 cifs_small_buf_release(buf);
Joe Perchesb6b38f72010-04-21 03:50:45 +0000560 cERROR(1, "Null session");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return -EIO;
562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Steve French79a58d12007-07-06 22:44:50 +0000564 if (ses->server->tcpStatus == CifsExiting) {
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400565 cifs_small_buf_release(buf);
Steve French31ca3bc2005-04-28 22:41:11 -0700566 return -ENOENT;
Steve French4b8f9302006-02-26 16:41:18 +0000567 }
Steve French31ca3bc2005-04-28 22:41:11 -0700568
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400569 /*
570 * Ensure that we do not send more than 50 overlapping requests
571 * to the same server. We may make this configurable later or
572 * use ses->maxReq.
573 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400575 rc = wait_for_free_request(ses->server, timeout, optype);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000576 if (rc) {
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400577 cifs_small_buf_release(buf);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000578 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000580
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400581 /*
582 * Make sure that we sign in the same order that we send on this socket
583 * and avoid races inside tcp sendmsg code that could cause corruption
584 * of smb data.
585 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Jeff Layton72ca5452008-12-01 07:09:36 -0500587 mutex_lock(&ses->server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Pavel Shilovsky082d0642012-05-17 12:18:21 +0400589 rc = ses->server->ops->setup_request(ses, iov, n_vec, &midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000590 if (rc) {
Jeff Layton72ca5452008-12-01 07:09:36 -0500591 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400592 cifs_small_buf_release(buf);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000593 /* Update # of requests on wire to server */
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400594 add_credits(ses->server, 1, optype);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000595 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400598 midQ->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +0000599 cifs_in_send_inc(ses->server);
Jeff Layton0496e022008-12-30 12:39:16 -0500600 rc = smb_sendv(ses->server, iov, n_vec);
Steve French789e6662011-08-09 18:44:44 +0000601 cifs_in_send_dec(ses->server);
602 cifs_save_when_sent(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000603
Jeff Layton72ca5452008-12-01 07:09:36 -0500604 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000605
Jeff Layton2db7c582011-01-28 07:08:28 -0500606 if (rc < 0) {
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400607 cifs_small_buf_release(buf);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000608 goto out;
Jeff Layton2db7c582011-01-28 07:08:28 -0500609 }
Steve French4b8f9302006-02-26 16:41:18 +0000610
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400611 if (timeout == CIFS_ASYNC_OP) {
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400612 cifs_small_buf_release(buf);
Steve French133672e2007-11-13 22:41:37 +0000613 goto out;
Jeff Layton2db7c582011-01-28 07:08:28 -0500614 }
Jeff Layton0ade6402011-01-11 07:24:02 -0500615
616 rc = wait_for_response(ses->server, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -0500617 if (rc != 0) {
Jeff Layton121b0462012-05-15 12:21:10 -0400618 send_cancel(ses->server, buf, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -0500619 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400620 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
Jeff Layton1be912d2011-01-28 07:08:28 -0500621 midQ->callback = DeleteMidQEntry;
622 spin_unlock(&GlobalMid_Lock);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400623 cifs_small_buf_release(buf);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400624 add_credits(ses->server, 1, optype);
Jeff Layton1be912d2011-01-28 07:08:28 -0500625 return rc;
626 }
627 spin_unlock(&GlobalMid_Lock);
628 }
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500629
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400630 cifs_small_buf_release(buf);
Jeff Layton2db7c582011-01-28 07:08:28 -0500631
Jeff Layton3c1105d2011-05-22 07:09:13 -0400632 rc = cifs_sync_mid_result(midQ, ses->server);
Jeff Layton053d5032011-01-11 07:24:02 -0500633 if (rc != 0) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400634 add_credits(ses->server, 1, optype);
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500635 return rc;
636 }
Steve French50c2f752007-07-13 00:33:32 +0000637
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400638 if (!midQ->resp_buf || midQ->mid_state != MID_RESPONSE_RECEIVED) {
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500639 rc = -EIO;
Jeff Layton2c8f9812011-05-19 16:22:52 -0400640 cFYI(1, "Bad MID state?");
Steve French2b2bdfb2008-12-11 17:26:54 +0000641 goto out;
642 }
Steve French84afc292005-12-02 13:32:45 -0800643
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400644 buf = (char *)midQ->resp_buf;
645 iov[0].iov_base = buf;
646 iov[0].iov_len = get_rfc1002_length(buf) + 4;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400647 if (midQ->large_buf)
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400648 *resp_buf_type = CIFS_LARGE_BUFFER;
Jeff Layton2c8f9812011-05-19 16:22:52 -0400649 else
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400650 *resp_buf_type = CIFS_SMALL_BUFFER;
651
652 credits = ses->server->ops->get_credits(midQ);
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500653
Pavel Shilovsky082d0642012-05-17 12:18:21 +0400654 rc = ses->server->ops->check_receive(midQ, ses->server,
655 flags & CIFS_LOG_ERROR);
Steve French2b2bdfb2008-12-11 17:26:54 +0000656
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700657 /* mark it so buf will not be freed by cifs_delete_mid */
Jeff Layton2c8f9812011-05-19 16:22:52 -0400658 if ((flags & CIFS_NO_RESP) == 0)
659 midQ->resp_buf = NULL;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000660out:
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700661 cifs_delete_mid(midQ);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400662 add_credits(ses->server, credits, optype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 return rc;
665}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667int
Steve French96daf2b2011-05-27 04:34:02 +0000668SendReceive(const unsigned int xid, struct cifs_ses *ses,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400670 int *pbytes_returned, const int timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 struct mid_q_entry *midQ;
674
675 if (ses == NULL) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000676 cERROR(1, "Null smb session");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return -EIO;
678 }
Steve French79a58d12007-07-06 22:44:50 +0000679 if (ses->server == NULL) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000680 cERROR(1, "Null tcp session");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return -EIO;
682 }
683
Steve French79a58d12007-07-06 22:44:50 +0000684 if (ses->server->tcpStatus == CifsExiting)
Steve French31ca3bc2005-04-28 22:41:11 -0700685 return -ENOENT;
686
Steve French79a58d12007-07-06 22:44:50 +0000687 /* Ensure that we do not send more than 50 overlapping requests
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 to the same server. We may make this configurable later or
689 use ses->maxReq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000691 if (be32_to_cpu(in_buf->smb_buf_length) > CIFSMaxBufSize +
692 MAX_CIFS_HDR_SIZE - 4) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000693 cERROR(1, "Illegal length, greater than maximum frame, %d",
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000694 be32_to_cpu(in_buf->smb_buf_length));
Volker Lendecke6d9c6d52008-12-08 20:50:24 +0000695 return -EIO;
696 }
697
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400698 rc = wait_for_free_request(ses->server, timeout, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000699 if (rc)
700 return rc;
701
Steve French79a58d12007-07-06 22:44:50 +0000702 /* make sure that we sign in the same order that we send on this socket
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 and avoid races inside tcp sendmsg code that could cause corruption
704 of smb data */
705
Jeff Layton72ca5452008-12-01 07:09:36 -0500706 mutex_lock(&ses->server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000708 rc = allocate_mid(ses, in_buf, &midQ);
709 if (rc) {
Jeff Layton72ca5452008-12-01 07:09:36 -0500710 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000711 /* Update # of requests on wire to server */
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400712 add_credits(ses->server, 1, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000713 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715
Steve Frenchad009ac2005-04-28 22:41:05 -0700716 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
Volker Lendecke829049c2008-12-06 16:00:53 +0100717 if (rc) {
718 mutex_unlock(&ses->server->srv_mutex);
719 goto out;
720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400722 midQ->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +0000723
724 cifs_in_send_inc(ses->server);
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000725 rc = smb_send(ses->server, in_buf, be32_to_cpu(in_buf->smb_buf_length));
Steve French789e6662011-08-09 18:44:44 +0000726 cifs_in_send_dec(ses->server);
727 cifs_save_when_sent(midQ);
Jeff Layton72ca5452008-12-01 07:09:36 -0500728 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000729
Steve French79a58d12007-07-06 22:44:50 +0000730 if (rc < 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000731 goto out;
732
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400733 if (timeout == CIFS_ASYNC_OP)
Steve French133672e2007-11-13 22:41:37 +0000734 goto out;
Jeff Layton0ade6402011-01-11 07:24:02 -0500735
736 rc = wait_for_response(ses->server, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -0500737 if (rc != 0) {
Jeff Layton121b0462012-05-15 12:21:10 -0400738 send_cancel(ses->server, in_buf, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -0500739 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400740 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
Jeff Layton1be912d2011-01-28 07:08:28 -0500741 /* no longer considered to be "in-flight" */
742 midQ->callback = DeleteMidQEntry;
743 spin_unlock(&GlobalMid_Lock);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400744 add_credits(ses->server, 1, 0);
Jeff Layton1be912d2011-01-28 07:08:28 -0500745 return rc;
746 }
747 spin_unlock(&GlobalMid_Lock);
748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Jeff Layton3c1105d2011-05-22 07:09:13 -0400750 rc = cifs_sync_mid_result(midQ, ses->server);
Jeff Layton053d5032011-01-11 07:24:02 -0500751 if (rc != 0) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400752 add_credits(ses->server, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 return rc;
754 }
Steve French50c2f752007-07-13 00:33:32 +0000755
Jeff Layton2c8f9812011-05-19 16:22:52 -0400756 if (!midQ->resp_buf || !out_buf ||
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400757 midQ->mid_state != MID_RESPONSE_RECEIVED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 rc = -EIO;
Jeff Layton2c8f9812011-05-19 16:22:52 -0400759 cERROR(1, "Bad MID state?");
Steve French2b2bdfb2008-12-11 17:26:54 +0000760 goto out;
761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400763 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
Jeff Layton2c8f9812011-05-19 16:22:52 -0400764 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
765 rc = cifs_check_receive(midQ, ses->server, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000766out:
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700767 cifs_delete_mid(midQ);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400768 add_credits(ses->server, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 return rc;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000771}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000773/* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
774 blocking lock to return. */
775
776static int
Steve French96daf2b2011-05-27 04:34:02 +0000777send_lock_cancel(const unsigned int xid, struct cifs_tcon *tcon,
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000778 struct smb_hdr *in_buf,
779 struct smb_hdr *out_buf)
780{
781 int bytes_returned;
Steve French96daf2b2011-05-27 04:34:02 +0000782 struct cifs_ses *ses = tcon->ses;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000783 LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
784
785 /* We just modify the current in_buf to change
786 the type of lock from LOCKING_ANDX_SHARED_LOCK
787 or LOCKING_ANDX_EXCLUSIVE_LOCK to
788 LOCKING_ANDX_CANCEL_LOCK. */
789
790 pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
791 pSMB->Timeout = 0;
Pavel Shilovsky88257362012-05-23 14:01:59 +0400792 pSMB->hdr.Mid = get_next_mid(ses->server);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000793
794 return SendReceive(xid, ses, in_buf, out_buf,
Jeff Layton77499812011-01-11 07:24:23 -0500795 &bytes_returned, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000796}
797
798int
Steve French96daf2b2011-05-27 04:34:02 +0000799SendReceiveBlockingLock(const unsigned int xid, struct cifs_tcon *tcon,
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000800 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
801 int *pbytes_returned)
802{
803 int rc = 0;
804 int rstart = 0;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000805 struct mid_q_entry *midQ;
Steve French96daf2b2011-05-27 04:34:02 +0000806 struct cifs_ses *ses;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000807
808 if (tcon == NULL || tcon->ses == NULL) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000809 cERROR(1, "Null smb session");
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000810 return -EIO;
811 }
812 ses = tcon->ses;
813
Steve French79a58d12007-07-06 22:44:50 +0000814 if (ses->server == NULL) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000815 cERROR(1, "Null tcp session");
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000816 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818
Steve French79a58d12007-07-06 22:44:50 +0000819 if (ses->server->tcpStatus == CifsExiting)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000820 return -ENOENT;
821
Steve French79a58d12007-07-06 22:44:50 +0000822 /* Ensure that we do not send more than 50 overlapping requests
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000823 to the same server. We may make this configurable later or
824 use ses->maxReq */
825
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000826 if (be32_to_cpu(in_buf->smb_buf_length) > CIFSMaxBufSize +
827 MAX_CIFS_HDR_SIZE - 4) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000828 cERROR(1, "Illegal length, greater than maximum frame, %d",
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000829 be32_to_cpu(in_buf->smb_buf_length));
Volker Lendecke6d9c6d52008-12-08 20:50:24 +0000830 return -EIO;
831 }
832
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400833 rc = wait_for_free_request(ses->server, CIFS_BLOCKING_OP, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000834 if (rc)
835 return rc;
836
Steve French79a58d12007-07-06 22:44:50 +0000837 /* make sure that we sign in the same order that we send on this socket
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000838 and avoid races inside tcp sendmsg code that could cause corruption
839 of smb data */
840
Jeff Layton72ca5452008-12-01 07:09:36 -0500841 mutex_lock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000842
843 rc = allocate_mid(ses, in_buf, &midQ);
844 if (rc) {
Jeff Layton72ca5452008-12-01 07:09:36 -0500845 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000846 return rc;
847 }
848
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000849 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
Volker Lendecke829049c2008-12-06 16:00:53 +0100850 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700851 cifs_delete_mid(midQ);
Volker Lendecke829049c2008-12-06 16:00:53 +0100852 mutex_unlock(&ses->server->srv_mutex);
853 return rc;
854 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000855
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400856 midQ->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +0000857 cifs_in_send_inc(ses->server);
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000858 rc = smb_send(ses->server, in_buf, be32_to_cpu(in_buf->smb_buf_length));
Steve French789e6662011-08-09 18:44:44 +0000859 cifs_in_send_dec(ses->server);
860 cifs_save_when_sent(midQ);
Jeff Layton72ca5452008-12-01 07:09:36 -0500861 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000862
Steve French79a58d12007-07-06 22:44:50 +0000863 if (rc < 0) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700864 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000865 return rc;
866 }
867
868 /* Wait for a reply - allow signals to interrupt. */
869 rc = wait_event_interruptible(ses->server->response_q,
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400870 (!(midQ->mid_state == MID_REQUEST_SUBMITTED)) ||
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000871 ((ses->server->tcpStatus != CifsGood) &&
872 (ses->server->tcpStatus != CifsNew)));
873
874 /* Were we interrupted by a signal ? */
875 if ((rc == -ERESTARTSYS) &&
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400876 (midQ->mid_state == MID_REQUEST_SUBMITTED) &&
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000877 ((ses->server->tcpStatus == CifsGood) ||
878 (ses->server->tcpStatus == CifsNew))) {
879
880 if (in_buf->Command == SMB_COM_TRANSACTION2) {
881 /* POSIX lock. We send a NT_CANCEL SMB to cause the
882 blocking lock to return. */
Jeff Layton121b0462012-05-15 12:21:10 -0400883 rc = send_cancel(ses->server, in_buf, midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000884 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700885 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000886 return rc;
887 }
888 } else {
889 /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
890 to cause the blocking lock to return. */
891
892 rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
893
894 /* If we get -ENOLCK back the lock may have
895 already been removed. Don't exit in this case. */
896 if (rc && rc != -ENOLCK) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700897 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000898 return rc;
899 }
900 }
901
Jeff Layton1be912d2011-01-28 07:08:28 -0500902 rc = wait_for_response(ses->server, midQ);
903 if (rc) {
Jeff Layton121b0462012-05-15 12:21:10 -0400904 send_cancel(ses->server, in_buf, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -0500905 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400906 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
Jeff Layton1be912d2011-01-28 07:08:28 -0500907 /* no longer considered to be "in-flight" */
908 midQ->callback = DeleteMidQEntry;
909 spin_unlock(&GlobalMid_Lock);
910 return rc;
911 }
912 spin_unlock(&GlobalMid_Lock);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000913 }
Jeff Layton1be912d2011-01-28 07:08:28 -0500914
915 /* We got the response - restart system call. */
916 rstart = 1;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000917 }
918
Jeff Layton3c1105d2011-05-22 07:09:13 -0400919 rc = cifs_sync_mid_result(midQ, ses->server);
Jeff Layton053d5032011-01-11 07:24:02 -0500920 if (rc != 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000921 return rc;
Steve French50c2f752007-07-13 00:33:32 +0000922
Volker Lendecke17c8bfe2008-12-06 16:38:19 +0100923 /* rcvd frame is ok */
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400924 if (out_buf == NULL || midQ->mid_state != MID_RESPONSE_RECEIVED) {
Volker Lendecke17c8bfe2008-12-06 16:38:19 +0100925 rc = -EIO;
Joe Perchesb6b38f72010-04-21 03:50:45 +0000926 cERROR(1, "Bad MID state?");
Volker Lendecke698e96a2008-12-06 16:39:31 +0100927 goto out;
Volker Lendecke17c8bfe2008-12-06 16:38:19 +0100928 }
929
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400930 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
Jeff Layton2c8f9812011-05-19 16:22:52 -0400931 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
932 rc = cifs_check_receive(midQ, ses->server, 0);
Volker Lendecke17c8bfe2008-12-06 16:38:19 +0100933out:
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700934 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000935 if (rstart && rc == -EACCES)
936 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return rc;
938}