Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * fs/cifs/transport.c |
| 3 | * |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 4 | * Copyright (C) International Business Machines Corp., 2002,2008 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Author(s): Steve French (sfrench@us.ibm.com) |
Steve French | 14a441a2b | 2006-07-16 04:32:51 +0000 | [diff] [blame] | 6 | * Jeremy Allison (jra@samba.org) 2006. |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 7 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * 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 French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #include <linux/fs.h> |
| 24 | #include <linux/list.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/wait.h> |
| 27 | #include <linux/net.h> |
| 28 | #include <linux/delay.h> |
Jeff Layton | f06ac72 | 2011-10-19 15:30:40 -0400 | [diff] [blame] | 29 | #include <linux/freezer.h> |
Jeff Layton | b8eed28 | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 30 | #include <linux/tcp.h> |
Christoph Hellwig | 2f8b544 | 2016-11-01 07:40:13 -0600 | [diff] [blame] | 31 | #include <linux/bvec.h> |
Jeff Layton | 97bc00b | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 32 | #include <linux/highmem.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 33 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #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 Aptel | 8bd68c6 | 2018-02-16 19:19:29 +0100 | [diff] [blame] | 40 | #include "smb2proto.h" |
Long Li | 9762c2d | 2017-11-22 17:38:43 -0700 | [diff] [blame] | 41 | #include "smbdirect.h" |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 42 | |
Ronnie Sahlberg | 3cecf48 | 2017-11-21 15:08:07 +1100 | [diff] [blame] | 43 | /* Max number of iovectors we can use off the stack when sending requests. */ |
| 44 | #define CIFS_MAX_IOV_SIZE 8 |
| 45 | |
Pavel Shilovsky | 2dc7e1c | 2011-12-26 22:53:34 +0400 | [diff] [blame] | 46 | void |
| 47 | cifs_wake_up_task(struct mid_q_entry *mid) |
Jeff Layton | 2b84a36c | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 48 | { |
| 49 | wake_up_process(mid->callback_data); |
| 50 | } |
| 51 | |
Jeff Layton | a6827c1 | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 52 | struct mid_q_entry * |
Jeff Layton | 24b9b06 | 2008-12-01 07:09:34 -0500 | [diff] [blame] | 53 | AllocMidQEntry(const struct smb_hdr *smb_buffer, struct TCP_Server_Info *server) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | { |
| 55 | struct mid_q_entry *temp; |
| 56 | |
Jeff Layton | 24b9b06 | 2008-12-01 07:09:34 -0500 | [diff] [blame] | 57 | if (server == NULL) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 58 | cifs_dbg(VFS, "Null TCP session in AllocMidQEntry\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | return NULL; |
| 60 | } |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 61 | |
Pekka Enberg | 232087c | 2008-09-15 13:22:54 +0300 | [diff] [blame] | 62 | temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS); |
NeilBrown | a6f74e8 | 2017-04-10 12:08:53 +1000 | [diff] [blame] | 63 | memset(temp, 0, sizeof(struct mid_q_entry)); |
Lars Persson | 696e420 | 2018-06-25 14:05:25 +0200 | [diff] [blame] | 64 | kref_init(&temp->refcount); |
NeilBrown | a6f74e8 | 2017-04-10 12:08:53 +1000 | [diff] [blame] | 65 | 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 French | 1047abc | 2005-10-11 19:58:06 -0700 | [diff] [blame] | 69 | /* do_gettimeofday(&temp->when_sent);*/ /* easier to use jiffies */ |
NeilBrown | a6f74e8 | 2017-04-10 12:08:53 +1000 | [diff] [blame] | 70 | /* when mid allocated can be before when sent */ |
| 71 | temp->when_alloc = jiffies; |
| 72 | temp->server = server; |
Jeff Layton | 2b84a36c | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 73 | |
NeilBrown | a6f74e8 | 2017-04-10 12:08:53 +1000 | [diff] [blame] | 74 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | atomic_inc(&midCount); |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 82 | temp->mid_state = MID_REQUEST_ALLOCATED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | return temp; |
| 84 | } |
| 85 | |
Lars Persson | 696e420 | 2018-06-25 14:05:25 +0200 | [diff] [blame] | 86 | static 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 | |
| 94 | void 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 Layton | 766fdbb | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 101 | void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | DeleteMidQEntry(struct mid_q_entry *midEntry) |
| 103 | { |
Steve French | 1047abc | 2005-10-11 19:58:06 -0700 | [diff] [blame] | 104 | #ifdef CONFIG_CIFS_STATS2 |
Pavel Shilovsky | 2dc7e1c | 2011-12-26 22:53:34 +0400 | [diff] [blame] | 105 | __le16 command = midEntry->server->vals->lock_cmd; |
Steve French | 1047abc | 2005-10-11 19:58:06 -0700 | [diff] [blame] | 106 | unsigned long now; |
| 107 | #endif |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 108 | midEntry->mid_state = MID_FREE; |
Jeff Layton | 8097531 | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 109 | atomic_dec(&midCount); |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 110 | if (midEntry->large_buf) |
Steve French | b8643e1 | 2005-04-28 22:41:07 -0700 | [diff] [blame] | 111 | cifs_buf_release(midEntry->resp_buf); |
| 112 | else |
| 113 | cifs_small_buf_release(midEntry->resp_buf); |
Steve French | 1047abc | 2005-10-11 19:58:06 -0700 | [diff] [blame] | 114 | #ifdef CONFIG_CIFS_STATS2 |
| 115 | now = jiffies; |
Steve French | 00778e2 | 2018-09-18 14:05:18 -0500 | [diff] [blame] | 116 | /* |
| 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 French | 020eec5 | 2018-08-01 16:38:07 -0500 | [diff] [blame] | 128 | (midEntry->command != command)) { |
Steve French | f5942db | 2018-11-14 01:37:39 -0600 | [diff] [blame] | 129 | /* |
| 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 French | 468d677 | 2018-08-04 05:24:34 -0500 | [diff] [blame] | 134 | cifs_stats_inc(&midEntry->server->smb2slowcmd[le16_to_cpu(midEntry->command)]); |
| 135 | |
Steve French | 020eec5 | 2018-08-01 16:38:07 -0500 | [diff] [blame] | 136 | 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 Shevchenko | 0b456f0 | 2014-08-27 16:49:44 +0300 | [diff] [blame] | 140 | pr_debug(" CIFS slow rsp: cmd %d mid %llu", |
Steve French | 1047abc | 2005-10-11 19:58:06 -0700 | [diff] [blame] | 141 | midEntry->command, midEntry->mid); |
Rodrigo Freire | f80eaed | 2018-10-07 12:21:26 -0300 | [diff] [blame] | 142 | cifs_info(" A: 0x%lx S: 0x%lx R: 0x%lx\n", |
Steve French | 1047abc | 2005-10-11 19:58:06 -0700 | [diff] [blame] | 143 | now - midEntry->when_alloc, |
| 144 | now - midEntry->when_sent, |
| 145 | now - midEntry->when_received); |
| 146 | } |
| 147 | } |
| 148 | #endif |
Lars Persson | 696e420 | 2018-06-25 14:05:25 +0200 | [diff] [blame] | 149 | cifs_mid_q_entry_release(midEntry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 152 | void |
| 153 | cifs_delete_mid(struct mid_q_entry *mid) |
Jeff Layton | ddc8cf8 | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 154 | { |
| 155 | spin_lock(&GlobalMid_Lock); |
Ronnie Sahlberg | ddf83af | 2018-08-30 10:12:59 +1000 | [diff] [blame] | 156 | list_del_init(&mid->qhead); |
| 157 | mid->mid_flags |= MID_DELETED; |
Jeff Layton | ddc8cf8 | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 158 | spin_unlock(&GlobalMid_Lock); |
| 159 | |
| 160 | DeleteMidQEntry(mid); |
| 161 | } |
| 162 | |
Jeff Layton | 6f49f46 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 163 | /* |
| 164 | * smb_send_kvec - send an array of kvecs to the server |
| 165 | * @server: Server to send the data to |
Al Viro | 3ab3f2a | 2015-11-13 02:36:04 -0500 | [diff] [blame] | 166 | * @smb_msg: Message to send |
Jeff Layton | 6f49f46 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 167 | * @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 French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 172 | static int |
Al Viro | 3ab3f2a | 2015-11-13 02:36:04 -0500 | [diff] [blame] | 173 | smb_send_kvec(struct TCP_Server_Info *server, struct msghdr *smb_msg, |
| 174 | size_t *sent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | { |
| 176 | int rc = 0; |
Al Viro | 3ab3f2a | 2015-11-13 02:36:04 -0500 | [diff] [blame] | 177 | int retries = 0; |
Steve French | edf1ae4 | 2008-10-29 00:47:57 +0000 | [diff] [blame] | 178 | struct socket *ssocket = server->ssocket; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 179 | |
Jeff Layton | 6f49f46 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 180 | *sent = 0; |
| 181 | |
Al Viro | 3ab3f2a | 2015-11-13 02:36:04 -0500 | [diff] [blame] | 182 | 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 Layton | 0496e02 | 2008-12-30 12:39:16 -0500 | [diff] [blame] | 186 | if (server->noblocksnd) |
Al Viro | 3ab3f2a | 2015-11-13 02:36:04 -0500 | [diff] [blame] | 187 | smb_msg->msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL; |
Steve French | edf1ae4 | 2008-10-29 00:47:57 +0000 | [diff] [blame] | 188 | else |
Al Viro | 3ab3f2a | 2015-11-13 02:36:04 -0500 | [diff] [blame] | 189 | smb_msg->msg_flags = MSG_NOSIGNAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
Al Viro | 3ab3f2a | 2015-11-13 02:36:04 -0500 | [diff] [blame] | 191 | while (msg_data_left(smb_msg)) { |
Jeff Layton | 6f49f46 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 192 | /* |
| 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 Viro | 3ab3f2a | 2015-11-13 02:36:04 -0500 | [diff] [blame] | 210 | rc = sock_sendmsg(ssocket, smb_msg); |
Jeff Layton | ce6c44e | 2013-03-22 08:36:45 -0400 | [diff] [blame] | 211 | if (rc == -EAGAIN) { |
Al Viro | 3ab3f2a | 2015-11-13 02:36:04 -0500 | [diff] [blame] | 212 | retries++; |
| 213 | if (retries >= 14 || |
| 214 | (!server->noblocksnd && (retries > 2))) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 215 | cifs_dbg(VFS, "sends on sock %p stuck for 15 seconds\n", |
| 216 | ssocket); |
Al Viro | 3ab3f2a | 2015-11-13 02:36:04 -0500 | [diff] [blame] | 217 | return -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | } |
Al Viro | 3ab3f2a | 2015-11-13 02:36:04 -0500 | [diff] [blame] | 219 | msleep(1 << retries); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | continue; |
| 221 | } |
Jeff Layton | 6f49f46 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 222 | |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 223 | if (rc < 0) |
Al Viro | 3ab3f2a | 2015-11-13 02:36:04 -0500 | [diff] [blame] | 224 | return rc; |
Jeff Layton | 6f49f46 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 225 | |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 226 | if (rc == 0) { |
Steve French | 3e84469 | 2005-10-03 13:37:24 -0700 | [diff] [blame] | 227 | /* should never happen, letting socket clear before |
| 228 | retrying is our only obvious option here */ |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 229 | cifs_dbg(VFS, "tcp sent no data\n"); |
Steve French | 3e84469 | 2005-10-03 13:37:24 -0700 | [diff] [blame] | 230 | msleep(500); |
| 231 | continue; |
| 232 | } |
Jeff Layton | 6f49f46 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 233 | |
Al Viro | 3ab3f2a | 2015-11-13 02:36:04 -0500 | [diff] [blame] | 234 | /* send was at least partially successful */ |
| 235 | *sent += rc; |
| 236 | retries = 0; /* in case we get ENOSPC on the next send */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | } |
Al Viro | 3ab3f2a | 2015-11-13 02:36:04 -0500 | [diff] [blame] | 238 | return 0; |
Jeff Layton | 97bc00b | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Paulo Alcantara | 35e2cc1 | 2018-06-15 10:22:44 -0300 | [diff] [blame] | 241 | unsigned long |
Ronnie Sahlberg | 81f39f9 | 2018-06-28 10:47:14 +1000 | [diff] [blame] | 242 | smb_rqst_len(struct TCP_Server_Info *server, struct smb_rqst *rqst) |
Jeff Layton | a26054d | 2014-02-14 07:21:00 -0500 | [diff] [blame] | 243 | { |
| 244 | unsigned int i; |
Paulo Alcantara | 35e2cc1 | 2018-06-15 10:22:44 -0300 | [diff] [blame] | 245 | struct kvec *iov; |
| 246 | int nvec; |
Jeff Layton | a26054d | 2014-02-14 07:21:00 -0500 | [diff] [blame] | 247 | unsigned long buflen = 0; |
| 248 | |
Ronnie Sahlberg | 81f39f9 | 2018-06-28 10:47:14 +1000 | [diff] [blame] | 249 | if (server->vals->header_preamble_size == 0 && |
| 250 | rqst->rq_nvec >= 2 && rqst->rq_iov[0].iov_len == 4) { |
Paulo Alcantara | 35e2cc1 | 2018-06-15 10:22:44 -0300 | [diff] [blame] | 251 | 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 Layton | a26054d | 2014-02-14 07:21:00 -0500 | [diff] [blame] | 258 | /* total up iov array first */ |
Paulo Alcantara | 35e2cc1 | 2018-06-15 10:22:44 -0300 | [diff] [blame] | 259 | for (i = 0; i < nvec; i++) |
Jeff Layton | a26054d | 2014-02-14 07:21:00 -0500 | [diff] [blame] | 260 | buflen += iov[i].iov_len; |
| 261 | |
Long Li | c06a0f2 | 2018-05-30 12:47:57 -0700 | [diff] [blame] | 262 | /* |
| 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 Layton | a26054d | 2014-02-14 07:21:00 -0500 | [diff] [blame] | 268 | if (rqst->rq_npages) { |
Long Li | c06a0f2 | 2018-05-30 12:47:57 -0700 | [diff] [blame] | 269 | 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 Layton | a26054d | 2014-02-14 07:21:00 -0500 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | return buflen; |
| 283 | } |
| 284 | |
Jeff Layton | 6f49f46 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 285 | static int |
Ronnie Sahlberg | 07cd952 | 2018-06-12 08:01:00 +1000 | [diff] [blame] | 286 | __smb_send_rqst(struct TCP_Server_Info *server, int num_rqst, |
| 287 | struct smb_rqst *rqst) |
Jeff Layton | 6f49f46 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 288 | { |
Ronnie Sahlberg | 07cd952 | 2018-06-12 08:01:00 +1000 | [diff] [blame] | 289 | int rc = 0; |
| 290 | struct kvec *iov; |
| 291 | int n_vec; |
| 292 | unsigned int send_length = 0; |
| 293 | unsigned int i, j; |
Al Viro | 3ab3f2a | 2015-11-13 02:36:04 -0500 | [diff] [blame] | 294 | size_t total_len = 0, sent, size; |
Jeff Layton | b8eed28 | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 295 | struct socket *ssocket = server->ssocket; |
Al Viro | 3ab3f2a | 2015-11-13 02:36:04 -0500 | [diff] [blame] | 296 | struct msghdr smb_msg; |
Jeff Layton | b8eed28 | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 297 | int val = 1; |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 298 | __be32 rfc1002_marker; |
| 299 | |
Long Li | 9762c2d | 2017-11-22 17:38:43 -0700 | [diff] [blame] | 300 | if (cifs_rdma_enabled(server) && server->smbd_conn) { |
Ronnie Sahlberg | 81f39f9 | 2018-06-28 10:47:14 +1000 | [diff] [blame] | 301 | rc = smbd_send(server, rqst); |
Long Li | 9762c2d | 2017-11-22 17:38:43 -0700 | [diff] [blame] | 302 | goto smbd_done; |
| 303 | } |
Jeff Layton | ea702b8 | 2012-12-27 07:28:55 -0500 | [diff] [blame] | 304 | if (ssocket == NULL) |
| 305 | return -ENOTSOCK; |
| 306 | |
Jeff Layton | b8eed28 | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 307 | /* cork the socket */ |
| 308 | kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK, |
| 309 | (char *)&val, sizeof(val)); |
| 310 | |
Ronnie Sahlberg | 07cd952 | 2018-06-12 08:01:00 +1000 | [diff] [blame] | 311 | for (j = 0; j < num_rqst; j++) |
Ronnie Sahlberg | 81f39f9 | 2018-06-28 10:47:14 +1000 | [diff] [blame] | 312 | send_length += smb_rqst_len(server, &rqst[j]); |
Ronnie Sahlberg | 07cd952 | 2018-06-12 08:01:00 +1000 | [diff] [blame] | 313 | rfc1002_marker = cpu_to_be32(send_length); |
| 314 | |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 315 | /* 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 Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 321 | iov_iter_kvec(&smb_msg.msg_iter, WRITE, &hiov, 1, 4); |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 322 | 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 Alcantara | 662bf5b | 2018-06-14 17:34:08 -0300 | [diff] [blame] | 330 | cifs_dbg(FYI, "Sending smb: smb_len=%u\n", send_length); |
| 331 | |
Ronnie Sahlberg | 07cd952 | 2018-06-12 08:01:00 +1000 | [diff] [blame] | 332 | for (j = 0; j < num_rqst; j++) { |
| 333 | iov = rqst[j].rq_iov; |
| 334 | n_vec = rqst[j].rq_nvec; |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 335 | |
Ronnie Sahlberg | 07cd952 | 2018-06-12 08:01:00 +1000 | [diff] [blame] | 336 | size = 0; |
Paulo Alcantara | 662bf5b | 2018-06-14 17:34:08 -0300 | [diff] [blame] | 337 | for (i = 0; i < n_vec; i++) { |
| 338 | dump_smb(iov[i].iov_base, iov[i].iov_len); |
Ronnie Sahlberg | 07cd952 | 2018-06-12 08:01:00 +1000 | [diff] [blame] | 339 | size += iov[i].iov_len; |
Paulo Alcantara | 662bf5b | 2018-06-14 17:34:08 -0300 | [diff] [blame] | 340 | } |
Al Viro | 3ab3f2a | 2015-11-13 02:36:04 -0500 | [diff] [blame] | 341 | |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 342 | iov_iter_kvec(&smb_msg.msg_iter, WRITE, iov, n_vec, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | |
Al Viro | 3ab3f2a | 2015-11-13 02:36:04 -0500 | [diff] [blame] | 344 | rc = smb_send_kvec(server, &smb_msg, &sent); |
Jeff Layton | 97bc00b | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 345 | if (rc < 0) |
Ronnie Sahlberg | 07cd952 | 2018-06-12 08:01:00 +1000 | [diff] [blame] | 346 | goto uncork; |
Jeff Layton | 97bc00b | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 347 | |
| 348 | total_len += sent; |
Ronnie Sahlberg | 07cd952 | 2018-06-12 08:01:00 +1000 | [diff] [blame] | 349 | |
| 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 Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 358 | iov_iter_bvec(&smb_msg.msg_iter, WRITE, |
Ronnie Sahlberg | 07cd952 | 2018-06-12 08:01:00 +1000 | [diff] [blame] | 359 | &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 Layton | 97bc00b | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | uncork: |
Jeff Layton | b8eed28 | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 369 | /* uncork it */ |
| 370 | val = 0; |
| 371 | kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK, |
| 372 | (char *)&val, sizeof(val)); |
| 373 | |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 374 | if ((total_len > 0) && (total_len != send_length)) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 375 | cifs_dbg(FYI, "partial send (wanted=%u sent=%zu): terminating session\n", |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 376 | send_length, total_len); |
Jeff Layton | 6f49f46 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 377 | /* |
| 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 French | edf1ae4 | 2008-10-29 00:47:57 +0000 | [diff] [blame] | 382 | server->tcpStatus = CifsNeedReconnect; |
Steve French | bf1fdeb | 2018-07-30 19:23:09 -0500 | [diff] [blame] | 383 | trace_smb3_partial_send_reconnect(server->CurrentMid, |
| 384 | server->hostname); |
Steve French | edf1ae4 | 2008-10-29 00:47:57 +0000 | [diff] [blame] | 385 | } |
Long Li | 9762c2d | 2017-11-22 17:38:43 -0700 | [diff] [blame] | 386 | smbd_done: |
Jeff Layton | d804d41 | 2011-01-28 15:05:43 -0500 | [diff] [blame] | 387 | if (rc < 0 && rc != -EINTR) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 388 | cifs_dbg(VFS, "Error %d sending data on socket to server\n", |
| 389 | rc); |
Pavel Shilovsky | ee13919 | 2019-01-10 11:27:28 -0800 | [diff] [blame] | 390 | else if (rc > 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | |
| 393 | return rc; |
| 394 | } |
| 395 | |
Jeff Layton | 6f49f46 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 396 | static int |
Ronnie Sahlberg | 1f3a8f5 | 2018-08-01 09:26:12 +1000 | [diff] [blame] | 397 | smb_send_rqst(struct TCP_Server_Info *server, int num_rqst, |
| 398 | struct smb_rqst *rqst, int flags) |
Jeff Layton | 6f49f46 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 399 | { |
Ronnie Sahlberg | b2c96de | 2018-08-01 09:26:11 +1000 | [diff] [blame] | 400 | struct kvec iov; |
| 401 | struct smb2_transform_hdr tr_hdr; |
| 402 | struct smb_rqst cur_rqst[MAX_COMPOUND]; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 403 | int rc; |
Jeff Layton | 6f49f46 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 404 | |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 405 | if (!(flags & CIFS_TRANSFORM_REQ)) |
Ronnie Sahlberg | 1f3a8f5 | 2018-08-01 09:26:12 +1000 | [diff] [blame] | 406 | return __smb_send_rqst(server, num_rqst, rqst); |
| 407 | |
| 408 | if (num_rqst > MAX_COMPOUND - 1) |
| 409 | return -ENOMEM; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 410 | |
Ronnie Sahlberg | b2c96de | 2018-08-01 09:26:11 +1000 | [diff] [blame] | 411 | 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 Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 423 | return -EIO; |
| 424 | } |
| 425 | |
Ronnie Sahlberg | 1f3a8f5 | 2018-08-01 09:26:12 +1000 | [diff] [blame] | 426 | rc = server->ops->init_transform_rq(server, num_rqst + 1, |
| 427 | &cur_rqst[0], rqst); |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 428 | if (rc) |
| 429 | return rc; |
| 430 | |
Ronnie Sahlberg | 1f3a8f5 | 2018-08-01 09:26:12 +1000 | [diff] [blame] | 431 | rc = __smb_send_rqst(server, num_rqst + 1, &cur_rqst[0]); |
| 432 | smb3_free_compound_rqst(num_rqst, &cur_rqst[1]); |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 433 | return rc; |
Jeff Layton | 6f49f46 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Jeff Layton | 0496e02 | 2008-12-30 12:39:16 -0500 | [diff] [blame] | 436 | int |
| 437 | smb_send(struct TCP_Server_Info *server, struct smb_hdr *smb_buffer, |
| 438 | unsigned int smb_buf_length) |
| 439 | { |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 440 | struct kvec iov[2]; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 441 | struct smb_rqst rqst = { .rq_iov = iov, |
| 442 | .rq_nvec = 2 }; |
Jeff Layton | 0496e02 | 2008-12-30 12:39:16 -0500 | [diff] [blame] | 443 | |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 444 | 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 Layton | 0496e02 | 2008-12-30 12:39:16 -0500 | [diff] [blame] | 448 | |
Ronnie Sahlberg | 07cd952 | 2018-06-12 08:01:00 +1000 | [diff] [blame] | 449 | return __smb_send_rqst(server, 1, &rqst); |
Jeff Layton | 0496e02 | 2008-12-30 12:39:16 -0500 | [diff] [blame] | 450 | } |
| 451 | |
Pavel Shilovsky | fc40f9c | 2012-02-17 17:09:12 +0300 | [diff] [blame] | 452 | static int |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 453 | wait_for_free_credits(struct TCP_Server_Info *server, const int timeout, |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame^] | 454 | int *credits, unsigned int *instance) |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 455 | { |
Pavel Shilovsky | 5bc5949 | 2012-02-21 19:56:08 +0300 | [diff] [blame] | 456 | int rc; |
| 457 | |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame^] | 458 | *instance = 0; |
| 459 | |
Pavel Shilovsky | fc40f9c | 2012-02-17 17:09:12 +0300 | [diff] [blame] | 460 | spin_lock(&server->req_lock); |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 461 | if (timeout == CIFS_ASYNC_OP) { |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 462 | /* oplock breaks must not be held up */ |
Pavel Shilovsky | fc40f9c | 2012-02-17 17:09:12 +0300 | [diff] [blame] | 463 | server->in_flight++; |
Pavel Shilovsky | bc205ed | 2012-03-15 13:22:27 +0300 | [diff] [blame] | 464 | *credits -= 1; |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame^] | 465 | *instance = server->reconnect_instance; |
Pavel Shilovsky | fc40f9c | 2012-02-17 17:09:12 +0300 | [diff] [blame] | 466 | spin_unlock(&server->req_lock); |
Volker Lendecke | 27a97a6 | 2008-12-08 20:59:39 +0000 | [diff] [blame] | 467 | return 0; |
| 468 | } |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 469 | |
Volker Lendecke | 27a97a6 | 2008-12-08 20:59:39 +0000 | [diff] [blame] | 470 | while (1) { |
Pavel Shilovsky | bc205ed | 2012-03-15 13:22:27 +0300 | [diff] [blame] | 471 | if (*credits <= 0) { |
Pavel Shilovsky | fc40f9c | 2012-02-17 17:09:12 +0300 | [diff] [blame] | 472 | spin_unlock(&server->req_lock); |
Steve French | 789e666 | 2011-08-09 18:44:44 +0000 | [diff] [blame] | 473 | cifs_num_waiters_inc(server); |
Pavel Shilovsky | 5bc5949 | 2012-02-21 19:56:08 +0300 | [diff] [blame] | 474 | rc = wait_event_killable(server->request_q, |
Pavel Shilovsky | bc205ed | 2012-03-15 13:22:27 +0300 | [diff] [blame] | 475 | has_credits(server, credits)); |
Steve French | 789e666 | 2011-08-09 18:44:44 +0000 | [diff] [blame] | 476 | cifs_num_waiters_dec(server); |
Pavel Shilovsky | 5bc5949 | 2012-02-21 19:56:08 +0300 | [diff] [blame] | 477 | if (rc) |
| 478 | return rc; |
Pavel Shilovsky | fc40f9c | 2012-02-17 17:09:12 +0300 | [diff] [blame] | 479 | spin_lock(&server->req_lock); |
Volker Lendecke | 27a97a6 | 2008-12-08 20:59:39 +0000 | [diff] [blame] | 480 | } else { |
Jeff Layton | c5797a9 | 2011-01-11 07:24:01 -0500 | [diff] [blame] | 481 | if (server->tcpStatus == CifsExiting) { |
Pavel Shilovsky | fc40f9c | 2012-02-17 17:09:12 +0300 | [diff] [blame] | 482 | spin_unlock(&server->req_lock); |
Volker Lendecke | 27a97a6 | 2008-12-08 20:59:39 +0000 | [diff] [blame] | 483 | return -ENOENT; |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 484 | } |
Volker Lendecke | 27a97a6 | 2008-12-08 20:59:39 +0000 | [diff] [blame] | 485 | |
Pavel Shilovsky | 2d86dbc | 2012-02-06 15:59:18 +0400 | [diff] [blame] | 486 | /* |
| 487 | * Can not count locking commands against total |
| 488 | * as they are allowed to block on server. |
| 489 | */ |
Volker Lendecke | 27a97a6 | 2008-12-08 20:59:39 +0000 | [diff] [blame] | 490 | |
| 491 | /* update # of requests on the wire to server */ |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 492 | if (timeout != CIFS_BLOCKING_OP) { |
Pavel Shilovsky | bc205ed | 2012-03-15 13:22:27 +0300 | [diff] [blame] | 493 | *credits -= 1; |
Pavel Shilovsky | fc40f9c | 2012-02-17 17:09:12 +0300 | [diff] [blame] | 494 | server->in_flight++; |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame^] | 495 | *instance = server->reconnect_instance; |
Pavel Shilovsky | 2d86dbc | 2012-02-06 15:59:18 +0400 | [diff] [blame] | 496 | } |
Pavel Shilovsky | fc40f9c | 2012-02-17 17:09:12 +0300 | [diff] [blame] | 497 | spin_unlock(&server->req_lock); |
Volker Lendecke | 27a97a6 | 2008-12-08 20:59:39 +0000 | [diff] [blame] | 498 | break; |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 499 | } |
| 500 | } |
| 501 | return 0; |
| 502 | } |
| 503 | |
Pavel Shilovsky | bc205ed | 2012-03-15 13:22:27 +0300 | [diff] [blame] | 504 | static int |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 505 | wait_for_free_request(struct TCP_Server_Info *server, const int timeout, |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame^] | 506 | const int optype, unsigned int *instance) |
Pavel Shilovsky | bc205ed | 2012-03-15 13:22:27 +0300 | [diff] [blame] | 507 | { |
Shirish Pargaonkar | eb4c7df | 2013-10-03 05:44:45 -0500 | [diff] [blame] | 508 | int *val; |
| 509 | |
| 510 | val = server->ops->get_credits_field(server, optype); |
| 511 | /* Since an echo is already inflight, no need to wait to send another */ |
| 512 | if (*val <= 0 && optype == CIFS_ECHO_OP) |
| 513 | return -EAGAIN; |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame^] | 514 | return wait_for_free_credits(server, timeout, val, instance); |
Pavel Shilovsky | bc205ed | 2012-03-15 13:22:27 +0300 | [diff] [blame] | 515 | } |
| 516 | |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 517 | int |
| 518 | cifs_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size, |
Pavel Shilovsky | 335b7b6 | 2019-01-16 11:12:41 -0800 | [diff] [blame] | 519 | unsigned int *num, struct cifs_credits *credits) |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 520 | { |
| 521 | *num = size; |
Pavel Shilovsky | 335b7b6 | 2019-01-16 11:12:41 -0800 | [diff] [blame] | 522 | credits->value = 0; |
| 523 | credits->instance = server->reconnect_instance; |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 524 | return 0; |
| 525 | } |
| 526 | |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 527 | static int allocate_mid(struct cifs_ses *ses, struct smb_hdr *in_buf, |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 528 | struct mid_q_entry **ppmidQ) |
| 529 | { |
| 530 | if (ses->server->tcpStatus == CifsExiting) { |
| 531 | return -ENOENT; |
Volker Lendecke | 8fbbd36 | 2008-12-06 13:12:34 +0100 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | if (ses->server->tcpStatus == CifsNeedReconnect) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 535 | cifs_dbg(FYI, "tcp session dead - return to caller to retry\n"); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 536 | return -EAGAIN; |
Volker Lendecke | 8fbbd36 | 2008-12-06 13:12:34 +0100 | [diff] [blame] | 537 | } |
| 538 | |
Shirish Pargaonkar | 7f48558 | 2013-10-12 10:06:03 -0500 | [diff] [blame] | 539 | if (ses->status == CifsNew) { |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 540 | if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) && |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 541 | (in_buf->Command != SMB_COM_NEGOTIATE)) |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 542 | return -EAGAIN; |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 543 | /* else ok - we are setting up session */ |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 544 | } |
Shirish Pargaonkar | 7f48558 | 2013-10-12 10:06:03 -0500 | [diff] [blame] | 545 | |
| 546 | if (ses->status == CifsExiting) { |
| 547 | /* check if SMB session is bad because we are setting it up */ |
| 548 | if (in_buf->Command != SMB_COM_LOGOFF_ANDX) |
| 549 | return -EAGAIN; |
| 550 | /* else ok - we are shutting down session */ |
| 551 | } |
| 552 | |
Jeff Layton | 24b9b06 | 2008-12-01 07:09:34 -0500 | [diff] [blame] | 553 | *ppmidQ = AllocMidQEntry(in_buf, ses->server); |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 554 | if (*ppmidQ == NULL) |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 555 | return -ENOMEM; |
Jeff Layton | ddc8cf8 | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 556 | spin_lock(&GlobalMid_Lock); |
| 557 | list_add_tail(&(*ppmidQ)->qhead, &ses->server->pending_mid_q); |
| 558 | spin_unlock(&GlobalMid_Lock); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 559 | return 0; |
| 560 | } |
| 561 | |
Jeff Layton | 0ade640 | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 562 | static int |
| 563 | wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *midQ) |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 564 | { |
Jeff Layton | 0ade640 | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 565 | int error; |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 566 | |
Colin Cross | 5853cc2 | 2013-05-07 17:52:05 +0000 | [diff] [blame] | 567 | error = wait_event_freezekillable_unsafe(server->response_q, |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 568 | midQ->mid_state != MID_REQUEST_SUBMITTED); |
Jeff Layton | 0ade640 | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 569 | if (error < 0) |
| 570 | return -ERESTARTSYS; |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 571 | |
Jeff Layton | 0ade640 | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 572 | return 0; |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 573 | } |
| 574 | |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 575 | struct mid_q_entry * |
| 576 | cifs_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst) |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 577 | { |
| 578 | int rc; |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 579 | struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base; |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 580 | struct mid_q_entry *mid; |
| 581 | |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 582 | if (rqst->rq_iov[0].iov_len != 4 || |
| 583 | rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base) |
| 584 | return ERR_PTR(-EIO); |
| 585 | |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 586 | /* enable signing if server requires it */ |
Jeff Layton | 38d77c5 | 2013-05-26 07:01:00 -0400 | [diff] [blame] | 587 | if (server->sign) |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 588 | hdr->Flags2 |= SMBFLG2_SECURITY_SIGNATURE; |
| 589 | |
| 590 | mid = AllocMidQEntry(hdr, server); |
| 591 | if (mid == NULL) |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 592 | return ERR_PTR(-ENOMEM); |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 593 | |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 594 | rc = cifs_sign_rqst(rqst, server, &mid->sequence_number); |
Sachin Prabhu | ffc61cc | 2012-07-11 12:28:05 +0100 | [diff] [blame] | 595 | if (rc) { |
| 596 | DeleteMidQEntry(mid); |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 597 | return ERR_PTR(rc); |
Sachin Prabhu | ffc61cc | 2012-07-11 12:28:05 +0100 | [diff] [blame] | 598 | } |
| 599 | |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 600 | return mid; |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 601 | } |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 602 | |
| 603 | /* |
Jeff Layton | a6827c1 | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 604 | * Send a SMB request and set the callback function in the mid to handle |
| 605 | * the result. Caller is responsible for dealing with timeouts. |
| 606 | */ |
| 607 | int |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 608 | cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst, |
Pavel Shilovsky | 9b7c18a | 2016-11-16 14:06:17 -0800 | [diff] [blame] | 609 | mid_receive_t *receive, mid_callback_t *callback, |
| 610 | mid_handle_t *handle, void *cbdata, const int flags) |
Jeff Layton | a6827c1 | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 611 | { |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 612 | int rc, timeout, optype; |
Jeff Layton | a6827c1 | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 613 | struct mid_q_entry *mid; |
Pavel Shilovsky | 335b7b6 | 2019-01-16 11:12:41 -0800 | [diff] [blame] | 614 | struct cifs_credits credits = { .value = 0, .instance = 0 }; |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame^] | 615 | unsigned int instance; |
Jeff Layton | a6827c1 | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 616 | |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 617 | timeout = flags & CIFS_TIMEOUT_MASK; |
| 618 | optype = flags & CIFS_OP_MASK; |
| 619 | |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 620 | if ((flags & CIFS_HAS_CREDITS) == 0) { |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame^] | 621 | rc = wait_for_free_request(server, timeout, optype, &instance); |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 622 | if (rc) |
| 623 | return rc; |
Pavel Shilovsky | 335b7b6 | 2019-01-16 11:12:41 -0800 | [diff] [blame] | 624 | credits.value = 1; |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame^] | 625 | credits.instance = instance; |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 626 | } |
Jeff Layton | a6827c1 | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 627 | |
| 628 | mutex_lock(&server->srv_mutex); |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 629 | mid = server->ops->setup_async_request(server, rqst); |
| 630 | if (IS_ERR(mid)) { |
Jeff Layton | a6827c1 | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 631 | mutex_unlock(&server->srv_mutex); |
Pavel Shilovsky | 335b7b6 | 2019-01-16 11:12:41 -0800 | [diff] [blame] | 632 | add_credits_and_wake_if(server, &credits, optype); |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 633 | return PTR_ERR(mid); |
Jeff Layton | a6827c1 | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 634 | } |
| 635 | |
Jeff Layton | 44d22d8 | 2011-10-19 15:29:49 -0400 | [diff] [blame] | 636 | mid->receive = receive; |
Jeff Layton | a6827c1 | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 637 | mid->callback = callback; |
| 638 | mid->callback_data = cbdata; |
Pavel Shilovsky | 9b7c18a | 2016-11-16 14:06:17 -0800 | [diff] [blame] | 639 | mid->handle = handle; |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 640 | mid->mid_state = MID_REQUEST_SUBMITTED; |
Steve French | 789e666 | 2011-08-09 18:44:44 +0000 | [diff] [blame] | 641 | |
Sachin Prabhu | ffc61cc | 2012-07-11 12:28:05 +0100 | [diff] [blame] | 642 | /* put it on the pending_mid_q */ |
| 643 | spin_lock(&GlobalMid_Lock); |
| 644 | list_add_tail(&mid->qhead, &server->pending_mid_q); |
| 645 | spin_unlock(&GlobalMid_Lock); |
| 646 | |
Long Li | 93d2cb6 | 2017-06-28 15:55:55 -0700 | [diff] [blame] | 647 | /* |
| 648 | * Need to store the time in mid before calling I/O. For call_async, |
| 649 | * I/O response may come back and free the mid entry on another thread. |
| 650 | */ |
| 651 | cifs_save_when_sent(mid); |
Steve French | 789e666 | 2011-08-09 18:44:44 +0000 | [diff] [blame] | 652 | cifs_in_send_inc(server); |
Ronnie Sahlberg | 1f3a8f5 | 2018-08-01 09:26:12 +1000 | [diff] [blame] | 653 | rc = smb_send_rqst(server, 1, rqst, flags); |
Steve French | 789e666 | 2011-08-09 18:44:44 +0000 | [diff] [blame] | 654 | cifs_in_send_dec(server); |
Jeff Layton | ad313cb | 2013-04-03 10:27:36 -0400 | [diff] [blame] | 655 | |
Rabin Vincent | 820962d | 2015-12-23 07:32:41 +0100 | [diff] [blame] | 656 | if (rc < 0) { |
Pavel Shilovsky | c781af7 | 2019-03-04 14:02:50 -0800 | [diff] [blame] | 657 | revert_current_mid(server, mid->credits); |
Jeff Layton | ad313cb | 2013-04-03 10:27:36 -0400 | [diff] [blame] | 658 | server->sequence_number -= 2; |
Rabin Vincent | 820962d | 2015-12-23 07:32:41 +0100 | [diff] [blame] | 659 | cifs_delete_mid(mid); |
| 660 | } |
| 661 | |
Jeff Layton | a6827c1 | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 662 | mutex_unlock(&server->srv_mutex); |
Steve French | 789e666 | 2011-08-09 18:44:44 +0000 | [diff] [blame] | 663 | |
Sachin Prabhu | ffc61cc | 2012-07-11 12:28:05 +0100 | [diff] [blame] | 664 | if (rc == 0) |
| 665 | return 0; |
Jeff Layton | a6827c1 | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 666 | |
Pavel Shilovsky | 335b7b6 | 2019-01-16 11:12:41 -0800 | [diff] [blame] | 667 | add_credits_and_wake_if(server, &credits, optype); |
Jeff Layton | a6827c1 | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 668 | return rc; |
| 669 | } |
| 670 | |
| 671 | /* |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 672 | * |
| 673 | * Send an SMB Request. No response info (other than return code) |
| 674 | * needs to be parsed. |
| 675 | * |
| 676 | * flags indicate the type of request buffer and how long to wait |
| 677 | * and whether to log NT STATUS code (error) before mapping it to POSIX error |
| 678 | * |
| 679 | */ |
| 680 | int |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 681 | SendReceiveNoRsp(const unsigned int xid, struct cifs_ses *ses, |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 682 | char *in_buf, int flags) |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 683 | { |
| 684 | int rc; |
| 685 | struct kvec iov[1]; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 686 | struct kvec rsp_iov; |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 687 | int resp_buf_type; |
| 688 | |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 689 | iov[0].iov_base = in_buf; |
| 690 | iov[0].iov_len = get_rfc1002_length(in_buf) + 4; |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 691 | flags |= CIFS_NO_RESP; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 692 | rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 693 | cifs_dbg(NOISY, "SendRcvNoRsp flags %d rc %d\n", flags, rc); |
Steve French | 90c81e0 | 2008-02-12 20:32:36 +0000 | [diff] [blame] | 694 | |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 695 | return rc; |
| 696 | } |
| 697 | |
Jeff Layton | 053d503 | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 698 | static int |
Jeff Layton | 3c1105d | 2011-05-22 07:09:13 -0400 | [diff] [blame] | 699 | cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server) |
Jeff Layton | 053d503 | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 700 | { |
| 701 | int rc = 0; |
| 702 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 703 | cifs_dbg(FYI, "%s: cmd=%d mid=%llu state=%d\n", |
| 704 | __func__, le16_to_cpu(mid->command), mid->mid, mid->mid_state); |
Jeff Layton | 053d503 | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 705 | |
Jeff Layton | 74dd92a | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 706 | spin_lock(&GlobalMid_Lock); |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 707 | switch (mid->mid_state) { |
Jeff Layton | 74dd92a | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 708 | case MID_RESPONSE_RECEIVED: |
Jeff Layton | 053d503 | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 709 | spin_unlock(&GlobalMid_Lock); |
| 710 | return rc; |
Jeff Layton | 74dd92a | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 711 | case MID_RETRY_NEEDED: |
| 712 | rc = -EAGAIN; |
| 713 | break; |
Jeff Layton | 71823ba | 2011-02-10 08:03:50 -0500 | [diff] [blame] | 714 | case MID_RESPONSE_MALFORMED: |
| 715 | rc = -EIO; |
| 716 | break; |
Jeff Layton | 3c1105d | 2011-05-22 07:09:13 -0400 | [diff] [blame] | 717 | case MID_SHUTDOWN: |
| 718 | rc = -EHOSTDOWN; |
| 719 | break; |
Jeff Layton | 74dd92a | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 720 | default: |
Jeff Layton | 3c1105d | 2011-05-22 07:09:13 -0400 | [diff] [blame] | 721 | list_del_init(&mid->qhead); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 722 | cifs_dbg(VFS, "%s: invalid mid state mid=%llu state=%d\n", |
| 723 | __func__, mid->mid, mid->mid_state); |
Jeff Layton | 74dd92a | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 724 | rc = -EIO; |
Jeff Layton | 053d503 | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 725 | } |
| 726 | spin_unlock(&GlobalMid_Lock); |
| 727 | |
Jeff Layton | 2b84a36c | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 728 | DeleteMidQEntry(mid); |
Jeff Layton | 053d503 | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 729 | return rc; |
| 730 | } |
| 731 | |
Jeff Layton | 121b046 | 2012-05-15 12:21:10 -0400 | [diff] [blame] | 732 | static inline int |
Pavel Shilovsky | fb2036d | 2016-11-23 15:08:14 -0800 | [diff] [blame] | 733 | send_cancel(struct TCP_Server_Info *server, struct smb_rqst *rqst, |
| 734 | struct mid_q_entry *mid) |
Jeff Layton | 76dcc26 | 2011-01-11 07:24:24 -0500 | [diff] [blame] | 735 | { |
Jeff Layton | 121b046 | 2012-05-15 12:21:10 -0400 | [diff] [blame] | 736 | return server->ops->send_cancel ? |
Pavel Shilovsky | fb2036d | 2016-11-23 15:08:14 -0800 | [diff] [blame] | 737 | server->ops->send_cancel(server, rqst, mid) : 0; |
Jeff Layton | 76dcc26 | 2011-01-11 07:24:24 -0500 | [diff] [blame] | 738 | } |
| 739 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | int |
Jeff Layton | 2c8f981 | 2011-05-19 16:22:52 -0400 | [diff] [blame] | 741 | cifs_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server, |
| 742 | bool log_error) |
| 743 | { |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 744 | unsigned int len = get_rfc1002_length(mid->resp_buf) + 4; |
Jeff Layton | 826a95e | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 745 | |
| 746 | dump_smb(mid->resp_buf, min_t(u32, 92, len)); |
Jeff Layton | 2c8f981 | 2011-05-19 16:22:52 -0400 | [diff] [blame] | 747 | |
| 748 | /* convert the length into a more usable form */ |
Jeff Layton | 38d77c5 | 2013-05-26 07:01:00 -0400 | [diff] [blame] | 749 | if (server->sign) { |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 750 | struct kvec iov[2]; |
Steve French | 985e4ff0 | 2012-08-03 09:42:45 -0500 | [diff] [blame] | 751 | int rc = 0; |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 752 | struct smb_rqst rqst = { .rq_iov = iov, |
| 753 | .rq_nvec = 2 }; |
Jeff Layton | 826a95e | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 754 | |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 755 | iov[0].iov_base = mid->resp_buf; |
| 756 | iov[0].iov_len = 4; |
| 757 | iov[1].iov_base = (char *)mid->resp_buf + 4; |
| 758 | iov[1].iov_len = len - 4; |
Jeff Layton | 2c8f981 | 2011-05-19 16:22:52 -0400 | [diff] [blame] | 759 | /* FIXME: add code to kill session */ |
Jeff Layton | bf5ea0e | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 760 | rc = cifs_verify_signature(&rqst, server, |
Jeff Layton | 0124cc4 | 2013-04-03 11:55:03 -0400 | [diff] [blame] | 761 | mid->sequence_number); |
Steve French | 985e4ff0 | 2012-08-03 09:42:45 -0500 | [diff] [blame] | 762 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 763 | cifs_dbg(VFS, "SMB signature verification returned error = %d\n", |
| 764 | rc); |
Jeff Layton | 2c8f981 | 2011-05-19 16:22:52 -0400 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | /* BB special case reconnect tid and uid here? */ |
| 768 | return map_smb_to_linux_error(mid->resp_buf, log_error); |
| 769 | } |
| 770 | |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 771 | struct mid_q_entry * |
| 772 | cifs_setup_request(struct cifs_ses *ses, struct smb_rqst *rqst) |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 773 | { |
| 774 | int rc; |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 775 | struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base; |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 776 | struct mid_q_entry *mid; |
| 777 | |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 778 | if (rqst->rq_iov[0].iov_len != 4 || |
| 779 | rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base) |
| 780 | return ERR_PTR(-EIO); |
| 781 | |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 782 | rc = allocate_mid(ses, hdr, &mid); |
| 783 | if (rc) |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 784 | return ERR_PTR(rc); |
| 785 | rc = cifs_sign_rqst(rqst, ses->server, &mid->sequence_number); |
| 786 | if (rc) { |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 787 | cifs_delete_mid(mid); |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 788 | return ERR_PTR(rc); |
| 789 | } |
| 790 | return mid; |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 791 | } |
| 792 | |
Ronnie Sahlberg | 4e34feb | 2018-08-30 10:13:00 +1000 | [diff] [blame] | 793 | static void |
Pavel Shilovsky | ee258d7 | 2019-01-03 15:53:10 -0800 | [diff] [blame] | 794 | cifs_compound_callback(struct mid_q_entry *mid) |
Pavel Shilovsky | 8a26f0f | 2019-01-03 16:45:27 -0800 | [diff] [blame] | 795 | { |
| 796 | struct TCP_Server_Info *server = mid->server; |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame^] | 797 | struct cifs_credits credits; |
Pavel Shilovsky | 8a26f0f | 2019-01-03 16:45:27 -0800 | [diff] [blame] | 798 | |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame^] | 799 | credits.value = server->ops->get_credits(mid); |
| 800 | credits.instance = server->reconnect_instance; |
| 801 | |
| 802 | add_credits(server, &credits, mid->optype); |
Pavel Shilovsky | 8a26f0f | 2019-01-03 16:45:27 -0800 | [diff] [blame] | 803 | } |
| 804 | |
Pavel Shilovsky | ee258d7 | 2019-01-03 15:53:10 -0800 | [diff] [blame] | 805 | static void |
| 806 | cifs_compound_last_callback(struct mid_q_entry *mid) |
| 807 | { |
| 808 | cifs_compound_callback(mid); |
| 809 | cifs_wake_up_task(mid); |
| 810 | } |
| 811 | |
| 812 | static void |
| 813 | cifs_cancelled_callback(struct mid_q_entry *mid) |
| 814 | { |
| 815 | cifs_compound_callback(mid); |
| 816 | DeleteMidQEntry(mid); |
| 817 | } |
| 818 | |
Pavel Shilovsky | b8f57ee | 2016-11-23 15:31:54 -0800 | [diff] [blame] | 819 | int |
Ronnie Sahlberg | e0bba0b8 | 2018-08-01 09:26:13 +1000 | [diff] [blame] | 820 | compound_send_recv(const unsigned int xid, struct cifs_ses *ses, |
| 821 | const int flags, const int num_rqst, struct smb_rqst *rqst, |
| 822 | int *resp_buf_type, struct kvec *resp_iov) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | { |
Ronnie Sahlberg | e0bba0b8 | 2018-08-01 09:26:13 +1000 | [diff] [blame] | 824 | int i, j, rc = 0; |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 825 | int timeout, optype; |
Ronnie Sahlberg | e0bba0b8 | 2018-08-01 09:26:13 +1000 | [diff] [blame] | 826 | struct mid_q_entry *midQ[MAX_COMPOUND]; |
Pavel Shilovsky | 8544f4a | 2018-12-22 12:40:05 -0800 | [diff] [blame] | 827 | bool cancelled_mid[MAX_COMPOUND] = {false}; |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame^] | 828 | struct cifs_credits credits[MAX_COMPOUND] = { |
| 829 | { .value = 0, .instance = 0 } |
| 830 | }; |
| 831 | unsigned int instance; |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 832 | char *buf; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 833 | |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 834 | timeout = flags & CIFS_TIMEOUT_MASK; |
| 835 | optype = flags & CIFS_OP_MASK; |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 836 | |
Ronnie Sahlberg | e0bba0b8 | 2018-08-01 09:26:13 +1000 | [diff] [blame] | 837 | for (i = 0; i < num_rqst; i++) |
| 838 | resp_buf_type[i] = CIFS_NO_BUFFER; /* no response buf yet */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | |
Steve French | 4b8f930 | 2006-02-26 16:41:18 +0000 | [diff] [blame] | 840 | if ((ses == NULL) || (ses->server == NULL)) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 841 | cifs_dbg(VFS, "Null session\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | return -EIO; |
| 843 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 845 | if (ses->server->tcpStatus == CifsExiting) |
Steve French | 31ca3bc | 2005-04-28 22:41:11 -0700 | [diff] [blame] | 846 | return -ENOENT; |
| 847 | |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 848 | /* |
Pavel Shilovsky | 8544f4a | 2018-12-22 12:40:05 -0800 | [diff] [blame] | 849 | * Ensure we obtain 1 credit per request in the compound chain. |
| 850 | * It can be optimized further by waiting for all the credits |
| 851 | * at once but this can wait long enough if we don't have enough |
| 852 | * credits due to some heavy operations in progress or the server |
| 853 | * not granting us much, so a fallback to the current approach is |
| 854 | * needed anyway. |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 855 | */ |
Pavel Shilovsky | 8544f4a | 2018-12-22 12:40:05 -0800 | [diff] [blame] | 856 | for (i = 0; i < num_rqst; i++) { |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame^] | 857 | rc = wait_for_free_request(ses->server, timeout, optype, |
| 858 | &instance); |
Pavel Shilovsky | 8544f4a | 2018-12-22 12:40:05 -0800 | [diff] [blame] | 859 | if (rc) { |
| 860 | /* |
| 861 | * We haven't sent an SMB packet to the server yet but |
| 862 | * we already obtained credits for i requests in the |
| 863 | * compound chain - need to return those credits back |
| 864 | * for future use. Note that we need to call add_credits |
| 865 | * multiple times to match the way we obtained credits |
| 866 | * in the first place and to account for in flight |
| 867 | * requests correctly. |
| 868 | */ |
| 869 | for (j = 0; j < i; j++) |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame^] | 870 | add_credits(ses->server, &credits[j], optype); |
Pavel Shilovsky | 8544f4a | 2018-12-22 12:40:05 -0800 | [diff] [blame] | 871 | return rc; |
| 872 | } |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame^] | 873 | credits[i].value = 1; |
| 874 | credits[i].instance = instance; |
Pavel Shilovsky | 8544f4a | 2018-12-22 12:40:05 -0800 | [diff] [blame] | 875 | } |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 876 | |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 877 | /* |
| 878 | * Make sure that we sign in the same order that we send on this socket |
| 879 | * and avoid races inside tcp sendmsg code that could cause corruption |
| 880 | * of smb data. |
| 881 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | |
Jeff Layton | 72ca545 | 2008-12-01 07:09:36 -0500 | [diff] [blame] | 883 | mutex_lock(&ses->server->srv_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | |
Ronnie Sahlberg | e0bba0b8 | 2018-08-01 09:26:13 +1000 | [diff] [blame] | 885 | for (i = 0; i < num_rqst; i++) { |
| 886 | midQ[i] = ses->server->ops->setup_request(ses, &rqst[i]); |
| 887 | if (IS_ERR(midQ[i])) { |
Pavel Shilovsky | c781af7 | 2019-03-04 14:02:50 -0800 | [diff] [blame] | 888 | revert_current_mid(ses->server, i); |
Ronnie Sahlberg | e0bba0b8 | 2018-08-01 09:26:13 +1000 | [diff] [blame] | 889 | for (j = 0; j < i; j++) |
| 890 | cifs_delete_mid(midQ[j]); |
| 891 | mutex_unlock(&ses->server->srv_mutex); |
Pavel Shilovsky | 8544f4a | 2018-12-22 12:40:05 -0800 | [diff] [blame] | 892 | |
Ronnie Sahlberg | e0bba0b8 | 2018-08-01 09:26:13 +1000 | [diff] [blame] | 893 | /* Update # of requests on wire to server */ |
Pavel Shilovsky | 8544f4a | 2018-12-22 12:40:05 -0800 | [diff] [blame] | 894 | for (j = 0; j < num_rqst; j++) |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame^] | 895 | add_credits(ses->server, &credits[j], optype); |
Ronnie Sahlberg | e0bba0b8 | 2018-08-01 09:26:13 +1000 | [diff] [blame] | 896 | return PTR_ERR(midQ[i]); |
| 897 | } |
| 898 | |
| 899 | midQ[i]->mid_state = MID_REQUEST_SUBMITTED; |
Pavel Shilovsky | 8a26f0f | 2019-01-03 16:45:27 -0800 | [diff] [blame] | 900 | midQ[i]->optype = optype; |
Ronnie Sahlberg | 4e34feb | 2018-08-30 10:13:00 +1000 | [diff] [blame] | 901 | /* |
Pavel Shilovsky | ee258d7 | 2019-01-03 15:53:10 -0800 | [diff] [blame] | 902 | * Invoke callback for every part of the compound chain |
| 903 | * to calculate credits properly. Wake up this thread only when |
| 904 | * the last element is received. |
Ronnie Sahlberg | 4e34feb | 2018-08-30 10:13:00 +1000 | [diff] [blame] | 905 | */ |
| 906 | if (i < num_rqst - 1) |
Pavel Shilovsky | ee258d7 | 2019-01-03 15:53:10 -0800 | [diff] [blame] | 907 | midQ[i]->callback = cifs_compound_callback; |
| 908 | else |
| 909 | midQ[i]->callback = cifs_compound_last_callback; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | } |
Steve French | 789e666 | 2011-08-09 18:44:44 +0000 | [diff] [blame] | 911 | cifs_in_send_inc(ses->server); |
Ronnie Sahlberg | e0bba0b8 | 2018-08-01 09:26:13 +1000 | [diff] [blame] | 912 | rc = smb_send_rqst(ses->server, num_rqst, rqst, flags); |
Steve French | 789e666 | 2011-08-09 18:44:44 +0000 | [diff] [blame] | 913 | cifs_in_send_dec(ses->server); |
Ronnie Sahlberg | e0bba0b8 | 2018-08-01 09:26:13 +1000 | [diff] [blame] | 914 | |
| 915 | for (i = 0; i < num_rqst; i++) |
| 916 | cifs_save_when_sent(midQ[i]); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 917 | |
Pavel Shilovsky | c781af7 | 2019-03-04 14:02:50 -0800 | [diff] [blame] | 918 | if (rc < 0) { |
| 919 | revert_current_mid(ses->server, num_rqst); |
Jeff Layton | ad313cb | 2013-04-03 10:27:36 -0400 | [diff] [blame] | 920 | ses->server->sequence_number -= 2; |
Pavel Shilovsky | c781af7 | 2019-03-04 14:02:50 -0800 | [diff] [blame] | 921 | } |
Ronnie Sahlberg | e0bba0b8 | 2018-08-01 09:26:13 +1000 | [diff] [blame] | 922 | |
Jeff Layton | 72ca545 | 2008-12-01 07:09:36 -0500 | [diff] [blame] | 923 | mutex_unlock(&ses->server->srv_mutex); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 924 | |
Pavel Shilovsky | ee258d7 | 2019-01-03 15:53:10 -0800 | [diff] [blame] | 925 | if (rc < 0) { |
| 926 | /* Sending failed for some reason - return credits back */ |
| 927 | for (i = 0; i < num_rqst; i++) |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame^] | 928 | add_credits(ses->server, &credits[i], optype); |
Ronnie Sahlberg | cb5c2e6 | 2018-10-10 15:29:06 +1000 | [diff] [blame] | 929 | goto out; |
Pavel Shilovsky | ee258d7 | 2019-01-03 15:53:10 -0800 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | /* |
| 933 | * At this point the request is passed to the network stack - we assume |
| 934 | * that any credits taken from the server structure on the client have |
| 935 | * been spent and we can't return them back. Once we receive responses |
| 936 | * we will collect credits granted by the server in the mid callbacks |
| 937 | * and add those credits to the server structure. |
| 938 | */ |
Ronnie Sahlberg | cb5c2e6 | 2018-10-10 15:29:06 +1000 | [diff] [blame] | 939 | |
| 940 | /* |
| 941 | * Compounding is never used during session establish. |
| 942 | */ |
| 943 | if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP)) |
| 944 | smb311_update_preauth_hash(ses, rqst[0].rq_iov, |
| 945 | rqst[0].rq_nvec); |
| 946 | |
| 947 | if (timeout == CIFS_ASYNC_OP) |
| 948 | goto out; |
| 949 | |
Ronnie Sahlberg | e0bba0b8 | 2018-08-01 09:26:13 +1000 | [diff] [blame] | 950 | for (i = 0; i < num_rqst; i++) { |
Ronnie Sahlberg | e0bba0b8 | 2018-08-01 09:26:13 +1000 | [diff] [blame] | 951 | rc = wait_for_response(ses->server, midQ[i]); |
Pavel Shilovsky | 8a26f0f | 2019-01-03 16:45:27 -0800 | [diff] [blame] | 952 | if (rc != 0) |
| 953 | break; |
| 954 | } |
| 955 | if (rc != 0) { |
| 956 | for (; i < num_rqst; i++) { |
Steve French | 43de1db | 2018-10-23 21:04:57 -0500 | [diff] [blame] | 957 | cifs_dbg(VFS, "Cancelling wait for mid %llu cmd: %d\n", |
| 958 | midQ[i]->mid, le16_to_cpu(midQ[i]->command)); |
Ronnie Sahlberg | e0bba0b8 | 2018-08-01 09:26:13 +1000 | [diff] [blame] | 959 | send_cancel(ses->server, &rqst[i], midQ[i]); |
| 960 | spin_lock(&GlobalMid_Lock); |
| 961 | if (midQ[i]->mid_state == MID_REQUEST_SUBMITTED) { |
| 962 | midQ[i]->mid_flags |= MID_WAIT_CANCELLED; |
Pavel Shilovsky | 8a26f0f | 2019-01-03 16:45:27 -0800 | [diff] [blame] | 963 | midQ[i]->callback = cifs_cancelled_callback; |
Pavel Shilovsky | 8544f4a | 2018-12-22 12:40:05 -0800 | [diff] [blame] | 964 | cancelled_mid[i] = true; |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame^] | 965 | credits[i].value = 0; |
Ronnie Sahlberg | e0bba0b8 | 2018-08-01 09:26:13 +1000 | [diff] [blame] | 966 | } |
Jeff Layton | 1be912d | 2011-01-28 07:08:28 -0500 | [diff] [blame] | 967 | spin_unlock(&GlobalMid_Lock); |
Ronnie Sahlberg | e0bba0b8 | 2018-08-01 09:26:13 +1000 | [diff] [blame] | 968 | } |
Ronnie Sahlberg | cb5c2e6 | 2018-10-10 15:29:06 +1000 | [diff] [blame] | 969 | } |
| 970 | |
Ronnie Sahlberg | cb5c2e6 | 2018-10-10 15:29:06 +1000 | [diff] [blame] | 971 | for (i = 0; i < num_rqst; i++) { |
| 972 | if (rc < 0) |
| 973 | goto out; |
Ronnie Sahlberg | e0bba0b8 | 2018-08-01 09:26:13 +1000 | [diff] [blame] | 974 | |
| 975 | rc = cifs_sync_mid_result(midQ[i], ses->server); |
| 976 | if (rc != 0) { |
Pavel Shilovsky | 8544f4a | 2018-12-22 12:40:05 -0800 | [diff] [blame] | 977 | /* mark this mid as cancelled to not free it below */ |
| 978 | cancelled_mid[i] = true; |
| 979 | goto out; |
Jeff Layton | 1be912d | 2011-01-28 07:08:28 -0500 | [diff] [blame] | 980 | } |
Ronnie Sahlberg | e0bba0b8 | 2018-08-01 09:26:13 +1000 | [diff] [blame] | 981 | |
| 982 | if (!midQ[i]->resp_buf || |
| 983 | midQ[i]->mid_state != MID_RESPONSE_RECEIVED) { |
| 984 | rc = -EIO; |
| 985 | cifs_dbg(FYI, "Bad MID state?\n"); |
| 986 | goto out; |
| 987 | } |
| 988 | |
| 989 | buf = (char *)midQ[i]->resp_buf; |
| 990 | resp_iov[i].iov_base = buf; |
| 991 | resp_iov[i].iov_len = midQ[i]->resp_buf_size + |
| 992 | ses->server->vals->header_preamble_size; |
| 993 | |
| 994 | if (midQ[i]->large_buf) |
| 995 | resp_buf_type[i] = CIFS_LARGE_BUFFER; |
| 996 | else |
| 997 | resp_buf_type[i] = CIFS_SMALL_BUFFER; |
| 998 | |
Ronnie Sahlberg | e0bba0b8 | 2018-08-01 09:26:13 +1000 | [diff] [blame] | 999 | rc = ses->server->ops->check_receive(midQ[i], ses->server, |
| 1000 | flags & CIFS_LOG_ERROR); |
| 1001 | |
| 1002 | /* mark it so buf will not be freed by cifs_delete_mid */ |
| 1003 | if ((flags & CIFS_NO_RESP) == 0) |
| 1004 | midQ[i]->resp_buf = NULL; |
Ronnie Sahlberg | cb5c2e6 | 2018-10-10 15:29:06 +1000 | [diff] [blame] | 1005 | |
Jeff Layton | 1be912d | 2011-01-28 07:08:28 -0500 | [diff] [blame] | 1006 | } |
Ronnie Sahlberg | cb5c2e6 | 2018-10-10 15:29:06 +1000 | [diff] [blame] | 1007 | |
| 1008 | /* |
| 1009 | * Compounding is never used during session establish. |
| 1010 | */ |
| 1011 | if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP)) { |
| 1012 | struct kvec iov = { |
| 1013 | .iov_base = resp_iov[0].iov_base, |
| 1014 | .iov_len = resp_iov[0].iov_len |
| 1015 | }; |
| 1016 | smb311_update_preauth_hash(ses, &iov, 1); |
| 1017 | } |
| 1018 | |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1019 | out: |
Ronnie Sahlberg | 4e34feb | 2018-08-30 10:13:00 +1000 | [diff] [blame] | 1020 | /* |
| 1021 | * This will dequeue all mids. After this it is important that the |
| 1022 | * demultiplex_thread will not process any of these mids any futher. |
| 1023 | * This is prevented above by using a noop callback that will not |
| 1024 | * wake this thread except for the very last PDU. |
| 1025 | */ |
Pavel Shilovsky | 8544f4a | 2018-12-22 12:40:05 -0800 | [diff] [blame] | 1026 | for (i = 0; i < num_rqst; i++) { |
| 1027 | if (!cancelled_mid[i]) |
| 1028 | cifs_delete_mid(midQ[i]); |
Pavel Shilovsky | 8544f4a | 2018-12-22 12:40:05 -0800 | [diff] [blame] | 1029 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | |
| 1031 | return rc; |
| 1032 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | |
| 1034 | int |
Ronnie Sahlberg | e0bba0b8 | 2018-08-01 09:26:13 +1000 | [diff] [blame] | 1035 | cifs_send_recv(const unsigned int xid, struct cifs_ses *ses, |
| 1036 | struct smb_rqst *rqst, int *resp_buf_type, const int flags, |
| 1037 | struct kvec *resp_iov) |
| 1038 | { |
| 1039 | return compound_send_recv(xid, ses, flags, 1, rqst, resp_buf_type, |
| 1040 | resp_iov); |
| 1041 | } |
| 1042 | |
| 1043 | int |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 1044 | SendReceive2(const unsigned int xid, struct cifs_ses *ses, |
| 1045 | struct kvec *iov, int n_vec, int *resp_buf_type /* ret */, |
| 1046 | const int flags, struct kvec *resp_iov) |
| 1047 | { |
| 1048 | struct smb_rqst rqst; |
Ronnie Sahlberg | 3cecf48 | 2017-11-21 15:08:07 +1100 | [diff] [blame] | 1049 | struct kvec s_iov[CIFS_MAX_IOV_SIZE], *new_iov; |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 1050 | int rc; |
| 1051 | |
Ronnie Sahlberg | 3cecf48 | 2017-11-21 15:08:07 +1100 | [diff] [blame] | 1052 | if (n_vec + 1 > CIFS_MAX_IOV_SIZE) { |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 1053 | new_iov = kmalloc_array(n_vec + 1, sizeof(struct kvec), |
| 1054 | GFP_KERNEL); |
Steve French | 117e3b7 | 2018-04-22 10:24:19 -0500 | [diff] [blame] | 1055 | if (!new_iov) { |
| 1056 | /* otherwise cifs_send_recv below sets resp_buf_type */ |
| 1057 | *resp_buf_type = CIFS_NO_BUFFER; |
Ronnie Sahlberg | 3cecf48 | 2017-11-21 15:08:07 +1100 | [diff] [blame] | 1058 | return -ENOMEM; |
Steve French | 117e3b7 | 2018-04-22 10:24:19 -0500 | [diff] [blame] | 1059 | } |
Ronnie Sahlberg | 3cecf48 | 2017-11-21 15:08:07 +1100 | [diff] [blame] | 1060 | } else |
| 1061 | new_iov = s_iov; |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 1062 | |
| 1063 | /* 1st iov is a RFC1001 length followed by the rest of the packet */ |
| 1064 | memcpy(new_iov + 1, iov, (sizeof(struct kvec) * n_vec)); |
| 1065 | |
| 1066 | new_iov[0].iov_base = new_iov[1].iov_base; |
| 1067 | new_iov[0].iov_len = 4; |
| 1068 | new_iov[1].iov_base += 4; |
| 1069 | new_iov[1].iov_len -= 4; |
| 1070 | |
| 1071 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 1072 | rqst.rq_iov = new_iov; |
| 1073 | rqst.rq_nvec = n_vec + 1; |
| 1074 | |
| 1075 | rc = cifs_send_recv(xid, ses, &rqst, resp_buf_type, flags, resp_iov); |
Ronnie Sahlberg | 3cecf48 | 2017-11-21 15:08:07 +1100 | [diff] [blame] | 1076 | if (n_vec + 1 > CIFS_MAX_IOV_SIZE) |
| 1077 | kfree(new_iov); |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 1078 | return rc; |
| 1079 | } |
| 1080 | |
| 1081 | int |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 1082 | SendReceive(const unsigned int xid, struct cifs_ses *ses, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | struct smb_hdr *in_buf, struct smb_hdr *out_buf, |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 1084 | int *pbytes_returned, const int timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | { |
| 1086 | int rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | struct mid_q_entry *midQ; |
Pavel Shilovsky | fb2036d | 2016-11-23 15:08:14 -0800 | [diff] [blame] | 1088 | unsigned int len = be32_to_cpu(in_buf->smb_buf_length); |
| 1089 | struct kvec iov = { .iov_base = in_buf, .iov_len = len }; |
| 1090 | struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 }; |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame^] | 1091 | struct cifs_credits credits = { .value = 1, .instance = 0 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | |
| 1093 | if (ses == NULL) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1094 | cifs_dbg(VFS, "Null smb session\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | return -EIO; |
| 1096 | } |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 1097 | if (ses->server == NULL) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1098 | cifs_dbg(VFS, "Null tcp session\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | return -EIO; |
| 1100 | } |
| 1101 | |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 1102 | if (ses->server->tcpStatus == CifsExiting) |
Steve French | 31ca3bc | 2005-04-28 22:41:11 -0700 | [diff] [blame] | 1103 | return -ENOENT; |
| 1104 | |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 1105 | /* Ensure that we do not send more than 50 overlapping requests |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | to the same server. We may make this configurable later or |
| 1107 | use ses->maxReq */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | |
Pavel Shilovsky | fb2036d | 2016-11-23 15:08:14 -0800 | [diff] [blame] | 1109 | if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1110 | cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n", |
Pavel Shilovsky | fb2036d | 2016-11-23 15:08:14 -0800 | [diff] [blame] | 1111 | len); |
Volker Lendecke | 6d9c6d5 | 2008-12-08 20:50:24 +0000 | [diff] [blame] | 1112 | return -EIO; |
| 1113 | } |
| 1114 | |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame^] | 1115 | rc = wait_for_free_request(ses->server, timeout, 0, &credits.instance); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1116 | if (rc) |
| 1117 | return rc; |
| 1118 | |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 1119 | /* make sure that we sign in the same order that we send on this socket |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | and avoid races inside tcp sendmsg code that could cause corruption |
| 1121 | of smb data */ |
| 1122 | |
Jeff Layton | 72ca545 | 2008-12-01 07:09:36 -0500 | [diff] [blame] | 1123 | mutex_lock(&ses->server->srv_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1125 | rc = allocate_mid(ses, in_buf, &midQ); |
| 1126 | if (rc) { |
Jeff Layton | 72ca545 | 2008-12-01 07:09:36 -0500 | [diff] [blame] | 1127 | mutex_unlock(&ses->server->srv_mutex); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1128 | /* Update # of requests on wire to server */ |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame^] | 1129 | add_credits(ses->server, &credits, 0); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1130 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | } |
| 1132 | |
Steve French | ad009ac | 2005-04-28 22:41:05 -0700 | [diff] [blame] | 1133 | rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number); |
Volker Lendecke | 829049c | 2008-12-06 16:00:53 +0100 | [diff] [blame] | 1134 | if (rc) { |
| 1135 | mutex_unlock(&ses->server->srv_mutex); |
| 1136 | goto out; |
| 1137 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 1139 | midQ->mid_state = MID_REQUEST_SUBMITTED; |
Steve French | 789e666 | 2011-08-09 18:44:44 +0000 | [diff] [blame] | 1140 | |
| 1141 | cifs_in_send_inc(ses->server); |
Pavel Shilovsky | fb2036d | 2016-11-23 15:08:14 -0800 | [diff] [blame] | 1142 | rc = smb_send(ses->server, in_buf, len); |
Steve French | 789e666 | 2011-08-09 18:44:44 +0000 | [diff] [blame] | 1143 | cifs_in_send_dec(ses->server); |
| 1144 | cifs_save_when_sent(midQ); |
Jeff Layton | ad313cb | 2013-04-03 10:27:36 -0400 | [diff] [blame] | 1145 | |
| 1146 | if (rc < 0) |
| 1147 | ses->server->sequence_number -= 2; |
| 1148 | |
Jeff Layton | 72ca545 | 2008-12-01 07:09:36 -0500 | [diff] [blame] | 1149 | mutex_unlock(&ses->server->srv_mutex); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1150 | |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 1151 | if (rc < 0) |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1152 | goto out; |
| 1153 | |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 1154 | if (timeout == CIFS_ASYNC_OP) |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 1155 | goto out; |
Jeff Layton | 0ade640 | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 1156 | |
| 1157 | rc = wait_for_response(ses->server, midQ); |
Jeff Layton | 1be912d | 2011-01-28 07:08:28 -0500 | [diff] [blame] | 1158 | if (rc != 0) { |
Pavel Shilovsky | fb2036d | 2016-11-23 15:08:14 -0800 | [diff] [blame] | 1159 | send_cancel(ses->server, &rqst, midQ); |
Jeff Layton | 1be912d | 2011-01-28 07:08:28 -0500 | [diff] [blame] | 1160 | spin_lock(&GlobalMid_Lock); |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 1161 | if (midQ->mid_state == MID_REQUEST_SUBMITTED) { |
Jeff Layton | 1be912d | 2011-01-28 07:08:28 -0500 | [diff] [blame] | 1162 | /* no longer considered to be "in-flight" */ |
| 1163 | midQ->callback = DeleteMidQEntry; |
| 1164 | spin_unlock(&GlobalMid_Lock); |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame^] | 1165 | add_credits(ses->server, &credits, 0); |
Jeff Layton | 1be912d | 2011-01-28 07:08:28 -0500 | [diff] [blame] | 1166 | return rc; |
| 1167 | } |
| 1168 | spin_unlock(&GlobalMid_Lock); |
| 1169 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | |
Jeff Layton | 3c1105d | 2011-05-22 07:09:13 -0400 | [diff] [blame] | 1171 | rc = cifs_sync_mid_result(midQ, ses->server); |
Jeff Layton | 053d503 | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 1172 | if (rc != 0) { |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame^] | 1173 | add_credits(ses->server, &credits, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | return rc; |
| 1175 | } |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1176 | |
Jeff Layton | 2c8f981 | 2011-05-19 16:22:52 -0400 | [diff] [blame] | 1177 | if (!midQ->resp_buf || !out_buf || |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 1178 | midQ->mid_state != MID_RESPONSE_RECEIVED) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | rc = -EIO; |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1180 | cifs_dbg(VFS, "Bad MID state?\n"); |
Steve French | 2b2bdfb | 2008-12-11 17:26:54 +0000 | [diff] [blame] | 1181 | goto out; |
| 1182 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | |
Pavel Shilovsky | d4e4854 | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 1184 | *pbytes_returned = get_rfc1002_length(midQ->resp_buf); |
Jeff Layton | 2c8f981 | 2011-05-19 16:22:52 -0400 | [diff] [blame] | 1185 | memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4); |
| 1186 | rc = cifs_check_receive(midQ, ses->server, 0); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1187 | out: |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1188 | cifs_delete_mid(midQ); |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame^] | 1189 | add_credits(ses->server, &credits, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | |
| 1191 | return rc; |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1192 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1194 | /* We send a LOCKINGX_CANCEL_LOCK to cause the Windows |
| 1195 | blocking lock to return. */ |
| 1196 | |
| 1197 | static int |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 1198 | send_lock_cancel(const unsigned int xid, struct cifs_tcon *tcon, |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1199 | struct smb_hdr *in_buf, |
| 1200 | struct smb_hdr *out_buf) |
| 1201 | { |
| 1202 | int bytes_returned; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 1203 | struct cifs_ses *ses = tcon->ses; |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1204 | LOCK_REQ *pSMB = (LOCK_REQ *)in_buf; |
| 1205 | |
| 1206 | /* We just modify the current in_buf to change |
| 1207 | the type of lock from LOCKING_ANDX_SHARED_LOCK |
| 1208 | or LOCKING_ANDX_EXCLUSIVE_LOCK to |
| 1209 | LOCKING_ANDX_CANCEL_LOCK. */ |
| 1210 | |
| 1211 | pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES; |
| 1212 | pSMB->Timeout = 0; |
Pavel Shilovsky | 8825736 | 2012-05-23 14:01:59 +0400 | [diff] [blame] | 1213 | pSMB->hdr.Mid = get_next_mid(ses->server); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1214 | |
| 1215 | return SendReceive(xid, ses, in_buf, out_buf, |
Jeff Layton | 7749981 | 2011-01-11 07:24:23 -0500 | [diff] [blame] | 1216 | &bytes_returned, 0); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | int |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 1220 | SendReceiveBlockingLock(const unsigned int xid, struct cifs_tcon *tcon, |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1221 | struct smb_hdr *in_buf, struct smb_hdr *out_buf, |
| 1222 | int *pbytes_returned) |
| 1223 | { |
| 1224 | int rc = 0; |
| 1225 | int rstart = 0; |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1226 | struct mid_q_entry *midQ; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 1227 | struct cifs_ses *ses; |
Pavel Shilovsky | fb2036d | 2016-11-23 15:08:14 -0800 | [diff] [blame] | 1228 | unsigned int len = be32_to_cpu(in_buf->smb_buf_length); |
| 1229 | struct kvec iov = { .iov_base = in_buf, .iov_len = len }; |
| 1230 | struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 }; |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame^] | 1231 | unsigned int instance; |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1232 | |
| 1233 | if (tcon == NULL || tcon->ses == NULL) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1234 | cifs_dbg(VFS, "Null smb session\n"); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1235 | return -EIO; |
| 1236 | } |
| 1237 | ses = tcon->ses; |
| 1238 | |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 1239 | if (ses->server == NULL) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1240 | cifs_dbg(VFS, "Null tcp session\n"); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1241 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | } |
| 1243 | |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 1244 | if (ses->server->tcpStatus == CifsExiting) |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1245 | return -ENOENT; |
| 1246 | |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 1247 | /* Ensure that we do not send more than 50 overlapping requests |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1248 | to the same server. We may make this configurable later or |
| 1249 | use ses->maxReq */ |
| 1250 | |
Pavel Shilovsky | fb2036d | 2016-11-23 15:08:14 -0800 | [diff] [blame] | 1251 | if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1252 | cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n", |
Pavel Shilovsky | fb2036d | 2016-11-23 15:08:14 -0800 | [diff] [blame] | 1253 | len); |
Volker Lendecke | 6d9c6d5 | 2008-12-08 20:50:24 +0000 | [diff] [blame] | 1254 | return -EIO; |
| 1255 | } |
| 1256 | |
Pavel Shilovsky | 34f4deb | 2019-01-16 11:22:29 -0800 | [diff] [blame^] | 1257 | rc = wait_for_free_request(ses->server, CIFS_BLOCKING_OP, 0, |
| 1258 | &instance); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1259 | if (rc) |
| 1260 | return rc; |
| 1261 | |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 1262 | /* make sure that we sign in the same order that we send on this socket |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1263 | and avoid races inside tcp sendmsg code that could cause corruption |
| 1264 | of smb data */ |
| 1265 | |
Jeff Layton | 72ca545 | 2008-12-01 07:09:36 -0500 | [diff] [blame] | 1266 | mutex_lock(&ses->server->srv_mutex); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1267 | |
| 1268 | rc = allocate_mid(ses, in_buf, &midQ); |
| 1269 | if (rc) { |
Jeff Layton | 72ca545 | 2008-12-01 07:09:36 -0500 | [diff] [blame] | 1270 | mutex_unlock(&ses->server->srv_mutex); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1271 | return rc; |
| 1272 | } |
| 1273 | |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1274 | rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number); |
Volker Lendecke | 829049c | 2008-12-06 16:00:53 +0100 | [diff] [blame] | 1275 | if (rc) { |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1276 | cifs_delete_mid(midQ); |
Volker Lendecke | 829049c | 2008-12-06 16:00:53 +0100 | [diff] [blame] | 1277 | mutex_unlock(&ses->server->srv_mutex); |
| 1278 | return rc; |
| 1279 | } |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1280 | |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 1281 | midQ->mid_state = MID_REQUEST_SUBMITTED; |
Steve French | 789e666 | 2011-08-09 18:44:44 +0000 | [diff] [blame] | 1282 | cifs_in_send_inc(ses->server); |
Pavel Shilovsky | fb2036d | 2016-11-23 15:08:14 -0800 | [diff] [blame] | 1283 | rc = smb_send(ses->server, in_buf, len); |
Steve French | 789e666 | 2011-08-09 18:44:44 +0000 | [diff] [blame] | 1284 | cifs_in_send_dec(ses->server); |
| 1285 | cifs_save_when_sent(midQ); |
Jeff Layton | ad313cb | 2013-04-03 10:27:36 -0400 | [diff] [blame] | 1286 | |
| 1287 | if (rc < 0) |
| 1288 | ses->server->sequence_number -= 2; |
| 1289 | |
Jeff Layton | 72ca545 | 2008-12-01 07:09:36 -0500 | [diff] [blame] | 1290 | mutex_unlock(&ses->server->srv_mutex); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1291 | |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 1292 | if (rc < 0) { |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1293 | cifs_delete_mid(midQ); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1294 | return rc; |
| 1295 | } |
| 1296 | |
| 1297 | /* Wait for a reply - allow signals to interrupt. */ |
| 1298 | rc = wait_event_interruptible(ses->server->response_q, |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 1299 | (!(midQ->mid_state == MID_REQUEST_SUBMITTED)) || |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1300 | ((ses->server->tcpStatus != CifsGood) && |
| 1301 | (ses->server->tcpStatus != CifsNew))); |
| 1302 | |
| 1303 | /* Were we interrupted by a signal ? */ |
| 1304 | if ((rc == -ERESTARTSYS) && |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 1305 | (midQ->mid_state == MID_REQUEST_SUBMITTED) && |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1306 | ((ses->server->tcpStatus == CifsGood) || |
| 1307 | (ses->server->tcpStatus == CifsNew))) { |
| 1308 | |
| 1309 | if (in_buf->Command == SMB_COM_TRANSACTION2) { |
| 1310 | /* POSIX lock. We send a NT_CANCEL SMB to cause the |
| 1311 | blocking lock to return. */ |
Pavel Shilovsky | fb2036d | 2016-11-23 15:08:14 -0800 | [diff] [blame] | 1312 | rc = send_cancel(ses->server, &rqst, midQ); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1313 | if (rc) { |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1314 | cifs_delete_mid(midQ); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1315 | return rc; |
| 1316 | } |
| 1317 | } else { |
| 1318 | /* Windows lock. We send a LOCKINGX_CANCEL_LOCK |
| 1319 | to cause the blocking lock to return. */ |
| 1320 | |
| 1321 | rc = send_lock_cancel(xid, tcon, in_buf, out_buf); |
| 1322 | |
| 1323 | /* If we get -ENOLCK back the lock may have |
| 1324 | already been removed. Don't exit in this case. */ |
| 1325 | if (rc && rc != -ENOLCK) { |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1326 | cifs_delete_mid(midQ); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1327 | return rc; |
| 1328 | } |
| 1329 | } |
| 1330 | |
Jeff Layton | 1be912d | 2011-01-28 07:08:28 -0500 | [diff] [blame] | 1331 | rc = wait_for_response(ses->server, midQ); |
| 1332 | if (rc) { |
Pavel Shilovsky | fb2036d | 2016-11-23 15:08:14 -0800 | [diff] [blame] | 1333 | send_cancel(ses->server, &rqst, midQ); |
Jeff Layton | 1be912d | 2011-01-28 07:08:28 -0500 | [diff] [blame] | 1334 | spin_lock(&GlobalMid_Lock); |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 1335 | if (midQ->mid_state == MID_REQUEST_SUBMITTED) { |
Jeff Layton | 1be912d | 2011-01-28 07:08:28 -0500 | [diff] [blame] | 1336 | /* no longer considered to be "in-flight" */ |
| 1337 | midQ->callback = DeleteMidQEntry; |
| 1338 | spin_unlock(&GlobalMid_Lock); |
| 1339 | return rc; |
| 1340 | } |
| 1341 | spin_unlock(&GlobalMid_Lock); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1342 | } |
Jeff Layton | 1be912d | 2011-01-28 07:08:28 -0500 | [diff] [blame] | 1343 | |
| 1344 | /* We got the response - restart system call. */ |
| 1345 | rstart = 1; |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
Jeff Layton | 3c1105d | 2011-05-22 07:09:13 -0400 | [diff] [blame] | 1348 | rc = cifs_sync_mid_result(midQ, ses->server); |
Jeff Layton | 053d503 | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 1349 | if (rc != 0) |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1350 | return rc; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1351 | |
Volker Lendecke | 17c8bfe | 2008-12-06 16:38:19 +0100 | [diff] [blame] | 1352 | /* rcvd frame is ok */ |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 1353 | if (out_buf == NULL || midQ->mid_state != MID_RESPONSE_RECEIVED) { |
Volker Lendecke | 17c8bfe | 2008-12-06 16:38:19 +0100 | [diff] [blame] | 1354 | rc = -EIO; |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1355 | cifs_dbg(VFS, "Bad MID state?\n"); |
Volker Lendecke | 698e96a | 2008-12-06 16:39:31 +0100 | [diff] [blame] | 1356 | goto out; |
Volker Lendecke | 17c8bfe | 2008-12-06 16:38:19 +0100 | [diff] [blame] | 1357 | } |
| 1358 | |
Pavel Shilovsky | d4e4854 | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 1359 | *pbytes_returned = get_rfc1002_length(midQ->resp_buf); |
Jeff Layton | 2c8f981 | 2011-05-19 16:22:52 -0400 | [diff] [blame] | 1360 | memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4); |
| 1361 | rc = cifs_check_receive(midQ, ses->server, 0); |
Volker Lendecke | 17c8bfe | 2008-12-06 16:38:19 +0100 | [diff] [blame] | 1362 | out: |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1363 | cifs_delete_mid(midQ); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1364 | if (rstart && rc == -EACCES) |
| 1365 | return -ERESTARTSYS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | return rc; |
| 1367 | } |