blob: 8731cfa6602628b7dc434da88976ffc881aa0edd [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>
Pavel Shilovskyb30c74c2019-03-05 15:51:57 -080036#include <linux/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "cifspdu.h"
38#include "cifsglob.h"
39#include "cifsproto.h"
40#include "cifs_debug.h"
Aurelien Aptel8bd68c62018-02-16 19:19:29 +010041#include "smb2proto.h"
Long Li9762c2d2017-11-22 17:38:43 -070042#include "smbdirect.h"
Steve French50c2f752007-07-13 00:33:32 +000043
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +110044/* Max number of iovectors we can use off the stack when sending requests. */
45#define CIFS_MAX_IOV_SIZE 8
46
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +040047void
48cifs_wake_up_task(struct mid_q_entry *mid)
Jeff Layton2b84a36c2011-01-11 07:24:21 -050049{
50 wake_up_process(mid->callback_data);
51}
52
Jeff Laytona6827c12011-01-11 07:24:21 -050053struct mid_q_entry *
Jeff Layton24b9b062008-12-01 07:09:34 -050054AllocMidQEntry(const struct smb_hdr *smb_buffer, struct TCP_Server_Info *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
56 struct mid_q_entry *temp;
57
Jeff Layton24b9b062008-12-01 07:09:34 -050058 if (server == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -050059 cifs_dbg(VFS, "Null TCP session in AllocMidQEntry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 return NULL;
61 }
Steve French50c2f752007-07-13 00:33:32 +000062
Pekka Enberg232087c2008-09-15 13:22:54 +030063 temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
NeilBrowna6f74e82017-04-10 12:08:53 +100064 memset(temp, 0, sizeof(struct mid_q_entry));
Lars Persson696e4202018-06-25 14:05:25 +020065 kref_init(&temp->refcount);
NeilBrowna6f74e82017-04-10 12:08:53 +100066 temp->mid = get_mid(smb_buffer);
67 temp->pid = current->pid;
68 temp->command = cpu_to_le16(smb_buffer->Command);
69 cifs_dbg(FYI, "For smb_command %d\n", smb_buffer->Command);
Steve French1047abc2005-10-11 19:58:06 -070070 /* do_gettimeofday(&temp->when_sent);*/ /* easier to use jiffies */
NeilBrowna6f74e82017-04-10 12:08:53 +100071 /* when mid allocated can be before when sent */
72 temp->when_alloc = jiffies;
73 temp->server = server;
Jeff Layton2b84a36c2011-01-11 07:24:21 -050074
NeilBrowna6f74e82017-04-10 12:08:53 +100075 /*
76 * The default is for the mid to be synchronous, so the
77 * default callback just wakes up the current task.
78 */
79 temp->callback = cifs_wake_up_task;
80 temp->callback_data = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 atomic_inc(&midCount);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -040083 temp->mid_state = MID_REQUEST_ALLOCATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return temp;
85}
86
Lars Persson696e4202018-06-25 14:05:25 +020087static void _cifs_mid_q_entry_release(struct kref *refcount)
88{
89 struct mid_q_entry *mid = container_of(refcount, struct mid_q_entry,
90 refcount);
91
92 mempool_free(mid, cifs_mid_poolp);
93}
94
95void cifs_mid_q_entry_release(struct mid_q_entry *midEntry)
96{
97 spin_lock(&GlobalMid_Lock);
98 kref_put(&midEntry->refcount, _cifs_mid_q_entry_release);
99 spin_unlock(&GlobalMid_Lock);
100}
101
Jeff Layton766fdbb2011-01-11 07:24:21 -0500102void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103DeleteMidQEntry(struct mid_q_entry *midEntry)
104{
Steve French1047abc2005-10-11 19:58:06 -0700105#ifdef CONFIG_CIFS_STATS2
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400106 __le16 command = midEntry->server->vals->lock_cmd;
Steve French1047abc2005-10-11 19:58:06 -0700107 unsigned long now;
108#endif
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400109 midEntry->mid_state = MID_FREE;
Jeff Layton80975312011-01-11 07:24:02 -0500110 atomic_dec(&midCount);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400111 if (midEntry->large_buf)
Steve Frenchb8643e12005-04-28 22:41:07 -0700112 cifs_buf_release(midEntry->resp_buf);
113 else
114 cifs_small_buf_release(midEntry->resp_buf);
Steve French1047abc2005-10-11 19:58:06 -0700115#ifdef CONFIG_CIFS_STATS2
116 now = jiffies;
Steve French00778e22018-09-18 14:05:18 -0500117 /*
118 * commands taking longer than one second (default) can be indications
119 * that something is wrong, unless it is quite a slow link or a very
120 * busy server. Note that this calc is unlikely or impossible to wrap
121 * as long as slow_rsp_threshold is not set way above recommended max
122 * value (32767 ie 9 hours) and is generally harmless even if wrong
123 * since only affects debug counters - so leaving the calc as simple
124 * comparison rather than doing multiple conversions and overflow
125 * checks
126 */
127 if ((slow_rsp_threshold != 0) &&
128 time_after(now, midEntry->when_alloc + (slow_rsp_threshold * HZ)) &&
Steve French020eec52018-08-01 16:38:07 -0500129 (midEntry->command != command)) {
Steve Frenchf5942db2018-11-14 01:37:39 -0600130 /*
131 * smb2slowcmd[NUMBER_OF_SMB2_COMMANDS] counts by command
132 * NB: le16_to_cpu returns unsigned so can not be negative below
133 */
134 if (le16_to_cpu(midEntry->command) < NUMBER_OF_SMB2_COMMANDS)
Steve French468d6772018-08-04 05:24:34 -0500135 cifs_stats_inc(&midEntry->server->smb2slowcmd[le16_to_cpu(midEntry->command)]);
136
Steve French020eec52018-08-01 16:38:07 -0500137 trace_smb3_slow_rsp(le16_to_cpu(midEntry->command),
138 midEntry->mid, midEntry->pid,
139 midEntry->when_sent, midEntry->when_received);
140 if (cifsFYI & CIFS_TIMER) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +0300141 pr_debug(" CIFS slow rsp: cmd %d mid %llu",
Steve French1047abc2005-10-11 19:58:06 -0700142 midEntry->command, midEntry->mid);
Rodrigo Freiref80eaed2018-10-07 12:21:26 -0300143 cifs_info(" A: 0x%lx S: 0x%lx R: 0x%lx\n",
Steve French1047abc2005-10-11 19:58:06 -0700144 now - midEntry->when_alloc,
145 now - midEntry->when_sent,
146 now - midEntry->when_received);
147 }
148 }
149#endif
Lars Persson696e4202018-06-25 14:05:25 +0200150 cifs_mid_q_entry_release(midEntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700153void
154cifs_delete_mid(struct mid_q_entry *mid)
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500155{
156 spin_lock(&GlobalMid_Lock);
Ronnie Sahlbergddf83af2018-08-30 10:12:59 +1000157 list_del_init(&mid->qhead);
158 mid->mid_flags |= MID_DELETED;
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500159 spin_unlock(&GlobalMid_Lock);
160
161 DeleteMidQEntry(mid);
162}
163
Jeff Layton6f49f462012-09-18 16:20:34 -0700164/*
165 * smb_send_kvec - send an array of kvecs to the server
166 * @server: Server to send the data to
Al Viro3ab3f2a2015-11-13 02:36:04 -0500167 * @smb_msg: Message to send
Jeff Layton6f49f462012-09-18 16:20:34 -0700168 * @sent: amount of data sent on socket is stored here
169 *
170 * Our basic "send data to server" function. Should be called with srv_mutex
171 * held. The caller is responsible for handling the results.
172 */
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500173static int
Al Viro3ab3f2a2015-11-13 02:36:04 -0500174smb_send_kvec(struct TCP_Server_Info *server, struct msghdr *smb_msg,
175 size_t *sent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 int rc = 0;
Al Viro3ab3f2a2015-11-13 02:36:04 -0500178 int retries = 0;
Steve Frenchedf1ae42008-10-29 00:47:57 +0000179 struct socket *ssocket = server->ssocket;
Steve French50c2f752007-07-13 00:33:32 +0000180
Jeff Layton6f49f462012-09-18 16:20:34 -0700181 *sent = 0;
182
Al Viro3ab3f2a2015-11-13 02:36:04 -0500183 smb_msg->msg_name = (struct sockaddr *) &server->dstaddr;
184 smb_msg->msg_namelen = sizeof(struct sockaddr);
185 smb_msg->msg_control = NULL;
186 smb_msg->msg_controllen = 0;
Jeff Layton0496e022008-12-30 12:39:16 -0500187 if (server->noblocksnd)
Al Viro3ab3f2a2015-11-13 02:36:04 -0500188 smb_msg->msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL;
Steve Frenchedf1ae42008-10-29 00:47:57 +0000189 else
Al Viro3ab3f2a2015-11-13 02:36:04 -0500190 smb_msg->msg_flags = MSG_NOSIGNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Al Viro3ab3f2a2015-11-13 02:36:04 -0500192 while (msg_data_left(smb_msg)) {
Jeff Layton6f49f462012-09-18 16:20:34 -0700193 /*
194 * If blocking send, we try 3 times, since each can block
195 * for 5 seconds. For nonblocking we have to try more
196 * but wait increasing amounts of time allowing time for
197 * socket to clear. The overall time we wait in either
198 * case to send on the socket is about 15 seconds.
199 * Similarly we wait for 15 seconds for a response from
200 * the server in SendReceive[2] for the server to send
201 * a response back for most types of requests (except
202 * SMB Write past end of file which can be slow, and
203 * blocking lock operations). NFS waits slightly longer
204 * than CIFS, but this can make it take longer for
205 * nonresponsive servers to be detected and 15 seconds
206 * is more than enough time for modern networks to
207 * send a packet. In most cases if we fail to send
208 * after the retries we will kill the socket and
209 * reconnect which may clear the network problem.
210 */
Al Viro3ab3f2a2015-11-13 02:36:04 -0500211 rc = sock_sendmsg(ssocket, smb_msg);
Jeff Laytonce6c44e2013-03-22 08:36:45 -0400212 if (rc == -EAGAIN) {
Al Viro3ab3f2a2015-11-13 02:36:04 -0500213 retries++;
214 if (retries >= 14 ||
215 (!server->noblocksnd && (retries > 2))) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500216 cifs_dbg(VFS, "sends on sock %p stuck for 15 seconds\n",
217 ssocket);
Al Viro3ab3f2a2015-11-13 02:36:04 -0500218 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
Al Viro3ab3f2a2015-11-13 02:36:04 -0500220 msleep(1 << retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 continue;
222 }
Jeff Layton6f49f462012-09-18 16:20:34 -0700223
Steve French79a58d12007-07-06 22:44:50 +0000224 if (rc < 0)
Al Viro3ab3f2a2015-11-13 02:36:04 -0500225 return rc;
Jeff Layton6f49f462012-09-18 16:20:34 -0700226
Steve French79a58d12007-07-06 22:44:50 +0000227 if (rc == 0) {
Steve French3e844692005-10-03 13:37:24 -0700228 /* should never happen, letting socket clear before
229 retrying is our only obvious option here */
Joe Perchesf96637b2013-05-04 22:12:25 -0500230 cifs_dbg(VFS, "tcp sent no data\n");
Steve French3e844692005-10-03 13:37:24 -0700231 msleep(500);
232 continue;
233 }
Jeff Layton6f49f462012-09-18 16:20:34 -0700234
Al Viro3ab3f2a2015-11-13 02:36:04 -0500235 /* send was at least partially successful */
236 *sent += rc;
237 retries = 0; /* in case we get ENOSPC on the next send */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
Al Viro3ab3f2a2015-11-13 02:36:04 -0500239 return 0;
Jeff Layton97bc00b2012-09-18 16:20:35 -0700240}
241
Paulo Alcantara35e2cc12018-06-15 10:22:44 -0300242unsigned long
Ronnie Sahlberg81f39f92018-06-28 10:47:14 +1000243smb_rqst_len(struct TCP_Server_Info *server, struct smb_rqst *rqst)
Jeff Laytona26054d2014-02-14 07:21:00 -0500244{
245 unsigned int i;
Paulo Alcantara35e2cc12018-06-15 10:22:44 -0300246 struct kvec *iov;
247 int nvec;
Jeff Laytona26054d2014-02-14 07:21:00 -0500248 unsigned long buflen = 0;
249
Ronnie Sahlberg81f39f92018-06-28 10:47:14 +1000250 if (server->vals->header_preamble_size == 0 &&
251 rqst->rq_nvec >= 2 && rqst->rq_iov[0].iov_len == 4) {
Paulo Alcantara35e2cc12018-06-15 10:22:44 -0300252 iov = &rqst->rq_iov[1];
253 nvec = rqst->rq_nvec - 1;
254 } else {
255 iov = rqst->rq_iov;
256 nvec = rqst->rq_nvec;
257 }
258
Jeff Laytona26054d2014-02-14 07:21:00 -0500259 /* total up iov array first */
Paulo Alcantara35e2cc12018-06-15 10:22:44 -0300260 for (i = 0; i < nvec; i++)
Jeff Laytona26054d2014-02-14 07:21:00 -0500261 buflen += iov[i].iov_len;
262
Long Lic06a0f22018-05-30 12:47:57 -0700263 /*
264 * Add in the page array if there is one. The caller needs to make
265 * sure rq_offset and rq_tailsz are set correctly. If a buffer of
266 * multiple pages ends at page boundary, rq_tailsz needs to be set to
267 * PAGE_SIZE.
268 */
Jeff Laytona26054d2014-02-14 07:21:00 -0500269 if (rqst->rq_npages) {
Long Lic06a0f22018-05-30 12:47:57 -0700270 if (rqst->rq_npages == 1)
271 buflen += rqst->rq_tailsz;
272 else {
273 /*
274 * If there is more than one page, calculate the
275 * buffer length based on rq_offset and rq_tailsz
276 */
277 buflen += rqst->rq_pagesz * (rqst->rq_npages - 1) -
278 rqst->rq_offset;
279 buflen += rqst->rq_tailsz;
280 }
Jeff Laytona26054d2014-02-14 07:21:00 -0500281 }
282
283 return buflen;
284}
285
Jeff Layton6f49f462012-09-18 16:20:34 -0700286static int
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000287__smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
288 struct smb_rqst *rqst)
Jeff Layton6f49f462012-09-18 16:20:34 -0700289{
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000290 int rc = 0;
291 struct kvec *iov;
292 int n_vec;
293 unsigned int send_length = 0;
294 unsigned int i, j;
Pavel Shilovskyb30c74c2019-03-05 15:51:57 -0800295 sigset_t mask, oldmask;
Al Viro3ab3f2a2015-11-13 02:36:04 -0500296 size_t total_len = 0, sent, size;
Jeff Laytonb8eed282012-09-18 16:20:35 -0700297 struct socket *ssocket = server->ssocket;
Al Viro3ab3f2a2015-11-13 02:36:04 -0500298 struct msghdr smb_msg;
Jeff Laytonb8eed282012-09-18 16:20:35 -0700299 int val = 1;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000300 __be32 rfc1002_marker;
301
Long Li9762c2d2017-11-22 17:38:43 -0700302 if (cifs_rdma_enabled(server) && server->smbd_conn) {
Ronnie Sahlberg81f39f92018-06-28 10:47:14 +1000303 rc = smbd_send(server, rqst);
Long Li9762c2d2017-11-22 17:38:43 -0700304 goto smbd_done;
305 }
Pavel Shilovskyafc18a62019-03-05 15:51:56 -0800306
Jeff Laytonea702b82012-12-27 07:28:55 -0500307 if (ssocket == NULL)
Pavel Shilovskyafc18a62019-03-05 15:51:56 -0800308 return -EAGAIN;
Jeff Laytonea702b82012-12-27 07:28:55 -0500309
Pavel Shilovskyb30c74c2019-03-05 15:51:57 -0800310 if (signal_pending(current)) {
311 cifs_dbg(FYI, "signal is pending before sending any data\n");
312 return -EINTR;
313 }
314
Jeff Laytonb8eed282012-09-18 16:20:35 -0700315 /* cork the socket */
316 kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
317 (char *)&val, sizeof(val));
318
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000319 for (j = 0; j < num_rqst; j++)
Ronnie Sahlberg81f39f92018-06-28 10:47:14 +1000320 send_length += smb_rqst_len(server, &rqst[j]);
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000321 rfc1002_marker = cpu_to_be32(send_length);
322
Pavel Shilovskyb30c74c2019-03-05 15:51:57 -0800323 /*
324 * We should not allow signals to interrupt the network send because
325 * any partial send will cause session reconnects thus increasing
326 * latency of system calls and overload a server with unnecessary
327 * requests.
328 */
329
330 sigfillset(&mask);
331 sigprocmask(SIG_BLOCK, &mask, &oldmask);
332
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000333 /* Generate a rfc1002 marker for SMB2+ */
334 if (server->vals->header_preamble_size == 0) {
335 struct kvec hiov = {
336 .iov_base = &rfc1002_marker,
337 .iov_len = 4
338 };
David Howellsaa563d72018-10-20 00:57:56 +0100339 iov_iter_kvec(&smb_msg.msg_iter, WRITE, &hiov, 1, 4);
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000340 rc = smb_send_kvec(server, &smb_msg, &sent);
341 if (rc < 0)
Pavel Shilovskyb30c74c2019-03-05 15:51:57 -0800342 goto unmask;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000343
344 total_len += sent;
345 send_length += 4;
346 }
347
Paulo Alcantara662bf5b2018-06-14 17:34:08 -0300348 cifs_dbg(FYI, "Sending smb: smb_len=%u\n", send_length);
349
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000350 for (j = 0; j < num_rqst; j++) {
351 iov = rqst[j].rq_iov;
352 n_vec = rqst[j].rq_nvec;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000353
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000354 size = 0;
Paulo Alcantara662bf5b2018-06-14 17:34:08 -0300355 for (i = 0; i < n_vec; i++) {
356 dump_smb(iov[i].iov_base, iov[i].iov_len);
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000357 size += iov[i].iov_len;
Paulo Alcantara662bf5b2018-06-14 17:34:08 -0300358 }
Al Viro3ab3f2a2015-11-13 02:36:04 -0500359
David Howellsaa563d72018-10-20 00:57:56 +0100360 iov_iter_kvec(&smb_msg.msg_iter, WRITE, iov, n_vec, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Al Viro3ab3f2a2015-11-13 02:36:04 -0500362 rc = smb_send_kvec(server, &smb_msg, &sent);
Jeff Layton97bc00b2012-09-18 16:20:35 -0700363 if (rc < 0)
Pavel Shilovskyb30c74c2019-03-05 15:51:57 -0800364 goto unmask;
Jeff Layton97bc00b2012-09-18 16:20:35 -0700365
366 total_len += sent;
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000367
368 /* now walk the page array and send each page in it */
369 for (i = 0; i < rqst[j].rq_npages; i++) {
370 struct bio_vec bvec;
371
372 bvec.bv_page = rqst[j].rq_pages[i];
373 rqst_page_get_length(&rqst[j], i, &bvec.bv_len,
374 &bvec.bv_offset);
375
David Howellsaa563d72018-10-20 00:57:56 +0100376 iov_iter_bvec(&smb_msg.msg_iter, WRITE,
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000377 &bvec, 1, bvec.bv_len);
378 rc = smb_send_kvec(server, &smb_msg, &sent);
379 if (rc < 0)
380 break;
381
382 total_len += sent;
383 }
Jeff Layton97bc00b2012-09-18 16:20:35 -0700384 }
385
Pavel Shilovskyb30c74c2019-03-05 15:51:57 -0800386unmask:
387 sigprocmask(SIG_SETMASK, &oldmask, NULL);
388
389 /*
390 * If signal is pending but we have already sent the whole packet to
391 * the server we need to return success status to allow a corresponding
392 * mid entry to be kept in the pending requests queue thus allowing
393 * to handle responses from the server by the client.
394 *
395 * If only part of the packet has been sent there is no need to hide
396 * interrupt because the session will be reconnected anyway, so there
397 * won't be any response from the server to handle.
398 */
399
400 if (signal_pending(current) && (total_len != send_length)) {
401 cifs_dbg(FYI, "signal is pending after attempt to send\n");
402 rc = -EINTR;
403 }
404
Jeff Laytonb8eed282012-09-18 16:20:35 -0700405 /* uncork it */
406 val = 0;
407 kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
408 (char *)&val, sizeof(val));
409
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000410 if ((total_len > 0) && (total_len != send_length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500411 cifs_dbg(FYI, "partial send (wanted=%u sent=%zu): terminating session\n",
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000412 send_length, total_len);
Jeff Layton6f49f462012-09-18 16:20:34 -0700413 /*
414 * If we have only sent part of an SMB then the next SMB could
415 * be taken as the remainder of this one. We need to kill the
416 * socket so the server throws away the partial SMB
417 */
Steve Frenchedf1ae42008-10-29 00:47:57 +0000418 server->tcpStatus = CifsNeedReconnect;
Steve Frenchbf1fdeb2018-07-30 19:23:09 -0500419 trace_smb3_partial_send_reconnect(server->CurrentMid,
420 server->hostname);
Steve Frenchedf1ae42008-10-29 00:47:57 +0000421 }
Long Li9762c2d2017-11-22 17:38:43 -0700422smbd_done:
Jeff Laytond804d412011-01-28 15:05:43 -0500423 if (rc < 0 && rc != -EINTR)
Joe Perchesf96637b2013-05-04 22:12:25 -0500424 cifs_dbg(VFS, "Error %d sending data on socket to server\n",
425 rc);
Pavel Shilovskyee139192019-01-10 11:27:28 -0800426 else if (rc > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 return rc;
430}
431
Jeff Layton6f49f462012-09-18 16:20:34 -0700432static int
Ronnie Sahlberg1f3a8f52018-08-01 09:26:12 +1000433smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
434 struct smb_rqst *rqst, int flags)
Jeff Layton6f49f462012-09-18 16:20:34 -0700435{
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +1000436 struct kvec iov;
437 struct smb2_transform_hdr tr_hdr;
438 struct smb_rqst cur_rqst[MAX_COMPOUND];
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700439 int rc;
Jeff Layton6f49f462012-09-18 16:20:34 -0700440
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700441 if (!(flags & CIFS_TRANSFORM_REQ))
Ronnie Sahlberg1f3a8f52018-08-01 09:26:12 +1000442 return __smb_send_rqst(server, num_rqst, rqst);
443
444 if (num_rqst > MAX_COMPOUND - 1)
445 return -ENOMEM;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700446
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +1000447 memset(&cur_rqst[0], 0, sizeof(cur_rqst));
448 memset(&iov, 0, sizeof(iov));
449 memset(&tr_hdr, 0, sizeof(tr_hdr));
450
451 iov.iov_base = &tr_hdr;
452 iov.iov_len = sizeof(tr_hdr);
453 cur_rqst[0].rq_iov = &iov;
454 cur_rqst[0].rq_nvec = 1;
455
456 if (!server->ops->init_transform_rq) {
457 cifs_dbg(VFS, "Encryption requested but transform callback "
458 "is missing\n");
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700459 return -EIO;
460 }
461
Ronnie Sahlberg1f3a8f52018-08-01 09:26:12 +1000462 rc = server->ops->init_transform_rq(server, num_rqst + 1,
463 &cur_rqst[0], rqst);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700464 if (rc)
465 return rc;
466
Ronnie Sahlberg1f3a8f52018-08-01 09:26:12 +1000467 rc = __smb_send_rqst(server, num_rqst + 1, &cur_rqst[0]);
468 smb3_free_compound_rqst(num_rqst, &cur_rqst[1]);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700469 return rc;
Jeff Layton6f49f462012-09-18 16:20:34 -0700470}
471
Jeff Layton0496e022008-12-30 12:39:16 -0500472int
473smb_send(struct TCP_Server_Info *server, struct smb_hdr *smb_buffer,
474 unsigned int smb_buf_length)
475{
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800476 struct kvec iov[2];
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700477 struct smb_rqst rqst = { .rq_iov = iov,
478 .rq_nvec = 2 };
Jeff Layton0496e022008-12-30 12:39:16 -0500479
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800480 iov[0].iov_base = smb_buffer;
481 iov[0].iov_len = 4;
482 iov[1].iov_base = (char *)smb_buffer + 4;
483 iov[1].iov_len = smb_buf_length;
Jeff Layton0496e022008-12-30 12:39:16 -0500484
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000485 return __smb_send_rqst(server, 1, &rqst);
Jeff Layton0496e022008-12-30 12:39:16 -0500486}
487
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300488static int
Ronnie Sahlbergb227d212019-03-08 12:58:20 +1000489wait_for_free_credits(struct TCP_Server_Info *server, const int num_credits,
Ronnie Sahlberg2b53b922019-03-08 12:58:22 +1000490 const int timeout, const int flags,
491 unsigned int *instance)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000492{
Pavel Shilovsky5bc59492012-02-21 19:56:08 +0300493 int rc;
Ronnie Sahlberg4230cff2019-03-08 12:58:19 +1000494 int *credits;
495 int optype;
Ronnie Sahlberg2b53b922019-03-08 12:58:22 +1000496 long int t;
497
498 if (timeout < 0)
499 t = MAX_JIFFY_OFFSET;
500 else
501 t = msecs_to_jiffies(timeout);
Ronnie Sahlberg4230cff2019-03-08 12:58:19 +1000502
503 optype = flags & CIFS_OP_MASK;
Pavel Shilovsky5bc59492012-02-21 19:56:08 +0300504
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -0800505 *instance = 0;
506
Ronnie Sahlberg4230cff2019-03-08 12:58:19 +1000507 credits = server->ops->get_credits_field(server, optype);
508 /* Since an echo is already inflight, no need to wait to send another */
509 if (*credits <= 0 && optype == CIFS_ECHO_OP)
510 return -EAGAIN;
511
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300512 spin_lock(&server->req_lock);
Ronnie Sahlberg4230cff2019-03-08 12:58:19 +1000513 if ((flags & CIFS_TIMEOUT_MASK) == CIFS_ASYNC_OP) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000514 /* oplock breaks must not be held up */
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300515 server->in_flight++;
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300516 *credits -= 1;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -0800517 *instance = server->reconnect_instance;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300518 spin_unlock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000519 return 0;
520 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000521
Volker Lendecke27a97a62008-12-08 20:59:39 +0000522 while (1) {
Ronnie Sahlbergb227d212019-03-08 12:58:20 +1000523 if (*credits < num_credits) {
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300524 spin_unlock(&server->req_lock);
Steve French789e6662011-08-09 18:44:44 +0000525 cifs_num_waiters_inc(server);
Ronnie Sahlberg2b53b922019-03-08 12:58:22 +1000526 rc = wait_event_killable_timeout(server->request_q,
527 has_credits(server, credits, num_credits), t);
Steve French789e6662011-08-09 18:44:44 +0000528 cifs_num_waiters_dec(server);
Ronnie Sahlberg2b53b922019-03-08 12:58:22 +1000529 if (!rc) {
Steve French7937ca92019-03-09 20:29:55 -0600530 trace_smb3_credit_timeout(server->CurrentMid,
531 server->hostname, num_credits);
Ronnie Sahlberg2b53b922019-03-08 12:58:22 +1000532 cifs_dbg(VFS, "wait timed out after %d ms\n",
533 timeout);
534 return -ENOTSUPP;
535 }
536 if (rc == -ERESTARTSYS)
537 return -ERESTARTSYS;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300538 spin_lock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000539 } else {
Jeff Laytonc5797a92011-01-11 07:24:01 -0500540 if (server->tcpStatus == CifsExiting) {
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300541 spin_unlock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000542 return -ENOENT;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000543 }
Volker Lendecke27a97a62008-12-08 20:59:39 +0000544
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400545 /*
Ronnie Sahlberg16b34aa2019-03-08 12:58:21 +1000546 * For normal commands, reserve the last MAX_COMPOUND
547 * credits to compound requests.
548 * Otherwise these compounds could be permanently
549 * starved for credits by single-credit requests.
550 *
551 * To prevent spinning CPU, block this thread until
552 * there are >MAX_COMPOUND credits available.
553 * But only do this is we already have a lot of
554 * credits in flight to avoid triggering this check
555 * for servers that are slow to hand out credits on
556 * new sessions.
557 */
558 if (!optype && num_credits == 1 &&
559 server->in_flight > 2 * MAX_COMPOUND &&
560 *credits <= MAX_COMPOUND) {
561 spin_unlock(&server->req_lock);
562 cifs_num_waiters_inc(server);
Ronnie Sahlberg2b53b922019-03-08 12:58:22 +1000563 rc = wait_event_killable_timeout(
564 server->request_q,
Ronnie Sahlberg16b34aa2019-03-08 12:58:21 +1000565 has_credits(server, credits,
Ronnie Sahlberg2b53b922019-03-08 12:58:22 +1000566 MAX_COMPOUND + 1),
567 t);
Ronnie Sahlberg16b34aa2019-03-08 12:58:21 +1000568 cifs_num_waiters_dec(server);
Ronnie Sahlberg2b53b922019-03-08 12:58:22 +1000569 if (!rc) {
Steve French7937ca92019-03-09 20:29:55 -0600570 trace_smb3_credit_timeout(
571 server->CurrentMid,
572 server->hostname, num_credits);
Ronnie Sahlberg2b53b922019-03-08 12:58:22 +1000573 cifs_dbg(VFS, "wait timed out after %d ms\n",
574 timeout);
575 return -ENOTSUPP;
576 }
577 if (rc == -ERESTARTSYS)
578 return -ERESTARTSYS;
Ronnie Sahlberg16b34aa2019-03-08 12:58:21 +1000579 spin_lock(&server->req_lock);
580 continue;
581 }
582
583 /*
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400584 * Can not count locking commands against total
585 * as they are allowed to block on server.
586 */
Volker Lendecke27a97a62008-12-08 20:59:39 +0000587
588 /* update # of requests on the wire to server */
Ronnie Sahlberg4230cff2019-03-08 12:58:19 +1000589 if ((flags & CIFS_TIMEOUT_MASK) != CIFS_BLOCKING_OP) {
Ronnie Sahlbergb227d212019-03-08 12:58:20 +1000590 *credits -= num_credits;
591 server->in_flight += num_credits;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -0800592 *instance = server->reconnect_instance;
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400593 }
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300594 spin_unlock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000595 break;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000596 }
597 }
598 return 0;
599}
600
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300601static int
Ronnie Sahlberg480b1cb2019-03-08 12:58:18 +1000602wait_for_free_request(struct TCP_Server_Info *server, const int flags,
603 unsigned int *instance)
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300604{
Ronnie Sahlberg2b53b922019-03-08 12:58:22 +1000605 return wait_for_free_credits(server, 1, -1, flags,
606 instance);
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300607}
608
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400609int
610cifs_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800611 unsigned int *num, struct cifs_credits *credits)
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400612{
613 *num = size;
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800614 credits->value = 0;
615 credits->instance = server->reconnect_instance;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400616 return 0;
617}
618
Steve French96daf2b2011-05-27 04:34:02 +0000619static int allocate_mid(struct cifs_ses *ses, struct smb_hdr *in_buf,
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000620 struct mid_q_entry **ppmidQ)
621{
622 if (ses->server->tcpStatus == CifsExiting) {
623 return -ENOENT;
Volker Lendecke8fbbd362008-12-06 13:12:34 +0100624 }
625
626 if (ses->server->tcpStatus == CifsNeedReconnect) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500627 cifs_dbg(FYI, "tcp session dead - return to caller to retry\n");
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000628 return -EAGAIN;
Volker Lendecke8fbbd362008-12-06 13:12:34 +0100629 }
630
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500631 if (ses->status == CifsNew) {
Steve French79a58d12007-07-06 22:44:50 +0000632 if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
Steve Frenchad7a2922008-02-07 23:25:02 +0000633 (in_buf->Command != SMB_COM_NEGOTIATE))
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000634 return -EAGAIN;
Steve Frenchad7a2922008-02-07 23:25:02 +0000635 /* else ok - we are setting up session */
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000636 }
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500637
638 if (ses->status == CifsExiting) {
639 /* check if SMB session is bad because we are setting it up */
640 if (in_buf->Command != SMB_COM_LOGOFF_ANDX)
641 return -EAGAIN;
642 /* else ok - we are shutting down session */
643 }
644
Jeff Layton24b9b062008-12-01 07:09:34 -0500645 *ppmidQ = AllocMidQEntry(in_buf, ses->server);
Steve French26f57362007-08-30 22:09:15 +0000646 if (*ppmidQ == NULL)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000647 return -ENOMEM;
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500648 spin_lock(&GlobalMid_Lock);
649 list_add_tail(&(*ppmidQ)->qhead, &ses->server->pending_mid_q);
650 spin_unlock(&GlobalMid_Lock);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000651 return 0;
652}
653
Jeff Layton0ade6402011-01-11 07:24:02 -0500654static int
655wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *midQ)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000656{
Jeff Layton0ade6402011-01-11 07:24:02 -0500657 int error;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000658
Colin Cross5853cc22013-05-07 17:52:05 +0000659 error = wait_event_freezekillable_unsafe(server->response_q,
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400660 midQ->mid_state != MID_REQUEST_SUBMITTED);
Jeff Layton0ade6402011-01-11 07:24:02 -0500661 if (error < 0)
662 return -ERESTARTSYS;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000663
Jeff Layton0ade6402011-01-11 07:24:02 -0500664 return 0;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000665}
666
Jeff Laytonfec344e2012-09-18 16:20:35 -0700667struct mid_q_entry *
668cifs_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400669{
670 int rc;
Jeff Laytonfec344e2012-09-18 16:20:35 -0700671 struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400672 struct mid_q_entry *mid;
673
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800674 if (rqst->rq_iov[0].iov_len != 4 ||
675 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
676 return ERR_PTR(-EIO);
677
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400678 /* enable signing if server requires it */
Jeff Layton38d77c52013-05-26 07:01:00 -0400679 if (server->sign)
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400680 hdr->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
681
682 mid = AllocMidQEntry(hdr, server);
683 if (mid == NULL)
Jeff Laytonfec344e2012-09-18 16:20:35 -0700684 return ERR_PTR(-ENOMEM);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400685
Jeff Laytonfec344e2012-09-18 16:20:35 -0700686 rc = cifs_sign_rqst(rqst, server, &mid->sequence_number);
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100687 if (rc) {
688 DeleteMidQEntry(mid);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700689 return ERR_PTR(rc);
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100690 }
691
Jeff Laytonfec344e2012-09-18 16:20:35 -0700692 return mid;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400693}
Steve French133672e2007-11-13 22:41:37 +0000694
695/*
Jeff Laytona6827c12011-01-11 07:24:21 -0500696 * Send a SMB request and set the callback function in the mid to handle
697 * the result. Caller is responsible for dealing with timeouts.
698 */
699int
Jeff Laytonfec344e2012-09-18 16:20:35 -0700700cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst,
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -0800701 mid_receive_t *receive, mid_callback_t *callback,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -0800702 mid_handle_t *handle, void *cbdata, const int flags,
703 const struct cifs_credits *exist_credits)
Jeff Laytona6827c12011-01-11 07:24:21 -0500704{
Ronnie Sahlberg480b1cb2019-03-08 12:58:18 +1000705 int rc;
Jeff Laytona6827c12011-01-11 07:24:21 -0500706 struct mid_q_entry *mid;
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800707 struct cifs_credits credits = { .value = 0, .instance = 0 };
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -0800708 unsigned int instance;
Ronnie Sahlberg480b1cb2019-03-08 12:58:18 +1000709 int optype;
Jeff Laytona6827c12011-01-11 07:24:21 -0500710
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400711 optype = flags & CIFS_OP_MASK;
712
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400713 if ((flags & CIFS_HAS_CREDITS) == 0) {
Ronnie Sahlberg480b1cb2019-03-08 12:58:18 +1000714 rc = wait_for_free_request(server, flags, &instance);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400715 if (rc)
716 return rc;
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800717 credits.value = 1;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -0800718 credits.instance = instance;
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -0800719 } else
720 instance = exist_credits->instance;
Jeff Laytona6827c12011-01-11 07:24:21 -0500721
722 mutex_lock(&server->srv_mutex);
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -0800723
724 /*
725 * We can't use credits obtained from the previous session to send this
726 * request. Check if there were reconnects after we obtained credits and
727 * return -EAGAIN in such cases to let callers handle it.
728 */
729 if (instance != server->reconnect_instance) {
730 mutex_unlock(&server->srv_mutex);
731 add_credits_and_wake_if(server, &credits, optype);
732 return -EAGAIN;
733 }
734
Jeff Laytonfec344e2012-09-18 16:20:35 -0700735 mid = server->ops->setup_async_request(server, rqst);
736 if (IS_ERR(mid)) {
Jeff Laytona6827c12011-01-11 07:24:21 -0500737 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800738 add_credits_and_wake_if(server, &credits, optype);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700739 return PTR_ERR(mid);
Jeff Laytona6827c12011-01-11 07:24:21 -0500740 }
741
Jeff Layton44d22d82011-10-19 15:29:49 -0400742 mid->receive = receive;
Jeff Laytona6827c12011-01-11 07:24:21 -0500743 mid->callback = callback;
744 mid->callback_data = cbdata;
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -0800745 mid->handle = handle;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400746 mid->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +0000747
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100748 /* put it on the pending_mid_q */
749 spin_lock(&GlobalMid_Lock);
750 list_add_tail(&mid->qhead, &server->pending_mid_q);
751 spin_unlock(&GlobalMid_Lock);
752
Long Li93d2cb62017-06-28 15:55:55 -0700753 /*
754 * Need to store the time in mid before calling I/O. For call_async,
755 * I/O response may come back and free the mid entry on another thread.
756 */
757 cifs_save_when_sent(mid);
Steve French789e6662011-08-09 18:44:44 +0000758 cifs_in_send_inc(server);
Ronnie Sahlberg1f3a8f52018-08-01 09:26:12 +1000759 rc = smb_send_rqst(server, 1, rqst, flags);
Steve French789e6662011-08-09 18:44:44 +0000760 cifs_in_send_dec(server);
Jeff Laytonad313cb2013-04-03 10:27:36 -0400761
Rabin Vincent820962d2015-12-23 07:32:41 +0100762 if (rc < 0) {
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800763 revert_current_mid(server, mid->credits);
Jeff Laytonad313cb2013-04-03 10:27:36 -0400764 server->sequence_number -= 2;
Rabin Vincent820962d2015-12-23 07:32:41 +0100765 cifs_delete_mid(mid);
766 }
767
Jeff Laytona6827c12011-01-11 07:24:21 -0500768 mutex_unlock(&server->srv_mutex);
Steve French789e6662011-08-09 18:44:44 +0000769
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100770 if (rc == 0)
771 return 0;
Jeff Laytona6827c12011-01-11 07:24:21 -0500772
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800773 add_credits_and_wake_if(server, &credits, optype);
Jeff Laytona6827c12011-01-11 07:24:21 -0500774 return rc;
775}
776
777/*
Steve French133672e2007-11-13 22:41:37 +0000778 *
779 * Send an SMB Request. No response info (other than return code)
780 * needs to be parsed.
781 *
782 * flags indicate the type of request buffer and how long to wait
783 * and whether to log NT STATUS code (error) before mapping it to POSIX error
784 *
785 */
786int
Steve French96daf2b2011-05-27 04:34:02 +0000787SendReceiveNoRsp(const unsigned int xid, struct cifs_ses *ses,
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400788 char *in_buf, int flags)
Steve French133672e2007-11-13 22:41:37 +0000789{
790 int rc;
791 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700792 struct kvec rsp_iov;
Steve French133672e2007-11-13 22:41:37 +0000793 int resp_buf_type;
794
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400795 iov[0].iov_base = in_buf;
796 iov[0].iov_len = get_rfc1002_length(in_buf) + 4;
Steve French133672e2007-11-13 22:41:37 +0000797 flags |= CIFS_NO_RESP;
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700798 rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Joe Perchesf96637b2013-05-04 22:12:25 -0500799 cifs_dbg(NOISY, "SendRcvNoRsp flags %d rc %d\n", flags, rc);
Steve French90c81e02008-02-12 20:32:36 +0000800
Steve French133672e2007-11-13 22:41:37 +0000801 return rc;
802}
803
Jeff Layton053d5032011-01-11 07:24:02 -0500804static int
Jeff Layton3c1105d2011-05-22 07:09:13 -0400805cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
Jeff Layton053d5032011-01-11 07:24:02 -0500806{
807 int rc = 0;
808
Joe Perchesf96637b2013-05-04 22:12:25 -0500809 cifs_dbg(FYI, "%s: cmd=%d mid=%llu state=%d\n",
810 __func__, le16_to_cpu(mid->command), mid->mid, mid->mid_state);
Jeff Layton053d5032011-01-11 07:24:02 -0500811
Jeff Layton74dd92a2011-01-11 07:24:02 -0500812 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400813 switch (mid->mid_state) {
Jeff Layton74dd92a2011-01-11 07:24:02 -0500814 case MID_RESPONSE_RECEIVED:
Jeff Layton053d5032011-01-11 07:24:02 -0500815 spin_unlock(&GlobalMid_Lock);
816 return rc;
Jeff Layton74dd92a2011-01-11 07:24:02 -0500817 case MID_RETRY_NEEDED:
818 rc = -EAGAIN;
819 break;
Jeff Layton71823ba2011-02-10 08:03:50 -0500820 case MID_RESPONSE_MALFORMED:
821 rc = -EIO;
822 break;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400823 case MID_SHUTDOWN:
824 rc = -EHOSTDOWN;
825 break;
Jeff Layton74dd92a2011-01-11 07:24:02 -0500826 default:
Jeff Layton3c1105d2011-05-22 07:09:13 -0400827 list_del_init(&mid->qhead);
Joe Perchesf96637b2013-05-04 22:12:25 -0500828 cifs_dbg(VFS, "%s: invalid mid state mid=%llu state=%d\n",
829 __func__, mid->mid, mid->mid_state);
Jeff Layton74dd92a2011-01-11 07:24:02 -0500830 rc = -EIO;
Jeff Layton053d5032011-01-11 07:24:02 -0500831 }
832 spin_unlock(&GlobalMid_Lock);
833
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500834 DeleteMidQEntry(mid);
Jeff Layton053d5032011-01-11 07:24:02 -0500835 return rc;
836}
837
Jeff Layton121b0462012-05-15 12:21:10 -0400838static inline int
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800839send_cancel(struct TCP_Server_Info *server, struct smb_rqst *rqst,
840 struct mid_q_entry *mid)
Jeff Layton76dcc262011-01-11 07:24:24 -0500841{
Jeff Layton121b0462012-05-15 12:21:10 -0400842 return server->ops->send_cancel ?
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800843 server->ops->send_cancel(server, rqst, mid) : 0;
Jeff Layton76dcc262011-01-11 07:24:24 -0500844}
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846int
Jeff Layton2c8f9812011-05-19 16:22:52 -0400847cifs_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
848 bool log_error)
849{
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400850 unsigned int len = get_rfc1002_length(mid->resp_buf) + 4;
Jeff Layton826a95e2011-10-11 06:41:32 -0400851
852 dump_smb(mid->resp_buf, min_t(u32, 92, len));
Jeff Layton2c8f9812011-05-19 16:22:52 -0400853
854 /* convert the length into a more usable form */
Jeff Layton38d77c52013-05-26 07:01:00 -0400855 if (server->sign) {
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800856 struct kvec iov[2];
Steve French985e4ff02012-08-03 09:42:45 -0500857 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800858 struct smb_rqst rqst = { .rq_iov = iov,
859 .rq_nvec = 2 };
Jeff Layton826a95e2011-10-11 06:41:32 -0400860
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800861 iov[0].iov_base = mid->resp_buf;
862 iov[0].iov_len = 4;
863 iov[1].iov_base = (char *)mid->resp_buf + 4;
864 iov[1].iov_len = len - 4;
Jeff Layton2c8f9812011-05-19 16:22:52 -0400865 /* FIXME: add code to kill session */
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700866 rc = cifs_verify_signature(&rqst, server,
Jeff Layton0124cc42013-04-03 11:55:03 -0400867 mid->sequence_number);
Steve French985e4ff02012-08-03 09:42:45 -0500868 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500869 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
870 rc);
Jeff Layton2c8f9812011-05-19 16:22:52 -0400871 }
872
873 /* BB special case reconnect tid and uid here? */
874 return map_smb_to_linux_error(mid->resp_buf, log_error);
875}
876
Jeff Laytonfec344e2012-09-18 16:20:35 -0700877struct mid_q_entry *
878cifs_setup_request(struct cifs_ses *ses, struct smb_rqst *rqst)
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400879{
880 int rc;
Jeff Laytonfec344e2012-09-18 16:20:35 -0700881 struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400882 struct mid_q_entry *mid;
883
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800884 if (rqst->rq_iov[0].iov_len != 4 ||
885 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
886 return ERR_PTR(-EIO);
887
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400888 rc = allocate_mid(ses, hdr, &mid);
889 if (rc)
Jeff Laytonfec344e2012-09-18 16:20:35 -0700890 return ERR_PTR(rc);
891 rc = cifs_sign_rqst(rqst, ses->server, &mid->sequence_number);
892 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700893 cifs_delete_mid(mid);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700894 return ERR_PTR(rc);
895 }
896 return mid;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400897}
898
Ronnie Sahlberg4e34feb2018-08-30 10:13:00 +1000899static void
Pavel Shilovskyee258d72019-01-03 15:53:10 -0800900cifs_compound_callback(struct mid_q_entry *mid)
Pavel Shilovsky8a26f0f2019-01-03 16:45:27 -0800901{
902 struct TCP_Server_Info *server = mid->server;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -0800903 struct cifs_credits credits;
Pavel Shilovsky8a26f0f2019-01-03 16:45:27 -0800904
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -0800905 credits.value = server->ops->get_credits(mid);
906 credits.instance = server->reconnect_instance;
907
908 add_credits(server, &credits, mid->optype);
Pavel Shilovsky8a26f0f2019-01-03 16:45:27 -0800909}
910
Pavel Shilovskyee258d72019-01-03 15:53:10 -0800911static void
912cifs_compound_last_callback(struct mid_q_entry *mid)
913{
914 cifs_compound_callback(mid);
915 cifs_wake_up_task(mid);
916}
917
918static void
919cifs_cancelled_callback(struct mid_q_entry *mid)
920{
921 cifs_compound_callback(mid);
922 DeleteMidQEntry(mid);
923}
924
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800925int
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000926compound_send_recv(const unsigned int xid, struct cifs_ses *ses,
927 const int flags, const int num_rqst, struct smb_rqst *rqst,
928 int *resp_buf_type, struct kvec *resp_iov)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Ronnie Sahlberg480b1cb2019-03-08 12:58:18 +1000930 int i, j, optype, rc = 0;
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000931 struct mid_q_entry *midQ[MAX_COMPOUND];
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -0800932 bool cancelled_mid[MAX_COMPOUND] = {false};
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -0800933 struct cifs_credits credits[MAX_COMPOUND] = {
934 { .value = 0, .instance = 0 }
935 };
936 unsigned int instance;
Pavel Shilovsky97ea4992019-01-15 16:07:52 -0800937 unsigned int first_instance = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800938 char *buf;
Steve French50c2f752007-07-13 00:33:32 +0000939
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400940 optype = flags & CIFS_OP_MASK;
Steve French133672e2007-11-13 22:41:37 +0000941
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000942 for (i = 0; i < num_rqst; i++)
943 resp_buf_type[i] = CIFS_NO_BUFFER; /* no response buf yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Steve French4b8f9302006-02-26 16:41:18 +0000945 if ((ses == NULL) || (ses->server == NULL)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500946 cifs_dbg(VFS, "Null session\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 return -EIO;
948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700950 if (ses->server->tcpStatus == CifsExiting)
Steve French31ca3bc2005-04-28 22:41:11 -0700951 return -ENOENT;
952
Pavel Shilovsky7091bca2019-01-30 16:58:09 -0800953 spin_lock(&ses->server->req_lock);
954 if (ses->server->credits < num_rqst) {
955 /*
956 * Return immediately if not too many requests in flight since
957 * we will likely be stuck on waiting for credits.
958 */
959 if (ses->server->in_flight < num_rqst - ses->server->credits) {
960 spin_unlock(&ses->server->req_lock);
961 return -ENOTSUPP;
962 }
963 } else {
964 /* enough credits to send the whole compounded request */
965 ses->server->credits -= num_rqst;
966 ses->server->in_flight += num_rqst;
967 first_instance = ses->server->reconnect_instance;
968 }
969 spin_unlock(&ses->server->req_lock);
970
971 if (first_instance) {
972 cifs_dbg(FYI, "Acquired %d credits at once\n", num_rqst);
973 for (i = 0; i < num_rqst; i++) {
974 credits[i].value = 1;
975 credits[i].instance = first_instance;
976 }
977 goto setup_rqsts;
978 }
979
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400980 /*
Pavel Shilovsky7091bca2019-01-30 16:58:09 -0800981 * There are not enough credits to send the whole compound request but
982 * there are requests in flight that may bring credits from the server.
983 * This approach still leaves the possibility to be stuck waiting for
984 * credits if the server doesn't grant credits to the outstanding
985 * requests. This should be fixed by returning immediately and letting
986 * a caller fallback to sequential commands instead of compounding.
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -0800987 * Ensure we obtain 1 credit per request in the compound chain.
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400988 */
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -0800989 for (i = 0; i < num_rqst; i++) {
Ronnie Sahlberg480b1cb2019-03-08 12:58:18 +1000990 rc = wait_for_free_request(ses->server, flags, &instance);
Pavel Shilovsky97ea4992019-01-15 16:07:52 -0800991
992 if (rc == 0) {
993 credits[i].value = 1;
994 credits[i].instance = instance;
995 /*
996 * All parts of the compound chain must get credits from
997 * the same session, otherwise we may end up using more
998 * credits than the server granted. If there were
999 * reconnects in between, return -EAGAIN and let callers
1000 * handle it.
1001 */
1002 if (i == 0)
1003 first_instance = instance;
1004 else if (first_instance != instance) {
1005 i++;
1006 rc = -EAGAIN;
1007 }
1008 }
1009
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -08001010 if (rc) {
1011 /*
1012 * We haven't sent an SMB packet to the server yet but
1013 * we already obtained credits for i requests in the
1014 * compound chain - need to return those credits back
1015 * for future use. Note that we need to call add_credits
1016 * multiple times to match the way we obtained credits
1017 * in the first place and to account for in flight
1018 * requests correctly.
1019 */
1020 for (j = 0; j < i; j++)
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08001021 add_credits(ses->server, &credits[j], optype);
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -08001022 return rc;
1023 }
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -08001024 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001025
Pavel Shilovsky7091bca2019-01-30 16:58:09 -08001026setup_rqsts:
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04001027 /*
1028 * Make sure that we sign in the same order that we send on this socket
1029 * and avoid races inside tcp sendmsg code that could cause corruption
1030 * of smb data.
1031 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Jeff Layton72ca5452008-12-01 07:09:36 -05001033 mutex_lock(&ses->server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Pavel Shilovsky97ea4992019-01-15 16:07:52 -08001035 /*
1036 * All the parts of the compound chain belong obtained credits from the
1037 * same session (see the appropriate checks above). In the same time
1038 * there might be reconnects after those checks but before we acquired
1039 * the srv_mutex. We can not use credits obtained from the previous
1040 * session to send this request. Check if there were reconnects after
1041 * we obtained credits and return -EAGAIN in such cases to let callers
1042 * handle it.
1043 */
1044 if (first_instance != ses->server->reconnect_instance) {
1045 mutex_unlock(&ses->server->srv_mutex);
1046 for (j = 0; j < num_rqst; j++)
1047 add_credits(ses->server, &credits[j], optype);
1048 return -EAGAIN;
1049 }
1050
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001051 for (i = 0; i < num_rqst; i++) {
1052 midQ[i] = ses->server->ops->setup_request(ses, &rqst[i]);
1053 if (IS_ERR(midQ[i])) {
Pavel Shilovskyc781af72019-03-04 14:02:50 -08001054 revert_current_mid(ses->server, i);
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001055 for (j = 0; j < i; j++)
1056 cifs_delete_mid(midQ[j]);
1057 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -08001058
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001059 /* Update # of requests on wire to server */
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -08001060 for (j = 0; j < num_rqst; j++)
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08001061 add_credits(ses->server, &credits[j], optype);
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001062 return PTR_ERR(midQ[i]);
1063 }
1064
1065 midQ[i]->mid_state = MID_REQUEST_SUBMITTED;
Pavel Shilovsky8a26f0f2019-01-03 16:45:27 -08001066 midQ[i]->optype = optype;
Ronnie Sahlberg4e34feb2018-08-30 10:13:00 +10001067 /*
Pavel Shilovskyee258d72019-01-03 15:53:10 -08001068 * Invoke callback for every part of the compound chain
1069 * to calculate credits properly. Wake up this thread only when
1070 * the last element is received.
Ronnie Sahlberg4e34feb2018-08-30 10:13:00 +10001071 */
1072 if (i < num_rqst - 1)
Pavel Shilovskyee258d72019-01-03 15:53:10 -08001073 midQ[i]->callback = cifs_compound_callback;
1074 else
1075 midQ[i]->callback = cifs_compound_last_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
Steve French789e6662011-08-09 18:44:44 +00001077 cifs_in_send_inc(ses->server);
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001078 rc = smb_send_rqst(ses->server, num_rqst, rqst, flags);
Steve French789e6662011-08-09 18:44:44 +00001079 cifs_in_send_dec(ses->server);
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001080
1081 for (i = 0; i < num_rqst; i++)
1082 cifs_save_when_sent(midQ[i]);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001083
Pavel Shilovskyc781af72019-03-04 14:02:50 -08001084 if (rc < 0) {
1085 revert_current_mid(ses->server, num_rqst);
Jeff Laytonad313cb2013-04-03 10:27:36 -04001086 ses->server->sequence_number -= 2;
Pavel Shilovskyc781af72019-03-04 14:02:50 -08001087 }
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001088
Jeff Layton72ca5452008-12-01 07:09:36 -05001089 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001090
Pavel Shilovskyee258d72019-01-03 15:53:10 -08001091 if (rc < 0) {
1092 /* Sending failed for some reason - return credits back */
1093 for (i = 0; i < num_rqst; i++)
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08001094 add_credits(ses->server, &credits[i], optype);
Ronnie Sahlbergcb5c2e62018-10-10 15:29:06 +10001095 goto out;
Pavel Shilovskyee258d72019-01-03 15:53:10 -08001096 }
1097
1098 /*
1099 * At this point the request is passed to the network stack - we assume
1100 * that any credits taken from the server structure on the client have
1101 * been spent and we can't return them back. Once we receive responses
1102 * we will collect credits granted by the server in the mid callbacks
1103 * and add those credits to the server structure.
1104 */
Ronnie Sahlbergcb5c2e62018-10-10 15:29:06 +10001105
1106 /*
1107 * Compounding is never used during session establish.
1108 */
1109 if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP))
1110 smb311_update_preauth_hash(ses, rqst[0].rq_iov,
1111 rqst[0].rq_nvec);
1112
Ronnie Sahlberg480b1cb2019-03-08 12:58:18 +10001113 if ((flags & CIFS_TIMEOUT_MASK) == CIFS_ASYNC_OP)
Ronnie Sahlbergcb5c2e62018-10-10 15:29:06 +10001114 goto out;
1115
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001116 for (i = 0; i < num_rqst; i++) {
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001117 rc = wait_for_response(ses->server, midQ[i]);
Pavel Shilovsky8a26f0f2019-01-03 16:45:27 -08001118 if (rc != 0)
1119 break;
1120 }
1121 if (rc != 0) {
1122 for (; i < num_rqst; i++) {
Steve French43de1db2018-10-23 21:04:57 -05001123 cifs_dbg(VFS, "Cancelling wait for mid %llu cmd: %d\n",
1124 midQ[i]->mid, le16_to_cpu(midQ[i]->command));
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001125 send_cancel(ses->server, &rqst[i], midQ[i]);
1126 spin_lock(&GlobalMid_Lock);
1127 if (midQ[i]->mid_state == MID_REQUEST_SUBMITTED) {
1128 midQ[i]->mid_flags |= MID_WAIT_CANCELLED;
Pavel Shilovsky8a26f0f2019-01-03 16:45:27 -08001129 midQ[i]->callback = cifs_cancelled_callback;
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -08001130 cancelled_mid[i] = true;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08001131 credits[i].value = 0;
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001132 }
Jeff Layton1be912d2011-01-28 07:08:28 -05001133 spin_unlock(&GlobalMid_Lock);
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001134 }
Ronnie Sahlbergcb5c2e62018-10-10 15:29:06 +10001135 }
1136
Ronnie Sahlbergcb5c2e62018-10-10 15:29:06 +10001137 for (i = 0; i < num_rqst; i++) {
1138 if (rc < 0)
1139 goto out;
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001140
1141 rc = cifs_sync_mid_result(midQ[i], ses->server);
1142 if (rc != 0) {
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -08001143 /* mark this mid as cancelled to not free it below */
1144 cancelled_mid[i] = true;
1145 goto out;
Jeff Layton1be912d2011-01-28 07:08:28 -05001146 }
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001147
1148 if (!midQ[i]->resp_buf ||
1149 midQ[i]->mid_state != MID_RESPONSE_RECEIVED) {
1150 rc = -EIO;
1151 cifs_dbg(FYI, "Bad MID state?\n");
1152 goto out;
1153 }
1154
1155 buf = (char *)midQ[i]->resp_buf;
1156 resp_iov[i].iov_base = buf;
1157 resp_iov[i].iov_len = midQ[i]->resp_buf_size +
1158 ses->server->vals->header_preamble_size;
1159
1160 if (midQ[i]->large_buf)
1161 resp_buf_type[i] = CIFS_LARGE_BUFFER;
1162 else
1163 resp_buf_type[i] = CIFS_SMALL_BUFFER;
1164
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001165 rc = ses->server->ops->check_receive(midQ[i], ses->server,
1166 flags & CIFS_LOG_ERROR);
1167
1168 /* mark it so buf will not be freed by cifs_delete_mid */
1169 if ((flags & CIFS_NO_RESP) == 0)
1170 midQ[i]->resp_buf = NULL;
Ronnie Sahlbergcb5c2e62018-10-10 15:29:06 +10001171
Jeff Layton1be912d2011-01-28 07:08:28 -05001172 }
Ronnie Sahlbergcb5c2e62018-10-10 15:29:06 +10001173
1174 /*
1175 * Compounding is never used during session establish.
1176 */
1177 if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP)) {
1178 struct kvec iov = {
1179 .iov_base = resp_iov[0].iov_base,
1180 .iov_len = resp_iov[0].iov_len
1181 };
1182 smb311_update_preauth_hash(ses, &iov, 1);
1183 }
1184
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001185out:
Ronnie Sahlberg4e34feb2018-08-30 10:13:00 +10001186 /*
1187 * This will dequeue all mids. After this it is important that the
1188 * demultiplex_thread will not process any of these mids any futher.
1189 * This is prevented above by using a noop callback that will not
1190 * wake this thread except for the very last PDU.
1191 */
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -08001192 for (i = 0; i < num_rqst; i++) {
1193 if (!cancelled_mid[i])
1194 cifs_delete_mid(midQ[i]);
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -08001195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 return rc;
1198}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200int
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001201cifs_send_recv(const unsigned int xid, struct cifs_ses *ses,
1202 struct smb_rqst *rqst, int *resp_buf_type, const int flags,
1203 struct kvec *resp_iov)
1204{
1205 return compound_send_recv(xid, ses, flags, 1, rqst, resp_buf_type,
1206 resp_iov);
1207}
1208
1209int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001210SendReceive2(const unsigned int xid, struct cifs_ses *ses,
1211 struct kvec *iov, int n_vec, int *resp_buf_type /* ret */,
1212 const int flags, struct kvec *resp_iov)
1213{
1214 struct smb_rqst rqst;
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +11001215 struct kvec s_iov[CIFS_MAX_IOV_SIZE], *new_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001216 int rc;
1217
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +11001218 if (n_vec + 1 > CIFS_MAX_IOV_SIZE) {
Kees Cook6da2ec52018-06-12 13:55:00 -07001219 new_iov = kmalloc_array(n_vec + 1, sizeof(struct kvec),
1220 GFP_KERNEL);
Steve French117e3b72018-04-22 10:24:19 -05001221 if (!new_iov) {
1222 /* otherwise cifs_send_recv below sets resp_buf_type */
1223 *resp_buf_type = CIFS_NO_BUFFER;
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +11001224 return -ENOMEM;
Steve French117e3b72018-04-22 10:24:19 -05001225 }
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +11001226 } else
1227 new_iov = s_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001228
1229 /* 1st iov is a RFC1001 length followed by the rest of the packet */
1230 memcpy(new_iov + 1, iov, (sizeof(struct kvec) * n_vec));
1231
1232 new_iov[0].iov_base = new_iov[1].iov_base;
1233 new_iov[0].iov_len = 4;
1234 new_iov[1].iov_base += 4;
1235 new_iov[1].iov_len -= 4;
1236
1237 memset(&rqst, 0, sizeof(struct smb_rqst));
1238 rqst.rq_iov = new_iov;
1239 rqst.rq_nvec = n_vec + 1;
1240
1241 rc = cifs_send_recv(xid, ses, &rqst, resp_buf_type, flags, resp_iov);
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +11001242 if (n_vec + 1 > CIFS_MAX_IOV_SIZE)
1243 kfree(new_iov);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001244 return rc;
1245}
1246
1247int
Steve French96daf2b2011-05-27 04:34:02 +00001248SendReceive(const unsigned int xid, struct cifs_ses *ses,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
Ronnie Sahlberg480b1cb2019-03-08 12:58:18 +10001250 int *pbytes_returned, const int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
1252 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 struct mid_q_entry *midQ;
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001254 unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
1255 struct kvec iov = { .iov_base = in_buf, .iov_len = len };
1256 struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08001257 struct cifs_credits credits = { .value = 1, .instance = 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 if (ses == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001260 cifs_dbg(VFS, "Null smb session\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 return -EIO;
1262 }
Steve French79a58d12007-07-06 22:44:50 +00001263 if (ses->server == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001264 cifs_dbg(VFS, "Null tcp session\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 return -EIO;
1266 }
1267
Steve French79a58d12007-07-06 22:44:50 +00001268 if (ses->server->tcpStatus == CifsExiting)
Steve French31ca3bc2005-04-28 22:41:11 -07001269 return -ENOENT;
1270
Steve French79a58d12007-07-06 22:44:50 +00001271 /* Ensure that we do not send more than 50 overlapping requests
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 to the same server. We may make this configurable later or
1273 use ses->maxReq */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001275 if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001276 cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001277 len);
Volker Lendecke6d9c6d52008-12-08 20:50:24 +00001278 return -EIO;
1279 }
1280
Ronnie Sahlberg480b1cb2019-03-08 12:58:18 +10001281 rc = wait_for_free_request(ses->server, flags, &credits.instance);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001282 if (rc)
1283 return rc;
1284
Steve French79a58d12007-07-06 22:44:50 +00001285 /* make sure that we sign in the same order that we send on this socket
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 and avoid races inside tcp sendmsg code that could cause corruption
1287 of smb data */
1288
Jeff Layton72ca5452008-12-01 07:09:36 -05001289 mutex_lock(&ses->server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001291 rc = allocate_mid(ses, in_buf, &midQ);
1292 if (rc) {
Jeff Layton72ca5452008-12-01 07:09:36 -05001293 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001294 /* Update # of requests on wire to server */
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08001295 add_credits(ses->server, &credits, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001296 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
1298
Steve Frenchad009ac2005-04-28 22:41:05 -07001299 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
Volker Lendecke829049c2008-12-06 16:00:53 +01001300 if (rc) {
1301 mutex_unlock(&ses->server->srv_mutex);
1302 goto out;
1303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001305 midQ->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +00001306
1307 cifs_in_send_inc(ses->server);
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001308 rc = smb_send(ses->server, in_buf, len);
Steve French789e6662011-08-09 18:44:44 +00001309 cifs_in_send_dec(ses->server);
1310 cifs_save_when_sent(midQ);
Jeff Laytonad313cb2013-04-03 10:27:36 -04001311
1312 if (rc < 0)
1313 ses->server->sequence_number -= 2;
1314
Jeff Layton72ca5452008-12-01 07:09:36 -05001315 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001316
Steve French79a58d12007-07-06 22:44:50 +00001317 if (rc < 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001318 goto out;
1319
Ronnie Sahlberg480b1cb2019-03-08 12:58:18 +10001320 if ((flags & CIFS_TIMEOUT_MASK) == CIFS_ASYNC_OP)
Steve French133672e2007-11-13 22:41:37 +00001321 goto out;
Jeff Layton0ade6402011-01-11 07:24:02 -05001322
1323 rc = wait_for_response(ses->server, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -05001324 if (rc != 0) {
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001325 send_cancel(ses->server, &rqst, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -05001326 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001327 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
Jeff Layton1be912d2011-01-28 07:08:28 -05001328 /* no longer considered to be "in-flight" */
1329 midQ->callback = DeleteMidQEntry;
1330 spin_unlock(&GlobalMid_Lock);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08001331 add_credits(ses->server, &credits, 0);
Jeff Layton1be912d2011-01-28 07:08:28 -05001332 return rc;
1333 }
1334 spin_unlock(&GlobalMid_Lock);
1335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Jeff Layton3c1105d2011-05-22 07:09:13 -04001337 rc = cifs_sync_mid_result(midQ, ses->server);
Jeff Layton053d5032011-01-11 07:24:02 -05001338 if (rc != 0) {
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08001339 add_credits(ses->server, &credits, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 return rc;
1341 }
Steve French50c2f752007-07-13 00:33:32 +00001342
Jeff Layton2c8f9812011-05-19 16:22:52 -04001343 if (!midQ->resp_buf || !out_buf ||
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001344 midQ->mid_state != MID_RESPONSE_RECEIVED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 rc = -EIO;
Joe Perchesf96637b2013-05-04 22:12:25 -05001346 cifs_dbg(VFS, "Bad MID state?\n");
Steve French2b2bdfb2008-12-11 17:26:54 +00001347 goto out;
1348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001350 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
Jeff Layton2c8f9812011-05-19 16:22:52 -04001351 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
1352 rc = cifs_check_receive(midQ, ses->server, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001353out:
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001354 cifs_delete_mid(midQ);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08001355 add_credits(ses->server, &credits, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 return rc;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001358}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001360/* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
1361 blocking lock to return. */
1362
1363static int
Steve French96daf2b2011-05-27 04:34:02 +00001364send_lock_cancel(const unsigned int xid, struct cifs_tcon *tcon,
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001365 struct smb_hdr *in_buf,
1366 struct smb_hdr *out_buf)
1367{
1368 int bytes_returned;
Steve French96daf2b2011-05-27 04:34:02 +00001369 struct cifs_ses *ses = tcon->ses;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001370 LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
1371
1372 /* We just modify the current in_buf to change
1373 the type of lock from LOCKING_ANDX_SHARED_LOCK
1374 or LOCKING_ANDX_EXCLUSIVE_LOCK to
1375 LOCKING_ANDX_CANCEL_LOCK. */
1376
1377 pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
1378 pSMB->Timeout = 0;
Pavel Shilovsky88257362012-05-23 14:01:59 +04001379 pSMB->hdr.Mid = get_next_mid(ses->server);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001380
1381 return SendReceive(xid, ses, in_buf, out_buf,
Jeff Layton77499812011-01-11 07:24:23 -05001382 &bytes_returned, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001383}
1384
1385int
Steve French96daf2b2011-05-27 04:34:02 +00001386SendReceiveBlockingLock(const unsigned int xid, struct cifs_tcon *tcon,
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001387 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
1388 int *pbytes_returned)
1389{
1390 int rc = 0;
1391 int rstart = 0;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001392 struct mid_q_entry *midQ;
Steve French96daf2b2011-05-27 04:34:02 +00001393 struct cifs_ses *ses;
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001394 unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
1395 struct kvec iov = { .iov_base = in_buf, .iov_len = len };
1396 struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08001397 unsigned int instance;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001398
1399 if (tcon == NULL || tcon->ses == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001400 cifs_dbg(VFS, "Null smb session\n");
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001401 return -EIO;
1402 }
1403 ses = tcon->ses;
1404
Steve French79a58d12007-07-06 22:44:50 +00001405 if (ses->server == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001406 cifs_dbg(VFS, "Null tcp session\n");
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001407 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
1409
Steve French79a58d12007-07-06 22:44:50 +00001410 if (ses->server->tcpStatus == CifsExiting)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001411 return -ENOENT;
1412
Steve French79a58d12007-07-06 22:44:50 +00001413 /* Ensure that we do not send more than 50 overlapping requests
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001414 to the same server. We may make this configurable later or
1415 use ses->maxReq */
1416
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001417 if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001418 cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001419 len);
Volker Lendecke6d9c6d52008-12-08 20:50:24 +00001420 return -EIO;
1421 }
1422
Ronnie Sahlberg480b1cb2019-03-08 12:58:18 +10001423 rc = wait_for_free_request(ses->server, CIFS_BLOCKING_OP, &instance);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001424 if (rc)
1425 return rc;
1426
Steve French79a58d12007-07-06 22:44:50 +00001427 /* make sure that we sign in the same order that we send on this socket
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001428 and avoid races inside tcp sendmsg code that could cause corruption
1429 of smb data */
1430
Jeff Layton72ca5452008-12-01 07:09:36 -05001431 mutex_lock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001432
1433 rc = allocate_mid(ses, in_buf, &midQ);
1434 if (rc) {
Jeff Layton72ca5452008-12-01 07:09:36 -05001435 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001436 return rc;
1437 }
1438
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001439 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
Volker Lendecke829049c2008-12-06 16:00:53 +01001440 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001441 cifs_delete_mid(midQ);
Volker Lendecke829049c2008-12-06 16:00:53 +01001442 mutex_unlock(&ses->server->srv_mutex);
1443 return rc;
1444 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001445
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001446 midQ->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +00001447 cifs_in_send_inc(ses->server);
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001448 rc = smb_send(ses->server, in_buf, len);
Steve French789e6662011-08-09 18:44:44 +00001449 cifs_in_send_dec(ses->server);
1450 cifs_save_when_sent(midQ);
Jeff Laytonad313cb2013-04-03 10:27:36 -04001451
1452 if (rc < 0)
1453 ses->server->sequence_number -= 2;
1454
Jeff Layton72ca5452008-12-01 07:09:36 -05001455 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001456
Steve French79a58d12007-07-06 22:44:50 +00001457 if (rc < 0) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001458 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001459 return rc;
1460 }
1461
1462 /* Wait for a reply - allow signals to interrupt. */
1463 rc = wait_event_interruptible(ses->server->response_q,
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001464 (!(midQ->mid_state == MID_REQUEST_SUBMITTED)) ||
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001465 ((ses->server->tcpStatus != CifsGood) &&
1466 (ses->server->tcpStatus != CifsNew)));
1467
1468 /* Were we interrupted by a signal ? */
1469 if ((rc == -ERESTARTSYS) &&
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001470 (midQ->mid_state == MID_REQUEST_SUBMITTED) &&
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001471 ((ses->server->tcpStatus == CifsGood) ||
1472 (ses->server->tcpStatus == CifsNew))) {
1473
1474 if (in_buf->Command == SMB_COM_TRANSACTION2) {
1475 /* POSIX lock. We send a NT_CANCEL SMB to cause the
1476 blocking lock to return. */
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001477 rc = send_cancel(ses->server, &rqst, midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001478 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001479 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001480 return rc;
1481 }
1482 } else {
1483 /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
1484 to cause the blocking lock to return. */
1485
1486 rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
1487
1488 /* If we get -ENOLCK back the lock may have
1489 already been removed. Don't exit in this case. */
1490 if (rc && rc != -ENOLCK) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001491 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001492 return rc;
1493 }
1494 }
1495
Jeff Layton1be912d2011-01-28 07:08:28 -05001496 rc = wait_for_response(ses->server, midQ);
1497 if (rc) {
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001498 send_cancel(ses->server, &rqst, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -05001499 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001500 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
Jeff Layton1be912d2011-01-28 07:08:28 -05001501 /* no longer considered to be "in-flight" */
1502 midQ->callback = DeleteMidQEntry;
1503 spin_unlock(&GlobalMid_Lock);
1504 return rc;
1505 }
1506 spin_unlock(&GlobalMid_Lock);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001507 }
Jeff Layton1be912d2011-01-28 07:08:28 -05001508
1509 /* We got the response - restart system call. */
1510 rstart = 1;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001511 }
1512
Jeff Layton3c1105d2011-05-22 07:09:13 -04001513 rc = cifs_sync_mid_result(midQ, ses->server);
Jeff Layton053d5032011-01-11 07:24:02 -05001514 if (rc != 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001515 return rc;
Steve French50c2f752007-07-13 00:33:32 +00001516
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001517 /* rcvd frame is ok */
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001518 if (out_buf == NULL || midQ->mid_state != MID_RESPONSE_RECEIVED) {
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001519 rc = -EIO;
Joe Perchesf96637b2013-05-04 22:12:25 -05001520 cifs_dbg(VFS, "Bad MID state?\n");
Volker Lendecke698e96a2008-12-06 16:39:31 +01001521 goto out;
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001522 }
1523
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001524 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
Jeff Layton2c8f9812011-05-19 16:22:52 -04001525 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
1526 rc = cifs_check_receive(midQ, ses->server, 0);
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001527out:
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001528 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001529 if (rstart && rc == -EACCES)
1530 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 return rc;
1532}