transport.c 26 KB

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