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; |
| 116 | /* commands taking longer than one second are indications that |
| 117 | something is wrong, unless it is quite a slow link or server */ |
Karim Eshapa | 4328fea | 2017-05-12 01:53:38 +0200 | [diff] [blame] | 118 | if (time_after(now, midEntry->when_alloc + HZ)) { |
Pavel Shilovsky | 2dc7e1c | 2011-12-26 22:53:34 +0400 | [diff] [blame] | 119 | if ((cifsFYI & CIFS_TIMER) && (midEntry->command != command)) { |
Andy Shevchenko | 0b456f0 | 2014-08-27 16:49:44 +0300 | [diff] [blame] | 120 | pr_debug(" CIFS slow rsp: cmd %d mid %llu", |
Steve French | 1047abc | 2005-10-11 19:58:06 -0700 | [diff] [blame] | 121 | midEntry->command, midEntry->mid); |
Andy Shevchenko | 0b456f0 | 2014-08-27 16:49:44 +0300 | [diff] [blame] | 122 | pr_info(" A: 0x%lx S: 0x%lx R: 0x%lx\n", |
Steve French | 1047abc | 2005-10-11 19:58:06 -0700 | [diff] [blame] | 123 | now - midEntry->when_alloc, |
| 124 | now - midEntry->when_sent, |
| 125 | now - midEntry->when_received); |
| 126 | } |
| 127 | } |
| 128 | #endif |
Lars Persson | 696e420 | 2018-06-25 14:05:25 +0200 | [diff] [blame] | 129 | cifs_mid_q_entry_release(midEntry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 132 | void |
| 133 | cifs_delete_mid(struct mid_q_entry *mid) |
Jeff Layton | ddc8cf8 | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 134 | { |
| 135 | spin_lock(&GlobalMid_Lock); |
| 136 | list_del(&mid->qhead); |
| 137 | spin_unlock(&GlobalMid_Lock); |
| 138 | |
| 139 | DeleteMidQEntry(mid); |
| 140 | } |
| 141 | |
Jeff Layton | 6f49f46 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 142 | /* |
| 143 | * smb_send_kvec - send an array of kvecs to the server |
| 144 | * @server: Server to send the data to |
Al Viro | 3ab3f2a | 2015-11-13 02:36:04 -0500 | [diff] [blame] | 145 | * @smb_msg: Message to send |
Jeff Layton | 6f49f46 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 146 | * @sent: amount of data sent on socket is stored here |
| 147 | * |
| 148 | * Our basic "send data to server" function. Should be called with srv_mutex |
| 149 | * held. The caller is responsible for handling the results. |
| 150 | */ |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 151 | static int |
Al Viro | 3ab3f2a | 2015-11-13 02:36:04 -0500 | [diff] [blame] | 152 | smb_send_kvec(struct TCP_Server_Info *server, struct msghdr *smb_msg, |
| 153 | size_t *sent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | { |
| 155 | int rc = 0; |
Al Viro | 3ab3f2a | 2015-11-13 02:36:04 -0500 | [diff] [blame] | 156 | int retries = 0; |
Steve French | edf1ae4 | 2008-10-29 00:47:57 +0000 | [diff] [blame] | 157 | struct socket *ssocket = server->ssocket; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 158 | |
Jeff Layton | 6f49f46 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 159 | *sent = 0; |
| 160 | |
Al Viro | 3ab3f2a | 2015-11-13 02:36:04 -0500 | [diff] [blame] | 161 | smb_msg->msg_name = (struct sockaddr *) &server->dstaddr; |
| 162 | smb_msg->msg_namelen = sizeof(struct sockaddr); |
| 163 | smb_msg->msg_control = NULL; |
| 164 | smb_msg->msg_controllen = 0; |
Jeff Layton | 0496e02 | 2008-12-30 12:39:16 -0500 | [diff] [blame] | 165 | if (server->noblocksnd) |
Al Viro | 3ab3f2a | 2015-11-13 02:36:04 -0500 | [diff] [blame] | 166 | smb_msg->msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL; |
Steve French | edf1ae4 | 2008-10-29 00:47:57 +0000 | [diff] [blame] | 167 | else |
Al Viro | 3ab3f2a | 2015-11-13 02:36:04 -0500 | [diff] [blame] | 168 | smb_msg->msg_flags = MSG_NOSIGNAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
Al Viro | 3ab3f2a | 2015-11-13 02:36:04 -0500 | [diff] [blame] | 170 | while (msg_data_left(smb_msg)) { |
Jeff Layton | 6f49f46 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 171 | /* |
| 172 | * If blocking send, we try 3 times, since each can block |
| 173 | * for 5 seconds. For nonblocking we have to try more |
| 174 | * but wait increasing amounts of time allowing time for |
| 175 | * socket to clear. The overall time we wait in either |
| 176 | * case to send on the socket is about 15 seconds. |
| 177 | * Similarly we wait for 15 seconds for a response from |
| 178 | * the server in SendReceive[2] for the server to send |
| 179 | * a response back for most types of requests (except |
| 180 | * SMB Write past end of file which can be slow, and |
| 181 | * blocking lock operations). NFS waits slightly longer |
| 182 | * than CIFS, but this can make it take longer for |
| 183 | * nonresponsive servers to be detected and 15 seconds |
| 184 | * is more than enough time for modern networks to |
| 185 | * send a packet. In most cases if we fail to send |
| 186 | * after the retries we will kill the socket and |
| 187 | * reconnect which may clear the network problem. |
| 188 | */ |
Al Viro | 3ab3f2a | 2015-11-13 02:36:04 -0500 | [diff] [blame] | 189 | rc = sock_sendmsg(ssocket, smb_msg); |
Jeff Layton | ce6c44e | 2013-03-22 08:36:45 -0400 | [diff] [blame] | 190 | if (rc == -EAGAIN) { |
Al Viro | 3ab3f2a | 2015-11-13 02:36:04 -0500 | [diff] [blame] | 191 | retries++; |
| 192 | if (retries >= 14 || |
| 193 | (!server->noblocksnd && (retries > 2))) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 194 | cifs_dbg(VFS, "sends on sock %p stuck for 15 seconds\n", |
| 195 | ssocket); |
Al Viro | 3ab3f2a | 2015-11-13 02:36:04 -0500 | [diff] [blame] | 196 | return -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | } |
Al Viro | 3ab3f2a | 2015-11-13 02:36:04 -0500 | [diff] [blame] | 198 | msleep(1 << retries); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | continue; |
| 200 | } |
Jeff Layton | 6f49f46 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 201 | |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 202 | if (rc < 0) |
Al Viro | 3ab3f2a | 2015-11-13 02:36:04 -0500 | [diff] [blame] | 203 | return rc; |
Jeff Layton | 6f49f46 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 204 | |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 205 | if (rc == 0) { |
Steve French | 3e84469 | 2005-10-03 13:37:24 -0700 | [diff] [blame] | 206 | /* should never happen, letting socket clear before |
| 207 | retrying is our only obvious option here */ |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 208 | cifs_dbg(VFS, "tcp sent no data\n"); |
Steve French | 3e84469 | 2005-10-03 13:37:24 -0700 | [diff] [blame] | 209 | msleep(500); |
| 210 | continue; |
| 211 | } |
Jeff Layton | 6f49f46 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 212 | |
Al Viro | 3ab3f2a | 2015-11-13 02:36:04 -0500 | [diff] [blame] | 213 | /* send was at least partially successful */ |
| 214 | *sent += rc; |
| 215 | retries = 0; /* in case we get ENOSPC on the next send */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | } |
Al Viro | 3ab3f2a | 2015-11-13 02:36:04 -0500 | [diff] [blame] | 217 | return 0; |
Jeff Layton | 97bc00b | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Paulo Alcantara | 35e2cc1 | 2018-06-15 10:22:44 -0300 | [diff] [blame] | 220 | unsigned long |
Ronnie Sahlberg | 81f39f9 | 2018-06-28 10:47:14 +1000 | [diff] [blame] | 221 | smb_rqst_len(struct TCP_Server_Info *server, struct smb_rqst *rqst) |
Jeff Layton | a26054d | 2014-02-14 07:21:00 -0500 | [diff] [blame] | 222 | { |
| 223 | unsigned int i; |
Paulo Alcantara | 35e2cc1 | 2018-06-15 10:22:44 -0300 | [diff] [blame] | 224 | struct kvec *iov; |
| 225 | int nvec; |
Jeff Layton | a26054d | 2014-02-14 07:21:00 -0500 | [diff] [blame] | 226 | unsigned long buflen = 0; |
| 227 | |
Ronnie Sahlberg | 81f39f9 | 2018-06-28 10:47:14 +1000 | [diff] [blame] | 228 | if (server->vals->header_preamble_size == 0 && |
| 229 | rqst->rq_nvec >= 2 && rqst->rq_iov[0].iov_len == 4) { |
Paulo Alcantara | 35e2cc1 | 2018-06-15 10:22:44 -0300 | [diff] [blame] | 230 | iov = &rqst->rq_iov[1]; |
| 231 | nvec = rqst->rq_nvec - 1; |
| 232 | } else { |
| 233 | iov = rqst->rq_iov; |
| 234 | nvec = rqst->rq_nvec; |
| 235 | } |
| 236 | |
Jeff Layton | a26054d | 2014-02-14 07:21:00 -0500 | [diff] [blame] | 237 | /* total up iov array first */ |
Paulo Alcantara | 35e2cc1 | 2018-06-15 10:22:44 -0300 | [diff] [blame] | 238 | for (i = 0; i < nvec; i++) |
Jeff Layton | a26054d | 2014-02-14 07:21:00 -0500 | [diff] [blame] | 239 | buflen += iov[i].iov_len; |
| 240 | |
Long Li | c06a0f2 | 2018-05-30 12:47:57 -0700 | [diff] [blame] | 241 | /* |
| 242 | * Add in the page array if there is one. The caller needs to make |
| 243 | * sure rq_offset and rq_tailsz are set correctly. If a buffer of |
| 244 | * multiple pages ends at page boundary, rq_tailsz needs to be set to |
| 245 | * PAGE_SIZE. |
| 246 | */ |
Jeff Layton | a26054d | 2014-02-14 07:21:00 -0500 | [diff] [blame] | 247 | if (rqst->rq_npages) { |
Long Li | c06a0f2 | 2018-05-30 12:47:57 -0700 | [diff] [blame] | 248 | if (rqst->rq_npages == 1) |
| 249 | buflen += rqst->rq_tailsz; |
| 250 | else { |
| 251 | /* |
| 252 | * If there is more than one page, calculate the |
| 253 | * buffer length based on rq_offset and rq_tailsz |
| 254 | */ |
| 255 | buflen += rqst->rq_pagesz * (rqst->rq_npages - 1) - |
| 256 | rqst->rq_offset; |
| 257 | buflen += rqst->rq_tailsz; |
| 258 | } |
Jeff Layton | a26054d | 2014-02-14 07:21:00 -0500 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | return buflen; |
| 262 | } |
| 263 | |
Jeff Layton | 6f49f46 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 264 | static int |
Ronnie Sahlberg | 07cd952 | 2018-06-12 08:01:00 +1000 | [diff] [blame] | 265 | __smb_send_rqst(struct TCP_Server_Info *server, int num_rqst, |
| 266 | struct smb_rqst *rqst) |
Jeff Layton | 6f49f46 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 267 | { |
Ronnie Sahlberg | 07cd952 | 2018-06-12 08:01:00 +1000 | [diff] [blame] | 268 | int rc = 0; |
| 269 | struct kvec *iov; |
| 270 | int n_vec; |
| 271 | unsigned int send_length = 0; |
| 272 | unsigned int i, j; |
Al Viro | 3ab3f2a | 2015-11-13 02:36:04 -0500 | [diff] [blame] | 273 | size_t total_len = 0, sent, size; |
Jeff Layton | b8eed28 | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 274 | struct socket *ssocket = server->ssocket; |
Al Viro | 3ab3f2a | 2015-11-13 02:36:04 -0500 | [diff] [blame] | 275 | struct msghdr smb_msg; |
Jeff Layton | b8eed28 | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 276 | int val = 1; |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 277 | __be32 rfc1002_marker; |
| 278 | |
Long Li | 9762c2d | 2017-11-22 17:38:43 -0700 | [diff] [blame] | 279 | if (cifs_rdma_enabled(server) && server->smbd_conn) { |
Ronnie Sahlberg | 81f39f9 | 2018-06-28 10:47:14 +1000 | [diff] [blame] | 280 | rc = smbd_send(server, rqst); |
Long Li | 9762c2d | 2017-11-22 17:38:43 -0700 | [diff] [blame] | 281 | goto smbd_done; |
| 282 | } |
Jeff Layton | ea702b8 | 2012-12-27 07:28:55 -0500 | [diff] [blame] | 283 | if (ssocket == NULL) |
| 284 | return -ENOTSOCK; |
| 285 | |
Jeff Layton | b8eed28 | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 286 | /* cork the socket */ |
| 287 | kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK, |
| 288 | (char *)&val, sizeof(val)); |
| 289 | |
Ronnie Sahlberg | 07cd952 | 2018-06-12 08:01:00 +1000 | [diff] [blame] | 290 | for (j = 0; j < num_rqst; j++) |
Ronnie Sahlberg | 81f39f9 | 2018-06-28 10:47:14 +1000 | [diff] [blame] | 291 | send_length += smb_rqst_len(server, &rqst[j]); |
Ronnie Sahlberg | 07cd952 | 2018-06-12 08:01:00 +1000 | [diff] [blame] | 292 | rfc1002_marker = cpu_to_be32(send_length); |
| 293 | |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 294 | /* Generate a rfc1002 marker for SMB2+ */ |
| 295 | if (server->vals->header_preamble_size == 0) { |
| 296 | struct kvec hiov = { |
| 297 | .iov_base = &rfc1002_marker, |
| 298 | .iov_len = 4 |
| 299 | }; |
| 300 | iov_iter_kvec(&smb_msg.msg_iter, WRITE | ITER_KVEC, &hiov, |
| 301 | 1, 4); |
| 302 | rc = smb_send_kvec(server, &smb_msg, &sent); |
| 303 | if (rc < 0) |
| 304 | goto uncork; |
| 305 | |
| 306 | total_len += sent; |
| 307 | send_length += 4; |
| 308 | } |
| 309 | |
Paulo Alcantara | 662bf5b | 2018-06-14 17:34:08 -0300 | [diff] [blame] | 310 | cifs_dbg(FYI, "Sending smb: smb_len=%u\n", send_length); |
| 311 | |
Ronnie Sahlberg | 07cd952 | 2018-06-12 08:01:00 +1000 | [diff] [blame] | 312 | for (j = 0; j < num_rqst; j++) { |
| 313 | iov = rqst[j].rq_iov; |
| 314 | n_vec = rqst[j].rq_nvec; |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 315 | |
Ronnie Sahlberg | 07cd952 | 2018-06-12 08:01:00 +1000 | [diff] [blame] | 316 | size = 0; |
Paulo Alcantara | 662bf5b | 2018-06-14 17:34:08 -0300 | [diff] [blame] | 317 | for (i = 0; i < n_vec; i++) { |
| 318 | dump_smb(iov[i].iov_base, iov[i].iov_len); |
Ronnie Sahlberg | 07cd952 | 2018-06-12 08:01:00 +1000 | [diff] [blame] | 319 | size += iov[i].iov_len; |
Paulo Alcantara | 662bf5b | 2018-06-14 17:34:08 -0300 | [diff] [blame] | 320 | } |
Al Viro | 3ab3f2a | 2015-11-13 02:36:04 -0500 | [diff] [blame] | 321 | |
Ronnie Sahlberg | 07cd952 | 2018-06-12 08:01:00 +1000 | [diff] [blame] | 322 | iov_iter_kvec(&smb_msg.msg_iter, WRITE | ITER_KVEC, |
| 323 | iov, n_vec, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | |
Al Viro | 3ab3f2a | 2015-11-13 02:36:04 -0500 | [diff] [blame] | 325 | rc = smb_send_kvec(server, &smb_msg, &sent); |
Jeff Layton | 97bc00b | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 326 | if (rc < 0) |
Ronnie Sahlberg | 07cd952 | 2018-06-12 08:01:00 +1000 | [diff] [blame] | 327 | goto uncork; |
Jeff Layton | 97bc00b | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 328 | |
| 329 | total_len += sent; |
Ronnie Sahlberg | 07cd952 | 2018-06-12 08:01:00 +1000 | [diff] [blame] | 330 | |
| 331 | /* now walk the page array and send each page in it */ |
| 332 | for (i = 0; i < rqst[j].rq_npages; i++) { |
| 333 | struct bio_vec bvec; |
| 334 | |
| 335 | bvec.bv_page = rqst[j].rq_pages[i]; |
| 336 | rqst_page_get_length(&rqst[j], i, &bvec.bv_len, |
| 337 | &bvec.bv_offset); |
| 338 | |
| 339 | iov_iter_bvec(&smb_msg.msg_iter, WRITE | ITER_BVEC, |
| 340 | &bvec, 1, bvec.bv_len); |
| 341 | rc = smb_send_kvec(server, &smb_msg, &sent); |
| 342 | if (rc < 0) |
| 343 | break; |
| 344 | |
| 345 | total_len += sent; |
| 346 | } |
Jeff Layton | 97bc00b | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | uncork: |
Jeff Layton | b8eed28 | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 350 | /* uncork it */ |
| 351 | val = 0; |
| 352 | kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK, |
| 353 | (char *)&val, sizeof(val)); |
| 354 | |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 355 | if ((total_len > 0) && (total_len != send_length)) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 356 | cifs_dbg(FYI, "partial send (wanted=%u sent=%zu): terminating session\n", |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 357 | send_length, total_len); |
Jeff Layton | 6f49f46 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 358 | /* |
| 359 | * If we have only sent part of an SMB then the next SMB could |
| 360 | * be taken as the remainder of this one. We need to kill the |
| 361 | * socket so the server throws away the partial SMB |
| 362 | */ |
Steve French | edf1ae4 | 2008-10-29 00:47:57 +0000 | [diff] [blame] | 363 | server->tcpStatus = CifsNeedReconnect; |
Steve French | bf1fdeb | 2018-07-30 19:23:09 -0500 | [diff] [blame^] | 364 | trace_smb3_partial_send_reconnect(server->CurrentMid, |
| 365 | server->hostname); |
Steve French | edf1ae4 | 2008-10-29 00:47:57 +0000 | [diff] [blame] | 366 | } |
Long Li | 9762c2d | 2017-11-22 17:38:43 -0700 | [diff] [blame] | 367 | smbd_done: |
Jeff Layton | d804d41 | 2011-01-28 15:05:43 -0500 | [diff] [blame] | 368 | if (rc < 0 && rc != -EINTR) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 369 | cifs_dbg(VFS, "Error %d sending data on socket to server\n", |
| 370 | rc); |
Jeff Layton | d804d41 | 2011-01-28 15:05:43 -0500 | [diff] [blame] | 371 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
| 374 | return rc; |
| 375 | } |
| 376 | |
Jeff Layton | 6f49f46 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 377 | static int |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 378 | smb_send_rqst(struct TCP_Server_Info *server, struct smb_rqst *rqst, int flags) |
Jeff Layton | 6f49f46 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 379 | { |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 380 | struct smb_rqst cur_rqst; |
| 381 | int rc; |
Jeff Layton | 6f49f46 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 382 | |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 383 | if (!(flags & CIFS_TRANSFORM_REQ)) |
Ronnie Sahlberg | 07cd952 | 2018-06-12 08:01:00 +1000 | [diff] [blame] | 384 | return __smb_send_rqst(server, 1, rqst); |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 385 | |
| 386 | if (!server->ops->init_transform_rq || |
| 387 | !server->ops->free_transform_rq) { |
| 388 | cifs_dbg(VFS, "Encryption requested but transform callbacks are missed\n"); |
| 389 | return -EIO; |
| 390 | } |
| 391 | |
| 392 | rc = server->ops->init_transform_rq(server, &cur_rqst, rqst); |
| 393 | if (rc) |
| 394 | return rc; |
| 395 | |
Ronnie Sahlberg | 07cd952 | 2018-06-12 08:01:00 +1000 | [diff] [blame] | 396 | rc = __smb_send_rqst(server, 1, &cur_rqst); |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 397 | server->ops->free_transform_rq(&cur_rqst); |
| 398 | return rc; |
Jeff Layton | 6f49f46 | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Jeff Layton | 0496e02 | 2008-12-30 12:39:16 -0500 | [diff] [blame] | 401 | int |
| 402 | smb_send(struct TCP_Server_Info *server, struct smb_hdr *smb_buffer, |
| 403 | unsigned int smb_buf_length) |
| 404 | { |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 405 | struct kvec iov[2]; |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 406 | struct smb_rqst rqst = { .rq_iov = iov, |
| 407 | .rq_nvec = 2 }; |
Jeff Layton | 0496e02 | 2008-12-30 12:39:16 -0500 | [diff] [blame] | 408 | |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 409 | iov[0].iov_base = smb_buffer; |
| 410 | iov[0].iov_len = 4; |
| 411 | iov[1].iov_base = (char *)smb_buffer + 4; |
| 412 | iov[1].iov_len = smb_buf_length; |
Jeff Layton | 0496e02 | 2008-12-30 12:39:16 -0500 | [diff] [blame] | 413 | |
Ronnie Sahlberg | 07cd952 | 2018-06-12 08:01:00 +1000 | [diff] [blame] | 414 | return __smb_send_rqst(server, 1, &rqst); |
Jeff Layton | 0496e02 | 2008-12-30 12:39:16 -0500 | [diff] [blame] | 415 | } |
| 416 | |
Pavel Shilovsky | fc40f9c | 2012-02-17 17:09:12 +0300 | [diff] [blame] | 417 | static int |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 418 | wait_for_free_credits(struct TCP_Server_Info *server, const int timeout, |
Pavel Shilovsky | bc205ed | 2012-03-15 13:22:27 +0300 | [diff] [blame] | 419 | int *credits) |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 420 | { |
Pavel Shilovsky | 5bc5949 | 2012-02-21 19:56:08 +0300 | [diff] [blame] | 421 | int rc; |
| 422 | |
Pavel Shilovsky | fc40f9c | 2012-02-17 17:09:12 +0300 | [diff] [blame] | 423 | spin_lock(&server->req_lock); |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 424 | if (timeout == CIFS_ASYNC_OP) { |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 425 | /* oplock breaks must not be held up */ |
Pavel Shilovsky | fc40f9c | 2012-02-17 17:09:12 +0300 | [diff] [blame] | 426 | server->in_flight++; |
Pavel Shilovsky | bc205ed | 2012-03-15 13:22:27 +0300 | [diff] [blame] | 427 | *credits -= 1; |
Pavel Shilovsky | fc40f9c | 2012-02-17 17:09:12 +0300 | [diff] [blame] | 428 | spin_unlock(&server->req_lock); |
Volker Lendecke | 27a97a6 | 2008-12-08 20:59:39 +0000 | [diff] [blame] | 429 | return 0; |
| 430 | } |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 431 | |
Volker Lendecke | 27a97a6 | 2008-12-08 20:59:39 +0000 | [diff] [blame] | 432 | while (1) { |
Pavel Shilovsky | bc205ed | 2012-03-15 13:22:27 +0300 | [diff] [blame] | 433 | if (*credits <= 0) { |
Pavel Shilovsky | fc40f9c | 2012-02-17 17:09:12 +0300 | [diff] [blame] | 434 | spin_unlock(&server->req_lock); |
Steve French | 789e666 | 2011-08-09 18:44:44 +0000 | [diff] [blame] | 435 | cifs_num_waiters_inc(server); |
Pavel Shilovsky | 5bc5949 | 2012-02-21 19:56:08 +0300 | [diff] [blame] | 436 | rc = wait_event_killable(server->request_q, |
Pavel Shilovsky | bc205ed | 2012-03-15 13:22:27 +0300 | [diff] [blame] | 437 | has_credits(server, credits)); |
Steve French | 789e666 | 2011-08-09 18:44:44 +0000 | [diff] [blame] | 438 | cifs_num_waiters_dec(server); |
Pavel Shilovsky | 5bc5949 | 2012-02-21 19:56:08 +0300 | [diff] [blame] | 439 | if (rc) |
| 440 | return rc; |
Pavel Shilovsky | fc40f9c | 2012-02-17 17:09:12 +0300 | [diff] [blame] | 441 | spin_lock(&server->req_lock); |
Volker Lendecke | 27a97a6 | 2008-12-08 20:59:39 +0000 | [diff] [blame] | 442 | } else { |
Jeff Layton | c5797a9 | 2011-01-11 07:24:01 -0500 | [diff] [blame] | 443 | if (server->tcpStatus == CifsExiting) { |
Pavel Shilovsky | fc40f9c | 2012-02-17 17:09:12 +0300 | [diff] [blame] | 444 | spin_unlock(&server->req_lock); |
Volker Lendecke | 27a97a6 | 2008-12-08 20:59:39 +0000 | [diff] [blame] | 445 | return -ENOENT; |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 446 | } |
Volker Lendecke | 27a97a6 | 2008-12-08 20:59:39 +0000 | [diff] [blame] | 447 | |
Pavel Shilovsky | 2d86dbc | 2012-02-06 15:59:18 +0400 | [diff] [blame] | 448 | /* |
| 449 | * Can not count locking commands against total |
| 450 | * as they are allowed to block on server. |
| 451 | */ |
Volker Lendecke | 27a97a6 | 2008-12-08 20:59:39 +0000 | [diff] [blame] | 452 | |
| 453 | /* update # of requests on the wire to server */ |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 454 | if (timeout != CIFS_BLOCKING_OP) { |
Pavel Shilovsky | bc205ed | 2012-03-15 13:22:27 +0300 | [diff] [blame] | 455 | *credits -= 1; |
Pavel Shilovsky | fc40f9c | 2012-02-17 17:09:12 +0300 | [diff] [blame] | 456 | server->in_flight++; |
Pavel Shilovsky | 2d86dbc | 2012-02-06 15:59:18 +0400 | [diff] [blame] | 457 | } |
Pavel Shilovsky | fc40f9c | 2012-02-17 17:09:12 +0300 | [diff] [blame] | 458 | spin_unlock(&server->req_lock); |
Volker Lendecke | 27a97a6 | 2008-12-08 20:59:39 +0000 | [diff] [blame] | 459 | break; |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | return 0; |
| 463 | } |
| 464 | |
Pavel Shilovsky | bc205ed | 2012-03-15 13:22:27 +0300 | [diff] [blame] | 465 | static int |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 466 | wait_for_free_request(struct TCP_Server_Info *server, const int timeout, |
| 467 | const int optype) |
Pavel Shilovsky | bc205ed | 2012-03-15 13:22:27 +0300 | [diff] [blame] | 468 | { |
Shirish Pargaonkar | eb4c7df | 2013-10-03 05:44:45 -0500 | [diff] [blame] | 469 | int *val; |
| 470 | |
| 471 | val = server->ops->get_credits_field(server, optype); |
| 472 | /* Since an echo is already inflight, no need to wait to send another */ |
| 473 | if (*val <= 0 && optype == CIFS_ECHO_OP) |
| 474 | return -EAGAIN; |
| 475 | return wait_for_free_credits(server, timeout, val); |
Pavel Shilovsky | bc205ed | 2012-03-15 13:22:27 +0300 | [diff] [blame] | 476 | } |
| 477 | |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 478 | int |
| 479 | cifs_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size, |
| 480 | unsigned int *num, unsigned int *credits) |
| 481 | { |
| 482 | *num = size; |
| 483 | *credits = 0; |
| 484 | return 0; |
| 485 | } |
| 486 | |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 487 | 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] | 488 | struct mid_q_entry **ppmidQ) |
| 489 | { |
| 490 | if (ses->server->tcpStatus == CifsExiting) { |
| 491 | return -ENOENT; |
Volker Lendecke | 8fbbd36 | 2008-12-06 13:12:34 +0100 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | if (ses->server->tcpStatus == CifsNeedReconnect) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 495 | cifs_dbg(FYI, "tcp session dead - return to caller to retry\n"); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 496 | return -EAGAIN; |
Volker Lendecke | 8fbbd36 | 2008-12-06 13:12:34 +0100 | [diff] [blame] | 497 | } |
| 498 | |
Shirish Pargaonkar | 7f48558 | 2013-10-12 10:06:03 -0500 | [diff] [blame] | 499 | if (ses->status == CifsNew) { |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 500 | if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) && |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 501 | (in_buf->Command != SMB_COM_NEGOTIATE)) |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 502 | return -EAGAIN; |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 503 | /* else ok - we are setting up session */ |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 504 | } |
Shirish Pargaonkar | 7f48558 | 2013-10-12 10:06:03 -0500 | [diff] [blame] | 505 | |
| 506 | if (ses->status == CifsExiting) { |
| 507 | /* check if SMB session is bad because we are setting it up */ |
| 508 | if (in_buf->Command != SMB_COM_LOGOFF_ANDX) |
| 509 | return -EAGAIN; |
| 510 | /* else ok - we are shutting down session */ |
| 511 | } |
| 512 | |
Jeff Layton | 24b9b06 | 2008-12-01 07:09:34 -0500 | [diff] [blame] | 513 | *ppmidQ = AllocMidQEntry(in_buf, ses->server); |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 514 | if (*ppmidQ == NULL) |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 515 | return -ENOMEM; |
Jeff Layton | ddc8cf8 | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 516 | spin_lock(&GlobalMid_Lock); |
| 517 | list_add_tail(&(*ppmidQ)->qhead, &ses->server->pending_mid_q); |
| 518 | spin_unlock(&GlobalMid_Lock); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 519 | return 0; |
| 520 | } |
| 521 | |
Jeff Layton | 0ade640 | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 522 | static int |
| 523 | 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] | 524 | { |
Jeff Layton | 0ade640 | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 525 | int error; |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 526 | |
Colin Cross | 5853cc2 | 2013-05-07 17:52:05 +0000 | [diff] [blame] | 527 | error = wait_event_freezekillable_unsafe(server->response_q, |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 528 | midQ->mid_state != MID_REQUEST_SUBMITTED); |
Jeff Layton | 0ade640 | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 529 | if (error < 0) |
| 530 | return -ERESTARTSYS; |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 531 | |
Jeff Layton | 0ade640 | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 532 | return 0; |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 535 | struct mid_q_entry * |
| 536 | 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] | 537 | { |
| 538 | int rc; |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 539 | 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] | 540 | struct mid_q_entry *mid; |
| 541 | |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 542 | if (rqst->rq_iov[0].iov_len != 4 || |
| 543 | rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base) |
| 544 | return ERR_PTR(-EIO); |
| 545 | |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 546 | /* enable signing if server requires it */ |
Jeff Layton | 38d77c5 | 2013-05-26 07:01:00 -0400 | [diff] [blame] | 547 | if (server->sign) |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 548 | hdr->Flags2 |= SMBFLG2_SECURITY_SIGNATURE; |
| 549 | |
| 550 | mid = AllocMidQEntry(hdr, server); |
| 551 | if (mid == NULL) |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 552 | return ERR_PTR(-ENOMEM); |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 553 | |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 554 | rc = cifs_sign_rqst(rqst, server, &mid->sequence_number); |
Sachin Prabhu | ffc61cc | 2012-07-11 12:28:05 +0100 | [diff] [blame] | 555 | if (rc) { |
| 556 | DeleteMidQEntry(mid); |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 557 | return ERR_PTR(rc); |
Sachin Prabhu | ffc61cc | 2012-07-11 12:28:05 +0100 | [diff] [blame] | 558 | } |
| 559 | |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 560 | return mid; |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 561 | } |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 562 | |
| 563 | /* |
Jeff Layton | a6827c1 | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 564 | * Send a SMB request and set the callback function in the mid to handle |
| 565 | * the result. Caller is responsible for dealing with timeouts. |
| 566 | */ |
| 567 | int |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 568 | cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst, |
Pavel Shilovsky | 9b7c18a | 2016-11-16 14:06:17 -0800 | [diff] [blame] | 569 | mid_receive_t *receive, mid_callback_t *callback, |
| 570 | mid_handle_t *handle, void *cbdata, const int flags) |
Jeff Layton | a6827c1 | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 571 | { |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 572 | int rc, timeout, optype; |
Jeff Layton | a6827c1 | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 573 | struct mid_q_entry *mid; |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 574 | unsigned int credits = 0; |
Jeff Layton | a6827c1 | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 575 | |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 576 | timeout = flags & CIFS_TIMEOUT_MASK; |
| 577 | optype = flags & CIFS_OP_MASK; |
| 578 | |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 579 | if ((flags & CIFS_HAS_CREDITS) == 0) { |
| 580 | rc = wait_for_free_request(server, timeout, optype); |
| 581 | if (rc) |
| 582 | return rc; |
| 583 | credits = 1; |
| 584 | } |
Jeff Layton | a6827c1 | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 585 | |
| 586 | mutex_lock(&server->srv_mutex); |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 587 | mid = server->ops->setup_async_request(server, rqst); |
| 588 | if (IS_ERR(mid)) { |
Jeff Layton | a6827c1 | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 589 | mutex_unlock(&server->srv_mutex); |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 590 | add_credits_and_wake_if(server, credits, optype); |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 591 | return PTR_ERR(mid); |
Jeff Layton | a6827c1 | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 592 | } |
| 593 | |
Jeff Layton | 44d22d8 | 2011-10-19 15:29:49 -0400 | [diff] [blame] | 594 | mid->receive = receive; |
Jeff Layton | a6827c1 | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 595 | mid->callback = callback; |
| 596 | mid->callback_data = cbdata; |
Pavel Shilovsky | 9b7c18a | 2016-11-16 14:06:17 -0800 | [diff] [blame] | 597 | mid->handle = handle; |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 598 | mid->mid_state = MID_REQUEST_SUBMITTED; |
Steve French | 789e666 | 2011-08-09 18:44:44 +0000 | [diff] [blame] | 599 | |
Sachin Prabhu | ffc61cc | 2012-07-11 12:28:05 +0100 | [diff] [blame] | 600 | /* put it on the pending_mid_q */ |
| 601 | spin_lock(&GlobalMid_Lock); |
| 602 | list_add_tail(&mid->qhead, &server->pending_mid_q); |
| 603 | spin_unlock(&GlobalMid_Lock); |
| 604 | |
Long Li | 93d2cb6 | 2017-06-28 15:55:55 -0700 | [diff] [blame] | 605 | /* |
| 606 | * Need to store the time in mid before calling I/O. For call_async, |
| 607 | * I/O response may come back and free the mid entry on another thread. |
| 608 | */ |
| 609 | cifs_save_when_sent(mid); |
Steve French | 789e666 | 2011-08-09 18:44:44 +0000 | [diff] [blame] | 610 | cifs_in_send_inc(server); |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 611 | rc = smb_send_rqst(server, rqst, flags); |
Steve French | 789e666 | 2011-08-09 18:44:44 +0000 | [diff] [blame] | 612 | cifs_in_send_dec(server); |
Jeff Layton | ad313cb | 2013-04-03 10:27:36 -0400 | [diff] [blame] | 613 | |
Rabin Vincent | 820962d | 2015-12-23 07:32:41 +0100 | [diff] [blame] | 614 | if (rc < 0) { |
Jeff Layton | ad313cb | 2013-04-03 10:27:36 -0400 | [diff] [blame] | 615 | server->sequence_number -= 2; |
Rabin Vincent | 820962d | 2015-12-23 07:32:41 +0100 | [diff] [blame] | 616 | cifs_delete_mid(mid); |
| 617 | } |
| 618 | |
Jeff Layton | a6827c1 | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 619 | mutex_unlock(&server->srv_mutex); |
Steve French | 789e666 | 2011-08-09 18:44:44 +0000 | [diff] [blame] | 620 | |
Sachin Prabhu | ffc61cc | 2012-07-11 12:28:05 +0100 | [diff] [blame] | 621 | if (rc == 0) |
| 622 | return 0; |
Jeff Layton | a6827c1 | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 623 | |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 624 | add_credits_and_wake_if(server, credits, optype); |
Jeff Layton | a6827c1 | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 625 | return rc; |
| 626 | } |
| 627 | |
| 628 | /* |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 629 | * |
| 630 | * Send an SMB Request. No response info (other than return code) |
| 631 | * needs to be parsed. |
| 632 | * |
| 633 | * flags indicate the type of request buffer and how long to wait |
| 634 | * and whether to log NT STATUS code (error) before mapping it to POSIX error |
| 635 | * |
| 636 | */ |
| 637 | int |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 638 | SendReceiveNoRsp(const unsigned int xid, struct cifs_ses *ses, |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 639 | char *in_buf, int flags) |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 640 | { |
| 641 | int rc; |
| 642 | struct kvec iov[1]; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 643 | struct kvec rsp_iov; |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 644 | int resp_buf_type; |
| 645 | |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 646 | iov[0].iov_base = in_buf; |
| 647 | iov[0].iov_len = get_rfc1002_length(in_buf) + 4; |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 648 | flags |= CIFS_NO_RESP; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 649 | rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 650 | cifs_dbg(NOISY, "SendRcvNoRsp flags %d rc %d\n", flags, rc); |
Steve French | 90c81e0 | 2008-02-12 20:32:36 +0000 | [diff] [blame] | 651 | |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 652 | return rc; |
| 653 | } |
| 654 | |
Jeff Layton | 053d503 | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 655 | static int |
Jeff Layton | 3c1105d | 2011-05-22 07:09:13 -0400 | [diff] [blame] | 656 | 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] | 657 | { |
| 658 | int rc = 0; |
| 659 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 660 | cifs_dbg(FYI, "%s: cmd=%d mid=%llu state=%d\n", |
| 661 | __func__, le16_to_cpu(mid->command), mid->mid, mid->mid_state); |
Jeff Layton | 053d503 | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 662 | |
Jeff Layton | 74dd92a | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 663 | spin_lock(&GlobalMid_Lock); |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 664 | switch (mid->mid_state) { |
Jeff Layton | 74dd92a | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 665 | case MID_RESPONSE_RECEIVED: |
Jeff Layton | 053d503 | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 666 | spin_unlock(&GlobalMid_Lock); |
| 667 | return rc; |
Jeff Layton | 74dd92a | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 668 | case MID_RETRY_NEEDED: |
| 669 | rc = -EAGAIN; |
| 670 | break; |
Jeff Layton | 71823ba | 2011-02-10 08:03:50 -0500 | [diff] [blame] | 671 | case MID_RESPONSE_MALFORMED: |
| 672 | rc = -EIO; |
| 673 | break; |
Jeff Layton | 3c1105d | 2011-05-22 07:09:13 -0400 | [diff] [blame] | 674 | case MID_SHUTDOWN: |
| 675 | rc = -EHOSTDOWN; |
| 676 | break; |
Jeff Layton | 74dd92a | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 677 | default: |
Jeff Layton | 3c1105d | 2011-05-22 07:09:13 -0400 | [diff] [blame] | 678 | list_del_init(&mid->qhead); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 679 | cifs_dbg(VFS, "%s: invalid mid state mid=%llu state=%d\n", |
| 680 | __func__, mid->mid, mid->mid_state); |
Jeff Layton | 74dd92a | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 681 | rc = -EIO; |
Jeff Layton | 053d503 | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 682 | } |
| 683 | spin_unlock(&GlobalMid_Lock); |
| 684 | |
Jeff Layton | 2b84a36c | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 685 | DeleteMidQEntry(mid); |
Jeff Layton | 053d503 | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 686 | return rc; |
| 687 | } |
| 688 | |
Jeff Layton | 121b046 | 2012-05-15 12:21:10 -0400 | [diff] [blame] | 689 | static inline int |
Pavel Shilovsky | fb2036d | 2016-11-23 15:08:14 -0800 | [diff] [blame] | 690 | send_cancel(struct TCP_Server_Info *server, struct smb_rqst *rqst, |
| 691 | struct mid_q_entry *mid) |
Jeff Layton | 76dcc26 | 2011-01-11 07:24:24 -0500 | [diff] [blame] | 692 | { |
Jeff Layton | 121b046 | 2012-05-15 12:21:10 -0400 | [diff] [blame] | 693 | return server->ops->send_cancel ? |
Pavel Shilovsky | fb2036d | 2016-11-23 15:08:14 -0800 | [diff] [blame] | 694 | server->ops->send_cancel(server, rqst, mid) : 0; |
Jeff Layton | 76dcc26 | 2011-01-11 07:24:24 -0500 | [diff] [blame] | 695 | } |
| 696 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | int |
Jeff Layton | 2c8f981 | 2011-05-19 16:22:52 -0400 | [diff] [blame] | 698 | cifs_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server, |
| 699 | bool log_error) |
| 700 | { |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 701 | unsigned int len = get_rfc1002_length(mid->resp_buf) + 4; |
Jeff Layton | 826a95e | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 702 | |
| 703 | dump_smb(mid->resp_buf, min_t(u32, 92, len)); |
Jeff Layton | 2c8f981 | 2011-05-19 16:22:52 -0400 | [diff] [blame] | 704 | |
| 705 | /* convert the length into a more usable form */ |
Jeff Layton | 38d77c5 | 2013-05-26 07:01:00 -0400 | [diff] [blame] | 706 | if (server->sign) { |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 707 | struct kvec iov[2]; |
Steve French | 985e4ff0 | 2012-08-03 09:42:45 -0500 | [diff] [blame] | 708 | int rc = 0; |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 709 | struct smb_rqst rqst = { .rq_iov = iov, |
| 710 | .rq_nvec = 2 }; |
Jeff Layton | 826a95e | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 711 | |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 712 | iov[0].iov_base = mid->resp_buf; |
| 713 | iov[0].iov_len = 4; |
| 714 | iov[1].iov_base = (char *)mid->resp_buf + 4; |
| 715 | iov[1].iov_len = len - 4; |
Jeff Layton | 2c8f981 | 2011-05-19 16:22:52 -0400 | [diff] [blame] | 716 | /* FIXME: add code to kill session */ |
Jeff Layton | bf5ea0e | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 717 | rc = cifs_verify_signature(&rqst, server, |
Jeff Layton | 0124cc4 | 2013-04-03 11:55:03 -0400 | [diff] [blame] | 718 | mid->sequence_number); |
Steve French | 985e4ff0 | 2012-08-03 09:42:45 -0500 | [diff] [blame] | 719 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 720 | cifs_dbg(VFS, "SMB signature verification returned error = %d\n", |
| 721 | rc); |
Jeff Layton | 2c8f981 | 2011-05-19 16:22:52 -0400 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | /* BB special case reconnect tid and uid here? */ |
| 725 | return map_smb_to_linux_error(mid->resp_buf, log_error); |
| 726 | } |
| 727 | |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 728 | struct mid_q_entry * |
| 729 | cifs_setup_request(struct cifs_ses *ses, struct smb_rqst *rqst) |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 730 | { |
| 731 | int rc; |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 732 | 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] | 733 | struct mid_q_entry *mid; |
| 734 | |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 735 | if (rqst->rq_iov[0].iov_len != 4 || |
| 736 | rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base) |
| 737 | return ERR_PTR(-EIO); |
| 738 | |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 739 | rc = allocate_mid(ses, hdr, &mid); |
| 740 | if (rc) |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 741 | return ERR_PTR(rc); |
| 742 | rc = cifs_sign_rqst(rqst, ses->server, &mid->sequence_number); |
| 743 | if (rc) { |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 744 | cifs_delete_mid(mid); |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 745 | return ERR_PTR(rc); |
| 746 | } |
| 747 | return mid; |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 748 | } |
| 749 | |
Pavel Shilovsky | b8f57ee | 2016-11-23 15:31:54 -0800 | [diff] [blame] | 750 | int |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 751 | cifs_send_recv(const unsigned int xid, struct cifs_ses *ses, |
| 752 | struct smb_rqst *rqst, int *resp_buf_type, const int flags, |
| 753 | struct kvec *resp_iov) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | { |
| 755 | int rc = 0; |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 756 | int timeout, optype; |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 757 | struct mid_q_entry *midQ; |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 758 | unsigned int credits = 1; |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 759 | char *buf; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 760 | |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 761 | timeout = flags & CIFS_TIMEOUT_MASK; |
| 762 | optype = flags & CIFS_OP_MASK; |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 763 | |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 764 | *resp_buf_type = CIFS_NO_BUFFER; /* no response buf yet */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | |
Steve French | 4b8f930 | 2006-02-26 16:41:18 +0000 | [diff] [blame] | 766 | if ((ses == NULL) || (ses->server == NULL)) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 767 | cifs_dbg(VFS, "Null session\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | return -EIO; |
| 769 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 771 | if (ses->server->tcpStatus == CifsExiting) |
Steve French | 31ca3bc | 2005-04-28 22:41:11 -0700 | [diff] [blame] | 772 | return -ENOENT; |
| 773 | |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 774 | /* |
| 775 | * Ensure that we do not send more than 50 overlapping requests |
| 776 | * to the same server. We may make this configurable later or |
| 777 | * use ses->maxReq. |
| 778 | */ |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 779 | rc = wait_for_free_request(ses->server, timeout, optype); |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 780 | if (rc) |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 781 | return rc; |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 782 | |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 783 | /* |
| 784 | * Make sure that we sign in the same order that we send on this socket |
| 785 | * and avoid races inside tcp sendmsg code that could cause corruption |
| 786 | * of smb data. |
| 787 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | |
Jeff Layton | 72ca545 | 2008-12-01 07:09:36 -0500 | [diff] [blame] | 789 | mutex_lock(&ses->server->srv_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 791 | midQ = ses->server->ops->setup_request(ses, rqst); |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 792 | if (IS_ERR(midQ)) { |
Jeff Layton | 72ca545 | 2008-12-01 07:09:36 -0500 | [diff] [blame] | 793 | mutex_unlock(&ses->server->srv_mutex); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 794 | /* Update # of requests on wire to server */ |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 795 | add_credits(ses->server, 1, optype); |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 796 | return PTR_ERR(midQ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 799 | midQ->mid_state = MID_REQUEST_SUBMITTED; |
Steve French | 789e666 | 2011-08-09 18:44:44 +0000 | [diff] [blame] | 800 | cifs_in_send_inc(ses->server); |
Pavel Shilovsky | 7fb8986 | 2016-10-31 13:49:30 -0700 | [diff] [blame] | 801 | rc = smb_send_rqst(ses->server, rqst, flags); |
Steve French | 789e666 | 2011-08-09 18:44:44 +0000 | [diff] [blame] | 802 | cifs_in_send_dec(ses->server); |
| 803 | cifs_save_when_sent(midQ); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 804 | |
Jeff Layton | ad313cb | 2013-04-03 10:27:36 -0400 | [diff] [blame] | 805 | if (rc < 0) |
| 806 | ses->server->sequence_number -= 2; |
Jeff Layton | 72ca545 | 2008-12-01 07:09:36 -0500 | [diff] [blame] | 807 | mutex_unlock(&ses->server->srv_mutex); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 808 | |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 809 | if (rc < 0) |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 810 | goto out; |
Steve French | 4b8f930 | 2006-02-26 16:41:18 +0000 | [diff] [blame] | 811 | |
Steve French | 0d5ec28 | 2018-04-22 19:51:22 -0500 | [diff] [blame] | 812 | if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP)) |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 813 | smb311_update_preauth_hash(ses, rqst->rq_iov, |
| 814 | rqst->rq_nvec); |
Aurelien Aptel | 8bd68c6 | 2018-02-16 19:19:29 +0100 | [diff] [blame] | 815 | |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 816 | if (timeout == CIFS_ASYNC_OP) |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 817 | goto out; |
Jeff Layton | 0ade640 | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 818 | |
| 819 | rc = wait_for_response(ses->server, midQ); |
Jeff Layton | 1be912d | 2011-01-28 07:08:28 -0500 | [diff] [blame] | 820 | if (rc != 0) { |
Sachin Prabhu | 38bd490 | 2017-03-03 15:41:38 -0800 | [diff] [blame] | 821 | cifs_dbg(FYI, "Cancelling wait for mid %llu\n", midQ->mid); |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 822 | send_cancel(ses->server, rqst, midQ); |
Jeff Layton | 1be912d | 2011-01-28 07:08:28 -0500 | [diff] [blame] | 823 | spin_lock(&GlobalMid_Lock); |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 824 | if (midQ->mid_state == MID_REQUEST_SUBMITTED) { |
Sachin Prabhu | 38bd490 | 2017-03-03 15:41:38 -0800 | [diff] [blame] | 825 | midQ->mid_flags |= MID_WAIT_CANCELLED; |
Jeff Layton | 1be912d | 2011-01-28 07:08:28 -0500 | [diff] [blame] | 826 | midQ->callback = DeleteMidQEntry; |
| 827 | spin_unlock(&GlobalMid_Lock); |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 828 | add_credits(ses->server, 1, optype); |
Jeff Layton | 1be912d | 2011-01-28 07:08:28 -0500 | [diff] [blame] | 829 | return rc; |
| 830 | } |
| 831 | spin_unlock(&GlobalMid_Lock); |
| 832 | } |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 833 | |
Jeff Layton | 3c1105d | 2011-05-22 07:09:13 -0400 | [diff] [blame] | 834 | rc = cifs_sync_mid_result(midQ, ses->server); |
Jeff Layton | 053d503 | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 835 | if (rc != 0) { |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 836 | add_credits(ses->server, 1, optype); |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 837 | return rc; |
| 838 | } |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 839 | |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 840 | if (!midQ->resp_buf || midQ->mid_state != MID_RESPONSE_RECEIVED) { |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 841 | rc = -EIO; |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 842 | cifs_dbg(FYI, "Bad MID state?\n"); |
Steve French | 2b2bdfb | 2008-12-11 17:26:54 +0000 | [diff] [blame] | 843 | goto out; |
| 844 | } |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 845 | |
Pavel Shilovsky | 792af7b | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 846 | buf = (char *)midQ->resp_buf; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 847 | resp_iov->iov_base = buf; |
Ronnie Sahlberg | e19b2bc | 2018-04-09 18:06:28 +1000 | [diff] [blame] | 848 | resp_iov->iov_len = midQ->resp_buf_size + |
Ronnie Sahlberg | 93012bf | 2018-03-31 11:45:31 +1100 | [diff] [blame] | 849 | ses->server->vals->header_preamble_size; |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 850 | if (midQ->large_buf) |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 851 | *resp_buf_type = CIFS_LARGE_BUFFER; |
Jeff Layton | 2c8f981 | 2011-05-19 16:22:52 -0400 | [diff] [blame] | 852 | else |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 853 | *resp_buf_type = CIFS_SMALL_BUFFER; |
| 854 | |
Steve French | 0d5ec28 | 2018-04-22 19:51:22 -0500 | [diff] [blame] | 855 | if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP)) { |
Aurelien Aptel | 8bd68c6 | 2018-02-16 19:19:29 +0100 | [diff] [blame] | 856 | struct kvec iov = { |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 857 | .iov_base = resp_iov->iov_base, |
| 858 | .iov_len = resp_iov->iov_len |
Aurelien Aptel | 8bd68c6 | 2018-02-16 19:19:29 +0100 | [diff] [blame] | 859 | }; |
| 860 | smb311_update_preauth_hash(ses, &iov, 1); |
| 861 | } |
Aurelien Aptel | 8bd68c6 | 2018-02-16 19:19:29 +0100 | [diff] [blame] | 862 | |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 863 | credits = ses->server->ops->get_credits(midQ); |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 864 | |
Pavel Shilovsky | 082d064 | 2012-05-17 12:18:21 +0400 | [diff] [blame] | 865 | rc = ses->server->ops->check_receive(midQ, ses->server, |
| 866 | flags & CIFS_LOG_ERROR); |
Steve French | 2b2bdfb | 2008-12-11 17:26:54 +0000 | [diff] [blame] | 867 | |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 868 | /* mark it so buf will not be freed by cifs_delete_mid */ |
Jeff Layton | 2c8f981 | 2011-05-19 16:22:52 -0400 | [diff] [blame] | 869 | if ((flags & CIFS_NO_RESP) == 0) |
| 870 | midQ->resp_buf = NULL; |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 871 | out: |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 872 | cifs_delete_mid(midQ); |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 873 | add_credits(ses->server, credits, optype); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | |
| 875 | return rc; |
| 876 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | |
| 878 | int |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 879 | SendReceive2(const unsigned int xid, struct cifs_ses *ses, |
| 880 | struct kvec *iov, int n_vec, int *resp_buf_type /* ret */, |
| 881 | const int flags, struct kvec *resp_iov) |
| 882 | { |
| 883 | struct smb_rqst rqst; |
Ronnie Sahlberg | 3cecf48 | 2017-11-21 15:08:07 +1100 | [diff] [blame] | 884 | struct kvec s_iov[CIFS_MAX_IOV_SIZE], *new_iov; |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 885 | int rc; |
| 886 | |
Ronnie Sahlberg | 3cecf48 | 2017-11-21 15:08:07 +1100 | [diff] [blame] | 887 | if (n_vec + 1 > CIFS_MAX_IOV_SIZE) { |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 888 | new_iov = kmalloc_array(n_vec + 1, sizeof(struct kvec), |
| 889 | GFP_KERNEL); |
Steve French | 117e3b7 | 2018-04-22 10:24:19 -0500 | [diff] [blame] | 890 | if (!new_iov) { |
| 891 | /* otherwise cifs_send_recv below sets resp_buf_type */ |
| 892 | *resp_buf_type = CIFS_NO_BUFFER; |
Ronnie Sahlberg | 3cecf48 | 2017-11-21 15:08:07 +1100 | [diff] [blame] | 893 | return -ENOMEM; |
Steve French | 117e3b7 | 2018-04-22 10:24:19 -0500 | [diff] [blame] | 894 | } |
Ronnie Sahlberg | 3cecf48 | 2017-11-21 15:08:07 +1100 | [diff] [blame] | 895 | } else |
| 896 | new_iov = s_iov; |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 897 | |
| 898 | /* 1st iov is a RFC1001 length followed by the rest of the packet */ |
| 899 | memcpy(new_iov + 1, iov, (sizeof(struct kvec) * n_vec)); |
| 900 | |
| 901 | new_iov[0].iov_base = new_iov[1].iov_base; |
| 902 | new_iov[0].iov_len = 4; |
| 903 | new_iov[1].iov_base += 4; |
| 904 | new_iov[1].iov_len -= 4; |
| 905 | |
| 906 | memset(&rqst, 0, sizeof(struct smb_rqst)); |
| 907 | rqst.rq_iov = new_iov; |
| 908 | rqst.rq_nvec = n_vec + 1; |
| 909 | |
| 910 | 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] | 911 | if (n_vec + 1 > CIFS_MAX_IOV_SIZE) |
| 912 | kfree(new_iov); |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 913 | return rc; |
| 914 | } |
| 915 | |
| 916 | int |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 917 | SendReceive(const unsigned int xid, struct cifs_ses *ses, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | struct smb_hdr *in_buf, struct smb_hdr *out_buf, |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 919 | int *pbytes_returned, const int timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | { |
| 921 | int rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | struct mid_q_entry *midQ; |
Pavel Shilovsky | fb2036d | 2016-11-23 15:08:14 -0800 | [diff] [blame] | 923 | unsigned int len = be32_to_cpu(in_buf->smb_buf_length); |
| 924 | struct kvec iov = { .iov_base = in_buf, .iov_len = len }; |
| 925 | struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | |
| 927 | if (ses == NULL) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 928 | cifs_dbg(VFS, "Null smb session\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | return -EIO; |
| 930 | } |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 931 | if (ses->server == NULL) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 932 | cifs_dbg(VFS, "Null tcp session\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | return -EIO; |
| 934 | } |
| 935 | |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 936 | if (ses->server->tcpStatus == CifsExiting) |
Steve French | 31ca3bc | 2005-04-28 22:41:11 -0700 | [diff] [blame] | 937 | return -ENOENT; |
| 938 | |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 939 | /* Ensure that we do not send more than 50 overlapping requests |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | to the same server. We may make this configurable later or |
| 941 | use ses->maxReq */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | |
Pavel Shilovsky | fb2036d | 2016-11-23 15:08:14 -0800 | [diff] [blame] | 943 | if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 944 | cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n", |
Pavel Shilovsky | fb2036d | 2016-11-23 15:08:14 -0800 | [diff] [blame] | 945 | len); |
Volker Lendecke | 6d9c6d5 | 2008-12-08 20:50:24 +0000 | [diff] [blame] | 946 | return -EIO; |
| 947 | } |
| 948 | |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 949 | rc = wait_for_free_request(ses->server, timeout, 0); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 950 | if (rc) |
| 951 | return rc; |
| 952 | |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 953 | /* 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] | 954 | and avoid races inside tcp sendmsg code that could cause corruption |
| 955 | of smb data */ |
| 956 | |
Jeff Layton | 72ca545 | 2008-12-01 07:09:36 -0500 | [diff] [blame] | 957 | mutex_lock(&ses->server->srv_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 959 | rc = allocate_mid(ses, in_buf, &midQ); |
| 960 | if (rc) { |
Jeff Layton | 72ca545 | 2008-12-01 07:09:36 -0500 | [diff] [blame] | 961 | mutex_unlock(&ses->server->srv_mutex); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 962 | /* Update # of requests on wire to server */ |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 963 | add_credits(ses->server, 1, 0); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 964 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | } |
| 966 | |
Steve French | ad009ac | 2005-04-28 22:41:05 -0700 | [diff] [blame] | 967 | rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number); |
Volker Lendecke | 829049c | 2008-12-06 16:00:53 +0100 | [diff] [blame] | 968 | if (rc) { |
| 969 | mutex_unlock(&ses->server->srv_mutex); |
| 970 | goto out; |
| 971 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 973 | midQ->mid_state = MID_REQUEST_SUBMITTED; |
Steve French | 789e666 | 2011-08-09 18:44:44 +0000 | [diff] [blame] | 974 | |
| 975 | cifs_in_send_inc(ses->server); |
Pavel Shilovsky | fb2036d | 2016-11-23 15:08:14 -0800 | [diff] [blame] | 976 | rc = smb_send(ses->server, in_buf, len); |
Steve French | 789e666 | 2011-08-09 18:44:44 +0000 | [diff] [blame] | 977 | cifs_in_send_dec(ses->server); |
| 978 | cifs_save_when_sent(midQ); |
Jeff Layton | ad313cb | 2013-04-03 10:27:36 -0400 | [diff] [blame] | 979 | |
| 980 | if (rc < 0) |
| 981 | ses->server->sequence_number -= 2; |
| 982 | |
Jeff Layton | 72ca545 | 2008-12-01 07:09:36 -0500 | [diff] [blame] | 983 | mutex_unlock(&ses->server->srv_mutex); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 984 | |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 985 | if (rc < 0) |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 986 | goto out; |
| 987 | |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 988 | if (timeout == CIFS_ASYNC_OP) |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 989 | goto out; |
Jeff Layton | 0ade640 | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 990 | |
| 991 | rc = wait_for_response(ses->server, midQ); |
Jeff Layton | 1be912d | 2011-01-28 07:08:28 -0500 | [diff] [blame] | 992 | if (rc != 0) { |
Pavel Shilovsky | fb2036d | 2016-11-23 15:08:14 -0800 | [diff] [blame] | 993 | send_cancel(ses->server, &rqst, midQ); |
Jeff Layton | 1be912d | 2011-01-28 07:08:28 -0500 | [diff] [blame] | 994 | spin_lock(&GlobalMid_Lock); |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 995 | if (midQ->mid_state == MID_REQUEST_SUBMITTED) { |
Jeff Layton | 1be912d | 2011-01-28 07:08:28 -0500 | [diff] [blame] | 996 | /* no longer considered to be "in-flight" */ |
| 997 | midQ->callback = DeleteMidQEntry; |
| 998 | spin_unlock(&GlobalMid_Lock); |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 999 | add_credits(ses->server, 1, 0); |
Jeff Layton | 1be912d | 2011-01-28 07:08:28 -0500 | [diff] [blame] | 1000 | return rc; |
| 1001 | } |
| 1002 | spin_unlock(&GlobalMid_Lock); |
| 1003 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | |
Jeff Layton | 3c1105d | 2011-05-22 07:09:13 -0400 | [diff] [blame] | 1005 | rc = cifs_sync_mid_result(midQ, ses->server); |
Jeff Layton | 053d503 | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 1006 | if (rc != 0) { |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 1007 | add_credits(ses->server, 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | return rc; |
| 1009 | } |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1010 | |
Jeff Layton | 2c8f981 | 2011-05-19 16:22:52 -0400 | [diff] [blame] | 1011 | if (!midQ->resp_buf || !out_buf || |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 1012 | midQ->mid_state != MID_RESPONSE_RECEIVED) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | rc = -EIO; |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1014 | cifs_dbg(VFS, "Bad MID state?\n"); |
Steve French | 2b2bdfb | 2008-12-11 17:26:54 +0000 | [diff] [blame] | 1015 | goto out; |
| 1016 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | |
Pavel Shilovsky | d4e4854 | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 1018 | *pbytes_returned = get_rfc1002_length(midQ->resp_buf); |
Jeff Layton | 2c8f981 | 2011-05-19 16:22:52 -0400 | [diff] [blame] | 1019 | memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4); |
| 1020 | rc = cifs_check_receive(midQ, ses->server, 0); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1021 | out: |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1022 | cifs_delete_mid(midQ); |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 1023 | add_credits(ses->server, 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | |
| 1025 | return rc; |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1026 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1028 | /* We send a LOCKINGX_CANCEL_LOCK to cause the Windows |
| 1029 | blocking lock to return. */ |
| 1030 | |
| 1031 | static int |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 1032 | send_lock_cancel(const unsigned int xid, struct cifs_tcon *tcon, |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1033 | struct smb_hdr *in_buf, |
| 1034 | struct smb_hdr *out_buf) |
| 1035 | { |
| 1036 | int bytes_returned; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 1037 | struct cifs_ses *ses = tcon->ses; |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1038 | LOCK_REQ *pSMB = (LOCK_REQ *)in_buf; |
| 1039 | |
| 1040 | /* We just modify the current in_buf to change |
| 1041 | the type of lock from LOCKING_ANDX_SHARED_LOCK |
| 1042 | or LOCKING_ANDX_EXCLUSIVE_LOCK to |
| 1043 | LOCKING_ANDX_CANCEL_LOCK. */ |
| 1044 | |
| 1045 | pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES; |
| 1046 | pSMB->Timeout = 0; |
Pavel Shilovsky | 8825736 | 2012-05-23 14:01:59 +0400 | [diff] [blame] | 1047 | pSMB->hdr.Mid = get_next_mid(ses->server); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1048 | |
| 1049 | return SendReceive(xid, ses, in_buf, out_buf, |
Jeff Layton | 7749981 | 2011-01-11 07:24:23 -0500 | [diff] [blame] | 1050 | &bytes_returned, 0); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | int |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 1054 | SendReceiveBlockingLock(const unsigned int xid, struct cifs_tcon *tcon, |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1055 | struct smb_hdr *in_buf, struct smb_hdr *out_buf, |
| 1056 | int *pbytes_returned) |
| 1057 | { |
| 1058 | int rc = 0; |
| 1059 | int rstart = 0; |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1060 | struct mid_q_entry *midQ; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 1061 | struct cifs_ses *ses; |
Pavel Shilovsky | fb2036d | 2016-11-23 15:08:14 -0800 | [diff] [blame] | 1062 | unsigned int len = be32_to_cpu(in_buf->smb_buf_length); |
| 1063 | struct kvec iov = { .iov_base = in_buf, .iov_len = len }; |
| 1064 | struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 }; |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1065 | |
| 1066 | if (tcon == NULL || tcon->ses == NULL) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1067 | cifs_dbg(VFS, "Null smb session\n"); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1068 | return -EIO; |
| 1069 | } |
| 1070 | ses = tcon->ses; |
| 1071 | |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 1072 | if (ses->server == NULL) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1073 | cifs_dbg(VFS, "Null tcp session\n"); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1074 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | } |
| 1076 | |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 1077 | if (ses->server->tcpStatus == CifsExiting) |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1078 | return -ENOENT; |
| 1079 | |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 1080 | /* Ensure that we do not send more than 50 overlapping requests |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1081 | to the same server. We may make this configurable later or |
| 1082 | use ses->maxReq */ |
| 1083 | |
Pavel Shilovsky | fb2036d | 2016-11-23 15:08:14 -0800 | [diff] [blame] | 1084 | if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1085 | cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n", |
Pavel Shilovsky | fb2036d | 2016-11-23 15:08:14 -0800 | [diff] [blame] | 1086 | len); |
Volker Lendecke | 6d9c6d5 | 2008-12-08 20:50:24 +0000 | [diff] [blame] | 1087 | return -EIO; |
| 1088 | } |
| 1089 | |
Pavel Shilovsky | a891f0f | 2012-05-23 16:14:34 +0400 | [diff] [blame] | 1090 | rc = wait_for_free_request(ses->server, CIFS_BLOCKING_OP, 0); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1091 | if (rc) |
| 1092 | return rc; |
| 1093 | |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 1094 | /* 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] | 1095 | and avoid races inside tcp sendmsg code that could cause corruption |
| 1096 | of smb data */ |
| 1097 | |
Jeff Layton | 72ca545 | 2008-12-01 07:09:36 -0500 | [diff] [blame] | 1098 | mutex_lock(&ses->server->srv_mutex); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1099 | |
| 1100 | rc = allocate_mid(ses, in_buf, &midQ); |
| 1101 | if (rc) { |
Jeff Layton | 72ca545 | 2008-12-01 07:09:36 -0500 | [diff] [blame] | 1102 | mutex_unlock(&ses->server->srv_mutex); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1103 | return rc; |
| 1104 | } |
| 1105 | |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1106 | rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number); |
Volker Lendecke | 829049c | 2008-12-06 16:00:53 +0100 | [diff] [blame] | 1107 | if (rc) { |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1108 | cifs_delete_mid(midQ); |
Volker Lendecke | 829049c | 2008-12-06 16:00:53 +0100 | [diff] [blame] | 1109 | mutex_unlock(&ses->server->srv_mutex); |
| 1110 | return rc; |
| 1111 | } |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1112 | |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 1113 | midQ->mid_state = MID_REQUEST_SUBMITTED; |
Steve French | 789e666 | 2011-08-09 18:44:44 +0000 | [diff] [blame] | 1114 | cifs_in_send_inc(ses->server); |
Pavel Shilovsky | fb2036d | 2016-11-23 15:08:14 -0800 | [diff] [blame] | 1115 | rc = smb_send(ses->server, in_buf, len); |
Steve French | 789e666 | 2011-08-09 18:44:44 +0000 | [diff] [blame] | 1116 | cifs_in_send_dec(ses->server); |
| 1117 | cifs_save_when_sent(midQ); |
Jeff Layton | ad313cb | 2013-04-03 10:27:36 -0400 | [diff] [blame] | 1118 | |
| 1119 | if (rc < 0) |
| 1120 | ses->server->sequence_number -= 2; |
| 1121 | |
Jeff Layton | 72ca545 | 2008-12-01 07:09:36 -0500 | [diff] [blame] | 1122 | mutex_unlock(&ses->server->srv_mutex); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1123 | |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 1124 | if (rc < 0) { |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1125 | cifs_delete_mid(midQ); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1126 | return rc; |
| 1127 | } |
| 1128 | |
| 1129 | /* Wait for a reply - allow signals to interrupt. */ |
| 1130 | rc = wait_event_interruptible(ses->server->response_q, |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 1131 | (!(midQ->mid_state == MID_REQUEST_SUBMITTED)) || |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1132 | ((ses->server->tcpStatus != CifsGood) && |
| 1133 | (ses->server->tcpStatus != CifsNew))); |
| 1134 | |
| 1135 | /* Were we interrupted by a signal ? */ |
| 1136 | if ((rc == -ERESTARTSYS) && |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 1137 | (midQ->mid_state == MID_REQUEST_SUBMITTED) && |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1138 | ((ses->server->tcpStatus == CifsGood) || |
| 1139 | (ses->server->tcpStatus == CifsNew))) { |
| 1140 | |
| 1141 | if (in_buf->Command == SMB_COM_TRANSACTION2) { |
| 1142 | /* POSIX lock. We send a NT_CANCEL SMB to cause the |
| 1143 | blocking lock to return. */ |
Pavel Shilovsky | fb2036d | 2016-11-23 15:08:14 -0800 | [diff] [blame] | 1144 | rc = send_cancel(ses->server, &rqst, midQ); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1145 | if (rc) { |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1146 | cifs_delete_mid(midQ); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1147 | return rc; |
| 1148 | } |
| 1149 | } else { |
| 1150 | /* Windows lock. We send a LOCKINGX_CANCEL_LOCK |
| 1151 | to cause the blocking lock to return. */ |
| 1152 | |
| 1153 | rc = send_lock_cancel(xid, tcon, in_buf, out_buf); |
| 1154 | |
| 1155 | /* If we get -ENOLCK back the lock may have |
| 1156 | already been removed. Don't exit in this case. */ |
| 1157 | if (rc && rc != -ENOLCK) { |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1158 | cifs_delete_mid(midQ); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1159 | return rc; |
| 1160 | } |
| 1161 | } |
| 1162 | |
Jeff Layton | 1be912d | 2011-01-28 07:08:28 -0500 | [diff] [blame] | 1163 | rc = wait_for_response(ses->server, midQ); |
| 1164 | if (rc) { |
Pavel Shilovsky | fb2036d | 2016-11-23 15:08:14 -0800 | [diff] [blame] | 1165 | send_cancel(ses->server, &rqst, midQ); |
Jeff Layton | 1be912d | 2011-01-28 07:08:28 -0500 | [diff] [blame] | 1166 | spin_lock(&GlobalMid_Lock); |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 1167 | if (midQ->mid_state == MID_REQUEST_SUBMITTED) { |
Jeff Layton | 1be912d | 2011-01-28 07:08:28 -0500 | [diff] [blame] | 1168 | /* no longer considered to be "in-flight" */ |
| 1169 | midQ->callback = DeleteMidQEntry; |
| 1170 | spin_unlock(&GlobalMid_Lock); |
| 1171 | return rc; |
| 1172 | } |
| 1173 | spin_unlock(&GlobalMid_Lock); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1174 | } |
Jeff Layton | 1be912d | 2011-01-28 07:08:28 -0500 | [diff] [blame] | 1175 | |
| 1176 | /* We got the response - restart system call. */ |
| 1177 | rstart = 1; |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
Jeff Layton | 3c1105d | 2011-05-22 07:09:13 -0400 | [diff] [blame] | 1180 | rc = cifs_sync_mid_result(midQ, ses->server); |
Jeff Layton | 053d503 | 2011-01-11 07:24:02 -0500 | [diff] [blame] | 1181 | if (rc != 0) |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1182 | return rc; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1183 | |
Volker Lendecke | 17c8bfe | 2008-12-06 16:38:19 +0100 | [diff] [blame] | 1184 | /* rcvd frame is ok */ |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 1185 | if (out_buf == NULL || midQ->mid_state != MID_RESPONSE_RECEIVED) { |
Volker Lendecke | 17c8bfe | 2008-12-06 16:38:19 +0100 | [diff] [blame] | 1186 | rc = -EIO; |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1187 | cifs_dbg(VFS, "Bad MID state?\n"); |
Volker Lendecke | 698e96a | 2008-12-06 16:39:31 +0100 | [diff] [blame] | 1188 | goto out; |
Volker Lendecke | 17c8bfe | 2008-12-06 16:38:19 +0100 | [diff] [blame] | 1189 | } |
| 1190 | |
Pavel Shilovsky | d4e4854 | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 1191 | *pbytes_returned = get_rfc1002_length(midQ->resp_buf); |
Jeff Layton | 2c8f981 | 2011-05-19 16:22:52 -0400 | [diff] [blame] | 1192 | memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4); |
| 1193 | rc = cifs_check_receive(midQ, ses->server, 0); |
Volker Lendecke | 17c8bfe | 2008-12-06 16:38:19 +0100 | [diff] [blame] | 1194 | out: |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1195 | cifs_delete_mid(midQ); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1196 | if (rstart && rc == -EACCES) |
| 1197 | return -ERESTARTSYS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | return rc; |
| 1199 | } |