transport.c 24 KB

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