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