transport.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  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);*/ /* easier to use jiffies */
  58. /* when mid allocated can be before when sent */
  59. temp->when_alloc = jiffies;
  60. temp->ses = ses;
  61. temp->tsk = current;
  62. }
  63. spin_lock(&GlobalMid_Lock);
  64. list_add_tail(&temp->qhead, &ses->server->pending_mid_q);
  65. atomic_inc(&midCount);
  66. temp->midState = MID_REQUEST_ALLOCATED;
  67. spin_unlock(&GlobalMid_Lock);
  68. return temp;
  69. }
  70. static void
  71. DeleteMidQEntry(struct mid_q_entry *midEntry)
  72. {
  73. #ifdef CONFIG_CIFS_STATS2
  74. unsigned long now;
  75. #endif
  76. spin_lock(&GlobalMid_Lock);
  77. midEntry->midState = MID_FREE;
  78. list_del(&midEntry->qhead);
  79. atomic_dec(&midCount);
  80. spin_unlock(&GlobalMid_Lock);
  81. if(midEntry->largeBuf)
  82. cifs_buf_release(midEntry->resp_buf);
  83. else
  84. cifs_small_buf_release(midEntry->resp_buf);
  85. #ifdef CONFIG_CIFS_STATS2
  86. now = jiffies;
  87. /* commands taking longer than one second are indications that
  88. something is wrong, unless it is quite a slow link or server */
  89. if((now - midEntry->when_alloc) > HZ) {
  90. if((cifsFYI & CIFS_TIMER) &&
  91. (midEntry->command != SMB_COM_LOCKING_ANDX)) {
  92. printk(KERN_DEBUG " CIFS slow rsp: cmd %d mid %d",
  93. midEntry->command, midEntry->mid);
  94. printk(" A: 0x%lx S: 0x%lx R: 0x%lx\n",
  95. now - midEntry->when_alloc,
  96. now - midEntry->when_sent,
  97. now - midEntry->when_received);
  98. }
  99. }
  100. #endif
  101. mempool_free(midEntry, cifs_mid_poolp);
  102. }
  103. struct oplock_q_entry *
  104. AllocOplockQEntry(struct inode * pinode, __u16 fid, struct cifsTconInfo * tcon)
  105. {
  106. struct oplock_q_entry *temp;
  107. if ((pinode== NULL) || (tcon == NULL)) {
  108. cERROR(1, ("Null parms passed to AllocOplockQEntry"));
  109. return NULL;
  110. }
  111. temp = (struct oplock_q_entry *) kmem_cache_alloc(cifs_oplock_cachep,
  112. SLAB_KERNEL);
  113. if (temp == NULL)
  114. return temp;
  115. else {
  116. temp->pinode = pinode;
  117. temp->tcon = tcon;
  118. temp->netfid = fid;
  119. spin_lock(&GlobalMid_Lock);
  120. list_add_tail(&temp->qhead, &GlobalOplock_Q);
  121. spin_unlock(&GlobalMid_Lock);
  122. }
  123. return temp;
  124. }
  125. void DeleteOplockQEntry(struct oplock_q_entry * oplockEntry)
  126. {
  127. spin_lock(&GlobalMid_Lock);
  128. /* should we check if list empty first? */
  129. list_del(&oplockEntry->qhead);
  130. spin_unlock(&GlobalMid_Lock);
  131. kmem_cache_free(cifs_oplock_cachep, oplockEntry);
  132. }
  133. int
  134. smb_send(struct socket *ssocket, struct smb_hdr *smb_buffer,
  135. unsigned int smb_buf_length, struct sockaddr *sin)
  136. {
  137. int rc = 0;
  138. int i = 0;
  139. struct msghdr smb_msg;
  140. struct kvec iov;
  141. unsigned len = smb_buf_length + 4;
  142. if(ssocket == NULL)
  143. return -ENOTSOCK; /* BB eventually add reconnect code here */
  144. iov.iov_base = smb_buffer;
  145. iov.iov_len = len;
  146. smb_msg.msg_name = sin;
  147. smb_msg.msg_namelen = sizeof (struct sockaddr);
  148. smb_msg.msg_control = NULL;
  149. smb_msg.msg_controllen = 0;
  150. smb_msg.msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL; /* BB add more flags?*/
  151. /* smb header is converted in header_assemble. bcc and rest of SMB word
  152. area, and byte area if necessary, is converted to littleendian in
  153. cifssmb.c and RFC1001 len is converted to bigendian in smb_send
  154. Flags2 is converted in SendReceive */
  155. smb_buffer->smb_buf_length = cpu_to_be32(smb_buffer->smb_buf_length);
  156. cFYI(1, ("Sending smb of length %d", smb_buf_length));
  157. dump_smb(smb_buffer, len);
  158. while (len > 0) {
  159. rc = kernel_sendmsg(ssocket, &smb_msg, &iov, 1, len);
  160. if ((rc == -ENOSPC) || (rc == -EAGAIN)) {
  161. i++;
  162. /* smaller timeout here than send2 since smaller size */
  163. /* Although it may not be required, this also is smaller
  164. oplock break time */
  165. if(i > 12) {
  166. cERROR(1,
  167. ("sends on sock %p stuck for 7 seconds",
  168. ssocket));
  169. rc = -EAGAIN;
  170. break;
  171. }
  172. msleep(1 << i);
  173. continue;
  174. }
  175. if (rc < 0)
  176. break;
  177. else
  178. i = 0; /* reset i after each successful send */
  179. iov.iov_base += rc;
  180. iov.iov_len -= rc;
  181. len -= rc;
  182. }
  183. if (rc < 0) {
  184. cERROR(1,("Error %d sending data on socket to server", rc));
  185. } else {
  186. rc = 0;
  187. }
  188. return rc;
  189. }
  190. static int
  191. smb_send2(struct socket *ssocket, struct kvec *iov, int n_vec,
  192. struct sockaddr *sin)
  193. {
  194. int rc = 0;
  195. int i = 0;
  196. struct msghdr smb_msg;
  197. struct smb_hdr *smb_buffer = iov[0].iov_base;
  198. unsigned int len = iov[0].iov_len;
  199. unsigned int total_len;
  200. int first_vec = 0;
  201. if(ssocket == NULL)
  202. return -ENOTSOCK; /* BB eventually add reconnect code here */
  203. smb_msg.msg_name = sin;
  204. smb_msg.msg_namelen = sizeof (struct sockaddr);
  205. smb_msg.msg_control = NULL;
  206. smb_msg.msg_controllen = 0;
  207. smb_msg.msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL; /* BB add more flags?*/
  208. /* smb header is converted in header_assemble. bcc and rest of SMB word
  209. area, and byte area if necessary, is converted to littleendian in
  210. cifssmb.c and RFC1001 len is converted to bigendian in smb_send
  211. Flags2 is converted in SendReceive */
  212. total_len = 0;
  213. for (i = 0; i < n_vec; i++)
  214. total_len += iov[i].iov_len;
  215. smb_buffer->smb_buf_length = cpu_to_be32(smb_buffer->smb_buf_length);
  216. cFYI(1, ("Sending smb: total_len %d", total_len));
  217. dump_smb(smb_buffer, len);
  218. while (total_len) {
  219. rc = kernel_sendmsg(ssocket, &smb_msg, &iov[first_vec],
  220. n_vec - first_vec, total_len);
  221. if ((rc == -ENOSPC) || (rc == -EAGAIN)) {
  222. i++;
  223. if(i >= 14) {
  224. cERROR(1,
  225. ("sends on sock %p stuck for 15 seconds",
  226. ssocket));
  227. rc = -EAGAIN;
  228. break;
  229. }
  230. msleep(1 << i);
  231. continue;
  232. }
  233. if (rc < 0)
  234. break;
  235. if (rc >= total_len) {
  236. WARN_ON(rc > total_len);
  237. break;
  238. }
  239. if(rc == 0) {
  240. /* should never happen, letting socket clear before
  241. retrying is our only obvious option here */
  242. cERROR(1,("tcp sent no data"));
  243. msleep(500);
  244. continue;
  245. }
  246. total_len -= rc;
  247. /* the line below resets i */
  248. for (i = first_vec; i < n_vec; i++) {
  249. if (iov[i].iov_len) {
  250. if (rc > iov[i].iov_len) {
  251. rc -= iov[i].iov_len;
  252. iov[i].iov_len = 0;
  253. } else {
  254. iov[i].iov_base += rc;
  255. iov[i].iov_len -= rc;
  256. first_vec = i;
  257. break;
  258. }
  259. }
  260. }
  261. i = 0; /* in case we get ENOSPC on the next send */
  262. }
  263. if (rc < 0) {
  264. cERROR(1,("Error %d sending data on socket to server", rc));
  265. } else
  266. rc = 0;
  267. return rc;
  268. }
  269. int
  270. SendReceive2(const unsigned int xid, struct cifsSesInfo *ses,
  271. struct kvec *iov, int n_vec, int * pRespBufType /* ret */,
  272. const int long_op)
  273. {
  274. int rc = 0;
  275. unsigned int receive_len;
  276. unsigned long timeout;
  277. struct mid_q_entry *midQ;
  278. struct smb_hdr *in_buf = iov[0].iov_base;
  279. *pRespBufType = CIFS_NO_BUFFER; /* no response buf yet */
  280. if ((ses == NULL) || (ses->server == NULL)) {
  281. cifs_small_buf_release(in_buf);
  282. cERROR(1,("Null session"));
  283. return -EIO;
  284. }
  285. if(ses->server->tcpStatus == CifsExiting) {
  286. cifs_small_buf_release(in_buf);
  287. return -ENOENT;
  288. }
  289. /* Ensure that we do not send more than 50 overlapping requests
  290. to the same server. We may make this configurable later or
  291. use ses->maxReq */
  292. if(long_op == -1) {
  293. /* oplock breaks must not be held up */
  294. atomic_inc(&ses->server->inFlight);
  295. } else {
  296. spin_lock(&GlobalMid_Lock);
  297. while(1) {
  298. if(atomic_read(&ses->server->inFlight) >=
  299. cifs_max_pending){
  300. spin_unlock(&GlobalMid_Lock);
  301. #ifdef CONFIG_CIFS_STATS2
  302. atomic_inc(&ses->server->num_waiters);
  303. #endif
  304. wait_event(ses->server->request_q,
  305. atomic_read(&ses->server->inFlight)
  306. < cifs_max_pending);
  307. #ifdef CONFIG_CIFS_STATS2
  308. atomic_dec(&ses->server->num_waiters);
  309. #endif
  310. spin_lock(&GlobalMid_Lock);
  311. } else {
  312. if(ses->server->tcpStatus == CifsExiting) {
  313. spin_unlock(&GlobalMid_Lock);
  314. cifs_small_buf_release(in_buf);
  315. return -ENOENT;
  316. }
  317. /* can not count locking commands against total since
  318. they are allowed to block on server */
  319. if(long_op < 3) {
  320. /* update # of requests on the wire to server */
  321. atomic_inc(&ses->server->inFlight);
  322. }
  323. spin_unlock(&GlobalMid_Lock);
  324. break;
  325. }
  326. }
  327. }
  328. /* make sure that we sign in the same order that we send on this socket
  329. and avoid races inside tcp sendmsg code that could cause corruption
  330. of smb data */
  331. down(&ses->server->tcpSem);
  332. if (ses->server->tcpStatus == CifsExiting) {
  333. rc = -ENOENT;
  334. goto out_unlock2;
  335. } else if (ses->server->tcpStatus == CifsNeedReconnect) {
  336. cFYI(1,("tcp session dead - return to caller to retry"));
  337. rc = -EAGAIN;
  338. goto out_unlock2;
  339. } else if (ses->status != CifsGood) {
  340. /* check if SMB session is bad because we are setting it up */
  341. if((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
  342. (in_buf->Command != SMB_COM_NEGOTIATE)) {
  343. rc = -EAGAIN;
  344. goto out_unlock2;
  345. } /* else ok - we are setting up session */
  346. }
  347. midQ = AllocMidQEntry(in_buf, ses);
  348. if (midQ == NULL) {
  349. up(&ses->server->tcpSem);
  350. cifs_small_buf_release(in_buf);
  351. /* If not lock req, update # of requests on wire to server */
  352. if(long_op < 3) {
  353. atomic_dec(&ses->server->inFlight);
  354. wake_up(&ses->server->request_q);
  355. }
  356. return -ENOMEM;
  357. }
  358. rc = cifs_sign_smb2(iov, n_vec, ses->server, &midQ->sequence_number);
  359. midQ->midState = MID_REQUEST_SUBMITTED;
  360. #ifdef CONFIG_CIFS_STATS2
  361. atomic_inc(&ses->server->inSend);
  362. #endif
  363. rc = smb_send2(ses->server->ssocket, iov, n_vec,
  364. (struct sockaddr *) &(ses->server->addr.sockAddr));
  365. #ifdef CONFIG_CIFS_STATS2
  366. atomic_dec(&ses->server->inSend);
  367. midQ->when_sent = jiffies;
  368. #endif
  369. if(rc < 0) {
  370. DeleteMidQEntry(midQ);
  371. up(&ses->server->tcpSem);
  372. cifs_small_buf_release(in_buf);
  373. /* If not lock req, update # of requests on wire to server */
  374. if(long_op < 3) {
  375. atomic_dec(&ses->server->inFlight);
  376. wake_up(&ses->server->request_q);
  377. }
  378. return rc;
  379. } else {
  380. up(&ses->server->tcpSem);
  381. cifs_small_buf_release(in_buf);
  382. }
  383. if (long_op == -1)
  384. goto cifs_no_response_exit2;
  385. else if (long_op == 2) /* writes past end of file can take loong time */
  386. timeout = 180 * HZ;
  387. else if (long_op == 1)
  388. timeout = 45 * HZ; /* should be greater than
  389. servers oplock break timeout (about 43 seconds) */
  390. else if (long_op > 2) {
  391. timeout = MAX_SCHEDULE_TIMEOUT;
  392. } else
  393. timeout = 15 * HZ;
  394. /* wait for 15 seconds or until woken up due to response arriving or
  395. due to last connection to this server being unmounted */
  396. if (signal_pending(current)) {
  397. /* if signal pending do not hold up user for full smb timeout
  398. but we still give response a change to complete */
  399. timeout = 2 * HZ;
  400. }
  401. /* No user interrupts in wait - wreaks havoc with performance */
  402. if(timeout != MAX_SCHEDULE_TIMEOUT) {
  403. timeout += jiffies;
  404. wait_event(ses->server->response_q,
  405. (!(midQ->midState & MID_REQUEST_SUBMITTED)) ||
  406. time_after(jiffies, timeout) ||
  407. ((ses->server->tcpStatus != CifsGood) &&
  408. (ses->server->tcpStatus != CifsNew)));
  409. } else {
  410. wait_event(ses->server->response_q,
  411. (!(midQ->midState & MID_REQUEST_SUBMITTED)) ||
  412. ((ses->server->tcpStatus != CifsGood) &&
  413. (ses->server->tcpStatus != CifsNew)));
  414. }
  415. spin_lock(&GlobalMid_Lock);
  416. if (midQ->resp_buf) {
  417. spin_unlock(&GlobalMid_Lock);
  418. receive_len = midQ->resp_buf->smb_buf_length;
  419. } else {
  420. cERROR(1,("No response to cmd %d mid %d",
  421. midQ->command, midQ->mid));
  422. if(midQ->midState == MID_REQUEST_SUBMITTED) {
  423. if(ses->server->tcpStatus == CifsExiting)
  424. rc = -EHOSTDOWN;
  425. else {
  426. ses->server->tcpStatus = CifsNeedReconnect;
  427. midQ->midState = MID_RETRY_NEEDED;
  428. }
  429. }
  430. if (rc != -EHOSTDOWN) {
  431. if(midQ->midState == MID_RETRY_NEEDED) {
  432. rc = -EAGAIN;
  433. cFYI(1,("marking request for retry"));
  434. } else {
  435. rc = -EIO;
  436. }
  437. }
  438. spin_unlock(&GlobalMid_Lock);
  439. DeleteMidQEntry(midQ);
  440. /* If not lock req, update # of requests on wire to server */
  441. if(long_op < 3) {
  442. atomic_dec(&ses->server->inFlight);
  443. wake_up(&ses->server->request_q);
  444. }
  445. return rc;
  446. }
  447. if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
  448. cERROR(1, ("Frame too large received. Length: %d Xid: %d",
  449. receive_len, xid));
  450. rc = -EIO;
  451. } else { /* rcvd frame is ok */
  452. if (midQ->resp_buf &&
  453. (midQ->midState == MID_RESPONSE_RECEIVED)) {
  454. iov[0].iov_base = (char *)midQ->resp_buf;
  455. if(midQ->largeBuf)
  456. *pRespBufType = CIFS_LARGE_BUFFER;
  457. else
  458. *pRespBufType = CIFS_SMALL_BUFFER;
  459. iov[0].iov_len = receive_len + 4;
  460. dump_smb(midQ->resp_buf, 80);
  461. /* convert the length into a more usable form */
  462. if((receive_len > 24) &&
  463. (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
  464. SECMODE_SIGN_ENABLED))) {
  465. rc = cifs_verify_signature(midQ->resp_buf,
  466. ses->server->mac_signing_key,
  467. midQ->sequence_number+1);
  468. if(rc) {
  469. cERROR(1,("Unexpected SMB signature"));
  470. /* BB FIXME add code to kill session */
  471. }
  472. }
  473. /* BB special case reconnect tid and uid here? */
  474. /* BB special case Errbadpassword and pwdexpired here */
  475. rc = map_smb_to_linux_error(midQ->resp_buf);
  476. /* convert ByteCount if necessary */
  477. if (receive_len >=
  478. sizeof (struct smb_hdr) -
  479. 4 /* do not count RFC1001 header */ +
  480. (2 * midQ->resp_buf->WordCount) + 2 /* bcc */ )
  481. BCC(midQ->resp_buf) =
  482. le16_to_cpu(BCC_LE(midQ->resp_buf));
  483. midQ->resp_buf = NULL; /* mark it so will not be freed
  484. by DeleteMidQEntry */
  485. } else {
  486. rc = -EIO;
  487. cFYI(1,("Bad MID state?"));
  488. }
  489. }
  490. cifs_no_response_exit2:
  491. DeleteMidQEntry(midQ);
  492. if(long_op < 3) {
  493. atomic_dec(&ses->server->inFlight);
  494. wake_up(&ses->server->request_q);
  495. }
  496. return rc;
  497. out_unlock2:
  498. up(&ses->server->tcpSem);
  499. cifs_small_buf_release(in_buf);
  500. /* If not lock req, update # of requests on wire to server */
  501. if(long_op < 3) {
  502. atomic_dec(&ses->server->inFlight);
  503. wake_up(&ses->server->request_q);
  504. }
  505. return rc;
  506. }
  507. int
  508. SendReceive(const unsigned int xid, struct cifsSesInfo *ses,
  509. struct smb_hdr *in_buf, struct smb_hdr *out_buf,
  510. int *pbytes_returned, const int long_op)
  511. {
  512. int rc = 0;
  513. unsigned int receive_len;
  514. unsigned long timeout;
  515. struct mid_q_entry *midQ;
  516. if (ses == NULL) {
  517. cERROR(1,("Null smb session"));
  518. return -EIO;
  519. }
  520. if(ses->server == NULL) {
  521. cERROR(1,("Null tcp session"));
  522. return -EIO;
  523. }
  524. if(ses->server->tcpStatus == CifsExiting)
  525. return -ENOENT;
  526. /* Ensure that we do not send more than 50 overlapping requests
  527. to the same server. We may make this configurable later or
  528. use ses->maxReq */
  529. if(long_op == -1) {
  530. /* oplock breaks must not be held up */
  531. atomic_inc(&ses->server->inFlight);
  532. } else {
  533. spin_lock(&GlobalMid_Lock);
  534. while(1) {
  535. if(atomic_read(&ses->server->inFlight) >=
  536. cifs_max_pending){
  537. spin_unlock(&GlobalMid_Lock);
  538. #ifdef CONFIG_CIFS_STATS2
  539. atomic_inc(&ses->server->num_waiters);
  540. #endif
  541. wait_event(ses->server->request_q,
  542. atomic_read(&ses->server->inFlight)
  543. < cifs_max_pending);
  544. #ifdef CONFIG_CIFS_STATS2
  545. atomic_dec(&ses->server->num_waiters);
  546. #endif
  547. spin_lock(&GlobalMid_Lock);
  548. } else {
  549. if(ses->server->tcpStatus == CifsExiting) {
  550. spin_unlock(&GlobalMid_Lock);
  551. return -ENOENT;
  552. }
  553. /* can not count locking commands against total since
  554. they are allowed to block on server */
  555. if(long_op < 3) {
  556. /* update # of requests on the wire to server */
  557. atomic_inc(&ses->server->inFlight);
  558. }
  559. spin_unlock(&GlobalMid_Lock);
  560. break;
  561. }
  562. }
  563. }
  564. /* make sure that we sign in the same order that we send on this socket
  565. and avoid races inside tcp sendmsg code that could cause corruption
  566. of smb data */
  567. down(&ses->server->tcpSem);
  568. if (ses->server->tcpStatus == CifsExiting) {
  569. rc = -ENOENT;
  570. goto out_unlock;
  571. } else if (ses->server->tcpStatus == CifsNeedReconnect) {
  572. cFYI(1,("tcp session dead - return to caller to retry"));
  573. rc = -EAGAIN;
  574. goto out_unlock;
  575. } else if (ses->status != CifsGood) {
  576. /* check if SMB session is bad because we are setting it up */
  577. if((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
  578. (in_buf->Command != SMB_COM_NEGOTIATE)) {
  579. rc = -EAGAIN;
  580. goto out_unlock;
  581. } /* else ok - we are setting up session */
  582. }
  583. midQ = AllocMidQEntry(in_buf, ses);
  584. if (midQ == NULL) {
  585. up(&ses->server->tcpSem);
  586. /* If not lock req, update # of requests on wire to server */
  587. if(long_op < 3) {
  588. atomic_dec(&ses->server->inFlight);
  589. wake_up(&ses->server->request_q);
  590. }
  591. return -ENOMEM;
  592. }
  593. if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
  594. up(&ses->server->tcpSem);
  595. cERROR(1,
  596. ("Illegal length, greater than maximum frame, %d ",
  597. in_buf->smb_buf_length));
  598. DeleteMidQEntry(midQ);
  599. /* If not lock req, update # of requests on wire to server */
  600. if(long_op < 3) {
  601. atomic_dec(&ses->server->inFlight);
  602. wake_up(&ses->server->request_q);
  603. }
  604. return -EIO;
  605. }
  606. rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
  607. midQ->midState = MID_REQUEST_SUBMITTED;
  608. #ifdef CONFIG_CIFS_STATS2
  609. atomic_inc(&ses->server->inSend);
  610. #endif
  611. rc = smb_send(ses->server->ssocket, in_buf, in_buf->smb_buf_length,
  612. (struct sockaddr *) &(ses->server->addr.sockAddr));
  613. #ifdef CONFIG_CIFS_STATS2
  614. atomic_dec(&ses->server->inSend);
  615. midQ->when_sent = jiffies;
  616. #endif
  617. if(rc < 0) {
  618. DeleteMidQEntry(midQ);
  619. up(&ses->server->tcpSem);
  620. /* If not lock req, update # of requests on wire to server */
  621. if(long_op < 3) {
  622. atomic_dec(&ses->server->inFlight);
  623. wake_up(&ses->server->request_q);
  624. }
  625. return rc;
  626. } else
  627. up(&ses->server->tcpSem);
  628. if (long_op == -1)
  629. goto cifs_no_response_exit;
  630. else if (long_op == 2) /* writes past end of file can take loong time */
  631. timeout = 180 * HZ;
  632. else if (long_op == 1)
  633. timeout = 45 * HZ; /* should be greater than
  634. servers oplock break timeout (about 43 seconds) */
  635. else if (long_op > 2) {
  636. timeout = MAX_SCHEDULE_TIMEOUT;
  637. } else
  638. timeout = 15 * HZ;
  639. /* wait for 15 seconds or until woken up due to response arriving or
  640. due to last connection to this server being unmounted */
  641. if (signal_pending(current)) {
  642. /* if signal pending do not hold up user for full smb timeout
  643. but we still give response a change to complete */
  644. timeout = 2 * HZ;
  645. }
  646. /* No user interrupts in wait - wreaks havoc with performance */
  647. if(timeout != MAX_SCHEDULE_TIMEOUT) {
  648. timeout += jiffies;
  649. wait_event(ses->server->response_q,
  650. (!(midQ->midState & MID_REQUEST_SUBMITTED)) ||
  651. time_after(jiffies, timeout) ||
  652. ((ses->server->tcpStatus != CifsGood) &&
  653. (ses->server->tcpStatus != CifsNew)));
  654. } else {
  655. wait_event(ses->server->response_q,
  656. (!(midQ->midState & MID_REQUEST_SUBMITTED)) ||
  657. ((ses->server->tcpStatus != CifsGood) &&
  658. (ses->server->tcpStatus != CifsNew)));
  659. }
  660. spin_lock(&GlobalMid_Lock);
  661. if (midQ->resp_buf) {
  662. spin_unlock(&GlobalMid_Lock);
  663. receive_len = midQ->resp_buf->smb_buf_length;
  664. } else {
  665. cERROR(1,("No response for cmd %d mid %d",
  666. midQ->command, midQ->mid));
  667. if(midQ->midState == MID_REQUEST_SUBMITTED) {
  668. if(ses->server->tcpStatus == CifsExiting)
  669. rc = -EHOSTDOWN;
  670. else {
  671. ses->server->tcpStatus = CifsNeedReconnect;
  672. midQ->midState = MID_RETRY_NEEDED;
  673. }
  674. }
  675. if (rc != -EHOSTDOWN) {
  676. if(midQ->midState == MID_RETRY_NEEDED) {
  677. rc = -EAGAIN;
  678. cFYI(1,("marking request for retry"));
  679. } else {
  680. rc = -EIO;
  681. }
  682. }
  683. spin_unlock(&GlobalMid_Lock);
  684. DeleteMidQEntry(midQ);
  685. /* If not lock req, update # of requests on wire to server */
  686. if(long_op < 3) {
  687. atomic_dec(&ses->server->inFlight);
  688. wake_up(&ses->server->request_q);
  689. }
  690. return rc;
  691. }
  692. if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
  693. cERROR(1, ("Frame too large received. Length: %d Xid: %d",
  694. receive_len, xid));
  695. rc = -EIO;
  696. } else { /* rcvd frame is ok */
  697. if (midQ->resp_buf && out_buf
  698. && (midQ->midState == MID_RESPONSE_RECEIVED)) {
  699. out_buf->smb_buf_length = receive_len;
  700. memcpy((char *)out_buf + 4,
  701. (char *)midQ->resp_buf + 4,
  702. receive_len);
  703. dump_smb(out_buf, 92);
  704. /* convert the length into a more usable form */
  705. if((receive_len > 24) &&
  706. (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
  707. SECMODE_SIGN_ENABLED))) {
  708. rc = cifs_verify_signature(out_buf,
  709. ses->server->mac_signing_key,
  710. midQ->sequence_number+1);
  711. if(rc) {
  712. cERROR(1,("Unexpected SMB signature"));
  713. /* BB FIXME add code to kill session */
  714. }
  715. }
  716. *pbytes_returned = out_buf->smb_buf_length;
  717. /* BB special case reconnect tid and uid here? */
  718. rc = map_smb_to_linux_error(out_buf);
  719. /* convert ByteCount if necessary */
  720. if (receive_len >=
  721. sizeof (struct smb_hdr) -
  722. 4 /* do not count RFC1001 header */ +
  723. (2 * out_buf->WordCount) + 2 /* bcc */ )
  724. BCC(out_buf) = le16_to_cpu(BCC_LE(out_buf));
  725. } else {
  726. rc = -EIO;
  727. cERROR(1,("Bad MID state?"));
  728. }
  729. }
  730. cifs_no_response_exit:
  731. DeleteMidQEntry(midQ);
  732. if(long_op < 3) {
  733. atomic_dec(&ses->server->inFlight);
  734. wake_up(&ses->server->request_q);
  735. }
  736. return rc;
  737. out_unlock:
  738. up(&ses->server->tcpSem);
  739. /* If not lock req, update # of requests on wire to server */
  740. if(long_op < 3) {
  741. atomic_dec(&ses->server->inFlight);
  742. wake_up(&ses->server->request_q);
  743. }
  744. return rc;
  745. }