blob: 3fb0e433b8e252207e9fea49e4f2e68f79359857 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/transport.c
3 *
Steve Frenchad7a2922008-02-07 23:25:02 +00004 * Copyright (C) International Business Machines Corp., 2002,2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Author(s): Steve French (sfrench@us.ibm.com)
Steve French14a441a2b2006-07-16 04:32:51 +00006 * Jeremy Allison (jra@samba.org) 2006.
Steve French79a58d12007-07-06 22:44:50 +00007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published
10 * by the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
16 * the GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this library; if not, write to the Free Software
Steve French79a58d12007-07-06 22:44:50 +000020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 */
22
23#include <linux/fs.h>
24#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/wait.h>
27#include <linux/net.h>
28#include <linux/delay.h>
Jeff Laytonf06ac722011-10-19 15:30:40 -040029#include <linux/freezer.h>
Jeff Laytonb8eed282012-09-18 16:20:35 -070030#include <linux/tcp.h>
Christoph Hellwig2f8b5442016-11-01 07:40:13 -060031#include <linux/bvec.h>
Jeff Layton97bc00b2012-09-18 16:20:35 -070032#include <linux/highmem.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080033#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/processor.h>
35#include <linux/mempool.h>
36#include "cifspdu.h"
37#include "cifsglob.h"
38#include "cifsproto.h"
39#include "cifs_debug.h"
Aurelien Aptel8bd68c62018-02-16 19:19:29 +010040#include "smb2proto.h"
Long Li9762c2d2017-11-22 17:38:43 -070041#include "smbdirect.h"
Steve French50c2f752007-07-13 00:33:32 +000042
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +110043/* Max number of iovectors we can use off the stack when sending requests. */
44#define CIFS_MAX_IOV_SIZE 8
45
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +040046void
47cifs_wake_up_task(struct mid_q_entry *mid)
Jeff Layton2b84a36c2011-01-11 07:24:21 -050048{
49 wake_up_process(mid->callback_data);
50}
51
Jeff Laytona6827c12011-01-11 07:24:21 -050052struct mid_q_entry *
Jeff Layton24b9b062008-12-01 07:09:34 -050053AllocMidQEntry(const struct smb_hdr *smb_buffer, struct TCP_Server_Info *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55 struct mid_q_entry *temp;
56
Jeff Layton24b9b062008-12-01 07:09:34 -050057 if (server == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -050058 cifs_dbg(VFS, "Null TCP session in AllocMidQEntry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 return NULL;
60 }
Steve French50c2f752007-07-13 00:33:32 +000061
Pekka Enberg232087c2008-09-15 13:22:54 +030062 temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
NeilBrowna6f74e82017-04-10 12:08:53 +100063 memset(temp, 0, sizeof(struct mid_q_entry));
64 temp->mid = get_mid(smb_buffer);
65 temp->pid = current->pid;
66 temp->command = cpu_to_le16(smb_buffer->Command);
67 cifs_dbg(FYI, "For smb_command %d\n", smb_buffer->Command);
Steve French1047abc2005-10-11 19:58:06 -070068 /* do_gettimeofday(&temp->when_sent);*/ /* easier to use jiffies */
NeilBrowna6f74e82017-04-10 12:08:53 +100069 /* when mid allocated can be before when sent */
70 temp->when_alloc = jiffies;
71 temp->server = server;
Jeff Layton2b84a36c2011-01-11 07:24:21 -050072
NeilBrowna6f74e82017-04-10 12:08:53 +100073 /*
74 * The default is for the mid to be synchronous, so the
75 * default callback just wakes up the current task.
76 */
77 temp->callback = cifs_wake_up_task;
78 temp->callback_data = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 atomic_inc(&midCount);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -040081 temp->mid_state = MID_REQUEST_ALLOCATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 return temp;
83}
84
Jeff Layton766fdbb2011-01-11 07:24:21 -050085void
Linus Torvalds1da177e2005-04-16 15:20:36 -070086DeleteMidQEntry(struct mid_q_entry *midEntry)
87{
Steve French1047abc2005-10-11 19:58:06 -070088#ifdef CONFIG_CIFS_STATS2
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +040089 __le16 command = midEntry->server->vals->lock_cmd;
Steve French1047abc2005-10-11 19:58:06 -070090 unsigned long now;
91#endif
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -040092 midEntry->mid_state = MID_FREE;
Jeff Layton80975312011-01-11 07:24:02 -050093 atomic_dec(&midCount);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -040094 if (midEntry->large_buf)
Steve Frenchb8643e12005-04-28 22:41:07 -070095 cifs_buf_release(midEntry->resp_buf);
96 else
97 cifs_small_buf_release(midEntry->resp_buf);
Steve French1047abc2005-10-11 19:58:06 -070098#ifdef CONFIG_CIFS_STATS2
99 now = jiffies;
100 /* commands taking longer than one second are indications that
101 something is wrong, unless it is quite a slow link or server */
Karim Eshapa4328fea2017-05-12 01:53:38 +0200102 if (time_after(now, midEntry->when_alloc + HZ)) {
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400103 if ((cifsFYI & CIFS_TIMER) && (midEntry->command != command)) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +0300104 pr_debug(" CIFS slow rsp: cmd %d mid %llu",
Steve French1047abc2005-10-11 19:58:06 -0700105 midEntry->command, midEntry->mid);
Andy Shevchenko0b456f02014-08-27 16:49:44 +0300106 pr_info(" A: 0x%lx S: 0x%lx R: 0x%lx\n",
Steve French1047abc2005-10-11 19:58:06 -0700107 now - midEntry->when_alloc,
108 now - midEntry->when_sent,
109 now - midEntry->when_received);
110 }
111 }
112#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 mempool_free(midEntry, cifs_mid_poolp);
114}
115
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700116void
117cifs_delete_mid(struct mid_q_entry *mid)
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500118{
119 spin_lock(&GlobalMid_Lock);
120 list_del(&mid->qhead);
121 spin_unlock(&GlobalMid_Lock);
122
123 DeleteMidQEntry(mid);
124}
125
Jeff Layton6f49f462012-09-18 16:20:34 -0700126/*
127 * smb_send_kvec - send an array of kvecs to the server
128 * @server: Server to send the data to
Al Viro3ab3f2a2015-11-13 02:36:04 -0500129 * @smb_msg: Message to send
Jeff Layton6f49f462012-09-18 16:20:34 -0700130 * @sent: amount of data sent on socket is stored here
131 *
132 * Our basic "send data to server" function. Should be called with srv_mutex
133 * held. The caller is responsible for handling the results.
134 */
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500135static int
Al Viro3ab3f2a2015-11-13 02:36:04 -0500136smb_send_kvec(struct TCP_Server_Info *server, struct msghdr *smb_msg,
137 size_t *sent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 int rc = 0;
Al Viro3ab3f2a2015-11-13 02:36:04 -0500140 int retries = 0;
Steve Frenchedf1ae42008-10-29 00:47:57 +0000141 struct socket *ssocket = server->ssocket;
Steve French50c2f752007-07-13 00:33:32 +0000142
Jeff Layton6f49f462012-09-18 16:20:34 -0700143 *sent = 0;
144
Al Viro3ab3f2a2015-11-13 02:36:04 -0500145 smb_msg->msg_name = (struct sockaddr *) &server->dstaddr;
146 smb_msg->msg_namelen = sizeof(struct sockaddr);
147 smb_msg->msg_control = NULL;
148 smb_msg->msg_controllen = 0;
Jeff Layton0496e022008-12-30 12:39:16 -0500149 if (server->noblocksnd)
Al Viro3ab3f2a2015-11-13 02:36:04 -0500150 smb_msg->msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL;
Steve Frenchedf1ae42008-10-29 00:47:57 +0000151 else
Al Viro3ab3f2a2015-11-13 02:36:04 -0500152 smb_msg->msg_flags = MSG_NOSIGNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Al Viro3ab3f2a2015-11-13 02:36:04 -0500154 while (msg_data_left(smb_msg)) {
Jeff Layton6f49f462012-09-18 16:20:34 -0700155 /*
156 * If blocking send, we try 3 times, since each can block
157 * for 5 seconds. For nonblocking we have to try more
158 * but wait increasing amounts of time allowing time for
159 * socket to clear. The overall time we wait in either
160 * case to send on the socket is about 15 seconds.
161 * Similarly we wait for 15 seconds for a response from
162 * the server in SendReceive[2] for the server to send
163 * a response back for most types of requests (except
164 * SMB Write past end of file which can be slow, and
165 * blocking lock operations). NFS waits slightly longer
166 * than CIFS, but this can make it take longer for
167 * nonresponsive servers to be detected and 15 seconds
168 * is more than enough time for modern networks to
169 * send a packet. In most cases if we fail to send
170 * after the retries we will kill the socket and
171 * reconnect which may clear the network problem.
172 */
Al Viro3ab3f2a2015-11-13 02:36:04 -0500173 rc = sock_sendmsg(ssocket, smb_msg);
Jeff Laytonce6c44e2013-03-22 08:36:45 -0400174 if (rc == -EAGAIN) {
Al Viro3ab3f2a2015-11-13 02:36:04 -0500175 retries++;
176 if (retries >= 14 ||
177 (!server->noblocksnd && (retries > 2))) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500178 cifs_dbg(VFS, "sends on sock %p stuck for 15 seconds\n",
179 ssocket);
Al Viro3ab3f2a2015-11-13 02:36:04 -0500180 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
Al Viro3ab3f2a2015-11-13 02:36:04 -0500182 msleep(1 << retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 continue;
184 }
Jeff Layton6f49f462012-09-18 16:20:34 -0700185
Steve French79a58d12007-07-06 22:44:50 +0000186 if (rc < 0)
Al Viro3ab3f2a2015-11-13 02:36:04 -0500187 return rc;
Jeff Layton6f49f462012-09-18 16:20:34 -0700188
Steve French79a58d12007-07-06 22:44:50 +0000189 if (rc == 0) {
Steve French3e844692005-10-03 13:37:24 -0700190 /* should never happen, letting socket clear before
191 retrying is our only obvious option here */
Joe Perchesf96637b2013-05-04 22:12:25 -0500192 cifs_dbg(VFS, "tcp sent no data\n");
Steve French3e844692005-10-03 13:37:24 -0700193 msleep(500);
194 continue;
195 }
Jeff Layton6f49f462012-09-18 16:20:34 -0700196
Al Viro3ab3f2a2015-11-13 02:36:04 -0500197 /* send was at least partially successful */
198 *sent += rc;
199 retries = 0; /* in case we get ENOSPC on the next send */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
Al Viro3ab3f2a2015-11-13 02:36:04 -0500201 return 0;
Jeff Layton97bc00b2012-09-18 16:20:35 -0700202}
203
Jeff Laytona26054d2014-02-14 07:21:00 -0500204static unsigned long
205rqst_len(struct smb_rqst *rqst)
206{
207 unsigned int i;
208 struct kvec *iov = rqst->rq_iov;
209 unsigned long buflen = 0;
210
211 /* total up iov array first */
212 for (i = 0; i < rqst->rq_nvec; i++)
213 buflen += iov[i].iov_len;
214
215 /* add in the page array if there is one */
216 if (rqst->rq_npages) {
217 buflen += rqst->rq_pagesz * (rqst->rq_npages - 1);
218 buflen += rqst->rq_tailsz;
219 }
220
221 return buflen;
222}
223
Jeff Layton6f49f462012-09-18 16:20:34 -0700224static int
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700225__smb_send_rqst(struct TCP_Server_Info *server, struct smb_rqst *rqst)
Jeff Layton6f49f462012-09-18 16:20:34 -0700226{
227 int rc;
228 struct kvec *iov = rqst->rq_iov;
229 int n_vec = rqst->rq_nvec;
230 unsigned int smb_buf_length = get_rfc1002_length(iov[0].iov_base);
Jeff Laytona26054d2014-02-14 07:21:00 -0500231 unsigned long send_length;
Jeff Layton97bc00b2012-09-18 16:20:35 -0700232 unsigned int i;
Al Viro3ab3f2a2015-11-13 02:36:04 -0500233 size_t total_len = 0, sent, size;
Jeff Laytonb8eed282012-09-18 16:20:35 -0700234 struct socket *ssocket = server->ssocket;
Al Viro3ab3f2a2015-11-13 02:36:04 -0500235 struct msghdr smb_msg;
Jeff Laytonb8eed282012-09-18 16:20:35 -0700236 int val = 1;
Long Li9762c2d2017-11-22 17:38:43 -0700237 if (cifs_rdma_enabled(server) && server->smbd_conn) {
238 rc = smbd_send(server->smbd_conn, rqst);
239 goto smbd_done;
240 }
Jeff Laytonea702b82012-12-27 07:28:55 -0500241 if (ssocket == NULL)
242 return -ENOTSOCK;
243
Jeff Laytona26054d2014-02-14 07:21:00 -0500244 /* sanity check send length */
245 send_length = rqst_len(rqst);
246 if (send_length != smb_buf_length + 4) {
247 WARN(1, "Send length mismatch(send_length=%lu smb_buf_length=%u)\n",
248 send_length, smb_buf_length);
249 return -EIO;
250 }
251
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800252 if (n_vec < 2)
253 return -EIO;
254
Joe Perchesf96637b2013-05-04 22:12:25 -0500255 cifs_dbg(FYI, "Sending smb: smb_len=%u\n", smb_buf_length);
Jeff Layton6f49f462012-09-18 16:20:34 -0700256 dump_smb(iov[0].iov_base, iov[0].iov_len);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800257 dump_smb(iov[1].iov_base, iov[1].iov_len);
Jeff Layton6f49f462012-09-18 16:20:34 -0700258
Jeff Laytonb8eed282012-09-18 16:20:35 -0700259 /* cork the socket */
260 kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
261 (char *)&val, sizeof(val));
262
Al Viro3ab3f2a2015-11-13 02:36:04 -0500263 size = 0;
264 for (i = 0; i < n_vec; i++)
265 size += iov[i].iov_len;
266
267 iov_iter_kvec(&smb_msg.msg_iter, WRITE | ITER_KVEC, iov, n_vec, size);
268
269 rc = smb_send_kvec(server, &smb_msg, &sent);
Jeff Layton97bc00b2012-09-18 16:20:35 -0700270 if (rc < 0)
271 goto uncork;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Jeff Layton97bc00b2012-09-18 16:20:35 -0700273 total_len += sent;
274
275 /* now walk the page array and send each page in it */
276 for (i = 0; i < rqst->rq_npages; i++) {
Al Viro3ab3f2a2015-11-13 02:36:04 -0500277 size_t len = i == rqst->rq_npages - 1
278 ? rqst->rq_tailsz
279 : rqst->rq_pagesz;
280 struct bio_vec bvec = {
281 .bv_page = rqst->rq_pages[i],
282 .bv_len = len
283 };
284 iov_iter_bvec(&smb_msg.msg_iter, WRITE | ITER_BVEC,
285 &bvec, 1, len);
286 rc = smb_send_kvec(server, &smb_msg, &sent);
Jeff Layton97bc00b2012-09-18 16:20:35 -0700287 if (rc < 0)
288 break;
289
290 total_len += sent;
291 }
292
293uncork:
Jeff Laytonb8eed282012-09-18 16:20:35 -0700294 /* uncork it */
295 val = 0;
296 kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
297 (char *)&val, sizeof(val));
298
Steve Frenchedf1ae42008-10-29 00:47:57 +0000299 if ((total_len > 0) && (total_len != smb_buf_length + 4)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500300 cifs_dbg(FYI, "partial send (wanted=%u sent=%zu): terminating session\n",
301 smb_buf_length + 4, total_len);
Jeff Layton6f49f462012-09-18 16:20:34 -0700302 /*
303 * If we have only sent part of an SMB then the next SMB could
304 * be taken as the remainder of this one. We need to kill the
305 * socket so the server throws away the partial SMB
306 */
Steve Frenchedf1ae42008-10-29 00:47:57 +0000307 server->tcpStatus = CifsNeedReconnect;
308 }
Long Li9762c2d2017-11-22 17:38:43 -0700309smbd_done:
Jeff Laytond804d412011-01-28 15:05:43 -0500310 if (rc < 0 && rc != -EINTR)
Joe Perchesf96637b2013-05-04 22:12:25 -0500311 cifs_dbg(VFS, "Error %d sending data on socket to server\n",
312 rc);
Jeff Laytond804d412011-01-28 15:05:43 -0500313 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 return rc;
317}
318
Jeff Layton6f49f462012-09-18 16:20:34 -0700319static int
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700320smb_send_rqst(struct TCP_Server_Info *server, struct smb_rqst *rqst, int flags)
Jeff Layton6f49f462012-09-18 16:20:34 -0700321{
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700322 struct smb_rqst cur_rqst;
323 int rc;
Jeff Layton6f49f462012-09-18 16:20:34 -0700324
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700325 if (!(flags & CIFS_TRANSFORM_REQ))
326 return __smb_send_rqst(server, rqst);
327
328 if (!server->ops->init_transform_rq ||
329 !server->ops->free_transform_rq) {
330 cifs_dbg(VFS, "Encryption requested but transform callbacks are missed\n");
331 return -EIO;
332 }
333
334 rc = server->ops->init_transform_rq(server, &cur_rqst, rqst);
335 if (rc)
336 return rc;
337
338 rc = __smb_send_rqst(server, &cur_rqst);
339 server->ops->free_transform_rq(&cur_rqst);
340 return rc;
Jeff Layton6f49f462012-09-18 16:20:34 -0700341}
342
Jeff Layton0496e022008-12-30 12:39:16 -0500343int
344smb_send(struct TCP_Server_Info *server, struct smb_hdr *smb_buffer,
345 unsigned int smb_buf_length)
346{
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800347 struct kvec iov[2];
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700348 struct smb_rqst rqst = { .rq_iov = iov,
349 .rq_nvec = 2 };
Jeff Layton0496e022008-12-30 12:39:16 -0500350
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800351 iov[0].iov_base = smb_buffer;
352 iov[0].iov_len = 4;
353 iov[1].iov_base = (char *)smb_buffer + 4;
354 iov[1].iov_len = smb_buf_length;
Jeff Layton0496e022008-12-30 12:39:16 -0500355
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700356 return __smb_send_rqst(server, &rqst);
Jeff Layton0496e022008-12-30 12:39:16 -0500357}
358
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300359static int
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400360wait_for_free_credits(struct TCP_Server_Info *server, const int timeout,
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300361 int *credits)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000362{
Pavel Shilovsky5bc59492012-02-21 19:56:08 +0300363 int rc;
364
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300365 spin_lock(&server->req_lock);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400366 if (timeout == CIFS_ASYNC_OP) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000367 /* oplock breaks must not be held up */
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300368 server->in_flight++;
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300369 *credits -= 1;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300370 spin_unlock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000371 return 0;
372 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000373
Volker Lendecke27a97a62008-12-08 20:59:39 +0000374 while (1) {
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300375 if (*credits <= 0) {
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300376 spin_unlock(&server->req_lock);
Steve French789e6662011-08-09 18:44:44 +0000377 cifs_num_waiters_inc(server);
Pavel Shilovsky5bc59492012-02-21 19:56:08 +0300378 rc = wait_event_killable(server->request_q,
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300379 has_credits(server, credits));
Steve French789e6662011-08-09 18:44:44 +0000380 cifs_num_waiters_dec(server);
Pavel Shilovsky5bc59492012-02-21 19:56:08 +0300381 if (rc)
382 return rc;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300383 spin_lock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000384 } else {
Jeff Laytonc5797a92011-01-11 07:24:01 -0500385 if (server->tcpStatus == CifsExiting) {
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300386 spin_unlock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000387 return -ENOENT;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000388 }
Volker Lendecke27a97a62008-12-08 20:59:39 +0000389
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400390 /*
391 * Can not count locking commands against total
392 * as they are allowed to block on server.
393 */
Volker Lendecke27a97a62008-12-08 20:59:39 +0000394
395 /* update # of requests on the wire to server */
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400396 if (timeout != CIFS_BLOCKING_OP) {
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300397 *credits -= 1;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300398 server->in_flight++;
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400399 }
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300400 spin_unlock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000401 break;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000402 }
403 }
404 return 0;
405}
406
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300407static int
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400408wait_for_free_request(struct TCP_Server_Info *server, const int timeout,
409 const int optype)
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300410{
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -0500411 int *val;
412
413 val = server->ops->get_credits_field(server, optype);
414 /* Since an echo is already inflight, no need to wait to send another */
415 if (*val <= 0 && optype == CIFS_ECHO_OP)
416 return -EAGAIN;
417 return wait_for_free_credits(server, timeout, val);
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300418}
419
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400420int
421cifs_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
422 unsigned int *num, unsigned int *credits)
423{
424 *num = size;
425 *credits = 0;
426 return 0;
427}
428
Steve French96daf2b2011-05-27 04:34:02 +0000429static int allocate_mid(struct cifs_ses *ses, struct smb_hdr *in_buf,
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000430 struct mid_q_entry **ppmidQ)
431{
432 if (ses->server->tcpStatus == CifsExiting) {
433 return -ENOENT;
Volker Lendecke8fbbd362008-12-06 13:12:34 +0100434 }
435
436 if (ses->server->tcpStatus == CifsNeedReconnect) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500437 cifs_dbg(FYI, "tcp session dead - return to caller to retry\n");
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000438 return -EAGAIN;
Volker Lendecke8fbbd362008-12-06 13:12:34 +0100439 }
440
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500441 if (ses->status == CifsNew) {
Steve French79a58d12007-07-06 22:44:50 +0000442 if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
Steve Frenchad7a2922008-02-07 23:25:02 +0000443 (in_buf->Command != SMB_COM_NEGOTIATE))
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000444 return -EAGAIN;
Steve Frenchad7a2922008-02-07 23:25:02 +0000445 /* else ok - we are setting up session */
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000446 }
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500447
448 if (ses->status == CifsExiting) {
449 /* check if SMB session is bad because we are setting it up */
450 if (in_buf->Command != SMB_COM_LOGOFF_ANDX)
451 return -EAGAIN;
452 /* else ok - we are shutting down session */
453 }
454
Jeff Layton24b9b062008-12-01 07:09:34 -0500455 *ppmidQ = AllocMidQEntry(in_buf, ses->server);
Steve French26f57362007-08-30 22:09:15 +0000456 if (*ppmidQ == NULL)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000457 return -ENOMEM;
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500458 spin_lock(&GlobalMid_Lock);
459 list_add_tail(&(*ppmidQ)->qhead, &ses->server->pending_mid_q);
460 spin_unlock(&GlobalMid_Lock);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000461 return 0;
462}
463
Jeff Layton0ade6402011-01-11 07:24:02 -0500464static int
465wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *midQ)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000466{
Jeff Layton0ade6402011-01-11 07:24:02 -0500467 int error;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000468
Colin Cross5853cc22013-05-07 17:52:05 +0000469 error = wait_event_freezekillable_unsafe(server->response_q,
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400470 midQ->mid_state != MID_REQUEST_SUBMITTED);
Jeff Layton0ade6402011-01-11 07:24:02 -0500471 if (error < 0)
472 return -ERESTARTSYS;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000473
Jeff Layton0ade6402011-01-11 07:24:02 -0500474 return 0;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000475}
476
Jeff Laytonfec344e2012-09-18 16:20:35 -0700477struct mid_q_entry *
478cifs_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400479{
480 int rc;
Jeff Laytonfec344e2012-09-18 16:20:35 -0700481 struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400482 struct mid_q_entry *mid;
483
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800484 if (rqst->rq_iov[0].iov_len != 4 ||
485 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
486 return ERR_PTR(-EIO);
487
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400488 /* enable signing if server requires it */
Jeff Layton38d77c52013-05-26 07:01:00 -0400489 if (server->sign)
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400490 hdr->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
491
492 mid = AllocMidQEntry(hdr, server);
493 if (mid == NULL)
Jeff Laytonfec344e2012-09-18 16:20:35 -0700494 return ERR_PTR(-ENOMEM);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400495
Jeff Laytonfec344e2012-09-18 16:20:35 -0700496 rc = cifs_sign_rqst(rqst, server, &mid->sequence_number);
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100497 if (rc) {
498 DeleteMidQEntry(mid);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700499 return ERR_PTR(rc);
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100500 }
501
Jeff Laytonfec344e2012-09-18 16:20:35 -0700502 return mid;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400503}
Steve French133672e2007-11-13 22:41:37 +0000504
505/*
Jeff Laytona6827c12011-01-11 07:24:21 -0500506 * Send a SMB request and set the callback function in the mid to handle
507 * the result. Caller is responsible for dealing with timeouts.
508 */
509int
Jeff Laytonfec344e2012-09-18 16:20:35 -0700510cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst,
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -0800511 mid_receive_t *receive, mid_callback_t *callback,
512 mid_handle_t *handle, void *cbdata, const int flags)
Jeff Laytona6827c12011-01-11 07:24:21 -0500513{
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400514 int rc, timeout, optype;
Jeff Laytona6827c12011-01-11 07:24:21 -0500515 struct mid_q_entry *mid;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400516 unsigned int credits = 0;
Jeff Laytona6827c12011-01-11 07:24:21 -0500517
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400518 timeout = flags & CIFS_TIMEOUT_MASK;
519 optype = flags & CIFS_OP_MASK;
520
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400521 if ((flags & CIFS_HAS_CREDITS) == 0) {
522 rc = wait_for_free_request(server, timeout, optype);
523 if (rc)
524 return rc;
525 credits = 1;
526 }
Jeff Laytona6827c12011-01-11 07:24:21 -0500527
528 mutex_lock(&server->srv_mutex);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700529 mid = server->ops->setup_async_request(server, rqst);
530 if (IS_ERR(mid)) {
Jeff Laytona6827c12011-01-11 07:24:21 -0500531 mutex_unlock(&server->srv_mutex);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400532 add_credits_and_wake_if(server, credits, optype);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700533 return PTR_ERR(mid);
Jeff Laytona6827c12011-01-11 07:24:21 -0500534 }
535
Jeff Layton44d22d82011-10-19 15:29:49 -0400536 mid->receive = receive;
Jeff Laytona6827c12011-01-11 07:24:21 -0500537 mid->callback = callback;
538 mid->callback_data = cbdata;
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -0800539 mid->handle = handle;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400540 mid->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +0000541
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100542 /* put it on the pending_mid_q */
543 spin_lock(&GlobalMid_Lock);
544 list_add_tail(&mid->qhead, &server->pending_mid_q);
545 spin_unlock(&GlobalMid_Lock);
546
Long Li93d2cb62017-06-28 15:55:55 -0700547 /*
548 * Need to store the time in mid before calling I/O. For call_async,
549 * I/O response may come back and free the mid entry on another thread.
550 */
551 cifs_save_when_sent(mid);
Steve French789e6662011-08-09 18:44:44 +0000552 cifs_in_send_inc(server);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700553 rc = smb_send_rqst(server, rqst, flags);
Steve French789e6662011-08-09 18:44:44 +0000554 cifs_in_send_dec(server);
Jeff Laytonad313cb2013-04-03 10:27:36 -0400555
Rabin Vincent820962d2015-12-23 07:32:41 +0100556 if (rc < 0) {
Jeff Laytonad313cb2013-04-03 10:27:36 -0400557 server->sequence_number -= 2;
Rabin Vincent820962d2015-12-23 07:32:41 +0100558 cifs_delete_mid(mid);
559 }
560
Jeff Laytona6827c12011-01-11 07:24:21 -0500561 mutex_unlock(&server->srv_mutex);
Steve French789e6662011-08-09 18:44:44 +0000562
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100563 if (rc == 0)
564 return 0;
Jeff Laytona6827c12011-01-11 07:24:21 -0500565
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400566 add_credits_and_wake_if(server, credits, optype);
Jeff Laytona6827c12011-01-11 07:24:21 -0500567 return rc;
568}
569
570/*
Steve French133672e2007-11-13 22:41:37 +0000571 *
572 * Send an SMB Request. No response info (other than return code)
573 * needs to be parsed.
574 *
575 * flags indicate the type of request buffer and how long to wait
576 * and whether to log NT STATUS code (error) before mapping it to POSIX error
577 *
578 */
579int
Steve French96daf2b2011-05-27 04:34:02 +0000580SendReceiveNoRsp(const unsigned int xid, struct cifs_ses *ses,
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400581 char *in_buf, int flags)
Steve French133672e2007-11-13 22:41:37 +0000582{
583 int rc;
584 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700585 struct kvec rsp_iov;
Steve French133672e2007-11-13 22:41:37 +0000586 int resp_buf_type;
587
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400588 iov[0].iov_base = in_buf;
589 iov[0].iov_len = get_rfc1002_length(in_buf) + 4;
Steve French133672e2007-11-13 22:41:37 +0000590 flags |= CIFS_NO_RESP;
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700591 rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Joe Perchesf96637b2013-05-04 22:12:25 -0500592 cifs_dbg(NOISY, "SendRcvNoRsp flags %d rc %d\n", flags, rc);
Steve French90c81e02008-02-12 20:32:36 +0000593
Steve French133672e2007-11-13 22:41:37 +0000594 return rc;
595}
596
Jeff Layton053d5032011-01-11 07:24:02 -0500597static int
Jeff Layton3c1105d2011-05-22 07:09:13 -0400598cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
Jeff Layton053d5032011-01-11 07:24:02 -0500599{
600 int rc = 0;
601
Joe Perchesf96637b2013-05-04 22:12:25 -0500602 cifs_dbg(FYI, "%s: cmd=%d mid=%llu state=%d\n",
603 __func__, le16_to_cpu(mid->command), mid->mid, mid->mid_state);
Jeff Layton053d5032011-01-11 07:24:02 -0500604
Jeff Layton74dd92a2011-01-11 07:24:02 -0500605 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400606 switch (mid->mid_state) {
Jeff Layton74dd92a2011-01-11 07:24:02 -0500607 case MID_RESPONSE_RECEIVED:
Jeff Layton053d5032011-01-11 07:24:02 -0500608 spin_unlock(&GlobalMid_Lock);
609 return rc;
Jeff Layton74dd92a2011-01-11 07:24:02 -0500610 case MID_RETRY_NEEDED:
611 rc = -EAGAIN;
612 break;
Jeff Layton71823ba2011-02-10 08:03:50 -0500613 case MID_RESPONSE_MALFORMED:
614 rc = -EIO;
615 break;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400616 case MID_SHUTDOWN:
617 rc = -EHOSTDOWN;
618 break;
Jeff Layton74dd92a2011-01-11 07:24:02 -0500619 default:
Jeff Layton3c1105d2011-05-22 07:09:13 -0400620 list_del_init(&mid->qhead);
Joe Perchesf96637b2013-05-04 22:12:25 -0500621 cifs_dbg(VFS, "%s: invalid mid state mid=%llu state=%d\n",
622 __func__, mid->mid, mid->mid_state);
Jeff Layton74dd92a2011-01-11 07:24:02 -0500623 rc = -EIO;
Jeff Layton053d5032011-01-11 07:24:02 -0500624 }
625 spin_unlock(&GlobalMid_Lock);
626
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500627 DeleteMidQEntry(mid);
Jeff Layton053d5032011-01-11 07:24:02 -0500628 return rc;
629}
630
Jeff Layton121b0462012-05-15 12:21:10 -0400631static inline int
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800632send_cancel(struct TCP_Server_Info *server, struct smb_rqst *rqst,
633 struct mid_q_entry *mid)
Jeff Layton76dcc262011-01-11 07:24:24 -0500634{
Jeff Layton121b0462012-05-15 12:21:10 -0400635 return server->ops->send_cancel ?
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800636 server->ops->send_cancel(server, rqst, mid) : 0;
Jeff Layton76dcc262011-01-11 07:24:24 -0500637}
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639int
Jeff Layton2c8f9812011-05-19 16:22:52 -0400640cifs_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
641 bool log_error)
642{
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400643 unsigned int len = get_rfc1002_length(mid->resp_buf) + 4;
Jeff Layton826a95e2011-10-11 06:41:32 -0400644
645 dump_smb(mid->resp_buf, min_t(u32, 92, len));
Jeff Layton2c8f9812011-05-19 16:22:52 -0400646
647 /* convert the length into a more usable form */
Jeff Layton38d77c52013-05-26 07:01:00 -0400648 if (server->sign) {
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800649 struct kvec iov[2];
Steve French985e4ff02012-08-03 09:42:45 -0500650 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800651 struct smb_rqst rqst = { .rq_iov = iov,
652 .rq_nvec = 2 };
Jeff Layton826a95e2011-10-11 06:41:32 -0400653
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800654 iov[0].iov_base = mid->resp_buf;
655 iov[0].iov_len = 4;
656 iov[1].iov_base = (char *)mid->resp_buf + 4;
657 iov[1].iov_len = len - 4;
Jeff Layton2c8f9812011-05-19 16:22:52 -0400658 /* FIXME: add code to kill session */
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700659 rc = cifs_verify_signature(&rqst, server,
Jeff Layton0124cc42013-04-03 11:55:03 -0400660 mid->sequence_number);
Steve French985e4ff02012-08-03 09:42:45 -0500661 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500662 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
663 rc);
Jeff Layton2c8f9812011-05-19 16:22:52 -0400664 }
665
666 /* BB special case reconnect tid and uid here? */
667 return map_smb_to_linux_error(mid->resp_buf, log_error);
668}
669
Jeff Laytonfec344e2012-09-18 16:20:35 -0700670struct mid_q_entry *
671cifs_setup_request(struct cifs_ses *ses, struct smb_rqst *rqst)
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400672{
673 int rc;
Jeff Laytonfec344e2012-09-18 16:20:35 -0700674 struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400675 struct mid_q_entry *mid;
676
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800677 if (rqst->rq_iov[0].iov_len != 4 ||
678 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
679 return ERR_PTR(-EIO);
680
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400681 rc = allocate_mid(ses, hdr, &mid);
682 if (rc)
Jeff Laytonfec344e2012-09-18 16:20:35 -0700683 return ERR_PTR(rc);
684 rc = cifs_sign_rqst(rqst, ses->server, &mid->sequence_number);
685 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700686 cifs_delete_mid(mid);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700687 return ERR_PTR(rc);
688 }
689 return mid;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400690}
691
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800692int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800693cifs_send_recv(const unsigned int xid, struct cifs_ses *ses,
694 struct smb_rqst *rqst, int *resp_buf_type, const int flags,
695 struct kvec *resp_iov)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
697 int rc = 0;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400698 int timeout, optype;
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500699 struct mid_q_entry *midQ;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400700 unsigned int credits = 1;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800701 char *buf;
Steve French50c2f752007-07-13 00:33:32 +0000702
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400703 timeout = flags & CIFS_TIMEOUT_MASK;
704 optype = flags & CIFS_OP_MASK;
Steve French133672e2007-11-13 22:41:37 +0000705
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400706 *resp_buf_type = CIFS_NO_BUFFER; /* no response buf yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Steve French4b8f9302006-02-26 16:41:18 +0000708 if ((ses == NULL) || (ses->server == NULL)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500709 cifs_dbg(VFS, "Null session\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return -EIO;
711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700713 if (ses->server->tcpStatus == CifsExiting)
Steve French31ca3bc2005-04-28 22:41:11 -0700714 return -ENOENT;
715
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400716 /*
717 * Ensure that we do not send more than 50 overlapping requests
718 * to the same server. We may make this configurable later or
719 * use ses->maxReq.
720 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400722 rc = wait_for_free_request(ses->server, timeout, optype);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700723 if (rc)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000724 return rc;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000725
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400726 /*
727 * Make sure that we sign in the same order that we send on this socket
728 * and avoid races inside tcp sendmsg code that could cause corruption
729 * of smb data.
730 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Jeff Layton72ca5452008-12-01 07:09:36 -0500732 mutex_lock(&ses->server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800734 midQ = ses->server->ops->setup_request(ses, rqst);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700735 if (IS_ERR(midQ)) {
Jeff Layton72ca5452008-12-01 07:09:36 -0500736 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000737 /* Update # of requests on wire to server */
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400738 add_credits(ses->server, 1, optype);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700739 return PTR_ERR(midQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400742 midQ->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +0000743 cifs_in_send_inc(ses->server);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700744 rc = smb_send_rqst(ses->server, rqst, flags);
Steve French789e6662011-08-09 18:44:44 +0000745 cifs_in_send_dec(ses->server);
746 cifs_save_when_sent(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000747
Jeff Laytonad313cb2013-04-03 10:27:36 -0400748 if (rc < 0)
749 ses->server->sequence_number -= 2;
Jeff Layton72ca5452008-12-01 07:09:36 -0500750 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000751
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700752 if (rc < 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000753 goto out;
Steve French4b8f9302006-02-26 16:41:18 +0000754
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100755#ifdef CONFIG_CIFS_SMB311
756 if (ses->status == CifsNew)
757 smb311_update_preauth_hash(ses, rqst->rq_iov+1,
758 rqst->rq_nvec-1);
759#endif
760
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700761 if (timeout == CIFS_ASYNC_OP)
Steve French133672e2007-11-13 22:41:37 +0000762 goto out;
Jeff Layton0ade6402011-01-11 07:24:02 -0500763
764 rc = wait_for_response(ses->server, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -0500765 if (rc != 0) {
Sachin Prabhu38bd4902017-03-03 15:41:38 -0800766 cifs_dbg(FYI, "Cancelling wait for mid %llu\n", midQ->mid);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800767 send_cancel(ses->server, rqst, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -0500768 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400769 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
Sachin Prabhu38bd4902017-03-03 15:41:38 -0800770 midQ->mid_flags |= MID_WAIT_CANCELLED;
Jeff Layton1be912d2011-01-28 07:08:28 -0500771 midQ->callback = DeleteMidQEntry;
772 spin_unlock(&GlobalMid_Lock);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400773 add_credits(ses->server, 1, optype);
Jeff Layton1be912d2011-01-28 07:08:28 -0500774 return rc;
775 }
776 spin_unlock(&GlobalMid_Lock);
777 }
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500778
Jeff Layton3c1105d2011-05-22 07:09:13 -0400779 rc = cifs_sync_mid_result(midQ, ses->server);
Jeff Layton053d5032011-01-11 07:24:02 -0500780 if (rc != 0) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400781 add_credits(ses->server, 1, optype);
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500782 return rc;
783 }
Steve French50c2f752007-07-13 00:33:32 +0000784
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400785 if (!midQ->resp_buf || midQ->mid_state != MID_RESPONSE_RECEIVED) {
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500786 rc = -EIO;
Joe Perchesf96637b2013-05-04 22:12:25 -0500787 cifs_dbg(FYI, "Bad MID state?\n");
Steve French2b2bdfb2008-12-11 17:26:54 +0000788 goto out;
789 }
Steve French84afc292005-12-02 13:32:45 -0800790
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400791 buf = (char *)midQ->resp_buf;
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700792 resp_iov->iov_base = buf;
Ronnie Sahlberge19b2bc2018-04-09 18:06:28 +1000793 resp_iov->iov_len = midQ->resp_buf_size +
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100794 ses->server->vals->header_preamble_size;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400795 if (midQ->large_buf)
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400796 *resp_buf_type = CIFS_LARGE_BUFFER;
Jeff Layton2c8f9812011-05-19 16:22:52 -0400797 else
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400798 *resp_buf_type = CIFS_SMALL_BUFFER;
799
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100800#ifdef CONFIG_CIFS_SMB311
801 if (ses->status == CifsNew) {
802 struct kvec iov = {
803 .iov_base = buf + 4,
804 .iov_len = get_rfc1002_length(buf)
805 };
806 smb311_update_preauth_hash(ses, &iov, 1);
807 }
808#endif
809
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400810 credits = ses->server->ops->get_credits(midQ);
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500811
Pavel Shilovsky082d0642012-05-17 12:18:21 +0400812 rc = ses->server->ops->check_receive(midQ, ses->server,
813 flags & CIFS_LOG_ERROR);
Steve French2b2bdfb2008-12-11 17:26:54 +0000814
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700815 /* mark it so buf will not be freed by cifs_delete_mid */
Jeff Layton2c8f9812011-05-19 16:22:52 -0400816 if ((flags & CIFS_NO_RESP) == 0)
817 midQ->resp_buf = NULL;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000818out:
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700819 cifs_delete_mid(midQ);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400820 add_credits(ses->server, credits, optype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 return rc;
823}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800826SendReceive2(const unsigned int xid, struct cifs_ses *ses,
827 struct kvec *iov, int n_vec, int *resp_buf_type /* ret */,
828 const int flags, struct kvec *resp_iov)
829{
830 struct smb_rqst rqst;
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +1100831 struct kvec s_iov[CIFS_MAX_IOV_SIZE], *new_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800832 int rc;
833
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +1100834 if (n_vec + 1 > CIFS_MAX_IOV_SIZE) {
835 new_iov = kmalloc(sizeof(struct kvec) * (n_vec + 1),
836 GFP_KERNEL);
Steve French117e3b72018-04-22 10:24:19 -0500837 if (!new_iov) {
838 /* otherwise cifs_send_recv below sets resp_buf_type */
839 *resp_buf_type = CIFS_NO_BUFFER;
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +1100840 return -ENOMEM;
Steve French117e3b72018-04-22 10:24:19 -0500841 }
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +1100842 } else
843 new_iov = s_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800844
845 /* 1st iov is a RFC1001 length followed by the rest of the packet */
846 memcpy(new_iov + 1, iov, (sizeof(struct kvec) * n_vec));
847
848 new_iov[0].iov_base = new_iov[1].iov_base;
849 new_iov[0].iov_len = 4;
850 new_iov[1].iov_base += 4;
851 new_iov[1].iov_len -= 4;
852
853 memset(&rqst, 0, sizeof(struct smb_rqst));
854 rqst.rq_iov = new_iov;
855 rqst.rq_nvec = n_vec + 1;
856
857 rc = cifs_send_recv(xid, ses, &rqst, resp_buf_type, flags, resp_iov);
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +1100858 if (n_vec + 1 > CIFS_MAX_IOV_SIZE)
859 kfree(new_iov);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800860 return rc;
861}
862
Ronnie Sahlberg83b77392017-11-09 12:14:15 +1100863/* Like SendReceive2 but iov[0] does not contain an rfc1002 header */
864int
865smb2_send_recv(const unsigned int xid, struct cifs_ses *ses,
866 struct kvec *iov, int n_vec, int *resp_buf_type /* ret */,
867 const int flags, struct kvec *resp_iov)
868{
869 struct smb_rqst rqst;
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +1100870 struct kvec s_iov[CIFS_MAX_IOV_SIZE], *new_iov;
Ronnie Sahlberg83b77392017-11-09 12:14:15 +1100871 int rc;
872 int i;
873 __u32 count;
874 __be32 rfc1002_marker;
875
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +1100876 if (n_vec + 1 > CIFS_MAX_IOV_SIZE) {
877 new_iov = kmalloc(sizeof(struct kvec) * (n_vec + 1),
878 GFP_KERNEL);
879 if (!new_iov)
880 return -ENOMEM;
881 } else
882 new_iov = s_iov;
Ronnie Sahlberg83b77392017-11-09 12:14:15 +1100883
884 /* 1st iov is an RFC1002 Session Message length */
885 memcpy(new_iov + 1, iov, (sizeof(struct kvec) * n_vec));
886
887 count = 0;
888 for (i = 1; i < n_vec + 1; i++)
889 count += new_iov[i].iov_len;
890
891 rfc1002_marker = cpu_to_be32(count);
892
893 new_iov[0].iov_base = &rfc1002_marker;
894 new_iov[0].iov_len = 4;
895
896 memset(&rqst, 0, sizeof(struct smb_rqst));
897 rqst.rq_iov = new_iov;
898 rqst.rq_nvec = n_vec + 1;
899
900 rc = cifs_send_recv(xid, ses, &rqst, resp_buf_type, flags, resp_iov);
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +1100901 if (n_vec + 1 > CIFS_MAX_IOV_SIZE)
902 kfree(new_iov);
Ronnie Sahlberg83b77392017-11-09 12:14:15 +1100903 return rc;
904}
905
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800906int
Steve French96daf2b2011-05-27 04:34:02 +0000907SendReceive(const unsigned int xid, struct cifs_ses *ses,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400909 int *pbytes_returned, const int timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
911 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 struct mid_q_entry *midQ;
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800913 unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
914 struct kvec iov = { .iov_base = in_buf, .iov_len = len };
915 struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917 if (ses == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500918 cifs_dbg(VFS, "Null smb session\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return -EIO;
920 }
Steve French79a58d12007-07-06 22:44:50 +0000921 if (ses->server == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500922 cifs_dbg(VFS, "Null tcp session\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return -EIO;
924 }
925
Steve French79a58d12007-07-06 22:44:50 +0000926 if (ses->server->tcpStatus == CifsExiting)
Steve French31ca3bc2005-04-28 22:41:11 -0700927 return -ENOENT;
928
Steve French79a58d12007-07-06 22:44:50 +0000929 /* Ensure that we do not send more than 50 overlapping requests
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 to the same server. We may make this configurable later or
931 use ses->maxReq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800933 if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500934 cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800935 len);
Volker Lendecke6d9c6d52008-12-08 20:50:24 +0000936 return -EIO;
937 }
938
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400939 rc = wait_for_free_request(ses->server, timeout, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000940 if (rc)
941 return rc;
942
Steve French79a58d12007-07-06 22:44:50 +0000943 /* make sure that we sign in the same order that we send on this socket
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 and avoid races inside tcp sendmsg code that could cause corruption
945 of smb data */
946
Jeff Layton72ca5452008-12-01 07:09:36 -0500947 mutex_lock(&ses->server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000949 rc = allocate_mid(ses, in_buf, &midQ);
950 if (rc) {
Jeff Layton72ca5452008-12-01 07:09:36 -0500951 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000952 /* Update # of requests on wire to server */
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400953 add_credits(ses->server, 1, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000954 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
956
Steve Frenchad009ac2005-04-28 22:41:05 -0700957 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
Volker Lendecke829049c2008-12-06 16:00:53 +0100958 if (rc) {
959 mutex_unlock(&ses->server->srv_mutex);
960 goto out;
961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400963 midQ->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +0000964
965 cifs_in_send_inc(ses->server);
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800966 rc = smb_send(ses->server, in_buf, len);
Steve French789e6662011-08-09 18:44:44 +0000967 cifs_in_send_dec(ses->server);
968 cifs_save_when_sent(midQ);
Jeff Laytonad313cb2013-04-03 10:27:36 -0400969
970 if (rc < 0)
971 ses->server->sequence_number -= 2;
972
Jeff Layton72ca5452008-12-01 07:09:36 -0500973 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000974
Steve French79a58d12007-07-06 22:44:50 +0000975 if (rc < 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000976 goto out;
977
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400978 if (timeout == CIFS_ASYNC_OP)
Steve French133672e2007-11-13 22:41:37 +0000979 goto out;
Jeff Layton0ade6402011-01-11 07:24:02 -0500980
981 rc = wait_for_response(ses->server, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -0500982 if (rc != 0) {
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800983 send_cancel(ses->server, &rqst, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -0500984 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400985 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
Jeff Layton1be912d2011-01-28 07:08:28 -0500986 /* no longer considered to be "in-flight" */
987 midQ->callback = DeleteMidQEntry;
988 spin_unlock(&GlobalMid_Lock);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400989 add_credits(ses->server, 1, 0);
Jeff Layton1be912d2011-01-28 07:08:28 -0500990 return rc;
991 }
992 spin_unlock(&GlobalMid_Lock);
993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Jeff Layton3c1105d2011-05-22 07:09:13 -0400995 rc = cifs_sync_mid_result(midQ, ses->server);
Jeff Layton053d5032011-01-11 07:24:02 -0500996 if (rc != 0) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400997 add_credits(ses->server, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 return rc;
999 }
Steve French50c2f752007-07-13 00:33:32 +00001000
Jeff Layton2c8f9812011-05-19 16:22:52 -04001001 if (!midQ->resp_buf || !out_buf ||
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001002 midQ->mid_state != MID_RESPONSE_RECEIVED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 rc = -EIO;
Joe Perchesf96637b2013-05-04 22:12:25 -05001004 cifs_dbg(VFS, "Bad MID state?\n");
Steve French2b2bdfb2008-12-11 17:26:54 +00001005 goto out;
1006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001008 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
Jeff Layton2c8f9812011-05-19 16:22:52 -04001009 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
1010 rc = cifs_check_receive(midQ, ses->server, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001011out:
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001012 cifs_delete_mid(midQ);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001013 add_credits(ses->server, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 return rc;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001016}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001018/* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
1019 blocking lock to return. */
1020
1021static int
Steve French96daf2b2011-05-27 04:34:02 +00001022send_lock_cancel(const unsigned int xid, struct cifs_tcon *tcon,
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001023 struct smb_hdr *in_buf,
1024 struct smb_hdr *out_buf)
1025{
1026 int bytes_returned;
Steve French96daf2b2011-05-27 04:34:02 +00001027 struct cifs_ses *ses = tcon->ses;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001028 LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
1029
1030 /* We just modify the current in_buf to change
1031 the type of lock from LOCKING_ANDX_SHARED_LOCK
1032 or LOCKING_ANDX_EXCLUSIVE_LOCK to
1033 LOCKING_ANDX_CANCEL_LOCK. */
1034
1035 pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
1036 pSMB->Timeout = 0;
Pavel Shilovsky88257362012-05-23 14:01:59 +04001037 pSMB->hdr.Mid = get_next_mid(ses->server);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001038
1039 return SendReceive(xid, ses, in_buf, out_buf,
Jeff Layton77499812011-01-11 07:24:23 -05001040 &bytes_returned, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001041}
1042
1043int
Steve French96daf2b2011-05-27 04:34:02 +00001044SendReceiveBlockingLock(const unsigned int xid, struct cifs_tcon *tcon,
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001045 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
1046 int *pbytes_returned)
1047{
1048 int rc = 0;
1049 int rstart = 0;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001050 struct mid_q_entry *midQ;
Steve French96daf2b2011-05-27 04:34:02 +00001051 struct cifs_ses *ses;
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001052 unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
1053 struct kvec iov = { .iov_base = in_buf, .iov_len = len };
1054 struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001055
1056 if (tcon == NULL || tcon->ses == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001057 cifs_dbg(VFS, "Null smb session\n");
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001058 return -EIO;
1059 }
1060 ses = tcon->ses;
1061
Steve French79a58d12007-07-06 22:44:50 +00001062 if (ses->server == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001063 cifs_dbg(VFS, "Null tcp session\n");
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001064 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
1066
Steve French79a58d12007-07-06 22:44:50 +00001067 if (ses->server->tcpStatus == CifsExiting)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001068 return -ENOENT;
1069
Steve French79a58d12007-07-06 22:44:50 +00001070 /* Ensure that we do not send more than 50 overlapping requests
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001071 to the same server. We may make this configurable later or
1072 use ses->maxReq */
1073
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001074 if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001075 cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001076 len);
Volker Lendecke6d9c6d52008-12-08 20:50:24 +00001077 return -EIO;
1078 }
1079
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001080 rc = wait_for_free_request(ses->server, CIFS_BLOCKING_OP, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001081 if (rc)
1082 return rc;
1083
Steve French79a58d12007-07-06 22:44:50 +00001084 /* make sure that we sign in the same order that we send on this socket
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001085 and avoid races inside tcp sendmsg code that could cause corruption
1086 of smb data */
1087
Jeff Layton72ca5452008-12-01 07:09:36 -05001088 mutex_lock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001089
1090 rc = allocate_mid(ses, in_buf, &midQ);
1091 if (rc) {
Jeff Layton72ca5452008-12-01 07:09:36 -05001092 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001093 return rc;
1094 }
1095
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001096 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
Volker Lendecke829049c2008-12-06 16:00:53 +01001097 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001098 cifs_delete_mid(midQ);
Volker Lendecke829049c2008-12-06 16:00:53 +01001099 mutex_unlock(&ses->server->srv_mutex);
1100 return rc;
1101 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001102
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001103 midQ->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +00001104 cifs_in_send_inc(ses->server);
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001105 rc = smb_send(ses->server, in_buf, len);
Steve French789e6662011-08-09 18:44:44 +00001106 cifs_in_send_dec(ses->server);
1107 cifs_save_when_sent(midQ);
Jeff Laytonad313cb2013-04-03 10:27:36 -04001108
1109 if (rc < 0)
1110 ses->server->sequence_number -= 2;
1111
Jeff Layton72ca5452008-12-01 07:09:36 -05001112 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001113
Steve French79a58d12007-07-06 22:44:50 +00001114 if (rc < 0) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001115 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001116 return rc;
1117 }
1118
1119 /* Wait for a reply - allow signals to interrupt. */
1120 rc = wait_event_interruptible(ses->server->response_q,
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001121 (!(midQ->mid_state == MID_REQUEST_SUBMITTED)) ||
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001122 ((ses->server->tcpStatus != CifsGood) &&
1123 (ses->server->tcpStatus != CifsNew)));
1124
1125 /* Were we interrupted by a signal ? */
1126 if ((rc == -ERESTARTSYS) &&
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001127 (midQ->mid_state == MID_REQUEST_SUBMITTED) &&
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001128 ((ses->server->tcpStatus == CifsGood) ||
1129 (ses->server->tcpStatus == CifsNew))) {
1130
1131 if (in_buf->Command == SMB_COM_TRANSACTION2) {
1132 /* POSIX lock. We send a NT_CANCEL SMB to cause the
1133 blocking lock to return. */
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001134 rc = send_cancel(ses->server, &rqst, midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001135 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001136 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001137 return rc;
1138 }
1139 } else {
1140 /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
1141 to cause the blocking lock to return. */
1142
1143 rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
1144
1145 /* If we get -ENOLCK back the lock may have
1146 already been removed. Don't exit in this case. */
1147 if (rc && rc != -ENOLCK) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001148 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001149 return rc;
1150 }
1151 }
1152
Jeff Layton1be912d2011-01-28 07:08:28 -05001153 rc = wait_for_response(ses->server, midQ);
1154 if (rc) {
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001155 send_cancel(ses->server, &rqst, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -05001156 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001157 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
Jeff Layton1be912d2011-01-28 07:08:28 -05001158 /* no longer considered to be "in-flight" */
1159 midQ->callback = DeleteMidQEntry;
1160 spin_unlock(&GlobalMid_Lock);
1161 return rc;
1162 }
1163 spin_unlock(&GlobalMid_Lock);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001164 }
Jeff Layton1be912d2011-01-28 07:08:28 -05001165
1166 /* We got the response - restart system call. */
1167 rstart = 1;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001168 }
1169
Jeff Layton3c1105d2011-05-22 07:09:13 -04001170 rc = cifs_sync_mid_result(midQ, ses->server);
Jeff Layton053d5032011-01-11 07:24:02 -05001171 if (rc != 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001172 return rc;
Steve French50c2f752007-07-13 00:33:32 +00001173
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001174 /* rcvd frame is ok */
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001175 if (out_buf == NULL || midQ->mid_state != MID_RESPONSE_RECEIVED) {
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001176 rc = -EIO;
Joe Perchesf96637b2013-05-04 22:12:25 -05001177 cifs_dbg(VFS, "Bad MID state?\n");
Volker Lendecke698e96a2008-12-06 16:39:31 +01001178 goto out;
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001179 }
1180
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001181 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
Jeff Layton2c8f9812011-05-19 16:22:52 -04001182 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
1183 rc = cifs_check_receive(midQ, ses->server, 0);
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001184out:
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001185 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001186 if (rstart && rc == -EACCES)
1187 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 return rc;
1189}