transport.c 28 KB

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