blob: 0ee36c3e66d346c838f6be82c3d6eaf9b591b536 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/transport.c
3 *
Steve Frenchad7a2922008-02-07 23:25:02 +00004 * Copyright (C) International Business Machines Corp., 2002,2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Author(s): Steve French (sfrench@us.ibm.com)
Steve French14a441a2b2006-07-16 04:32:51 +00006 * Jeremy Allison (jra@samba.org) 2006.
Steve French79a58d12007-07-06 22:44:50 +00007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published
10 * by the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
16 * the GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this library; if not, write to the Free Software
Steve French79a58d12007-07-06 22:44:50 +000020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 */
22
23#include <linux/fs.h>
24#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/wait.h>
27#include <linux/net.h>
28#include <linux/delay.h>
Jeff Laytonf06ac722011-10-19 15:30:40 -040029#include <linux/freezer.h>
Jeff Laytonb8eed282012-09-18 16:20:35 -070030#include <linux/tcp.h>
Christoph Hellwig2f8b5442016-11-01 07:40:13 -060031#include <linux/bvec.h>
Jeff Layton97bc00b2012-09-18 16:20:35 -070032#include <linux/highmem.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080033#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/processor.h>
35#include <linux/mempool.h>
36#include "cifspdu.h"
37#include "cifsglob.h"
38#include "cifsproto.h"
39#include "cifs_debug.h"
Aurelien Aptel8bd68c62018-02-16 19:19:29 +010040#include "smb2proto.h"
Long Li9762c2d2017-11-22 17:38:43 -070041#include "smbdirect.h"
Steve French50c2f752007-07-13 00:33:32 +000042
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +110043/* Max number of iovectors we can use off the stack when sending requests. */
44#define CIFS_MAX_IOV_SIZE 8
45
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +040046void
47cifs_wake_up_task(struct mid_q_entry *mid)
Jeff Layton2b84a36c2011-01-11 07:24:21 -050048{
49 wake_up_process(mid->callback_data);
50}
51
Jeff Laytona6827c12011-01-11 07:24:21 -050052struct mid_q_entry *
Jeff Layton24b9b062008-12-01 07:09:34 -050053AllocMidQEntry(const struct smb_hdr *smb_buffer, struct TCP_Server_Info *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55 struct mid_q_entry *temp;
56
Jeff Layton24b9b062008-12-01 07:09:34 -050057 if (server == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -050058 cifs_dbg(VFS, "Null TCP session in AllocMidQEntry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 return NULL;
60 }
Steve French50c2f752007-07-13 00:33:32 +000061
Pekka Enberg232087c2008-09-15 13:22:54 +030062 temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
NeilBrowna6f74e82017-04-10 12:08:53 +100063 memset(temp, 0, sizeof(struct mid_q_entry));
Lars Persson696e4202018-06-25 14:05:25 +020064 kref_init(&temp->refcount);
NeilBrowna6f74e82017-04-10 12:08:53 +100065 temp->mid = get_mid(smb_buffer);
66 temp->pid = current->pid;
67 temp->command = cpu_to_le16(smb_buffer->Command);
68 cifs_dbg(FYI, "For smb_command %d\n", smb_buffer->Command);
Steve French1047abc2005-10-11 19:58:06 -070069 /* do_gettimeofday(&temp->when_sent);*/ /* easier to use jiffies */
NeilBrowna6f74e82017-04-10 12:08:53 +100070 /* when mid allocated can be before when sent */
71 temp->when_alloc = jiffies;
72 temp->server = server;
Jeff Layton2b84a36c2011-01-11 07:24:21 -050073
NeilBrowna6f74e82017-04-10 12:08:53 +100074 /*
75 * The default is for the mid to be synchronous, so the
76 * default callback just wakes up the current task.
77 */
78 temp->callback = cifs_wake_up_task;
79 temp->callback_data = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 atomic_inc(&midCount);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -040082 temp->mid_state = MID_REQUEST_ALLOCATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 return temp;
84}
85
Lars Persson696e4202018-06-25 14:05:25 +020086static void _cifs_mid_q_entry_release(struct kref *refcount)
87{
88 struct mid_q_entry *mid = container_of(refcount, struct mid_q_entry,
89 refcount);
90
91 mempool_free(mid, cifs_mid_poolp);
92}
93
94void cifs_mid_q_entry_release(struct mid_q_entry *midEntry)
95{
96 spin_lock(&GlobalMid_Lock);
97 kref_put(&midEntry->refcount, _cifs_mid_q_entry_release);
98 spin_unlock(&GlobalMid_Lock);
99}
100
Jeff Layton766fdbb2011-01-11 07:24:21 -0500101void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102DeleteMidQEntry(struct mid_q_entry *midEntry)
103{
Steve French1047abc2005-10-11 19:58:06 -0700104#ifdef CONFIG_CIFS_STATS2
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400105 __le16 command = midEntry->server->vals->lock_cmd;
Steve French1047abc2005-10-11 19:58:06 -0700106 unsigned long now;
107#endif
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400108 midEntry->mid_state = MID_FREE;
Jeff Layton80975312011-01-11 07:24:02 -0500109 atomic_dec(&midCount);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400110 if (midEntry->large_buf)
Steve Frenchb8643e12005-04-28 22:41:07 -0700111 cifs_buf_release(midEntry->resp_buf);
112 else
113 cifs_small_buf_release(midEntry->resp_buf);
Steve French1047abc2005-10-11 19:58:06 -0700114#ifdef CONFIG_CIFS_STATS2
115 now = jiffies;
Steve French00778e22018-09-18 14:05:18 -0500116 /*
117 * commands taking longer than one second (default) can be indications
118 * that something is wrong, unless it is quite a slow link or a very
119 * busy server. Note that this calc is unlikely or impossible to wrap
120 * as long as slow_rsp_threshold is not set way above recommended max
121 * value (32767 ie 9 hours) and is generally harmless even if wrong
122 * since only affects debug counters - so leaving the calc as simple
123 * comparison rather than doing multiple conversions and overflow
124 * checks
125 */
126 if ((slow_rsp_threshold != 0) &&
127 time_after(now, midEntry->when_alloc + (slow_rsp_threshold * HZ)) &&
Steve French020eec52018-08-01 16:38:07 -0500128 (midEntry->command != command)) {
Steve Frenchf5942db2018-11-14 01:37:39 -0600129 /*
130 * smb2slowcmd[NUMBER_OF_SMB2_COMMANDS] counts by command
131 * NB: le16_to_cpu returns unsigned so can not be negative below
132 */
133 if (le16_to_cpu(midEntry->command) < NUMBER_OF_SMB2_COMMANDS)
Steve French468d6772018-08-04 05:24:34 -0500134 cifs_stats_inc(&midEntry->server->smb2slowcmd[le16_to_cpu(midEntry->command)]);
135
Steve French020eec52018-08-01 16:38:07 -0500136 trace_smb3_slow_rsp(le16_to_cpu(midEntry->command),
137 midEntry->mid, midEntry->pid,
138 midEntry->when_sent, midEntry->when_received);
139 if (cifsFYI & CIFS_TIMER) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +0300140 pr_debug(" CIFS slow rsp: cmd %d mid %llu",
Steve French1047abc2005-10-11 19:58:06 -0700141 midEntry->command, midEntry->mid);
Rodrigo Freiref80eaed2018-10-07 12:21:26 -0300142 cifs_info(" A: 0x%lx S: 0x%lx R: 0x%lx\n",
Steve French1047abc2005-10-11 19:58:06 -0700143 now - midEntry->when_alloc,
144 now - midEntry->when_sent,
145 now - midEntry->when_received);
146 }
147 }
148#endif
Lars Persson696e4202018-06-25 14:05:25 +0200149 cifs_mid_q_entry_release(midEntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700152void
153cifs_delete_mid(struct mid_q_entry *mid)
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500154{
155 spin_lock(&GlobalMid_Lock);
Ronnie Sahlbergddf83af2018-08-30 10:12:59 +1000156 list_del_init(&mid->qhead);
157 mid->mid_flags |= MID_DELETED;
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500158 spin_unlock(&GlobalMid_Lock);
159
160 DeleteMidQEntry(mid);
161}
162
Jeff Layton6f49f462012-09-18 16:20:34 -0700163/*
164 * smb_send_kvec - send an array of kvecs to the server
165 * @server: Server to send the data to
Al Viro3ab3f2a2015-11-13 02:36:04 -0500166 * @smb_msg: Message to send
Jeff Layton6f49f462012-09-18 16:20:34 -0700167 * @sent: amount of data sent on socket is stored here
168 *
169 * Our basic "send data to server" function. Should be called with srv_mutex
170 * held. The caller is responsible for handling the results.
171 */
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500172static int
Al Viro3ab3f2a2015-11-13 02:36:04 -0500173smb_send_kvec(struct TCP_Server_Info *server, struct msghdr *smb_msg,
174 size_t *sent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 int rc = 0;
Al Viro3ab3f2a2015-11-13 02:36:04 -0500177 int retries = 0;
Steve Frenchedf1ae42008-10-29 00:47:57 +0000178 struct socket *ssocket = server->ssocket;
Steve French50c2f752007-07-13 00:33:32 +0000179
Jeff Layton6f49f462012-09-18 16:20:34 -0700180 *sent = 0;
181
Al Viro3ab3f2a2015-11-13 02:36:04 -0500182 smb_msg->msg_name = (struct sockaddr *) &server->dstaddr;
183 smb_msg->msg_namelen = sizeof(struct sockaddr);
184 smb_msg->msg_control = NULL;
185 smb_msg->msg_controllen = 0;
Jeff Layton0496e022008-12-30 12:39:16 -0500186 if (server->noblocksnd)
Al Viro3ab3f2a2015-11-13 02:36:04 -0500187 smb_msg->msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL;
Steve Frenchedf1ae42008-10-29 00:47:57 +0000188 else
Al Viro3ab3f2a2015-11-13 02:36:04 -0500189 smb_msg->msg_flags = MSG_NOSIGNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Al Viro3ab3f2a2015-11-13 02:36:04 -0500191 while (msg_data_left(smb_msg)) {
Jeff Layton6f49f462012-09-18 16:20:34 -0700192 /*
193 * If blocking send, we try 3 times, since each can block
194 * for 5 seconds. For nonblocking we have to try more
195 * but wait increasing amounts of time allowing time for
196 * socket to clear. The overall time we wait in either
197 * case to send on the socket is about 15 seconds.
198 * Similarly we wait for 15 seconds for a response from
199 * the server in SendReceive[2] for the server to send
200 * a response back for most types of requests (except
201 * SMB Write past end of file which can be slow, and
202 * blocking lock operations). NFS waits slightly longer
203 * than CIFS, but this can make it take longer for
204 * nonresponsive servers to be detected and 15 seconds
205 * is more than enough time for modern networks to
206 * send a packet. In most cases if we fail to send
207 * after the retries we will kill the socket and
208 * reconnect which may clear the network problem.
209 */
Al Viro3ab3f2a2015-11-13 02:36:04 -0500210 rc = sock_sendmsg(ssocket, smb_msg);
Jeff Laytonce6c44e2013-03-22 08:36:45 -0400211 if (rc == -EAGAIN) {
Al Viro3ab3f2a2015-11-13 02:36:04 -0500212 retries++;
213 if (retries >= 14 ||
214 (!server->noblocksnd && (retries > 2))) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500215 cifs_dbg(VFS, "sends on sock %p stuck for 15 seconds\n",
216 ssocket);
Al Viro3ab3f2a2015-11-13 02:36:04 -0500217 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
Al Viro3ab3f2a2015-11-13 02:36:04 -0500219 msleep(1 << retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 continue;
221 }
Jeff Layton6f49f462012-09-18 16:20:34 -0700222
Steve French79a58d12007-07-06 22:44:50 +0000223 if (rc < 0)
Al Viro3ab3f2a2015-11-13 02:36:04 -0500224 return rc;
Jeff Layton6f49f462012-09-18 16:20:34 -0700225
Steve French79a58d12007-07-06 22:44:50 +0000226 if (rc == 0) {
Steve French3e844692005-10-03 13:37:24 -0700227 /* should never happen, letting socket clear before
228 retrying is our only obvious option here */
Joe Perchesf96637b2013-05-04 22:12:25 -0500229 cifs_dbg(VFS, "tcp sent no data\n");
Steve French3e844692005-10-03 13:37:24 -0700230 msleep(500);
231 continue;
232 }
Jeff Layton6f49f462012-09-18 16:20:34 -0700233
Al Viro3ab3f2a2015-11-13 02:36:04 -0500234 /* send was at least partially successful */
235 *sent += rc;
236 retries = 0; /* in case we get ENOSPC on the next send */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
Al Viro3ab3f2a2015-11-13 02:36:04 -0500238 return 0;
Jeff Layton97bc00b2012-09-18 16:20:35 -0700239}
240
Paulo Alcantara35e2cc12018-06-15 10:22:44 -0300241unsigned long
Ronnie Sahlberg81f39f92018-06-28 10:47:14 +1000242smb_rqst_len(struct TCP_Server_Info *server, struct smb_rqst *rqst)
Jeff Laytona26054d2014-02-14 07:21:00 -0500243{
244 unsigned int i;
Paulo Alcantara35e2cc12018-06-15 10:22:44 -0300245 struct kvec *iov;
246 int nvec;
Jeff Laytona26054d2014-02-14 07:21:00 -0500247 unsigned long buflen = 0;
248
Ronnie Sahlberg81f39f92018-06-28 10:47:14 +1000249 if (server->vals->header_preamble_size == 0 &&
250 rqst->rq_nvec >= 2 && rqst->rq_iov[0].iov_len == 4) {
Paulo Alcantara35e2cc12018-06-15 10:22:44 -0300251 iov = &rqst->rq_iov[1];
252 nvec = rqst->rq_nvec - 1;
253 } else {
254 iov = rqst->rq_iov;
255 nvec = rqst->rq_nvec;
256 }
257
Jeff Laytona26054d2014-02-14 07:21:00 -0500258 /* total up iov array first */
Paulo Alcantara35e2cc12018-06-15 10:22:44 -0300259 for (i = 0; i < nvec; i++)
Jeff Laytona26054d2014-02-14 07:21:00 -0500260 buflen += iov[i].iov_len;
261
Long Lic06a0f22018-05-30 12:47:57 -0700262 /*
263 * Add in the page array if there is one. The caller needs to make
264 * sure rq_offset and rq_tailsz are set correctly. If a buffer of
265 * multiple pages ends at page boundary, rq_tailsz needs to be set to
266 * PAGE_SIZE.
267 */
Jeff Laytona26054d2014-02-14 07:21:00 -0500268 if (rqst->rq_npages) {
Long Lic06a0f22018-05-30 12:47:57 -0700269 if (rqst->rq_npages == 1)
270 buflen += rqst->rq_tailsz;
271 else {
272 /*
273 * If there is more than one page, calculate the
274 * buffer length based on rq_offset and rq_tailsz
275 */
276 buflen += rqst->rq_pagesz * (rqst->rq_npages - 1) -
277 rqst->rq_offset;
278 buflen += rqst->rq_tailsz;
279 }
Jeff Laytona26054d2014-02-14 07:21:00 -0500280 }
281
282 return buflen;
283}
284
Jeff Layton6f49f462012-09-18 16:20:34 -0700285static int
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000286__smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
287 struct smb_rqst *rqst)
Jeff Layton6f49f462012-09-18 16:20:34 -0700288{
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000289 int rc = 0;
290 struct kvec *iov;
291 int n_vec;
292 unsigned int send_length = 0;
293 unsigned int i, j;
Al Viro3ab3f2a2015-11-13 02:36:04 -0500294 size_t total_len = 0, sent, size;
Jeff Laytonb8eed282012-09-18 16:20:35 -0700295 struct socket *ssocket = server->ssocket;
Al Viro3ab3f2a2015-11-13 02:36:04 -0500296 struct msghdr smb_msg;
Jeff Laytonb8eed282012-09-18 16:20:35 -0700297 int val = 1;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000298 __be32 rfc1002_marker;
299
Long Li9762c2d2017-11-22 17:38:43 -0700300 if (cifs_rdma_enabled(server) && server->smbd_conn) {
Ronnie Sahlberg81f39f92018-06-28 10:47:14 +1000301 rc = smbd_send(server, rqst);
Long Li9762c2d2017-11-22 17:38:43 -0700302 goto smbd_done;
303 }
Jeff Laytonea702b82012-12-27 07:28:55 -0500304 if (ssocket == NULL)
305 return -ENOTSOCK;
306
Jeff Laytonb8eed282012-09-18 16:20:35 -0700307 /* cork the socket */
308 kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
309 (char *)&val, sizeof(val));
310
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000311 for (j = 0; j < num_rqst; j++)
Ronnie Sahlberg81f39f92018-06-28 10:47:14 +1000312 send_length += smb_rqst_len(server, &rqst[j]);
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000313 rfc1002_marker = cpu_to_be32(send_length);
314
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000315 /* Generate a rfc1002 marker for SMB2+ */
316 if (server->vals->header_preamble_size == 0) {
317 struct kvec hiov = {
318 .iov_base = &rfc1002_marker,
319 .iov_len = 4
320 };
David Howellsaa563d72018-10-20 00:57:56 +0100321 iov_iter_kvec(&smb_msg.msg_iter, WRITE, &hiov, 1, 4);
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000322 rc = smb_send_kvec(server, &smb_msg, &sent);
323 if (rc < 0)
324 goto uncork;
325
326 total_len += sent;
327 send_length += 4;
328 }
329
Paulo Alcantara662bf5b2018-06-14 17:34:08 -0300330 cifs_dbg(FYI, "Sending smb: smb_len=%u\n", send_length);
331
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000332 for (j = 0; j < num_rqst; j++) {
333 iov = rqst[j].rq_iov;
334 n_vec = rqst[j].rq_nvec;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000335
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000336 size = 0;
Paulo Alcantara662bf5b2018-06-14 17:34:08 -0300337 for (i = 0; i < n_vec; i++) {
338 dump_smb(iov[i].iov_base, iov[i].iov_len);
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000339 size += iov[i].iov_len;
Paulo Alcantara662bf5b2018-06-14 17:34:08 -0300340 }
Al Viro3ab3f2a2015-11-13 02:36:04 -0500341
David Howellsaa563d72018-10-20 00:57:56 +0100342 iov_iter_kvec(&smb_msg.msg_iter, WRITE, iov, n_vec, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Al Viro3ab3f2a2015-11-13 02:36:04 -0500344 rc = smb_send_kvec(server, &smb_msg, &sent);
Jeff Layton97bc00b2012-09-18 16:20:35 -0700345 if (rc < 0)
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000346 goto uncork;
Jeff Layton97bc00b2012-09-18 16:20:35 -0700347
348 total_len += sent;
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000349
350 /* now walk the page array and send each page in it */
351 for (i = 0; i < rqst[j].rq_npages; i++) {
352 struct bio_vec bvec;
353
354 bvec.bv_page = rqst[j].rq_pages[i];
355 rqst_page_get_length(&rqst[j], i, &bvec.bv_len,
356 &bvec.bv_offset);
357
David Howellsaa563d72018-10-20 00:57:56 +0100358 iov_iter_bvec(&smb_msg.msg_iter, WRITE,
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000359 &bvec, 1, bvec.bv_len);
360 rc = smb_send_kvec(server, &smb_msg, &sent);
361 if (rc < 0)
362 break;
363
364 total_len += sent;
365 }
Jeff Layton97bc00b2012-09-18 16:20:35 -0700366 }
367
368uncork:
Jeff Laytonb8eed282012-09-18 16:20:35 -0700369 /* uncork it */
370 val = 0;
371 kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
372 (char *)&val, sizeof(val));
373
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000374 if ((total_len > 0) && (total_len != send_length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500375 cifs_dbg(FYI, "partial send (wanted=%u sent=%zu): terminating session\n",
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000376 send_length, total_len);
Jeff Layton6f49f462012-09-18 16:20:34 -0700377 /*
378 * If we have only sent part of an SMB then the next SMB could
379 * be taken as the remainder of this one. We need to kill the
380 * socket so the server throws away the partial SMB
381 */
Steve Frenchedf1ae42008-10-29 00:47:57 +0000382 server->tcpStatus = CifsNeedReconnect;
Steve Frenchbf1fdeb2018-07-30 19:23:09 -0500383 trace_smb3_partial_send_reconnect(server->CurrentMid,
384 server->hostname);
Steve Frenchedf1ae42008-10-29 00:47:57 +0000385 }
Long Li9762c2d2017-11-22 17:38:43 -0700386smbd_done:
Jeff Laytond804d412011-01-28 15:05:43 -0500387 if (rc < 0 && rc != -EINTR)
Joe Perchesf96637b2013-05-04 22:12:25 -0500388 cifs_dbg(VFS, "Error %d sending data on socket to server\n",
389 rc);
Pavel Shilovskyee139192019-01-10 11:27:28 -0800390 else if (rc > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 return rc;
394}
395
Jeff Layton6f49f462012-09-18 16:20:34 -0700396static int
Ronnie Sahlberg1f3a8f52018-08-01 09:26:12 +1000397smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
398 struct smb_rqst *rqst, int flags)
Jeff Layton6f49f462012-09-18 16:20:34 -0700399{
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +1000400 struct kvec iov;
401 struct smb2_transform_hdr tr_hdr;
402 struct smb_rqst cur_rqst[MAX_COMPOUND];
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700403 int rc;
Jeff Layton6f49f462012-09-18 16:20:34 -0700404
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700405 if (!(flags & CIFS_TRANSFORM_REQ))
Ronnie Sahlberg1f3a8f52018-08-01 09:26:12 +1000406 return __smb_send_rqst(server, num_rqst, rqst);
407
408 if (num_rqst > MAX_COMPOUND - 1)
409 return -ENOMEM;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700410
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +1000411 memset(&cur_rqst[0], 0, sizeof(cur_rqst));
412 memset(&iov, 0, sizeof(iov));
413 memset(&tr_hdr, 0, sizeof(tr_hdr));
414
415 iov.iov_base = &tr_hdr;
416 iov.iov_len = sizeof(tr_hdr);
417 cur_rqst[0].rq_iov = &iov;
418 cur_rqst[0].rq_nvec = 1;
419
420 if (!server->ops->init_transform_rq) {
421 cifs_dbg(VFS, "Encryption requested but transform callback "
422 "is missing\n");
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700423 return -EIO;
424 }
425
Ronnie Sahlberg1f3a8f52018-08-01 09:26:12 +1000426 rc = server->ops->init_transform_rq(server, num_rqst + 1,
427 &cur_rqst[0], rqst);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700428 if (rc)
429 return rc;
430
Ronnie Sahlberg1f3a8f52018-08-01 09:26:12 +1000431 rc = __smb_send_rqst(server, num_rqst + 1, &cur_rqst[0]);
432 smb3_free_compound_rqst(num_rqst, &cur_rqst[1]);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700433 return rc;
Jeff Layton6f49f462012-09-18 16:20:34 -0700434}
435
Jeff Layton0496e022008-12-30 12:39:16 -0500436int
437smb_send(struct TCP_Server_Info *server, struct smb_hdr *smb_buffer,
438 unsigned int smb_buf_length)
439{
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800440 struct kvec iov[2];
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700441 struct smb_rqst rqst = { .rq_iov = iov,
442 .rq_nvec = 2 };
Jeff Layton0496e022008-12-30 12:39:16 -0500443
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800444 iov[0].iov_base = smb_buffer;
445 iov[0].iov_len = 4;
446 iov[1].iov_base = (char *)smb_buffer + 4;
447 iov[1].iov_len = smb_buf_length;
Jeff Layton0496e022008-12-30 12:39:16 -0500448
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000449 return __smb_send_rqst(server, 1, &rqst);
Jeff Layton0496e022008-12-30 12:39:16 -0500450}
451
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300452static int
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400453wait_for_free_credits(struct TCP_Server_Info *server, const int timeout,
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -0800454 int *credits, unsigned int *instance)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000455{
Pavel Shilovsky5bc59492012-02-21 19:56:08 +0300456 int rc;
457
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -0800458 *instance = 0;
459
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300460 spin_lock(&server->req_lock);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400461 if (timeout == CIFS_ASYNC_OP) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000462 /* oplock breaks must not be held up */
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300463 server->in_flight++;
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300464 *credits -= 1;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -0800465 *instance = server->reconnect_instance;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300466 spin_unlock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000467 return 0;
468 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000469
Volker Lendecke27a97a62008-12-08 20:59:39 +0000470 while (1) {
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300471 if (*credits <= 0) {
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300472 spin_unlock(&server->req_lock);
Steve French789e6662011-08-09 18:44:44 +0000473 cifs_num_waiters_inc(server);
Pavel Shilovsky5bc59492012-02-21 19:56:08 +0300474 rc = wait_event_killable(server->request_q,
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300475 has_credits(server, credits));
Steve French789e6662011-08-09 18:44:44 +0000476 cifs_num_waiters_dec(server);
Pavel Shilovsky5bc59492012-02-21 19:56:08 +0300477 if (rc)
478 return rc;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300479 spin_lock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000480 } else {
Jeff Laytonc5797a92011-01-11 07:24:01 -0500481 if (server->tcpStatus == CifsExiting) {
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300482 spin_unlock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000483 return -ENOENT;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000484 }
Volker Lendecke27a97a62008-12-08 20:59:39 +0000485
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400486 /*
487 * Can not count locking commands against total
488 * as they are allowed to block on server.
489 */
Volker Lendecke27a97a62008-12-08 20:59:39 +0000490
491 /* update # of requests on the wire to server */
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400492 if (timeout != CIFS_BLOCKING_OP) {
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300493 *credits -= 1;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300494 server->in_flight++;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -0800495 *instance = server->reconnect_instance;
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400496 }
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300497 spin_unlock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000498 break;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000499 }
500 }
501 return 0;
502}
503
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300504static int
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400505wait_for_free_request(struct TCP_Server_Info *server, const int timeout,
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -0800506 const int optype, unsigned int *instance)
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300507{
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -0500508 int *val;
509
510 val = server->ops->get_credits_field(server, optype);
511 /* Since an echo is already inflight, no need to wait to send another */
512 if (*val <= 0 && optype == CIFS_ECHO_OP)
513 return -EAGAIN;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -0800514 return wait_for_free_credits(server, timeout, val, instance);
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300515}
516
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400517int
518cifs_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800519 unsigned int *num, struct cifs_credits *credits)
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400520{
521 *num = size;
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800522 credits->value = 0;
523 credits->instance = server->reconnect_instance;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400524 return 0;
525}
526
Steve French96daf2b2011-05-27 04:34:02 +0000527static int allocate_mid(struct cifs_ses *ses, struct smb_hdr *in_buf,
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000528 struct mid_q_entry **ppmidQ)
529{
530 if (ses->server->tcpStatus == CifsExiting) {
531 return -ENOENT;
Volker Lendecke8fbbd362008-12-06 13:12:34 +0100532 }
533
534 if (ses->server->tcpStatus == CifsNeedReconnect) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500535 cifs_dbg(FYI, "tcp session dead - return to caller to retry\n");
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000536 return -EAGAIN;
Volker Lendecke8fbbd362008-12-06 13:12:34 +0100537 }
538
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500539 if (ses->status == CifsNew) {
Steve French79a58d12007-07-06 22:44:50 +0000540 if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
Steve Frenchad7a2922008-02-07 23:25:02 +0000541 (in_buf->Command != SMB_COM_NEGOTIATE))
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000542 return -EAGAIN;
Steve Frenchad7a2922008-02-07 23:25:02 +0000543 /* else ok - we are setting up session */
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000544 }
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500545
546 if (ses->status == CifsExiting) {
547 /* check if SMB session is bad because we are setting it up */
548 if (in_buf->Command != SMB_COM_LOGOFF_ANDX)
549 return -EAGAIN;
550 /* else ok - we are shutting down session */
551 }
552
Jeff Layton24b9b062008-12-01 07:09:34 -0500553 *ppmidQ = AllocMidQEntry(in_buf, ses->server);
Steve French26f57362007-08-30 22:09:15 +0000554 if (*ppmidQ == NULL)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000555 return -ENOMEM;
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500556 spin_lock(&GlobalMid_Lock);
557 list_add_tail(&(*ppmidQ)->qhead, &ses->server->pending_mid_q);
558 spin_unlock(&GlobalMid_Lock);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000559 return 0;
560}
561
Jeff Layton0ade6402011-01-11 07:24:02 -0500562static int
563wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *midQ)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000564{
Jeff Layton0ade6402011-01-11 07:24:02 -0500565 int error;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000566
Colin Cross5853cc22013-05-07 17:52:05 +0000567 error = wait_event_freezekillable_unsafe(server->response_q,
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400568 midQ->mid_state != MID_REQUEST_SUBMITTED);
Jeff Layton0ade6402011-01-11 07:24:02 -0500569 if (error < 0)
570 return -ERESTARTSYS;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000571
Jeff Layton0ade6402011-01-11 07:24:02 -0500572 return 0;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000573}
574
Jeff Laytonfec344e2012-09-18 16:20:35 -0700575struct mid_q_entry *
576cifs_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400577{
578 int rc;
Jeff Laytonfec344e2012-09-18 16:20:35 -0700579 struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400580 struct mid_q_entry *mid;
581
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800582 if (rqst->rq_iov[0].iov_len != 4 ||
583 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
584 return ERR_PTR(-EIO);
585
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400586 /* enable signing if server requires it */
Jeff Layton38d77c52013-05-26 07:01:00 -0400587 if (server->sign)
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400588 hdr->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
589
590 mid = AllocMidQEntry(hdr, server);
591 if (mid == NULL)
Jeff Laytonfec344e2012-09-18 16:20:35 -0700592 return ERR_PTR(-ENOMEM);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400593
Jeff Laytonfec344e2012-09-18 16:20:35 -0700594 rc = cifs_sign_rqst(rqst, server, &mid->sequence_number);
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100595 if (rc) {
596 DeleteMidQEntry(mid);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700597 return ERR_PTR(rc);
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100598 }
599
Jeff Laytonfec344e2012-09-18 16:20:35 -0700600 return mid;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400601}
Steve French133672e2007-11-13 22:41:37 +0000602
603/*
Jeff Laytona6827c12011-01-11 07:24:21 -0500604 * Send a SMB request and set the callback function in the mid to handle
605 * the result. Caller is responsible for dealing with timeouts.
606 */
607int
Jeff Laytonfec344e2012-09-18 16:20:35 -0700608cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst,
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -0800609 mid_receive_t *receive, mid_callback_t *callback,
610 mid_handle_t *handle, void *cbdata, const int flags)
Jeff Laytona6827c12011-01-11 07:24:21 -0500611{
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400612 int rc, timeout, optype;
Jeff Laytona6827c12011-01-11 07:24:21 -0500613 struct mid_q_entry *mid;
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800614 struct cifs_credits credits = { .value = 0, .instance = 0 };
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -0800615 unsigned int instance;
Jeff Laytona6827c12011-01-11 07:24:21 -0500616
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400617 timeout = flags & CIFS_TIMEOUT_MASK;
618 optype = flags & CIFS_OP_MASK;
619
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400620 if ((flags & CIFS_HAS_CREDITS) == 0) {
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -0800621 rc = wait_for_free_request(server, timeout, optype, &instance);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400622 if (rc)
623 return rc;
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800624 credits.value = 1;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -0800625 credits.instance = instance;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400626 }
Jeff Laytona6827c12011-01-11 07:24:21 -0500627
628 mutex_lock(&server->srv_mutex);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700629 mid = server->ops->setup_async_request(server, rqst);
630 if (IS_ERR(mid)) {
Jeff Laytona6827c12011-01-11 07:24:21 -0500631 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800632 add_credits_and_wake_if(server, &credits, optype);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700633 return PTR_ERR(mid);
Jeff Laytona6827c12011-01-11 07:24:21 -0500634 }
635
Jeff Layton44d22d82011-10-19 15:29:49 -0400636 mid->receive = receive;
Jeff Laytona6827c12011-01-11 07:24:21 -0500637 mid->callback = callback;
638 mid->callback_data = cbdata;
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -0800639 mid->handle = handle;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400640 mid->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +0000641
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100642 /* put it on the pending_mid_q */
643 spin_lock(&GlobalMid_Lock);
644 list_add_tail(&mid->qhead, &server->pending_mid_q);
645 spin_unlock(&GlobalMid_Lock);
646
Long Li93d2cb62017-06-28 15:55:55 -0700647 /*
648 * Need to store the time in mid before calling I/O. For call_async,
649 * I/O response may come back and free the mid entry on another thread.
650 */
651 cifs_save_when_sent(mid);
Steve French789e6662011-08-09 18:44:44 +0000652 cifs_in_send_inc(server);
Ronnie Sahlberg1f3a8f52018-08-01 09:26:12 +1000653 rc = smb_send_rqst(server, 1, rqst, flags);
Steve French789e6662011-08-09 18:44:44 +0000654 cifs_in_send_dec(server);
Jeff Laytonad313cb2013-04-03 10:27:36 -0400655
Rabin Vincent820962d2015-12-23 07:32:41 +0100656 if (rc < 0) {
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800657 revert_current_mid(server, mid->credits);
Jeff Laytonad313cb2013-04-03 10:27:36 -0400658 server->sequence_number -= 2;
Rabin Vincent820962d2015-12-23 07:32:41 +0100659 cifs_delete_mid(mid);
660 }
661
Jeff Laytona6827c12011-01-11 07:24:21 -0500662 mutex_unlock(&server->srv_mutex);
Steve French789e6662011-08-09 18:44:44 +0000663
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100664 if (rc == 0)
665 return 0;
Jeff Laytona6827c12011-01-11 07:24:21 -0500666
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800667 add_credits_and_wake_if(server, &credits, optype);
Jeff Laytona6827c12011-01-11 07:24:21 -0500668 return rc;
669}
670
671/*
Steve French133672e2007-11-13 22:41:37 +0000672 *
673 * Send an SMB Request. No response info (other than return code)
674 * needs to be parsed.
675 *
676 * flags indicate the type of request buffer and how long to wait
677 * and whether to log NT STATUS code (error) before mapping it to POSIX error
678 *
679 */
680int
Steve French96daf2b2011-05-27 04:34:02 +0000681SendReceiveNoRsp(const unsigned int xid, struct cifs_ses *ses,
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400682 char *in_buf, int flags)
Steve French133672e2007-11-13 22:41:37 +0000683{
684 int rc;
685 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700686 struct kvec rsp_iov;
Steve French133672e2007-11-13 22:41:37 +0000687 int resp_buf_type;
688
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400689 iov[0].iov_base = in_buf;
690 iov[0].iov_len = get_rfc1002_length(in_buf) + 4;
Steve French133672e2007-11-13 22:41:37 +0000691 flags |= CIFS_NO_RESP;
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700692 rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Joe Perchesf96637b2013-05-04 22:12:25 -0500693 cifs_dbg(NOISY, "SendRcvNoRsp flags %d rc %d\n", flags, rc);
Steve French90c81e02008-02-12 20:32:36 +0000694
Steve French133672e2007-11-13 22:41:37 +0000695 return rc;
696}
697
Jeff Layton053d5032011-01-11 07:24:02 -0500698static int
Jeff Layton3c1105d2011-05-22 07:09:13 -0400699cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
Jeff Layton053d5032011-01-11 07:24:02 -0500700{
701 int rc = 0;
702
Joe Perchesf96637b2013-05-04 22:12:25 -0500703 cifs_dbg(FYI, "%s: cmd=%d mid=%llu state=%d\n",
704 __func__, le16_to_cpu(mid->command), mid->mid, mid->mid_state);
Jeff Layton053d5032011-01-11 07:24:02 -0500705
Jeff Layton74dd92a2011-01-11 07:24:02 -0500706 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400707 switch (mid->mid_state) {
Jeff Layton74dd92a2011-01-11 07:24:02 -0500708 case MID_RESPONSE_RECEIVED:
Jeff Layton053d5032011-01-11 07:24:02 -0500709 spin_unlock(&GlobalMid_Lock);
710 return rc;
Jeff Layton74dd92a2011-01-11 07:24:02 -0500711 case MID_RETRY_NEEDED:
712 rc = -EAGAIN;
713 break;
Jeff Layton71823ba2011-02-10 08:03:50 -0500714 case MID_RESPONSE_MALFORMED:
715 rc = -EIO;
716 break;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400717 case MID_SHUTDOWN:
718 rc = -EHOSTDOWN;
719 break;
Jeff Layton74dd92a2011-01-11 07:24:02 -0500720 default:
Jeff Layton3c1105d2011-05-22 07:09:13 -0400721 list_del_init(&mid->qhead);
Joe Perchesf96637b2013-05-04 22:12:25 -0500722 cifs_dbg(VFS, "%s: invalid mid state mid=%llu state=%d\n",
723 __func__, mid->mid, mid->mid_state);
Jeff Layton74dd92a2011-01-11 07:24:02 -0500724 rc = -EIO;
Jeff Layton053d5032011-01-11 07:24:02 -0500725 }
726 spin_unlock(&GlobalMid_Lock);
727
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500728 DeleteMidQEntry(mid);
Jeff Layton053d5032011-01-11 07:24:02 -0500729 return rc;
730}
731
Jeff Layton121b0462012-05-15 12:21:10 -0400732static inline int
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800733send_cancel(struct TCP_Server_Info *server, struct smb_rqst *rqst,
734 struct mid_q_entry *mid)
Jeff Layton76dcc262011-01-11 07:24:24 -0500735{
Jeff Layton121b0462012-05-15 12:21:10 -0400736 return server->ops->send_cancel ?
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800737 server->ops->send_cancel(server, rqst, mid) : 0;
Jeff Layton76dcc262011-01-11 07:24:24 -0500738}
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740int
Jeff Layton2c8f9812011-05-19 16:22:52 -0400741cifs_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
742 bool log_error)
743{
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400744 unsigned int len = get_rfc1002_length(mid->resp_buf) + 4;
Jeff Layton826a95e2011-10-11 06:41:32 -0400745
746 dump_smb(mid->resp_buf, min_t(u32, 92, len));
Jeff Layton2c8f9812011-05-19 16:22:52 -0400747
748 /* convert the length into a more usable form */
Jeff Layton38d77c52013-05-26 07:01:00 -0400749 if (server->sign) {
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800750 struct kvec iov[2];
Steve French985e4ff02012-08-03 09:42:45 -0500751 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800752 struct smb_rqst rqst = { .rq_iov = iov,
753 .rq_nvec = 2 };
Jeff Layton826a95e2011-10-11 06:41:32 -0400754
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800755 iov[0].iov_base = mid->resp_buf;
756 iov[0].iov_len = 4;
757 iov[1].iov_base = (char *)mid->resp_buf + 4;
758 iov[1].iov_len = len - 4;
Jeff Layton2c8f9812011-05-19 16:22:52 -0400759 /* FIXME: add code to kill session */
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700760 rc = cifs_verify_signature(&rqst, server,
Jeff Layton0124cc42013-04-03 11:55:03 -0400761 mid->sequence_number);
Steve French985e4ff02012-08-03 09:42:45 -0500762 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500763 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
764 rc);
Jeff Layton2c8f9812011-05-19 16:22:52 -0400765 }
766
767 /* BB special case reconnect tid and uid here? */
768 return map_smb_to_linux_error(mid->resp_buf, log_error);
769}
770
Jeff Laytonfec344e2012-09-18 16:20:35 -0700771struct mid_q_entry *
772cifs_setup_request(struct cifs_ses *ses, struct smb_rqst *rqst)
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400773{
774 int rc;
Jeff Laytonfec344e2012-09-18 16:20:35 -0700775 struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400776 struct mid_q_entry *mid;
777
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800778 if (rqst->rq_iov[0].iov_len != 4 ||
779 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
780 return ERR_PTR(-EIO);
781
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400782 rc = allocate_mid(ses, hdr, &mid);
783 if (rc)
Jeff Laytonfec344e2012-09-18 16:20:35 -0700784 return ERR_PTR(rc);
785 rc = cifs_sign_rqst(rqst, ses->server, &mid->sequence_number);
786 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700787 cifs_delete_mid(mid);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700788 return ERR_PTR(rc);
789 }
790 return mid;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400791}
792
Ronnie Sahlberg4e34feb2018-08-30 10:13:00 +1000793static void
Pavel Shilovskyee258d72019-01-03 15:53:10 -0800794cifs_compound_callback(struct mid_q_entry *mid)
Pavel Shilovsky8a26f0f2019-01-03 16:45:27 -0800795{
796 struct TCP_Server_Info *server = mid->server;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -0800797 struct cifs_credits credits;
Pavel Shilovsky8a26f0f2019-01-03 16:45:27 -0800798
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -0800799 credits.value = server->ops->get_credits(mid);
800 credits.instance = server->reconnect_instance;
801
802 add_credits(server, &credits, mid->optype);
Pavel Shilovsky8a26f0f2019-01-03 16:45:27 -0800803}
804
Pavel Shilovskyee258d72019-01-03 15:53:10 -0800805static void
806cifs_compound_last_callback(struct mid_q_entry *mid)
807{
808 cifs_compound_callback(mid);
809 cifs_wake_up_task(mid);
810}
811
812static void
813cifs_cancelled_callback(struct mid_q_entry *mid)
814{
815 cifs_compound_callback(mid);
816 DeleteMidQEntry(mid);
817}
818
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800819int
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000820compound_send_recv(const unsigned int xid, struct cifs_ses *ses,
821 const int flags, const int num_rqst, struct smb_rqst *rqst,
822 int *resp_buf_type, struct kvec *resp_iov)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000824 int i, j, rc = 0;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400825 int timeout, optype;
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000826 struct mid_q_entry *midQ[MAX_COMPOUND];
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -0800827 bool cancelled_mid[MAX_COMPOUND] = {false};
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -0800828 struct cifs_credits credits[MAX_COMPOUND] = {
829 { .value = 0, .instance = 0 }
830 };
831 unsigned int instance;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800832 char *buf;
Steve French50c2f752007-07-13 00:33:32 +0000833
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400834 timeout = flags & CIFS_TIMEOUT_MASK;
835 optype = flags & CIFS_OP_MASK;
Steve French133672e2007-11-13 22:41:37 +0000836
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000837 for (i = 0; i < num_rqst; i++)
838 resp_buf_type[i] = CIFS_NO_BUFFER; /* no response buf yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Steve French4b8f9302006-02-26 16:41:18 +0000840 if ((ses == NULL) || (ses->server == NULL)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500841 cifs_dbg(VFS, "Null session\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return -EIO;
843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700845 if (ses->server->tcpStatus == CifsExiting)
Steve French31ca3bc2005-04-28 22:41:11 -0700846 return -ENOENT;
847
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400848 /*
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -0800849 * Ensure we obtain 1 credit per request in the compound chain.
850 * It can be optimized further by waiting for all the credits
851 * at once but this can wait long enough if we don't have enough
852 * credits due to some heavy operations in progress or the server
853 * not granting us much, so a fallback to the current approach is
854 * needed anyway.
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400855 */
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -0800856 for (i = 0; i < num_rqst; i++) {
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -0800857 rc = wait_for_free_request(ses->server, timeout, optype,
858 &instance);
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -0800859 if (rc) {
860 /*
861 * We haven't sent an SMB packet to the server yet but
862 * we already obtained credits for i requests in the
863 * compound chain - need to return those credits back
864 * for future use. Note that we need to call add_credits
865 * multiple times to match the way we obtained credits
866 * in the first place and to account for in flight
867 * requests correctly.
868 */
869 for (j = 0; j < i; j++)
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -0800870 add_credits(ses->server, &credits[j], optype);
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -0800871 return rc;
872 }
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -0800873 credits[i].value = 1;
874 credits[i].instance = instance;
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -0800875 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000876
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400877 /*
878 * Make sure that we sign in the same order that we send on this socket
879 * and avoid races inside tcp sendmsg code that could cause corruption
880 * of smb data.
881 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Jeff Layton72ca5452008-12-01 07:09:36 -0500883 mutex_lock(&ses->server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000885 for (i = 0; i < num_rqst; i++) {
886 midQ[i] = ses->server->ops->setup_request(ses, &rqst[i]);
887 if (IS_ERR(midQ[i])) {
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800888 revert_current_mid(ses->server, i);
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000889 for (j = 0; j < i; j++)
890 cifs_delete_mid(midQ[j]);
891 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -0800892
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000893 /* Update # of requests on wire to server */
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -0800894 for (j = 0; j < num_rqst; j++)
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -0800895 add_credits(ses->server, &credits[j], optype);
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000896 return PTR_ERR(midQ[i]);
897 }
898
899 midQ[i]->mid_state = MID_REQUEST_SUBMITTED;
Pavel Shilovsky8a26f0f2019-01-03 16:45:27 -0800900 midQ[i]->optype = optype;
Ronnie Sahlberg4e34feb2018-08-30 10:13:00 +1000901 /*
Pavel Shilovskyee258d72019-01-03 15:53:10 -0800902 * Invoke callback for every part of the compound chain
903 * to calculate credits properly. Wake up this thread only when
904 * the last element is received.
Ronnie Sahlberg4e34feb2018-08-30 10:13:00 +1000905 */
906 if (i < num_rqst - 1)
Pavel Shilovskyee258d72019-01-03 15:53:10 -0800907 midQ[i]->callback = cifs_compound_callback;
908 else
909 midQ[i]->callback = cifs_compound_last_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
Steve French789e6662011-08-09 18:44:44 +0000911 cifs_in_send_inc(ses->server);
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000912 rc = smb_send_rqst(ses->server, num_rqst, rqst, flags);
Steve French789e6662011-08-09 18:44:44 +0000913 cifs_in_send_dec(ses->server);
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000914
915 for (i = 0; i < num_rqst; i++)
916 cifs_save_when_sent(midQ[i]);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000917
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800918 if (rc < 0) {
919 revert_current_mid(ses->server, num_rqst);
Jeff Laytonad313cb2013-04-03 10:27:36 -0400920 ses->server->sequence_number -= 2;
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800921 }
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000922
Jeff Layton72ca5452008-12-01 07:09:36 -0500923 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000924
Pavel Shilovskyee258d72019-01-03 15:53:10 -0800925 if (rc < 0) {
926 /* Sending failed for some reason - return credits back */
927 for (i = 0; i < num_rqst; i++)
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -0800928 add_credits(ses->server, &credits[i], optype);
Ronnie Sahlbergcb5c2e62018-10-10 15:29:06 +1000929 goto out;
Pavel Shilovskyee258d72019-01-03 15:53:10 -0800930 }
931
932 /*
933 * At this point the request is passed to the network stack - we assume
934 * that any credits taken from the server structure on the client have
935 * been spent and we can't return them back. Once we receive responses
936 * we will collect credits granted by the server in the mid callbacks
937 * and add those credits to the server structure.
938 */
Ronnie Sahlbergcb5c2e62018-10-10 15:29:06 +1000939
940 /*
941 * Compounding is never used during session establish.
942 */
943 if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP))
944 smb311_update_preauth_hash(ses, rqst[0].rq_iov,
945 rqst[0].rq_nvec);
946
947 if (timeout == CIFS_ASYNC_OP)
948 goto out;
949
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000950 for (i = 0; i < num_rqst; i++) {
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000951 rc = wait_for_response(ses->server, midQ[i]);
Pavel Shilovsky8a26f0f2019-01-03 16:45:27 -0800952 if (rc != 0)
953 break;
954 }
955 if (rc != 0) {
956 for (; i < num_rqst; i++) {
Steve French43de1db2018-10-23 21:04:57 -0500957 cifs_dbg(VFS, "Cancelling wait for mid %llu cmd: %d\n",
958 midQ[i]->mid, le16_to_cpu(midQ[i]->command));
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000959 send_cancel(ses->server, &rqst[i], midQ[i]);
960 spin_lock(&GlobalMid_Lock);
961 if (midQ[i]->mid_state == MID_REQUEST_SUBMITTED) {
962 midQ[i]->mid_flags |= MID_WAIT_CANCELLED;
Pavel Shilovsky8a26f0f2019-01-03 16:45:27 -0800963 midQ[i]->callback = cifs_cancelled_callback;
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -0800964 cancelled_mid[i] = true;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -0800965 credits[i].value = 0;
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000966 }
Jeff Layton1be912d2011-01-28 07:08:28 -0500967 spin_unlock(&GlobalMid_Lock);
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000968 }
Ronnie Sahlbergcb5c2e62018-10-10 15:29:06 +1000969 }
970
Ronnie Sahlbergcb5c2e62018-10-10 15:29:06 +1000971 for (i = 0; i < num_rqst; i++) {
972 if (rc < 0)
973 goto out;
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000974
975 rc = cifs_sync_mid_result(midQ[i], ses->server);
976 if (rc != 0) {
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -0800977 /* mark this mid as cancelled to not free it below */
978 cancelled_mid[i] = true;
979 goto out;
Jeff Layton1be912d2011-01-28 07:08:28 -0500980 }
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000981
982 if (!midQ[i]->resp_buf ||
983 midQ[i]->mid_state != MID_RESPONSE_RECEIVED) {
984 rc = -EIO;
985 cifs_dbg(FYI, "Bad MID state?\n");
986 goto out;
987 }
988
989 buf = (char *)midQ[i]->resp_buf;
990 resp_iov[i].iov_base = buf;
991 resp_iov[i].iov_len = midQ[i]->resp_buf_size +
992 ses->server->vals->header_preamble_size;
993
994 if (midQ[i]->large_buf)
995 resp_buf_type[i] = CIFS_LARGE_BUFFER;
996 else
997 resp_buf_type[i] = CIFS_SMALL_BUFFER;
998
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000999 rc = ses->server->ops->check_receive(midQ[i], ses->server,
1000 flags & CIFS_LOG_ERROR);
1001
1002 /* mark it so buf will not be freed by cifs_delete_mid */
1003 if ((flags & CIFS_NO_RESP) == 0)
1004 midQ[i]->resp_buf = NULL;
Ronnie Sahlbergcb5c2e62018-10-10 15:29:06 +10001005
Jeff Layton1be912d2011-01-28 07:08:28 -05001006 }
Ronnie Sahlbergcb5c2e62018-10-10 15:29:06 +10001007
1008 /*
1009 * Compounding is never used during session establish.
1010 */
1011 if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP)) {
1012 struct kvec iov = {
1013 .iov_base = resp_iov[0].iov_base,
1014 .iov_len = resp_iov[0].iov_len
1015 };
1016 smb311_update_preauth_hash(ses, &iov, 1);
1017 }
1018
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001019out:
Ronnie Sahlberg4e34feb2018-08-30 10:13:00 +10001020 /*
1021 * This will dequeue all mids. After this it is important that the
1022 * demultiplex_thread will not process any of these mids any futher.
1023 * This is prevented above by using a noop callback that will not
1024 * wake this thread except for the very last PDU.
1025 */
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -08001026 for (i = 0; i < num_rqst; i++) {
1027 if (!cancelled_mid[i])
1028 cifs_delete_mid(midQ[i]);
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -08001029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 return rc;
1032}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034int
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +10001035cifs_send_recv(const unsigned int xid, struct cifs_ses *ses,
1036 struct smb_rqst *rqst, int *resp_buf_type, const int flags,
1037 struct kvec *resp_iov)
1038{
1039 return compound_send_recv(xid, ses, flags, 1, rqst, resp_buf_type,
1040 resp_iov);
1041}
1042
1043int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001044SendReceive2(const unsigned int xid, struct cifs_ses *ses,
1045 struct kvec *iov, int n_vec, int *resp_buf_type /* ret */,
1046 const int flags, struct kvec *resp_iov)
1047{
1048 struct smb_rqst rqst;
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +11001049 struct kvec s_iov[CIFS_MAX_IOV_SIZE], *new_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001050 int rc;
1051
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +11001052 if (n_vec + 1 > CIFS_MAX_IOV_SIZE) {
Kees Cook6da2ec52018-06-12 13:55:00 -07001053 new_iov = kmalloc_array(n_vec + 1, sizeof(struct kvec),
1054 GFP_KERNEL);
Steve French117e3b72018-04-22 10:24:19 -05001055 if (!new_iov) {
1056 /* otherwise cifs_send_recv below sets resp_buf_type */
1057 *resp_buf_type = CIFS_NO_BUFFER;
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +11001058 return -ENOMEM;
Steve French117e3b72018-04-22 10:24:19 -05001059 }
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +11001060 } else
1061 new_iov = s_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001062
1063 /* 1st iov is a RFC1001 length followed by the rest of the packet */
1064 memcpy(new_iov + 1, iov, (sizeof(struct kvec) * n_vec));
1065
1066 new_iov[0].iov_base = new_iov[1].iov_base;
1067 new_iov[0].iov_len = 4;
1068 new_iov[1].iov_base += 4;
1069 new_iov[1].iov_len -= 4;
1070
1071 memset(&rqst, 0, sizeof(struct smb_rqst));
1072 rqst.rq_iov = new_iov;
1073 rqst.rq_nvec = n_vec + 1;
1074
1075 rc = cifs_send_recv(xid, ses, &rqst, resp_buf_type, flags, resp_iov);
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +11001076 if (n_vec + 1 > CIFS_MAX_IOV_SIZE)
1077 kfree(new_iov);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001078 return rc;
1079}
1080
1081int
Steve French96daf2b2011-05-27 04:34:02 +00001082SendReceive(const unsigned int xid, struct cifs_ses *ses,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001084 int *pbytes_returned, const int timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
1086 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 struct mid_q_entry *midQ;
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001088 unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
1089 struct kvec iov = { .iov_base = in_buf, .iov_len = len };
1090 struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08001091 struct cifs_credits credits = { .value = 1, .instance = 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 if (ses == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001094 cifs_dbg(VFS, "Null smb session\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 return -EIO;
1096 }
Steve French79a58d12007-07-06 22:44:50 +00001097 if (ses->server == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001098 cifs_dbg(VFS, "Null tcp session\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 return -EIO;
1100 }
1101
Steve French79a58d12007-07-06 22:44:50 +00001102 if (ses->server->tcpStatus == CifsExiting)
Steve French31ca3bc2005-04-28 22:41:11 -07001103 return -ENOENT;
1104
Steve French79a58d12007-07-06 22:44:50 +00001105 /* Ensure that we do not send more than 50 overlapping requests
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 to the same server. We may make this configurable later or
1107 use ses->maxReq */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001109 if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001110 cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001111 len);
Volker Lendecke6d9c6d52008-12-08 20:50:24 +00001112 return -EIO;
1113 }
1114
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08001115 rc = wait_for_free_request(ses->server, timeout, 0, &credits.instance);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001116 if (rc)
1117 return rc;
1118
Steve French79a58d12007-07-06 22:44:50 +00001119 /* make sure that we sign in the same order that we send on this socket
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 and avoid races inside tcp sendmsg code that could cause corruption
1121 of smb data */
1122
Jeff Layton72ca5452008-12-01 07:09:36 -05001123 mutex_lock(&ses->server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001125 rc = allocate_mid(ses, in_buf, &midQ);
1126 if (rc) {
Jeff Layton72ca5452008-12-01 07:09:36 -05001127 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001128 /* Update # of requests on wire to server */
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08001129 add_credits(ses->server, &credits, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001130 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 }
1132
Steve Frenchad009ac2005-04-28 22:41:05 -07001133 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
Volker Lendecke829049c2008-12-06 16:00:53 +01001134 if (rc) {
1135 mutex_unlock(&ses->server->srv_mutex);
1136 goto out;
1137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001139 midQ->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +00001140
1141 cifs_in_send_inc(ses->server);
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001142 rc = smb_send(ses->server, in_buf, len);
Steve French789e6662011-08-09 18:44:44 +00001143 cifs_in_send_dec(ses->server);
1144 cifs_save_when_sent(midQ);
Jeff Laytonad313cb2013-04-03 10:27:36 -04001145
1146 if (rc < 0)
1147 ses->server->sequence_number -= 2;
1148
Jeff Layton72ca5452008-12-01 07:09:36 -05001149 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001150
Steve French79a58d12007-07-06 22:44:50 +00001151 if (rc < 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001152 goto out;
1153
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001154 if (timeout == CIFS_ASYNC_OP)
Steve French133672e2007-11-13 22:41:37 +00001155 goto out;
Jeff Layton0ade6402011-01-11 07:24:02 -05001156
1157 rc = wait_for_response(ses->server, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -05001158 if (rc != 0) {
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001159 send_cancel(ses->server, &rqst, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -05001160 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001161 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
Jeff Layton1be912d2011-01-28 07:08:28 -05001162 /* no longer considered to be "in-flight" */
1163 midQ->callback = DeleteMidQEntry;
1164 spin_unlock(&GlobalMid_Lock);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08001165 add_credits(ses->server, &credits, 0);
Jeff Layton1be912d2011-01-28 07:08:28 -05001166 return rc;
1167 }
1168 spin_unlock(&GlobalMid_Lock);
1169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Jeff Layton3c1105d2011-05-22 07:09:13 -04001171 rc = cifs_sync_mid_result(midQ, ses->server);
Jeff Layton053d5032011-01-11 07:24:02 -05001172 if (rc != 0) {
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08001173 add_credits(ses->server, &credits, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 return rc;
1175 }
Steve French50c2f752007-07-13 00:33:32 +00001176
Jeff Layton2c8f9812011-05-19 16:22:52 -04001177 if (!midQ->resp_buf || !out_buf ||
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001178 midQ->mid_state != MID_RESPONSE_RECEIVED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 rc = -EIO;
Joe Perchesf96637b2013-05-04 22:12:25 -05001180 cifs_dbg(VFS, "Bad MID state?\n");
Steve French2b2bdfb2008-12-11 17:26:54 +00001181 goto out;
1182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001184 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
Jeff Layton2c8f9812011-05-19 16:22:52 -04001185 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
1186 rc = cifs_check_receive(midQ, ses->server, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001187out:
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001188 cifs_delete_mid(midQ);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08001189 add_credits(ses->server, &credits, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 return rc;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001192}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001194/* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
1195 blocking lock to return. */
1196
1197static int
Steve French96daf2b2011-05-27 04:34:02 +00001198send_lock_cancel(const unsigned int xid, struct cifs_tcon *tcon,
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001199 struct smb_hdr *in_buf,
1200 struct smb_hdr *out_buf)
1201{
1202 int bytes_returned;
Steve French96daf2b2011-05-27 04:34:02 +00001203 struct cifs_ses *ses = tcon->ses;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001204 LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
1205
1206 /* We just modify the current in_buf to change
1207 the type of lock from LOCKING_ANDX_SHARED_LOCK
1208 or LOCKING_ANDX_EXCLUSIVE_LOCK to
1209 LOCKING_ANDX_CANCEL_LOCK. */
1210
1211 pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
1212 pSMB->Timeout = 0;
Pavel Shilovsky88257362012-05-23 14:01:59 +04001213 pSMB->hdr.Mid = get_next_mid(ses->server);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001214
1215 return SendReceive(xid, ses, in_buf, out_buf,
Jeff Layton77499812011-01-11 07:24:23 -05001216 &bytes_returned, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001217}
1218
1219int
Steve French96daf2b2011-05-27 04:34:02 +00001220SendReceiveBlockingLock(const unsigned int xid, struct cifs_tcon *tcon,
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001221 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
1222 int *pbytes_returned)
1223{
1224 int rc = 0;
1225 int rstart = 0;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001226 struct mid_q_entry *midQ;
Steve French96daf2b2011-05-27 04:34:02 +00001227 struct cifs_ses *ses;
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001228 unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
1229 struct kvec iov = { .iov_base = in_buf, .iov_len = len };
1230 struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08001231 unsigned int instance;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001232
1233 if (tcon == NULL || tcon->ses == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001234 cifs_dbg(VFS, "Null smb session\n");
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001235 return -EIO;
1236 }
1237 ses = tcon->ses;
1238
Steve French79a58d12007-07-06 22:44:50 +00001239 if (ses->server == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001240 cifs_dbg(VFS, "Null tcp session\n");
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001241 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 }
1243
Steve French79a58d12007-07-06 22:44:50 +00001244 if (ses->server->tcpStatus == CifsExiting)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001245 return -ENOENT;
1246
Steve French79a58d12007-07-06 22:44:50 +00001247 /* Ensure that we do not send more than 50 overlapping requests
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001248 to the same server. We may make this configurable later or
1249 use ses->maxReq */
1250
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001251 if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001252 cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001253 len);
Volker Lendecke6d9c6d52008-12-08 20:50:24 +00001254 return -EIO;
1255 }
1256
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08001257 rc = wait_for_free_request(ses->server, CIFS_BLOCKING_OP, 0,
1258 &instance);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001259 if (rc)
1260 return rc;
1261
Steve French79a58d12007-07-06 22:44:50 +00001262 /* make sure that we sign in the same order that we send on this socket
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001263 and avoid races inside tcp sendmsg code that could cause corruption
1264 of smb data */
1265
Jeff Layton72ca5452008-12-01 07:09:36 -05001266 mutex_lock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001267
1268 rc = allocate_mid(ses, in_buf, &midQ);
1269 if (rc) {
Jeff Layton72ca5452008-12-01 07:09:36 -05001270 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001271 return rc;
1272 }
1273
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001274 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
Volker Lendecke829049c2008-12-06 16:00:53 +01001275 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001276 cifs_delete_mid(midQ);
Volker Lendecke829049c2008-12-06 16:00:53 +01001277 mutex_unlock(&ses->server->srv_mutex);
1278 return rc;
1279 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001280
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001281 midQ->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +00001282 cifs_in_send_inc(ses->server);
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001283 rc = smb_send(ses->server, in_buf, len);
Steve French789e6662011-08-09 18:44:44 +00001284 cifs_in_send_dec(ses->server);
1285 cifs_save_when_sent(midQ);
Jeff Laytonad313cb2013-04-03 10:27:36 -04001286
1287 if (rc < 0)
1288 ses->server->sequence_number -= 2;
1289
Jeff Layton72ca5452008-12-01 07:09:36 -05001290 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001291
Steve French79a58d12007-07-06 22:44:50 +00001292 if (rc < 0) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001293 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001294 return rc;
1295 }
1296
1297 /* Wait for a reply - allow signals to interrupt. */
1298 rc = wait_event_interruptible(ses->server->response_q,
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001299 (!(midQ->mid_state == MID_REQUEST_SUBMITTED)) ||
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001300 ((ses->server->tcpStatus != CifsGood) &&
1301 (ses->server->tcpStatus != CifsNew)));
1302
1303 /* Were we interrupted by a signal ? */
1304 if ((rc == -ERESTARTSYS) &&
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001305 (midQ->mid_state == MID_REQUEST_SUBMITTED) &&
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001306 ((ses->server->tcpStatus == CifsGood) ||
1307 (ses->server->tcpStatus == CifsNew))) {
1308
1309 if (in_buf->Command == SMB_COM_TRANSACTION2) {
1310 /* POSIX lock. We send a NT_CANCEL SMB to cause the
1311 blocking lock to return. */
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001312 rc = send_cancel(ses->server, &rqst, midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001313 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001314 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001315 return rc;
1316 }
1317 } else {
1318 /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
1319 to cause the blocking lock to return. */
1320
1321 rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
1322
1323 /* If we get -ENOLCK back the lock may have
1324 already been removed. Don't exit in this case. */
1325 if (rc && rc != -ENOLCK) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001326 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001327 return rc;
1328 }
1329 }
1330
Jeff Layton1be912d2011-01-28 07:08:28 -05001331 rc = wait_for_response(ses->server, midQ);
1332 if (rc) {
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001333 send_cancel(ses->server, &rqst, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -05001334 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001335 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
Jeff Layton1be912d2011-01-28 07:08:28 -05001336 /* no longer considered to be "in-flight" */
1337 midQ->callback = DeleteMidQEntry;
1338 spin_unlock(&GlobalMid_Lock);
1339 return rc;
1340 }
1341 spin_unlock(&GlobalMid_Lock);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001342 }
Jeff Layton1be912d2011-01-28 07:08:28 -05001343
1344 /* We got the response - restart system call. */
1345 rstart = 1;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001346 }
1347
Jeff Layton3c1105d2011-05-22 07:09:13 -04001348 rc = cifs_sync_mid_result(midQ, ses->server);
Jeff Layton053d5032011-01-11 07:24:02 -05001349 if (rc != 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001350 return rc;
Steve French50c2f752007-07-13 00:33:32 +00001351
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001352 /* rcvd frame is ok */
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001353 if (out_buf == NULL || midQ->mid_state != MID_RESPONSE_RECEIVED) {
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001354 rc = -EIO;
Joe Perchesf96637b2013-05-04 22:12:25 -05001355 cifs_dbg(VFS, "Bad MID state?\n");
Volker Lendecke698e96a2008-12-06 16:39:31 +01001356 goto out;
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001357 }
1358
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001359 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
Jeff Layton2c8f9812011-05-19 16:22:52 -04001360 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
1361 rc = cifs_check_receive(midQ, ses->server, 0);
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001362out:
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001363 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001364 if (rstart && rc == -EACCES)
1365 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 return rc;
1367}