transport.c 24 KB

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