transport.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  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 && rc != -EINTR)
  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. /* enable signing if server requires it */
  322. if (server->secMode & (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
  323. in_buf->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
  324. mutex_lock(&server->srv_mutex);
  325. mid = AllocMidQEntry(in_buf, server);
  326. if (mid == NULL) {
  327. mutex_unlock(&server->srv_mutex);
  328. return -ENOMEM;
  329. }
  330. /* put it on the pending_mid_q */
  331. spin_lock(&GlobalMid_Lock);
  332. list_add_tail(&mid->qhead, &server->pending_mid_q);
  333. spin_unlock(&GlobalMid_Lock);
  334. rc = cifs_sign_smb(in_buf, server, &mid->sequence_number);
  335. if (rc) {
  336. mutex_unlock(&server->srv_mutex);
  337. goto out_err;
  338. }
  339. mid->callback = callback;
  340. mid->callback_data = cbdata;
  341. mid->midState = MID_REQUEST_SUBMITTED;
  342. #ifdef CONFIG_CIFS_STATS2
  343. atomic_inc(&server->inSend);
  344. #endif
  345. rc = smb_send(server, in_buf, in_buf->smb_buf_length);
  346. #ifdef CONFIG_CIFS_STATS2
  347. atomic_dec(&server->inSend);
  348. mid->when_sent = jiffies;
  349. #endif
  350. mutex_unlock(&server->srv_mutex);
  351. if (rc)
  352. goto out_err;
  353. return rc;
  354. out_err:
  355. delete_mid(mid);
  356. atomic_dec(&server->inFlight);
  357. wake_up(&server->request_q);
  358. return rc;
  359. }
  360. /*
  361. *
  362. * Send an SMB Request. No response info (other than return code)
  363. * needs to be parsed.
  364. *
  365. * flags indicate the type of request buffer and how long to wait
  366. * and whether to log NT STATUS code (error) before mapping it to POSIX error
  367. *
  368. */
  369. int
  370. SendReceiveNoRsp(const unsigned int xid, struct cifsSesInfo *ses,
  371. struct smb_hdr *in_buf, int flags)
  372. {
  373. int rc;
  374. struct kvec iov[1];
  375. int resp_buf_type;
  376. iov[0].iov_base = (char *)in_buf;
  377. iov[0].iov_len = in_buf->smb_buf_length + 4;
  378. flags |= CIFS_NO_RESP;
  379. rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags);
  380. cFYI(DBG2, "SendRcvNoRsp flags %d rc %d", flags, rc);
  381. return rc;
  382. }
  383. static int
  384. sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
  385. {
  386. int rc = 0;
  387. cFYI(1, "%s: cmd=%d mid=%d state=%d", __func__, mid->command,
  388. mid->mid, mid->midState);
  389. spin_lock(&GlobalMid_Lock);
  390. /* ensure that it's no longer on the pending_mid_q */
  391. list_del_init(&mid->qhead);
  392. switch (mid->midState) {
  393. case MID_RESPONSE_RECEIVED:
  394. spin_unlock(&GlobalMid_Lock);
  395. return rc;
  396. case MID_REQUEST_SUBMITTED:
  397. /* socket is going down, reject all calls */
  398. if (server->tcpStatus == CifsExiting) {
  399. cERROR(1, "%s: canceling mid=%d cmd=0x%x state=%d",
  400. __func__, mid->mid, mid->command, mid->midState);
  401. rc = -EHOSTDOWN;
  402. break;
  403. }
  404. case MID_RETRY_NEEDED:
  405. rc = -EAGAIN;
  406. break;
  407. case MID_RESPONSE_MALFORMED:
  408. rc = -EIO;
  409. break;
  410. default:
  411. cERROR(1, "%s: invalid mid state mid=%d state=%d", __func__,
  412. mid->mid, mid->midState);
  413. rc = -EIO;
  414. }
  415. spin_unlock(&GlobalMid_Lock);
  416. DeleteMidQEntry(mid);
  417. return rc;
  418. }
  419. /*
  420. * An NT cancel request header looks just like the original request except:
  421. *
  422. * The Command is SMB_COM_NT_CANCEL
  423. * The WordCount is zeroed out
  424. * The ByteCount is zeroed out
  425. *
  426. * This function mangles an existing request buffer into a
  427. * SMB_COM_NT_CANCEL request and then sends it.
  428. */
  429. static int
  430. send_nt_cancel(struct TCP_Server_Info *server, struct smb_hdr *in_buf,
  431. struct mid_q_entry *mid)
  432. {
  433. int rc = 0;
  434. /* -4 for RFC1001 length and +2 for BCC field */
  435. in_buf->smb_buf_length = sizeof(struct smb_hdr) - 4 + 2;
  436. in_buf->Command = SMB_COM_NT_CANCEL;
  437. in_buf->WordCount = 0;
  438. put_bcc_le(0, in_buf);
  439. mutex_lock(&server->srv_mutex);
  440. rc = cifs_sign_smb(in_buf, server, &mid->sequence_number);
  441. if (rc) {
  442. mutex_unlock(&server->srv_mutex);
  443. return rc;
  444. }
  445. rc = smb_send(server, in_buf, in_buf->smb_buf_length);
  446. mutex_unlock(&server->srv_mutex);
  447. cFYI(1, "issued NT_CANCEL for mid %u, rc = %d",
  448. in_buf->Mid, rc);
  449. return rc;
  450. }
  451. int
  452. SendReceive2(const unsigned int xid, struct cifsSesInfo *ses,
  453. struct kvec *iov, int n_vec, int *pRespBufType /* ret */,
  454. const int flags)
  455. {
  456. int rc = 0;
  457. int long_op;
  458. unsigned int receive_len;
  459. struct mid_q_entry *midQ;
  460. struct smb_hdr *in_buf = iov[0].iov_base;
  461. long_op = flags & CIFS_TIMEOUT_MASK;
  462. *pRespBufType = CIFS_NO_BUFFER; /* no response buf yet */
  463. if ((ses == NULL) || (ses->server == NULL)) {
  464. cifs_small_buf_release(in_buf);
  465. cERROR(1, "Null session");
  466. return -EIO;
  467. }
  468. if (ses->server->tcpStatus == CifsExiting) {
  469. cifs_small_buf_release(in_buf);
  470. return -ENOENT;
  471. }
  472. /* Ensure that we do not send more than 50 overlapping requests
  473. to the same server. We may make this configurable later or
  474. use ses->maxReq */
  475. rc = wait_for_free_request(ses->server, long_op);
  476. if (rc) {
  477. cifs_small_buf_release(in_buf);
  478. return rc;
  479. }
  480. /* make sure that we sign in the same order that we send on this socket
  481. and avoid races inside tcp sendmsg code that could cause corruption
  482. of smb data */
  483. mutex_lock(&ses->server->srv_mutex);
  484. rc = allocate_mid(ses, in_buf, &midQ);
  485. if (rc) {
  486. mutex_unlock(&ses->server->srv_mutex);
  487. cifs_small_buf_release(in_buf);
  488. /* Update # of requests on wire to server */
  489. atomic_dec(&ses->server->inFlight);
  490. wake_up(&ses->server->request_q);
  491. return rc;
  492. }
  493. rc = cifs_sign_smb2(iov, n_vec, ses->server, &midQ->sequence_number);
  494. if (rc) {
  495. mutex_unlock(&ses->server->srv_mutex);
  496. cifs_small_buf_release(in_buf);
  497. goto out;
  498. }
  499. midQ->midState = MID_REQUEST_SUBMITTED;
  500. #ifdef CONFIG_CIFS_STATS2
  501. atomic_inc(&ses->server->inSend);
  502. #endif
  503. rc = smb_sendv(ses->server, iov, n_vec);
  504. #ifdef CONFIG_CIFS_STATS2
  505. atomic_dec(&ses->server->inSend);
  506. midQ->when_sent = jiffies;
  507. #endif
  508. mutex_unlock(&ses->server->srv_mutex);
  509. if (rc < 0) {
  510. cifs_small_buf_release(in_buf);
  511. goto out;
  512. }
  513. if (long_op == CIFS_ASYNC_OP) {
  514. cifs_small_buf_release(in_buf);
  515. goto out;
  516. }
  517. rc = wait_for_response(ses->server, midQ);
  518. if (rc != 0) {
  519. send_nt_cancel(ses->server, in_buf, midQ);
  520. spin_lock(&GlobalMid_Lock);
  521. if (midQ->midState == MID_REQUEST_SUBMITTED) {
  522. midQ->callback = DeleteMidQEntry;
  523. spin_unlock(&GlobalMid_Lock);
  524. cifs_small_buf_release(in_buf);
  525. atomic_dec(&ses->server->inFlight);
  526. wake_up(&ses->server->request_q);
  527. return rc;
  528. }
  529. spin_unlock(&GlobalMid_Lock);
  530. }
  531. cifs_small_buf_release(in_buf);
  532. rc = sync_mid_result(midQ, ses->server);
  533. if (rc != 0) {
  534. atomic_dec(&ses->server->inFlight);
  535. wake_up(&ses->server->request_q);
  536. return rc;
  537. }
  538. receive_len = midQ->resp_buf->smb_buf_length;
  539. if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
  540. cERROR(1, "Frame too large received. Length: %d Xid: %d",
  541. receive_len, xid);
  542. rc = -EIO;
  543. goto out;
  544. }
  545. /* rcvd frame is ok */
  546. if (midQ->resp_buf &&
  547. (midQ->midState == MID_RESPONSE_RECEIVED)) {
  548. iov[0].iov_base = (char *)midQ->resp_buf;
  549. if (midQ->largeBuf)
  550. *pRespBufType = CIFS_LARGE_BUFFER;
  551. else
  552. *pRespBufType = CIFS_SMALL_BUFFER;
  553. iov[0].iov_len = receive_len + 4;
  554. dump_smb(midQ->resp_buf, 80);
  555. /* convert the length into a more usable form */
  556. if ((receive_len > 24) &&
  557. (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
  558. SECMODE_SIGN_ENABLED))) {
  559. rc = cifs_verify_signature(midQ->resp_buf,
  560. ses->server,
  561. midQ->sequence_number+1);
  562. if (rc) {
  563. cERROR(1, "Unexpected SMB signature");
  564. /* BB FIXME add code to kill session */
  565. }
  566. }
  567. /* BB special case reconnect tid and uid here? */
  568. rc = map_smb_to_linux_error(midQ->resp_buf,
  569. flags & CIFS_LOG_ERROR);
  570. /* convert ByteCount if necessary */
  571. if (receive_len >= sizeof(struct smb_hdr) - 4
  572. /* do not count RFC1001 header */ +
  573. (2 * midQ->resp_buf->WordCount) + 2 /* bcc */ )
  574. put_bcc(get_bcc_le(midQ->resp_buf), midQ->resp_buf);
  575. if ((flags & CIFS_NO_RESP) == 0)
  576. midQ->resp_buf = NULL; /* mark it so buf will
  577. not be freed by
  578. delete_mid */
  579. } else {
  580. rc = -EIO;
  581. cFYI(1, "Bad MID state?");
  582. }
  583. out:
  584. delete_mid(midQ);
  585. atomic_dec(&ses->server->inFlight);
  586. wake_up(&ses->server->request_q);
  587. return rc;
  588. }
  589. int
  590. SendReceive(const unsigned int xid, struct cifsSesInfo *ses,
  591. struct smb_hdr *in_buf, struct smb_hdr *out_buf,
  592. int *pbytes_returned, const int long_op)
  593. {
  594. int rc = 0;
  595. unsigned int receive_len;
  596. struct mid_q_entry *midQ;
  597. if (ses == NULL) {
  598. cERROR(1, "Null smb session");
  599. return -EIO;
  600. }
  601. if (ses->server == NULL) {
  602. cERROR(1, "Null tcp session");
  603. return -EIO;
  604. }
  605. if (ses->server->tcpStatus == CifsExiting)
  606. return -ENOENT;
  607. /* Ensure that we do not send more than 50 overlapping requests
  608. to the same server. We may make this configurable later or
  609. use ses->maxReq */
  610. if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
  611. cERROR(1, "Illegal length, greater than maximum frame, %d",
  612. in_buf->smb_buf_length);
  613. return -EIO;
  614. }
  615. rc = wait_for_free_request(ses->server, long_op);
  616. if (rc)
  617. return rc;
  618. /* make sure that we sign in the same order that we send on this socket
  619. and avoid races inside tcp sendmsg code that could cause corruption
  620. of smb data */
  621. mutex_lock(&ses->server->srv_mutex);
  622. rc = allocate_mid(ses, in_buf, &midQ);
  623. if (rc) {
  624. mutex_unlock(&ses->server->srv_mutex);
  625. /* Update # of requests on wire to server */
  626. atomic_dec(&ses->server->inFlight);
  627. wake_up(&ses->server->request_q);
  628. return rc;
  629. }
  630. rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
  631. if (rc) {
  632. mutex_unlock(&ses->server->srv_mutex);
  633. goto out;
  634. }
  635. midQ->midState = MID_REQUEST_SUBMITTED;
  636. #ifdef CONFIG_CIFS_STATS2
  637. atomic_inc(&ses->server->inSend);
  638. #endif
  639. rc = smb_send(ses->server, in_buf, in_buf->smb_buf_length);
  640. #ifdef CONFIG_CIFS_STATS2
  641. atomic_dec(&ses->server->inSend);
  642. midQ->when_sent = jiffies;
  643. #endif
  644. mutex_unlock(&ses->server->srv_mutex);
  645. if (rc < 0)
  646. goto out;
  647. if (long_op == CIFS_ASYNC_OP)
  648. goto out;
  649. rc = wait_for_response(ses->server, midQ);
  650. if (rc != 0) {
  651. send_nt_cancel(ses->server, in_buf, midQ);
  652. spin_lock(&GlobalMid_Lock);
  653. if (midQ->midState == MID_REQUEST_SUBMITTED) {
  654. /* no longer considered to be "in-flight" */
  655. midQ->callback = DeleteMidQEntry;
  656. spin_unlock(&GlobalMid_Lock);
  657. atomic_dec(&ses->server->inFlight);
  658. wake_up(&ses->server->request_q);
  659. return rc;
  660. }
  661. spin_unlock(&GlobalMid_Lock);
  662. }
  663. rc = sync_mid_result(midQ, ses->server);
  664. if (rc != 0) {
  665. atomic_dec(&ses->server->inFlight);
  666. wake_up(&ses->server->request_q);
  667. return rc;
  668. }
  669. receive_len = midQ->resp_buf->smb_buf_length;
  670. if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
  671. cERROR(1, "Frame too large received. Length: %d Xid: %d",
  672. receive_len, xid);
  673. rc = -EIO;
  674. goto out;
  675. }
  676. /* rcvd frame is ok */
  677. if (midQ->resp_buf && out_buf
  678. && (midQ->midState == MID_RESPONSE_RECEIVED)) {
  679. out_buf->smb_buf_length = receive_len;
  680. memcpy((char *)out_buf + 4,
  681. (char *)midQ->resp_buf + 4,
  682. receive_len);
  683. dump_smb(out_buf, 92);
  684. /* convert the length into a more usable form */
  685. if ((receive_len > 24) &&
  686. (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
  687. SECMODE_SIGN_ENABLED))) {
  688. rc = cifs_verify_signature(out_buf,
  689. ses->server,
  690. midQ->sequence_number+1);
  691. if (rc) {
  692. cERROR(1, "Unexpected SMB signature");
  693. /* BB FIXME add code to kill session */
  694. }
  695. }
  696. *pbytes_returned = out_buf->smb_buf_length;
  697. /* BB special case reconnect tid and uid here? */
  698. rc = map_smb_to_linux_error(out_buf, 0 /* no log */ );
  699. /* convert ByteCount if necessary */
  700. if (receive_len >= sizeof(struct smb_hdr) - 4
  701. /* do not count RFC1001 header */ +
  702. (2 * out_buf->WordCount) + 2 /* bcc */ )
  703. put_bcc(get_bcc_le(midQ->resp_buf), midQ->resp_buf);
  704. } else {
  705. rc = -EIO;
  706. cERROR(1, "Bad MID state?");
  707. }
  708. out:
  709. delete_mid(midQ);
  710. atomic_dec(&ses->server->inFlight);
  711. wake_up(&ses->server->request_q);
  712. return rc;
  713. }
  714. /* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
  715. blocking lock to return. */
  716. static int
  717. send_lock_cancel(const unsigned int xid, struct cifsTconInfo *tcon,
  718. struct smb_hdr *in_buf,
  719. struct smb_hdr *out_buf)
  720. {
  721. int bytes_returned;
  722. struct cifsSesInfo *ses = tcon->ses;
  723. LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
  724. /* We just modify the current in_buf to change
  725. the type of lock from LOCKING_ANDX_SHARED_LOCK
  726. or LOCKING_ANDX_EXCLUSIVE_LOCK to
  727. LOCKING_ANDX_CANCEL_LOCK. */
  728. pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
  729. pSMB->Timeout = 0;
  730. pSMB->hdr.Mid = GetNextMid(ses->server);
  731. return SendReceive(xid, ses, in_buf, out_buf,
  732. &bytes_returned, 0);
  733. }
  734. int
  735. SendReceiveBlockingLock(const unsigned int xid, struct cifsTconInfo *tcon,
  736. struct smb_hdr *in_buf, struct smb_hdr *out_buf,
  737. int *pbytes_returned)
  738. {
  739. int rc = 0;
  740. int rstart = 0;
  741. unsigned int receive_len;
  742. struct mid_q_entry *midQ;
  743. struct cifsSesInfo *ses;
  744. if (tcon == NULL || tcon->ses == NULL) {
  745. cERROR(1, "Null smb session");
  746. return -EIO;
  747. }
  748. ses = tcon->ses;
  749. if (ses->server == NULL) {
  750. cERROR(1, "Null tcp session");
  751. return -EIO;
  752. }
  753. if (ses->server->tcpStatus == CifsExiting)
  754. return -ENOENT;
  755. /* Ensure that we do not send more than 50 overlapping requests
  756. to the same server. We may make this configurable later or
  757. use ses->maxReq */
  758. if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
  759. cERROR(1, "Illegal length, greater than maximum frame, %d",
  760. in_buf->smb_buf_length);
  761. return -EIO;
  762. }
  763. rc = wait_for_free_request(ses->server, CIFS_BLOCKING_OP);
  764. if (rc)
  765. return rc;
  766. /* make sure that we sign in the same order that we send on this socket
  767. and avoid races inside tcp sendmsg code that could cause corruption
  768. of smb data */
  769. mutex_lock(&ses->server->srv_mutex);
  770. rc = allocate_mid(ses, in_buf, &midQ);
  771. if (rc) {
  772. mutex_unlock(&ses->server->srv_mutex);
  773. return rc;
  774. }
  775. rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
  776. if (rc) {
  777. delete_mid(midQ);
  778. mutex_unlock(&ses->server->srv_mutex);
  779. return rc;
  780. }
  781. midQ->midState = MID_REQUEST_SUBMITTED;
  782. #ifdef CONFIG_CIFS_STATS2
  783. atomic_inc(&ses->server->inSend);
  784. #endif
  785. rc = smb_send(ses->server, in_buf, in_buf->smb_buf_length);
  786. #ifdef CONFIG_CIFS_STATS2
  787. atomic_dec(&ses->server->inSend);
  788. midQ->when_sent = jiffies;
  789. #endif
  790. mutex_unlock(&ses->server->srv_mutex);
  791. if (rc < 0) {
  792. delete_mid(midQ);
  793. return rc;
  794. }
  795. /* Wait for a reply - allow signals to interrupt. */
  796. rc = wait_event_interruptible(ses->server->response_q,
  797. (!(midQ->midState == MID_REQUEST_SUBMITTED)) ||
  798. ((ses->server->tcpStatus != CifsGood) &&
  799. (ses->server->tcpStatus != CifsNew)));
  800. /* Were we interrupted by a signal ? */
  801. if ((rc == -ERESTARTSYS) &&
  802. (midQ->midState == MID_REQUEST_SUBMITTED) &&
  803. ((ses->server->tcpStatus == CifsGood) ||
  804. (ses->server->tcpStatus == CifsNew))) {
  805. if (in_buf->Command == SMB_COM_TRANSACTION2) {
  806. /* POSIX lock. We send a NT_CANCEL SMB to cause the
  807. blocking lock to return. */
  808. rc = send_nt_cancel(ses->server, in_buf, midQ);
  809. if (rc) {
  810. delete_mid(midQ);
  811. return rc;
  812. }
  813. } else {
  814. /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
  815. to cause the blocking lock to return. */
  816. rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
  817. /* If we get -ENOLCK back the lock may have
  818. already been removed. Don't exit in this case. */
  819. if (rc && rc != -ENOLCK) {
  820. delete_mid(midQ);
  821. return rc;
  822. }
  823. }
  824. rc = wait_for_response(ses->server, midQ);
  825. if (rc) {
  826. send_nt_cancel(ses->server, in_buf, midQ);
  827. spin_lock(&GlobalMid_Lock);
  828. if (midQ->midState == MID_REQUEST_SUBMITTED) {
  829. /* no longer considered to be "in-flight" */
  830. midQ->callback = DeleteMidQEntry;
  831. spin_unlock(&GlobalMid_Lock);
  832. return rc;
  833. }
  834. spin_unlock(&GlobalMid_Lock);
  835. }
  836. /* We got the response - restart system call. */
  837. rstart = 1;
  838. }
  839. rc = sync_mid_result(midQ, ses->server);
  840. if (rc != 0)
  841. return rc;
  842. receive_len = midQ->resp_buf->smb_buf_length;
  843. if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
  844. cERROR(1, "Frame too large received. Length: %d Xid: %d",
  845. receive_len, xid);
  846. rc = -EIO;
  847. goto out;
  848. }
  849. /* rcvd frame is ok */
  850. if ((out_buf == NULL) || (midQ->midState != MID_RESPONSE_RECEIVED)) {
  851. rc = -EIO;
  852. cERROR(1, "Bad MID state?");
  853. goto out;
  854. }
  855. out_buf->smb_buf_length = receive_len;
  856. memcpy((char *)out_buf + 4,
  857. (char *)midQ->resp_buf + 4,
  858. receive_len);
  859. dump_smb(out_buf, 92);
  860. /* convert the length into a more usable form */
  861. if ((receive_len > 24) &&
  862. (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
  863. SECMODE_SIGN_ENABLED))) {
  864. rc = cifs_verify_signature(out_buf,
  865. ses->server,
  866. midQ->sequence_number+1);
  867. if (rc) {
  868. cERROR(1, "Unexpected SMB signature");
  869. /* BB FIXME add code to kill session */
  870. }
  871. }
  872. *pbytes_returned = out_buf->smb_buf_length;
  873. /* BB special case reconnect tid and uid here? */
  874. rc = map_smb_to_linux_error(out_buf, 0 /* no log */ );
  875. /* convert ByteCount if necessary */
  876. if (receive_len >= sizeof(struct smb_hdr) - 4
  877. /* do not count RFC1001 header */ +
  878. (2 * out_buf->WordCount) + 2 /* bcc */ )
  879. put_bcc(get_bcc_le(out_buf), out_buf);
  880. out:
  881. delete_mid(midQ);
  882. if (rstart && rc == -EACCES)
  883. return -ERESTARTSYS;
  884. return rc;
  885. }