transport.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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. #ifdef CONFIG_CIFS_EXPERIMENTAL
  191. static int
  192. smb_send2(struct socket *ssocket, struct kvec *iov, int n_vec,
  193. struct sockaddr *sin)
  194. {
  195. int rc = 0;
  196. int i = 0;
  197. struct msghdr smb_msg;
  198. struct smb_hdr *smb_buffer = iov[0].iov_base;
  199. unsigned int len = iov[0].iov_len;
  200. unsigned int total_len;
  201. int first_vec = 0;
  202. if(ssocket == NULL)
  203. return -ENOTSOCK; /* BB eventually add reconnect code here */
  204. smb_msg.msg_name = sin;
  205. smb_msg.msg_namelen = sizeof (struct sockaddr);
  206. smb_msg.msg_control = NULL;
  207. smb_msg.msg_controllen = 0;
  208. smb_msg.msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL; /* BB add more flags?*/
  209. /* smb header is converted in header_assemble. bcc and rest of SMB word
  210. area, and byte area if necessary, is converted to littleendian in
  211. cifssmb.c and RFC1001 len is converted to bigendian in smb_send
  212. Flags2 is converted in SendReceive */
  213. total_len = 0;
  214. for (i = 0; i < n_vec; i++)
  215. total_len += iov[i].iov_len;
  216. smb_buffer->smb_buf_length = cpu_to_be32(smb_buffer->smb_buf_length);
  217. cFYI(1, ("Sending smb: total_len %d", total_len));
  218. dump_smb(smb_buffer, len);
  219. while (total_len) {
  220. rc = kernel_sendmsg(ssocket, &smb_msg, &iov[first_vec],
  221. n_vec - first_vec, total_len);
  222. if ((rc == -ENOSPC) || (rc == -EAGAIN)) {
  223. i++;
  224. if(i >= 14) {
  225. cERROR(1,
  226. ("sends on sock %p stuck for 15 seconds",
  227. ssocket));
  228. rc = -EAGAIN;
  229. break;
  230. }
  231. msleep(1 << i);
  232. continue;
  233. }
  234. if (rc < 0)
  235. break;
  236. if (rc >= total_len) {
  237. WARN_ON(rc > total_len);
  238. break;
  239. }
  240. if(rc == 0) {
  241. /* should never happen, letting socket clear before
  242. retrying is our only obvious option here */
  243. cERROR(1,("tcp sent no data"));
  244. msleep(500);
  245. continue;
  246. }
  247. total_len -= rc;
  248. /* the line below resets i */
  249. for (i = first_vec; i < n_vec; i++) {
  250. if (iov[i].iov_len) {
  251. if (rc > iov[i].iov_len) {
  252. rc -= iov[i].iov_len;
  253. iov[i].iov_len = 0;
  254. } else {
  255. iov[i].iov_base += rc;
  256. iov[i].iov_len -= rc;
  257. first_vec = i;
  258. break;
  259. }
  260. }
  261. }
  262. i = 0; /* in case we get ENOSPC on the next send */
  263. }
  264. if (rc < 0) {
  265. cERROR(1,("Error %d sending data on socket to server", rc));
  266. } else
  267. rc = 0;
  268. return rc;
  269. }
  270. int
  271. SendReceive2(const unsigned int xid, struct cifsSesInfo *ses,
  272. struct kvec *iov, int n_vec, int *pbytes_returned,
  273. const int long_op)
  274. {
  275. int rc = 0;
  276. unsigned int receive_len;
  277. unsigned long timeout;
  278. struct mid_q_entry *midQ;
  279. struct smb_hdr *in_buf = iov[0].iov_base;
  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. /* BB FIXME */
  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. /* If not lock req, update # of requests on wire to server */
  373. if(long_op < 3) {
  374. atomic_dec(&ses->server->inFlight);
  375. wake_up(&ses->server->request_q);
  376. }
  377. return rc;
  378. } else
  379. up(&ses->server->tcpSem);
  380. if (long_op == -1)
  381. goto cifs_no_response_exit2;
  382. else if (long_op == 2) /* writes past end of file can take loong time */
  383. timeout = 180 * HZ;
  384. else if (long_op == 1)
  385. timeout = 45 * HZ; /* should be greater than
  386. servers oplock break timeout (about 43 seconds) */
  387. else if (long_op > 2) {
  388. timeout = MAX_SCHEDULE_TIMEOUT;
  389. } else
  390. timeout = 15 * HZ;
  391. /* wait for 15 seconds or until woken up due to response arriving or
  392. due to last connection to this server being unmounted */
  393. if (signal_pending(current)) {
  394. /* if signal pending do not hold up user for full smb timeout
  395. but we still give response a change to complete */
  396. timeout = 2 * HZ;
  397. }
  398. /* No user interrupts in wait - wreaks havoc with performance */
  399. if(timeout != MAX_SCHEDULE_TIMEOUT) {
  400. timeout += jiffies;
  401. wait_event(ses->server->response_q,
  402. (!(midQ->midState & MID_REQUEST_SUBMITTED)) ||
  403. time_after(jiffies, timeout) ||
  404. ((ses->server->tcpStatus != CifsGood) &&
  405. (ses->server->tcpStatus != CifsNew)));
  406. } else {
  407. wait_event(ses->server->response_q,
  408. (!(midQ->midState & MID_REQUEST_SUBMITTED)) ||
  409. ((ses->server->tcpStatus != CifsGood) &&
  410. (ses->server->tcpStatus != CifsNew)));
  411. }
  412. spin_lock(&GlobalMid_Lock);
  413. if (midQ->resp_buf) {
  414. spin_unlock(&GlobalMid_Lock);
  415. receive_len = midQ->resp_buf->smb_buf_length;
  416. } else {
  417. cERROR(1,("No response to cmd %d mid %d",
  418. midQ->command, midQ->mid));
  419. if(midQ->midState == MID_REQUEST_SUBMITTED) {
  420. if(ses->server->tcpStatus == CifsExiting)
  421. rc = -EHOSTDOWN;
  422. else {
  423. ses->server->tcpStatus = CifsNeedReconnect;
  424. midQ->midState = MID_RETRY_NEEDED;
  425. }
  426. }
  427. if (rc != -EHOSTDOWN) {
  428. if(midQ->midState == MID_RETRY_NEEDED) {
  429. rc = -EAGAIN;
  430. cFYI(1,("marking request for retry"));
  431. } else {
  432. rc = -EIO;
  433. }
  434. }
  435. spin_unlock(&GlobalMid_Lock);
  436. DeleteMidQEntry(midQ);
  437. /* If not lock req, update # of requests on wire to server */
  438. if(long_op < 3) {
  439. atomic_dec(&ses->server->inFlight);
  440. wake_up(&ses->server->request_q);
  441. }
  442. return rc;
  443. }
  444. if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
  445. cERROR(1, ("Frame too large received. Length: %d Xid: %d",
  446. receive_len, xid));
  447. rc = -EIO;
  448. } else { /* rcvd frame is ok */
  449. if (midQ->resp_buf &&
  450. (midQ->midState == MID_RESPONSE_RECEIVED)) {
  451. in_buf->smb_buf_length = receive_len;
  452. /* BB verify that length would not overrun small buf */
  453. memcpy((char *)in_buf + 4,
  454. (char *)midQ->resp_buf + 4,
  455. receive_len);
  456. dump_smb(in_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(in_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. *pbytes_returned = in_buf->smb_buf_length;
  470. /* BB special case reconnect tid and uid here? */
  471. /* BB special case Errbadpassword and pwdexpired here */
  472. rc = map_smb_to_linux_error(in_buf);
  473. /* convert ByteCount if necessary */
  474. if (receive_len >=
  475. sizeof (struct smb_hdr) -
  476. 4 /* do not count RFC1001 header */ +
  477. (2 * in_buf->WordCount) + 2 /* bcc */ )
  478. BCC(in_buf) = le16_to_cpu(BCC_LE(in_buf));
  479. } else {
  480. rc = -EIO;
  481. cFYI(1,("Bad MID state?"));
  482. }
  483. }
  484. cifs_no_response_exit2:
  485. DeleteMidQEntry(midQ);
  486. if(long_op < 3) {
  487. atomic_dec(&ses->server->inFlight);
  488. wake_up(&ses->server->request_q);
  489. }
  490. return rc;
  491. out_unlock2:
  492. up(&ses->server->tcpSem);
  493. /* If not lock req, update # of requests on wire to server */
  494. if(long_op < 3) {
  495. atomic_dec(&ses->server->inFlight);
  496. wake_up(&ses->server->request_q);
  497. }
  498. return rc;
  499. }
  500. #endif /* CIFS_EXPERIMENTAL */
  501. int
  502. SendReceive(const unsigned int xid, struct cifsSesInfo *ses,
  503. struct smb_hdr *in_buf, struct smb_hdr *out_buf,
  504. int *pbytes_returned, const int long_op)
  505. {
  506. int rc = 0;
  507. unsigned int receive_len;
  508. unsigned long timeout;
  509. struct mid_q_entry *midQ;
  510. if (ses == NULL) {
  511. cERROR(1,("Null smb session"));
  512. return -EIO;
  513. }
  514. if(ses->server == NULL) {
  515. cERROR(1,("Null tcp session"));
  516. return -EIO;
  517. }
  518. if(ses->server->tcpStatus == CifsExiting)
  519. return -ENOENT;
  520. /* Ensure that we do not send more than 50 overlapping requests
  521. to the same server. We may make this configurable later or
  522. use ses->maxReq */
  523. if(long_op == -1) {
  524. /* oplock breaks must not be held up */
  525. atomic_inc(&ses->server->inFlight);
  526. } else {
  527. spin_lock(&GlobalMid_Lock);
  528. while(1) {
  529. if(atomic_read(&ses->server->inFlight) >=
  530. cifs_max_pending){
  531. spin_unlock(&GlobalMid_Lock);
  532. #ifdef CONFIG_CIFS_STATS2
  533. atomic_inc(&ses->server->num_waiters);
  534. #endif
  535. wait_event(ses->server->request_q,
  536. atomic_read(&ses->server->inFlight)
  537. < cifs_max_pending);
  538. #ifdef CONFIG_CIFS_STATS2
  539. atomic_dec(&ses->server->num_waiters);
  540. #endif
  541. spin_lock(&GlobalMid_Lock);
  542. } else {
  543. if(ses->server->tcpStatus == CifsExiting) {
  544. spin_unlock(&GlobalMid_Lock);
  545. return -ENOENT;
  546. }
  547. /* can not count locking commands against total since
  548. they are allowed to block on server */
  549. if(long_op < 3) {
  550. /* update # of requests on the wire to server */
  551. atomic_inc(&ses->server->inFlight);
  552. }
  553. spin_unlock(&GlobalMid_Lock);
  554. break;
  555. }
  556. }
  557. }
  558. /* make sure that we sign in the same order that we send on this socket
  559. and avoid races inside tcp sendmsg code that could cause corruption
  560. of smb data */
  561. down(&ses->server->tcpSem);
  562. if (ses->server->tcpStatus == CifsExiting) {
  563. rc = -ENOENT;
  564. goto out_unlock;
  565. } else if (ses->server->tcpStatus == CifsNeedReconnect) {
  566. cFYI(1,("tcp session dead - return to caller to retry"));
  567. rc = -EAGAIN;
  568. goto out_unlock;
  569. } else if (ses->status != CifsGood) {
  570. /* check if SMB session is bad because we are setting it up */
  571. if((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
  572. (in_buf->Command != SMB_COM_NEGOTIATE)) {
  573. rc = -EAGAIN;
  574. goto out_unlock;
  575. } /* else ok - we are setting up session */
  576. }
  577. midQ = AllocMidQEntry(in_buf, ses);
  578. if (midQ == NULL) {
  579. up(&ses->server->tcpSem);
  580. /* If not lock req, update # of requests on wire to server */
  581. if(long_op < 3) {
  582. atomic_dec(&ses->server->inFlight);
  583. wake_up(&ses->server->request_q);
  584. }
  585. return -ENOMEM;
  586. }
  587. if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
  588. up(&ses->server->tcpSem);
  589. cERROR(1,
  590. ("Illegal length, greater than maximum frame, %d ",
  591. in_buf->smb_buf_length));
  592. DeleteMidQEntry(midQ);
  593. /* If not lock req, update # of requests on wire to server */
  594. if(long_op < 3) {
  595. atomic_dec(&ses->server->inFlight);
  596. wake_up(&ses->server->request_q);
  597. }
  598. return -EIO;
  599. }
  600. rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
  601. midQ->midState = MID_REQUEST_SUBMITTED;
  602. #ifdef CONFIG_CIFS_STATS2
  603. atomic_inc(&ses->server->inSend);
  604. #endif
  605. rc = smb_send(ses->server->ssocket, in_buf, in_buf->smb_buf_length,
  606. (struct sockaddr *) &(ses->server->addr.sockAddr));
  607. #ifdef CONFIG_CIFS_STATS2
  608. atomic_dec(&ses->server->inSend);
  609. midQ->when_sent = jiffies;
  610. #endif
  611. if(rc < 0) {
  612. DeleteMidQEntry(midQ);
  613. up(&ses->server->tcpSem);
  614. /* If not lock req, update # of requests on wire to server */
  615. if(long_op < 3) {
  616. atomic_dec(&ses->server->inFlight);
  617. wake_up(&ses->server->request_q);
  618. }
  619. return rc;
  620. } else
  621. up(&ses->server->tcpSem);
  622. if (long_op == -1)
  623. goto cifs_no_response_exit;
  624. else if (long_op == 2) /* writes past end of file can take loong time */
  625. timeout = 180 * HZ;
  626. else if (long_op == 1)
  627. timeout = 45 * HZ; /* should be greater than
  628. servers oplock break timeout (about 43 seconds) */
  629. else if (long_op > 2) {
  630. timeout = MAX_SCHEDULE_TIMEOUT;
  631. } else
  632. timeout = 15 * HZ;
  633. /* wait for 15 seconds or until woken up due to response arriving or
  634. due to last connection to this server being unmounted */
  635. if (signal_pending(current)) {
  636. /* if signal pending do not hold up user for full smb timeout
  637. but we still give response a change to complete */
  638. timeout = 2 * HZ;
  639. }
  640. /* No user interrupts in wait - wreaks havoc with performance */
  641. if(timeout != MAX_SCHEDULE_TIMEOUT) {
  642. timeout += jiffies;
  643. wait_event(ses->server->response_q,
  644. (!(midQ->midState & MID_REQUEST_SUBMITTED)) ||
  645. time_after(jiffies, timeout) ||
  646. ((ses->server->tcpStatus != CifsGood) &&
  647. (ses->server->tcpStatus != CifsNew)));
  648. } else {
  649. wait_event(ses->server->response_q,
  650. (!(midQ->midState & MID_REQUEST_SUBMITTED)) ||
  651. ((ses->server->tcpStatus != CifsGood) &&
  652. (ses->server->tcpStatus != CifsNew)));
  653. }
  654. spin_lock(&GlobalMid_Lock);
  655. if (midQ->resp_buf) {
  656. spin_unlock(&GlobalMid_Lock);
  657. receive_len = midQ->resp_buf->smb_buf_length;
  658. } else {
  659. cERROR(1,("No response for cmd %d mid %d",
  660. midQ->command, midQ->mid));
  661. if(midQ->midState == MID_REQUEST_SUBMITTED) {
  662. if(ses->server->tcpStatus == CifsExiting)
  663. rc = -EHOSTDOWN;
  664. else {
  665. ses->server->tcpStatus = CifsNeedReconnect;
  666. midQ->midState = MID_RETRY_NEEDED;
  667. }
  668. }
  669. if (rc != -EHOSTDOWN) {
  670. if(midQ->midState == MID_RETRY_NEEDED) {
  671. rc = -EAGAIN;
  672. cFYI(1,("marking request for retry"));
  673. } else {
  674. rc = -EIO;
  675. }
  676. }
  677. spin_unlock(&GlobalMid_Lock);
  678. DeleteMidQEntry(midQ);
  679. /* If not lock req, update # of requests on wire to server */
  680. if(long_op < 3) {
  681. atomic_dec(&ses->server->inFlight);
  682. wake_up(&ses->server->request_q);
  683. }
  684. return rc;
  685. }
  686. if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
  687. cERROR(1, ("Frame too large received. Length: %d Xid: %d",
  688. receive_len, xid));
  689. rc = -EIO;
  690. } else { /* rcvd frame is ok */
  691. if (midQ->resp_buf && out_buf
  692. && (midQ->midState == MID_RESPONSE_RECEIVED)) {
  693. out_buf->smb_buf_length = receive_len;
  694. memcpy((char *)out_buf + 4,
  695. (char *)midQ->resp_buf + 4,
  696. receive_len);
  697. dump_smb(out_buf, 92);
  698. /* convert the length into a more usable form */
  699. if((receive_len > 24) &&
  700. (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
  701. SECMODE_SIGN_ENABLED))) {
  702. rc = cifs_verify_signature(out_buf,
  703. ses->server->mac_signing_key,
  704. midQ->sequence_number+1);
  705. if(rc) {
  706. cERROR(1,("Unexpected SMB signature"));
  707. /* BB FIXME add code to kill session */
  708. }
  709. }
  710. *pbytes_returned = out_buf->smb_buf_length;
  711. /* BB special case reconnect tid and uid here? */
  712. rc = map_smb_to_linux_error(out_buf);
  713. /* convert ByteCount if necessary */
  714. if (receive_len >=
  715. sizeof (struct smb_hdr) -
  716. 4 /* do not count RFC1001 header */ +
  717. (2 * out_buf->WordCount) + 2 /* bcc */ )
  718. BCC(out_buf) = le16_to_cpu(BCC_LE(out_buf));
  719. } else {
  720. rc = -EIO;
  721. cERROR(1,("Bad MID state? "));
  722. }
  723. }
  724. cifs_no_response_exit:
  725. DeleteMidQEntry(midQ);
  726. if(long_op < 3) {
  727. atomic_dec(&ses->server->inFlight);
  728. wake_up(&ses->server->request_q);
  729. }
  730. return rc;
  731. out_unlock:
  732. up(&ses->server->tcpSem);
  733. /* If not lock req, update # of requests on wire to server */
  734. if(long_op < 3) {
  735. atomic_dec(&ses->server->inFlight);
  736. wake_up(&ses->server->request_q);
  737. }
  738. return rc;
  739. }