transport.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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. * Jeremy Allison (jra@samba.org) 2006.
  7. *
  8. * This library is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published
  10. * by the Free Software Foundation; either version 2.1 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  16. * the GNU Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/fs.h>
  23. #include <linux/list.h>
  24. #include <linux/wait.h>
  25. #include <linux/net.h>
  26. #include <linux/delay.h>
  27. #include <asm/uaccess.h>
  28. #include <asm/processor.h>
  29. #include <linux/mempool.h>
  30. #include "cifspdu.h"
  31. #include "cifsglob.h"
  32. #include "cifsproto.h"
  33. #include "cifs_debug.h"
  34. extern mempool_t *cifs_mid_poolp;
  35. extern kmem_cache_t *cifs_oplock_cachep;
  36. static struct mid_q_entry *
  37. AllocMidQEntry(struct smb_hdr *smb_buffer, struct cifsSesInfo *ses)
  38. {
  39. struct mid_q_entry *temp;
  40. if (ses == NULL) {
  41. cERROR(1, ("Null session passed in to AllocMidQEntry"));
  42. return NULL;
  43. }
  44. if (ses->server == NULL) {
  45. cERROR(1, ("Null TCP session in AllocMidQEntry"));
  46. return NULL;
  47. }
  48. temp = (struct mid_q_entry *) mempool_alloc(cifs_mid_poolp,
  49. SLAB_KERNEL | SLAB_NOFS);
  50. if (temp == NULL)
  51. return temp;
  52. else {
  53. memset(temp, 0, sizeof (struct mid_q_entry));
  54. temp->mid = smb_buffer->Mid; /* always LE */
  55. temp->pid = current->pid;
  56. temp->command = smb_buffer->Command;
  57. cFYI(1, ("For smb_command %d", temp->command));
  58. /* do_gettimeofday(&temp->when_sent);*/ /* easier to use jiffies */
  59. /* when mid allocated can be before when sent */
  60. temp->when_alloc = jiffies;
  61. temp->ses = ses;
  62. temp->tsk = current;
  63. }
  64. spin_lock(&GlobalMid_Lock);
  65. list_add_tail(&temp->qhead, &ses->server->pending_mid_q);
  66. atomic_inc(&midCount);
  67. temp->midState = MID_REQUEST_ALLOCATED;
  68. spin_unlock(&GlobalMid_Lock);
  69. return temp;
  70. }
  71. static void
  72. DeleteMidQEntry(struct mid_q_entry *midEntry)
  73. {
  74. #ifdef CONFIG_CIFS_STATS2
  75. unsigned long now;
  76. #endif
  77. spin_lock(&GlobalMid_Lock);
  78. midEntry->midState = MID_FREE;
  79. list_del(&midEntry->qhead);
  80. atomic_dec(&midCount);
  81. spin_unlock(&GlobalMid_Lock);
  82. if(midEntry->largeBuf)
  83. cifs_buf_release(midEntry->resp_buf);
  84. else
  85. cifs_small_buf_release(midEntry->resp_buf);
  86. #ifdef CONFIG_CIFS_STATS2
  87. now = jiffies;
  88. /* commands taking longer than one second are indications that
  89. something is wrong, unless it is quite a slow link or server */
  90. if((now - midEntry->when_alloc) > HZ) {
  91. if((cifsFYI & CIFS_TIMER) &&
  92. (midEntry->command != SMB_COM_LOCKING_ANDX)) {
  93. printk(KERN_DEBUG " CIFS slow rsp: cmd %d mid %d",
  94. midEntry->command, midEntry->mid);
  95. printk(" A: 0x%lx S: 0x%lx R: 0x%lx\n",
  96. now - midEntry->when_alloc,
  97. now - midEntry->when_sent,
  98. now - midEntry->when_received);
  99. }
  100. }
  101. #endif
  102. mempool_free(midEntry, cifs_mid_poolp);
  103. }
  104. struct oplock_q_entry *
  105. AllocOplockQEntry(struct inode * pinode, __u16 fid, struct cifsTconInfo * tcon)
  106. {
  107. struct oplock_q_entry *temp;
  108. if ((pinode== NULL) || (tcon == NULL)) {
  109. cERROR(1, ("Null parms passed to AllocOplockQEntry"));
  110. return NULL;
  111. }
  112. temp = (struct oplock_q_entry *) kmem_cache_alloc(cifs_oplock_cachep,
  113. SLAB_KERNEL);
  114. if (temp == NULL)
  115. return temp;
  116. else {
  117. temp->pinode = pinode;
  118. temp->tcon = tcon;
  119. temp->netfid = fid;
  120. spin_lock(&GlobalMid_Lock);
  121. list_add_tail(&temp->qhead, &GlobalOplock_Q);
  122. spin_unlock(&GlobalMid_Lock);
  123. }
  124. return temp;
  125. }
  126. void DeleteOplockQEntry(struct oplock_q_entry * oplockEntry)
  127. {
  128. spin_lock(&GlobalMid_Lock);
  129. /* should we check if list empty first? */
  130. list_del(&oplockEntry->qhead);
  131. spin_unlock(&GlobalMid_Lock);
  132. kmem_cache_free(cifs_oplock_cachep, oplockEntry);
  133. }
  134. int
  135. smb_send(struct socket *ssocket, struct smb_hdr *smb_buffer,
  136. unsigned int smb_buf_length, struct sockaddr *sin)
  137. {
  138. int rc = 0;
  139. int i = 0;
  140. struct msghdr smb_msg;
  141. struct kvec iov;
  142. unsigned len = smb_buf_length + 4;
  143. if(ssocket == NULL)
  144. return -ENOTSOCK; /* BB eventually add reconnect code here */
  145. iov.iov_base = smb_buffer;
  146. iov.iov_len = len;
  147. smb_msg.msg_name = sin;
  148. smb_msg.msg_namelen = sizeof (struct sockaddr);
  149. smb_msg.msg_control = NULL;
  150. smb_msg.msg_controllen = 0;
  151. smb_msg.msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL; /* BB add more flags?*/
  152. /* smb header is converted in header_assemble. bcc and rest of SMB word
  153. area, and byte area if necessary, is converted to littleendian in
  154. cifssmb.c and RFC1001 len is converted to bigendian in smb_send
  155. Flags2 is converted in SendReceive */
  156. smb_buffer->smb_buf_length = cpu_to_be32(smb_buffer->smb_buf_length);
  157. cFYI(1, ("Sending smb of length %d", smb_buf_length));
  158. dump_smb(smb_buffer, len);
  159. while (len > 0) {
  160. rc = kernel_sendmsg(ssocket, &smb_msg, &iov, 1, len);
  161. if ((rc == -ENOSPC) || (rc == -EAGAIN)) {
  162. i++;
  163. /* smaller timeout here than send2 since smaller size */
  164. /* Although it may not be required, this also is smaller
  165. oplock break time */
  166. if(i > 12) {
  167. cERROR(1,
  168. ("sends on sock %p stuck for 7 seconds",
  169. ssocket));
  170. rc = -EAGAIN;
  171. break;
  172. }
  173. msleep(1 << i);
  174. continue;
  175. }
  176. if (rc < 0)
  177. break;
  178. else
  179. i = 0; /* reset i after each successful send */
  180. iov.iov_base += rc;
  181. iov.iov_len -= rc;
  182. len -= rc;
  183. }
  184. if (rc < 0) {
  185. cERROR(1,("Error %d sending data on socket to server", rc));
  186. } else {
  187. rc = 0;
  188. }
  189. return rc;
  190. }
  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 * pRespBufType /* ret */,
  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. *pRespBufType = CIFS_NO_BUFFER; /* no response buf yet */
  281. if ((ses == NULL) || (ses->server == NULL)) {
  282. cifs_small_buf_release(in_buf);
  283. cERROR(1,("Null session"));
  284. return -EIO;
  285. }
  286. if(ses->server->tcpStatus == CifsExiting) {
  287. cifs_small_buf_release(in_buf);
  288. return -ENOENT;
  289. }
  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. cifs_small_buf_release(in_buf);
  316. return -ENOENT;
  317. }
  318. /* can not count locking commands against total since
  319. they are allowed to block on server */
  320. if(long_op < 3) {
  321. /* update # of requests on the wire to server */
  322. atomic_inc(&ses->server->inFlight);
  323. }
  324. spin_unlock(&GlobalMid_Lock);
  325. break;
  326. }
  327. }
  328. }
  329. /* make sure that we sign in the same order that we send on this socket
  330. and avoid races inside tcp sendmsg code that could cause corruption
  331. of smb data */
  332. down(&ses->server->tcpSem);
  333. if (ses->server->tcpStatus == CifsExiting) {
  334. rc = -ENOENT;
  335. goto out_unlock2;
  336. } else if (ses->server->tcpStatus == CifsNeedReconnect) {
  337. cFYI(1,("tcp session dead - return to caller to retry"));
  338. rc = -EAGAIN;
  339. goto out_unlock2;
  340. } else if (ses->status != CifsGood) {
  341. /* check if SMB session is bad because we are setting it up */
  342. if((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
  343. (in_buf->Command != SMB_COM_NEGOTIATE)) {
  344. rc = -EAGAIN;
  345. goto out_unlock2;
  346. } /* else ok - we are setting up session */
  347. }
  348. midQ = AllocMidQEntry(in_buf, ses);
  349. if (midQ == NULL) {
  350. up(&ses->server->tcpSem);
  351. cifs_small_buf_release(in_buf);
  352. /* If not lock req, update # of requests on wire to server */
  353. if(long_op < 3) {
  354. atomic_dec(&ses->server->inFlight);
  355. wake_up(&ses->server->request_q);
  356. }
  357. return -ENOMEM;
  358. }
  359. rc = cifs_sign_smb2(iov, n_vec, ses->server, &midQ->sequence_number);
  360. midQ->midState = MID_REQUEST_SUBMITTED;
  361. #ifdef CONFIG_CIFS_STATS2
  362. atomic_inc(&ses->server->inSend);
  363. #endif
  364. rc = smb_send2(ses->server->ssocket, iov, n_vec,
  365. (struct sockaddr *) &(ses->server->addr.sockAddr));
  366. #ifdef CONFIG_CIFS_STATS2
  367. atomic_dec(&ses->server->inSend);
  368. midQ->when_sent = jiffies;
  369. #endif
  370. if(rc < 0) {
  371. DeleteMidQEntry(midQ);
  372. up(&ses->server->tcpSem);
  373. cifs_small_buf_release(in_buf);
  374. /* If not lock req, update # of requests on wire to server */
  375. if(long_op < 3) {
  376. atomic_dec(&ses->server->inFlight);
  377. wake_up(&ses->server->request_q);
  378. }
  379. return rc;
  380. } else {
  381. up(&ses->server->tcpSem);
  382. cifs_small_buf_release(in_buf);
  383. }
  384. if (long_op == -1)
  385. goto cifs_no_response_exit2;
  386. else if (long_op == 2) /* writes past end of file can take loong time */
  387. timeout = 180 * HZ;
  388. else if (long_op == 1)
  389. timeout = 45 * HZ; /* should be greater than
  390. servers oplock break timeout (about 43 seconds) */
  391. else if (long_op > 2) {
  392. timeout = MAX_SCHEDULE_TIMEOUT;
  393. } else
  394. timeout = 15 * HZ;
  395. /* wait for 15 seconds or until woken up due to response arriving or
  396. due to last connection to this server being unmounted */
  397. if (signal_pending(current)) {
  398. /* if signal pending do not hold up user for full smb timeout
  399. but we still give response a change to complete */
  400. timeout = 2 * HZ;
  401. }
  402. /* No user interrupts in wait - wreaks havoc with performance */
  403. if(timeout != MAX_SCHEDULE_TIMEOUT) {
  404. unsigned long curr_timeout;
  405. for (;;) {
  406. curr_timeout = timeout + jiffies;
  407. wait_event(ses->server->response_q,
  408. (!(midQ->midState & MID_REQUEST_SUBMITTED)) ||
  409. time_after(jiffies, curr_timeout) ||
  410. ((ses->server->tcpStatus != CifsGood) &&
  411. (ses->server->tcpStatus != CifsNew)));
  412. if (time_after(jiffies, curr_timeout) &&
  413. (midQ->midState & MID_REQUEST_SUBMITTED) &&
  414. ((ses->server->tcpStatus == CifsGood) ||
  415. (ses->server->tcpStatus == CifsNew))) {
  416. unsigned long lrt;
  417. /* We timed out. Is the server still
  418. sending replies ? */
  419. spin_lock(&GlobalMid_Lock);
  420. lrt = ses->server->lstrp;
  421. spin_unlock(&GlobalMid_Lock);
  422. /* Calculate 10 seconds past last receive time.
  423. Although we prefer not to time out if the
  424. server is still responding - we will time
  425. out if the server takes more than 15 (or 45
  426. or 180) seconds to respond to this request
  427. and has not responded to any request from
  428. other threads on the client within 10 seconds */
  429. lrt += (10 * HZ);
  430. if (time_after(jiffies, lrt)) {
  431. /* No replies for 10 seconds. */
  432. cERROR(1,("server not responding"));
  433. break;
  434. }
  435. } else {
  436. break;
  437. }
  438. }
  439. } else {
  440. wait_event(ses->server->response_q,
  441. (!(midQ->midState & MID_REQUEST_SUBMITTED)) ||
  442. ((ses->server->tcpStatus != CifsGood) &&
  443. (ses->server->tcpStatus != CifsNew)));
  444. }
  445. spin_lock(&GlobalMid_Lock);
  446. if (midQ->resp_buf) {
  447. spin_unlock(&GlobalMid_Lock);
  448. receive_len = midQ->resp_buf->smb_buf_length;
  449. } else {
  450. cERROR(1,("No response to cmd %d mid %d",
  451. midQ->command, midQ->mid));
  452. if(midQ->midState == MID_REQUEST_SUBMITTED) {
  453. if(ses->server->tcpStatus == CifsExiting)
  454. rc = -EHOSTDOWN;
  455. else {
  456. ses->server->tcpStatus = CifsNeedReconnect;
  457. midQ->midState = MID_RETRY_NEEDED;
  458. }
  459. }
  460. if (rc != -EHOSTDOWN) {
  461. if(midQ->midState == MID_RETRY_NEEDED) {
  462. rc = -EAGAIN;
  463. cFYI(1,("marking request for retry"));
  464. } else {
  465. rc = -EIO;
  466. }
  467. }
  468. spin_unlock(&GlobalMid_Lock);
  469. DeleteMidQEntry(midQ);
  470. /* If not lock req, update # of requests on wire to server */
  471. if(long_op < 3) {
  472. atomic_dec(&ses->server->inFlight);
  473. wake_up(&ses->server->request_q);
  474. }
  475. return rc;
  476. }
  477. if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
  478. cERROR(1, ("Frame too large received. Length: %d Xid: %d",
  479. receive_len, xid));
  480. rc = -EIO;
  481. } else { /* rcvd frame is ok */
  482. if (midQ->resp_buf &&
  483. (midQ->midState == MID_RESPONSE_RECEIVED)) {
  484. iov[0].iov_base = (char *)midQ->resp_buf;
  485. if(midQ->largeBuf)
  486. *pRespBufType = CIFS_LARGE_BUFFER;
  487. else
  488. *pRespBufType = CIFS_SMALL_BUFFER;
  489. iov[0].iov_len = receive_len + 4;
  490. dump_smb(midQ->resp_buf, 80);
  491. /* convert the length into a more usable form */
  492. if((receive_len > 24) &&
  493. (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
  494. SECMODE_SIGN_ENABLED))) {
  495. rc = cifs_verify_signature(midQ->resp_buf,
  496. ses->server->mac_signing_key,
  497. midQ->sequence_number+1);
  498. if(rc) {
  499. cERROR(1,("Unexpected SMB signature"));
  500. /* BB FIXME add code to kill session */
  501. }
  502. }
  503. /* BB special case reconnect tid and uid here? */
  504. /* BB special case Errbadpassword and pwdexpired here */
  505. rc = map_smb_to_linux_error(midQ->resp_buf);
  506. /* convert ByteCount if necessary */
  507. if (receive_len >=
  508. sizeof (struct smb_hdr) -
  509. 4 /* do not count RFC1001 header */ +
  510. (2 * midQ->resp_buf->WordCount) + 2 /* bcc */ )
  511. BCC(midQ->resp_buf) =
  512. le16_to_cpu(BCC_LE(midQ->resp_buf));
  513. midQ->resp_buf = NULL; /* mark it so will not be freed
  514. by DeleteMidQEntry */
  515. } else {
  516. rc = -EIO;
  517. cFYI(1,("Bad MID state?"));
  518. }
  519. }
  520. cifs_no_response_exit2:
  521. DeleteMidQEntry(midQ);
  522. if(long_op < 3) {
  523. atomic_dec(&ses->server->inFlight);
  524. wake_up(&ses->server->request_q);
  525. }
  526. return rc;
  527. out_unlock2:
  528. up(&ses->server->tcpSem);
  529. cifs_small_buf_release(in_buf);
  530. /* If not lock req, update # of requests on wire to server */
  531. if(long_op < 3) {
  532. atomic_dec(&ses->server->inFlight);
  533. wake_up(&ses->server->request_q);
  534. }
  535. return rc;
  536. }
  537. int
  538. SendReceive(const unsigned int xid, struct cifsSesInfo *ses,
  539. struct smb_hdr *in_buf, struct smb_hdr *out_buf,
  540. int *pbytes_returned, const int long_op)
  541. {
  542. int rc = 0;
  543. unsigned int receive_len;
  544. unsigned long timeout;
  545. struct mid_q_entry *midQ;
  546. if (ses == NULL) {
  547. cERROR(1,("Null smb session"));
  548. return -EIO;
  549. }
  550. if(ses->server == NULL) {
  551. cERROR(1,("Null tcp session"));
  552. return -EIO;
  553. }
  554. if(ses->server->tcpStatus == CifsExiting)
  555. return -ENOENT;
  556. /* Ensure that we do not send more than 50 overlapping requests
  557. to the same server. We may make this configurable later or
  558. use ses->maxReq */
  559. if(long_op == -1) {
  560. /* oplock breaks must not be held up */
  561. atomic_inc(&ses->server->inFlight);
  562. } else {
  563. spin_lock(&GlobalMid_Lock);
  564. while(1) {
  565. if(atomic_read(&ses->server->inFlight) >=
  566. cifs_max_pending){
  567. spin_unlock(&GlobalMid_Lock);
  568. #ifdef CONFIG_CIFS_STATS2
  569. atomic_inc(&ses->server->num_waiters);
  570. #endif
  571. wait_event(ses->server->request_q,
  572. atomic_read(&ses->server->inFlight)
  573. < cifs_max_pending);
  574. #ifdef CONFIG_CIFS_STATS2
  575. atomic_dec(&ses->server->num_waiters);
  576. #endif
  577. spin_lock(&GlobalMid_Lock);
  578. } else {
  579. if(ses->server->tcpStatus == CifsExiting) {
  580. spin_unlock(&GlobalMid_Lock);
  581. return -ENOENT;
  582. }
  583. /* can not count locking commands against total since
  584. they are allowed to block on server */
  585. if(long_op < 3) {
  586. /* update # of requests on the wire to server */
  587. atomic_inc(&ses->server->inFlight);
  588. }
  589. spin_unlock(&GlobalMid_Lock);
  590. break;
  591. }
  592. }
  593. }
  594. /* make sure that we sign in the same order that we send on this socket
  595. and avoid races inside tcp sendmsg code that could cause corruption
  596. of smb data */
  597. down(&ses->server->tcpSem);
  598. if (ses->server->tcpStatus == CifsExiting) {
  599. rc = -ENOENT;
  600. goto out_unlock;
  601. } else if (ses->server->tcpStatus == CifsNeedReconnect) {
  602. cFYI(1,("tcp session dead - return to caller to retry"));
  603. rc = -EAGAIN;
  604. goto out_unlock;
  605. } else if (ses->status != CifsGood) {
  606. /* check if SMB session is bad because we are setting it up */
  607. if((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
  608. (in_buf->Command != SMB_COM_NEGOTIATE)) {
  609. rc = -EAGAIN;
  610. goto out_unlock;
  611. } /* else ok - we are setting up session */
  612. }
  613. midQ = AllocMidQEntry(in_buf, ses);
  614. if (midQ == NULL) {
  615. up(&ses->server->tcpSem);
  616. /* If not lock req, update # of requests on wire to server */
  617. if(long_op < 3) {
  618. atomic_dec(&ses->server->inFlight);
  619. wake_up(&ses->server->request_q);
  620. }
  621. return -ENOMEM;
  622. }
  623. if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
  624. up(&ses->server->tcpSem);
  625. cERROR(1, ("Illegal length, greater than maximum frame, %d",
  626. in_buf->smb_buf_length));
  627. DeleteMidQEntry(midQ);
  628. /* If not lock req, update # of requests on wire to server */
  629. if(long_op < 3) {
  630. atomic_dec(&ses->server->inFlight);
  631. wake_up(&ses->server->request_q);
  632. }
  633. return -EIO;
  634. }
  635. rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
  636. midQ->midState = MID_REQUEST_SUBMITTED;
  637. #ifdef CONFIG_CIFS_STATS2
  638. atomic_inc(&ses->server->inSend);
  639. #endif
  640. rc = smb_send(ses->server->ssocket, in_buf, in_buf->smb_buf_length,
  641. (struct sockaddr *) &(ses->server->addr.sockAddr));
  642. #ifdef CONFIG_CIFS_STATS2
  643. atomic_dec(&ses->server->inSend);
  644. midQ->when_sent = jiffies;
  645. #endif
  646. if(rc < 0) {
  647. DeleteMidQEntry(midQ);
  648. up(&ses->server->tcpSem);
  649. /* If not lock req, update # of requests on wire to server */
  650. if(long_op < 3) {
  651. atomic_dec(&ses->server->inFlight);
  652. wake_up(&ses->server->request_q);
  653. }
  654. return rc;
  655. } else
  656. up(&ses->server->tcpSem);
  657. if (long_op == -1)
  658. goto cifs_no_response_exit;
  659. else if (long_op == 2) /* writes past end of file can take loong time */
  660. timeout = 180 * HZ;
  661. else if (long_op == 1)
  662. timeout = 45 * HZ; /* should be greater than
  663. servers oplock break timeout (about 43 seconds) */
  664. else if (long_op > 2) {
  665. timeout = MAX_SCHEDULE_TIMEOUT;
  666. } else
  667. timeout = 15 * HZ;
  668. /* wait for 15 seconds or until woken up due to response arriving or
  669. due to last connection to this server being unmounted */
  670. if (signal_pending(current)) {
  671. /* if signal pending do not hold up user for full smb timeout
  672. but we still give response a change to complete */
  673. timeout = 2 * HZ;
  674. }
  675. /* No user interrupts in wait - wreaks havoc with performance */
  676. if(timeout != MAX_SCHEDULE_TIMEOUT) {
  677. unsigned long curr_timeout;
  678. for (;;) {
  679. curr_timeout = timeout + jiffies;
  680. wait_event(ses->server->response_q,
  681. (!(midQ->midState & MID_REQUEST_SUBMITTED)) ||
  682. time_after(jiffies, curr_timeout) ||
  683. ((ses->server->tcpStatus != CifsGood) &&
  684. (ses->server->tcpStatus != CifsNew)));
  685. if (time_after(jiffies, curr_timeout) &&
  686. (midQ->midState & MID_REQUEST_SUBMITTED) &&
  687. ((ses->server->tcpStatus == CifsGood) ||
  688. (ses->server->tcpStatus == CifsNew))) {
  689. unsigned long lrt;
  690. /* We timed out. Is the server still
  691. sending replies ? */
  692. spin_lock(&GlobalMid_Lock);
  693. lrt = ses->server->lstrp;
  694. spin_unlock(&GlobalMid_Lock);
  695. /* Calculate 10 seconds past last receive time*/
  696. lrt += (10 * HZ);
  697. if (time_after(jiffies, lrt)) {
  698. /* Server sent no reply in 10 seconds */
  699. cERROR(1,("Server not responding"));
  700. break;
  701. }
  702. } else {
  703. break;
  704. }
  705. }
  706. } else {
  707. wait_event(ses->server->response_q,
  708. (!(midQ->midState & MID_REQUEST_SUBMITTED)) ||
  709. ((ses->server->tcpStatus != CifsGood) &&
  710. (ses->server->tcpStatus != CifsNew)));
  711. }
  712. spin_lock(&GlobalMid_Lock);
  713. if (midQ->resp_buf) {
  714. spin_unlock(&GlobalMid_Lock);
  715. receive_len = midQ->resp_buf->smb_buf_length;
  716. } else {
  717. cERROR(1,("No response for cmd %d mid %d",
  718. midQ->command, midQ->mid));
  719. if(midQ->midState == MID_REQUEST_SUBMITTED) {
  720. if(ses->server->tcpStatus == CifsExiting)
  721. rc = -EHOSTDOWN;
  722. else {
  723. ses->server->tcpStatus = CifsNeedReconnect;
  724. midQ->midState = MID_RETRY_NEEDED;
  725. }
  726. }
  727. if (rc != -EHOSTDOWN) {
  728. if(midQ->midState == MID_RETRY_NEEDED) {
  729. rc = -EAGAIN;
  730. cFYI(1,("marking request for retry"));
  731. } else {
  732. rc = -EIO;
  733. }
  734. }
  735. spin_unlock(&GlobalMid_Lock);
  736. DeleteMidQEntry(midQ);
  737. /* If not lock req, update # of requests on wire to server */
  738. if(long_op < 3) {
  739. atomic_dec(&ses->server->inFlight);
  740. wake_up(&ses->server->request_q);
  741. }
  742. return rc;
  743. }
  744. if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
  745. cERROR(1, ("Frame too large received. Length: %d Xid: %d",
  746. receive_len, xid));
  747. rc = -EIO;
  748. } else { /* rcvd frame is ok */
  749. if (midQ->resp_buf && out_buf
  750. && (midQ->midState == MID_RESPONSE_RECEIVED)) {
  751. out_buf->smb_buf_length = receive_len;
  752. memcpy((char *)out_buf + 4,
  753. (char *)midQ->resp_buf + 4,
  754. receive_len);
  755. dump_smb(out_buf, 92);
  756. /* convert the length into a more usable form */
  757. if((receive_len > 24) &&
  758. (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
  759. SECMODE_SIGN_ENABLED))) {
  760. rc = cifs_verify_signature(out_buf,
  761. ses->server->mac_signing_key,
  762. midQ->sequence_number+1);
  763. if(rc) {
  764. cERROR(1,("Unexpected SMB signature"));
  765. /* BB FIXME add code to kill session */
  766. }
  767. }
  768. *pbytes_returned = out_buf->smb_buf_length;
  769. /* BB special case reconnect tid and uid here? */
  770. rc = map_smb_to_linux_error(out_buf);
  771. /* convert ByteCount if necessary */
  772. if (receive_len >=
  773. sizeof (struct smb_hdr) -
  774. 4 /* do not count RFC1001 header */ +
  775. (2 * out_buf->WordCount) + 2 /* bcc */ )
  776. BCC(out_buf) = le16_to_cpu(BCC_LE(out_buf));
  777. } else {
  778. rc = -EIO;
  779. cERROR(1,("Bad MID state?"));
  780. }
  781. }
  782. cifs_no_response_exit:
  783. DeleteMidQEntry(midQ);
  784. if(long_op < 3) {
  785. atomic_dec(&ses->server->inFlight);
  786. wake_up(&ses->server->request_q);
  787. }
  788. return rc;
  789. out_unlock:
  790. up(&ses->server->tcpSem);
  791. /* If not lock req, update # of requests on wire to server */
  792. if(long_op < 3) {
  793. atomic_dec(&ses->server->inFlight);
  794. wake_up(&ses->server->request_q);
  795. }
  796. return rc;
  797. }