transport.c 25 KB

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