transport.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  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 = cpu_to_le16(smb_buffer->Command);
  58. cFYI(1, "For smb_command %d", smb_buffer->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->mid_state = 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->mid_state = MID_FREE;
  80. atomic_dec(&midCount);
  81. if (midEntry->large_buf)
  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 != cpu_to_le16(SMB_COM_LOCKING_ANDX))) {
  92. printk(KERN_DEBUG " CIFS slow rsp: cmd %d mid %llu",
  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. __be32 *buf_len = (__be32 *)(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 = get_rfc1002_length(iov[0].iov_base);
  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(iov[0].iov_base, 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. /*
  145. * If blocking send we try 3 times, since each can block
  146. * for 5 seconds. For nonblocking we have to try more
  147. * but wait increasing amounts of time allowing time for
  148. * socket to clear. The overall time we wait in either
  149. * case to send on the socket is about 15 seconds.
  150. * Similarly we wait for 15 seconds for a response from
  151. * the server in SendReceive[2] for the server to send
  152. * a response back for most types of requests (except
  153. * SMB Write 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 side effect of this call. */
  218. *buf_len = 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
  231. wait_for_free_credits(struct TCP_Server_Info *server, const int optype,
  232. int *credits)
  233. {
  234. int rc;
  235. spin_lock(&server->req_lock);
  236. if (optype == CIFS_ASYNC_OP) {
  237. /* oplock breaks must not be held up */
  238. server->in_flight++;
  239. *credits -= 1;
  240. spin_unlock(&server->req_lock);
  241. return 0;
  242. }
  243. while (1) {
  244. if (*credits <= 0) {
  245. spin_unlock(&server->req_lock);
  246. cifs_num_waiters_inc(server);
  247. rc = wait_event_killable(server->request_q,
  248. has_credits(server, credits));
  249. cifs_num_waiters_dec(server);
  250. if (rc)
  251. return rc;
  252. spin_lock(&server->req_lock);
  253. } else {
  254. if (server->tcpStatus == CifsExiting) {
  255. spin_unlock(&server->req_lock);
  256. return -ENOENT;
  257. }
  258. /*
  259. * Can not count locking commands against total
  260. * as they are allowed to block on server.
  261. */
  262. /* update # of requests on the wire to server */
  263. if (optype != CIFS_BLOCKING_OP) {
  264. *credits -= 1;
  265. server->in_flight++;
  266. }
  267. spin_unlock(&server->req_lock);
  268. break;
  269. }
  270. }
  271. return 0;
  272. }
  273. static int
  274. wait_for_free_request(struct TCP_Server_Info *server, const int optype)
  275. {
  276. return wait_for_free_credits(server, optype,
  277. server->ops->get_credits_field(server));
  278. }
  279. static int allocate_mid(struct cifs_ses *ses, struct smb_hdr *in_buf,
  280. struct mid_q_entry **ppmidQ)
  281. {
  282. if (ses->server->tcpStatus == CifsExiting) {
  283. return -ENOENT;
  284. }
  285. if (ses->server->tcpStatus == CifsNeedReconnect) {
  286. cFYI(1, "tcp session dead - return to caller to retry");
  287. return -EAGAIN;
  288. }
  289. if (ses->status != CifsGood) {
  290. /* check if SMB session is bad because we are setting it up */
  291. if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
  292. (in_buf->Command != SMB_COM_NEGOTIATE))
  293. return -EAGAIN;
  294. /* else ok - we are setting up session */
  295. }
  296. *ppmidQ = AllocMidQEntry(in_buf, ses->server);
  297. if (*ppmidQ == NULL)
  298. return -ENOMEM;
  299. spin_lock(&GlobalMid_Lock);
  300. list_add_tail(&(*ppmidQ)->qhead, &ses->server->pending_mid_q);
  301. spin_unlock(&GlobalMid_Lock);
  302. return 0;
  303. }
  304. static int
  305. wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *midQ)
  306. {
  307. int error;
  308. error = wait_event_freezekillable(server->response_q,
  309. midQ->mid_state != MID_REQUEST_SUBMITTED);
  310. if (error < 0)
  311. return -ERESTARTSYS;
  312. return 0;
  313. }
  314. static int
  315. cifs_setup_async_request(struct TCP_Server_Info *server, struct kvec *iov,
  316. unsigned int nvec, struct mid_q_entry **ret_mid)
  317. {
  318. int rc;
  319. struct smb_hdr *hdr = (struct smb_hdr *)iov[0].iov_base;
  320. struct mid_q_entry *mid;
  321. /* enable signing if server requires it */
  322. if (server->sec_mode & (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
  323. hdr->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
  324. mid = AllocMidQEntry(hdr, server);
  325. if (mid == NULL)
  326. return -ENOMEM;
  327. /* put it on the pending_mid_q */
  328. spin_lock(&GlobalMid_Lock);
  329. list_add_tail(&mid->qhead, &server->pending_mid_q);
  330. spin_unlock(&GlobalMid_Lock);
  331. rc = cifs_sign_smb2(iov, nvec, server, &mid->sequence_number);
  332. if (rc)
  333. delete_mid(mid);
  334. *ret_mid = mid;
  335. return rc;
  336. }
  337. /*
  338. * Send a SMB request and set the callback function in the mid to handle
  339. * the result. Caller is responsible for dealing with timeouts.
  340. */
  341. int
  342. cifs_call_async(struct TCP_Server_Info *server, struct kvec *iov,
  343. unsigned int nvec, mid_receive_t *receive,
  344. mid_callback_t *callback, void *cbdata, bool ignore_pend)
  345. {
  346. int rc;
  347. struct mid_q_entry *mid;
  348. rc = wait_for_free_request(server, ignore_pend ? CIFS_ASYNC_OP : 0);
  349. if (rc)
  350. return rc;
  351. mutex_lock(&server->srv_mutex);
  352. rc = cifs_setup_async_request(server, iov, nvec, &mid);
  353. if (rc) {
  354. mutex_unlock(&server->srv_mutex);
  355. add_credits(server, 1);
  356. wake_up(&server->request_q);
  357. return rc;
  358. }
  359. mid->receive = receive;
  360. mid->callback = callback;
  361. mid->callback_data = cbdata;
  362. mid->mid_state = MID_REQUEST_SUBMITTED;
  363. cifs_in_send_inc(server);
  364. rc = smb_sendv(server, iov, nvec);
  365. cifs_in_send_dec(server);
  366. cifs_save_when_sent(mid);
  367. mutex_unlock(&server->srv_mutex);
  368. if (rc)
  369. goto out_err;
  370. return rc;
  371. out_err:
  372. delete_mid(mid);
  373. add_credits(server, 1);
  374. wake_up(&server->request_q);
  375. return rc;
  376. }
  377. /*
  378. *
  379. * Send an SMB Request. No response info (other than return code)
  380. * needs to be parsed.
  381. *
  382. * flags indicate the type of request buffer and how long to wait
  383. * and whether to log NT STATUS code (error) before mapping it to POSIX error
  384. *
  385. */
  386. int
  387. SendReceiveNoRsp(const unsigned int xid, struct cifs_ses *ses,
  388. char *in_buf, int flags)
  389. {
  390. int rc;
  391. struct kvec iov[1];
  392. int resp_buf_type;
  393. iov[0].iov_base = in_buf;
  394. iov[0].iov_len = get_rfc1002_length(in_buf) + 4;
  395. flags |= CIFS_NO_RESP;
  396. rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags);
  397. cFYI(DBG2, "SendRcvNoRsp flags %d rc %d", flags, rc);
  398. return rc;
  399. }
  400. static int
  401. cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
  402. {
  403. int rc = 0;
  404. cFYI(1, "%s: cmd=%d mid=%llu state=%d", __func__,
  405. le16_to_cpu(mid->command), mid->mid, mid->mid_state);
  406. spin_lock(&GlobalMid_Lock);
  407. switch (mid->mid_state) {
  408. case MID_RESPONSE_RECEIVED:
  409. spin_unlock(&GlobalMid_Lock);
  410. return rc;
  411. case MID_RETRY_NEEDED:
  412. rc = -EAGAIN;
  413. break;
  414. case MID_RESPONSE_MALFORMED:
  415. rc = -EIO;
  416. break;
  417. case MID_SHUTDOWN:
  418. rc = -EHOSTDOWN;
  419. break;
  420. default:
  421. list_del_init(&mid->qhead);
  422. cERROR(1, "%s: invalid mid state mid=%llu state=%d", __func__,
  423. mid->mid, mid->mid_state);
  424. rc = -EIO;
  425. }
  426. spin_unlock(&GlobalMid_Lock);
  427. DeleteMidQEntry(mid);
  428. return rc;
  429. }
  430. static inline int
  431. send_cancel(struct TCP_Server_Info *server, void *buf, struct mid_q_entry *mid)
  432. {
  433. return server->ops->send_cancel ?
  434. server->ops->send_cancel(server, buf, mid) : 0;
  435. }
  436. int
  437. cifs_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
  438. bool log_error)
  439. {
  440. unsigned int len = get_rfc1002_length(mid->resp_buf) + 4;
  441. dump_smb(mid->resp_buf, min_t(u32, 92, len));
  442. /* convert the length into a more usable form */
  443. if (server->sec_mode & (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED)) {
  444. struct kvec iov;
  445. iov.iov_base = mid->resp_buf;
  446. iov.iov_len = len;
  447. /* FIXME: add code to kill session */
  448. if (cifs_verify_signature(&iov, 1, server,
  449. mid->sequence_number + 1) != 0)
  450. cERROR(1, "Unexpected SMB signature");
  451. }
  452. /* BB special case reconnect tid and uid here? */
  453. return map_smb_to_linux_error(mid->resp_buf, log_error);
  454. }
  455. int
  456. cifs_setup_request(struct cifs_ses *ses, struct kvec *iov,
  457. unsigned int nvec, struct mid_q_entry **ret_mid)
  458. {
  459. int rc;
  460. struct smb_hdr *hdr = (struct smb_hdr *)iov[0].iov_base;
  461. struct mid_q_entry *mid;
  462. rc = allocate_mid(ses, hdr, &mid);
  463. if (rc)
  464. return rc;
  465. rc = cifs_sign_smb2(iov, nvec, ses->server, &mid->sequence_number);
  466. if (rc)
  467. delete_mid(mid);
  468. *ret_mid = mid;
  469. return rc;
  470. }
  471. int
  472. SendReceive2(const unsigned int xid, struct cifs_ses *ses,
  473. struct kvec *iov, int n_vec, int *pRespBufType /* ret */,
  474. const int flags)
  475. {
  476. int rc = 0;
  477. int long_op;
  478. struct mid_q_entry *midQ;
  479. char *buf = iov[0].iov_base;
  480. long_op = flags & CIFS_TIMEOUT_MASK;
  481. *pRespBufType = CIFS_NO_BUFFER; /* no response buf yet */
  482. if ((ses == NULL) || (ses->server == NULL)) {
  483. cifs_small_buf_release(buf);
  484. cERROR(1, "Null session");
  485. return -EIO;
  486. }
  487. if (ses->server->tcpStatus == CifsExiting) {
  488. cifs_small_buf_release(buf);
  489. return -ENOENT;
  490. }
  491. /*
  492. * Ensure that we do not send more than 50 overlapping requests
  493. * to the same server. We may make this configurable later or
  494. * use ses->maxReq.
  495. */
  496. rc = wait_for_free_request(ses->server, long_op);
  497. if (rc) {
  498. cifs_small_buf_release(buf);
  499. return rc;
  500. }
  501. /*
  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. */
  506. mutex_lock(&ses->server->srv_mutex);
  507. rc = ses->server->ops->setup_request(ses, iov, n_vec, &midQ);
  508. if (rc) {
  509. mutex_unlock(&ses->server->srv_mutex);
  510. cifs_small_buf_release(buf);
  511. /* Update # of requests on wire to server */
  512. add_credits(ses->server, 1);
  513. return rc;
  514. }
  515. midQ->mid_state = MID_REQUEST_SUBMITTED;
  516. cifs_in_send_inc(ses->server);
  517. rc = smb_sendv(ses->server, iov, n_vec);
  518. cifs_in_send_dec(ses->server);
  519. cifs_save_when_sent(midQ);
  520. mutex_unlock(&ses->server->srv_mutex);
  521. if (rc < 0) {
  522. cifs_small_buf_release(buf);
  523. goto out;
  524. }
  525. if (long_op == CIFS_ASYNC_OP) {
  526. cifs_small_buf_release(buf);
  527. goto out;
  528. }
  529. rc = wait_for_response(ses->server, midQ);
  530. if (rc != 0) {
  531. send_cancel(ses->server, buf, midQ);
  532. spin_lock(&GlobalMid_Lock);
  533. if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
  534. midQ->callback = DeleteMidQEntry;
  535. spin_unlock(&GlobalMid_Lock);
  536. cifs_small_buf_release(buf);
  537. add_credits(ses->server, 1);
  538. return rc;
  539. }
  540. spin_unlock(&GlobalMid_Lock);
  541. }
  542. cifs_small_buf_release(buf);
  543. rc = cifs_sync_mid_result(midQ, ses->server);
  544. if (rc != 0) {
  545. add_credits(ses->server, 1);
  546. return rc;
  547. }
  548. if (!midQ->resp_buf || midQ->mid_state != MID_RESPONSE_RECEIVED) {
  549. rc = -EIO;
  550. cFYI(1, "Bad MID state?");
  551. goto out;
  552. }
  553. buf = (char *)midQ->resp_buf;
  554. iov[0].iov_base = buf;
  555. iov[0].iov_len = get_rfc1002_length(buf) + 4;
  556. if (midQ->large_buf)
  557. *pRespBufType = CIFS_LARGE_BUFFER;
  558. else
  559. *pRespBufType = CIFS_SMALL_BUFFER;
  560. rc = ses->server->ops->check_receive(midQ, ses->server,
  561. flags & CIFS_LOG_ERROR);
  562. /* mark it so buf will not be freed by delete_mid */
  563. if ((flags & CIFS_NO_RESP) == 0)
  564. midQ->resp_buf = NULL;
  565. out:
  566. delete_mid(midQ);
  567. add_credits(ses->server, 1);
  568. return rc;
  569. }
  570. int
  571. SendReceive(const unsigned int xid, struct cifs_ses *ses,
  572. struct smb_hdr *in_buf, struct smb_hdr *out_buf,
  573. int *pbytes_returned, const int long_op)
  574. {
  575. int rc = 0;
  576. struct mid_q_entry *midQ;
  577. if (ses == NULL) {
  578. cERROR(1, "Null smb session");
  579. return -EIO;
  580. }
  581. if (ses->server == NULL) {
  582. cERROR(1, "Null tcp session");
  583. return -EIO;
  584. }
  585. if (ses->server->tcpStatus == CifsExiting)
  586. return -ENOENT;
  587. /* Ensure that we do not send more than 50 overlapping requests
  588. to the same server. We may make this configurable later or
  589. use ses->maxReq */
  590. if (be32_to_cpu(in_buf->smb_buf_length) > CIFSMaxBufSize +
  591. MAX_CIFS_HDR_SIZE - 4) {
  592. cERROR(1, "Illegal length, greater than maximum frame, %d",
  593. be32_to_cpu(in_buf->smb_buf_length));
  594. return -EIO;
  595. }
  596. rc = wait_for_free_request(ses->server, long_op);
  597. if (rc)
  598. return rc;
  599. /* make sure that we sign in the same order that we send on this socket
  600. and avoid races inside tcp sendmsg code that could cause corruption
  601. of smb data */
  602. mutex_lock(&ses->server->srv_mutex);
  603. rc = allocate_mid(ses, in_buf, &midQ);
  604. if (rc) {
  605. mutex_unlock(&ses->server->srv_mutex);
  606. /* Update # of requests on wire to server */
  607. add_credits(ses->server, 1);
  608. return rc;
  609. }
  610. rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
  611. if (rc) {
  612. mutex_unlock(&ses->server->srv_mutex);
  613. goto out;
  614. }
  615. midQ->mid_state = MID_REQUEST_SUBMITTED;
  616. cifs_in_send_inc(ses->server);
  617. rc = smb_send(ses->server, in_buf, be32_to_cpu(in_buf->smb_buf_length));
  618. cifs_in_send_dec(ses->server);
  619. cifs_save_when_sent(midQ);
  620. mutex_unlock(&ses->server->srv_mutex);
  621. if (rc < 0)
  622. goto out;
  623. if (long_op == CIFS_ASYNC_OP)
  624. goto out;
  625. rc = wait_for_response(ses->server, midQ);
  626. if (rc != 0) {
  627. send_cancel(ses->server, in_buf, midQ);
  628. spin_lock(&GlobalMid_Lock);
  629. if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
  630. /* no longer considered to be "in-flight" */
  631. midQ->callback = DeleteMidQEntry;
  632. spin_unlock(&GlobalMid_Lock);
  633. add_credits(ses->server, 1);
  634. return rc;
  635. }
  636. spin_unlock(&GlobalMid_Lock);
  637. }
  638. rc = cifs_sync_mid_result(midQ, ses->server);
  639. if (rc != 0) {
  640. add_credits(ses->server, 1);
  641. return rc;
  642. }
  643. if (!midQ->resp_buf || !out_buf ||
  644. midQ->mid_state != MID_RESPONSE_RECEIVED) {
  645. rc = -EIO;
  646. cERROR(1, "Bad MID state?");
  647. goto out;
  648. }
  649. *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
  650. memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
  651. rc = cifs_check_receive(midQ, ses->server, 0);
  652. out:
  653. delete_mid(midQ);
  654. add_credits(ses->server, 1);
  655. return rc;
  656. }
  657. /* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
  658. blocking lock to return. */
  659. static int
  660. send_lock_cancel(const unsigned int xid, struct cifs_tcon *tcon,
  661. struct smb_hdr *in_buf,
  662. struct smb_hdr *out_buf)
  663. {
  664. int bytes_returned;
  665. struct cifs_ses *ses = tcon->ses;
  666. LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
  667. /* We just modify the current in_buf to change
  668. the type of lock from LOCKING_ANDX_SHARED_LOCK
  669. or LOCKING_ANDX_EXCLUSIVE_LOCK to
  670. LOCKING_ANDX_CANCEL_LOCK. */
  671. pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
  672. pSMB->Timeout = 0;
  673. pSMB->hdr.Mid = get_next_mid(ses->server);
  674. return SendReceive(xid, ses, in_buf, out_buf,
  675. &bytes_returned, 0);
  676. }
  677. int
  678. SendReceiveBlockingLock(const unsigned int xid, struct cifs_tcon *tcon,
  679. struct smb_hdr *in_buf, struct smb_hdr *out_buf,
  680. int *pbytes_returned)
  681. {
  682. int rc = 0;
  683. int rstart = 0;
  684. struct mid_q_entry *midQ;
  685. struct cifs_ses *ses;
  686. if (tcon == NULL || tcon->ses == NULL) {
  687. cERROR(1, "Null smb session");
  688. return -EIO;
  689. }
  690. ses = tcon->ses;
  691. if (ses->server == NULL) {
  692. cERROR(1, "Null tcp session");
  693. return -EIO;
  694. }
  695. if (ses->server->tcpStatus == CifsExiting)
  696. return -ENOENT;
  697. /* Ensure that we do not send more than 50 overlapping requests
  698. to the same server. We may make this configurable later or
  699. use ses->maxReq */
  700. if (be32_to_cpu(in_buf->smb_buf_length) > CIFSMaxBufSize +
  701. MAX_CIFS_HDR_SIZE - 4) {
  702. cERROR(1, "Illegal length, greater than maximum frame, %d",
  703. be32_to_cpu(in_buf->smb_buf_length));
  704. return -EIO;
  705. }
  706. rc = wait_for_free_request(ses->server, CIFS_BLOCKING_OP);
  707. if (rc)
  708. return rc;
  709. /* make sure that we sign in the same order that we send on this socket
  710. and avoid races inside tcp sendmsg code that could cause corruption
  711. of smb data */
  712. mutex_lock(&ses->server->srv_mutex);
  713. rc = allocate_mid(ses, in_buf, &midQ);
  714. if (rc) {
  715. mutex_unlock(&ses->server->srv_mutex);
  716. return rc;
  717. }
  718. rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
  719. if (rc) {
  720. delete_mid(midQ);
  721. mutex_unlock(&ses->server->srv_mutex);
  722. return rc;
  723. }
  724. midQ->mid_state = MID_REQUEST_SUBMITTED;
  725. cifs_in_send_inc(ses->server);
  726. rc = smb_send(ses->server, in_buf, be32_to_cpu(in_buf->smb_buf_length));
  727. cifs_in_send_dec(ses->server);
  728. cifs_save_when_sent(midQ);
  729. mutex_unlock(&ses->server->srv_mutex);
  730. if (rc < 0) {
  731. delete_mid(midQ);
  732. return rc;
  733. }
  734. /* Wait for a reply - allow signals to interrupt. */
  735. rc = wait_event_interruptible(ses->server->response_q,
  736. (!(midQ->mid_state == MID_REQUEST_SUBMITTED)) ||
  737. ((ses->server->tcpStatus != CifsGood) &&
  738. (ses->server->tcpStatus != CifsNew)));
  739. /* Were we interrupted by a signal ? */
  740. if ((rc == -ERESTARTSYS) &&
  741. (midQ->mid_state == MID_REQUEST_SUBMITTED) &&
  742. ((ses->server->tcpStatus == CifsGood) ||
  743. (ses->server->tcpStatus == CifsNew))) {
  744. if (in_buf->Command == SMB_COM_TRANSACTION2) {
  745. /* POSIX lock. We send a NT_CANCEL SMB to cause the
  746. blocking lock to return. */
  747. rc = send_cancel(ses->server, in_buf, midQ);
  748. if (rc) {
  749. delete_mid(midQ);
  750. return rc;
  751. }
  752. } else {
  753. /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
  754. to cause the blocking lock to return. */
  755. rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
  756. /* If we get -ENOLCK back the lock may have
  757. already been removed. Don't exit in this case. */
  758. if (rc && rc != -ENOLCK) {
  759. delete_mid(midQ);
  760. return rc;
  761. }
  762. }
  763. rc = wait_for_response(ses->server, midQ);
  764. if (rc) {
  765. send_cancel(ses->server, in_buf, midQ);
  766. spin_lock(&GlobalMid_Lock);
  767. if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
  768. /* no longer considered to be "in-flight" */
  769. midQ->callback = DeleteMidQEntry;
  770. spin_unlock(&GlobalMid_Lock);
  771. return rc;
  772. }
  773. spin_unlock(&GlobalMid_Lock);
  774. }
  775. /* We got the response - restart system call. */
  776. rstart = 1;
  777. }
  778. rc = cifs_sync_mid_result(midQ, ses->server);
  779. if (rc != 0)
  780. return rc;
  781. /* rcvd frame is ok */
  782. if (out_buf == NULL || midQ->mid_state != MID_RESPONSE_RECEIVED) {
  783. rc = -EIO;
  784. cERROR(1, "Bad MID state?");
  785. goto out;
  786. }
  787. *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
  788. memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
  789. rc = cifs_check_receive(midQ, ses->server, 0);
  790. out:
  791. delete_mid(midQ);
  792. if (rstart && rc == -EACCES)
  793. return -ERESTARTSYS;
  794. return rc;
  795. }