transport.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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) {
  281. cERROR(1,("Null smb session"));
  282. return -EIO;
  283. }
  284. if(ses->server == NULL) {
  285. cERROR(1,("Null tcp session"));
  286. return -EIO;
  287. }
  288. if(ses->server->tcpStatus == CifsExiting)
  289. return -ENOENT;
  290. /* Ensure that we do not send more than 50 overlapping requests
  291. to the same server. We may make this configurable later or
  292. use ses->maxReq */
  293. if(long_op == -1) {
  294. /* oplock breaks must not be held up */
  295. atomic_inc(&ses->server->inFlight);
  296. } else {
  297. spin_lock(&GlobalMid_Lock);
  298. while(1) {
  299. if(atomic_read(&ses->server->inFlight) >=
  300. cifs_max_pending){
  301. spin_unlock(&GlobalMid_Lock);
  302. #ifdef CONFIG_CIFS_STATS2
  303. atomic_inc(&ses->server->num_waiters);
  304. #endif
  305. wait_event(ses->server->request_q,
  306. atomic_read(&ses->server->inFlight)
  307. < cifs_max_pending);
  308. #ifdef CONFIG_CIFS_STATS2
  309. atomic_dec(&ses->server->num_waiters);
  310. #endif
  311. spin_lock(&GlobalMid_Lock);
  312. } else {
  313. if(ses->server->tcpStatus == CifsExiting) {
  314. spin_unlock(&GlobalMid_Lock);
  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. /* If not lock req, update # of requests on wire to server */
  351. if(long_op < 3) {
  352. atomic_dec(&ses->server->inFlight);
  353. wake_up(&ses->server->request_q);
  354. }
  355. return -ENOMEM;
  356. }
  357. rc = cifs_sign_smb2(iov, n_vec, ses->server, &midQ->sequence_number);
  358. midQ->midState = MID_REQUEST_SUBMITTED;
  359. #ifdef CONFIG_CIFS_STATS2
  360. atomic_inc(&ses->server->inSend);
  361. #endif
  362. rc = smb_send2(ses->server->ssocket, iov, n_vec,
  363. (struct sockaddr *) &(ses->server->addr.sockAddr));
  364. #ifdef CONFIG_CIFS_STATS2
  365. atomic_dec(&ses->server->inSend);
  366. midQ->when_sent = jiffies;
  367. #endif
  368. if(rc < 0) {
  369. DeleteMidQEntry(midQ);
  370. up(&ses->server->tcpSem);
  371. /* If not lock req, update # of requests on wire to server */
  372. if(long_op < 3) {
  373. atomic_dec(&ses->server->inFlight);
  374. wake_up(&ses->server->request_q);
  375. }
  376. return rc;
  377. } else
  378. up(&ses->server->tcpSem);
  379. if (long_op == -1)
  380. goto cifs_no_response_exit2;
  381. else if (long_op == 2) /* writes past end of file can take loong time */
  382. timeout = 180 * HZ;
  383. else if (long_op == 1)
  384. timeout = 45 * HZ; /* should be greater than
  385. servers oplock break timeout (about 43 seconds) */
  386. else if (long_op > 2) {
  387. timeout = MAX_SCHEDULE_TIMEOUT;
  388. } else
  389. timeout = 15 * HZ;
  390. /* wait for 15 seconds or until woken up due to response arriving or
  391. due to last connection to this server being unmounted */
  392. if (signal_pending(current)) {
  393. /* if signal pending do not hold up user for full smb timeout
  394. but we still give response a change to complete */
  395. timeout = 2 * HZ;
  396. }
  397. /* No user interrupts in wait - wreaks havoc with performance */
  398. if(timeout != MAX_SCHEDULE_TIMEOUT) {
  399. timeout += jiffies;
  400. wait_event(ses->server->response_q,
  401. (!(midQ->midState & MID_REQUEST_SUBMITTED)) ||
  402. time_after(jiffies, timeout) ||
  403. ((ses->server->tcpStatus != CifsGood) &&
  404. (ses->server->tcpStatus != CifsNew)));
  405. } else {
  406. wait_event(ses->server->response_q,
  407. (!(midQ->midState & MID_REQUEST_SUBMITTED)) ||
  408. ((ses->server->tcpStatus != CifsGood) &&
  409. (ses->server->tcpStatus != CifsNew)));
  410. }
  411. spin_lock(&GlobalMid_Lock);
  412. if (midQ->resp_buf) {
  413. spin_unlock(&GlobalMid_Lock);
  414. receive_len = midQ->resp_buf->smb_buf_length;
  415. } else {
  416. cERROR(1,("No response to cmd %d mid %d",
  417. midQ->command, midQ->mid));
  418. if(midQ->midState == MID_REQUEST_SUBMITTED) {
  419. if(ses->server->tcpStatus == CifsExiting)
  420. rc = -EHOSTDOWN;
  421. else {
  422. ses->server->tcpStatus = CifsNeedReconnect;
  423. midQ->midState = MID_RETRY_NEEDED;
  424. }
  425. }
  426. if (rc != -EHOSTDOWN) {
  427. if(midQ->midState == MID_RETRY_NEEDED) {
  428. rc = -EAGAIN;
  429. cFYI(1,("marking request for retry"));
  430. } else {
  431. rc = -EIO;
  432. }
  433. }
  434. spin_unlock(&GlobalMid_Lock);
  435. DeleteMidQEntry(midQ);
  436. /* If not lock req, update # of requests on wire to server */
  437. if(long_op < 3) {
  438. atomic_dec(&ses->server->inFlight);
  439. wake_up(&ses->server->request_q);
  440. }
  441. return rc;
  442. }
  443. if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
  444. cERROR(1, ("Frame too large received. Length: %d Xid: %d",
  445. receive_len, xid));
  446. rc = -EIO;
  447. } else { /* rcvd frame is ok */
  448. if (midQ->resp_buf &&
  449. (midQ->midState == MID_RESPONSE_RECEIVED)) {
  450. iov[0].iov_base = (char *)midQ->resp_buf;
  451. if(midQ->largeBuf)
  452. *pRespBufType = CIFS_LARGE_BUFFER;
  453. else
  454. *pRespBufType = CIFS_SMALL_BUFFER;
  455. iov[0].iov_len = receive_len + 4;
  456. dump_smb(midQ->resp_buf, 80);
  457. /* convert the length into a more usable form */
  458. if((receive_len > 24) &&
  459. (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
  460. SECMODE_SIGN_ENABLED))) {
  461. rc = cifs_verify_signature(midQ->resp_buf,
  462. ses->server->mac_signing_key,
  463. midQ->sequence_number+1);
  464. if(rc) {
  465. cERROR(1,("Unexpected SMB signature"));
  466. /* BB FIXME add code to kill session */
  467. }
  468. }
  469. /* BB special case reconnect tid and uid here? */
  470. /* BB special case Errbadpassword and pwdexpired here */
  471. rc = map_smb_to_linux_error(midQ->resp_buf);
  472. /* convert ByteCount if necessary */
  473. if (receive_len >=
  474. sizeof (struct smb_hdr) -
  475. 4 /* do not count RFC1001 header */ +
  476. (2 * midQ->resp_buf->WordCount) + 2 /* bcc */ )
  477. BCC(midQ->resp_buf) =
  478. le16_to_cpu(BCC_LE(midQ->resp_buf));
  479. midQ->resp_buf = NULL; /* mark it so will not be freed
  480. by DeleteMidQEntry */
  481. } else {
  482. rc = -EIO;
  483. cFYI(1,("Bad MID state?"));
  484. }
  485. }
  486. cifs_no_response_exit2:
  487. DeleteMidQEntry(midQ);
  488. if(long_op < 3) {
  489. atomic_dec(&ses->server->inFlight);
  490. wake_up(&ses->server->request_q);
  491. }
  492. return rc;
  493. out_unlock2:
  494. up(&ses->server->tcpSem);
  495. /* If not lock req, update # of requests on wire to server */
  496. if(long_op < 3) {
  497. atomic_dec(&ses->server->inFlight);
  498. wake_up(&ses->server->request_q);
  499. }
  500. return rc;
  501. }
  502. int
  503. SendReceive(const unsigned int xid, struct cifsSesInfo *ses,
  504. struct smb_hdr *in_buf, struct smb_hdr *out_buf,
  505. int *pbytes_returned, const int long_op)
  506. {
  507. int rc = 0;
  508. unsigned int receive_len;
  509. unsigned long timeout;
  510. struct mid_q_entry *midQ;
  511. if (ses == NULL) {
  512. cERROR(1,("Null smb session"));
  513. return -EIO;
  514. }
  515. if(ses->server == NULL) {
  516. cERROR(1,("Null tcp session"));
  517. return -EIO;
  518. }
  519. if(ses->server->tcpStatus == CifsExiting)
  520. return -ENOENT;
  521. /* Ensure that we do not send more than 50 overlapping requests
  522. to the same server. We may make this configurable later or
  523. use ses->maxReq */
  524. if(long_op == -1) {
  525. /* oplock breaks must not be held up */
  526. atomic_inc(&ses->server->inFlight);
  527. } else {
  528. spin_lock(&GlobalMid_Lock);
  529. while(1) {
  530. if(atomic_read(&ses->server->inFlight) >=
  531. cifs_max_pending){
  532. spin_unlock(&GlobalMid_Lock);
  533. #ifdef CONFIG_CIFS_STATS2
  534. atomic_inc(&ses->server->num_waiters);
  535. #endif
  536. wait_event(ses->server->request_q,
  537. atomic_read(&ses->server->inFlight)
  538. < cifs_max_pending);
  539. #ifdef CONFIG_CIFS_STATS2
  540. atomic_dec(&ses->server->num_waiters);
  541. #endif
  542. spin_lock(&GlobalMid_Lock);
  543. } else {
  544. if(ses->server->tcpStatus == CifsExiting) {
  545. spin_unlock(&GlobalMid_Lock);
  546. return -ENOENT;
  547. }
  548. /* can not count locking commands against total since
  549. they are allowed to block on server */
  550. if(long_op < 3) {
  551. /* update # of requests on the wire to server */
  552. atomic_inc(&ses->server->inFlight);
  553. }
  554. spin_unlock(&GlobalMid_Lock);
  555. break;
  556. }
  557. }
  558. }
  559. /* make sure that we sign in the same order that we send on this socket
  560. and avoid races inside tcp sendmsg code that could cause corruption
  561. of smb data */
  562. down(&ses->server->tcpSem);
  563. if (ses->server->tcpStatus == CifsExiting) {
  564. rc = -ENOENT;
  565. goto out_unlock;
  566. } else if (ses->server->tcpStatus == CifsNeedReconnect) {
  567. cFYI(1,("tcp session dead - return to caller to retry"));
  568. rc = -EAGAIN;
  569. goto out_unlock;
  570. } else if (ses->status != CifsGood) {
  571. /* check if SMB session is bad because we are setting it up */
  572. if((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
  573. (in_buf->Command != SMB_COM_NEGOTIATE)) {
  574. rc = -EAGAIN;
  575. goto out_unlock;
  576. } /* else ok - we are setting up session */
  577. }
  578. midQ = AllocMidQEntry(in_buf, ses);
  579. if (midQ == NULL) {
  580. up(&ses->server->tcpSem);
  581. /* If not lock req, update # of requests on wire to server */
  582. if(long_op < 3) {
  583. atomic_dec(&ses->server->inFlight);
  584. wake_up(&ses->server->request_q);
  585. }
  586. return -ENOMEM;
  587. }
  588. if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
  589. up(&ses->server->tcpSem);
  590. cERROR(1,
  591. ("Illegal length, greater than maximum frame, %d ",
  592. in_buf->smb_buf_length));
  593. DeleteMidQEntry(midQ);
  594. /* If not lock req, update # of requests on wire to server */
  595. if(long_op < 3) {
  596. atomic_dec(&ses->server->inFlight);
  597. wake_up(&ses->server->request_q);
  598. }
  599. return -EIO;
  600. }
  601. rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
  602. midQ->midState = MID_REQUEST_SUBMITTED;
  603. #ifdef CONFIG_CIFS_STATS2
  604. atomic_inc(&ses->server->inSend);
  605. #endif
  606. rc = smb_send(ses->server->ssocket, in_buf, in_buf->smb_buf_length,
  607. (struct sockaddr *) &(ses->server->addr.sockAddr));
  608. #ifdef CONFIG_CIFS_STATS2
  609. atomic_dec(&ses->server->inSend);
  610. midQ->when_sent = jiffies;
  611. #endif
  612. if(rc < 0) {
  613. DeleteMidQEntry(midQ);
  614. up(&ses->server->tcpSem);
  615. /* If not lock req, update # of requests on wire to server */
  616. if(long_op < 3) {
  617. atomic_dec(&ses->server->inFlight);
  618. wake_up(&ses->server->request_q);
  619. }
  620. return rc;
  621. } else
  622. up(&ses->server->tcpSem);
  623. if (long_op == -1)
  624. goto cifs_no_response_exit;
  625. else if (long_op == 2) /* writes past end of file can take loong time */
  626. timeout = 180 * HZ;
  627. else if (long_op == 1)
  628. timeout = 45 * HZ; /* should be greater than
  629. servers oplock break timeout (about 43 seconds) */
  630. else if (long_op > 2) {
  631. timeout = MAX_SCHEDULE_TIMEOUT;
  632. } else
  633. timeout = 15 * HZ;
  634. /* wait for 15 seconds or until woken up due to response arriving or
  635. due to last connection to this server being unmounted */
  636. if (signal_pending(current)) {
  637. /* if signal pending do not hold up user for full smb timeout
  638. but we still give response a change to complete */
  639. timeout = 2 * HZ;
  640. }
  641. /* No user interrupts in wait - wreaks havoc with performance */
  642. if(timeout != MAX_SCHEDULE_TIMEOUT) {
  643. timeout += jiffies;
  644. wait_event(ses->server->response_q,
  645. (!(midQ->midState & MID_REQUEST_SUBMITTED)) ||
  646. time_after(jiffies, timeout) ||
  647. ((ses->server->tcpStatus != CifsGood) &&
  648. (ses->server->tcpStatus != CifsNew)));
  649. } else {
  650. wait_event(ses->server->response_q,
  651. (!(midQ->midState & MID_REQUEST_SUBMITTED)) ||
  652. ((ses->server->tcpStatus != CifsGood) &&
  653. (ses->server->tcpStatus != CifsNew)));
  654. }
  655. spin_lock(&GlobalMid_Lock);
  656. if (midQ->resp_buf) {
  657. spin_unlock(&GlobalMid_Lock);
  658. receive_len = midQ->resp_buf->smb_buf_length;
  659. } else {
  660. cERROR(1,("No response for cmd %d mid %d",
  661. midQ->command, midQ->mid));
  662. if(midQ->midState == MID_REQUEST_SUBMITTED) {
  663. if(ses->server->tcpStatus == CifsExiting)
  664. rc = -EHOSTDOWN;
  665. else {
  666. ses->server->tcpStatus = CifsNeedReconnect;
  667. midQ->midState = MID_RETRY_NEEDED;
  668. }
  669. }
  670. if (rc != -EHOSTDOWN) {
  671. if(midQ->midState == MID_RETRY_NEEDED) {
  672. rc = -EAGAIN;
  673. cFYI(1,("marking request for retry"));
  674. } else {
  675. rc = -EIO;
  676. }
  677. }
  678. spin_unlock(&GlobalMid_Lock);
  679. DeleteMidQEntry(midQ);
  680. /* If not lock req, update # of requests on wire to server */
  681. if(long_op < 3) {
  682. atomic_dec(&ses->server->inFlight);
  683. wake_up(&ses->server->request_q);
  684. }
  685. return rc;
  686. }
  687. if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
  688. cERROR(1, ("Frame too large received. Length: %d Xid: %d",
  689. receive_len, xid));
  690. rc = -EIO;
  691. } else { /* rcvd frame is ok */
  692. if (midQ->resp_buf && out_buf
  693. && (midQ->midState == MID_RESPONSE_RECEIVED)) {
  694. out_buf->smb_buf_length = receive_len;
  695. memcpy((char *)out_buf + 4,
  696. (char *)midQ->resp_buf + 4,
  697. receive_len);
  698. dump_smb(out_buf, 92);
  699. /* convert the length into a more usable form */
  700. if((receive_len > 24) &&
  701. (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
  702. SECMODE_SIGN_ENABLED))) {
  703. rc = cifs_verify_signature(out_buf,
  704. ses->server->mac_signing_key,
  705. midQ->sequence_number+1);
  706. if(rc) {
  707. cERROR(1,("Unexpected SMB signature"));
  708. /* BB FIXME add code to kill session */
  709. }
  710. }
  711. *pbytes_returned = out_buf->smb_buf_length;
  712. /* BB special case reconnect tid and uid here? */
  713. rc = map_smb_to_linux_error(out_buf);
  714. /* convert ByteCount if necessary */
  715. if (receive_len >=
  716. sizeof (struct smb_hdr) -
  717. 4 /* do not count RFC1001 header */ +
  718. (2 * out_buf->WordCount) + 2 /* bcc */ )
  719. BCC(out_buf) = le16_to_cpu(BCC_LE(out_buf));
  720. } else {
  721. rc = -EIO;
  722. cERROR(1,("Bad MID state?"));
  723. }
  724. }
  725. cifs_no_response_exit:
  726. DeleteMidQEntry(midQ);
  727. if(long_op < 3) {
  728. atomic_dec(&ses->server->inFlight);
  729. wake_up(&ses->server->request_q);
  730. }
  731. return rc;
  732. out_unlock:
  733. up(&ses->server->tcpSem);
  734. /* If not lock req, update # of requests on wire to server */
  735. if(long_op < 3) {
  736. atomic_dec(&ses->server->inFlight);
  737. wake_up(&ses->server->request_q);
  738. }
  739. return rc;
  740. }