1 // SPDX-License-Identifier: LGPL-2.1
5 * Copyright (C) International Business Machines Corp., 2002,2008
6 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Jeremy Allison (jra@samba.org) 2006.
12 #include <linux/list.h>
13 #include <linux/gfp.h>
14 #include <linux/wait.h>
15 #include <linux/net.h>
16 #include <linux/delay.h>
17 #include <linux/freezer.h>
18 #include <linux/tcp.h>
19 #include <linux/bvec.h>
20 #include <linux/highmem.h>
21 #include <linux/uaccess.h>
22 #include <asm/processor.h>
23 #include <linux/mempool.h>
24 #include <linux/sched/signal.h>
27 #include "cifsproto.h"
28 #include "cifs_debug.h"
29 #include "smb2proto.h"
30 #include "smbdirect.h"
32 /* Max number of iovectors we can use off the stack when sending requests. */
33 #define CIFS_MAX_IOV_SIZE 8
36 cifs_wake_up_task(struct mid_q_entry *mid)
38 wake_up_process(mid->callback_data);
42 AllocMidQEntry(const struct smb_hdr *smb_buffer, struct TCP_Server_Info *server)
44 struct mid_q_entry *temp;
47 cifs_dbg(VFS, "Null TCP session in AllocMidQEntry\n");
51 temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
52 memset(temp, 0, sizeof(struct mid_q_entry));
53 kref_init(&temp->refcount);
54 temp->mid = get_mid(smb_buffer);
55 temp->pid = current->pid;
56 temp->command = cpu_to_le16(smb_buffer->Command);
57 cifs_dbg(FYI, "For smb_command %d\n", smb_buffer->Command);
58 /* do_gettimeofday(&temp->when_sent);*/ /* easier to use jiffies */
59 /* when mid allocated can be before when sent */
60 temp->when_alloc = jiffies;
61 temp->server = server;
64 * The default is for the mid to be synchronous, so the
65 * default callback just wakes up the current task.
67 get_task_struct(current);
68 temp->creator = current;
69 temp->callback = cifs_wake_up_task;
70 temp->callback_data = current;
72 atomic_inc(&midCount);
73 temp->mid_state = MID_REQUEST_ALLOCATED;
77 static void _cifs_mid_q_entry_release(struct kref *refcount)
79 struct mid_q_entry *midEntry =
80 container_of(refcount, struct mid_q_entry, refcount);
81 #ifdef CONFIG_CIFS_STATS2
82 __le16 command = midEntry->server->vals->lock_cmd;
83 __u16 smb_cmd = le16_to_cpu(midEntry->command);
85 unsigned long roundtrip_time;
87 struct TCP_Server_Info *server = midEntry->server;
89 if (midEntry->resp_buf && (midEntry->mid_flags & MID_WAIT_CANCELLED) &&
90 midEntry->mid_state == MID_RESPONSE_RECEIVED &&
91 server->ops->handle_cancelled_mid)
92 server->ops->handle_cancelled_mid(midEntry, server);
94 midEntry->mid_state = MID_FREE;
95 atomic_dec(&midCount);
96 if (midEntry->large_buf)
97 cifs_buf_release(midEntry->resp_buf);
99 cifs_small_buf_release(midEntry->resp_buf);
100 #ifdef CONFIG_CIFS_STATS2
102 if (now < midEntry->when_alloc)
103 cifs_server_dbg(VFS, "Invalid mid allocation time\n");
104 roundtrip_time = now - midEntry->when_alloc;
106 if (smb_cmd < NUMBER_OF_SMB2_COMMANDS) {
107 if (atomic_read(&server->num_cmds[smb_cmd]) == 0) {
108 server->slowest_cmd[smb_cmd] = roundtrip_time;
109 server->fastest_cmd[smb_cmd] = roundtrip_time;
111 if (server->slowest_cmd[smb_cmd] < roundtrip_time)
112 server->slowest_cmd[smb_cmd] = roundtrip_time;
113 else if (server->fastest_cmd[smb_cmd] > roundtrip_time)
114 server->fastest_cmd[smb_cmd] = roundtrip_time;
116 cifs_stats_inc(&server->num_cmds[smb_cmd]);
117 server->time_per_cmd[smb_cmd] += roundtrip_time;
120 * commands taking longer than one second (default) can be indications
121 * that something is wrong, unless it is quite a slow link or a very
122 * busy server. Note that this calc is unlikely or impossible to wrap
123 * as long as slow_rsp_threshold is not set way above recommended max
124 * value (32767 ie 9 hours) and is generally harmless even if wrong
125 * since only affects debug counters - so leaving the calc as simple
126 * comparison rather than doing multiple conversions and overflow
129 if ((slow_rsp_threshold != 0) &&
130 time_after(now, midEntry->when_alloc + (slow_rsp_threshold * HZ)) &&
131 (midEntry->command != command)) {
133 * smb2slowcmd[NUMBER_OF_SMB2_COMMANDS] counts by command
134 * NB: le16_to_cpu returns unsigned so can not be negative below
136 if (smb_cmd < NUMBER_OF_SMB2_COMMANDS)
137 cifs_stats_inc(&server->smb2slowcmd[smb_cmd]);
139 trace_smb3_slow_rsp(smb_cmd, midEntry->mid, midEntry->pid,
140 midEntry->when_sent, midEntry->when_received);
141 if (cifsFYI & CIFS_TIMER) {
142 pr_debug("slow rsp: cmd %d mid %llu",
143 midEntry->command, midEntry->mid);
144 cifs_info("A: 0x%lx S: 0x%lx R: 0x%lx\n",
145 now - midEntry->when_alloc,
146 now - midEntry->when_sent,
147 now - midEntry->when_received);
151 put_task_struct(midEntry->creator);
153 mempool_free(midEntry, cifs_mid_poolp);
156 void cifs_mid_q_entry_release(struct mid_q_entry *midEntry)
158 spin_lock(&GlobalMid_Lock);
159 kref_put(&midEntry->refcount, _cifs_mid_q_entry_release);
160 spin_unlock(&GlobalMid_Lock);
163 void DeleteMidQEntry(struct mid_q_entry *midEntry)
165 cifs_mid_q_entry_release(midEntry);
169 cifs_delete_mid(struct mid_q_entry *mid)
171 spin_lock(&GlobalMid_Lock);
172 if (!(mid->mid_flags & MID_DELETED)) {
173 list_del_init(&mid->qhead);
174 mid->mid_flags |= MID_DELETED;
176 spin_unlock(&GlobalMid_Lock);
178 DeleteMidQEntry(mid);
182 * smb_send_kvec - send an array of kvecs to the server
183 * @server: Server to send the data to
184 * @smb_msg: Message to send
185 * @sent: amount of data sent on socket is stored here
187 * Our basic "send data to server" function. Should be called with srv_mutex
188 * held. The caller is responsible for handling the results.
191 smb_send_kvec(struct TCP_Server_Info *server, struct msghdr *smb_msg,
196 struct socket *ssocket = server->ssocket;
200 smb_msg->msg_name = (struct sockaddr *) &server->dstaddr;
201 smb_msg->msg_namelen = sizeof(struct sockaddr);
202 smb_msg->msg_control = NULL;
203 smb_msg->msg_controllen = 0;
204 if (server->noblocksnd)
205 smb_msg->msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL;
207 smb_msg->msg_flags = MSG_NOSIGNAL;
209 while (msg_data_left(smb_msg)) {
211 * If blocking send, we try 3 times, since each can block
212 * for 5 seconds. For nonblocking we have to try more
213 * but wait increasing amounts of time allowing time for
214 * socket to clear. The overall time we wait in either
215 * case to send on the socket is about 15 seconds.
216 * Similarly we wait for 15 seconds for a response from
217 * the server in SendReceive[2] for the server to send
218 * a response back for most types of requests (except
219 * SMB Write past end of file which can be slow, and
220 * blocking lock operations). NFS waits slightly longer
221 * than CIFS, but this can make it take longer for
222 * nonresponsive servers to be detected and 15 seconds
223 * is more than enough time for modern networks to
224 * send a packet. In most cases if we fail to send
225 * after the retries we will kill the socket and
226 * reconnect which may clear the network problem.
228 rc = sock_sendmsg(ssocket, smb_msg);
232 (!server->noblocksnd && (retries > 2))) {
233 cifs_server_dbg(VFS, "sends on sock %p stuck for 15 seconds\n",
237 msleep(1 << retries);
245 /* should never happen, letting socket clear before
246 retrying is our only obvious option here */
247 cifs_server_dbg(VFS, "tcp sent no data\n");
252 /* send was at least partially successful */
254 retries = 0; /* in case we get ENOSPC on the next send */
260 smb_rqst_len(struct TCP_Server_Info *server, struct smb_rqst *rqst)
265 unsigned long buflen = 0;
267 if (server->vals->header_preamble_size == 0 &&
268 rqst->rq_nvec >= 2 && rqst->rq_iov[0].iov_len == 4) {
269 iov = &rqst->rq_iov[1];
270 nvec = rqst->rq_nvec - 1;
273 nvec = rqst->rq_nvec;
276 /* total up iov array first */
277 for (i = 0; i < nvec; i++)
278 buflen += iov[i].iov_len;
281 * Add in the page array if there is one. The caller needs to make
282 * sure rq_offset and rq_tailsz are set correctly. If a buffer of
283 * multiple pages ends at page boundary, rq_tailsz needs to be set to
286 if (rqst->rq_npages) {
287 if (rqst->rq_npages == 1)
288 buflen += rqst->rq_tailsz;
291 * If there is more than one page, calculate the
292 * buffer length based on rq_offset and rq_tailsz
294 buflen += rqst->rq_pagesz * (rqst->rq_npages - 1) -
296 buflen += rqst->rq_tailsz;
304 __smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
305 struct smb_rqst *rqst)
310 unsigned int send_length = 0;
312 sigset_t mask, oldmask;
313 size_t total_len = 0, sent, size;
314 struct socket *ssocket = server->ssocket;
315 struct msghdr smb_msg;
316 __be32 rfc1002_marker;
318 if (cifs_rdma_enabled(server)) {
319 /* return -EAGAIN when connecting or reconnecting */
321 if (server->smbd_conn)
322 rc = smbd_send(server, num_rqst, rqst);
329 if (fatal_signal_pending(current)) {
330 cifs_dbg(FYI, "signal pending before send request\n");
334 /* cork the socket */
335 tcp_sock_set_cork(ssocket->sk, true);
337 for (j = 0; j < num_rqst; j++)
338 send_length += smb_rqst_len(server, &rqst[j]);
339 rfc1002_marker = cpu_to_be32(send_length);
342 * We should not allow signals to interrupt the network send because
343 * any partial send will cause session reconnects thus increasing
344 * latency of system calls and overload a server with unnecessary
349 sigprocmask(SIG_BLOCK, &mask, &oldmask);
351 /* Generate a rfc1002 marker for SMB2+ */
352 if (server->vals->header_preamble_size == 0) {
354 .iov_base = &rfc1002_marker,
357 iov_iter_kvec(&smb_msg.msg_iter, WRITE, &hiov, 1, 4);
358 rc = smb_send_kvec(server, &smb_msg, &sent);
366 cifs_dbg(FYI, "Sending smb: smb_len=%u\n", send_length);
368 for (j = 0; j < num_rqst; j++) {
369 iov = rqst[j].rq_iov;
370 n_vec = rqst[j].rq_nvec;
373 for (i = 0; i < n_vec; i++) {
374 dump_smb(iov[i].iov_base, iov[i].iov_len);
375 size += iov[i].iov_len;
378 iov_iter_kvec(&smb_msg.msg_iter, WRITE, iov, n_vec, size);
380 rc = smb_send_kvec(server, &smb_msg, &sent);
386 /* now walk the page array and send each page in it */
387 for (i = 0; i < rqst[j].rq_npages; i++) {
390 bvec.bv_page = rqst[j].rq_pages[i];
391 rqst_page_get_length(&rqst[j], i, &bvec.bv_len,
394 iov_iter_bvec(&smb_msg.msg_iter, WRITE,
395 &bvec, 1, bvec.bv_len);
396 rc = smb_send_kvec(server, &smb_msg, &sent);
405 sigprocmask(SIG_SETMASK, &oldmask, NULL);
408 * If signal is pending but we have already sent the whole packet to
409 * the server we need to return success status to allow a corresponding
410 * mid entry to be kept in the pending requests queue thus allowing
411 * to handle responses from the server by the client.
413 * If only part of the packet has been sent there is no need to hide
414 * interrupt because the session will be reconnected anyway, so there
415 * won't be any response from the server to handle.
418 if (signal_pending(current) && (total_len != send_length)) {
419 cifs_dbg(FYI, "signal is pending after attempt to send\n");
424 tcp_sock_set_cork(ssocket->sk, false);
426 if ((total_len > 0) && (total_len != send_length)) {
427 cifs_dbg(FYI, "partial send (wanted=%u sent=%zu): terminating session\n",
428 send_length, total_len);
430 * If we have only sent part of an SMB then the next SMB could
431 * be taken as the remainder of this one. We need to kill the
432 * socket so the server throws away the partial SMB
434 server->tcpStatus = CifsNeedReconnect;
435 trace_smb3_partial_send_reconnect(server->CurrentMid,
436 server->conn_id, server->hostname);
439 if (rc < 0 && rc != -EINTR)
440 cifs_server_dbg(VFS, "Error %d sending data on socket to server\n",
449 smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
450 struct smb_rqst *rqst, int flags)
453 struct smb2_transform_hdr *tr_hdr;
454 struct smb_rqst cur_rqst[MAX_COMPOUND];
457 if (!(flags & CIFS_TRANSFORM_REQ))
458 return __smb_send_rqst(server, num_rqst, rqst);
460 if (num_rqst > MAX_COMPOUND - 1)
463 if (!server->ops->init_transform_rq) {
464 cifs_server_dbg(VFS, "Encryption requested but transform callback is missing\n");
468 tr_hdr = kmalloc(sizeof(*tr_hdr), GFP_NOFS);
472 memset(&cur_rqst[0], 0, sizeof(cur_rqst));
473 memset(&iov, 0, sizeof(iov));
474 memset(tr_hdr, 0, sizeof(*tr_hdr));
476 iov.iov_base = tr_hdr;
477 iov.iov_len = sizeof(*tr_hdr);
478 cur_rqst[0].rq_iov = &iov;
479 cur_rqst[0].rq_nvec = 1;
481 rc = server->ops->init_transform_rq(server, num_rqst + 1,
486 rc = __smb_send_rqst(server, num_rqst + 1, &cur_rqst[0]);
487 smb3_free_compound_rqst(num_rqst, &cur_rqst[1]);
494 smb_send(struct TCP_Server_Info *server, struct smb_hdr *smb_buffer,
495 unsigned int smb_buf_length)
498 struct smb_rqst rqst = { .rq_iov = iov,
501 iov[0].iov_base = smb_buffer;
503 iov[1].iov_base = (char *)smb_buffer + 4;
504 iov[1].iov_len = smb_buf_length;
506 return __smb_send_rqst(server, 1, &rqst);
510 wait_for_free_credits(struct TCP_Server_Info *server, const int num_credits,
511 const int timeout, const int flags,
512 unsigned int *instance)
518 int scredits, in_flight;
521 t = MAX_JIFFY_OFFSET;
523 t = msecs_to_jiffies(timeout);
525 optype = flags & CIFS_OP_MASK;
529 credits = server->ops->get_credits_field(server, optype);
530 /* Since an echo is already inflight, no need to wait to send another */
531 if (*credits <= 0 && optype == CIFS_ECHO_OP)
534 spin_lock(&server->req_lock);
535 if ((flags & CIFS_TIMEOUT_MASK) == CIFS_NON_BLOCKING) {
536 /* oplock breaks must not be held up */
538 if (server->in_flight > server->max_in_flight)
539 server->max_in_flight = server->in_flight;
541 *instance = server->reconnect_instance;
543 in_flight = server->in_flight;
544 spin_unlock(&server->req_lock);
546 trace_smb3_add_credits(server->CurrentMid,
547 server->conn_id, server->hostname, scredits, -1, in_flight);
548 cifs_dbg(FYI, "%s: remove %u credits total=%d\n",
549 __func__, 1, scredits);
555 if (*credits < num_credits) {
557 spin_unlock(&server->req_lock);
559 cifs_num_waiters_inc(server);
560 rc = wait_event_killable_timeout(server->request_q,
561 has_credits(server, credits, num_credits), t);
562 cifs_num_waiters_dec(server);
564 spin_lock(&server->req_lock);
566 in_flight = server->in_flight;
567 spin_unlock(&server->req_lock);
569 trace_smb3_credit_timeout(server->CurrentMid,
570 server->conn_id, server->hostname, scredits,
571 num_credits, in_flight);
572 cifs_server_dbg(VFS, "wait timed out after %d ms\n",
576 if (rc == -ERESTARTSYS)
578 spin_lock(&server->req_lock);
580 if (server->tcpStatus == CifsExiting) {
581 spin_unlock(&server->req_lock);
586 * For normal commands, reserve the last MAX_COMPOUND
587 * credits to compound requests.
588 * Otherwise these compounds could be permanently
589 * starved for credits by single-credit requests.
591 * To prevent spinning CPU, block this thread until
592 * there are >MAX_COMPOUND credits available.
593 * But only do this is we already have a lot of
594 * credits in flight to avoid triggering this check
595 * for servers that are slow to hand out credits on
598 if (!optype && num_credits == 1 &&
599 server->in_flight > 2 * MAX_COMPOUND &&
600 *credits <= MAX_COMPOUND) {
601 spin_unlock(&server->req_lock);
603 cifs_num_waiters_inc(server);
604 rc = wait_event_killable_timeout(
606 has_credits(server, credits,
609 cifs_num_waiters_dec(server);
611 spin_lock(&server->req_lock);
613 in_flight = server->in_flight;
614 spin_unlock(&server->req_lock);
616 trace_smb3_credit_timeout(
618 server->conn_id, server->hostname,
619 scredits, num_credits, in_flight);
620 cifs_server_dbg(VFS, "wait timed out after %d ms\n",
624 if (rc == -ERESTARTSYS)
626 spin_lock(&server->req_lock);
631 * Can not count locking commands against total
632 * as they are allowed to block on server.
635 /* update # of requests on the wire to server */
636 if ((flags & CIFS_TIMEOUT_MASK) != CIFS_BLOCKING_OP) {
637 *credits -= num_credits;
638 server->in_flight += num_credits;
639 if (server->in_flight > server->max_in_flight)
640 server->max_in_flight = server->in_flight;
641 *instance = server->reconnect_instance;
644 in_flight = server->in_flight;
645 spin_unlock(&server->req_lock);
647 trace_smb3_add_credits(server->CurrentMid,
648 server->conn_id, server->hostname, scredits,
649 -(num_credits), in_flight);
650 cifs_dbg(FYI, "%s: remove %u credits total=%d\n",
651 __func__, num_credits, scredits);
659 wait_for_free_request(struct TCP_Server_Info *server, const int flags,
660 unsigned int *instance)
662 return wait_for_free_credits(server, 1, -1, flags,
667 wait_for_compound_request(struct TCP_Server_Info *server, int num,
668 const int flags, unsigned int *instance)
671 int scredits, in_flight;
673 credits = server->ops->get_credits_field(server, flags & CIFS_OP_MASK);
675 spin_lock(&server->req_lock);
677 in_flight = server->in_flight;
679 if (*credits < num) {
681 * If the server is tight on resources or just gives us less
682 * credits for other reasons (e.g. requests are coming out of
683 * order and the server delays granting more credits until it
684 * processes a missing mid) and we exhausted most available
685 * credits there may be situations when we try to send
686 * a compound request but we don't have enough credits. At this
687 * point the client needs to decide if it should wait for
688 * additional credits or fail the request. If at least one
689 * request is in flight there is a high probability that the
690 * server will return enough credits to satisfy this compound
693 * Return immediately if no requests in flight since we will be
694 * stuck on waiting for credits.
696 if (server->in_flight == 0) {
697 spin_unlock(&server->req_lock);
698 trace_smb3_insufficient_credits(server->CurrentMid,
699 server->conn_id, server->hostname, scredits,
701 cifs_dbg(FYI, "%s: %d requests in flight, needed %d total=%d\n",
702 __func__, in_flight, num, scredits);
706 spin_unlock(&server->req_lock);
708 return wait_for_free_credits(server, num, 60000, flags,
713 cifs_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
714 unsigned int *num, struct cifs_credits *credits)
718 credits->instance = server->reconnect_instance;
722 static int allocate_mid(struct cifs_ses *ses, struct smb_hdr *in_buf,
723 struct mid_q_entry **ppmidQ)
725 if (ses->server->tcpStatus == CifsExiting) {
729 if (ses->server->tcpStatus == CifsNeedReconnect) {
730 cifs_dbg(FYI, "tcp session dead - return to caller to retry\n");
734 if (ses->status == CifsNew) {
735 if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
736 (in_buf->Command != SMB_COM_NEGOTIATE))
738 /* else ok - we are setting up session */
741 if (ses->status == CifsExiting) {
742 /* check if SMB session is bad because we are setting it up */
743 if (in_buf->Command != SMB_COM_LOGOFF_ANDX)
745 /* else ok - we are shutting down session */
748 *ppmidQ = AllocMidQEntry(in_buf, ses->server);
751 spin_lock(&GlobalMid_Lock);
752 list_add_tail(&(*ppmidQ)->qhead, &ses->server->pending_mid_q);
753 spin_unlock(&GlobalMid_Lock);
758 wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *midQ)
762 error = wait_event_freezekillable_unsafe(server->response_q,
763 midQ->mid_state != MID_REQUEST_SUBMITTED);
771 cifs_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
774 struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
775 struct mid_q_entry *mid;
777 if (rqst->rq_iov[0].iov_len != 4 ||
778 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
779 return ERR_PTR(-EIO);
781 /* enable signing if server requires it */
783 hdr->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
785 mid = AllocMidQEntry(hdr, server);
787 return ERR_PTR(-ENOMEM);
789 rc = cifs_sign_rqst(rqst, server, &mid->sequence_number);
791 DeleteMidQEntry(mid);
799 * Send a SMB request and set the callback function in the mid to handle
800 * the result. Caller is responsible for dealing with timeouts.
803 cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst,
804 mid_receive_t *receive, mid_callback_t *callback,
805 mid_handle_t *handle, void *cbdata, const int flags,
806 const struct cifs_credits *exist_credits)
809 struct mid_q_entry *mid;
810 struct cifs_credits credits = { .value = 0, .instance = 0 };
811 unsigned int instance;
814 optype = flags & CIFS_OP_MASK;
816 if ((flags & CIFS_HAS_CREDITS) == 0) {
817 rc = wait_for_free_request(server, flags, &instance);
821 credits.instance = instance;
823 instance = exist_credits->instance;
825 mutex_lock(&server->srv_mutex);
828 * We can't use credits obtained from the previous session to send this
829 * request. Check if there were reconnects after we obtained credits and
830 * return -EAGAIN in such cases to let callers handle it.
832 if (instance != server->reconnect_instance) {
833 mutex_unlock(&server->srv_mutex);
834 add_credits_and_wake_if(server, &credits, optype);
838 mid = server->ops->setup_async_request(server, rqst);
840 mutex_unlock(&server->srv_mutex);
841 add_credits_and_wake_if(server, &credits, optype);
845 mid->receive = receive;
846 mid->callback = callback;
847 mid->callback_data = cbdata;
848 mid->handle = handle;
849 mid->mid_state = MID_REQUEST_SUBMITTED;
851 /* put it on the pending_mid_q */
852 spin_lock(&GlobalMid_Lock);
853 list_add_tail(&mid->qhead, &server->pending_mid_q);
854 spin_unlock(&GlobalMid_Lock);
857 * Need to store the time in mid before calling I/O. For call_async,
858 * I/O response may come back and free the mid entry on another thread.
860 cifs_save_when_sent(mid);
861 cifs_in_send_inc(server);
862 rc = smb_send_rqst(server, 1, rqst, flags);
863 cifs_in_send_dec(server);
866 revert_current_mid(server, mid->credits);
867 server->sequence_number -= 2;
868 cifs_delete_mid(mid);
871 mutex_unlock(&server->srv_mutex);
876 add_credits_and_wake_if(server, &credits, optype);
882 * Send an SMB Request. No response info (other than return code)
883 * needs to be parsed.
885 * flags indicate the type of request buffer and how long to wait
886 * and whether to log NT STATUS code (error) before mapping it to POSIX error
890 SendReceiveNoRsp(const unsigned int xid, struct cifs_ses *ses,
891 char *in_buf, int flags)
898 iov[0].iov_base = in_buf;
899 iov[0].iov_len = get_rfc1002_length(in_buf) + 4;
900 flags |= CIFS_NO_RSP_BUF;
901 rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
902 cifs_dbg(NOISY, "SendRcvNoRsp flags %d rc %d\n", flags, rc);
908 cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
912 cifs_dbg(FYI, "%s: cmd=%d mid=%llu state=%d\n",
913 __func__, le16_to_cpu(mid->command), mid->mid, mid->mid_state);
915 spin_lock(&GlobalMid_Lock);
916 switch (mid->mid_state) {
917 case MID_RESPONSE_RECEIVED:
918 spin_unlock(&GlobalMid_Lock);
920 case MID_RETRY_NEEDED:
923 case MID_RESPONSE_MALFORMED:
930 if (!(mid->mid_flags & MID_DELETED)) {
931 list_del_init(&mid->qhead);
932 mid->mid_flags |= MID_DELETED;
934 cifs_server_dbg(VFS, "%s: invalid mid state mid=%llu state=%d\n",
935 __func__, mid->mid, mid->mid_state);
938 spin_unlock(&GlobalMid_Lock);
940 DeleteMidQEntry(mid);
945 send_cancel(struct TCP_Server_Info *server, struct smb_rqst *rqst,
946 struct mid_q_entry *mid)
948 return server->ops->send_cancel ?
949 server->ops->send_cancel(server, rqst, mid) : 0;
953 cifs_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
956 unsigned int len = get_rfc1002_length(mid->resp_buf) + 4;
958 dump_smb(mid->resp_buf, min_t(u32, 92, len));
960 /* convert the length into a more usable form */
964 struct smb_rqst rqst = { .rq_iov = iov,
967 iov[0].iov_base = mid->resp_buf;
969 iov[1].iov_base = (char *)mid->resp_buf + 4;
970 iov[1].iov_len = len - 4;
971 /* FIXME: add code to kill session */
972 rc = cifs_verify_signature(&rqst, server,
973 mid->sequence_number);
975 cifs_server_dbg(VFS, "SMB signature verification returned error = %d\n",
979 /* BB special case reconnect tid and uid here? */
980 return map_and_check_smb_error(mid, log_error);
984 cifs_setup_request(struct cifs_ses *ses, struct TCP_Server_Info *ignored,
985 struct smb_rqst *rqst)
988 struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
989 struct mid_q_entry *mid;
991 if (rqst->rq_iov[0].iov_len != 4 ||
992 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
993 return ERR_PTR(-EIO);
995 rc = allocate_mid(ses, hdr, &mid);
998 rc = cifs_sign_rqst(rqst, ses->server, &mid->sequence_number);
1000 cifs_delete_mid(mid);
1007 cifs_compound_callback(struct mid_q_entry *mid)
1009 struct TCP_Server_Info *server = mid->server;
1010 struct cifs_credits credits;
1012 credits.value = server->ops->get_credits(mid);
1013 credits.instance = server->reconnect_instance;
1015 add_credits(server, &credits, mid->optype);
1019 cifs_compound_last_callback(struct mid_q_entry *mid)
1021 cifs_compound_callback(mid);
1022 cifs_wake_up_task(mid);
1026 cifs_cancelled_callback(struct mid_q_entry *mid)
1028 cifs_compound_callback(mid);
1029 DeleteMidQEntry(mid);
1033 * Return a channel (master if none) of @ses that can be used to send
1036 * If we are currently binding a new channel (negprot/sess.setup),
1037 * return the new incomplete channel.
1039 struct TCP_Server_Info *cifs_pick_channel(struct cifs_ses *ses)
1046 if (!ses->binding) {
1048 if (ses->chan_count > 1) {
1049 index = (uint)atomic_inc_return(&ses->chan_seq);
1050 index %= ses->chan_count;
1052 return ses->chans[index].server;
1054 return cifs_ses_server(ses);
1059 compound_send_recv(const unsigned int xid, struct cifs_ses *ses,
1060 struct TCP_Server_Info *server,
1061 const int flags, const int num_rqst, struct smb_rqst *rqst,
1062 int *resp_buf_type, struct kvec *resp_iov)
1064 int i, j, optype, rc = 0;
1065 struct mid_q_entry *midQ[MAX_COMPOUND];
1066 bool cancelled_mid[MAX_COMPOUND] = {false};
1067 struct cifs_credits credits[MAX_COMPOUND] = {
1068 { .value = 0, .instance = 0 }
1070 unsigned int instance;
1073 optype = flags & CIFS_OP_MASK;
1075 for (i = 0; i < num_rqst; i++)
1076 resp_buf_type[i] = CIFS_NO_BUFFER; /* no response buf yet */
1078 if (!ses || !ses->server || !server) {
1079 cifs_dbg(VFS, "Null session\n");
1083 if (server->tcpStatus == CifsExiting)
1087 * Wait for all the requests to become available.
1088 * This approach still leaves the possibility to be stuck waiting for
1089 * credits if the server doesn't grant credits to the outstanding
1090 * requests and if the client is completely idle, not generating any
1092 * This can be handled by the eventual session reconnect.
1094 rc = wait_for_compound_request(server, num_rqst, flags,
1099 for (i = 0; i < num_rqst; i++) {
1100 credits[i].value = 1;
1101 credits[i].instance = instance;
1105 * Make sure that we sign in the same order that we send on this socket
1106 * and avoid races inside tcp sendmsg code that could cause corruption
1110 mutex_lock(&server->srv_mutex);
1113 * All the parts of the compound chain belong obtained credits from the
1114 * same session. We can not use credits obtained from the previous
1115 * session to send this request. Check if there were reconnects after
1116 * we obtained credits and return -EAGAIN in such cases to let callers
1119 if (instance != server->reconnect_instance) {
1120 mutex_unlock(&server->srv_mutex);
1121 for (j = 0; j < num_rqst; j++)
1122 add_credits(server, &credits[j], optype);
1126 for (i = 0; i < num_rqst; i++) {
1127 midQ[i] = server->ops->setup_request(ses, server, &rqst[i]);
1128 if (IS_ERR(midQ[i])) {
1129 revert_current_mid(server, i);
1130 for (j = 0; j < i; j++)
1131 cifs_delete_mid(midQ[j]);
1132 mutex_unlock(&server->srv_mutex);
1134 /* Update # of requests on wire to server */
1135 for (j = 0; j < num_rqst; j++)
1136 add_credits(server, &credits[j], optype);
1137 return PTR_ERR(midQ[i]);
1140 midQ[i]->mid_state = MID_REQUEST_SUBMITTED;
1141 midQ[i]->optype = optype;
1143 * Invoke callback for every part of the compound chain
1144 * to calculate credits properly. Wake up this thread only when
1145 * the last element is received.
1147 if (i < num_rqst - 1)
1148 midQ[i]->callback = cifs_compound_callback;
1150 midQ[i]->callback = cifs_compound_last_callback;
1152 cifs_in_send_inc(server);
1153 rc = smb_send_rqst(server, num_rqst, rqst, flags);
1154 cifs_in_send_dec(server);
1156 for (i = 0; i < num_rqst; i++)
1157 cifs_save_when_sent(midQ[i]);
1160 revert_current_mid(server, num_rqst);
1161 server->sequence_number -= 2;
1164 mutex_unlock(&server->srv_mutex);
1167 * If sending failed for some reason or it is an oplock break that we
1168 * will not receive a response to - return credits back
1170 if (rc < 0 || (flags & CIFS_NO_SRV_RSP)) {
1171 for (i = 0; i < num_rqst; i++)
1172 add_credits(server, &credits[i], optype);
1177 * At this point the request is passed to the network stack - we assume
1178 * that any credits taken from the server structure on the client have
1179 * been spent and we can't return them back. Once we receive responses
1180 * we will collect credits granted by the server in the mid callbacks
1181 * and add those credits to the server structure.
1185 * Compounding is never used during session establish.
1187 if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP) || (optype & CIFS_SESS_OP)) {
1188 mutex_lock(&server->srv_mutex);
1189 smb311_update_preauth_hash(ses, rqst[0].rq_iov,
1191 mutex_unlock(&server->srv_mutex);
1194 for (i = 0; i < num_rqst; i++) {
1195 rc = wait_for_response(server, midQ[i]);
1200 for (; i < num_rqst; i++) {
1201 cifs_server_dbg(FYI, "Cancelling wait for mid %llu cmd: %d\n",
1202 midQ[i]->mid, le16_to_cpu(midQ[i]->command));
1203 send_cancel(server, &rqst[i], midQ[i]);
1204 spin_lock(&GlobalMid_Lock);
1205 midQ[i]->mid_flags |= MID_WAIT_CANCELLED;
1206 if (midQ[i]->mid_state == MID_REQUEST_SUBMITTED) {
1207 midQ[i]->callback = cifs_cancelled_callback;
1208 cancelled_mid[i] = true;
1209 credits[i].value = 0;
1211 spin_unlock(&GlobalMid_Lock);
1215 for (i = 0; i < num_rqst; i++) {
1219 rc = cifs_sync_mid_result(midQ[i], server);
1221 /* mark this mid as cancelled to not free it below */
1222 cancelled_mid[i] = true;
1226 if (!midQ[i]->resp_buf ||
1227 midQ[i]->mid_state != MID_RESPONSE_RECEIVED) {
1229 cifs_dbg(FYI, "Bad MID state?\n");
1233 buf = (char *)midQ[i]->resp_buf;
1234 resp_iov[i].iov_base = buf;
1235 resp_iov[i].iov_len = midQ[i]->resp_buf_size +
1236 server->vals->header_preamble_size;
1238 if (midQ[i]->large_buf)
1239 resp_buf_type[i] = CIFS_LARGE_BUFFER;
1241 resp_buf_type[i] = CIFS_SMALL_BUFFER;
1243 rc = server->ops->check_receive(midQ[i], server,
1244 flags & CIFS_LOG_ERROR);
1246 /* mark it so buf will not be freed by cifs_delete_mid */
1247 if ((flags & CIFS_NO_RSP_BUF) == 0)
1248 midQ[i]->resp_buf = NULL;
1253 * Compounding is never used during session establish.
1255 if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP) || (optype & CIFS_SESS_OP)) {
1257 .iov_base = resp_iov[0].iov_base,
1258 .iov_len = resp_iov[0].iov_len
1260 mutex_lock(&server->srv_mutex);
1261 smb311_update_preauth_hash(ses, &iov, 1);
1262 mutex_unlock(&server->srv_mutex);
1267 * This will dequeue all mids. After this it is important that the
1268 * demultiplex_thread will not process any of these mids any futher.
1269 * This is prevented above by using a noop callback that will not
1270 * wake this thread except for the very last PDU.
1272 for (i = 0; i < num_rqst; i++) {
1273 if (!cancelled_mid[i])
1274 cifs_delete_mid(midQ[i]);
1281 cifs_send_recv(const unsigned int xid, struct cifs_ses *ses,
1282 struct TCP_Server_Info *server,
1283 struct smb_rqst *rqst, int *resp_buf_type, const int flags,
1284 struct kvec *resp_iov)
1286 return compound_send_recv(xid, ses, server, flags, 1,
1287 rqst, resp_buf_type, resp_iov);
1291 SendReceive2(const unsigned int xid, struct cifs_ses *ses,
1292 struct kvec *iov, int n_vec, int *resp_buf_type /* ret */,
1293 const int flags, struct kvec *resp_iov)
1295 struct smb_rqst rqst;
1296 struct kvec s_iov[CIFS_MAX_IOV_SIZE], *new_iov;
1299 if (n_vec + 1 > CIFS_MAX_IOV_SIZE) {
1300 new_iov = kmalloc_array(n_vec + 1, sizeof(struct kvec),
1303 /* otherwise cifs_send_recv below sets resp_buf_type */
1304 *resp_buf_type = CIFS_NO_BUFFER;
1310 /* 1st iov is a RFC1001 length followed by the rest of the packet */
1311 memcpy(new_iov + 1, iov, (sizeof(struct kvec) * n_vec));
1313 new_iov[0].iov_base = new_iov[1].iov_base;
1314 new_iov[0].iov_len = 4;
1315 new_iov[1].iov_base += 4;
1316 new_iov[1].iov_len -= 4;
1318 memset(&rqst, 0, sizeof(struct smb_rqst));
1319 rqst.rq_iov = new_iov;
1320 rqst.rq_nvec = n_vec + 1;
1322 rc = cifs_send_recv(xid, ses, ses->server,
1323 &rqst, resp_buf_type, flags, resp_iov);
1324 if (n_vec + 1 > CIFS_MAX_IOV_SIZE)
1330 SendReceive(const unsigned int xid, struct cifs_ses *ses,
1331 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
1332 int *pbytes_returned, const int flags)
1335 struct mid_q_entry *midQ;
1336 unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
1337 struct kvec iov = { .iov_base = in_buf, .iov_len = len };
1338 struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
1339 struct cifs_credits credits = { .value = 1, .instance = 0 };
1340 struct TCP_Server_Info *server;
1343 cifs_dbg(VFS, "Null smb session\n");
1346 server = ses->server;
1347 if (server == NULL) {
1348 cifs_dbg(VFS, "Null tcp session\n");
1352 if (server->tcpStatus == CifsExiting)
1355 /* Ensure that we do not send more than 50 overlapping requests
1356 to the same server. We may make this configurable later or
1359 if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
1360 cifs_server_dbg(VFS, "Invalid length, greater than maximum frame, %d\n",
1365 rc = wait_for_free_request(server, flags, &credits.instance);
1369 /* make sure that we sign in the same order that we send on this socket
1370 and avoid races inside tcp sendmsg code that could cause corruption
1373 mutex_lock(&server->srv_mutex);
1375 rc = allocate_mid(ses, in_buf, &midQ);
1377 mutex_unlock(&server->srv_mutex);
1378 /* Update # of requests on wire to server */
1379 add_credits(server, &credits, 0);
1383 rc = cifs_sign_smb(in_buf, server, &midQ->sequence_number);
1385 mutex_unlock(&server->srv_mutex);
1389 midQ->mid_state = MID_REQUEST_SUBMITTED;
1391 cifs_in_send_inc(server);
1392 rc = smb_send(server, in_buf, len);
1393 cifs_in_send_dec(server);
1394 cifs_save_when_sent(midQ);
1397 server->sequence_number -= 2;
1399 mutex_unlock(&server->srv_mutex);
1404 rc = wait_for_response(server, midQ);
1406 send_cancel(server, &rqst, midQ);
1407 spin_lock(&GlobalMid_Lock);
1408 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
1409 /* no longer considered to be "in-flight" */
1410 midQ->callback = DeleteMidQEntry;
1411 spin_unlock(&GlobalMid_Lock);
1412 add_credits(server, &credits, 0);
1415 spin_unlock(&GlobalMid_Lock);
1418 rc = cifs_sync_mid_result(midQ, server);
1420 add_credits(server, &credits, 0);
1424 if (!midQ->resp_buf || !out_buf ||
1425 midQ->mid_state != MID_RESPONSE_RECEIVED) {
1427 cifs_server_dbg(VFS, "Bad MID state?\n");
1431 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
1432 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
1433 rc = cifs_check_receive(midQ, server, 0);
1435 cifs_delete_mid(midQ);
1436 add_credits(server, &credits, 0);
1441 /* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
1442 blocking lock to return. */
1445 send_lock_cancel(const unsigned int xid, struct cifs_tcon *tcon,
1446 struct smb_hdr *in_buf,
1447 struct smb_hdr *out_buf)
1450 struct cifs_ses *ses = tcon->ses;
1451 LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
1453 /* We just modify the current in_buf to change
1454 the type of lock from LOCKING_ANDX_SHARED_LOCK
1455 or LOCKING_ANDX_EXCLUSIVE_LOCK to
1456 LOCKING_ANDX_CANCEL_LOCK. */
1458 pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
1460 pSMB->hdr.Mid = get_next_mid(ses->server);
1462 return SendReceive(xid, ses, in_buf, out_buf,
1463 &bytes_returned, 0);
1467 SendReceiveBlockingLock(const unsigned int xid, struct cifs_tcon *tcon,
1468 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
1469 int *pbytes_returned)
1473 struct mid_q_entry *midQ;
1474 struct cifs_ses *ses;
1475 unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
1476 struct kvec iov = { .iov_base = in_buf, .iov_len = len };
1477 struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
1478 unsigned int instance;
1479 struct TCP_Server_Info *server;
1481 if (tcon == NULL || tcon->ses == NULL) {
1482 cifs_dbg(VFS, "Null smb session\n");
1486 server = ses->server;
1488 if (server == NULL) {
1489 cifs_dbg(VFS, "Null tcp session\n");
1493 if (server->tcpStatus == CifsExiting)
1496 /* Ensure that we do not send more than 50 overlapping requests
1497 to the same server. We may make this configurable later or
1500 if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
1501 cifs_tcon_dbg(VFS, "Invalid length, greater than maximum frame, %d\n",
1506 rc = wait_for_free_request(server, CIFS_BLOCKING_OP, &instance);
1510 /* make sure that we sign in the same order that we send on this socket
1511 and avoid races inside tcp sendmsg code that could cause corruption
1514 mutex_lock(&server->srv_mutex);
1516 rc = allocate_mid(ses, in_buf, &midQ);
1518 mutex_unlock(&server->srv_mutex);
1522 rc = cifs_sign_smb(in_buf, server, &midQ->sequence_number);
1524 cifs_delete_mid(midQ);
1525 mutex_unlock(&server->srv_mutex);
1529 midQ->mid_state = MID_REQUEST_SUBMITTED;
1530 cifs_in_send_inc(server);
1531 rc = smb_send(server, in_buf, len);
1532 cifs_in_send_dec(server);
1533 cifs_save_when_sent(midQ);
1536 server->sequence_number -= 2;
1538 mutex_unlock(&server->srv_mutex);
1541 cifs_delete_mid(midQ);
1545 /* Wait for a reply - allow signals to interrupt. */
1546 rc = wait_event_interruptible(server->response_q,
1547 (!(midQ->mid_state == MID_REQUEST_SUBMITTED)) ||
1548 ((server->tcpStatus != CifsGood) &&
1549 (server->tcpStatus != CifsNew)));
1551 /* Were we interrupted by a signal ? */
1552 if ((rc == -ERESTARTSYS) &&
1553 (midQ->mid_state == MID_REQUEST_SUBMITTED) &&
1554 ((server->tcpStatus == CifsGood) ||
1555 (server->tcpStatus == CifsNew))) {
1557 if (in_buf->Command == SMB_COM_TRANSACTION2) {
1558 /* POSIX lock. We send a NT_CANCEL SMB to cause the
1559 blocking lock to return. */
1560 rc = send_cancel(server, &rqst, midQ);
1562 cifs_delete_mid(midQ);
1566 /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
1567 to cause the blocking lock to return. */
1569 rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
1571 /* If we get -ENOLCK back the lock may have
1572 already been removed. Don't exit in this case. */
1573 if (rc && rc != -ENOLCK) {
1574 cifs_delete_mid(midQ);
1579 rc = wait_for_response(server, midQ);
1581 send_cancel(server, &rqst, midQ);
1582 spin_lock(&GlobalMid_Lock);
1583 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
1584 /* no longer considered to be "in-flight" */
1585 midQ->callback = DeleteMidQEntry;
1586 spin_unlock(&GlobalMid_Lock);
1589 spin_unlock(&GlobalMid_Lock);
1592 /* We got the response - restart system call. */
1596 rc = cifs_sync_mid_result(midQ, server);
1600 /* rcvd frame is ok */
1601 if (out_buf == NULL || midQ->mid_state != MID_RESPONSE_RECEIVED) {
1603 cifs_tcon_dbg(VFS, "Bad MID state?\n");
1607 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
1608 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
1609 rc = cifs_check_receive(midQ, server, 0);
1611 cifs_delete_mid(midQ);
1612 if (rstart && rc == -EACCES)
1613 return -ERESTARTSYS;