blob: 9391cd17a2b55c0665684e20f4bb582cf8661efe [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>
Ronnie Sahlberg14e25972019-05-13 11:24:17 +100036#include <linux/sched/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 */
Vincent Whitchurchf1f27ad2020-01-23 17:09:06 +010079 get_task_struct(current);
80 temp->creator = current;
NeilBrowna6f74e82017-04-10 12:08:53 +100081 temp->callback = cifs_wake_up_task;
82 temp->callback_data = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 atomic_inc(&midCount);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -040085 temp->mid_state = MID_REQUEST_ALLOCATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return temp;
87}
88
Lars Persson696e4202018-06-25 14:05:25 +020089static void _cifs_mid_q_entry_release(struct kref *refcount)
90{
Pavel Shilovskyabe57072019-10-22 08:41:42 -070091 struct mid_q_entry *midEntry =
92 container_of(refcount, struct mid_q_entry, refcount);
Steve French1047abc2005-10-11 19:58:06 -070093#ifdef CONFIG_CIFS_STATS2
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +040094 __le16 command = midEntry->server->vals->lock_cmd;
Steve French433b8dd2019-03-26 13:53:21 -050095 __u16 smb_cmd = le16_to_cpu(midEntry->command);
Steve French1047abc2005-10-11 19:58:06 -070096 unsigned long now;
Steve French433b8dd2019-03-26 13:53:21 -050097 unsigned long roundtrip_time;
Steve French1047abc2005-10-11 19:58:06 -070098#endif
Pavel Shilovsky7b718432019-11-21 11:35:14 -080099 struct TCP_Server_Info *server = midEntry->server;
100
101 if (midEntry->resp_buf && (midEntry->mid_flags & MID_WAIT_CANCELLED) &&
102 midEntry->mid_state == MID_RESPONSE_RECEIVED &&
103 server->ops->handle_cancelled_mid)
104 server->ops->handle_cancelled_mid(midEntry->resp_buf, server);
105
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400106 midEntry->mid_state = MID_FREE;
Jeff Layton80975312011-01-11 07:24:02 -0500107 atomic_dec(&midCount);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400108 if (midEntry->large_buf)
Steve Frenchb8643e12005-04-28 22:41:07 -0700109 cifs_buf_release(midEntry->resp_buf);
110 else
111 cifs_small_buf_release(midEntry->resp_buf);
Steve French1047abc2005-10-11 19:58:06 -0700112#ifdef CONFIG_CIFS_STATS2
113 now = jiffies;
Steve French433b8dd2019-03-26 13:53:21 -0500114 if (now < midEntry->when_alloc)
Joe Perchesa0a30362020-04-14 22:42:53 -0700115 cifs_server_dbg(VFS, "Invalid mid allocation time\n");
Steve French433b8dd2019-03-26 13:53:21 -0500116 roundtrip_time = now - midEntry->when_alloc;
117
118 if (smb_cmd < NUMBER_OF_SMB2_COMMANDS) {
119 if (atomic_read(&server->num_cmds[smb_cmd]) == 0) {
120 server->slowest_cmd[smb_cmd] = roundtrip_time;
121 server->fastest_cmd[smb_cmd] = roundtrip_time;
122 } else {
123 if (server->slowest_cmd[smb_cmd] < roundtrip_time)
124 server->slowest_cmd[smb_cmd] = roundtrip_time;
125 else if (server->fastest_cmd[smb_cmd] > roundtrip_time)
126 server->fastest_cmd[smb_cmd] = roundtrip_time;
127 }
128 cifs_stats_inc(&server->num_cmds[smb_cmd]);
129 server->time_per_cmd[smb_cmd] += roundtrip_time;
130 }
Steve French00778e22018-09-18 14:05:18 -0500131 /*
132 * commands taking longer than one second (default) can be indications
133 * that something is wrong, unless it is quite a slow link or a very
134 * busy server. Note that this calc is unlikely or impossible to wrap
135 * as long as slow_rsp_threshold is not set way above recommended max
136 * value (32767 ie 9 hours) and is generally harmless even if wrong
137 * since only affects debug counters - so leaving the calc as simple
138 * comparison rather than doing multiple conversions and overflow
139 * checks
140 */
141 if ((slow_rsp_threshold != 0) &&
142 time_after(now, midEntry->when_alloc + (slow_rsp_threshold * HZ)) &&
Steve French020eec52018-08-01 16:38:07 -0500143 (midEntry->command != command)) {
Steve Frenchf5942db2018-11-14 01:37:39 -0600144 /*
145 * smb2slowcmd[NUMBER_OF_SMB2_COMMANDS] counts by command
146 * NB: le16_to_cpu returns unsigned so can not be negative below
147 */
Steve French433b8dd2019-03-26 13:53:21 -0500148 if (smb_cmd < NUMBER_OF_SMB2_COMMANDS)
149 cifs_stats_inc(&server->smb2slowcmd[smb_cmd]);
Steve French468d6772018-08-04 05:24:34 -0500150
Steve French433b8dd2019-03-26 13:53:21 -0500151 trace_smb3_slow_rsp(smb_cmd, midEntry->mid, midEntry->pid,
Steve French020eec52018-08-01 16:38:07 -0500152 midEntry->when_sent, midEntry->when_received);
153 if (cifsFYI & CIFS_TIMER) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700154 pr_debug("slow rsp: cmd %d mid %llu",
155 midEntry->command, midEntry->mid);
156 cifs_info("A: 0x%lx S: 0x%lx R: 0x%lx\n",
157 now - midEntry->when_alloc,
158 now - midEntry->when_sent,
159 now - midEntry->when_received);
Steve French1047abc2005-10-11 19:58:06 -0700160 }
161 }
162#endif
Vincent Whitchurchf1f27ad2020-01-23 17:09:06 +0100163 put_task_struct(midEntry->creator);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700164
165 mempool_free(midEntry, cifs_mid_poolp);
166}
167
168void cifs_mid_q_entry_release(struct mid_q_entry *midEntry)
169{
170 spin_lock(&GlobalMid_Lock);
171 kref_put(&midEntry->refcount, _cifs_mid_q_entry_release);
172 spin_unlock(&GlobalMid_Lock);
173}
174
175void DeleteMidQEntry(struct mid_q_entry *midEntry)
176{
Lars Persson696e4202018-06-25 14:05:25 +0200177 cifs_mid_q_entry_release(midEntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700180void
181cifs_delete_mid(struct mid_q_entry *mid)
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500182{
183 spin_lock(&GlobalMid_Lock);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700184 if (!(mid->mid_flags & MID_DELETED)) {
185 list_del_init(&mid->qhead);
186 mid->mid_flags |= MID_DELETED;
187 }
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500188 spin_unlock(&GlobalMid_Lock);
189
190 DeleteMidQEntry(mid);
191}
192
Jeff Layton6f49f462012-09-18 16:20:34 -0700193/*
194 * smb_send_kvec - send an array of kvecs to the server
195 * @server: Server to send the data to
Al Viro3ab3f2a2015-11-13 02:36:04 -0500196 * @smb_msg: Message to send
Jeff Layton6f49f462012-09-18 16:20:34 -0700197 * @sent: amount of data sent on socket is stored here
198 *
199 * Our basic "send data to server" function. Should be called with srv_mutex
200 * held. The caller is responsible for handling the results.
201 */
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500202static int
Al Viro3ab3f2a2015-11-13 02:36:04 -0500203smb_send_kvec(struct TCP_Server_Info *server, struct msghdr *smb_msg,
204 size_t *sent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 int rc = 0;
Al Viro3ab3f2a2015-11-13 02:36:04 -0500207 int retries = 0;
Steve Frenchedf1ae42008-10-29 00:47:57 +0000208 struct socket *ssocket = server->ssocket;
Steve French50c2f752007-07-13 00:33:32 +0000209
Jeff Layton6f49f462012-09-18 16:20:34 -0700210 *sent = 0;
211
Al Viro3ab3f2a2015-11-13 02:36:04 -0500212 smb_msg->msg_name = (struct sockaddr *) &server->dstaddr;
213 smb_msg->msg_namelen = sizeof(struct sockaddr);
214 smb_msg->msg_control = NULL;
215 smb_msg->msg_controllen = 0;
Jeff Layton0496e022008-12-30 12:39:16 -0500216 if (server->noblocksnd)
Al Viro3ab3f2a2015-11-13 02:36:04 -0500217 smb_msg->msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL;
Steve Frenchedf1ae42008-10-29 00:47:57 +0000218 else
Al Viro3ab3f2a2015-11-13 02:36:04 -0500219 smb_msg->msg_flags = MSG_NOSIGNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Al Viro3ab3f2a2015-11-13 02:36:04 -0500221 while (msg_data_left(smb_msg)) {
Jeff Layton6f49f462012-09-18 16:20:34 -0700222 /*
223 * If blocking send, we try 3 times, since each can block
224 * for 5 seconds. For nonblocking we have to try more
225 * but wait increasing amounts of time allowing time for
226 * socket to clear. The overall time we wait in either
227 * case to send on the socket is about 15 seconds.
228 * Similarly we wait for 15 seconds for a response from
229 * the server in SendReceive[2] for the server to send
230 * a response back for most types of requests (except
231 * SMB Write past end of file which can be slow, and
232 * blocking lock operations). NFS waits slightly longer
233 * than CIFS, but this can make it take longer for
234 * nonresponsive servers to be detected and 15 seconds
235 * is more than enough time for modern networks to
236 * send a packet. In most cases if we fail to send
237 * after the retries we will kill the socket and
238 * reconnect which may clear the network problem.
239 */
Al Viro3ab3f2a2015-11-13 02:36:04 -0500240 rc = sock_sendmsg(ssocket, smb_msg);
Jeff Laytonce6c44e2013-03-22 08:36:45 -0400241 if (rc == -EAGAIN) {
Al Viro3ab3f2a2015-11-13 02:36:04 -0500242 retries++;
243 if (retries >= 14 ||
244 (!server->noblocksnd && (retries > 2))) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000245 cifs_server_dbg(VFS, "sends on sock %p stuck for 15 seconds\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500246 ssocket);
Al Viro3ab3f2a2015-11-13 02:36:04 -0500247 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
Al Viro3ab3f2a2015-11-13 02:36:04 -0500249 msleep(1 << retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 continue;
251 }
Jeff Layton6f49f462012-09-18 16:20:34 -0700252
Steve French79a58d12007-07-06 22:44:50 +0000253 if (rc < 0)
Al Viro3ab3f2a2015-11-13 02:36:04 -0500254 return rc;
Jeff Layton6f49f462012-09-18 16:20:34 -0700255
Steve French79a58d12007-07-06 22:44:50 +0000256 if (rc == 0) {
Steve French3e844692005-10-03 13:37:24 -0700257 /* should never happen, letting socket clear before
258 retrying is our only obvious option here */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000259 cifs_server_dbg(VFS, "tcp sent no data\n");
Steve French3e844692005-10-03 13:37:24 -0700260 msleep(500);
261 continue;
262 }
Jeff Layton6f49f462012-09-18 16:20:34 -0700263
Al Viro3ab3f2a2015-11-13 02:36:04 -0500264 /* send was at least partially successful */
265 *sent += rc;
266 retries = 0; /* in case we get ENOSPC on the next send */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
Al Viro3ab3f2a2015-11-13 02:36:04 -0500268 return 0;
Jeff Layton97bc00b2012-09-18 16:20:35 -0700269}
270
Paulo Alcantara35e2cc12018-06-15 10:22:44 -0300271unsigned long
Ronnie Sahlberg81f39f92018-06-28 10:47:14 +1000272smb_rqst_len(struct TCP_Server_Info *server, struct smb_rqst *rqst)
Jeff Laytona26054d2014-02-14 07:21:00 -0500273{
274 unsigned int i;
Paulo Alcantara35e2cc12018-06-15 10:22:44 -0300275 struct kvec *iov;
276 int nvec;
Jeff Laytona26054d2014-02-14 07:21:00 -0500277 unsigned long buflen = 0;
278
Ronnie Sahlberg81f39f92018-06-28 10:47:14 +1000279 if (server->vals->header_preamble_size == 0 &&
280 rqst->rq_nvec >= 2 && rqst->rq_iov[0].iov_len == 4) {
Paulo Alcantara35e2cc12018-06-15 10:22:44 -0300281 iov = &rqst->rq_iov[1];
282 nvec = rqst->rq_nvec - 1;
283 } else {
284 iov = rqst->rq_iov;
285 nvec = rqst->rq_nvec;
286 }
287
Jeff Laytona26054d2014-02-14 07:21:00 -0500288 /* total up iov array first */
Paulo Alcantara35e2cc12018-06-15 10:22:44 -0300289 for (i = 0; i < nvec; i++)
Jeff Laytona26054d2014-02-14 07:21:00 -0500290 buflen += iov[i].iov_len;
291
Long Lic06a0f22018-05-30 12:47:57 -0700292 /*
293 * Add in the page array if there is one. The caller needs to make
294 * sure rq_offset and rq_tailsz are set correctly. If a buffer of
295 * multiple pages ends at page boundary, rq_tailsz needs to be set to
296 * PAGE_SIZE.
297 */
Jeff Laytona26054d2014-02-14 07:21:00 -0500298 if (rqst->rq_npages) {
Long Lic06a0f22018-05-30 12:47:57 -0700299 if (rqst->rq_npages == 1)
300 buflen += rqst->rq_tailsz;
301 else {
302 /*
303 * If there is more than one page, calculate the
304 * buffer length based on rq_offset and rq_tailsz
305 */
306 buflen += rqst->rq_pagesz * (rqst->rq_npages - 1) -
307 rqst->rq_offset;
308 buflen += rqst->rq_tailsz;
309 }
Jeff Laytona26054d2014-02-14 07:21:00 -0500310 }
311
312 return buflen;
313}
314
Jeff Layton6f49f462012-09-18 16:20:34 -0700315static int
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000316__smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
317 struct smb_rqst *rqst)
Jeff Layton6f49f462012-09-18 16:20:34 -0700318{
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000319 int rc = 0;
320 struct kvec *iov;
321 int n_vec;
322 unsigned int send_length = 0;
323 unsigned int i, j;
Pavel Shilovskyb30c74c2019-03-05 15:51:57 -0800324 sigset_t mask, oldmask;
Al Viro3ab3f2a2015-11-13 02:36:04 -0500325 size_t total_len = 0, sent, size;
Jeff Laytonb8eed282012-09-18 16:20:35 -0700326 struct socket *ssocket = server->ssocket;
Al Viro3ab3f2a2015-11-13 02:36:04 -0500327 struct msghdr smb_msg;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000328 __be32 rfc1002_marker;
329
Long Li4357d452019-10-16 13:51:56 -0700330 if (cifs_rdma_enabled(server)) {
331 /* return -EAGAIN when connecting or reconnecting */
332 rc = -EAGAIN;
333 if (server->smbd_conn)
334 rc = smbd_send(server, num_rqst, rqst);
Long Li9762c2d2017-11-22 17:38:43 -0700335 goto smbd_done;
336 }
Pavel Shilovskyafc18a62019-03-05 15:51:56 -0800337
Jeff Laytonea702b82012-12-27 07:28:55 -0500338 if (ssocket == NULL)
Pavel Shilovskyafc18a62019-03-05 15:51:56 -0800339 return -EAGAIN;
Jeff Laytonea702b82012-12-27 07:28:55 -0500340
Ronnie Sahlberg2edf2c92021-01-21 08:22:48 +1000341 if (fatal_signal_pending(current)) {
Paulo Alcantara6988a612020-11-28 15:57:06 -0300342 cifs_dbg(FYI, "signal pending before send request\n");
343 return -ERESTARTSYS;
Pavel Shilovskyb30c74c2019-03-05 15:51:57 -0800344 }
345
Jeff Laytonb8eed282012-09-18 16:20:35 -0700346 /* cork the socket */
Christoph Hellwigdb105382020-05-28 07:12:18 +0200347 tcp_sock_set_cork(ssocket->sk, true);
Jeff Laytonb8eed282012-09-18 16:20:35 -0700348
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000349 for (j = 0; j < num_rqst; j++)
Ronnie Sahlberg81f39f92018-06-28 10:47:14 +1000350 send_length += smb_rqst_len(server, &rqst[j]);
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000351 rfc1002_marker = cpu_to_be32(send_length);
352
Pavel Shilovskyb30c74c2019-03-05 15:51:57 -0800353 /*
354 * We should not allow signals to interrupt the network send because
355 * any partial send will cause session reconnects thus increasing
356 * latency of system calls and overload a server with unnecessary
357 * requests.
358 */
359
360 sigfillset(&mask);
361 sigprocmask(SIG_BLOCK, &mask, &oldmask);
362
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000363 /* Generate a rfc1002 marker for SMB2+ */
364 if (server->vals->header_preamble_size == 0) {
365 struct kvec hiov = {
366 .iov_base = &rfc1002_marker,
367 .iov_len = 4
368 };
David Howellsaa563d72018-10-20 00:57:56 +0100369 iov_iter_kvec(&smb_msg.msg_iter, WRITE, &hiov, 1, 4);
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000370 rc = smb_send_kvec(server, &smb_msg, &sent);
371 if (rc < 0)
Pavel Shilovskyb30c74c2019-03-05 15:51:57 -0800372 goto unmask;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000373
374 total_len += sent;
375 send_length += 4;
376 }
377
Paulo Alcantara662bf5b2018-06-14 17:34:08 -0300378 cifs_dbg(FYI, "Sending smb: smb_len=%u\n", send_length);
379
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000380 for (j = 0; j < num_rqst; j++) {
381 iov = rqst[j].rq_iov;
382 n_vec = rqst[j].rq_nvec;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000383
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000384 size = 0;
Paulo Alcantara662bf5b2018-06-14 17:34:08 -0300385 for (i = 0; i < n_vec; i++) {
386 dump_smb(iov[i].iov_base, iov[i].iov_len);
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000387 size += iov[i].iov_len;
Paulo Alcantara662bf5b2018-06-14 17:34:08 -0300388 }
Al Viro3ab3f2a2015-11-13 02:36:04 -0500389
David Howellsaa563d72018-10-20 00:57:56 +0100390 iov_iter_kvec(&smb_msg.msg_iter, WRITE, iov, n_vec, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Al Viro3ab3f2a2015-11-13 02:36:04 -0500392 rc = smb_send_kvec(server, &smb_msg, &sent);
Jeff Layton97bc00b2012-09-18 16:20:35 -0700393 if (rc < 0)
Pavel Shilovskyb30c74c2019-03-05 15:51:57 -0800394 goto unmask;
Jeff Layton97bc00b2012-09-18 16:20:35 -0700395
396 total_len += sent;
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000397
398 /* now walk the page array and send each page in it */
399 for (i = 0; i < rqst[j].rq_npages; i++) {
400 struct bio_vec bvec;
401
402 bvec.bv_page = rqst[j].rq_pages[i];
403 rqst_page_get_length(&rqst[j], i, &bvec.bv_len,
404 &bvec.bv_offset);
405
David Howellsaa563d72018-10-20 00:57:56 +0100406 iov_iter_bvec(&smb_msg.msg_iter, WRITE,
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000407 &bvec, 1, bvec.bv_len);
408 rc = smb_send_kvec(server, &smb_msg, &sent);
409 if (rc < 0)
410 break;
411
412 total_len += sent;
413 }
Jeff Layton97bc00b2012-09-18 16:20:35 -0700414 }
415
Pavel Shilovskyb30c74c2019-03-05 15:51:57 -0800416unmask:
417 sigprocmask(SIG_SETMASK, &oldmask, NULL);
418
419 /*
420 * If signal is pending but we have already sent the whole packet to
421 * the server we need to return success status to allow a corresponding
422 * mid entry to be kept in the pending requests queue thus allowing
423 * to handle responses from the server by the client.
424 *
425 * If only part of the packet has been sent there is no need to hide
426 * interrupt because the session will be reconnected anyway, so there
427 * won't be any response from the server to handle.
428 */
429
430 if (signal_pending(current) && (total_len != send_length)) {
431 cifs_dbg(FYI, "signal is pending after attempt to send\n");
Ronnie Sahlberg2edf2c92021-01-21 08:22:48 +1000432 rc = -ERESTARTSYS;
Pavel Shilovskyb30c74c2019-03-05 15:51:57 -0800433 }
434
Jeff Laytonb8eed282012-09-18 16:20:35 -0700435 /* uncork it */
Christoph Hellwigdb105382020-05-28 07:12:18 +0200436 tcp_sock_set_cork(ssocket->sk, false);
Jeff Laytonb8eed282012-09-18 16:20:35 -0700437
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000438 if ((total_len > 0) && (total_len != send_length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500439 cifs_dbg(FYI, "partial send (wanted=%u sent=%zu): terminating session\n",
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000440 send_length, total_len);
Jeff Layton6f49f462012-09-18 16:20:34 -0700441 /*
442 * If we have only sent part of an SMB then the next SMB could
443 * be taken as the remainder of this one. We need to kill the
444 * socket so the server throws away the partial SMB
445 */
Steve Frenchedf1ae42008-10-29 00:47:57 +0000446 server->tcpStatus = CifsNeedReconnect;
Steve Frenchbf1fdeb2018-07-30 19:23:09 -0500447 trace_smb3_partial_send_reconnect(server->CurrentMid,
448 server->hostname);
Steve Frenchedf1ae42008-10-29 00:47:57 +0000449 }
Long Li9762c2d2017-11-22 17:38:43 -0700450smbd_done:
Jeff Laytond804d412011-01-28 15:05:43 -0500451 if (rc < 0 && rc != -EINTR)
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000452 cifs_server_dbg(VFS, "Error %d sending data on socket to server\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500453 rc);
Pavel Shilovskyee139192019-01-10 11:27:28 -0800454 else if (rc > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 return rc;
458}
459
Jeff Layton6f49f462012-09-18 16:20:34 -0700460static int
Ronnie Sahlberg1f3a8f52018-08-01 09:26:12 +1000461smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
462 struct smb_rqst *rqst, int flags)
Jeff Layton6f49f462012-09-18 16:20:34 -0700463{
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +1000464 struct kvec iov;
Long Li3946d0d2020-03-26 22:09:20 -0700465 struct smb2_transform_hdr *tr_hdr;
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +1000466 struct smb_rqst cur_rqst[MAX_COMPOUND];
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700467 int rc;
Jeff Layton6f49f462012-09-18 16:20:34 -0700468
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700469 if (!(flags & CIFS_TRANSFORM_REQ))
Ronnie Sahlberg1f3a8f52018-08-01 09:26:12 +1000470 return __smb_send_rqst(server, num_rqst, rqst);
471
472 if (num_rqst > MAX_COMPOUND - 1)
473 return -ENOMEM;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700474
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +1000475 if (!server->ops->init_transform_rq) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700476 cifs_server_dbg(VFS, "Encryption requested but transform callback is missing\n");
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700477 return -EIO;
478 }
479
Long Li3946d0d2020-03-26 22:09:20 -0700480 tr_hdr = kmalloc(sizeof(*tr_hdr), GFP_NOFS);
481 if (!tr_hdr)
482 return -ENOMEM;
483
484 memset(&cur_rqst[0], 0, sizeof(cur_rqst));
485 memset(&iov, 0, sizeof(iov));
486 memset(tr_hdr, 0, sizeof(*tr_hdr));
487
488 iov.iov_base = tr_hdr;
489 iov.iov_len = sizeof(*tr_hdr);
490 cur_rqst[0].rq_iov = &iov;
491 cur_rqst[0].rq_nvec = 1;
492
Ronnie Sahlberg1f3a8f52018-08-01 09:26:12 +1000493 rc = server->ops->init_transform_rq(server, num_rqst + 1,
494 &cur_rqst[0], rqst);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700495 if (rc)
Long Li3946d0d2020-03-26 22:09:20 -0700496 goto out;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700497
Ronnie Sahlberg1f3a8f52018-08-01 09:26:12 +1000498 rc = __smb_send_rqst(server, num_rqst + 1, &cur_rqst[0]);
499 smb3_free_compound_rqst(num_rqst, &cur_rqst[1]);
Long Li3946d0d2020-03-26 22:09:20 -0700500out:
501 kfree(tr_hdr);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700502 return rc;
Jeff Layton6f49f462012-09-18 16:20:34 -0700503}
504
Jeff Layton0496e022008-12-30 12:39:16 -0500505int
506smb_send(struct TCP_Server_Info *server, struct smb_hdr *smb_buffer,
507 unsigned int smb_buf_length)
508{
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800509 struct kvec iov[2];
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700510 struct smb_rqst rqst = { .rq_iov = iov,
511 .rq_nvec = 2 };
Jeff Layton0496e022008-12-30 12:39:16 -0500512
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800513 iov[0].iov_base = smb_buffer;
514 iov[0].iov_len = 4;
515 iov[1].iov_base = (char *)smb_buffer + 4;
516 iov[1].iov_len = smb_buf_length;
Jeff Layton0496e022008-12-30 12:39:16 -0500517
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000518 return __smb_send_rqst(server, 1, &rqst);
Jeff Layton0496e022008-12-30 12:39:16 -0500519}
520
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300521static int
Ronnie Sahlbergb227d212019-03-08 12:58:20 +1000522wait_for_free_credits(struct TCP_Server_Info *server, const int num_credits,
Ronnie Sahlberg2b53b922019-03-08 12:58:22 +1000523 const int timeout, const int flags,
524 unsigned int *instance)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000525{
Ronnie Sahlberg19e88862020-07-02 10:55:41 +1000526 long rc;
Ronnie Sahlberg4230cff2019-03-08 12:58:19 +1000527 int *credits;
528 int optype;
Ronnie Sahlberg2b53b922019-03-08 12:58:22 +1000529 long int t;
530
531 if (timeout < 0)
532 t = MAX_JIFFY_OFFSET;
533 else
534 t = msecs_to_jiffies(timeout);
Ronnie Sahlberg4230cff2019-03-08 12:58:19 +1000535
536 optype = flags & CIFS_OP_MASK;
Pavel Shilovsky5bc59492012-02-21 19:56:08 +0300537
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -0800538 *instance = 0;
539
Ronnie Sahlberg4230cff2019-03-08 12:58:19 +1000540 credits = server->ops->get_credits_field(server, optype);
541 /* Since an echo is already inflight, no need to wait to send another */
542 if (*credits <= 0 && optype == CIFS_ECHO_OP)
543 return -EAGAIN;
544
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300545 spin_lock(&server->req_lock);
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +1000546 if ((flags & CIFS_TIMEOUT_MASK) == CIFS_NON_BLOCKING) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000547 /* oplock breaks must not be held up */
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300548 server->in_flight++;
Steve French1b63f182019-09-09 22:57:11 -0500549 if (server->in_flight > server->max_in_flight)
550 server->max_in_flight = server->in_flight;
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300551 *credits -= 1;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -0800552 *instance = server->reconnect_instance;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300553 spin_unlock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000554 return 0;
555 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000556
Volker Lendecke27a97a62008-12-08 20:59:39 +0000557 while (1) {
Ronnie Sahlbergb227d212019-03-08 12:58:20 +1000558 if (*credits < num_credits) {
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300559 spin_unlock(&server->req_lock);
Steve French789e6662011-08-09 18:44:44 +0000560 cifs_num_waiters_inc(server);
Ronnie Sahlberg2b53b922019-03-08 12:58:22 +1000561 rc = wait_event_killable_timeout(server->request_q,
562 has_credits(server, credits, num_credits), t);
Steve French789e6662011-08-09 18:44:44 +0000563 cifs_num_waiters_dec(server);
Ronnie Sahlberg2b53b922019-03-08 12:58:22 +1000564 if (!rc) {
Steve French7937ca92019-03-09 20:29:55 -0600565 trace_smb3_credit_timeout(server->CurrentMid,
Steve French9eec21b2020-10-19 18:18:15 -0500566 server->hostname, num_credits, 0);
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000567 cifs_server_dbg(VFS, "wait timed out after %d ms\n",
Ronnie Sahlberg2b53b922019-03-08 12:58:22 +1000568 timeout);
569 return -ENOTSUPP;
570 }
571 if (rc == -ERESTARTSYS)
572 return -ERESTARTSYS;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300573 spin_lock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000574 } else {
Jeff Laytonc5797a92011-01-11 07:24:01 -0500575 if (server->tcpStatus == CifsExiting) {
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300576 spin_unlock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000577 return -ENOENT;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000578 }
Volker Lendecke27a97a62008-12-08 20:59:39 +0000579
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400580 /*
Ronnie Sahlberg16b34aa2019-03-08 12:58:21 +1000581 * For normal commands, reserve the last MAX_COMPOUND
582 * credits to compound requests.
583 * Otherwise these compounds could be permanently
584 * starved for credits by single-credit requests.
585 *
586 * To prevent spinning CPU, block this thread until
587 * there are >MAX_COMPOUND credits available.
588 * But only do this is we already have a lot of
589 * credits in flight to avoid triggering this check
590 * for servers that are slow to hand out credits on
591 * new sessions.
592 */
593 if (!optype && num_credits == 1 &&
594 server->in_flight > 2 * MAX_COMPOUND &&
595 *credits <= MAX_COMPOUND) {
596 spin_unlock(&server->req_lock);
597 cifs_num_waiters_inc(server);
Ronnie Sahlberg2b53b922019-03-08 12:58:22 +1000598 rc = wait_event_killable_timeout(
599 server->request_q,
Ronnie Sahlberg16b34aa2019-03-08 12:58:21 +1000600 has_credits(server, credits,
Ronnie Sahlberg2b53b922019-03-08 12:58:22 +1000601 MAX_COMPOUND + 1),
602 t);
Ronnie Sahlberg16b34aa2019-03-08 12:58:21 +1000603 cifs_num_waiters_dec(server);
Ronnie Sahlberg2b53b922019-03-08 12:58:22 +1000604 if (!rc) {
Steve French7937ca92019-03-09 20:29:55 -0600605 trace_smb3_credit_timeout(
606 server->CurrentMid,
Steve French9eec21b2020-10-19 18:18:15 -0500607 server->hostname, num_credits,
608 0);
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000609 cifs_server_dbg(VFS, "wait timed out after %d ms\n",
Ronnie Sahlberg2b53b922019-03-08 12:58:22 +1000610 timeout);
611 return -ENOTSUPP;
612 }
613 if (rc == -ERESTARTSYS)
614 return -ERESTARTSYS;
Ronnie Sahlberg16b34aa2019-03-08 12:58:21 +1000615 spin_lock(&server->req_lock);
616 continue;
617 }
618
619 /*
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400620 * Can not count locking commands against total
621 * as they are allowed to block on server.
622 */
Volker Lendecke27a97a62008-12-08 20:59:39 +0000623
624 /* update # of requests on the wire to server */
Ronnie Sahlberg4230cff2019-03-08 12:58:19 +1000625 if ((flags & CIFS_TIMEOUT_MASK) != CIFS_BLOCKING_OP) {
Ronnie Sahlbergb227d212019-03-08 12:58:20 +1000626 *credits -= num_credits;
627 server->in_flight += num_credits;
Steve French1b63f182019-09-09 22:57:11 -0500628 if (server->in_flight > server->max_in_flight)
629 server->max_in_flight = server->in_flight;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -0800630 *instance = server->reconnect_instance;
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400631 }
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300632 spin_unlock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000633 break;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000634 }
635 }
636 return 0;
637}
638
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300639static int
Ronnie Sahlberg480b1cb2019-03-08 12:58:18 +1000640wait_for_free_request(struct TCP_Server_Info *server, const int flags,
641 unsigned int *instance)
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300642{
Ronnie Sahlberg2b53b922019-03-08 12:58:22 +1000643 return wait_for_free_credits(server, 1, -1, flags,
644 instance);
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300645}
646
Ronnie Sahlberg257b7802019-03-11 12:18:58 +1000647static int
648wait_for_compound_request(struct TCP_Server_Info *server, int num,
649 const int flags, unsigned int *instance)
650{
651 int *credits;
652
653 credits = server->ops->get_credits_field(server, flags & CIFS_OP_MASK);
654
655 spin_lock(&server->req_lock);
656 if (*credits < num) {
657 /*
Pavel Shilovsky25026102021-02-02 22:34:32 -0600658 * If the server is tight on resources or just gives us less
659 * credits for other reasons (e.g. requests are coming out of
660 * order and the server delays granting more credits until it
661 * processes a missing mid) and we exhausted most available
662 * credits there may be situations when we try to send
663 * a compound request but we don't have enough credits. At this
664 * point the client needs to decide if it should wait for
665 * additional credits or fail the request. If at least one
666 * request is in flight there is a high probability that the
667 * server will return enough credits to satisfy this compound
668 * request.
669 *
670 * Return immediately if no requests in flight since we will be
671 * stuck on waiting for credits.
Ronnie Sahlberg257b7802019-03-11 12:18:58 +1000672 */
Pavel Shilovsky25026102021-02-02 22:34:32 -0600673 if (server->in_flight == 0) {
Ronnie Sahlberg257b7802019-03-11 12:18:58 +1000674 spin_unlock(&server->req_lock);
675 return -ENOTSUPP;
676 }
677 }
678 spin_unlock(&server->req_lock);
679
680 return wait_for_free_credits(server, num, 60000, flags,
681 instance);
682}
683
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400684int
685cifs_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800686 unsigned int *num, struct cifs_credits *credits)
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400687{
688 *num = size;
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800689 credits->value = 0;
690 credits->instance = server->reconnect_instance;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400691 return 0;
692}
693
Steve French96daf2b2011-05-27 04:34:02 +0000694static int allocate_mid(struct cifs_ses *ses, struct smb_hdr *in_buf,
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000695 struct mid_q_entry **ppmidQ)
696{
697 if (ses->server->tcpStatus == CifsExiting) {
698 return -ENOENT;
Volker Lendecke8fbbd362008-12-06 13:12:34 +0100699 }
700
701 if (ses->server->tcpStatus == CifsNeedReconnect) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500702 cifs_dbg(FYI, "tcp session dead - return to caller to retry\n");
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000703 return -EAGAIN;
Volker Lendecke8fbbd362008-12-06 13:12:34 +0100704 }
705
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500706 if (ses->status == CifsNew) {
Steve French79a58d12007-07-06 22:44:50 +0000707 if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
Steve Frenchad7a2922008-02-07 23:25:02 +0000708 (in_buf->Command != SMB_COM_NEGOTIATE))
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000709 return -EAGAIN;
Steve Frenchad7a2922008-02-07 23:25:02 +0000710 /* else ok - we are setting up session */
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000711 }
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500712
713 if (ses->status == CifsExiting) {
714 /* check if SMB session is bad because we are setting it up */
715 if (in_buf->Command != SMB_COM_LOGOFF_ANDX)
716 return -EAGAIN;
717 /* else ok - we are shutting down session */
718 }
719
Jeff Layton24b9b062008-12-01 07:09:34 -0500720 *ppmidQ = AllocMidQEntry(in_buf, ses->server);
Steve French26f57362007-08-30 22:09:15 +0000721 if (*ppmidQ == NULL)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000722 return -ENOMEM;
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500723 spin_lock(&GlobalMid_Lock);
724 list_add_tail(&(*ppmidQ)->qhead, &ses->server->pending_mid_q);
725 spin_unlock(&GlobalMid_Lock);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000726 return 0;
727}
728
Jeff Layton0ade6402011-01-11 07:24:02 -0500729static int
730wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *midQ)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000731{
Jeff Layton0ade6402011-01-11 07:24:02 -0500732 int error;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000733
Colin Cross5853cc22013-05-07 17:52:05 +0000734 error = wait_event_freezekillable_unsafe(server->response_q,
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400735 midQ->mid_state != MID_REQUEST_SUBMITTED);
Jeff Layton0ade6402011-01-11 07:24:02 -0500736 if (error < 0)
737 return -ERESTARTSYS;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000738
Jeff Layton0ade6402011-01-11 07:24:02 -0500739 return 0;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000740}
741
Jeff Laytonfec344e2012-09-18 16:20:35 -0700742struct mid_q_entry *
743cifs_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400744{
745 int rc;
Jeff Laytonfec344e2012-09-18 16:20:35 -0700746 struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400747 struct mid_q_entry *mid;
748
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800749 if (rqst->rq_iov[0].iov_len != 4 ||
750 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
751 return ERR_PTR(-EIO);
752
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400753 /* enable signing if server requires it */
Jeff Layton38d77c52013-05-26 07:01:00 -0400754 if (server->sign)
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400755 hdr->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
756
757 mid = AllocMidQEntry(hdr, server);
758 if (mid == NULL)
Jeff Laytonfec344e2012-09-18 16:20:35 -0700759 return ERR_PTR(-ENOMEM);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400760
Jeff Laytonfec344e2012-09-18 16:20:35 -0700761 rc = cifs_sign_rqst(rqst, server, &mid->sequence_number);
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100762 if (rc) {
763 DeleteMidQEntry(mid);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700764 return ERR_PTR(rc);
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100765 }
766
Jeff Laytonfec344e2012-09-18 16:20:35 -0700767 return mid;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400768}
Steve French133672e2007-11-13 22:41:37 +0000769
770/*
Jeff Laytona6827c12011-01-11 07:24:21 -0500771 * Send a SMB request and set the callback function in the mid to handle
772 * the result. Caller is responsible for dealing with timeouts.
773 */
774int
Jeff Laytonfec344e2012-09-18 16:20:35 -0700775cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst,
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -0800776 mid_receive_t *receive, mid_callback_t *callback,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -0800777 mid_handle_t *handle, void *cbdata, const int flags,
778 const struct cifs_credits *exist_credits)
Jeff Laytona6827c12011-01-11 07:24:21 -0500779{
Ronnie Sahlberg480b1cb2019-03-08 12:58:18 +1000780 int rc;
Jeff Laytona6827c12011-01-11 07:24:21 -0500781 struct mid_q_entry *mid;
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800782 struct cifs_credits credits = { .value = 0, .instance = 0 };
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -0800783 unsigned int instance;
Ronnie Sahlberg480b1cb2019-03-08 12:58:18 +1000784 int optype;
Jeff Laytona6827c12011-01-11 07:24:21 -0500785
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400786 optype = flags & CIFS_OP_MASK;
787
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400788 if ((flags & CIFS_HAS_CREDITS) == 0) {
Ronnie Sahlberg480b1cb2019-03-08 12:58:18 +1000789 rc = wait_for_free_request(server, flags, &instance);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400790 if (rc)
791 return rc;
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800792 credits.value = 1;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -0800793 credits.instance = instance;
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -0800794 } else
795 instance = exist_credits->instance;
Jeff Laytona6827c12011-01-11 07:24:21 -0500796
797 mutex_lock(&server->srv_mutex);
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -0800798
799 /*
800 * We can't use credits obtained from the previous session to send this
801 * request. Check if there were reconnects after we obtained credits and
802 * return -EAGAIN in such cases to let callers handle it.
803 */
804 if (instance != server->reconnect_instance) {
805 mutex_unlock(&server->srv_mutex);
806 add_credits_and_wake_if(server, &credits, optype);
807 return -EAGAIN;
808 }
809
Jeff Laytonfec344e2012-09-18 16:20:35 -0700810 mid = server->ops->setup_async_request(server, rqst);
811 if (IS_ERR(mid)) {
Jeff Laytona6827c12011-01-11 07:24:21 -0500812 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800813 add_credits_and_wake_if(server, &credits, optype);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700814 return PTR_ERR(mid);
Jeff Laytona6827c12011-01-11 07:24:21 -0500815 }
816
Jeff Layton44d22d82011-10-19 15:29:49 -0400817 mid->receive = receive;
Jeff Laytona6827c12011-01-11 07:24:21 -0500818 mid->callback = callback;
819 mid->callback_data = cbdata;
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -0800820 mid->handle = handle;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400821 mid->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +0000822
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100823 /* put it on the pending_mid_q */
824 spin_lock(&GlobalMid_Lock);
825 list_add_tail(&mid->qhead, &server->pending_mid_q);
826 spin_unlock(&GlobalMid_Lock);
827
Long Li93d2cb62017-06-28 15:55:55 -0700828 /*
829 * Need to store the time in mid before calling I/O. For call_async,
830 * I/O response may come back and free the mid entry on another thread.
831 */
832 cifs_save_when_sent(mid);
Steve French789e6662011-08-09 18:44:44 +0000833 cifs_in_send_inc(server);
Ronnie Sahlberg1f3a8f52018-08-01 09:26:12 +1000834 rc = smb_send_rqst(server, 1, rqst, flags);
Steve French789e6662011-08-09 18:44:44 +0000835 cifs_in_send_dec(server);
Jeff Laytonad313cb2013-04-03 10:27:36 -0400836
Rabin Vincent820962d2015-12-23 07:32:41 +0100837 if (rc < 0) {
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800838 revert_current_mid(server, mid->credits);
Jeff Laytonad313cb2013-04-03 10:27:36 -0400839 server->sequence_number -= 2;
Rabin Vincent820962d2015-12-23 07:32:41 +0100840 cifs_delete_mid(mid);
841 }
842
Jeff Laytona6827c12011-01-11 07:24:21 -0500843 mutex_unlock(&server->srv_mutex);
Steve French789e6662011-08-09 18:44:44 +0000844
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100845 if (rc == 0)
846 return 0;
Jeff Laytona6827c12011-01-11 07:24:21 -0500847
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800848 add_credits_and_wake_if(server, &credits, optype);
Jeff Laytona6827c12011-01-11 07:24:21 -0500849 return rc;
850}
851
852/*
Steve French133672e2007-11-13 22:41:37 +0000853 *
854 * Send an SMB Request. No response info (other than return code)
855 * needs to be parsed.
856 *
857 * flags indicate the type of request buffer and how long to wait
858 * and whether to log NT STATUS code (error) before mapping it to POSIX error
859 *
860 */
861int
Steve French96daf2b2011-05-27 04:34:02 +0000862SendReceiveNoRsp(const unsigned int xid, struct cifs_ses *ses,
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400863 char *in_buf, int flags)
Steve French133672e2007-11-13 22:41:37 +0000864{
865 int rc;
866 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700867 struct kvec rsp_iov;
Steve French133672e2007-11-13 22:41:37 +0000868 int resp_buf_type;
869
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400870 iov[0].iov_base = in_buf;
871 iov[0].iov_len = get_rfc1002_length(in_buf) + 4;
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +1000872 flags |= CIFS_NO_RSP_BUF;
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700873 rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Joe Perchesf96637b2013-05-04 22:12:25 -0500874 cifs_dbg(NOISY, "SendRcvNoRsp flags %d rc %d\n", flags, rc);
Steve French90c81e02008-02-12 20:32:36 +0000875
Steve French133672e2007-11-13 22:41:37 +0000876 return rc;
877}
878
Jeff Layton053d5032011-01-11 07:24:02 -0500879static int
Jeff Layton3c1105d2011-05-22 07:09:13 -0400880cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
Jeff Layton053d5032011-01-11 07:24:02 -0500881{
882 int rc = 0;
883
Joe Perchesf96637b2013-05-04 22:12:25 -0500884 cifs_dbg(FYI, "%s: cmd=%d mid=%llu state=%d\n",
885 __func__, le16_to_cpu(mid->command), mid->mid, mid->mid_state);
Jeff Layton053d5032011-01-11 07:24:02 -0500886
Jeff Layton74dd92a2011-01-11 07:24:02 -0500887 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400888 switch (mid->mid_state) {
Jeff Layton74dd92a2011-01-11 07:24:02 -0500889 case MID_RESPONSE_RECEIVED:
Jeff Layton053d5032011-01-11 07:24:02 -0500890 spin_unlock(&GlobalMid_Lock);
891 return rc;
Jeff Layton74dd92a2011-01-11 07:24:02 -0500892 case MID_RETRY_NEEDED:
893 rc = -EAGAIN;
894 break;
Jeff Layton71823ba2011-02-10 08:03:50 -0500895 case MID_RESPONSE_MALFORMED:
896 rc = -EIO;
897 break;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400898 case MID_SHUTDOWN:
899 rc = -EHOSTDOWN;
900 break;
Jeff Layton74dd92a2011-01-11 07:24:02 -0500901 default:
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700902 if (!(mid->mid_flags & MID_DELETED)) {
903 list_del_init(&mid->qhead);
904 mid->mid_flags |= MID_DELETED;
905 }
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000906 cifs_server_dbg(VFS, "%s: invalid mid state mid=%llu state=%d\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500907 __func__, mid->mid, mid->mid_state);
Jeff Layton74dd92a2011-01-11 07:24:02 -0500908 rc = -EIO;
Jeff Layton053d5032011-01-11 07:24:02 -0500909 }
910 spin_unlock(&GlobalMid_Lock);
911
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500912 DeleteMidQEntry(mid);
Jeff Layton053d5032011-01-11 07:24:02 -0500913 return rc;
914}
915
Jeff Layton121b0462012-05-15 12:21:10 -0400916static inline int
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800917send_cancel(struct TCP_Server_Info *server, struct smb_rqst *rqst,
918 struct mid_q_entry *mid)
Jeff Layton76dcc262011-01-11 07:24:24 -0500919{
Jeff Layton121b0462012-05-15 12:21:10 -0400920 return server->ops->send_cancel ?
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800921 server->ops->send_cancel(server, rqst, mid) : 0;
Jeff Layton76dcc262011-01-11 07:24:24 -0500922}
923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924int
Jeff Layton2c8f9812011-05-19 16:22:52 -0400925cifs_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
926 bool log_error)
927{
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400928 unsigned int len = get_rfc1002_length(mid->resp_buf) + 4;
Jeff Layton826a95e2011-10-11 06:41:32 -0400929
930 dump_smb(mid->resp_buf, min_t(u32, 92, len));
Jeff Layton2c8f9812011-05-19 16:22:52 -0400931
932 /* convert the length into a more usable form */
Jeff Layton38d77c52013-05-26 07:01:00 -0400933 if (server->sign) {
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800934 struct kvec iov[2];
Steve French985e4ff02012-08-03 09:42:45 -0500935 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800936 struct smb_rqst rqst = { .rq_iov = iov,
937 .rq_nvec = 2 };
Jeff Layton826a95e2011-10-11 06:41:32 -0400938
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800939 iov[0].iov_base = mid->resp_buf;
940 iov[0].iov_len = 4;
941 iov[1].iov_base = (char *)mid->resp_buf + 4;
942 iov[1].iov_len = len - 4;
Jeff Layton2c8f9812011-05-19 16:22:52 -0400943 /* FIXME: add code to kill session */
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700944 rc = cifs_verify_signature(&rqst, server,
Jeff Layton0124cc42013-04-03 11:55:03 -0400945 mid->sequence_number);
Steve French985e4ff02012-08-03 09:42:45 -0500946 if (rc)
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000947 cifs_server_dbg(VFS, "SMB signature verification returned error = %d\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500948 rc);
Jeff Layton2c8f9812011-05-19 16:22:52 -0400949 }
950
951 /* BB special case reconnect tid and uid here? */
Roberto Bergantinos Corpasa3713ec2020-07-03 11:29:32 +0200952 return map_and_check_smb_error(mid, log_error);
Jeff Layton2c8f9812011-05-19 16:22:52 -0400953}
954
Jeff Laytonfec344e2012-09-18 16:20:35 -0700955struct mid_q_entry *
Aurelien Aptelf780bd32019-09-20 06:08:34 +0200956cifs_setup_request(struct cifs_ses *ses, struct TCP_Server_Info *ignored,
957 struct smb_rqst *rqst)
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400958{
959 int rc;
Jeff Laytonfec344e2012-09-18 16:20:35 -0700960 struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400961 struct mid_q_entry *mid;
962
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800963 if (rqst->rq_iov[0].iov_len != 4 ||
964 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
965 return ERR_PTR(-EIO);
966
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400967 rc = allocate_mid(ses, hdr, &mid);
968 if (rc)
Jeff Laytonfec344e2012-09-18 16:20:35 -0700969 return ERR_PTR(rc);
970 rc = cifs_sign_rqst(rqst, ses->server, &mid->sequence_number);
971 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700972 cifs_delete_mid(mid);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700973 return ERR_PTR(rc);
974 }
975 return mid;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400976}
977
Ronnie Sahlberg4e34feb2018-08-30 10:13:00 +1000978static void
Pavel Shilovskyee258d72019-01-03 15:53:10 -0800979cifs_compound_callback(struct mid_q_entry *mid)
Pavel Shilovsky8a26f0f2019-01-03 16:45:27 -0800980{
981 struct TCP_Server_Info *server = mid->server;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -0800982 struct cifs_credits credits;
Pavel Shilovsky8a26f0f2019-01-03 16:45:27 -0800983
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -0800984 credits.value = server->ops->get_credits(mid);
985 credits.instance = server->reconnect_instance;
986
987 add_credits(server, &credits, mid->optype);
Pavel Shilovsky8a26f0f2019-01-03 16:45:27 -0800988}
989
Pavel Shilovskyee258d72019-01-03 15:53:10 -0800990static void
991cifs_compound_last_callback(struct mid_q_entry *mid)
992{
993 cifs_compound_callback(mid);
994 cifs_wake_up_task(mid);
995}
996
997static void
998cifs_cancelled_callback(struct mid_q_entry *mid)
999{
1000 cifs_compound_callback(mid);
1001 DeleteMidQEntry(mid);
1002}
1003
Aurelien Aptel5f68ea42020-04-22 15:58:57 +02001004/*
1005 * Return a channel (master if none) of @ses that can be used to send
1006 * regular requests.
1007 *
1008 * If we are currently binding a new channel (negprot/sess.setup),
1009 * return the new incomplete channel.
1010 */
1011struct TCP_Server_Info *cifs_pick_channel(struct cifs_ses *ses)
1012{
1013 uint index = 0;
1014
1015 if (!ses)
1016 return NULL;
1017
1018 if (!ses->binding) {
1019 /* round robin */
1020 if (ses->chan_count > 1) {
1021 index = (uint)atomic_inc_return(&ses->chan_seq);
1022 index %= ses->chan_count;
1023 }
1024 return ses->chans[index].server;
1025 } else {
1026 return cifs_ses_server(ses);
1027 }
1028}
1029
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08001030int
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001031compound_send_recv(const unsigned int xid, struct cifs_ses *ses,
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001032 struct TCP_Server_Info *server,
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001033 const int flags, const int num_rqst, struct smb_rqst *rqst,
1034 int *resp_buf_type, struct kvec *resp_iov)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
Ronnie Sahlberg480b1cb2019-03-08 12:58:18 +10001036 int i, j, optype, rc = 0;
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001037 struct mid_q_entry *midQ[MAX_COMPOUND];
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -08001038 bool cancelled_mid[MAX_COMPOUND] = {false};
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08001039 struct cifs_credits credits[MAX_COMPOUND] = {
1040 { .value = 0, .instance = 0 }
1041 };
1042 unsigned int instance;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001043 char *buf;
Steve French50c2f752007-07-13 00:33:32 +00001044
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001045 optype = flags & CIFS_OP_MASK;
Steve French133672e2007-11-13 22:41:37 +00001046
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001047 for (i = 0; i < num_rqst; i++)
1048 resp_buf_type[i] = CIFS_NO_BUFFER; /* no response buf yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001050 if (!ses || !ses->server || !server) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001051 cifs_dbg(VFS, "Null session\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 return -EIO;
1053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Aurelien Aptel3190b592019-06-24 13:00:12 -05001055 if (server->tcpStatus == CifsExiting)
Steve French31ca3bc2005-04-28 22:41:11 -07001056 return -ENOENT;
1057
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04001058 /*
Ronnie Sahlberg257b7802019-03-11 12:18:58 +10001059 * Wait for all the requests to become available.
Pavel Shilovsky7091bca2019-01-30 16:58:09 -08001060 * This approach still leaves the possibility to be stuck waiting for
1061 * credits if the server doesn't grant credits to the outstanding
Ronnie Sahlberg257b7802019-03-11 12:18:58 +10001062 * requests and if the client is completely idle, not generating any
1063 * other requests.
1064 * This can be handled by the eventual session reconnect.
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04001065 */
Aurelien Aptel3190b592019-06-24 13:00:12 -05001066 rc = wait_for_compound_request(server, num_rqst, flags,
Ronnie Sahlberg257b7802019-03-11 12:18:58 +10001067 &instance);
1068 if (rc)
1069 return rc;
1070
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -08001071 for (i = 0; i < num_rqst; i++) {
Ronnie Sahlberg257b7802019-03-11 12:18:58 +10001072 credits[i].value = 1;
1073 credits[i].instance = instance;
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -08001074 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001075
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04001076 /*
1077 * Make sure that we sign in the same order that we send on this socket
1078 * and avoid races inside tcp sendmsg code that could cause corruption
1079 * of smb data.
1080 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Aurelien Aptel3190b592019-06-24 13:00:12 -05001082 mutex_lock(&server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Pavel Shilovsky97ea4992019-01-15 16:07:52 -08001084 /*
1085 * All the parts of the compound chain belong obtained credits from the
Ronnie Sahlberg257b7802019-03-11 12:18:58 +10001086 * same session. We can not use credits obtained from the previous
Pavel Shilovsky97ea4992019-01-15 16:07:52 -08001087 * session to send this request. Check if there were reconnects after
1088 * we obtained credits and return -EAGAIN in such cases to let callers
1089 * handle it.
1090 */
Aurelien Aptel3190b592019-06-24 13:00:12 -05001091 if (instance != server->reconnect_instance) {
1092 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky97ea4992019-01-15 16:07:52 -08001093 for (j = 0; j < num_rqst; j++)
Aurelien Aptel3190b592019-06-24 13:00:12 -05001094 add_credits(server, &credits[j], optype);
Pavel Shilovsky97ea4992019-01-15 16:07:52 -08001095 return -EAGAIN;
1096 }
1097
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001098 for (i = 0; i < num_rqst; i++) {
Aurelien Aptelf780bd32019-09-20 06:08:34 +02001099 midQ[i] = server->ops->setup_request(ses, server, &rqst[i]);
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001100 if (IS_ERR(midQ[i])) {
Aurelien Aptel3190b592019-06-24 13:00:12 -05001101 revert_current_mid(server, i);
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001102 for (j = 0; j < i; j++)
1103 cifs_delete_mid(midQ[j]);
Aurelien Aptel3190b592019-06-24 13:00:12 -05001104 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -08001105
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001106 /* Update # of requests on wire to server */
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -08001107 for (j = 0; j < num_rqst; j++)
Aurelien Aptel3190b592019-06-24 13:00:12 -05001108 add_credits(server, &credits[j], optype);
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001109 return PTR_ERR(midQ[i]);
1110 }
1111
1112 midQ[i]->mid_state = MID_REQUEST_SUBMITTED;
Pavel Shilovsky8a26f0f2019-01-03 16:45:27 -08001113 midQ[i]->optype = optype;
Ronnie Sahlberg4e34feb2018-08-30 10:13:00 +10001114 /*
Pavel Shilovskyee258d72019-01-03 15:53:10 -08001115 * Invoke callback for every part of the compound chain
1116 * to calculate credits properly. Wake up this thread only when
1117 * the last element is received.
Ronnie Sahlberg4e34feb2018-08-30 10:13:00 +10001118 */
1119 if (i < num_rqst - 1)
Pavel Shilovskyee258d72019-01-03 15:53:10 -08001120 midQ[i]->callback = cifs_compound_callback;
1121 else
1122 midQ[i]->callback = cifs_compound_last_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
Aurelien Aptel3190b592019-06-24 13:00:12 -05001124 cifs_in_send_inc(server);
1125 rc = smb_send_rqst(server, num_rqst, rqst, flags);
1126 cifs_in_send_dec(server);
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001127
1128 for (i = 0; i < num_rqst; i++)
1129 cifs_save_when_sent(midQ[i]);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001130
Pavel Shilovskyc781af72019-03-04 14:02:50 -08001131 if (rc < 0) {
Aurelien Aptel3190b592019-06-24 13:00:12 -05001132 revert_current_mid(server, num_rqst);
1133 server->sequence_number -= 2;
Pavel Shilovskyc781af72019-03-04 14:02:50 -08001134 }
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001135
Aurelien Aptel3190b592019-06-24 13:00:12 -05001136 mutex_unlock(&server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001137
Ronnie Sahlbergd69cb722019-05-01 12:03:41 +10001138 /*
1139 * If sending failed for some reason or it is an oplock break that we
1140 * will not receive a response to - return credits back
1141 */
1142 if (rc < 0 || (flags & CIFS_NO_SRV_RSP)) {
Pavel Shilovskyee258d72019-01-03 15:53:10 -08001143 for (i = 0; i < num_rqst; i++)
Aurelien Aptel3190b592019-06-24 13:00:12 -05001144 add_credits(server, &credits[i], optype);
Ronnie Sahlbergcb5c2e62018-10-10 15:29:06 +10001145 goto out;
Pavel Shilovskyee258d72019-01-03 15:53:10 -08001146 }
1147
1148 /*
1149 * At this point the request is passed to the network stack - we assume
1150 * that any credits taken from the server structure on the client have
1151 * been spent and we can't return them back. Once we receive responses
1152 * we will collect credits granted by the server in the mid callbacks
1153 * and add those credits to the server structure.
1154 */
Ronnie Sahlbergcb5c2e62018-10-10 15:29:06 +10001155
1156 /*
1157 * Compounding is never used during session establish.
1158 */
1159 if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP))
1160 smb311_update_preauth_hash(ses, rqst[0].rq_iov,
1161 rqst[0].rq_nvec);
1162
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001163 for (i = 0; i < num_rqst; i++) {
Aurelien Aptel3190b592019-06-24 13:00:12 -05001164 rc = wait_for_response(server, midQ[i]);
Pavel Shilovsky8a26f0f2019-01-03 16:45:27 -08001165 if (rc != 0)
1166 break;
1167 }
1168 if (rc != 0) {
1169 for (; i < num_rqst; i++) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001170 cifs_server_dbg(VFS, "Cancelling wait for mid %llu cmd: %d\n",
Steve French43de1db2018-10-23 21:04:57 -05001171 midQ[i]->mid, le16_to_cpu(midQ[i]->command));
Aurelien Aptel3190b592019-06-24 13:00:12 -05001172 send_cancel(server, &rqst[i], midQ[i]);
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001173 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7b718432019-11-21 11:35:14 -08001174 midQ[i]->mid_flags |= MID_WAIT_CANCELLED;
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001175 if (midQ[i]->mid_state == MID_REQUEST_SUBMITTED) {
Pavel Shilovsky8a26f0f2019-01-03 16:45:27 -08001176 midQ[i]->callback = cifs_cancelled_callback;
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -08001177 cancelled_mid[i] = true;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08001178 credits[i].value = 0;
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001179 }
Jeff Layton1be912d2011-01-28 07:08:28 -05001180 spin_unlock(&GlobalMid_Lock);
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001181 }
Ronnie Sahlbergcb5c2e62018-10-10 15:29:06 +10001182 }
1183
Ronnie Sahlbergcb5c2e62018-10-10 15:29:06 +10001184 for (i = 0; i < num_rqst; i++) {
1185 if (rc < 0)
1186 goto out;
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001187
Aurelien Aptel3190b592019-06-24 13:00:12 -05001188 rc = cifs_sync_mid_result(midQ[i], server);
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001189 if (rc != 0) {
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -08001190 /* mark this mid as cancelled to not free it below */
1191 cancelled_mid[i] = true;
1192 goto out;
Jeff Layton1be912d2011-01-28 07:08:28 -05001193 }
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001194
1195 if (!midQ[i]->resp_buf ||
1196 midQ[i]->mid_state != MID_RESPONSE_RECEIVED) {
1197 rc = -EIO;
1198 cifs_dbg(FYI, "Bad MID state?\n");
1199 goto out;
1200 }
1201
1202 buf = (char *)midQ[i]->resp_buf;
1203 resp_iov[i].iov_base = buf;
1204 resp_iov[i].iov_len = midQ[i]->resp_buf_size +
Aurelien Aptel3190b592019-06-24 13:00:12 -05001205 server->vals->header_preamble_size;
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001206
1207 if (midQ[i]->large_buf)
1208 resp_buf_type[i] = CIFS_LARGE_BUFFER;
1209 else
1210 resp_buf_type[i] = CIFS_SMALL_BUFFER;
1211
Aurelien Aptel3190b592019-06-24 13:00:12 -05001212 rc = server->ops->check_receive(midQ[i], server,
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001213 flags & CIFS_LOG_ERROR);
1214
1215 /* mark it so buf will not be freed by cifs_delete_mid */
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10001216 if ((flags & CIFS_NO_RSP_BUF) == 0)
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001217 midQ[i]->resp_buf = NULL;
Ronnie Sahlbergcb5c2e62018-10-10 15:29:06 +10001218
Jeff Layton1be912d2011-01-28 07:08:28 -05001219 }
Ronnie Sahlbergcb5c2e62018-10-10 15:29:06 +10001220
1221 /*
1222 * Compounding is never used during session establish.
1223 */
1224 if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP)) {
1225 struct kvec iov = {
1226 .iov_base = resp_iov[0].iov_base,
1227 .iov_len = resp_iov[0].iov_len
1228 };
1229 smb311_update_preauth_hash(ses, &iov, 1);
1230 }
1231
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001232out:
Ronnie Sahlberg4e34feb2018-08-30 10:13:00 +10001233 /*
1234 * This will dequeue all mids. After this it is important that the
1235 * demultiplex_thread will not process any of these mids any futher.
1236 * This is prevented above by using a noop callback that will not
1237 * wake this thread except for the very last PDU.
1238 */
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -08001239 for (i = 0; i < num_rqst; i++) {
1240 if (!cancelled_mid[i])
1241 cifs_delete_mid(midQ[i]);
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -08001242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 return rc;
1245}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247int
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001248cifs_send_recv(const unsigned int xid, struct cifs_ses *ses,
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001249 struct TCP_Server_Info *server,
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001250 struct smb_rqst *rqst, int *resp_buf_type, const int flags,
1251 struct kvec *resp_iov)
1252{
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001253 return compound_send_recv(xid, ses, server, flags, 1,
1254 rqst, resp_buf_type, resp_iov);
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001255}
1256
1257int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001258SendReceive2(const unsigned int xid, struct cifs_ses *ses,
1259 struct kvec *iov, int n_vec, int *resp_buf_type /* ret */,
1260 const int flags, struct kvec *resp_iov)
1261{
1262 struct smb_rqst rqst;
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +11001263 struct kvec s_iov[CIFS_MAX_IOV_SIZE], *new_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001264 int rc;
1265
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +11001266 if (n_vec + 1 > CIFS_MAX_IOV_SIZE) {
Kees Cook6da2ec52018-06-12 13:55:00 -07001267 new_iov = kmalloc_array(n_vec + 1, sizeof(struct kvec),
1268 GFP_KERNEL);
Steve French117e3b72018-04-22 10:24:19 -05001269 if (!new_iov) {
1270 /* otherwise cifs_send_recv below sets resp_buf_type */
1271 *resp_buf_type = CIFS_NO_BUFFER;
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +11001272 return -ENOMEM;
Steve French117e3b72018-04-22 10:24:19 -05001273 }
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +11001274 } else
1275 new_iov = s_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001276
1277 /* 1st iov is a RFC1001 length followed by the rest of the packet */
1278 memcpy(new_iov + 1, iov, (sizeof(struct kvec) * n_vec));
1279
1280 new_iov[0].iov_base = new_iov[1].iov_base;
1281 new_iov[0].iov_len = 4;
1282 new_iov[1].iov_base += 4;
1283 new_iov[1].iov_len -= 4;
1284
1285 memset(&rqst, 0, sizeof(struct smb_rqst));
1286 rqst.rq_iov = new_iov;
1287 rqst.rq_nvec = n_vec + 1;
1288
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001289 rc = cifs_send_recv(xid, ses, ses->server,
1290 &rqst, resp_buf_type, flags, resp_iov);
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +11001291 if (n_vec + 1 > CIFS_MAX_IOV_SIZE)
1292 kfree(new_iov);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001293 return rc;
1294}
1295
1296int
Steve French96daf2b2011-05-27 04:34:02 +00001297SendReceive(const unsigned int xid, struct cifs_ses *ses,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
Ronnie Sahlberg480b1cb2019-03-08 12:58:18 +10001299 int *pbytes_returned, const int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
1301 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 struct mid_q_entry *midQ;
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001303 unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
1304 struct kvec iov = { .iov_base = in_buf, .iov_len = len };
1305 struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08001306 struct cifs_credits credits = { .value = 1, .instance = 0 };
Colin Ian Kingac6ad7a2019-09-02 16:10:59 +01001307 struct TCP_Server_Info *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309 if (ses == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001310 cifs_dbg(VFS, "Null smb session\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 return -EIO;
1312 }
Colin Ian Kingac6ad7a2019-09-02 16:10:59 +01001313 server = ses->server;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001314 if (server == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001315 cifs_dbg(VFS, "Null tcp session\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 return -EIO;
1317 }
1318
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001319 if (server->tcpStatus == CifsExiting)
Steve French31ca3bc2005-04-28 22:41:11 -07001320 return -ENOENT;
1321
Steve French79a58d12007-07-06 22:44:50 +00001322 /* Ensure that we do not send more than 50 overlapping requests
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 to the same server. We may make this configurable later or
1324 use ses->maxReq */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001326 if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001327 cifs_server_dbg(VFS, "Invalid length, greater than maximum frame, %d\n",
1328 len);
Volker Lendecke6d9c6d52008-12-08 20:50:24 +00001329 return -EIO;
1330 }
1331
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001332 rc = wait_for_free_request(server, flags, &credits.instance);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001333 if (rc)
1334 return rc;
1335
Steve French79a58d12007-07-06 22:44:50 +00001336 /* make sure that we sign in the same order that we send on this socket
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 and avoid races inside tcp sendmsg code that could cause corruption
1338 of smb data */
1339
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001340 mutex_lock(&server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001342 rc = allocate_mid(ses, in_buf, &midQ);
1343 if (rc) {
Dan Carpenter8bd37542019-10-25 13:35:08 +03001344 mutex_unlock(&server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001345 /* Update # of requests on wire to server */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001346 add_credits(server, &credits, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001347 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 }
1349
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001350 rc = cifs_sign_smb(in_buf, server, &midQ->sequence_number);
Volker Lendecke829049c2008-12-06 16:00:53 +01001351 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001352 mutex_unlock(&server->srv_mutex);
Volker Lendecke829049c2008-12-06 16:00:53 +01001353 goto out;
1354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001356 midQ->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +00001357
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001358 cifs_in_send_inc(server);
1359 rc = smb_send(server, in_buf, len);
1360 cifs_in_send_dec(server);
Steve French789e6662011-08-09 18:44:44 +00001361 cifs_save_when_sent(midQ);
Jeff Laytonad313cb2013-04-03 10:27:36 -04001362
1363 if (rc < 0)
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001364 server->sequence_number -= 2;
Jeff Laytonad313cb2013-04-03 10:27:36 -04001365
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001366 mutex_unlock(&server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001367
Steve French79a58d12007-07-06 22:44:50 +00001368 if (rc < 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001369 goto out;
1370
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001371 rc = wait_for_response(server, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -05001372 if (rc != 0) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001373 send_cancel(server, &rqst, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -05001374 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001375 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
Jeff Layton1be912d2011-01-28 07:08:28 -05001376 /* no longer considered to be "in-flight" */
1377 midQ->callback = DeleteMidQEntry;
1378 spin_unlock(&GlobalMid_Lock);
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001379 add_credits(server, &credits, 0);
Jeff Layton1be912d2011-01-28 07:08:28 -05001380 return rc;
1381 }
1382 spin_unlock(&GlobalMid_Lock);
1383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001385 rc = cifs_sync_mid_result(midQ, server);
Jeff Layton053d5032011-01-11 07:24:02 -05001386 if (rc != 0) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001387 add_credits(server, &credits, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 return rc;
1389 }
Steve French50c2f752007-07-13 00:33:32 +00001390
Jeff Layton2c8f9812011-05-19 16:22:52 -04001391 if (!midQ->resp_buf || !out_buf ||
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001392 midQ->mid_state != MID_RESPONSE_RECEIVED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 rc = -EIO;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001394 cifs_server_dbg(VFS, "Bad MID state?\n");
Steve French2b2bdfb2008-12-11 17:26:54 +00001395 goto out;
1396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001398 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
Jeff Layton2c8f9812011-05-19 16:22:52 -04001399 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001400 rc = cifs_check_receive(midQ, server, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001401out:
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001402 cifs_delete_mid(midQ);
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001403 add_credits(server, &credits, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 return rc;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001406}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001408/* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
1409 blocking lock to return. */
1410
1411static int
Steve French96daf2b2011-05-27 04:34:02 +00001412send_lock_cancel(const unsigned int xid, struct cifs_tcon *tcon,
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001413 struct smb_hdr *in_buf,
1414 struct smb_hdr *out_buf)
1415{
1416 int bytes_returned;
Steve French96daf2b2011-05-27 04:34:02 +00001417 struct cifs_ses *ses = tcon->ses;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001418 LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
1419
1420 /* We just modify the current in_buf to change
1421 the type of lock from LOCKING_ANDX_SHARED_LOCK
1422 or LOCKING_ANDX_EXCLUSIVE_LOCK to
1423 LOCKING_ANDX_CANCEL_LOCK. */
1424
1425 pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
1426 pSMB->Timeout = 0;
Pavel Shilovsky88257362012-05-23 14:01:59 +04001427 pSMB->hdr.Mid = get_next_mid(ses->server);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001428
1429 return SendReceive(xid, ses, in_buf, out_buf,
Jeff Layton77499812011-01-11 07:24:23 -05001430 &bytes_returned, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001431}
1432
1433int
Steve French96daf2b2011-05-27 04:34:02 +00001434SendReceiveBlockingLock(const unsigned int xid, struct cifs_tcon *tcon,
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001435 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
1436 int *pbytes_returned)
1437{
1438 int rc = 0;
1439 int rstart = 0;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001440 struct mid_q_entry *midQ;
Steve French96daf2b2011-05-27 04:34:02 +00001441 struct cifs_ses *ses;
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001442 unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
1443 struct kvec iov = { .iov_base = in_buf, .iov_len = len };
1444 struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08001445 unsigned int instance;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001446 struct TCP_Server_Info *server;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001447
1448 if (tcon == NULL || tcon->ses == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001449 cifs_dbg(VFS, "Null smb session\n");
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001450 return -EIO;
1451 }
1452 ses = tcon->ses;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001453 server = ses->server;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001454
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001455 if (server == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001456 cifs_dbg(VFS, "Null tcp session\n");
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001457 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 }
1459
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001460 if (server->tcpStatus == CifsExiting)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001461 return -ENOENT;
1462
Steve French79a58d12007-07-06 22:44:50 +00001463 /* Ensure that we do not send more than 50 overlapping requests
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001464 to the same server. We may make this configurable later or
1465 use ses->maxReq */
1466
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001467 if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001468 cifs_tcon_dbg(VFS, "Invalid length, greater than maximum frame, %d\n",
1469 len);
Volker Lendecke6d9c6d52008-12-08 20:50:24 +00001470 return -EIO;
1471 }
1472
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001473 rc = wait_for_free_request(server, CIFS_BLOCKING_OP, &instance);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001474 if (rc)
1475 return rc;
1476
Steve French79a58d12007-07-06 22:44:50 +00001477 /* make sure that we sign in the same order that we send on this socket
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001478 and avoid races inside tcp sendmsg code that could cause corruption
1479 of smb data */
1480
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001481 mutex_lock(&server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001482
1483 rc = allocate_mid(ses, in_buf, &midQ);
1484 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001485 mutex_unlock(&server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001486 return rc;
1487 }
1488
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001489 rc = cifs_sign_smb(in_buf, server, &midQ->sequence_number);
Volker Lendecke829049c2008-12-06 16:00:53 +01001490 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001491 cifs_delete_mid(midQ);
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001492 mutex_unlock(&server->srv_mutex);
Volker Lendecke829049c2008-12-06 16:00:53 +01001493 return rc;
1494 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001495
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001496 midQ->mid_state = MID_REQUEST_SUBMITTED;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001497 cifs_in_send_inc(server);
1498 rc = smb_send(server, in_buf, len);
1499 cifs_in_send_dec(server);
Steve French789e6662011-08-09 18:44:44 +00001500 cifs_save_when_sent(midQ);
Jeff Laytonad313cb2013-04-03 10:27:36 -04001501
1502 if (rc < 0)
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001503 server->sequence_number -= 2;
Jeff Laytonad313cb2013-04-03 10:27:36 -04001504
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001505 mutex_unlock(&server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001506
Steve French79a58d12007-07-06 22:44:50 +00001507 if (rc < 0) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001508 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001509 return rc;
1510 }
1511
1512 /* Wait for a reply - allow signals to interrupt. */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001513 rc = wait_event_interruptible(server->response_q,
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001514 (!(midQ->mid_state == MID_REQUEST_SUBMITTED)) ||
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001515 ((server->tcpStatus != CifsGood) &&
1516 (server->tcpStatus != CifsNew)));
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001517
1518 /* Were we interrupted by a signal ? */
1519 if ((rc == -ERESTARTSYS) &&
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001520 (midQ->mid_state == MID_REQUEST_SUBMITTED) &&
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001521 ((server->tcpStatus == CifsGood) ||
1522 (server->tcpStatus == CifsNew))) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001523
1524 if (in_buf->Command == SMB_COM_TRANSACTION2) {
1525 /* POSIX lock. We send a NT_CANCEL SMB to cause the
1526 blocking lock to return. */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001527 rc = send_cancel(server, &rqst, midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001528 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001529 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001530 return rc;
1531 }
1532 } else {
1533 /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
1534 to cause the blocking lock to return. */
1535
1536 rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
1537
1538 /* If we get -ENOLCK back the lock may have
1539 already been removed. Don't exit in this case. */
1540 if (rc && rc != -ENOLCK) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001541 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001542 return rc;
1543 }
1544 }
1545
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001546 rc = wait_for_response(server, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -05001547 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001548 send_cancel(server, &rqst, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -05001549 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001550 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
Jeff Layton1be912d2011-01-28 07:08:28 -05001551 /* no longer considered to be "in-flight" */
1552 midQ->callback = DeleteMidQEntry;
1553 spin_unlock(&GlobalMid_Lock);
1554 return rc;
1555 }
1556 spin_unlock(&GlobalMid_Lock);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001557 }
Jeff Layton1be912d2011-01-28 07:08:28 -05001558
1559 /* We got the response - restart system call. */
1560 rstart = 1;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001561 }
1562
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001563 rc = cifs_sync_mid_result(midQ, server);
Jeff Layton053d5032011-01-11 07:24:02 -05001564 if (rc != 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001565 return rc;
Steve French50c2f752007-07-13 00:33:32 +00001566
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001567 /* rcvd frame is ok */
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001568 if (out_buf == NULL || midQ->mid_state != MID_RESPONSE_RECEIVED) {
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001569 rc = -EIO;
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001570 cifs_tcon_dbg(VFS, "Bad MID state?\n");
Volker Lendecke698e96a2008-12-06 16:39:31 +01001571 goto out;
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001572 }
1573
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001574 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
Jeff Layton2c8f9812011-05-19 16:22:52 -04001575 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001576 rc = cifs_check_receive(midQ, server, 0);
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001577out:
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001578 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001579 if (rstart && rc == -EACCES)
1580 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 return rc;
1582}