transport.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  1. /*
  2. * fs/cifs/transport.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2002,2008
  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/gfp.h>
  25. #include <linux/wait.h>
  26. #include <linux/net.h>
  27. #include <linux/delay.h>
  28. #include <asm/uaccess.h>
  29. #include <asm/processor.h>
  30. #include <linux/mempool.h>
  31. #include "cifspdu.h"
  32. #include "cifsglob.h"
  33. #include "cifsproto.h"
  34. #include "cifs_debug.h"
  35. extern mempool_t *cifs_mid_poolp;
  36. static struct mid_q_entry *
  37. AllocMidQEntry(const struct smb_hdr *smb_buffer, struct TCP_Server_Info *server)
  38. {
  39. struct mid_q_entry *temp;
  40. if (server == NULL) {
  41. cERROR(1, "Null TCP session in AllocMidQEntry");
  42. return NULL;
  43. }
  44. temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
  45. if (temp == NULL)
  46. return temp;
  47. else {
  48. memset(temp, 0, sizeof(struct mid_q_entry));
  49. temp->mid = smb_buffer->Mid; /* always LE */
  50. temp->pid = current->pid;
  51. temp->command = smb_buffer->Command;
  52. cFYI(1, "For smb_command %d", temp->command);
  53. /* do_gettimeofday(&temp->when_sent);*/ /* easier to use jiffies */
  54. /* when mid allocated can be before when sent */
  55. temp->when_alloc = jiffies;
  56. temp->tsk = current;
  57. }
  58. atomic_inc(&midCount);
  59. temp->midState = MID_REQUEST_ALLOCATED;
  60. return temp;
  61. }
  62. static void
  63. DeleteMidQEntry(struct mid_q_entry *midEntry)
  64. {
  65. #ifdef CONFIG_CIFS_STATS2
  66. unsigned long now;
  67. #endif
  68. midEntry->midState = MID_FREE;
  69. atomic_dec(&midCount);
  70. if (midEntry->largeBuf)
  71. cifs_buf_release(midEntry->resp_buf);
  72. else
  73. cifs_small_buf_release(midEntry->resp_buf);
  74. #ifdef CONFIG_CIFS_STATS2
  75. now = jiffies;
  76. /* commands taking longer than one second are indications that
  77. something is wrong, unless it is quite a slow link or server */
  78. if ((now - midEntry->when_alloc) > HZ) {
  79. if ((cifsFYI & CIFS_TIMER) &&
  80. (midEntry->command != SMB_COM_LOCKING_ANDX)) {
  81. printk(KERN_DEBUG " CIFS slow rsp: cmd %d mid %d",
  82. midEntry->command, midEntry->mid);
  83. printk(" A: 0x%lx S: 0x%lx R: 0x%lx\n",
  84. now - midEntry->when_alloc,
  85. now - midEntry->when_sent,
  86. now - midEntry->when_received);
  87. }
  88. }
  89. #endif
  90. mempool_free(midEntry, cifs_mid_poolp);
  91. }
  92. static void
  93. delete_mid(struct mid_q_entry *mid)
  94. {
  95. spin_lock(&GlobalMid_Lock);
  96. list_del(&mid->qhead);
  97. spin_unlock(&GlobalMid_Lock);
  98. DeleteMidQEntry(mid);
  99. }
  100. static int
  101. smb_sendv(struct TCP_Server_Info *server, struct kvec *iov, int n_vec)
  102. {
  103. int rc = 0;
  104. int i = 0;
  105. struct msghdr smb_msg;
  106. struct smb_hdr *smb_buffer = iov[0].iov_base;
  107. unsigned int len = iov[0].iov_len;
  108. unsigned int total_len;
  109. int first_vec = 0;
  110. unsigned int smb_buf_length = smb_buffer->smb_buf_length;
  111. struct socket *ssocket = server->ssocket;
  112. if (ssocket == NULL)
  113. return -ENOTSOCK; /* BB eventually add reconnect code here */
  114. smb_msg.msg_name = (struct sockaddr *) &server->dstaddr;
  115. smb_msg.msg_namelen = sizeof(struct sockaddr);
  116. smb_msg.msg_control = NULL;
  117. smb_msg.msg_controllen = 0;
  118. if (server->noblocksnd)
  119. smb_msg.msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL;
  120. else
  121. smb_msg.msg_flags = MSG_NOSIGNAL;
  122. /* smb header is converted in header_assemble. bcc and rest of SMB word
  123. area, and byte area if necessary, is converted to littleendian in
  124. cifssmb.c and RFC1001 len is converted to bigendian in smb_send
  125. Flags2 is converted in SendReceive */
  126. total_len = 0;
  127. for (i = 0; i < n_vec; i++)
  128. total_len += iov[i].iov_len;
  129. smb_buffer->smb_buf_length = cpu_to_be32(smb_buffer->smb_buf_length);
  130. cFYI(1, "Sending smb: total_len %d", total_len);
  131. dump_smb(smb_buffer, len);
  132. i = 0;
  133. while (total_len) {
  134. rc = kernel_sendmsg(ssocket, &smb_msg, &iov[first_vec],
  135. n_vec - first_vec, total_len);
  136. if ((rc == -ENOSPC) || (rc == -EAGAIN)) {
  137. i++;
  138. /* if blocking send we try 3 times, since each can block
  139. for 5 seconds. For nonblocking we have to try more
  140. but wait increasing amounts of time allowing time for
  141. socket to clear. The overall time we wait in either
  142. case to send on the socket is about 15 seconds.
  143. Similarly we wait for 15 seconds for
  144. a response from the server in SendReceive[2]
  145. for the server to send a response back for
  146. most types of requests (except SMB Write
  147. past end of file which can be slow, and
  148. blocking lock operations). NFS waits slightly longer
  149. than CIFS, but this can make it take longer for
  150. nonresponsive servers to be detected and 15 seconds
  151. is more than enough time for modern networks to
  152. send a packet. In most cases if we fail to send
  153. after the retries we will kill the socket and
  154. reconnect which may clear the network problem.
  155. */
  156. if ((i >= 14) || (!server->noblocksnd && (i > 2))) {
  157. cERROR(1, "sends on sock %p stuck for 15 seconds",
  158. ssocket);
  159. rc = -EAGAIN;
  160. break;
  161. }
  162. msleep(1 << i);
  163. continue;
  164. }
  165. if (rc < 0)
  166. break;
  167. if (rc == total_len) {
  168. total_len = 0;
  169. break;
  170. } else if (rc > total_len) {
  171. cERROR(1, "sent %d requested %d", rc, total_len);
  172. break;
  173. }
  174. if (rc == 0) {
  175. /* should never happen, letting socket clear before
  176. retrying is our only obvious option here */
  177. cERROR(1, "tcp sent no data");
  178. msleep(500);
  179. continue;
  180. }
  181. total_len -= rc;
  182. /* the line below resets i */
  183. for (i = first_vec; i < n_vec; i++) {
  184. if (iov[i].iov_len) {
  185. if (rc > iov[i].iov_len) {
  186. rc -= iov[i].iov_len;
  187. iov[i].iov_len = 0;
  188. } else {
  189. iov[i].iov_base += rc;
  190. iov[i].iov_len -= rc;
  191. first_vec = i;
  192. break;
  193. }
  194. }
  195. }
  196. i = 0; /* in case we get ENOSPC on the next send */
  197. }
  198. if ((total_len > 0) && (total_len != smb_buf_length + 4)) {
  199. cFYI(1, "partial send (%d remaining), terminating session",
  200. total_len);
  201. /* If we have only sent part of an SMB then the next SMB
  202. could be taken as the remainder of this one. We need
  203. to kill the socket so the server throws away the partial
  204. SMB */
  205. server->tcpStatus = CifsNeedReconnect;
  206. }
  207. if (rc < 0) {
  208. cERROR(1, "Error %d sending data on socket to server", rc);
  209. } else
  210. rc = 0;
  211. /* Don't want to modify the buffer as a
  212. side effect of this call. */
  213. smb_buffer->smb_buf_length = smb_buf_length;
  214. return rc;
  215. }
  216. int
  217. smb_send(struct TCP_Server_Info *server, struct smb_hdr *smb_buffer,
  218. unsigned int smb_buf_length)
  219. {
  220. struct kvec iov;
  221. iov.iov_base = smb_buffer;
  222. iov.iov_len = smb_buf_length + 4;
  223. return smb_sendv(server, &iov, 1);
  224. }
  225. static int wait_for_free_request(struct TCP_Server_Info *server,
  226. const int long_op)
  227. {
  228. if (long_op == CIFS_ASYNC_OP) {
  229. /* oplock breaks must not be held up */
  230. atomic_inc(&server->inFlight);
  231. return 0;
  232. }
  233. spin_lock(&GlobalMid_Lock);
  234. while (1) {
  235. if (atomic_read(&server->inFlight) >= cifs_max_pending) {
  236. spin_unlock(&GlobalMid_Lock);
  237. #ifdef CONFIG_CIFS_STATS2
  238. atomic_inc(&server->num_waiters);
  239. #endif
  240. wait_event(server->request_q,
  241. atomic_read(&server->inFlight)
  242. < cifs_max_pending);
  243. #ifdef CONFIG_CIFS_STATS2
  244. atomic_dec(&server->num_waiters);
  245. #endif
  246. spin_lock(&GlobalMid_Lock);
  247. } else {
  248. if (server->tcpStatus == CifsExiting) {
  249. spin_unlock(&GlobalMid_Lock);
  250. return -ENOENT;
  251. }
  252. /* can not count locking commands against total
  253. as they are allowed to block on server */
  254. /* update # of requests on the wire to server */
  255. if (long_op != CIFS_BLOCKING_OP)
  256. atomic_inc(&server->inFlight);
  257. spin_unlock(&GlobalMid_Lock);
  258. break;
  259. }
  260. }
  261. return 0;
  262. }
  263. static int allocate_mid(struct cifsSesInfo *ses, struct smb_hdr *in_buf,
  264. struct mid_q_entry **ppmidQ)
  265. {
  266. if (ses->server->tcpStatus == CifsExiting) {
  267. return -ENOENT;
  268. }
  269. if (ses->server->tcpStatus == CifsNeedReconnect) {
  270. cFYI(1, "tcp session dead - return to caller to retry");
  271. return -EAGAIN;
  272. }
  273. if (ses->status != CifsGood) {
  274. /* check if SMB session is bad because we are setting it up */
  275. if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
  276. (in_buf->Command != SMB_COM_NEGOTIATE))
  277. return -EAGAIN;
  278. /* else ok - we are setting up session */
  279. }
  280. *ppmidQ = AllocMidQEntry(in_buf, ses->server);
  281. if (*ppmidQ == NULL)
  282. return -ENOMEM;
  283. spin_lock(&GlobalMid_Lock);
  284. list_add_tail(&(*ppmidQ)->qhead, &ses->server->pending_mid_q);
  285. spin_unlock(&GlobalMid_Lock);
  286. return 0;
  287. }
  288. static int
  289. wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *midQ)
  290. {
  291. int error;
  292. error = wait_event_killable(server->response_q,
  293. midQ->midState != MID_REQUEST_SUBMITTED);
  294. if (error < 0)
  295. return -ERESTARTSYS;
  296. return 0;
  297. }
  298. /*
  299. *
  300. * Send an SMB Request. No response info (other than return code)
  301. * needs to be parsed.
  302. *
  303. * flags indicate the type of request buffer and how long to wait
  304. * and whether to log NT STATUS code (error) before mapping it to POSIX error
  305. *
  306. */
  307. int
  308. SendReceiveNoRsp(const unsigned int xid, struct cifsSesInfo *ses,
  309. struct smb_hdr *in_buf, int flags)
  310. {
  311. int rc;
  312. struct kvec iov[1];
  313. int resp_buf_type;
  314. iov[0].iov_base = (char *)in_buf;
  315. iov[0].iov_len = in_buf->smb_buf_length + 4;
  316. flags |= CIFS_NO_RESP;
  317. rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags);
  318. cFYI(DBG2, "SendRcvNoRsp flags %d rc %d", flags, rc);
  319. return rc;
  320. }
  321. static int
  322. sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
  323. {
  324. int rc = 0;
  325. spin_lock(&GlobalMid_Lock);
  326. if (mid->resp_buf) {
  327. spin_unlock(&GlobalMid_Lock);
  328. return rc;
  329. }
  330. cERROR(1, "No response to cmd %d mid %d", mid->command, mid->mid);
  331. if (mid->midState == MID_REQUEST_SUBMITTED) {
  332. if (server->tcpStatus == CifsExiting)
  333. rc = -EHOSTDOWN;
  334. else
  335. mid->midState = MID_RETRY_NEEDED;
  336. }
  337. if (rc != -EHOSTDOWN) {
  338. if (mid->midState == MID_RETRY_NEEDED) {
  339. rc = -EAGAIN;
  340. cFYI(1, "marking request for retry");
  341. } else {
  342. rc = -EIO;
  343. }
  344. }
  345. spin_unlock(&GlobalMid_Lock);
  346. delete_mid(mid);
  347. return rc;
  348. }
  349. int
  350. SendReceive2(const unsigned int xid, struct cifsSesInfo *ses,
  351. struct kvec *iov, int n_vec, int *pRespBufType /* ret */,
  352. const int flags)
  353. {
  354. int rc = 0;
  355. int long_op;
  356. unsigned int receive_len;
  357. struct mid_q_entry *midQ;
  358. struct smb_hdr *in_buf = iov[0].iov_base;
  359. long_op = flags & CIFS_TIMEOUT_MASK;
  360. *pRespBufType = CIFS_NO_BUFFER; /* no response buf yet */
  361. if ((ses == NULL) || (ses->server == NULL)) {
  362. cifs_small_buf_release(in_buf);
  363. cERROR(1, "Null session");
  364. return -EIO;
  365. }
  366. if (ses->server->tcpStatus == CifsExiting) {
  367. cifs_small_buf_release(in_buf);
  368. return -ENOENT;
  369. }
  370. /* Ensure that we do not send more than 50 overlapping requests
  371. to the same server. We may make this configurable later or
  372. use ses->maxReq */
  373. rc = wait_for_free_request(ses->server, long_op);
  374. if (rc) {
  375. cifs_small_buf_release(in_buf);
  376. return rc;
  377. }
  378. /* make sure that we sign in the same order that we send on this socket
  379. and avoid races inside tcp sendmsg code that could cause corruption
  380. of smb data */
  381. mutex_lock(&ses->server->srv_mutex);
  382. rc = allocate_mid(ses, in_buf, &midQ);
  383. if (rc) {
  384. mutex_unlock(&ses->server->srv_mutex);
  385. cifs_small_buf_release(in_buf);
  386. /* Update # of requests on wire to server */
  387. atomic_dec(&ses->server->inFlight);
  388. wake_up(&ses->server->request_q);
  389. return rc;
  390. }
  391. rc = cifs_sign_smb2(iov, n_vec, ses->server, &midQ->sequence_number);
  392. if (rc) {
  393. mutex_unlock(&ses->server->srv_mutex);
  394. cifs_small_buf_release(in_buf);
  395. goto out;
  396. }
  397. midQ->midState = MID_REQUEST_SUBMITTED;
  398. #ifdef CONFIG_CIFS_STATS2
  399. atomic_inc(&ses->server->inSend);
  400. #endif
  401. rc = smb_sendv(ses->server, iov, n_vec);
  402. #ifdef CONFIG_CIFS_STATS2
  403. atomic_dec(&ses->server->inSend);
  404. midQ->when_sent = jiffies;
  405. #endif
  406. mutex_unlock(&ses->server->srv_mutex);
  407. cifs_small_buf_release(in_buf);
  408. if (rc < 0)
  409. goto out;
  410. if (long_op == CIFS_ASYNC_OP)
  411. goto out;
  412. rc = wait_for_response(ses->server, midQ);
  413. if (rc != 0)
  414. goto out;
  415. rc = sync_mid_result(midQ, ses->server);
  416. if (rc != 0) {
  417. atomic_dec(&ses->server->inFlight);
  418. wake_up(&ses->server->request_q);
  419. return rc;
  420. }
  421. receive_len = midQ->resp_buf->smb_buf_length;
  422. if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
  423. cERROR(1, "Frame too large received. Length: %d Xid: %d",
  424. receive_len, xid);
  425. rc = -EIO;
  426. goto out;
  427. }
  428. /* rcvd frame is ok */
  429. if (midQ->resp_buf &&
  430. (midQ->midState == MID_RESPONSE_RECEIVED)) {
  431. iov[0].iov_base = (char *)midQ->resp_buf;
  432. if (midQ->largeBuf)
  433. *pRespBufType = CIFS_LARGE_BUFFER;
  434. else
  435. *pRespBufType = CIFS_SMALL_BUFFER;
  436. iov[0].iov_len = receive_len + 4;
  437. dump_smb(midQ->resp_buf, 80);
  438. /* convert the length into a more usable form */
  439. if ((receive_len > 24) &&
  440. (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
  441. SECMODE_SIGN_ENABLED))) {
  442. rc = cifs_verify_signature(midQ->resp_buf,
  443. ses->server,
  444. midQ->sequence_number+1);
  445. if (rc) {
  446. cERROR(1, "Unexpected SMB signature");
  447. /* BB FIXME add code to kill session */
  448. }
  449. }
  450. /* BB special case reconnect tid and uid here? */
  451. rc = map_smb_to_linux_error(midQ->resp_buf,
  452. flags & CIFS_LOG_ERROR);
  453. /* convert ByteCount if necessary */
  454. if (receive_len >= sizeof(struct smb_hdr) - 4
  455. /* do not count RFC1001 header */ +
  456. (2 * midQ->resp_buf->WordCount) + 2 /* bcc */ )
  457. BCC(midQ->resp_buf) =
  458. le16_to_cpu(BCC_LE(midQ->resp_buf));
  459. if ((flags & CIFS_NO_RESP) == 0)
  460. midQ->resp_buf = NULL; /* mark it so buf will
  461. not be freed by
  462. delete_mid */
  463. } else {
  464. rc = -EIO;
  465. cFYI(1, "Bad MID state?");
  466. }
  467. out:
  468. delete_mid(midQ);
  469. atomic_dec(&ses->server->inFlight);
  470. wake_up(&ses->server->request_q);
  471. return rc;
  472. }
  473. int
  474. SendReceive(const unsigned int xid, struct cifsSesInfo *ses,
  475. struct smb_hdr *in_buf, struct smb_hdr *out_buf,
  476. int *pbytes_returned, const int long_op)
  477. {
  478. int rc = 0;
  479. unsigned int receive_len;
  480. struct mid_q_entry *midQ;
  481. if (ses == NULL) {
  482. cERROR(1, "Null smb session");
  483. return -EIO;
  484. }
  485. if (ses->server == NULL) {
  486. cERROR(1, "Null tcp session");
  487. return -EIO;
  488. }
  489. if (ses->server->tcpStatus == CifsExiting)
  490. return -ENOENT;
  491. /* Ensure that we do not send more than 50 overlapping requests
  492. to the same server. We may make this configurable later or
  493. use ses->maxReq */
  494. if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
  495. cERROR(1, "Illegal length, greater than maximum frame, %d",
  496. in_buf->smb_buf_length);
  497. return -EIO;
  498. }
  499. rc = wait_for_free_request(ses->server, long_op);
  500. if (rc)
  501. return rc;
  502. /* make sure that we sign in the same order that we send on this socket
  503. and avoid races inside tcp sendmsg code that could cause corruption
  504. of smb data */
  505. mutex_lock(&ses->server->srv_mutex);
  506. rc = allocate_mid(ses, in_buf, &midQ);
  507. if (rc) {
  508. mutex_unlock(&ses->server->srv_mutex);
  509. /* Update # of requests on wire to server */
  510. atomic_dec(&ses->server->inFlight);
  511. wake_up(&ses->server->request_q);
  512. return rc;
  513. }
  514. rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
  515. if (rc) {
  516. mutex_unlock(&ses->server->srv_mutex);
  517. goto out;
  518. }
  519. midQ->midState = MID_REQUEST_SUBMITTED;
  520. #ifdef CONFIG_CIFS_STATS2
  521. atomic_inc(&ses->server->inSend);
  522. #endif
  523. rc = smb_send(ses->server, in_buf, in_buf->smb_buf_length);
  524. #ifdef CONFIG_CIFS_STATS2
  525. atomic_dec(&ses->server->inSend);
  526. midQ->when_sent = jiffies;
  527. #endif
  528. mutex_unlock(&ses->server->srv_mutex);
  529. if (rc < 0)
  530. goto out;
  531. if (long_op == CIFS_ASYNC_OP)
  532. goto out;
  533. rc = wait_for_response(ses->server, midQ);
  534. if (rc != 0)
  535. goto out;
  536. rc = sync_mid_result(midQ, ses->server);
  537. if (rc != 0) {
  538. atomic_dec(&ses->server->inFlight);
  539. wake_up(&ses->server->request_q);
  540. return rc;
  541. }
  542. receive_len = midQ->resp_buf->smb_buf_length;
  543. if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
  544. cERROR(1, "Frame too large received. Length: %d Xid: %d",
  545. receive_len, xid);
  546. rc = -EIO;
  547. goto out;
  548. }
  549. /* rcvd frame is ok */
  550. if (midQ->resp_buf && out_buf
  551. && (midQ->midState == MID_RESPONSE_RECEIVED)) {
  552. out_buf->smb_buf_length = receive_len;
  553. memcpy((char *)out_buf + 4,
  554. (char *)midQ->resp_buf + 4,
  555. receive_len);
  556. dump_smb(out_buf, 92);
  557. /* convert the length into a more usable form */
  558. if ((receive_len > 24) &&
  559. (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
  560. SECMODE_SIGN_ENABLED))) {
  561. rc = cifs_verify_signature(out_buf,
  562. ses->server,
  563. midQ->sequence_number+1);
  564. if (rc) {
  565. cERROR(1, "Unexpected SMB signature");
  566. /* BB FIXME add code to kill session */
  567. }
  568. }
  569. *pbytes_returned = out_buf->smb_buf_length;
  570. /* BB special case reconnect tid and uid here? */
  571. rc = map_smb_to_linux_error(out_buf, 0 /* no log */ );
  572. /* convert ByteCount if necessary */
  573. if (receive_len >= sizeof(struct smb_hdr) - 4
  574. /* do not count RFC1001 header */ +
  575. (2 * out_buf->WordCount) + 2 /* bcc */ )
  576. BCC(out_buf) = le16_to_cpu(BCC_LE(out_buf));
  577. } else {
  578. rc = -EIO;
  579. cERROR(1, "Bad MID state?");
  580. }
  581. out:
  582. delete_mid(midQ);
  583. atomic_dec(&ses->server->inFlight);
  584. wake_up(&ses->server->request_q);
  585. return rc;
  586. }
  587. /* Send an NT_CANCEL SMB to cause the POSIX blocking lock to return. */
  588. static int
  589. send_nt_cancel(struct cifsTconInfo *tcon, struct smb_hdr *in_buf,
  590. struct mid_q_entry *midQ)
  591. {
  592. int rc = 0;
  593. struct cifsSesInfo *ses = tcon->ses;
  594. __u16 mid = in_buf->Mid;
  595. header_assemble(in_buf, SMB_COM_NT_CANCEL, tcon, 0);
  596. in_buf->Mid = mid;
  597. mutex_lock(&ses->server->srv_mutex);
  598. rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
  599. if (rc) {
  600. mutex_unlock(&ses->server->srv_mutex);
  601. return rc;
  602. }
  603. rc = smb_send(ses->server, in_buf, in_buf->smb_buf_length);
  604. mutex_unlock(&ses->server->srv_mutex);
  605. return rc;
  606. }
  607. /* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
  608. blocking lock to return. */
  609. static int
  610. send_lock_cancel(const unsigned int xid, struct cifsTconInfo *tcon,
  611. struct smb_hdr *in_buf,
  612. struct smb_hdr *out_buf)
  613. {
  614. int bytes_returned;
  615. struct cifsSesInfo *ses = tcon->ses;
  616. LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
  617. /* We just modify the current in_buf to change
  618. the type of lock from LOCKING_ANDX_SHARED_LOCK
  619. or LOCKING_ANDX_EXCLUSIVE_LOCK to
  620. LOCKING_ANDX_CANCEL_LOCK. */
  621. pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
  622. pSMB->Timeout = 0;
  623. pSMB->hdr.Mid = GetNextMid(ses->server);
  624. return SendReceive(xid, ses, in_buf, out_buf,
  625. &bytes_returned, CIFS_STD_OP);
  626. }
  627. int
  628. SendReceiveBlockingLock(const unsigned int xid, struct cifsTconInfo *tcon,
  629. struct smb_hdr *in_buf, struct smb_hdr *out_buf,
  630. int *pbytes_returned)
  631. {
  632. int rc = 0;
  633. int rstart = 0;
  634. unsigned int receive_len;
  635. struct mid_q_entry *midQ;
  636. struct cifsSesInfo *ses;
  637. if (tcon == NULL || tcon->ses == NULL) {
  638. cERROR(1, "Null smb session");
  639. return -EIO;
  640. }
  641. ses = tcon->ses;
  642. if (ses->server == NULL) {
  643. cERROR(1, "Null tcp session");
  644. return -EIO;
  645. }
  646. if (ses->server->tcpStatus == CifsExiting)
  647. return -ENOENT;
  648. /* Ensure that we do not send more than 50 overlapping requests
  649. to the same server. We may make this configurable later or
  650. use ses->maxReq */
  651. if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
  652. cERROR(1, "Illegal length, greater than maximum frame, %d",
  653. in_buf->smb_buf_length);
  654. return -EIO;
  655. }
  656. rc = wait_for_free_request(ses->server, CIFS_BLOCKING_OP);
  657. if (rc)
  658. return rc;
  659. /* make sure that we sign in the same order that we send on this socket
  660. and avoid races inside tcp sendmsg code that could cause corruption
  661. of smb data */
  662. mutex_lock(&ses->server->srv_mutex);
  663. rc = allocate_mid(ses, in_buf, &midQ);
  664. if (rc) {
  665. mutex_unlock(&ses->server->srv_mutex);
  666. return rc;
  667. }
  668. rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
  669. if (rc) {
  670. delete_mid(midQ);
  671. mutex_unlock(&ses->server->srv_mutex);
  672. return rc;
  673. }
  674. midQ->midState = MID_REQUEST_SUBMITTED;
  675. #ifdef CONFIG_CIFS_STATS2
  676. atomic_inc(&ses->server->inSend);
  677. #endif
  678. rc = smb_send(ses->server, in_buf, in_buf->smb_buf_length);
  679. #ifdef CONFIG_CIFS_STATS2
  680. atomic_dec(&ses->server->inSend);
  681. midQ->when_sent = jiffies;
  682. #endif
  683. mutex_unlock(&ses->server->srv_mutex);
  684. if (rc < 0) {
  685. delete_mid(midQ);
  686. return rc;
  687. }
  688. /* Wait for a reply - allow signals to interrupt. */
  689. rc = wait_event_interruptible(ses->server->response_q,
  690. (!(midQ->midState == MID_REQUEST_SUBMITTED)) ||
  691. ((ses->server->tcpStatus != CifsGood) &&
  692. (ses->server->tcpStatus != CifsNew)));
  693. /* Were we interrupted by a signal ? */
  694. if ((rc == -ERESTARTSYS) &&
  695. (midQ->midState == MID_REQUEST_SUBMITTED) &&
  696. ((ses->server->tcpStatus == CifsGood) ||
  697. (ses->server->tcpStatus == CifsNew))) {
  698. if (in_buf->Command == SMB_COM_TRANSACTION2) {
  699. /* POSIX lock. We send a NT_CANCEL SMB to cause the
  700. blocking lock to return. */
  701. rc = send_nt_cancel(tcon, in_buf, midQ);
  702. if (rc) {
  703. delete_mid(midQ);
  704. return rc;
  705. }
  706. } else {
  707. /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
  708. to cause the blocking lock to return. */
  709. rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
  710. /* If we get -ENOLCK back the lock may have
  711. already been removed. Don't exit in this case. */
  712. if (rc && rc != -ENOLCK) {
  713. delete_mid(midQ);
  714. return rc;
  715. }
  716. }
  717. if (wait_for_response(ses->server, midQ) == 0) {
  718. /* We got the response - restart system call. */
  719. rstart = 1;
  720. }
  721. }
  722. rc = sync_mid_result(midQ, ses->server);
  723. if (rc != 0)
  724. return rc;
  725. receive_len = midQ->resp_buf->smb_buf_length;
  726. if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
  727. cERROR(1, "Frame too large received. Length: %d Xid: %d",
  728. receive_len, xid);
  729. rc = -EIO;
  730. goto out;
  731. }
  732. /* rcvd frame is ok */
  733. if ((out_buf == NULL) || (midQ->midState != MID_RESPONSE_RECEIVED)) {
  734. rc = -EIO;
  735. cERROR(1, "Bad MID state?");
  736. goto out;
  737. }
  738. out_buf->smb_buf_length = receive_len;
  739. memcpy((char *)out_buf + 4,
  740. (char *)midQ->resp_buf + 4,
  741. receive_len);
  742. dump_smb(out_buf, 92);
  743. /* convert the length into a more usable form */
  744. if ((receive_len > 24) &&
  745. (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
  746. SECMODE_SIGN_ENABLED))) {
  747. rc = cifs_verify_signature(out_buf,
  748. ses->server,
  749. midQ->sequence_number+1);
  750. if (rc) {
  751. cERROR(1, "Unexpected SMB signature");
  752. /* BB FIXME add code to kill session */
  753. }
  754. }
  755. *pbytes_returned = out_buf->smb_buf_length;
  756. /* BB special case reconnect tid and uid here? */
  757. rc = map_smb_to_linux_error(out_buf, 0 /* no log */ );
  758. /* convert ByteCount if necessary */
  759. if (receive_len >= sizeof(struct smb_hdr) - 4
  760. /* do not count RFC1001 header */ +
  761. (2 * out_buf->WordCount) + 2 /* bcc */ )
  762. BCC(out_buf) = le16_to_cpu(BCC_LE(out_buf));
  763. out:
  764. delete_mid(midQ);
  765. if (rstart && rc == -EACCES)
  766. return -ERESTARTSYS;
  767. return rc;
  768. }