blob: 169e767ff57f5736106f89e00cdfa4e5500fb3f7 [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;
116 /* commands taking longer than one second are indications that
117 something is wrong, unless it is quite a slow link or server */
Karim Eshapa4328fea2017-05-12 01:53:38 +0200118 if (time_after(now, midEntry->when_alloc + HZ)) {
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400119 if ((cifsFYI & CIFS_TIMER) && (midEntry->command != command)) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +0300120 pr_debug(" CIFS slow rsp: cmd %d mid %llu",
Steve French1047abc2005-10-11 19:58:06 -0700121 midEntry->command, midEntry->mid);
Andy Shevchenko0b456f02014-08-27 16:49:44 +0300122 pr_info(" A: 0x%lx S: 0x%lx R: 0x%lx\n",
Steve French1047abc2005-10-11 19:58:06 -0700123 now - midEntry->when_alloc,
124 now - midEntry->when_sent,
125 now - midEntry->when_received);
126 }
127 }
128#endif
Lars Persson696e4202018-06-25 14:05:25 +0200129 cifs_mid_q_entry_release(midEntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700132void
133cifs_delete_mid(struct mid_q_entry *mid)
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500134{
135 spin_lock(&GlobalMid_Lock);
136 list_del(&mid->qhead);
137 spin_unlock(&GlobalMid_Lock);
138
139 DeleteMidQEntry(mid);
140}
141
Jeff Layton6f49f462012-09-18 16:20:34 -0700142/*
143 * smb_send_kvec - send an array of kvecs to the server
144 * @server: Server to send the data to
Al Viro3ab3f2a2015-11-13 02:36:04 -0500145 * @smb_msg: Message to send
Jeff Layton6f49f462012-09-18 16:20:34 -0700146 * @sent: amount of data sent on socket is stored here
147 *
148 * Our basic "send data to server" function. Should be called with srv_mutex
149 * held. The caller is responsible for handling the results.
150 */
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500151static int
Al Viro3ab3f2a2015-11-13 02:36:04 -0500152smb_send_kvec(struct TCP_Server_Info *server, struct msghdr *smb_msg,
153 size_t *sent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 int rc = 0;
Al Viro3ab3f2a2015-11-13 02:36:04 -0500156 int retries = 0;
Steve Frenchedf1ae42008-10-29 00:47:57 +0000157 struct socket *ssocket = server->ssocket;
Steve French50c2f752007-07-13 00:33:32 +0000158
Jeff Layton6f49f462012-09-18 16:20:34 -0700159 *sent = 0;
160
Al Viro3ab3f2a2015-11-13 02:36:04 -0500161 smb_msg->msg_name = (struct sockaddr *) &server->dstaddr;
162 smb_msg->msg_namelen = sizeof(struct sockaddr);
163 smb_msg->msg_control = NULL;
164 smb_msg->msg_controllen = 0;
Jeff Layton0496e022008-12-30 12:39:16 -0500165 if (server->noblocksnd)
Al Viro3ab3f2a2015-11-13 02:36:04 -0500166 smb_msg->msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL;
Steve Frenchedf1ae42008-10-29 00:47:57 +0000167 else
Al Viro3ab3f2a2015-11-13 02:36:04 -0500168 smb_msg->msg_flags = MSG_NOSIGNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Al Viro3ab3f2a2015-11-13 02:36:04 -0500170 while (msg_data_left(smb_msg)) {
Jeff Layton6f49f462012-09-18 16:20:34 -0700171 /*
172 * If blocking send, we try 3 times, since each can block
173 * for 5 seconds. For nonblocking we have to try more
174 * but wait increasing amounts of time allowing time for
175 * socket to clear. The overall time we wait in either
176 * case to send on the socket is about 15 seconds.
177 * Similarly we wait for 15 seconds for a response from
178 * the server in SendReceive[2] for the server to send
179 * a response back for most types of requests (except
180 * SMB Write past end of file which can be slow, and
181 * blocking lock operations). NFS waits slightly longer
182 * than CIFS, but this can make it take longer for
183 * nonresponsive servers to be detected and 15 seconds
184 * is more than enough time for modern networks to
185 * send a packet. In most cases if we fail to send
186 * after the retries we will kill the socket and
187 * reconnect which may clear the network problem.
188 */
Al Viro3ab3f2a2015-11-13 02:36:04 -0500189 rc = sock_sendmsg(ssocket, smb_msg);
Jeff Laytonce6c44e2013-03-22 08:36:45 -0400190 if (rc == -EAGAIN) {
Al Viro3ab3f2a2015-11-13 02:36:04 -0500191 retries++;
192 if (retries >= 14 ||
193 (!server->noblocksnd && (retries > 2))) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500194 cifs_dbg(VFS, "sends on sock %p stuck for 15 seconds\n",
195 ssocket);
Al Viro3ab3f2a2015-11-13 02:36:04 -0500196 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
Al Viro3ab3f2a2015-11-13 02:36:04 -0500198 msleep(1 << retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 continue;
200 }
Jeff Layton6f49f462012-09-18 16:20:34 -0700201
Steve French79a58d12007-07-06 22:44:50 +0000202 if (rc < 0)
Al Viro3ab3f2a2015-11-13 02:36:04 -0500203 return rc;
Jeff Layton6f49f462012-09-18 16:20:34 -0700204
Steve French79a58d12007-07-06 22:44:50 +0000205 if (rc == 0) {
Steve French3e844692005-10-03 13:37:24 -0700206 /* should never happen, letting socket clear before
207 retrying is our only obvious option here */
Joe Perchesf96637b2013-05-04 22:12:25 -0500208 cifs_dbg(VFS, "tcp sent no data\n");
Steve French3e844692005-10-03 13:37:24 -0700209 msleep(500);
210 continue;
211 }
Jeff Layton6f49f462012-09-18 16:20:34 -0700212
Al Viro3ab3f2a2015-11-13 02:36:04 -0500213 /* send was at least partially successful */
214 *sent += rc;
215 retries = 0; /* in case we get ENOSPC on the next send */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
Al Viro3ab3f2a2015-11-13 02:36:04 -0500217 return 0;
Jeff Layton97bc00b2012-09-18 16:20:35 -0700218}
219
Paulo Alcantara35e2cc12018-06-15 10:22:44 -0300220unsigned long
Ronnie Sahlberg81f39f92018-06-28 10:47:14 +1000221smb_rqst_len(struct TCP_Server_Info *server, struct smb_rqst *rqst)
Jeff Laytona26054d2014-02-14 07:21:00 -0500222{
223 unsigned int i;
Paulo Alcantara35e2cc12018-06-15 10:22:44 -0300224 struct kvec *iov;
225 int nvec;
Jeff Laytona26054d2014-02-14 07:21:00 -0500226 unsigned long buflen = 0;
227
Ronnie Sahlberg81f39f92018-06-28 10:47:14 +1000228 if (server->vals->header_preamble_size == 0 &&
229 rqst->rq_nvec >= 2 && rqst->rq_iov[0].iov_len == 4) {
Paulo Alcantara35e2cc12018-06-15 10:22:44 -0300230 iov = &rqst->rq_iov[1];
231 nvec = rqst->rq_nvec - 1;
232 } else {
233 iov = rqst->rq_iov;
234 nvec = rqst->rq_nvec;
235 }
236
Jeff Laytona26054d2014-02-14 07:21:00 -0500237 /* total up iov array first */
Paulo Alcantara35e2cc12018-06-15 10:22:44 -0300238 for (i = 0; i < nvec; i++)
Jeff Laytona26054d2014-02-14 07:21:00 -0500239 buflen += iov[i].iov_len;
240
Long Lic06a0f22018-05-30 12:47:57 -0700241 /*
242 * Add in the page array if there is one. The caller needs to make
243 * sure rq_offset and rq_tailsz are set correctly. If a buffer of
244 * multiple pages ends at page boundary, rq_tailsz needs to be set to
245 * PAGE_SIZE.
246 */
Jeff Laytona26054d2014-02-14 07:21:00 -0500247 if (rqst->rq_npages) {
Long Lic06a0f22018-05-30 12:47:57 -0700248 if (rqst->rq_npages == 1)
249 buflen += rqst->rq_tailsz;
250 else {
251 /*
252 * If there is more than one page, calculate the
253 * buffer length based on rq_offset and rq_tailsz
254 */
255 buflen += rqst->rq_pagesz * (rqst->rq_npages - 1) -
256 rqst->rq_offset;
257 buflen += rqst->rq_tailsz;
258 }
Jeff Laytona26054d2014-02-14 07:21:00 -0500259 }
260
261 return buflen;
262}
263
Jeff Layton6f49f462012-09-18 16:20:34 -0700264static int
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000265__smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
266 struct smb_rqst *rqst)
Jeff Layton6f49f462012-09-18 16:20:34 -0700267{
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000268 int rc = 0;
269 struct kvec *iov;
270 int n_vec;
271 unsigned int send_length = 0;
272 unsigned int i, j;
Al Viro3ab3f2a2015-11-13 02:36:04 -0500273 size_t total_len = 0, sent, size;
Jeff Laytonb8eed282012-09-18 16:20:35 -0700274 struct socket *ssocket = server->ssocket;
Al Viro3ab3f2a2015-11-13 02:36:04 -0500275 struct msghdr smb_msg;
Jeff Laytonb8eed282012-09-18 16:20:35 -0700276 int val = 1;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000277 __be32 rfc1002_marker;
278
Long Li9762c2d2017-11-22 17:38:43 -0700279 if (cifs_rdma_enabled(server) && server->smbd_conn) {
Ronnie Sahlberg81f39f92018-06-28 10:47:14 +1000280 rc = smbd_send(server, rqst);
Long Li9762c2d2017-11-22 17:38:43 -0700281 goto smbd_done;
282 }
Jeff Laytonea702b82012-12-27 07:28:55 -0500283 if (ssocket == NULL)
284 return -ENOTSOCK;
285
Jeff Laytonb8eed282012-09-18 16:20:35 -0700286 /* cork the socket */
287 kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
288 (char *)&val, sizeof(val));
289
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000290 for (j = 0; j < num_rqst; j++)
Ronnie Sahlberg81f39f92018-06-28 10:47:14 +1000291 send_length += smb_rqst_len(server, &rqst[j]);
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000292 rfc1002_marker = cpu_to_be32(send_length);
293
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000294 /* Generate a rfc1002 marker for SMB2+ */
295 if (server->vals->header_preamble_size == 0) {
296 struct kvec hiov = {
297 .iov_base = &rfc1002_marker,
298 .iov_len = 4
299 };
300 iov_iter_kvec(&smb_msg.msg_iter, WRITE | ITER_KVEC, &hiov,
301 1, 4);
302 rc = smb_send_kvec(server, &smb_msg, &sent);
303 if (rc < 0)
304 goto uncork;
305
306 total_len += sent;
307 send_length += 4;
308 }
309
Paulo Alcantara662bf5b2018-06-14 17:34:08 -0300310 cifs_dbg(FYI, "Sending smb: smb_len=%u\n", send_length);
311
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000312 for (j = 0; j < num_rqst; j++) {
313 iov = rqst[j].rq_iov;
314 n_vec = rqst[j].rq_nvec;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000315
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000316 size = 0;
Paulo Alcantara662bf5b2018-06-14 17:34:08 -0300317 for (i = 0; i < n_vec; i++) {
318 dump_smb(iov[i].iov_base, iov[i].iov_len);
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000319 size += iov[i].iov_len;
Paulo Alcantara662bf5b2018-06-14 17:34:08 -0300320 }
Al Viro3ab3f2a2015-11-13 02:36:04 -0500321
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000322 iov_iter_kvec(&smb_msg.msg_iter, WRITE | ITER_KVEC,
323 iov, n_vec, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Al Viro3ab3f2a2015-11-13 02:36:04 -0500325 rc = smb_send_kvec(server, &smb_msg, &sent);
Jeff Layton97bc00b2012-09-18 16:20:35 -0700326 if (rc < 0)
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000327 goto uncork;
Jeff Layton97bc00b2012-09-18 16:20:35 -0700328
329 total_len += sent;
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000330
331 /* now walk the page array and send each page in it */
332 for (i = 0; i < rqst[j].rq_npages; i++) {
333 struct bio_vec bvec;
334
335 bvec.bv_page = rqst[j].rq_pages[i];
336 rqst_page_get_length(&rqst[j], i, &bvec.bv_len,
337 &bvec.bv_offset);
338
339 iov_iter_bvec(&smb_msg.msg_iter, WRITE | ITER_BVEC,
340 &bvec, 1, bvec.bv_len);
341 rc = smb_send_kvec(server, &smb_msg, &sent);
342 if (rc < 0)
343 break;
344
345 total_len += sent;
346 }
Jeff Layton97bc00b2012-09-18 16:20:35 -0700347 }
348
349uncork:
Jeff Laytonb8eed282012-09-18 16:20:35 -0700350 /* uncork it */
351 val = 0;
352 kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
353 (char *)&val, sizeof(val));
354
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000355 if ((total_len > 0) && (total_len != send_length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500356 cifs_dbg(FYI, "partial send (wanted=%u sent=%zu): terminating session\n",
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000357 send_length, total_len);
Jeff Layton6f49f462012-09-18 16:20:34 -0700358 /*
359 * If we have only sent part of an SMB then the next SMB could
360 * be taken as the remainder of this one. We need to kill the
361 * socket so the server throws away the partial SMB
362 */
Steve Frenchedf1ae42008-10-29 00:47:57 +0000363 server->tcpStatus = CifsNeedReconnect;
Steve Frenchbf1fdeb2018-07-30 19:23:09 -0500364 trace_smb3_partial_send_reconnect(server->CurrentMid,
365 server->hostname);
Steve Frenchedf1ae42008-10-29 00:47:57 +0000366 }
Long Li9762c2d2017-11-22 17:38:43 -0700367smbd_done:
Jeff Laytond804d412011-01-28 15:05:43 -0500368 if (rc < 0 && rc != -EINTR)
Joe Perchesf96637b2013-05-04 22:12:25 -0500369 cifs_dbg(VFS, "Error %d sending data on socket to server\n",
370 rc);
Jeff Laytond804d412011-01-28 15:05:43 -0500371 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 return rc;
375}
376
Ronnie Sahlberg1f3a8f52018-08-01 09:26:12 +1000377#define MAX_COMPOUND 5
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +1000378
Jeff Layton6f49f462012-09-18 16:20:34 -0700379static int
Ronnie Sahlberg1f3a8f52018-08-01 09:26:12 +1000380smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
381 struct smb_rqst *rqst, int flags)
Jeff Layton6f49f462012-09-18 16:20:34 -0700382{
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +1000383 struct kvec iov;
384 struct smb2_transform_hdr tr_hdr;
385 struct smb_rqst cur_rqst[MAX_COMPOUND];
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700386 int rc;
Jeff Layton6f49f462012-09-18 16:20:34 -0700387
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700388 if (!(flags & CIFS_TRANSFORM_REQ))
Ronnie Sahlberg1f3a8f52018-08-01 09:26:12 +1000389 return __smb_send_rqst(server, num_rqst, rqst);
390
391 if (num_rqst > MAX_COMPOUND - 1)
392 return -ENOMEM;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700393
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +1000394 memset(&cur_rqst[0], 0, sizeof(cur_rqst));
395 memset(&iov, 0, sizeof(iov));
396 memset(&tr_hdr, 0, sizeof(tr_hdr));
397
398 iov.iov_base = &tr_hdr;
399 iov.iov_len = sizeof(tr_hdr);
400 cur_rqst[0].rq_iov = &iov;
401 cur_rqst[0].rq_nvec = 1;
402
403 if (!server->ops->init_transform_rq) {
404 cifs_dbg(VFS, "Encryption requested but transform callback "
405 "is missing\n");
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700406 return -EIO;
407 }
408
Ronnie Sahlberg1f3a8f52018-08-01 09:26:12 +1000409 rc = server->ops->init_transform_rq(server, num_rqst + 1,
410 &cur_rqst[0], rqst);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700411 if (rc)
412 return rc;
413
Ronnie Sahlberg1f3a8f52018-08-01 09:26:12 +1000414 rc = __smb_send_rqst(server, num_rqst + 1, &cur_rqst[0]);
415 smb3_free_compound_rqst(num_rqst, &cur_rqst[1]);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700416 return rc;
Jeff Layton6f49f462012-09-18 16:20:34 -0700417}
418
Jeff Layton0496e022008-12-30 12:39:16 -0500419int
420smb_send(struct TCP_Server_Info *server, struct smb_hdr *smb_buffer,
421 unsigned int smb_buf_length)
422{
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800423 struct kvec iov[2];
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700424 struct smb_rqst rqst = { .rq_iov = iov,
425 .rq_nvec = 2 };
Jeff Layton0496e022008-12-30 12:39:16 -0500426
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800427 iov[0].iov_base = smb_buffer;
428 iov[0].iov_len = 4;
429 iov[1].iov_base = (char *)smb_buffer + 4;
430 iov[1].iov_len = smb_buf_length;
Jeff Layton0496e022008-12-30 12:39:16 -0500431
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000432 return __smb_send_rqst(server, 1, &rqst);
Jeff Layton0496e022008-12-30 12:39:16 -0500433}
434
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300435static int
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400436wait_for_free_credits(struct TCP_Server_Info *server, const int timeout,
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300437 int *credits)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000438{
Pavel Shilovsky5bc59492012-02-21 19:56:08 +0300439 int rc;
440
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300441 spin_lock(&server->req_lock);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400442 if (timeout == CIFS_ASYNC_OP) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000443 /* oplock breaks must not be held up */
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300444 server->in_flight++;
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300445 *credits -= 1;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300446 spin_unlock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000447 return 0;
448 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000449
Volker Lendecke27a97a62008-12-08 20:59:39 +0000450 while (1) {
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300451 if (*credits <= 0) {
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300452 spin_unlock(&server->req_lock);
Steve French789e6662011-08-09 18:44:44 +0000453 cifs_num_waiters_inc(server);
Pavel Shilovsky5bc59492012-02-21 19:56:08 +0300454 rc = wait_event_killable(server->request_q,
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300455 has_credits(server, credits));
Steve French789e6662011-08-09 18:44:44 +0000456 cifs_num_waiters_dec(server);
Pavel Shilovsky5bc59492012-02-21 19:56:08 +0300457 if (rc)
458 return rc;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300459 spin_lock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000460 } else {
Jeff Laytonc5797a92011-01-11 07:24:01 -0500461 if (server->tcpStatus == CifsExiting) {
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300462 spin_unlock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000463 return -ENOENT;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000464 }
Volker Lendecke27a97a62008-12-08 20:59:39 +0000465
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400466 /*
467 * Can not count locking commands against total
468 * as they are allowed to block on server.
469 */
Volker Lendecke27a97a62008-12-08 20:59:39 +0000470
471 /* update # of requests on the wire to server */
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400472 if (timeout != CIFS_BLOCKING_OP) {
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300473 *credits -= 1;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300474 server->in_flight++;
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400475 }
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300476 spin_unlock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000477 break;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000478 }
479 }
480 return 0;
481}
482
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300483static int
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400484wait_for_free_request(struct TCP_Server_Info *server, const int timeout,
485 const int optype)
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300486{
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -0500487 int *val;
488
489 val = server->ops->get_credits_field(server, optype);
490 /* Since an echo is already inflight, no need to wait to send another */
491 if (*val <= 0 && optype == CIFS_ECHO_OP)
492 return -EAGAIN;
493 return wait_for_free_credits(server, timeout, val);
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300494}
495
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400496int
497cifs_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
498 unsigned int *num, unsigned int *credits)
499{
500 *num = size;
501 *credits = 0;
502 return 0;
503}
504
Steve French96daf2b2011-05-27 04:34:02 +0000505static int allocate_mid(struct cifs_ses *ses, struct smb_hdr *in_buf,
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000506 struct mid_q_entry **ppmidQ)
507{
508 if (ses->server->tcpStatus == CifsExiting) {
509 return -ENOENT;
Volker Lendecke8fbbd362008-12-06 13:12:34 +0100510 }
511
512 if (ses->server->tcpStatus == CifsNeedReconnect) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500513 cifs_dbg(FYI, "tcp session dead - return to caller to retry\n");
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000514 return -EAGAIN;
Volker Lendecke8fbbd362008-12-06 13:12:34 +0100515 }
516
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500517 if (ses->status == CifsNew) {
Steve French79a58d12007-07-06 22:44:50 +0000518 if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
Steve Frenchad7a2922008-02-07 23:25:02 +0000519 (in_buf->Command != SMB_COM_NEGOTIATE))
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000520 return -EAGAIN;
Steve Frenchad7a2922008-02-07 23:25:02 +0000521 /* else ok - we are setting up session */
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000522 }
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500523
524 if (ses->status == CifsExiting) {
525 /* check if SMB session is bad because we are setting it up */
526 if (in_buf->Command != SMB_COM_LOGOFF_ANDX)
527 return -EAGAIN;
528 /* else ok - we are shutting down session */
529 }
530
Jeff Layton24b9b062008-12-01 07:09:34 -0500531 *ppmidQ = AllocMidQEntry(in_buf, ses->server);
Steve French26f57362007-08-30 22:09:15 +0000532 if (*ppmidQ == NULL)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000533 return -ENOMEM;
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500534 spin_lock(&GlobalMid_Lock);
535 list_add_tail(&(*ppmidQ)->qhead, &ses->server->pending_mid_q);
536 spin_unlock(&GlobalMid_Lock);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000537 return 0;
538}
539
Jeff Layton0ade6402011-01-11 07:24:02 -0500540static int
541wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *midQ)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000542{
Jeff Layton0ade6402011-01-11 07:24:02 -0500543 int error;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000544
Colin Cross5853cc22013-05-07 17:52:05 +0000545 error = wait_event_freezekillable_unsafe(server->response_q,
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400546 midQ->mid_state != MID_REQUEST_SUBMITTED);
Jeff Layton0ade6402011-01-11 07:24:02 -0500547 if (error < 0)
548 return -ERESTARTSYS;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000549
Jeff Layton0ade6402011-01-11 07:24:02 -0500550 return 0;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000551}
552
Jeff Laytonfec344e2012-09-18 16:20:35 -0700553struct mid_q_entry *
554cifs_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400555{
556 int rc;
Jeff Laytonfec344e2012-09-18 16:20:35 -0700557 struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400558 struct mid_q_entry *mid;
559
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800560 if (rqst->rq_iov[0].iov_len != 4 ||
561 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
562 return ERR_PTR(-EIO);
563
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400564 /* enable signing if server requires it */
Jeff Layton38d77c52013-05-26 07:01:00 -0400565 if (server->sign)
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400566 hdr->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
567
568 mid = AllocMidQEntry(hdr, server);
569 if (mid == NULL)
Jeff Laytonfec344e2012-09-18 16:20:35 -0700570 return ERR_PTR(-ENOMEM);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400571
Jeff Laytonfec344e2012-09-18 16:20:35 -0700572 rc = cifs_sign_rqst(rqst, server, &mid->sequence_number);
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100573 if (rc) {
574 DeleteMidQEntry(mid);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700575 return ERR_PTR(rc);
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100576 }
577
Jeff Laytonfec344e2012-09-18 16:20:35 -0700578 return mid;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400579}
Steve French133672e2007-11-13 22:41:37 +0000580
581/*
Jeff Laytona6827c12011-01-11 07:24:21 -0500582 * Send a SMB request and set the callback function in the mid to handle
583 * the result. Caller is responsible for dealing with timeouts.
584 */
585int
Jeff Laytonfec344e2012-09-18 16:20:35 -0700586cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst,
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -0800587 mid_receive_t *receive, mid_callback_t *callback,
588 mid_handle_t *handle, void *cbdata, const int flags)
Jeff Laytona6827c12011-01-11 07:24:21 -0500589{
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400590 int rc, timeout, optype;
Jeff Laytona6827c12011-01-11 07:24:21 -0500591 struct mid_q_entry *mid;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400592 unsigned int credits = 0;
Jeff Laytona6827c12011-01-11 07:24:21 -0500593
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400594 timeout = flags & CIFS_TIMEOUT_MASK;
595 optype = flags & CIFS_OP_MASK;
596
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400597 if ((flags & CIFS_HAS_CREDITS) == 0) {
598 rc = wait_for_free_request(server, timeout, optype);
599 if (rc)
600 return rc;
601 credits = 1;
602 }
Jeff Laytona6827c12011-01-11 07:24:21 -0500603
604 mutex_lock(&server->srv_mutex);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700605 mid = server->ops->setup_async_request(server, rqst);
606 if (IS_ERR(mid)) {
Jeff Laytona6827c12011-01-11 07:24:21 -0500607 mutex_unlock(&server->srv_mutex);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400608 add_credits_and_wake_if(server, credits, optype);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700609 return PTR_ERR(mid);
Jeff Laytona6827c12011-01-11 07:24:21 -0500610 }
611
Jeff Layton44d22d82011-10-19 15:29:49 -0400612 mid->receive = receive;
Jeff Laytona6827c12011-01-11 07:24:21 -0500613 mid->callback = callback;
614 mid->callback_data = cbdata;
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -0800615 mid->handle = handle;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400616 mid->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +0000617
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100618 /* put it on the pending_mid_q */
619 spin_lock(&GlobalMid_Lock);
620 list_add_tail(&mid->qhead, &server->pending_mid_q);
621 spin_unlock(&GlobalMid_Lock);
622
Long Li93d2cb62017-06-28 15:55:55 -0700623 /*
624 * Need to store the time in mid before calling I/O. For call_async,
625 * I/O response may come back and free the mid entry on another thread.
626 */
627 cifs_save_when_sent(mid);
Steve French789e6662011-08-09 18:44:44 +0000628 cifs_in_send_inc(server);
Ronnie Sahlberg1f3a8f52018-08-01 09:26:12 +1000629 rc = smb_send_rqst(server, 1, rqst, flags);
Steve French789e6662011-08-09 18:44:44 +0000630 cifs_in_send_dec(server);
Jeff Laytonad313cb2013-04-03 10:27:36 -0400631
Rabin Vincent820962d2015-12-23 07:32:41 +0100632 if (rc < 0) {
Jeff Laytonad313cb2013-04-03 10:27:36 -0400633 server->sequence_number -= 2;
Rabin Vincent820962d2015-12-23 07:32:41 +0100634 cifs_delete_mid(mid);
635 }
636
Jeff Laytona6827c12011-01-11 07:24:21 -0500637 mutex_unlock(&server->srv_mutex);
Steve French789e6662011-08-09 18:44:44 +0000638
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100639 if (rc == 0)
640 return 0;
Jeff Laytona6827c12011-01-11 07:24:21 -0500641
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400642 add_credits_and_wake_if(server, credits, optype);
Jeff Laytona6827c12011-01-11 07:24:21 -0500643 return rc;
644}
645
646/*
Steve French133672e2007-11-13 22:41:37 +0000647 *
648 * Send an SMB Request. No response info (other than return code)
649 * needs to be parsed.
650 *
651 * flags indicate the type of request buffer and how long to wait
652 * and whether to log NT STATUS code (error) before mapping it to POSIX error
653 *
654 */
655int
Steve French96daf2b2011-05-27 04:34:02 +0000656SendReceiveNoRsp(const unsigned int xid, struct cifs_ses *ses,
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400657 char *in_buf, int flags)
Steve French133672e2007-11-13 22:41:37 +0000658{
659 int rc;
660 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700661 struct kvec rsp_iov;
Steve French133672e2007-11-13 22:41:37 +0000662 int resp_buf_type;
663
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400664 iov[0].iov_base = in_buf;
665 iov[0].iov_len = get_rfc1002_length(in_buf) + 4;
Steve French133672e2007-11-13 22:41:37 +0000666 flags |= CIFS_NO_RESP;
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700667 rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Joe Perchesf96637b2013-05-04 22:12:25 -0500668 cifs_dbg(NOISY, "SendRcvNoRsp flags %d rc %d\n", flags, rc);
Steve French90c81e02008-02-12 20:32:36 +0000669
Steve French133672e2007-11-13 22:41:37 +0000670 return rc;
671}
672
Jeff Layton053d5032011-01-11 07:24:02 -0500673static int
Jeff Layton3c1105d2011-05-22 07:09:13 -0400674cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
Jeff Layton053d5032011-01-11 07:24:02 -0500675{
676 int rc = 0;
677
Joe Perchesf96637b2013-05-04 22:12:25 -0500678 cifs_dbg(FYI, "%s: cmd=%d mid=%llu state=%d\n",
679 __func__, le16_to_cpu(mid->command), mid->mid, mid->mid_state);
Jeff Layton053d5032011-01-11 07:24:02 -0500680
Jeff Layton74dd92a2011-01-11 07:24:02 -0500681 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400682 switch (mid->mid_state) {
Jeff Layton74dd92a2011-01-11 07:24:02 -0500683 case MID_RESPONSE_RECEIVED:
Jeff Layton053d5032011-01-11 07:24:02 -0500684 spin_unlock(&GlobalMid_Lock);
685 return rc;
Jeff Layton74dd92a2011-01-11 07:24:02 -0500686 case MID_RETRY_NEEDED:
687 rc = -EAGAIN;
688 break;
Jeff Layton71823ba2011-02-10 08:03:50 -0500689 case MID_RESPONSE_MALFORMED:
690 rc = -EIO;
691 break;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400692 case MID_SHUTDOWN:
693 rc = -EHOSTDOWN;
694 break;
Jeff Layton74dd92a2011-01-11 07:24:02 -0500695 default:
Jeff Layton3c1105d2011-05-22 07:09:13 -0400696 list_del_init(&mid->qhead);
Joe Perchesf96637b2013-05-04 22:12:25 -0500697 cifs_dbg(VFS, "%s: invalid mid state mid=%llu state=%d\n",
698 __func__, mid->mid, mid->mid_state);
Jeff Layton74dd92a2011-01-11 07:24:02 -0500699 rc = -EIO;
Jeff Layton053d5032011-01-11 07:24:02 -0500700 }
701 spin_unlock(&GlobalMid_Lock);
702
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500703 DeleteMidQEntry(mid);
Jeff Layton053d5032011-01-11 07:24:02 -0500704 return rc;
705}
706
Jeff Layton121b0462012-05-15 12:21:10 -0400707static inline int
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800708send_cancel(struct TCP_Server_Info *server, struct smb_rqst *rqst,
709 struct mid_q_entry *mid)
Jeff Layton76dcc262011-01-11 07:24:24 -0500710{
Jeff Layton121b0462012-05-15 12:21:10 -0400711 return server->ops->send_cancel ?
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800712 server->ops->send_cancel(server, rqst, mid) : 0;
Jeff Layton76dcc262011-01-11 07:24:24 -0500713}
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715int
Jeff Layton2c8f9812011-05-19 16:22:52 -0400716cifs_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
717 bool log_error)
718{
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400719 unsigned int len = get_rfc1002_length(mid->resp_buf) + 4;
Jeff Layton826a95e2011-10-11 06:41:32 -0400720
721 dump_smb(mid->resp_buf, min_t(u32, 92, len));
Jeff Layton2c8f9812011-05-19 16:22:52 -0400722
723 /* convert the length into a more usable form */
Jeff Layton38d77c52013-05-26 07:01:00 -0400724 if (server->sign) {
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800725 struct kvec iov[2];
Steve French985e4ff02012-08-03 09:42:45 -0500726 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800727 struct smb_rqst rqst = { .rq_iov = iov,
728 .rq_nvec = 2 };
Jeff Layton826a95e2011-10-11 06:41:32 -0400729
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800730 iov[0].iov_base = mid->resp_buf;
731 iov[0].iov_len = 4;
732 iov[1].iov_base = (char *)mid->resp_buf + 4;
733 iov[1].iov_len = len - 4;
Jeff Layton2c8f9812011-05-19 16:22:52 -0400734 /* FIXME: add code to kill session */
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700735 rc = cifs_verify_signature(&rqst, server,
Jeff Layton0124cc42013-04-03 11:55:03 -0400736 mid->sequence_number);
Steve French985e4ff02012-08-03 09:42:45 -0500737 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500738 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
739 rc);
Jeff Layton2c8f9812011-05-19 16:22:52 -0400740 }
741
742 /* BB special case reconnect tid and uid here? */
743 return map_smb_to_linux_error(mid->resp_buf, log_error);
744}
745
Jeff Laytonfec344e2012-09-18 16:20:35 -0700746struct mid_q_entry *
747cifs_setup_request(struct cifs_ses *ses, struct smb_rqst *rqst)
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400748{
749 int rc;
Jeff Laytonfec344e2012-09-18 16:20:35 -0700750 struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400751 struct mid_q_entry *mid;
752
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800753 if (rqst->rq_iov[0].iov_len != 4 ||
754 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
755 return ERR_PTR(-EIO);
756
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400757 rc = allocate_mid(ses, hdr, &mid);
758 if (rc)
Jeff Laytonfec344e2012-09-18 16:20:35 -0700759 return ERR_PTR(rc);
760 rc = cifs_sign_rqst(rqst, ses->server, &mid->sequence_number);
761 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700762 cifs_delete_mid(mid);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700763 return ERR_PTR(rc);
764 }
765 return mid;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400766}
767
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800768int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800769cifs_send_recv(const unsigned int xid, struct cifs_ses *ses,
770 struct smb_rqst *rqst, int *resp_buf_type, const int flags,
771 struct kvec *resp_iov)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
773 int rc = 0;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400774 int timeout, optype;
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500775 struct mid_q_entry *midQ;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400776 unsigned int credits = 1;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800777 char *buf;
Steve French50c2f752007-07-13 00:33:32 +0000778
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400779 timeout = flags & CIFS_TIMEOUT_MASK;
780 optype = flags & CIFS_OP_MASK;
Steve French133672e2007-11-13 22:41:37 +0000781
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400782 *resp_buf_type = CIFS_NO_BUFFER; /* no response buf yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Steve French4b8f9302006-02-26 16:41:18 +0000784 if ((ses == NULL) || (ses->server == NULL)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500785 cifs_dbg(VFS, "Null session\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return -EIO;
787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700789 if (ses->server->tcpStatus == CifsExiting)
Steve French31ca3bc2005-04-28 22:41:11 -0700790 return -ENOENT;
791
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400792 /*
793 * Ensure that we do not send more than 50 overlapping requests
794 * to the same server. We may make this configurable later or
795 * use ses->maxReq.
796 */
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400797 rc = wait_for_free_request(ses->server, timeout, optype);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700798 if (rc)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000799 return rc;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000800
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400801 /*
802 * Make sure that we sign in the same order that we send on this socket
803 * and avoid races inside tcp sendmsg code that could cause corruption
804 * of smb data.
805 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Jeff Layton72ca5452008-12-01 07:09:36 -0500807 mutex_lock(&ses->server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800809 midQ = ses->server->ops->setup_request(ses, rqst);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700810 if (IS_ERR(midQ)) {
Jeff Layton72ca5452008-12-01 07:09:36 -0500811 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000812 /* Update # of requests on wire to server */
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400813 add_credits(ses->server, 1, optype);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700814 return PTR_ERR(midQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400817 midQ->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +0000818 cifs_in_send_inc(ses->server);
Ronnie Sahlberg1f3a8f52018-08-01 09:26:12 +1000819 rc = smb_send_rqst(ses->server, 1, rqst, flags);
Steve French789e6662011-08-09 18:44:44 +0000820 cifs_in_send_dec(ses->server);
821 cifs_save_when_sent(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000822
Jeff Laytonad313cb2013-04-03 10:27:36 -0400823 if (rc < 0)
824 ses->server->sequence_number -= 2;
Jeff Layton72ca5452008-12-01 07:09:36 -0500825 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000826
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700827 if (rc < 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000828 goto out;
Steve French4b8f9302006-02-26 16:41:18 +0000829
Steve French0d5ec282018-04-22 19:51:22 -0500830 if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP))
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000831 smb311_update_preauth_hash(ses, rqst->rq_iov,
832 rqst->rq_nvec);
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100833
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700834 if (timeout == CIFS_ASYNC_OP)
Steve French133672e2007-11-13 22:41:37 +0000835 goto out;
Jeff Layton0ade6402011-01-11 07:24:02 -0500836
837 rc = wait_for_response(ses->server, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -0500838 if (rc != 0) {
Sachin Prabhu38bd4902017-03-03 15:41:38 -0800839 cifs_dbg(FYI, "Cancelling wait for mid %llu\n", midQ->mid);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800840 send_cancel(ses->server, rqst, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -0500841 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400842 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
Sachin Prabhu38bd4902017-03-03 15:41:38 -0800843 midQ->mid_flags |= MID_WAIT_CANCELLED;
Jeff Layton1be912d2011-01-28 07:08:28 -0500844 midQ->callback = DeleteMidQEntry;
845 spin_unlock(&GlobalMid_Lock);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400846 add_credits(ses->server, 1, optype);
Jeff Layton1be912d2011-01-28 07:08:28 -0500847 return rc;
848 }
849 spin_unlock(&GlobalMid_Lock);
850 }
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500851
Jeff Layton3c1105d2011-05-22 07:09:13 -0400852 rc = cifs_sync_mid_result(midQ, ses->server);
Jeff Layton053d5032011-01-11 07:24:02 -0500853 if (rc != 0) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400854 add_credits(ses->server, 1, optype);
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500855 return rc;
856 }
Steve French50c2f752007-07-13 00:33:32 +0000857
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400858 if (!midQ->resp_buf || midQ->mid_state != MID_RESPONSE_RECEIVED) {
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500859 rc = -EIO;
Joe Perchesf96637b2013-05-04 22:12:25 -0500860 cifs_dbg(FYI, "Bad MID state?\n");
Steve French2b2bdfb2008-12-11 17:26:54 +0000861 goto out;
862 }
Steve French84afc292005-12-02 13:32:45 -0800863
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400864 buf = (char *)midQ->resp_buf;
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700865 resp_iov->iov_base = buf;
Ronnie Sahlberge19b2bc2018-04-09 18:06:28 +1000866 resp_iov->iov_len = midQ->resp_buf_size +
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100867 ses->server->vals->header_preamble_size;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400868 if (midQ->large_buf)
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400869 *resp_buf_type = CIFS_LARGE_BUFFER;
Jeff Layton2c8f9812011-05-19 16:22:52 -0400870 else
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400871 *resp_buf_type = CIFS_SMALL_BUFFER;
872
Steve French0d5ec282018-04-22 19:51:22 -0500873 if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP)) {
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100874 struct kvec iov = {
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000875 .iov_base = resp_iov->iov_base,
876 .iov_len = resp_iov->iov_len
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100877 };
878 smb311_update_preauth_hash(ses, &iov, 1);
879 }
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100880
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400881 credits = ses->server->ops->get_credits(midQ);
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500882
Pavel Shilovsky082d0642012-05-17 12:18:21 +0400883 rc = ses->server->ops->check_receive(midQ, ses->server,
884 flags & CIFS_LOG_ERROR);
Steve French2b2bdfb2008-12-11 17:26:54 +0000885
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700886 /* mark it so buf will not be freed by cifs_delete_mid */
Jeff Layton2c8f9812011-05-19 16:22:52 -0400887 if ((flags & CIFS_NO_RESP) == 0)
888 midQ->resp_buf = NULL;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000889out:
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700890 cifs_delete_mid(midQ);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400891 add_credits(ses->server, credits, optype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 return rc;
894}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800897SendReceive2(const unsigned int xid, struct cifs_ses *ses,
898 struct kvec *iov, int n_vec, int *resp_buf_type /* ret */,
899 const int flags, struct kvec *resp_iov)
900{
901 struct smb_rqst rqst;
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +1100902 struct kvec s_iov[CIFS_MAX_IOV_SIZE], *new_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800903 int rc;
904
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +1100905 if (n_vec + 1 > CIFS_MAX_IOV_SIZE) {
Kees Cook6da2ec52018-06-12 13:55:00 -0700906 new_iov = kmalloc_array(n_vec + 1, sizeof(struct kvec),
907 GFP_KERNEL);
Steve French117e3b72018-04-22 10:24:19 -0500908 if (!new_iov) {
909 /* otherwise cifs_send_recv below sets resp_buf_type */
910 *resp_buf_type = CIFS_NO_BUFFER;
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +1100911 return -ENOMEM;
Steve French117e3b72018-04-22 10:24:19 -0500912 }
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +1100913 } else
914 new_iov = s_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800915
916 /* 1st iov is a RFC1001 length followed by the rest of the packet */
917 memcpy(new_iov + 1, iov, (sizeof(struct kvec) * n_vec));
918
919 new_iov[0].iov_base = new_iov[1].iov_base;
920 new_iov[0].iov_len = 4;
921 new_iov[1].iov_base += 4;
922 new_iov[1].iov_len -= 4;
923
924 memset(&rqst, 0, sizeof(struct smb_rqst));
925 rqst.rq_iov = new_iov;
926 rqst.rq_nvec = n_vec + 1;
927
928 rc = cifs_send_recv(xid, ses, &rqst, resp_buf_type, flags, resp_iov);
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +1100929 if (n_vec + 1 > CIFS_MAX_IOV_SIZE)
930 kfree(new_iov);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800931 return rc;
932}
933
934int
Steve French96daf2b2011-05-27 04:34:02 +0000935SendReceive(const unsigned int xid, struct cifs_ses *ses,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400937 int *pbytes_returned, const int timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
939 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 struct mid_q_entry *midQ;
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800941 unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
942 struct kvec iov = { .iov_base = in_buf, .iov_len = len };
943 struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 if (ses == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500946 cifs_dbg(VFS, "Null smb session\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 return -EIO;
948 }
Steve French79a58d12007-07-06 22:44:50 +0000949 if (ses->server == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500950 cifs_dbg(VFS, "Null tcp session\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 return -EIO;
952 }
953
Steve French79a58d12007-07-06 22:44:50 +0000954 if (ses->server->tcpStatus == CifsExiting)
Steve French31ca3bc2005-04-28 22:41:11 -0700955 return -ENOENT;
956
Steve French79a58d12007-07-06 22:44:50 +0000957 /* Ensure that we do not send more than 50 overlapping requests
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 to the same server. We may make this configurable later or
959 use ses->maxReq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800961 if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500962 cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800963 len);
Volker Lendecke6d9c6d52008-12-08 20:50:24 +0000964 return -EIO;
965 }
966
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400967 rc = wait_for_free_request(ses->server, timeout, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000968 if (rc)
969 return rc;
970
Steve French79a58d12007-07-06 22:44:50 +0000971 /* make sure that we sign in the same order that we send on this socket
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 and avoid races inside tcp sendmsg code that could cause corruption
973 of smb data */
974
Jeff Layton72ca5452008-12-01 07:09:36 -0500975 mutex_lock(&ses->server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000977 rc = allocate_mid(ses, in_buf, &midQ);
978 if (rc) {
Jeff Layton72ca5452008-12-01 07:09:36 -0500979 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000980 /* Update # of requests on wire to server */
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400981 add_credits(ses->server, 1, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000982 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
984
Steve Frenchad009ac2005-04-28 22:41:05 -0700985 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
Volker Lendecke829049c2008-12-06 16:00:53 +0100986 if (rc) {
987 mutex_unlock(&ses->server->srv_mutex);
988 goto out;
989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400991 midQ->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +0000992
993 cifs_in_send_inc(ses->server);
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800994 rc = smb_send(ses->server, in_buf, len);
Steve French789e6662011-08-09 18:44:44 +0000995 cifs_in_send_dec(ses->server);
996 cifs_save_when_sent(midQ);
Jeff Laytonad313cb2013-04-03 10:27:36 -0400997
998 if (rc < 0)
999 ses->server->sequence_number -= 2;
1000
Jeff Layton72ca5452008-12-01 07:09:36 -05001001 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001002
Steve French79a58d12007-07-06 22:44:50 +00001003 if (rc < 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001004 goto out;
1005
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001006 if (timeout == CIFS_ASYNC_OP)
Steve French133672e2007-11-13 22:41:37 +00001007 goto out;
Jeff Layton0ade6402011-01-11 07:24:02 -05001008
1009 rc = wait_for_response(ses->server, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -05001010 if (rc != 0) {
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001011 send_cancel(ses->server, &rqst, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -05001012 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001013 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
Jeff Layton1be912d2011-01-28 07:08:28 -05001014 /* no longer considered to be "in-flight" */
1015 midQ->callback = DeleteMidQEntry;
1016 spin_unlock(&GlobalMid_Lock);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001017 add_credits(ses->server, 1, 0);
Jeff Layton1be912d2011-01-28 07:08:28 -05001018 return rc;
1019 }
1020 spin_unlock(&GlobalMid_Lock);
1021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Jeff Layton3c1105d2011-05-22 07:09:13 -04001023 rc = cifs_sync_mid_result(midQ, ses->server);
Jeff Layton053d5032011-01-11 07:24:02 -05001024 if (rc != 0) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001025 add_credits(ses->server, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return rc;
1027 }
Steve French50c2f752007-07-13 00:33:32 +00001028
Jeff Layton2c8f9812011-05-19 16:22:52 -04001029 if (!midQ->resp_buf || !out_buf ||
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001030 midQ->mid_state != MID_RESPONSE_RECEIVED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 rc = -EIO;
Joe Perchesf96637b2013-05-04 22:12:25 -05001032 cifs_dbg(VFS, "Bad MID state?\n");
Steve French2b2bdfb2008-12-11 17:26:54 +00001033 goto out;
1034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001036 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
Jeff Layton2c8f9812011-05-19 16:22:52 -04001037 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
1038 rc = cifs_check_receive(midQ, ses->server, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001039out:
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001040 cifs_delete_mid(midQ);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001041 add_credits(ses->server, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 return rc;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001044}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001046/* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
1047 blocking lock to return. */
1048
1049static int
Steve French96daf2b2011-05-27 04:34:02 +00001050send_lock_cancel(const unsigned int xid, struct cifs_tcon *tcon,
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001051 struct smb_hdr *in_buf,
1052 struct smb_hdr *out_buf)
1053{
1054 int bytes_returned;
Steve French96daf2b2011-05-27 04:34:02 +00001055 struct cifs_ses *ses = tcon->ses;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001056 LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
1057
1058 /* We just modify the current in_buf to change
1059 the type of lock from LOCKING_ANDX_SHARED_LOCK
1060 or LOCKING_ANDX_EXCLUSIVE_LOCK to
1061 LOCKING_ANDX_CANCEL_LOCK. */
1062
1063 pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
1064 pSMB->Timeout = 0;
Pavel Shilovsky88257362012-05-23 14:01:59 +04001065 pSMB->hdr.Mid = get_next_mid(ses->server);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001066
1067 return SendReceive(xid, ses, in_buf, out_buf,
Jeff Layton77499812011-01-11 07:24:23 -05001068 &bytes_returned, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001069}
1070
1071int
Steve French96daf2b2011-05-27 04:34:02 +00001072SendReceiveBlockingLock(const unsigned int xid, struct cifs_tcon *tcon,
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001073 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
1074 int *pbytes_returned)
1075{
1076 int rc = 0;
1077 int rstart = 0;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001078 struct mid_q_entry *midQ;
Steve French96daf2b2011-05-27 04:34:02 +00001079 struct cifs_ses *ses;
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001080 unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
1081 struct kvec iov = { .iov_base = in_buf, .iov_len = len };
1082 struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001083
1084 if (tcon == NULL || tcon->ses == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001085 cifs_dbg(VFS, "Null smb session\n");
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001086 return -EIO;
1087 }
1088 ses = tcon->ses;
1089
Steve French79a58d12007-07-06 22:44:50 +00001090 if (ses->server == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001091 cifs_dbg(VFS, "Null tcp session\n");
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001092 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 }
1094
Steve French79a58d12007-07-06 22:44:50 +00001095 if (ses->server->tcpStatus == CifsExiting)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001096 return -ENOENT;
1097
Steve French79a58d12007-07-06 22:44:50 +00001098 /* Ensure that we do not send more than 50 overlapping requests
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001099 to the same server. We may make this configurable later or
1100 use ses->maxReq */
1101
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001102 if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001103 cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001104 len);
Volker Lendecke6d9c6d52008-12-08 20:50:24 +00001105 return -EIO;
1106 }
1107
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001108 rc = wait_for_free_request(ses->server, CIFS_BLOCKING_OP, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001109 if (rc)
1110 return rc;
1111
Steve French79a58d12007-07-06 22:44:50 +00001112 /* make sure that we sign in the same order that we send on this socket
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001113 and avoid races inside tcp sendmsg code that could cause corruption
1114 of smb data */
1115
Jeff Layton72ca5452008-12-01 07:09:36 -05001116 mutex_lock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001117
1118 rc = allocate_mid(ses, in_buf, &midQ);
1119 if (rc) {
Jeff Layton72ca5452008-12-01 07:09:36 -05001120 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001121 return rc;
1122 }
1123
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001124 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
Volker Lendecke829049c2008-12-06 16:00:53 +01001125 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001126 cifs_delete_mid(midQ);
Volker Lendecke829049c2008-12-06 16:00:53 +01001127 mutex_unlock(&ses->server->srv_mutex);
1128 return rc;
1129 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001130
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001131 midQ->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +00001132 cifs_in_send_inc(ses->server);
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001133 rc = smb_send(ses->server, in_buf, len);
Steve French789e6662011-08-09 18:44:44 +00001134 cifs_in_send_dec(ses->server);
1135 cifs_save_when_sent(midQ);
Jeff Laytonad313cb2013-04-03 10:27:36 -04001136
1137 if (rc < 0)
1138 ses->server->sequence_number -= 2;
1139
Jeff Layton72ca5452008-12-01 07:09:36 -05001140 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001141
Steve French79a58d12007-07-06 22:44:50 +00001142 if (rc < 0) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001143 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001144 return rc;
1145 }
1146
1147 /* Wait for a reply - allow signals to interrupt. */
1148 rc = wait_event_interruptible(ses->server->response_q,
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001149 (!(midQ->mid_state == MID_REQUEST_SUBMITTED)) ||
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001150 ((ses->server->tcpStatus != CifsGood) &&
1151 (ses->server->tcpStatus != CifsNew)));
1152
1153 /* Were we interrupted by a signal ? */
1154 if ((rc == -ERESTARTSYS) &&
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001155 (midQ->mid_state == MID_REQUEST_SUBMITTED) &&
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001156 ((ses->server->tcpStatus == CifsGood) ||
1157 (ses->server->tcpStatus == CifsNew))) {
1158
1159 if (in_buf->Command == SMB_COM_TRANSACTION2) {
1160 /* POSIX lock. We send a NT_CANCEL SMB to cause the
1161 blocking lock to return. */
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001162 rc = send_cancel(ses->server, &rqst, midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001163 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001164 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001165 return rc;
1166 }
1167 } else {
1168 /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
1169 to cause the blocking lock to return. */
1170
1171 rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
1172
1173 /* If we get -ENOLCK back the lock may have
1174 already been removed. Don't exit in this case. */
1175 if (rc && rc != -ENOLCK) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001176 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001177 return rc;
1178 }
1179 }
1180
Jeff Layton1be912d2011-01-28 07:08:28 -05001181 rc = wait_for_response(ses->server, midQ);
1182 if (rc) {
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001183 send_cancel(ses->server, &rqst, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -05001184 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001185 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
Jeff Layton1be912d2011-01-28 07:08:28 -05001186 /* no longer considered to be "in-flight" */
1187 midQ->callback = DeleteMidQEntry;
1188 spin_unlock(&GlobalMid_Lock);
1189 return rc;
1190 }
1191 spin_unlock(&GlobalMid_Lock);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001192 }
Jeff Layton1be912d2011-01-28 07:08:28 -05001193
1194 /* We got the response - restart system call. */
1195 rstart = 1;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001196 }
1197
Jeff Layton3c1105d2011-05-22 07:09:13 -04001198 rc = cifs_sync_mid_result(midQ, ses->server);
Jeff Layton053d5032011-01-11 07:24:02 -05001199 if (rc != 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001200 return rc;
Steve French50c2f752007-07-13 00:33:32 +00001201
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001202 /* rcvd frame is ok */
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001203 if (out_buf == NULL || midQ->mid_state != MID_RESPONSE_RECEIVED) {
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001204 rc = -EIO;
Joe Perchesf96637b2013-05-04 22:12:25 -05001205 cifs_dbg(VFS, "Bad MID state?\n");
Volker Lendecke698e96a2008-12-06 16:39:31 +01001206 goto out;
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001207 }
1208
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001209 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
Jeff Layton2c8f9812011-05-19 16:22:52 -04001210 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
1211 rc = cifs_check_receive(midQ, ses->server, 0);
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001212out:
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001213 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001214 if (rstart && rc == -EACCES)
1215 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 return rc;
1217}