transport.c 27 KB

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