transport.c 27 KB

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