xref: /linux/fs/smb/client/transport.c (revision 89a312991dc6e638a36adc43ccb91dbc25504c04)
1 // SPDX-License-Identifier: LGPL-2.1
2 /*
3  *
4  *   Copyright (C) International Business Machines  Corp., 2002,2008
5  *   Author(s): Steve French (sfrench@us.ibm.com)
6  *   Jeremy Allison (jra@samba.org) 2006.
7  *
8  */
9 
10 #include <linux/fs.h>
11 #include <linux/list.h>
12 #include <linux/gfp.h>
13 #include <linux/wait.h>
14 #include <linux/net.h>
15 #include <linux/delay.h>
16 #include <linux/freezer.h>
17 #include <linux/tcp.h>
18 #include <linux/bvec.h>
19 #include <linux/highmem.h>
20 #include <linux/uaccess.h>
21 #include <linux/processor.h>
22 #include <linux/mempool.h>
23 #include <linux/sched/signal.h>
24 #include <linux/task_io_accounting_ops.h>
25 #include <linux/task_work.h>
26 #include "cifsglob.h"
27 #include "cifsproto.h"
28 #include "cifs_debug.h"
29 #include "smb2proto.h"
30 #include "smbdirect.h"
31 #include "compress.h"
32 
33 void
cifs_wake_up_task(struct TCP_Server_Info * server,struct mid_q_entry * mid)34 cifs_wake_up_task(struct TCP_Server_Info *server, struct mid_q_entry *mid)
35 {
36 	if (mid->mid_state == MID_RESPONSE_RECEIVED)
37 		mid->mid_state = MID_RESPONSE_READY;
38 	wake_up_process(mid->callback_data);
39 }
40 
__release_mid(struct TCP_Server_Info * server,struct mid_q_entry * midEntry)41 void __release_mid(struct TCP_Server_Info *server, struct mid_q_entry *midEntry)
42 {
43 #ifdef CONFIG_CIFS_STATS2
44 	__le16 command = server->vals->lock_cmd;
45 	__u16 smb_cmd = le16_to_cpu(midEntry->command);
46 	unsigned long now;
47 	unsigned long roundtrip_time;
48 #endif
49 
50 	if (midEntry->resp_buf && (midEntry->wait_cancelled) &&
51 	    (midEntry->mid_state == MID_RESPONSE_RECEIVED ||
52 	     midEntry->mid_state == MID_RESPONSE_READY) &&
53 	    server->ops->handle_cancelled_mid)
54 		server->ops->handle_cancelled_mid(midEntry, server);
55 
56 	midEntry->mid_state = MID_FREE;
57 	atomic_dec(&mid_count);
58 	if (midEntry->large_buf)
59 		cifs_buf_release(midEntry->resp_buf);
60 	else
61 		cifs_small_buf_release(midEntry->resp_buf);
62 #ifdef CONFIG_CIFS_STATS2
63 	now = jiffies;
64 	if (now < midEntry->when_alloc)
65 		cifs_server_dbg(VFS, "Invalid mid allocation time\n");
66 	roundtrip_time = now - midEntry->when_alloc;
67 
68 	if (smb_cmd < NUMBER_OF_SMB2_COMMANDS) {
69 		if (atomic_read(&server->num_cmds[smb_cmd]) == 0) {
70 			server->slowest_cmd[smb_cmd] = roundtrip_time;
71 			server->fastest_cmd[smb_cmd] = roundtrip_time;
72 		} else {
73 			if (server->slowest_cmd[smb_cmd] < roundtrip_time)
74 				server->slowest_cmd[smb_cmd] = roundtrip_time;
75 			else if (server->fastest_cmd[smb_cmd] > roundtrip_time)
76 				server->fastest_cmd[smb_cmd] = roundtrip_time;
77 		}
78 		cifs_stats_inc(&server->num_cmds[smb_cmd]);
79 		server->time_per_cmd[smb_cmd] += roundtrip_time;
80 	}
81 	/*
82 	 * commands taking longer than one second (default) can be indications
83 	 * that something is wrong, unless it is quite a slow link or a very
84 	 * busy server. Note that this calc is unlikely or impossible to wrap
85 	 * as long as slow_rsp_threshold is not set way above recommended max
86 	 * value (32767 ie 9 hours) and is generally harmless even if wrong
87 	 * since only affects debug counters - so leaving the calc as simple
88 	 * comparison rather than doing multiple conversions and overflow
89 	 * checks
90 	 */
91 	if ((slow_rsp_threshold != 0) &&
92 	    time_after(now, midEntry->when_alloc + (slow_rsp_threshold * HZ)) &&
93 	    (midEntry->command != command)) {
94 		/*
95 		 * smb2slowcmd[NUMBER_OF_SMB2_COMMANDS] counts by command
96 		 * NB: le16_to_cpu returns unsigned so can not be negative below
97 		 */
98 		if (smb_cmd < NUMBER_OF_SMB2_COMMANDS)
99 			cifs_stats_inc(&server->smb2slowcmd[smb_cmd]);
100 
101 		trace_smb3_slow_rsp(smb_cmd, midEntry->mid, midEntry->pid,
102 			       midEntry->when_sent, midEntry->when_received);
103 		if (cifsFYI & CIFS_TIMER) {
104 			pr_debug("slow rsp: cmd %d mid %llu A: 0x%lx S: 0x%lx R: 0x%lx\n",
105 				 midEntry->command, midEntry->mid,
106 				 now - midEntry->when_alloc,
107 				 now - midEntry->when_sent,
108 				 now - midEntry->when_received);
109 		}
110 	}
111 #endif
112 	put_task_struct(midEntry->creator);
113 
114 	mempool_free(midEntry, &cifs_mid_pool);
115 }
116 
117 void
delete_mid(struct TCP_Server_Info * server,struct mid_q_entry * mid)118 delete_mid(struct TCP_Server_Info *server, struct mid_q_entry *mid)
119 {
120 	spin_lock(&server->mid_queue_lock);
121 
122 	if (!mid->deleted_from_q) {
123 		list_del_init(&mid->qhead);
124 		mid->deleted_from_q = true;
125 	}
126 	spin_unlock(&server->mid_queue_lock);
127 
128 	release_mid(server, mid);
129 }
130 
131 /*
132  * smb_send_kvec - send an array of kvecs to the server
133  * @server:	Server to send the data to
134  * @smb_msg:	Message to send
135  * @sent:	amount of data sent on socket is stored here
136  *
137  * Our basic "send data to server" function. Should be called with srv_mutex
138  * held. The caller is responsible for handling the results.
139  */
140 int
smb_send_kvec(struct TCP_Server_Info * server,struct msghdr * smb_msg,size_t * sent)141 smb_send_kvec(struct TCP_Server_Info *server, struct msghdr *smb_msg,
142 	      size_t *sent)
143 {
144 	int rc = 0;
145 	int retries = 0;
146 	struct socket *ssocket = server->ssocket;
147 
148 	*sent = 0;
149 
150 	if (server->noblocksnd)
151 		smb_msg->msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL;
152 	else
153 		smb_msg->msg_flags = MSG_NOSIGNAL;
154 
155 	while (msg_data_left(smb_msg)) {
156 		/*
157 		 * If blocking send, we try 3 times, since each can block
158 		 * for 5 seconds. For nonblocking  we have to try more
159 		 * but wait increasing amounts of time allowing time for
160 		 * socket to clear.  The overall time we wait in either
161 		 * case to send on the socket is about 15 seconds.
162 		 * Similarly we wait for 15 seconds for a response from
163 		 * the server in SendReceive[2] for the server to send
164 		 * a response back for most types of requests (except
165 		 * SMB Write past end of file which can be slow, and
166 		 * blocking lock operations). NFS waits slightly longer
167 		 * than CIFS, but this can make it take longer for
168 		 * nonresponsive servers to be detected and 15 seconds
169 		 * is more than enough time for modern networks to
170 		 * send a packet.  In most cases if we fail to send
171 		 * after the retries we will kill the socket and
172 		 * reconnect which may clear the network problem.
173 		 *
174 		 * Even if regular signals are masked, EINTR might be
175 		 * propagated from sk_stream_wait_memory() to here when
176 		 * TIF_NOTIFY_SIGNAL is used for task work. For example,
177 		 * certain io_uring completions will use that. Treat
178 		 * having EINTR with pending task work the same as EAGAIN
179 		 * to avoid unnecessary reconnects.
180 		 */
181 		rc = sock_sendmsg(ssocket, smb_msg);
182 		if (rc == -EAGAIN || unlikely(rc == -EINTR && task_work_pending(current))) {
183 			retries++;
184 			if (retries >= 14 ||
185 			    (!server->noblocksnd && (retries > 2))) {
186 				cifs_server_dbg(VFS, "sends on sock %p stuck for 15 seconds\n",
187 					 ssocket);
188 				return -EAGAIN;
189 			}
190 			msleep(1 << retries);
191 			continue;
192 		}
193 
194 		if (rc < 0)
195 			return rc;
196 
197 		if (rc == 0) {
198 			/* should never happen, letting socket clear before
199 			   retrying is our only obvious option here */
200 			cifs_server_dbg(VFS, "tcp sent no data\n");
201 			msleep(500);
202 			continue;
203 		}
204 
205 		/* send was at least partially successful */
206 		*sent += rc;
207 		retries = 0; /* in case we get ENOSPC on the next send */
208 	}
209 	return 0;
210 }
211 
212 unsigned long
smb_rqst_len(struct TCP_Server_Info * server,struct smb_rqst * rqst)213 smb_rqst_len(struct TCP_Server_Info *server, struct smb_rqst *rqst)
214 {
215 	unsigned int i;
216 	struct kvec *iov;
217 	int nvec;
218 	unsigned long buflen = 0;
219 
220 	if (!is_smb1(server) && rqst->rq_nvec >= 2 &&
221 	    rqst->rq_iov[0].iov_len == 4) {
222 		iov = &rqst->rq_iov[1];
223 		nvec = rqst->rq_nvec - 1;
224 	} else {
225 		iov = rqst->rq_iov;
226 		nvec = rqst->rq_nvec;
227 	}
228 
229 	/* total up iov array first */
230 	for (i = 0; i < nvec; i++)
231 		buflen += iov[i].iov_len;
232 
233 	buflen += iov_iter_count(&rqst->rq_iter);
234 	return buflen;
235 }
236 
__smb_send_rqst(struct TCP_Server_Info * server,int num_rqst,struct smb_rqst * rqst)237 int __smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
238 		    struct smb_rqst *rqst)
239 {
240 	int rc;
241 	struct kvec *iov;
242 	int n_vec;
243 	unsigned int send_length = 0;
244 	unsigned int i, j;
245 	sigset_t mask, oldmask;
246 	size_t total_len = 0, sent, size;
247 	struct socket *ssocket = server->ssocket;
248 	struct msghdr smb_msg = {};
249 	__be32 rfc1002_marker;
250 
251 	cifs_in_send_inc(server);
252 	if (cifs_rdma_enabled(server)) {
253 		/* return -EAGAIN when connecting or reconnecting */
254 		rc = -EAGAIN;
255 		if (server->smbd_conn)
256 			rc = smbd_send(server, num_rqst, rqst);
257 		goto smbd_done;
258 	}
259 
260 	rc = -EAGAIN;
261 	if (ssocket == NULL)
262 		goto out;
263 
264 	rc = -ERESTARTSYS;
265 	if (fatal_signal_pending(current)) {
266 		cifs_dbg(FYI, "signal pending before send request\n");
267 		goto out;
268 	}
269 
270 	rc = 0;
271 	/* cork the socket */
272 	tcp_sock_set_cork(ssocket->sk, true);
273 
274 	for (j = 0; j < num_rqst; j++)
275 		send_length += smb_rqst_len(server, &rqst[j]);
276 	rfc1002_marker = cpu_to_be32(send_length);
277 
278 	/*
279 	 * We should not allow signals to interrupt the network send because
280 	 * any partial send will cause session reconnects thus increasing
281 	 * latency of system calls and overload a server with unnecessary
282 	 * requests.
283 	 */
284 
285 	sigfillset(&mask);
286 	sigprocmask(SIG_BLOCK, &mask, &oldmask);
287 
288 	/* Generate a rfc1002 marker */
289 	{
290 		struct kvec hiov = {
291 			.iov_base = &rfc1002_marker,
292 			.iov_len  = 4
293 		};
294 		iov_iter_kvec(&smb_msg.msg_iter, ITER_SOURCE, &hiov, 1, 4);
295 		rc = smb_send_kvec(server, &smb_msg, &sent);
296 		if (rc < 0)
297 			goto unmask;
298 
299 		total_len += sent;
300 		send_length += 4;
301 	}
302 
303 	cifs_dbg(FYI, "Sending smb: smb_len=%u\n", send_length);
304 
305 	for (j = 0; j < num_rqst; j++) {
306 		iov = rqst[j].rq_iov;
307 		n_vec = rqst[j].rq_nvec;
308 
309 		size = 0;
310 		for (i = 0; i < n_vec; i++) {
311 			dump_smb(iov[i].iov_base, iov[i].iov_len);
312 			size += iov[i].iov_len;
313 		}
314 
315 		iov_iter_kvec(&smb_msg.msg_iter, ITER_SOURCE, iov, n_vec, size);
316 
317 		rc = smb_send_kvec(server, &smb_msg, &sent);
318 		if (rc < 0)
319 			goto unmask;
320 
321 		total_len += sent;
322 
323 		if (iov_iter_count(&rqst[j].rq_iter) > 0) {
324 			smb_msg.msg_iter = rqst[j].rq_iter;
325 			rc = smb_send_kvec(server, &smb_msg, &sent);
326 			if (rc < 0)
327 				break;
328 			total_len += sent;
329 		}
330 	}
331 
332 unmask:
333 	sigprocmask(SIG_SETMASK, &oldmask, NULL);
334 
335 	/*
336 	 * If signal is pending but we have already sent the whole packet to
337 	 * the server we need to return success status to allow a corresponding
338 	 * mid entry to be kept in the pending requests queue thus allowing
339 	 * to handle responses from the server by the client.
340 	 *
341 	 * If only part of the packet has been sent there is no need to hide
342 	 * interrupt because the session will be reconnected anyway, so there
343 	 * won't be any response from the server to handle.
344 	 */
345 
346 	if (signal_pending(current) && (total_len != send_length)) {
347 		cifs_dbg(FYI, "signal is pending after attempt to send\n");
348 		rc = -ERESTARTSYS;
349 	}
350 
351 	/* uncork it */
352 	tcp_sock_set_cork(ssocket->sk, false);
353 
354 	if ((total_len > 0) && (total_len != send_length)) {
355 		cifs_dbg(FYI, "partial send (wanted=%u sent=%zu): terminating session\n",
356 			 send_length, total_len);
357 		/*
358 		 * If we have only sent part of an SMB then the next SMB could
359 		 * be taken as the remainder of this one. We need to kill the
360 		 * socket so the server throws away the partial SMB
361 		 */
362 		cifs_signal_cifsd_for_reconnect(server, false);
363 		trace_smb3_partial_send_reconnect(server->current_mid,
364 						  server->conn_id, server->hostname);
365 	}
366 smbd_done:
367 	/*
368 	 * there's hardly any use for the layers above to know the
369 	 * actual error code here. All they should do at this point is
370 	 * to retry the connection and hope it goes away.
371 	 */
372 	if (rc < 0 && rc != -EINTR && rc != -EAGAIN) {
373 		cifs_server_dbg(VFS, "Error %d sending data on socket to server\n",
374 			 rc);
375 		rc = -ECONNABORTED;
376 		cifs_signal_cifsd_for_reconnect(server, false);
377 	} else if (rc > 0)
378 		rc = 0;
379 out:
380 	cifs_in_send_dec(server);
381 	return rc;
382 }
383 
384 static int
smb_send_rqst(struct TCP_Server_Info * server,int num_rqst,struct smb_rqst * rqst,int flags)385 smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
386 	      struct smb_rqst *rqst, int flags)
387 {
388 	struct smb2_transform_hdr tr_hdr;
389 	struct smb_rqst new_rqst[MAX_COMPOUND] = {};
390 	struct kvec iov = {
391 		.iov_base = &tr_hdr,
392 		.iov_len = sizeof(tr_hdr),
393 	};
394 	int rc;
395 
396 	if (flags & CIFS_COMPRESS_REQ)
397 		return smb_compress(server, &rqst[0], __smb_send_rqst);
398 
399 	if (!(flags & CIFS_TRANSFORM_REQ))
400 		return __smb_send_rqst(server, num_rqst, rqst);
401 
402 	if (WARN_ON_ONCE(num_rqst > MAX_COMPOUND - 1))
403 		return smb_EIO1(smb_eio_trace_tx_max_compound, num_rqst);
404 
405 	if (!server->ops->init_transform_rq) {
406 		cifs_server_dbg(VFS, "Encryption requested but transform callback is missing\n");
407 		return smb_EIO(smb_eio_trace_tx_need_transform);
408 	}
409 
410 	new_rqst[0].rq_iov = &iov;
411 	new_rqst[0].rq_nvec = 1;
412 
413 	rc = server->ops->init_transform_rq(server, num_rqst + 1,
414 					    new_rqst, rqst);
415 	if (!rc) {
416 		rc = __smb_send_rqst(server, num_rqst + 1, new_rqst);
417 		smb3_free_compound_rqst(num_rqst, &new_rqst[1]);
418 	}
419 	return rc;
420 }
421 
422 static int
wait_for_free_credits(struct TCP_Server_Info * server,const int num_credits,const int timeout,const int flags,unsigned int * instance)423 wait_for_free_credits(struct TCP_Server_Info *server, const int num_credits,
424 		      const int timeout, const int flags,
425 		      unsigned int *instance)
426 {
427 	long rc;
428 	int *credits;
429 	int optype;
430 	long int t;
431 	int scredits, in_flight;
432 
433 	if (timeout < 0)
434 		t = MAX_JIFFY_OFFSET;
435 	else
436 		t = msecs_to_jiffies(timeout);
437 
438 	optype = flags & CIFS_OP_MASK;
439 
440 	*instance = 0;
441 
442 	credits = server->ops->get_credits_field(server, optype);
443 	/* Since an echo is already inflight, no need to wait to send another */
444 	if (*credits <= 0 && optype == CIFS_ECHO_OP)
445 		return -EAGAIN;
446 
447 	spin_lock(&server->req_lock);
448 	if ((flags & CIFS_TIMEOUT_MASK) == CIFS_NON_BLOCKING) {
449 		/* oplock breaks must not be held up */
450 		server->in_flight++;
451 		if (server->in_flight > server->max_in_flight)
452 			server->max_in_flight = server->in_flight;
453 		*credits -= 1;
454 		*instance = server->reconnect_instance;
455 		scredits = *credits;
456 		in_flight = server->in_flight;
457 		spin_unlock(&server->req_lock);
458 
459 		trace_smb3_nblk_credits(server->current_mid,
460 				server->conn_id, server->hostname, scredits, -1, in_flight);
461 		cifs_dbg(FYI, "%s: remove %u credits total=%d\n",
462 				__func__, 1, scredits);
463 
464 		return 0;
465 	}
466 
467 	while (1) {
468 		spin_unlock(&server->req_lock);
469 
470 		spin_lock(&server->srv_lock);
471 		if (server->tcpStatus == CifsExiting) {
472 			spin_unlock(&server->srv_lock);
473 			return -ENOENT;
474 		}
475 		spin_unlock(&server->srv_lock);
476 
477 		spin_lock(&server->req_lock);
478 		if (*credits < num_credits) {
479 			scredits = *credits;
480 			spin_unlock(&server->req_lock);
481 
482 			cifs_num_waiters_inc(server);
483 			rc = wait_event_killable_timeout(server->request_q,
484 				has_credits(server, credits, num_credits), t);
485 			cifs_num_waiters_dec(server);
486 			if (!rc) {
487 				spin_lock(&server->req_lock);
488 				scredits = *credits;
489 				in_flight = server->in_flight;
490 				spin_unlock(&server->req_lock);
491 
492 				trace_smb3_credit_timeout(server->current_mid,
493 						server->conn_id, server->hostname, scredits,
494 						num_credits, in_flight);
495 				cifs_server_dbg(VFS, "wait timed out after %d ms\n",
496 						timeout);
497 				return -EBUSY;
498 			}
499 			if (rc == -ERESTARTSYS)
500 				return -ERESTARTSYS;
501 			spin_lock(&server->req_lock);
502 		} else {
503 			/*
504 			 * For normal commands, reserve the last MAX_COMPOUND
505 			 * credits to compound requests.
506 			 * Otherwise these compounds could be permanently
507 			 * starved for credits by single-credit requests.
508 			 *
509 			 * To prevent spinning CPU, block this thread until
510 			 * there are >MAX_COMPOUND credits available.
511 			 * But only do this is we already have a lot of
512 			 * credits in flight to avoid triggering this check
513 			 * for servers that are slow to hand out credits on
514 			 * new sessions.
515 			 */
516 			if (!optype && num_credits == 1 &&
517 			    server->in_flight > 2 * MAX_COMPOUND &&
518 			    *credits <= MAX_COMPOUND) {
519 				spin_unlock(&server->req_lock);
520 
521 				cifs_num_waiters_inc(server);
522 				rc = wait_event_killable_timeout(
523 					server->request_q,
524 					has_credits(server, credits,
525 						    MAX_COMPOUND + 1),
526 					t);
527 				cifs_num_waiters_dec(server);
528 				if (!rc) {
529 					spin_lock(&server->req_lock);
530 					scredits = *credits;
531 					in_flight = server->in_flight;
532 					spin_unlock(&server->req_lock);
533 
534 					trace_smb3_credit_timeout(
535 							server->current_mid,
536 							server->conn_id, server->hostname,
537 							scredits, num_credits, in_flight);
538 					cifs_server_dbg(VFS, "wait timed out after %d ms\n",
539 							timeout);
540 					return -EBUSY;
541 				}
542 				if (rc == -ERESTARTSYS)
543 					return -ERESTARTSYS;
544 				spin_lock(&server->req_lock);
545 				continue;
546 			}
547 
548 			/*
549 			 * Can not count locking commands against total
550 			 * as they are allowed to block on server.
551 			 */
552 
553 			/* update # of requests on the wire to server */
554 			if ((flags & CIFS_TIMEOUT_MASK) != CIFS_BLOCKING_OP) {
555 				*credits -= num_credits;
556 				server->in_flight += num_credits;
557 				if (server->in_flight > server->max_in_flight)
558 					server->max_in_flight = server->in_flight;
559 				*instance = server->reconnect_instance;
560 			}
561 			scredits = *credits;
562 			in_flight = server->in_flight;
563 			spin_unlock(&server->req_lock);
564 
565 			trace_smb3_waitff_credits(server->current_mid,
566 					server->conn_id, server->hostname, scredits,
567 					-(num_credits), in_flight);
568 			cifs_dbg(FYI, "%s: remove %u credits total=%d\n",
569 					__func__, num_credits, scredits);
570 			break;
571 		}
572 	}
573 	return 0;
574 }
575 
wait_for_free_request(struct TCP_Server_Info * server,const int flags,unsigned int * instance)576 int wait_for_free_request(struct TCP_Server_Info *server, const int flags,
577 			  unsigned int *instance)
578 {
579 	return wait_for_free_credits(server, 1, -1, flags,
580 				     instance);
581 }
582 
583 static int
wait_for_compound_request(struct TCP_Server_Info * server,int num,const int flags,unsigned int * instance)584 wait_for_compound_request(struct TCP_Server_Info *server, int num,
585 			  const int flags, unsigned int *instance)
586 {
587 	int *credits;
588 	int scredits, in_flight;
589 
590 	credits = server->ops->get_credits_field(server, flags & CIFS_OP_MASK);
591 
592 	spin_lock(&server->req_lock);
593 	scredits = *credits;
594 	in_flight = server->in_flight;
595 
596 	if (*credits < num) {
597 		/*
598 		 * If the server is tight on resources or just gives us less
599 		 * credits for other reasons (e.g. requests are coming out of
600 		 * order and the server delays granting more credits until it
601 		 * processes a missing mid) and we exhausted most available
602 		 * credits there may be situations when we try to send
603 		 * a compound request but we don't have enough credits. At this
604 		 * point the client needs to decide if it should wait for
605 		 * additional credits or fail the request. If at least one
606 		 * request is in flight there is a high probability that the
607 		 * server will return enough credits to satisfy this compound
608 		 * request.
609 		 *
610 		 * Return immediately if no requests in flight since we will be
611 		 * stuck on waiting for credits.
612 		 */
613 		if (server->in_flight == 0) {
614 			spin_unlock(&server->req_lock);
615 			trace_smb3_insufficient_credits(server->current_mid,
616 					server->conn_id, server->hostname, scredits,
617 					num, in_flight);
618 			cifs_dbg(FYI, "%s: %d requests in flight, needed %d total=%d\n",
619 					__func__, in_flight, num, scredits);
620 			return -EDEADLK;
621 		}
622 	}
623 	spin_unlock(&server->req_lock);
624 
625 	return wait_for_free_credits(server, num, 60000, flags,
626 				     instance);
627 }
628 
629 int
cifs_wait_mtu_credits(struct TCP_Server_Info * server,size_t size,size_t * num,struct cifs_credits * credits)630 cifs_wait_mtu_credits(struct TCP_Server_Info *server, size_t size,
631 		      size_t *num, struct cifs_credits *credits)
632 {
633 	*num = size;
634 	credits->value = 0;
635 	credits->instance = server->reconnect_instance;
636 	return 0;
637 }
638 
wait_for_response(struct TCP_Server_Info * server,struct mid_q_entry * mid)639 int wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *mid)
640 {
641 	unsigned int sleep_state = TASK_KILLABLE;
642 	int error;
643 
644 	if (mid->sr_flags & CIFS_INTERRUPTIBLE_WAIT)
645 		sleep_state = TASK_INTERRUPTIBLE;
646 
647 	error = wait_event_state(server->response_q,
648 				 mid->mid_state != MID_REQUEST_SUBMITTED &&
649 				 mid->mid_state != MID_RESPONSE_RECEIVED,
650 				 (sleep_state | TASK_FREEZABLE_UNSAFE));
651 	if (error < 0)
652 		return -ERESTARTSYS;
653 
654 	return 0;
655 }
656 
657 /*
658  * Send a SMB request and set the callback function in the mid to handle
659  * the result. Caller is responsible for dealing with timeouts.
660  */
661 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)662 cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst,
663 		mid_receive_t receive, mid_callback_t callback,
664 		mid_handle_t handle, void *cbdata, const int flags,
665 		const struct cifs_credits *exist_credits)
666 {
667 	int rc;
668 	struct mid_q_entry *mid;
669 	struct cifs_credits credits = { .value = 0, .instance = 0 };
670 	unsigned int instance;
671 	int optype;
672 
673 	optype = flags & CIFS_OP_MASK;
674 
675 	if ((flags & CIFS_HAS_CREDITS) == 0) {
676 		rc = wait_for_free_request(server, flags, &instance);
677 		if (rc)
678 			return rc;
679 		credits.value = 1;
680 		credits.instance = instance;
681 	} else
682 		instance = exist_credits->instance;
683 
684 	cifs_server_lock(server);
685 
686 	/*
687 	 * We can't use credits obtained from the previous session to send this
688 	 * request. Check if there were reconnects after we obtained credits and
689 	 * return -EAGAIN in such cases to let callers handle it.
690 	 */
691 	if (instance != server->reconnect_instance) {
692 		cifs_server_unlock(server);
693 		add_credits_and_wake_if(server, &credits, optype);
694 		return -EAGAIN;
695 	}
696 
697 	mid = server->ops->setup_async_request(server, rqst);
698 	if (IS_ERR(mid)) {
699 		cifs_server_unlock(server);
700 		add_credits_and_wake_if(server, &credits, optype);
701 		return PTR_ERR(mid);
702 	}
703 
704 	mid->sr_flags = flags;
705 	mid->receive = receive;
706 	mid->callback = callback;
707 	mid->callback_data = cbdata;
708 	mid->handle = handle;
709 	mid->mid_state = MID_REQUEST_SUBMITTED;
710 
711 	/* put it on the pending_mid_q */
712 	spin_lock(&server->mid_queue_lock);
713 	list_add_tail(&mid->qhead, &server->pending_mid_q);
714 	spin_unlock(&server->mid_queue_lock);
715 
716 	/*
717 	 * Need to store the time in mid before calling I/O. For call_async,
718 	 * I/O response may come back and free the mid entry on another thread.
719 	 */
720 	cifs_save_when_sent(mid);
721 	rc = smb_send_rqst(server, 1, rqst, flags);
722 
723 	if (rc < 0) {
724 		revert_current_mid(server, mid->credits);
725 		server->sequence_number -= 2;
726 		delete_mid(server, mid);
727 	}
728 
729 	cifs_server_unlock(server);
730 
731 	if (rc == 0)
732 		return 0;
733 
734 	add_credits_and_wake_if(server, &credits, optype);
735 	return rc;
736 }
737 
cifs_sync_mid_result(struct mid_q_entry * mid,struct TCP_Server_Info * server)738 int cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
739 {
740 	int rc = 0;
741 
742 	cifs_dbg(FYI, "%s: cmd=%d mid=%llu state=%d\n",
743 		 __func__, le16_to_cpu(mid->command), mid->mid, mid->mid_state);
744 
745 	spin_lock(&server->mid_queue_lock);
746 	switch (mid->mid_state) {
747 	case MID_RESPONSE_READY:
748 		spin_unlock(&server->mid_queue_lock);
749 		return rc;
750 	case MID_RETRY_NEEDED:
751 		rc = -EAGAIN;
752 		break;
753 	case MID_RESPONSE_MALFORMED:
754 		rc = smb_EIO(smb_eio_trace_rx_sync_mid_malformed);
755 		break;
756 	case MID_SHUTDOWN:
757 		rc = -EHOSTDOWN;
758 		break;
759 	case MID_RC:
760 		rc = mid->mid_rc;
761 		break;
762 	default:
763 		if (mid->deleted_from_q == false) {
764 			list_del_init(&mid->qhead);
765 			mid->deleted_from_q = true;
766 		}
767 		spin_unlock(&server->mid_queue_lock);
768 		cifs_server_dbg(VFS, "%s: invalid mid state mid=%llu state=%d\n",
769 			 __func__, mid->mid, mid->mid_state);
770 		rc = smb_EIO1(smb_eio_trace_rx_sync_mid_invalid, mid->mid_state);
771 		goto sync_mid_done;
772 	}
773 	spin_unlock(&server->mid_queue_lock);
774 
775 sync_mid_done:
776 	release_mid(server, mid);
777 	return rc;
778 }
779 
780 static void
cifs_compound_callback(struct TCP_Server_Info * server,struct mid_q_entry * mid)781 cifs_compound_callback(struct TCP_Server_Info *server, struct mid_q_entry *mid)
782 {
783 	struct cifs_credits credits = {
784 		.value = server->ops->get_credits(mid),
785 		.instance = server->reconnect_instance,
786 	};
787 
788 	add_credits(server, &credits, mid->optype);
789 
790 	if (mid->mid_state == MID_RESPONSE_RECEIVED)
791 		mid->mid_state = MID_RESPONSE_READY;
792 }
793 
794 static void
cifs_compound_last_callback(struct TCP_Server_Info * server,struct mid_q_entry * mid)795 cifs_compound_last_callback(struct TCP_Server_Info *server, struct mid_q_entry *mid)
796 {
797 	cifs_compound_callback(server, mid);
798 	cifs_wake_up_task(server, mid);
799 }
800 
801 static void
cifs_cancelled_callback(struct TCP_Server_Info * server,struct mid_q_entry * mid)802 cifs_cancelled_callback(struct TCP_Server_Info *server, struct mid_q_entry *mid)
803 {
804 	cifs_compound_callback(server, mid);
805 	release_mid(server, mid);
806 }
807 
808 /*
809  * cifs_pick_channel - pick an eligible channel for network operations
810  *
811  * @ses: session reference
812  *
813  * Select an eligible channel (not terminating and not marked as needing
814  * reconnect), preferring the least loaded one. If no eligible channel is
815  * found, fall back to the primary channel (index 0).
816  *
817  * Return: TCP_Server_Info pointer for the chosen channel, or NULL if @ses is
818  * NULL.
819  */
cifs_pick_channel(struct cifs_ses * ses)820 struct TCP_Server_Info *cifs_pick_channel(struct cifs_ses *ses)
821 {
822 	uint index = 0;
823 	unsigned int min_in_flight = UINT_MAX;
824 	struct TCP_Server_Info *server = NULL;
825 	int i, start, cur;
826 
827 	if (!ses)
828 		return NULL;
829 
830 	spin_lock(&ses->chan_lock);
831 	start = atomic_inc_return(&ses->chan_seq);
832 	for (i = 0; i < ses->chan_count; i++) {
833 		cur = (start + i) % ses->chan_count;
834 		server = ses->chans[cur].server;
835 		if (!server || server->terminate)
836 			continue;
837 
838 		if (CIFS_CHAN_NEEDS_RECONNECT(ses, cur))
839 			continue;
840 
841 		/*
842 		 * strictly speaking, we should pick up req_lock to read
843 		 * server->in_flight. But it shouldn't matter much here if we
844 		 * race while reading this data. The worst that can happen is
845 		 * that we could use a channel that's not least loaded. Avoiding
846 		 * taking the lock could help reduce wait time, which is
847 		 * important for this function
848 		 */
849 		if (server->in_flight < min_in_flight) {
850 			min_in_flight = server->in_flight;
851 			index = cur;
852 		}
853 	}
854 
855 	server = ses->chans[index].server;
856 	spin_unlock(&ses->chan_lock);
857 
858 	return server;
859 }
860 
861 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)862 compound_send_recv(const unsigned int xid, struct cifs_ses *ses,
863 		   struct TCP_Server_Info *server,
864 		   const int flags, const int num_rqst, struct smb_rqst *rqst,
865 		   int *resp_buf_type, struct kvec *resp_iov)
866 {
867 	int i, j, optype, rc = 0;
868 	struct mid_q_entry *mid[MAX_COMPOUND];
869 	bool cancelled_mid[MAX_COMPOUND] = {false};
870 	struct cifs_credits credits[MAX_COMPOUND] = {
871 		{ .value = 0, .instance = 0 }
872 	};
873 	unsigned int instance;
874 	char *buf;
875 
876 	optype = flags & CIFS_OP_MASK;
877 
878 	for (i = 0; i < num_rqst; i++)
879 		resp_buf_type[i] = CIFS_NO_BUFFER;  /* no response buf yet */
880 
881 	if (!ses || !ses->server || !server) {
882 		cifs_dbg(VFS, "Null session\n");
883 		return smb_EIO(smb_eio_trace_null_pointers);
884 	}
885 
886 	spin_lock(&server->srv_lock);
887 	if (server->tcpStatus == CifsExiting) {
888 		spin_unlock(&server->srv_lock);
889 		return -ENOENT;
890 	}
891 	spin_unlock(&server->srv_lock);
892 
893 	/*
894 	 * Wait for all the requests to become available.
895 	 * This approach still leaves the possibility to be stuck waiting for
896 	 * credits if the server doesn't grant credits to the outstanding
897 	 * requests and if the client is completely idle, not generating any
898 	 * other requests.
899 	 * This can be handled by the eventual session reconnect.
900 	 */
901 	rc = wait_for_compound_request(server, num_rqst, flags,
902 				       &instance);
903 	if (rc)
904 		return rc;
905 
906 	for (i = 0; i < num_rqst; i++) {
907 		credits[i].value = 1;
908 		credits[i].instance = instance;
909 	}
910 
911 	/*
912 	 * Make sure that we sign in the same order that we send on this socket
913 	 * and avoid races inside tcp sendmsg code that could cause corruption
914 	 * of smb data.
915 	 */
916 
917 	cifs_server_lock(server);
918 
919 	/*
920 	 * All the parts of the compound chain belong obtained credits from the
921 	 * same session. We can not use credits obtained from the previous
922 	 * session to send this request. Check if there were reconnects after
923 	 * we obtained credits and return -EAGAIN in such cases to let callers
924 	 * handle it.
925 	 */
926 	if (instance != server->reconnect_instance) {
927 		cifs_server_unlock(server);
928 		for (j = 0; j < num_rqst; j++)
929 			add_credits(server, &credits[j], optype);
930 		return -EAGAIN;
931 	}
932 
933 	for (i = 0; i < num_rqst; i++) {
934 		mid[i] = server->ops->setup_request(ses, server, &rqst[i]);
935 		if (IS_ERR(mid[i])) {
936 			revert_current_mid(server, i);
937 			for (j = 0; j < i; j++)
938 				delete_mid(server, mid[j]);
939 			cifs_server_unlock(server);
940 
941 			/* Update # of requests on wire to server */
942 			for (j = 0; j < num_rqst; j++)
943 				add_credits(server, &credits[j], optype);
944 			return PTR_ERR(mid[i]);
945 		}
946 
947 		mid[i]->sr_flags = flags;
948 		mid[i]->mid_state = MID_REQUEST_SUBMITTED;
949 		mid[i]->optype = optype;
950 		/*
951 		 * Invoke callback for every part of the compound chain
952 		 * to calculate credits properly. Wake up this thread only when
953 		 * the last element is received.
954 		 */
955 		if (i < num_rqst - 1)
956 			mid[i]->callback = cifs_compound_callback;
957 		else
958 			mid[i]->callback = cifs_compound_last_callback;
959 	}
960 	rc = smb_send_rqst(server, num_rqst, rqst, flags);
961 
962 	for (i = 0; i < num_rqst; i++)
963 		cifs_save_when_sent(mid[i]);
964 
965 	if (rc < 0) {
966 		revert_current_mid(server, num_rqst);
967 		server->sequence_number -= 2;
968 	}
969 
970 	cifs_server_unlock(server);
971 
972 	/*
973 	 * If sending failed for some reason or it is an oplock break that we
974 	 * will not receive a response to - return credits back
975 	 */
976 	if (rc < 0 || (flags & CIFS_NO_SRV_RSP)) {
977 		for (i = 0; i < num_rqst; i++)
978 			add_credits(server, &credits[i], optype);
979 		goto out;
980 	}
981 
982 	/*
983 	 * At this point the request is passed to the network stack - we assume
984 	 * that any credits taken from the server structure on the client have
985 	 * been spent and we can't return them back. Once we receive responses
986 	 * we will collect credits granted by the server in the mid callbacks
987 	 * and add those credits to the server structure.
988 	 */
989 
990 	/*
991 	 * Compounding is never used during session establish.
992 	 */
993 	spin_lock(&ses->ses_lock);
994 	if ((ses->ses_status == SES_NEW) || (optype & CIFS_NEG_OP) || (optype & CIFS_SESS_OP)) {
995 		spin_unlock(&ses->ses_lock);
996 
997 		if (WARN_ON_ONCE(num_rqst != 1 || !resp_iov))
998 			return -EINVAL;
999 
1000 		cifs_server_lock(server);
1001 		smb311_update_preauth_hash(ses, server, rqst[0].rq_iov, rqst[0].rq_nvec);
1002 		cifs_server_unlock(server);
1003 
1004 		spin_lock(&ses->ses_lock);
1005 	}
1006 	spin_unlock(&ses->ses_lock);
1007 
1008 	for (i = 0; i < num_rqst; i++) {
1009 		rc = wait_for_response(server, mid[i]);
1010 		if (rc != 0)
1011 			break;
1012 	}
1013 	if (rc != 0) {
1014 		for (; i < num_rqst; i++) {
1015 			cifs_server_dbg(FYI, "Cancelling wait for mid %llu cmd: %d\n",
1016 				 mid[i]->mid, le16_to_cpu(mid[i]->command));
1017 			send_cancel(ses, server, &rqst[i], mid[i], xid);
1018 			spin_lock(&mid[i]->mid_lock);
1019 			mid[i]->wait_cancelled = true;
1020 			if (mid[i]->mid_state == MID_REQUEST_SUBMITTED ||
1021 			    mid[i]->mid_state == MID_RESPONSE_RECEIVED) {
1022 				mid[i]->callback = cifs_cancelled_callback;
1023 				cancelled_mid[i] = true;
1024 				credits[i].value = 0;
1025 			}
1026 			spin_unlock(&mid[i]->mid_lock);
1027 		}
1028 	}
1029 
1030 	for (i = 0; i < num_rqst; i++) {
1031 		if (rc < 0)
1032 			goto out;
1033 
1034 		rc = cifs_sync_mid_result(mid[i], server);
1035 		if (rc != 0) {
1036 			/* mark this mid as cancelled to not free it below */
1037 			cancelled_mid[i] = true;
1038 			goto out;
1039 		}
1040 
1041 		if (!mid[i]->resp_buf ||
1042 		    mid[i]->mid_state != MID_RESPONSE_READY) {
1043 			rc = smb_EIO1(smb_eio_trace_rx_mid_unready, mid[i]->mid_state);
1044 			cifs_dbg(FYI, "Bad MID state?\n");
1045 			goto out;
1046 		}
1047 
1048 		rc = server->ops->check_receive(mid[i], server,
1049 						flags & CIFS_LOG_ERROR);
1050 
1051 		if (resp_iov) {
1052 			buf = (char *)mid[i]->resp_buf;
1053 			resp_iov[i].iov_base = buf;
1054 			resp_iov[i].iov_len = mid[i]->resp_buf_size;
1055 
1056 			if (mid[i]->large_buf)
1057 				resp_buf_type[i] = CIFS_LARGE_BUFFER;
1058 			else
1059 				resp_buf_type[i] = CIFS_SMALL_BUFFER;
1060 
1061 			/* mark it so buf will not be freed by delete_mid */
1062 			if ((flags & CIFS_NO_RSP_BUF) == 0)
1063 				mid[i]->resp_buf = NULL;
1064 		}
1065 	}
1066 
1067 	/*
1068 	 * Compounding is never used during session establish.
1069 	 */
1070 	spin_lock(&ses->ses_lock);
1071 	if ((ses->ses_status == SES_NEW) || (optype & CIFS_NEG_OP) || (optype & CIFS_SESS_OP)) {
1072 		struct kvec iov = {
1073 			.iov_base = resp_iov[0].iov_base,
1074 			.iov_len = resp_iov[0].iov_len
1075 		};
1076 		spin_unlock(&ses->ses_lock);
1077 		cifs_server_lock(server);
1078 		smb311_update_preauth_hash(ses, server, &iov, 1);
1079 		cifs_server_unlock(server);
1080 		spin_lock(&ses->ses_lock);
1081 	}
1082 	spin_unlock(&ses->ses_lock);
1083 
1084 out:
1085 	/*
1086 	 * This will dequeue all mids. After this it is important that the
1087 	 * demultiplex_thread will not process any of these mids any further.
1088 	 * This is prevented above by using a noop callback that will not
1089 	 * wake this thread except for the very last PDU.
1090 	 */
1091 	for (i = 0; i < num_rqst; i++) {
1092 		if (!cancelled_mid[i])
1093 			delete_mid(server, mid[i]);
1094 	}
1095 
1096 	return rc;
1097 }
1098 
1099 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)1100 cifs_send_recv(const unsigned int xid, struct cifs_ses *ses,
1101 	       struct TCP_Server_Info *server,
1102 	       struct smb_rqst *rqst, int *resp_buf_type, const int flags,
1103 	       struct kvec *resp_iov)
1104 {
1105 	return compound_send_recv(xid, ses, server, flags, 1,
1106 				  rqst, resp_buf_type, resp_iov);
1107 }
1108 
1109 
1110 /*
1111  * Discard any remaining data in the current SMB. To do this, we borrow the
1112  * current bigbuf.
1113  */
1114 int
cifs_discard_remaining_data(struct TCP_Server_Info * server)1115 cifs_discard_remaining_data(struct TCP_Server_Info *server)
1116 {
1117 	unsigned int rfclen = server->pdu_size;
1118 	size_t remaining = rfclen - server->total_read;
1119 
1120 	while (remaining > 0) {
1121 		ssize_t length;
1122 
1123 		length = cifs_discard_from_socket(server,
1124 				min_t(size_t, remaining,
1125 				      CIFSMaxBufSize + MAX_HEADER_SIZE(server)));
1126 		if (length < 0)
1127 			return length;
1128 		server->total_read += length;
1129 		remaining -= length;
1130 	}
1131 
1132 	return 0;
1133 }
1134 
1135 static int
__cifs_readv_discard(struct TCP_Server_Info * server,struct mid_q_entry * mid,bool malformed)1136 __cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid,
1137 		     bool malformed)
1138 {
1139 	int length;
1140 
1141 	length = cifs_discard_remaining_data(server);
1142 	dequeue_mid(server, mid, malformed);
1143 	mid->resp_buf = server->smallbuf;
1144 	server->smallbuf = NULL;
1145 	return length;
1146 }
1147 
1148 static int
cifs_readv_discard(struct TCP_Server_Info * server,struct mid_q_entry * mid)1149 cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1150 {
1151 	struct cifs_io_subrequest *rdata = mid->callback_data;
1152 
1153 	return  __cifs_readv_discard(server, mid, rdata->result);
1154 }
1155 
1156 int
cifs_readv_receive(struct TCP_Server_Info * server,struct mid_q_entry * mid)1157 cifs_readv_receive(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1158 {
1159 	int length, len;
1160 	unsigned int data_offset, data_len, end_off;
1161 	struct cifs_io_subrequest *rdata = mid->callback_data;
1162 	char *buf = server->smallbuf;
1163 	unsigned int buflen = server->pdu_size;
1164 	bool use_rdma_mr = false;
1165 
1166 	cifs_dbg(FYI, "%s: mid=%llu offset=%llu bytes=%zu\n",
1167 		 __func__, mid->mid, rdata->subreq.start, rdata->subreq.len);
1168 
1169 	/*
1170 	 * read the rest of READ_RSP header (sans Data array), or whatever we
1171 	 * can if there's not enough data. At this point, we've read down to
1172 	 * the Mid.
1173 	 */
1174 	len = min_t(unsigned int, buflen, server->vals->read_rsp_size) -
1175 							HEADER_SIZE(server) + 1;
1176 
1177 	length = cifs_read_from_socket(server,
1178 				       buf + HEADER_SIZE(server) - 1, len);
1179 	if (length < 0)
1180 		return length;
1181 	server->total_read += length;
1182 
1183 	if (server->ops->is_session_expired &&
1184 	    server->ops->is_session_expired(buf)) {
1185 		cifs_reconnect(server, true);
1186 		return -1;
1187 	}
1188 
1189 	if (server->ops->is_status_pending &&
1190 	    server->ops->is_status_pending(buf, server)) {
1191 		cifs_discard_remaining_data(server);
1192 		return -1;
1193 	}
1194 
1195 	/* set up first two iov for signature check and to get credits */
1196 	rdata->iov[0].iov_base = buf;
1197 	rdata->iov[0].iov_len = server->total_read;
1198 	cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
1199 		 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
1200 
1201 	/* Was the SMB read successful? */
1202 	rdata->result = server->ops->map_error(buf, false);
1203 	if (rdata->result != 0) {
1204 		cifs_dbg(FYI, "%s: server returned error %d\n",
1205 			 __func__, rdata->result);
1206 		/* normal error on read response */
1207 		return __cifs_readv_discard(server, mid, false);
1208 	}
1209 
1210 	/* Is there enough to get to the rest of the READ_RSP header? */
1211 	if (server->total_read < server->vals->read_rsp_size) {
1212 		cifs_dbg(FYI, "%s: server returned short header. got=%u expected=%zu\n",
1213 			 __func__, server->total_read,
1214 			 server->vals->read_rsp_size);
1215 		rdata->result = smb_EIO2(smb_eio_trace_read_rsp_short,
1216 					 server->total_read, server->vals->read_rsp_size);
1217 		return cifs_readv_discard(server, mid);
1218 	}
1219 
1220 	data_offset = server->ops->read_data_offset(buf);
1221 	if (data_offset < server->total_read) {
1222 		/*
1223 		 * win2k8 sometimes sends an offset of 0 when the read
1224 		 * is beyond the EOF. Treat it as if the data starts just after
1225 		 * the header.
1226 		 */
1227 		cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
1228 			 __func__, data_offset);
1229 		data_offset = server->total_read;
1230 	} else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
1231 		/* data_offset is beyond the end of smallbuf */
1232 		cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
1233 			 __func__, data_offset);
1234 		rdata->result = smb_EIO1(smb_eio_trace_read_overlarge,
1235 					 data_offset);
1236 		return cifs_readv_discard(server, mid);
1237 	}
1238 
1239 	cifs_dbg(FYI, "%s: total_read=%u data_offset=%u\n",
1240 		 __func__, server->total_read, data_offset);
1241 
1242 	len = data_offset - server->total_read;
1243 	if (len > 0) {
1244 		/* read any junk before data into the rest of smallbuf */
1245 		length = cifs_read_from_socket(server,
1246 					       buf + server->total_read, len);
1247 		if (length < 0)
1248 			return length;
1249 		server->total_read += length;
1250 		rdata->iov[0].iov_len = server->total_read;
1251 	}
1252 
1253 	/* how much data is in the response? */
1254 #ifdef CONFIG_CIFS_SMB_DIRECT
1255 	use_rdma_mr = rdata->mr;
1256 #endif
1257 	data_len = server->ops->read_data_length(buf, use_rdma_mr);
1258 	if (!use_rdma_mr) {
1259 		if (check_add_overflow(data_offset, data_len, &end_off) ||
1260 		    end_off > buflen) {
1261 			/* data_len is corrupt -- discard frame */
1262 			rdata->result = smb_EIO2(smb_eio_trace_read_rsp_malformed,
1263 						 end_off, buflen);
1264 			return cifs_readv_discard(server, mid);
1265 		}
1266 	}
1267 
1268 #ifdef CONFIG_CIFS_SMB_DIRECT
1269 	if (rdata->mr)
1270 		length = data_len; /* An RDMA read is already done. */
1271 	else
1272 #endif
1273 		length = cifs_read_iter_from_socket(server, &rdata->subreq.io_iter,
1274 						    data_len);
1275 	if (length > 0)
1276 		rdata->got_bytes += length;
1277 	server->total_read += length;
1278 
1279 	cifs_dbg(FYI, "total_read=%u buflen=%u remaining=%u\n",
1280 		 server->total_read, buflen, data_len);
1281 
1282 	/* discard anything left over */
1283 	if (server->total_read < buflen)
1284 		return cifs_readv_discard(server, mid);
1285 
1286 	dequeue_mid(server, mid, false);
1287 	mid->resp_buf = server->smallbuf;
1288 	server->smallbuf = NULL;
1289 	return length;
1290 }
1291