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