blob: bcc02b476f6e5c93d09d9d8037fb66060a84620f [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
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500112static void
113delete_mid(struct mid_q_entry *mid)
114{
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 Shilovsky792af7b2012-03-23 14:28:02 -0400348static int
349cifs_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 Shilovsky792af7b2012-03-23 14:28:02 -0400394 rc = cifs_setup_async_request(server, iov, nvec, &mid);
395 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
Jeff Laytona6827c12011-01-11 07:24:21 -0500422 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;
506
507 iov.iov_base = mid->resp_buf;
508 iov.iov_len = len;
Jeff Layton2c8f9812011-05-19 16:22:52 -0400509 /* FIXME: add code to kill session */
Jeff Layton826a95e2011-10-11 06:41:32 -0400510 if (cifs_verify_signature(&iov, 1, server,
Jeff Layton2c8f9812011-05-19 16:22:52 -0400511 mid->sequence_number + 1) != 0)
512 cERROR(1, "Unexpected SMB signature");
513 }
514
515 /* BB special case reconnect tid and uid here? */
516 return map_smb_to_linux_error(mid->resp_buf, log_error);
517}
518
Pavel Shilovsky082d0642012-05-17 12:18:21 +0400519int
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400520cifs_setup_request(struct cifs_ses *ses, struct kvec *iov,
521 unsigned int nvec, struct mid_q_entry **ret_mid)
522{
523 int rc;
524 struct smb_hdr *hdr = (struct smb_hdr *)iov[0].iov_base;
525 struct mid_q_entry *mid;
526
527 rc = allocate_mid(ses, hdr, &mid);
528 if (rc)
529 return rc;
Jeff Layton762a4202012-07-23 13:28:38 -0400530 rc = cifs_sign_smbv(iov, nvec, ses->server, &mid->sequence_number);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400531 if (rc)
532 delete_mid(mid);
533 *ret_mid = mid;
534 return rc;
535}
536
Jeff Layton2c8f9812011-05-19 16:22:52 -0400537int
Steve French96daf2b2011-05-27 04:34:02 +0000538SendReceive2(const unsigned int xid, struct cifs_ses *ses,
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400539 struct kvec *iov, int n_vec, int *resp_buf_type /* ret */,
Steve French133672e2007-11-13 22:41:37 +0000540 const int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 int rc = 0;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400543 int timeout, optype;
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500544 struct mid_q_entry *midQ;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400545 char *buf = iov[0].iov_base;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400546 unsigned int credits = 1;
Steve French50c2f752007-07-13 00:33:32 +0000547
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400548 timeout = flags & CIFS_TIMEOUT_MASK;
549 optype = flags & CIFS_OP_MASK;
Steve French133672e2007-11-13 22:41:37 +0000550
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400551 *resp_buf_type = CIFS_NO_BUFFER; /* no response buf yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Steve French4b8f9302006-02-26 16:41:18 +0000553 if ((ses == NULL) || (ses->server == NULL)) {
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400554 cifs_small_buf_release(buf);
Joe Perchesb6b38f72010-04-21 03:50:45 +0000555 cERROR(1, "Null session");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return -EIO;
557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Steve French79a58d12007-07-06 22:44:50 +0000559 if (ses->server->tcpStatus == CifsExiting) {
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400560 cifs_small_buf_release(buf);
Steve French31ca3bc2005-04-28 22:41:11 -0700561 return -ENOENT;
Steve French4b8f9302006-02-26 16:41:18 +0000562 }
Steve French31ca3bc2005-04-28 22:41:11 -0700563
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400564 /*
565 * Ensure that we do not send more than 50 overlapping requests
566 * to the same server. We may make this configurable later or
567 * use ses->maxReq.
568 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400570 rc = wait_for_free_request(ses->server, timeout, optype);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000571 if (rc) {
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400572 cifs_small_buf_release(buf);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000573 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000575
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400576 /*
577 * Make sure that we sign in the same order that we send on this socket
578 * and avoid races inside tcp sendmsg code that could cause corruption
579 * of smb data.
580 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Jeff Layton72ca5452008-12-01 07:09:36 -0500582 mutex_lock(&ses->server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Pavel Shilovsky082d0642012-05-17 12:18:21 +0400584 rc = ses->server->ops->setup_request(ses, iov, n_vec, &midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000585 if (rc) {
Jeff Layton72ca5452008-12-01 07:09:36 -0500586 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400587 cifs_small_buf_release(buf);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000588 /* Update # of requests on wire to server */
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400589 add_credits(ses->server, 1, optype);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000590 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400593 midQ->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +0000594 cifs_in_send_inc(ses->server);
Jeff Layton0496e022008-12-30 12:39:16 -0500595 rc = smb_sendv(ses->server, iov, n_vec);
Steve French789e6662011-08-09 18:44:44 +0000596 cifs_in_send_dec(ses->server);
597 cifs_save_when_sent(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000598
Jeff Layton72ca5452008-12-01 07:09:36 -0500599 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000600
Jeff Layton2db7c582011-01-28 07:08:28 -0500601 if (rc < 0) {
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400602 cifs_small_buf_release(buf);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000603 goto out;
Jeff Layton2db7c582011-01-28 07:08:28 -0500604 }
Steve French4b8f9302006-02-26 16:41:18 +0000605
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400606 if (timeout == CIFS_ASYNC_OP) {
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400607 cifs_small_buf_release(buf);
Steve French133672e2007-11-13 22:41:37 +0000608 goto out;
Jeff Layton2db7c582011-01-28 07:08:28 -0500609 }
Jeff Layton0ade6402011-01-11 07:24:02 -0500610
611 rc = wait_for_response(ses->server, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -0500612 if (rc != 0) {
Jeff Layton121b0462012-05-15 12:21:10 -0400613 send_cancel(ses->server, buf, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -0500614 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400615 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
Jeff Layton1be912d2011-01-28 07:08:28 -0500616 midQ->callback = DeleteMidQEntry;
617 spin_unlock(&GlobalMid_Lock);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400618 cifs_small_buf_release(buf);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400619 add_credits(ses->server, 1, optype);
Jeff Layton1be912d2011-01-28 07:08:28 -0500620 return rc;
621 }
622 spin_unlock(&GlobalMid_Lock);
623 }
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500624
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400625 cifs_small_buf_release(buf);
Jeff Layton2db7c582011-01-28 07:08:28 -0500626
Jeff Layton3c1105d2011-05-22 07:09:13 -0400627 rc = cifs_sync_mid_result(midQ, ses->server);
Jeff Layton053d5032011-01-11 07:24:02 -0500628 if (rc != 0) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400629 add_credits(ses->server, 1, optype);
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500630 return rc;
631 }
Steve French50c2f752007-07-13 00:33:32 +0000632
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400633 if (!midQ->resp_buf || midQ->mid_state != MID_RESPONSE_RECEIVED) {
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500634 rc = -EIO;
Jeff Layton2c8f9812011-05-19 16:22:52 -0400635 cFYI(1, "Bad MID state?");
Steve French2b2bdfb2008-12-11 17:26:54 +0000636 goto out;
637 }
Steve French84afc292005-12-02 13:32:45 -0800638
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400639 buf = (char *)midQ->resp_buf;
640 iov[0].iov_base = buf;
641 iov[0].iov_len = get_rfc1002_length(buf) + 4;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400642 if (midQ->large_buf)
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400643 *resp_buf_type = CIFS_LARGE_BUFFER;
Jeff Layton2c8f9812011-05-19 16:22:52 -0400644 else
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400645 *resp_buf_type = CIFS_SMALL_BUFFER;
646
647 credits = ses->server->ops->get_credits(midQ);
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500648
Pavel Shilovsky082d0642012-05-17 12:18:21 +0400649 rc = ses->server->ops->check_receive(midQ, ses->server,
650 flags & CIFS_LOG_ERROR);
Steve French2b2bdfb2008-12-11 17:26:54 +0000651
Jeff Layton2c8f9812011-05-19 16:22:52 -0400652 /* mark it so buf will not be freed by delete_mid */
653 if ((flags & CIFS_NO_RESP) == 0)
654 midQ->resp_buf = NULL;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000655out:
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500656 delete_mid(midQ);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400657 add_credits(ses->server, credits, optype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 return rc;
660}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662int
Steve French96daf2b2011-05-27 04:34:02 +0000663SendReceive(const unsigned int xid, struct cifs_ses *ses,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400665 int *pbytes_returned, const int timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
667 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 struct mid_q_entry *midQ;
669
670 if (ses == NULL) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000671 cERROR(1, "Null smb session");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return -EIO;
673 }
Steve French79a58d12007-07-06 22:44:50 +0000674 if (ses->server == NULL) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000675 cERROR(1, "Null tcp session");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return -EIO;
677 }
678
Steve French79a58d12007-07-06 22:44:50 +0000679 if (ses->server->tcpStatus == CifsExiting)
Steve French31ca3bc2005-04-28 22:41:11 -0700680 return -ENOENT;
681
Steve French79a58d12007-07-06 22:44:50 +0000682 /* Ensure that we do not send more than 50 overlapping requests
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 to the same server. We may make this configurable later or
684 use ses->maxReq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000686 if (be32_to_cpu(in_buf->smb_buf_length) > CIFSMaxBufSize +
687 MAX_CIFS_HDR_SIZE - 4) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000688 cERROR(1, "Illegal length, greater than maximum frame, %d",
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000689 be32_to_cpu(in_buf->smb_buf_length));
Volker Lendecke6d9c6d52008-12-08 20:50:24 +0000690 return -EIO;
691 }
692
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400693 rc = wait_for_free_request(ses->server, timeout, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000694 if (rc)
695 return rc;
696
Steve French79a58d12007-07-06 22:44:50 +0000697 /* make sure that we sign in the same order that we send on this socket
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 and avoid races inside tcp sendmsg code that could cause corruption
699 of smb data */
700
Jeff Layton72ca5452008-12-01 07:09:36 -0500701 mutex_lock(&ses->server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000703 rc = allocate_mid(ses, in_buf, &midQ);
704 if (rc) {
Jeff Layton72ca5452008-12-01 07:09:36 -0500705 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000706 /* Update # of requests on wire to server */
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400707 add_credits(ses->server, 1, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000708 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
710
Steve Frenchad009ac2005-04-28 22:41:05 -0700711 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
Volker Lendecke829049c2008-12-06 16:00:53 +0100712 if (rc) {
713 mutex_unlock(&ses->server->srv_mutex);
714 goto out;
715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400717 midQ->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +0000718
719 cifs_in_send_inc(ses->server);
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000720 rc = smb_send(ses->server, in_buf, be32_to_cpu(in_buf->smb_buf_length));
Steve French789e6662011-08-09 18:44:44 +0000721 cifs_in_send_dec(ses->server);
722 cifs_save_when_sent(midQ);
Jeff Layton72ca5452008-12-01 07:09:36 -0500723 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000724
Steve French79a58d12007-07-06 22:44:50 +0000725 if (rc < 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000726 goto out;
727
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400728 if (timeout == CIFS_ASYNC_OP)
Steve French133672e2007-11-13 22:41:37 +0000729 goto out;
Jeff Layton0ade6402011-01-11 07:24:02 -0500730
731 rc = wait_for_response(ses->server, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -0500732 if (rc != 0) {
Jeff Layton121b0462012-05-15 12:21:10 -0400733 send_cancel(ses->server, in_buf, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -0500734 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400735 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
Jeff Layton1be912d2011-01-28 07:08:28 -0500736 /* no longer considered to be "in-flight" */
737 midQ->callback = DeleteMidQEntry;
738 spin_unlock(&GlobalMid_Lock);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400739 add_credits(ses->server, 1, 0);
Jeff Layton1be912d2011-01-28 07:08:28 -0500740 return rc;
741 }
742 spin_unlock(&GlobalMid_Lock);
743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Jeff Layton3c1105d2011-05-22 07:09:13 -0400745 rc = cifs_sync_mid_result(midQ, ses->server);
Jeff Layton053d5032011-01-11 07:24:02 -0500746 if (rc != 0) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400747 add_credits(ses->server, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return rc;
749 }
Steve French50c2f752007-07-13 00:33:32 +0000750
Jeff Layton2c8f9812011-05-19 16:22:52 -0400751 if (!midQ->resp_buf || !out_buf ||
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400752 midQ->mid_state != MID_RESPONSE_RECEIVED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 rc = -EIO;
Jeff Layton2c8f9812011-05-19 16:22:52 -0400754 cERROR(1, "Bad MID state?");
Steve French2b2bdfb2008-12-11 17:26:54 +0000755 goto out;
756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400758 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
Jeff Layton2c8f9812011-05-19 16:22:52 -0400759 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
760 rc = cifs_check_receive(midQ, ses->server, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000761out:
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500762 delete_mid(midQ);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400763 add_credits(ses->server, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 return rc;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000766}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000768/* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
769 blocking lock to return. */
770
771static int
Steve French96daf2b2011-05-27 04:34:02 +0000772send_lock_cancel(const unsigned int xid, struct cifs_tcon *tcon,
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000773 struct smb_hdr *in_buf,
774 struct smb_hdr *out_buf)
775{
776 int bytes_returned;
Steve French96daf2b2011-05-27 04:34:02 +0000777 struct cifs_ses *ses = tcon->ses;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000778 LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
779
780 /* We just modify the current in_buf to change
781 the type of lock from LOCKING_ANDX_SHARED_LOCK
782 or LOCKING_ANDX_EXCLUSIVE_LOCK to
783 LOCKING_ANDX_CANCEL_LOCK. */
784
785 pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
786 pSMB->Timeout = 0;
Pavel Shilovsky88257362012-05-23 14:01:59 +0400787 pSMB->hdr.Mid = get_next_mid(ses->server);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000788
789 return SendReceive(xid, ses, in_buf, out_buf,
Jeff Layton77499812011-01-11 07:24:23 -0500790 &bytes_returned, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000791}
792
793int
Steve French96daf2b2011-05-27 04:34:02 +0000794SendReceiveBlockingLock(const unsigned int xid, struct cifs_tcon *tcon,
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000795 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
796 int *pbytes_returned)
797{
798 int rc = 0;
799 int rstart = 0;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000800 struct mid_q_entry *midQ;
Steve French96daf2b2011-05-27 04:34:02 +0000801 struct cifs_ses *ses;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000802
803 if (tcon == NULL || tcon->ses == NULL) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000804 cERROR(1, "Null smb session");
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000805 return -EIO;
806 }
807 ses = tcon->ses;
808
Steve French79a58d12007-07-06 22:44:50 +0000809 if (ses->server == NULL) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000810 cERROR(1, "Null tcp session");
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000811 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
813
Steve French79a58d12007-07-06 22:44:50 +0000814 if (ses->server->tcpStatus == CifsExiting)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000815 return -ENOENT;
816
Steve French79a58d12007-07-06 22:44:50 +0000817 /* Ensure that we do not send more than 50 overlapping requests
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000818 to the same server. We may make this configurable later or
819 use ses->maxReq */
820
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000821 if (be32_to_cpu(in_buf->smb_buf_length) > CIFSMaxBufSize +
822 MAX_CIFS_HDR_SIZE - 4) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000823 cERROR(1, "Illegal length, greater than maximum frame, %d",
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000824 be32_to_cpu(in_buf->smb_buf_length));
Volker Lendecke6d9c6d52008-12-08 20:50:24 +0000825 return -EIO;
826 }
827
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400828 rc = wait_for_free_request(ses->server, CIFS_BLOCKING_OP, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000829 if (rc)
830 return rc;
831
Steve French79a58d12007-07-06 22:44:50 +0000832 /* make sure that we sign in the same order that we send on this socket
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000833 and avoid races inside tcp sendmsg code that could cause corruption
834 of smb data */
835
Jeff Layton72ca5452008-12-01 07:09:36 -0500836 mutex_lock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000837
838 rc = allocate_mid(ses, in_buf, &midQ);
839 if (rc) {
Jeff Layton72ca5452008-12-01 07:09:36 -0500840 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000841 return rc;
842 }
843
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000844 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
Volker Lendecke829049c2008-12-06 16:00:53 +0100845 if (rc) {
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500846 delete_mid(midQ);
Volker Lendecke829049c2008-12-06 16:00:53 +0100847 mutex_unlock(&ses->server->srv_mutex);
848 return rc;
849 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000850
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400851 midQ->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +0000852 cifs_in_send_inc(ses->server);
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000853 rc = smb_send(ses->server, in_buf, be32_to_cpu(in_buf->smb_buf_length));
Steve French789e6662011-08-09 18:44:44 +0000854 cifs_in_send_dec(ses->server);
855 cifs_save_when_sent(midQ);
Jeff Layton72ca5452008-12-01 07:09:36 -0500856 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000857
Steve French79a58d12007-07-06 22:44:50 +0000858 if (rc < 0) {
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500859 delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000860 return rc;
861 }
862
863 /* Wait for a reply - allow signals to interrupt. */
864 rc = wait_event_interruptible(ses->server->response_q,
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400865 (!(midQ->mid_state == MID_REQUEST_SUBMITTED)) ||
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000866 ((ses->server->tcpStatus != CifsGood) &&
867 (ses->server->tcpStatus != CifsNew)));
868
869 /* Were we interrupted by a signal ? */
870 if ((rc == -ERESTARTSYS) &&
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400871 (midQ->mid_state == MID_REQUEST_SUBMITTED) &&
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000872 ((ses->server->tcpStatus == CifsGood) ||
873 (ses->server->tcpStatus == CifsNew))) {
874
875 if (in_buf->Command == SMB_COM_TRANSACTION2) {
876 /* POSIX lock. We send a NT_CANCEL SMB to cause the
877 blocking lock to return. */
Jeff Layton121b0462012-05-15 12:21:10 -0400878 rc = send_cancel(ses->server, in_buf, midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000879 if (rc) {
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500880 delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000881 return rc;
882 }
883 } else {
884 /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
885 to cause the blocking lock to return. */
886
887 rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
888
889 /* If we get -ENOLCK back the lock may have
890 already been removed. Don't exit in this case. */
891 if (rc && rc != -ENOLCK) {
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500892 delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000893 return rc;
894 }
895 }
896
Jeff Layton1be912d2011-01-28 07:08:28 -0500897 rc = wait_for_response(ses->server, midQ);
898 if (rc) {
Jeff Layton121b0462012-05-15 12:21:10 -0400899 send_cancel(ses->server, in_buf, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -0500900 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400901 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
Jeff Layton1be912d2011-01-28 07:08:28 -0500902 /* no longer considered to be "in-flight" */
903 midQ->callback = DeleteMidQEntry;
904 spin_unlock(&GlobalMid_Lock);
905 return rc;
906 }
907 spin_unlock(&GlobalMid_Lock);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000908 }
Jeff Layton1be912d2011-01-28 07:08:28 -0500909
910 /* We got the response - restart system call. */
911 rstart = 1;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000912 }
913
Jeff Layton3c1105d2011-05-22 07:09:13 -0400914 rc = cifs_sync_mid_result(midQ, ses->server);
Jeff Layton053d5032011-01-11 07:24:02 -0500915 if (rc != 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000916 return rc;
Steve French50c2f752007-07-13 00:33:32 +0000917
Volker Lendecke17c8bfe2008-12-06 16:38:19 +0100918 /* rcvd frame is ok */
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400919 if (out_buf == NULL || midQ->mid_state != MID_RESPONSE_RECEIVED) {
Volker Lendecke17c8bfe2008-12-06 16:38:19 +0100920 rc = -EIO;
Joe Perchesb6b38f72010-04-21 03:50:45 +0000921 cERROR(1, "Bad MID state?");
Volker Lendecke698e96a2008-12-06 16:39:31 +0100922 goto out;
Volker Lendecke17c8bfe2008-12-06 16:38:19 +0100923 }
924
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400925 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
Jeff Layton2c8f9812011-05-19 16:22:52 -0400926 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
927 rc = cifs_check_receive(midQ, ses->server, 0);
Volker Lendecke17c8bfe2008-12-06 16:38:19 +0100928out:
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500929 delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000930 if (rstart && rc == -EACCES)
931 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return rc;
933}