[CIFS] fix small memory leak in an error path in new posix mkdir
[linux-2.6] / fs / cifs / transport.c
1 /*
2  *   fs/cifs/transport.c
3  *
4  *   Copyright (C) International Business Machines  Corp., 2002,2007
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/wait.h>
26 #include <linux/net.h>
27 #include <linux/delay.h>
28 #include <asm/uaccess.h>
29 #include <asm/processor.h>
30 #include <linux/mempool.h>
31 #include "cifspdu.h"
32 #include "cifsglob.h"
33 #include "cifsproto.h"
34 #include "cifs_debug.h"
35
36 extern mempool_t *cifs_mid_poolp;
37 extern struct kmem_cache *cifs_oplock_cachep;
38
39 static struct mid_q_entry *
40 AllocMidQEntry(const struct smb_hdr *smb_buffer, struct cifsSesInfo *ses)
41 {
42         struct mid_q_entry *temp;
43
44         if (ses == NULL) {
45                 cERROR(1, ("Null session passed in to AllocMidQEntry"));
46                 return NULL;
47         }
48         if (ses->server == NULL) {
49                 cERROR(1, ("Null TCP session in AllocMidQEntry"));
50                 return NULL;
51         }
52
53         temp = (struct mid_q_entry *) mempool_alloc(cifs_mid_poolp,
54                                                     GFP_KERNEL | GFP_NOFS);
55         if (temp == NULL)
56                 return temp;
57         else {
58                 memset(temp, 0, sizeof(struct mid_q_entry));
59                 temp->mid = smb_buffer->Mid;    /* always LE */
60                 temp->pid = current->pid;
61                 temp->command = smb_buffer->Command;
62                 cFYI(1, ("For smb_command %d", temp->command));
63         /*      do_gettimeofday(&temp->when_sent);*/ /* easier to use jiffies */
64                 /* when mid allocated can be before when sent */
65                 temp->when_alloc = jiffies;
66                 temp->ses = ses;
67                 temp->tsk = current;
68         }
69
70         spin_lock(&GlobalMid_Lock);
71         list_add_tail(&temp->qhead, &ses->server->pending_mid_q);
72         atomic_inc(&midCount);
73         temp->midState = MID_REQUEST_ALLOCATED;
74         spin_unlock(&GlobalMid_Lock);
75         return temp;
76 }
77
78 static void
79 DeleteMidQEntry(struct mid_q_entry *midEntry)
80 {
81 #ifdef CONFIG_CIFS_STATS2
82         unsigned long now;
83 #endif
84         spin_lock(&GlobalMid_Lock);
85         midEntry->midState = MID_FREE;
86         list_del(&midEntry->qhead);
87         atomic_dec(&midCount);
88         spin_unlock(&GlobalMid_Lock);
89         if (midEntry->largeBuf)
90                 cifs_buf_release(midEntry->resp_buf);
91         else
92                 cifs_small_buf_release(midEntry->resp_buf);
93 #ifdef CONFIG_CIFS_STATS2
94         now = jiffies;
95         /* commands taking longer than one second are indications that
96            something is wrong, unless it is quite a slow link or server */
97         if ((now - midEntry->when_alloc) > HZ) {
98                 if ((cifsFYI & CIFS_TIMER) &&
99                    (midEntry->command != SMB_COM_LOCKING_ANDX)) {
100                         printk(KERN_DEBUG " CIFS slow rsp: cmd %d mid %d",
101                                midEntry->command, midEntry->mid);
102                         printk(" A: 0x%lx S: 0x%lx R: 0x%lx\n",
103                                now - midEntry->when_alloc,
104                                now - midEntry->when_sent,
105                                now - midEntry->when_received);
106                 }
107         }
108 #endif
109         mempool_free(midEntry, cifs_mid_poolp);
110 }
111
112 struct oplock_q_entry *
113 AllocOplockQEntry(struct inode *pinode, __u16 fid, struct cifsTconInfo *tcon)
114 {
115         struct oplock_q_entry *temp;
116         if ((pinode == NULL) || (tcon == NULL)) {
117                 cERROR(1, ("Null parms passed to AllocOplockQEntry"));
118                 return NULL;
119         }
120         temp = (struct oplock_q_entry *) kmem_cache_alloc(cifs_oplock_cachep,
121                                                        GFP_KERNEL);
122         if (temp == NULL)
123                 return temp;
124         else {
125                 temp->pinode = pinode;
126                 temp->tcon = tcon;
127                 temp->netfid = fid;
128                 spin_lock(&GlobalMid_Lock);
129                 list_add_tail(&temp->qhead, &GlobalOplock_Q);
130                 spin_unlock(&GlobalMid_Lock);
131         }
132         return temp;
133
134 }
135
136 void DeleteOplockQEntry(struct oplock_q_entry *oplockEntry)
137 {
138         spin_lock(&GlobalMid_Lock);
139     /* should we check if list empty first? */
140         list_del(&oplockEntry->qhead);
141         spin_unlock(&GlobalMid_Lock);
142         kmem_cache_free(cifs_oplock_cachep, oplockEntry);
143 }
144
145 int
146 smb_send(struct socket *ssocket, struct smb_hdr *smb_buffer,
147          unsigned int smb_buf_length, struct sockaddr *sin)
148 {
149         int rc = 0;
150         int i = 0;
151         struct msghdr smb_msg;
152         struct kvec iov;
153         unsigned len = smb_buf_length + 4;
154
155         if (ssocket == NULL)
156                 return -ENOTSOCK; /* BB eventually add reconnect code here */
157         iov.iov_base = smb_buffer;
158         iov.iov_len = len;
159
160         smb_msg.msg_name = sin;
161         smb_msg.msg_namelen = sizeof(struct sockaddr);
162         smb_msg.msg_control = NULL;
163         smb_msg.msg_controllen = 0;
164         smb_msg.msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL; /* BB add more flags?*/
165
166         /* smb header is converted in header_assemble. bcc and rest of SMB word
167            area, and byte area if necessary, is converted to littleendian in
168            cifssmb.c and RFC1001 len is converted to bigendian in smb_send
169            Flags2 is converted in SendReceive */
170
171         smb_buffer->smb_buf_length = cpu_to_be32(smb_buffer->smb_buf_length);
172         cFYI(1, ("Sending smb of length %d", smb_buf_length));
173         dump_smb(smb_buffer, len);
174
175         while (len > 0) {
176                 rc = kernel_sendmsg(ssocket, &smb_msg, &iov, 1, len);
177                 if ((rc == -ENOSPC) || (rc == -EAGAIN)) {
178                         i++;
179                 /* smaller timeout here than send2 since smaller size */
180                 /* Although it may not be required, this also is smaller
181                    oplock break time */
182                         if (i > 12) {
183                                 cERROR(1,
184                                    ("sends on sock %p stuck for 7 seconds",
185                                     ssocket));
186                                 rc = -EAGAIN;
187                                 break;
188                         }
189                         msleep(1 << i);
190                         continue;
191                 }
192                 if (rc < 0)
193                         break;
194                 else
195                         i = 0; /* reset i after each successful send */
196                 iov.iov_base += rc;
197                 iov.iov_len -= rc;
198                 len -= rc;
199         }
200
201         if (rc < 0) {
202                 cERROR(1, ("Error %d sending data on socket to server", rc));
203         } else {
204                 rc = 0;
205         }
206
207         /* Don't want to modify the buffer as a
208            side effect of this call. */
209         smb_buffer->smb_buf_length = smb_buf_length;
210
211         return rc;
212 }
213
214 static int
215 smb_send2(struct socket *ssocket, struct kvec *iov, int n_vec,
216           struct sockaddr *sin)
217 {
218         int rc = 0;
219         int i = 0;
220         struct msghdr smb_msg;
221         struct smb_hdr *smb_buffer = iov[0].iov_base;
222         unsigned int len = iov[0].iov_len;
223         unsigned int total_len;
224         int first_vec = 0;
225         unsigned int smb_buf_length = smb_buffer->smb_buf_length;
226
227         if (ssocket == NULL)
228                 return -ENOTSOCK; /* BB eventually add reconnect code here */
229
230         smb_msg.msg_name = sin;
231         smb_msg.msg_namelen = sizeof(struct sockaddr);
232         smb_msg.msg_control = NULL;
233         smb_msg.msg_controllen = 0;
234         smb_msg.msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL; /* BB add more flags?*/
235
236         /* smb header is converted in header_assemble. bcc and rest of SMB word
237            area, and byte area if necessary, is converted to littleendian in
238            cifssmb.c and RFC1001 len is converted to bigendian in smb_send
239            Flags2 is converted in SendReceive */
240
241
242         total_len = 0;
243         for (i = 0; i < n_vec; i++)
244                 total_len += iov[i].iov_len;
245
246         smb_buffer->smb_buf_length = cpu_to_be32(smb_buffer->smb_buf_length);
247         cFYI(1, ("Sending smb:  total_len %d", total_len));
248         dump_smb(smb_buffer, len);
249
250         while (total_len) {
251                 rc = kernel_sendmsg(ssocket, &smb_msg, &iov[first_vec],
252                                     n_vec - first_vec, total_len);
253                 if ((rc == -ENOSPC) || (rc == -EAGAIN)) {
254                         i++;
255                         if (i >= 14) {
256                                 cERROR(1,
257                                    ("sends on sock %p stuck for 15 seconds",
258                                     ssocket));
259                                 rc = -EAGAIN;
260                                 break;
261                         }
262                         msleep(1 << i);
263                         continue;
264                 }
265                 if (rc < 0)
266                         break;
267
268                 if (rc >= total_len) {
269                         WARN_ON(rc > total_len);
270                         break;
271                 }
272                 if (rc == 0) {
273                         /* should never happen, letting socket clear before
274                            retrying is our only obvious option here */
275                         cERROR(1, ("tcp sent no data"));
276                         msleep(500);
277                         continue;
278                 }
279                 total_len -= rc;
280                 /* the line below resets i */
281                 for (i = first_vec; i < n_vec; i++) {
282                         if (iov[i].iov_len) {
283                                 if (rc > iov[i].iov_len) {
284                                         rc -= iov[i].iov_len;
285                                         iov[i].iov_len = 0;
286                                 } else {
287                                         iov[i].iov_base += rc;
288                                         iov[i].iov_len -= rc;
289                                         first_vec = i;
290                                         break;
291                                 }
292                         }
293                 }
294                 i = 0; /* in case we get ENOSPC on the next send */
295         }
296
297         if (rc < 0) {
298                 cERROR(1, ("Error %d sending data on socket to server", rc));
299         } else
300                 rc = 0;
301
302         /* Don't want to modify the buffer as a
303            side effect of this call. */
304         smb_buffer->smb_buf_length = smb_buf_length;
305
306         return rc;
307 }
308
309 static int wait_for_free_request(struct cifsSesInfo *ses, const int long_op)
310 {
311         if (long_op == -1) {
312                 /* oplock breaks must not be held up */
313                 atomic_inc(&ses->server->inFlight);
314         } else {
315                 spin_lock(&GlobalMid_Lock);
316                 while (1) {
317                         if (atomic_read(&ses->server->inFlight) >=
318                                         cifs_max_pending){
319                                 spin_unlock(&GlobalMid_Lock);
320 #ifdef CONFIG_CIFS_STATS2
321                                 atomic_inc(&ses->server->num_waiters);
322 #endif
323                                 wait_event(ses->server->request_q,
324                                         atomic_read(&ses->server->inFlight)
325                                          < cifs_max_pending);
326 #ifdef CONFIG_CIFS_STATS2
327                                 atomic_dec(&ses->server->num_waiters);
328 #endif
329                                 spin_lock(&GlobalMid_Lock);
330                         } else {
331                                 if (ses->server->tcpStatus == CifsExiting) {
332                                         spin_unlock(&GlobalMid_Lock);
333                                         return -ENOENT;
334                                 }
335
336                                 /* can not count locking commands against total
337                                    as they are allowed to block on server */
338
339                                 /* update # of requests on the wire to server */
340                                 if (long_op < 3)
341                                         atomic_inc(&ses->server->inFlight);
342                                 spin_unlock(&GlobalMid_Lock);
343                                 break;
344                         }
345                 }
346         }
347         return 0;
348 }
349
350 static int allocate_mid(struct cifsSesInfo *ses, struct smb_hdr *in_buf,
351                         struct mid_q_entry **ppmidQ)
352 {
353         if (ses->server->tcpStatus == CifsExiting) {
354                 return -ENOENT;
355         } else if (ses->server->tcpStatus == CifsNeedReconnect) {
356                 cFYI(1, ("tcp session dead - return to caller to retry"));
357                 return -EAGAIN;
358         } else if (ses->status != CifsGood) {
359                 /* check if SMB session is bad because we are setting it up */
360                 if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
361                         (in_buf->Command != SMB_COM_NEGOTIATE)) {
362                         return -EAGAIN;
363                 } /* else ok - we are setting up session */
364         }
365         *ppmidQ = AllocMidQEntry(in_buf, ses);
366         if (*ppmidQ == NULL)
367                 return -ENOMEM;
368         return 0;
369 }
370
371 static int wait_for_response(struct cifsSesInfo *ses,
372                         struct mid_q_entry *midQ,
373                         unsigned long timeout,
374                         unsigned long time_to_wait)
375 {
376         unsigned long curr_timeout;
377
378         for (;;) {
379                 curr_timeout = timeout + jiffies;
380                 wait_event(ses->server->response_q,
381                         (!(midQ->midState == MID_REQUEST_SUBMITTED)) ||
382                         time_after(jiffies, curr_timeout) ||
383                         ((ses->server->tcpStatus != CifsGood) &&
384                          (ses->server->tcpStatus != CifsNew)));
385
386                 if (time_after(jiffies, curr_timeout) &&
387                         (midQ->midState == MID_REQUEST_SUBMITTED) &&
388                         ((ses->server->tcpStatus == CifsGood) ||
389                          (ses->server->tcpStatus == CifsNew))) {
390
391                         unsigned long lrt;
392
393                         /* We timed out. Is the server still
394                            sending replies ? */
395                         spin_lock(&GlobalMid_Lock);
396                         lrt = ses->server->lstrp;
397                         spin_unlock(&GlobalMid_Lock);
398
399                         /* Calculate time_to_wait past last receive time.
400                          Although we prefer not to time out if the
401                          server is still responding - we will time
402                          out if the server takes more than 15 (or 45
403                          or 180) seconds to respond to this request
404                          and has not responded to any request from
405                          other threads on the client within 10 seconds */
406                         lrt += time_to_wait;
407                         if (time_after(jiffies, lrt)) {
408                                 /* No replies for time_to_wait. */
409                                 cERROR(1, ("server not responding"));
410                                 return -1;
411                         }
412                 } else {
413                         return 0;
414                 }
415         }
416 }
417
418 int
419 SendReceive2(const unsigned int xid, struct cifsSesInfo *ses,
420              struct kvec *iov, int n_vec, int *pRespBufType /* ret */,
421              const int long_op)
422 {
423         int rc = 0;
424         unsigned int receive_len;
425         unsigned long timeout;
426         struct mid_q_entry *midQ;
427         struct smb_hdr *in_buf = iov[0].iov_base;
428
429         *pRespBufType = CIFS_NO_BUFFER;  /* no response buf yet */
430
431         if ((ses == NULL) || (ses->server == NULL)) {
432                 cifs_small_buf_release(in_buf);
433                 cERROR(1, ("Null session"));
434                 return -EIO;
435         }
436
437         if (ses->server->tcpStatus == CifsExiting) {
438                 cifs_small_buf_release(in_buf);
439                 return -ENOENT;
440         }
441
442         /* Ensure that we do not send more than 50 overlapping requests
443            to the same server. We may make this configurable later or
444            use ses->maxReq */
445
446         rc = wait_for_free_request(ses, long_op);
447         if (rc) {
448                 cifs_small_buf_release(in_buf);
449                 return rc;
450         }
451
452         /* make sure that we sign in the same order that we send on this socket
453            and avoid races inside tcp sendmsg code that could cause corruption
454            of smb data */
455
456         down(&ses->server->tcpSem);
457
458         rc = allocate_mid(ses, in_buf, &midQ);
459         if (rc) {
460                 up(&ses->server->tcpSem);
461                 cifs_small_buf_release(in_buf);
462                 /* Update # of requests on wire to server */
463                 atomic_dec(&ses->server->inFlight);
464                 wake_up(&ses->server->request_q);
465                 return rc;
466         }
467
468         rc = cifs_sign_smb2(iov, n_vec, ses->server, &midQ->sequence_number);
469
470         midQ->midState = MID_REQUEST_SUBMITTED;
471 #ifdef CONFIG_CIFS_STATS2
472         atomic_inc(&ses->server->inSend);
473 #endif
474         rc = smb_send2(ses->server->ssocket, iov, n_vec,
475                       (struct sockaddr *) &(ses->server->addr.sockAddr));
476 #ifdef CONFIG_CIFS_STATS2
477         atomic_dec(&ses->server->inSend);
478         midQ->when_sent = jiffies;
479 #endif
480
481         up(&ses->server->tcpSem);
482         cifs_small_buf_release(in_buf);
483
484         if (rc < 0)
485                 goto out;
486
487         if (long_op == -1)
488                 goto out;
489         else if (long_op == 2) /* writes past end of file can take loong time */
490                 timeout = 180 * HZ;
491         else if (long_op == 1)
492                 timeout = 45 * HZ; /* should be greater than
493                         servers oplock break timeout (about 43 seconds) */
494         else
495                 timeout = 15 * HZ;
496
497         /* wait for 15 seconds or until woken up due to response arriving or
498            due to last connection to this server being unmounted */
499         if (signal_pending(current)) {
500                 /* if signal pending do not hold up user for full smb timeout
501                 but we still give response a chance to complete */
502                 timeout = 2 * HZ;
503         }
504
505         /* No user interrupts in wait - wreaks havoc with performance */
506         wait_for_response(ses, midQ, timeout, 10 * HZ);
507
508         spin_lock(&GlobalMid_Lock);
509         if (midQ->resp_buf) {
510                 spin_unlock(&GlobalMid_Lock);
511                 receive_len = midQ->resp_buf->smb_buf_length;
512         } else {
513                 cERROR(1, ("No response to cmd %d mid %d",
514                         midQ->command, midQ->mid));
515                 if (midQ->midState == MID_REQUEST_SUBMITTED) {
516                         if (ses->server->tcpStatus == CifsExiting)
517                                 rc = -EHOSTDOWN;
518                         else {
519                                 ses->server->tcpStatus = CifsNeedReconnect;
520                                 midQ->midState = MID_RETRY_NEEDED;
521                         }
522                 }
523
524                 if (rc != -EHOSTDOWN) {
525                         if (midQ->midState == MID_RETRY_NEEDED) {
526                                 rc = -EAGAIN;
527                                 cFYI(1, ("marking request for retry"));
528                         } else {
529                                 rc = -EIO;
530                         }
531                 }
532                 spin_unlock(&GlobalMid_Lock);
533                 DeleteMidQEntry(midQ);
534                 /* Update # of requests on wire to server */
535                 atomic_dec(&ses->server->inFlight);
536                 wake_up(&ses->server->request_q);
537                 return rc;
538         }
539
540         if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
541                 cERROR(1, ("Frame too large received.  Length: %d  Xid: %d",
542                         receive_len, xid));
543                 rc = -EIO;
544         } else {                /* rcvd frame is ok */
545                 if (midQ->resp_buf &&
546                         (midQ->midState == MID_RESPONSE_RECEIVED)) {
547
548                         iov[0].iov_base = (char *)midQ->resp_buf;
549                         if (midQ->largeBuf)
550                                 *pRespBufType = CIFS_LARGE_BUFFER;
551                         else
552                                 *pRespBufType = CIFS_SMALL_BUFFER;
553                         iov[0].iov_len = receive_len + 4;
554
555                         dump_smb(midQ->resp_buf, 80);
556                         /* convert the length into a more usable form */
557                         if ((receive_len > 24) &&
558                            (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
559                                         SECMODE_SIGN_ENABLED))) {
560                                 rc = cifs_verify_signature(midQ->resp_buf,
561                                                 &ses->server->mac_signing_key,
562                                                 midQ->sequence_number+1);
563                                 if (rc) {
564                                         cERROR(1, ("Unexpected SMB signature"));
565                                         /* BB FIXME add code to kill session */
566                                 }
567                         }
568
569                         /* BB special case reconnect tid and uid here? */
570                         /* BB special case Errbadpassword and pwdexpired here */
571                         rc = map_smb_to_linux_error(midQ->resp_buf);
572
573                         /* convert ByteCount if necessary */
574                         if (receive_len >= sizeof(struct smb_hdr) - 4
575                             /* do not count RFC1001 header */  +
576                             (2 * midQ->resp_buf->WordCount) + 2 /* bcc */ )
577                                 BCC(midQ->resp_buf) =
578                                         le16_to_cpu(BCC_LE(midQ->resp_buf));
579                         midQ->resp_buf = NULL;  /* mark it so will not be freed
580                                                 by DeleteMidQEntry */
581                 } else {
582                         rc = -EIO;
583                         cFYI(1, ("Bad MID state?"));
584                 }
585         }
586
587 out:
588         DeleteMidQEntry(midQ);
589         atomic_dec(&ses->server->inFlight);
590         wake_up(&ses->server->request_q);
591
592         return rc;
593 }
594
595 int
596 SendReceive(const unsigned int xid, struct cifsSesInfo *ses,
597             struct smb_hdr *in_buf, struct smb_hdr *out_buf,
598             int *pbytes_returned, const int long_op)
599 {
600         int rc = 0;
601         unsigned int receive_len;
602         unsigned long timeout;
603         struct mid_q_entry *midQ;
604
605         if (ses == NULL) {
606                 cERROR(1, ("Null smb session"));
607                 return -EIO;
608         }
609         if (ses->server == NULL) {
610                 cERROR(1, ("Null tcp session"));
611                 return -EIO;
612         }
613
614         if (ses->server->tcpStatus == CifsExiting)
615                 return -ENOENT;
616
617         /* Ensure that we do not send more than 50 overlapping requests
618            to the same server. We may make this configurable later or
619            use ses->maxReq */
620
621         rc = wait_for_free_request(ses, long_op);
622         if (rc)
623                 return rc;
624
625         /* make sure that we sign in the same order that we send on this socket
626            and avoid races inside tcp sendmsg code that could cause corruption
627            of smb data */
628
629         down(&ses->server->tcpSem);
630
631         rc = allocate_mid(ses, in_buf, &midQ);
632         if (rc) {
633                 up(&ses->server->tcpSem);
634                 /* Update # of requests on wire to server */
635                 atomic_dec(&ses->server->inFlight);
636                 wake_up(&ses->server->request_q);
637                 return rc;
638         }
639
640         if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
641                 cERROR(1, ("Illegal length, greater than maximum frame, %d",
642                         in_buf->smb_buf_length));
643                 DeleteMidQEntry(midQ);
644                 up(&ses->server->tcpSem);
645                 /* Update # of requests on wire to server */
646                 atomic_dec(&ses->server->inFlight);
647                 wake_up(&ses->server->request_q);
648                 return -EIO;
649         }
650
651         rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
652
653         midQ->midState = MID_REQUEST_SUBMITTED;
654 #ifdef CONFIG_CIFS_STATS2
655         atomic_inc(&ses->server->inSend);
656 #endif
657         rc = smb_send(ses->server->ssocket, in_buf, in_buf->smb_buf_length,
658                       (struct sockaddr *) &(ses->server->addr.sockAddr));
659 #ifdef CONFIG_CIFS_STATS2
660         atomic_dec(&ses->server->inSend);
661         midQ->when_sent = jiffies;
662 #endif
663         up(&ses->server->tcpSem);
664
665         if (rc < 0)
666                 goto out;
667
668         if (long_op == -1)
669                 goto out;
670         else if (long_op == 2) /* writes past end of file can take loong time */
671                 timeout = 180 * HZ;
672         else if (long_op == 1)
673                 timeout = 45 * HZ; /* should be greater than
674                         servers oplock break timeout (about 43 seconds) */
675         else
676                 timeout = 15 * HZ;
677         /* wait for 15 seconds or until woken up due to response arriving or
678            due to last connection to this server being unmounted */
679         if (signal_pending(current)) {
680                 /* if signal pending do not hold up user for full smb timeout
681                 but we still give response a chance to complete */
682                 timeout = 2 * HZ;
683         }
684
685         /* No user interrupts in wait - wreaks havoc with performance */
686         wait_for_response(ses, midQ, timeout, 10 * HZ);
687
688         spin_lock(&GlobalMid_Lock);
689         if (midQ->resp_buf) {
690                 spin_unlock(&GlobalMid_Lock);
691                 receive_len = midQ->resp_buf->smb_buf_length;
692         } else {
693                 cERROR(1, ("No response for cmd %d mid %d",
694                           midQ->command, midQ->mid));
695                 if (midQ->midState == MID_REQUEST_SUBMITTED) {
696                         if (ses->server->tcpStatus == CifsExiting)
697                                 rc = -EHOSTDOWN;
698                         else {
699                                 ses->server->tcpStatus = CifsNeedReconnect;
700                                 midQ->midState = MID_RETRY_NEEDED;
701                         }
702                 }
703
704                 if (rc != -EHOSTDOWN) {
705                         if (midQ->midState == MID_RETRY_NEEDED) {
706                                 rc = -EAGAIN;
707                                 cFYI(1, ("marking request for retry"));
708                         } else {
709                                 rc = -EIO;
710                         }
711                 }
712                 spin_unlock(&GlobalMid_Lock);
713                 DeleteMidQEntry(midQ);
714                 /* Update # of requests on wire to server */
715                 atomic_dec(&ses->server->inFlight);
716                 wake_up(&ses->server->request_q);
717                 return rc;
718         }
719
720         if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
721                 cERROR(1, ("Frame too large received.  Length: %d  Xid: %d",
722                         receive_len, xid));
723                 rc = -EIO;
724         } else {                /* rcvd frame is ok */
725
726                 if (midQ->resp_buf && out_buf
727                     && (midQ->midState == MID_RESPONSE_RECEIVED)) {
728                         out_buf->smb_buf_length = receive_len;
729                         memcpy((char *)out_buf + 4,
730                                (char *)midQ->resp_buf + 4,
731                                receive_len);
732
733                         dump_smb(out_buf, 92);
734                         /* convert the length into a more usable form */
735                         if ((receive_len > 24) &&
736                            (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
737                                         SECMODE_SIGN_ENABLED))) {
738                                 rc = cifs_verify_signature(out_buf,
739                                                 &ses->server->mac_signing_key,
740                                                 midQ->sequence_number+1);
741                                 if (rc) {
742                                         cERROR(1, ("Unexpected SMB signature"));
743                                         /* BB FIXME add code to kill session */
744                                 }
745                         }
746
747                         *pbytes_returned = out_buf->smb_buf_length;
748
749                         /* BB special case reconnect tid and uid here? */
750                         rc = map_smb_to_linux_error(out_buf);
751
752                         /* convert ByteCount if necessary */
753                         if (receive_len >= sizeof(struct smb_hdr) - 4
754                             /* do not count RFC1001 header */  +
755                             (2 * out_buf->WordCount) + 2 /* bcc */ )
756                                 BCC(out_buf) = le16_to_cpu(BCC_LE(out_buf));
757                 } else {
758                         rc = -EIO;
759                         cERROR(1, ("Bad MID state?"));
760                 }
761         }
762
763 out:
764         DeleteMidQEntry(midQ);
765         atomic_dec(&ses->server->inFlight);
766         wake_up(&ses->server->request_q);
767
768         return rc;
769 }
770
771 /* Send an NT_CANCEL SMB to cause the POSIX blocking lock to return. */
772
773 static int
774 send_nt_cancel(struct cifsTconInfo *tcon, struct smb_hdr *in_buf,
775                 struct mid_q_entry *midQ)
776 {
777         int rc = 0;
778         struct cifsSesInfo *ses = tcon->ses;
779         __u16 mid = in_buf->Mid;
780
781         header_assemble(in_buf, SMB_COM_NT_CANCEL, tcon, 0);
782         in_buf->Mid = mid;
783         down(&ses->server->tcpSem);
784         rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
785         if (rc) {
786                 up(&ses->server->tcpSem);
787                 return rc;
788         }
789         rc = smb_send(ses->server->ssocket, in_buf, in_buf->smb_buf_length,
790               (struct sockaddr *) &(ses->server->addr.sockAddr));
791         up(&ses->server->tcpSem);
792         return rc;
793 }
794
795 /* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
796    blocking lock to return. */
797
798 static int
799 send_lock_cancel(const unsigned int xid, struct cifsTconInfo *tcon,
800                         struct smb_hdr *in_buf,
801                         struct smb_hdr *out_buf)
802 {
803         int bytes_returned;
804         struct cifsSesInfo *ses = tcon->ses;
805         LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
806
807         /* We just modify the current in_buf to change
808            the type of lock from LOCKING_ANDX_SHARED_LOCK
809            or LOCKING_ANDX_EXCLUSIVE_LOCK to
810            LOCKING_ANDX_CANCEL_LOCK. */
811
812         pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
813         pSMB->Timeout = 0;
814         pSMB->hdr.Mid = GetNextMid(ses->server);
815
816         return SendReceive(xid, ses, in_buf, out_buf,
817                         &bytes_returned, 0);
818 }
819
820 int
821 SendReceiveBlockingLock(const unsigned int xid, struct cifsTconInfo *tcon,
822             struct smb_hdr *in_buf, struct smb_hdr *out_buf,
823             int *pbytes_returned)
824 {
825         int rc = 0;
826         int rstart = 0;
827         unsigned int receive_len;
828         struct mid_q_entry *midQ;
829         struct cifsSesInfo *ses;
830
831         if (tcon == NULL || tcon->ses == NULL) {
832                 cERROR(1, ("Null smb session"));
833                 return -EIO;
834         }
835         ses = tcon->ses;
836
837         if (ses->server == NULL) {
838                 cERROR(1, ("Null tcp session"));
839                 return -EIO;
840         }
841
842         if (ses->server->tcpStatus == CifsExiting)
843                 return -ENOENT;
844
845         /* Ensure that we do not send more than 50 overlapping requests
846            to the same server. We may make this configurable later or
847            use ses->maxReq */
848
849         rc = wait_for_free_request(ses, 3);
850         if (rc)
851                 return rc;
852
853         /* make sure that we sign in the same order that we send on this socket
854            and avoid races inside tcp sendmsg code that could cause corruption
855            of smb data */
856
857         down(&ses->server->tcpSem);
858
859         rc = allocate_mid(ses, in_buf, &midQ);
860         if (rc) {
861                 up(&ses->server->tcpSem);
862                 return rc;
863         }
864
865         if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
866                 up(&ses->server->tcpSem);
867                 cERROR(1, ("Illegal length, greater than maximum frame, %d",
868                         in_buf->smb_buf_length));
869                 DeleteMidQEntry(midQ);
870                 return -EIO;
871         }
872
873         rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
874
875         midQ->midState = MID_REQUEST_SUBMITTED;
876 #ifdef CONFIG_CIFS_STATS2
877         atomic_inc(&ses->server->inSend);
878 #endif
879         rc = smb_send(ses->server->ssocket, in_buf, in_buf->smb_buf_length,
880                       (struct sockaddr *) &(ses->server->addr.sockAddr));
881 #ifdef CONFIG_CIFS_STATS2
882         atomic_dec(&ses->server->inSend);
883         midQ->when_sent = jiffies;
884 #endif
885         up(&ses->server->tcpSem);
886
887         if (rc < 0) {
888                 DeleteMidQEntry(midQ);
889                 return rc;
890         }
891
892         /* Wait for a reply - allow signals to interrupt. */
893         rc = wait_event_interruptible(ses->server->response_q,
894                 (!(midQ->midState == MID_REQUEST_SUBMITTED)) ||
895                 ((ses->server->tcpStatus != CifsGood) &&
896                  (ses->server->tcpStatus != CifsNew)));
897
898         /* Were we interrupted by a signal ? */
899         if ((rc == -ERESTARTSYS) &&
900                 (midQ->midState == MID_REQUEST_SUBMITTED) &&
901                 ((ses->server->tcpStatus == CifsGood) ||
902                  (ses->server->tcpStatus == CifsNew))) {
903
904                 if (in_buf->Command == SMB_COM_TRANSACTION2) {
905                         /* POSIX lock. We send a NT_CANCEL SMB to cause the
906                            blocking lock to return. */
907
908                         rc = send_nt_cancel(tcon, in_buf, midQ);
909                         if (rc) {
910                                 DeleteMidQEntry(midQ);
911                                 return rc;
912                         }
913                 } else {
914                         /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
915                            to cause the blocking lock to return. */
916
917                         rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
918
919                         /* If we get -ENOLCK back the lock may have
920                            already been removed. Don't exit in this case. */
921                         if (rc && rc != -ENOLCK) {
922                                 DeleteMidQEntry(midQ);
923                                 return rc;
924                         }
925                 }
926
927                 /* Wait 5 seconds for the response. */
928                 if (wait_for_response(ses, midQ, 5 * HZ, 5 * HZ) == 0) {
929                         /* We got the response - restart system call. */
930                         rstart = 1;
931                 }
932         }
933
934         spin_lock(&GlobalMid_Lock);
935         if (midQ->resp_buf) {
936                 spin_unlock(&GlobalMid_Lock);
937                 receive_len = midQ->resp_buf->smb_buf_length;
938         } else {
939                 cERROR(1, ("No response for cmd %d mid %d",
940                           midQ->command, midQ->mid));
941                 if (midQ->midState == MID_REQUEST_SUBMITTED) {
942                         if (ses->server->tcpStatus == CifsExiting)
943                                 rc = -EHOSTDOWN;
944                         else {
945                                 ses->server->tcpStatus = CifsNeedReconnect;
946                                 midQ->midState = MID_RETRY_NEEDED;
947                         }
948                 }
949
950                 if (rc != -EHOSTDOWN) {
951                         if (midQ->midState == MID_RETRY_NEEDED) {
952                                 rc = -EAGAIN;
953                                 cFYI(1, ("marking request for retry"));
954                         } else {
955                                 rc = -EIO;
956                         }
957                 }
958                 spin_unlock(&GlobalMid_Lock);
959                 DeleteMidQEntry(midQ);
960                 return rc;
961         }
962
963         if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
964                 cERROR(1, ("Frame too large received.  Length: %d  Xid: %d",
965                         receive_len, xid));
966                 rc = -EIO;
967         } else {                /* rcvd frame is ok */
968
969                 if (midQ->resp_buf && out_buf
970                     && (midQ->midState == MID_RESPONSE_RECEIVED)) {
971                         out_buf->smb_buf_length = receive_len;
972                         memcpy((char *)out_buf + 4,
973                                (char *)midQ->resp_buf + 4,
974                                receive_len);
975
976                         dump_smb(out_buf, 92);
977                         /* convert the length into a more usable form */
978                         if ((receive_len > 24) &&
979                            (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
980                                         SECMODE_SIGN_ENABLED))) {
981                                 rc = cifs_verify_signature(out_buf,
982                                                 &ses->server->mac_signing_key,
983                                                 midQ->sequence_number+1);
984                                 if (rc) {
985                                         cERROR(1, ("Unexpected SMB signature"));
986                                         /* BB FIXME add code to kill session */
987                                 }
988                         }
989
990                         *pbytes_returned = out_buf->smb_buf_length;
991
992                         /* BB special case reconnect tid and uid here? */
993                         rc = map_smb_to_linux_error(out_buf);
994
995                         /* convert ByteCount if necessary */
996                         if (receive_len >= sizeof(struct smb_hdr) - 4
997                             /* do not count RFC1001 header */  +
998                             (2 * out_buf->WordCount) + 2 /* bcc */ )
999                                 BCC(out_buf) = le16_to_cpu(BCC_LE(out_buf));
1000                 } else {
1001                         rc = -EIO;
1002                         cERROR(1, ("Bad MID state?"));
1003                 }
1004         }
1005         DeleteMidQEntry(midQ);
1006         if (rstart && rc == -EACCES)
1007                 return -ERESTARTSYS;
1008         return rc;
1009 }