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