transport.c 24 KB

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