transport.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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 = be32_to_cpu(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. total_len = 0;
  133. for (i = 0; i < n_vec; i++)
  134. total_len += iov[i].iov_len;
  135. cFYI(1, "Sending smb: total_len %d", total_len);
  136. dump_smb(smb_buffer, len);
  137. i = 0;
  138. while (total_len) {
  139. rc = kernel_sendmsg(ssocket, &smb_msg, &iov[first_vec],
  140. n_vec - first_vec, total_len);
  141. if ((rc == -ENOSPC) || (rc == -EAGAIN)) {
  142. i++;
  143. /* if blocking send we try 3 times, since each can block
  144. for 5 seconds. For nonblocking we have to try more
  145. but wait increasing amounts of time allowing time for
  146. socket to clear. The overall time we wait in either
  147. case to send on the socket is about 15 seconds.
  148. Similarly we wait for 15 seconds for
  149. a response from the server in SendReceive[2]
  150. for the server to send a response back for
  151. most types of requests (except SMB Write
  152. past end of file which can be slow, and
  153. blocking lock operations). NFS waits slightly longer
  154. than CIFS, but this can make it take longer for
  155. nonresponsive servers to be detected and 15 seconds
  156. is more than enough time for modern networks to
  157. send a packet. In most cases if we fail to send
  158. after the retries we will kill the socket and
  159. reconnect which may clear the network problem.
  160. */
  161. if ((i >= 14) || (!server->noblocksnd && (i > 2))) {
  162. cERROR(1, "sends on sock %p stuck for 15 seconds",
  163. ssocket);
  164. rc = -EAGAIN;
  165. break;
  166. }
  167. msleep(1 << i);
  168. continue;
  169. }
  170. if (rc < 0)
  171. break;
  172. if (rc == total_len) {
  173. total_len = 0;
  174. break;
  175. } else if (rc > total_len) {
  176. cERROR(1, "sent %d requested %d", rc, total_len);
  177. break;
  178. }
  179. if (rc == 0) {
  180. /* should never happen, letting socket clear before
  181. retrying is our only obvious option here */
  182. cERROR(1, "tcp sent no data");
  183. msleep(500);
  184. continue;
  185. }
  186. total_len -= rc;
  187. /* the line below resets i */
  188. for (i = first_vec; i < n_vec; i++) {
  189. if (iov[i].iov_len) {
  190. if (rc > iov[i].iov_len) {
  191. rc -= iov[i].iov_len;
  192. iov[i].iov_len = 0;
  193. } else {
  194. iov[i].iov_base += rc;
  195. iov[i].iov_len -= rc;
  196. first_vec = i;
  197. break;
  198. }
  199. }
  200. }
  201. i = 0; /* in case we get ENOSPC on the next send */
  202. }
  203. if ((total_len > 0) && (total_len != smb_buf_length + 4)) {
  204. cFYI(1, "partial send (%d remaining), terminating session",
  205. total_len);
  206. /* If we have only sent part of an SMB then the next SMB
  207. could be taken as the remainder of this one. We need
  208. to kill the socket so the server throws away the partial
  209. SMB */
  210. server->tcpStatus = CifsNeedReconnect;
  211. }
  212. if (rc < 0 && rc != -EINTR)
  213. cERROR(1, "Error %d sending data on socket to server", rc);
  214. else
  215. rc = 0;
  216. /* Don't want to modify the buffer as a
  217. side effect of this call. */
  218. smb_buffer->smb_buf_length = cpu_to_be32(smb_buf_length);
  219. return rc;
  220. }
  221. int
  222. smb_send(struct TCP_Server_Info *server, struct smb_hdr *smb_buffer,
  223. unsigned int smb_buf_length)
  224. {
  225. struct kvec iov;
  226. iov.iov_base = smb_buffer;
  227. iov.iov_len = smb_buf_length + 4;
  228. return smb_sendv(server, &iov, 1);
  229. }
  230. static int wait_for_free_request(struct TCP_Server_Info *server,
  231. const int long_op)
  232. {
  233. if (long_op == CIFS_ASYNC_OP) {
  234. /* oplock breaks must not be held up */
  235. atomic_inc(&server->inFlight);
  236. return 0;
  237. }
  238. spin_lock(&GlobalMid_Lock);
  239. while (1) {
  240. if (atomic_read(&server->inFlight) >= cifs_max_pending) {
  241. spin_unlock(&GlobalMid_Lock);
  242. #ifdef CONFIG_CIFS_STATS2
  243. atomic_inc(&server->num_waiters);
  244. #endif
  245. wait_event(server->request_q,
  246. atomic_read(&server->inFlight)
  247. < cifs_max_pending);
  248. #ifdef CONFIG_CIFS_STATS2
  249. atomic_dec(&server->num_waiters);
  250. #endif
  251. spin_lock(&GlobalMid_Lock);
  252. } else {
  253. if (server->tcpStatus == CifsExiting) {
  254. spin_unlock(&GlobalMid_Lock);
  255. return -ENOENT;
  256. }
  257. /* can not count locking commands against total
  258. as they are allowed to block on server */
  259. /* update # of requests on the wire to server */
  260. if (long_op != CIFS_BLOCKING_OP)
  261. atomic_inc(&server->inFlight);
  262. spin_unlock(&GlobalMid_Lock);
  263. break;
  264. }
  265. }
  266. return 0;
  267. }
  268. static int allocate_mid(struct cifsSesInfo *ses, struct smb_hdr *in_buf,
  269. struct mid_q_entry **ppmidQ)
  270. {
  271. if (ses->server->tcpStatus == CifsExiting) {
  272. return -ENOENT;
  273. }
  274. if (ses->server->tcpStatus == CifsNeedReconnect) {
  275. cFYI(1, "tcp session dead - return to caller to retry");
  276. return -EAGAIN;
  277. }
  278. if (ses->status != CifsGood) {
  279. /* check if SMB session is bad because we are setting it up */
  280. if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
  281. (in_buf->Command != SMB_COM_NEGOTIATE))
  282. return -EAGAIN;
  283. /* else ok - we are setting up session */
  284. }
  285. *ppmidQ = AllocMidQEntry(in_buf, ses->server);
  286. if (*ppmidQ == NULL)
  287. return -ENOMEM;
  288. spin_lock(&GlobalMid_Lock);
  289. list_add_tail(&(*ppmidQ)->qhead, &ses->server->pending_mid_q);
  290. spin_unlock(&GlobalMid_Lock);
  291. return 0;
  292. }
  293. static int
  294. wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *midQ)
  295. {
  296. int error;
  297. error = wait_event_killable(server->response_q,
  298. midQ->midState != MID_REQUEST_SUBMITTED);
  299. if (error < 0)
  300. return -ERESTARTSYS;
  301. return 0;
  302. }
  303. /*
  304. * Send a SMB request and set the callback function in the mid to handle
  305. * the result. Caller is responsible for dealing with timeouts.
  306. */
  307. int
  308. cifs_call_async(struct TCP_Server_Info *server, struct smb_hdr *in_buf,
  309. mid_callback_t *callback, void *cbdata)
  310. {
  311. int rc;
  312. struct mid_q_entry *mid;
  313. rc = wait_for_free_request(server, CIFS_ASYNC_OP);
  314. if (rc)
  315. return rc;
  316. /* enable signing if server requires it */
  317. if (server->secMode & (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
  318. in_buf->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
  319. mutex_lock(&server->srv_mutex);
  320. mid = AllocMidQEntry(in_buf, server);
  321. if (mid == NULL) {
  322. mutex_unlock(&server->srv_mutex);
  323. return -ENOMEM;
  324. }
  325. /* put it on the pending_mid_q */
  326. spin_lock(&GlobalMid_Lock);
  327. list_add_tail(&mid->qhead, &server->pending_mid_q);
  328. spin_unlock(&GlobalMid_Lock);
  329. rc = cifs_sign_smb(in_buf, server, &mid->sequence_number);
  330. if (rc) {
  331. mutex_unlock(&server->srv_mutex);
  332. goto out_err;
  333. }
  334. mid->callback = callback;
  335. mid->callback_data = cbdata;
  336. mid->midState = MID_REQUEST_SUBMITTED;
  337. #ifdef CONFIG_CIFS_STATS2
  338. atomic_inc(&server->inSend);
  339. #endif
  340. rc = smb_send(server, in_buf, be32_to_cpu(in_buf->smb_buf_length));
  341. #ifdef CONFIG_CIFS_STATS2
  342. atomic_dec(&server->inSend);
  343. mid->when_sent = jiffies;
  344. #endif
  345. mutex_unlock(&server->srv_mutex);
  346. if (rc)
  347. goto out_err;
  348. return rc;
  349. out_err:
  350. delete_mid(mid);
  351. atomic_dec(&server->inFlight);
  352. wake_up(&server->request_q);
  353. return rc;
  354. }
  355. /*
  356. *
  357. * Send an SMB Request. No response info (other than return code)
  358. * needs to be parsed.
  359. *
  360. * flags indicate the type of request buffer and how long to wait
  361. * and whether to log NT STATUS code (error) before mapping it to POSIX error
  362. *
  363. */
  364. int
  365. SendReceiveNoRsp(const unsigned int xid, struct cifsSesInfo *ses,
  366. struct smb_hdr *in_buf, int flags)
  367. {
  368. int rc;
  369. struct kvec iov[1];
  370. int resp_buf_type;
  371. iov[0].iov_base = (char *)in_buf;
  372. iov[0].iov_len = be32_to_cpu(in_buf->smb_buf_length) + 4;
  373. flags |= CIFS_NO_RESP;
  374. rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags);
  375. cFYI(DBG2, "SendRcvNoRsp flags %d rc %d", flags, rc);
  376. return rc;
  377. }
  378. static int
  379. sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
  380. {
  381. int rc = 0;
  382. cFYI(1, "%s: cmd=%d mid=%d state=%d", __func__, mid->command,
  383. mid->mid, mid->midState);
  384. spin_lock(&GlobalMid_Lock);
  385. /* ensure that it's no longer on the pending_mid_q */
  386. list_del_init(&mid->qhead);
  387. switch (mid->midState) {
  388. case MID_RESPONSE_RECEIVED:
  389. spin_unlock(&GlobalMid_Lock);
  390. return rc;
  391. case MID_REQUEST_SUBMITTED:
  392. /* socket is going down, reject all calls */
  393. if (server->tcpStatus == CifsExiting) {
  394. cERROR(1, "%s: canceling mid=%d cmd=0x%x state=%d",
  395. __func__, mid->mid, mid->command, mid->midState);
  396. rc = -EHOSTDOWN;
  397. break;
  398. }
  399. case MID_RETRY_NEEDED:
  400. rc = -EAGAIN;
  401. break;
  402. case MID_RESPONSE_MALFORMED:
  403. rc = -EIO;
  404. break;
  405. default:
  406. cERROR(1, "%s: invalid mid state mid=%d state=%d", __func__,
  407. mid->mid, mid->midState);
  408. rc = -EIO;
  409. }
  410. spin_unlock(&GlobalMid_Lock);
  411. DeleteMidQEntry(mid);
  412. return rc;
  413. }
  414. /*
  415. * An NT cancel request header looks just like the original request except:
  416. *
  417. * The Command is SMB_COM_NT_CANCEL
  418. * The WordCount is zeroed out
  419. * The ByteCount is zeroed out
  420. *
  421. * This function mangles an existing request buffer into a
  422. * SMB_COM_NT_CANCEL request and then sends it.
  423. */
  424. static int
  425. send_nt_cancel(struct TCP_Server_Info *server, struct smb_hdr *in_buf,
  426. struct mid_q_entry *mid)
  427. {
  428. int rc = 0;
  429. /* -4 for RFC1001 length and +2 for BCC field */
  430. in_buf->smb_buf_length = cpu_to_be32(sizeof(struct smb_hdr) - 4 + 2);
  431. in_buf->Command = SMB_COM_NT_CANCEL;
  432. in_buf->WordCount = 0;
  433. put_bcc(0, in_buf);
  434. mutex_lock(&server->srv_mutex);
  435. rc = cifs_sign_smb(in_buf, server, &mid->sequence_number);
  436. if (rc) {
  437. mutex_unlock(&server->srv_mutex);
  438. return rc;
  439. }
  440. rc = smb_send(server, in_buf, be32_to_cpu(in_buf->smb_buf_length));
  441. mutex_unlock(&server->srv_mutex);
  442. cFYI(1, "issued NT_CANCEL for mid %u, rc = %d",
  443. in_buf->Mid, rc);
  444. return rc;
  445. }
  446. int
  447. SendReceive2(const unsigned int xid, struct cifsSesInfo *ses,
  448. struct kvec *iov, int n_vec, int *pRespBufType /* ret */,
  449. const int flags)
  450. {
  451. int rc = 0;
  452. int long_op;
  453. unsigned int receive_len;
  454. struct mid_q_entry *midQ;
  455. struct smb_hdr *in_buf = iov[0].iov_base;
  456. long_op = flags & CIFS_TIMEOUT_MASK;
  457. *pRespBufType = CIFS_NO_BUFFER; /* no response buf yet */
  458. if ((ses == NULL) || (ses->server == NULL)) {
  459. cifs_small_buf_release(in_buf);
  460. cERROR(1, "Null session");
  461. return -EIO;
  462. }
  463. if (ses->server->tcpStatus == CifsExiting) {
  464. cifs_small_buf_release(in_buf);
  465. return -ENOENT;
  466. }
  467. /* Ensure that we do not send more than 50 overlapping requests
  468. to the same server. We may make this configurable later or
  469. use ses->maxReq */
  470. rc = wait_for_free_request(ses->server, long_op);
  471. if (rc) {
  472. cifs_small_buf_release(in_buf);
  473. return rc;
  474. }
  475. /* make sure that we sign in the same order that we send on this socket
  476. and avoid races inside tcp sendmsg code that could cause corruption
  477. of smb data */
  478. mutex_lock(&ses->server->srv_mutex);
  479. rc = allocate_mid(ses, in_buf, &midQ);
  480. if (rc) {
  481. mutex_unlock(&ses->server->srv_mutex);
  482. cifs_small_buf_release(in_buf);
  483. /* Update # of requests on wire to server */
  484. atomic_dec(&ses->server->inFlight);
  485. wake_up(&ses->server->request_q);
  486. return rc;
  487. }
  488. rc = cifs_sign_smb2(iov, n_vec, ses->server, &midQ->sequence_number);
  489. if (rc) {
  490. mutex_unlock(&ses->server->srv_mutex);
  491. cifs_small_buf_release(in_buf);
  492. goto out;
  493. }
  494. midQ->midState = MID_REQUEST_SUBMITTED;
  495. #ifdef CONFIG_CIFS_STATS2
  496. atomic_inc(&ses->server->inSend);
  497. #endif
  498. rc = smb_sendv(ses->server, iov, n_vec);
  499. #ifdef CONFIG_CIFS_STATS2
  500. atomic_dec(&ses->server->inSend);
  501. midQ->when_sent = jiffies;
  502. #endif
  503. mutex_unlock(&ses->server->srv_mutex);
  504. if (rc < 0) {
  505. cifs_small_buf_release(in_buf);
  506. goto out;
  507. }
  508. if (long_op == CIFS_ASYNC_OP) {
  509. cifs_small_buf_release(in_buf);
  510. goto out;
  511. }
  512. rc = wait_for_response(ses->server, midQ);
  513. if (rc != 0) {
  514. send_nt_cancel(ses->server, in_buf, midQ);
  515. spin_lock(&GlobalMid_Lock);
  516. if (midQ->midState == MID_REQUEST_SUBMITTED) {
  517. midQ->callback = DeleteMidQEntry;
  518. spin_unlock(&GlobalMid_Lock);
  519. cifs_small_buf_release(in_buf);
  520. atomic_dec(&ses->server->inFlight);
  521. wake_up(&ses->server->request_q);
  522. return rc;
  523. }
  524. spin_unlock(&GlobalMid_Lock);
  525. }
  526. cifs_small_buf_release(in_buf);
  527. rc = sync_mid_result(midQ, ses->server);
  528. if (rc != 0) {
  529. atomic_dec(&ses->server->inFlight);
  530. wake_up(&ses->server->request_q);
  531. return rc;
  532. }
  533. receive_len = be32_to_cpu(midQ->resp_buf->smb_buf_length);
  534. if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
  535. cERROR(1, "Frame too large received. Length: %d Xid: %d",
  536. receive_len, xid);
  537. rc = -EIO;
  538. goto out;
  539. }
  540. /* rcvd frame is ok */
  541. if (midQ->resp_buf &&
  542. (midQ->midState == MID_RESPONSE_RECEIVED)) {
  543. iov[0].iov_base = (char *)midQ->resp_buf;
  544. if (midQ->largeBuf)
  545. *pRespBufType = CIFS_LARGE_BUFFER;
  546. else
  547. *pRespBufType = CIFS_SMALL_BUFFER;
  548. iov[0].iov_len = receive_len + 4;
  549. dump_smb(midQ->resp_buf, 80);
  550. /* convert the length into a more usable form */
  551. if ((receive_len > 24) &&
  552. (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
  553. SECMODE_SIGN_ENABLED))) {
  554. rc = cifs_verify_signature(midQ->resp_buf,
  555. ses->server,
  556. midQ->sequence_number+1);
  557. if (rc) {
  558. cERROR(1, "Unexpected SMB signature");
  559. /* BB FIXME add code to kill session */
  560. }
  561. }
  562. /* BB special case reconnect tid and uid here? */
  563. rc = map_smb_to_linux_error(midQ->resp_buf,
  564. flags & CIFS_LOG_ERROR);
  565. if ((flags & CIFS_NO_RESP) == 0)
  566. midQ->resp_buf = NULL; /* mark it so buf will
  567. not be freed by
  568. delete_mid */
  569. } else {
  570. rc = -EIO;
  571. cFYI(1, "Bad MID state?");
  572. }
  573. out:
  574. delete_mid(midQ);
  575. atomic_dec(&ses->server->inFlight);
  576. wake_up(&ses->server->request_q);
  577. return rc;
  578. }
  579. int
  580. SendReceive(const unsigned int xid, struct cifsSesInfo *ses,
  581. struct smb_hdr *in_buf, struct smb_hdr *out_buf,
  582. int *pbytes_returned, const int long_op)
  583. {
  584. int rc = 0;
  585. unsigned int receive_len;
  586. struct mid_q_entry *midQ;
  587. if (ses == NULL) {
  588. cERROR(1, "Null smb session");
  589. return -EIO;
  590. }
  591. if (ses->server == NULL) {
  592. cERROR(1, "Null tcp session");
  593. return -EIO;
  594. }
  595. if (ses->server->tcpStatus == CifsExiting)
  596. return -ENOENT;
  597. /* Ensure that we do not send more than 50 overlapping requests
  598. to the same server. We may make this configurable later or
  599. use ses->maxReq */
  600. if (be32_to_cpu(in_buf->smb_buf_length) > CIFSMaxBufSize +
  601. MAX_CIFS_HDR_SIZE - 4) {
  602. cERROR(1, "Illegal length, greater than maximum frame, %d",
  603. be32_to_cpu(in_buf->smb_buf_length));
  604. return -EIO;
  605. }
  606. rc = wait_for_free_request(ses->server, long_op);
  607. if (rc)
  608. return rc;
  609. /* make sure that we sign in the same order that we send on this socket
  610. and avoid races inside tcp sendmsg code that could cause corruption
  611. of smb data */
  612. mutex_lock(&ses->server->srv_mutex);
  613. rc = allocate_mid(ses, in_buf, &midQ);
  614. if (rc) {
  615. mutex_unlock(&ses->server->srv_mutex);
  616. /* Update # of requests on wire to server */
  617. atomic_dec(&ses->server->inFlight);
  618. wake_up(&ses->server->request_q);
  619. return rc;
  620. }
  621. rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
  622. if (rc) {
  623. mutex_unlock(&ses->server->srv_mutex);
  624. goto out;
  625. }
  626. midQ->midState = MID_REQUEST_SUBMITTED;
  627. #ifdef CONFIG_CIFS_STATS2
  628. atomic_inc(&ses->server->inSend);
  629. #endif
  630. rc = smb_send(ses->server, in_buf, be32_to_cpu(in_buf->smb_buf_length));
  631. #ifdef CONFIG_CIFS_STATS2
  632. atomic_dec(&ses->server->inSend);
  633. midQ->when_sent = jiffies;
  634. #endif
  635. mutex_unlock(&ses->server->srv_mutex);
  636. if (rc < 0)
  637. goto out;
  638. if (long_op == CIFS_ASYNC_OP)
  639. goto out;
  640. rc = wait_for_response(ses->server, midQ);
  641. if (rc != 0) {
  642. send_nt_cancel(ses->server, in_buf, midQ);
  643. spin_lock(&GlobalMid_Lock);
  644. if (midQ->midState == MID_REQUEST_SUBMITTED) {
  645. /* no longer considered to be "in-flight" */
  646. midQ->callback = DeleteMidQEntry;
  647. spin_unlock(&GlobalMid_Lock);
  648. atomic_dec(&ses->server->inFlight);
  649. wake_up(&ses->server->request_q);
  650. return rc;
  651. }
  652. spin_unlock(&GlobalMid_Lock);
  653. }
  654. rc = sync_mid_result(midQ, ses->server);
  655. if (rc != 0) {
  656. atomic_dec(&ses->server->inFlight);
  657. wake_up(&ses->server->request_q);
  658. return rc;
  659. }
  660. receive_len = be32_to_cpu(midQ->resp_buf->smb_buf_length);
  661. if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
  662. cERROR(1, "Frame too large received. Length: %d Xid: %d",
  663. receive_len, xid);
  664. rc = -EIO;
  665. goto out;
  666. }
  667. /* rcvd frame is ok */
  668. if (midQ->resp_buf && out_buf
  669. && (midQ->midState == MID_RESPONSE_RECEIVED)) {
  670. out_buf->smb_buf_length = cpu_to_be32(receive_len);
  671. memcpy((char *)out_buf + 4,
  672. (char *)midQ->resp_buf + 4,
  673. receive_len);
  674. dump_smb(out_buf, 92);
  675. /* convert the length into a more usable form */
  676. if ((receive_len > 24) &&
  677. (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
  678. SECMODE_SIGN_ENABLED))) {
  679. rc = cifs_verify_signature(out_buf,
  680. ses->server,
  681. midQ->sequence_number+1);
  682. if (rc) {
  683. cERROR(1, "Unexpected SMB signature");
  684. /* BB FIXME add code to kill session */
  685. }
  686. }
  687. *pbytes_returned = be32_to_cpu(out_buf->smb_buf_length);
  688. /* BB special case reconnect tid and uid here? */
  689. rc = map_smb_to_linux_error(out_buf, 0 /* no log */ );
  690. } else {
  691. rc = -EIO;
  692. cERROR(1, "Bad MID state?");
  693. }
  694. out:
  695. delete_mid(midQ);
  696. atomic_dec(&ses->server->inFlight);
  697. wake_up(&ses->server->request_q);
  698. return rc;
  699. }
  700. /* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
  701. blocking lock to return. */
  702. static int
  703. send_lock_cancel(const unsigned int xid, struct cifsTconInfo *tcon,
  704. struct smb_hdr *in_buf,
  705. struct smb_hdr *out_buf)
  706. {
  707. int bytes_returned;
  708. struct cifsSesInfo *ses = tcon->ses;
  709. LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
  710. /* We just modify the current in_buf to change
  711. the type of lock from LOCKING_ANDX_SHARED_LOCK
  712. or LOCKING_ANDX_EXCLUSIVE_LOCK to
  713. LOCKING_ANDX_CANCEL_LOCK. */
  714. pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
  715. pSMB->Timeout = 0;
  716. pSMB->hdr.Mid = GetNextMid(ses->server);
  717. return SendReceive(xid, ses, in_buf, out_buf,
  718. &bytes_returned, 0);
  719. }
  720. int
  721. SendReceiveBlockingLock(const unsigned int xid, struct cifsTconInfo *tcon,
  722. struct smb_hdr *in_buf, struct smb_hdr *out_buf,
  723. int *pbytes_returned)
  724. {
  725. int rc = 0;
  726. int rstart = 0;
  727. unsigned int receive_len;
  728. struct mid_q_entry *midQ;
  729. struct cifsSesInfo *ses;
  730. if (tcon == NULL || tcon->ses == NULL) {
  731. cERROR(1, "Null smb session");
  732. return -EIO;
  733. }
  734. ses = tcon->ses;
  735. if (ses->server == NULL) {
  736. cERROR(1, "Null tcp session");
  737. return -EIO;
  738. }
  739. if (ses->server->tcpStatus == CifsExiting)
  740. return -ENOENT;
  741. /* Ensure that we do not send more than 50 overlapping requests
  742. to the same server. We may make this configurable later or
  743. use ses->maxReq */
  744. if (be32_to_cpu(in_buf->smb_buf_length) > CIFSMaxBufSize +
  745. MAX_CIFS_HDR_SIZE - 4) {
  746. cERROR(1, "Illegal length, greater than maximum frame, %d",
  747. be32_to_cpu(in_buf->smb_buf_length));
  748. return -EIO;
  749. }
  750. rc = wait_for_free_request(ses->server, CIFS_BLOCKING_OP);
  751. if (rc)
  752. return rc;
  753. /* make sure that we sign in the same order that we send on this socket
  754. and avoid races inside tcp sendmsg code that could cause corruption
  755. of smb data */
  756. mutex_lock(&ses->server->srv_mutex);
  757. rc = allocate_mid(ses, in_buf, &midQ);
  758. if (rc) {
  759. mutex_unlock(&ses->server->srv_mutex);
  760. return rc;
  761. }
  762. rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
  763. if (rc) {
  764. delete_mid(midQ);
  765. mutex_unlock(&ses->server->srv_mutex);
  766. return rc;
  767. }
  768. midQ->midState = MID_REQUEST_SUBMITTED;
  769. #ifdef CONFIG_CIFS_STATS2
  770. atomic_inc(&ses->server->inSend);
  771. #endif
  772. rc = smb_send(ses->server, in_buf, be32_to_cpu(in_buf->smb_buf_length));
  773. #ifdef CONFIG_CIFS_STATS2
  774. atomic_dec(&ses->server->inSend);
  775. midQ->when_sent = jiffies;
  776. #endif
  777. mutex_unlock(&ses->server->srv_mutex);
  778. if (rc < 0) {
  779. delete_mid(midQ);
  780. return rc;
  781. }
  782. /* Wait for a reply - allow signals to interrupt. */
  783. rc = wait_event_interruptible(ses->server->response_q,
  784. (!(midQ->midState == MID_REQUEST_SUBMITTED)) ||
  785. ((ses->server->tcpStatus != CifsGood) &&
  786. (ses->server->tcpStatus != CifsNew)));
  787. /* Were we interrupted by a signal ? */
  788. if ((rc == -ERESTARTSYS) &&
  789. (midQ->midState == MID_REQUEST_SUBMITTED) &&
  790. ((ses->server->tcpStatus == CifsGood) ||
  791. (ses->server->tcpStatus == CifsNew))) {
  792. if (in_buf->Command == SMB_COM_TRANSACTION2) {
  793. /* POSIX lock. We send a NT_CANCEL SMB to cause the
  794. blocking lock to return. */
  795. rc = send_nt_cancel(ses->server, in_buf, midQ);
  796. if (rc) {
  797. delete_mid(midQ);
  798. return rc;
  799. }
  800. } else {
  801. /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
  802. to cause the blocking lock to return. */
  803. rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
  804. /* If we get -ENOLCK back the lock may have
  805. already been removed. Don't exit in this case. */
  806. if (rc && rc != -ENOLCK) {
  807. delete_mid(midQ);
  808. return rc;
  809. }
  810. }
  811. rc = wait_for_response(ses->server, midQ);
  812. if (rc) {
  813. send_nt_cancel(ses->server, in_buf, midQ);
  814. spin_lock(&GlobalMid_Lock);
  815. if (midQ->midState == MID_REQUEST_SUBMITTED) {
  816. /* no longer considered to be "in-flight" */
  817. midQ->callback = DeleteMidQEntry;
  818. spin_unlock(&GlobalMid_Lock);
  819. return rc;
  820. }
  821. spin_unlock(&GlobalMid_Lock);
  822. }
  823. /* We got the response - restart system call. */
  824. rstart = 1;
  825. }
  826. rc = sync_mid_result(midQ, ses->server);
  827. if (rc != 0)
  828. return rc;
  829. receive_len = be32_to_cpu(midQ->resp_buf->smb_buf_length);
  830. if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
  831. cERROR(1, "Frame too large received. Length: %d Xid: %d",
  832. receive_len, xid);
  833. rc = -EIO;
  834. goto out;
  835. }
  836. /* rcvd frame is ok */
  837. if ((out_buf == NULL) || (midQ->midState != MID_RESPONSE_RECEIVED)) {
  838. rc = -EIO;
  839. cERROR(1, "Bad MID state?");
  840. goto out;
  841. }
  842. out_buf->smb_buf_length = cpu_to_be32(receive_len);
  843. memcpy((char *)out_buf + 4,
  844. (char *)midQ->resp_buf + 4,
  845. receive_len);
  846. dump_smb(out_buf, 92);
  847. /* convert the length into a more usable form */
  848. if ((receive_len > 24) &&
  849. (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
  850. SECMODE_SIGN_ENABLED))) {
  851. rc = cifs_verify_signature(out_buf,
  852. ses->server,
  853. midQ->sequence_number+1);
  854. if (rc) {
  855. cERROR(1, "Unexpected SMB signature");
  856. /* BB FIXME add code to kill session */
  857. }
  858. }
  859. *pbytes_returned = be32_to_cpu(out_buf->smb_buf_length);
  860. /* BB special case reconnect tid and uid here? */
  861. rc = map_smb_to_linux_error(out_buf, 0 /* no log */ );
  862. out:
  863. delete_mid(midQ);
  864. if (rstart && rc == -EACCES)
  865. return -ERESTARTSYS;
  866. return rc;
  867. }