blob: a3ea42a4cb98d1cd1a2f91f033e9b53980cb0b9d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/transport.c
3 *
Steve Frenchad7a2922008-02-07 23:25:02 +00004 * Copyright (C) International Business Machines Corp., 2002,2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Author(s): Steve French (sfrench@us.ibm.com)
Steve French14a441a2b2006-07-16 04:32:51 +00006 * Jeremy Allison (jra@samba.org) 2006.
Steve French79a58d12007-07-06 22:44:50 +00007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published
10 * by the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
16 * the GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this library; if not, write to the Free Software
Steve French79a58d12007-07-06 22:44:50 +000020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 */
22
23#include <linux/fs.h>
24#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/wait.h>
27#include <linux/net.h>
28#include <linux/delay.h>
Jeff Laytonf06ac722011-10-19 15:30:40 -040029#include <linux/freezer.h>
Jeff Laytonb8eed282012-09-18 16:20:35 -070030#include <linux/tcp.h>
Christoph Hellwig2f8b5442016-11-01 07:40:13 -060031#include <linux/bvec.h>
Jeff Layton97bc00b2012-09-18 16:20:35 -070032#include <linux/highmem.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080033#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/processor.h>
35#include <linux/mempool.h>
36#include "cifspdu.h"
37#include "cifsglob.h"
38#include "cifsproto.h"
39#include "cifs_debug.h"
Aurelien Aptel8bd68c62018-02-16 19:19:29 +010040#include "smb2proto.h"
Long Li9762c2d2017-11-22 17:38:43 -070041#include "smbdirect.h"
Steve French50c2f752007-07-13 00:33:32 +000042
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +110043/* Max number of iovectors we can use off the stack when sending requests. */
44#define CIFS_MAX_IOV_SIZE 8
45
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +040046void
47cifs_wake_up_task(struct mid_q_entry *mid)
Jeff Layton2b84a36c2011-01-11 07:24:21 -050048{
49 wake_up_process(mid->callback_data);
50}
51
Jeff Laytona6827c12011-01-11 07:24:21 -050052struct mid_q_entry *
Jeff Layton24b9b062008-12-01 07:09:34 -050053AllocMidQEntry(const struct smb_hdr *smb_buffer, struct TCP_Server_Info *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55 struct mid_q_entry *temp;
56
Jeff Layton24b9b062008-12-01 07:09:34 -050057 if (server == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -050058 cifs_dbg(VFS, "Null TCP session in AllocMidQEntry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 return NULL;
60 }
Steve French50c2f752007-07-13 00:33:32 +000061
Pekka Enberg232087c2008-09-15 13:22:54 +030062 temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
NeilBrowna6f74e82017-04-10 12:08:53 +100063 memset(temp, 0, sizeof(struct mid_q_entry));
64 temp->mid = get_mid(smb_buffer);
65 temp->pid = current->pid;
66 temp->command = cpu_to_le16(smb_buffer->Command);
67 cifs_dbg(FYI, "For smb_command %d\n", smb_buffer->Command);
Steve French1047abc2005-10-11 19:58:06 -070068 /* do_gettimeofday(&temp->when_sent);*/ /* easier to use jiffies */
NeilBrowna6f74e82017-04-10 12:08:53 +100069 /* when mid allocated can be before when sent */
70 temp->when_alloc = jiffies;
71 temp->server = server;
Jeff Layton2b84a36c2011-01-11 07:24:21 -050072
NeilBrowna6f74e82017-04-10 12:08:53 +100073 /*
74 * The default is for the mid to be synchronous, so the
75 * default callback just wakes up the current task.
76 */
77 temp->callback = cifs_wake_up_task;
78 temp->callback_data = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 atomic_inc(&midCount);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -040081 temp->mid_state = MID_REQUEST_ALLOCATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 return temp;
83}
84
Jeff Layton766fdbb2011-01-11 07:24:21 -050085void
Linus Torvalds1da177e2005-04-16 15:20:36 -070086DeleteMidQEntry(struct mid_q_entry *midEntry)
87{
Steve French1047abc2005-10-11 19:58:06 -070088#ifdef CONFIG_CIFS_STATS2
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +040089 __le16 command = midEntry->server->vals->lock_cmd;
Steve French1047abc2005-10-11 19:58:06 -070090 unsigned long now;
91#endif
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -040092 midEntry->mid_state = MID_FREE;
Jeff Layton80975312011-01-11 07:24:02 -050093 atomic_dec(&midCount);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -040094 if (midEntry->large_buf)
Steve Frenchb8643e12005-04-28 22:41:07 -070095 cifs_buf_release(midEntry->resp_buf);
96 else
97 cifs_small_buf_release(midEntry->resp_buf);
Steve French1047abc2005-10-11 19:58:06 -070098#ifdef CONFIG_CIFS_STATS2
99 now = jiffies;
100 /* commands taking longer than one second are indications that
101 something is wrong, unless it is quite a slow link or server */
Karim Eshapa4328fea2017-05-12 01:53:38 +0200102 if (time_after(now, midEntry->when_alloc + HZ)) {
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400103 if ((cifsFYI & CIFS_TIMER) && (midEntry->command != command)) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +0300104 pr_debug(" CIFS slow rsp: cmd %d mid %llu",
Steve French1047abc2005-10-11 19:58:06 -0700105 midEntry->command, midEntry->mid);
Andy Shevchenko0b456f02014-08-27 16:49:44 +0300106 pr_info(" A: 0x%lx S: 0x%lx R: 0x%lx\n",
Steve French1047abc2005-10-11 19:58:06 -0700107 now - midEntry->when_alloc,
108 now - midEntry->when_sent,
109 now - midEntry->when_received);
110 }
111 }
112#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 mempool_free(midEntry, cifs_mid_poolp);
114}
115
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700116void
117cifs_delete_mid(struct mid_q_entry *mid)
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500118{
119 spin_lock(&GlobalMid_Lock);
120 list_del(&mid->qhead);
121 spin_unlock(&GlobalMid_Lock);
122
123 DeleteMidQEntry(mid);
124}
125
Jeff Layton6f49f462012-09-18 16:20:34 -0700126/*
127 * smb_send_kvec - send an array of kvecs to the server
128 * @server: Server to send the data to
Al Viro3ab3f2a2015-11-13 02:36:04 -0500129 * @smb_msg: Message to send
Jeff Layton6f49f462012-09-18 16:20:34 -0700130 * @sent: amount of data sent on socket is stored here
131 *
132 * Our basic "send data to server" function. Should be called with srv_mutex
133 * held. The caller is responsible for handling the results.
134 */
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500135static int
Al Viro3ab3f2a2015-11-13 02:36:04 -0500136smb_send_kvec(struct TCP_Server_Info *server, struct msghdr *smb_msg,
137 size_t *sent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 int rc = 0;
Al Viro3ab3f2a2015-11-13 02:36:04 -0500140 int retries = 0;
Steve Frenchedf1ae42008-10-29 00:47:57 +0000141 struct socket *ssocket = server->ssocket;
Steve French50c2f752007-07-13 00:33:32 +0000142
Jeff Layton6f49f462012-09-18 16:20:34 -0700143 *sent = 0;
144
Al Viro3ab3f2a2015-11-13 02:36:04 -0500145 smb_msg->msg_name = (struct sockaddr *) &server->dstaddr;
146 smb_msg->msg_namelen = sizeof(struct sockaddr);
147 smb_msg->msg_control = NULL;
148 smb_msg->msg_controllen = 0;
Jeff Layton0496e022008-12-30 12:39:16 -0500149 if (server->noblocksnd)
Al Viro3ab3f2a2015-11-13 02:36:04 -0500150 smb_msg->msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL;
Steve Frenchedf1ae42008-10-29 00:47:57 +0000151 else
Al Viro3ab3f2a2015-11-13 02:36:04 -0500152 smb_msg->msg_flags = MSG_NOSIGNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Al Viro3ab3f2a2015-11-13 02:36:04 -0500154 while (msg_data_left(smb_msg)) {
Jeff Layton6f49f462012-09-18 16:20:34 -0700155 /*
156 * If blocking send, we try 3 times, since each can block
157 * for 5 seconds. For nonblocking we have to try more
158 * but wait increasing amounts of time allowing time for
159 * socket to clear. The overall time we wait in either
160 * case to send on the socket is about 15 seconds.
161 * Similarly we wait for 15 seconds for a response from
162 * the server in SendReceive[2] for the server to send
163 * a response back for most types of requests (except
164 * SMB Write past end of file which can be slow, and
165 * blocking lock operations). NFS waits slightly longer
166 * than CIFS, but this can make it take longer for
167 * nonresponsive servers to be detected and 15 seconds
168 * is more than enough time for modern networks to
169 * send a packet. In most cases if we fail to send
170 * after the retries we will kill the socket and
171 * reconnect which may clear the network problem.
172 */
Al Viro3ab3f2a2015-11-13 02:36:04 -0500173 rc = sock_sendmsg(ssocket, smb_msg);
Jeff Laytonce6c44e2013-03-22 08:36:45 -0400174 if (rc == -EAGAIN) {
Al Viro3ab3f2a2015-11-13 02:36:04 -0500175 retries++;
176 if (retries >= 14 ||
177 (!server->noblocksnd && (retries > 2))) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500178 cifs_dbg(VFS, "sends on sock %p stuck for 15 seconds\n",
179 ssocket);
Al Viro3ab3f2a2015-11-13 02:36:04 -0500180 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
Al Viro3ab3f2a2015-11-13 02:36:04 -0500182 msleep(1 << retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 continue;
184 }
Jeff Layton6f49f462012-09-18 16:20:34 -0700185
Steve French79a58d12007-07-06 22:44:50 +0000186 if (rc < 0)
Al Viro3ab3f2a2015-11-13 02:36:04 -0500187 return rc;
Jeff Layton6f49f462012-09-18 16:20:34 -0700188
Steve French79a58d12007-07-06 22:44:50 +0000189 if (rc == 0) {
Steve French3e844692005-10-03 13:37:24 -0700190 /* should never happen, letting socket clear before
191 retrying is our only obvious option here */
Joe Perchesf96637b2013-05-04 22:12:25 -0500192 cifs_dbg(VFS, "tcp sent no data\n");
Steve French3e844692005-10-03 13:37:24 -0700193 msleep(500);
194 continue;
195 }
Jeff Layton6f49f462012-09-18 16:20:34 -0700196
Al Viro3ab3f2a2015-11-13 02:36:04 -0500197 /* send was at least partially successful */
198 *sent += rc;
199 retries = 0; /* in case we get ENOSPC on the next send */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
Al Viro3ab3f2a2015-11-13 02:36:04 -0500201 return 0;
Jeff Layton97bc00b2012-09-18 16:20:35 -0700202}
203
Jeff Laytona26054d2014-02-14 07:21:00 -0500204static unsigned long
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000205smb2_rqst_len(struct smb_rqst *rqst)
Jeff Laytona26054d2014-02-14 07:21:00 -0500206{
207 unsigned int i;
208 struct kvec *iov = rqst->rq_iov;
209 unsigned long buflen = 0;
210
211 /* total up iov array first */
212 for (i = 0; i < rqst->rq_nvec; i++)
213 buflen += iov[i].iov_len;
214
Long Lic06a0f22018-05-30 12:47:57 -0700215 /*
216 * Add in the page array if there is one. The caller needs to make
217 * sure rq_offset and rq_tailsz are set correctly. If a buffer of
218 * multiple pages ends at page boundary, rq_tailsz needs to be set to
219 * PAGE_SIZE.
220 */
Jeff Laytona26054d2014-02-14 07:21:00 -0500221 if (rqst->rq_npages) {
Long Lic06a0f22018-05-30 12:47:57 -0700222 if (rqst->rq_npages == 1)
223 buflen += rqst->rq_tailsz;
224 else {
225 /*
226 * If there is more than one page, calculate the
227 * buffer length based on rq_offset and rq_tailsz
228 */
229 buflen += rqst->rq_pagesz * (rqst->rq_npages - 1) -
230 rqst->rq_offset;
231 buflen += rqst->rq_tailsz;
232 }
Jeff Laytona26054d2014-02-14 07:21:00 -0500233 }
234
235 return buflen;
236}
237
Jeff Layton6f49f462012-09-18 16:20:34 -0700238static int
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000239__smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
240 struct smb_rqst *rqst)
Jeff Layton6f49f462012-09-18 16:20:34 -0700241{
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000242 int rc = 0;
243 struct kvec *iov;
244 int n_vec;
245 unsigned int send_length = 0;
246 unsigned int i, j;
Al Viro3ab3f2a2015-11-13 02:36:04 -0500247 size_t total_len = 0, sent, size;
Jeff Laytonb8eed282012-09-18 16:20:35 -0700248 struct socket *ssocket = server->ssocket;
Al Viro3ab3f2a2015-11-13 02:36:04 -0500249 struct msghdr smb_msg;
Jeff Laytonb8eed282012-09-18 16:20:35 -0700250 int val = 1;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000251 __be32 rfc1002_marker;
252
Long Li9762c2d2017-11-22 17:38:43 -0700253 if (cifs_rdma_enabled(server) && server->smbd_conn) {
254 rc = smbd_send(server->smbd_conn, rqst);
255 goto smbd_done;
256 }
Jeff Laytonea702b82012-12-27 07:28:55 -0500257 if (ssocket == NULL)
258 return -ENOTSOCK;
259
Jeff Laytonb8eed282012-09-18 16:20:35 -0700260 /* cork the socket */
261 kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
262 (char *)&val, sizeof(val));
263
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000264 for (j = 0; j < num_rqst; j++)
265 send_length += smb2_rqst_len(&rqst[j]);
266 rfc1002_marker = cpu_to_be32(send_length);
267
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000268 /* Generate a rfc1002 marker for SMB2+ */
269 if (server->vals->header_preamble_size == 0) {
270 struct kvec hiov = {
271 .iov_base = &rfc1002_marker,
272 .iov_len = 4
273 };
274 iov_iter_kvec(&smb_msg.msg_iter, WRITE | ITER_KVEC, &hiov,
275 1, 4);
276 rc = smb_send_kvec(server, &smb_msg, &sent);
277 if (rc < 0)
278 goto uncork;
279
280 total_len += sent;
281 send_length += 4;
282 }
283
Paulo Alcantara662bf5b2018-06-14 17:34:08 -0300284 cifs_dbg(FYI, "Sending smb: smb_len=%u\n", send_length);
285
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000286 for (j = 0; j < num_rqst; j++) {
287 iov = rqst[j].rq_iov;
288 n_vec = rqst[j].rq_nvec;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000289
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000290 size = 0;
Paulo Alcantara662bf5b2018-06-14 17:34:08 -0300291 for (i = 0; i < n_vec; i++) {
292 dump_smb(iov[i].iov_base, iov[i].iov_len);
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000293 size += iov[i].iov_len;
Paulo Alcantara662bf5b2018-06-14 17:34:08 -0300294 }
Al Viro3ab3f2a2015-11-13 02:36:04 -0500295
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000296 iov_iter_kvec(&smb_msg.msg_iter, WRITE | ITER_KVEC,
297 iov, n_vec, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Al Viro3ab3f2a2015-11-13 02:36:04 -0500299 rc = smb_send_kvec(server, &smb_msg, &sent);
Jeff Layton97bc00b2012-09-18 16:20:35 -0700300 if (rc < 0)
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000301 goto uncork;
Jeff Layton97bc00b2012-09-18 16:20:35 -0700302
303 total_len += sent;
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000304
305 /* now walk the page array and send each page in it */
306 for (i = 0; i < rqst[j].rq_npages; i++) {
307 struct bio_vec bvec;
308
309 bvec.bv_page = rqst[j].rq_pages[i];
310 rqst_page_get_length(&rqst[j], i, &bvec.bv_len,
311 &bvec.bv_offset);
312
313 iov_iter_bvec(&smb_msg.msg_iter, WRITE | ITER_BVEC,
314 &bvec, 1, bvec.bv_len);
315 rc = smb_send_kvec(server, &smb_msg, &sent);
316 if (rc < 0)
317 break;
318
319 total_len += sent;
320 }
Jeff Layton97bc00b2012-09-18 16:20:35 -0700321 }
322
323uncork:
Jeff Laytonb8eed282012-09-18 16:20:35 -0700324 /* uncork it */
325 val = 0;
326 kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
327 (char *)&val, sizeof(val));
328
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000329 if ((total_len > 0) && (total_len != send_length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500330 cifs_dbg(FYI, "partial send (wanted=%u sent=%zu): terminating session\n",
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000331 send_length, total_len);
Jeff Layton6f49f462012-09-18 16:20:34 -0700332 /*
333 * If we have only sent part of an SMB then the next SMB could
334 * be taken as the remainder of this one. We need to kill the
335 * socket so the server throws away the partial SMB
336 */
Steve Frenchedf1ae42008-10-29 00:47:57 +0000337 server->tcpStatus = CifsNeedReconnect;
338 }
Long Li9762c2d2017-11-22 17:38:43 -0700339smbd_done:
Jeff Laytond804d412011-01-28 15:05:43 -0500340 if (rc < 0 && rc != -EINTR)
Joe Perchesf96637b2013-05-04 22:12:25 -0500341 cifs_dbg(VFS, "Error %d sending data on socket to server\n",
342 rc);
Jeff Laytond804d412011-01-28 15:05:43 -0500343 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 return rc;
347}
348
Jeff Layton6f49f462012-09-18 16:20:34 -0700349static int
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700350smb_send_rqst(struct TCP_Server_Info *server, struct smb_rqst *rqst, int flags)
Jeff Layton6f49f462012-09-18 16:20:34 -0700351{
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700352 struct smb_rqst cur_rqst;
353 int rc;
Jeff Layton6f49f462012-09-18 16:20:34 -0700354
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700355 if (!(flags & CIFS_TRANSFORM_REQ))
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000356 return __smb_send_rqst(server, 1, rqst);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700357
358 if (!server->ops->init_transform_rq ||
359 !server->ops->free_transform_rq) {
360 cifs_dbg(VFS, "Encryption requested but transform callbacks are missed\n");
361 return -EIO;
362 }
363
364 rc = server->ops->init_transform_rq(server, &cur_rqst, rqst);
365 if (rc)
366 return rc;
367
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000368 rc = __smb_send_rqst(server, 1, &cur_rqst);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700369 server->ops->free_transform_rq(&cur_rqst);
370 return rc;
Jeff Layton6f49f462012-09-18 16:20:34 -0700371}
372
Jeff Layton0496e022008-12-30 12:39:16 -0500373int
374smb_send(struct TCP_Server_Info *server, struct smb_hdr *smb_buffer,
375 unsigned int smb_buf_length)
376{
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800377 struct kvec iov[2];
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700378 struct smb_rqst rqst = { .rq_iov = iov,
379 .rq_nvec = 2 };
Jeff Layton0496e022008-12-30 12:39:16 -0500380
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800381 iov[0].iov_base = smb_buffer;
382 iov[0].iov_len = 4;
383 iov[1].iov_base = (char *)smb_buffer + 4;
384 iov[1].iov_len = smb_buf_length;
Jeff Layton0496e022008-12-30 12:39:16 -0500385
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000386 return __smb_send_rqst(server, 1, &rqst);
Jeff Layton0496e022008-12-30 12:39:16 -0500387}
388
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300389static int
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400390wait_for_free_credits(struct TCP_Server_Info *server, const int timeout,
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300391 int *credits)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000392{
Pavel Shilovsky5bc59492012-02-21 19:56:08 +0300393 int rc;
394
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300395 spin_lock(&server->req_lock);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400396 if (timeout == CIFS_ASYNC_OP) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000397 /* oplock breaks must not be held up */
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300398 server->in_flight++;
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300399 *credits -= 1;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300400 spin_unlock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000401 return 0;
402 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000403
Volker Lendecke27a97a62008-12-08 20:59:39 +0000404 while (1) {
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300405 if (*credits <= 0) {
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300406 spin_unlock(&server->req_lock);
Steve French789e6662011-08-09 18:44:44 +0000407 cifs_num_waiters_inc(server);
Pavel Shilovsky5bc59492012-02-21 19:56:08 +0300408 rc = wait_event_killable(server->request_q,
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300409 has_credits(server, credits));
Steve French789e6662011-08-09 18:44:44 +0000410 cifs_num_waiters_dec(server);
Pavel Shilovsky5bc59492012-02-21 19:56:08 +0300411 if (rc)
412 return rc;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300413 spin_lock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000414 } else {
Jeff Laytonc5797a92011-01-11 07:24:01 -0500415 if (server->tcpStatus == CifsExiting) {
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300416 spin_unlock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000417 return -ENOENT;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000418 }
Volker Lendecke27a97a62008-12-08 20:59:39 +0000419
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400420 /*
421 * Can not count locking commands against total
422 * as they are allowed to block on server.
423 */
Volker Lendecke27a97a62008-12-08 20:59:39 +0000424
425 /* update # of requests on the wire to server */
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400426 if (timeout != CIFS_BLOCKING_OP) {
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300427 *credits -= 1;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300428 server->in_flight++;
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400429 }
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300430 spin_unlock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000431 break;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000432 }
433 }
434 return 0;
435}
436
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300437static int
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400438wait_for_free_request(struct TCP_Server_Info *server, const int timeout,
439 const int optype)
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300440{
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -0500441 int *val;
442
443 val = server->ops->get_credits_field(server, optype);
444 /* Since an echo is already inflight, no need to wait to send another */
445 if (*val <= 0 && optype == CIFS_ECHO_OP)
446 return -EAGAIN;
447 return wait_for_free_credits(server, timeout, val);
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300448}
449
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400450int
451cifs_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
452 unsigned int *num, unsigned int *credits)
453{
454 *num = size;
455 *credits = 0;
456 return 0;
457}
458
Steve French96daf2b2011-05-27 04:34:02 +0000459static int allocate_mid(struct cifs_ses *ses, struct smb_hdr *in_buf,
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000460 struct mid_q_entry **ppmidQ)
461{
462 if (ses->server->tcpStatus == CifsExiting) {
463 return -ENOENT;
Volker Lendecke8fbbd362008-12-06 13:12:34 +0100464 }
465
466 if (ses->server->tcpStatus == CifsNeedReconnect) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500467 cifs_dbg(FYI, "tcp session dead - return to caller to retry\n");
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000468 return -EAGAIN;
Volker Lendecke8fbbd362008-12-06 13:12:34 +0100469 }
470
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500471 if (ses->status == CifsNew) {
Steve French79a58d12007-07-06 22:44:50 +0000472 if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
Steve Frenchad7a2922008-02-07 23:25:02 +0000473 (in_buf->Command != SMB_COM_NEGOTIATE))
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000474 return -EAGAIN;
Steve Frenchad7a2922008-02-07 23:25:02 +0000475 /* else ok - we are setting up session */
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000476 }
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500477
478 if (ses->status == CifsExiting) {
479 /* check if SMB session is bad because we are setting it up */
480 if (in_buf->Command != SMB_COM_LOGOFF_ANDX)
481 return -EAGAIN;
482 /* else ok - we are shutting down session */
483 }
484
Jeff Layton24b9b062008-12-01 07:09:34 -0500485 *ppmidQ = AllocMidQEntry(in_buf, ses->server);
Steve French26f57362007-08-30 22:09:15 +0000486 if (*ppmidQ == NULL)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000487 return -ENOMEM;
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500488 spin_lock(&GlobalMid_Lock);
489 list_add_tail(&(*ppmidQ)->qhead, &ses->server->pending_mid_q);
490 spin_unlock(&GlobalMid_Lock);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000491 return 0;
492}
493
Jeff Layton0ade6402011-01-11 07:24:02 -0500494static int
495wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *midQ)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000496{
Jeff Layton0ade6402011-01-11 07:24:02 -0500497 int error;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000498
Colin Cross5853cc22013-05-07 17:52:05 +0000499 error = wait_event_freezekillable_unsafe(server->response_q,
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400500 midQ->mid_state != MID_REQUEST_SUBMITTED);
Jeff Layton0ade6402011-01-11 07:24:02 -0500501 if (error < 0)
502 return -ERESTARTSYS;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000503
Jeff Layton0ade6402011-01-11 07:24:02 -0500504 return 0;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000505}
506
Jeff Laytonfec344e2012-09-18 16:20:35 -0700507struct mid_q_entry *
508cifs_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400509{
510 int rc;
Jeff Laytonfec344e2012-09-18 16:20:35 -0700511 struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400512 struct mid_q_entry *mid;
513
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800514 if (rqst->rq_iov[0].iov_len != 4 ||
515 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
516 return ERR_PTR(-EIO);
517
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400518 /* enable signing if server requires it */
Jeff Layton38d77c52013-05-26 07:01:00 -0400519 if (server->sign)
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400520 hdr->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
521
522 mid = AllocMidQEntry(hdr, server);
523 if (mid == NULL)
Jeff Laytonfec344e2012-09-18 16:20:35 -0700524 return ERR_PTR(-ENOMEM);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400525
Jeff Laytonfec344e2012-09-18 16:20:35 -0700526 rc = cifs_sign_rqst(rqst, server, &mid->sequence_number);
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100527 if (rc) {
528 DeleteMidQEntry(mid);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700529 return ERR_PTR(rc);
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100530 }
531
Jeff Laytonfec344e2012-09-18 16:20:35 -0700532 return mid;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400533}
Steve French133672e2007-11-13 22:41:37 +0000534
535/*
Jeff Laytona6827c12011-01-11 07:24:21 -0500536 * Send a SMB request and set the callback function in the mid to handle
537 * the result. Caller is responsible for dealing with timeouts.
538 */
539int
Jeff Laytonfec344e2012-09-18 16:20:35 -0700540cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst,
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -0800541 mid_receive_t *receive, mid_callback_t *callback,
542 mid_handle_t *handle, void *cbdata, const int flags)
Jeff Laytona6827c12011-01-11 07:24:21 -0500543{
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400544 int rc, timeout, optype;
Jeff Laytona6827c12011-01-11 07:24:21 -0500545 struct mid_q_entry *mid;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400546 unsigned int credits = 0;
Jeff Laytona6827c12011-01-11 07:24:21 -0500547
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400548 timeout = flags & CIFS_TIMEOUT_MASK;
549 optype = flags & CIFS_OP_MASK;
550
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400551 if ((flags & CIFS_HAS_CREDITS) == 0) {
552 rc = wait_for_free_request(server, timeout, optype);
553 if (rc)
554 return rc;
555 credits = 1;
556 }
Jeff Laytona6827c12011-01-11 07:24:21 -0500557
558 mutex_lock(&server->srv_mutex);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700559 mid = server->ops->setup_async_request(server, rqst);
560 if (IS_ERR(mid)) {
Jeff Laytona6827c12011-01-11 07:24:21 -0500561 mutex_unlock(&server->srv_mutex);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400562 add_credits_and_wake_if(server, credits, optype);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700563 return PTR_ERR(mid);
Jeff Laytona6827c12011-01-11 07:24:21 -0500564 }
565
Jeff Layton44d22d82011-10-19 15:29:49 -0400566 mid->receive = receive;
Jeff Laytona6827c12011-01-11 07:24:21 -0500567 mid->callback = callback;
568 mid->callback_data = cbdata;
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -0800569 mid->handle = handle;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400570 mid->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +0000571
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100572 /* put it on the pending_mid_q */
573 spin_lock(&GlobalMid_Lock);
574 list_add_tail(&mid->qhead, &server->pending_mid_q);
575 spin_unlock(&GlobalMid_Lock);
576
Long Li93d2cb62017-06-28 15:55:55 -0700577 /*
578 * Need to store the time in mid before calling I/O. For call_async,
579 * I/O response may come back and free the mid entry on another thread.
580 */
581 cifs_save_when_sent(mid);
Steve French789e6662011-08-09 18:44:44 +0000582 cifs_in_send_inc(server);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700583 rc = smb_send_rqst(server, rqst, flags);
Steve French789e6662011-08-09 18:44:44 +0000584 cifs_in_send_dec(server);
Jeff Laytonad313cb2013-04-03 10:27:36 -0400585
Rabin Vincent820962d2015-12-23 07:32:41 +0100586 if (rc < 0) {
Jeff Laytonad313cb2013-04-03 10:27:36 -0400587 server->sequence_number -= 2;
Rabin Vincent820962d2015-12-23 07:32:41 +0100588 cifs_delete_mid(mid);
589 }
590
Jeff Laytona6827c12011-01-11 07:24:21 -0500591 mutex_unlock(&server->srv_mutex);
Steve French789e6662011-08-09 18:44:44 +0000592
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100593 if (rc == 0)
594 return 0;
Jeff Laytona6827c12011-01-11 07:24:21 -0500595
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400596 add_credits_and_wake_if(server, credits, optype);
Jeff Laytona6827c12011-01-11 07:24:21 -0500597 return rc;
598}
599
600/*
Steve French133672e2007-11-13 22:41:37 +0000601 *
602 * Send an SMB Request. No response info (other than return code)
603 * needs to be parsed.
604 *
605 * flags indicate the type of request buffer and how long to wait
606 * and whether to log NT STATUS code (error) before mapping it to POSIX error
607 *
608 */
609int
Steve French96daf2b2011-05-27 04:34:02 +0000610SendReceiveNoRsp(const unsigned int xid, struct cifs_ses *ses,
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400611 char *in_buf, int flags)
Steve French133672e2007-11-13 22:41:37 +0000612{
613 int rc;
614 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700615 struct kvec rsp_iov;
Steve French133672e2007-11-13 22:41:37 +0000616 int resp_buf_type;
617
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400618 iov[0].iov_base = in_buf;
619 iov[0].iov_len = get_rfc1002_length(in_buf) + 4;
Steve French133672e2007-11-13 22:41:37 +0000620 flags |= CIFS_NO_RESP;
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700621 rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Joe Perchesf96637b2013-05-04 22:12:25 -0500622 cifs_dbg(NOISY, "SendRcvNoRsp flags %d rc %d\n", flags, rc);
Steve French90c81e02008-02-12 20:32:36 +0000623
Steve French133672e2007-11-13 22:41:37 +0000624 return rc;
625}
626
Jeff Layton053d5032011-01-11 07:24:02 -0500627static int
Jeff Layton3c1105d2011-05-22 07:09:13 -0400628cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
Jeff Layton053d5032011-01-11 07:24:02 -0500629{
630 int rc = 0;
631
Joe Perchesf96637b2013-05-04 22:12:25 -0500632 cifs_dbg(FYI, "%s: cmd=%d mid=%llu state=%d\n",
633 __func__, le16_to_cpu(mid->command), mid->mid, mid->mid_state);
Jeff Layton053d5032011-01-11 07:24:02 -0500634
Jeff Layton74dd92a2011-01-11 07:24:02 -0500635 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400636 switch (mid->mid_state) {
Jeff Layton74dd92a2011-01-11 07:24:02 -0500637 case MID_RESPONSE_RECEIVED:
Jeff Layton053d5032011-01-11 07:24:02 -0500638 spin_unlock(&GlobalMid_Lock);
639 return rc;
Jeff Layton74dd92a2011-01-11 07:24:02 -0500640 case MID_RETRY_NEEDED:
641 rc = -EAGAIN;
642 break;
Jeff Layton71823ba2011-02-10 08:03:50 -0500643 case MID_RESPONSE_MALFORMED:
644 rc = -EIO;
645 break;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400646 case MID_SHUTDOWN:
647 rc = -EHOSTDOWN;
648 break;
Jeff Layton74dd92a2011-01-11 07:24:02 -0500649 default:
Jeff Layton3c1105d2011-05-22 07:09:13 -0400650 list_del_init(&mid->qhead);
Joe Perchesf96637b2013-05-04 22:12:25 -0500651 cifs_dbg(VFS, "%s: invalid mid state mid=%llu state=%d\n",
652 __func__, mid->mid, mid->mid_state);
Jeff Layton74dd92a2011-01-11 07:24:02 -0500653 rc = -EIO;
Jeff Layton053d5032011-01-11 07:24:02 -0500654 }
655 spin_unlock(&GlobalMid_Lock);
656
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500657 DeleteMidQEntry(mid);
Jeff Layton053d5032011-01-11 07:24:02 -0500658 return rc;
659}
660
Jeff Layton121b0462012-05-15 12:21:10 -0400661static inline int
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800662send_cancel(struct TCP_Server_Info *server, struct smb_rqst *rqst,
663 struct mid_q_entry *mid)
Jeff Layton76dcc262011-01-11 07:24:24 -0500664{
Jeff Layton121b0462012-05-15 12:21:10 -0400665 return server->ops->send_cancel ?
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800666 server->ops->send_cancel(server, rqst, mid) : 0;
Jeff Layton76dcc262011-01-11 07:24:24 -0500667}
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669int
Jeff Layton2c8f9812011-05-19 16:22:52 -0400670cifs_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
671 bool log_error)
672{
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400673 unsigned int len = get_rfc1002_length(mid->resp_buf) + 4;
Jeff Layton826a95e2011-10-11 06:41:32 -0400674
675 dump_smb(mid->resp_buf, min_t(u32, 92, len));
Jeff Layton2c8f9812011-05-19 16:22:52 -0400676
677 /* convert the length into a more usable form */
Jeff Layton38d77c52013-05-26 07:01:00 -0400678 if (server->sign) {
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800679 struct kvec iov[2];
Steve French985e4ff02012-08-03 09:42:45 -0500680 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800681 struct smb_rqst rqst = { .rq_iov = iov,
682 .rq_nvec = 2 };
Jeff Layton826a95e2011-10-11 06:41:32 -0400683
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800684 iov[0].iov_base = mid->resp_buf;
685 iov[0].iov_len = 4;
686 iov[1].iov_base = (char *)mid->resp_buf + 4;
687 iov[1].iov_len = len - 4;
Jeff Layton2c8f9812011-05-19 16:22:52 -0400688 /* FIXME: add code to kill session */
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700689 rc = cifs_verify_signature(&rqst, server,
Jeff Layton0124cc42013-04-03 11:55:03 -0400690 mid->sequence_number);
Steve French985e4ff02012-08-03 09:42:45 -0500691 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500692 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
693 rc);
Jeff Layton2c8f9812011-05-19 16:22:52 -0400694 }
695
696 /* BB special case reconnect tid and uid here? */
697 return map_smb_to_linux_error(mid->resp_buf, log_error);
698}
699
Jeff Laytonfec344e2012-09-18 16:20:35 -0700700struct mid_q_entry *
701cifs_setup_request(struct cifs_ses *ses, struct smb_rqst *rqst)
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400702{
703 int rc;
Jeff Laytonfec344e2012-09-18 16:20:35 -0700704 struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400705 struct mid_q_entry *mid;
706
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800707 if (rqst->rq_iov[0].iov_len != 4 ||
708 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
709 return ERR_PTR(-EIO);
710
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400711 rc = allocate_mid(ses, hdr, &mid);
712 if (rc)
Jeff Laytonfec344e2012-09-18 16:20:35 -0700713 return ERR_PTR(rc);
714 rc = cifs_sign_rqst(rqst, ses->server, &mid->sequence_number);
715 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700716 cifs_delete_mid(mid);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700717 return ERR_PTR(rc);
718 }
719 return mid;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400720}
721
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800722int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800723cifs_send_recv(const unsigned int xid, struct cifs_ses *ses,
724 struct smb_rqst *rqst, int *resp_buf_type, const int flags,
725 struct kvec *resp_iov)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
727 int rc = 0;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400728 int timeout, optype;
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500729 struct mid_q_entry *midQ;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400730 unsigned int credits = 1;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800731 char *buf;
Steve French50c2f752007-07-13 00:33:32 +0000732
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400733 timeout = flags & CIFS_TIMEOUT_MASK;
734 optype = flags & CIFS_OP_MASK;
Steve French133672e2007-11-13 22:41:37 +0000735
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400736 *resp_buf_type = CIFS_NO_BUFFER; /* no response buf yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Steve French4b8f9302006-02-26 16:41:18 +0000738 if ((ses == NULL) || (ses->server == NULL)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500739 cifs_dbg(VFS, "Null session\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 return -EIO;
741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700743 if (ses->server->tcpStatus == CifsExiting)
Steve French31ca3bc2005-04-28 22:41:11 -0700744 return -ENOENT;
745
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400746 /*
747 * Ensure that we do not send more than 50 overlapping requests
748 * to the same server. We may make this configurable later or
749 * use ses->maxReq.
750 */
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400751 rc = wait_for_free_request(ses->server, timeout, optype);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700752 if (rc)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000753 return rc;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000754
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400755 /*
756 * Make sure that we sign in the same order that we send on this socket
757 * and avoid races inside tcp sendmsg code that could cause corruption
758 * of smb data.
759 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Jeff Layton72ca5452008-12-01 07:09:36 -0500761 mutex_lock(&ses->server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800763 midQ = ses->server->ops->setup_request(ses, rqst);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700764 if (IS_ERR(midQ)) {
Jeff Layton72ca5452008-12-01 07:09:36 -0500765 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000766 /* Update # of requests on wire to server */
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400767 add_credits(ses->server, 1, optype);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700768 return PTR_ERR(midQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400771 midQ->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +0000772 cifs_in_send_inc(ses->server);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700773 rc = smb_send_rqst(ses->server, rqst, flags);
Steve French789e6662011-08-09 18:44:44 +0000774 cifs_in_send_dec(ses->server);
775 cifs_save_when_sent(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000776
Jeff Laytonad313cb2013-04-03 10:27:36 -0400777 if (rc < 0)
778 ses->server->sequence_number -= 2;
Jeff Layton72ca5452008-12-01 07:09:36 -0500779 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000780
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700781 if (rc < 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000782 goto out;
Steve French4b8f9302006-02-26 16:41:18 +0000783
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100784#ifdef CONFIG_CIFS_SMB311
Steve French0d5ec282018-04-22 19:51:22 -0500785 if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP))
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000786 smb311_update_preauth_hash(ses, rqst->rq_iov,
787 rqst->rq_nvec);
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100788#endif
789
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700790 if (timeout == CIFS_ASYNC_OP)
Steve French133672e2007-11-13 22:41:37 +0000791 goto out;
Jeff Layton0ade6402011-01-11 07:24:02 -0500792
793 rc = wait_for_response(ses->server, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -0500794 if (rc != 0) {
Sachin Prabhu38bd4902017-03-03 15:41:38 -0800795 cifs_dbg(FYI, "Cancelling wait for mid %llu\n", midQ->mid);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800796 send_cancel(ses->server, rqst, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -0500797 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400798 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
Sachin Prabhu38bd4902017-03-03 15:41:38 -0800799 midQ->mid_flags |= MID_WAIT_CANCELLED;
Jeff Layton1be912d2011-01-28 07:08:28 -0500800 midQ->callback = DeleteMidQEntry;
801 spin_unlock(&GlobalMid_Lock);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400802 add_credits(ses->server, 1, optype);
Jeff Layton1be912d2011-01-28 07:08:28 -0500803 return rc;
804 }
805 spin_unlock(&GlobalMid_Lock);
806 }
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500807
Jeff Layton3c1105d2011-05-22 07:09:13 -0400808 rc = cifs_sync_mid_result(midQ, ses->server);
Jeff Layton053d5032011-01-11 07:24:02 -0500809 if (rc != 0) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400810 add_credits(ses->server, 1, optype);
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500811 return rc;
812 }
Steve French50c2f752007-07-13 00:33:32 +0000813
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400814 if (!midQ->resp_buf || midQ->mid_state != MID_RESPONSE_RECEIVED) {
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500815 rc = -EIO;
Joe Perchesf96637b2013-05-04 22:12:25 -0500816 cifs_dbg(FYI, "Bad MID state?\n");
Steve French2b2bdfb2008-12-11 17:26:54 +0000817 goto out;
818 }
Steve French84afc292005-12-02 13:32:45 -0800819
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400820 buf = (char *)midQ->resp_buf;
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700821 resp_iov->iov_base = buf;
Ronnie Sahlberge19b2bc2018-04-09 18:06:28 +1000822 resp_iov->iov_len = midQ->resp_buf_size +
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100823 ses->server->vals->header_preamble_size;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400824 if (midQ->large_buf)
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400825 *resp_buf_type = CIFS_LARGE_BUFFER;
Jeff Layton2c8f9812011-05-19 16:22:52 -0400826 else
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400827 *resp_buf_type = CIFS_SMALL_BUFFER;
828
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100829#ifdef CONFIG_CIFS_SMB311
Steve French0d5ec282018-04-22 19:51:22 -0500830 if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP)) {
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100831 struct kvec iov = {
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000832 .iov_base = resp_iov->iov_base,
833 .iov_len = resp_iov->iov_len
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100834 };
835 smb311_update_preauth_hash(ses, &iov, 1);
836 }
837#endif
838
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400839 credits = ses->server->ops->get_credits(midQ);
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500840
Pavel Shilovsky082d0642012-05-17 12:18:21 +0400841 rc = ses->server->ops->check_receive(midQ, ses->server,
842 flags & CIFS_LOG_ERROR);
Steve French2b2bdfb2008-12-11 17:26:54 +0000843
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700844 /* mark it so buf will not be freed by cifs_delete_mid */
Jeff Layton2c8f9812011-05-19 16:22:52 -0400845 if ((flags & CIFS_NO_RESP) == 0)
846 midQ->resp_buf = NULL;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000847out:
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700848 cifs_delete_mid(midQ);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400849 add_credits(ses->server, credits, optype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 return rc;
852}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800855SendReceive2(const unsigned int xid, struct cifs_ses *ses,
856 struct kvec *iov, int n_vec, int *resp_buf_type /* ret */,
857 const int flags, struct kvec *resp_iov)
858{
859 struct smb_rqst rqst;
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +1100860 struct kvec s_iov[CIFS_MAX_IOV_SIZE], *new_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800861 int rc;
862
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +1100863 if (n_vec + 1 > CIFS_MAX_IOV_SIZE) {
Kees Cook6da2ec52018-06-12 13:55:00 -0700864 new_iov = kmalloc_array(n_vec + 1, sizeof(struct kvec),
865 GFP_KERNEL);
Steve French117e3b72018-04-22 10:24:19 -0500866 if (!new_iov) {
867 /* otherwise cifs_send_recv below sets resp_buf_type */
868 *resp_buf_type = CIFS_NO_BUFFER;
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +1100869 return -ENOMEM;
Steve French117e3b72018-04-22 10:24:19 -0500870 }
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +1100871 } else
872 new_iov = s_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800873
874 /* 1st iov is a RFC1001 length followed by the rest of the packet */
875 memcpy(new_iov + 1, iov, (sizeof(struct kvec) * n_vec));
876
877 new_iov[0].iov_base = new_iov[1].iov_base;
878 new_iov[0].iov_len = 4;
879 new_iov[1].iov_base += 4;
880 new_iov[1].iov_len -= 4;
881
882 memset(&rqst, 0, sizeof(struct smb_rqst));
883 rqst.rq_iov = new_iov;
884 rqst.rq_nvec = n_vec + 1;
885
886 rc = cifs_send_recv(xid, ses, &rqst, resp_buf_type, flags, resp_iov);
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +1100887 if (n_vec + 1 > CIFS_MAX_IOV_SIZE)
888 kfree(new_iov);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800889 return rc;
890}
891
892int
Steve French96daf2b2011-05-27 04:34:02 +0000893SendReceive(const unsigned int xid, struct cifs_ses *ses,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400895 int *pbytes_returned, const int timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
897 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 struct mid_q_entry *midQ;
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800899 unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
900 struct kvec iov = { .iov_base = in_buf, .iov_len = len };
901 struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 if (ses == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500904 cifs_dbg(VFS, "Null smb session\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return -EIO;
906 }
Steve French79a58d12007-07-06 22:44:50 +0000907 if (ses->server == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500908 cifs_dbg(VFS, "Null tcp session\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return -EIO;
910 }
911
Steve French79a58d12007-07-06 22:44:50 +0000912 if (ses->server->tcpStatus == CifsExiting)
Steve French31ca3bc2005-04-28 22:41:11 -0700913 return -ENOENT;
914
Steve French79a58d12007-07-06 22:44:50 +0000915 /* Ensure that we do not send more than 50 overlapping requests
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 to the same server. We may make this configurable later or
917 use ses->maxReq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800919 if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500920 cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800921 len);
Volker Lendecke6d9c6d52008-12-08 20:50:24 +0000922 return -EIO;
923 }
924
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400925 rc = wait_for_free_request(ses->server, timeout, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000926 if (rc)
927 return rc;
928
Steve French79a58d12007-07-06 22:44:50 +0000929 /* make sure that we sign in the same order that we send on this socket
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 and avoid races inside tcp sendmsg code that could cause corruption
931 of smb data */
932
Jeff Layton72ca5452008-12-01 07:09:36 -0500933 mutex_lock(&ses->server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000935 rc = allocate_mid(ses, in_buf, &midQ);
936 if (rc) {
Jeff Layton72ca5452008-12-01 07:09:36 -0500937 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000938 /* Update # of requests on wire to server */
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400939 add_credits(ses->server, 1, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000940 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
942
Steve Frenchad009ac2005-04-28 22:41:05 -0700943 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
Volker Lendecke829049c2008-12-06 16:00:53 +0100944 if (rc) {
945 mutex_unlock(&ses->server->srv_mutex);
946 goto out;
947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400949 midQ->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +0000950
951 cifs_in_send_inc(ses->server);
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800952 rc = smb_send(ses->server, in_buf, len);
Steve French789e6662011-08-09 18:44:44 +0000953 cifs_in_send_dec(ses->server);
954 cifs_save_when_sent(midQ);
Jeff Laytonad313cb2013-04-03 10:27:36 -0400955
956 if (rc < 0)
957 ses->server->sequence_number -= 2;
958
Jeff Layton72ca5452008-12-01 07:09:36 -0500959 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000960
Steve French79a58d12007-07-06 22:44:50 +0000961 if (rc < 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000962 goto out;
963
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400964 if (timeout == CIFS_ASYNC_OP)
Steve French133672e2007-11-13 22:41:37 +0000965 goto out;
Jeff Layton0ade6402011-01-11 07:24:02 -0500966
967 rc = wait_for_response(ses->server, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -0500968 if (rc != 0) {
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800969 send_cancel(ses->server, &rqst, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -0500970 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400971 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
Jeff Layton1be912d2011-01-28 07:08:28 -0500972 /* no longer considered to be "in-flight" */
973 midQ->callback = DeleteMidQEntry;
974 spin_unlock(&GlobalMid_Lock);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400975 add_credits(ses->server, 1, 0);
Jeff Layton1be912d2011-01-28 07:08:28 -0500976 return rc;
977 }
978 spin_unlock(&GlobalMid_Lock);
979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Jeff Layton3c1105d2011-05-22 07:09:13 -0400981 rc = cifs_sync_mid_result(midQ, ses->server);
Jeff Layton053d5032011-01-11 07:24:02 -0500982 if (rc != 0) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400983 add_credits(ses->server, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 return rc;
985 }
Steve French50c2f752007-07-13 00:33:32 +0000986
Jeff Layton2c8f9812011-05-19 16:22:52 -0400987 if (!midQ->resp_buf || !out_buf ||
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400988 midQ->mid_state != MID_RESPONSE_RECEIVED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 rc = -EIO;
Joe Perchesf96637b2013-05-04 22:12:25 -0500990 cifs_dbg(VFS, "Bad MID state?\n");
Steve French2b2bdfb2008-12-11 17:26:54 +0000991 goto out;
992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400994 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
Jeff Layton2c8f9812011-05-19 16:22:52 -0400995 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
996 rc = cifs_check_receive(midQ, ses->server, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000997out:
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700998 cifs_delete_mid(midQ);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400999 add_credits(ses->server, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 return rc;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001002}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001004/* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
1005 blocking lock to return. */
1006
1007static int
Steve French96daf2b2011-05-27 04:34:02 +00001008send_lock_cancel(const unsigned int xid, struct cifs_tcon *tcon,
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001009 struct smb_hdr *in_buf,
1010 struct smb_hdr *out_buf)
1011{
1012 int bytes_returned;
Steve French96daf2b2011-05-27 04:34:02 +00001013 struct cifs_ses *ses = tcon->ses;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001014 LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
1015
1016 /* We just modify the current in_buf to change
1017 the type of lock from LOCKING_ANDX_SHARED_LOCK
1018 or LOCKING_ANDX_EXCLUSIVE_LOCK to
1019 LOCKING_ANDX_CANCEL_LOCK. */
1020
1021 pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
1022 pSMB->Timeout = 0;
Pavel Shilovsky88257362012-05-23 14:01:59 +04001023 pSMB->hdr.Mid = get_next_mid(ses->server);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001024
1025 return SendReceive(xid, ses, in_buf, out_buf,
Jeff Layton77499812011-01-11 07:24:23 -05001026 &bytes_returned, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001027}
1028
1029int
Steve French96daf2b2011-05-27 04:34:02 +00001030SendReceiveBlockingLock(const unsigned int xid, struct cifs_tcon *tcon,
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001031 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
1032 int *pbytes_returned)
1033{
1034 int rc = 0;
1035 int rstart = 0;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001036 struct mid_q_entry *midQ;
Steve French96daf2b2011-05-27 04:34:02 +00001037 struct cifs_ses *ses;
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001038 unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
1039 struct kvec iov = { .iov_base = in_buf, .iov_len = len };
1040 struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001041
1042 if (tcon == NULL || tcon->ses == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001043 cifs_dbg(VFS, "Null smb session\n");
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001044 return -EIO;
1045 }
1046 ses = tcon->ses;
1047
Steve French79a58d12007-07-06 22:44:50 +00001048 if (ses->server == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001049 cifs_dbg(VFS, "Null tcp session\n");
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001050 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 }
1052
Steve French79a58d12007-07-06 22:44:50 +00001053 if (ses->server->tcpStatus == CifsExiting)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001054 return -ENOENT;
1055
Steve French79a58d12007-07-06 22:44:50 +00001056 /* Ensure that we do not send more than 50 overlapping requests
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001057 to the same server. We may make this configurable later or
1058 use ses->maxReq */
1059
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001060 if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001061 cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001062 len);
Volker Lendecke6d9c6d52008-12-08 20:50:24 +00001063 return -EIO;
1064 }
1065
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001066 rc = wait_for_free_request(ses->server, CIFS_BLOCKING_OP, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001067 if (rc)
1068 return rc;
1069
Steve French79a58d12007-07-06 22:44:50 +00001070 /* make sure that we sign in the same order that we send on this socket
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001071 and avoid races inside tcp sendmsg code that could cause corruption
1072 of smb data */
1073
Jeff Layton72ca5452008-12-01 07:09:36 -05001074 mutex_lock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001075
1076 rc = allocate_mid(ses, in_buf, &midQ);
1077 if (rc) {
Jeff Layton72ca5452008-12-01 07:09:36 -05001078 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001079 return rc;
1080 }
1081
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001082 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
Volker Lendecke829049c2008-12-06 16:00:53 +01001083 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001084 cifs_delete_mid(midQ);
Volker Lendecke829049c2008-12-06 16:00:53 +01001085 mutex_unlock(&ses->server->srv_mutex);
1086 return rc;
1087 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001088
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001089 midQ->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +00001090 cifs_in_send_inc(ses->server);
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001091 rc = smb_send(ses->server, in_buf, len);
Steve French789e6662011-08-09 18:44:44 +00001092 cifs_in_send_dec(ses->server);
1093 cifs_save_when_sent(midQ);
Jeff Laytonad313cb2013-04-03 10:27:36 -04001094
1095 if (rc < 0)
1096 ses->server->sequence_number -= 2;
1097
Jeff Layton72ca5452008-12-01 07:09:36 -05001098 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001099
Steve French79a58d12007-07-06 22:44:50 +00001100 if (rc < 0) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001101 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001102 return rc;
1103 }
1104
1105 /* Wait for a reply - allow signals to interrupt. */
1106 rc = wait_event_interruptible(ses->server->response_q,
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001107 (!(midQ->mid_state == MID_REQUEST_SUBMITTED)) ||
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001108 ((ses->server->tcpStatus != CifsGood) &&
1109 (ses->server->tcpStatus != CifsNew)));
1110
1111 /* Were we interrupted by a signal ? */
1112 if ((rc == -ERESTARTSYS) &&
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001113 (midQ->mid_state == MID_REQUEST_SUBMITTED) &&
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001114 ((ses->server->tcpStatus == CifsGood) ||
1115 (ses->server->tcpStatus == CifsNew))) {
1116
1117 if (in_buf->Command == SMB_COM_TRANSACTION2) {
1118 /* POSIX lock. We send a NT_CANCEL SMB to cause the
1119 blocking lock to return. */
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001120 rc = send_cancel(ses->server, &rqst, midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001121 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001122 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001123 return rc;
1124 }
1125 } else {
1126 /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
1127 to cause the blocking lock to return. */
1128
1129 rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
1130
1131 /* If we get -ENOLCK back the lock may have
1132 already been removed. Don't exit in this case. */
1133 if (rc && rc != -ENOLCK) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001134 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001135 return rc;
1136 }
1137 }
1138
Jeff Layton1be912d2011-01-28 07:08:28 -05001139 rc = wait_for_response(ses->server, midQ);
1140 if (rc) {
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001141 send_cancel(ses->server, &rqst, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -05001142 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001143 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
Jeff Layton1be912d2011-01-28 07:08:28 -05001144 /* no longer considered to be "in-flight" */
1145 midQ->callback = DeleteMidQEntry;
1146 spin_unlock(&GlobalMid_Lock);
1147 return rc;
1148 }
1149 spin_unlock(&GlobalMid_Lock);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001150 }
Jeff Layton1be912d2011-01-28 07:08:28 -05001151
1152 /* We got the response - restart system call. */
1153 rstart = 1;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001154 }
1155
Jeff Layton3c1105d2011-05-22 07:09:13 -04001156 rc = cifs_sync_mid_result(midQ, ses->server);
Jeff Layton053d5032011-01-11 07:24:02 -05001157 if (rc != 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001158 return rc;
Steve French50c2f752007-07-13 00:33:32 +00001159
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001160 /* rcvd frame is ok */
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001161 if (out_buf == NULL || midQ->mid_state != MID_RESPONSE_RECEIVED) {
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001162 rc = -EIO;
Joe Perchesf96637b2013-05-04 22:12:25 -05001163 cifs_dbg(VFS, "Bad MID state?\n");
Volker Lendecke698e96a2008-12-06 16:39:31 +01001164 goto out;
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001165 }
1166
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001167 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
Jeff Layton2c8f9812011-05-19 16:22:52 -04001168 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
1169 rc = cifs_check_receive(midQ, ses->server, 0);
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001170out:
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001171 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001172 if (rstart && rc == -EACCES)
1173 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 return rc;
1175}