blob: 333729cf46cdb82fffa174f2fc37f87697e19aa4 [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 */
Steve French020eec52018-08-01 16:38:07 -0500118 if (time_after(now, midEntry->when_alloc + HZ) &&
119 (midEntry->command != command)) {
Steve French468d6772018-08-04 05:24:34 -0500120 /* smb2slowcmd[NUMBER_OF_SMB2_COMMANDS] counts by command */
121 if ((le16_to_cpu(midEntry->command) < NUMBER_OF_SMB2_COMMANDS) &&
122 (le16_to_cpu(midEntry->command) >= 0))
123 cifs_stats_inc(&midEntry->server->smb2slowcmd[le16_to_cpu(midEntry->command)]);
124
Steve French020eec52018-08-01 16:38:07 -0500125 trace_smb3_slow_rsp(le16_to_cpu(midEntry->command),
126 midEntry->mid, midEntry->pid,
127 midEntry->when_sent, midEntry->when_received);
128 if (cifsFYI & CIFS_TIMER) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +0300129 pr_debug(" CIFS slow rsp: cmd %d mid %llu",
Steve French1047abc2005-10-11 19:58:06 -0700130 midEntry->command, midEntry->mid);
Andy Shevchenko0b456f02014-08-27 16:49:44 +0300131 pr_info(" A: 0x%lx S: 0x%lx R: 0x%lx\n",
Steve French1047abc2005-10-11 19:58:06 -0700132 now - midEntry->when_alloc,
133 now - midEntry->when_sent,
134 now - midEntry->when_received);
135 }
136 }
137#endif
Lars Persson696e4202018-06-25 14:05:25 +0200138 cifs_mid_q_entry_release(midEntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700141void
142cifs_delete_mid(struct mid_q_entry *mid)
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500143{
144 spin_lock(&GlobalMid_Lock);
Ronnie Sahlbergddf83af2018-08-30 10:12:59 +1000145 list_del_init(&mid->qhead);
146 mid->mid_flags |= MID_DELETED;
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500147 spin_unlock(&GlobalMid_Lock);
148
149 DeleteMidQEntry(mid);
150}
151
Jeff Layton6f49f462012-09-18 16:20:34 -0700152/*
153 * smb_send_kvec - send an array of kvecs to the server
154 * @server: Server to send the data to
Al Viro3ab3f2a2015-11-13 02:36:04 -0500155 * @smb_msg: Message to send
Jeff Layton6f49f462012-09-18 16:20:34 -0700156 * @sent: amount of data sent on socket is stored here
157 *
158 * Our basic "send data to server" function. Should be called with srv_mutex
159 * held. The caller is responsible for handling the results.
160 */
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500161static int
Al Viro3ab3f2a2015-11-13 02:36:04 -0500162smb_send_kvec(struct TCP_Server_Info *server, struct msghdr *smb_msg,
163 size_t *sent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
165 int rc = 0;
Al Viro3ab3f2a2015-11-13 02:36:04 -0500166 int retries = 0;
Steve Frenchedf1ae42008-10-29 00:47:57 +0000167 struct socket *ssocket = server->ssocket;
Steve French50c2f752007-07-13 00:33:32 +0000168
Jeff Layton6f49f462012-09-18 16:20:34 -0700169 *sent = 0;
170
Al Viro3ab3f2a2015-11-13 02:36:04 -0500171 smb_msg->msg_name = (struct sockaddr *) &server->dstaddr;
172 smb_msg->msg_namelen = sizeof(struct sockaddr);
173 smb_msg->msg_control = NULL;
174 smb_msg->msg_controllen = 0;
Jeff Layton0496e022008-12-30 12:39:16 -0500175 if (server->noblocksnd)
Al Viro3ab3f2a2015-11-13 02:36:04 -0500176 smb_msg->msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL;
Steve Frenchedf1ae42008-10-29 00:47:57 +0000177 else
Al Viro3ab3f2a2015-11-13 02:36:04 -0500178 smb_msg->msg_flags = MSG_NOSIGNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Al Viro3ab3f2a2015-11-13 02:36:04 -0500180 while (msg_data_left(smb_msg)) {
Jeff Layton6f49f462012-09-18 16:20:34 -0700181 /*
182 * If blocking send, we try 3 times, since each can block
183 * for 5 seconds. For nonblocking we have to try more
184 * but wait increasing amounts of time allowing time for
185 * socket to clear. The overall time we wait in either
186 * case to send on the socket is about 15 seconds.
187 * Similarly we wait for 15 seconds for a response from
188 * the server in SendReceive[2] for the server to send
189 * a response back for most types of requests (except
190 * SMB Write past end of file which can be slow, and
191 * blocking lock operations). NFS waits slightly longer
192 * than CIFS, but this can make it take longer for
193 * nonresponsive servers to be detected and 15 seconds
194 * is more than enough time for modern networks to
195 * send a packet. In most cases if we fail to send
196 * after the retries we will kill the socket and
197 * reconnect which may clear the network problem.
198 */
Al Viro3ab3f2a2015-11-13 02:36:04 -0500199 rc = sock_sendmsg(ssocket, smb_msg);
Jeff Laytonce6c44e2013-03-22 08:36:45 -0400200 if (rc == -EAGAIN) {
Al Viro3ab3f2a2015-11-13 02:36:04 -0500201 retries++;
202 if (retries >= 14 ||
203 (!server->noblocksnd && (retries > 2))) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500204 cifs_dbg(VFS, "sends on sock %p stuck for 15 seconds\n",
205 ssocket);
Al Viro3ab3f2a2015-11-13 02:36:04 -0500206 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
Al Viro3ab3f2a2015-11-13 02:36:04 -0500208 msleep(1 << retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 continue;
210 }
Jeff Layton6f49f462012-09-18 16:20:34 -0700211
Steve French79a58d12007-07-06 22:44:50 +0000212 if (rc < 0)
Al Viro3ab3f2a2015-11-13 02:36:04 -0500213 return rc;
Jeff Layton6f49f462012-09-18 16:20:34 -0700214
Steve French79a58d12007-07-06 22:44:50 +0000215 if (rc == 0) {
Steve French3e844692005-10-03 13:37:24 -0700216 /* should never happen, letting socket clear before
217 retrying is our only obvious option here */
Joe Perchesf96637b2013-05-04 22:12:25 -0500218 cifs_dbg(VFS, "tcp sent no data\n");
Steve French3e844692005-10-03 13:37:24 -0700219 msleep(500);
220 continue;
221 }
Jeff Layton6f49f462012-09-18 16:20:34 -0700222
Al Viro3ab3f2a2015-11-13 02:36:04 -0500223 /* send was at least partially successful */
224 *sent += rc;
225 retries = 0; /* in case we get ENOSPC on the next send */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
Al Viro3ab3f2a2015-11-13 02:36:04 -0500227 return 0;
Jeff Layton97bc00b2012-09-18 16:20:35 -0700228}
229
Paulo Alcantara35e2cc12018-06-15 10:22:44 -0300230unsigned long
Ronnie Sahlberg81f39f92018-06-28 10:47:14 +1000231smb_rqst_len(struct TCP_Server_Info *server, struct smb_rqst *rqst)
Jeff Laytona26054d2014-02-14 07:21:00 -0500232{
233 unsigned int i;
Paulo Alcantara35e2cc12018-06-15 10:22:44 -0300234 struct kvec *iov;
235 int nvec;
Jeff Laytona26054d2014-02-14 07:21:00 -0500236 unsigned long buflen = 0;
237
Ronnie Sahlberg81f39f92018-06-28 10:47:14 +1000238 if (server->vals->header_preamble_size == 0 &&
239 rqst->rq_nvec >= 2 && rqst->rq_iov[0].iov_len == 4) {
Paulo Alcantara35e2cc12018-06-15 10:22:44 -0300240 iov = &rqst->rq_iov[1];
241 nvec = rqst->rq_nvec - 1;
242 } else {
243 iov = rqst->rq_iov;
244 nvec = rqst->rq_nvec;
245 }
246
Jeff Laytona26054d2014-02-14 07:21:00 -0500247 /* total up iov array first */
Paulo Alcantara35e2cc12018-06-15 10:22:44 -0300248 for (i = 0; i < nvec; i++)
Jeff Laytona26054d2014-02-14 07:21:00 -0500249 buflen += iov[i].iov_len;
250
Long Lic06a0f22018-05-30 12:47:57 -0700251 /*
252 * Add in the page array if there is one. The caller needs to make
253 * sure rq_offset and rq_tailsz are set correctly. If a buffer of
254 * multiple pages ends at page boundary, rq_tailsz needs to be set to
255 * PAGE_SIZE.
256 */
Jeff Laytona26054d2014-02-14 07:21:00 -0500257 if (rqst->rq_npages) {
Long Lic06a0f22018-05-30 12:47:57 -0700258 if (rqst->rq_npages == 1)
259 buflen += rqst->rq_tailsz;
260 else {
261 /*
262 * If there is more than one page, calculate the
263 * buffer length based on rq_offset and rq_tailsz
264 */
265 buflen += rqst->rq_pagesz * (rqst->rq_npages - 1) -
266 rqst->rq_offset;
267 buflen += rqst->rq_tailsz;
268 }
Jeff Laytona26054d2014-02-14 07:21:00 -0500269 }
270
271 return buflen;
272}
273
Jeff Layton6f49f462012-09-18 16:20:34 -0700274static int
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000275__smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
276 struct smb_rqst *rqst)
Jeff Layton6f49f462012-09-18 16:20:34 -0700277{
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000278 int rc = 0;
279 struct kvec *iov;
280 int n_vec;
281 unsigned int send_length = 0;
282 unsigned int i, j;
Al Viro3ab3f2a2015-11-13 02:36:04 -0500283 size_t total_len = 0, sent, size;
Jeff Laytonb8eed282012-09-18 16:20:35 -0700284 struct socket *ssocket = server->ssocket;
Al Viro3ab3f2a2015-11-13 02:36:04 -0500285 struct msghdr smb_msg;
Jeff Laytonb8eed282012-09-18 16:20:35 -0700286 int val = 1;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000287 __be32 rfc1002_marker;
288
Long Li9762c2d2017-11-22 17:38:43 -0700289 if (cifs_rdma_enabled(server) && server->smbd_conn) {
Ronnie Sahlberg81f39f92018-06-28 10:47:14 +1000290 rc = smbd_send(server, rqst);
Long Li9762c2d2017-11-22 17:38:43 -0700291 goto smbd_done;
292 }
Jeff Laytonea702b82012-12-27 07:28:55 -0500293 if (ssocket == NULL)
294 return -ENOTSOCK;
295
Jeff Laytonb8eed282012-09-18 16:20:35 -0700296 /* cork the socket */
297 kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
298 (char *)&val, sizeof(val));
299
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000300 for (j = 0; j < num_rqst; j++)
Ronnie Sahlberg81f39f92018-06-28 10:47:14 +1000301 send_length += smb_rqst_len(server, &rqst[j]);
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000302 rfc1002_marker = cpu_to_be32(send_length);
303
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000304 /* Generate a rfc1002 marker for SMB2+ */
305 if (server->vals->header_preamble_size == 0) {
306 struct kvec hiov = {
307 .iov_base = &rfc1002_marker,
308 .iov_len = 4
309 };
310 iov_iter_kvec(&smb_msg.msg_iter, WRITE | ITER_KVEC, &hiov,
311 1, 4);
312 rc = smb_send_kvec(server, &smb_msg, &sent);
313 if (rc < 0)
314 goto uncork;
315
316 total_len += sent;
317 send_length += 4;
318 }
319
Paulo Alcantara662bf5b2018-06-14 17:34:08 -0300320 cifs_dbg(FYI, "Sending smb: smb_len=%u\n", send_length);
321
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000322 for (j = 0; j < num_rqst; j++) {
323 iov = rqst[j].rq_iov;
324 n_vec = rqst[j].rq_nvec;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000325
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000326 size = 0;
Paulo Alcantara662bf5b2018-06-14 17:34:08 -0300327 for (i = 0; i < n_vec; i++) {
328 dump_smb(iov[i].iov_base, iov[i].iov_len);
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000329 size += iov[i].iov_len;
Paulo Alcantara662bf5b2018-06-14 17:34:08 -0300330 }
Al Viro3ab3f2a2015-11-13 02:36:04 -0500331
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000332 iov_iter_kvec(&smb_msg.msg_iter, WRITE | ITER_KVEC,
333 iov, n_vec, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Al Viro3ab3f2a2015-11-13 02:36:04 -0500335 rc = smb_send_kvec(server, &smb_msg, &sent);
Jeff Layton97bc00b2012-09-18 16:20:35 -0700336 if (rc < 0)
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000337 goto uncork;
Jeff Layton97bc00b2012-09-18 16:20:35 -0700338
339 total_len += sent;
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000340
341 /* now walk the page array and send each page in it */
342 for (i = 0; i < rqst[j].rq_npages; i++) {
343 struct bio_vec bvec;
344
345 bvec.bv_page = rqst[j].rq_pages[i];
346 rqst_page_get_length(&rqst[j], i, &bvec.bv_len,
347 &bvec.bv_offset);
348
349 iov_iter_bvec(&smb_msg.msg_iter, WRITE | ITER_BVEC,
350 &bvec, 1, bvec.bv_len);
351 rc = smb_send_kvec(server, &smb_msg, &sent);
352 if (rc < 0)
353 break;
354
355 total_len += sent;
356 }
Jeff Layton97bc00b2012-09-18 16:20:35 -0700357 }
358
359uncork:
Jeff Laytonb8eed282012-09-18 16:20:35 -0700360 /* uncork it */
361 val = 0;
362 kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
363 (char *)&val, sizeof(val));
364
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000365 if ((total_len > 0) && (total_len != send_length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500366 cifs_dbg(FYI, "partial send (wanted=%u sent=%zu): terminating session\n",
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000367 send_length, total_len);
Jeff Layton6f49f462012-09-18 16:20:34 -0700368 /*
369 * If we have only sent part of an SMB then the next SMB could
370 * be taken as the remainder of this one. We need to kill the
371 * socket so the server throws away the partial SMB
372 */
Steve Frenchedf1ae42008-10-29 00:47:57 +0000373 server->tcpStatus = CifsNeedReconnect;
Steve Frenchbf1fdeb2018-07-30 19:23:09 -0500374 trace_smb3_partial_send_reconnect(server->CurrentMid,
375 server->hostname);
Steve Frenchedf1ae42008-10-29 00:47:57 +0000376 }
Long Li9762c2d2017-11-22 17:38:43 -0700377smbd_done:
Jeff Laytond804d412011-01-28 15:05:43 -0500378 if (rc < 0 && rc != -EINTR)
Joe Perchesf96637b2013-05-04 22:12:25 -0500379 cifs_dbg(VFS, "Error %d sending data on socket to server\n",
380 rc);
Jeff Laytond804d412011-01-28 15:05:43 -0500381 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 return rc;
385}
386
Jeff Layton6f49f462012-09-18 16:20:34 -0700387static int
Ronnie Sahlberg1f3a8f52018-08-01 09:26:12 +1000388smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
389 struct smb_rqst *rqst, int flags)
Jeff Layton6f49f462012-09-18 16:20:34 -0700390{
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +1000391 struct kvec iov;
392 struct smb2_transform_hdr tr_hdr;
393 struct smb_rqst cur_rqst[MAX_COMPOUND];
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700394 int rc;
Jeff Layton6f49f462012-09-18 16:20:34 -0700395
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700396 if (!(flags & CIFS_TRANSFORM_REQ))
Ronnie Sahlberg1f3a8f52018-08-01 09:26:12 +1000397 return __smb_send_rqst(server, num_rqst, rqst);
398
399 if (num_rqst > MAX_COMPOUND - 1)
400 return -ENOMEM;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700401
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +1000402 memset(&cur_rqst[0], 0, sizeof(cur_rqst));
403 memset(&iov, 0, sizeof(iov));
404 memset(&tr_hdr, 0, sizeof(tr_hdr));
405
406 iov.iov_base = &tr_hdr;
407 iov.iov_len = sizeof(tr_hdr);
408 cur_rqst[0].rq_iov = &iov;
409 cur_rqst[0].rq_nvec = 1;
410
411 if (!server->ops->init_transform_rq) {
412 cifs_dbg(VFS, "Encryption requested but transform callback "
413 "is missing\n");
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700414 return -EIO;
415 }
416
Ronnie Sahlberg1f3a8f52018-08-01 09:26:12 +1000417 rc = server->ops->init_transform_rq(server, num_rqst + 1,
418 &cur_rqst[0], rqst);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700419 if (rc)
420 return rc;
421
Ronnie Sahlberg1f3a8f52018-08-01 09:26:12 +1000422 rc = __smb_send_rqst(server, num_rqst + 1, &cur_rqst[0]);
423 smb3_free_compound_rqst(num_rqst, &cur_rqst[1]);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700424 return rc;
Jeff Layton6f49f462012-09-18 16:20:34 -0700425}
426
Jeff Layton0496e022008-12-30 12:39:16 -0500427int
428smb_send(struct TCP_Server_Info *server, struct smb_hdr *smb_buffer,
429 unsigned int smb_buf_length)
430{
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800431 struct kvec iov[2];
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700432 struct smb_rqst rqst = { .rq_iov = iov,
433 .rq_nvec = 2 };
Jeff Layton0496e022008-12-30 12:39:16 -0500434
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800435 iov[0].iov_base = smb_buffer;
436 iov[0].iov_len = 4;
437 iov[1].iov_base = (char *)smb_buffer + 4;
438 iov[1].iov_len = smb_buf_length;
Jeff Layton0496e022008-12-30 12:39:16 -0500439
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000440 return __smb_send_rqst(server, 1, &rqst);
Jeff Layton0496e022008-12-30 12:39:16 -0500441}
442
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300443static int
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400444wait_for_free_credits(struct TCP_Server_Info *server, const int timeout,
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300445 int *credits)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000446{
Pavel Shilovsky5bc59492012-02-21 19:56:08 +0300447 int rc;
448
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300449 spin_lock(&server->req_lock);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400450 if (timeout == CIFS_ASYNC_OP) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000451 /* oplock breaks must not be held up */
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300452 server->in_flight++;
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300453 *credits -= 1;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300454 spin_unlock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000455 return 0;
456 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000457
Volker Lendecke27a97a62008-12-08 20:59:39 +0000458 while (1) {
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300459 if (*credits <= 0) {
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300460 spin_unlock(&server->req_lock);
Steve French789e6662011-08-09 18:44:44 +0000461 cifs_num_waiters_inc(server);
Pavel Shilovsky5bc59492012-02-21 19:56:08 +0300462 rc = wait_event_killable(server->request_q,
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300463 has_credits(server, credits));
Steve French789e6662011-08-09 18:44:44 +0000464 cifs_num_waiters_dec(server);
Pavel Shilovsky5bc59492012-02-21 19:56:08 +0300465 if (rc)
466 return rc;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300467 spin_lock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000468 } else {
Jeff Laytonc5797a92011-01-11 07:24:01 -0500469 if (server->tcpStatus == CifsExiting) {
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300470 spin_unlock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000471 return -ENOENT;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000472 }
Volker Lendecke27a97a62008-12-08 20:59:39 +0000473
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400474 /*
475 * Can not count locking commands against total
476 * as they are allowed to block on server.
477 */
Volker Lendecke27a97a62008-12-08 20:59:39 +0000478
479 /* update # of requests on the wire to server */
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400480 if (timeout != CIFS_BLOCKING_OP) {
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300481 *credits -= 1;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300482 server->in_flight++;
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400483 }
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300484 spin_unlock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000485 break;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000486 }
487 }
488 return 0;
489}
490
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300491static int
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400492wait_for_free_request(struct TCP_Server_Info *server, const int timeout,
493 const int optype)
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300494{
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -0500495 int *val;
496
497 val = server->ops->get_credits_field(server, optype);
498 /* Since an echo is already inflight, no need to wait to send another */
499 if (*val <= 0 && optype == CIFS_ECHO_OP)
500 return -EAGAIN;
501 return wait_for_free_credits(server, timeout, val);
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300502}
503
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400504int
505cifs_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
506 unsigned int *num, unsigned int *credits)
507{
508 *num = size;
509 *credits = 0;
510 return 0;
511}
512
Steve French96daf2b2011-05-27 04:34:02 +0000513static int allocate_mid(struct cifs_ses *ses, struct smb_hdr *in_buf,
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000514 struct mid_q_entry **ppmidQ)
515{
516 if (ses->server->tcpStatus == CifsExiting) {
517 return -ENOENT;
Volker Lendecke8fbbd362008-12-06 13:12:34 +0100518 }
519
520 if (ses->server->tcpStatus == CifsNeedReconnect) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500521 cifs_dbg(FYI, "tcp session dead - return to caller to retry\n");
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000522 return -EAGAIN;
Volker Lendecke8fbbd362008-12-06 13:12:34 +0100523 }
524
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500525 if (ses->status == CifsNew) {
Steve French79a58d12007-07-06 22:44:50 +0000526 if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
Steve Frenchad7a2922008-02-07 23:25:02 +0000527 (in_buf->Command != SMB_COM_NEGOTIATE))
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000528 return -EAGAIN;
Steve Frenchad7a2922008-02-07 23:25:02 +0000529 /* else ok - we are setting up session */
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000530 }
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500531
532 if (ses->status == CifsExiting) {
533 /* check if SMB session is bad because we are setting it up */
534 if (in_buf->Command != SMB_COM_LOGOFF_ANDX)
535 return -EAGAIN;
536 /* else ok - we are shutting down session */
537 }
538
Jeff Layton24b9b062008-12-01 07:09:34 -0500539 *ppmidQ = AllocMidQEntry(in_buf, ses->server);
Steve French26f57362007-08-30 22:09:15 +0000540 if (*ppmidQ == NULL)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000541 return -ENOMEM;
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500542 spin_lock(&GlobalMid_Lock);
543 list_add_tail(&(*ppmidQ)->qhead, &ses->server->pending_mid_q);
544 spin_unlock(&GlobalMid_Lock);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000545 return 0;
546}
547
Jeff Layton0ade6402011-01-11 07:24:02 -0500548static int
549wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *midQ)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000550{
Jeff Layton0ade6402011-01-11 07:24:02 -0500551 int error;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000552
Colin Cross5853cc22013-05-07 17:52:05 +0000553 error = wait_event_freezekillable_unsafe(server->response_q,
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400554 midQ->mid_state != MID_REQUEST_SUBMITTED);
Jeff Layton0ade6402011-01-11 07:24:02 -0500555 if (error < 0)
556 return -ERESTARTSYS;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000557
Jeff Layton0ade6402011-01-11 07:24:02 -0500558 return 0;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000559}
560
Jeff Laytonfec344e2012-09-18 16:20:35 -0700561struct mid_q_entry *
562cifs_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400563{
564 int rc;
Jeff Laytonfec344e2012-09-18 16:20:35 -0700565 struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400566 struct mid_q_entry *mid;
567
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800568 if (rqst->rq_iov[0].iov_len != 4 ||
569 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
570 return ERR_PTR(-EIO);
571
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400572 /* enable signing if server requires it */
Jeff Layton38d77c52013-05-26 07:01:00 -0400573 if (server->sign)
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400574 hdr->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
575
576 mid = AllocMidQEntry(hdr, server);
577 if (mid == NULL)
Jeff Laytonfec344e2012-09-18 16:20:35 -0700578 return ERR_PTR(-ENOMEM);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400579
Jeff Laytonfec344e2012-09-18 16:20:35 -0700580 rc = cifs_sign_rqst(rqst, server, &mid->sequence_number);
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100581 if (rc) {
582 DeleteMidQEntry(mid);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700583 return ERR_PTR(rc);
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100584 }
585
Jeff Laytonfec344e2012-09-18 16:20:35 -0700586 return mid;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400587}
Steve French133672e2007-11-13 22:41:37 +0000588
589/*
Jeff Laytona6827c12011-01-11 07:24:21 -0500590 * Send a SMB request and set the callback function in the mid to handle
591 * the result. Caller is responsible for dealing with timeouts.
592 */
593int
Jeff Laytonfec344e2012-09-18 16:20:35 -0700594cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst,
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -0800595 mid_receive_t *receive, mid_callback_t *callback,
596 mid_handle_t *handle, void *cbdata, const int flags)
Jeff Laytona6827c12011-01-11 07:24:21 -0500597{
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400598 int rc, timeout, optype;
Jeff Laytona6827c12011-01-11 07:24:21 -0500599 struct mid_q_entry *mid;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400600 unsigned int credits = 0;
Jeff Laytona6827c12011-01-11 07:24:21 -0500601
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400602 timeout = flags & CIFS_TIMEOUT_MASK;
603 optype = flags & CIFS_OP_MASK;
604
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400605 if ((flags & CIFS_HAS_CREDITS) == 0) {
606 rc = wait_for_free_request(server, timeout, optype);
607 if (rc)
608 return rc;
609 credits = 1;
610 }
Jeff Laytona6827c12011-01-11 07:24:21 -0500611
612 mutex_lock(&server->srv_mutex);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700613 mid = server->ops->setup_async_request(server, rqst);
614 if (IS_ERR(mid)) {
Jeff Laytona6827c12011-01-11 07:24:21 -0500615 mutex_unlock(&server->srv_mutex);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400616 add_credits_and_wake_if(server, credits, optype);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700617 return PTR_ERR(mid);
Jeff Laytona6827c12011-01-11 07:24:21 -0500618 }
619
Jeff Layton44d22d82011-10-19 15:29:49 -0400620 mid->receive = receive;
Jeff Laytona6827c12011-01-11 07:24:21 -0500621 mid->callback = callback;
622 mid->callback_data = cbdata;
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -0800623 mid->handle = handle;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400624 mid->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +0000625
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100626 /* put it on the pending_mid_q */
627 spin_lock(&GlobalMid_Lock);
628 list_add_tail(&mid->qhead, &server->pending_mid_q);
629 spin_unlock(&GlobalMid_Lock);
630
Long Li93d2cb62017-06-28 15:55:55 -0700631 /*
632 * Need to store the time in mid before calling I/O. For call_async,
633 * I/O response may come back and free the mid entry on another thread.
634 */
635 cifs_save_when_sent(mid);
Steve French789e6662011-08-09 18:44:44 +0000636 cifs_in_send_inc(server);
Ronnie Sahlberg1f3a8f52018-08-01 09:26:12 +1000637 rc = smb_send_rqst(server, 1, rqst, flags);
Steve French789e6662011-08-09 18:44:44 +0000638 cifs_in_send_dec(server);
Jeff Laytonad313cb2013-04-03 10:27:36 -0400639
Rabin Vincent820962d2015-12-23 07:32:41 +0100640 if (rc < 0) {
Jeff Laytonad313cb2013-04-03 10:27:36 -0400641 server->sequence_number -= 2;
Rabin Vincent820962d2015-12-23 07:32:41 +0100642 cifs_delete_mid(mid);
643 }
644
Jeff Laytona6827c12011-01-11 07:24:21 -0500645 mutex_unlock(&server->srv_mutex);
Steve French789e6662011-08-09 18:44:44 +0000646
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100647 if (rc == 0)
648 return 0;
Jeff Laytona6827c12011-01-11 07:24:21 -0500649
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400650 add_credits_and_wake_if(server, credits, optype);
Jeff Laytona6827c12011-01-11 07:24:21 -0500651 return rc;
652}
653
654/*
Steve French133672e2007-11-13 22:41:37 +0000655 *
656 * Send an SMB Request. No response info (other than return code)
657 * needs to be parsed.
658 *
659 * flags indicate the type of request buffer and how long to wait
660 * and whether to log NT STATUS code (error) before mapping it to POSIX error
661 *
662 */
663int
Steve French96daf2b2011-05-27 04:34:02 +0000664SendReceiveNoRsp(const unsigned int xid, struct cifs_ses *ses,
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400665 char *in_buf, int flags)
Steve French133672e2007-11-13 22:41:37 +0000666{
667 int rc;
668 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700669 struct kvec rsp_iov;
Steve French133672e2007-11-13 22:41:37 +0000670 int resp_buf_type;
671
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400672 iov[0].iov_base = in_buf;
673 iov[0].iov_len = get_rfc1002_length(in_buf) + 4;
Steve French133672e2007-11-13 22:41:37 +0000674 flags |= CIFS_NO_RESP;
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700675 rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Joe Perchesf96637b2013-05-04 22:12:25 -0500676 cifs_dbg(NOISY, "SendRcvNoRsp flags %d rc %d\n", flags, rc);
Steve French90c81e02008-02-12 20:32:36 +0000677
Steve French133672e2007-11-13 22:41:37 +0000678 return rc;
679}
680
Jeff Layton053d5032011-01-11 07:24:02 -0500681static int
Jeff Layton3c1105d2011-05-22 07:09:13 -0400682cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
Jeff Layton053d5032011-01-11 07:24:02 -0500683{
684 int rc = 0;
685
Joe Perchesf96637b2013-05-04 22:12:25 -0500686 cifs_dbg(FYI, "%s: cmd=%d mid=%llu state=%d\n",
687 __func__, le16_to_cpu(mid->command), mid->mid, mid->mid_state);
Jeff Layton053d5032011-01-11 07:24:02 -0500688
Jeff Layton74dd92a2011-01-11 07:24:02 -0500689 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400690 switch (mid->mid_state) {
Jeff Layton74dd92a2011-01-11 07:24:02 -0500691 case MID_RESPONSE_RECEIVED:
Jeff Layton053d5032011-01-11 07:24:02 -0500692 spin_unlock(&GlobalMid_Lock);
693 return rc;
Jeff Layton74dd92a2011-01-11 07:24:02 -0500694 case MID_RETRY_NEEDED:
695 rc = -EAGAIN;
696 break;
Jeff Layton71823ba2011-02-10 08:03:50 -0500697 case MID_RESPONSE_MALFORMED:
698 rc = -EIO;
699 break;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400700 case MID_SHUTDOWN:
701 rc = -EHOSTDOWN;
702 break;
Jeff Layton74dd92a2011-01-11 07:24:02 -0500703 default:
Jeff Layton3c1105d2011-05-22 07:09:13 -0400704 list_del_init(&mid->qhead);
Joe Perchesf96637b2013-05-04 22:12:25 -0500705 cifs_dbg(VFS, "%s: invalid mid state mid=%llu state=%d\n",
706 __func__, mid->mid, mid->mid_state);
Jeff Layton74dd92a2011-01-11 07:24:02 -0500707 rc = -EIO;
Jeff Layton053d5032011-01-11 07:24:02 -0500708 }
709 spin_unlock(&GlobalMid_Lock);
710
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500711 DeleteMidQEntry(mid);
Jeff Layton053d5032011-01-11 07:24:02 -0500712 return rc;
713}
714
Jeff Layton121b0462012-05-15 12:21:10 -0400715static inline int
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800716send_cancel(struct TCP_Server_Info *server, struct smb_rqst *rqst,
717 struct mid_q_entry *mid)
Jeff Layton76dcc262011-01-11 07:24:24 -0500718{
Jeff Layton121b0462012-05-15 12:21:10 -0400719 return server->ops->send_cancel ?
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800720 server->ops->send_cancel(server, rqst, mid) : 0;
Jeff Layton76dcc262011-01-11 07:24:24 -0500721}
722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723int
Jeff Layton2c8f9812011-05-19 16:22:52 -0400724cifs_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
725 bool log_error)
726{
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400727 unsigned int len = get_rfc1002_length(mid->resp_buf) + 4;
Jeff Layton826a95e2011-10-11 06:41:32 -0400728
729 dump_smb(mid->resp_buf, min_t(u32, 92, len));
Jeff Layton2c8f9812011-05-19 16:22:52 -0400730
731 /* convert the length into a more usable form */
Jeff Layton38d77c52013-05-26 07:01:00 -0400732 if (server->sign) {
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800733 struct kvec iov[2];
Steve French985e4ff02012-08-03 09:42:45 -0500734 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800735 struct smb_rqst rqst = { .rq_iov = iov,
736 .rq_nvec = 2 };
Jeff Layton826a95e2011-10-11 06:41:32 -0400737
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800738 iov[0].iov_base = mid->resp_buf;
739 iov[0].iov_len = 4;
740 iov[1].iov_base = (char *)mid->resp_buf + 4;
741 iov[1].iov_len = len - 4;
Jeff Layton2c8f9812011-05-19 16:22:52 -0400742 /* FIXME: add code to kill session */
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700743 rc = cifs_verify_signature(&rqst, server,
Jeff Layton0124cc42013-04-03 11:55:03 -0400744 mid->sequence_number);
Steve French985e4ff02012-08-03 09:42:45 -0500745 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500746 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
747 rc);
Jeff Layton2c8f9812011-05-19 16:22:52 -0400748 }
749
750 /* BB special case reconnect tid and uid here? */
751 return map_smb_to_linux_error(mid->resp_buf, log_error);
752}
753
Jeff Laytonfec344e2012-09-18 16:20:35 -0700754struct mid_q_entry *
755cifs_setup_request(struct cifs_ses *ses, struct smb_rqst *rqst)
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400756{
757 int rc;
Jeff Laytonfec344e2012-09-18 16:20:35 -0700758 struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400759 struct mid_q_entry *mid;
760
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800761 if (rqst->rq_iov[0].iov_len != 4 ||
762 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
763 return ERR_PTR(-EIO);
764
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400765 rc = allocate_mid(ses, hdr, &mid);
766 if (rc)
Jeff Laytonfec344e2012-09-18 16:20:35 -0700767 return ERR_PTR(rc);
768 rc = cifs_sign_rqst(rqst, ses->server, &mid->sequence_number);
769 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700770 cifs_delete_mid(mid);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700771 return ERR_PTR(rc);
772 }
773 return mid;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400774}
775
Ronnie Sahlberg4e34feb2018-08-30 10:13:00 +1000776static void
777cifs_noop_callback(struct mid_q_entry *mid)
778{
779}
780
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800781int
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000782compound_send_recv(const unsigned int xid, struct cifs_ses *ses,
783 const int flags, const int num_rqst, struct smb_rqst *rqst,
784 int *resp_buf_type, struct kvec *resp_iov)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000786 int i, j, rc = 0;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400787 int timeout, optype;
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000788 struct mid_q_entry *midQ[MAX_COMPOUND];
Ronnie Sahlbergcb5c2e62018-10-10 15:29:06 +1000789 unsigned int credits = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800790 char *buf;
Steve French50c2f752007-07-13 00:33:32 +0000791
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400792 timeout = flags & CIFS_TIMEOUT_MASK;
793 optype = flags & CIFS_OP_MASK;
Steve French133672e2007-11-13 22:41:37 +0000794
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000795 for (i = 0; i < num_rqst; i++)
796 resp_buf_type[i] = CIFS_NO_BUFFER; /* no response buf yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Steve French4b8f9302006-02-26 16:41:18 +0000798 if ((ses == NULL) || (ses->server == NULL)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500799 cifs_dbg(VFS, "Null session\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return -EIO;
801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700803 if (ses->server->tcpStatus == CifsExiting)
Steve French31ca3bc2005-04-28 22:41:11 -0700804 return -ENOENT;
805
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400806 /*
807 * Ensure that we do not send more than 50 overlapping requests
808 * to the same server. We may make this configurable later or
809 * use ses->maxReq.
810 */
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400811 rc = wait_for_free_request(ses->server, timeout, optype);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700812 if (rc)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000813 return rc;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000814
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400815 /*
816 * Make sure that we sign in the same order that we send on this socket
817 * and avoid races inside tcp sendmsg code that could cause corruption
818 * of smb data.
819 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Jeff Layton72ca5452008-12-01 07:09:36 -0500821 mutex_lock(&ses->server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000823 for (i = 0; i < num_rqst; i++) {
824 midQ[i] = ses->server->ops->setup_request(ses, &rqst[i]);
825 if (IS_ERR(midQ[i])) {
826 for (j = 0; j < i; j++)
827 cifs_delete_mid(midQ[j]);
828 mutex_unlock(&ses->server->srv_mutex);
829 /* Update # of requests on wire to server */
830 add_credits(ses->server, 1, optype);
831 return PTR_ERR(midQ[i]);
832 }
833
834 midQ[i]->mid_state = MID_REQUEST_SUBMITTED;
Ronnie Sahlberg4e34feb2018-08-30 10:13:00 +1000835 /*
836 * We don't invoke the callback compounds unless it is the last
837 * request.
838 */
839 if (i < num_rqst - 1)
840 midQ[i]->callback = cifs_noop_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
Steve French789e6662011-08-09 18:44:44 +0000842 cifs_in_send_inc(ses->server);
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000843 rc = smb_send_rqst(ses->server, num_rqst, rqst, flags);
Steve French789e6662011-08-09 18:44:44 +0000844 cifs_in_send_dec(ses->server);
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000845
846 for (i = 0; i < num_rqst; i++)
847 cifs_save_when_sent(midQ[i]);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000848
Jeff Laytonad313cb2013-04-03 10:27:36 -0400849 if (rc < 0)
850 ses->server->sequence_number -= 2;
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000851
Jeff Layton72ca5452008-12-01 07:09:36 -0500852 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000853
Ronnie Sahlbergcb5c2e62018-10-10 15:29:06 +1000854 if (rc < 0)
855 goto out;
856
857 /*
858 * Compounding is never used during session establish.
859 */
860 if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP))
861 smb311_update_preauth_hash(ses, rqst[0].rq_iov,
862 rqst[0].rq_nvec);
863
864 if (timeout == CIFS_ASYNC_OP)
865 goto out;
866
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000867 for (i = 0; i < num_rqst; i++) {
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000868 rc = wait_for_response(ses->server, midQ[i]);
869 if (rc != 0) {
870 cifs_dbg(FYI, "Cancelling wait for mid %llu\n",
871 midQ[i]->mid);
872 send_cancel(ses->server, &rqst[i], midQ[i]);
873 spin_lock(&GlobalMid_Lock);
874 if (midQ[i]->mid_state == MID_REQUEST_SUBMITTED) {
875 midQ[i]->mid_flags |= MID_WAIT_CANCELLED;
876 midQ[i]->callback = DeleteMidQEntry;
877 spin_unlock(&GlobalMid_Lock);
878 add_credits(ses->server, 1, optype);
879 return rc;
880 }
Jeff Layton1be912d2011-01-28 07:08:28 -0500881 spin_unlock(&GlobalMid_Lock);
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000882 }
Ronnie Sahlbergcb5c2e62018-10-10 15:29:06 +1000883 }
884
885 for (i = 0; i < num_rqst; i++)
886 if (midQ[i]->resp_buf)
887 credits += ses->server->ops->get_credits(midQ[i]);
888 if (!credits)
889 credits = 1;
890
891 for (i = 0; i < num_rqst; i++) {
892 if (rc < 0)
893 goto out;
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000894
895 rc = cifs_sync_mid_result(midQ[i], ses->server);
896 if (rc != 0) {
Ronnie Sahlbergcb5c2e62018-10-10 15:29:06 +1000897 add_credits(ses->server, credits, optype);
Jeff Layton1be912d2011-01-28 07:08:28 -0500898 return rc;
899 }
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000900
901 if (!midQ[i]->resp_buf ||
902 midQ[i]->mid_state != MID_RESPONSE_RECEIVED) {
903 rc = -EIO;
904 cifs_dbg(FYI, "Bad MID state?\n");
905 goto out;
906 }
907
908 buf = (char *)midQ[i]->resp_buf;
909 resp_iov[i].iov_base = buf;
910 resp_iov[i].iov_len = midQ[i]->resp_buf_size +
911 ses->server->vals->header_preamble_size;
912
913 if (midQ[i]->large_buf)
914 resp_buf_type[i] = CIFS_LARGE_BUFFER;
915 else
916 resp_buf_type[i] = CIFS_SMALL_BUFFER;
917
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000918 rc = ses->server->ops->check_receive(midQ[i], ses->server,
919 flags & CIFS_LOG_ERROR);
920
921 /* mark it so buf will not be freed by cifs_delete_mid */
922 if ((flags & CIFS_NO_RESP) == 0)
923 midQ[i]->resp_buf = NULL;
Ronnie Sahlbergcb5c2e62018-10-10 15:29:06 +1000924
Jeff Layton1be912d2011-01-28 07:08:28 -0500925 }
Ronnie Sahlbergcb5c2e62018-10-10 15:29:06 +1000926
927 /*
928 * Compounding is never used during session establish.
929 */
930 if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP)) {
931 struct kvec iov = {
932 .iov_base = resp_iov[0].iov_base,
933 .iov_len = resp_iov[0].iov_len
934 };
935 smb311_update_preauth_hash(ses, &iov, 1);
936 }
937
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000938out:
Ronnie Sahlberg4e34feb2018-08-30 10:13:00 +1000939 /*
940 * This will dequeue all mids. After this it is important that the
941 * demultiplex_thread will not process any of these mids any futher.
942 * This is prevented above by using a noop callback that will not
943 * wake this thread except for the very last PDU.
944 */
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000945 for (i = 0; i < num_rqst; i++)
946 cifs_delete_mid(midQ[i]);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400947 add_credits(ses->server, credits, optype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 return rc;
950}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952int
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000953cifs_send_recv(const unsigned int xid, struct cifs_ses *ses,
954 struct smb_rqst *rqst, int *resp_buf_type, const int flags,
955 struct kvec *resp_iov)
956{
957 return compound_send_recv(xid, ses, flags, 1, rqst, resp_buf_type,
958 resp_iov);
959}
960
961int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800962SendReceive2(const unsigned int xid, struct cifs_ses *ses,
963 struct kvec *iov, int n_vec, int *resp_buf_type /* ret */,
964 const int flags, struct kvec *resp_iov)
965{
966 struct smb_rqst rqst;
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +1100967 struct kvec s_iov[CIFS_MAX_IOV_SIZE], *new_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800968 int rc;
969
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +1100970 if (n_vec + 1 > CIFS_MAX_IOV_SIZE) {
Kees Cook6da2ec52018-06-12 13:55:00 -0700971 new_iov = kmalloc_array(n_vec + 1, sizeof(struct kvec),
972 GFP_KERNEL);
Steve French117e3b72018-04-22 10:24:19 -0500973 if (!new_iov) {
974 /* otherwise cifs_send_recv below sets resp_buf_type */
975 *resp_buf_type = CIFS_NO_BUFFER;
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +1100976 return -ENOMEM;
Steve French117e3b72018-04-22 10:24:19 -0500977 }
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +1100978 } else
979 new_iov = s_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800980
981 /* 1st iov is a RFC1001 length followed by the rest of the packet */
982 memcpy(new_iov + 1, iov, (sizeof(struct kvec) * n_vec));
983
984 new_iov[0].iov_base = new_iov[1].iov_base;
985 new_iov[0].iov_len = 4;
986 new_iov[1].iov_base += 4;
987 new_iov[1].iov_len -= 4;
988
989 memset(&rqst, 0, sizeof(struct smb_rqst));
990 rqst.rq_iov = new_iov;
991 rqst.rq_nvec = n_vec + 1;
992
993 rc = cifs_send_recv(xid, ses, &rqst, resp_buf_type, flags, resp_iov);
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +1100994 if (n_vec + 1 > CIFS_MAX_IOV_SIZE)
995 kfree(new_iov);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800996 return rc;
997}
998
999int
Steve French96daf2b2011-05-27 04:34:02 +00001000SendReceive(const unsigned int xid, struct cifs_ses *ses,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001002 int *pbytes_returned, const int timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
1004 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 struct mid_q_entry *midQ;
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001006 unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
1007 struct kvec iov = { .iov_base = in_buf, .iov_len = len };
1008 struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 if (ses == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001011 cifs_dbg(VFS, "Null smb session\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 return -EIO;
1013 }
Steve French79a58d12007-07-06 22:44:50 +00001014 if (ses->server == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001015 cifs_dbg(VFS, "Null tcp session\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return -EIO;
1017 }
1018
Steve French79a58d12007-07-06 22:44:50 +00001019 if (ses->server->tcpStatus == CifsExiting)
Steve French31ca3bc2005-04-28 22:41:11 -07001020 return -ENOENT;
1021
Steve French79a58d12007-07-06 22:44:50 +00001022 /* Ensure that we do not send more than 50 overlapping requests
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 to the same server. We may make this configurable later or
1024 use ses->maxReq */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001026 if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001027 cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001028 len);
Volker Lendecke6d9c6d52008-12-08 20:50:24 +00001029 return -EIO;
1030 }
1031
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001032 rc = wait_for_free_request(ses->server, timeout, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001033 if (rc)
1034 return rc;
1035
Steve French79a58d12007-07-06 22:44:50 +00001036 /* make sure that we sign in the same order that we send on this socket
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 and avoid races inside tcp sendmsg code that could cause corruption
1038 of smb data */
1039
Jeff Layton72ca5452008-12-01 07:09:36 -05001040 mutex_lock(&ses->server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001042 rc = allocate_mid(ses, in_buf, &midQ);
1043 if (rc) {
Jeff Layton72ca5452008-12-01 07:09:36 -05001044 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001045 /* Update # of requests on wire to server */
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001046 add_credits(ses->server, 1, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001047 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 }
1049
Steve Frenchad009ac2005-04-28 22:41:05 -07001050 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
Volker Lendecke829049c2008-12-06 16:00:53 +01001051 if (rc) {
1052 mutex_unlock(&ses->server->srv_mutex);
1053 goto out;
1054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001056 midQ->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +00001057
1058 cifs_in_send_inc(ses->server);
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001059 rc = smb_send(ses->server, in_buf, len);
Steve French789e6662011-08-09 18:44:44 +00001060 cifs_in_send_dec(ses->server);
1061 cifs_save_when_sent(midQ);
Jeff Laytonad313cb2013-04-03 10:27:36 -04001062
1063 if (rc < 0)
1064 ses->server->sequence_number -= 2;
1065
Jeff Layton72ca5452008-12-01 07:09:36 -05001066 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001067
Steve French79a58d12007-07-06 22:44:50 +00001068 if (rc < 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001069 goto out;
1070
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001071 if (timeout == CIFS_ASYNC_OP)
Steve French133672e2007-11-13 22:41:37 +00001072 goto out;
Jeff Layton0ade6402011-01-11 07:24:02 -05001073
1074 rc = wait_for_response(ses->server, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -05001075 if (rc != 0) {
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001076 send_cancel(ses->server, &rqst, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -05001077 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001078 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
Jeff Layton1be912d2011-01-28 07:08:28 -05001079 /* no longer considered to be "in-flight" */
1080 midQ->callback = DeleteMidQEntry;
1081 spin_unlock(&GlobalMid_Lock);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001082 add_credits(ses->server, 1, 0);
Jeff Layton1be912d2011-01-28 07:08:28 -05001083 return rc;
1084 }
1085 spin_unlock(&GlobalMid_Lock);
1086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Jeff Layton3c1105d2011-05-22 07:09:13 -04001088 rc = cifs_sync_mid_result(midQ, ses->server);
Jeff Layton053d5032011-01-11 07:24:02 -05001089 if (rc != 0) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001090 add_credits(ses->server, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 return rc;
1092 }
Steve French50c2f752007-07-13 00:33:32 +00001093
Jeff Layton2c8f9812011-05-19 16:22:52 -04001094 if (!midQ->resp_buf || !out_buf ||
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001095 midQ->mid_state != MID_RESPONSE_RECEIVED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 rc = -EIO;
Joe Perchesf96637b2013-05-04 22:12:25 -05001097 cifs_dbg(VFS, "Bad MID state?\n");
Steve French2b2bdfb2008-12-11 17:26:54 +00001098 goto out;
1099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001101 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
Jeff Layton2c8f9812011-05-19 16:22:52 -04001102 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
1103 rc = cifs_check_receive(midQ, ses->server, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001104out:
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001105 cifs_delete_mid(midQ);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001106 add_credits(ses->server, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 return rc;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001109}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001111/* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
1112 blocking lock to return. */
1113
1114static int
Steve French96daf2b2011-05-27 04:34:02 +00001115send_lock_cancel(const unsigned int xid, struct cifs_tcon *tcon,
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001116 struct smb_hdr *in_buf,
1117 struct smb_hdr *out_buf)
1118{
1119 int bytes_returned;
Steve French96daf2b2011-05-27 04:34:02 +00001120 struct cifs_ses *ses = tcon->ses;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001121 LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
1122
1123 /* We just modify the current in_buf to change
1124 the type of lock from LOCKING_ANDX_SHARED_LOCK
1125 or LOCKING_ANDX_EXCLUSIVE_LOCK to
1126 LOCKING_ANDX_CANCEL_LOCK. */
1127
1128 pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
1129 pSMB->Timeout = 0;
Pavel Shilovsky88257362012-05-23 14:01:59 +04001130 pSMB->hdr.Mid = get_next_mid(ses->server);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001131
1132 return SendReceive(xid, ses, in_buf, out_buf,
Jeff Layton77499812011-01-11 07:24:23 -05001133 &bytes_returned, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001134}
1135
1136int
Steve French96daf2b2011-05-27 04:34:02 +00001137SendReceiveBlockingLock(const unsigned int xid, struct cifs_tcon *tcon,
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001138 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
1139 int *pbytes_returned)
1140{
1141 int rc = 0;
1142 int rstart = 0;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001143 struct mid_q_entry *midQ;
Steve French96daf2b2011-05-27 04:34:02 +00001144 struct cifs_ses *ses;
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001145 unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
1146 struct kvec iov = { .iov_base = in_buf, .iov_len = len };
1147 struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001148
1149 if (tcon == NULL || tcon->ses == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001150 cifs_dbg(VFS, "Null smb session\n");
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001151 return -EIO;
1152 }
1153 ses = tcon->ses;
1154
Steve French79a58d12007-07-06 22:44:50 +00001155 if (ses->server == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001156 cifs_dbg(VFS, "Null tcp session\n");
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001157 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
1159
Steve French79a58d12007-07-06 22:44:50 +00001160 if (ses->server->tcpStatus == CifsExiting)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001161 return -ENOENT;
1162
Steve French79a58d12007-07-06 22:44:50 +00001163 /* Ensure that we do not send more than 50 overlapping requests
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001164 to the same server. We may make this configurable later or
1165 use ses->maxReq */
1166
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001167 if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001168 cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001169 len);
Volker Lendecke6d9c6d52008-12-08 20:50:24 +00001170 return -EIO;
1171 }
1172
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001173 rc = wait_for_free_request(ses->server, CIFS_BLOCKING_OP, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001174 if (rc)
1175 return rc;
1176
Steve French79a58d12007-07-06 22:44:50 +00001177 /* make sure that we sign in the same order that we send on this socket
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001178 and avoid races inside tcp sendmsg code that could cause corruption
1179 of smb data */
1180
Jeff Layton72ca5452008-12-01 07:09:36 -05001181 mutex_lock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001182
1183 rc = allocate_mid(ses, in_buf, &midQ);
1184 if (rc) {
Jeff Layton72ca5452008-12-01 07:09:36 -05001185 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001186 return rc;
1187 }
1188
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001189 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
Volker Lendecke829049c2008-12-06 16:00:53 +01001190 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001191 cifs_delete_mid(midQ);
Volker Lendecke829049c2008-12-06 16:00:53 +01001192 mutex_unlock(&ses->server->srv_mutex);
1193 return rc;
1194 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001195
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001196 midQ->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +00001197 cifs_in_send_inc(ses->server);
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001198 rc = smb_send(ses->server, in_buf, len);
Steve French789e6662011-08-09 18:44:44 +00001199 cifs_in_send_dec(ses->server);
1200 cifs_save_when_sent(midQ);
Jeff Laytonad313cb2013-04-03 10:27:36 -04001201
1202 if (rc < 0)
1203 ses->server->sequence_number -= 2;
1204
Jeff Layton72ca5452008-12-01 07:09:36 -05001205 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001206
Steve French79a58d12007-07-06 22:44:50 +00001207 if (rc < 0) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001208 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001209 return rc;
1210 }
1211
1212 /* Wait for a reply - allow signals to interrupt. */
1213 rc = wait_event_interruptible(ses->server->response_q,
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001214 (!(midQ->mid_state == MID_REQUEST_SUBMITTED)) ||
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001215 ((ses->server->tcpStatus != CifsGood) &&
1216 (ses->server->tcpStatus != CifsNew)));
1217
1218 /* Were we interrupted by a signal ? */
1219 if ((rc == -ERESTARTSYS) &&
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001220 (midQ->mid_state == MID_REQUEST_SUBMITTED) &&
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001221 ((ses->server->tcpStatus == CifsGood) ||
1222 (ses->server->tcpStatus == CifsNew))) {
1223
1224 if (in_buf->Command == SMB_COM_TRANSACTION2) {
1225 /* POSIX lock. We send a NT_CANCEL SMB to cause the
1226 blocking lock to return. */
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001227 rc = send_cancel(ses->server, &rqst, midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001228 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001229 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001230 return rc;
1231 }
1232 } else {
1233 /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
1234 to cause the blocking lock to return. */
1235
1236 rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
1237
1238 /* If we get -ENOLCK back the lock may have
1239 already been removed. Don't exit in this case. */
1240 if (rc && rc != -ENOLCK) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001241 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001242 return rc;
1243 }
1244 }
1245
Jeff Layton1be912d2011-01-28 07:08:28 -05001246 rc = wait_for_response(ses->server, midQ);
1247 if (rc) {
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001248 send_cancel(ses->server, &rqst, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -05001249 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001250 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
Jeff Layton1be912d2011-01-28 07:08:28 -05001251 /* no longer considered to be "in-flight" */
1252 midQ->callback = DeleteMidQEntry;
1253 spin_unlock(&GlobalMid_Lock);
1254 return rc;
1255 }
1256 spin_unlock(&GlobalMid_Lock);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001257 }
Jeff Layton1be912d2011-01-28 07:08:28 -05001258
1259 /* We got the response - restart system call. */
1260 rstart = 1;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001261 }
1262
Jeff Layton3c1105d2011-05-22 07:09:13 -04001263 rc = cifs_sync_mid_result(midQ, ses->server);
Jeff Layton053d5032011-01-11 07:24:02 -05001264 if (rc != 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001265 return rc;
Steve French50c2f752007-07-13 00:33:32 +00001266
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001267 /* rcvd frame is ok */
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001268 if (out_buf == NULL || midQ->mid_state != MID_RESPONSE_RECEIVED) {
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001269 rc = -EIO;
Joe Perchesf96637b2013-05-04 22:12:25 -05001270 cifs_dbg(VFS, "Bad MID state?\n");
Volker Lendecke698e96a2008-12-06 16:39:31 +01001271 goto out;
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001272 }
1273
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001274 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
Jeff Layton2c8f9812011-05-19 16:22:52 -04001275 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
1276 rc = cifs_check_receive(midQ, ses->server, 0);
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001277out:
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001278 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001279 if (rstart && rc == -EACCES)
1280 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 return rc;
1282}