transport.c 29 KB

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