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> |
| 25 | #include <linux/wait.h> |
| 26 | #include <linux/net.h> |
| 27 | #include <linux/delay.h> |
| 28 | #include <asm/uaccess.h> |
| 29 | #include <asm/processor.h> |
| 30 | #include <linux/mempool.h> |
| 31 | #include "cifspdu.h" |
| 32 | #include "cifsglob.h" |
| 33 | #include "cifsproto.h" |
| 34 | #include "cifs_debug.h" |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | extern mempool_t *cifs_mid_poolp; |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 37 | extern struct kmem_cache *cifs_oplock_cachep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | static struct mid_q_entry * |
Jeff Layton | 24b9b06 | 2008-12-01 07:09:34 -0500 | [diff] [blame] | 40 | AllocMidQEntry(const struct smb_hdr *smb_buffer, struct TCP_Server_Info *server) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | { |
| 42 | struct mid_q_entry *temp; |
| 43 | |
Jeff Layton | 24b9b06 | 2008-12-01 07:09:34 -0500 | [diff] [blame] | 44 | if (server == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | cERROR(1, ("Null TCP session in AllocMidQEntry")); |
| 46 | return NULL; |
| 47 | } |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 48 | |
Pekka Enberg | 232087c | 2008-09-15 13:22:54 +0300 | [diff] [blame] | 49 | temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | if (temp == NULL) |
| 51 | return temp; |
| 52 | else { |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 53 | memset(temp, 0, sizeof(struct mid_q_entry)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | temp->mid = smb_buffer->Mid; /* always LE */ |
| 55 | temp->pid = current->pid; |
| 56 | temp->command = smb_buffer->Command; |
| 57 | cFYI(1, ("For smb_command %d", temp->command)); |
Steve French | 1047abc | 2005-10-11 19:58:06 -0700 | [diff] [blame] | 58 | /* do_gettimeofday(&temp->when_sent);*/ /* easier to use jiffies */ |
| 59 | /* when mid allocated can be before when sent */ |
| 60 | temp->when_alloc = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | temp->tsk = current; |
| 62 | } |
| 63 | |
| 64 | spin_lock(&GlobalMid_Lock); |
Jeff Layton | 24b9b06 | 2008-12-01 07:09:34 -0500 | [diff] [blame] | 65 | list_add_tail(&temp->qhead, &server->pending_mid_q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | atomic_inc(&midCount); |
| 67 | temp->midState = MID_REQUEST_ALLOCATED; |
| 68 | spin_unlock(&GlobalMid_Lock); |
| 69 | return temp; |
| 70 | } |
| 71 | |
| 72 | static void |
| 73 | DeleteMidQEntry(struct mid_q_entry *midEntry) |
| 74 | { |
Steve French | 1047abc | 2005-10-11 19:58:06 -0700 | [diff] [blame] | 75 | #ifdef CONFIG_CIFS_STATS2 |
| 76 | unsigned long now; |
| 77 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | spin_lock(&GlobalMid_Lock); |
| 79 | midEntry->midState = MID_FREE; |
| 80 | list_del(&midEntry->qhead); |
| 81 | atomic_dec(&midCount); |
| 82 | spin_unlock(&GlobalMid_Lock); |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 83 | if (midEntry->largeBuf) |
Steve French | b8643e1 | 2005-04-28 22:41:07 -0700 | [diff] [blame] | 84 | cifs_buf_release(midEntry->resp_buf); |
| 85 | else |
| 86 | cifs_small_buf_release(midEntry->resp_buf); |
Steve French | 1047abc | 2005-10-11 19:58:06 -0700 | [diff] [blame] | 87 | #ifdef CONFIG_CIFS_STATS2 |
| 88 | now = jiffies; |
| 89 | /* commands taking longer than one second are indications that |
| 90 | something is wrong, unless it is quite a slow link or server */ |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 91 | if ((now - midEntry->when_alloc) > HZ) { |
| 92 | if ((cifsFYI & CIFS_TIMER) && |
Steve French | 1047abc | 2005-10-11 19:58:06 -0700 | [diff] [blame] | 93 | (midEntry->command != SMB_COM_LOCKING_ANDX)) { |
| 94 | printk(KERN_DEBUG " CIFS slow rsp: cmd %d mid %d", |
| 95 | midEntry->command, midEntry->mid); |
| 96 | printk(" A: 0x%lx S: 0x%lx R: 0x%lx\n", |
| 97 | now - midEntry->when_alloc, |
| 98 | now - midEntry->when_sent, |
| 99 | now - midEntry->when_received); |
| 100 | } |
| 101 | } |
| 102 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | mempool_free(midEntry, cifs_mid_poolp); |
| 104 | } |
| 105 | |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 106 | static int |
Jeff Layton | 0496e02 | 2008-12-30 12:39:16 -0500 | [diff] [blame] | 107 | smb_sendv(struct TCP_Server_Info *server, struct kvec *iov, int n_vec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | { |
| 109 | int rc = 0; |
| 110 | int i = 0; |
| 111 | struct msghdr smb_msg; |
Steve French | 3e84469 | 2005-10-03 13:37:24 -0700 | [diff] [blame] | 112 | struct smb_hdr *smb_buffer = iov[0].iov_base; |
| 113 | unsigned int len = iov[0].iov_len; |
| 114 | unsigned int total_len; |
| 115 | int first_vec = 0; |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 116 | unsigned int smb_buf_length = smb_buffer->smb_buf_length; |
Steve French | edf1ae4 | 2008-10-29 00:47:57 +0000 | [diff] [blame] | 117 | struct socket *ssocket = server->ssocket; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 118 | |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 119 | if (ssocket == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | return -ENOTSOCK; /* BB eventually add reconnect code here */ |
Steve French | 3e84469 | 2005-10-03 13:37:24 -0700 | [diff] [blame] | 121 | |
Jeff Layton | 0496e02 | 2008-12-30 12:39:16 -0500 | [diff] [blame] | 122 | smb_msg.msg_name = (struct sockaddr *) &server->addr.sockAddr; |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 123 | smb_msg.msg_namelen = sizeof(struct sockaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | smb_msg.msg_control = NULL; |
| 125 | smb_msg.msg_controllen = 0; |
Jeff Layton | 0496e02 | 2008-12-30 12:39:16 -0500 | [diff] [blame] | 126 | if (server->noblocksnd) |
Steve French | edf1ae4 | 2008-10-29 00:47:57 +0000 | [diff] [blame] | 127 | smb_msg.msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL; |
| 128 | else |
| 129 | smb_msg.msg_flags = MSG_NOSIGNAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
| 131 | /* smb header is converted in header_assemble. bcc and rest of SMB word |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 132 | area, and byte area if necessary, is converted to littleendian in |
| 133 | cifssmb.c and RFC1001 len is converted to bigendian in smb_send |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | Flags2 is converted in SendReceive */ |
| 135 | |
Steve French | 3e84469 | 2005-10-03 13:37:24 -0700 | [diff] [blame] | 136 | |
| 137 | total_len = 0; |
| 138 | for (i = 0; i < n_vec; i++) |
| 139 | total_len += iov[i].iov_len; |
| 140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | smb_buffer->smb_buf_length = cpu_to_be32(smb_buffer->smb_buf_length); |
Steve French | 3e84469 | 2005-10-03 13:37:24 -0700 | [diff] [blame] | 142 | cFYI(1, ("Sending smb: total_len %d", total_len)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | dump_smb(smb_buffer, len); |
| 144 | |
Shirish Pargaonkar | 1768035 | 2008-07-29 21:26:13 +0000 | [diff] [blame] | 145 | i = 0; |
Steve French | 3e84469 | 2005-10-03 13:37:24 -0700 | [diff] [blame] | 146 | while (total_len) { |
| 147 | rc = kernel_sendmsg(ssocket, &smb_msg, &iov[first_vec], |
| 148 | n_vec - first_vec, total_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | if ((rc == -ENOSPC) || (rc == -EAGAIN)) { |
| 150 | i++; |
Steve French | da505c3 | 2009-01-19 03:49:35 +0000 | [diff] [blame] | 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 |
| 157 | a response from the server in SendReceive[2] |
| 158 | for the server to send a response back for |
| 159 | most types of requests (except SMB Write |
| 160 | past end of file which can be slow, and |
| 161 | blocking lock operations). NFS waits slightly longer |
| 162 | than CIFS, but this can make it take longer for |
| 163 | nonresponsive servers to be detected and 15 seconds |
| 164 | is more than enough time for modern networks to |
| 165 | send a packet. In most cases if we fail to send |
| 166 | after the retries we will kill the socket and |
| 167 | reconnect which may clear the network problem. |
| 168 | */ |
| 169 | if ((i >= 14) || (!server->noblocksnd && (i > 2))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | cERROR(1, |
Steve French | 68058e7 | 2005-10-10 10:34:22 -0700 | [diff] [blame] | 171 | ("sends on sock %p stuck for 15 seconds", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | ssocket)); |
| 173 | rc = -EAGAIN; |
| 174 | break; |
| 175 | } |
Steve French | 68058e7 | 2005-10-10 10:34:22 -0700 | [diff] [blame] | 176 | msleep(1 << i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | continue; |
| 178 | } |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 179 | if (rc < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | break; |
Steve French | 3e84469 | 2005-10-03 13:37:24 -0700 | [diff] [blame] | 181 | |
Steve French | 61de800 | 2008-10-30 20:15:22 +0000 | [diff] [blame] | 182 | if (rc == total_len) { |
| 183 | total_len = 0; |
| 184 | break; |
| 185 | } else if (rc > total_len) { |
| 186 | cERROR(1, ("sent %d requested %d", rc, total_len)); |
Steve French | 3e84469 | 2005-10-03 13:37:24 -0700 | [diff] [blame] | 187 | break; |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [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 */ |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 192 | cERROR(1, ("tcp sent no data")); |
Steve French | 3e84469 | 2005-10-03 13:37:24 -0700 | [diff] [blame] | 193 | msleep(500); |
| 194 | continue; |
| 195 | } |
| 196 | total_len -= rc; |
Steve French | 68058e7 | 2005-10-10 10:34:22 -0700 | [diff] [blame] | 197 | /* the line below resets i */ |
Steve French | 3e84469 | 2005-10-03 13:37:24 -0700 | [diff] [blame] | 198 | for (i = first_vec; i < n_vec; i++) { |
| 199 | if (iov[i].iov_len) { |
| 200 | if (rc > iov[i].iov_len) { |
| 201 | rc -= iov[i].iov_len; |
| 202 | iov[i].iov_len = 0; |
| 203 | } else { |
| 204 | iov[i].iov_base += rc; |
| 205 | iov[i].iov_len -= rc; |
| 206 | first_vec = i; |
| 207 | break; |
| 208 | } |
| 209 | } |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 210 | } |
Steve French | 5e1253b | 2005-10-10 14:06:37 -0700 | [diff] [blame] | 211 | i = 0; /* in case we get ENOSPC on the next send */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Steve French | edf1ae4 | 2008-10-29 00:47:57 +0000 | [diff] [blame] | 214 | if ((total_len > 0) && (total_len != smb_buf_length + 4)) { |
| 215 | cFYI(1, ("partial send (%d remaining), terminating session", |
| 216 | total_len)); |
| 217 | /* If we have only sent part of an SMB then the next SMB |
| 218 | could be taken as the remainder of this one. We need |
| 219 | to kill the socket so the server throws away the partial |
| 220 | SMB */ |
| 221 | server->tcpStatus = CifsNeedReconnect; |
| 222 | } |
| 223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | if (rc < 0) { |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 225 | cERROR(1, ("Error %d sending data on socket to server", rc)); |
Steve French | 3e84469 | 2005-10-03 13:37:24 -0700 | [diff] [blame] | 226 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 229 | /* Don't want to modify the buffer as a |
| 230 | side effect of this call. */ |
| 231 | smb_buffer->smb_buf_length = smb_buf_length; |
| 232 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | return rc; |
| 234 | } |
| 235 | |
Jeff Layton | 0496e02 | 2008-12-30 12:39:16 -0500 | [diff] [blame] | 236 | int |
| 237 | smb_send(struct TCP_Server_Info *server, struct smb_hdr *smb_buffer, |
| 238 | unsigned int smb_buf_length) |
| 239 | { |
| 240 | struct kvec iov; |
| 241 | |
| 242 | iov.iov_base = smb_buffer; |
| 243 | iov.iov_len = smb_buf_length + 4; |
| 244 | |
| 245 | return smb_sendv(server, &iov, 1); |
| 246 | } |
| 247 | |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 248 | static int wait_for_free_request(struct cifsSesInfo *ses, const int long_op) |
| 249 | { |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 250 | if (long_op == CIFS_ASYNC_OP) { |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 251 | /* oplock breaks must not be held up */ |
| 252 | atomic_inc(&ses->server->inFlight); |
Volker Lendecke | 27a97a6 | 2008-12-08 20:59:39 +0000 | [diff] [blame] | 253 | return 0; |
| 254 | } |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 255 | |
Volker Lendecke | 27a97a6 | 2008-12-08 20:59:39 +0000 | [diff] [blame] | 256 | spin_lock(&GlobalMid_Lock); |
| 257 | while (1) { |
| 258 | if (atomic_read(&ses->server->inFlight) >= |
| 259 | cifs_max_pending){ |
| 260 | spin_unlock(&GlobalMid_Lock); |
| 261 | #ifdef CONFIG_CIFS_STATS2 |
| 262 | atomic_inc(&ses->server->num_waiters); |
| 263 | #endif |
| 264 | wait_event(ses->server->request_q, |
| 265 | atomic_read(&ses->server->inFlight) |
| 266 | < cifs_max_pending); |
| 267 | #ifdef CONFIG_CIFS_STATS2 |
| 268 | atomic_dec(&ses->server->num_waiters); |
| 269 | #endif |
| 270 | spin_lock(&GlobalMid_Lock); |
| 271 | } else { |
| 272 | if (ses->server->tcpStatus == CifsExiting) { |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 273 | spin_unlock(&GlobalMid_Lock); |
Volker Lendecke | 27a97a6 | 2008-12-08 20:59:39 +0000 | [diff] [blame] | 274 | return -ENOENT; |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 275 | } |
Volker Lendecke | 27a97a6 | 2008-12-08 20:59:39 +0000 | [diff] [blame] | 276 | |
| 277 | /* can not count locking commands against total |
| 278 | as they are allowed to block on server */ |
| 279 | |
| 280 | /* update # of requests on the wire to server */ |
| 281 | if (long_op != CIFS_BLOCKING_OP) |
| 282 | atomic_inc(&ses->server->inFlight); |
| 283 | spin_unlock(&GlobalMid_Lock); |
| 284 | break; |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | static int allocate_mid(struct cifsSesInfo *ses, struct smb_hdr *in_buf, |
| 291 | struct mid_q_entry **ppmidQ) |
| 292 | { |
| 293 | if (ses->server->tcpStatus == CifsExiting) { |
| 294 | return -ENOENT; |
Volker Lendecke | 8fbbd36 | 2008-12-06 13:12:34 +0100 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | if (ses->server->tcpStatus == CifsNeedReconnect) { |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 298 | cFYI(1, ("tcp session dead - return to caller to retry")); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 299 | return -EAGAIN; |
Volker Lendecke | 8fbbd36 | 2008-12-06 13:12:34 +0100 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | if (ses->status != CifsGood) { |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 303 | /* check if SMB session is bad because we are setting it up */ |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 304 | if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) && |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 305 | (in_buf->Command != SMB_COM_NEGOTIATE)) |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 306 | return -EAGAIN; |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 307 | /* else ok - we are setting up session */ |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 308 | } |
Jeff Layton | 24b9b06 | 2008-12-01 07:09:34 -0500 | [diff] [blame] | 309 | *ppmidQ = AllocMidQEntry(in_buf, ses->server); |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 310 | if (*ppmidQ == NULL) |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 311 | return -ENOMEM; |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 312 | return 0; |
| 313 | } |
| 314 | |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 315 | static int wait_for_response(struct cifsSesInfo *ses, |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 316 | struct mid_q_entry *midQ, |
| 317 | unsigned long timeout, |
| 318 | unsigned long time_to_wait) |
| 319 | { |
| 320 | unsigned long curr_timeout; |
| 321 | |
| 322 | for (;;) { |
| 323 | curr_timeout = timeout + jiffies; |
Jeff Layton | 8570552 | 2008-12-05 20:41:21 -0500 | [diff] [blame] | 324 | wait_event_timeout(ses->server->response_q, |
| 325 | midQ->midState != MID_REQUEST_SUBMITTED, timeout); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 326 | |
| 327 | if (time_after(jiffies, curr_timeout) && |
| 328 | (midQ->midState == MID_REQUEST_SUBMITTED) && |
| 329 | ((ses->server->tcpStatus == CifsGood) || |
| 330 | (ses->server->tcpStatus == CifsNew))) { |
| 331 | |
| 332 | unsigned long lrt; |
| 333 | |
| 334 | /* We timed out. Is the server still |
| 335 | sending replies ? */ |
| 336 | spin_lock(&GlobalMid_Lock); |
| 337 | lrt = ses->server->lstrp; |
| 338 | spin_unlock(&GlobalMid_Lock); |
| 339 | |
| 340 | /* Calculate time_to_wait past last receive time. |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 341 | Although we prefer not to time out if the |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 342 | server is still responding - we will time |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 343 | out if the server takes more than 15 (or 45 |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 344 | or 180) seconds to respond to this request |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 345 | and has not responded to any request from |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 346 | other threads on the client within 10 seconds */ |
| 347 | lrt += time_to_wait; |
| 348 | if (time_after(jiffies, lrt)) { |
| 349 | /* No replies for time_to_wait. */ |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 350 | cERROR(1, ("server not responding")); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 351 | return -1; |
| 352 | } |
| 353 | } else { |
| 354 | return 0; |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 359 | |
| 360 | /* |
| 361 | * |
| 362 | * Send an SMB Request. No response info (other than return code) |
| 363 | * needs to be parsed. |
| 364 | * |
| 365 | * flags indicate the type of request buffer and how long to wait |
| 366 | * and whether to log NT STATUS code (error) before mapping it to POSIX error |
| 367 | * |
| 368 | */ |
| 369 | int |
| 370 | SendReceiveNoRsp(const unsigned int xid, struct cifsSesInfo *ses, |
| 371 | struct smb_hdr *in_buf, int flags) |
| 372 | { |
| 373 | int rc; |
| 374 | struct kvec iov[1]; |
| 375 | int resp_buf_type; |
| 376 | |
| 377 | iov[0].iov_base = (char *)in_buf; |
| 378 | iov[0].iov_len = in_buf->smb_buf_length + 4; |
| 379 | flags |= CIFS_NO_RESP; |
| 380 | rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags); |
Steve French | 90c81e0 | 2008-02-12 20:32:36 +0000 | [diff] [blame] | 381 | cFYI(DBG2, ("SendRcvNoRsp flags %d rc %d", flags, rc)); |
| 382 | |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 383 | return rc; |
| 384 | } |
| 385 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | int |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 387 | SendReceive2(const unsigned int xid, struct cifsSesInfo *ses, |
| 388 | struct kvec *iov, int n_vec, int *pRespBufType /* ret */, |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 389 | const int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | { |
| 391 | int rc = 0; |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 392 | int long_op; |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 393 | unsigned int receive_len; |
| 394 | unsigned long timeout; |
| 395 | struct mid_q_entry *midQ; |
Steve French | 3e84469 | 2005-10-03 13:37:24 -0700 | [diff] [blame] | 396 | struct smb_hdr *in_buf = iov[0].iov_base; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 397 | |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 398 | long_op = flags & CIFS_TIMEOUT_MASK; |
| 399 | |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 400 | *pRespBufType = CIFS_NO_BUFFER; /* no response buf yet */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | |
Steve French | 4b8f930 | 2006-02-26 16:41:18 +0000 | [diff] [blame] | 402 | if ((ses == NULL) || (ses->server == NULL)) { |
| 403 | cifs_small_buf_release(in_buf); |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 404 | cERROR(1, ("Null session")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | return -EIO; |
| 406 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 408 | if (ses->server->tcpStatus == CifsExiting) { |
Steve French | 4b8f930 | 2006-02-26 16:41:18 +0000 | [diff] [blame] | 409 | cifs_small_buf_release(in_buf); |
Steve French | 31ca3bc | 2005-04-28 22:41:11 -0700 | [diff] [blame] | 410 | return -ENOENT; |
Steve French | 4b8f930 | 2006-02-26 16:41:18 +0000 | [diff] [blame] | 411 | } |
Steve French | 31ca3bc | 2005-04-28 22:41:11 -0700 | [diff] [blame] | 412 | |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 413 | /* Ensure that we do not send more than 50 overlapping requests |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | to the same server. We may make this configurable later or |
| 415 | use ses->maxReq */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 417 | rc = wait_for_free_request(ses, long_op); |
| 418 | if (rc) { |
| 419 | cifs_small_buf_release(in_buf); |
| 420 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | } |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 422 | |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 423 | /* 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] | 424 | and avoid races inside tcp sendmsg code that could cause corruption |
| 425 | of smb data */ |
| 426 | |
Jeff Layton | 72ca545 | 2008-12-01 07:09:36 -0500 | [diff] [blame] | 427 | mutex_lock(&ses->server->srv_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 429 | rc = allocate_mid(ses, in_buf, &midQ); |
| 430 | if (rc) { |
Jeff Layton | 72ca545 | 2008-12-01 07:09:36 -0500 | [diff] [blame] | 431 | mutex_unlock(&ses->server->srv_mutex); |
Steve French | 4b8f930 | 2006-02-26 16:41:18 +0000 | [diff] [blame] | 432 | cifs_small_buf_release(in_buf); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 433 | /* Update # of requests on wire to server */ |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 434 | atomic_dec(&ses->server->inFlight); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 435 | wake_up(&ses->server->request_q); |
| 436 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | } |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 438 | rc = cifs_sign_smb2(iov, n_vec, ses->server, &midQ->sequence_number); |
Volker Lendecke | 829049c | 2008-12-06 16:00:53 +0100 | [diff] [blame] | 439 | if (rc) { |
| 440 | mutex_unlock(&ses->server->srv_mutex); |
| 441 | cifs_small_buf_release(in_buf); |
| 442 | goto out; |
| 443 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | |
| 445 | midQ->midState = MID_REQUEST_SUBMITTED; |
Steve French | 131afd0b | 2005-10-07 09:51:05 -0700 | [diff] [blame] | 446 | #ifdef CONFIG_CIFS_STATS2 |
| 447 | atomic_inc(&ses->server->inSend); |
| 448 | #endif |
Jeff Layton | 0496e02 | 2008-12-30 12:39:16 -0500 | [diff] [blame] | 449 | rc = smb_sendv(ses->server, iov, n_vec); |
Steve French | 131afd0b | 2005-10-07 09:51:05 -0700 | [diff] [blame] | 450 | #ifdef CONFIG_CIFS_STATS2 |
| 451 | atomic_dec(&ses->server->inSend); |
Steve French | 1047abc | 2005-10-11 19:58:06 -0700 | [diff] [blame] | 452 | midQ->when_sent = jiffies; |
Steve French | 131afd0b | 2005-10-07 09:51:05 -0700 | [diff] [blame] | 453 | #endif |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 454 | |
Jeff Layton | 72ca545 | 2008-12-01 07:09:36 -0500 | [diff] [blame] | 455 | mutex_unlock(&ses->server->srv_mutex); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 456 | cifs_small_buf_release(in_buf); |
| 457 | |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 458 | if (rc < 0) |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 459 | goto out; |
Steve French | 4b8f930 | 2006-02-26 16:41:18 +0000 | [diff] [blame] | 460 | |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 461 | if (long_op == CIFS_STD_OP) |
| 462 | timeout = 15 * HZ; |
| 463 | else if (long_op == CIFS_VLONG_OP) /* e.g. slow writes past EOF */ |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 464 | timeout = 180 * HZ; |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 465 | else if (long_op == CIFS_LONG_OP) |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 466 | timeout = 45 * HZ; /* should be greater than |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 467 | servers oplock break timeout (about 43 seconds) */ |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 468 | else if (long_op == CIFS_ASYNC_OP) |
| 469 | goto out; |
| 470 | else if (long_op == CIFS_BLOCKING_OP) |
| 471 | timeout = 0x7FFFFFFF; /* large, but not so large as to wrap */ |
| 472 | else { |
| 473 | cERROR(1, ("unknown timeout flag %d", long_op)); |
| 474 | rc = -EIO; |
| 475 | goto out; |
| 476 | } |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 477 | |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 478 | /* wait for 15 seconds or until woken up due to response arriving or |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 479 | due to last connection to this server being unmounted */ |
| 480 | if (signal_pending(current)) { |
| 481 | /* if signal pending do not hold up user for full smb timeout |
Steve French | 8a23626 | 2007-03-06 00:31:00 +0000 | [diff] [blame] | 482 | but we still give response a chance to complete */ |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 483 | timeout = 2 * HZ; |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 484 | } |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 485 | |
| 486 | /* No user interrupts in wait - wreaks havoc with performance */ |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 487 | wait_for_response(ses, midQ, timeout, 10 * HZ); |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 488 | |
| 489 | spin_lock(&GlobalMid_Lock); |
Volker Lendecke | 8e4f2e8 | 2008-12-06 16:22:15 +0100 | [diff] [blame] | 490 | |
| 491 | if (midQ->resp_buf == NULL) { |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 492 | cERROR(1, ("No response to cmd %d mid %d", |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 493 | midQ->command, midQ->mid)); |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 494 | if (midQ->midState == MID_REQUEST_SUBMITTED) { |
| 495 | if (ses->server->tcpStatus == CifsExiting) |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 496 | rc = -EHOSTDOWN; |
| 497 | else { |
| 498 | ses->server->tcpStatus = CifsNeedReconnect; |
| 499 | midQ->midState = MID_RETRY_NEEDED; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | if (rc != -EHOSTDOWN) { |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 504 | if (midQ->midState == MID_RETRY_NEEDED) { |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 505 | rc = -EAGAIN; |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 506 | cFYI(1, ("marking request for retry")); |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 507 | } else { |
| 508 | rc = -EIO; |
| 509 | } |
| 510 | } |
| 511 | spin_unlock(&GlobalMid_Lock); |
| 512 | DeleteMidQEntry(midQ); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 513 | /* Update # of requests on wire to server */ |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 514 | atomic_dec(&ses->server->inFlight); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 515 | wake_up(&ses->server->request_q); |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 516 | return rc; |
| 517 | } |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 518 | |
Volker Lendecke | 8e4f2e8 | 2008-12-06 16:22:15 +0100 | [diff] [blame] | 519 | spin_unlock(&GlobalMid_Lock); |
| 520 | receive_len = midQ->resp_buf->smb_buf_length; |
| 521 | |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 522 | if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) { |
| 523 | cERROR(1, ("Frame too large received. Length: %d Xid: %d", |
| 524 | receive_len, xid)); |
| 525 | rc = -EIO; |
Steve French | 2b2bdfb | 2008-12-11 17:26:54 +0000 | [diff] [blame] | 526 | goto out; |
| 527 | } |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 528 | |
Steve French | 2b2bdfb | 2008-12-11 17:26:54 +0000 | [diff] [blame] | 529 | /* rcvd frame is ok */ |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 530 | |
Steve French | 2b2bdfb | 2008-12-11 17:26:54 +0000 | [diff] [blame] | 531 | if (midQ->resp_buf && |
| 532 | (midQ->midState == MID_RESPONSE_RECEIVED)) { |
| 533 | |
| 534 | iov[0].iov_base = (char *)midQ->resp_buf; |
| 535 | if (midQ->largeBuf) |
| 536 | *pRespBufType = CIFS_LARGE_BUFFER; |
| 537 | else |
| 538 | *pRespBufType = CIFS_SMALL_BUFFER; |
| 539 | iov[0].iov_len = receive_len + 4; |
| 540 | |
| 541 | dump_smb(midQ->resp_buf, 80); |
| 542 | /* convert the length into a more usable form */ |
| 543 | if ((receive_len > 24) && |
| 544 | (ses->server->secMode & (SECMODE_SIGN_REQUIRED | |
| 545 | SECMODE_SIGN_ENABLED))) { |
| 546 | rc = cifs_verify_signature(midQ->resp_buf, |
Steve French | b609f06 | 2007-07-09 07:55:14 +0000 | [diff] [blame] | 547 | &ses->server->mac_signing_key, |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 548 | midQ->sequence_number+1); |
Steve French | 2b2bdfb | 2008-12-11 17:26:54 +0000 | [diff] [blame] | 549 | if (rc) { |
| 550 | cERROR(1, ("Unexpected SMB signature")); |
| 551 | /* BB FIXME add code to kill session */ |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 552 | } |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 553 | } |
Steve French | 2b2bdfb | 2008-12-11 17:26:54 +0000 | [diff] [blame] | 554 | |
| 555 | /* BB special case reconnect tid and uid here? */ |
| 556 | rc = map_smb_to_linux_error(midQ->resp_buf, |
| 557 | flags & CIFS_LOG_ERROR); |
| 558 | |
| 559 | /* convert ByteCount if necessary */ |
| 560 | if (receive_len >= sizeof(struct smb_hdr) - 4 |
| 561 | /* do not count RFC1001 header */ + |
| 562 | (2 * midQ->resp_buf->WordCount) + 2 /* bcc */ ) |
| 563 | BCC(midQ->resp_buf) = |
| 564 | le16_to_cpu(BCC_LE(midQ->resp_buf)); |
| 565 | if ((flags & CIFS_NO_RESP) == 0) |
| 566 | midQ->resp_buf = NULL; /* mark it so buf will |
| 567 | not be freed by |
| 568 | DeleteMidQEntry */ |
| 569 | } else { |
| 570 | rc = -EIO; |
| 571 | cFYI(1, ("Bad MID state?")); |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 572 | } |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 573 | |
| 574 | out: |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 575 | DeleteMidQEntry(midQ); |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 576 | atomic_dec(&ses->server->inFlight); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 577 | wake_up(&ses->server->request_q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | |
| 579 | return rc; |
| 580 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | |
| 582 | int |
| 583 | SendReceive(const unsigned int xid, struct cifsSesInfo *ses, |
| 584 | struct smb_hdr *in_buf, struct smb_hdr *out_buf, |
| 585 | int *pbytes_returned, const int long_op) |
| 586 | { |
| 587 | int rc = 0; |
| 588 | unsigned int receive_len; |
| 589 | unsigned long timeout; |
| 590 | struct mid_q_entry *midQ; |
| 591 | |
| 592 | if (ses == NULL) { |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 593 | cERROR(1, ("Null smb session")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | return -EIO; |
| 595 | } |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 596 | if (ses->server == NULL) { |
| 597 | cERROR(1, ("Null tcp session")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | return -EIO; |
| 599 | } |
| 600 | |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 601 | if (ses->server->tcpStatus == CifsExiting) |
Steve French | 31ca3bc | 2005-04-28 22:41:11 -0700 | [diff] [blame] | 602 | return -ENOENT; |
| 603 | |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 604 | /* Ensure that we do not send more than 50 overlapping requests |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | to the same server. We may make this configurable later or |
| 606 | use ses->maxReq */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | |
Volker Lendecke | 6d9c6d5 | 2008-12-08 20:50:24 +0000 | [diff] [blame] | 608 | if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) { |
| 609 | cERROR(1, ("Illegal length, greater than maximum frame, %d", |
| 610 | in_buf->smb_buf_length)); |
| 611 | return -EIO; |
| 612 | } |
| 613 | |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 614 | rc = wait_for_free_request(ses, long_op); |
| 615 | if (rc) |
| 616 | return rc; |
| 617 | |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 618 | /* 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] | 619 | and avoid races inside tcp sendmsg code that could cause corruption |
| 620 | of smb data */ |
| 621 | |
Jeff Layton | 72ca545 | 2008-12-01 07:09:36 -0500 | [diff] [blame] | 622 | mutex_lock(&ses->server->srv_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 624 | rc = allocate_mid(ses, in_buf, &midQ); |
| 625 | if (rc) { |
Jeff Layton | 72ca545 | 2008-12-01 07:09:36 -0500 | [diff] [blame] | 626 | mutex_unlock(&ses->server->srv_mutex); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 627 | /* Update # of requests on wire to server */ |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 628 | atomic_dec(&ses->server->inFlight); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 629 | wake_up(&ses->server->request_q); |
| 630 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | } |
| 632 | |
Steve French | ad009ac | 2005-04-28 22:41:05 -0700 | [diff] [blame] | 633 | rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number); |
Volker Lendecke | 829049c | 2008-12-06 16:00:53 +0100 | [diff] [blame] | 634 | if (rc) { |
| 635 | mutex_unlock(&ses->server->srv_mutex); |
| 636 | goto out; |
| 637 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | |
| 639 | midQ->midState = MID_REQUEST_SUBMITTED; |
Steve French | 131afd0b | 2005-10-07 09:51:05 -0700 | [diff] [blame] | 640 | #ifdef CONFIG_CIFS_STATS2 |
| 641 | atomic_inc(&ses->server->inSend); |
| 642 | #endif |
Jeff Layton | 0496e02 | 2008-12-30 12:39:16 -0500 | [diff] [blame] | 643 | rc = smb_send(ses->server, in_buf, in_buf->smb_buf_length); |
Steve French | 131afd0b | 2005-10-07 09:51:05 -0700 | [diff] [blame] | 644 | #ifdef CONFIG_CIFS_STATS2 |
| 645 | atomic_dec(&ses->server->inSend); |
Steve French | 1047abc | 2005-10-11 19:58:06 -0700 | [diff] [blame] | 646 | midQ->when_sent = jiffies; |
Steve French | 131afd0b | 2005-10-07 09:51:05 -0700 | [diff] [blame] | 647 | #endif |
Jeff Layton | 72ca545 | 2008-12-01 07:09:36 -0500 | [diff] [blame] | 648 | mutex_unlock(&ses->server->srv_mutex); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 649 | |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 650 | if (rc < 0) |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 651 | goto out; |
| 652 | |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 653 | if (long_op == CIFS_STD_OP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | timeout = 15 * HZ; |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 655 | /* wait for 15 seconds or until woken up due to response arriving or |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | due to last connection to this server being unmounted */ |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 657 | else if (long_op == CIFS_ASYNC_OP) |
| 658 | goto out; |
| 659 | else if (long_op == CIFS_VLONG_OP) /* writes past EOF can be slow */ |
| 660 | timeout = 180 * HZ; |
| 661 | else if (long_op == CIFS_LONG_OP) |
| 662 | timeout = 45 * HZ; /* should be greater than |
| 663 | servers oplock break timeout (about 43 seconds) */ |
| 664 | else if (long_op == CIFS_BLOCKING_OP) |
| 665 | timeout = 0x7FFFFFFF; /* large but no so large as to wrap */ |
| 666 | else { |
| 667 | cERROR(1, ("unknown timeout flag %d", long_op)); |
| 668 | rc = -EIO; |
| 669 | goto out; |
| 670 | } |
| 671 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | if (signal_pending(current)) { |
| 673 | /* if signal pending do not hold up user for full smb timeout |
Steve French | 8a23626 | 2007-03-06 00:31:00 +0000 | [diff] [blame] | 674 | but we still give response a chance to complete */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | timeout = 2 * HZ; |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 676 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | |
| 678 | /* No user interrupts in wait - wreaks havoc with performance */ |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 679 | wait_for_response(ses, midQ, timeout, 10 * HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | |
| 681 | spin_lock(&GlobalMid_Lock); |
Volker Lendecke | 8e4f2e8 | 2008-12-06 16:22:15 +0100 | [diff] [blame] | 682 | if (midQ->resp_buf == NULL) { |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 683 | cERROR(1, ("No response for cmd %d mid %d", |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 684 | midQ->command, midQ->mid)); |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 685 | if (midQ->midState == MID_REQUEST_SUBMITTED) { |
| 686 | if (ses->server->tcpStatus == CifsExiting) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | rc = -EHOSTDOWN; |
| 688 | else { |
| 689 | ses->server->tcpStatus = CifsNeedReconnect; |
| 690 | midQ->midState = MID_RETRY_NEEDED; |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | if (rc != -EHOSTDOWN) { |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 695 | if (midQ->midState == MID_RETRY_NEEDED) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | rc = -EAGAIN; |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 697 | cFYI(1, ("marking request for retry")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | } else { |
| 699 | rc = -EIO; |
| 700 | } |
| 701 | } |
| 702 | spin_unlock(&GlobalMid_Lock); |
| 703 | DeleteMidQEntry(midQ); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 704 | /* Update # of requests on wire to server */ |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 705 | atomic_dec(&ses->server->inFlight); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 706 | wake_up(&ses->server->request_q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | return rc; |
| 708 | } |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 709 | |
Volker Lendecke | 8e4f2e8 | 2008-12-06 16:22:15 +0100 | [diff] [blame] | 710 | spin_unlock(&GlobalMid_Lock); |
| 711 | receive_len = midQ->resp_buf->smb_buf_length; |
| 712 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) { |
Steve French | ad009ac | 2005-04-28 22:41:05 -0700 | [diff] [blame] | 714 | cERROR(1, ("Frame too large received. Length: %d Xid: %d", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | receive_len, xid)); |
| 716 | rc = -EIO; |
Steve French | 2b2bdfb | 2008-12-11 17:26:54 +0000 | [diff] [blame] | 717 | goto out; |
| 718 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | |
Steve French | 2b2bdfb | 2008-12-11 17:26:54 +0000 | [diff] [blame] | 720 | /* rcvd frame is ok */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | |
Steve French | 2b2bdfb | 2008-12-11 17:26:54 +0000 | [diff] [blame] | 722 | if (midQ->resp_buf && out_buf |
| 723 | && (midQ->midState == MID_RESPONSE_RECEIVED)) { |
| 724 | out_buf->smb_buf_length = receive_len; |
| 725 | memcpy((char *)out_buf + 4, |
| 726 | (char *)midQ->resp_buf + 4, |
| 727 | receive_len); |
| 728 | |
| 729 | dump_smb(out_buf, 92); |
| 730 | /* convert the length into a more usable form */ |
| 731 | if ((receive_len > 24) && |
| 732 | (ses->server->secMode & (SECMODE_SIGN_REQUIRED | |
| 733 | SECMODE_SIGN_ENABLED))) { |
| 734 | rc = cifs_verify_signature(out_buf, |
Steve French | b609f06 | 2007-07-09 07:55:14 +0000 | [diff] [blame] | 735 | &ses->server->mac_signing_key, |
Steve French | ad009ac | 2005-04-28 22:41:05 -0700 | [diff] [blame] | 736 | midQ->sequence_number+1); |
Steve French | 2b2bdfb | 2008-12-11 17:26:54 +0000 | [diff] [blame] | 737 | if (rc) { |
| 738 | cERROR(1, ("Unexpected SMB signature")); |
| 739 | /* BB FIXME add code to kill session */ |
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 | } |
Steve French | 2b2bdfb | 2008-12-11 17:26:54 +0000 | [diff] [blame] | 742 | |
| 743 | *pbytes_returned = out_buf->smb_buf_length; |
| 744 | |
| 745 | /* BB special case reconnect tid and uid here? */ |
| 746 | rc = map_smb_to_linux_error(out_buf, 0 /* no log */ ); |
| 747 | |
| 748 | /* convert ByteCount if necessary */ |
| 749 | if (receive_len >= sizeof(struct smb_hdr) - 4 |
| 750 | /* do not count RFC1001 header */ + |
| 751 | (2 * out_buf->WordCount) + 2 /* bcc */ ) |
| 752 | BCC(out_buf) = le16_to_cpu(BCC_LE(out_buf)); |
| 753 | } else { |
| 754 | rc = -EIO; |
| 755 | cERROR(1, ("Bad MID state?")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 758 | out: |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 759 | DeleteMidQEntry(midQ); |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 760 | atomic_dec(&ses->server->inFlight); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 761 | wake_up(&ses->server->request_q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | |
| 763 | return rc; |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 764 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 766 | /* Send an NT_CANCEL SMB to cause the POSIX blocking lock to return. */ |
| 767 | |
| 768 | static int |
| 769 | send_nt_cancel(struct cifsTconInfo *tcon, struct smb_hdr *in_buf, |
| 770 | struct mid_q_entry *midQ) |
| 771 | { |
| 772 | int rc = 0; |
| 773 | struct cifsSesInfo *ses = tcon->ses; |
| 774 | __u16 mid = in_buf->Mid; |
| 775 | |
| 776 | header_assemble(in_buf, SMB_COM_NT_CANCEL, tcon, 0); |
| 777 | in_buf->Mid = mid; |
Jeff Layton | 72ca545 | 2008-12-01 07:09:36 -0500 | [diff] [blame] | 778 | mutex_lock(&ses->server->srv_mutex); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 779 | rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number); |
| 780 | if (rc) { |
Jeff Layton | 72ca545 | 2008-12-01 07:09:36 -0500 | [diff] [blame] | 781 | mutex_unlock(&ses->server->srv_mutex); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 782 | return rc; |
| 783 | } |
Jeff Layton | 0496e02 | 2008-12-30 12:39:16 -0500 | [diff] [blame] | 784 | rc = smb_send(ses->server, in_buf, in_buf->smb_buf_length); |
Jeff Layton | 72ca545 | 2008-12-01 07:09:36 -0500 | [diff] [blame] | 785 | mutex_unlock(&ses->server->srv_mutex); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 786 | return rc; |
| 787 | } |
| 788 | |
| 789 | /* We send a LOCKINGX_CANCEL_LOCK to cause the Windows |
| 790 | blocking lock to return. */ |
| 791 | |
| 792 | static int |
| 793 | send_lock_cancel(const unsigned int xid, struct cifsTconInfo *tcon, |
| 794 | struct smb_hdr *in_buf, |
| 795 | struct smb_hdr *out_buf) |
| 796 | { |
| 797 | int bytes_returned; |
| 798 | struct cifsSesInfo *ses = tcon->ses; |
| 799 | LOCK_REQ *pSMB = (LOCK_REQ *)in_buf; |
| 800 | |
| 801 | /* We just modify the current in_buf to change |
| 802 | the type of lock from LOCKING_ANDX_SHARED_LOCK |
| 803 | or LOCKING_ANDX_EXCLUSIVE_LOCK to |
| 804 | LOCKING_ANDX_CANCEL_LOCK. */ |
| 805 | |
| 806 | pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES; |
| 807 | pSMB->Timeout = 0; |
| 808 | pSMB->hdr.Mid = GetNextMid(ses->server); |
| 809 | |
| 810 | return SendReceive(xid, ses, in_buf, out_buf, |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 811 | &bytes_returned, CIFS_STD_OP); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | int |
| 815 | SendReceiveBlockingLock(const unsigned int xid, struct cifsTconInfo *tcon, |
| 816 | struct smb_hdr *in_buf, struct smb_hdr *out_buf, |
| 817 | int *pbytes_returned) |
| 818 | { |
| 819 | int rc = 0; |
| 820 | int rstart = 0; |
| 821 | unsigned int receive_len; |
| 822 | struct mid_q_entry *midQ; |
| 823 | struct cifsSesInfo *ses; |
| 824 | |
| 825 | if (tcon == NULL || tcon->ses == NULL) { |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 826 | cERROR(1, ("Null smb session")); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 827 | return -EIO; |
| 828 | } |
| 829 | ses = tcon->ses; |
| 830 | |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 831 | if (ses->server == NULL) { |
| 832 | cERROR(1, ("Null tcp session")); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 833 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | } |
| 835 | |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 836 | if (ses->server->tcpStatus == CifsExiting) |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 837 | return -ENOENT; |
| 838 | |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 839 | /* Ensure that we do not send more than 50 overlapping requests |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 840 | to the same server. We may make this configurable later or |
| 841 | use ses->maxReq */ |
| 842 | |
Volker Lendecke | 6d9c6d5 | 2008-12-08 20:50:24 +0000 | [diff] [blame] | 843 | if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) { |
| 844 | cERROR(1, ("Illegal length, greater than maximum frame, %d", |
| 845 | in_buf->smb_buf_length)); |
| 846 | return -EIO; |
| 847 | } |
| 848 | |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 849 | rc = wait_for_free_request(ses, CIFS_BLOCKING_OP); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 850 | if (rc) |
| 851 | return rc; |
| 852 | |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 853 | /* 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] | 854 | and avoid races inside tcp sendmsg code that could cause corruption |
| 855 | of smb data */ |
| 856 | |
Jeff Layton | 72ca545 | 2008-12-01 07:09:36 -0500 | [diff] [blame] | 857 | mutex_lock(&ses->server->srv_mutex); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 858 | |
| 859 | rc = allocate_mid(ses, in_buf, &midQ); |
| 860 | if (rc) { |
Jeff Layton | 72ca545 | 2008-12-01 07:09:36 -0500 | [diff] [blame] | 861 | mutex_unlock(&ses->server->srv_mutex); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 862 | return rc; |
| 863 | } |
| 864 | |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 865 | rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number); |
Volker Lendecke | 829049c | 2008-12-06 16:00:53 +0100 | [diff] [blame] | 866 | if (rc) { |
| 867 | DeleteMidQEntry(midQ); |
| 868 | mutex_unlock(&ses->server->srv_mutex); |
| 869 | return rc; |
| 870 | } |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 871 | |
| 872 | midQ->midState = MID_REQUEST_SUBMITTED; |
| 873 | #ifdef CONFIG_CIFS_STATS2 |
| 874 | atomic_inc(&ses->server->inSend); |
| 875 | #endif |
Jeff Layton | 0496e02 | 2008-12-30 12:39:16 -0500 | [diff] [blame] | 876 | rc = smb_send(ses->server, in_buf, in_buf->smb_buf_length); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 877 | #ifdef CONFIG_CIFS_STATS2 |
| 878 | atomic_dec(&ses->server->inSend); |
| 879 | midQ->when_sent = jiffies; |
| 880 | #endif |
Jeff Layton | 72ca545 | 2008-12-01 07:09:36 -0500 | [diff] [blame] | 881 | mutex_unlock(&ses->server->srv_mutex); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 882 | |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 883 | if (rc < 0) { |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 884 | DeleteMidQEntry(midQ); |
| 885 | return rc; |
| 886 | } |
| 887 | |
| 888 | /* Wait for a reply - allow signals to interrupt. */ |
| 889 | rc = wait_event_interruptible(ses->server->response_q, |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 890 | (!(midQ->midState == MID_REQUEST_SUBMITTED)) || |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 891 | ((ses->server->tcpStatus != CifsGood) && |
| 892 | (ses->server->tcpStatus != CifsNew))); |
| 893 | |
| 894 | /* Were we interrupted by a signal ? */ |
| 895 | if ((rc == -ERESTARTSYS) && |
| 896 | (midQ->midState == MID_REQUEST_SUBMITTED) && |
| 897 | ((ses->server->tcpStatus == CifsGood) || |
| 898 | (ses->server->tcpStatus == CifsNew))) { |
| 899 | |
| 900 | if (in_buf->Command == SMB_COM_TRANSACTION2) { |
| 901 | /* POSIX lock. We send a NT_CANCEL SMB to cause the |
| 902 | blocking lock to return. */ |
| 903 | |
| 904 | rc = send_nt_cancel(tcon, in_buf, midQ); |
| 905 | if (rc) { |
| 906 | DeleteMidQEntry(midQ); |
| 907 | return rc; |
| 908 | } |
| 909 | } else { |
| 910 | /* Windows lock. We send a LOCKINGX_CANCEL_LOCK |
| 911 | to cause the blocking lock to return. */ |
| 912 | |
| 913 | rc = send_lock_cancel(xid, tcon, in_buf, out_buf); |
| 914 | |
| 915 | /* If we get -ENOLCK back the lock may have |
| 916 | already been removed. Don't exit in this case. */ |
| 917 | if (rc && rc != -ENOLCK) { |
| 918 | DeleteMidQEntry(midQ); |
| 919 | return rc; |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | /* Wait 5 seconds for the response. */ |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 924 | if (wait_for_response(ses, midQ, 5 * HZ, 5 * HZ) == 0) { |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 925 | /* We got the response - restart system call. */ |
| 926 | rstart = 1; |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | spin_lock(&GlobalMid_Lock); |
| 931 | if (midQ->resp_buf) { |
| 932 | spin_unlock(&GlobalMid_Lock); |
| 933 | receive_len = midQ->resp_buf->smb_buf_length; |
| 934 | } else { |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 935 | cERROR(1, ("No response for cmd %d mid %d", |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 936 | midQ->command, midQ->mid)); |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 937 | if (midQ->midState == MID_REQUEST_SUBMITTED) { |
| 938 | if (ses->server->tcpStatus == CifsExiting) |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 939 | rc = -EHOSTDOWN; |
| 940 | else { |
| 941 | ses->server->tcpStatus = CifsNeedReconnect; |
| 942 | midQ->midState = MID_RETRY_NEEDED; |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | if (rc != -EHOSTDOWN) { |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 947 | if (midQ->midState == MID_RETRY_NEEDED) { |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 948 | rc = -EAGAIN; |
Steve French | 79a58d1 | 2007-07-06 22:44:50 +0000 | [diff] [blame] | 949 | cFYI(1, ("marking request for retry")); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 950 | } else { |
| 951 | rc = -EIO; |
| 952 | } |
| 953 | } |
| 954 | spin_unlock(&GlobalMid_Lock); |
| 955 | DeleteMidQEntry(midQ); |
| 956 | return rc; |
| 957 | } |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 958 | |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 959 | if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) { |
| 960 | cERROR(1, ("Frame too large received. Length: %d Xid: %d", |
| 961 | receive_len, xid)); |
| 962 | rc = -EIO; |
Volker Lendecke | 17c8bfe | 2008-12-06 16:38:19 +0100 | [diff] [blame] | 963 | goto out; |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 964 | } |
Volker Lendecke | 17c8bfe | 2008-12-06 16:38:19 +0100 | [diff] [blame] | 965 | |
| 966 | /* rcvd frame is ok */ |
| 967 | |
Volker Lendecke | ac6a3ef | 2008-12-06 16:40:40 +0100 | [diff] [blame] | 968 | if ((out_buf == NULL) || (midQ->midState != MID_RESPONSE_RECEIVED)) { |
Volker Lendecke | 17c8bfe | 2008-12-06 16:38:19 +0100 | [diff] [blame] | 969 | rc = -EIO; |
| 970 | cERROR(1, ("Bad MID state?")); |
Volker Lendecke | 698e96a | 2008-12-06 16:39:31 +0100 | [diff] [blame] | 971 | goto out; |
Volker Lendecke | 17c8bfe | 2008-12-06 16:38:19 +0100 | [diff] [blame] | 972 | } |
| 973 | |
Volker Lendecke | 698e96a | 2008-12-06 16:39:31 +0100 | [diff] [blame] | 974 | out_buf->smb_buf_length = receive_len; |
| 975 | memcpy((char *)out_buf + 4, |
| 976 | (char *)midQ->resp_buf + 4, |
| 977 | receive_len); |
| 978 | |
| 979 | dump_smb(out_buf, 92); |
| 980 | /* convert the length into a more usable form */ |
| 981 | if ((receive_len > 24) && |
| 982 | (ses->server->secMode & (SECMODE_SIGN_REQUIRED | |
| 983 | SECMODE_SIGN_ENABLED))) { |
| 984 | rc = cifs_verify_signature(out_buf, |
| 985 | &ses->server->mac_signing_key, |
| 986 | midQ->sequence_number+1); |
| 987 | if (rc) { |
| 988 | cERROR(1, ("Unexpected SMB signature")); |
| 989 | /* BB FIXME add code to kill session */ |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | *pbytes_returned = out_buf->smb_buf_length; |
| 994 | |
| 995 | /* BB special case reconnect tid and uid here? */ |
| 996 | rc = map_smb_to_linux_error(out_buf, 0 /* no log */ ); |
| 997 | |
| 998 | /* convert ByteCount if necessary */ |
| 999 | if (receive_len >= sizeof(struct smb_hdr) - 4 |
| 1000 | /* do not count RFC1001 header */ + |
| 1001 | (2 * out_buf->WordCount) + 2 /* bcc */ ) |
| 1002 | BCC(out_buf) = le16_to_cpu(BCC_LE(out_buf)); |
| 1003 | |
Volker Lendecke | 17c8bfe | 2008-12-06 16:38:19 +0100 | [diff] [blame] | 1004 | out: |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1005 | DeleteMidQEntry(midQ); |
| 1006 | if (rstart && rc == -EACCES) |
| 1007 | return -ERESTARTSYS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | return rc; |
| 1009 | } |