blob: 78f96fa3d7d990217014395edc043cf422f6252d [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);
145 list_del(&mid->qhead);
146 spin_unlock(&GlobalMid_Lock);
147
148 DeleteMidQEntry(mid);
149}
150
Jeff Layton6f49f462012-09-18 16:20:34 -0700151/*
152 * smb_send_kvec - send an array of kvecs to the server
153 * @server: Server to send the data to
Al Viro3ab3f2a2015-11-13 02:36:04 -0500154 * @smb_msg: Message to send
Jeff Layton6f49f462012-09-18 16:20:34 -0700155 * @sent: amount of data sent on socket is stored here
156 *
157 * Our basic "send data to server" function. Should be called with srv_mutex
158 * held. The caller is responsible for handling the results.
159 */
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500160static int
Al Viro3ab3f2a2015-11-13 02:36:04 -0500161smb_send_kvec(struct TCP_Server_Info *server, struct msghdr *smb_msg,
162 size_t *sent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 int rc = 0;
Al Viro3ab3f2a2015-11-13 02:36:04 -0500165 int retries = 0;
Steve Frenchedf1ae42008-10-29 00:47:57 +0000166 struct socket *ssocket = server->ssocket;
Steve French50c2f752007-07-13 00:33:32 +0000167
Jeff Layton6f49f462012-09-18 16:20:34 -0700168 *sent = 0;
169
Al Viro3ab3f2a2015-11-13 02:36:04 -0500170 smb_msg->msg_name = (struct sockaddr *) &server->dstaddr;
171 smb_msg->msg_namelen = sizeof(struct sockaddr);
172 smb_msg->msg_control = NULL;
173 smb_msg->msg_controllen = 0;
Jeff Layton0496e022008-12-30 12:39:16 -0500174 if (server->noblocksnd)
Al Viro3ab3f2a2015-11-13 02:36:04 -0500175 smb_msg->msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL;
Steve Frenchedf1ae42008-10-29 00:47:57 +0000176 else
Al Viro3ab3f2a2015-11-13 02:36:04 -0500177 smb_msg->msg_flags = MSG_NOSIGNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Al Viro3ab3f2a2015-11-13 02:36:04 -0500179 while (msg_data_left(smb_msg)) {
Jeff Layton6f49f462012-09-18 16:20:34 -0700180 /*
181 * If blocking send, we try 3 times, since each can block
182 * for 5 seconds. For nonblocking we have to try more
183 * but wait increasing amounts of time allowing time for
184 * socket to clear. The overall time we wait in either
185 * case to send on the socket is about 15 seconds.
186 * Similarly we wait for 15 seconds for a response from
187 * the server in SendReceive[2] for the server to send
188 * a response back for most types of requests (except
189 * SMB Write past end of file which can be slow, and
190 * blocking lock operations). NFS waits slightly longer
191 * than CIFS, but this can make it take longer for
192 * nonresponsive servers to be detected and 15 seconds
193 * is more than enough time for modern networks to
194 * send a packet. In most cases if we fail to send
195 * after the retries we will kill the socket and
196 * reconnect which may clear the network problem.
197 */
Al Viro3ab3f2a2015-11-13 02:36:04 -0500198 rc = sock_sendmsg(ssocket, smb_msg);
Jeff Laytonce6c44e2013-03-22 08:36:45 -0400199 if (rc == -EAGAIN) {
Al Viro3ab3f2a2015-11-13 02:36:04 -0500200 retries++;
201 if (retries >= 14 ||
202 (!server->noblocksnd && (retries > 2))) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500203 cifs_dbg(VFS, "sends on sock %p stuck for 15 seconds\n",
204 ssocket);
Al Viro3ab3f2a2015-11-13 02:36:04 -0500205 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
Al Viro3ab3f2a2015-11-13 02:36:04 -0500207 msleep(1 << retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 continue;
209 }
Jeff Layton6f49f462012-09-18 16:20:34 -0700210
Steve French79a58d12007-07-06 22:44:50 +0000211 if (rc < 0)
Al Viro3ab3f2a2015-11-13 02:36:04 -0500212 return rc;
Jeff Layton6f49f462012-09-18 16:20:34 -0700213
Steve French79a58d12007-07-06 22:44:50 +0000214 if (rc == 0) {
Steve French3e844692005-10-03 13:37:24 -0700215 /* should never happen, letting socket clear before
216 retrying is our only obvious option here */
Joe Perchesf96637b2013-05-04 22:12:25 -0500217 cifs_dbg(VFS, "tcp sent no data\n");
Steve French3e844692005-10-03 13:37:24 -0700218 msleep(500);
219 continue;
220 }
Jeff Layton6f49f462012-09-18 16:20:34 -0700221
Al Viro3ab3f2a2015-11-13 02:36:04 -0500222 /* send was at least partially successful */
223 *sent += rc;
224 retries = 0; /* in case we get ENOSPC on the next send */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
Al Viro3ab3f2a2015-11-13 02:36:04 -0500226 return 0;
Jeff Layton97bc00b2012-09-18 16:20:35 -0700227}
228
Paulo Alcantara35e2cc12018-06-15 10:22:44 -0300229unsigned long
Ronnie Sahlberg81f39f92018-06-28 10:47:14 +1000230smb_rqst_len(struct TCP_Server_Info *server, struct smb_rqst *rqst)
Jeff Laytona26054d2014-02-14 07:21:00 -0500231{
232 unsigned int i;
Paulo Alcantara35e2cc12018-06-15 10:22:44 -0300233 struct kvec *iov;
234 int nvec;
Jeff Laytona26054d2014-02-14 07:21:00 -0500235 unsigned long buflen = 0;
236
Ronnie Sahlberg81f39f92018-06-28 10:47:14 +1000237 if (server->vals->header_preamble_size == 0 &&
238 rqst->rq_nvec >= 2 && rqst->rq_iov[0].iov_len == 4) {
Paulo Alcantara35e2cc12018-06-15 10:22:44 -0300239 iov = &rqst->rq_iov[1];
240 nvec = rqst->rq_nvec - 1;
241 } else {
242 iov = rqst->rq_iov;
243 nvec = rqst->rq_nvec;
244 }
245
Jeff Laytona26054d2014-02-14 07:21:00 -0500246 /* total up iov array first */
Paulo Alcantara35e2cc12018-06-15 10:22:44 -0300247 for (i = 0; i < nvec; i++)
Jeff Laytona26054d2014-02-14 07:21:00 -0500248 buflen += iov[i].iov_len;
249
Long Lic06a0f22018-05-30 12:47:57 -0700250 /*
251 * Add in the page array if there is one. The caller needs to make
252 * sure rq_offset and rq_tailsz are set correctly. If a buffer of
253 * multiple pages ends at page boundary, rq_tailsz needs to be set to
254 * PAGE_SIZE.
255 */
Jeff Laytona26054d2014-02-14 07:21:00 -0500256 if (rqst->rq_npages) {
Long Lic06a0f22018-05-30 12:47:57 -0700257 if (rqst->rq_npages == 1)
258 buflen += rqst->rq_tailsz;
259 else {
260 /*
261 * If there is more than one page, calculate the
262 * buffer length based on rq_offset and rq_tailsz
263 */
264 buflen += rqst->rq_pagesz * (rqst->rq_npages - 1) -
265 rqst->rq_offset;
266 buflen += rqst->rq_tailsz;
267 }
Jeff Laytona26054d2014-02-14 07:21:00 -0500268 }
269
270 return buflen;
271}
272
Jeff Layton6f49f462012-09-18 16:20:34 -0700273static int
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000274__smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
275 struct smb_rqst *rqst)
Jeff Layton6f49f462012-09-18 16:20:34 -0700276{
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000277 int rc = 0;
278 struct kvec *iov;
279 int n_vec;
280 unsigned int send_length = 0;
281 unsigned int i, j;
Al Viro3ab3f2a2015-11-13 02:36:04 -0500282 size_t total_len = 0, sent, size;
Jeff Laytonb8eed282012-09-18 16:20:35 -0700283 struct socket *ssocket = server->ssocket;
Al Viro3ab3f2a2015-11-13 02:36:04 -0500284 struct msghdr smb_msg;
Jeff Laytonb8eed282012-09-18 16:20:35 -0700285 int val = 1;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000286 __be32 rfc1002_marker;
287
Long Li9762c2d2017-11-22 17:38:43 -0700288 if (cifs_rdma_enabled(server) && server->smbd_conn) {
Ronnie Sahlberg81f39f92018-06-28 10:47:14 +1000289 rc = smbd_send(server, rqst);
Long Li9762c2d2017-11-22 17:38:43 -0700290 goto smbd_done;
291 }
Jeff Laytonea702b82012-12-27 07:28:55 -0500292 if (ssocket == NULL)
293 return -ENOTSOCK;
294
Jeff Laytonb8eed282012-09-18 16:20:35 -0700295 /* cork the socket */
296 kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
297 (char *)&val, sizeof(val));
298
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000299 for (j = 0; j < num_rqst; j++)
Ronnie Sahlberg81f39f92018-06-28 10:47:14 +1000300 send_length += smb_rqst_len(server, &rqst[j]);
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000301 rfc1002_marker = cpu_to_be32(send_length);
302
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000303 /* Generate a rfc1002 marker for SMB2+ */
304 if (server->vals->header_preamble_size == 0) {
305 struct kvec hiov = {
306 .iov_base = &rfc1002_marker,
307 .iov_len = 4
308 };
309 iov_iter_kvec(&smb_msg.msg_iter, WRITE | ITER_KVEC, &hiov,
310 1, 4);
311 rc = smb_send_kvec(server, &smb_msg, &sent);
312 if (rc < 0)
313 goto uncork;
314
315 total_len += sent;
316 send_length += 4;
317 }
318
Paulo Alcantara662bf5b2018-06-14 17:34:08 -0300319 cifs_dbg(FYI, "Sending smb: smb_len=%u\n", send_length);
320
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000321 for (j = 0; j < num_rqst; j++) {
322 iov = rqst[j].rq_iov;
323 n_vec = rqst[j].rq_nvec;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000324
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000325 size = 0;
Paulo Alcantara662bf5b2018-06-14 17:34:08 -0300326 for (i = 0; i < n_vec; i++) {
327 dump_smb(iov[i].iov_base, iov[i].iov_len);
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000328 size += iov[i].iov_len;
Paulo Alcantara662bf5b2018-06-14 17:34:08 -0300329 }
Al Viro3ab3f2a2015-11-13 02:36:04 -0500330
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000331 iov_iter_kvec(&smb_msg.msg_iter, WRITE | ITER_KVEC,
332 iov, n_vec, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Al Viro3ab3f2a2015-11-13 02:36:04 -0500334 rc = smb_send_kvec(server, &smb_msg, &sent);
Jeff Layton97bc00b2012-09-18 16:20:35 -0700335 if (rc < 0)
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000336 goto uncork;
Jeff Layton97bc00b2012-09-18 16:20:35 -0700337
338 total_len += sent;
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000339
340 /* now walk the page array and send each page in it */
341 for (i = 0; i < rqst[j].rq_npages; i++) {
342 struct bio_vec bvec;
343
344 bvec.bv_page = rqst[j].rq_pages[i];
345 rqst_page_get_length(&rqst[j], i, &bvec.bv_len,
346 &bvec.bv_offset);
347
348 iov_iter_bvec(&smb_msg.msg_iter, WRITE | ITER_BVEC,
349 &bvec, 1, bvec.bv_len);
350 rc = smb_send_kvec(server, &smb_msg, &sent);
351 if (rc < 0)
352 break;
353
354 total_len += sent;
355 }
Jeff Layton97bc00b2012-09-18 16:20:35 -0700356 }
357
358uncork:
Jeff Laytonb8eed282012-09-18 16:20:35 -0700359 /* uncork it */
360 val = 0;
361 kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
362 (char *)&val, sizeof(val));
363
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000364 if ((total_len > 0) && (total_len != send_length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500365 cifs_dbg(FYI, "partial send (wanted=%u sent=%zu): terminating session\n",
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000366 send_length, total_len);
Jeff Layton6f49f462012-09-18 16:20:34 -0700367 /*
368 * If we have only sent part of an SMB then the next SMB could
369 * be taken as the remainder of this one. We need to kill the
370 * socket so the server throws away the partial SMB
371 */
Steve Frenchedf1ae42008-10-29 00:47:57 +0000372 server->tcpStatus = CifsNeedReconnect;
Steve Frenchbf1fdeb2018-07-30 19:23:09 -0500373 trace_smb3_partial_send_reconnect(server->CurrentMid,
374 server->hostname);
Steve Frenchedf1ae42008-10-29 00:47:57 +0000375 }
Long Li9762c2d2017-11-22 17:38:43 -0700376smbd_done:
Jeff Laytond804d412011-01-28 15:05:43 -0500377 if (rc < 0 && rc != -EINTR)
Joe Perchesf96637b2013-05-04 22:12:25 -0500378 cifs_dbg(VFS, "Error %d sending data on socket to server\n",
379 rc);
Jeff Laytond804d412011-01-28 15:05:43 -0500380 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 return rc;
384}
385
Jeff Layton6f49f462012-09-18 16:20:34 -0700386static int
Ronnie Sahlberg1f3a8f52018-08-01 09:26:12 +1000387smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
388 struct smb_rqst *rqst, int flags)
Jeff Layton6f49f462012-09-18 16:20:34 -0700389{
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +1000390 struct kvec iov;
391 struct smb2_transform_hdr tr_hdr;
392 struct smb_rqst cur_rqst[MAX_COMPOUND];
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700393 int rc;
Jeff Layton6f49f462012-09-18 16:20:34 -0700394
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700395 if (!(flags & CIFS_TRANSFORM_REQ))
Ronnie Sahlberg1f3a8f52018-08-01 09:26:12 +1000396 return __smb_send_rqst(server, num_rqst, rqst);
397
398 if (num_rqst > MAX_COMPOUND - 1)
399 return -ENOMEM;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700400
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +1000401 memset(&cur_rqst[0], 0, sizeof(cur_rqst));
402 memset(&iov, 0, sizeof(iov));
403 memset(&tr_hdr, 0, sizeof(tr_hdr));
404
405 iov.iov_base = &tr_hdr;
406 iov.iov_len = sizeof(tr_hdr);
407 cur_rqst[0].rq_iov = &iov;
408 cur_rqst[0].rq_nvec = 1;
409
410 if (!server->ops->init_transform_rq) {
411 cifs_dbg(VFS, "Encryption requested but transform callback "
412 "is missing\n");
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700413 return -EIO;
414 }
415
Ronnie Sahlberg1f3a8f52018-08-01 09:26:12 +1000416 rc = server->ops->init_transform_rq(server, num_rqst + 1,
417 &cur_rqst[0], rqst);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700418 if (rc)
419 return rc;
420
Ronnie Sahlberg1f3a8f52018-08-01 09:26:12 +1000421 rc = __smb_send_rqst(server, num_rqst + 1, &cur_rqst[0]);
422 smb3_free_compound_rqst(num_rqst, &cur_rqst[1]);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700423 return rc;
Jeff Layton6f49f462012-09-18 16:20:34 -0700424}
425
Jeff Layton0496e022008-12-30 12:39:16 -0500426int
427smb_send(struct TCP_Server_Info *server, struct smb_hdr *smb_buffer,
428 unsigned int smb_buf_length)
429{
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800430 struct kvec iov[2];
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700431 struct smb_rqst rqst = { .rq_iov = iov,
432 .rq_nvec = 2 };
Jeff Layton0496e022008-12-30 12:39:16 -0500433
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800434 iov[0].iov_base = smb_buffer;
435 iov[0].iov_len = 4;
436 iov[1].iov_base = (char *)smb_buffer + 4;
437 iov[1].iov_len = smb_buf_length;
Jeff Layton0496e022008-12-30 12:39:16 -0500438
Ronnie Sahlberg07cd9522018-06-12 08:01:00 +1000439 return __smb_send_rqst(server, 1, &rqst);
Jeff Layton0496e022008-12-30 12:39:16 -0500440}
441
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300442static int
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400443wait_for_free_credits(struct TCP_Server_Info *server, const int timeout,
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300444 int *credits)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000445{
Pavel Shilovsky5bc59492012-02-21 19:56:08 +0300446 int rc;
447
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300448 spin_lock(&server->req_lock);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400449 if (timeout == CIFS_ASYNC_OP) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000450 /* oplock breaks must not be held up */
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300451 server->in_flight++;
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300452 *credits -= 1;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300453 spin_unlock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000454 return 0;
455 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000456
Volker Lendecke27a97a62008-12-08 20:59:39 +0000457 while (1) {
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300458 if (*credits <= 0) {
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300459 spin_unlock(&server->req_lock);
Steve French789e6662011-08-09 18:44:44 +0000460 cifs_num_waiters_inc(server);
Pavel Shilovsky5bc59492012-02-21 19:56:08 +0300461 rc = wait_event_killable(server->request_q,
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300462 has_credits(server, credits));
Steve French789e6662011-08-09 18:44:44 +0000463 cifs_num_waiters_dec(server);
Pavel Shilovsky5bc59492012-02-21 19:56:08 +0300464 if (rc)
465 return rc;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300466 spin_lock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000467 } else {
Jeff Laytonc5797a92011-01-11 07:24:01 -0500468 if (server->tcpStatus == CifsExiting) {
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300469 spin_unlock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000470 return -ENOENT;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000471 }
Volker Lendecke27a97a62008-12-08 20:59:39 +0000472
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400473 /*
474 * Can not count locking commands against total
475 * as they are allowed to block on server.
476 */
Volker Lendecke27a97a62008-12-08 20:59:39 +0000477
478 /* update # of requests on the wire to server */
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400479 if (timeout != CIFS_BLOCKING_OP) {
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300480 *credits -= 1;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300481 server->in_flight++;
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400482 }
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300483 spin_unlock(&server->req_lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000484 break;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000485 }
486 }
487 return 0;
488}
489
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300490static int
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400491wait_for_free_request(struct TCP_Server_Info *server, const int timeout,
492 const int optype)
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300493{
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -0500494 int *val;
495
496 val = server->ops->get_credits_field(server, optype);
497 /* Since an echo is already inflight, no need to wait to send another */
498 if (*val <= 0 && optype == CIFS_ECHO_OP)
499 return -EAGAIN;
500 return wait_for_free_credits(server, timeout, val);
Pavel Shilovskybc205ed2012-03-15 13:22:27 +0300501}
502
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400503int
504cifs_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
505 unsigned int *num, unsigned int *credits)
506{
507 *num = size;
508 *credits = 0;
509 return 0;
510}
511
Steve French96daf2b2011-05-27 04:34:02 +0000512static int allocate_mid(struct cifs_ses *ses, struct smb_hdr *in_buf,
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000513 struct mid_q_entry **ppmidQ)
514{
515 if (ses->server->tcpStatus == CifsExiting) {
516 return -ENOENT;
Volker Lendecke8fbbd362008-12-06 13:12:34 +0100517 }
518
519 if (ses->server->tcpStatus == CifsNeedReconnect) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500520 cifs_dbg(FYI, "tcp session dead - return to caller to retry\n");
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000521 return -EAGAIN;
Volker Lendecke8fbbd362008-12-06 13:12:34 +0100522 }
523
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500524 if (ses->status == CifsNew) {
Steve French79a58d12007-07-06 22:44:50 +0000525 if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
Steve Frenchad7a2922008-02-07 23:25:02 +0000526 (in_buf->Command != SMB_COM_NEGOTIATE))
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000527 return -EAGAIN;
Steve Frenchad7a2922008-02-07 23:25:02 +0000528 /* else ok - we are setting up session */
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000529 }
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500530
531 if (ses->status == CifsExiting) {
532 /* check if SMB session is bad because we are setting it up */
533 if (in_buf->Command != SMB_COM_LOGOFF_ANDX)
534 return -EAGAIN;
535 /* else ok - we are shutting down session */
536 }
537
Jeff Layton24b9b062008-12-01 07:09:34 -0500538 *ppmidQ = AllocMidQEntry(in_buf, ses->server);
Steve French26f57362007-08-30 22:09:15 +0000539 if (*ppmidQ == NULL)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000540 return -ENOMEM;
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500541 spin_lock(&GlobalMid_Lock);
542 list_add_tail(&(*ppmidQ)->qhead, &ses->server->pending_mid_q);
543 spin_unlock(&GlobalMid_Lock);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000544 return 0;
545}
546
Jeff Layton0ade6402011-01-11 07:24:02 -0500547static int
548wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *midQ)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000549{
Jeff Layton0ade6402011-01-11 07:24:02 -0500550 int error;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000551
Colin Cross5853cc22013-05-07 17:52:05 +0000552 error = wait_event_freezekillable_unsafe(server->response_q,
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400553 midQ->mid_state != MID_REQUEST_SUBMITTED);
Jeff Layton0ade6402011-01-11 07:24:02 -0500554 if (error < 0)
555 return -ERESTARTSYS;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000556
Jeff Layton0ade6402011-01-11 07:24:02 -0500557 return 0;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000558}
559
Jeff Laytonfec344e2012-09-18 16:20:35 -0700560struct mid_q_entry *
561cifs_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400562{
563 int rc;
Jeff Laytonfec344e2012-09-18 16:20:35 -0700564 struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400565 struct mid_q_entry *mid;
566
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800567 if (rqst->rq_iov[0].iov_len != 4 ||
568 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
569 return ERR_PTR(-EIO);
570
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400571 /* enable signing if server requires it */
Jeff Layton38d77c52013-05-26 07:01:00 -0400572 if (server->sign)
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400573 hdr->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
574
575 mid = AllocMidQEntry(hdr, server);
576 if (mid == NULL)
Jeff Laytonfec344e2012-09-18 16:20:35 -0700577 return ERR_PTR(-ENOMEM);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400578
Jeff Laytonfec344e2012-09-18 16:20:35 -0700579 rc = cifs_sign_rqst(rqst, server, &mid->sequence_number);
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100580 if (rc) {
581 DeleteMidQEntry(mid);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700582 return ERR_PTR(rc);
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100583 }
584
Jeff Laytonfec344e2012-09-18 16:20:35 -0700585 return mid;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400586}
Steve French133672e2007-11-13 22:41:37 +0000587
588/*
Jeff Laytona6827c12011-01-11 07:24:21 -0500589 * Send a SMB request and set the callback function in the mid to handle
590 * the result. Caller is responsible for dealing with timeouts.
591 */
592int
Jeff Laytonfec344e2012-09-18 16:20:35 -0700593cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst,
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -0800594 mid_receive_t *receive, mid_callback_t *callback,
595 mid_handle_t *handle, void *cbdata, const int flags)
Jeff Laytona6827c12011-01-11 07:24:21 -0500596{
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400597 int rc, timeout, optype;
Jeff Laytona6827c12011-01-11 07:24:21 -0500598 struct mid_q_entry *mid;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400599 unsigned int credits = 0;
Jeff Laytona6827c12011-01-11 07:24:21 -0500600
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400601 timeout = flags & CIFS_TIMEOUT_MASK;
602 optype = flags & CIFS_OP_MASK;
603
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400604 if ((flags & CIFS_HAS_CREDITS) == 0) {
605 rc = wait_for_free_request(server, timeout, optype);
606 if (rc)
607 return rc;
608 credits = 1;
609 }
Jeff Laytona6827c12011-01-11 07:24:21 -0500610
611 mutex_lock(&server->srv_mutex);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700612 mid = server->ops->setup_async_request(server, rqst);
613 if (IS_ERR(mid)) {
Jeff Laytona6827c12011-01-11 07:24:21 -0500614 mutex_unlock(&server->srv_mutex);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400615 add_credits_and_wake_if(server, credits, optype);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700616 return PTR_ERR(mid);
Jeff Laytona6827c12011-01-11 07:24:21 -0500617 }
618
Jeff Layton44d22d82011-10-19 15:29:49 -0400619 mid->receive = receive;
Jeff Laytona6827c12011-01-11 07:24:21 -0500620 mid->callback = callback;
621 mid->callback_data = cbdata;
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -0800622 mid->handle = handle;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400623 mid->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +0000624
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100625 /* put it on the pending_mid_q */
626 spin_lock(&GlobalMid_Lock);
627 list_add_tail(&mid->qhead, &server->pending_mid_q);
628 spin_unlock(&GlobalMid_Lock);
629
Long Li93d2cb62017-06-28 15:55:55 -0700630 /*
631 * Need to store the time in mid before calling I/O. For call_async,
632 * I/O response may come back and free the mid entry on another thread.
633 */
634 cifs_save_when_sent(mid);
Steve French789e6662011-08-09 18:44:44 +0000635 cifs_in_send_inc(server);
Ronnie Sahlberg1f3a8f52018-08-01 09:26:12 +1000636 rc = smb_send_rqst(server, 1, rqst, flags);
Steve French789e6662011-08-09 18:44:44 +0000637 cifs_in_send_dec(server);
Jeff Laytonad313cb2013-04-03 10:27:36 -0400638
Rabin Vincent820962d2015-12-23 07:32:41 +0100639 if (rc < 0) {
Jeff Laytonad313cb2013-04-03 10:27:36 -0400640 server->sequence_number -= 2;
Rabin Vincent820962d2015-12-23 07:32:41 +0100641 cifs_delete_mid(mid);
642 }
643
Jeff Laytona6827c12011-01-11 07:24:21 -0500644 mutex_unlock(&server->srv_mutex);
Steve French789e6662011-08-09 18:44:44 +0000645
Sachin Prabhuffc61cc2012-07-11 12:28:05 +0100646 if (rc == 0)
647 return 0;
Jeff Laytona6827c12011-01-11 07:24:21 -0500648
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400649 add_credits_and_wake_if(server, credits, optype);
Jeff Laytona6827c12011-01-11 07:24:21 -0500650 return rc;
651}
652
653/*
Steve French133672e2007-11-13 22:41:37 +0000654 *
655 * Send an SMB Request. No response info (other than return code)
656 * needs to be parsed.
657 *
658 * flags indicate the type of request buffer and how long to wait
659 * and whether to log NT STATUS code (error) before mapping it to POSIX error
660 *
661 */
662int
Steve French96daf2b2011-05-27 04:34:02 +0000663SendReceiveNoRsp(const unsigned int xid, struct cifs_ses *ses,
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400664 char *in_buf, int flags)
Steve French133672e2007-11-13 22:41:37 +0000665{
666 int rc;
667 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700668 struct kvec rsp_iov;
Steve French133672e2007-11-13 22:41:37 +0000669 int resp_buf_type;
670
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400671 iov[0].iov_base = in_buf;
672 iov[0].iov_len = get_rfc1002_length(in_buf) + 4;
Steve French133672e2007-11-13 22:41:37 +0000673 flags |= CIFS_NO_RESP;
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700674 rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Joe Perchesf96637b2013-05-04 22:12:25 -0500675 cifs_dbg(NOISY, "SendRcvNoRsp flags %d rc %d\n", flags, rc);
Steve French90c81e02008-02-12 20:32:36 +0000676
Steve French133672e2007-11-13 22:41:37 +0000677 return rc;
678}
679
Jeff Layton053d5032011-01-11 07:24:02 -0500680static int
Jeff Layton3c1105d2011-05-22 07:09:13 -0400681cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
Jeff Layton053d5032011-01-11 07:24:02 -0500682{
683 int rc = 0;
684
Joe Perchesf96637b2013-05-04 22:12:25 -0500685 cifs_dbg(FYI, "%s: cmd=%d mid=%llu state=%d\n",
686 __func__, le16_to_cpu(mid->command), mid->mid, mid->mid_state);
Jeff Layton053d5032011-01-11 07:24:02 -0500687
Jeff Layton74dd92a2011-01-11 07:24:02 -0500688 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400689 switch (mid->mid_state) {
Jeff Layton74dd92a2011-01-11 07:24:02 -0500690 case MID_RESPONSE_RECEIVED:
Jeff Layton053d5032011-01-11 07:24:02 -0500691 spin_unlock(&GlobalMid_Lock);
692 return rc;
Jeff Layton74dd92a2011-01-11 07:24:02 -0500693 case MID_RETRY_NEEDED:
694 rc = -EAGAIN;
695 break;
Jeff Layton71823ba2011-02-10 08:03:50 -0500696 case MID_RESPONSE_MALFORMED:
697 rc = -EIO;
698 break;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400699 case MID_SHUTDOWN:
700 rc = -EHOSTDOWN;
701 break;
Jeff Layton74dd92a2011-01-11 07:24:02 -0500702 default:
Jeff Layton3c1105d2011-05-22 07:09:13 -0400703 list_del_init(&mid->qhead);
Joe Perchesf96637b2013-05-04 22:12:25 -0500704 cifs_dbg(VFS, "%s: invalid mid state mid=%llu state=%d\n",
705 __func__, mid->mid, mid->mid_state);
Jeff Layton74dd92a2011-01-11 07:24:02 -0500706 rc = -EIO;
Jeff Layton053d5032011-01-11 07:24:02 -0500707 }
708 spin_unlock(&GlobalMid_Lock);
709
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500710 DeleteMidQEntry(mid);
Jeff Layton053d5032011-01-11 07:24:02 -0500711 return rc;
712}
713
Jeff Layton121b0462012-05-15 12:21:10 -0400714static inline int
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800715send_cancel(struct TCP_Server_Info *server, struct smb_rqst *rqst,
716 struct mid_q_entry *mid)
Jeff Layton76dcc262011-01-11 07:24:24 -0500717{
Jeff Layton121b0462012-05-15 12:21:10 -0400718 return server->ops->send_cancel ?
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800719 server->ops->send_cancel(server, rqst, mid) : 0;
Jeff Layton76dcc262011-01-11 07:24:24 -0500720}
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722int
Jeff Layton2c8f9812011-05-19 16:22:52 -0400723cifs_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
724 bool log_error)
725{
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400726 unsigned int len = get_rfc1002_length(mid->resp_buf) + 4;
Jeff Layton826a95e2011-10-11 06:41:32 -0400727
728 dump_smb(mid->resp_buf, min_t(u32, 92, len));
Jeff Layton2c8f9812011-05-19 16:22:52 -0400729
730 /* convert the length into a more usable form */
Jeff Layton38d77c52013-05-26 07:01:00 -0400731 if (server->sign) {
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800732 struct kvec iov[2];
Steve French985e4ff02012-08-03 09:42:45 -0500733 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800734 struct smb_rqst rqst = { .rq_iov = iov,
735 .rq_nvec = 2 };
Jeff Layton826a95e2011-10-11 06:41:32 -0400736
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800737 iov[0].iov_base = mid->resp_buf;
738 iov[0].iov_len = 4;
739 iov[1].iov_base = (char *)mid->resp_buf + 4;
740 iov[1].iov_len = len - 4;
Jeff Layton2c8f9812011-05-19 16:22:52 -0400741 /* FIXME: add code to kill session */
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700742 rc = cifs_verify_signature(&rqst, server,
Jeff Layton0124cc42013-04-03 11:55:03 -0400743 mid->sequence_number);
Steve French985e4ff02012-08-03 09:42:45 -0500744 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500745 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
746 rc);
Jeff Layton2c8f9812011-05-19 16:22:52 -0400747 }
748
749 /* BB special case reconnect tid and uid here? */
750 return map_smb_to_linux_error(mid->resp_buf, log_error);
751}
752
Jeff Laytonfec344e2012-09-18 16:20:35 -0700753struct mid_q_entry *
754cifs_setup_request(struct cifs_ses *ses, struct smb_rqst *rqst)
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400755{
756 int rc;
Jeff Laytonfec344e2012-09-18 16:20:35 -0700757 struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400758 struct mid_q_entry *mid;
759
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800760 if (rqst->rq_iov[0].iov_len != 4 ||
761 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
762 return ERR_PTR(-EIO);
763
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400764 rc = allocate_mid(ses, hdr, &mid);
765 if (rc)
Jeff Laytonfec344e2012-09-18 16:20:35 -0700766 return ERR_PTR(rc);
767 rc = cifs_sign_rqst(rqst, ses->server, &mid->sequence_number);
768 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700769 cifs_delete_mid(mid);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700770 return ERR_PTR(rc);
771 }
772 return mid;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400773}
774
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800775int
Ronnie Sahlberge0bba0b2018-08-01 09:26:13 +1000776compound_send_recv(const unsigned int xid, struct cifs_ses *ses,
777 const int flags, const int num_rqst, struct smb_rqst *rqst,
778 int *resp_buf_type, struct kvec *resp_iov)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
Ronnie Sahlberge0bba0b2018-08-01 09:26:13 +1000780 int i, j, rc = 0;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400781 int timeout, optype;
Ronnie Sahlberge0bba0b2018-08-01 09:26:13 +1000782 struct mid_q_entry *midQ[MAX_COMPOUND];
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400783 unsigned int credits = 1;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800784 char *buf;
Steve French50c2f752007-07-13 00:33:32 +0000785
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400786 timeout = flags & CIFS_TIMEOUT_MASK;
787 optype = flags & CIFS_OP_MASK;
Steve French133672e2007-11-13 22:41:37 +0000788
Ronnie Sahlberge0bba0b2018-08-01 09:26:13 +1000789 for (i = 0; i < num_rqst; i++)
790 resp_buf_type[i] = CIFS_NO_BUFFER; /* no response buf yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Steve French4b8f9302006-02-26 16:41:18 +0000792 if ((ses == NULL) || (ses->server == NULL)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500793 cifs_dbg(VFS, "Null session\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return -EIO;
795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700797 if (ses->server->tcpStatus == CifsExiting)
Steve French31ca3bc2005-04-28 22:41:11 -0700798 return -ENOENT;
799
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400800 /*
801 * Ensure that we do not send more than 50 overlapping requests
802 * to the same server. We may make this configurable later or
803 * use ses->maxReq.
804 */
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400805 rc = wait_for_free_request(ses->server, timeout, optype);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700806 if (rc)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000807 return rc;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000808
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400809 /*
810 * Make sure that we sign in the same order that we send on this socket
811 * and avoid races inside tcp sendmsg code that could cause corruption
812 * of smb data.
813 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Jeff Layton72ca5452008-12-01 07:09:36 -0500815 mutex_lock(&ses->server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Ronnie Sahlberge0bba0b2018-08-01 09:26:13 +1000817 for (i = 0; i < num_rqst; i++) {
818 midQ[i] = ses->server->ops->setup_request(ses, &rqst[i]);
819 if (IS_ERR(midQ[i])) {
820 for (j = 0; j < i; j++)
821 cifs_delete_mid(midQ[j]);
822 mutex_unlock(&ses->server->srv_mutex);
823 /* Update # of requests on wire to server */
824 add_credits(ses->server, 1, optype);
825 return PTR_ERR(midQ[i]);
826 }
827
828 midQ[i]->mid_state = MID_REQUEST_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Steve French789e6662011-08-09 18:44:44 +0000831 cifs_in_send_inc(ses->server);
Ronnie Sahlberge0bba0b2018-08-01 09:26:13 +1000832 rc = smb_send_rqst(ses->server, num_rqst, rqst, flags);
Steve French789e6662011-08-09 18:44:44 +0000833 cifs_in_send_dec(ses->server);
Ronnie Sahlberge0bba0b2018-08-01 09:26:13 +1000834
835 for (i = 0; i < num_rqst; i++)
836 cifs_save_when_sent(midQ[i]);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000837
Jeff Laytonad313cb2013-04-03 10:27:36 -0400838 if (rc < 0)
839 ses->server->sequence_number -= 2;
Ronnie Sahlberge0bba0b2018-08-01 09:26:13 +1000840
Jeff Layton72ca5452008-12-01 07:09:36 -0500841 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000842
Ronnie Sahlberge0bba0b2018-08-01 09:26:13 +1000843 for (i = 0; i < num_rqst; i++) {
844 if (rc < 0)
845 goto out;
Steve French4b8f9302006-02-26 16:41:18 +0000846
Ronnie Sahlberge0bba0b2018-08-01 09:26:13 +1000847 if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP))
848 smb311_update_preauth_hash(ses, rqst[i].rq_iov,
849 rqst[i].rq_nvec);
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100850
Ronnie Sahlberge0bba0b2018-08-01 09:26:13 +1000851 if (timeout == CIFS_ASYNC_OP)
852 goto out;
Jeff Layton0ade6402011-01-11 07:24:02 -0500853
Ronnie Sahlberge0bba0b2018-08-01 09:26:13 +1000854 rc = wait_for_response(ses->server, midQ[i]);
855 if (rc != 0) {
856 cifs_dbg(FYI, "Cancelling wait for mid %llu\n",
857 midQ[i]->mid);
858 send_cancel(ses->server, &rqst[i], midQ[i]);
859 spin_lock(&GlobalMid_Lock);
860 if (midQ[i]->mid_state == MID_REQUEST_SUBMITTED) {
861 midQ[i]->mid_flags |= MID_WAIT_CANCELLED;
862 midQ[i]->callback = DeleteMidQEntry;
863 spin_unlock(&GlobalMid_Lock);
864 add_credits(ses->server, 1, optype);
865 return rc;
866 }
Jeff Layton1be912d2011-01-28 07:08:28 -0500867 spin_unlock(&GlobalMid_Lock);
Ronnie Sahlberge0bba0b2018-08-01 09:26:13 +1000868 }
869
870 rc = cifs_sync_mid_result(midQ[i], ses->server);
871 if (rc != 0) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400872 add_credits(ses->server, 1, optype);
Jeff Layton1be912d2011-01-28 07:08:28 -0500873 return rc;
874 }
Ronnie Sahlberge0bba0b2018-08-01 09:26:13 +1000875
876 if (!midQ[i]->resp_buf ||
877 midQ[i]->mid_state != MID_RESPONSE_RECEIVED) {
878 rc = -EIO;
879 cifs_dbg(FYI, "Bad MID state?\n");
880 goto out;
881 }
882
883 buf = (char *)midQ[i]->resp_buf;
884 resp_iov[i].iov_base = buf;
885 resp_iov[i].iov_len = midQ[i]->resp_buf_size +
886 ses->server->vals->header_preamble_size;
887
888 if (midQ[i]->large_buf)
889 resp_buf_type[i] = CIFS_LARGE_BUFFER;
890 else
891 resp_buf_type[i] = CIFS_SMALL_BUFFER;
892
893 if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP)) {
894 struct kvec iov = {
895 .iov_base = resp_iov[i].iov_base,
896 .iov_len = resp_iov[i].iov_len
897 };
898 smb311_update_preauth_hash(ses, &iov, 1);
899 }
900
901 credits = ses->server->ops->get_credits(midQ[i]);
902
903 rc = ses->server->ops->check_receive(midQ[i], ses->server,
904 flags & CIFS_LOG_ERROR);
905
906 /* mark it so buf will not be freed by cifs_delete_mid */
907 if ((flags & CIFS_NO_RESP) == 0)
908 midQ[i]->resp_buf = NULL;
Jeff Layton1be912d2011-01-28 07:08:28 -0500909 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000910out:
Ronnie Sahlberge0bba0b2018-08-01 09:26:13 +1000911 for (i = 0; i < num_rqst; i++)
912 cifs_delete_mid(midQ[i]);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400913 add_credits(ses->server, credits, optype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 return rc;
916}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918int
Ronnie Sahlberge0bba0b2018-08-01 09:26:13 +1000919cifs_send_recv(const unsigned int xid, struct cifs_ses *ses,
920 struct smb_rqst *rqst, int *resp_buf_type, const int flags,
921 struct kvec *resp_iov)
922{
923 return compound_send_recv(xid, ses, flags, 1, rqst, resp_buf_type,
924 resp_iov);
925}
926
927int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800928SendReceive2(const unsigned int xid, struct cifs_ses *ses,
929 struct kvec *iov, int n_vec, int *resp_buf_type /* ret */,
930 const int flags, struct kvec *resp_iov)
931{
932 struct smb_rqst rqst;
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +1100933 struct kvec s_iov[CIFS_MAX_IOV_SIZE], *new_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800934 int rc;
935
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +1100936 if (n_vec + 1 > CIFS_MAX_IOV_SIZE) {
Kees Cook6da2ec52018-06-12 13:55:00 -0700937 new_iov = kmalloc_array(n_vec + 1, sizeof(struct kvec),
938 GFP_KERNEL);
Steve French117e3b72018-04-22 10:24:19 -0500939 if (!new_iov) {
940 /* otherwise cifs_send_recv below sets resp_buf_type */
941 *resp_buf_type = CIFS_NO_BUFFER;
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +1100942 return -ENOMEM;
Steve French117e3b72018-04-22 10:24:19 -0500943 }
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +1100944 } else
945 new_iov = s_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800946
947 /* 1st iov is a RFC1001 length followed by the rest of the packet */
948 memcpy(new_iov + 1, iov, (sizeof(struct kvec) * n_vec));
949
950 new_iov[0].iov_base = new_iov[1].iov_base;
951 new_iov[0].iov_len = 4;
952 new_iov[1].iov_base += 4;
953 new_iov[1].iov_len -= 4;
954
955 memset(&rqst, 0, sizeof(struct smb_rqst));
956 rqst.rq_iov = new_iov;
957 rqst.rq_nvec = n_vec + 1;
958
959 rc = cifs_send_recv(xid, ses, &rqst, resp_buf_type, flags, resp_iov);
Ronnie Sahlberg3cecf482017-11-21 15:08:07 +1100960 if (n_vec + 1 > CIFS_MAX_IOV_SIZE)
961 kfree(new_iov);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800962 return rc;
963}
964
965int
Steve French96daf2b2011-05-27 04:34:02 +0000966SendReceive(const unsigned int xid, struct cifs_ses *ses,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400968 int *pbytes_returned, const int timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
970 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 struct mid_q_entry *midQ;
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800972 unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
973 struct kvec iov = { .iov_base = in_buf, .iov_len = len };
974 struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 if (ses == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500977 cifs_dbg(VFS, "Null smb session\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return -EIO;
979 }
Steve French79a58d12007-07-06 22:44:50 +0000980 if (ses->server == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500981 cifs_dbg(VFS, "Null tcp session\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 return -EIO;
983 }
984
Steve French79a58d12007-07-06 22:44:50 +0000985 if (ses->server->tcpStatus == CifsExiting)
Steve French31ca3bc2005-04-28 22:41:11 -0700986 return -ENOENT;
987
Steve French79a58d12007-07-06 22:44:50 +0000988 /* Ensure that we do not send more than 50 overlapping requests
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 to the same server. We may make this configurable later or
990 use ses->maxReq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800992 if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500993 cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -0800994 len);
Volker Lendecke6d9c6d52008-12-08 20:50:24 +0000995 return -EIO;
996 }
997
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400998 rc = wait_for_free_request(ses->server, timeout, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000999 if (rc)
1000 return rc;
1001
Steve French79a58d12007-07-06 22:44:50 +00001002 /* make sure that we sign in the same order that we send on this socket
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 and avoid races inside tcp sendmsg code that could cause corruption
1004 of smb data */
1005
Jeff Layton72ca5452008-12-01 07:09:36 -05001006 mutex_lock(&ses->server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001008 rc = allocate_mid(ses, in_buf, &midQ);
1009 if (rc) {
Jeff Layton72ca5452008-12-01 07:09:36 -05001010 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001011 /* Update # of requests on wire to server */
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001012 add_credits(ses->server, 1, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001013 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 }
1015
Steve Frenchad009ac2005-04-28 22:41:05 -07001016 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
Volker Lendecke829049c2008-12-06 16:00:53 +01001017 if (rc) {
1018 mutex_unlock(&ses->server->srv_mutex);
1019 goto out;
1020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001022 midQ->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +00001023
1024 cifs_in_send_inc(ses->server);
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001025 rc = smb_send(ses->server, in_buf, len);
Steve French789e6662011-08-09 18:44:44 +00001026 cifs_in_send_dec(ses->server);
1027 cifs_save_when_sent(midQ);
Jeff Laytonad313cb2013-04-03 10:27:36 -04001028
1029 if (rc < 0)
1030 ses->server->sequence_number -= 2;
1031
Jeff Layton72ca5452008-12-01 07:09:36 -05001032 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001033
Steve French79a58d12007-07-06 22:44:50 +00001034 if (rc < 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001035 goto out;
1036
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001037 if (timeout == CIFS_ASYNC_OP)
Steve French133672e2007-11-13 22:41:37 +00001038 goto out;
Jeff Layton0ade6402011-01-11 07:24:02 -05001039
1040 rc = wait_for_response(ses->server, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -05001041 if (rc != 0) {
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001042 send_cancel(ses->server, &rqst, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -05001043 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001044 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
Jeff Layton1be912d2011-01-28 07:08:28 -05001045 /* no longer considered to be "in-flight" */
1046 midQ->callback = DeleteMidQEntry;
1047 spin_unlock(&GlobalMid_Lock);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001048 add_credits(ses->server, 1, 0);
Jeff Layton1be912d2011-01-28 07:08:28 -05001049 return rc;
1050 }
1051 spin_unlock(&GlobalMid_Lock);
1052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Jeff Layton3c1105d2011-05-22 07:09:13 -04001054 rc = cifs_sync_mid_result(midQ, ses->server);
Jeff Layton053d5032011-01-11 07:24:02 -05001055 if (rc != 0) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001056 add_credits(ses->server, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 return rc;
1058 }
Steve French50c2f752007-07-13 00:33:32 +00001059
Jeff Layton2c8f9812011-05-19 16:22:52 -04001060 if (!midQ->resp_buf || !out_buf ||
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001061 midQ->mid_state != MID_RESPONSE_RECEIVED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 rc = -EIO;
Joe Perchesf96637b2013-05-04 22:12:25 -05001063 cifs_dbg(VFS, "Bad MID state?\n");
Steve French2b2bdfb2008-12-11 17:26:54 +00001064 goto out;
1065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001067 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
Jeff Layton2c8f9812011-05-19 16:22:52 -04001068 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
1069 rc = cifs_check_receive(midQ, ses->server, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001070out:
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001071 cifs_delete_mid(midQ);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001072 add_credits(ses->server, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 return rc;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001075}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001077/* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
1078 blocking lock to return. */
1079
1080static int
Steve French96daf2b2011-05-27 04:34:02 +00001081send_lock_cancel(const unsigned int xid, struct cifs_tcon *tcon,
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001082 struct smb_hdr *in_buf,
1083 struct smb_hdr *out_buf)
1084{
1085 int bytes_returned;
Steve French96daf2b2011-05-27 04:34:02 +00001086 struct cifs_ses *ses = tcon->ses;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001087 LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
1088
1089 /* We just modify the current in_buf to change
1090 the type of lock from LOCKING_ANDX_SHARED_LOCK
1091 or LOCKING_ANDX_EXCLUSIVE_LOCK to
1092 LOCKING_ANDX_CANCEL_LOCK. */
1093
1094 pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
1095 pSMB->Timeout = 0;
Pavel Shilovsky88257362012-05-23 14:01:59 +04001096 pSMB->hdr.Mid = get_next_mid(ses->server);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001097
1098 return SendReceive(xid, ses, in_buf, out_buf,
Jeff Layton77499812011-01-11 07:24:23 -05001099 &bytes_returned, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001100}
1101
1102int
Steve French96daf2b2011-05-27 04:34:02 +00001103SendReceiveBlockingLock(const unsigned int xid, struct cifs_tcon *tcon,
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001104 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
1105 int *pbytes_returned)
1106{
1107 int rc = 0;
1108 int rstart = 0;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001109 struct mid_q_entry *midQ;
Steve French96daf2b2011-05-27 04:34:02 +00001110 struct cifs_ses *ses;
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001111 unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
1112 struct kvec iov = { .iov_base = in_buf, .iov_len = len };
1113 struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001114
1115 if (tcon == NULL || tcon->ses == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001116 cifs_dbg(VFS, "Null smb session\n");
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001117 return -EIO;
1118 }
1119 ses = tcon->ses;
1120
Steve French79a58d12007-07-06 22:44:50 +00001121 if (ses->server == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001122 cifs_dbg(VFS, "Null tcp session\n");
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001123 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
1125
Steve French79a58d12007-07-06 22:44:50 +00001126 if (ses->server->tcpStatus == CifsExiting)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001127 return -ENOENT;
1128
Steve French79a58d12007-07-06 22:44:50 +00001129 /* Ensure that we do not send more than 50 overlapping requests
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001130 to the same server. We may make this configurable later or
1131 use ses->maxReq */
1132
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001133 if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001134 cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001135 len);
Volker Lendecke6d9c6d52008-12-08 20:50:24 +00001136 return -EIO;
1137 }
1138
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001139 rc = wait_for_free_request(ses->server, CIFS_BLOCKING_OP, 0);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001140 if (rc)
1141 return rc;
1142
Steve French79a58d12007-07-06 22:44:50 +00001143 /* make sure that we sign in the same order that we send on this socket
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001144 and avoid races inside tcp sendmsg code that could cause corruption
1145 of smb data */
1146
Jeff Layton72ca5452008-12-01 07:09:36 -05001147 mutex_lock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001148
1149 rc = allocate_mid(ses, in_buf, &midQ);
1150 if (rc) {
Jeff Layton72ca5452008-12-01 07:09:36 -05001151 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001152 return rc;
1153 }
1154
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001155 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
Volker Lendecke829049c2008-12-06 16:00:53 +01001156 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001157 cifs_delete_mid(midQ);
Volker Lendecke829049c2008-12-06 16:00:53 +01001158 mutex_unlock(&ses->server->srv_mutex);
1159 return rc;
1160 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001161
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001162 midQ->mid_state = MID_REQUEST_SUBMITTED;
Steve French789e6662011-08-09 18:44:44 +00001163 cifs_in_send_inc(ses->server);
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001164 rc = smb_send(ses->server, in_buf, len);
Steve French789e6662011-08-09 18:44:44 +00001165 cifs_in_send_dec(ses->server);
1166 cifs_save_when_sent(midQ);
Jeff Laytonad313cb2013-04-03 10:27:36 -04001167
1168 if (rc < 0)
1169 ses->server->sequence_number -= 2;
1170
Jeff Layton72ca5452008-12-01 07:09:36 -05001171 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001172
Steve French79a58d12007-07-06 22:44:50 +00001173 if (rc < 0) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001174 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001175 return rc;
1176 }
1177
1178 /* Wait for a reply - allow signals to interrupt. */
1179 rc = wait_event_interruptible(ses->server->response_q,
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001180 (!(midQ->mid_state == MID_REQUEST_SUBMITTED)) ||
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001181 ((ses->server->tcpStatus != CifsGood) &&
1182 (ses->server->tcpStatus != CifsNew)));
1183
1184 /* Were we interrupted by a signal ? */
1185 if ((rc == -ERESTARTSYS) &&
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001186 (midQ->mid_state == MID_REQUEST_SUBMITTED) &&
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001187 ((ses->server->tcpStatus == CifsGood) ||
1188 (ses->server->tcpStatus == CifsNew))) {
1189
1190 if (in_buf->Command == SMB_COM_TRANSACTION2) {
1191 /* POSIX lock. We send a NT_CANCEL SMB to cause the
1192 blocking lock to return. */
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001193 rc = send_cancel(ses->server, &rqst, midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001194 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001195 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001196 return rc;
1197 }
1198 } else {
1199 /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
1200 to cause the blocking lock to return. */
1201
1202 rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
1203
1204 /* If we get -ENOLCK back the lock may have
1205 already been removed. Don't exit in this case. */
1206 if (rc && rc != -ENOLCK) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001207 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001208 return rc;
1209 }
1210 }
1211
Jeff Layton1be912d2011-01-28 07:08:28 -05001212 rc = wait_for_response(ses->server, midQ);
1213 if (rc) {
Pavel Shilovskyfb2036d2016-11-23 15:08:14 -08001214 send_cancel(ses->server, &rqst, midQ);
Jeff Layton1be912d2011-01-28 07:08:28 -05001215 spin_lock(&GlobalMid_Lock);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001216 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
Jeff Layton1be912d2011-01-28 07:08:28 -05001217 /* no longer considered to be "in-flight" */
1218 midQ->callback = DeleteMidQEntry;
1219 spin_unlock(&GlobalMid_Lock);
1220 return rc;
1221 }
1222 spin_unlock(&GlobalMid_Lock);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001223 }
Jeff Layton1be912d2011-01-28 07:08:28 -05001224
1225 /* We got the response - restart system call. */
1226 rstart = 1;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001227 }
1228
Jeff Layton3c1105d2011-05-22 07:09:13 -04001229 rc = cifs_sync_mid_result(midQ, ses->server);
Jeff Layton053d5032011-01-11 07:24:02 -05001230 if (rc != 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001231 return rc;
Steve French50c2f752007-07-13 00:33:32 +00001232
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001233 /* rcvd frame is ok */
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001234 if (out_buf == NULL || midQ->mid_state != MID_RESPONSE_RECEIVED) {
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001235 rc = -EIO;
Joe Perchesf96637b2013-05-04 22:12:25 -05001236 cifs_dbg(VFS, "Bad MID state?\n");
Volker Lendecke698e96a2008-12-06 16:39:31 +01001237 goto out;
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001238 }
1239
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001240 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
Jeff Layton2c8f9812011-05-19 16:22:52 -04001241 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
1242 rc = cifs_check_receive(midQ, ses->server, 0);
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001243out:
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001244 cifs_delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001245 if (rstart && rc == -EACCES)
1246 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 return rc;
1248}