transport.c 25 KB

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