transport.c 23 KB

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