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