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