blob: 63f25f919b24eea24e2e68cc6cd77bb8b4a34966 [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
Long Lic06a0f22018-05-30 12:47:57 -0700215 /*
216 * Add in the page array if there is one. The caller needs to make
217 * sure rq_offset and rq_tailsz are set correctly. If a buffer of
218 * multiple pages ends at page boundary, rq_tailsz needs to be set to
219 * PAGE_SIZE.
220 */
Jeff Laytona26054d2014-02-14 07:21:00 -0500221 if (rqst->rq_npages) {
Long Lic06a0f22018-05-30 12:47:57 -0700222 if (rqst->rq_npages == 1)
223 buflen += rqst->rq_tailsz;
224 else {
225 /*
226 * If there is more than one page, calculate the
227 * buffer length based on rq_offset and rq_tailsz
228 */
229 buflen += rqst->rq_pagesz * (rqst->rq_npages - 1) -
230 rqst->rq_offset;
231 buflen += rqst->rq_tailsz;
232 }
Jeff Laytona26054d2014-02-14 07:21:00 -0500233 }
234
235 return buflen;
236}
237
Jeff Layton6f49f462012-09-18 16:20:34 -0700238static int
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700239__smb_send_rqst(struct TCP_Server_Info *server, struct smb_rqst *rqst)
Jeff Layton6f49f462012-09-18 16:20:34 -0700240{
241 int rc;
242 struct kvec *iov = rqst->rq_iov;
243 int n_vec = rqst->rq_nvec;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000244 unsigned int send_length;
Jeff Layton97bc00b2012-09-18 16:20:35 -0700245 unsigned int i;
Al Viro3ab3f2a2015-11-13 02:36:04 -0500246 size_t total_len = 0, sent, size;
Jeff Laytonb8eed282012-09-18 16:20:35 -0700247 struct socket *ssocket = server->ssocket;
Al Viro3ab3f2a2015-11-13 02:36:04 -0500248 struct msghdr smb_msg;
Jeff Laytonb8eed282012-09-18 16:20:35 -0700249 int val = 1;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000250 __be32 rfc1002_marker;
251
Long Li9762c2d2017-11-22 17:38:43 -0700252 if (cifs_rdma_enabled(server) && server->smbd_conn) {
253 rc = smbd_send(server->smbd_conn, rqst);
254 goto smbd_done;
255 }
Jeff Laytonea702b82012-12-27 07:28:55 -0500256 if (ssocket == NULL)
257 return -ENOTSOCK;
258
Jeff Laytona26054d2014-02-14 07:21:00 -0500259 send_length = rqst_len(rqst);
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000260 rfc1002_marker = cpu_to_be32(send_length);
Jeff Layton6f49f462012-09-18 16:20:34 -0700261
Jeff Laytonb8eed282012-09-18 16:20:35 -0700262 /* cork the socket */
263 kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
264 (char *)&val, sizeof(val));
265
Al Viro3ab3f2a2015-11-13 02:36:04 -0500266 size = 0;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000267 /* Generate a rfc1002 marker for SMB2+ */
268 if (server->vals->header_preamble_size == 0) {
269 struct kvec hiov = {
270 .iov_base = &rfc1002_marker,
271 .iov_len = 4
272 };
273 iov_iter_kvec(&smb_msg.msg_iter, WRITE | ITER_KVEC, &hiov,
274 1, 4);
275 rc = smb_send_kvec(server, &smb_msg, &sent);
276 if (rc < 0)
277 goto uncork;
278
279 total_len += sent;
280 send_length += 4;
281 }
282
283 cifs_dbg(FYI, "Sending smb: smb_len=%u\n", send_length);
284 dump_smb(iov[0].iov_base, iov[0].iov_len);
285 dump_smb(iov[1].iov_base, iov[1].iov_len);
286
Al Viro3ab3f2a2015-11-13 02:36:04 -0500287 for (i = 0; i < n_vec; i++)
288 size += iov[i].iov_len;
289
290 iov_iter_kvec(&smb_msg.msg_iter, WRITE | ITER_KVEC, iov, n_vec, size);
291
292 rc = smb_send_kvec(server, &smb_msg, &sent);
Jeff Layton97bc00b2012-09-18 16:20:35 -0700293 if (rc < 0)
294 goto uncork;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Jeff Layton97bc00b2012-09-18 16:20:35 -0700296 total_len += sent;
297
298 /* now walk the page array and send each page in it */
299 for (i = 0; i < rqst->rq_npages; i++) {
Long Lie8157b22018-05-30 12:47:59 -0700300 struct bio_vec bvec;
301
302 bvec.bv_page = rqst->rq_pages[i];
303 rqst_page_get_length(rqst, i, &bvec.bv_len, &bvec.bv_offset);
304
Al Viro3ab3f2a2015-11-13 02:36:04 -0500305 iov_iter_bvec(&smb_msg.msg_iter, WRITE | ITER_BVEC,
Long Lie8157b22018-05-30 12:47:59 -0700306 &bvec, 1, bvec.bv_len);
Al Viro3ab3f2a2015-11-13 02:36:04 -0500307 rc = smb_send_kvec(server, &smb_msg, &sent);
Jeff Layton97bc00b2012-09-18 16:20:35 -0700308 if (rc < 0)
309 break;
310
311 total_len += sent;
312 }
313
314uncork:
Jeff Laytonb8eed282012-09-18 16:20:35 -0700315 /* uncork it */
316 val = 0;
317 kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
318 (char *)&val, sizeof(val));
319
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000320 if ((total_len > 0) && (total_len != send_length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500321 cifs_dbg(FYI, "partial send (wanted=%u sent=%zu): terminating session\n",
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000322 send_length, total_len);
Jeff Layton6f49f462012-09-18 16:20:34 -0700323 /*
324 * If we have only sent part of an SMB then the next SMB could
325 * be taken as the remainder of this one. We need to kill the
326 * socket so the server throws away the partial SMB
327 */
Steve Frenchedf1ae42008-10-29 00:47:57 +0000328 server->tcpStatus = CifsNeedReconnect;
329 }
Long Li9762c2d2017-11-22 17:38:43 -0700330smbd_done:
Jeff Laytond804d412011-01-28 15:05:43 -0500331 if (rc < 0 && rc != -EINTR)
Joe Perchesf96637b2013-05-04 22:12:25 -0500332 cifs_dbg(VFS, "Error %d sending data on socket to server\n",
333 rc);
Jeff Laytond804d412011-01-28 15:05:43 -0500334 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 return rc;
338}
339
Jeff Layton6f49f462012-09-18 16:20:34 -0700340static int
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700341smb_send_rqst(struct TCP_Server_Info *server, struct smb_rqst *rqst, int flags)
Jeff Layton6f49f462012-09-18 16:20:34 -0700342{
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700343 struct smb_rqst cur_rqst;
344 int rc;
Jeff Layton6f49f462012-09-18 16:20:34 -0700345
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700346 if (!(flags & CIFS_TRANSFORM_REQ))
347 return __smb_send_rqst(server, rqst);
348
349 if (!server->ops->init_transform_rq ||
350 !server->ops->free_transform_rq) {
351 cifs_dbg(VFS, "Encryption requested but transform callbacks are missed\n");
352 return -EIO;
353 }
354
355 rc = server->ops->init_transform_rq(server, &cur_rqst, rqst);
356 if (rc)
357 return rc;
358
359 rc = __smb_send_rqst(server, &cur_rqst);
360 server->ops->free_transform_rq(&cur_rqst);
361 return rc;
Jeff Layton6f49f462012-09-18 16:20:34 -0700362}
363
Jeff Layton0496e022008-12-30 12:39:16 -0500364int
365smb_send(struct TCP_Server_Info *server, struct smb_hdr *smb_buffer,
366 unsigned int smb_buf_length)
367{
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800368 struct kvec iov[2];
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700369 struct smb_rqst rqst = { .rq_iov = iov,
370 .rq_nvec = 2 };
Jeff Layton0496e022008-12-30 12:39:16 -0500371
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800372 iov[0].iov_base = smb_buffer;
373 iov[0].iov_len = 4;
374 iov[1].iov_base = (char *)smb_buffer + 4;
375 iov[1].iov_len = smb_buf_length;
Jeff Layton0496e022008-12-30 12:39:16 -0500376
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700377 return __smb_send_rqst(server, &rqst);
Jeff Layton0496e022008-12-30 12:39:16 -0500378}
379
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300380static int
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400381wait_for_free_credits(struct TCP_Server_Info *server, const int timeout,
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300382 int *credits)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000383{
Pavel Shilovsky5bc59492012-02-21 19:56:08 +0300384 int rc;
385
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300386 spin_lock(&server->req_lock);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400387 if (timeout == CIFS_ASYNC_OP) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000388 /* oplock breaks must not be held up */
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300389 server->in_flight++;
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300390 *credits -= 1;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300391 spin_unlock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000392 return 0;
393 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000394
Volker Lendecke27a97a62008-12-08 20:59:39 +0000395 while (1) {
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300396 if (*credits <= 0) {
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300397 spin_unlock(&server->req_lock);
Steve French789e6662011-08-09 18:44:44 +0000398 cifs_num_waiters_inc(server);
Pavel Shilovsky5bc59492012-02-21 19:56:08 +0300399 rc = wait_event_killable(server->request_q,
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300400 has_credits(server, credits));
Steve French789e6662011-08-09 18:44:44 +0000401 cifs_num_waiters_dec(server);
Pavel Shilovsky5bc59492012-02-21 19:56:08 +0300402 if (rc)
403 return rc;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300404 spin_lock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000405 } else {
Jeff Laytonc5797a92011-01-11 07:24:01 -0500406 if (server->tcpStatus == CifsExiting) {
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300407 spin_unlock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000408 return -ENOENT;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000409 }
Volker Lendecke27a97a62008-12-08 20:59:39 +0000410
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400411 /*
412 * Can not count locking commands against total
413 * as they are allowed to block on server.
414 */
Volker Lendecke27a97a62008-12-08 20:59:39 +0000415
416 /* update # of requests on the wire to server */
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400417 if (timeout != CIFS_BLOCKING_OP) {
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300418 *credits -= 1;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300419 server->in_flight++;
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400420 }
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300421 spin_unlock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000422 break;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000423 }
424 }
425 return 0;
426}
427
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300428static int
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400429wait_for_free_request(struct TCP_Server_Info *server, const int timeout,
430 const int optype)
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300431{
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -0500432 int *val;
433
434 val = server->ops->get_credits_field(server, optype);
435 /* Since an echo is already inflight, no need to wait to send another */
436 if (*val <= 0 && optype == CIFS_ECHO_OP)
437 return -EAGAIN;
438 return wait_for_free_credits(server, timeout, val);
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300439}
440
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400441int
442cifs_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
443 unsigned int *num, unsigned int *credits)
444{
445 *num = size;
446 *credits = 0;
447 return 0;
448}
449
Steve French96daf2b2011-05-27 04:34:02 +0000450static int allocate_mid(struct cifs_ses *ses, struct smb_hdr *in_buf,
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000451 struct mid_q_entry **ppmidQ)
452{
453 if (ses->server->tcpStatus == CifsExiting) {
454 return -ENOENT;
Volker Lendecke8fbbd362008-12-06 13:12:34 +0100455 }
456
457 if (ses->server->tcpStatus == CifsNeedReconnect) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500458 cifs_dbg(FYI, "tcp session dead - return to caller to retry\n");
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000459 return -EAGAIN;
Volker Lendecke8fbbd362008-12-06 13:12:34 +0100460 }
461
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500462 if (ses->status == CifsNew) {
Steve French79a58d12007-07-06 22:44:50 +0000463 if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
Steve Frenchad7a2922008-02-07 23:25:02 +0000464 (in_buf->Command != SMB_COM_NEGOTIATE))
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000465 return -EAGAIN;
Steve Frenchad7a2922008-02-07 23:25:02 +0000466 /* else ok - we are setting up session */
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000467 }
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500468
469 if (ses->status == CifsExiting) {
470 /* check if SMB session is bad because we are setting it up */
471 if (in_buf->Command != SMB_COM_LOGOFF_ANDX)
472 return -EAGAIN;
473 /* else ok - we are shutting down session */
474 }
475
Jeff Layton24b9b062008-12-01 07:09:34 -0500476 *ppmidQ = AllocMidQEntry(in_buf, ses->server);
Steve French26f57362007-08-30 22:09:15 +0000477 if (*ppmidQ == NULL)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000478 return -ENOMEM;
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500479 spin_lock(&GlobalMid_Lock);
480 list_add_tail(&(*ppmidQ)->qhead, &ses->server->pending_mid_q);
481 spin_unlock(&GlobalMid_Lock);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000482 return 0;
483}
484
Jeff Layton0ade6402011-01-11 07:24:02 -0500485static int
486wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *midQ)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000487{
Jeff Layton0ade6402011-01-11 07:24:02 -0500488 int error;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000489
Colin Cross5853cc22013-05-07 17:52:05 +0000490 error = wait_event_freezekillable_unsafe(server->response_q,
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400491 midQ->mid_state != MID_REQUEST_SUBMITTED);
Jeff Layton0ade6402011-01-11 07:24:02 -0500492 if (error < 0)
493 return -ERESTARTSYS;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000494
Jeff Layton0ade6402011-01-11 07:24:02 -0500495 return 0;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000496}
497
Jeff Laytonfec344e2012-09-18 16:20:35 -0700498struct mid_q_entry *
499cifs_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400500{
501 int rc;
Jeff Laytonfec344e2012-09-18 16:20:35 -0700502 struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400503 struct mid_q_entry *mid;
504
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800505 if (rqst->rq_iov[0].iov_len != 4 ||
506 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
507 return ERR_PTR(-EIO);
508
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400509 /* enable signing if server requires it */
Jeff Layton38d77c52013-05-26 07:01:00 -0400510 if (server->sign)
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400511 hdr->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
512
513 mid = AllocMidQEntry(hdr, server);
514 if (mid == NULL)
Jeff Laytonfec344e2012-09-18 16:20:35 -0700515 return ERR_PTR(-ENOMEM);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400516
Jeff Laytonfec344e2012-09-18 16:20:35 -0700517 rc = cifs_sign_rqst(rqst, server, &mid->sequence_number);
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100518 if (rc) {
519 DeleteMidQEntry(mid);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700520 return ERR_PTR(rc);
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100521 }
522
Jeff Laytonfec344e2012-09-18 16:20:35 -0700523 return mid;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400524}
Steve French133672e2007-11-13 22:41:37 +0000525
526/*
Jeff Laytona6827c12011-01-11 07:24:21 -0500527 * Send a SMB request and set the callback function in the mid to handle
528 * the result. Caller is responsible for dealing with timeouts.
529 */
530int
Jeff Laytonfec344e2012-09-18 16:20:35 -0700531cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst,
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -0800532 mid_receive_t *receive, mid_callback_t *callback,
533 mid_handle_t *handle, void *cbdata, const int flags)
Jeff Laytona6827c12011-01-11 07:24:21 -0500534{
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400535 int rc, timeout, optype;
Jeff Laytona6827c12011-01-11 07:24:21 -0500536 struct mid_q_entry *mid;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400537 unsigned int credits = 0;
Jeff Laytona6827c12011-01-11 07:24:21 -0500538
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400539 timeout = flags & CIFS_TIMEOUT_MASK;
540 optype = flags & CIFS_OP_MASK;
541
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400542 if ((flags & CIFS_HAS_CREDITS) == 0) {
543 rc = wait_for_free_request(server, timeout, optype);
544 if (rc)
545 return rc;
546 credits = 1;
547 }
Jeff Laytona6827c12011-01-11 07:24:21 -0500548
549 mutex_lock(&server->srv_mutex);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700550 mid = server->ops->setup_async_request(server, rqst);
551 if (IS_ERR(mid)) {
Jeff Laytona6827c12011-01-11 07:24:21 -0500552 mutex_unlock(&server->srv_mutex);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400553 add_credits_and_wake_if(server, credits, optype);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700554 return PTR_ERR(mid);
Jeff Laytona6827c12011-01-11 07:24:21 -0500555 }
556
Jeff Layton44d22d82011-10-19 15:29:49 -0400557 mid->receive = receive;
Jeff Laytona6827c12011-01-11 07:24:21 -0500558 mid->callback = callback;
559 mid->callback_data = cbdata;
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -0800560 mid->handle = handle;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400561 mid->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +0000562
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100563 /* put it on the pending_mid_q */
564 spin_lock(&GlobalMid_Lock);
565 list_add_tail(&mid->qhead, &server->pending_mid_q);
566 spin_unlock(&GlobalMid_Lock);
567
Long Li93d2cb62017-06-28 15:55:55 -0700568 /*
569 * Need to store the time in mid before calling I/O. For call_async,
570 * I/O response may come back and free the mid entry on another thread.
571 */
572 cifs_save_when_sent(mid);
Steve French789e6662011-08-09 18:44:44 +0000573 cifs_in_send_inc(server);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700574 rc = smb_send_rqst(server, rqst, flags);
Steve French789e6662011-08-09 18:44:44 +0000575 cifs_in_send_dec(server);
Jeff Laytonad313cb2013-04-03 10:27:36 -0400576
Rabin Vincent820962d2015-12-23 07:32:41 +0100577 if (rc < 0) {
Jeff Laytonad313cb2013-04-03 10:27:36 -0400578 server->sequence_number -= 2;
Rabin Vincent820962d2015-12-23 07:32:41 +0100579 cifs_delete_mid(mid);
580 }
581
Jeff Laytona6827c12011-01-11 07:24:21 -0500582 mutex_unlock(&server->srv_mutex);
Steve French789e6662011-08-09 18:44:44 +0000583
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100584 if (rc == 0)
585 return 0;
Jeff Laytona6827c12011-01-11 07:24:21 -0500586
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400587 add_credits_and_wake_if(server, credits, optype);
Jeff Laytona6827c12011-01-11 07:24:21 -0500588 return rc;
589}
590
591/*
Steve French133672e2007-11-13 22:41:37 +0000592 *
593 * Send an SMB Request. No response info (other than return code)
594 * needs to be parsed.
595 *
596 * flags indicate the type of request buffer and how long to wait
597 * and whether to log NT STATUS code (error) before mapping it to POSIX error
598 *
599 */
600int
Steve French96daf2b2011-05-27 04:34:02 +0000601SendReceiveNoRsp(const unsigned int xid, struct cifs_ses *ses,
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400602 char *in_buf, int flags)
Steve French133672e2007-11-13 22:41:37 +0000603{
604 int rc;
605 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700606 struct kvec rsp_iov;
Steve French133672e2007-11-13 22:41:37 +0000607 int resp_buf_type;
608
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400609 iov[0].iov_base = in_buf;
610 iov[0].iov_len = get_rfc1002_length(in_buf) + 4;
Steve French133672e2007-11-13 22:41:37 +0000611 flags |= CIFS_NO_RESP;
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700612 rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Joe Perchesf96637b2013-05-04 22:12:25 -0500613 cifs_dbg(NOISY, "SendRcvNoRsp flags %d rc %d\n", flags, rc);
Steve French90c81e02008-02-12 20:32:36 +0000614
Steve French133672e2007-11-13 22:41:37 +0000615 return rc;
616}
617
Jeff Layton053d5032011-01-11 07:24:02 -0500618static int
Jeff Layton3c1105d2011-05-22 07:09:13 -0400619cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
Jeff Layton053d5032011-01-11 07:24:02 -0500620{
621 int rc = 0;
622
Joe Perchesf96637b2013-05-04 22:12:25 -0500623 cifs_dbg(FYI, "%s: cmd=%d mid=%llu state=%d\n",
624 __func__, le16_to_cpu(mid->command), mid->mid, mid->mid_state);
Jeff Layton053d5032011-01-11 07:24:02 -0500625
Jeff Layton74dd92a2011-01-11 07:24:02 -0500626 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400627 switch (mid->mid_state) {
Jeff Layton74dd92a2011-01-11 07:24:02 -0500628 case MID_RESPONSE_RECEIVED:
Jeff Layton053d5032011-01-11 07:24:02 -0500629 spin_unlock(&GlobalMid_Lock);
630 return rc;
Jeff Layton74dd92a2011-01-11 07:24:02 -0500631 case MID_RETRY_NEEDED:
632 rc = -EAGAIN;
633 break;
Jeff Layton71823ba2011-02-10 08:03:50 -0500634 case MID_RESPONSE_MALFORMED:
635 rc = -EIO;
636 break;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400637 case MID_SHUTDOWN:
638 rc = -EHOSTDOWN;
639 break;
Jeff Layton74dd92a2011-01-11 07:24:02 -0500640 default:
Jeff Layton3c1105d2011-05-22 07:09:13 -0400641 list_del_init(&mid->qhead);
Joe Perchesf96637b2013-05-04 22:12:25 -0500642 cifs_dbg(VFS, "%s: invalid mid state mid=%llu state=%d\n",
643 __func__, mid->mid, mid->mid_state);
Jeff Layton74dd92a2011-01-11 07:24:02 -0500644 rc = -EIO;
Jeff Layton053d5032011-01-11 07:24:02 -0500645 }
646 spin_unlock(&GlobalMid_Lock);
647
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500648 DeleteMidQEntry(mid);
Jeff Layton053d5032011-01-11 07:24:02 -0500649 return rc;
650}
651
Jeff Layton121b0462012-05-15 12:21:10 -0400652static inline int
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800653send_cancel(struct TCP_Server_Info *server, struct smb_rqst *rqst,
654 struct mid_q_entry *mid)
Jeff Layton76dcc262011-01-11 07:24:24 -0500655{
Jeff Layton121b0462012-05-15 12:21:10 -0400656 return server->ops->send_cancel ?
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800657 server->ops->send_cancel(server, rqst, mid) : 0;
Jeff Layton76dcc262011-01-11 07:24:24 -0500658}
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660int
Jeff Layton2c8f9812011-05-19 16:22:52 -0400661cifs_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
662 bool log_error)
663{
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400664 unsigned int len = get_rfc1002_length(mid->resp_buf) + 4;
Jeff Layton826a95e2011-10-11 06:41:32 -0400665
666 dump_smb(mid->resp_buf, min_t(u32, 92, len));
Jeff Layton2c8f9812011-05-19 16:22:52 -0400667
668 /* convert the length into a more usable form */
Jeff Layton38d77c52013-05-26 07:01:00 -0400669 if (server->sign) {
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800670 struct kvec iov[2];
Steve French985e4ff02012-08-03 09:42:45 -0500671 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800672 struct smb_rqst rqst = { .rq_iov = iov,
673 .rq_nvec = 2 };
Jeff Layton826a95e2011-10-11 06:41:32 -0400674
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800675 iov[0].iov_base = mid->resp_buf;
676 iov[0].iov_len = 4;
677 iov[1].iov_base = (char *)mid->resp_buf + 4;
678 iov[1].iov_len = len - 4;
Jeff Layton2c8f9812011-05-19 16:22:52 -0400679 /* FIXME: add code to kill session */
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700680 rc = cifs_verify_signature(&rqst, server,
Jeff Layton0124cc42013-04-03 11:55:03 -0400681 mid->sequence_number);
Steve French985e4ff02012-08-03 09:42:45 -0500682 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500683 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
684 rc);
Jeff Layton2c8f9812011-05-19 16:22:52 -0400685 }
686
687 /* BB special case reconnect tid and uid here? */
688 return map_smb_to_linux_error(mid->resp_buf, log_error);
689}
690
Jeff Laytonfec344e2012-09-18 16:20:35 -0700691struct mid_q_entry *
692cifs_setup_request(struct cifs_ses *ses, struct smb_rqst *rqst)
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400693{
694 int rc;
Jeff Laytonfec344e2012-09-18 16:20:35 -0700695 struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400696 struct mid_q_entry *mid;
697
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800698 if (rqst->rq_iov[0].iov_len != 4 ||
699 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
700 return ERR_PTR(-EIO);
701
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400702 rc = allocate_mid(ses, hdr, &mid);
703 if (rc)
Jeff Laytonfec344e2012-09-18 16:20:35 -0700704 return ERR_PTR(rc);
705 rc = cifs_sign_rqst(rqst, ses->server, &mid->sequence_number);
706 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700707 cifs_delete_mid(mid);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700708 return ERR_PTR(rc);
709 }
710 return mid;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400711}
712
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800713int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800714cifs_send_recv(const unsigned int xid, struct cifs_ses *ses,
715 struct smb_rqst *rqst, int *resp_buf_type, const int flags,
716 struct kvec *resp_iov)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
718 int rc = 0;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400719 int timeout, optype;
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500720 struct mid_q_entry *midQ;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400721 unsigned int credits = 1;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800722 char *buf;
Steve French50c2f752007-07-13 00:33:32 +0000723
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400724 timeout = flags & CIFS_TIMEOUT_MASK;
725 optype = flags & CIFS_OP_MASK;
Steve French133672e2007-11-13 22:41:37 +0000726
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400727 *resp_buf_type = CIFS_NO_BUFFER; /* no response buf yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Steve French4b8f9302006-02-26 16:41:18 +0000729 if ((ses == NULL) || (ses->server == NULL)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500730 cifs_dbg(VFS, "Null session\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return -EIO;
732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700734 if (ses->server->tcpStatus == CifsExiting)
Steve French31ca3bc2005-04-28 22:41:11 -0700735 return -ENOENT;
736
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400737 /*
738 * Ensure that we do not send more than 50 overlapping requests
739 * to the same server. We may make this configurable later or
740 * use ses->maxReq.
741 */
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400742 rc = wait_for_free_request(ses->server, timeout, optype);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700743 if (rc)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000744 return rc;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000745
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400746 /*
747 * Make sure that we sign in the same order that we send on this socket
748 * and avoid races inside tcp sendmsg code that could cause corruption
749 * of smb data.
750 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Jeff Layton72ca5452008-12-01 07:09:36 -0500752 mutex_lock(&ses->server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800754 midQ = ses->server->ops->setup_request(ses, rqst);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700755 if (IS_ERR(midQ)) {
Jeff Layton72ca5452008-12-01 07:09:36 -0500756 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000757 /* Update # of requests on wire to server */
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400758 add_credits(ses->server, 1, optype);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700759 return PTR_ERR(midQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400762 midQ->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +0000763 cifs_in_send_inc(ses->server);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700764 rc = smb_send_rqst(ses->server, rqst, flags);
Steve French789e6662011-08-09 18:44:44 +0000765 cifs_in_send_dec(ses->server);
766 cifs_save_when_sent(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000767
Jeff Laytonad313cb2013-04-03 10:27:36 -0400768 if (rc < 0)
769 ses->server->sequence_number -= 2;
Jeff Layton72ca5452008-12-01 07:09:36 -0500770 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000771
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700772 if (rc < 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000773 goto out;
Steve French4b8f9302006-02-26 16:41:18 +0000774
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100775#ifdef CONFIG_CIFS_SMB311
Steve French0d5ec282018-04-22 19:51:22 -0500776 if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP))
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000777 smb311_update_preauth_hash(ses, rqst->rq_iov,
778 rqst->rq_nvec);
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100779#endif
780
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700781 if (timeout == CIFS_ASYNC_OP)
Steve French133672e2007-11-13 22:41:37 +0000782 goto out;
Jeff Layton0ade6402011-01-11 07:24:02 -0500783
784 rc = wait_for_response(ses->server, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -0500785 if (rc != 0) {
Sachin Prabhu38bd4902017-03-03 15:41:38 -0800786 cifs_dbg(FYI, "Cancelling wait for mid %llu\n", midQ->mid);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800787 send_cancel(ses->server, rqst, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -0500788 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400789 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
Sachin Prabhu38bd4902017-03-03 15:41:38 -0800790 midQ->mid_flags |= MID_WAIT_CANCELLED;
Jeff Layton1be912d2011-01-28 07:08:28 -0500791 midQ->callback = DeleteMidQEntry;
792 spin_unlock(&GlobalMid_Lock);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400793 add_credits(ses->server, 1, optype);
Jeff Layton1be912d2011-01-28 07:08:28 -0500794 return rc;
795 }
796 spin_unlock(&GlobalMid_Lock);
797 }
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500798
Jeff Layton3c1105d2011-05-22 07:09:13 -0400799 rc = cifs_sync_mid_result(midQ, ses->server);
Jeff Layton053d5032011-01-11 07:24:02 -0500800 if (rc != 0) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400801 add_credits(ses->server, 1, optype);
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500802 return rc;
803 }
Steve French50c2f752007-07-13 00:33:32 +0000804
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400805 if (!midQ->resp_buf || midQ->mid_state != MID_RESPONSE_RECEIVED) {
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500806 rc = -EIO;
Joe Perchesf96637b2013-05-04 22:12:25 -0500807 cifs_dbg(FYI, "Bad MID state?\n");
Steve French2b2bdfb2008-12-11 17:26:54 +0000808 goto out;
809 }
Steve French84afc292005-12-02 13:32:45 -0800810
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400811 buf = (char *)midQ->resp_buf;
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700812 resp_iov->iov_base = buf;
Ronnie Sahlberge19b2bc2018-04-09 18:06:28 +1000813 resp_iov->iov_len = midQ->resp_buf_size +
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100814 ses->server->vals->header_preamble_size;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400815 if (midQ->large_buf)
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400816 *resp_buf_type = CIFS_LARGE_BUFFER;
Jeff Layton2c8f9812011-05-19 16:22:52 -0400817 else
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400818 *resp_buf_type = CIFS_SMALL_BUFFER;
819
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100820#ifdef CONFIG_CIFS_SMB311
Steve French0d5ec282018-04-22 19:51:22 -0500821 if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP)) {
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100822 struct kvec iov = {
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000823 .iov_base = resp_iov->iov_base,
824 .iov_len = resp_iov->iov_len
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100825 };
826 smb311_update_preauth_hash(ses, &iov, 1);
827 }
828#endif
829
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400830 credits = ses->server->ops->get_credits(midQ);
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500831
Pavel Shilovsky082d0642012-05-17 12:18:21 +0400832 rc = ses->server->ops->check_receive(midQ, ses->server,
833 flags & CIFS_LOG_ERROR);
Steve French2b2bdfb2008-12-11 17:26:54 +0000834
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700835 /* mark it so buf will not be freed by cifs_delete_mid */
Jeff Layton2c8f9812011-05-19 16:22:52 -0400836 if ((flags & CIFS_NO_RESP) == 0)
837 midQ->resp_buf = NULL;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000838out:
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700839 cifs_delete_mid(midQ);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400840 add_credits(ses->server, credits, optype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 return rc;
843}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800846SendReceive2(const unsigned int xid, struct cifs_ses *ses,
847 struct kvec *iov, int n_vec, int *resp_buf_type /* ret */,
848 const int flags, struct kvec *resp_iov)
849{
850 struct smb_rqst rqst;
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +1100851 struct kvec s_iov[CIFS_MAX_IOV_SIZE], *new_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800852 int rc;
853
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +1100854 if (n_vec + 1 > CIFS_MAX_IOV_SIZE) {
Kees Cook6da2ec52018-06-12 13:55:00 -0700855 new_iov = kmalloc_array(n_vec + 1, sizeof(struct kvec),
856 GFP_KERNEL);
Steve French117e3b72018-04-22 10:24:19 -0500857 if (!new_iov) {
858 /* otherwise cifs_send_recv below sets resp_buf_type */
859 *resp_buf_type = CIFS_NO_BUFFER;
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +1100860 return -ENOMEM;
Steve French117e3b72018-04-22 10:24:19 -0500861 }
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +1100862 } else
863 new_iov = s_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800864
865 /* 1st iov is a RFC1001 length followed by the rest of the packet */
866 memcpy(new_iov + 1, iov, (sizeof(struct kvec) * n_vec));
867
868 new_iov[0].iov_base = new_iov[1].iov_base;
869 new_iov[0].iov_len = 4;
870 new_iov[1].iov_base += 4;
871 new_iov[1].iov_len -= 4;
872
873 memset(&rqst, 0, sizeof(struct smb_rqst));
874 rqst.rq_iov = new_iov;
875 rqst.rq_nvec = n_vec + 1;
876
877 rc = cifs_send_recv(xid, ses, &rqst, resp_buf_type, flags, resp_iov);
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +1100878 if (n_vec + 1 > CIFS_MAX_IOV_SIZE)
879 kfree(new_iov);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800880 return rc;
881}
882
Ronnie Sahlberg83b77392017-11-09 12:14:15 +1100883/* Like SendReceive2 but iov[0] does not contain an rfc1002 header */
884int
885smb2_send_recv(const unsigned int xid, struct cifs_ses *ses,
886 struct kvec *iov, int n_vec, int *resp_buf_type /* ret */,
887 const int flags, struct kvec *resp_iov)
888{
889 struct smb_rqst rqst;
Ronnie Sahlberg83b77392017-11-09 12:14:15 +1100890 int rc;
Ronnie Sahlberg83b77392017-11-09 12:14:15 +1100891
892 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000893 rqst.rq_iov = iov;
894 rqst.rq_nvec = n_vec;
Ronnie Sahlberg83b77392017-11-09 12:14:15 +1100895
896 rc = cifs_send_recv(xid, ses, &rqst, resp_buf_type, flags, resp_iov);
Ronnie Sahlberg83b77392017-11-09 12:14:15 +1100897 return rc;
898}
899
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800900int
Steve French96daf2b2011-05-27 04:34:02 +0000901SendReceive(const unsigned int xid, struct cifs_ses *ses,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400903 int *pbytes_returned, const int timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
905 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 struct mid_q_entry *midQ;
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800907 unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
908 struct kvec iov = { .iov_base = in_buf, .iov_len = len };
909 struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 if (ses == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500912 cifs_dbg(VFS, "Null smb session\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 return -EIO;
914 }
Steve French79a58d12007-07-06 22:44:50 +0000915 if (ses->server == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500916 cifs_dbg(VFS, "Null tcp session\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return -EIO;
918 }
919
Steve French79a58d12007-07-06 22:44:50 +0000920 if (ses->server->tcpStatus == CifsExiting)
Steve French31ca3bc2005-04-28 22:41:11 -0700921 return -ENOENT;
922
Steve French79a58d12007-07-06 22:44:50 +0000923 /* Ensure that we do not send more than 50 overlapping requests
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 to the same server. We may make this configurable later or
925 use ses->maxReq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800927 if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500928 cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800929 len);
Volker Lendecke6d9c6d52008-12-08 20:50:24 +0000930 return -EIO;
931 }
932
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400933 rc = wait_for_free_request(ses->server, timeout, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000934 if (rc)
935 return rc;
936
Steve French79a58d12007-07-06 22:44:50 +0000937 /* make sure that we sign in the same order that we send on this socket
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 and avoid races inside tcp sendmsg code that could cause corruption
939 of smb data */
940
Jeff Layton72ca5452008-12-01 07:09:36 -0500941 mutex_lock(&ses->server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000943 rc = allocate_mid(ses, in_buf, &midQ);
944 if (rc) {
Jeff Layton72ca5452008-12-01 07:09:36 -0500945 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000946 /* Update # of requests on wire to server */
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400947 add_credits(ses->server, 1, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000948 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
950
Steve Frenchad009ac2005-04-28 22:41:05 -0700951 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
Volker Lendecke829049c2008-12-06 16:00:53 +0100952 if (rc) {
953 mutex_unlock(&ses->server->srv_mutex);
954 goto out;
955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400957 midQ->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +0000958
959 cifs_in_send_inc(ses->server);
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800960 rc = smb_send(ses->server, in_buf, len);
Steve French789e6662011-08-09 18:44:44 +0000961 cifs_in_send_dec(ses->server);
962 cifs_save_when_sent(midQ);
Jeff Laytonad313cb2013-04-03 10:27:36 -0400963
964 if (rc < 0)
965 ses->server->sequence_number -= 2;
966
Jeff Layton72ca5452008-12-01 07:09:36 -0500967 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000968
Steve French79a58d12007-07-06 22:44:50 +0000969 if (rc < 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000970 goto out;
971
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400972 if (timeout == CIFS_ASYNC_OP)
Steve French133672e2007-11-13 22:41:37 +0000973 goto out;
Jeff Layton0ade6402011-01-11 07:24:02 -0500974
975 rc = wait_for_response(ses->server, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -0500976 if (rc != 0) {
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800977 send_cancel(ses->server, &rqst, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -0500978 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400979 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
Jeff Layton1be912d2011-01-28 07:08:28 -0500980 /* no longer considered to be "in-flight" */
981 midQ->callback = DeleteMidQEntry;
982 spin_unlock(&GlobalMid_Lock);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400983 add_credits(ses->server, 1, 0);
Jeff Layton1be912d2011-01-28 07:08:28 -0500984 return rc;
985 }
986 spin_unlock(&GlobalMid_Lock);
987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Jeff Layton3c1105d2011-05-22 07:09:13 -0400989 rc = cifs_sync_mid_result(midQ, ses->server);
Jeff Layton053d5032011-01-11 07:24:02 -0500990 if (rc != 0) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400991 add_credits(ses->server, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return rc;
993 }
Steve French50c2f752007-07-13 00:33:32 +0000994
Jeff Layton2c8f9812011-05-19 16:22:52 -0400995 if (!midQ->resp_buf || !out_buf ||
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400996 midQ->mid_state != MID_RESPONSE_RECEIVED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 rc = -EIO;
Joe Perchesf96637b2013-05-04 22:12:25 -0500998 cifs_dbg(VFS, "Bad MID state?\n");
Steve French2b2bdfb2008-12-11 17:26:54 +0000999 goto out;
1000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001002 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
Jeff Layton2c8f9812011-05-19 16:22:52 -04001003 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
1004 rc = cifs_check_receive(midQ, ses->server, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001005out:
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001006 cifs_delete_mid(midQ);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001007 add_credits(ses->server, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 return rc;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001010}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001012/* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
1013 blocking lock to return. */
1014
1015static int
Steve French96daf2b2011-05-27 04:34:02 +00001016send_lock_cancel(const unsigned int xid, struct cifs_tcon *tcon,
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001017 struct smb_hdr *in_buf,
1018 struct smb_hdr *out_buf)
1019{
1020 int bytes_returned;
Steve French96daf2b2011-05-27 04:34:02 +00001021 struct cifs_ses *ses = tcon->ses;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001022 LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
1023
1024 /* We just modify the current in_buf to change
1025 the type of lock from LOCKING_ANDX_SHARED_LOCK
1026 or LOCKING_ANDX_EXCLUSIVE_LOCK to
1027 LOCKING_ANDX_CANCEL_LOCK. */
1028
1029 pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
1030 pSMB->Timeout = 0;
Pavel Shilovsky88257362012-05-23 14:01:59 +04001031 pSMB->hdr.Mid = get_next_mid(ses->server);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001032
1033 return SendReceive(xid, ses, in_buf, out_buf,
Jeff Layton77499812011-01-11 07:24:23 -05001034 &bytes_returned, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001035}
1036
1037int
Steve French96daf2b2011-05-27 04:34:02 +00001038SendReceiveBlockingLock(const unsigned int xid, struct cifs_tcon *tcon,
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001039 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
1040 int *pbytes_returned)
1041{
1042 int rc = 0;
1043 int rstart = 0;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001044 struct mid_q_entry *midQ;
Steve French96daf2b2011-05-27 04:34:02 +00001045 struct cifs_ses *ses;
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001046 unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
1047 struct kvec iov = { .iov_base = in_buf, .iov_len = len };
1048 struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001049
1050 if (tcon == NULL || tcon->ses == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001051 cifs_dbg(VFS, "Null smb session\n");
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001052 return -EIO;
1053 }
1054 ses = tcon->ses;
1055
Steve French79a58d12007-07-06 22:44:50 +00001056 if (ses->server == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001057 cifs_dbg(VFS, "Null tcp session\n");
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001058 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
1060
Steve French79a58d12007-07-06 22:44:50 +00001061 if (ses->server->tcpStatus == CifsExiting)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001062 return -ENOENT;
1063
Steve French79a58d12007-07-06 22:44:50 +00001064 /* Ensure that we do not send more than 50 overlapping requests
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001065 to the same server. We may make this configurable later or
1066 use ses->maxReq */
1067
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001068 if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001069 cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001070 len);
Volker Lendecke6d9c6d52008-12-08 20:50:24 +00001071 return -EIO;
1072 }
1073
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001074 rc = wait_for_free_request(ses->server, CIFS_BLOCKING_OP, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001075 if (rc)
1076 return rc;
1077
Steve French79a58d12007-07-06 22:44:50 +00001078 /* make sure that we sign in the same order that we send on this socket
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001079 and avoid races inside tcp sendmsg code that could cause corruption
1080 of smb data */
1081
Jeff Layton72ca5452008-12-01 07:09:36 -05001082 mutex_lock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001083
1084 rc = allocate_mid(ses, in_buf, &midQ);
1085 if (rc) {
Jeff Layton72ca5452008-12-01 07:09:36 -05001086 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001087 return rc;
1088 }
1089
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001090 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
Volker Lendecke829049c2008-12-06 16:00:53 +01001091 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001092 cifs_delete_mid(midQ);
Volker Lendecke829049c2008-12-06 16:00:53 +01001093 mutex_unlock(&ses->server->srv_mutex);
1094 return rc;
1095 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001096
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001097 midQ->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +00001098 cifs_in_send_inc(ses->server);
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001099 rc = smb_send(ses->server, in_buf, len);
Steve French789e6662011-08-09 18:44:44 +00001100 cifs_in_send_dec(ses->server);
1101 cifs_save_when_sent(midQ);
Jeff Laytonad313cb2013-04-03 10:27:36 -04001102
1103 if (rc < 0)
1104 ses->server->sequence_number -= 2;
1105
Jeff Layton72ca5452008-12-01 07:09:36 -05001106 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001107
Steve French79a58d12007-07-06 22:44:50 +00001108 if (rc < 0) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001109 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001110 return rc;
1111 }
1112
1113 /* Wait for a reply - allow signals to interrupt. */
1114 rc = wait_event_interruptible(ses->server->response_q,
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001115 (!(midQ->mid_state == MID_REQUEST_SUBMITTED)) ||
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001116 ((ses->server->tcpStatus != CifsGood) &&
1117 (ses->server->tcpStatus != CifsNew)));
1118
1119 /* Were we interrupted by a signal ? */
1120 if ((rc == -ERESTARTSYS) &&
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 if (in_buf->Command == SMB_COM_TRANSACTION2) {
1126 /* POSIX lock. We send a NT_CANCEL SMB to cause the
1127 blocking lock to return. */
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001128 rc = send_cancel(ses->server, &rqst, midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001129 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001130 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001131 return rc;
1132 }
1133 } else {
1134 /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
1135 to cause the blocking lock to return. */
1136
1137 rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
1138
1139 /* If we get -ENOLCK back the lock may have
1140 already been removed. Don't exit in this case. */
1141 if (rc && rc != -ENOLCK) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001142 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001143 return rc;
1144 }
1145 }
1146
Jeff Layton1be912d2011-01-28 07:08:28 -05001147 rc = wait_for_response(ses->server, midQ);
1148 if (rc) {
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001149 send_cancel(ses->server, &rqst, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -05001150 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001151 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
Jeff Layton1be912d2011-01-28 07:08:28 -05001152 /* no longer considered to be "in-flight" */
1153 midQ->callback = DeleteMidQEntry;
1154 spin_unlock(&GlobalMid_Lock);
1155 return rc;
1156 }
1157 spin_unlock(&GlobalMid_Lock);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001158 }
Jeff Layton1be912d2011-01-28 07:08:28 -05001159
1160 /* We got the response - restart system call. */
1161 rstart = 1;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001162 }
1163
Jeff Layton3c1105d2011-05-22 07:09:13 -04001164 rc = cifs_sync_mid_result(midQ, ses->server);
Jeff Layton053d5032011-01-11 07:24:02 -05001165 if (rc != 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001166 return rc;
Steve French50c2f752007-07-13 00:33:32 +00001167
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001168 /* rcvd frame is ok */
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001169 if (out_buf == NULL || midQ->mid_state != MID_RESPONSE_RECEIVED) {
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001170 rc = -EIO;
Joe Perchesf96637b2013-05-04 22:12:25 -05001171 cifs_dbg(VFS, "Bad MID state?\n");
Volker Lendecke698e96a2008-12-06 16:39:31 +01001172 goto out;
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001173 }
1174
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001175 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
Jeff Layton2c8f9812011-05-19 16:22:52 -04001176 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
1177 rc = cifs_check_receive(midQ, ses->server, 0);
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001178out:
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001179 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001180 if (rstart && rc == -EACCES)
1181 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return rc;
1183}