24887a0898c05dbc92f59d4034d7a2c1a9a0d382
[sfrench/cifs-2.6.git] / fs / cifs / transport.c
1 /*
2  *   fs/cifs/transport.c
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  *   This library is free software; you can redistribute it and/or modify
9  *   it under the terms of the GNU Lesser General Public License as published
10  *   by the Free Software Foundation; either version 2.1 of the License, or
11  *   (at your option) any later version.
12  *
13  *   This library is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
16  *   the GNU Lesser General Public License for more details.
17  *
18  *   You should have received a copy of the GNU Lesser General Public License
19  *   along with this library; if not, write to the Free Software
20  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  */
22
23 #include <linux/fs.h>
24 #include <linux/list.h>
25 #include <linux/gfp.h>
26 #include <linux/wait.h>
27 #include <linux/net.h>
28 #include <linux/delay.h>
29 #include <linux/freezer.h>
30 #include <linux/tcp.h>
31 #include <linux/bvec.h>
32 #include <linux/highmem.h>
33 #include <linux/uaccess.h>
34 #include <asm/processor.h>
35 #include <linux/mempool.h>
36 #include "cifspdu.h"
37 #include "cifsglob.h"
38 #include "cifsproto.h"
39 #include "cifs_debug.h"
40 #include "smb2proto.h"
41 #include "smbdirect.h"
42
43 /* Max number of iovectors we can use off the stack when sending requests. */
44 #define CIFS_MAX_IOV_SIZE 8
45
46 void
47 cifs_wake_up_task(struct mid_q_entry *mid)
48 {
49         wake_up_process(mid->callback_data);
50 }
51
52 struct mid_q_entry *
53 AllocMidQEntry(const struct smb_hdr *smb_buffer, struct TCP_Server_Info *server)
54 {
55         struct mid_q_entry *temp;
56
57         if (server == NULL) {
58                 cifs_dbg(VFS, "Null TCP session in AllocMidQEntry\n");
59                 return NULL;
60         }
61
62         temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
63         memset(temp, 0, sizeof(struct mid_q_entry));
64         temp->mid = get_mid(smb_buffer);
65         temp->pid = current->pid;
66         temp->command = cpu_to_le16(smb_buffer->Command);
67         cifs_dbg(FYI, "For smb_command %d\n", smb_buffer->Command);
68         /*      do_gettimeofday(&temp->when_sent);*/ /* easier to use jiffies */
69         /* when mid allocated can be before when sent */
70         temp->when_alloc = jiffies;
71         temp->server = server;
72
73         /*
74          * The default is for the mid to be synchronous, so the
75          * default callback just wakes up the current task.
76          */
77         temp->callback = cifs_wake_up_task;
78         temp->callback_data = current;
79
80         atomic_inc(&midCount);
81         temp->mid_state = MID_REQUEST_ALLOCATED;
82         return temp;
83 }
84
85 void
86 DeleteMidQEntry(struct mid_q_entry *midEntry)
87 {
88 #ifdef CONFIG_CIFS_STATS2
89         __le16 command = midEntry->server->vals->lock_cmd;
90         unsigned long now;
91 #endif
92         midEntry->mid_state = MID_FREE;
93         atomic_dec(&midCount);
94         if (midEntry->large_buf)
95                 cifs_buf_release(midEntry->resp_buf);
96         else
97                 cifs_small_buf_release(midEntry->resp_buf);
98 #ifdef CONFIG_CIFS_STATS2
99         now = jiffies;
100         /* commands taking longer than one second are indications that
101            something is wrong, unless it is quite a slow link or server */
102         if (time_after(now, midEntry->when_alloc + HZ)) {
103                 if ((cifsFYI & CIFS_TIMER) && (midEntry->command != command)) {
104                         pr_debug(" CIFS slow rsp: cmd %d mid %llu",
105                                midEntry->command, midEntry->mid);
106                         pr_info(" A: 0x%lx S: 0x%lx R: 0x%lx\n",
107                                now - midEntry->when_alloc,
108                                now - midEntry->when_sent,
109                                now - midEntry->when_received);
110                 }
111         }
112 #endif
113         mempool_free(midEntry, cifs_mid_poolp);
114 }
115
116 void
117 cifs_delete_mid(struct mid_q_entry *mid)
118 {
119         spin_lock(&GlobalMid_Lock);
120         list_del(&mid->qhead);
121         spin_unlock(&GlobalMid_Lock);
122
123         DeleteMidQEntry(mid);
124 }
125
126 /*
127  * smb_send_kvec - send an array of kvecs to the server
128  * @server:     Server to send the data to
129  * @smb_msg:    Message to send
130  * @sent:       amount of data sent on socket is stored here
131  *
132  * Our basic "send data to server" function. Should be called with srv_mutex
133  * held. The caller is responsible for handling the results.
134  */
135 static int
136 smb_send_kvec(struct TCP_Server_Info *server, struct msghdr *smb_msg,
137               size_t *sent)
138 {
139         int rc = 0;
140         int retries = 0;
141         struct socket *ssocket = server->ssocket;
142
143         *sent = 0;
144
145         smb_msg->msg_name = (struct sockaddr *) &server->dstaddr;
146         smb_msg->msg_namelen = sizeof(struct sockaddr);
147         smb_msg->msg_control = NULL;
148         smb_msg->msg_controllen = 0;
149         if (server->noblocksnd)
150                 smb_msg->msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL;
151         else
152                 smb_msg->msg_flags = MSG_NOSIGNAL;
153
154         while (msg_data_left(smb_msg)) {
155                 /*
156                  * If blocking send, we try 3 times, since each can block
157                  * for 5 seconds. For nonblocking  we have to try more
158                  * but wait increasing amounts of time allowing time for
159                  * socket to clear.  The overall time we wait in either
160                  * case to send on the socket is about 15 seconds.
161                  * Similarly we wait for 15 seconds for a response from
162                  * the server in SendReceive[2] for the server to send
163                  * a response back for most types of requests (except
164                  * SMB Write past end of file which can be slow, and
165                  * blocking lock operations). NFS waits slightly longer
166                  * than CIFS, but this can make it take longer for
167                  * nonresponsive servers to be detected and 15 seconds
168                  * is more than enough time for modern networks to
169                  * send a packet.  In most cases if we fail to send
170                  * after the retries we will kill the socket and
171                  * reconnect which may clear the network problem.
172                  */
173                 rc = sock_sendmsg(ssocket, smb_msg);
174                 if (rc == -EAGAIN) {
175                         retries++;
176                         if (retries >= 14 ||
177                             (!server->noblocksnd && (retries > 2))) {
178                                 cifs_dbg(VFS, "sends on sock %p stuck for 15 seconds\n",
179                                          ssocket);
180                                 return -EAGAIN;
181                         }
182                         msleep(1 << retries);
183                         continue;
184                 }
185
186                 if (rc < 0)
187                         return rc;
188
189                 if (rc == 0) {
190                         /* should never happen, letting socket clear before
191                            retrying is our only obvious option here */
192                         cifs_dbg(VFS, "tcp sent no data\n");
193                         msleep(500);
194                         continue;
195                 }
196
197                 /* send was at least partially successful */
198                 *sent += rc;
199                 retries = 0; /* in case we get ENOSPC on the next send */
200         }
201         return 0;
202 }
203
204 static unsigned long
205 rqst_len(struct smb_rqst *rqst)
206 {
207         unsigned int i;
208         struct kvec *iov = rqst->rq_iov;
209         unsigned long buflen = 0;
210
211         /* total up iov array first */
212         for (i = 0; i < rqst->rq_nvec; i++)
213                 buflen += iov[i].iov_len;
214
215         /*
216          * Add in the page array if there is one. The caller needs to make
217          * sure rq_offset and rq_tailsz are set correctly. If a buffer of
218          * multiple pages ends at page boundary, rq_tailsz needs to be set to
219          * PAGE_SIZE.
220          */
221         if (rqst->rq_npages) {
222                 if (rqst->rq_npages == 1)
223                         buflen += rqst->rq_tailsz;
224                 else {
225                         /*
226                          * If there is more than one page, calculate the
227                          * buffer length based on rq_offset and rq_tailsz
228                          */
229                         buflen += rqst->rq_pagesz * (rqst->rq_npages - 1) -
230                                         rqst->rq_offset;
231                         buflen += rqst->rq_tailsz;
232                 }
233         }
234
235         return buflen;
236 }
237
238 static int
239 __smb_send_rqst(struct TCP_Server_Info *server, struct smb_rqst *rqst)
240 {
241         int rc;
242         struct kvec *iov = rqst->rq_iov;
243         int n_vec = rqst->rq_nvec;
244         unsigned int smb_buf_length = get_rfc1002_length(iov[0].iov_base);
245         unsigned long send_length;
246         unsigned int i;
247         size_t total_len = 0, sent, size;
248         struct socket *ssocket = server->ssocket;
249         struct msghdr smb_msg;
250         int val = 1;
251         if (cifs_rdma_enabled(server) && server->smbd_conn) {
252                 rc = smbd_send(server->smbd_conn, rqst);
253                 goto smbd_done;
254         }
255         if (ssocket == NULL)
256                 return -ENOTSOCK;
257
258         /* sanity check send length */
259         send_length = rqst_len(rqst);
260         if (send_length != smb_buf_length + 4) {
261                 WARN(1, "Send length mismatch(send_length=%lu smb_buf_length=%u)\n",
262                         send_length, smb_buf_length);
263                 return -EIO;
264         }
265
266         if (n_vec < 2)
267                 return -EIO;
268
269         cifs_dbg(FYI, "Sending smb: smb_len=%u\n", smb_buf_length);
270         dump_smb(iov[0].iov_base, iov[0].iov_len);
271         dump_smb(iov[1].iov_base, iov[1].iov_len);
272
273         /* cork the socket */
274         kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
275                                 (char *)&val, sizeof(val));
276
277         size = 0;
278         for (i = 0; i < n_vec; i++)
279                 size += iov[i].iov_len;
280
281         iov_iter_kvec(&smb_msg.msg_iter, WRITE | ITER_KVEC, iov, n_vec, size);
282
283         rc = smb_send_kvec(server, &smb_msg, &sent);
284         if (rc < 0)
285                 goto uncork;
286
287         total_len += sent;
288
289         /* now walk the page array and send each page in it */
290         for (i = 0; i < rqst->rq_npages; i++) {
291                 struct bio_vec bvec;
292
293                 bvec.bv_page = rqst->rq_pages[i];
294                 rqst_page_get_length(rqst, i, &bvec.bv_len, &bvec.bv_offset);
295
296                 iov_iter_bvec(&smb_msg.msg_iter, WRITE | ITER_BVEC,
297                               &bvec, 1, bvec.bv_len);
298                 rc = smb_send_kvec(server, &smb_msg, &sent);
299                 if (rc < 0)
300                         break;
301
302                 total_len += sent;
303         }
304
305 uncork:
306         /* uncork it */
307         val = 0;
308         kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
309                                 (char *)&val, sizeof(val));
310
311         if ((total_len > 0) && (total_len != smb_buf_length + 4)) {
312                 cifs_dbg(FYI, "partial send (wanted=%u sent=%zu): terminating session\n",
313                          smb_buf_length + 4, total_len);
314                 /*
315                  * If we have only sent part of an SMB then the next SMB could
316                  * be taken as the remainder of this one. We need to kill the
317                  * socket so the server throws away the partial SMB
318                  */
319                 server->tcpStatus = CifsNeedReconnect;
320         }
321 smbd_done:
322         if (rc < 0 && rc != -EINTR)
323                 cifs_dbg(VFS, "Error %d sending data on socket to server\n",
324                          rc);
325         else
326                 rc = 0;
327
328         return rc;
329 }
330
331 static int
332 smb_send_rqst(struct TCP_Server_Info *server, struct smb_rqst *rqst, int flags)
333 {
334         struct smb_rqst cur_rqst;
335         int rc;
336
337         if (!(flags & CIFS_TRANSFORM_REQ))
338                 return __smb_send_rqst(server, rqst);
339
340         if (!server->ops->init_transform_rq ||
341             !server->ops->free_transform_rq) {
342                 cifs_dbg(VFS, "Encryption requested but transform callbacks are missed\n");
343                 return -EIO;
344         }
345
346         rc = server->ops->init_transform_rq(server, &cur_rqst, rqst);
347         if (rc)
348                 return rc;
349
350         rc = __smb_send_rqst(server, &cur_rqst);
351         server->ops->free_transform_rq(&cur_rqst);
352         return rc;
353 }
354
355 int
356 smb_send(struct TCP_Server_Info *server, struct smb_hdr *smb_buffer,
357          unsigned int smb_buf_length)
358 {
359         struct kvec iov[2];
360         struct smb_rqst rqst = { .rq_iov = iov,
361                                  .rq_nvec = 2 };
362
363         iov[0].iov_base = smb_buffer;
364         iov[0].iov_len = 4;
365         iov[1].iov_base = (char *)smb_buffer + 4;
366         iov[1].iov_len = smb_buf_length;
367
368         return __smb_send_rqst(server, &rqst);
369 }
370
371 static int
372 wait_for_free_credits(struct TCP_Server_Info *server, const int timeout,
373                       int *credits)
374 {
375         int rc;
376
377         spin_lock(&server->req_lock);
378         if (timeout == CIFS_ASYNC_OP) {
379                 /* oplock breaks must not be held up */
380                 server->in_flight++;
381                 *credits -= 1;
382                 spin_unlock(&server->req_lock);
383                 return 0;
384         }
385
386         while (1) {
387                 if (*credits <= 0) {
388                         spin_unlock(&server->req_lock);
389                         cifs_num_waiters_inc(server);
390                         rc = wait_event_killable(server->request_q,
391                                                  has_credits(server, credits));
392                         cifs_num_waiters_dec(server);
393                         if (rc)
394                                 return rc;
395                         spin_lock(&server->req_lock);
396                 } else {
397                         if (server->tcpStatus == CifsExiting) {
398                                 spin_unlock(&server->req_lock);
399                                 return -ENOENT;
400                         }
401
402                         /*
403                          * Can not count locking commands against total
404                          * as they are allowed to block on server.
405                          */
406
407                         /* update # of requests on the wire to server */
408                         if (timeout != CIFS_BLOCKING_OP) {
409                                 *credits -= 1;
410                                 server->in_flight++;
411                         }
412                         spin_unlock(&server->req_lock);
413                         break;
414                 }
415         }
416         return 0;
417 }
418
419 static int
420 wait_for_free_request(struct TCP_Server_Info *server, const int timeout,
421                       const int optype)
422 {
423         int *val;
424
425         val = server->ops->get_credits_field(server, optype);
426         /* Since an echo is already inflight, no need to wait to send another */
427         if (*val <= 0 && optype == CIFS_ECHO_OP)
428                 return -EAGAIN;
429         return wait_for_free_credits(server, timeout, val);
430 }
431
432 int
433 cifs_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
434                       unsigned int *num, unsigned int *credits)
435 {
436         *num = size;
437         *credits = 0;
438         return 0;
439 }
440
441 static int allocate_mid(struct cifs_ses *ses, struct smb_hdr *in_buf,
442                         struct mid_q_entry **ppmidQ)
443 {
444         if (ses->server->tcpStatus == CifsExiting) {
445                 return -ENOENT;
446         }
447
448         if (ses->server->tcpStatus == CifsNeedReconnect) {
449                 cifs_dbg(FYI, "tcp session dead - return to caller to retry\n");
450                 return -EAGAIN;
451         }
452
453         if (ses->status == CifsNew) {
454                 if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
455                         (in_buf->Command != SMB_COM_NEGOTIATE))
456                         return -EAGAIN;
457                 /* else ok - we are setting up session */
458         }
459
460         if (ses->status == CifsExiting) {
461                 /* check if SMB session is bad because we are setting it up */
462                 if (in_buf->Command != SMB_COM_LOGOFF_ANDX)
463                         return -EAGAIN;
464                 /* else ok - we are shutting down session */
465         }
466
467         *ppmidQ = AllocMidQEntry(in_buf, ses->server);
468         if (*ppmidQ == NULL)
469                 return -ENOMEM;
470         spin_lock(&GlobalMid_Lock);
471         list_add_tail(&(*ppmidQ)->qhead, &ses->server->pending_mid_q);
472         spin_unlock(&GlobalMid_Lock);
473         return 0;
474 }
475
476 static int
477 wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *midQ)
478 {
479         int error;
480
481         error = wait_event_freezekillable_unsafe(server->response_q,
482                                     midQ->mid_state != MID_REQUEST_SUBMITTED);
483         if (error < 0)
484                 return -ERESTARTSYS;
485
486         return 0;
487 }
488
489 struct mid_q_entry *
490 cifs_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
491 {
492         int rc;
493         struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
494         struct mid_q_entry *mid;
495
496         if (rqst->rq_iov[0].iov_len != 4 ||
497             rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
498                 return ERR_PTR(-EIO);
499
500         /* enable signing if server requires it */
501         if (server->sign)
502                 hdr->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
503
504         mid = AllocMidQEntry(hdr, server);
505         if (mid == NULL)
506                 return ERR_PTR(-ENOMEM);
507
508         rc = cifs_sign_rqst(rqst, server, &mid->sequence_number);
509         if (rc) {
510                 DeleteMidQEntry(mid);
511                 return ERR_PTR(rc);
512         }
513
514         return mid;
515 }
516
517 /*
518  * Send a SMB request and set the callback function in the mid to handle
519  * the result. Caller is responsible for dealing with timeouts.
520  */
521 int
522 cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst,
523                 mid_receive_t *receive, mid_callback_t *callback,
524                 mid_handle_t *handle, void *cbdata, const int flags)
525 {
526         int rc, timeout, optype;
527         struct mid_q_entry *mid;
528         unsigned int credits = 0;
529
530         timeout = flags & CIFS_TIMEOUT_MASK;
531         optype = flags & CIFS_OP_MASK;
532
533         if ((flags & CIFS_HAS_CREDITS) == 0) {
534                 rc = wait_for_free_request(server, timeout, optype);
535                 if (rc)
536                         return rc;
537                 credits = 1;
538         }
539
540         mutex_lock(&server->srv_mutex);
541         mid = server->ops->setup_async_request(server, rqst);
542         if (IS_ERR(mid)) {
543                 mutex_unlock(&server->srv_mutex);
544                 add_credits_and_wake_if(server, credits, optype);
545                 return PTR_ERR(mid);
546         }
547
548         mid->receive = receive;
549         mid->callback = callback;
550         mid->callback_data = cbdata;
551         mid->handle = handle;
552         mid->mid_state = MID_REQUEST_SUBMITTED;
553
554         /* put it on the pending_mid_q */
555         spin_lock(&GlobalMid_Lock);
556         list_add_tail(&mid->qhead, &server->pending_mid_q);
557         spin_unlock(&GlobalMid_Lock);
558
559         /*
560          * Need to store the time in mid before calling I/O. For call_async,
561          * I/O response may come back and free the mid entry on another thread.
562          */
563         cifs_save_when_sent(mid);
564         cifs_in_send_inc(server);
565         rc = smb_send_rqst(server, rqst, flags);
566         cifs_in_send_dec(server);
567
568         if (rc < 0) {
569                 server->sequence_number -= 2;
570                 cifs_delete_mid(mid);
571         }
572
573         mutex_unlock(&server->srv_mutex);
574
575         if (rc == 0)
576                 return 0;
577
578         add_credits_and_wake_if(server, credits, optype);
579         return rc;
580 }
581
582 /*
583  *
584  * Send an SMB Request.  No response info (other than return code)
585  * needs to be parsed.
586  *
587  * flags indicate the type of request buffer and how long to wait
588  * and whether to log NT STATUS code (error) before mapping it to POSIX error
589  *
590  */
591 int
592 SendReceiveNoRsp(const unsigned int xid, struct cifs_ses *ses,
593                  char *in_buf, int flags)
594 {
595         int rc;
596         struct kvec iov[1];
597         struct kvec rsp_iov;
598         int resp_buf_type;
599
600         iov[0].iov_base = in_buf;
601         iov[0].iov_len = get_rfc1002_length(in_buf) + 4;
602         flags |= CIFS_NO_RESP;
603         rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
604         cifs_dbg(NOISY, "SendRcvNoRsp flags %d rc %d\n", flags, rc);
605
606         return rc;
607 }
608
609 static int
610 cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
611 {
612         int rc = 0;
613
614         cifs_dbg(FYI, "%s: cmd=%d mid=%llu state=%d\n",
615                  __func__, le16_to_cpu(mid->command), mid->mid, mid->mid_state);
616
617         spin_lock(&GlobalMid_Lock);
618         switch (mid->mid_state) {
619         case MID_RESPONSE_RECEIVED:
620                 spin_unlock(&GlobalMid_Lock);
621                 return rc;
622         case MID_RETRY_NEEDED:
623                 rc = -EAGAIN;
624                 break;
625         case MID_RESPONSE_MALFORMED:
626                 rc = -EIO;
627                 break;
628         case MID_SHUTDOWN:
629                 rc = -EHOSTDOWN;
630                 break;
631         default:
632                 list_del_init(&mid->qhead);
633                 cifs_dbg(VFS, "%s: invalid mid state mid=%llu state=%d\n",
634                          __func__, mid->mid, mid->mid_state);
635                 rc = -EIO;
636         }
637         spin_unlock(&GlobalMid_Lock);
638
639         DeleteMidQEntry(mid);
640         return rc;
641 }
642
643 static inline int
644 send_cancel(struct TCP_Server_Info *server, struct smb_rqst *rqst,
645             struct mid_q_entry *mid)
646 {
647         return server->ops->send_cancel ?
648                                 server->ops->send_cancel(server, rqst, mid) : 0;
649 }
650
651 int
652 cifs_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
653                    bool log_error)
654 {
655         unsigned int len = get_rfc1002_length(mid->resp_buf) + 4;
656
657         dump_smb(mid->resp_buf, min_t(u32, 92, len));
658
659         /* convert the length into a more usable form */
660         if (server->sign) {
661                 struct kvec iov[2];
662                 int rc = 0;
663                 struct smb_rqst rqst = { .rq_iov = iov,
664                                          .rq_nvec = 2 };
665
666                 iov[0].iov_base = mid->resp_buf;
667                 iov[0].iov_len = 4;
668                 iov[1].iov_base = (char *)mid->resp_buf + 4;
669                 iov[1].iov_len = len - 4;
670                 /* FIXME: add code to kill session */
671                 rc = cifs_verify_signature(&rqst, server,
672                                            mid->sequence_number);
673                 if (rc)
674                         cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
675                                  rc);
676         }
677
678         /* BB special case reconnect tid and uid here? */
679         return map_smb_to_linux_error(mid->resp_buf, log_error);
680 }
681
682 struct mid_q_entry *
683 cifs_setup_request(struct cifs_ses *ses, struct smb_rqst *rqst)
684 {
685         int rc;
686         struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
687         struct mid_q_entry *mid;
688
689         if (rqst->rq_iov[0].iov_len != 4 ||
690             rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
691                 return ERR_PTR(-EIO);
692
693         rc = allocate_mid(ses, hdr, &mid);
694         if (rc)
695                 return ERR_PTR(rc);
696         rc = cifs_sign_rqst(rqst, ses->server, &mid->sequence_number);
697         if (rc) {
698                 cifs_delete_mid(mid);
699                 return ERR_PTR(rc);
700         }
701         return mid;
702 }
703
704 int
705 cifs_send_recv(const unsigned int xid, struct cifs_ses *ses,
706                struct smb_rqst *rqst, int *resp_buf_type, const int flags,
707                struct kvec *resp_iov)
708 {
709         int rc = 0;
710         int timeout, optype;
711         struct mid_q_entry *midQ;
712         unsigned int credits = 1;
713         char *buf;
714
715         timeout = flags & CIFS_TIMEOUT_MASK;
716         optype = flags & CIFS_OP_MASK;
717
718         *resp_buf_type = CIFS_NO_BUFFER;  /* no response buf yet */
719
720         if ((ses == NULL) || (ses->server == NULL)) {
721                 cifs_dbg(VFS, "Null session\n");
722                 return -EIO;
723         }
724
725         if (ses->server->tcpStatus == CifsExiting)
726                 return -ENOENT;
727
728         /*
729          * Ensure that we do not send more than 50 overlapping requests
730          * to the same server. We may make this configurable later or
731          * use ses->maxReq.
732          */
733
734         rc = wait_for_free_request(ses->server, timeout, optype);
735         if (rc)
736                 return rc;
737
738         /*
739          * Make sure that we sign in the same order that we send on this socket
740          * and avoid races inside tcp sendmsg code that could cause corruption
741          * of smb data.
742          */
743
744         mutex_lock(&ses->server->srv_mutex);
745
746         midQ = ses->server->ops->setup_request(ses, rqst);
747         if (IS_ERR(midQ)) {
748                 mutex_unlock(&ses->server->srv_mutex);
749                 /* Update # of requests on wire to server */
750                 add_credits(ses->server, 1, optype);
751                 return PTR_ERR(midQ);
752         }
753
754         midQ->mid_state = MID_REQUEST_SUBMITTED;
755         cifs_in_send_inc(ses->server);
756         rc = smb_send_rqst(ses->server, rqst, flags);
757         cifs_in_send_dec(ses->server);
758         cifs_save_when_sent(midQ);
759
760         if (rc < 0)
761                 ses->server->sequence_number -= 2;
762         mutex_unlock(&ses->server->srv_mutex);
763
764         if (rc < 0)
765                 goto out;
766
767 #ifdef CONFIG_CIFS_SMB311
768         if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP))
769                 smb311_update_preauth_hash(ses, rqst->rq_iov+1,
770                                            rqst->rq_nvec-1);
771 #endif
772
773         if (timeout == CIFS_ASYNC_OP)
774                 goto out;
775
776         rc = wait_for_response(ses->server, midQ);
777         if (rc != 0) {
778                 cifs_dbg(FYI, "Cancelling wait for mid %llu\n", midQ->mid);
779                 send_cancel(ses->server, rqst, midQ);
780                 spin_lock(&GlobalMid_Lock);
781                 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
782                         midQ->mid_flags |= MID_WAIT_CANCELLED;
783                         midQ->callback = DeleteMidQEntry;
784                         spin_unlock(&GlobalMid_Lock);
785                         add_credits(ses->server, 1, optype);
786                         return rc;
787                 }
788                 spin_unlock(&GlobalMid_Lock);
789         }
790
791         rc = cifs_sync_mid_result(midQ, ses->server);
792         if (rc != 0) {
793                 add_credits(ses->server, 1, optype);
794                 return rc;
795         }
796
797         if (!midQ->resp_buf || midQ->mid_state != MID_RESPONSE_RECEIVED) {
798                 rc = -EIO;
799                 cifs_dbg(FYI, "Bad MID state?\n");
800                 goto out;
801         }
802
803         buf = (char *)midQ->resp_buf;
804         resp_iov->iov_base = buf;
805         resp_iov->iov_len = midQ->resp_buf_size +
806                 ses->server->vals->header_preamble_size;
807         if (midQ->large_buf)
808                 *resp_buf_type = CIFS_LARGE_BUFFER;
809         else
810                 *resp_buf_type = CIFS_SMALL_BUFFER;
811
812 #ifdef CONFIG_CIFS_SMB311
813         if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP)) {
814                 struct kvec iov = {
815                         .iov_base = buf,
816                         .iov_len = midQ->resp_buf_size
817                 };
818                 smb311_update_preauth_hash(ses, &iov, 1);
819         }
820 #endif
821
822         credits = ses->server->ops->get_credits(midQ);
823
824         rc = ses->server->ops->check_receive(midQ, ses->server,
825                                              flags & CIFS_LOG_ERROR);
826
827         /* mark it so buf will not be freed by cifs_delete_mid */
828         if ((flags & CIFS_NO_RESP) == 0)
829                 midQ->resp_buf = NULL;
830 out:
831         cifs_delete_mid(midQ);
832         add_credits(ses->server, credits, optype);
833
834         return rc;
835 }
836
837 int
838 SendReceive2(const unsigned int xid, struct cifs_ses *ses,
839              struct kvec *iov, int n_vec, int *resp_buf_type /* ret */,
840              const int flags, struct kvec *resp_iov)
841 {
842         struct smb_rqst rqst;
843         struct kvec s_iov[CIFS_MAX_IOV_SIZE], *new_iov;
844         int rc;
845
846         if (n_vec + 1 > CIFS_MAX_IOV_SIZE) {
847                 new_iov = kmalloc(sizeof(struct kvec) * (n_vec + 1),
848                                   GFP_KERNEL);
849                 if (!new_iov) {
850                         /* otherwise cifs_send_recv below sets resp_buf_type */
851                         *resp_buf_type = CIFS_NO_BUFFER;
852                         return -ENOMEM;
853                 }
854         } else
855                 new_iov = s_iov;
856
857         /* 1st iov is a RFC1001 length followed by the rest of the packet */
858         memcpy(new_iov + 1, iov, (sizeof(struct kvec) * n_vec));
859
860         new_iov[0].iov_base = new_iov[1].iov_base;
861         new_iov[0].iov_len = 4;
862         new_iov[1].iov_base += 4;
863         new_iov[1].iov_len -= 4;
864
865         memset(&rqst, 0, sizeof(struct smb_rqst));
866         rqst.rq_iov = new_iov;
867         rqst.rq_nvec = n_vec + 1;
868
869         rc = cifs_send_recv(xid, ses, &rqst, resp_buf_type, flags, resp_iov);
870         if (n_vec + 1 > CIFS_MAX_IOV_SIZE)
871                 kfree(new_iov);
872         return rc;
873 }
874
875 /* Like SendReceive2 but iov[0] does not contain an rfc1002 header */
876 int
877 smb2_send_recv(const unsigned int xid, struct cifs_ses *ses,
878                struct kvec *iov, int n_vec, int *resp_buf_type /* ret */,
879                const int flags, struct kvec *resp_iov)
880 {
881         struct smb_rqst rqst;
882         struct kvec s_iov[CIFS_MAX_IOV_SIZE], *new_iov;
883         int rc;
884         int i;
885         __u32 count;
886         __be32 rfc1002_marker;
887
888         if (n_vec + 1 > CIFS_MAX_IOV_SIZE) {
889                 new_iov = kmalloc(sizeof(struct kvec) * (n_vec + 1),
890                                   GFP_KERNEL);
891                 if (!new_iov)
892                         return -ENOMEM;
893         } else
894                 new_iov = s_iov;
895
896         /* 1st iov is an RFC1002 Session Message length */
897         memcpy(new_iov + 1, iov, (sizeof(struct kvec) * n_vec));
898
899         count = 0;
900         for (i = 1; i < n_vec + 1; i++)
901                 count += new_iov[i].iov_len;
902
903         rfc1002_marker = cpu_to_be32(count);
904
905         new_iov[0].iov_base = &rfc1002_marker;
906         new_iov[0].iov_len = 4;
907
908         memset(&rqst, 0, sizeof(struct smb_rqst));
909         rqst.rq_iov = new_iov;
910         rqst.rq_nvec = n_vec + 1;
911
912         rc = cifs_send_recv(xid, ses, &rqst, resp_buf_type, flags, resp_iov);
913         if (n_vec + 1 > CIFS_MAX_IOV_SIZE)
914                 kfree(new_iov);
915         return rc;
916 }
917
918 int
919 SendReceive(const unsigned int xid, struct cifs_ses *ses,
920             struct smb_hdr *in_buf, struct smb_hdr *out_buf,
921             int *pbytes_returned, const int timeout)
922 {
923         int rc = 0;
924         struct mid_q_entry *midQ;
925         unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
926         struct kvec iov = { .iov_base = in_buf, .iov_len = len };
927         struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
928
929         if (ses == NULL) {
930                 cifs_dbg(VFS, "Null smb session\n");
931                 return -EIO;
932         }
933         if (ses->server == NULL) {
934                 cifs_dbg(VFS, "Null tcp session\n");
935                 return -EIO;
936         }
937
938         if (ses->server->tcpStatus == CifsExiting)
939                 return -ENOENT;
940
941         /* Ensure that we do not send more than 50 overlapping requests
942            to the same server. We may make this configurable later or
943            use ses->maxReq */
944
945         if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
946                 cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
947                          len);
948                 return -EIO;
949         }
950
951         rc = wait_for_free_request(ses->server, timeout, 0);
952         if (rc)
953                 return rc;
954
955         /* make sure that we sign in the same order that we send on this socket
956            and avoid races inside tcp sendmsg code that could cause corruption
957            of smb data */
958
959         mutex_lock(&ses->server->srv_mutex);
960
961         rc = allocate_mid(ses, in_buf, &midQ);
962         if (rc) {
963                 mutex_unlock(&ses->server->srv_mutex);
964                 /* Update # of requests on wire to server */
965                 add_credits(ses->server, 1, 0);
966                 return rc;
967         }
968
969         rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
970         if (rc) {
971                 mutex_unlock(&ses->server->srv_mutex);
972                 goto out;
973         }
974
975         midQ->mid_state = MID_REQUEST_SUBMITTED;
976
977         cifs_in_send_inc(ses->server);
978         rc = smb_send(ses->server, in_buf, len);
979         cifs_in_send_dec(ses->server);
980         cifs_save_when_sent(midQ);
981
982         if (rc < 0)
983                 ses->server->sequence_number -= 2;
984
985         mutex_unlock(&ses->server->srv_mutex);
986
987         if (rc < 0)
988                 goto out;
989
990         if (timeout == CIFS_ASYNC_OP)
991                 goto out;
992
993         rc = wait_for_response(ses->server, midQ);
994         if (rc != 0) {
995                 send_cancel(ses->server, &rqst, midQ);
996                 spin_lock(&GlobalMid_Lock);
997                 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
998                         /* no longer considered to be "in-flight" */
999                         midQ->callback = DeleteMidQEntry;
1000                         spin_unlock(&GlobalMid_Lock);
1001                         add_credits(ses->server, 1, 0);
1002                         return rc;
1003                 }
1004                 spin_unlock(&GlobalMid_Lock);
1005         }
1006
1007         rc = cifs_sync_mid_result(midQ, ses->server);
1008         if (rc != 0) {
1009                 add_credits(ses->server, 1, 0);
1010                 return rc;
1011         }
1012
1013         if (!midQ->resp_buf || !out_buf ||
1014             midQ->mid_state != MID_RESPONSE_RECEIVED) {
1015                 rc = -EIO;
1016                 cifs_dbg(VFS, "Bad MID state?\n");
1017                 goto out;
1018         }
1019
1020         *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
1021         memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
1022         rc = cifs_check_receive(midQ, ses->server, 0);
1023 out:
1024         cifs_delete_mid(midQ);
1025         add_credits(ses->server, 1, 0);
1026
1027         return rc;
1028 }
1029
1030 /* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
1031    blocking lock to return. */
1032
1033 static int
1034 send_lock_cancel(const unsigned int xid, struct cifs_tcon *tcon,
1035                         struct smb_hdr *in_buf,
1036                         struct smb_hdr *out_buf)
1037 {
1038         int bytes_returned;
1039         struct cifs_ses *ses = tcon->ses;
1040         LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
1041
1042         /* We just modify the current in_buf to change
1043            the type of lock from LOCKING_ANDX_SHARED_LOCK
1044            or LOCKING_ANDX_EXCLUSIVE_LOCK to
1045            LOCKING_ANDX_CANCEL_LOCK. */
1046
1047         pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
1048         pSMB->Timeout = 0;
1049         pSMB->hdr.Mid = get_next_mid(ses->server);
1050
1051         return SendReceive(xid, ses, in_buf, out_buf,
1052                         &bytes_returned, 0);
1053 }
1054
1055 int
1056 SendReceiveBlockingLock(const unsigned int xid, struct cifs_tcon *tcon,
1057             struct smb_hdr *in_buf, struct smb_hdr *out_buf,
1058             int *pbytes_returned)
1059 {
1060         int rc = 0;
1061         int rstart = 0;
1062         struct mid_q_entry *midQ;
1063         struct cifs_ses *ses;
1064         unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
1065         struct kvec iov = { .iov_base = in_buf, .iov_len = len };
1066         struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
1067
1068         if (tcon == NULL || tcon->ses == NULL) {
1069                 cifs_dbg(VFS, "Null smb session\n");
1070                 return -EIO;
1071         }
1072         ses = tcon->ses;
1073
1074         if (ses->server == NULL) {
1075                 cifs_dbg(VFS, "Null tcp session\n");
1076                 return -EIO;
1077         }
1078
1079         if (ses->server->tcpStatus == CifsExiting)
1080                 return -ENOENT;
1081
1082         /* Ensure that we do not send more than 50 overlapping requests
1083            to the same server. We may make this configurable later or
1084            use ses->maxReq */
1085
1086         if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
1087                 cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
1088                          len);
1089                 return -EIO;
1090         }
1091
1092         rc = wait_for_free_request(ses->server, CIFS_BLOCKING_OP, 0);
1093         if (rc)
1094                 return rc;
1095
1096         /* make sure that we sign in the same order that we send on this socket
1097            and avoid races inside tcp sendmsg code that could cause corruption
1098            of smb data */
1099
1100         mutex_lock(&ses->server->srv_mutex);
1101
1102         rc = allocate_mid(ses, in_buf, &midQ);
1103         if (rc) {
1104                 mutex_unlock(&ses->server->srv_mutex);
1105                 return rc;
1106         }
1107
1108         rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
1109         if (rc) {
1110                 cifs_delete_mid(midQ);
1111                 mutex_unlock(&ses->server->srv_mutex);
1112                 return rc;
1113         }
1114
1115         midQ->mid_state = MID_REQUEST_SUBMITTED;
1116         cifs_in_send_inc(ses->server);
1117         rc = smb_send(ses->server, in_buf, len);
1118         cifs_in_send_dec(ses->server);
1119         cifs_save_when_sent(midQ);
1120
1121         if (rc < 0)
1122                 ses->server->sequence_number -= 2;
1123
1124         mutex_unlock(&ses->server->srv_mutex);
1125
1126         if (rc < 0) {
1127                 cifs_delete_mid(midQ);
1128                 return rc;
1129         }
1130
1131         /* Wait for a reply - allow signals to interrupt. */
1132         rc = wait_event_interruptible(ses->server->response_q,
1133                 (!(midQ->mid_state == MID_REQUEST_SUBMITTED)) ||
1134                 ((ses->server->tcpStatus != CifsGood) &&
1135                  (ses->server->tcpStatus != CifsNew)));
1136
1137         /* Were we interrupted by a signal ? */
1138         if ((rc == -ERESTARTSYS) &&
1139                 (midQ->mid_state == MID_REQUEST_SUBMITTED) &&
1140                 ((ses->server->tcpStatus == CifsGood) ||
1141                  (ses->server->tcpStatus == CifsNew))) {
1142
1143                 if (in_buf->Command == SMB_COM_TRANSACTION2) {
1144                         /* POSIX lock. We send a NT_CANCEL SMB to cause the
1145                            blocking lock to return. */
1146                         rc = send_cancel(ses->server, &rqst, midQ);
1147                         if (rc) {
1148                                 cifs_delete_mid(midQ);
1149                                 return rc;
1150                         }
1151                 } else {
1152                         /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
1153                            to cause the blocking lock to return. */
1154
1155                         rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
1156
1157                         /* If we get -ENOLCK back the lock may have
1158                            already been removed. Don't exit in this case. */
1159                         if (rc && rc != -ENOLCK) {
1160                                 cifs_delete_mid(midQ);
1161                                 return rc;
1162                         }
1163                 }
1164
1165                 rc = wait_for_response(ses->server, midQ);
1166                 if (rc) {
1167                         send_cancel(ses->server, &rqst, midQ);
1168                         spin_lock(&GlobalMid_Lock);
1169                         if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
1170                                 /* no longer considered to be "in-flight" */
1171                                 midQ->callback = DeleteMidQEntry;
1172                                 spin_unlock(&GlobalMid_Lock);
1173                                 return rc;
1174                         }
1175                         spin_unlock(&GlobalMid_Lock);
1176                 }
1177
1178                 /* We got the response - restart system call. */
1179                 rstart = 1;
1180         }
1181
1182         rc = cifs_sync_mid_result(midQ, ses->server);
1183         if (rc != 0)
1184                 return rc;
1185
1186         /* rcvd frame is ok */
1187         if (out_buf == NULL || midQ->mid_state != MID_RESPONSE_RECEIVED) {
1188                 rc = -EIO;
1189                 cifs_dbg(VFS, "Bad MID state?\n");
1190                 goto out;
1191         }
1192
1193         *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
1194         memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
1195         rc = cifs_check_receive(midQ, ses->server, 0);
1196 out:
1197         cifs_delete_mid(midQ);
1198         if (rstart && rc == -EACCES)
1199                 return -ERESTARTSYS;
1200         return rc;
1201 }