transport.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  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. cERROR(1, "Null TCP session in AllocMidQEntry");
  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 = smb_buffer->Mid; /* always LE */
  57. temp->pid = current->pid;
  58. temp->command = cpu_to_le16(smb_buffer->Command);
  59. cFYI(1, "For smb_command %d", 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. printk(KERN_DEBUG " CIFS slow rsp: cmd %d mid %llu",
  95. midEntry->command, midEntry->mid);
  96. printk(" 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. if (ssocket == NULL)
  135. return -ENOTSOCK; /* BB eventually add reconnect code here */
  136. smb_msg.msg_name = (struct sockaddr *) &server->dstaddr;
  137. smb_msg.msg_namelen = sizeof(struct sockaddr);
  138. smb_msg.msg_control = NULL;
  139. smb_msg.msg_controllen = 0;
  140. if (server->noblocksnd)
  141. smb_msg.msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL;
  142. else
  143. smb_msg.msg_flags = MSG_NOSIGNAL;
  144. remaining = 0;
  145. for (i = 0; i < n_vec; i++)
  146. remaining += iov[i].iov_len;
  147. i = 0;
  148. while (remaining) {
  149. /*
  150. * If blocking send, we try 3 times, since each can block
  151. * for 5 seconds. For nonblocking we have to try more
  152. * but wait increasing amounts of time allowing time for
  153. * socket to clear. The overall time we wait in either
  154. * case to send on the socket is about 15 seconds.
  155. * Similarly we wait for 15 seconds for a response from
  156. * the server in SendReceive[2] for the server to send
  157. * a response back for most types of requests (except
  158. * SMB Write past end of file which can be slow, and
  159. * blocking lock operations). NFS waits slightly longer
  160. * than CIFS, but this can make it take longer for
  161. * nonresponsive servers to be detected and 15 seconds
  162. * is more than enough time for modern networks to
  163. * send a packet. In most cases if we fail to send
  164. * after the retries we will kill the socket and
  165. * reconnect which may clear the network problem.
  166. */
  167. rc = kernel_sendmsg(ssocket, &smb_msg, &iov[first_vec],
  168. n_vec - first_vec, remaining);
  169. if (rc == -ENOSPC || rc == -EAGAIN) {
  170. i++;
  171. if (i >= 14 || (!server->noblocksnd && (i > 2))) {
  172. cERROR(1, "sends on sock %p stuck for 15 "
  173. "seconds", ssocket);
  174. rc = -EAGAIN;
  175. break;
  176. }
  177. msleep(1 << i);
  178. continue;
  179. }
  180. if (rc < 0)
  181. break;
  182. /* send was at least partially successful */
  183. *sent += rc;
  184. if (rc == remaining) {
  185. remaining = 0;
  186. break;
  187. }
  188. if (rc > remaining) {
  189. cERROR(1, "sent %d requested %d", rc, remaining);
  190. break;
  191. }
  192. if (rc == 0) {
  193. /* should never happen, letting socket clear before
  194. retrying is our only obvious option here */
  195. cERROR(1, "tcp sent no data");
  196. msleep(500);
  197. continue;
  198. }
  199. remaining -= rc;
  200. /* the line below resets i */
  201. for (i = first_vec; i < n_vec; i++) {
  202. if (iov[i].iov_len) {
  203. if (rc > iov[i].iov_len) {
  204. rc -= iov[i].iov_len;
  205. iov[i].iov_len = 0;
  206. } else {
  207. iov[i].iov_base += rc;
  208. iov[i].iov_len -= rc;
  209. first_vec = i;
  210. break;
  211. }
  212. }
  213. }
  214. i = 0; /* in case we get ENOSPC on the next send */
  215. rc = 0;
  216. }
  217. return rc;
  218. }
  219. /**
  220. * rqst_page_to_kvec - Turn a slot in the smb_rqst page array into a kvec
  221. * @rqst: pointer to smb_rqst
  222. * @idx: index into the array of the page
  223. * @iov: pointer to struct kvec that will hold the result
  224. *
  225. * Helper function to convert a slot in the rqst->rq_pages array into a kvec.
  226. * The page will be kmapped and the address placed into iov_base. The length
  227. * will then be adjusted according to the ptailoff.
  228. */
  229. static void
  230. cifs_rqst_page_to_kvec(struct smb_rqst *rqst, unsigned int idx,
  231. struct kvec *iov)
  232. {
  233. /*
  234. * FIXME: We could avoid this kmap altogether if we used
  235. * kernel_sendpage instead of kernel_sendmsg. That will only
  236. * work if signing is disabled though as sendpage inlines the
  237. * page directly into the fraglist. If userspace modifies the
  238. * page after we calculate the signature, then the server will
  239. * reject it and may break the connection. kernel_sendmsg does
  240. * an extra copy of the data and avoids that issue.
  241. */
  242. iov->iov_base = kmap(rqst->rq_pages[idx]);
  243. /* if last page, don't send beyond this offset into page */
  244. if (idx == (rqst->rq_npages - 1))
  245. iov->iov_len = rqst->rq_tailsz;
  246. else
  247. iov->iov_len = rqst->rq_pagesz;
  248. }
  249. static int
  250. smb_send_rqst(struct TCP_Server_Info *server, struct smb_rqst *rqst)
  251. {
  252. int rc;
  253. struct kvec *iov = rqst->rq_iov;
  254. int n_vec = rqst->rq_nvec;
  255. unsigned int smb_buf_length = get_rfc1002_length(iov[0].iov_base);
  256. unsigned int i;
  257. size_t total_len = 0, sent;
  258. struct socket *ssocket = server->ssocket;
  259. int val = 1;
  260. cFYI(1, "Sending smb: smb_len=%u", smb_buf_length);
  261. dump_smb(iov[0].iov_base, iov[0].iov_len);
  262. /* cork the socket */
  263. kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
  264. (char *)&val, sizeof(val));
  265. rc = smb_send_kvec(server, iov, n_vec, &sent);
  266. if (rc < 0)
  267. goto uncork;
  268. total_len += sent;
  269. /* now walk the page array and send each page in it */
  270. for (i = 0; i < rqst->rq_npages; i++) {
  271. struct kvec p_iov;
  272. cifs_rqst_page_to_kvec(rqst, i, &p_iov);
  273. rc = smb_send_kvec(server, &p_iov, 1, &sent);
  274. kunmap(rqst->rq_pages[i]);
  275. if (rc < 0)
  276. break;
  277. total_len += sent;
  278. }
  279. uncork:
  280. /* uncork it */
  281. val = 0;
  282. kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
  283. (char *)&val, sizeof(val));
  284. if ((total_len > 0) && (total_len != smb_buf_length + 4)) {
  285. cFYI(1, "partial send (wanted=%u sent=%zu): terminating "
  286. "session", smb_buf_length + 4, total_len);
  287. /*
  288. * If we have only sent part of an SMB then the next SMB could
  289. * be taken as the remainder of this one. We need to kill the
  290. * socket so the server throws away the partial SMB
  291. */
  292. server->tcpStatus = CifsNeedReconnect;
  293. }
  294. if (rc < 0 && rc != -EINTR)
  295. cERROR(1, "Error %d sending data on socket to server", rc);
  296. else
  297. rc = 0;
  298. return rc;
  299. }
  300. static int
  301. smb_sendv(struct TCP_Server_Info *server, struct kvec *iov, int n_vec)
  302. {
  303. struct smb_rqst rqst = { .rq_iov = iov,
  304. .rq_nvec = n_vec };
  305. return smb_send_rqst(server, &rqst);
  306. }
  307. int
  308. smb_send(struct TCP_Server_Info *server, struct smb_hdr *smb_buffer,
  309. unsigned int smb_buf_length)
  310. {
  311. struct kvec iov;
  312. iov.iov_base = smb_buffer;
  313. iov.iov_len = smb_buf_length + 4;
  314. return smb_sendv(server, &iov, 1);
  315. }
  316. static int
  317. wait_for_free_credits(struct TCP_Server_Info *server, const int timeout,
  318. int *credits)
  319. {
  320. int rc;
  321. spin_lock(&server->req_lock);
  322. if (timeout == CIFS_ASYNC_OP) {
  323. /* oplock breaks must not be held up */
  324. server->in_flight++;
  325. *credits -= 1;
  326. spin_unlock(&server->req_lock);
  327. return 0;
  328. }
  329. while (1) {
  330. if (*credits <= 0) {
  331. spin_unlock(&server->req_lock);
  332. cifs_num_waiters_inc(server);
  333. rc = wait_event_killable(server->request_q,
  334. has_credits(server, credits));
  335. cifs_num_waiters_dec(server);
  336. if (rc)
  337. return rc;
  338. spin_lock(&server->req_lock);
  339. } else {
  340. if (server->tcpStatus == CifsExiting) {
  341. spin_unlock(&server->req_lock);
  342. return -ENOENT;
  343. }
  344. /*
  345. * Can not count locking commands against total
  346. * as they are allowed to block on server.
  347. */
  348. /* update # of requests on the wire to server */
  349. if (timeout != CIFS_BLOCKING_OP) {
  350. *credits -= 1;
  351. server->in_flight++;
  352. }
  353. spin_unlock(&server->req_lock);
  354. break;
  355. }
  356. }
  357. return 0;
  358. }
  359. static int
  360. wait_for_free_request(struct TCP_Server_Info *server, const int timeout,
  361. const int optype)
  362. {
  363. return wait_for_free_credits(server, timeout,
  364. server->ops->get_credits_field(server, optype));
  365. }
  366. static int allocate_mid(struct cifs_ses *ses, struct smb_hdr *in_buf,
  367. struct mid_q_entry **ppmidQ)
  368. {
  369. if (ses->server->tcpStatus == CifsExiting) {
  370. return -ENOENT;
  371. }
  372. if (ses->server->tcpStatus == CifsNeedReconnect) {
  373. cFYI(1, "tcp session dead - return to caller to retry");
  374. return -EAGAIN;
  375. }
  376. if (ses->status != CifsGood) {
  377. /* check if SMB session is bad because we are setting it up */
  378. if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
  379. (in_buf->Command != SMB_COM_NEGOTIATE))
  380. return -EAGAIN;
  381. /* else ok - we are setting up session */
  382. }
  383. *ppmidQ = AllocMidQEntry(in_buf, ses->server);
  384. if (*ppmidQ == NULL)
  385. return -ENOMEM;
  386. spin_lock(&GlobalMid_Lock);
  387. list_add_tail(&(*ppmidQ)->qhead, &ses->server->pending_mid_q);
  388. spin_unlock(&GlobalMid_Lock);
  389. return 0;
  390. }
  391. static int
  392. wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *midQ)
  393. {
  394. int error;
  395. error = wait_event_freezekillable(server->response_q,
  396. midQ->mid_state != MID_REQUEST_SUBMITTED);
  397. if (error < 0)
  398. return -ERESTARTSYS;
  399. return 0;
  400. }
  401. int
  402. cifs_setup_async_request(struct TCP_Server_Info *server, struct kvec *iov,
  403. unsigned int nvec, struct mid_q_entry **ret_mid)
  404. {
  405. int rc;
  406. struct smb_hdr *hdr = (struct smb_hdr *)iov[0].iov_base;
  407. struct mid_q_entry *mid;
  408. /* enable signing if server requires it */
  409. if (server->sec_mode & (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
  410. hdr->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
  411. mid = AllocMidQEntry(hdr, server);
  412. if (mid == NULL)
  413. return -ENOMEM;
  414. rc = cifs_sign_smbv(iov, nvec, server, &mid->sequence_number);
  415. if (rc) {
  416. DeleteMidQEntry(mid);
  417. return rc;
  418. }
  419. *ret_mid = mid;
  420. return 0;
  421. }
  422. /*
  423. * Send a SMB request and set the callback function in the mid to handle
  424. * the result. Caller is responsible for dealing with timeouts.
  425. */
  426. int
  427. cifs_call_async(struct TCP_Server_Info *server, struct kvec *iov,
  428. unsigned int nvec, mid_receive_t *receive,
  429. mid_callback_t *callback, void *cbdata, const int flags)
  430. {
  431. int rc, timeout, optype;
  432. struct mid_q_entry *mid;
  433. timeout = flags & CIFS_TIMEOUT_MASK;
  434. optype = flags & CIFS_OP_MASK;
  435. rc = wait_for_free_request(server, timeout, optype);
  436. if (rc)
  437. return rc;
  438. mutex_lock(&server->srv_mutex);
  439. rc = server->ops->setup_async_request(server, iov, nvec, &mid);
  440. if (rc) {
  441. mutex_unlock(&server->srv_mutex);
  442. add_credits(server, 1, optype);
  443. wake_up(&server->request_q);
  444. return rc;
  445. }
  446. mid->receive = receive;
  447. mid->callback = callback;
  448. mid->callback_data = cbdata;
  449. mid->mid_state = MID_REQUEST_SUBMITTED;
  450. /* put it on the pending_mid_q */
  451. spin_lock(&GlobalMid_Lock);
  452. list_add_tail(&mid->qhead, &server->pending_mid_q);
  453. spin_unlock(&GlobalMid_Lock);
  454. cifs_in_send_inc(server);
  455. rc = smb_sendv(server, iov, nvec);
  456. cifs_in_send_dec(server);
  457. cifs_save_when_sent(mid);
  458. mutex_unlock(&server->srv_mutex);
  459. if (rc == 0)
  460. return 0;
  461. cifs_delete_mid(mid);
  462. add_credits(server, 1, optype);
  463. wake_up(&server->request_q);
  464. return rc;
  465. }
  466. /*
  467. *
  468. * Send an SMB Request. No response info (other than return code)
  469. * needs to be parsed.
  470. *
  471. * flags indicate the type of request buffer and how long to wait
  472. * and whether to log NT STATUS code (error) before mapping it to POSIX error
  473. *
  474. */
  475. int
  476. SendReceiveNoRsp(const unsigned int xid, struct cifs_ses *ses,
  477. char *in_buf, int flags)
  478. {
  479. int rc;
  480. struct kvec iov[1];
  481. int resp_buf_type;
  482. iov[0].iov_base = in_buf;
  483. iov[0].iov_len = get_rfc1002_length(in_buf) + 4;
  484. flags |= CIFS_NO_RESP;
  485. rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags);
  486. cFYI(DBG2, "SendRcvNoRsp flags %d rc %d", flags, rc);
  487. return rc;
  488. }
  489. static int
  490. cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
  491. {
  492. int rc = 0;
  493. cFYI(1, "%s: cmd=%d mid=%llu state=%d", __func__,
  494. le16_to_cpu(mid->command), mid->mid, mid->mid_state);
  495. spin_lock(&GlobalMid_Lock);
  496. switch (mid->mid_state) {
  497. case MID_RESPONSE_RECEIVED:
  498. spin_unlock(&GlobalMid_Lock);
  499. return rc;
  500. case MID_RETRY_NEEDED:
  501. rc = -EAGAIN;
  502. break;
  503. case MID_RESPONSE_MALFORMED:
  504. rc = -EIO;
  505. break;
  506. case MID_SHUTDOWN:
  507. rc = -EHOSTDOWN;
  508. break;
  509. default:
  510. list_del_init(&mid->qhead);
  511. cERROR(1, "%s: invalid mid state mid=%llu state=%d", __func__,
  512. mid->mid, mid->mid_state);
  513. rc = -EIO;
  514. }
  515. spin_unlock(&GlobalMid_Lock);
  516. DeleteMidQEntry(mid);
  517. return rc;
  518. }
  519. static inline int
  520. send_cancel(struct TCP_Server_Info *server, void *buf, struct mid_q_entry *mid)
  521. {
  522. return server->ops->send_cancel ?
  523. server->ops->send_cancel(server, buf, mid) : 0;
  524. }
  525. int
  526. cifs_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
  527. bool log_error)
  528. {
  529. unsigned int len = get_rfc1002_length(mid->resp_buf) + 4;
  530. dump_smb(mid->resp_buf, min_t(u32, 92, len));
  531. /* convert the length into a more usable form */
  532. if (server->sec_mode & (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED)) {
  533. struct kvec iov;
  534. int rc = 0;
  535. struct smb_rqst rqst = { .rq_iov = &iov,
  536. .rq_nvec = 1 };
  537. iov.iov_base = mid->resp_buf;
  538. iov.iov_len = len;
  539. /* FIXME: add code to kill session */
  540. rc = cifs_verify_signature(&rqst, server,
  541. mid->sequence_number + 1);
  542. if (rc)
  543. cERROR(1, "SMB signature verification returned error = "
  544. "%d", rc);
  545. }
  546. /* BB special case reconnect tid and uid here? */
  547. return map_smb_to_linux_error(mid->resp_buf, log_error);
  548. }
  549. int
  550. cifs_setup_request(struct cifs_ses *ses, struct kvec *iov,
  551. unsigned int nvec, struct mid_q_entry **ret_mid)
  552. {
  553. int rc;
  554. struct smb_hdr *hdr = (struct smb_hdr *)iov[0].iov_base;
  555. struct mid_q_entry *mid;
  556. rc = allocate_mid(ses, hdr, &mid);
  557. if (rc)
  558. return rc;
  559. rc = cifs_sign_smbv(iov, nvec, ses->server, &mid->sequence_number);
  560. if (rc)
  561. cifs_delete_mid(mid);
  562. *ret_mid = mid;
  563. return rc;
  564. }
  565. int
  566. SendReceive2(const unsigned int xid, struct cifs_ses *ses,
  567. struct kvec *iov, int n_vec, int *resp_buf_type /* ret */,
  568. const int flags)
  569. {
  570. int rc = 0;
  571. int timeout, optype;
  572. struct mid_q_entry *midQ;
  573. char *buf = iov[0].iov_base;
  574. unsigned int credits = 1;
  575. timeout = flags & CIFS_TIMEOUT_MASK;
  576. optype = flags & CIFS_OP_MASK;
  577. *resp_buf_type = CIFS_NO_BUFFER; /* no response buf yet */
  578. if ((ses == NULL) || (ses->server == NULL)) {
  579. cifs_small_buf_release(buf);
  580. cERROR(1, "Null session");
  581. return -EIO;
  582. }
  583. if (ses->server->tcpStatus == CifsExiting) {
  584. cifs_small_buf_release(buf);
  585. return -ENOENT;
  586. }
  587. /*
  588. * Ensure that we do not send more than 50 overlapping requests
  589. * to the same server. We may make this configurable later or
  590. * use ses->maxReq.
  591. */
  592. rc = wait_for_free_request(ses->server, timeout, optype);
  593. if (rc) {
  594. cifs_small_buf_release(buf);
  595. return rc;
  596. }
  597. /*
  598. * Make sure that we sign in the same order that we send on this socket
  599. * and avoid races inside tcp sendmsg code that could cause corruption
  600. * of smb data.
  601. */
  602. mutex_lock(&ses->server->srv_mutex);
  603. rc = ses->server->ops->setup_request(ses, iov, n_vec, &midQ);
  604. if (rc) {
  605. mutex_unlock(&ses->server->srv_mutex);
  606. cifs_small_buf_release(buf);
  607. /* Update # of requests on wire to server */
  608. add_credits(ses->server, 1, optype);
  609. return rc;
  610. }
  611. midQ->mid_state = MID_REQUEST_SUBMITTED;
  612. cifs_in_send_inc(ses->server);
  613. rc = smb_sendv(ses->server, iov, n_vec);
  614. cifs_in_send_dec(ses->server);
  615. cifs_save_when_sent(midQ);
  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. cFYI(1, "Bad MID state?");
  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. cERROR(1, "Null smb session");
  676. return -EIO;
  677. }
  678. if (ses->server == NULL) {
  679. cERROR(1, "Null tcp session");
  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. cERROR(1, "Illegal length, greater than maximum frame, %d",
  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. mutex_unlock(&ses->server->srv_mutex);
  718. if (rc < 0)
  719. goto out;
  720. if (timeout == CIFS_ASYNC_OP)
  721. goto out;
  722. rc = wait_for_response(ses->server, midQ);
  723. if (rc != 0) {
  724. send_cancel(ses->server, in_buf, midQ);
  725. spin_lock(&GlobalMid_Lock);
  726. if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
  727. /* no longer considered to be "in-flight" */
  728. midQ->callback = DeleteMidQEntry;
  729. spin_unlock(&GlobalMid_Lock);
  730. add_credits(ses->server, 1, 0);
  731. return rc;
  732. }
  733. spin_unlock(&GlobalMid_Lock);
  734. }
  735. rc = cifs_sync_mid_result(midQ, ses->server);
  736. if (rc != 0) {
  737. add_credits(ses->server, 1, 0);
  738. return rc;
  739. }
  740. if (!midQ->resp_buf || !out_buf ||
  741. midQ->mid_state != MID_RESPONSE_RECEIVED) {
  742. rc = -EIO;
  743. cERROR(1, "Bad MID state?");
  744. goto out;
  745. }
  746. *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
  747. memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
  748. rc = cifs_check_receive(midQ, ses->server, 0);
  749. out:
  750. cifs_delete_mid(midQ);
  751. add_credits(ses->server, 1, 0);
  752. return rc;
  753. }
  754. /* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
  755. blocking lock to return. */
  756. static int
  757. send_lock_cancel(const unsigned int xid, struct cifs_tcon *tcon,
  758. struct smb_hdr *in_buf,
  759. struct smb_hdr *out_buf)
  760. {
  761. int bytes_returned;
  762. struct cifs_ses *ses = tcon->ses;
  763. LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
  764. /* We just modify the current in_buf to change
  765. the type of lock from LOCKING_ANDX_SHARED_LOCK
  766. or LOCKING_ANDX_EXCLUSIVE_LOCK to
  767. LOCKING_ANDX_CANCEL_LOCK. */
  768. pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
  769. pSMB->Timeout = 0;
  770. pSMB->hdr.Mid = get_next_mid(ses->server);
  771. return SendReceive(xid, ses, in_buf, out_buf,
  772. &bytes_returned, 0);
  773. }
  774. int
  775. SendReceiveBlockingLock(const unsigned int xid, struct cifs_tcon *tcon,
  776. struct smb_hdr *in_buf, struct smb_hdr *out_buf,
  777. int *pbytes_returned)
  778. {
  779. int rc = 0;
  780. int rstart = 0;
  781. struct mid_q_entry *midQ;
  782. struct cifs_ses *ses;
  783. if (tcon == NULL || tcon->ses == NULL) {
  784. cERROR(1, "Null smb session");
  785. return -EIO;
  786. }
  787. ses = tcon->ses;
  788. if (ses->server == NULL) {
  789. cERROR(1, "Null tcp session");
  790. return -EIO;
  791. }
  792. if (ses->server->tcpStatus == CifsExiting)
  793. return -ENOENT;
  794. /* Ensure that we do not send more than 50 overlapping requests
  795. to the same server. We may make this configurable later or
  796. use ses->maxReq */
  797. if (be32_to_cpu(in_buf->smb_buf_length) > CIFSMaxBufSize +
  798. MAX_CIFS_HDR_SIZE - 4) {
  799. cERROR(1, "Illegal length, greater than maximum frame, %d",
  800. be32_to_cpu(in_buf->smb_buf_length));
  801. return -EIO;
  802. }
  803. rc = wait_for_free_request(ses->server, CIFS_BLOCKING_OP, 0);
  804. if (rc)
  805. return rc;
  806. /* make sure that we sign in the same order that we send on this socket
  807. and avoid races inside tcp sendmsg code that could cause corruption
  808. of smb data */
  809. mutex_lock(&ses->server->srv_mutex);
  810. rc = allocate_mid(ses, in_buf, &midQ);
  811. if (rc) {
  812. mutex_unlock(&ses->server->srv_mutex);
  813. return rc;
  814. }
  815. rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
  816. if (rc) {
  817. cifs_delete_mid(midQ);
  818. mutex_unlock(&ses->server->srv_mutex);
  819. return rc;
  820. }
  821. midQ->mid_state = MID_REQUEST_SUBMITTED;
  822. cifs_in_send_inc(ses->server);
  823. rc = smb_send(ses->server, in_buf, be32_to_cpu(in_buf->smb_buf_length));
  824. cifs_in_send_dec(ses->server);
  825. cifs_save_when_sent(midQ);
  826. mutex_unlock(&ses->server->srv_mutex);
  827. if (rc < 0) {
  828. cifs_delete_mid(midQ);
  829. return rc;
  830. }
  831. /* Wait for a reply - allow signals to interrupt. */
  832. rc = wait_event_interruptible(ses->server->response_q,
  833. (!(midQ->mid_state == MID_REQUEST_SUBMITTED)) ||
  834. ((ses->server->tcpStatus != CifsGood) &&
  835. (ses->server->tcpStatus != CifsNew)));
  836. /* Were we interrupted by a signal ? */
  837. if ((rc == -ERESTARTSYS) &&
  838. (midQ->mid_state == MID_REQUEST_SUBMITTED) &&
  839. ((ses->server->tcpStatus == CifsGood) ||
  840. (ses->server->tcpStatus == CifsNew))) {
  841. if (in_buf->Command == SMB_COM_TRANSACTION2) {
  842. /* POSIX lock. We send a NT_CANCEL SMB to cause the
  843. blocking lock to return. */
  844. rc = send_cancel(ses->server, in_buf, midQ);
  845. if (rc) {
  846. cifs_delete_mid(midQ);
  847. return rc;
  848. }
  849. } else {
  850. /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
  851. to cause the blocking lock to return. */
  852. rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
  853. /* If we get -ENOLCK back the lock may have
  854. already been removed. Don't exit in this case. */
  855. if (rc && rc != -ENOLCK) {
  856. cifs_delete_mid(midQ);
  857. return rc;
  858. }
  859. }
  860. rc = wait_for_response(ses->server, midQ);
  861. if (rc) {
  862. send_cancel(ses->server, in_buf, midQ);
  863. spin_lock(&GlobalMid_Lock);
  864. if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
  865. /* no longer considered to be "in-flight" */
  866. midQ->callback = DeleteMidQEntry;
  867. spin_unlock(&GlobalMid_Lock);
  868. return rc;
  869. }
  870. spin_unlock(&GlobalMid_Lock);
  871. }
  872. /* We got the response - restart system call. */
  873. rstart = 1;
  874. }
  875. rc = cifs_sync_mid_result(midQ, ses->server);
  876. if (rc != 0)
  877. return rc;
  878. /* rcvd frame is ok */
  879. if (out_buf == NULL || midQ->mid_state != MID_RESPONSE_RECEIVED) {
  880. rc = -EIO;
  881. cERROR(1, "Bad MID state?");
  882. goto out;
  883. }
  884. *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
  885. memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
  886. rc = cifs_check_receive(midQ, ses->server, 0);
  887. out:
  888. cifs_delete_mid(midQ);
  889. if (rstart && rc == -EACCES)
  890. return -ERESTARTSYS;
  891. return rc;
  892. }