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