transport.c 26 KB

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