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