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