transport.c 29 KB

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