transport.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. /*
  2. * fs/cifs/transport.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2002,2005
  5. * Author(s): Steve French (sfrench@us.ibm.com)
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published
  9. * by the Free Software Foundation; either version 2.1 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  15. * the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/fs.h>
  22. #include <linux/list.h>
  23. #include <linux/wait.h>
  24. #include <linux/net.h>
  25. #include <linux/delay.h>
  26. #include <asm/uaccess.h>
  27. #include <asm/processor.h>
  28. #include <linux/mempool.h>
  29. #include "cifspdu.h"
  30. #include "cifsglob.h"
  31. #include "cifsproto.h"
  32. #include "cifs_debug.h"
  33. extern mempool_t *cifs_mid_poolp;
  34. extern kmem_cache_t *cifs_oplock_cachep;
  35. static struct mid_q_entry *
  36. AllocMidQEntry(struct smb_hdr *smb_buffer, struct cifsSesInfo *ses)
  37. {
  38. struct mid_q_entry *temp;
  39. if (ses == NULL) {
  40. cERROR(1, ("Null session passed in to AllocMidQEntry"));
  41. return NULL;
  42. }
  43. if (ses->server == NULL) {
  44. cERROR(1, ("Null TCP session in AllocMidQEntry"));
  45. return NULL;
  46. }
  47. temp = (struct mid_q_entry *) mempool_alloc(cifs_mid_poolp,
  48. SLAB_KERNEL | SLAB_NOFS);
  49. if (temp == NULL)
  50. return temp;
  51. else {
  52. memset(temp, 0, sizeof (struct mid_q_entry));
  53. temp->mid = smb_buffer->Mid; /* always LE */
  54. temp->pid = current->pid;
  55. temp->command = smb_buffer->Command;
  56. cFYI(1, ("For smb_command %d", temp->command));
  57. do_gettimeofday(&temp->when_sent);
  58. temp->ses = ses;
  59. temp->tsk = current;
  60. }
  61. spin_lock(&GlobalMid_Lock);
  62. list_add_tail(&temp->qhead, &ses->server->pending_mid_q);
  63. atomic_inc(&midCount);
  64. temp->midState = MID_REQUEST_ALLOCATED;
  65. spin_unlock(&GlobalMid_Lock);
  66. return temp;
  67. }
  68. static void
  69. DeleteMidQEntry(struct mid_q_entry *midEntry)
  70. {
  71. spin_lock(&GlobalMid_Lock);
  72. midEntry->midState = MID_FREE;
  73. list_del(&midEntry->qhead);
  74. atomic_dec(&midCount);
  75. spin_unlock(&GlobalMid_Lock);
  76. if(midEntry->largeBuf)
  77. cifs_buf_release(midEntry->resp_buf);
  78. else
  79. cifs_small_buf_release(midEntry->resp_buf);
  80. mempool_free(midEntry, cifs_mid_poolp);
  81. }
  82. struct oplock_q_entry *
  83. AllocOplockQEntry(struct inode * pinode, __u16 fid, struct cifsTconInfo * tcon)
  84. {
  85. struct oplock_q_entry *temp;
  86. if ((pinode== NULL) || (tcon == NULL)) {
  87. cERROR(1, ("Null parms passed to AllocOplockQEntry"));
  88. return NULL;
  89. }
  90. temp = (struct oplock_q_entry *) kmem_cache_alloc(cifs_oplock_cachep,
  91. SLAB_KERNEL);
  92. if (temp == NULL)
  93. return temp;
  94. else {
  95. temp->pinode = pinode;
  96. temp->tcon = tcon;
  97. temp->netfid = fid;
  98. spin_lock(&GlobalMid_Lock);
  99. list_add_tail(&temp->qhead, &GlobalOplock_Q);
  100. spin_unlock(&GlobalMid_Lock);
  101. }
  102. return temp;
  103. }
  104. void DeleteOplockQEntry(struct oplock_q_entry * oplockEntry)
  105. {
  106. spin_lock(&GlobalMid_Lock);
  107. /* should we check if list empty first? */
  108. list_del(&oplockEntry->qhead);
  109. spin_unlock(&GlobalMid_Lock);
  110. kmem_cache_free(cifs_oplock_cachep, oplockEntry);
  111. }
  112. int
  113. smb_send(struct socket *ssocket, struct smb_hdr *smb_buffer,
  114. unsigned int smb_buf_length, struct sockaddr *sin)
  115. {
  116. int rc = 0;
  117. int i = 0;
  118. struct msghdr smb_msg;
  119. struct kvec iov;
  120. unsigned len = smb_buf_length + 4;
  121. if(ssocket == NULL)
  122. return -ENOTSOCK; /* BB eventually add reconnect code here */
  123. iov.iov_base = smb_buffer;
  124. iov.iov_len = len;
  125. smb_msg.msg_name = sin;
  126. smb_msg.msg_namelen = sizeof (struct sockaddr);
  127. smb_msg.msg_control = NULL;
  128. smb_msg.msg_controllen = 0;
  129. smb_msg.msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL; /* BB add more flags?*/
  130. /* smb header is converted in header_assemble. bcc and rest of SMB word
  131. area, and byte area if necessary, is converted to littleendian in
  132. cifssmb.c and RFC1001 len is converted to bigendian in smb_send
  133. Flags2 is converted in SendReceive */
  134. smb_buffer->smb_buf_length = cpu_to_be32(smb_buffer->smb_buf_length);
  135. cFYI(1, ("Sending smb of length %d ", smb_buf_length));
  136. dump_smb(smb_buffer, len);
  137. while (len > 0) {
  138. rc = kernel_sendmsg(ssocket, &smb_msg, &iov, 1, len);
  139. if ((rc == -ENOSPC) || (rc == -EAGAIN)) {
  140. i++;
  141. if(i > 60) {
  142. cERROR(1,
  143. ("sends on sock %p stuck for 30 seconds",
  144. ssocket));
  145. rc = -EAGAIN;
  146. break;
  147. }
  148. msleep(500);
  149. continue;
  150. }
  151. if (rc < 0)
  152. break;
  153. iov.iov_base += rc;
  154. iov.iov_len -= rc;
  155. len -= rc;
  156. }
  157. if (rc < 0) {
  158. cERROR(1,("Error %d sending data on socket to server.", rc));
  159. } else {
  160. rc = 0;
  161. }
  162. return rc;
  163. }
  164. #ifdef CONFIG_CIFS_EXPERIMENTAL
  165. static int
  166. smb_send2(struct socket *ssocket, struct smb_hdr *smb_buffer,
  167. unsigned int smb_hdr_length, const char * data, unsigned int datalen,
  168. struct sockaddr *sin)
  169. {
  170. int rc = 0;
  171. int i = 0;
  172. struct msghdr smb_msg;
  173. struct kvec iov[2];
  174. unsigned len = smb_hdr_length + 4;
  175. if(ssocket == NULL)
  176. return -ENOTSOCK; /* BB eventually add reconnect code here */
  177. iov[0].iov_base = smb_buffer;
  178. iov[0].iov_len = len;
  179. iov[1].iov_base = data;
  180. iov[1].iov_len = datalen;
  181. smb_msg.msg_name = sin;
  182. smb_msg.msg_namelen = sizeof (struct sockaddr);
  183. smb_msg.msg_control = NULL;
  184. smb_msg.msg_controllen = 0;
  185. smb_msg.msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL; /* BB add more flags?*/
  186. /* smb header is converted in header_assemble. bcc and rest of SMB word
  187. area, and byte area if necessary, is converted to littleendian in
  188. cifssmb.c and RFC1001 len is converted to bigendian in smb_send
  189. Flags2 is converted in SendReceive */
  190. smb_buffer->smb_buf_length = cpu_to_be32(smb_buffer->smb_buf_length);
  191. cFYI(1, ("Sending smb: hdrlen %d datalen %d",
  192. smb_hdr_length,datalen));
  193. dump_smb(smb_buffer, len);
  194. while (len + datalen > 0) {
  195. rc = kernel_sendmsg(ssocket, &smb_msg, iov, 2, len);
  196. if ((rc == -ENOSPC) || (rc == -EAGAIN)) {
  197. i++;
  198. if(i > 60) {
  199. cERROR(1,
  200. ("sends on sock %p stuck for 30 seconds",
  201. ssocket));
  202. rc = -EAGAIN;
  203. break;
  204. }
  205. msleep(500);
  206. continue;
  207. }
  208. if (rc < 0)
  209. break;
  210. if(iov[0].iov_len > 0) {
  211. if(rc >= len) {
  212. iov[0].iov_len = 0;
  213. rc -= len;
  214. len = 0;
  215. } else { /* some of hdr was not sent */
  216. len -= rc;
  217. iov[0].iov_len -= rc;
  218. iov[0].iov_base += rc;
  219. continue;
  220. }
  221. }
  222. if((iov[0].iov_len == 0) && (rc > 0)){
  223. iov[1].iov_base += rc;
  224. iov[1].iov_len -= rc;
  225. datalen -= rc;
  226. }
  227. }
  228. if (rc < 0) {
  229. cERROR(1,("Error %d sending data on socket to server.", rc));
  230. } else {
  231. rc = 0;
  232. }
  233. return rc;
  234. }
  235. int
  236. SendReceive2(const unsigned int xid, struct cifsSesInfo *ses,
  237. struct smb_hdr *in_buf, int hdrlen, const char * data,
  238. int datalen, int *pbytes_returned, const int long_op)
  239. {
  240. int rc = 0;
  241. unsigned int receive_len;
  242. unsigned long timeout;
  243. struct mid_q_entry *midQ;
  244. if (ses == NULL) {
  245. cERROR(1,("Null smb session"));
  246. return -EIO;
  247. }
  248. if(ses->server == NULL) {
  249. cERROR(1,("Null tcp session"));
  250. return -EIO;
  251. }
  252. if(ses->server->tcpStatus == CifsExiting)
  253. return -ENOENT;
  254. /* Ensure that we do not send more than 50 overlapping requests
  255. to the same server. We may make this configurable later or
  256. use ses->maxReq */
  257. if(long_op == -1) {
  258. /* oplock breaks must not be held up */
  259. atomic_inc(&ses->server->inFlight);
  260. } else {
  261. spin_lock(&GlobalMid_Lock);
  262. while(1) {
  263. if(atomic_read(&ses->server->inFlight) >=
  264. cifs_max_pending){
  265. spin_unlock(&GlobalMid_Lock);
  266. wait_event(ses->server->request_q,
  267. atomic_read(&ses->server->inFlight)
  268. < cifs_max_pending);
  269. spin_lock(&GlobalMid_Lock);
  270. } else {
  271. if(ses->server->tcpStatus == CifsExiting) {
  272. spin_unlock(&GlobalMid_Lock);
  273. return -ENOENT;
  274. }
  275. /* can not count locking commands against total since
  276. they are allowed to block on server */
  277. if(long_op < 3) {
  278. /* update # of requests on the wire to server */
  279. atomic_inc(&ses->server->inFlight);
  280. }
  281. spin_unlock(&GlobalMid_Lock);
  282. break;
  283. }
  284. }
  285. }
  286. /* make sure that we sign in the same order that we send on this socket
  287. and avoid races inside tcp sendmsg code that could cause corruption
  288. of smb data */
  289. down(&ses->server->tcpSem);
  290. if (ses->server->tcpStatus == CifsExiting) {
  291. rc = -ENOENT;
  292. goto out_unlock2;
  293. } else if (ses->server->tcpStatus == CifsNeedReconnect) {
  294. cFYI(1,("tcp session dead - return to caller to retry"));
  295. rc = -EAGAIN;
  296. goto out_unlock2;
  297. } else if (ses->status != CifsGood) {
  298. /* check if SMB session is bad because we are setting it up */
  299. if((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
  300. (in_buf->Command != SMB_COM_NEGOTIATE)) {
  301. rc = -EAGAIN;
  302. goto out_unlock2;
  303. } /* else ok - we are setting up session */
  304. }
  305. midQ = AllocMidQEntry(in_buf, ses);
  306. if (midQ == NULL) {
  307. up(&ses->server->tcpSem);
  308. /* If not lock req, update # of requests on wire to server */
  309. if(long_op < 3) {
  310. atomic_dec(&ses->server->inFlight);
  311. wake_up(&ses->server->request_q);
  312. }
  313. return -ENOMEM;
  314. }
  315. if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
  316. up(&ses->server->tcpSem);
  317. cERROR(1,
  318. ("Illegal length, greater than maximum frame, %d ",
  319. in_buf->smb_buf_length));
  320. DeleteMidQEntry(midQ);
  321. /* If not lock req, update # of requests on wire to server */
  322. if(long_op < 3) {
  323. atomic_dec(&ses->server->inFlight);
  324. wake_up(&ses->server->request_q);
  325. }
  326. return -EIO;
  327. }
  328. /* BB FIXME */
  329. /* rc = cifs_sign_smb2(in_buf, data, ses->server, &midQ->sequence_number); */
  330. midQ->midState = MID_REQUEST_SUBMITTED;
  331. rc = smb_send2(ses->server->ssocket, in_buf, hdrlen, data, datalen,
  332. (struct sockaddr *) &(ses->server->addr.sockAddr));
  333. if(rc < 0) {
  334. DeleteMidQEntry(midQ);
  335. up(&ses->server->tcpSem);
  336. /* If not lock req, update # of requests on wire to server */
  337. if(long_op < 3) {
  338. atomic_dec(&ses->server->inFlight);
  339. wake_up(&ses->server->request_q);
  340. }
  341. return rc;
  342. } else
  343. up(&ses->server->tcpSem);
  344. if (long_op == -1)
  345. goto cifs_no_response_exit2;
  346. else if (long_op == 2) /* writes past end of file can take loong time */
  347. timeout = 300 * HZ;
  348. else if (long_op == 1)
  349. timeout = 45 * HZ; /* should be greater than
  350. servers oplock break timeout (about 43 seconds) */
  351. else if (long_op > 2) {
  352. timeout = MAX_SCHEDULE_TIMEOUT;
  353. } else
  354. timeout = 15 * HZ;
  355. /* wait for 15 seconds or until woken up due to response arriving or
  356. due to last connection to this server being unmounted */
  357. if (signal_pending(current)) {
  358. /* if signal pending do not hold up user for full smb timeout
  359. but we still give response a change to complete */
  360. timeout = 2 * HZ;
  361. }
  362. /* No user interrupts in wait - wreaks havoc with performance */
  363. if(timeout != MAX_SCHEDULE_TIMEOUT) {
  364. timeout += jiffies;
  365. wait_event(ses->server->response_q,
  366. (!(midQ->midState & MID_REQUEST_SUBMITTED)) ||
  367. time_after(jiffies, timeout) ||
  368. ((ses->server->tcpStatus != CifsGood) &&
  369. (ses->server->tcpStatus != CifsNew)));
  370. } else {
  371. wait_event(ses->server->response_q,
  372. (!(midQ->midState & MID_REQUEST_SUBMITTED)) ||
  373. ((ses->server->tcpStatus != CifsGood) &&
  374. (ses->server->tcpStatus != CifsNew)));
  375. }
  376. spin_lock(&GlobalMid_Lock);
  377. if (midQ->resp_buf) {
  378. spin_unlock(&GlobalMid_Lock);
  379. receive_len = be32_to_cpu(*(__be32 *)midQ->resp_buf);
  380. } else {
  381. cERROR(1,("No response buffer"));
  382. if(midQ->midState == MID_REQUEST_SUBMITTED) {
  383. if(ses->server->tcpStatus == CifsExiting)
  384. rc = -EHOSTDOWN;
  385. else {
  386. ses->server->tcpStatus = CifsNeedReconnect;
  387. midQ->midState = MID_RETRY_NEEDED;
  388. }
  389. }
  390. if (rc != -EHOSTDOWN) {
  391. if(midQ->midState == MID_RETRY_NEEDED) {
  392. rc = -EAGAIN;
  393. cFYI(1,("marking request for retry"));
  394. } else {
  395. rc = -EIO;
  396. }
  397. }
  398. spin_unlock(&GlobalMid_Lock);
  399. DeleteMidQEntry(midQ);
  400. /* If not lock req, update # of requests on wire to server */
  401. if(long_op < 3) {
  402. atomic_dec(&ses->server->inFlight);
  403. wake_up(&ses->server->request_q);
  404. }
  405. return rc;
  406. }
  407. if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
  408. cERROR(1, ("Frame too large received. Length: %d Xid: %d",
  409. receive_len, xid));
  410. rc = -EIO;
  411. } else { /* rcvd frame is ok */
  412. if (midQ->resp_buf &&
  413. (midQ->midState == MID_RESPONSE_RECEIVED)) {
  414. in_buf->smb_buf_length = receive_len;
  415. /* BB verify that length would not overrun small buf */
  416. memcpy((char *)in_buf + 4,
  417. (char *)midQ->resp_buf + 4,
  418. receive_len);
  419. dump_smb(in_buf, 80);
  420. /* convert the length into a more usable form */
  421. if((receive_len > 24) &&
  422. (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
  423. SECMODE_SIGN_ENABLED))) {
  424. rc = cifs_verify_signature(in_buf,
  425. ses->server->mac_signing_key,
  426. midQ->sequence_number+1);
  427. if(rc) {
  428. cERROR(1,("Unexpected SMB signature"));
  429. /* BB FIXME add code to kill session */
  430. }
  431. }
  432. *pbytes_returned = in_buf->smb_buf_length;
  433. /* BB special case reconnect tid and uid here? */
  434. rc = map_smb_to_linux_error(in_buf);
  435. /* convert ByteCount if necessary */
  436. if (receive_len >=
  437. sizeof (struct smb_hdr) -
  438. 4 /* do not count RFC1001 header */ +
  439. (2 * in_buf->WordCount) + 2 /* bcc */ )
  440. BCC(in_buf) = le16_to_cpu(BCC(in_buf));
  441. } else {
  442. rc = -EIO;
  443. cFYI(1,("Bad MID state? "));
  444. }
  445. }
  446. cifs_no_response_exit2:
  447. DeleteMidQEntry(midQ);
  448. if(long_op < 3) {
  449. atomic_dec(&ses->server->inFlight);
  450. wake_up(&ses->server->request_q);
  451. }
  452. return rc;
  453. out_unlock2:
  454. up(&ses->server->tcpSem);
  455. /* If not lock req, update # of requests on wire to server */
  456. if(long_op < 3) {
  457. atomic_dec(&ses->server->inFlight);
  458. wake_up(&ses->server->request_q);
  459. }
  460. return rc;
  461. }
  462. #endif /* CIFS_EXPERIMENTAL */
  463. int
  464. SendReceive(const unsigned int xid, struct cifsSesInfo *ses,
  465. struct smb_hdr *in_buf, struct smb_hdr *out_buf,
  466. int *pbytes_returned, const int long_op)
  467. {
  468. int rc = 0;
  469. unsigned int receive_len;
  470. unsigned long timeout;
  471. struct mid_q_entry *midQ;
  472. if (ses == NULL) {
  473. cERROR(1,("Null smb session"));
  474. return -EIO;
  475. }
  476. if(ses->server == NULL) {
  477. cERROR(1,("Null tcp session"));
  478. return -EIO;
  479. }
  480. if(ses->server->tcpStatus == CifsExiting)
  481. return -ENOENT;
  482. /* Ensure that we do not send more than 50 overlapping requests
  483. to the same server. We may make this configurable later or
  484. use ses->maxReq */
  485. if(long_op == -1) {
  486. /* oplock breaks must not be held up */
  487. atomic_inc(&ses->server->inFlight);
  488. } else {
  489. spin_lock(&GlobalMid_Lock);
  490. while(1) {
  491. if(atomic_read(&ses->server->inFlight) >=
  492. cifs_max_pending){
  493. spin_unlock(&GlobalMid_Lock);
  494. wait_event(ses->server->request_q,
  495. atomic_read(&ses->server->inFlight)
  496. < cifs_max_pending);
  497. spin_lock(&GlobalMid_Lock);
  498. } else {
  499. if(ses->server->tcpStatus == CifsExiting) {
  500. spin_unlock(&GlobalMid_Lock);
  501. return -ENOENT;
  502. }
  503. /* can not count locking commands against total since
  504. they are allowed to block on server */
  505. if(long_op < 3) {
  506. /* update # of requests on the wire to server */
  507. atomic_inc(&ses->server->inFlight);
  508. }
  509. spin_unlock(&GlobalMid_Lock);
  510. break;
  511. }
  512. }
  513. }
  514. /* make sure that we sign in the same order that we send on this socket
  515. and avoid races inside tcp sendmsg code that could cause corruption
  516. of smb data */
  517. down(&ses->server->tcpSem);
  518. if (ses->server->tcpStatus == CifsExiting) {
  519. rc = -ENOENT;
  520. goto out_unlock;
  521. } else if (ses->server->tcpStatus == CifsNeedReconnect) {
  522. cFYI(1,("tcp session dead - return to caller to retry"));
  523. rc = -EAGAIN;
  524. goto out_unlock;
  525. } else if (ses->status != CifsGood) {
  526. /* check if SMB session is bad because we are setting it up */
  527. if((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
  528. (in_buf->Command != SMB_COM_NEGOTIATE)) {
  529. rc = -EAGAIN;
  530. goto out_unlock;
  531. } /* else ok - we are setting up session */
  532. }
  533. midQ = AllocMidQEntry(in_buf, ses);
  534. if (midQ == NULL) {
  535. up(&ses->server->tcpSem);
  536. /* If not lock req, update # of requests on wire to server */
  537. if(long_op < 3) {
  538. atomic_dec(&ses->server->inFlight);
  539. wake_up(&ses->server->request_q);
  540. }
  541. return -ENOMEM;
  542. }
  543. if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
  544. up(&ses->server->tcpSem);
  545. cERROR(1,
  546. ("Illegal length, greater than maximum frame, %d ",
  547. in_buf->smb_buf_length));
  548. DeleteMidQEntry(midQ);
  549. /* If not lock req, update # of requests on wire to server */
  550. if(long_op < 3) {
  551. atomic_dec(&ses->server->inFlight);
  552. wake_up(&ses->server->request_q);
  553. }
  554. return -EIO;
  555. }
  556. rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
  557. midQ->midState = MID_REQUEST_SUBMITTED;
  558. rc = smb_send(ses->server->ssocket, in_buf, in_buf->smb_buf_length,
  559. (struct sockaddr *) &(ses->server->addr.sockAddr));
  560. if(rc < 0) {
  561. DeleteMidQEntry(midQ);
  562. up(&ses->server->tcpSem);
  563. /* If not lock req, update # of requests on wire to server */
  564. if(long_op < 3) {
  565. atomic_dec(&ses->server->inFlight);
  566. wake_up(&ses->server->request_q);
  567. }
  568. return rc;
  569. } else
  570. up(&ses->server->tcpSem);
  571. if (long_op == -1)
  572. goto cifs_no_response_exit;
  573. else if (long_op == 2) /* writes past end of file can take loong time */
  574. timeout = 300 * HZ;
  575. else if (long_op == 1)
  576. timeout = 45 * HZ; /* should be greater than
  577. servers oplock break timeout (about 43 seconds) */
  578. else if (long_op > 2) {
  579. timeout = MAX_SCHEDULE_TIMEOUT;
  580. } else
  581. timeout = 15 * HZ;
  582. /* wait for 15 seconds or until woken up due to response arriving or
  583. due to last connection to this server being unmounted */
  584. if (signal_pending(current)) {
  585. /* if signal pending do not hold up user for full smb timeout
  586. but we still give response a change to complete */
  587. timeout = 2 * HZ;
  588. }
  589. /* No user interrupts in wait - wreaks havoc with performance */
  590. if(timeout != MAX_SCHEDULE_TIMEOUT) {
  591. timeout += jiffies;
  592. wait_event(ses->server->response_q,
  593. (!(midQ->midState & MID_REQUEST_SUBMITTED)) ||
  594. time_after(jiffies, timeout) ||
  595. ((ses->server->tcpStatus != CifsGood) &&
  596. (ses->server->tcpStatus != CifsNew)));
  597. } else {
  598. wait_event(ses->server->response_q,
  599. (!(midQ->midState & MID_REQUEST_SUBMITTED)) ||
  600. ((ses->server->tcpStatus != CifsGood) &&
  601. (ses->server->tcpStatus != CifsNew)));
  602. }
  603. spin_lock(&GlobalMid_Lock);
  604. if (midQ->resp_buf) {
  605. spin_unlock(&GlobalMid_Lock);
  606. receive_len = be32_to_cpu(*(__be32 *)midQ->resp_buf);
  607. } else {
  608. cERROR(1,("No response buffer"));
  609. if(midQ->midState == MID_REQUEST_SUBMITTED) {
  610. if(ses->server->tcpStatus == CifsExiting)
  611. rc = -EHOSTDOWN;
  612. else {
  613. ses->server->tcpStatus = CifsNeedReconnect;
  614. midQ->midState = MID_RETRY_NEEDED;
  615. }
  616. }
  617. if (rc != -EHOSTDOWN) {
  618. if(midQ->midState == MID_RETRY_NEEDED) {
  619. rc = -EAGAIN;
  620. cFYI(1,("marking request for retry"));
  621. } else {
  622. rc = -EIO;
  623. }
  624. }
  625. spin_unlock(&GlobalMid_Lock);
  626. DeleteMidQEntry(midQ);
  627. /* If not lock req, update # of requests on wire to server */
  628. if(long_op < 3) {
  629. atomic_dec(&ses->server->inFlight);
  630. wake_up(&ses->server->request_q);
  631. }
  632. return rc;
  633. }
  634. if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
  635. cERROR(1, ("Frame too large received. Length: %d Xid: %d",
  636. receive_len, xid));
  637. rc = -EIO;
  638. } else { /* rcvd frame is ok */
  639. if (midQ->resp_buf && out_buf
  640. && (midQ->midState == MID_RESPONSE_RECEIVED)) {
  641. out_buf->smb_buf_length = receive_len;
  642. memcpy((char *)out_buf + 4,
  643. (char *)midQ->resp_buf + 4,
  644. receive_len);
  645. dump_smb(out_buf, 92);
  646. /* convert the length into a more usable form */
  647. if((receive_len > 24) &&
  648. (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
  649. SECMODE_SIGN_ENABLED))) {
  650. rc = cifs_verify_signature(out_buf,
  651. ses->server->mac_signing_key,
  652. midQ->sequence_number+1);
  653. if(rc) {
  654. cERROR(1,("Unexpected SMB signature"));
  655. /* BB FIXME add code to kill session */
  656. }
  657. }
  658. *pbytes_returned = out_buf->smb_buf_length;
  659. /* BB special case reconnect tid and uid here? */
  660. rc = map_smb_to_linux_error(out_buf);
  661. /* convert ByteCount if necessary */
  662. if (receive_len >=
  663. sizeof (struct smb_hdr) -
  664. 4 /* do not count RFC1001 header */ +
  665. (2 * out_buf->WordCount) + 2 /* bcc */ )
  666. BCC(out_buf) = le16_to_cpu(BCC(out_buf));
  667. } else {
  668. rc = -EIO;
  669. cFYI(1,("Bad MID state? "));
  670. }
  671. }
  672. cifs_no_response_exit:
  673. DeleteMidQEntry(midQ);
  674. if(long_op < 3) {
  675. atomic_dec(&ses->server->inFlight);
  676. wake_up(&ses->server->request_q);
  677. }
  678. return rc;
  679. out_unlock:
  680. up(&ses->server->tcpSem);
  681. /* If not lock req, update # of requests on wire to server */
  682. if(long_op < 3) {
  683. atomic_dec(&ses->server->inFlight);
  684. wake_up(&ses->server->request_q);
  685. }
  686. return rc;
  687. }