netfilter: nf_tables: move dumper state allocation into ->start
[linux-2.6-microblaze.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 unsigned long
205 smb2_rqst_len(struct smb_rqst *rqst, bool skip_rfc1002_marker)
206 {
207         unsigned int i;
208         struct kvec *iov;
209         int nvec;
210         unsigned long buflen = 0;
211
212         if (skip_rfc1002_marker && rqst->rq_iov[0].iov_len == 4) {
213                 iov = &rqst->rq_iov[1];
214                 nvec = rqst->rq_nvec - 1;
215         } else {
216                 iov = rqst->rq_iov;
217                 nvec = rqst->rq_nvec;
218         }
219
220         /* total up iov array first */
221         for (i = 0; i < nvec; i++)
222                 buflen += iov[i].iov_len;
223
224         /*
225          * Add in the page array if there is one. The caller needs to make
226          * sure rq_offset and rq_tailsz are set correctly. If a buffer of
227          * multiple pages ends at page boundary, rq_tailsz needs to be set to
228          * PAGE_SIZE.
229          */
230         if (rqst->rq_npages) {
231                 if (rqst->rq_npages == 1)
232                         buflen += rqst->rq_tailsz;
233                 else {
234                         /*
235                          * If there is more than one page, calculate the
236                          * buffer length based on rq_offset and rq_tailsz
237                          */
238                         buflen += rqst->rq_pagesz * (rqst->rq_npages - 1) -
239                                         rqst->rq_offset;
240                         buflen += rqst->rq_tailsz;
241                 }
242         }
243
244         return buflen;
245 }
246
247 static int
248 __smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
249                 struct smb_rqst *rqst)
250 {
251         int rc = 0;
252         struct kvec *iov;
253         int n_vec;
254         unsigned int send_length = 0;
255         unsigned int i, j;
256         size_t total_len = 0, sent, size;
257         struct socket *ssocket = server->ssocket;
258         struct msghdr smb_msg;
259         int val = 1;
260         __be32 rfc1002_marker;
261
262         if (cifs_rdma_enabled(server) && server->smbd_conn) {
263                 rc = smbd_send(server->smbd_conn, rqst);
264                 goto smbd_done;
265         }
266         if (ssocket == NULL)
267                 return -ENOTSOCK;
268
269         /* cork the socket */
270         kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
271                                 (char *)&val, sizeof(val));
272
273         for (j = 0; j < num_rqst; j++)
274                 send_length += smb2_rqst_len(&rqst[j], true);
275         rfc1002_marker = cpu_to_be32(send_length);
276
277         /* Generate a rfc1002 marker for SMB2+ */
278         if (server->vals->header_preamble_size == 0) {
279                 struct kvec hiov = {
280                         .iov_base = &rfc1002_marker,
281                         .iov_len  = 4
282                 };
283                 iov_iter_kvec(&smb_msg.msg_iter, WRITE | ITER_KVEC, &hiov,
284                               1, 4);
285                 rc = smb_send_kvec(server, &smb_msg, &sent);
286                 if (rc < 0)
287                         goto uncork;
288
289                 total_len += sent;
290                 send_length += 4;
291         }
292
293         cifs_dbg(FYI, "Sending smb: smb_len=%u\n", send_length);
294
295         for (j = 0; j < num_rqst; j++) {
296                 iov = rqst[j].rq_iov;
297                 n_vec = rqst[j].rq_nvec;
298
299                 size = 0;
300                 for (i = 0; i < n_vec; i++) {
301                         dump_smb(iov[i].iov_base, iov[i].iov_len);
302                         size += iov[i].iov_len;
303                 }
304
305                 iov_iter_kvec(&smb_msg.msg_iter, WRITE | ITER_KVEC,
306                               iov, n_vec, size);
307
308                 rc = smb_send_kvec(server, &smb_msg, &sent);
309                 if (rc < 0)
310                         goto uncork;
311
312                 total_len += sent;
313
314                 /* now walk the page array and send each page in it */
315                 for (i = 0; i < rqst[j].rq_npages; i++) {
316                         struct bio_vec bvec;
317
318                         bvec.bv_page = rqst[j].rq_pages[i];
319                         rqst_page_get_length(&rqst[j], i, &bvec.bv_len,
320                                              &bvec.bv_offset);
321
322                         iov_iter_bvec(&smb_msg.msg_iter, WRITE | ITER_BVEC,
323                                       &bvec, 1, bvec.bv_len);
324                         rc = smb_send_kvec(server, &smb_msg, &sent);
325                         if (rc < 0)
326                                 break;
327
328                         total_len += sent;
329                 }
330         }
331
332 uncork:
333         /* uncork it */
334         val = 0;
335         kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
336                                 (char *)&val, sizeof(val));
337
338         if ((total_len > 0) && (total_len != send_length)) {
339                 cifs_dbg(FYI, "partial send (wanted=%u sent=%zu): terminating session\n",
340                          send_length, total_len);
341                 /*
342                  * If we have only sent part of an SMB then the next SMB could
343                  * be taken as the remainder of this one. We need to kill the
344                  * socket so the server throws away the partial SMB
345                  */
346                 server->tcpStatus = CifsNeedReconnect;
347         }
348 smbd_done:
349         if (rc < 0 && rc != -EINTR)
350                 cifs_dbg(VFS, "Error %d sending data on socket to server\n",
351                          rc);
352         else
353                 rc = 0;
354
355         return rc;
356 }
357
358 static int
359 smb_send_rqst(struct TCP_Server_Info *server, struct smb_rqst *rqst, int flags)
360 {
361         struct smb_rqst cur_rqst;
362         int rc;
363
364         if (!(flags & CIFS_TRANSFORM_REQ))
365                 return __smb_send_rqst(server, 1, rqst);
366
367         if (!server->ops->init_transform_rq ||
368             !server->ops->free_transform_rq) {
369                 cifs_dbg(VFS, "Encryption requested but transform callbacks are missed\n");
370                 return -EIO;
371         }
372
373         rc = server->ops->init_transform_rq(server, &cur_rqst, rqst);
374         if (rc)
375                 return rc;
376
377         rc = __smb_send_rqst(server, 1, &cur_rqst);
378         server->ops->free_transform_rq(&cur_rqst);
379         return rc;
380 }
381
382 int
383 smb_send(struct TCP_Server_Info *server, struct smb_hdr *smb_buffer,
384          unsigned int smb_buf_length)
385 {
386         struct kvec iov[2];
387         struct smb_rqst rqst = { .rq_iov = iov,
388                                  .rq_nvec = 2 };
389
390         iov[0].iov_base = smb_buffer;
391         iov[0].iov_len = 4;
392         iov[1].iov_base = (char *)smb_buffer + 4;
393         iov[1].iov_len = smb_buf_length;
394
395         return __smb_send_rqst(server, 1, &rqst);
396 }
397
398 static int
399 wait_for_free_credits(struct TCP_Server_Info *server, const int timeout,
400                       int *credits)
401 {
402         int rc;
403
404         spin_lock(&server->req_lock);
405         if (timeout == CIFS_ASYNC_OP) {
406                 /* oplock breaks must not be held up */
407                 server->in_flight++;
408                 *credits -= 1;
409                 spin_unlock(&server->req_lock);
410                 return 0;
411         }
412
413         while (1) {
414                 if (*credits <= 0) {
415                         spin_unlock(&server->req_lock);
416                         cifs_num_waiters_inc(server);
417                         rc = wait_event_killable(server->request_q,
418                                                  has_credits(server, credits));
419                         cifs_num_waiters_dec(server);
420                         if (rc)
421                                 return rc;
422                         spin_lock(&server->req_lock);
423                 } else {
424                         if (server->tcpStatus == CifsExiting) {
425                                 spin_unlock(&server->req_lock);
426                                 return -ENOENT;
427                         }
428
429                         /*
430                          * Can not count locking commands against total
431                          * as they are allowed to block on server.
432                          */
433
434                         /* update # of requests on the wire to server */
435                         if (timeout != CIFS_BLOCKING_OP) {
436                                 *credits -= 1;
437                                 server->in_flight++;
438                         }
439                         spin_unlock(&server->req_lock);
440                         break;
441                 }
442         }
443         return 0;
444 }
445
446 static int
447 wait_for_free_request(struct TCP_Server_Info *server, const int timeout,
448                       const int optype)
449 {
450         int *val;
451
452         val = server->ops->get_credits_field(server, optype);
453         /* Since an echo is already inflight, no need to wait to send another */
454         if (*val <= 0 && optype == CIFS_ECHO_OP)
455                 return -EAGAIN;
456         return wait_for_free_credits(server, timeout, val);
457 }
458
459 int
460 cifs_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
461                       unsigned int *num, unsigned int *credits)
462 {
463         *num = size;
464         *credits = 0;
465         return 0;
466 }
467
468 static int allocate_mid(struct cifs_ses *ses, struct smb_hdr *in_buf,
469                         struct mid_q_entry **ppmidQ)
470 {
471         if (ses->server->tcpStatus == CifsExiting) {
472                 return -ENOENT;
473         }
474
475         if (ses->server->tcpStatus == CifsNeedReconnect) {
476                 cifs_dbg(FYI, "tcp session dead - return to caller to retry\n");
477                 return -EAGAIN;
478         }
479
480         if (ses->status == CifsNew) {
481                 if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
482                         (in_buf->Command != SMB_COM_NEGOTIATE))
483                         return -EAGAIN;
484                 /* else ok - we are setting up session */
485         }
486
487         if (ses->status == CifsExiting) {
488                 /* check if SMB session is bad because we are setting it up */
489                 if (in_buf->Command != SMB_COM_LOGOFF_ANDX)
490                         return -EAGAIN;
491                 /* else ok - we are shutting down session */
492         }
493
494         *ppmidQ = AllocMidQEntry(in_buf, ses->server);
495         if (*ppmidQ == NULL)
496                 return -ENOMEM;
497         spin_lock(&GlobalMid_Lock);
498         list_add_tail(&(*ppmidQ)->qhead, &ses->server->pending_mid_q);
499         spin_unlock(&GlobalMid_Lock);
500         return 0;
501 }
502
503 static int
504 wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *midQ)
505 {
506         int error;
507
508         error = wait_event_freezekillable_unsafe(server->response_q,
509                                     midQ->mid_state != MID_REQUEST_SUBMITTED);
510         if (error < 0)
511                 return -ERESTARTSYS;
512
513         return 0;
514 }
515
516 struct mid_q_entry *
517 cifs_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
518 {
519         int rc;
520         struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
521         struct mid_q_entry *mid;
522
523         if (rqst->rq_iov[0].iov_len != 4 ||
524             rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
525                 return ERR_PTR(-EIO);
526
527         /* enable signing if server requires it */
528         if (server->sign)
529                 hdr->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
530
531         mid = AllocMidQEntry(hdr, server);
532         if (mid == NULL)
533                 return ERR_PTR(-ENOMEM);
534
535         rc = cifs_sign_rqst(rqst, server, &mid->sequence_number);
536         if (rc) {
537                 DeleteMidQEntry(mid);
538                 return ERR_PTR(rc);
539         }
540
541         return mid;
542 }
543
544 /*
545  * Send a SMB request and set the callback function in the mid to handle
546  * the result. Caller is responsible for dealing with timeouts.
547  */
548 int
549 cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst,
550                 mid_receive_t *receive, mid_callback_t *callback,
551                 mid_handle_t *handle, void *cbdata, const int flags)
552 {
553         int rc, timeout, optype;
554         struct mid_q_entry *mid;
555         unsigned int credits = 0;
556
557         timeout = flags & CIFS_TIMEOUT_MASK;
558         optype = flags & CIFS_OP_MASK;
559
560         if ((flags & CIFS_HAS_CREDITS) == 0) {
561                 rc = wait_for_free_request(server, timeout, optype);
562                 if (rc)
563                         return rc;
564                 credits = 1;
565         }
566
567         mutex_lock(&server->srv_mutex);
568         mid = server->ops->setup_async_request(server, rqst);
569         if (IS_ERR(mid)) {
570                 mutex_unlock(&server->srv_mutex);
571                 add_credits_and_wake_if(server, credits, optype);
572                 return PTR_ERR(mid);
573         }
574
575         mid->receive = receive;
576         mid->callback = callback;
577         mid->callback_data = cbdata;
578         mid->handle = handle;
579         mid->mid_state = MID_REQUEST_SUBMITTED;
580
581         /* put it on the pending_mid_q */
582         spin_lock(&GlobalMid_Lock);
583         list_add_tail(&mid->qhead, &server->pending_mid_q);
584         spin_unlock(&GlobalMid_Lock);
585
586         /*
587          * Need to store the time in mid before calling I/O. For call_async,
588          * I/O response may come back and free the mid entry on another thread.
589          */
590         cifs_save_when_sent(mid);
591         cifs_in_send_inc(server);
592         rc = smb_send_rqst(server, rqst, flags);
593         cifs_in_send_dec(server);
594
595         if (rc < 0) {
596                 server->sequence_number -= 2;
597                 cifs_delete_mid(mid);
598         }
599
600         mutex_unlock(&server->srv_mutex);
601
602         if (rc == 0)
603                 return 0;
604
605         add_credits_and_wake_if(server, credits, optype);
606         return rc;
607 }
608
609 /*
610  *
611  * Send an SMB Request.  No response info (other than return code)
612  * needs to be parsed.
613  *
614  * flags indicate the type of request buffer and how long to wait
615  * and whether to log NT STATUS code (error) before mapping it to POSIX error
616  *
617  */
618 int
619 SendReceiveNoRsp(const unsigned int xid, struct cifs_ses *ses,
620                  char *in_buf, int flags)
621 {
622         int rc;
623         struct kvec iov[1];
624         struct kvec rsp_iov;
625         int resp_buf_type;
626
627         iov[0].iov_base = in_buf;
628         iov[0].iov_len = get_rfc1002_length(in_buf) + 4;
629         flags |= CIFS_NO_RESP;
630         rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
631         cifs_dbg(NOISY, "SendRcvNoRsp flags %d rc %d\n", flags, rc);
632
633         return rc;
634 }
635
636 static int
637 cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
638 {
639         int rc = 0;
640
641         cifs_dbg(FYI, "%s: cmd=%d mid=%llu state=%d\n",
642                  __func__, le16_to_cpu(mid->command), mid->mid, mid->mid_state);
643
644         spin_lock(&GlobalMid_Lock);
645         switch (mid->mid_state) {
646         case MID_RESPONSE_RECEIVED:
647                 spin_unlock(&GlobalMid_Lock);
648                 return rc;
649         case MID_RETRY_NEEDED:
650                 rc = -EAGAIN;
651                 break;
652         case MID_RESPONSE_MALFORMED:
653                 rc = -EIO;
654                 break;
655         case MID_SHUTDOWN:
656                 rc = -EHOSTDOWN;
657                 break;
658         default:
659                 list_del_init(&mid->qhead);
660                 cifs_dbg(VFS, "%s: invalid mid state mid=%llu state=%d\n",
661                          __func__, mid->mid, mid->mid_state);
662                 rc = -EIO;
663         }
664         spin_unlock(&GlobalMid_Lock);
665
666         DeleteMidQEntry(mid);
667         return rc;
668 }
669
670 static inline int
671 send_cancel(struct TCP_Server_Info *server, struct smb_rqst *rqst,
672             struct mid_q_entry *mid)
673 {
674         return server->ops->send_cancel ?
675                                 server->ops->send_cancel(server, rqst, mid) : 0;
676 }
677
678 int
679 cifs_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
680                    bool log_error)
681 {
682         unsigned int len = get_rfc1002_length(mid->resp_buf) + 4;
683
684         dump_smb(mid->resp_buf, min_t(u32, 92, len));
685
686         /* convert the length into a more usable form */
687         if (server->sign) {
688                 struct kvec iov[2];
689                 int rc = 0;
690                 struct smb_rqst rqst = { .rq_iov = iov,
691                                          .rq_nvec = 2 };
692
693                 iov[0].iov_base = mid->resp_buf;
694                 iov[0].iov_len = 4;
695                 iov[1].iov_base = (char *)mid->resp_buf + 4;
696                 iov[1].iov_len = len - 4;
697                 /* FIXME: add code to kill session */
698                 rc = cifs_verify_signature(&rqst, server,
699                                            mid->sequence_number);
700                 if (rc)
701                         cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
702                                  rc);
703         }
704
705         /* BB special case reconnect tid and uid here? */
706         return map_smb_to_linux_error(mid->resp_buf, log_error);
707 }
708
709 struct mid_q_entry *
710 cifs_setup_request(struct cifs_ses *ses, struct smb_rqst *rqst)
711 {
712         int rc;
713         struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
714         struct mid_q_entry *mid;
715
716         if (rqst->rq_iov[0].iov_len != 4 ||
717             rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
718                 return ERR_PTR(-EIO);
719
720         rc = allocate_mid(ses, hdr, &mid);
721         if (rc)
722                 return ERR_PTR(rc);
723         rc = cifs_sign_rqst(rqst, ses->server, &mid->sequence_number);
724         if (rc) {
725                 cifs_delete_mid(mid);
726                 return ERR_PTR(rc);
727         }
728         return mid;
729 }
730
731 int
732 cifs_send_recv(const unsigned int xid, struct cifs_ses *ses,
733                struct smb_rqst *rqst, int *resp_buf_type, const int flags,
734                struct kvec *resp_iov)
735 {
736         int rc = 0;
737         int timeout, optype;
738         struct mid_q_entry *midQ;
739         unsigned int credits = 1;
740         char *buf;
741
742         timeout = flags & CIFS_TIMEOUT_MASK;
743         optype = flags & CIFS_OP_MASK;
744
745         *resp_buf_type = CIFS_NO_BUFFER;  /* no response buf yet */
746
747         if ((ses == NULL) || (ses->server == NULL)) {
748                 cifs_dbg(VFS, "Null session\n");
749                 return -EIO;
750         }
751
752         if (ses->server->tcpStatus == CifsExiting)
753                 return -ENOENT;
754
755         /*
756          * Ensure that we do not send more than 50 overlapping requests
757          * to the same server. We may make this configurable later or
758          * use ses->maxReq.
759          */
760         rc = wait_for_free_request(ses->server, timeout, optype);
761         if (rc)
762                 return rc;
763
764         /*
765          * Make sure that we sign in the same order that we send on this socket
766          * and avoid races inside tcp sendmsg code that could cause corruption
767          * of smb data.
768          */
769
770         mutex_lock(&ses->server->srv_mutex);
771
772         midQ = ses->server->ops->setup_request(ses, rqst);
773         if (IS_ERR(midQ)) {
774                 mutex_unlock(&ses->server->srv_mutex);
775                 /* Update # of requests on wire to server */
776                 add_credits(ses->server, 1, optype);
777                 return PTR_ERR(midQ);
778         }
779
780         midQ->mid_state = MID_REQUEST_SUBMITTED;
781         cifs_in_send_inc(ses->server);
782         rc = smb_send_rqst(ses->server, rqst, flags);
783         cifs_in_send_dec(ses->server);
784         cifs_save_when_sent(midQ);
785
786         if (rc < 0)
787                 ses->server->sequence_number -= 2;
788         mutex_unlock(&ses->server->srv_mutex);
789
790         if (rc < 0)
791                 goto out;
792
793 #ifdef CONFIG_CIFS_SMB311
794         if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP))
795                 smb311_update_preauth_hash(ses, rqst->rq_iov,
796                                            rqst->rq_nvec);
797 #endif
798
799         if (timeout == CIFS_ASYNC_OP)
800                 goto out;
801
802         rc = wait_for_response(ses->server, midQ);
803         if (rc != 0) {
804                 cifs_dbg(FYI, "Cancelling wait for mid %llu\n", midQ->mid);
805                 send_cancel(ses->server, rqst, midQ);
806                 spin_lock(&GlobalMid_Lock);
807                 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
808                         midQ->mid_flags |= MID_WAIT_CANCELLED;
809                         midQ->callback = DeleteMidQEntry;
810                         spin_unlock(&GlobalMid_Lock);
811                         add_credits(ses->server, 1, optype);
812                         return rc;
813                 }
814                 spin_unlock(&GlobalMid_Lock);
815         }
816
817         rc = cifs_sync_mid_result(midQ, ses->server);
818         if (rc != 0) {
819                 add_credits(ses->server, 1, optype);
820                 return rc;
821         }
822
823         if (!midQ->resp_buf || midQ->mid_state != MID_RESPONSE_RECEIVED) {
824                 rc = -EIO;
825                 cifs_dbg(FYI, "Bad MID state?\n");
826                 goto out;
827         }
828
829         buf = (char *)midQ->resp_buf;
830         resp_iov->iov_base = buf;
831         resp_iov->iov_len = midQ->resp_buf_size +
832                 ses->server->vals->header_preamble_size;
833         if (midQ->large_buf)
834                 *resp_buf_type = CIFS_LARGE_BUFFER;
835         else
836                 *resp_buf_type = CIFS_SMALL_BUFFER;
837
838 #ifdef CONFIG_CIFS_SMB311
839         if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP)) {
840                 struct kvec iov = {
841                         .iov_base = resp_iov->iov_base,
842                         .iov_len = resp_iov->iov_len
843                 };
844                 smb311_update_preauth_hash(ses, &iov, 1);
845         }
846 #endif
847
848         credits = ses->server->ops->get_credits(midQ);
849
850         rc = ses->server->ops->check_receive(midQ, ses->server,
851                                              flags & CIFS_LOG_ERROR);
852
853         /* mark it so buf will not be freed by cifs_delete_mid */
854         if ((flags & CIFS_NO_RESP) == 0)
855                 midQ->resp_buf = NULL;
856 out:
857         cifs_delete_mid(midQ);
858         add_credits(ses->server, credits, optype);
859
860         return rc;
861 }
862
863 int
864 SendReceive2(const unsigned int xid, struct cifs_ses *ses,
865              struct kvec *iov, int n_vec, int *resp_buf_type /* ret */,
866              const int flags, struct kvec *resp_iov)
867 {
868         struct smb_rqst rqst;
869         struct kvec s_iov[CIFS_MAX_IOV_SIZE], *new_iov;
870         int rc;
871
872         if (n_vec + 1 > CIFS_MAX_IOV_SIZE) {
873                 new_iov = kmalloc_array(n_vec + 1, sizeof(struct kvec),
874                                         GFP_KERNEL);
875                 if (!new_iov) {
876                         /* otherwise cifs_send_recv below sets resp_buf_type */
877                         *resp_buf_type = CIFS_NO_BUFFER;
878                         return -ENOMEM;
879                 }
880         } else
881                 new_iov = s_iov;
882
883         /* 1st iov is a RFC1001 length followed by the rest of the packet */
884         memcpy(new_iov + 1, iov, (sizeof(struct kvec) * n_vec));
885
886         new_iov[0].iov_base = new_iov[1].iov_base;
887         new_iov[0].iov_len = 4;
888         new_iov[1].iov_base += 4;
889         new_iov[1].iov_len -= 4;
890
891         memset(&rqst, 0, sizeof(struct smb_rqst));
892         rqst.rq_iov = new_iov;
893         rqst.rq_nvec = n_vec + 1;
894
895         rc = cifs_send_recv(xid, ses, &rqst, resp_buf_type, flags, resp_iov);
896         if (n_vec + 1 > CIFS_MAX_IOV_SIZE)
897                 kfree(new_iov);
898         return rc;
899 }
900
901 int
902 SendReceive(const unsigned int xid, struct cifs_ses *ses,
903             struct smb_hdr *in_buf, struct smb_hdr *out_buf,
904             int *pbytes_returned, const int timeout)
905 {
906         int rc = 0;
907         struct mid_q_entry *midQ;
908         unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
909         struct kvec iov = { .iov_base = in_buf, .iov_len = len };
910         struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
911
912         if (ses == NULL) {
913                 cifs_dbg(VFS, "Null smb session\n");
914                 return -EIO;
915         }
916         if (ses->server == NULL) {
917                 cifs_dbg(VFS, "Null tcp session\n");
918                 return -EIO;
919         }
920
921         if (ses->server->tcpStatus == CifsExiting)
922                 return -ENOENT;
923
924         /* Ensure that we do not send more than 50 overlapping requests
925            to the same server. We may make this configurable later or
926            use ses->maxReq */
927
928         if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
929                 cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
930                          len);
931                 return -EIO;
932         }
933
934         rc = wait_for_free_request(ses->server, timeout, 0);
935         if (rc)
936                 return rc;
937
938         /* make sure that we sign in the same order that we send on this socket
939            and avoid races inside tcp sendmsg code that could cause corruption
940            of smb data */
941
942         mutex_lock(&ses->server->srv_mutex);
943
944         rc = allocate_mid(ses, in_buf, &midQ);
945         if (rc) {
946                 mutex_unlock(&ses->server->srv_mutex);
947                 /* Update # of requests on wire to server */
948                 add_credits(ses->server, 1, 0);
949                 return rc;
950         }
951
952         rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
953         if (rc) {
954                 mutex_unlock(&ses->server->srv_mutex);
955                 goto out;
956         }
957
958         midQ->mid_state = MID_REQUEST_SUBMITTED;
959
960         cifs_in_send_inc(ses->server);
961         rc = smb_send(ses->server, in_buf, len);
962         cifs_in_send_dec(ses->server);
963         cifs_save_when_sent(midQ);
964
965         if (rc < 0)
966                 ses->server->sequence_number -= 2;
967
968         mutex_unlock(&ses->server->srv_mutex);
969
970         if (rc < 0)
971                 goto out;
972
973         if (timeout == CIFS_ASYNC_OP)
974                 goto out;
975
976         rc = wait_for_response(ses->server, midQ);
977         if (rc != 0) {
978                 send_cancel(ses->server, &rqst, midQ);
979                 spin_lock(&GlobalMid_Lock);
980                 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
981                         /* no longer considered to be "in-flight" */
982                         midQ->callback = DeleteMidQEntry;
983                         spin_unlock(&GlobalMid_Lock);
984                         add_credits(ses->server, 1, 0);
985                         return rc;
986                 }
987                 spin_unlock(&GlobalMid_Lock);
988         }
989
990         rc = cifs_sync_mid_result(midQ, ses->server);
991         if (rc != 0) {
992                 add_credits(ses->server, 1, 0);
993                 return rc;
994         }
995
996         if (!midQ->resp_buf || !out_buf ||
997             midQ->mid_state != MID_RESPONSE_RECEIVED) {
998                 rc = -EIO;
999                 cifs_dbg(VFS, "Bad MID state?\n");
1000                 goto out;
1001         }
1002
1003         *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
1004         memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
1005         rc = cifs_check_receive(midQ, ses->server, 0);
1006 out:
1007         cifs_delete_mid(midQ);
1008         add_credits(ses->server, 1, 0);
1009
1010         return rc;
1011 }
1012
1013 /* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
1014    blocking lock to return. */
1015
1016 static int
1017 send_lock_cancel(const unsigned int xid, struct cifs_tcon *tcon,
1018                         struct smb_hdr *in_buf,
1019                         struct smb_hdr *out_buf)
1020 {
1021         int bytes_returned;
1022         struct cifs_ses *ses = tcon->ses;
1023         LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
1024
1025         /* We just modify the current in_buf to change
1026            the type of lock from LOCKING_ANDX_SHARED_LOCK
1027            or LOCKING_ANDX_EXCLUSIVE_LOCK to
1028            LOCKING_ANDX_CANCEL_LOCK. */
1029
1030         pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
1031         pSMB->Timeout = 0;
1032         pSMB->hdr.Mid = get_next_mid(ses->server);
1033
1034         return SendReceive(xid, ses, in_buf, out_buf,
1035                         &bytes_returned, 0);
1036 }
1037
1038 int
1039 SendReceiveBlockingLock(const unsigned int xid, struct cifs_tcon *tcon,
1040             struct smb_hdr *in_buf, struct smb_hdr *out_buf,
1041             int *pbytes_returned)
1042 {
1043         int rc = 0;
1044         int rstart = 0;
1045         struct mid_q_entry *midQ;
1046         struct cifs_ses *ses;
1047         unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
1048         struct kvec iov = { .iov_base = in_buf, .iov_len = len };
1049         struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
1050
1051         if (tcon == NULL || tcon->ses == NULL) {
1052                 cifs_dbg(VFS, "Null smb session\n");
1053                 return -EIO;
1054         }
1055         ses = tcon->ses;
1056
1057         if (ses->server == NULL) {
1058                 cifs_dbg(VFS, "Null tcp session\n");
1059                 return -EIO;
1060         }
1061
1062         if (ses->server->tcpStatus == CifsExiting)
1063                 return -ENOENT;
1064
1065         /* Ensure that we do not send more than 50 overlapping requests
1066            to the same server. We may make this configurable later or
1067            use ses->maxReq */
1068
1069         if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
1070                 cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
1071                          len);
1072                 return -EIO;
1073         }
1074
1075         rc = wait_for_free_request(ses->server, CIFS_BLOCKING_OP, 0);
1076         if (rc)
1077                 return rc;
1078
1079         /* make sure that we sign in the same order that we send on this socket
1080            and avoid races inside tcp sendmsg code that could cause corruption
1081            of smb data */
1082
1083         mutex_lock(&ses->server->srv_mutex);
1084
1085         rc = allocate_mid(ses, in_buf, &midQ);
1086         if (rc) {
1087                 mutex_unlock(&ses->server->srv_mutex);
1088                 return rc;
1089         }
1090
1091         rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
1092         if (rc) {
1093                 cifs_delete_mid(midQ);
1094                 mutex_unlock(&ses->server->srv_mutex);
1095                 return rc;
1096         }
1097
1098         midQ->mid_state = MID_REQUEST_SUBMITTED;
1099         cifs_in_send_inc(ses->server);
1100         rc = smb_send(ses->server, in_buf, len);
1101         cifs_in_send_dec(ses->server);
1102         cifs_save_when_sent(midQ);
1103
1104         if (rc < 0)
1105                 ses->server->sequence_number -= 2;
1106
1107         mutex_unlock(&ses->server->srv_mutex);
1108
1109         if (rc < 0) {
1110                 cifs_delete_mid(midQ);
1111                 return rc;
1112         }
1113
1114         /* Wait for a reply - allow signals to interrupt. */
1115         rc = wait_event_interruptible(ses->server->response_q,
1116                 (!(midQ->mid_state == MID_REQUEST_SUBMITTED)) ||
1117                 ((ses->server->tcpStatus != CifsGood) &&
1118                  (ses->server->tcpStatus != CifsNew)));
1119
1120         /* Were we interrupted by a signal ? */
1121         if ((rc == -ERESTARTSYS) &&
1122                 (midQ->mid_state == MID_REQUEST_SUBMITTED) &&
1123                 ((ses->server->tcpStatus == CifsGood) ||
1124                  (ses->server->tcpStatus == CifsNew))) {
1125
1126                 if (in_buf->Command == SMB_COM_TRANSACTION2) {
1127                         /* POSIX lock. We send a NT_CANCEL SMB to cause the
1128                            blocking lock to return. */
1129                         rc = send_cancel(ses->server, &rqst, midQ);
1130                         if (rc) {
1131                                 cifs_delete_mid(midQ);
1132                                 return rc;
1133                         }
1134                 } else {
1135                         /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
1136                            to cause the blocking lock to return. */
1137
1138                         rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
1139
1140                         /* If we get -ENOLCK back the lock may have
1141                            already been removed. Don't exit in this case. */
1142                         if (rc && rc != -ENOLCK) {
1143                                 cifs_delete_mid(midQ);
1144                                 return rc;
1145                         }
1146                 }
1147
1148                 rc = wait_for_response(ses->server, midQ);
1149                 if (rc) {
1150                         send_cancel(ses->server, &rqst, midQ);
1151                         spin_lock(&GlobalMid_Lock);
1152                         if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
1153                                 /* no longer considered to be "in-flight" */
1154                                 midQ->callback = DeleteMidQEntry;
1155                                 spin_unlock(&GlobalMid_Lock);
1156                                 return rc;
1157                         }
1158                         spin_unlock(&GlobalMid_Lock);
1159                 }
1160
1161                 /* We got the response - restart system call. */
1162                 rstart = 1;
1163         }
1164
1165         rc = cifs_sync_mid_result(midQ, ses->server);
1166         if (rc != 0)
1167                 return rc;
1168
1169         /* rcvd frame is ok */
1170         if (out_buf == NULL || midQ->mid_state != MID_RESPONSE_RECEIVED) {
1171                 rc = -EIO;
1172                 cifs_dbg(VFS, "Bad MID state?\n");
1173                 goto out;
1174         }
1175
1176         *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
1177         memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
1178         rc = cifs_check_receive(midQ, ses->server, 0);
1179 out:
1180         cifs_delete_mid(midQ);
1181         if (rstart && rc == -EACCES)
1182                 return -ERESTARTSYS;
1183         return rc;
1184 }