transport.c 28 KB

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