xref: /OK3568_Linux_fs/kernel/fs/cifs/transport.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  *   fs/cifs/transport.c
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  *   Copyright (C) International Business Machines  Corp., 2002,2008
5*4882a593Smuzhiyun  *   Author(s): Steve French (sfrench@us.ibm.com)
6*4882a593Smuzhiyun  *   Jeremy Allison (jra@samba.org) 2006.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  *   This library is free software; you can redistribute it and/or modify
9*4882a593Smuzhiyun  *   it under the terms of the GNU Lesser General Public License as published
10*4882a593Smuzhiyun  *   by the Free Software Foundation; either version 2.1 of the License, or
11*4882a593Smuzhiyun  *   (at your option) any later version.
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  *   This library is distributed in the hope that it will be useful,
14*4882a593Smuzhiyun  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15*4882a593Smuzhiyun  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
16*4882a593Smuzhiyun  *   the GNU Lesser General Public License for more details.
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  *   You should have received a copy of the GNU Lesser General Public License
19*4882a593Smuzhiyun  *   along with this library; if not, write to the Free Software
20*4882a593Smuzhiyun  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21*4882a593Smuzhiyun  */
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #include <linux/fs.h>
24*4882a593Smuzhiyun #include <linux/list.h>
25*4882a593Smuzhiyun #include <linux/gfp.h>
26*4882a593Smuzhiyun #include <linux/wait.h>
27*4882a593Smuzhiyun #include <linux/net.h>
28*4882a593Smuzhiyun #include <linux/delay.h>
29*4882a593Smuzhiyun #include <linux/freezer.h>
30*4882a593Smuzhiyun #include <linux/tcp.h>
31*4882a593Smuzhiyun #include <linux/bvec.h>
32*4882a593Smuzhiyun #include <linux/highmem.h>
33*4882a593Smuzhiyun #include <linux/uaccess.h>
34*4882a593Smuzhiyun #include <asm/processor.h>
35*4882a593Smuzhiyun #include <linux/mempool.h>
36*4882a593Smuzhiyun #include <linux/sched/signal.h>
37*4882a593Smuzhiyun #include "cifspdu.h"
38*4882a593Smuzhiyun #include "cifsglob.h"
39*4882a593Smuzhiyun #include "cifsproto.h"
40*4882a593Smuzhiyun #include "cifs_debug.h"
41*4882a593Smuzhiyun #include "smb2proto.h"
42*4882a593Smuzhiyun #include "smbdirect.h"
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun /* Max number of iovectors we can use off the stack when sending requests. */
45*4882a593Smuzhiyun #define CIFS_MAX_IOV_SIZE 8
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun void
cifs_wake_up_task(struct mid_q_entry * mid)48*4882a593Smuzhiyun cifs_wake_up_task(struct mid_q_entry *mid)
49*4882a593Smuzhiyun {
50*4882a593Smuzhiyun 	wake_up_process(mid->callback_data);
51*4882a593Smuzhiyun }
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun struct mid_q_entry *
AllocMidQEntry(const struct smb_hdr * smb_buffer,struct TCP_Server_Info * server)54*4882a593Smuzhiyun AllocMidQEntry(const struct smb_hdr *smb_buffer, struct TCP_Server_Info *server)
55*4882a593Smuzhiyun {
56*4882a593Smuzhiyun 	struct mid_q_entry *temp;
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 	if (server == NULL) {
59*4882a593Smuzhiyun 		cifs_dbg(VFS, "Null TCP session in AllocMidQEntry\n");
60*4882a593Smuzhiyun 		return NULL;
61*4882a593Smuzhiyun 	}
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 	temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
64*4882a593Smuzhiyun 	memset(temp, 0, sizeof(struct mid_q_entry));
65*4882a593Smuzhiyun 	kref_init(&temp->refcount);
66*4882a593Smuzhiyun 	temp->mid = get_mid(smb_buffer);
67*4882a593Smuzhiyun 	temp->pid = current->pid;
68*4882a593Smuzhiyun 	temp->command = cpu_to_le16(smb_buffer->Command);
69*4882a593Smuzhiyun 	cifs_dbg(FYI, "For smb_command %d\n", smb_buffer->Command);
70*4882a593Smuzhiyun 	/*	do_gettimeofday(&temp->when_sent);*/ /* easier to use jiffies */
71*4882a593Smuzhiyun 	/* when mid allocated can be before when sent */
72*4882a593Smuzhiyun 	temp->when_alloc = jiffies;
73*4882a593Smuzhiyun 	temp->server = server;
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 	/*
76*4882a593Smuzhiyun 	 * The default is for the mid to be synchronous, so the
77*4882a593Smuzhiyun 	 * default callback just wakes up the current task.
78*4882a593Smuzhiyun 	 */
79*4882a593Smuzhiyun 	get_task_struct(current);
80*4882a593Smuzhiyun 	temp->creator = current;
81*4882a593Smuzhiyun 	temp->callback = cifs_wake_up_task;
82*4882a593Smuzhiyun 	temp->callback_data = current;
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	atomic_inc(&midCount);
85*4882a593Smuzhiyun 	temp->mid_state = MID_REQUEST_ALLOCATED;
86*4882a593Smuzhiyun 	return temp;
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun 
_cifs_mid_q_entry_release(struct kref * refcount)89*4882a593Smuzhiyun static void _cifs_mid_q_entry_release(struct kref *refcount)
90*4882a593Smuzhiyun {
91*4882a593Smuzhiyun 	struct mid_q_entry *midEntry =
92*4882a593Smuzhiyun 			container_of(refcount, struct mid_q_entry, refcount);
93*4882a593Smuzhiyun #ifdef CONFIG_CIFS_STATS2
94*4882a593Smuzhiyun 	__le16 command = midEntry->server->vals->lock_cmd;
95*4882a593Smuzhiyun 	__u16 smb_cmd = le16_to_cpu(midEntry->command);
96*4882a593Smuzhiyun 	unsigned long now;
97*4882a593Smuzhiyun 	unsigned long roundtrip_time;
98*4882a593Smuzhiyun #endif
99*4882a593Smuzhiyun 	struct TCP_Server_Info *server = midEntry->server;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	if (midEntry->resp_buf && (midEntry->mid_flags & MID_WAIT_CANCELLED) &&
102*4882a593Smuzhiyun 	    midEntry->mid_state == MID_RESPONSE_RECEIVED &&
103*4882a593Smuzhiyun 	    server->ops->handle_cancelled_mid)
104*4882a593Smuzhiyun 		server->ops->handle_cancelled_mid(midEntry, server);
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	midEntry->mid_state = MID_FREE;
107*4882a593Smuzhiyun 	atomic_dec(&midCount);
108*4882a593Smuzhiyun 	if (midEntry->large_buf)
109*4882a593Smuzhiyun 		cifs_buf_release(midEntry->resp_buf);
110*4882a593Smuzhiyun 	else
111*4882a593Smuzhiyun 		cifs_small_buf_release(midEntry->resp_buf);
112*4882a593Smuzhiyun #ifdef CONFIG_CIFS_STATS2
113*4882a593Smuzhiyun 	now = jiffies;
114*4882a593Smuzhiyun 	if (now < midEntry->when_alloc)
115*4882a593Smuzhiyun 		cifs_server_dbg(VFS, "Invalid mid allocation time\n");
116*4882a593Smuzhiyun 	roundtrip_time = now - midEntry->when_alloc;
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 	if (smb_cmd < NUMBER_OF_SMB2_COMMANDS) {
119*4882a593Smuzhiyun 		if (atomic_read(&server->num_cmds[smb_cmd]) == 0) {
120*4882a593Smuzhiyun 			server->slowest_cmd[smb_cmd] = roundtrip_time;
121*4882a593Smuzhiyun 			server->fastest_cmd[smb_cmd] = roundtrip_time;
122*4882a593Smuzhiyun 		} else {
123*4882a593Smuzhiyun 			if (server->slowest_cmd[smb_cmd] < roundtrip_time)
124*4882a593Smuzhiyun 				server->slowest_cmd[smb_cmd] = roundtrip_time;
125*4882a593Smuzhiyun 			else if (server->fastest_cmd[smb_cmd] > roundtrip_time)
126*4882a593Smuzhiyun 				server->fastest_cmd[smb_cmd] = roundtrip_time;
127*4882a593Smuzhiyun 		}
128*4882a593Smuzhiyun 		cifs_stats_inc(&server->num_cmds[smb_cmd]);
129*4882a593Smuzhiyun 		server->time_per_cmd[smb_cmd] += roundtrip_time;
130*4882a593Smuzhiyun 	}
131*4882a593Smuzhiyun 	/*
132*4882a593Smuzhiyun 	 * commands taking longer than one second (default) can be indications
133*4882a593Smuzhiyun 	 * that something is wrong, unless it is quite a slow link or a very
134*4882a593Smuzhiyun 	 * busy server. Note that this calc is unlikely or impossible to wrap
135*4882a593Smuzhiyun 	 * as long as slow_rsp_threshold is not set way above recommended max
136*4882a593Smuzhiyun 	 * value (32767 ie 9 hours) and is generally harmless even if wrong
137*4882a593Smuzhiyun 	 * since only affects debug counters - so leaving the calc as simple
138*4882a593Smuzhiyun 	 * comparison rather than doing multiple conversions and overflow
139*4882a593Smuzhiyun 	 * checks
140*4882a593Smuzhiyun 	 */
141*4882a593Smuzhiyun 	if ((slow_rsp_threshold != 0) &&
142*4882a593Smuzhiyun 	    time_after(now, midEntry->when_alloc + (slow_rsp_threshold * HZ)) &&
143*4882a593Smuzhiyun 	    (midEntry->command != command)) {
144*4882a593Smuzhiyun 		/*
145*4882a593Smuzhiyun 		 * smb2slowcmd[NUMBER_OF_SMB2_COMMANDS] counts by command
146*4882a593Smuzhiyun 		 * NB: le16_to_cpu returns unsigned so can not be negative below
147*4882a593Smuzhiyun 		 */
148*4882a593Smuzhiyun 		if (smb_cmd < NUMBER_OF_SMB2_COMMANDS)
149*4882a593Smuzhiyun 			cifs_stats_inc(&server->smb2slowcmd[smb_cmd]);
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 		trace_smb3_slow_rsp(smb_cmd, midEntry->mid, midEntry->pid,
152*4882a593Smuzhiyun 			       midEntry->when_sent, midEntry->when_received);
153*4882a593Smuzhiyun 		if (cifsFYI & CIFS_TIMER) {
154*4882a593Smuzhiyun 			pr_debug("slow rsp: cmd %d mid %llu",
155*4882a593Smuzhiyun 				 midEntry->command, midEntry->mid);
156*4882a593Smuzhiyun 			cifs_info("A: 0x%lx S: 0x%lx R: 0x%lx\n",
157*4882a593Smuzhiyun 				  now - midEntry->when_alloc,
158*4882a593Smuzhiyun 				  now - midEntry->when_sent,
159*4882a593Smuzhiyun 				  now - midEntry->when_received);
160*4882a593Smuzhiyun 		}
161*4882a593Smuzhiyun 	}
162*4882a593Smuzhiyun #endif
163*4882a593Smuzhiyun 	put_task_struct(midEntry->creator);
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	mempool_free(midEntry, cifs_mid_poolp);
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun 
cifs_mid_q_entry_release(struct mid_q_entry * midEntry)168*4882a593Smuzhiyun void cifs_mid_q_entry_release(struct mid_q_entry *midEntry)
169*4882a593Smuzhiyun {
170*4882a593Smuzhiyun 	spin_lock(&GlobalMid_Lock);
171*4882a593Smuzhiyun 	kref_put(&midEntry->refcount, _cifs_mid_q_entry_release);
172*4882a593Smuzhiyun 	spin_unlock(&GlobalMid_Lock);
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun 
DeleteMidQEntry(struct mid_q_entry * midEntry)175*4882a593Smuzhiyun void DeleteMidQEntry(struct mid_q_entry *midEntry)
176*4882a593Smuzhiyun {
177*4882a593Smuzhiyun 	cifs_mid_q_entry_release(midEntry);
178*4882a593Smuzhiyun }
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun void
cifs_delete_mid(struct mid_q_entry * mid)181*4882a593Smuzhiyun cifs_delete_mid(struct mid_q_entry *mid)
182*4882a593Smuzhiyun {
183*4882a593Smuzhiyun 	spin_lock(&GlobalMid_Lock);
184*4882a593Smuzhiyun 	if (!(mid->mid_flags & MID_DELETED)) {
185*4882a593Smuzhiyun 		list_del_init(&mid->qhead);
186*4882a593Smuzhiyun 		mid->mid_flags |= MID_DELETED;
187*4882a593Smuzhiyun 	}
188*4882a593Smuzhiyun 	spin_unlock(&GlobalMid_Lock);
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 	DeleteMidQEntry(mid);
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun /*
194*4882a593Smuzhiyun  * smb_send_kvec - send an array of kvecs to the server
195*4882a593Smuzhiyun  * @server:	Server to send the data to
196*4882a593Smuzhiyun  * @smb_msg:	Message to send
197*4882a593Smuzhiyun  * @sent:	amount of data sent on socket is stored here
198*4882a593Smuzhiyun  *
199*4882a593Smuzhiyun  * Our basic "send data to server" function. Should be called with srv_mutex
200*4882a593Smuzhiyun  * held. The caller is responsible for handling the results.
201*4882a593Smuzhiyun  */
202*4882a593Smuzhiyun static int
smb_send_kvec(struct TCP_Server_Info * server,struct msghdr * smb_msg,size_t * sent)203*4882a593Smuzhiyun smb_send_kvec(struct TCP_Server_Info *server, struct msghdr *smb_msg,
204*4882a593Smuzhiyun 	      size_t *sent)
205*4882a593Smuzhiyun {
206*4882a593Smuzhiyun 	int rc = 0;
207*4882a593Smuzhiyun 	int retries = 0;
208*4882a593Smuzhiyun 	struct socket *ssocket = server->ssocket;
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	*sent = 0;
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	if (server->noblocksnd)
213*4882a593Smuzhiyun 		smb_msg->msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL;
214*4882a593Smuzhiyun 	else
215*4882a593Smuzhiyun 		smb_msg->msg_flags = MSG_NOSIGNAL;
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 	while (msg_data_left(smb_msg)) {
218*4882a593Smuzhiyun 		/*
219*4882a593Smuzhiyun 		 * If blocking send, we try 3 times, since each can block
220*4882a593Smuzhiyun 		 * for 5 seconds. For nonblocking  we have to try more
221*4882a593Smuzhiyun 		 * but wait increasing amounts of time allowing time for
222*4882a593Smuzhiyun 		 * socket to clear.  The overall time we wait in either
223*4882a593Smuzhiyun 		 * case to send on the socket is about 15 seconds.
224*4882a593Smuzhiyun 		 * Similarly we wait for 15 seconds for a response from
225*4882a593Smuzhiyun 		 * the server in SendReceive[2] for the server to send
226*4882a593Smuzhiyun 		 * a response back for most types of requests (except
227*4882a593Smuzhiyun 		 * SMB Write past end of file which can be slow, and
228*4882a593Smuzhiyun 		 * blocking lock operations). NFS waits slightly longer
229*4882a593Smuzhiyun 		 * than CIFS, but this can make it take longer for
230*4882a593Smuzhiyun 		 * nonresponsive servers to be detected and 15 seconds
231*4882a593Smuzhiyun 		 * is more than enough time for modern networks to
232*4882a593Smuzhiyun 		 * send a packet.  In most cases if we fail to send
233*4882a593Smuzhiyun 		 * after the retries we will kill the socket and
234*4882a593Smuzhiyun 		 * reconnect which may clear the network problem.
235*4882a593Smuzhiyun 		 */
236*4882a593Smuzhiyun 		rc = sock_sendmsg(ssocket, smb_msg);
237*4882a593Smuzhiyun 		if (rc == -EAGAIN) {
238*4882a593Smuzhiyun 			retries++;
239*4882a593Smuzhiyun 			if (retries >= 14 ||
240*4882a593Smuzhiyun 			    (!server->noblocksnd && (retries > 2))) {
241*4882a593Smuzhiyun 				cifs_server_dbg(VFS, "sends on sock %p stuck for 15 seconds\n",
242*4882a593Smuzhiyun 					 ssocket);
243*4882a593Smuzhiyun 				return -EAGAIN;
244*4882a593Smuzhiyun 			}
245*4882a593Smuzhiyun 			msleep(1 << retries);
246*4882a593Smuzhiyun 			continue;
247*4882a593Smuzhiyun 		}
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 		if (rc < 0)
250*4882a593Smuzhiyun 			return rc;
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 		if (rc == 0) {
253*4882a593Smuzhiyun 			/* should never happen, letting socket clear before
254*4882a593Smuzhiyun 			   retrying is our only obvious option here */
255*4882a593Smuzhiyun 			cifs_server_dbg(VFS, "tcp sent no data\n");
256*4882a593Smuzhiyun 			msleep(500);
257*4882a593Smuzhiyun 			continue;
258*4882a593Smuzhiyun 		}
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 		/* send was at least partially successful */
261*4882a593Smuzhiyun 		*sent += rc;
262*4882a593Smuzhiyun 		retries = 0; /* in case we get ENOSPC on the next send */
263*4882a593Smuzhiyun 	}
264*4882a593Smuzhiyun 	return 0;
265*4882a593Smuzhiyun }
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun unsigned long
smb_rqst_len(struct TCP_Server_Info * server,struct smb_rqst * rqst)268*4882a593Smuzhiyun smb_rqst_len(struct TCP_Server_Info *server, struct smb_rqst *rqst)
269*4882a593Smuzhiyun {
270*4882a593Smuzhiyun 	unsigned int i;
271*4882a593Smuzhiyun 	struct kvec *iov;
272*4882a593Smuzhiyun 	int nvec;
273*4882a593Smuzhiyun 	unsigned long buflen = 0;
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun 	if (server->vals->header_preamble_size == 0 &&
276*4882a593Smuzhiyun 	    rqst->rq_nvec >= 2 && rqst->rq_iov[0].iov_len == 4) {
277*4882a593Smuzhiyun 		iov = &rqst->rq_iov[1];
278*4882a593Smuzhiyun 		nvec = rqst->rq_nvec - 1;
279*4882a593Smuzhiyun 	} else {
280*4882a593Smuzhiyun 		iov = rqst->rq_iov;
281*4882a593Smuzhiyun 		nvec = rqst->rq_nvec;
282*4882a593Smuzhiyun 	}
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun 	/* total up iov array first */
285*4882a593Smuzhiyun 	for (i = 0; i < nvec; i++)
286*4882a593Smuzhiyun 		buflen += iov[i].iov_len;
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 	/*
289*4882a593Smuzhiyun 	 * Add in the page array if there is one. The caller needs to make
290*4882a593Smuzhiyun 	 * sure rq_offset and rq_tailsz are set correctly. If a buffer of
291*4882a593Smuzhiyun 	 * multiple pages ends at page boundary, rq_tailsz needs to be set to
292*4882a593Smuzhiyun 	 * PAGE_SIZE.
293*4882a593Smuzhiyun 	 */
294*4882a593Smuzhiyun 	if (rqst->rq_npages) {
295*4882a593Smuzhiyun 		if (rqst->rq_npages == 1)
296*4882a593Smuzhiyun 			buflen += rqst->rq_tailsz;
297*4882a593Smuzhiyun 		else {
298*4882a593Smuzhiyun 			/*
299*4882a593Smuzhiyun 			 * If there is more than one page, calculate the
300*4882a593Smuzhiyun 			 * buffer length based on rq_offset and rq_tailsz
301*4882a593Smuzhiyun 			 */
302*4882a593Smuzhiyun 			buflen += rqst->rq_pagesz * (rqst->rq_npages - 1) -
303*4882a593Smuzhiyun 					rqst->rq_offset;
304*4882a593Smuzhiyun 			buflen += rqst->rq_tailsz;
305*4882a593Smuzhiyun 		}
306*4882a593Smuzhiyun 	}
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 	return buflen;
309*4882a593Smuzhiyun }
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun static int
__smb_send_rqst(struct TCP_Server_Info * server,int num_rqst,struct smb_rqst * rqst)312*4882a593Smuzhiyun __smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
313*4882a593Smuzhiyun 		struct smb_rqst *rqst)
314*4882a593Smuzhiyun {
315*4882a593Smuzhiyun 	int rc = 0;
316*4882a593Smuzhiyun 	struct kvec *iov;
317*4882a593Smuzhiyun 	int n_vec;
318*4882a593Smuzhiyun 	unsigned int send_length = 0;
319*4882a593Smuzhiyun 	unsigned int i, j;
320*4882a593Smuzhiyun 	sigset_t mask, oldmask;
321*4882a593Smuzhiyun 	size_t total_len = 0, sent, size;
322*4882a593Smuzhiyun 	struct socket *ssocket = server->ssocket;
323*4882a593Smuzhiyun 	struct msghdr smb_msg = {};
324*4882a593Smuzhiyun 	__be32 rfc1002_marker;
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 	if (cifs_rdma_enabled(server)) {
327*4882a593Smuzhiyun 		/* return -EAGAIN when connecting or reconnecting */
328*4882a593Smuzhiyun 		rc = -EAGAIN;
329*4882a593Smuzhiyun 		if (server->smbd_conn)
330*4882a593Smuzhiyun 			rc = smbd_send(server, num_rqst, rqst);
331*4882a593Smuzhiyun 		goto smbd_done;
332*4882a593Smuzhiyun 	}
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 	if (ssocket == NULL)
335*4882a593Smuzhiyun 		return -EAGAIN;
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 	if (fatal_signal_pending(current)) {
338*4882a593Smuzhiyun 		cifs_dbg(FYI, "signal pending before send request\n");
339*4882a593Smuzhiyun 		return -ERESTARTSYS;
340*4882a593Smuzhiyun 	}
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun 	/* cork the socket */
343*4882a593Smuzhiyun 	tcp_sock_set_cork(ssocket->sk, true);
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun 	for (j = 0; j < num_rqst; j++)
346*4882a593Smuzhiyun 		send_length += smb_rqst_len(server, &rqst[j]);
347*4882a593Smuzhiyun 	rfc1002_marker = cpu_to_be32(send_length);
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun 	/*
350*4882a593Smuzhiyun 	 * We should not allow signals to interrupt the network send because
351*4882a593Smuzhiyun 	 * any partial send will cause session reconnects thus increasing
352*4882a593Smuzhiyun 	 * latency of system calls and overload a server with unnecessary
353*4882a593Smuzhiyun 	 * requests.
354*4882a593Smuzhiyun 	 */
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun 	sigfillset(&mask);
357*4882a593Smuzhiyun 	sigprocmask(SIG_BLOCK, &mask, &oldmask);
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun 	/* Generate a rfc1002 marker for SMB2+ */
360*4882a593Smuzhiyun 	if (server->vals->header_preamble_size == 0) {
361*4882a593Smuzhiyun 		struct kvec hiov = {
362*4882a593Smuzhiyun 			.iov_base = &rfc1002_marker,
363*4882a593Smuzhiyun 			.iov_len  = 4
364*4882a593Smuzhiyun 		};
365*4882a593Smuzhiyun 		iov_iter_kvec(&smb_msg.msg_iter, WRITE, &hiov, 1, 4);
366*4882a593Smuzhiyun 		rc = smb_send_kvec(server, &smb_msg, &sent);
367*4882a593Smuzhiyun 		if (rc < 0)
368*4882a593Smuzhiyun 			goto unmask;
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun 		total_len += sent;
371*4882a593Smuzhiyun 		send_length += 4;
372*4882a593Smuzhiyun 	}
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun 	cifs_dbg(FYI, "Sending smb: smb_len=%u\n", send_length);
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 	for (j = 0; j < num_rqst; j++) {
377*4882a593Smuzhiyun 		iov = rqst[j].rq_iov;
378*4882a593Smuzhiyun 		n_vec = rqst[j].rq_nvec;
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun 		size = 0;
381*4882a593Smuzhiyun 		for (i = 0; i < n_vec; i++) {
382*4882a593Smuzhiyun 			dump_smb(iov[i].iov_base, iov[i].iov_len);
383*4882a593Smuzhiyun 			size += iov[i].iov_len;
384*4882a593Smuzhiyun 		}
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun 		iov_iter_kvec(&smb_msg.msg_iter, WRITE, iov, n_vec, size);
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun 		rc = smb_send_kvec(server, &smb_msg, &sent);
389*4882a593Smuzhiyun 		if (rc < 0)
390*4882a593Smuzhiyun 			goto unmask;
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 		total_len += sent;
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun 		/* now walk the page array and send each page in it */
395*4882a593Smuzhiyun 		for (i = 0; i < rqst[j].rq_npages; i++) {
396*4882a593Smuzhiyun 			struct bio_vec bvec;
397*4882a593Smuzhiyun 
398*4882a593Smuzhiyun 			bvec.bv_page = rqst[j].rq_pages[i];
399*4882a593Smuzhiyun 			rqst_page_get_length(&rqst[j], i, &bvec.bv_len,
400*4882a593Smuzhiyun 					     &bvec.bv_offset);
401*4882a593Smuzhiyun 
402*4882a593Smuzhiyun 			iov_iter_bvec(&smb_msg.msg_iter, WRITE,
403*4882a593Smuzhiyun 				      &bvec, 1, bvec.bv_len);
404*4882a593Smuzhiyun 			rc = smb_send_kvec(server, &smb_msg, &sent);
405*4882a593Smuzhiyun 			if (rc < 0)
406*4882a593Smuzhiyun 				break;
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun 			total_len += sent;
409*4882a593Smuzhiyun 		}
410*4882a593Smuzhiyun 	}
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun unmask:
413*4882a593Smuzhiyun 	sigprocmask(SIG_SETMASK, &oldmask, NULL);
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun 	/*
416*4882a593Smuzhiyun 	 * If signal is pending but we have already sent the whole packet to
417*4882a593Smuzhiyun 	 * the server we need to return success status to allow a corresponding
418*4882a593Smuzhiyun 	 * mid entry to be kept in the pending requests queue thus allowing
419*4882a593Smuzhiyun 	 * to handle responses from the server by the client.
420*4882a593Smuzhiyun 	 *
421*4882a593Smuzhiyun 	 * If only part of the packet has been sent there is no need to hide
422*4882a593Smuzhiyun 	 * interrupt because the session will be reconnected anyway, so there
423*4882a593Smuzhiyun 	 * won't be any response from the server to handle.
424*4882a593Smuzhiyun 	 */
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun 	if (signal_pending(current) && (total_len != send_length)) {
427*4882a593Smuzhiyun 		cifs_dbg(FYI, "signal is pending after attempt to send\n");
428*4882a593Smuzhiyun 		rc = -ERESTARTSYS;
429*4882a593Smuzhiyun 	}
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun 	/* uncork it */
432*4882a593Smuzhiyun 	tcp_sock_set_cork(ssocket->sk, false);
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun 	if ((total_len > 0) && (total_len != send_length)) {
435*4882a593Smuzhiyun 		cifs_dbg(FYI, "partial send (wanted=%u sent=%zu): terminating session\n",
436*4882a593Smuzhiyun 			 send_length, total_len);
437*4882a593Smuzhiyun 		/*
438*4882a593Smuzhiyun 		 * If we have only sent part of an SMB then the next SMB could
439*4882a593Smuzhiyun 		 * be taken as the remainder of this one. We need to kill the
440*4882a593Smuzhiyun 		 * socket so the server throws away the partial SMB
441*4882a593Smuzhiyun 		 */
442*4882a593Smuzhiyun 		server->tcpStatus = CifsNeedReconnect;
443*4882a593Smuzhiyun 		trace_smb3_partial_send_reconnect(server->CurrentMid,
444*4882a593Smuzhiyun 						  server->hostname);
445*4882a593Smuzhiyun 	}
446*4882a593Smuzhiyun smbd_done:
447*4882a593Smuzhiyun 	if (rc < 0 && rc != -EINTR)
448*4882a593Smuzhiyun 		cifs_server_dbg(VFS, "Error %d sending data on socket to server\n",
449*4882a593Smuzhiyun 			 rc);
450*4882a593Smuzhiyun 	else if (rc > 0)
451*4882a593Smuzhiyun 		rc = 0;
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun 	return rc;
454*4882a593Smuzhiyun }
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun static int
smb_send_rqst(struct TCP_Server_Info * server,int num_rqst,struct smb_rqst * rqst,int flags)457*4882a593Smuzhiyun smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
458*4882a593Smuzhiyun 	      struct smb_rqst *rqst, int flags)
459*4882a593Smuzhiyun {
460*4882a593Smuzhiyun 	struct kvec iov;
461*4882a593Smuzhiyun 	struct smb2_transform_hdr *tr_hdr;
462*4882a593Smuzhiyun 	struct smb_rqst cur_rqst[MAX_COMPOUND];
463*4882a593Smuzhiyun 	int rc;
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun 	if (!(flags & CIFS_TRANSFORM_REQ))
466*4882a593Smuzhiyun 		return __smb_send_rqst(server, num_rqst, rqst);
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun 	if (num_rqst > MAX_COMPOUND - 1)
469*4882a593Smuzhiyun 		return -ENOMEM;
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun 	if (!server->ops->init_transform_rq) {
472*4882a593Smuzhiyun 		cifs_server_dbg(VFS, "Encryption requested but transform callback is missing\n");
473*4882a593Smuzhiyun 		return -EIO;
474*4882a593Smuzhiyun 	}
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun 	tr_hdr = kmalloc(sizeof(*tr_hdr), GFP_NOFS);
477*4882a593Smuzhiyun 	if (!tr_hdr)
478*4882a593Smuzhiyun 		return -ENOMEM;
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun 	memset(&cur_rqst[0], 0, sizeof(cur_rqst));
481*4882a593Smuzhiyun 	memset(&iov, 0, sizeof(iov));
482*4882a593Smuzhiyun 	memset(tr_hdr, 0, sizeof(*tr_hdr));
483*4882a593Smuzhiyun 
484*4882a593Smuzhiyun 	iov.iov_base = tr_hdr;
485*4882a593Smuzhiyun 	iov.iov_len = sizeof(*tr_hdr);
486*4882a593Smuzhiyun 	cur_rqst[0].rq_iov = &iov;
487*4882a593Smuzhiyun 	cur_rqst[0].rq_nvec = 1;
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun 	rc = server->ops->init_transform_rq(server, num_rqst + 1,
490*4882a593Smuzhiyun 					    &cur_rqst[0], rqst);
491*4882a593Smuzhiyun 	if (rc)
492*4882a593Smuzhiyun 		goto out;
493*4882a593Smuzhiyun 
494*4882a593Smuzhiyun 	rc = __smb_send_rqst(server, num_rqst + 1, &cur_rqst[0]);
495*4882a593Smuzhiyun 	smb3_free_compound_rqst(num_rqst, &cur_rqst[1]);
496*4882a593Smuzhiyun out:
497*4882a593Smuzhiyun 	kfree(tr_hdr);
498*4882a593Smuzhiyun 	return rc;
499*4882a593Smuzhiyun }
500*4882a593Smuzhiyun 
501*4882a593Smuzhiyun int
smb_send(struct TCP_Server_Info * server,struct smb_hdr * smb_buffer,unsigned int smb_buf_length)502*4882a593Smuzhiyun smb_send(struct TCP_Server_Info *server, struct smb_hdr *smb_buffer,
503*4882a593Smuzhiyun 	 unsigned int smb_buf_length)
504*4882a593Smuzhiyun {
505*4882a593Smuzhiyun 	struct kvec iov[2];
506*4882a593Smuzhiyun 	struct smb_rqst rqst = { .rq_iov = iov,
507*4882a593Smuzhiyun 				 .rq_nvec = 2 };
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun 	iov[0].iov_base = smb_buffer;
510*4882a593Smuzhiyun 	iov[0].iov_len = 4;
511*4882a593Smuzhiyun 	iov[1].iov_base = (char *)smb_buffer + 4;
512*4882a593Smuzhiyun 	iov[1].iov_len = smb_buf_length;
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun 	return __smb_send_rqst(server, 1, &rqst);
515*4882a593Smuzhiyun }
516*4882a593Smuzhiyun 
517*4882a593Smuzhiyun static int
wait_for_free_credits(struct TCP_Server_Info * server,const int num_credits,const int timeout,const int flags,unsigned int * instance)518*4882a593Smuzhiyun wait_for_free_credits(struct TCP_Server_Info *server, const int num_credits,
519*4882a593Smuzhiyun 		      const int timeout, const int flags,
520*4882a593Smuzhiyun 		      unsigned int *instance)
521*4882a593Smuzhiyun {
522*4882a593Smuzhiyun 	long rc;
523*4882a593Smuzhiyun 	int *credits;
524*4882a593Smuzhiyun 	int optype;
525*4882a593Smuzhiyun 	long int t;
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun 	if (timeout < 0)
528*4882a593Smuzhiyun 		t = MAX_JIFFY_OFFSET;
529*4882a593Smuzhiyun 	else
530*4882a593Smuzhiyun 		t = msecs_to_jiffies(timeout);
531*4882a593Smuzhiyun 
532*4882a593Smuzhiyun 	optype = flags & CIFS_OP_MASK;
533*4882a593Smuzhiyun 
534*4882a593Smuzhiyun 	*instance = 0;
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun 	credits = server->ops->get_credits_field(server, optype);
537*4882a593Smuzhiyun 	/* Since an echo is already inflight, no need to wait to send another */
538*4882a593Smuzhiyun 	if (*credits <= 0 && optype == CIFS_ECHO_OP)
539*4882a593Smuzhiyun 		return -EAGAIN;
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun 	spin_lock(&server->req_lock);
542*4882a593Smuzhiyun 	if ((flags & CIFS_TIMEOUT_MASK) == CIFS_NON_BLOCKING) {
543*4882a593Smuzhiyun 		/* oplock breaks must not be held up */
544*4882a593Smuzhiyun 		server->in_flight++;
545*4882a593Smuzhiyun 		if (server->in_flight > server->max_in_flight)
546*4882a593Smuzhiyun 			server->max_in_flight = server->in_flight;
547*4882a593Smuzhiyun 		*credits -= 1;
548*4882a593Smuzhiyun 		*instance = server->reconnect_instance;
549*4882a593Smuzhiyun 		spin_unlock(&server->req_lock);
550*4882a593Smuzhiyun 		return 0;
551*4882a593Smuzhiyun 	}
552*4882a593Smuzhiyun 
553*4882a593Smuzhiyun 	while (1) {
554*4882a593Smuzhiyun 		if (*credits < num_credits) {
555*4882a593Smuzhiyun 			spin_unlock(&server->req_lock);
556*4882a593Smuzhiyun 			cifs_num_waiters_inc(server);
557*4882a593Smuzhiyun 			rc = wait_event_killable_timeout(server->request_q,
558*4882a593Smuzhiyun 				has_credits(server, credits, num_credits), t);
559*4882a593Smuzhiyun 			cifs_num_waiters_dec(server);
560*4882a593Smuzhiyun 			if (!rc) {
561*4882a593Smuzhiyun 				trace_smb3_credit_timeout(server->CurrentMid,
562*4882a593Smuzhiyun 					server->hostname, num_credits, 0);
563*4882a593Smuzhiyun 				cifs_server_dbg(VFS, "wait timed out after %d ms\n",
564*4882a593Smuzhiyun 					 timeout);
565*4882a593Smuzhiyun 				return -ENOTSUPP;
566*4882a593Smuzhiyun 			}
567*4882a593Smuzhiyun 			if (rc == -ERESTARTSYS)
568*4882a593Smuzhiyun 				return -ERESTARTSYS;
569*4882a593Smuzhiyun 			spin_lock(&server->req_lock);
570*4882a593Smuzhiyun 		} else {
571*4882a593Smuzhiyun 			if (server->tcpStatus == CifsExiting) {
572*4882a593Smuzhiyun 				spin_unlock(&server->req_lock);
573*4882a593Smuzhiyun 				return -ENOENT;
574*4882a593Smuzhiyun 			}
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun 			/*
577*4882a593Smuzhiyun 			 * For normal commands, reserve the last MAX_COMPOUND
578*4882a593Smuzhiyun 			 * credits to compound requests.
579*4882a593Smuzhiyun 			 * Otherwise these compounds could be permanently
580*4882a593Smuzhiyun 			 * starved for credits by single-credit requests.
581*4882a593Smuzhiyun 			 *
582*4882a593Smuzhiyun 			 * To prevent spinning CPU, block this thread until
583*4882a593Smuzhiyun 			 * there are >MAX_COMPOUND credits available.
584*4882a593Smuzhiyun 			 * But only do this is we already have a lot of
585*4882a593Smuzhiyun 			 * credits in flight to avoid triggering this check
586*4882a593Smuzhiyun 			 * for servers that are slow to hand out credits on
587*4882a593Smuzhiyun 			 * new sessions.
588*4882a593Smuzhiyun 			 */
589*4882a593Smuzhiyun 			if (!optype && num_credits == 1 &&
590*4882a593Smuzhiyun 			    server->in_flight > 2 * MAX_COMPOUND &&
591*4882a593Smuzhiyun 			    *credits <= MAX_COMPOUND) {
592*4882a593Smuzhiyun 				spin_unlock(&server->req_lock);
593*4882a593Smuzhiyun 				cifs_num_waiters_inc(server);
594*4882a593Smuzhiyun 				rc = wait_event_killable_timeout(
595*4882a593Smuzhiyun 					server->request_q,
596*4882a593Smuzhiyun 					has_credits(server, credits,
597*4882a593Smuzhiyun 						    MAX_COMPOUND + 1),
598*4882a593Smuzhiyun 					t);
599*4882a593Smuzhiyun 				cifs_num_waiters_dec(server);
600*4882a593Smuzhiyun 				if (!rc) {
601*4882a593Smuzhiyun 					trace_smb3_credit_timeout(
602*4882a593Smuzhiyun 						server->CurrentMid,
603*4882a593Smuzhiyun 						server->hostname, num_credits,
604*4882a593Smuzhiyun 						0);
605*4882a593Smuzhiyun 					cifs_server_dbg(VFS, "wait timed out after %d ms\n",
606*4882a593Smuzhiyun 						 timeout);
607*4882a593Smuzhiyun 					return -ENOTSUPP;
608*4882a593Smuzhiyun 				}
609*4882a593Smuzhiyun 				if (rc == -ERESTARTSYS)
610*4882a593Smuzhiyun 					return -ERESTARTSYS;
611*4882a593Smuzhiyun 				spin_lock(&server->req_lock);
612*4882a593Smuzhiyun 				continue;
613*4882a593Smuzhiyun 			}
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun 			/*
616*4882a593Smuzhiyun 			 * Can not count locking commands against total
617*4882a593Smuzhiyun 			 * as they are allowed to block on server.
618*4882a593Smuzhiyun 			 */
619*4882a593Smuzhiyun 
620*4882a593Smuzhiyun 			/* update # of requests on the wire to server */
621*4882a593Smuzhiyun 			if ((flags & CIFS_TIMEOUT_MASK) != CIFS_BLOCKING_OP) {
622*4882a593Smuzhiyun 				*credits -= num_credits;
623*4882a593Smuzhiyun 				server->in_flight += num_credits;
624*4882a593Smuzhiyun 				if (server->in_flight > server->max_in_flight)
625*4882a593Smuzhiyun 					server->max_in_flight = server->in_flight;
626*4882a593Smuzhiyun 				*instance = server->reconnect_instance;
627*4882a593Smuzhiyun 			}
628*4882a593Smuzhiyun 			spin_unlock(&server->req_lock);
629*4882a593Smuzhiyun 			break;
630*4882a593Smuzhiyun 		}
631*4882a593Smuzhiyun 	}
632*4882a593Smuzhiyun 	return 0;
633*4882a593Smuzhiyun }
634*4882a593Smuzhiyun 
635*4882a593Smuzhiyun static int
wait_for_free_request(struct TCP_Server_Info * server,const int flags,unsigned int * instance)636*4882a593Smuzhiyun wait_for_free_request(struct TCP_Server_Info *server, const int flags,
637*4882a593Smuzhiyun 		      unsigned int *instance)
638*4882a593Smuzhiyun {
639*4882a593Smuzhiyun 	return wait_for_free_credits(server, 1, -1, flags,
640*4882a593Smuzhiyun 				     instance);
641*4882a593Smuzhiyun }
642*4882a593Smuzhiyun 
643*4882a593Smuzhiyun static int
wait_for_compound_request(struct TCP_Server_Info * server,int num,const int flags,unsigned int * instance)644*4882a593Smuzhiyun wait_for_compound_request(struct TCP_Server_Info *server, int num,
645*4882a593Smuzhiyun 			  const int flags, unsigned int *instance)
646*4882a593Smuzhiyun {
647*4882a593Smuzhiyun 	int *credits;
648*4882a593Smuzhiyun 
649*4882a593Smuzhiyun 	credits = server->ops->get_credits_field(server, flags & CIFS_OP_MASK);
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun 	spin_lock(&server->req_lock);
652*4882a593Smuzhiyun 	if (*credits < num) {
653*4882a593Smuzhiyun 		/*
654*4882a593Smuzhiyun 		 * If the server is tight on resources or just gives us less
655*4882a593Smuzhiyun 		 * credits for other reasons (e.g. requests are coming out of
656*4882a593Smuzhiyun 		 * order and the server delays granting more credits until it
657*4882a593Smuzhiyun 		 * processes a missing mid) and we exhausted most available
658*4882a593Smuzhiyun 		 * credits there may be situations when we try to send
659*4882a593Smuzhiyun 		 * a compound request but we don't have enough credits. At this
660*4882a593Smuzhiyun 		 * point the client needs to decide if it should wait for
661*4882a593Smuzhiyun 		 * additional credits or fail the request. If at least one
662*4882a593Smuzhiyun 		 * request is in flight there is a high probability that the
663*4882a593Smuzhiyun 		 * server will return enough credits to satisfy this compound
664*4882a593Smuzhiyun 		 * request.
665*4882a593Smuzhiyun 		 *
666*4882a593Smuzhiyun 		 * Return immediately if no requests in flight since we will be
667*4882a593Smuzhiyun 		 * stuck on waiting for credits.
668*4882a593Smuzhiyun 		 */
669*4882a593Smuzhiyun 		if (server->in_flight == 0) {
670*4882a593Smuzhiyun 			spin_unlock(&server->req_lock);
671*4882a593Smuzhiyun 			return -ENOTSUPP;
672*4882a593Smuzhiyun 		}
673*4882a593Smuzhiyun 	}
674*4882a593Smuzhiyun 	spin_unlock(&server->req_lock);
675*4882a593Smuzhiyun 
676*4882a593Smuzhiyun 	return wait_for_free_credits(server, num, 60000, flags,
677*4882a593Smuzhiyun 				     instance);
678*4882a593Smuzhiyun }
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun int
cifs_wait_mtu_credits(struct TCP_Server_Info * server,unsigned int size,unsigned int * num,struct cifs_credits * credits)681*4882a593Smuzhiyun cifs_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
682*4882a593Smuzhiyun 		      unsigned int *num, struct cifs_credits *credits)
683*4882a593Smuzhiyun {
684*4882a593Smuzhiyun 	*num = size;
685*4882a593Smuzhiyun 	credits->value = 0;
686*4882a593Smuzhiyun 	credits->instance = server->reconnect_instance;
687*4882a593Smuzhiyun 	return 0;
688*4882a593Smuzhiyun }
689*4882a593Smuzhiyun 
allocate_mid(struct cifs_ses * ses,struct smb_hdr * in_buf,struct mid_q_entry ** ppmidQ)690*4882a593Smuzhiyun static int allocate_mid(struct cifs_ses *ses, struct smb_hdr *in_buf,
691*4882a593Smuzhiyun 			struct mid_q_entry **ppmidQ)
692*4882a593Smuzhiyun {
693*4882a593Smuzhiyun 	if (ses->server->tcpStatus == CifsExiting) {
694*4882a593Smuzhiyun 		return -ENOENT;
695*4882a593Smuzhiyun 	}
696*4882a593Smuzhiyun 
697*4882a593Smuzhiyun 	if (ses->server->tcpStatus == CifsNeedReconnect) {
698*4882a593Smuzhiyun 		cifs_dbg(FYI, "tcp session dead - return to caller to retry\n");
699*4882a593Smuzhiyun 		return -EAGAIN;
700*4882a593Smuzhiyun 	}
701*4882a593Smuzhiyun 
702*4882a593Smuzhiyun 	if (ses->status == CifsNew) {
703*4882a593Smuzhiyun 		if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
704*4882a593Smuzhiyun 			(in_buf->Command != SMB_COM_NEGOTIATE))
705*4882a593Smuzhiyun 			return -EAGAIN;
706*4882a593Smuzhiyun 		/* else ok - we are setting up session */
707*4882a593Smuzhiyun 	}
708*4882a593Smuzhiyun 
709*4882a593Smuzhiyun 	if (ses->status == CifsExiting) {
710*4882a593Smuzhiyun 		/* check if SMB session is bad because we are setting it up */
711*4882a593Smuzhiyun 		if (in_buf->Command != SMB_COM_LOGOFF_ANDX)
712*4882a593Smuzhiyun 			return -EAGAIN;
713*4882a593Smuzhiyun 		/* else ok - we are shutting down session */
714*4882a593Smuzhiyun 	}
715*4882a593Smuzhiyun 
716*4882a593Smuzhiyun 	*ppmidQ = AllocMidQEntry(in_buf, ses->server);
717*4882a593Smuzhiyun 	if (*ppmidQ == NULL)
718*4882a593Smuzhiyun 		return -ENOMEM;
719*4882a593Smuzhiyun 	spin_lock(&GlobalMid_Lock);
720*4882a593Smuzhiyun 	list_add_tail(&(*ppmidQ)->qhead, &ses->server->pending_mid_q);
721*4882a593Smuzhiyun 	spin_unlock(&GlobalMid_Lock);
722*4882a593Smuzhiyun 	return 0;
723*4882a593Smuzhiyun }
724*4882a593Smuzhiyun 
725*4882a593Smuzhiyun static int
wait_for_response(struct TCP_Server_Info * server,struct mid_q_entry * midQ)726*4882a593Smuzhiyun wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *midQ)
727*4882a593Smuzhiyun {
728*4882a593Smuzhiyun 	int error;
729*4882a593Smuzhiyun 
730*4882a593Smuzhiyun 	error = wait_event_freezekillable_unsafe(server->response_q,
731*4882a593Smuzhiyun 				    midQ->mid_state != MID_REQUEST_SUBMITTED);
732*4882a593Smuzhiyun 	if (error < 0)
733*4882a593Smuzhiyun 		return -ERESTARTSYS;
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun 	return 0;
736*4882a593Smuzhiyun }
737*4882a593Smuzhiyun 
738*4882a593Smuzhiyun struct mid_q_entry *
cifs_setup_async_request(struct TCP_Server_Info * server,struct smb_rqst * rqst)739*4882a593Smuzhiyun cifs_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
740*4882a593Smuzhiyun {
741*4882a593Smuzhiyun 	int rc;
742*4882a593Smuzhiyun 	struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
743*4882a593Smuzhiyun 	struct mid_q_entry *mid;
744*4882a593Smuzhiyun 
745*4882a593Smuzhiyun 	if (rqst->rq_iov[0].iov_len != 4 ||
746*4882a593Smuzhiyun 	    rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
747*4882a593Smuzhiyun 		return ERR_PTR(-EIO);
748*4882a593Smuzhiyun 
749*4882a593Smuzhiyun 	/* enable signing if server requires it */
750*4882a593Smuzhiyun 	if (server->sign)
751*4882a593Smuzhiyun 		hdr->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
752*4882a593Smuzhiyun 
753*4882a593Smuzhiyun 	mid = AllocMidQEntry(hdr, server);
754*4882a593Smuzhiyun 	if (mid == NULL)
755*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
756*4882a593Smuzhiyun 
757*4882a593Smuzhiyun 	rc = cifs_sign_rqst(rqst, server, &mid->sequence_number);
758*4882a593Smuzhiyun 	if (rc) {
759*4882a593Smuzhiyun 		DeleteMidQEntry(mid);
760*4882a593Smuzhiyun 		return ERR_PTR(rc);
761*4882a593Smuzhiyun 	}
762*4882a593Smuzhiyun 
763*4882a593Smuzhiyun 	return mid;
764*4882a593Smuzhiyun }
765*4882a593Smuzhiyun 
766*4882a593Smuzhiyun /*
767*4882a593Smuzhiyun  * Send a SMB request and set the callback function in the mid to handle
768*4882a593Smuzhiyun  * the result. Caller is responsible for dealing with timeouts.
769*4882a593Smuzhiyun  */
770*4882a593Smuzhiyun int
cifs_call_async(struct TCP_Server_Info * server,struct smb_rqst * rqst,mid_receive_t * receive,mid_callback_t * callback,mid_handle_t * handle,void * cbdata,const int flags,const struct cifs_credits * exist_credits)771*4882a593Smuzhiyun cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst,
772*4882a593Smuzhiyun 		mid_receive_t *receive, mid_callback_t *callback,
773*4882a593Smuzhiyun 		mid_handle_t *handle, void *cbdata, const int flags,
774*4882a593Smuzhiyun 		const struct cifs_credits *exist_credits)
775*4882a593Smuzhiyun {
776*4882a593Smuzhiyun 	int rc;
777*4882a593Smuzhiyun 	struct mid_q_entry *mid;
778*4882a593Smuzhiyun 	struct cifs_credits credits = { .value = 0, .instance = 0 };
779*4882a593Smuzhiyun 	unsigned int instance;
780*4882a593Smuzhiyun 	int optype;
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun 	optype = flags & CIFS_OP_MASK;
783*4882a593Smuzhiyun 
784*4882a593Smuzhiyun 	if ((flags & CIFS_HAS_CREDITS) == 0) {
785*4882a593Smuzhiyun 		rc = wait_for_free_request(server, flags, &instance);
786*4882a593Smuzhiyun 		if (rc)
787*4882a593Smuzhiyun 			return rc;
788*4882a593Smuzhiyun 		credits.value = 1;
789*4882a593Smuzhiyun 		credits.instance = instance;
790*4882a593Smuzhiyun 	} else
791*4882a593Smuzhiyun 		instance = exist_credits->instance;
792*4882a593Smuzhiyun 
793*4882a593Smuzhiyun 	mutex_lock(&server->srv_mutex);
794*4882a593Smuzhiyun 
795*4882a593Smuzhiyun 	/*
796*4882a593Smuzhiyun 	 * We can't use credits obtained from the previous session to send this
797*4882a593Smuzhiyun 	 * request. Check if there were reconnects after we obtained credits and
798*4882a593Smuzhiyun 	 * return -EAGAIN in such cases to let callers handle it.
799*4882a593Smuzhiyun 	 */
800*4882a593Smuzhiyun 	if (instance != server->reconnect_instance) {
801*4882a593Smuzhiyun 		mutex_unlock(&server->srv_mutex);
802*4882a593Smuzhiyun 		add_credits_and_wake_if(server, &credits, optype);
803*4882a593Smuzhiyun 		return -EAGAIN;
804*4882a593Smuzhiyun 	}
805*4882a593Smuzhiyun 
806*4882a593Smuzhiyun 	mid = server->ops->setup_async_request(server, rqst);
807*4882a593Smuzhiyun 	if (IS_ERR(mid)) {
808*4882a593Smuzhiyun 		mutex_unlock(&server->srv_mutex);
809*4882a593Smuzhiyun 		add_credits_and_wake_if(server, &credits, optype);
810*4882a593Smuzhiyun 		return PTR_ERR(mid);
811*4882a593Smuzhiyun 	}
812*4882a593Smuzhiyun 
813*4882a593Smuzhiyun 	mid->receive = receive;
814*4882a593Smuzhiyun 	mid->callback = callback;
815*4882a593Smuzhiyun 	mid->callback_data = cbdata;
816*4882a593Smuzhiyun 	mid->handle = handle;
817*4882a593Smuzhiyun 	mid->mid_state = MID_REQUEST_SUBMITTED;
818*4882a593Smuzhiyun 
819*4882a593Smuzhiyun 	/* put it on the pending_mid_q */
820*4882a593Smuzhiyun 	spin_lock(&GlobalMid_Lock);
821*4882a593Smuzhiyun 	list_add_tail(&mid->qhead, &server->pending_mid_q);
822*4882a593Smuzhiyun 	spin_unlock(&GlobalMid_Lock);
823*4882a593Smuzhiyun 
824*4882a593Smuzhiyun 	/*
825*4882a593Smuzhiyun 	 * Need to store the time in mid before calling I/O. For call_async,
826*4882a593Smuzhiyun 	 * I/O response may come back and free the mid entry on another thread.
827*4882a593Smuzhiyun 	 */
828*4882a593Smuzhiyun 	cifs_save_when_sent(mid);
829*4882a593Smuzhiyun 	cifs_in_send_inc(server);
830*4882a593Smuzhiyun 	rc = smb_send_rqst(server, 1, rqst, flags);
831*4882a593Smuzhiyun 	cifs_in_send_dec(server);
832*4882a593Smuzhiyun 
833*4882a593Smuzhiyun 	if (rc < 0) {
834*4882a593Smuzhiyun 		revert_current_mid(server, mid->credits);
835*4882a593Smuzhiyun 		server->sequence_number -= 2;
836*4882a593Smuzhiyun 		cifs_delete_mid(mid);
837*4882a593Smuzhiyun 	}
838*4882a593Smuzhiyun 
839*4882a593Smuzhiyun 	mutex_unlock(&server->srv_mutex);
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun 	if (rc == 0)
842*4882a593Smuzhiyun 		return 0;
843*4882a593Smuzhiyun 
844*4882a593Smuzhiyun 	add_credits_and_wake_if(server, &credits, optype);
845*4882a593Smuzhiyun 	return rc;
846*4882a593Smuzhiyun }
847*4882a593Smuzhiyun 
848*4882a593Smuzhiyun /*
849*4882a593Smuzhiyun  *
850*4882a593Smuzhiyun  * Send an SMB Request.  No response info (other than return code)
851*4882a593Smuzhiyun  * needs to be parsed.
852*4882a593Smuzhiyun  *
853*4882a593Smuzhiyun  * flags indicate the type of request buffer and how long to wait
854*4882a593Smuzhiyun  * and whether to log NT STATUS code (error) before mapping it to POSIX error
855*4882a593Smuzhiyun  *
856*4882a593Smuzhiyun  */
857*4882a593Smuzhiyun int
SendReceiveNoRsp(const unsigned int xid,struct cifs_ses * ses,char * in_buf,int flags)858*4882a593Smuzhiyun SendReceiveNoRsp(const unsigned int xid, struct cifs_ses *ses,
859*4882a593Smuzhiyun 		 char *in_buf, int flags)
860*4882a593Smuzhiyun {
861*4882a593Smuzhiyun 	int rc;
862*4882a593Smuzhiyun 	struct kvec iov[1];
863*4882a593Smuzhiyun 	struct kvec rsp_iov;
864*4882a593Smuzhiyun 	int resp_buf_type;
865*4882a593Smuzhiyun 
866*4882a593Smuzhiyun 	iov[0].iov_base = in_buf;
867*4882a593Smuzhiyun 	iov[0].iov_len = get_rfc1002_length(in_buf) + 4;
868*4882a593Smuzhiyun 	flags |= CIFS_NO_RSP_BUF;
869*4882a593Smuzhiyun 	rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
870*4882a593Smuzhiyun 	cifs_dbg(NOISY, "SendRcvNoRsp flags %d rc %d\n", flags, rc);
871*4882a593Smuzhiyun 
872*4882a593Smuzhiyun 	return rc;
873*4882a593Smuzhiyun }
874*4882a593Smuzhiyun 
875*4882a593Smuzhiyun static int
cifs_sync_mid_result(struct mid_q_entry * mid,struct TCP_Server_Info * server)876*4882a593Smuzhiyun cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
877*4882a593Smuzhiyun {
878*4882a593Smuzhiyun 	int rc = 0;
879*4882a593Smuzhiyun 
880*4882a593Smuzhiyun 	cifs_dbg(FYI, "%s: cmd=%d mid=%llu state=%d\n",
881*4882a593Smuzhiyun 		 __func__, le16_to_cpu(mid->command), mid->mid, mid->mid_state);
882*4882a593Smuzhiyun 
883*4882a593Smuzhiyun 	spin_lock(&GlobalMid_Lock);
884*4882a593Smuzhiyun 	switch (mid->mid_state) {
885*4882a593Smuzhiyun 	case MID_RESPONSE_RECEIVED:
886*4882a593Smuzhiyun 		spin_unlock(&GlobalMid_Lock);
887*4882a593Smuzhiyun 		return rc;
888*4882a593Smuzhiyun 	case MID_RETRY_NEEDED:
889*4882a593Smuzhiyun 		rc = -EAGAIN;
890*4882a593Smuzhiyun 		break;
891*4882a593Smuzhiyun 	case MID_RESPONSE_MALFORMED:
892*4882a593Smuzhiyun 		rc = -EIO;
893*4882a593Smuzhiyun 		break;
894*4882a593Smuzhiyun 	case MID_SHUTDOWN:
895*4882a593Smuzhiyun 		rc = -EHOSTDOWN;
896*4882a593Smuzhiyun 		break;
897*4882a593Smuzhiyun 	default:
898*4882a593Smuzhiyun 		if (!(mid->mid_flags & MID_DELETED)) {
899*4882a593Smuzhiyun 			list_del_init(&mid->qhead);
900*4882a593Smuzhiyun 			mid->mid_flags |= MID_DELETED;
901*4882a593Smuzhiyun 		}
902*4882a593Smuzhiyun 		cifs_server_dbg(VFS, "%s: invalid mid state mid=%llu state=%d\n",
903*4882a593Smuzhiyun 			 __func__, mid->mid, mid->mid_state);
904*4882a593Smuzhiyun 		rc = -EIO;
905*4882a593Smuzhiyun 	}
906*4882a593Smuzhiyun 	spin_unlock(&GlobalMid_Lock);
907*4882a593Smuzhiyun 
908*4882a593Smuzhiyun 	DeleteMidQEntry(mid);
909*4882a593Smuzhiyun 	return rc;
910*4882a593Smuzhiyun }
911*4882a593Smuzhiyun 
912*4882a593Smuzhiyun static inline int
send_cancel(struct TCP_Server_Info * server,struct smb_rqst * rqst,struct mid_q_entry * mid)913*4882a593Smuzhiyun send_cancel(struct TCP_Server_Info *server, struct smb_rqst *rqst,
914*4882a593Smuzhiyun 	    struct mid_q_entry *mid)
915*4882a593Smuzhiyun {
916*4882a593Smuzhiyun 	return server->ops->send_cancel ?
917*4882a593Smuzhiyun 				server->ops->send_cancel(server, rqst, mid) : 0;
918*4882a593Smuzhiyun }
919*4882a593Smuzhiyun 
920*4882a593Smuzhiyun int
cifs_check_receive(struct mid_q_entry * mid,struct TCP_Server_Info * server,bool log_error)921*4882a593Smuzhiyun cifs_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
922*4882a593Smuzhiyun 		   bool log_error)
923*4882a593Smuzhiyun {
924*4882a593Smuzhiyun 	unsigned int len = get_rfc1002_length(mid->resp_buf) + 4;
925*4882a593Smuzhiyun 
926*4882a593Smuzhiyun 	dump_smb(mid->resp_buf, min_t(u32, 92, len));
927*4882a593Smuzhiyun 
928*4882a593Smuzhiyun 	/* convert the length into a more usable form */
929*4882a593Smuzhiyun 	if (server->sign) {
930*4882a593Smuzhiyun 		struct kvec iov[2];
931*4882a593Smuzhiyun 		int rc = 0;
932*4882a593Smuzhiyun 		struct smb_rqst rqst = { .rq_iov = iov,
933*4882a593Smuzhiyun 					 .rq_nvec = 2 };
934*4882a593Smuzhiyun 
935*4882a593Smuzhiyun 		iov[0].iov_base = mid->resp_buf;
936*4882a593Smuzhiyun 		iov[0].iov_len = 4;
937*4882a593Smuzhiyun 		iov[1].iov_base = (char *)mid->resp_buf + 4;
938*4882a593Smuzhiyun 		iov[1].iov_len = len - 4;
939*4882a593Smuzhiyun 		/* FIXME: add code to kill session */
940*4882a593Smuzhiyun 		rc = cifs_verify_signature(&rqst, server,
941*4882a593Smuzhiyun 					   mid->sequence_number);
942*4882a593Smuzhiyun 		if (rc)
943*4882a593Smuzhiyun 			cifs_server_dbg(VFS, "SMB signature verification returned error = %d\n",
944*4882a593Smuzhiyun 				 rc);
945*4882a593Smuzhiyun 	}
946*4882a593Smuzhiyun 
947*4882a593Smuzhiyun 	/* BB special case reconnect tid and uid here? */
948*4882a593Smuzhiyun 	return map_and_check_smb_error(mid, log_error);
949*4882a593Smuzhiyun }
950*4882a593Smuzhiyun 
951*4882a593Smuzhiyun struct mid_q_entry *
cifs_setup_request(struct cifs_ses * ses,struct TCP_Server_Info * ignored,struct smb_rqst * rqst)952*4882a593Smuzhiyun cifs_setup_request(struct cifs_ses *ses, struct TCP_Server_Info *ignored,
953*4882a593Smuzhiyun 		   struct smb_rqst *rqst)
954*4882a593Smuzhiyun {
955*4882a593Smuzhiyun 	int rc;
956*4882a593Smuzhiyun 	struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
957*4882a593Smuzhiyun 	struct mid_q_entry *mid;
958*4882a593Smuzhiyun 
959*4882a593Smuzhiyun 	if (rqst->rq_iov[0].iov_len != 4 ||
960*4882a593Smuzhiyun 	    rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
961*4882a593Smuzhiyun 		return ERR_PTR(-EIO);
962*4882a593Smuzhiyun 
963*4882a593Smuzhiyun 	rc = allocate_mid(ses, hdr, &mid);
964*4882a593Smuzhiyun 	if (rc)
965*4882a593Smuzhiyun 		return ERR_PTR(rc);
966*4882a593Smuzhiyun 	rc = cifs_sign_rqst(rqst, ses->server, &mid->sequence_number);
967*4882a593Smuzhiyun 	if (rc) {
968*4882a593Smuzhiyun 		cifs_delete_mid(mid);
969*4882a593Smuzhiyun 		return ERR_PTR(rc);
970*4882a593Smuzhiyun 	}
971*4882a593Smuzhiyun 	return mid;
972*4882a593Smuzhiyun }
973*4882a593Smuzhiyun 
974*4882a593Smuzhiyun static void
cifs_compound_callback(struct mid_q_entry * mid)975*4882a593Smuzhiyun cifs_compound_callback(struct mid_q_entry *mid)
976*4882a593Smuzhiyun {
977*4882a593Smuzhiyun 	struct TCP_Server_Info *server = mid->server;
978*4882a593Smuzhiyun 	struct cifs_credits credits;
979*4882a593Smuzhiyun 
980*4882a593Smuzhiyun 	credits.value = server->ops->get_credits(mid);
981*4882a593Smuzhiyun 	credits.instance = server->reconnect_instance;
982*4882a593Smuzhiyun 
983*4882a593Smuzhiyun 	add_credits(server, &credits, mid->optype);
984*4882a593Smuzhiyun }
985*4882a593Smuzhiyun 
986*4882a593Smuzhiyun static void
cifs_compound_last_callback(struct mid_q_entry * mid)987*4882a593Smuzhiyun cifs_compound_last_callback(struct mid_q_entry *mid)
988*4882a593Smuzhiyun {
989*4882a593Smuzhiyun 	cifs_compound_callback(mid);
990*4882a593Smuzhiyun 	cifs_wake_up_task(mid);
991*4882a593Smuzhiyun }
992*4882a593Smuzhiyun 
993*4882a593Smuzhiyun static void
cifs_cancelled_callback(struct mid_q_entry * mid)994*4882a593Smuzhiyun cifs_cancelled_callback(struct mid_q_entry *mid)
995*4882a593Smuzhiyun {
996*4882a593Smuzhiyun 	cifs_compound_callback(mid);
997*4882a593Smuzhiyun 	DeleteMidQEntry(mid);
998*4882a593Smuzhiyun }
999*4882a593Smuzhiyun 
1000*4882a593Smuzhiyun /*
1001*4882a593Smuzhiyun  * Return a channel (master if none) of @ses that can be used to send
1002*4882a593Smuzhiyun  * regular requests.
1003*4882a593Smuzhiyun  *
1004*4882a593Smuzhiyun  * If we are currently binding a new channel (negprot/sess.setup),
1005*4882a593Smuzhiyun  * return the new incomplete channel.
1006*4882a593Smuzhiyun  */
cifs_pick_channel(struct cifs_ses * ses)1007*4882a593Smuzhiyun struct TCP_Server_Info *cifs_pick_channel(struct cifs_ses *ses)
1008*4882a593Smuzhiyun {
1009*4882a593Smuzhiyun 	uint index = 0;
1010*4882a593Smuzhiyun 
1011*4882a593Smuzhiyun 	if (!ses)
1012*4882a593Smuzhiyun 		return NULL;
1013*4882a593Smuzhiyun 
1014*4882a593Smuzhiyun 	if (!ses->binding) {
1015*4882a593Smuzhiyun 		/* round robin */
1016*4882a593Smuzhiyun 		if (ses->chan_count > 1) {
1017*4882a593Smuzhiyun 			index = (uint)atomic_inc_return(&ses->chan_seq);
1018*4882a593Smuzhiyun 			index %= ses->chan_count;
1019*4882a593Smuzhiyun 		}
1020*4882a593Smuzhiyun 		return ses->chans[index].server;
1021*4882a593Smuzhiyun 	} else {
1022*4882a593Smuzhiyun 		return cifs_ses_server(ses);
1023*4882a593Smuzhiyun 	}
1024*4882a593Smuzhiyun }
1025*4882a593Smuzhiyun 
1026*4882a593Smuzhiyun int
compound_send_recv(const unsigned int xid,struct cifs_ses * ses,struct TCP_Server_Info * server,const int flags,const int num_rqst,struct smb_rqst * rqst,int * resp_buf_type,struct kvec * resp_iov)1027*4882a593Smuzhiyun compound_send_recv(const unsigned int xid, struct cifs_ses *ses,
1028*4882a593Smuzhiyun 		   struct TCP_Server_Info *server,
1029*4882a593Smuzhiyun 		   const int flags, const int num_rqst, struct smb_rqst *rqst,
1030*4882a593Smuzhiyun 		   int *resp_buf_type, struct kvec *resp_iov)
1031*4882a593Smuzhiyun {
1032*4882a593Smuzhiyun 	int i, j, optype, rc = 0;
1033*4882a593Smuzhiyun 	struct mid_q_entry *midQ[MAX_COMPOUND];
1034*4882a593Smuzhiyun 	bool cancelled_mid[MAX_COMPOUND] = {false};
1035*4882a593Smuzhiyun 	struct cifs_credits credits[MAX_COMPOUND] = {
1036*4882a593Smuzhiyun 		{ .value = 0, .instance = 0 }
1037*4882a593Smuzhiyun 	};
1038*4882a593Smuzhiyun 	unsigned int instance;
1039*4882a593Smuzhiyun 	char *buf;
1040*4882a593Smuzhiyun 
1041*4882a593Smuzhiyun 	optype = flags & CIFS_OP_MASK;
1042*4882a593Smuzhiyun 
1043*4882a593Smuzhiyun 	for (i = 0; i < num_rqst; i++)
1044*4882a593Smuzhiyun 		resp_buf_type[i] = CIFS_NO_BUFFER;  /* no response buf yet */
1045*4882a593Smuzhiyun 
1046*4882a593Smuzhiyun 	if (!ses || !ses->server || !server) {
1047*4882a593Smuzhiyun 		cifs_dbg(VFS, "Null session\n");
1048*4882a593Smuzhiyun 		return -EIO;
1049*4882a593Smuzhiyun 	}
1050*4882a593Smuzhiyun 
1051*4882a593Smuzhiyun 	if (server->tcpStatus == CifsExiting)
1052*4882a593Smuzhiyun 		return -ENOENT;
1053*4882a593Smuzhiyun 
1054*4882a593Smuzhiyun 	/*
1055*4882a593Smuzhiyun 	 * Wait for all the requests to become available.
1056*4882a593Smuzhiyun 	 * This approach still leaves the possibility to be stuck waiting for
1057*4882a593Smuzhiyun 	 * credits if the server doesn't grant credits to the outstanding
1058*4882a593Smuzhiyun 	 * requests and if the client is completely idle, not generating any
1059*4882a593Smuzhiyun 	 * other requests.
1060*4882a593Smuzhiyun 	 * This can be handled by the eventual session reconnect.
1061*4882a593Smuzhiyun 	 */
1062*4882a593Smuzhiyun 	rc = wait_for_compound_request(server, num_rqst, flags,
1063*4882a593Smuzhiyun 				       &instance);
1064*4882a593Smuzhiyun 	if (rc)
1065*4882a593Smuzhiyun 		return rc;
1066*4882a593Smuzhiyun 
1067*4882a593Smuzhiyun 	for (i = 0; i < num_rqst; i++) {
1068*4882a593Smuzhiyun 		credits[i].value = 1;
1069*4882a593Smuzhiyun 		credits[i].instance = instance;
1070*4882a593Smuzhiyun 	}
1071*4882a593Smuzhiyun 
1072*4882a593Smuzhiyun 	/*
1073*4882a593Smuzhiyun 	 * Make sure that we sign in the same order that we send on this socket
1074*4882a593Smuzhiyun 	 * and avoid races inside tcp sendmsg code that could cause corruption
1075*4882a593Smuzhiyun 	 * of smb data.
1076*4882a593Smuzhiyun 	 */
1077*4882a593Smuzhiyun 
1078*4882a593Smuzhiyun 	mutex_lock(&server->srv_mutex);
1079*4882a593Smuzhiyun 
1080*4882a593Smuzhiyun 	/*
1081*4882a593Smuzhiyun 	 * All the parts of the compound chain belong obtained credits from the
1082*4882a593Smuzhiyun 	 * same session. We can not use credits obtained from the previous
1083*4882a593Smuzhiyun 	 * session to send this request. Check if there were reconnects after
1084*4882a593Smuzhiyun 	 * we obtained credits and return -EAGAIN in such cases to let callers
1085*4882a593Smuzhiyun 	 * handle it.
1086*4882a593Smuzhiyun 	 */
1087*4882a593Smuzhiyun 	if (instance != server->reconnect_instance) {
1088*4882a593Smuzhiyun 		mutex_unlock(&server->srv_mutex);
1089*4882a593Smuzhiyun 		for (j = 0; j < num_rqst; j++)
1090*4882a593Smuzhiyun 			add_credits(server, &credits[j], optype);
1091*4882a593Smuzhiyun 		return -EAGAIN;
1092*4882a593Smuzhiyun 	}
1093*4882a593Smuzhiyun 
1094*4882a593Smuzhiyun 	for (i = 0; i < num_rqst; i++) {
1095*4882a593Smuzhiyun 		midQ[i] = server->ops->setup_request(ses, server, &rqst[i]);
1096*4882a593Smuzhiyun 		if (IS_ERR(midQ[i])) {
1097*4882a593Smuzhiyun 			revert_current_mid(server, i);
1098*4882a593Smuzhiyun 			for (j = 0; j < i; j++)
1099*4882a593Smuzhiyun 				cifs_delete_mid(midQ[j]);
1100*4882a593Smuzhiyun 			mutex_unlock(&server->srv_mutex);
1101*4882a593Smuzhiyun 
1102*4882a593Smuzhiyun 			/* Update # of requests on wire to server */
1103*4882a593Smuzhiyun 			for (j = 0; j < num_rqst; j++)
1104*4882a593Smuzhiyun 				add_credits(server, &credits[j], optype);
1105*4882a593Smuzhiyun 			return PTR_ERR(midQ[i]);
1106*4882a593Smuzhiyun 		}
1107*4882a593Smuzhiyun 
1108*4882a593Smuzhiyun 		midQ[i]->mid_state = MID_REQUEST_SUBMITTED;
1109*4882a593Smuzhiyun 		midQ[i]->optype = optype;
1110*4882a593Smuzhiyun 		/*
1111*4882a593Smuzhiyun 		 * Invoke callback for every part of the compound chain
1112*4882a593Smuzhiyun 		 * to calculate credits properly. Wake up this thread only when
1113*4882a593Smuzhiyun 		 * the last element is received.
1114*4882a593Smuzhiyun 		 */
1115*4882a593Smuzhiyun 		if (i < num_rqst - 1)
1116*4882a593Smuzhiyun 			midQ[i]->callback = cifs_compound_callback;
1117*4882a593Smuzhiyun 		else
1118*4882a593Smuzhiyun 			midQ[i]->callback = cifs_compound_last_callback;
1119*4882a593Smuzhiyun 	}
1120*4882a593Smuzhiyun 	cifs_in_send_inc(server);
1121*4882a593Smuzhiyun 	rc = smb_send_rqst(server, num_rqst, rqst, flags);
1122*4882a593Smuzhiyun 	cifs_in_send_dec(server);
1123*4882a593Smuzhiyun 
1124*4882a593Smuzhiyun 	for (i = 0; i < num_rqst; i++)
1125*4882a593Smuzhiyun 		cifs_save_when_sent(midQ[i]);
1126*4882a593Smuzhiyun 
1127*4882a593Smuzhiyun 	if (rc < 0) {
1128*4882a593Smuzhiyun 		revert_current_mid(server, num_rqst);
1129*4882a593Smuzhiyun 		server->sequence_number -= 2;
1130*4882a593Smuzhiyun 	}
1131*4882a593Smuzhiyun 
1132*4882a593Smuzhiyun 	mutex_unlock(&server->srv_mutex);
1133*4882a593Smuzhiyun 
1134*4882a593Smuzhiyun 	/*
1135*4882a593Smuzhiyun 	 * If sending failed for some reason or it is an oplock break that we
1136*4882a593Smuzhiyun 	 * will not receive a response to - return credits back
1137*4882a593Smuzhiyun 	 */
1138*4882a593Smuzhiyun 	if (rc < 0 || (flags & CIFS_NO_SRV_RSP)) {
1139*4882a593Smuzhiyun 		for (i = 0; i < num_rqst; i++)
1140*4882a593Smuzhiyun 			add_credits(server, &credits[i], optype);
1141*4882a593Smuzhiyun 		goto out;
1142*4882a593Smuzhiyun 	}
1143*4882a593Smuzhiyun 
1144*4882a593Smuzhiyun 	/*
1145*4882a593Smuzhiyun 	 * At this point the request is passed to the network stack - we assume
1146*4882a593Smuzhiyun 	 * that any credits taken from the server structure on the client have
1147*4882a593Smuzhiyun 	 * been spent and we can't return them back. Once we receive responses
1148*4882a593Smuzhiyun 	 * we will collect credits granted by the server in the mid callbacks
1149*4882a593Smuzhiyun 	 * and add those credits to the server structure.
1150*4882a593Smuzhiyun 	 */
1151*4882a593Smuzhiyun 
1152*4882a593Smuzhiyun 	/*
1153*4882a593Smuzhiyun 	 * Compounding is never used during session establish.
1154*4882a593Smuzhiyun 	 */
1155*4882a593Smuzhiyun 	if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP)) {
1156*4882a593Smuzhiyun 		mutex_lock(&server->srv_mutex);
1157*4882a593Smuzhiyun 		smb311_update_preauth_hash(ses, rqst[0].rq_iov,
1158*4882a593Smuzhiyun 					   rqst[0].rq_nvec);
1159*4882a593Smuzhiyun 		mutex_unlock(&server->srv_mutex);
1160*4882a593Smuzhiyun 	}
1161*4882a593Smuzhiyun 
1162*4882a593Smuzhiyun 	for (i = 0; i < num_rqst; i++) {
1163*4882a593Smuzhiyun 		rc = wait_for_response(server, midQ[i]);
1164*4882a593Smuzhiyun 		if (rc != 0)
1165*4882a593Smuzhiyun 			break;
1166*4882a593Smuzhiyun 	}
1167*4882a593Smuzhiyun 	if (rc != 0) {
1168*4882a593Smuzhiyun 		for (; i < num_rqst; i++) {
1169*4882a593Smuzhiyun 			cifs_server_dbg(FYI, "Cancelling wait for mid %llu cmd: %d\n",
1170*4882a593Smuzhiyun 				 midQ[i]->mid, le16_to_cpu(midQ[i]->command));
1171*4882a593Smuzhiyun 			send_cancel(server, &rqst[i], midQ[i]);
1172*4882a593Smuzhiyun 			spin_lock(&GlobalMid_Lock);
1173*4882a593Smuzhiyun 			midQ[i]->mid_flags |= MID_WAIT_CANCELLED;
1174*4882a593Smuzhiyun 			if (midQ[i]->mid_state == MID_REQUEST_SUBMITTED) {
1175*4882a593Smuzhiyun 				midQ[i]->callback = cifs_cancelled_callback;
1176*4882a593Smuzhiyun 				cancelled_mid[i] = true;
1177*4882a593Smuzhiyun 				credits[i].value = 0;
1178*4882a593Smuzhiyun 			}
1179*4882a593Smuzhiyun 			spin_unlock(&GlobalMid_Lock);
1180*4882a593Smuzhiyun 		}
1181*4882a593Smuzhiyun 	}
1182*4882a593Smuzhiyun 
1183*4882a593Smuzhiyun 	for (i = 0; i < num_rqst; i++) {
1184*4882a593Smuzhiyun 		if (rc < 0)
1185*4882a593Smuzhiyun 			goto out;
1186*4882a593Smuzhiyun 
1187*4882a593Smuzhiyun 		rc = cifs_sync_mid_result(midQ[i], server);
1188*4882a593Smuzhiyun 		if (rc != 0) {
1189*4882a593Smuzhiyun 			/* mark this mid as cancelled to not free it below */
1190*4882a593Smuzhiyun 			cancelled_mid[i] = true;
1191*4882a593Smuzhiyun 			goto out;
1192*4882a593Smuzhiyun 		}
1193*4882a593Smuzhiyun 
1194*4882a593Smuzhiyun 		if (!midQ[i]->resp_buf ||
1195*4882a593Smuzhiyun 		    midQ[i]->mid_state != MID_RESPONSE_RECEIVED) {
1196*4882a593Smuzhiyun 			rc = -EIO;
1197*4882a593Smuzhiyun 			cifs_dbg(FYI, "Bad MID state?\n");
1198*4882a593Smuzhiyun 			goto out;
1199*4882a593Smuzhiyun 		}
1200*4882a593Smuzhiyun 
1201*4882a593Smuzhiyun 		buf = (char *)midQ[i]->resp_buf;
1202*4882a593Smuzhiyun 		resp_iov[i].iov_base = buf;
1203*4882a593Smuzhiyun 		resp_iov[i].iov_len = midQ[i]->resp_buf_size +
1204*4882a593Smuzhiyun 			server->vals->header_preamble_size;
1205*4882a593Smuzhiyun 
1206*4882a593Smuzhiyun 		if (midQ[i]->large_buf)
1207*4882a593Smuzhiyun 			resp_buf_type[i] = CIFS_LARGE_BUFFER;
1208*4882a593Smuzhiyun 		else
1209*4882a593Smuzhiyun 			resp_buf_type[i] = CIFS_SMALL_BUFFER;
1210*4882a593Smuzhiyun 
1211*4882a593Smuzhiyun 		rc = server->ops->check_receive(midQ[i], server,
1212*4882a593Smuzhiyun 						     flags & CIFS_LOG_ERROR);
1213*4882a593Smuzhiyun 
1214*4882a593Smuzhiyun 		/* mark it so buf will not be freed by cifs_delete_mid */
1215*4882a593Smuzhiyun 		if ((flags & CIFS_NO_RSP_BUF) == 0)
1216*4882a593Smuzhiyun 			midQ[i]->resp_buf = NULL;
1217*4882a593Smuzhiyun 
1218*4882a593Smuzhiyun 	}
1219*4882a593Smuzhiyun 
1220*4882a593Smuzhiyun 	/*
1221*4882a593Smuzhiyun 	 * Compounding is never used during session establish.
1222*4882a593Smuzhiyun 	 */
1223*4882a593Smuzhiyun 	if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP)) {
1224*4882a593Smuzhiyun 		struct kvec iov = {
1225*4882a593Smuzhiyun 			.iov_base = resp_iov[0].iov_base,
1226*4882a593Smuzhiyun 			.iov_len = resp_iov[0].iov_len
1227*4882a593Smuzhiyun 		};
1228*4882a593Smuzhiyun 		mutex_lock(&server->srv_mutex);
1229*4882a593Smuzhiyun 		smb311_update_preauth_hash(ses, &iov, 1);
1230*4882a593Smuzhiyun 		mutex_unlock(&server->srv_mutex);
1231*4882a593Smuzhiyun 	}
1232*4882a593Smuzhiyun 
1233*4882a593Smuzhiyun out:
1234*4882a593Smuzhiyun 	/*
1235*4882a593Smuzhiyun 	 * This will dequeue all mids. After this it is important that the
1236*4882a593Smuzhiyun 	 * demultiplex_thread will not process any of these mids any futher.
1237*4882a593Smuzhiyun 	 * This is prevented above by using a noop callback that will not
1238*4882a593Smuzhiyun 	 * wake this thread except for the very last PDU.
1239*4882a593Smuzhiyun 	 */
1240*4882a593Smuzhiyun 	for (i = 0; i < num_rqst; i++) {
1241*4882a593Smuzhiyun 		if (!cancelled_mid[i])
1242*4882a593Smuzhiyun 			cifs_delete_mid(midQ[i]);
1243*4882a593Smuzhiyun 	}
1244*4882a593Smuzhiyun 
1245*4882a593Smuzhiyun 	return rc;
1246*4882a593Smuzhiyun }
1247*4882a593Smuzhiyun 
1248*4882a593Smuzhiyun int
cifs_send_recv(const unsigned int xid,struct cifs_ses * ses,struct TCP_Server_Info * server,struct smb_rqst * rqst,int * resp_buf_type,const int flags,struct kvec * resp_iov)1249*4882a593Smuzhiyun cifs_send_recv(const unsigned int xid, struct cifs_ses *ses,
1250*4882a593Smuzhiyun 	       struct TCP_Server_Info *server,
1251*4882a593Smuzhiyun 	       struct smb_rqst *rqst, int *resp_buf_type, const int flags,
1252*4882a593Smuzhiyun 	       struct kvec *resp_iov)
1253*4882a593Smuzhiyun {
1254*4882a593Smuzhiyun 	return compound_send_recv(xid, ses, server, flags, 1,
1255*4882a593Smuzhiyun 				  rqst, resp_buf_type, resp_iov);
1256*4882a593Smuzhiyun }
1257*4882a593Smuzhiyun 
1258*4882a593Smuzhiyun int
SendReceive2(const unsigned int xid,struct cifs_ses * ses,struct kvec * iov,int n_vec,int * resp_buf_type,const int flags,struct kvec * resp_iov)1259*4882a593Smuzhiyun SendReceive2(const unsigned int xid, struct cifs_ses *ses,
1260*4882a593Smuzhiyun 	     struct kvec *iov, int n_vec, int *resp_buf_type /* ret */,
1261*4882a593Smuzhiyun 	     const int flags, struct kvec *resp_iov)
1262*4882a593Smuzhiyun {
1263*4882a593Smuzhiyun 	struct smb_rqst rqst;
1264*4882a593Smuzhiyun 	struct kvec s_iov[CIFS_MAX_IOV_SIZE], *new_iov;
1265*4882a593Smuzhiyun 	int rc;
1266*4882a593Smuzhiyun 
1267*4882a593Smuzhiyun 	if (n_vec + 1 > CIFS_MAX_IOV_SIZE) {
1268*4882a593Smuzhiyun 		new_iov = kmalloc_array(n_vec + 1, sizeof(struct kvec),
1269*4882a593Smuzhiyun 					GFP_KERNEL);
1270*4882a593Smuzhiyun 		if (!new_iov) {
1271*4882a593Smuzhiyun 			/* otherwise cifs_send_recv below sets resp_buf_type */
1272*4882a593Smuzhiyun 			*resp_buf_type = CIFS_NO_BUFFER;
1273*4882a593Smuzhiyun 			return -ENOMEM;
1274*4882a593Smuzhiyun 		}
1275*4882a593Smuzhiyun 	} else
1276*4882a593Smuzhiyun 		new_iov = s_iov;
1277*4882a593Smuzhiyun 
1278*4882a593Smuzhiyun 	/* 1st iov is a RFC1001 length followed by the rest of the packet */
1279*4882a593Smuzhiyun 	memcpy(new_iov + 1, iov, (sizeof(struct kvec) * n_vec));
1280*4882a593Smuzhiyun 
1281*4882a593Smuzhiyun 	new_iov[0].iov_base = new_iov[1].iov_base;
1282*4882a593Smuzhiyun 	new_iov[0].iov_len = 4;
1283*4882a593Smuzhiyun 	new_iov[1].iov_base += 4;
1284*4882a593Smuzhiyun 	new_iov[1].iov_len -= 4;
1285*4882a593Smuzhiyun 
1286*4882a593Smuzhiyun 	memset(&rqst, 0, sizeof(struct smb_rqst));
1287*4882a593Smuzhiyun 	rqst.rq_iov = new_iov;
1288*4882a593Smuzhiyun 	rqst.rq_nvec = n_vec + 1;
1289*4882a593Smuzhiyun 
1290*4882a593Smuzhiyun 	rc = cifs_send_recv(xid, ses, ses->server,
1291*4882a593Smuzhiyun 			    &rqst, resp_buf_type, flags, resp_iov);
1292*4882a593Smuzhiyun 	if (n_vec + 1 > CIFS_MAX_IOV_SIZE)
1293*4882a593Smuzhiyun 		kfree(new_iov);
1294*4882a593Smuzhiyun 	return rc;
1295*4882a593Smuzhiyun }
1296*4882a593Smuzhiyun 
1297*4882a593Smuzhiyun int
SendReceive(const unsigned int xid,struct cifs_ses * ses,struct smb_hdr * in_buf,struct smb_hdr * out_buf,int * pbytes_returned,const int flags)1298*4882a593Smuzhiyun SendReceive(const unsigned int xid, struct cifs_ses *ses,
1299*4882a593Smuzhiyun 	    struct smb_hdr *in_buf, struct smb_hdr *out_buf,
1300*4882a593Smuzhiyun 	    int *pbytes_returned, const int flags)
1301*4882a593Smuzhiyun {
1302*4882a593Smuzhiyun 	int rc = 0;
1303*4882a593Smuzhiyun 	struct mid_q_entry *midQ;
1304*4882a593Smuzhiyun 	unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
1305*4882a593Smuzhiyun 	struct kvec iov = { .iov_base = in_buf, .iov_len = len };
1306*4882a593Smuzhiyun 	struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
1307*4882a593Smuzhiyun 	struct cifs_credits credits = { .value = 1, .instance = 0 };
1308*4882a593Smuzhiyun 	struct TCP_Server_Info *server;
1309*4882a593Smuzhiyun 
1310*4882a593Smuzhiyun 	if (ses == NULL) {
1311*4882a593Smuzhiyun 		cifs_dbg(VFS, "Null smb session\n");
1312*4882a593Smuzhiyun 		return -EIO;
1313*4882a593Smuzhiyun 	}
1314*4882a593Smuzhiyun 	server = ses->server;
1315*4882a593Smuzhiyun 	if (server == NULL) {
1316*4882a593Smuzhiyun 		cifs_dbg(VFS, "Null tcp session\n");
1317*4882a593Smuzhiyun 		return -EIO;
1318*4882a593Smuzhiyun 	}
1319*4882a593Smuzhiyun 
1320*4882a593Smuzhiyun 	if (server->tcpStatus == CifsExiting)
1321*4882a593Smuzhiyun 		return -ENOENT;
1322*4882a593Smuzhiyun 
1323*4882a593Smuzhiyun 	/* Ensure that we do not send more than 50 overlapping requests
1324*4882a593Smuzhiyun 	   to the same server. We may make this configurable later or
1325*4882a593Smuzhiyun 	   use ses->maxReq */
1326*4882a593Smuzhiyun 
1327*4882a593Smuzhiyun 	if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
1328*4882a593Smuzhiyun 		cifs_server_dbg(VFS, "Invalid length, greater than maximum frame, %d\n",
1329*4882a593Smuzhiyun 				len);
1330*4882a593Smuzhiyun 		return -EIO;
1331*4882a593Smuzhiyun 	}
1332*4882a593Smuzhiyun 
1333*4882a593Smuzhiyun 	rc = wait_for_free_request(server, flags, &credits.instance);
1334*4882a593Smuzhiyun 	if (rc)
1335*4882a593Smuzhiyun 		return rc;
1336*4882a593Smuzhiyun 
1337*4882a593Smuzhiyun 	/* make sure that we sign in the same order that we send on this socket
1338*4882a593Smuzhiyun 	   and avoid races inside tcp sendmsg code that could cause corruption
1339*4882a593Smuzhiyun 	   of smb data */
1340*4882a593Smuzhiyun 
1341*4882a593Smuzhiyun 	mutex_lock(&server->srv_mutex);
1342*4882a593Smuzhiyun 
1343*4882a593Smuzhiyun 	rc = allocate_mid(ses, in_buf, &midQ);
1344*4882a593Smuzhiyun 	if (rc) {
1345*4882a593Smuzhiyun 		mutex_unlock(&server->srv_mutex);
1346*4882a593Smuzhiyun 		/* Update # of requests on wire to server */
1347*4882a593Smuzhiyun 		add_credits(server, &credits, 0);
1348*4882a593Smuzhiyun 		return rc;
1349*4882a593Smuzhiyun 	}
1350*4882a593Smuzhiyun 
1351*4882a593Smuzhiyun 	rc = cifs_sign_smb(in_buf, server, &midQ->sequence_number);
1352*4882a593Smuzhiyun 	if (rc) {
1353*4882a593Smuzhiyun 		mutex_unlock(&server->srv_mutex);
1354*4882a593Smuzhiyun 		goto out;
1355*4882a593Smuzhiyun 	}
1356*4882a593Smuzhiyun 
1357*4882a593Smuzhiyun 	midQ->mid_state = MID_REQUEST_SUBMITTED;
1358*4882a593Smuzhiyun 
1359*4882a593Smuzhiyun 	cifs_in_send_inc(server);
1360*4882a593Smuzhiyun 	rc = smb_send(server, in_buf, len);
1361*4882a593Smuzhiyun 	cifs_in_send_dec(server);
1362*4882a593Smuzhiyun 	cifs_save_when_sent(midQ);
1363*4882a593Smuzhiyun 
1364*4882a593Smuzhiyun 	if (rc < 0)
1365*4882a593Smuzhiyun 		server->sequence_number -= 2;
1366*4882a593Smuzhiyun 
1367*4882a593Smuzhiyun 	mutex_unlock(&server->srv_mutex);
1368*4882a593Smuzhiyun 
1369*4882a593Smuzhiyun 	if (rc < 0)
1370*4882a593Smuzhiyun 		goto out;
1371*4882a593Smuzhiyun 
1372*4882a593Smuzhiyun 	rc = wait_for_response(server, midQ);
1373*4882a593Smuzhiyun 	if (rc != 0) {
1374*4882a593Smuzhiyun 		send_cancel(server, &rqst, midQ);
1375*4882a593Smuzhiyun 		spin_lock(&GlobalMid_Lock);
1376*4882a593Smuzhiyun 		if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
1377*4882a593Smuzhiyun 			/* no longer considered to be "in-flight" */
1378*4882a593Smuzhiyun 			midQ->callback = DeleteMidQEntry;
1379*4882a593Smuzhiyun 			spin_unlock(&GlobalMid_Lock);
1380*4882a593Smuzhiyun 			add_credits(server, &credits, 0);
1381*4882a593Smuzhiyun 			return rc;
1382*4882a593Smuzhiyun 		}
1383*4882a593Smuzhiyun 		spin_unlock(&GlobalMid_Lock);
1384*4882a593Smuzhiyun 	}
1385*4882a593Smuzhiyun 
1386*4882a593Smuzhiyun 	rc = cifs_sync_mid_result(midQ, server);
1387*4882a593Smuzhiyun 	if (rc != 0) {
1388*4882a593Smuzhiyun 		add_credits(server, &credits, 0);
1389*4882a593Smuzhiyun 		return rc;
1390*4882a593Smuzhiyun 	}
1391*4882a593Smuzhiyun 
1392*4882a593Smuzhiyun 	if (!midQ->resp_buf || !out_buf ||
1393*4882a593Smuzhiyun 	    midQ->mid_state != MID_RESPONSE_RECEIVED) {
1394*4882a593Smuzhiyun 		rc = -EIO;
1395*4882a593Smuzhiyun 		cifs_server_dbg(VFS, "Bad MID state?\n");
1396*4882a593Smuzhiyun 		goto out;
1397*4882a593Smuzhiyun 	}
1398*4882a593Smuzhiyun 
1399*4882a593Smuzhiyun 	*pbytes_returned = get_rfc1002_length(midQ->resp_buf);
1400*4882a593Smuzhiyun 	memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
1401*4882a593Smuzhiyun 	rc = cifs_check_receive(midQ, server, 0);
1402*4882a593Smuzhiyun out:
1403*4882a593Smuzhiyun 	cifs_delete_mid(midQ);
1404*4882a593Smuzhiyun 	add_credits(server, &credits, 0);
1405*4882a593Smuzhiyun 
1406*4882a593Smuzhiyun 	return rc;
1407*4882a593Smuzhiyun }
1408*4882a593Smuzhiyun 
1409*4882a593Smuzhiyun /* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
1410*4882a593Smuzhiyun    blocking lock to return. */
1411*4882a593Smuzhiyun 
1412*4882a593Smuzhiyun static int
send_lock_cancel(const unsigned int xid,struct cifs_tcon * tcon,struct smb_hdr * in_buf,struct smb_hdr * out_buf)1413*4882a593Smuzhiyun send_lock_cancel(const unsigned int xid, struct cifs_tcon *tcon,
1414*4882a593Smuzhiyun 			struct smb_hdr *in_buf,
1415*4882a593Smuzhiyun 			struct smb_hdr *out_buf)
1416*4882a593Smuzhiyun {
1417*4882a593Smuzhiyun 	int bytes_returned;
1418*4882a593Smuzhiyun 	struct cifs_ses *ses = tcon->ses;
1419*4882a593Smuzhiyun 	LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
1420*4882a593Smuzhiyun 
1421*4882a593Smuzhiyun 	/* We just modify the current in_buf to change
1422*4882a593Smuzhiyun 	   the type of lock from LOCKING_ANDX_SHARED_LOCK
1423*4882a593Smuzhiyun 	   or LOCKING_ANDX_EXCLUSIVE_LOCK to
1424*4882a593Smuzhiyun 	   LOCKING_ANDX_CANCEL_LOCK. */
1425*4882a593Smuzhiyun 
1426*4882a593Smuzhiyun 	pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
1427*4882a593Smuzhiyun 	pSMB->Timeout = 0;
1428*4882a593Smuzhiyun 	pSMB->hdr.Mid = get_next_mid(ses->server);
1429*4882a593Smuzhiyun 
1430*4882a593Smuzhiyun 	return SendReceive(xid, ses, in_buf, out_buf,
1431*4882a593Smuzhiyun 			&bytes_returned, 0);
1432*4882a593Smuzhiyun }
1433*4882a593Smuzhiyun 
1434*4882a593Smuzhiyun int
SendReceiveBlockingLock(const unsigned int xid,struct cifs_tcon * tcon,struct smb_hdr * in_buf,struct smb_hdr * out_buf,int * pbytes_returned)1435*4882a593Smuzhiyun SendReceiveBlockingLock(const unsigned int xid, struct cifs_tcon *tcon,
1436*4882a593Smuzhiyun 	    struct smb_hdr *in_buf, struct smb_hdr *out_buf,
1437*4882a593Smuzhiyun 	    int *pbytes_returned)
1438*4882a593Smuzhiyun {
1439*4882a593Smuzhiyun 	int rc = 0;
1440*4882a593Smuzhiyun 	int rstart = 0;
1441*4882a593Smuzhiyun 	struct mid_q_entry *midQ;
1442*4882a593Smuzhiyun 	struct cifs_ses *ses;
1443*4882a593Smuzhiyun 	unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
1444*4882a593Smuzhiyun 	struct kvec iov = { .iov_base = in_buf, .iov_len = len };
1445*4882a593Smuzhiyun 	struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
1446*4882a593Smuzhiyun 	unsigned int instance;
1447*4882a593Smuzhiyun 	struct TCP_Server_Info *server;
1448*4882a593Smuzhiyun 
1449*4882a593Smuzhiyun 	if (tcon == NULL || tcon->ses == NULL) {
1450*4882a593Smuzhiyun 		cifs_dbg(VFS, "Null smb session\n");
1451*4882a593Smuzhiyun 		return -EIO;
1452*4882a593Smuzhiyun 	}
1453*4882a593Smuzhiyun 	ses = tcon->ses;
1454*4882a593Smuzhiyun 	server = ses->server;
1455*4882a593Smuzhiyun 
1456*4882a593Smuzhiyun 	if (server == NULL) {
1457*4882a593Smuzhiyun 		cifs_dbg(VFS, "Null tcp session\n");
1458*4882a593Smuzhiyun 		return -EIO;
1459*4882a593Smuzhiyun 	}
1460*4882a593Smuzhiyun 
1461*4882a593Smuzhiyun 	if (server->tcpStatus == CifsExiting)
1462*4882a593Smuzhiyun 		return -ENOENT;
1463*4882a593Smuzhiyun 
1464*4882a593Smuzhiyun 	/* Ensure that we do not send more than 50 overlapping requests
1465*4882a593Smuzhiyun 	   to the same server. We may make this configurable later or
1466*4882a593Smuzhiyun 	   use ses->maxReq */
1467*4882a593Smuzhiyun 
1468*4882a593Smuzhiyun 	if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
1469*4882a593Smuzhiyun 		cifs_tcon_dbg(VFS, "Invalid length, greater than maximum frame, %d\n",
1470*4882a593Smuzhiyun 			      len);
1471*4882a593Smuzhiyun 		return -EIO;
1472*4882a593Smuzhiyun 	}
1473*4882a593Smuzhiyun 
1474*4882a593Smuzhiyun 	rc = wait_for_free_request(server, CIFS_BLOCKING_OP, &instance);
1475*4882a593Smuzhiyun 	if (rc)
1476*4882a593Smuzhiyun 		return rc;
1477*4882a593Smuzhiyun 
1478*4882a593Smuzhiyun 	/* make sure that we sign in the same order that we send on this socket
1479*4882a593Smuzhiyun 	   and avoid races inside tcp sendmsg code that could cause corruption
1480*4882a593Smuzhiyun 	   of smb data */
1481*4882a593Smuzhiyun 
1482*4882a593Smuzhiyun 	mutex_lock(&server->srv_mutex);
1483*4882a593Smuzhiyun 
1484*4882a593Smuzhiyun 	rc = allocate_mid(ses, in_buf, &midQ);
1485*4882a593Smuzhiyun 	if (rc) {
1486*4882a593Smuzhiyun 		mutex_unlock(&server->srv_mutex);
1487*4882a593Smuzhiyun 		return rc;
1488*4882a593Smuzhiyun 	}
1489*4882a593Smuzhiyun 
1490*4882a593Smuzhiyun 	rc = cifs_sign_smb(in_buf, server, &midQ->sequence_number);
1491*4882a593Smuzhiyun 	if (rc) {
1492*4882a593Smuzhiyun 		cifs_delete_mid(midQ);
1493*4882a593Smuzhiyun 		mutex_unlock(&server->srv_mutex);
1494*4882a593Smuzhiyun 		return rc;
1495*4882a593Smuzhiyun 	}
1496*4882a593Smuzhiyun 
1497*4882a593Smuzhiyun 	midQ->mid_state = MID_REQUEST_SUBMITTED;
1498*4882a593Smuzhiyun 	cifs_in_send_inc(server);
1499*4882a593Smuzhiyun 	rc = smb_send(server, in_buf, len);
1500*4882a593Smuzhiyun 	cifs_in_send_dec(server);
1501*4882a593Smuzhiyun 	cifs_save_when_sent(midQ);
1502*4882a593Smuzhiyun 
1503*4882a593Smuzhiyun 	if (rc < 0)
1504*4882a593Smuzhiyun 		server->sequence_number -= 2;
1505*4882a593Smuzhiyun 
1506*4882a593Smuzhiyun 	mutex_unlock(&server->srv_mutex);
1507*4882a593Smuzhiyun 
1508*4882a593Smuzhiyun 	if (rc < 0) {
1509*4882a593Smuzhiyun 		cifs_delete_mid(midQ);
1510*4882a593Smuzhiyun 		return rc;
1511*4882a593Smuzhiyun 	}
1512*4882a593Smuzhiyun 
1513*4882a593Smuzhiyun 	/* Wait for a reply - allow signals to interrupt. */
1514*4882a593Smuzhiyun 	rc = wait_event_interruptible(server->response_q,
1515*4882a593Smuzhiyun 		(!(midQ->mid_state == MID_REQUEST_SUBMITTED)) ||
1516*4882a593Smuzhiyun 		((server->tcpStatus != CifsGood) &&
1517*4882a593Smuzhiyun 		 (server->tcpStatus != CifsNew)));
1518*4882a593Smuzhiyun 
1519*4882a593Smuzhiyun 	/* Were we interrupted by a signal ? */
1520*4882a593Smuzhiyun 	if ((rc == -ERESTARTSYS) &&
1521*4882a593Smuzhiyun 		(midQ->mid_state == MID_REQUEST_SUBMITTED) &&
1522*4882a593Smuzhiyun 		((server->tcpStatus == CifsGood) ||
1523*4882a593Smuzhiyun 		 (server->tcpStatus == CifsNew))) {
1524*4882a593Smuzhiyun 
1525*4882a593Smuzhiyun 		if (in_buf->Command == SMB_COM_TRANSACTION2) {
1526*4882a593Smuzhiyun 			/* POSIX lock. We send a NT_CANCEL SMB to cause the
1527*4882a593Smuzhiyun 			   blocking lock to return. */
1528*4882a593Smuzhiyun 			rc = send_cancel(server, &rqst, midQ);
1529*4882a593Smuzhiyun 			if (rc) {
1530*4882a593Smuzhiyun 				cifs_delete_mid(midQ);
1531*4882a593Smuzhiyun 				return rc;
1532*4882a593Smuzhiyun 			}
1533*4882a593Smuzhiyun 		} else {
1534*4882a593Smuzhiyun 			/* Windows lock. We send a LOCKINGX_CANCEL_LOCK
1535*4882a593Smuzhiyun 			   to cause the blocking lock to return. */
1536*4882a593Smuzhiyun 
1537*4882a593Smuzhiyun 			rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
1538*4882a593Smuzhiyun 
1539*4882a593Smuzhiyun 			/* If we get -ENOLCK back the lock may have
1540*4882a593Smuzhiyun 			   already been removed. Don't exit in this case. */
1541*4882a593Smuzhiyun 			if (rc && rc != -ENOLCK) {
1542*4882a593Smuzhiyun 				cifs_delete_mid(midQ);
1543*4882a593Smuzhiyun 				return rc;
1544*4882a593Smuzhiyun 			}
1545*4882a593Smuzhiyun 		}
1546*4882a593Smuzhiyun 
1547*4882a593Smuzhiyun 		rc = wait_for_response(server, midQ);
1548*4882a593Smuzhiyun 		if (rc) {
1549*4882a593Smuzhiyun 			send_cancel(server, &rqst, midQ);
1550*4882a593Smuzhiyun 			spin_lock(&GlobalMid_Lock);
1551*4882a593Smuzhiyun 			if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
1552*4882a593Smuzhiyun 				/* no longer considered to be "in-flight" */
1553*4882a593Smuzhiyun 				midQ->callback = DeleteMidQEntry;
1554*4882a593Smuzhiyun 				spin_unlock(&GlobalMid_Lock);
1555*4882a593Smuzhiyun 				return rc;
1556*4882a593Smuzhiyun 			}
1557*4882a593Smuzhiyun 			spin_unlock(&GlobalMid_Lock);
1558*4882a593Smuzhiyun 		}
1559*4882a593Smuzhiyun 
1560*4882a593Smuzhiyun 		/* We got the response - restart system call. */
1561*4882a593Smuzhiyun 		rstart = 1;
1562*4882a593Smuzhiyun 	}
1563*4882a593Smuzhiyun 
1564*4882a593Smuzhiyun 	rc = cifs_sync_mid_result(midQ, server);
1565*4882a593Smuzhiyun 	if (rc != 0)
1566*4882a593Smuzhiyun 		return rc;
1567*4882a593Smuzhiyun 
1568*4882a593Smuzhiyun 	/* rcvd frame is ok */
1569*4882a593Smuzhiyun 	if (out_buf == NULL || midQ->mid_state != MID_RESPONSE_RECEIVED) {
1570*4882a593Smuzhiyun 		rc = -EIO;
1571*4882a593Smuzhiyun 		cifs_tcon_dbg(VFS, "Bad MID state?\n");
1572*4882a593Smuzhiyun 		goto out;
1573*4882a593Smuzhiyun 	}
1574*4882a593Smuzhiyun 
1575*4882a593Smuzhiyun 	*pbytes_returned = get_rfc1002_length(midQ->resp_buf);
1576*4882a593Smuzhiyun 	memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
1577*4882a593Smuzhiyun 	rc = cifs_check_receive(midQ, server, 0);
1578*4882a593Smuzhiyun out:
1579*4882a593Smuzhiyun 	cifs_delete_mid(midQ);
1580*4882a593Smuzhiyun 	if (rstart && rc == -EACCES)
1581*4882a593Smuzhiyun 		return -ERESTARTSYS;
1582*4882a593Smuzhiyun 	return rc;
1583*4882a593Smuzhiyun }
1584