blob: e047f06c9b4fb78223a1e8b01d5abb9636de9aaf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/transport.c
3 *
Steve Frenchad7a2922008-02-07 23:25:02 +00004 * Copyright (C) International Business Machines Corp., 2002,2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Author(s): Steve French (sfrench@us.ibm.com)
Steve French14a441a2b2006-07-16 04:32:51 +00006 * Jeremy Allison (jra@samba.org) 2006.
Steve French79a58d12007-07-06 22:44:50 +00007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published
10 * by the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
16 * the GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this library; if not, write to the Free Software
Steve French79a58d12007-07-06 22:44:50 +000020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 */
22
23#include <linux/fs.h>
24#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/wait.h>
27#include <linux/net.h>
28#include <linux/delay.h>
Jeff Laytonf06ac722011-10-19 15:30:40 -040029#include <linux/freezer.h>
Jeff Laytonb8eed282012-09-18 16:20:35 -070030#include <linux/tcp.h>
Christoph Hellwig2f8b5442016-11-01 07:40:13 -060031#include <linux/bvec.h>
Jeff Layton97bc00b2012-09-18 16:20:35 -070032#include <linux/highmem.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080033#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/processor.h>
35#include <linux/mempool.h>
36#include "cifspdu.h"
37#include "cifsglob.h"
38#include "cifsproto.h"
39#include "cifs_debug.h"
Aurelien Aptel8bd68c62018-02-16 19:19:29 +010040#include "smb2proto.h"
Long Li9762c2d2017-11-22 17:38:43 -070041#include "smbdirect.h"
Steve French50c2f752007-07-13 00:33:32 +000042
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +110043/* Max number of iovectors we can use off the stack when sending requests. */
44#define CIFS_MAX_IOV_SIZE 8
45
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +040046void
47cifs_wake_up_task(struct mid_q_entry *mid)
Jeff Layton2b84a36c2011-01-11 07:24:21 -050048{
49 wake_up_process(mid->callback_data);
50}
51
Jeff Laytona6827c12011-01-11 07:24:21 -050052struct mid_q_entry *
Jeff Layton24b9b062008-12-01 07:09:34 -050053AllocMidQEntry(const struct smb_hdr *smb_buffer, struct TCP_Server_Info *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55 struct mid_q_entry *temp;
56
Jeff Layton24b9b062008-12-01 07:09:34 -050057 if (server == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -050058 cifs_dbg(VFS, "Null TCP session in AllocMidQEntry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 return NULL;
60 }
Steve French50c2f752007-07-13 00:33:32 +000061
Pekka Enberg232087c2008-09-15 13:22:54 +030062 temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
NeilBrowna6f74e82017-04-10 12:08:53 +100063 memset(temp, 0, sizeof(struct mid_q_entry));
Lars Persson696e4202018-06-25 14:05:25 +020064 kref_init(&temp->refcount);
NeilBrowna6f74e82017-04-10 12:08:53 +100065 temp->mid = get_mid(smb_buffer);
66 temp->pid = current->pid;
67 temp->command = cpu_to_le16(smb_buffer->Command);
68 cifs_dbg(FYI, "For smb_command %d\n", smb_buffer->Command);
Steve French1047abc2005-10-11 19:58:06 -070069 /* do_gettimeofday(&temp->when_sent);*/ /* easier to use jiffies */
NeilBrowna6f74e82017-04-10 12:08:53 +100070 /* when mid allocated can be before when sent */
71 temp->when_alloc = jiffies;
72 temp->server = server;
Jeff Layton2b84a36c2011-01-11 07:24:21 -050073
NeilBrowna6f74e82017-04-10 12:08:53 +100074 /*
75 * The default is for the mid to be synchronous, so the
76 * default callback just wakes up the current task.
77 */
78 temp->callback = cifs_wake_up_task;
79 temp->callback_data = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 atomic_inc(&midCount);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -040082 temp->mid_state = MID_REQUEST_ALLOCATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 return temp;
84}
85
Lars Persson696e4202018-06-25 14:05:25 +020086static void _cifs_mid_q_entry_release(struct kref *refcount)
87{
88 struct mid_q_entry *mid = container_of(refcount, struct mid_q_entry,
89 refcount);
90
91 mempool_free(mid, cifs_mid_poolp);
92}
93
94void cifs_mid_q_entry_release(struct mid_q_entry *midEntry)
95{
96 spin_lock(&GlobalMid_Lock);
97 kref_put(&midEntry->refcount, _cifs_mid_q_entry_release);
98 spin_unlock(&GlobalMid_Lock);
99}
100
Jeff Layton766fdbb2011-01-11 07:24:21 -0500101void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102DeleteMidQEntry(struct mid_q_entry *midEntry)
103{
Steve French1047abc2005-10-11 19:58:06 -0700104#ifdef CONFIG_CIFS_STATS2
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400105 __le16 command = midEntry->server->vals->lock_cmd;
Steve French1047abc2005-10-11 19:58:06 -0700106 unsigned long now;
107#endif
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400108 midEntry->mid_state = MID_FREE;
Jeff Layton80975312011-01-11 07:24:02 -0500109 atomic_dec(&midCount);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400110 if (midEntry->large_buf)
Steve Frenchb8643e12005-04-28 22:41:07 -0700111 cifs_buf_release(midEntry->resp_buf);
112 else
113 cifs_small_buf_release(midEntry->resp_buf);
Steve French1047abc2005-10-11 19:58:06 -0700114#ifdef CONFIG_CIFS_STATS2
115 now = jiffies;
Steve French00778e22018-09-18 14:05:18 -0500116 /*
117 * commands taking longer than one second (default) can be indications
118 * that something is wrong, unless it is quite a slow link or a very
119 * busy server. Note that this calc is unlikely or impossible to wrap
120 * as long as slow_rsp_threshold is not set way above recommended max
121 * value (32767 ie 9 hours) and is generally harmless even if wrong
122 * since only affects debug counters - so leaving the calc as simple
123 * comparison rather than doing multiple conversions and overflow
124 * checks
125 */
126 if ((slow_rsp_threshold != 0) &&
127 time_after(now, midEntry->when_alloc + (slow_rsp_threshold * HZ)) &&
Steve French020eec52018-08-01 16:38:07 -0500128 (midEntry->command != command)) {
Steve Frenchf5942db2018-11-14 01:37:39 -0600129 /*
130 * smb2slowcmd[NUMBER_OF_SMB2_COMMANDS] counts by command
131 * NB: le16_to_cpu returns unsigned so can not be negative below
132 */
133 if (le16_to_cpu(midEntry->command) < NUMBER_OF_SMB2_COMMANDS)
Steve French468d6772018-08-04 05:24:34 -0500134 cifs_stats_inc(&midEntry->server->smb2slowcmd[le16_to_cpu(midEntry->command)]);
135
Steve French020eec52018-08-01 16:38:07 -0500136 trace_smb3_slow_rsp(le16_to_cpu(midEntry->command),
137 midEntry->mid, midEntry->pid,
138 midEntry->when_sent, midEntry->when_received);
139 if (cifsFYI & CIFS_TIMER) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +0300140 pr_debug(" CIFS slow rsp: cmd %d mid %llu",
Steve French1047abc2005-10-11 19:58:06 -0700141 midEntry->command, midEntry->mid);
Rodrigo Freiref80eaed2018-10-07 12:21:26 -0300142 cifs_info(" A: 0x%lx S: 0x%lx R: 0x%lx\n",
Steve French1047abc2005-10-11 19:58:06 -0700143 now - midEntry->when_alloc,
144 now - midEntry->when_sent,
145 now - midEntry->when_received);
146 }
147 }
148#endif
Lars Persson696e4202018-06-25 14:05:25 +0200149 cifs_mid_q_entry_release(midEntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700152void
153cifs_delete_mid(struct mid_q_entry *mid)
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500154{
155 spin_lock(&GlobalMid_Lock);
Ronnie Sahlbergddf83af2018-08-30 10:12:59 +1000156 list_del_init(&mid->qhead);
157 mid->mid_flags |= MID_DELETED;
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500158 spin_unlock(&GlobalMid_Lock);
159
160 DeleteMidQEntry(mid);
161}
162
Jeff Layton6f49f462012-09-18 16:20:34 -0700163/*
164 * smb_send_kvec - send an array of kvecs to the server
165 * @server: Server to send the data to
Al Viro3ab3f2a2015-11-13 02:36:04 -0500166 * @smb_msg: Message to send
Jeff Layton6f49f462012-09-18 16:20:34 -0700167 * @sent: amount of data sent on socket is stored here
168 *
169 * Our basic "send data to server" function. Should be called with srv_mutex
170 * held. The caller is responsible for handling the results.
171 */
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500172static int
Al Viro3ab3f2a2015-11-13 02:36:04 -0500173smb_send_kvec(struct TCP_Server_Info *server, struct msghdr *smb_msg,
174 size_t *sent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 int rc = 0;
Al Viro3ab3f2a2015-11-13 02:36:04 -0500177 int retries = 0;
Steve Frenchedf1ae42008-10-29 00:47:57 +0000178 struct socket *ssocket = server->ssocket;
Steve French50c2f752007-07-13 00:33:32 +0000179
Jeff Layton6f49f462012-09-18 16:20:34 -0700180 *sent = 0;
181
Al Viro3ab3f2a2015-11-13 02:36:04 -0500182 smb_msg->msg_name = (struct sockaddr *) &server->dstaddr;
183 smb_msg->msg_namelen = sizeof(struct sockaddr);
184 smb_msg->msg_control = NULL;
185 smb_msg->msg_controllen = 0;
Jeff Layton0496e022008-12-30 12:39:16 -0500186 if (server->noblocksnd)
Al Viro3ab3f2a2015-11-13 02:36:04 -0500187 smb_msg->msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL;
Steve Frenchedf1ae42008-10-29 00:47:57 +0000188 else
Al Viro3ab3f2a2015-11-13 02:36:04 -0500189 smb_msg->msg_flags = MSG_NOSIGNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Al Viro3ab3f2a2015-11-13 02:36:04 -0500191 while (msg_data_left(smb_msg)) {
Jeff Layton6f49f462012-09-18 16:20:34 -0700192 /*
193 * If blocking send, we try 3 times, since each can block
194 * for 5 seconds. For nonblocking we have to try more
195 * but wait increasing amounts of time allowing time for
196 * socket to clear. The overall time we wait in either
197 * case to send on the socket is about 15 seconds.
198 * Similarly we wait for 15 seconds for a response from
199 * the server in SendReceive[2] for the server to send
200 * a response back for most types of requests (except
201 * SMB Write past end of file which can be slow, and
202 * blocking lock operations). NFS waits slightly longer
203 * than CIFS, but this can make it take longer for
204 * nonresponsive servers to be detected and 15 seconds
205 * is more than enough time for modern networks to
206 * send a packet. In most cases if we fail to send
207 * after the retries we will kill the socket and
208 * reconnect which may clear the network problem.
209 */
Al Viro3ab3f2a2015-11-13 02:36:04 -0500210 rc = sock_sendmsg(ssocket, smb_msg);
Jeff Laytonce6c44e2013-03-22 08:36:45 -0400211 if (rc == -EAGAIN) {
Al Viro3ab3f2a2015-11-13 02:36:04 -0500212 retries++;
213 if (retries >= 14 ||
214 (!server->noblocksnd && (retries > 2))) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500215 cifs_dbg(VFS, "sends on sock %p stuck for 15 seconds\n",
216 ssocket);
Al Viro3ab3f2a2015-11-13 02:36:04 -0500217 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
Al Viro3ab3f2a2015-11-13 02:36:04 -0500219 msleep(1 << retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 continue;
221 }
Jeff Layton6f49f462012-09-18 16:20:34 -0700222
Steve French79a58d12007-07-06 22:44:50 +0000223 if (rc < 0)
Al Viro3ab3f2a2015-11-13 02:36:04 -0500224 return rc;
Jeff Layton6f49f462012-09-18 16:20:34 -0700225
Steve French79a58d12007-07-06 22:44:50 +0000226 if (rc == 0) {
Steve French3e844692005-10-03 13:37:24 -0700227 /* should never happen, letting socket clear before
228 retrying is our only obvious option here */
Joe Perchesf96637b2013-05-04 22:12:25 -0500229 cifs_dbg(VFS, "tcp sent no data\n");
Steve French3e844692005-10-03 13:37:24 -0700230 msleep(500);
231 continue;
232 }
Jeff Layton6f49f462012-09-18 16:20:34 -0700233
Al Viro3ab3f2a2015-11-13 02:36:04 -0500234 /* send was at least partially successful */
235 *sent += rc;
236 retries = 0; /* in case we get ENOSPC on the next send */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
Al Viro3ab3f2a2015-11-13 02:36:04 -0500238 return 0;
Jeff Layton97bc00b2012-09-18 16:20:35 -0700239}
240
Paulo Alcantara35e2cc12018-06-15 10:22:44 -0300241unsigned long
Ronnie Sahlberg81f39f92018-06-28 10:47:14 +1000242smb_rqst_len(struct TCP_Server_Info *server, struct smb_rqst *rqst)
Jeff Laytona26054d2014-02-14 07:21:00 -0500243{
244 unsigned int i;
Paulo Alcantara35e2cc12018-06-15 10:22:44 -0300245 struct kvec *iov;
246 int nvec;
Jeff Laytona26054d2014-02-14 07:21:00 -0500247 unsigned long buflen = 0;
248
Ronnie Sahlberg81f39f92018-06-28 10:47:14 +1000249 if (server->vals->header_preamble_size == 0 &&
250 rqst->rq_nvec >= 2 && rqst->rq_iov[0].iov_len == 4) {
Paulo Alcantara35e2cc12018-06-15 10:22:44 -0300251 iov = &rqst->rq_iov[1];
252 nvec = rqst->rq_nvec - 1;
253 } else {
254 iov = rqst->rq_iov;
255 nvec = rqst->rq_nvec;
256 }
257
Jeff Laytona26054d2014-02-14 07:21:00 -0500258 /* total up iov array first */
Paulo Alcantara35e2cc12018-06-15 10:22:44 -0300259 for (i = 0; i < nvec; i++)
Jeff Laytona26054d2014-02-14 07:21:00 -0500260 buflen += iov[i].iov_len;
261
Long Lic06a0f22018-05-30 12:47:57 -0700262 /*
263 * Add in the page array if there is one. The caller needs to make
264 * sure rq_offset and rq_tailsz are set correctly. If a buffer of
265 * multiple pages ends at page boundary, rq_tailsz needs to be set to
266 * PAGE_SIZE.
267 */
Jeff Laytona26054d2014-02-14 07:21:00 -0500268 if (rqst->rq_npages) {
Long Lic06a0f22018-05-30 12:47:57 -0700269 if (rqst->rq_npages == 1)
270 buflen += rqst->rq_tailsz;
271 else {
272 /*
273 * If there is more than one page, calculate the
274 * buffer length based on rq_offset and rq_tailsz
275 */
276 buflen += rqst->rq_pagesz * (rqst->rq_npages - 1) -
277 rqst->rq_offset;
278 buflen += rqst->rq_tailsz;
279 }
Jeff Laytona26054d2014-02-14 07:21:00 -0500280 }
281
282 return buflen;
283}
284
Jeff Layton6f49f462012-09-18 16:20:34 -0700285static int
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000286__smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
287 struct smb_rqst *rqst)
Jeff Layton6f49f462012-09-18 16:20:34 -0700288{
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000289 int rc = 0;
290 struct kvec *iov;
291 int n_vec;
292 unsigned int send_length = 0;
293 unsigned int i, j;
Al Viro3ab3f2a2015-11-13 02:36:04 -0500294 size_t total_len = 0, sent, size;
Jeff Laytonb8eed282012-09-18 16:20:35 -0700295 struct socket *ssocket = server->ssocket;
Al Viro3ab3f2a2015-11-13 02:36:04 -0500296 struct msghdr smb_msg;
Jeff Laytonb8eed282012-09-18 16:20:35 -0700297 int val = 1;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000298 __be32 rfc1002_marker;
299
Long Li9762c2d2017-11-22 17:38:43 -0700300 if (cifs_rdma_enabled(server) && server->smbd_conn) {
Ronnie Sahlberg81f39f92018-06-28 10:47:14 +1000301 rc = smbd_send(server, rqst);
Long Li9762c2d2017-11-22 17:38:43 -0700302 goto smbd_done;
303 }
Jeff Laytonea702b82012-12-27 07:28:55 -0500304 if (ssocket == NULL)
305 return -ENOTSOCK;
306
Jeff Laytonb8eed282012-09-18 16:20:35 -0700307 /* cork the socket */
308 kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
309 (char *)&val, sizeof(val));
310
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000311 for (j = 0; j < num_rqst; j++)
Ronnie Sahlberg81f39f92018-06-28 10:47:14 +1000312 send_length += smb_rqst_len(server, &rqst[j]);
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000313 rfc1002_marker = cpu_to_be32(send_length);
314
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000315 /* Generate a rfc1002 marker for SMB2+ */
316 if (server->vals->header_preamble_size == 0) {
317 struct kvec hiov = {
318 .iov_base = &rfc1002_marker,
319 .iov_len = 4
320 };
David Howellsaa563d72018-10-20 00:57:56 +0100321 iov_iter_kvec(&smb_msg.msg_iter, WRITE, &hiov, 1, 4);
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000322 rc = smb_send_kvec(server, &smb_msg, &sent);
323 if (rc < 0)
324 goto uncork;
325
326 total_len += sent;
327 send_length += 4;
328 }
329
Paulo Alcantara662bf5b2018-06-14 17:34:08 -0300330 cifs_dbg(FYI, "Sending smb: smb_len=%u\n", send_length);
331
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000332 for (j = 0; j < num_rqst; j++) {
333 iov = rqst[j].rq_iov;
334 n_vec = rqst[j].rq_nvec;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000335
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000336 size = 0;
Paulo Alcantara662bf5b2018-06-14 17:34:08 -0300337 for (i = 0; i < n_vec; i++) {
338 dump_smb(iov[i].iov_base, iov[i].iov_len);
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000339 size += iov[i].iov_len;
Paulo Alcantara662bf5b2018-06-14 17:34:08 -0300340 }
Al Viro3ab3f2a2015-11-13 02:36:04 -0500341
David Howellsaa563d72018-10-20 00:57:56 +0100342 iov_iter_kvec(&smb_msg.msg_iter, WRITE, iov, n_vec, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Al Viro3ab3f2a2015-11-13 02:36:04 -0500344 rc = smb_send_kvec(server, &smb_msg, &sent);
Jeff Layton97bc00b2012-09-18 16:20:35 -0700345 if (rc < 0)
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000346 goto uncork;
Jeff Layton97bc00b2012-09-18 16:20:35 -0700347
348 total_len += sent;
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000349
350 /* now walk the page array and send each page in it */
351 for (i = 0; i < rqst[j].rq_npages; i++) {
352 struct bio_vec bvec;
353
354 bvec.bv_page = rqst[j].rq_pages[i];
355 rqst_page_get_length(&rqst[j], i, &bvec.bv_len,
356 &bvec.bv_offset);
357
David Howellsaa563d72018-10-20 00:57:56 +0100358 iov_iter_bvec(&smb_msg.msg_iter, WRITE,
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000359 &bvec, 1, bvec.bv_len);
360 rc = smb_send_kvec(server, &smb_msg, &sent);
361 if (rc < 0)
362 break;
363
364 total_len += sent;
365 }
Jeff Layton97bc00b2012-09-18 16:20:35 -0700366 }
367
368uncork:
Jeff Laytonb8eed282012-09-18 16:20:35 -0700369 /* uncork it */
370 val = 0;
371 kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
372 (char *)&val, sizeof(val));
373
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000374 if ((total_len > 0) && (total_len != send_length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500375 cifs_dbg(FYI, "partial send (wanted=%u sent=%zu): terminating session\n",
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000376 send_length, total_len);
Jeff Layton6f49f462012-09-18 16:20:34 -0700377 /*
378 * If we have only sent part of an SMB then the next SMB could
379 * be taken as the remainder of this one. We need to kill the
380 * socket so the server throws away the partial SMB
381 */
Steve Frenchedf1ae42008-10-29 00:47:57 +0000382 server->tcpStatus = CifsNeedReconnect;
Steve Frenchbf1fdeb2018-07-30 19:23:09 -0500383 trace_smb3_partial_send_reconnect(server->CurrentMid,
384 server->hostname);
Steve Frenchedf1ae42008-10-29 00:47:57 +0000385 }
Long Li9762c2d2017-11-22 17:38:43 -0700386smbd_done:
Jeff Laytond804d412011-01-28 15:05:43 -0500387 if (rc < 0 && rc != -EINTR)
Joe Perchesf96637b2013-05-04 22:12:25 -0500388 cifs_dbg(VFS, "Error %d sending data on socket to server\n",
389 rc);
Jeff Laytond804d412011-01-28 15:05:43 -0500390 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 return rc;
394}
395
Jeff Layton6f49f462012-09-18 16:20:34 -0700396static int
Ronnie Sahlberg1f3a8f52018-08-01 09:26:12 +1000397smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
398 struct smb_rqst *rqst, int flags)
Jeff Layton6f49f462012-09-18 16:20:34 -0700399{
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +1000400 struct kvec iov;
401 struct smb2_transform_hdr tr_hdr;
402 struct smb_rqst cur_rqst[MAX_COMPOUND];
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700403 int rc;
Jeff Layton6f49f462012-09-18 16:20:34 -0700404
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700405 if (!(flags & CIFS_TRANSFORM_REQ))
Ronnie Sahlberg1f3a8f52018-08-01 09:26:12 +1000406 return __smb_send_rqst(server, num_rqst, rqst);
407
408 if (num_rqst > MAX_COMPOUND - 1)
409 return -ENOMEM;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700410
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +1000411 memset(&cur_rqst[0], 0, sizeof(cur_rqst));
412 memset(&iov, 0, sizeof(iov));
413 memset(&tr_hdr, 0, sizeof(tr_hdr));
414
415 iov.iov_base = &tr_hdr;
416 iov.iov_len = sizeof(tr_hdr);
417 cur_rqst[0].rq_iov = &iov;
418 cur_rqst[0].rq_nvec = 1;
419
420 if (!server->ops->init_transform_rq) {
421 cifs_dbg(VFS, "Encryption requested but transform callback "
422 "is missing\n");
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700423 return -EIO;
424 }
425
Ronnie Sahlberg1f3a8f52018-08-01 09:26:12 +1000426 rc = server->ops->init_transform_rq(server, num_rqst + 1,
427 &cur_rqst[0], rqst);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700428 if (rc)
429 return rc;
430
Ronnie Sahlberg1f3a8f52018-08-01 09:26:12 +1000431 rc = __smb_send_rqst(server, num_rqst + 1, &cur_rqst[0]);
432 smb3_free_compound_rqst(num_rqst, &cur_rqst[1]);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700433 return rc;
Jeff Layton6f49f462012-09-18 16:20:34 -0700434}
435
Jeff Layton0496e022008-12-30 12:39:16 -0500436int
437smb_send(struct TCP_Server_Info *server, struct smb_hdr *smb_buffer,
438 unsigned int smb_buf_length)
439{
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800440 struct kvec iov[2];
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700441 struct smb_rqst rqst = { .rq_iov = iov,
442 .rq_nvec = 2 };
Jeff Layton0496e022008-12-30 12:39:16 -0500443
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800444 iov[0].iov_base = smb_buffer;
445 iov[0].iov_len = 4;
446 iov[1].iov_base = (char *)smb_buffer + 4;
447 iov[1].iov_len = smb_buf_length;
Jeff Layton0496e022008-12-30 12:39:16 -0500448
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000449 return __smb_send_rqst(server, 1, &rqst);
Jeff Layton0496e022008-12-30 12:39:16 -0500450}
451
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300452static int
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400453wait_for_free_credits(struct TCP_Server_Info *server, const int timeout,
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300454 int *credits)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000455{
Pavel Shilovsky5bc59492012-02-21 19:56:08 +0300456 int rc;
457
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300458 spin_lock(&server->req_lock);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400459 if (timeout == CIFS_ASYNC_OP) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000460 /* oplock breaks must not be held up */
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300461 server->in_flight++;
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300462 *credits -= 1;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300463 spin_unlock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000464 return 0;
465 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000466
Volker Lendecke27a97a62008-12-08 20:59:39 +0000467 while (1) {
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300468 if (*credits <= 0) {
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300469 spin_unlock(&server->req_lock);
Steve French789e6662011-08-09 18:44:44 +0000470 cifs_num_waiters_inc(server);
Pavel Shilovsky5bc59492012-02-21 19:56:08 +0300471 rc = wait_event_killable(server->request_q,
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300472 has_credits(server, credits));
Steve French789e6662011-08-09 18:44:44 +0000473 cifs_num_waiters_dec(server);
Pavel Shilovsky5bc59492012-02-21 19:56:08 +0300474 if (rc)
475 return rc;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300476 spin_lock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000477 } else {
Jeff Laytonc5797a92011-01-11 07:24:01 -0500478 if (server->tcpStatus == CifsExiting) {
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300479 spin_unlock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000480 return -ENOENT;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000481 }
Volker Lendecke27a97a62008-12-08 20:59:39 +0000482
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400483 /*
484 * Can not count locking commands against total
485 * as they are allowed to block on server.
486 */
Volker Lendecke27a97a62008-12-08 20:59:39 +0000487
488 /* update # of requests on the wire to server */
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400489 if (timeout != CIFS_BLOCKING_OP) {
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300490 *credits -= 1;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300491 server->in_flight++;
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400492 }
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300493 spin_unlock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000494 break;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000495 }
496 }
497 return 0;
498}
499
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300500static int
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400501wait_for_free_request(struct TCP_Server_Info *server, const int timeout,
502 const int optype)
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300503{
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -0500504 int *val;
505
506 val = server->ops->get_credits_field(server, optype);
507 /* Since an echo is already inflight, no need to wait to send another */
508 if (*val <= 0 && optype == CIFS_ECHO_OP)
509 return -EAGAIN;
510 return wait_for_free_credits(server, timeout, val);
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300511}
512
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400513int
514cifs_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
515 unsigned int *num, unsigned int *credits)
516{
517 *num = size;
518 *credits = 0;
519 return 0;
520}
521
Steve French96daf2b2011-05-27 04:34:02 +0000522static int allocate_mid(struct cifs_ses *ses, struct smb_hdr *in_buf,
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000523 struct mid_q_entry **ppmidQ)
524{
525 if (ses->server->tcpStatus == CifsExiting) {
526 return -ENOENT;
Volker Lendecke8fbbd362008-12-06 13:12:34 +0100527 }
528
529 if (ses->server->tcpStatus == CifsNeedReconnect) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500530 cifs_dbg(FYI, "tcp session dead - return to caller to retry\n");
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000531 return -EAGAIN;
Volker Lendecke8fbbd362008-12-06 13:12:34 +0100532 }
533
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500534 if (ses->status == CifsNew) {
Steve French79a58d12007-07-06 22:44:50 +0000535 if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
Steve Frenchad7a2922008-02-07 23:25:02 +0000536 (in_buf->Command != SMB_COM_NEGOTIATE))
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000537 return -EAGAIN;
Steve Frenchad7a2922008-02-07 23:25:02 +0000538 /* else ok - we are setting up session */
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000539 }
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500540
541 if (ses->status == CifsExiting) {
542 /* check if SMB session is bad because we are setting it up */
543 if (in_buf->Command != SMB_COM_LOGOFF_ANDX)
544 return -EAGAIN;
545 /* else ok - we are shutting down session */
546 }
547
Jeff Layton24b9b062008-12-01 07:09:34 -0500548 *ppmidQ = AllocMidQEntry(in_buf, ses->server);
Steve French26f57362007-08-30 22:09:15 +0000549 if (*ppmidQ == NULL)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000550 return -ENOMEM;
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500551 spin_lock(&GlobalMid_Lock);
552 list_add_tail(&(*ppmidQ)->qhead, &ses->server->pending_mid_q);
553 spin_unlock(&GlobalMid_Lock);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000554 return 0;
555}
556
Jeff Layton0ade6402011-01-11 07:24:02 -0500557static int
558wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *midQ)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000559{
Jeff Layton0ade6402011-01-11 07:24:02 -0500560 int error;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000561
Colin Cross5853cc22013-05-07 17:52:05 +0000562 error = wait_event_freezekillable_unsafe(server->response_q,
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400563 midQ->mid_state != MID_REQUEST_SUBMITTED);
Jeff Layton0ade6402011-01-11 07:24:02 -0500564 if (error < 0)
565 return -ERESTARTSYS;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000566
Jeff Layton0ade6402011-01-11 07:24:02 -0500567 return 0;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000568}
569
Jeff Laytonfec344e2012-09-18 16:20:35 -0700570struct mid_q_entry *
571cifs_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400572{
573 int rc;
Jeff Laytonfec344e2012-09-18 16:20:35 -0700574 struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400575 struct mid_q_entry *mid;
576
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800577 if (rqst->rq_iov[0].iov_len != 4 ||
578 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
579 return ERR_PTR(-EIO);
580
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400581 /* enable signing if server requires it */
Jeff Layton38d77c52013-05-26 07:01:00 -0400582 if (server->sign)
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400583 hdr->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
584
585 mid = AllocMidQEntry(hdr, server);
586 if (mid == NULL)
Jeff Laytonfec344e2012-09-18 16:20:35 -0700587 return ERR_PTR(-ENOMEM);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400588
Jeff Laytonfec344e2012-09-18 16:20:35 -0700589 rc = cifs_sign_rqst(rqst, server, &mid->sequence_number);
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100590 if (rc) {
591 DeleteMidQEntry(mid);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700592 return ERR_PTR(rc);
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100593 }
594
Jeff Laytonfec344e2012-09-18 16:20:35 -0700595 return mid;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400596}
Steve French133672e2007-11-13 22:41:37 +0000597
598/*
Jeff Laytona6827c12011-01-11 07:24:21 -0500599 * Send a SMB request and set the callback function in the mid to handle
600 * the result. Caller is responsible for dealing with timeouts.
601 */
602int
Jeff Laytonfec344e2012-09-18 16:20:35 -0700603cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst,
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -0800604 mid_receive_t *receive, mid_callback_t *callback,
605 mid_handle_t *handle, void *cbdata, const int flags)
Jeff Laytona6827c12011-01-11 07:24:21 -0500606{
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400607 int rc, timeout, optype;
Jeff Laytona6827c12011-01-11 07:24:21 -0500608 struct mid_q_entry *mid;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400609 unsigned int credits = 0;
Jeff Laytona6827c12011-01-11 07:24:21 -0500610
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400611 timeout = flags & CIFS_TIMEOUT_MASK;
612 optype = flags & CIFS_OP_MASK;
613
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400614 if ((flags & CIFS_HAS_CREDITS) == 0) {
615 rc = wait_for_free_request(server, timeout, optype);
616 if (rc)
617 return rc;
618 credits = 1;
619 }
Jeff Laytona6827c12011-01-11 07:24:21 -0500620
621 mutex_lock(&server->srv_mutex);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700622 mid = server->ops->setup_async_request(server, rqst);
623 if (IS_ERR(mid)) {
Jeff Laytona6827c12011-01-11 07:24:21 -0500624 mutex_unlock(&server->srv_mutex);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400625 add_credits_and_wake_if(server, credits, optype);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700626 return PTR_ERR(mid);
Jeff Laytona6827c12011-01-11 07:24:21 -0500627 }
628
Jeff Layton44d22d82011-10-19 15:29:49 -0400629 mid->receive = receive;
Jeff Laytona6827c12011-01-11 07:24:21 -0500630 mid->callback = callback;
631 mid->callback_data = cbdata;
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -0800632 mid->handle = handle;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400633 mid->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +0000634
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100635 /* put it on the pending_mid_q */
636 spin_lock(&GlobalMid_Lock);
637 list_add_tail(&mid->qhead, &server->pending_mid_q);
638 spin_unlock(&GlobalMid_Lock);
639
Long Li93d2cb62017-06-28 15:55:55 -0700640 /*
641 * Need to store the time in mid before calling I/O. For call_async,
642 * I/O response may come back and free the mid entry on another thread.
643 */
644 cifs_save_when_sent(mid);
Steve French789e6662011-08-09 18:44:44 +0000645 cifs_in_send_inc(server);
Ronnie Sahlberg1f3a8f52018-08-01 09:26:12 +1000646 rc = smb_send_rqst(server, 1, rqst, flags);
Steve French789e6662011-08-09 18:44:44 +0000647 cifs_in_send_dec(server);
Jeff Laytonad313cb2013-04-03 10:27:36 -0400648
Rabin Vincent820962d2015-12-23 07:32:41 +0100649 if (rc < 0) {
Jeff Laytonad313cb2013-04-03 10:27:36 -0400650 server->sequence_number -= 2;
Rabin Vincent820962d2015-12-23 07:32:41 +0100651 cifs_delete_mid(mid);
652 }
653
Jeff Laytona6827c12011-01-11 07:24:21 -0500654 mutex_unlock(&server->srv_mutex);
Steve French789e6662011-08-09 18:44:44 +0000655
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100656 if (rc == 0)
657 return 0;
Jeff Laytona6827c12011-01-11 07:24:21 -0500658
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400659 add_credits_and_wake_if(server, credits, optype);
Jeff Laytona6827c12011-01-11 07:24:21 -0500660 return rc;
661}
662
663/*
Steve French133672e2007-11-13 22:41:37 +0000664 *
665 * Send an SMB Request. No response info (other than return code)
666 * needs to be parsed.
667 *
668 * flags indicate the type of request buffer and how long to wait
669 * and whether to log NT STATUS code (error) before mapping it to POSIX error
670 *
671 */
672int
Steve French96daf2b2011-05-27 04:34:02 +0000673SendReceiveNoRsp(const unsigned int xid, struct cifs_ses *ses,
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400674 char *in_buf, int flags)
Steve French133672e2007-11-13 22:41:37 +0000675{
676 int rc;
677 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700678 struct kvec rsp_iov;
Steve French133672e2007-11-13 22:41:37 +0000679 int resp_buf_type;
680
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400681 iov[0].iov_base = in_buf;
682 iov[0].iov_len = get_rfc1002_length(in_buf) + 4;
Steve French133672e2007-11-13 22:41:37 +0000683 flags |= CIFS_NO_RESP;
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700684 rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Joe Perchesf96637b2013-05-04 22:12:25 -0500685 cifs_dbg(NOISY, "SendRcvNoRsp flags %d rc %d\n", flags, rc);
Steve French90c81e02008-02-12 20:32:36 +0000686
Steve French133672e2007-11-13 22:41:37 +0000687 return rc;
688}
689
Jeff Layton053d5032011-01-11 07:24:02 -0500690static int
Jeff Layton3c1105d2011-05-22 07:09:13 -0400691cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
Jeff Layton053d5032011-01-11 07:24:02 -0500692{
693 int rc = 0;
694
Joe Perchesf96637b2013-05-04 22:12:25 -0500695 cifs_dbg(FYI, "%s: cmd=%d mid=%llu state=%d\n",
696 __func__, le16_to_cpu(mid->command), mid->mid, mid->mid_state);
Jeff Layton053d5032011-01-11 07:24:02 -0500697
Jeff Layton74dd92a2011-01-11 07:24:02 -0500698 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400699 switch (mid->mid_state) {
Jeff Layton74dd92a2011-01-11 07:24:02 -0500700 case MID_RESPONSE_RECEIVED:
Jeff Layton053d5032011-01-11 07:24:02 -0500701 spin_unlock(&GlobalMid_Lock);
702 return rc;
Jeff Layton74dd92a2011-01-11 07:24:02 -0500703 case MID_RETRY_NEEDED:
704 rc = -EAGAIN;
705 break;
Jeff Layton71823ba2011-02-10 08:03:50 -0500706 case MID_RESPONSE_MALFORMED:
707 rc = -EIO;
708 break;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400709 case MID_SHUTDOWN:
710 rc = -EHOSTDOWN;
711 break;
Jeff Layton74dd92a2011-01-11 07:24:02 -0500712 default:
Jeff Layton3c1105d2011-05-22 07:09:13 -0400713 list_del_init(&mid->qhead);
Joe Perchesf96637b2013-05-04 22:12:25 -0500714 cifs_dbg(VFS, "%s: invalid mid state mid=%llu state=%d\n",
715 __func__, mid->mid, mid->mid_state);
Jeff Layton74dd92a2011-01-11 07:24:02 -0500716 rc = -EIO;
Jeff Layton053d5032011-01-11 07:24:02 -0500717 }
718 spin_unlock(&GlobalMid_Lock);
719
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500720 DeleteMidQEntry(mid);
Jeff Layton053d5032011-01-11 07:24:02 -0500721 return rc;
722}
723
Jeff Layton121b0462012-05-15 12:21:10 -0400724static inline int
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800725send_cancel(struct TCP_Server_Info *server, struct smb_rqst *rqst,
726 struct mid_q_entry *mid)
Jeff Layton76dcc262011-01-11 07:24:24 -0500727{
Jeff Layton121b0462012-05-15 12:21:10 -0400728 return server->ops->send_cancel ?
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800729 server->ops->send_cancel(server, rqst, mid) : 0;
Jeff Layton76dcc262011-01-11 07:24:24 -0500730}
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732int
Jeff Layton2c8f9812011-05-19 16:22:52 -0400733cifs_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
734 bool log_error)
735{
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400736 unsigned int len = get_rfc1002_length(mid->resp_buf) + 4;
Jeff Layton826a95e2011-10-11 06:41:32 -0400737
738 dump_smb(mid->resp_buf, min_t(u32, 92, len));
Jeff Layton2c8f9812011-05-19 16:22:52 -0400739
740 /* convert the length into a more usable form */
Jeff Layton38d77c52013-05-26 07:01:00 -0400741 if (server->sign) {
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800742 struct kvec iov[2];
Steve French985e4ff02012-08-03 09:42:45 -0500743 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800744 struct smb_rqst rqst = { .rq_iov = iov,
745 .rq_nvec = 2 };
Jeff Layton826a95e2011-10-11 06:41:32 -0400746
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800747 iov[0].iov_base = mid->resp_buf;
748 iov[0].iov_len = 4;
749 iov[1].iov_base = (char *)mid->resp_buf + 4;
750 iov[1].iov_len = len - 4;
Jeff Layton2c8f9812011-05-19 16:22:52 -0400751 /* FIXME: add code to kill session */
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700752 rc = cifs_verify_signature(&rqst, server,
Jeff Layton0124cc42013-04-03 11:55:03 -0400753 mid->sequence_number);
Steve French985e4ff02012-08-03 09:42:45 -0500754 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500755 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
756 rc);
Jeff Layton2c8f9812011-05-19 16:22:52 -0400757 }
758
759 /* BB special case reconnect tid and uid here? */
760 return map_smb_to_linux_error(mid->resp_buf, log_error);
761}
762
Jeff Laytonfec344e2012-09-18 16:20:35 -0700763struct mid_q_entry *
764cifs_setup_request(struct cifs_ses *ses, struct smb_rqst *rqst)
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400765{
766 int rc;
Jeff Laytonfec344e2012-09-18 16:20:35 -0700767 struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400768 struct mid_q_entry *mid;
769
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800770 if (rqst->rq_iov[0].iov_len != 4 ||
771 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
772 return ERR_PTR(-EIO);
773
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400774 rc = allocate_mid(ses, hdr, &mid);
775 if (rc)
Jeff Laytonfec344e2012-09-18 16:20:35 -0700776 return ERR_PTR(rc);
777 rc = cifs_sign_rqst(rqst, ses->server, &mid->sequence_number);
778 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700779 cifs_delete_mid(mid);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700780 return ERR_PTR(rc);
781 }
782 return mid;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400783}
784
Ronnie Sahlberg4e34feb2018-08-30 10:13:00 +1000785static void
786cifs_noop_callback(struct mid_q_entry *mid)
787{
788}
789
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800790int
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000791compound_send_recv(const unsigned int xid, struct cifs_ses *ses,
792 const int flags, const int num_rqst, struct smb_rqst *rqst,
793 int *resp_buf_type, struct kvec *resp_iov)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000795 int i, j, rc = 0;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400796 int timeout, optype;
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000797 struct mid_q_entry *midQ[MAX_COMPOUND];
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -0800798 bool cancelled_mid[MAX_COMPOUND] = {false};
799 unsigned int credits[MAX_COMPOUND] = {0};
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800800 char *buf;
Steve French50c2f752007-07-13 00:33:32 +0000801
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400802 timeout = flags & CIFS_TIMEOUT_MASK;
803 optype = flags & CIFS_OP_MASK;
Steve French133672e2007-11-13 22:41:37 +0000804
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000805 for (i = 0; i < num_rqst; i++)
806 resp_buf_type[i] = CIFS_NO_BUFFER; /* no response buf yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Steve French4b8f9302006-02-26 16:41:18 +0000808 if ((ses == NULL) || (ses->server == NULL)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500809 cifs_dbg(VFS, "Null session\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return -EIO;
811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700813 if (ses->server->tcpStatus == CifsExiting)
Steve French31ca3bc2005-04-28 22:41:11 -0700814 return -ENOENT;
815
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400816 /*
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -0800817 * Ensure we obtain 1 credit per request in the compound chain.
818 * It can be optimized further by waiting for all the credits
819 * at once but this can wait long enough if we don't have enough
820 * credits due to some heavy operations in progress or the server
821 * not granting us much, so a fallback to the current approach is
822 * needed anyway.
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400823 */
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -0800824 for (i = 0; i < num_rqst; i++) {
825 rc = wait_for_free_request(ses->server, timeout, optype);
826 if (rc) {
827 /*
828 * We haven't sent an SMB packet to the server yet but
829 * we already obtained credits for i requests in the
830 * compound chain - need to return those credits back
831 * for future use. Note that we need to call add_credits
832 * multiple times to match the way we obtained credits
833 * in the first place and to account for in flight
834 * requests correctly.
835 */
836 for (j = 0; j < i; j++)
837 add_credits(ses->server, 1, optype);
838 return rc;
839 }
840 credits[i] = 1;
841 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000842
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400843 /*
844 * Make sure that we sign in the same order that we send on this socket
845 * and avoid races inside tcp sendmsg code that could cause corruption
846 * of smb data.
847 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Jeff Layton72ca5452008-12-01 07:09:36 -0500849 mutex_lock(&ses->server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000851 for (i = 0; i < num_rqst; i++) {
852 midQ[i] = ses->server->ops->setup_request(ses, &rqst[i]);
853 if (IS_ERR(midQ[i])) {
854 for (j = 0; j < i; j++)
855 cifs_delete_mid(midQ[j]);
856 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -0800857
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000858 /* Update # of requests on wire to server */
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -0800859 for (j = 0; j < num_rqst; j++)
860 add_credits(ses->server, credits[j], optype);
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000861 return PTR_ERR(midQ[i]);
862 }
863
864 midQ[i]->mid_state = MID_REQUEST_SUBMITTED;
Ronnie Sahlberg4e34feb2018-08-30 10:13:00 +1000865 /*
866 * We don't invoke the callback compounds unless it is the last
867 * request.
868 */
869 if (i < num_rqst - 1)
870 midQ[i]->callback = cifs_noop_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
Steve French789e6662011-08-09 18:44:44 +0000872 cifs_in_send_inc(ses->server);
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000873 rc = smb_send_rqst(ses->server, num_rqst, rqst, flags);
Steve French789e6662011-08-09 18:44:44 +0000874 cifs_in_send_dec(ses->server);
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000875
876 for (i = 0; i < num_rqst; i++)
877 cifs_save_when_sent(midQ[i]);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000878
Jeff Laytonad313cb2013-04-03 10:27:36 -0400879 if (rc < 0)
880 ses->server->sequence_number -= 2;
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000881
Jeff Layton72ca5452008-12-01 07:09:36 -0500882 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000883
Ronnie Sahlbergcb5c2e62018-10-10 15:29:06 +1000884 if (rc < 0)
885 goto out;
886
887 /*
888 * Compounding is never used during session establish.
889 */
890 if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP))
891 smb311_update_preauth_hash(ses, rqst[0].rq_iov,
892 rqst[0].rq_nvec);
893
894 if (timeout == CIFS_ASYNC_OP)
895 goto out;
896
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000897 for (i = 0; i < num_rqst; i++) {
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000898 rc = wait_for_response(ses->server, midQ[i]);
899 if (rc != 0) {
Steve French43de1db2018-10-23 21:04:57 -0500900 cifs_dbg(VFS, "Cancelling wait for mid %llu cmd: %d\n",
901 midQ[i]->mid, le16_to_cpu(midQ[i]->command));
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000902 send_cancel(ses->server, &rqst[i], midQ[i]);
903 spin_lock(&GlobalMid_Lock);
904 if (midQ[i]->mid_state == MID_REQUEST_SUBMITTED) {
905 midQ[i]->mid_flags |= MID_WAIT_CANCELLED;
906 midQ[i]->callback = DeleteMidQEntry;
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -0800907 cancelled_mid[i] = true;
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000908 }
Jeff Layton1be912d2011-01-28 07:08:28 -0500909 spin_unlock(&GlobalMid_Lock);
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000910 }
Ronnie Sahlbergcb5c2e62018-10-10 15:29:06 +1000911 }
912
913 for (i = 0; i < num_rqst; i++)
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -0800914 if (!cancelled_mid[i] && midQ[i]->resp_buf
915 && (midQ[i]->mid_state == MID_RESPONSE_RECEIVED))
916 credits[i] = ses->server->ops->get_credits(midQ[i]);
Ronnie Sahlbergcb5c2e62018-10-10 15:29:06 +1000917
918 for (i = 0; i < num_rqst; i++) {
919 if (rc < 0)
920 goto out;
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000921
922 rc = cifs_sync_mid_result(midQ[i], ses->server);
923 if (rc != 0) {
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -0800924 /* mark this mid as cancelled to not free it below */
925 cancelled_mid[i] = true;
926 goto out;
Jeff Layton1be912d2011-01-28 07:08:28 -0500927 }
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000928
929 if (!midQ[i]->resp_buf ||
930 midQ[i]->mid_state != MID_RESPONSE_RECEIVED) {
931 rc = -EIO;
932 cifs_dbg(FYI, "Bad MID state?\n");
933 goto out;
934 }
935
936 buf = (char *)midQ[i]->resp_buf;
937 resp_iov[i].iov_base = buf;
938 resp_iov[i].iov_len = midQ[i]->resp_buf_size +
939 ses->server->vals->header_preamble_size;
940
941 if (midQ[i]->large_buf)
942 resp_buf_type[i] = CIFS_LARGE_BUFFER;
943 else
944 resp_buf_type[i] = CIFS_SMALL_BUFFER;
945
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000946 rc = ses->server->ops->check_receive(midQ[i], ses->server,
947 flags & CIFS_LOG_ERROR);
948
949 /* mark it so buf will not be freed by cifs_delete_mid */
950 if ((flags & CIFS_NO_RESP) == 0)
951 midQ[i]->resp_buf = NULL;
Ronnie Sahlbergcb5c2e62018-10-10 15:29:06 +1000952
Jeff Layton1be912d2011-01-28 07:08:28 -0500953 }
Ronnie Sahlbergcb5c2e62018-10-10 15:29:06 +1000954
955 /*
956 * Compounding is never used during session establish.
957 */
958 if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP)) {
959 struct kvec iov = {
960 .iov_base = resp_iov[0].iov_base,
961 .iov_len = resp_iov[0].iov_len
962 };
963 smb311_update_preauth_hash(ses, &iov, 1);
964 }
965
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000966out:
Ronnie Sahlberg4e34feb2018-08-30 10:13:00 +1000967 /*
968 * This will dequeue all mids. After this it is important that the
969 * demultiplex_thread will not process any of these mids any futher.
970 * This is prevented above by using a noop callback that will not
971 * wake this thread except for the very last PDU.
972 */
Pavel Shilovsky8544f4a2018-12-22 12:40:05 -0800973 for (i = 0; i < num_rqst; i++) {
974 if (!cancelled_mid[i])
975 cifs_delete_mid(midQ[i]);
976 add_credits(ses->server, credits[i], optype);
977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
979 return rc;
980}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982int
Ronnie Sahlberge0bba0b82018-08-01 09:26:13 +1000983cifs_send_recv(const unsigned int xid, struct cifs_ses *ses,
984 struct smb_rqst *rqst, int *resp_buf_type, const int flags,
985 struct kvec *resp_iov)
986{
987 return compound_send_recv(xid, ses, flags, 1, rqst, resp_buf_type,
988 resp_iov);
989}
990
991int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800992SendReceive2(const unsigned int xid, struct cifs_ses *ses,
993 struct kvec *iov, int n_vec, int *resp_buf_type /* ret */,
994 const int flags, struct kvec *resp_iov)
995{
996 struct smb_rqst rqst;
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +1100997 struct kvec s_iov[CIFS_MAX_IOV_SIZE], *new_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800998 int rc;
999
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +11001000 if (n_vec + 1 > CIFS_MAX_IOV_SIZE) {
Kees Cook6da2ec52018-06-12 13:55:00 -07001001 new_iov = kmalloc_array(n_vec + 1, sizeof(struct kvec),
1002 GFP_KERNEL);
Steve French117e3b72018-04-22 10:24:19 -05001003 if (!new_iov) {
1004 /* otherwise cifs_send_recv below sets resp_buf_type */
1005 *resp_buf_type = CIFS_NO_BUFFER;
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +11001006 return -ENOMEM;
Steve French117e3b72018-04-22 10:24:19 -05001007 }
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +11001008 } else
1009 new_iov = s_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001010
1011 /* 1st iov is a RFC1001 length followed by the rest of the packet */
1012 memcpy(new_iov + 1, iov, (sizeof(struct kvec) * n_vec));
1013
1014 new_iov[0].iov_base = new_iov[1].iov_base;
1015 new_iov[0].iov_len = 4;
1016 new_iov[1].iov_base += 4;
1017 new_iov[1].iov_len -= 4;
1018
1019 memset(&rqst, 0, sizeof(struct smb_rqst));
1020 rqst.rq_iov = new_iov;
1021 rqst.rq_nvec = n_vec + 1;
1022
1023 rc = cifs_send_recv(xid, ses, &rqst, resp_buf_type, flags, resp_iov);
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +11001024 if (n_vec + 1 > CIFS_MAX_IOV_SIZE)
1025 kfree(new_iov);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001026 return rc;
1027}
1028
1029int
Steve French96daf2b2011-05-27 04:34:02 +00001030SendReceive(const unsigned int xid, struct cifs_ses *ses,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001032 int *pbytes_returned, const int timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
1034 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 struct mid_q_entry *midQ;
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001036 unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
1037 struct kvec iov = { .iov_base = in_buf, .iov_len = len };
1038 struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 if (ses == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001041 cifs_dbg(VFS, "Null smb session\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 return -EIO;
1043 }
Steve French79a58d12007-07-06 22:44:50 +00001044 if (ses->server == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001045 cifs_dbg(VFS, "Null tcp session\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 return -EIO;
1047 }
1048
Steve French79a58d12007-07-06 22:44:50 +00001049 if (ses->server->tcpStatus == CifsExiting)
Steve French31ca3bc2005-04-28 22:41:11 -07001050 return -ENOENT;
1051
Steve French79a58d12007-07-06 22:44:50 +00001052 /* Ensure that we do not send more than 50 overlapping requests
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 to the same server. We may make this configurable later or
1054 use ses->maxReq */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001056 if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001057 cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001058 len);
Volker Lendecke6d9c6d52008-12-08 20:50:24 +00001059 return -EIO;
1060 }
1061
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001062 rc = wait_for_free_request(ses->server, timeout, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001063 if (rc)
1064 return rc;
1065
Steve French79a58d12007-07-06 22:44:50 +00001066 /* make sure that we sign in the same order that we send on this socket
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 and avoid races inside tcp sendmsg code that could cause corruption
1068 of smb data */
1069
Jeff Layton72ca5452008-12-01 07:09:36 -05001070 mutex_lock(&ses->server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001072 rc = allocate_mid(ses, in_buf, &midQ);
1073 if (rc) {
Jeff Layton72ca5452008-12-01 07:09:36 -05001074 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001075 /* Update # of requests on wire to server */
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001076 add_credits(ses->server, 1, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001077 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 }
1079
Steve Frenchad009ac2005-04-28 22:41:05 -07001080 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
Volker Lendecke829049c2008-12-06 16:00:53 +01001081 if (rc) {
1082 mutex_unlock(&ses->server->srv_mutex);
1083 goto out;
1084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001086 midQ->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +00001087
1088 cifs_in_send_inc(ses->server);
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001089 rc = smb_send(ses->server, in_buf, len);
Steve French789e6662011-08-09 18:44:44 +00001090 cifs_in_send_dec(ses->server);
1091 cifs_save_when_sent(midQ);
Jeff Laytonad313cb2013-04-03 10:27:36 -04001092
1093 if (rc < 0)
1094 ses->server->sequence_number -= 2;
1095
Jeff Layton72ca5452008-12-01 07:09:36 -05001096 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001097
Steve French79a58d12007-07-06 22:44:50 +00001098 if (rc < 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001099 goto out;
1100
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001101 if (timeout == CIFS_ASYNC_OP)
Steve French133672e2007-11-13 22:41:37 +00001102 goto out;
Jeff Layton0ade6402011-01-11 07:24:02 -05001103
1104 rc = wait_for_response(ses->server, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -05001105 if (rc != 0) {
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001106 send_cancel(ses->server, &rqst, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -05001107 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001108 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
Jeff Layton1be912d2011-01-28 07:08:28 -05001109 /* no longer considered to be "in-flight" */
1110 midQ->callback = DeleteMidQEntry;
1111 spin_unlock(&GlobalMid_Lock);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001112 add_credits(ses->server, 1, 0);
Jeff Layton1be912d2011-01-28 07:08:28 -05001113 return rc;
1114 }
1115 spin_unlock(&GlobalMid_Lock);
1116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Jeff Layton3c1105d2011-05-22 07:09:13 -04001118 rc = cifs_sync_mid_result(midQ, ses->server);
Jeff Layton053d5032011-01-11 07:24:02 -05001119 if (rc != 0) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001120 add_credits(ses->server, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 return rc;
1122 }
Steve French50c2f752007-07-13 00:33:32 +00001123
Jeff Layton2c8f9812011-05-19 16:22:52 -04001124 if (!midQ->resp_buf || !out_buf ||
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001125 midQ->mid_state != MID_RESPONSE_RECEIVED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 rc = -EIO;
Joe Perchesf96637b2013-05-04 22:12:25 -05001127 cifs_dbg(VFS, "Bad MID state?\n");
Steve French2b2bdfb2008-12-11 17:26:54 +00001128 goto out;
1129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001131 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
Jeff Layton2c8f9812011-05-19 16:22:52 -04001132 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
1133 rc = cifs_check_receive(midQ, ses->server, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001134out:
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001135 cifs_delete_mid(midQ);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001136 add_credits(ses->server, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 return rc;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001139}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001141/* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
1142 blocking lock to return. */
1143
1144static int
Steve French96daf2b2011-05-27 04:34:02 +00001145send_lock_cancel(const unsigned int xid, struct cifs_tcon *tcon,
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001146 struct smb_hdr *in_buf,
1147 struct smb_hdr *out_buf)
1148{
1149 int bytes_returned;
Steve French96daf2b2011-05-27 04:34:02 +00001150 struct cifs_ses *ses = tcon->ses;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001151 LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
1152
1153 /* We just modify the current in_buf to change
1154 the type of lock from LOCKING_ANDX_SHARED_LOCK
1155 or LOCKING_ANDX_EXCLUSIVE_LOCK to
1156 LOCKING_ANDX_CANCEL_LOCK. */
1157
1158 pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
1159 pSMB->Timeout = 0;
Pavel Shilovsky88257362012-05-23 14:01:59 +04001160 pSMB->hdr.Mid = get_next_mid(ses->server);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001161
1162 return SendReceive(xid, ses, in_buf, out_buf,
Jeff Layton77499812011-01-11 07:24:23 -05001163 &bytes_returned, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001164}
1165
1166int
Steve French96daf2b2011-05-27 04:34:02 +00001167SendReceiveBlockingLock(const unsigned int xid, struct cifs_tcon *tcon,
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001168 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
1169 int *pbytes_returned)
1170{
1171 int rc = 0;
1172 int rstart = 0;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001173 struct mid_q_entry *midQ;
Steve French96daf2b2011-05-27 04:34:02 +00001174 struct cifs_ses *ses;
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001175 unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
1176 struct kvec iov = { .iov_base = in_buf, .iov_len = len };
1177 struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001178
1179 if (tcon == NULL || tcon->ses == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001180 cifs_dbg(VFS, "Null smb session\n");
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001181 return -EIO;
1182 }
1183 ses = tcon->ses;
1184
Steve French79a58d12007-07-06 22:44:50 +00001185 if (ses->server == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001186 cifs_dbg(VFS, "Null tcp session\n");
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001187 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
1189
Steve French79a58d12007-07-06 22:44:50 +00001190 if (ses->server->tcpStatus == CifsExiting)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001191 return -ENOENT;
1192
Steve French79a58d12007-07-06 22:44:50 +00001193 /* Ensure that we do not send more than 50 overlapping requests
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001194 to the same server. We may make this configurable later or
1195 use ses->maxReq */
1196
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001197 if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001198 cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001199 len);
Volker Lendecke6d9c6d52008-12-08 20:50:24 +00001200 return -EIO;
1201 }
1202
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001203 rc = wait_for_free_request(ses->server, CIFS_BLOCKING_OP, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001204 if (rc)
1205 return rc;
1206
Steve French79a58d12007-07-06 22:44:50 +00001207 /* make sure that we sign in the same order that we send on this socket
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001208 and avoid races inside tcp sendmsg code that could cause corruption
1209 of smb data */
1210
Jeff Layton72ca5452008-12-01 07:09:36 -05001211 mutex_lock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001212
1213 rc = allocate_mid(ses, in_buf, &midQ);
1214 if (rc) {
Jeff Layton72ca5452008-12-01 07:09:36 -05001215 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001216 return rc;
1217 }
1218
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001219 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
Volker Lendecke829049c2008-12-06 16:00:53 +01001220 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001221 cifs_delete_mid(midQ);
Volker Lendecke829049c2008-12-06 16:00:53 +01001222 mutex_unlock(&ses->server->srv_mutex);
1223 return rc;
1224 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001225
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001226 midQ->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +00001227 cifs_in_send_inc(ses->server);
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001228 rc = smb_send(ses->server, in_buf, len);
Steve French789e6662011-08-09 18:44:44 +00001229 cifs_in_send_dec(ses->server);
1230 cifs_save_when_sent(midQ);
Jeff Laytonad313cb2013-04-03 10:27:36 -04001231
1232 if (rc < 0)
1233 ses->server->sequence_number -= 2;
1234
Jeff Layton72ca5452008-12-01 07:09:36 -05001235 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001236
Steve French79a58d12007-07-06 22:44:50 +00001237 if (rc < 0) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001238 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001239 return rc;
1240 }
1241
1242 /* Wait for a reply - allow signals to interrupt. */
1243 rc = wait_event_interruptible(ses->server->response_q,
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001244 (!(midQ->mid_state == MID_REQUEST_SUBMITTED)) ||
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001245 ((ses->server->tcpStatus != CifsGood) &&
1246 (ses->server->tcpStatus != CifsNew)));
1247
1248 /* Were we interrupted by a signal ? */
1249 if ((rc == -ERESTARTSYS) &&
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001250 (midQ->mid_state == MID_REQUEST_SUBMITTED) &&
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001251 ((ses->server->tcpStatus == CifsGood) ||
1252 (ses->server->tcpStatus == CifsNew))) {
1253
1254 if (in_buf->Command == SMB_COM_TRANSACTION2) {
1255 /* POSIX lock. We send a NT_CANCEL SMB to cause the
1256 blocking lock to return. */
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001257 rc = send_cancel(ses->server, &rqst, midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001258 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001259 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001260 return rc;
1261 }
1262 } else {
1263 /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
1264 to cause the blocking lock to return. */
1265
1266 rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
1267
1268 /* If we get -ENOLCK back the lock may have
1269 already been removed. Don't exit in this case. */
1270 if (rc && rc != -ENOLCK) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001271 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001272 return rc;
1273 }
1274 }
1275
Jeff Layton1be912d2011-01-28 07:08:28 -05001276 rc = wait_for_response(ses->server, midQ);
1277 if (rc) {
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001278 send_cancel(ses->server, &rqst, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -05001279 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001280 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
Jeff Layton1be912d2011-01-28 07:08:28 -05001281 /* no longer considered to be "in-flight" */
1282 midQ->callback = DeleteMidQEntry;
1283 spin_unlock(&GlobalMid_Lock);
1284 return rc;
1285 }
1286 spin_unlock(&GlobalMid_Lock);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001287 }
Jeff Layton1be912d2011-01-28 07:08:28 -05001288
1289 /* We got the response - restart system call. */
1290 rstart = 1;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001291 }
1292
Jeff Layton3c1105d2011-05-22 07:09:13 -04001293 rc = cifs_sync_mid_result(midQ, ses->server);
Jeff Layton053d5032011-01-11 07:24:02 -05001294 if (rc != 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001295 return rc;
Steve French50c2f752007-07-13 00:33:32 +00001296
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001297 /* rcvd frame is ok */
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001298 if (out_buf == NULL || midQ->mid_state != MID_RESPONSE_RECEIVED) {
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001299 rc = -EIO;
Joe Perchesf96637b2013-05-04 22:12:25 -05001300 cifs_dbg(VFS, "Bad MID state?\n");
Volker Lendecke698e96a2008-12-06 16:39:31 +01001301 goto out;
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001302 }
1303
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001304 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
Jeff Layton2c8f9812011-05-19 16:22:52 -04001305 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
1306 rc = cifs_check_receive(midQ, ses->server, 0);
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001307out:
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001308 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001309 if (rstart && rc == -EACCES)
1310 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 return rc;
1312}