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