transport.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  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/gfp.h>
  25. #include <linux/wait.h>
  26. #include <linux/net.h>
  27. #include <linux/delay.h>
  28. #include <linux/freezer.h>
  29. #include <linux/tcp.h>
  30. #include <linux/bvec.h>
  31. #include <linux/highmem.h>
  32. #include <linux/uaccess.h>
  33. #include <asm/processor.h>
  34. #include <linux/mempool.h>
  35. #include "cifspdu.h"
  36. #include "cifsglob.h"
  37. #include "cifsproto.h"
  38. #include "cifs_debug.h"
  39. #include "smb2proto.h"
  40. #include "smbdirect.h"
  41. /* Max number of iovectors we can use off the stack when sending requests. */
  42. #define CIFS_MAX_IOV_SIZE 8
  43. void
  44. cifs_wake_up_task(struct mid_q_entry *mid)
  45. {
  46. wake_up_process(mid->callback_data);
  47. }
  48. struct mid_q_entry *
  49. AllocMidQEntry(const struct smb_hdr *smb_buffer, struct TCP_Server_Info *server)
  50. {
  51. struct mid_q_entry *temp;
  52. if (server == NULL) {
  53. cifs_dbg(VFS, "Null TCP session in AllocMidQEntry\n");
  54. return NULL;
  55. }
  56. temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
  57. memset(temp, 0, sizeof(struct mid_q_entry));
  58. kref_init(&temp->refcount);
  59. temp->mid = get_mid(smb_buffer);
  60. temp->pid = current->pid;
  61. temp->command = cpu_to_le16(smb_buffer->Command);
  62. cifs_dbg(FYI, "For smb_command %d\n", smb_buffer->Command);
  63. /* do_gettimeofday(&temp->when_sent);*/ /* easier to use jiffies */
  64. /* when mid allocated can be before when sent */
  65. temp->when_alloc = jiffies;
  66. temp->server = server;
  67. /*
  68. * The default is for the mid to be synchronous, so the
  69. * default callback just wakes up the current task.
  70. */
  71. temp->callback = cifs_wake_up_task;
  72. temp->callback_data = current;
  73. atomic_inc(&midCount);
  74. temp->mid_state = MID_REQUEST_ALLOCATED;
  75. return temp;
  76. }
  77. static void _cifs_mid_q_entry_release(struct kref *refcount)
  78. {
  79. struct mid_q_entry *mid = container_of(refcount, struct mid_q_entry,
  80. refcount);
  81. mempool_free(mid, cifs_mid_poolp);
  82. }
  83. void cifs_mid_q_entry_release(struct mid_q_entry *midEntry)
  84. {
  85. spin_lock(&GlobalMid_Lock);
  86. kref_put(&midEntry->refcount, _cifs_mid_q_entry_release);
  87. spin_unlock(&GlobalMid_Lock);
  88. }
  89. void
  90. DeleteMidQEntry(struct mid_q_entry *midEntry)
  91. {
  92. #ifdef CONFIG_CIFS_STATS2
  93. __le16 command = midEntry->server->vals->lock_cmd;
  94. unsigned long now;
  95. #endif
  96. midEntry->mid_state = MID_FREE;
  97. atomic_dec(&midCount);
  98. if (midEntry->large_buf)
  99. cifs_buf_release(midEntry->resp_buf);
  100. else
  101. cifs_small_buf_release(midEntry->resp_buf);
  102. #ifdef CONFIG_CIFS_STATS2
  103. now = jiffies;
  104. /*
  105. * commands taking longer than one second (default) can be indications
  106. * that something is wrong, unless it is quite a slow link or a very
  107. * busy server. Note that this calc is unlikely or impossible to wrap
  108. * as long as slow_rsp_threshold is not set way above recommended max
  109. * value (32767 ie 9 hours) and is generally harmless even if wrong
  110. * since only affects debug counters - so leaving the calc as simple
  111. * comparison rather than doing multiple conversions and overflow
  112. * checks
  113. */
  114. if ((slow_rsp_threshold != 0) &&
  115. time_after(now, midEntry->when_alloc + (slow_rsp_threshold * HZ)) &&
  116. (midEntry->command != command)) {
  117. /* smb2slowcmd[NUMBER_OF_SMB2_COMMANDS] counts by command */
  118. if ((le16_to_cpu(midEntry->command) < NUMBER_OF_SMB2_COMMANDS) &&
  119. (le16_to_cpu(midEntry->command) >= 0))
  120. cifs_stats_inc(&midEntry->server->smb2slowcmd[le16_to_cpu(midEntry->command)]);
  121. trace_smb3_slow_rsp(le16_to_cpu(midEntry->command),
  122. midEntry->mid, midEntry->pid,
  123. midEntry->when_sent, midEntry->when_received);
  124. if (cifsFYI & CIFS_TIMER) {
  125. pr_debug(" CIFS slow rsp: cmd %d mid %llu",
  126. midEntry->command, midEntry->mid);
  127. cifs_info(" A: 0x%lx S: 0x%lx R: 0x%lx\n",
  128. now - midEntry->when_alloc,
  129. now - midEntry->when_sent,
  130. now - midEntry->when_received);
  131. }
  132. }
  133. #endif
  134. cifs_mid_q_entry_release(midEntry);
  135. }
  136. void
  137. cifs_delete_mid(struct mid_q_entry *mid)
  138. {
  139. spin_lock(&GlobalMid_Lock);
  140. list_del_init(&mid->qhead);
  141. mid->mid_flags |= MID_DELETED;
  142. spin_unlock(&GlobalMid_Lock);
  143. DeleteMidQEntry(mid);
  144. }
  145. /*
  146. * smb_send_kvec - send an array of kvecs to the server
  147. * @server: Server to send the data to
  148. * @smb_msg: Message to send
  149. * @sent: amount of data sent on socket is stored here
  150. *
  151. * Our basic "send data to server" function. Should be called with srv_mutex
  152. * held. The caller is responsible for handling the results.
  153. */
  154. static int
  155. smb_send_kvec(struct TCP_Server_Info *server, struct msghdr *smb_msg,
  156. size_t *sent)
  157. {
  158. int rc = 0;
  159. int retries = 0;
  160. struct socket *ssocket = server->ssocket;
  161. *sent = 0;
  162. smb_msg->msg_name = (struct sockaddr *) &server->dstaddr;
  163. smb_msg->msg_namelen = sizeof(struct sockaddr);
  164. smb_msg->msg_control = NULL;
  165. smb_msg->msg_controllen = 0;
  166. if (server->noblocksnd)
  167. smb_msg->msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL;
  168. else
  169. smb_msg->msg_flags = MSG_NOSIGNAL;
  170. while (msg_data_left(smb_msg)) {
  171. /*
  172. * If blocking send, we try 3 times, since each can block
  173. * for 5 seconds. For nonblocking we have to try more
  174. * but wait increasing amounts of time allowing time for
  175. * socket to clear. The overall time we wait in either
  176. * case to send on the socket is about 15 seconds.
  177. * Similarly we wait for 15 seconds for a response from
  178. * the server in SendReceive[2] for the server to send
  179. * a response back for most types of requests (except
  180. * SMB Write past end of file which can be slow, and
  181. * blocking lock operations). NFS waits slightly longer
  182. * than CIFS, but this can make it take longer for
  183. * nonresponsive servers to be detected and 15 seconds
  184. * is more than enough time for modern networks to
  185. * send a packet. In most cases if we fail to send
  186. * after the retries we will kill the socket and
  187. * reconnect which may clear the network problem.
  188. */
  189. rc = sock_sendmsg(ssocket, smb_msg);
  190. if (rc == -EAGAIN) {
  191. retries++;
  192. if (retries >= 14 ||
  193. (!server->noblocksnd && (retries > 2))) {
  194. cifs_dbg(VFS, "sends on sock %p stuck for 15 seconds\n",
  195. ssocket);
  196. return -EAGAIN;
  197. }
  198. msleep(1 << retries);
  199. continue;
  200. }
  201. if (rc < 0)
  202. return rc;
  203. if (rc == 0) {
  204. /* should never happen, letting socket clear before
  205. retrying is our only obvious option here */
  206. cifs_dbg(VFS, "tcp sent no data\n");
  207. msleep(500);
  208. continue;
  209. }
  210. /* send was at least partially successful */
  211. *sent += rc;
  212. retries = 0; /* in case we get ENOSPC on the next send */
  213. }
  214. return 0;
  215. }
  216. unsigned long
  217. smb_rqst_len(struct TCP_Server_Info *server, struct smb_rqst *rqst)
  218. {
  219. unsigned int i;
  220. struct kvec *iov;
  221. int nvec;
  222. unsigned long buflen = 0;
  223. if (server->vals->header_preamble_size == 0 &&
  224. rqst->rq_nvec >= 2 && rqst->rq_iov[0].iov_len == 4) {
  225. iov = &rqst->rq_iov[1];
  226. nvec = rqst->rq_nvec - 1;
  227. } else {
  228. iov = rqst->rq_iov;
  229. nvec = rqst->rq_nvec;
  230. }
  231. /* total up iov array first */
  232. for (i = 0; i < nvec; i++)
  233. buflen += iov[i].iov_len;
  234. /*
  235. * Add in the page array if there is one. The caller needs to make
  236. * sure rq_offset and rq_tailsz are set correctly. If a buffer of
  237. * multiple pages ends at page boundary, rq_tailsz needs to be set to
  238. * PAGE_SIZE.
  239. */
  240. if (rqst->rq_npages) {
  241. if (rqst->rq_npages == 1)
  242. buflen += rqst->rq_tailsz;
  243. else {
  244. /*
  245. * If there is more than one page, calculate the
  246. * buffer length based on rq_offset and rq_tailsz
  247. */
  248. buflen += rqst->rq_pagesz * (rqst->rq_npages - 1) -
  249. rqst->rq_offset;
  250. buflen += rqst->rq_tailsz;
  251. }
  252. }
  253. return buflen;
  254. }
  255. static int
  256. __smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
  257. struct smb_rqst *rqst)
  258. {
  259. int rc = 0;
  260. struct kvec *iov;
  261. int n_vec;
  262. unsigned int send_length = 0;
  263. unsigned int i, j;
  264. size_t total_len = 0, sent, size;
  265. struct socket *ssocket = server->ssocket;
  266. struct msghdr smb_msg;
  267. int val = 1;
  268. __be32 rfc1002_marker;
  269. if (cifs_rdma_enabled(server) && server->smbd_conn) {
  270. rc = smbd_send(server, rqst);
  271. goto smbd_done;
  272. }
  273. if (ssocket == NULL)
  274. return -ENOTSOCK;
  275. /* cork the socket */
  276. kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
  277. (char *)&val, sizeof(val));
  278. for (j = 0; j < num_rqst; j++)
  279. send_length += smb_rqst_len(server, &rqst[j]);
  280. rfc1002_marker = cpu_to_be32(send_length);
  281. /* Generate a rfc1002 marker for SMB2+ */
  282. if (server->vals->header_preamble_size == 0) {
  283. struct kvec hiov = {
  284. .iov_base = &rfc1002_marker,
  285. .iov_len = 4
  286. };
  287. iov_iter_kvec(&smb_msg.msg_iter, WRITE, &hiov, 1, 4);
  288. rc = smb_send_kvec(server, &smb_msg, &sent);
  289. if (rc < 0)
  290. goto uncork;
  291. total_len += sent;
  292. send_length += 4;
  293. }
  294. cifs_dbg(FYI, "Sending smb: smb_len=%u\n", send_length);
  295. for (j = 0; j < num_rqst; j++) {
  296. iov = rqst[j].rq_iov;
  297. n_vec = rqst[j].rq_nvec;
  298. size = 0;
  299. for (i = 0; i < n_vec; i++) {
  300. dump_smb(iov[i].iov_base, iov[i].iov_len);
  301. size += iov[i].iov_len;
  302. }
  303. iov_iter_kvec(&smb_msg.msg_iter, WRITE, iov, n_vec, size);
  304. rc = smb_send_kvec(server, &smb_msg, &sent);
  305. if (rc < 0)
  306. goto uncork;
  307. total_len += sent;
  308. /* now walk the page array and send each page in it */
  309. for (i = 0; i < rqst[j].rq_npages; i++) {
  310. struct bio_vec bvec;
  311. bvec.bv_page = rqst[j].rq_pages[i];
  312. rqst_page_get_length(&rqst[j], i, &bvec.bv_len,
  313. &bvec.bv_offset);
  314. iov_iter_bvec(&smb_msg.msg_iter, WRITE,
  315. &bvec, 1, bvec.bv_len);
  316. rc = smb_send_kvec(server, &smb_msg, &sent);
  317. if (rc < 0)
  318. break;
  319. total_len += sent;
  320. }
  321. }
  322. uncork:
  323. /* uncork it */
  324. val = 0;
  325. kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
  326. (char *)&val, sizeof(val));
  327. if ((total_len > 0) && (total_len != send_length)) {
  328. cifs_dbg(FYI, "partial send (wanted=%u sent=%zu): terminating session\n",
  329. send_length, total_len);
  330. /*
  331. * If we have only sent part of an SMB then the next SMB could
  332. * be taken as the remainder of this one. We need to kill the
  333. * socket so the server throws away the partial SMB
  334. */
  335. server->tcpStatus = CifsNeedReconnect;
  336. trace_smb3_partial_send_reconnect(server->CurrentMid,
  337. server->hostname);
  338. }
  339. smbd_done:
  340. if (rc < 0 && rc != -EINTR)
  341. cifs_dbg(VFS, "Error %d sending data on socket to server\n",
  342. rc);
  343. else
  344. rc = 0;
  345. return rc;
  346. }
  347. static int
  348. smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
  349. struct smb_rqst *rqst, int flags)
  350. {
  351. struct kvec iov;
  352. struct smb2_transform_hdr tr_hdr;
  353. struct smb_rqst cur_rqst[MAX_COMPOUND];
  354. int rc;
  355. if (!(flags & CIFS_TRANSFORM_REQ))
  356. return __smb_send_rqst(server, num_rqst, rqst);
  357. if (num_rqst > MAX_COMPOUND - 1)
  358. return -ENOMEM;
  359. memset(&cur_rqst[0], 0, sizeof(cur_rqst));
  360. memset(&iov, 0, sizeof(iov));
  361. memset(&tr_hdr, 0, sizeof(tr_hdr));
  362. iov.iov_base = &tr_hdr;
  363. iov.iov_len = sizeof(tr_hdr);
  364. cur_rqst[0].rq_iov = &iov;
  365. cur_rqst[0].rq_nvec = 1;
  366. if (!server->ops->init_transform_rq) {
  367. cifs_dbg(VFS, "Encryption requested but transform callback "
  368. "is missing\n");
  369. return -EIO;
  370. }
  371. rc = server->ops->init_transform_rq(server, num_rqst + 1,
  372. &cur_rqst[0], rqst);
  373. if (rc)
  374. return rc;
  375. rc = __smb_send_rqst(server, num_rqst + 1, &cur_rqst[0]);
  376. smb3_free_compound_rqst(num_rqst, &cur_rqst[1]);
  377. return rc;
  378. }
  379. int
  380. smb_send(struct TCP_Server_Info *server, struct smb_hdr *smb_buffer,
  381. unsigned int smb_buf_length)
  382. {
  383. struct kvec iov[2];
  384. struct smb_rqst rqst = { .rq_iov = iov,
  385. .rq_nvec = 2 };
  386. iov[0].iov_base = smb_buffer;
  387. iov[0].iov_len = 4;
  388. iov[1].iov_base = (char *)smb_buffer + 4;
  389. iov[1].iov_len = smb_buf_length;
  390. return __smb_send_rqst(server, 1, &rqst);
  391. }
  392. static int
  393. wait_for_free_credits(struct TCP_Server_Info *server, const int timeout,
  394. int *credits)
  395. {
  396. int rc;
  397. spin_lock(&server->req_lock);
  398. if (timeout == CIFS_ASYNC_OP) {
  399. /* oplock breaks must not be held up */
  400. server->in_flight++;
  401. *credits -= 1;
  402. spin_unlock(&server->req_lock);
  403. return 0;
  404. }
  405. while (1) {
  406. if (*credits <= 0) {
  407. spin_unlock(&server->req_lock);
  408. cifs_num_waiters_inc(server);
  409. rc = wait_event_killable(server->request_q,
  410. has_credits(server, credits));
  411. cifs_num_waiters_dec(server);
  412. if (rc)
  413. return rc;
  414. spin_lock(&server->req_lock);
  415. } else {
  416. if (server->tcpStatus == CifsExiting) {
  417. spin_unlock(&server->req_lock);
  418. return -ENOENT;
  419. }
  420. /*
  421. * Can not count locking commands against total
  422. * as they are allowed to block on server.
  423. */
  424. /* update # of requests on the wire to server */
  425. if (timeout != CIFS_BLOCKING_OP) {
  426. *credits -= 1;
  427. server->in_flight++;
  428. }
  429. spin_unlock(&server->req_lock);
  430. break;
  431. }
  432. }
  433. return 0;
  434. }
  435. static int
  436. wait_for_free_request(struct TCP_Server_Info *server, const int timeout,
  437. const int optype)
  438. {
  439. int *val;
  440. val = server->ops->get_credits_field(server, optype);
  441. /* Since an echo is already inflight, no need to wait to send another */
  442. if (*val <= 0 && optype == CIFS_ECHO_OP)
  443. return -EAGAIN;
  444. return wait_for_free_credits(server, timeout, val);
  445. }
  446. int
  447. cifs_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
  448. unsigned int *num, unsigned int *credits)
  449. {
  450. *num = size;
  451. *credits = 0;
  452. return 0;
  453. }
  454. static int allocate_mid(struct cifs_ses *ses, struct smb_hdr *in_buf,
  455. struct mid_q_entry **ppmidQ)
  456. {
  457. if (ses->server->tcpStatus == CifsExiting) {
  458. return -ENOENT;
  459. }
  460. if (ses->server->tcpStatus == CifsNeedReconnect) {
  461. cifs_dbg(FYI, "tcp session dead - return to caller to retry\n");
  462. return -EAGAIN;
  463. }
  464. if (ses->status == CifsNew) {
  465. if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
  466. (in_buf->Command != SMB_COM_NEGOTIATE))
  467. return -EAGAIN;
  468. /* else ok - we are setting up session */
  469. }
  470. if (ses->status == CifsExiting) {
  471. /* check if SMB session is bad because we are setting it up */
  472. if (in_buf->Command != SMB_COM_LOGOFF_ANDX)
  473. return -EAGAIN;
  474. /* else ok - we are shutting down session */
  475. }
  476. *ppmidQ = AllocMidQEntry(in_buf, ses->server);
  477. if (*ppmidQ == NULL)
  478. return -ENOMEM;
  479. spin_lock(&GlobalMid_Lock);
  480. list_add_tail(&(*ppmidQ)->qhead, &ses->server->pending_mid_q);
  481. spin_unlock(&GlobalMid_Lock);
  482. return 0;
  483. }
  484. static int
  485. wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *midQ)
  486. {
  487. int error;
  488. error = wait_event_freezekillable_unsafe(server->response_q,
  489. midQ->mid_state != MID_REQUEST_SUBMITTED);
  490. if (error < 0)
  491. return -ERESTARTSYS;
  492. return 0;
  493. }
  494. struct mid_q_entry *
  495. cifs_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
  496. {
  497. int rc;
  498. struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
  499. struct mid_q_entry *mid;
  500. if (rqst->rq_iov[0].iov_len != 4 ||
  501. rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
  502. return ERR_PTR(-EIO);
  503. /* enable signing if server requires it */
  504. if (server->sign)
  505. hdr->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
  506. mid = AllocMidQEntry(hdr, server);
  507. if (mid == NULL)
  508. return ERR_PTR(-ENOMEM);
  509. rc = cifs_sign_rqst(rqst, server, &mid->sequence_number);
  510. if (rc) {
  511. DeleteMidQEntry(mid);
  512. return ERR_PTR(rc);
  513. }
  514. return mid;
  515. }
  516. /*
  517. * Send a SMB request and set the callback function in the mid to handle
  518. * the result. Caller is responsible for dealing with timeouts.
  519. */
  520. int
  521. cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst,
  522. mid_receive_t *receive, mid_callback_t *callback,
  523. mid_handle_t *handle, void *cbdata, const int flags)
  524. {
  525. int rc, timeout, optype;
  526. struct mid_q_entry *mid;
  527. unsigned int credits = 0;
  528. timeout = flags & CIFS_TIMEOUT_MASK;
  529. optype = flags & CIFS_OP_MASK;
  530. if ((flags & CIFS_HAS_CREDITS) == 0) {
  531. rc = wait_for_free_request(server, timeout, optype);
  532. if (rc)
  533. return rc;
  534. credits = 1;
  535. }
  536. mutex_lock(&server->srv_mutex);
  537. mid = server->ops->setup_async_request(server, rqst);
  538. if (IS_ERR(mid)) {
  539. mutex_unlock(&server->srv_mutex);
  540. add_credits_and_wake_if(server, credits, optype);
  541. return PTR_ERR(mid);
  542. }
  543. mid->receive = receive;
  544. mid->callback = callback;
  545. mid->callback_data = cbdata;
  546. mid->handle = handle;
  547. mid->mid_state = MID_REQUEST_SUBMITTED;
  548. /* put it on the pending_mid_q */
  549. spin_lock(&GlobalMid_Lock);
  550. list_add_tail(&mid->qhead, &server->pending_mid_q);
  551. spin_unlock(&GlobalMid_Lock);
  552. /*
  553. * Need to store the time in mid before calling I/O. For call_async,
  554. * I/O response may come back and free the mid entry on another thread.
  555. */
  556. cifs_save_when_sent(mid);
  557. cifs_in_send_inc(server);
  558. rc = smb_send_rqst(server, 1, rqst, flags);
  559. cifs_in_send_dec(server);
  560. if (rc < 0) {
  561. server->sequence_number -= 2;
  562. cifs_delete_mid(mid);
  563. }
  564. mutex_unlock(&server->srv_mutex);
  565. if (rc == 0)
  566. return 0;
  567. add_credits_and_wake_if(server, credits, optype);
  568. return rc;
  569. }
  570. /*
  571. *
  572. * Send an SMB Request. No response info (other than return code)
  573. * needs to be parsed.
  574. *
  575. * flags indicate the type of request buffer and how long to wait
  576. * and whether to log NT STATUS code (error) before mapping it to POSIX error
  577. *
  578. */
  579. int
  580. SendReceiveNoRsp(const unsigned int xid, struct cifs_ses *ses,
  581. char *in_buf, int flags)
  582. {
  583. int rc;
  584. struct kvec iov[1];
  585. struct kvec rsp_iov;
  586. int resp_buf_type;
  587. iov[0].iov_base = in_buf;
  588. iov[0].iov_len = get_rfc1002_length(in_buf) + 4;
  589. flags |= CIFS_NO_RESP;
  590. rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
  591. cifs_dbg(NOISY, "SendRcvNoRsp flags %d rc %d\n", flags, rc);
  592. return rc;
  593. }
  594. static int
  595. cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
  596. {
  597. int rc = 0;
  598. cifs_dbg(FYI, "%s: cmd=%d mid=%llu state=%d\n",
  599. __func__, le16_to_cpu(mid->command), mid->mid, mid->mid_state);
  600. spin_lock(&GlobalMid_Lock);
  601. switch (mid->mid_state) {
  602. case MID_RESPONSE_RECEIVED:
  603. spin_unlock(&GlobalMid_Lock);
  604. return rc;
  605. case MID_RETRY_NEEDED:
  606. rc = -EAGAIN;
  607. break;
  608. case MID_RESPONSE_MALFORMED:
  609. rc = -EIO;
  610. break;
  611. case MID_SHUTDOWN:
  612. rc = -EHOSTDOWN;
  613. break;
  614. default:
  615. list_del_init(&mid->qhead);
  616. cifs_dbg(VFS, "%s: invalid mid state mid=%llu state=%d\n",
  617. __func__, mid->mid, mid->mid_state);
  618. rc = -EIO;
  619. }
  620. spin_unlock(&GlobalMid_Lock);
  621. DeleteMidQEntry(mid);
  622. return rc;
  623. }
  624. static inline int
  625. send_cancel(struct TCP_Server_Info *server, struct smb_rqst *rqst,
  626. struct mid_q_entry *mid)
  627. {
  628. return server->ops->send_cancel ?
  629. server->ops->send_cancel(server, rqst, mid) : 0;
  630. }
  631. int
  632. cifs_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
  633. bool log_error)
  634. {
  635. unsigned int len = get_rfc1002_length(mid->resp_buf) + 4;
  636. dump_smb(mid->resp_buf, min_t(u32, 92, len));
  637. /* convert the length into a more usable form */
  638. if (server->sign) {
  639. struct kvec iov[2];
  640. int rc = 0;
  641. struct smb_rqst rqst = { .rq_iov = iov,
  642. .rq_nvec = 2 };
  643. iov[0].iov_base = mid->resp_buf;
  644. iov[0].iov_len = 4;
  645. iov[1].iov_base = (char *)mid->resp_buf + 4;
  646. iov[1].iov_len = len - 4;
  647. /* FIXME: add code to kill session */
  648. rc = cifs_verify_signature(&rqst, server,
  649. mid->sequence_number);
  650. if (rc)
  651. cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
  652. rc);
  653. }
  654. /* BB special case reconnect tid and uid here? */
  655. return map_smb_to_linux_error(mid->resp_buf, log_error);
  656. }
  657. struct mid_q_entry *
  658. cifs_setup_request(struct cifs_ses *ses, struct smb_rqst *rqst)
  659. {
  660. int rc;
  661. struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
  662. struct mid_q_entry *mid;
  663. if (rqst->rq_iov[0].iov_len != 4 ||
  664. rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
  665. return ERR_PTR(-EIO);
  666. rc = allocate_mid(ses, hdr, &mid);
  667. if (rc)
  668. return ERR_PTR(rc);
  669. rc = cifs_sign_rqst(rqst, ses->server, &mid->sequence_number);
  670. if (rc) {
  671. cifs_delete_mid(mid);
  672. return ERR_PTR(rc);
  673. }
  674. return mid;
  675. }
  676. static void
  677. cifs_noop_callback(struct mid_q_entry *mid)
  678. {
  679. }
  680. int
  681. compound_send_recv(const unsigned int xid, struct cifs_ses *ses,
  682. const int flags, const int num_rqst, struct smb_rqst *rqst,
  683. int *resp_buf_type, struct kvec *resp_iov)
  684. {
  685. int i, j, rc = 0;
  686. int timeout, optype;
  687. struct mid_q_entry *midQ[MAX_COMPOUND];
  688. unsigned int credits = 0;
  689. char *buf;
  690. timeout = flags & CIFS_TIMEOUT_MASK;
  691. optype = flags & CIFS_OP_MASK;
  692. for (i = 0; i < num_rqst; i++)
  693. resp_buf_type[i] = CIFS_NO_BUFFER; /* no response buf yet */
  694. if ((ses == NULL) || (ses->server == NULL)) {
  695. cifs_dbg(VFS, "Null session\n");
  696. return -EIO;
  697. }
  698. if (ses->server->tcpStatus == CifsExiting)
  699. return -ENOENT;
  700. /*
  701. * Ensure that we do not send more than 50 overlapping requests
  702. * to the same server. We may make this configurable later or
  703. * use ses->maxReq.
  704. */
  705. rc = wait_for_free_request(ses->server, timeout, optype);
  706. if (rc)
  707. return rc;
  708. /*
  709. * Make sure that we sign in the same order that we send on this socket
  710. * and avoid races inside tcp sendmsg code that could cause corruption
  711. * of smb data.
  712. */
  713. mutex_lock(&ses->server->srv_mutex);
  714. for (i = 0; i < num_rqst; i++) {
  715. midQ[i] = ses->server->ops->setup_request(ses, &rqst[i]);
  716. if (IS_ERR(midQ[i])) {
  717. for (j = 0; j < i; j++)
  718. cifs_delete_mid(midQ[j]);
  719. mutex_unlock(&ses->server->srv_mutex);
  720. /* Update # of requests on wire to server */
  721. add_credits(ses->server, 1, optype);
  722. return PTR_ERR(midQ[i]);
  723. }
  724. midQ[i]->mid_state = MID_REQUEST_SUBMITTED;
  725. /*
  726. * We don't invoke the callback compounds unless it is the last
  727. * request.
  728. */
  729. if (i < num_rqst - 1)
  730. midQ[i]->callback = cifs_noop_callback;
  731. }
  732. cifs_in_send_inc(ses->server);
  733. rc = smb_send_rqst(ses->server, num_rqst, rqst, flags);
  734. cifs_in_send_dec(ses->server);
  735. for (i = 0; i < num_rqst; i++)
  736. cifs_save_when_sent(midQ[i]);
  737. if (rc < 0)
  738. ses->server->sequence_number -= 2;
  739. mutex_unlock(&ses->server->srv_mutex);
  740. if (rc < 0)
  741. goto out;
  742. /*
  743. * Compounding is never used during session establish.
  744. */
  745. if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP))
  746. smb311_update_preauth_hash(ses, rqst[0].rq_iov,
  747. rqst[0].rq_nvec);
  748. if (timeout == CIFS_ASYNC_OP)
  749. goto out;
  750. for (i = 0; i < num_rqst; i++) {
  751. rc = wait_for_response(ses->server, midQ[i]);
  752. if (rc != 0) {
  753. cifs_dbg(VFS, "Cancelling wait for mid %llu cmd: %d\n",
  754. midQ[i]->mid, le16_to_cpu(midQ[i]->command));
  755. send_cancel(ses->server, &rqst[i], midQ[i]);
  756. spin_lock(&GlobalMid_Lock);
  757. if (midQ[i]->mid_state == MID_REQUEST_SUBMITTED) {
  758. midQ[i]->mid_flags |= MID_WAIT_CANCELLED;
  759. midQ[i]->callback = DeleteMidQEntry;
  760. spin_unlock(&GlobalMid_Lock);
  761. add_credits(ses->server, 1, optype);
  762. return rc;
  763. }
  764. spin_unlock(&GlobalMid_Lock);
  765. }
  766. }
  767. for (i = 0; i < num_rqst; i++)
  768. if (midQ[i]->resp_buf)
  769. credits += ses->server->ops->get_credits(midQ[i]);
  770. if (!credits)
  771. credits = 1;
  772. for (i = 0; i < num_rqst; i++) {
  773. if (rc < 0)
  774. goto out;
  775. rc = cifs_sync_mid_result(midQ[i], ses->server);
  776. if (rc != 0) {
  777. add_credits(ses->server, credits, optype);
  778. return rc;
  779. }
  780. if (!midQ[i]->resp_buf ||
  781. midQ[i]->mid_state != MID_RESPONSE_RECEIVED) {
  782. rc = -EIO;
  783. cifs_dbg(FYI, "Bad MID state?\n");
  784. goto out;
  785. }
  786. buf = (char *)midQ[i]->resp_buf;
  787. resp_iov[i].iov_base = buf;
  788. resp_iov[i].iov_len = midQ[i]->resp_buf_size +
  789. ses->server->vals->header_preamble_size;
  790. if (midQ[i]->large_buf)
  791. resp_buf_type[i] = CIFS_LARGE_BUFFER;
  792. else
  793. resp_buf_type[i] = CIFS_SMALL_BUFFER;
  794. rc = ses->server->ops->check_receive(midQ[i], ses->server,
  795. flags & CIFS_LOG_ERROR);
  796. /* mark it so buf will not be freed by cifs_delete_mid */
  797. if ((flags & CIFS_NO_RESP) == 0)
  798. midQ[i]->resp_buf = NULL;
  799. }
  800. /*
  801. * Compounding is never used during session establish.
  802. */
  803. if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP)) {
  804. struct kvec iov = {
  805. .iov_base = resp_iov[0].iov_base,
  806. .iov_len = resp_iov[0].iov_len
  807. };
  808. smb311_update_preauth_hash(ses, &iov, 1);
  809. }
  810. out:
  811. /*
  812. * This will dequeue all mids. After this it is important that the
  813. * demultiplex_thread will not process any of these mids any futher.
  814. * This is prevented above by using a noop callback that will not
  815. * wake this thread except for the very last PDU.
  816. */
  817. for (i = 0; i < num_rqst; i++)
  818. cifs_delete_mid(midQ[i]);
  819. add_credits(ses->server, credits, optype);
  820. return rc;
  821. }
  822. int
  823. cifs_send_recv(const unsigned int xid, struct cifs_ses *ses,
  824. struct smb_rqst *rqst, int *resp_buf_type, const int flags,
  825. struct kvec *resp_iov)
  826. {
  827. return compound_send_recv(xid, ses, flags, 1, rqst, resp_buf_type,
  828. resp_iov);
  829. }
  830. int
  831. SendReceive2(const unsigned int xid, struct cifs_ses *ses,
  832. struct kvec *iov, int n_vec, int *resp_buf_type /* ret */,
  833. const int flags, struct kvec *resp_iov)
  834. {
  835. struct smb_rqst rqst;
  836. struct kvec s_iov[CIFS_MAX_IOV_SIZE], *new_iov;
  837. int rc;
  838. if (n_vec + 1 > CIFS_MAX_IOV_SIZE) {
  839. new_iov = kmalloc_array(n_vec + 1, sizeof(struct kvec),
  840. GFP_KERNEL);
  841. if (!new_iov) {
  842. /* otherwise cifs_send_recv below sets resp_buf_type */
  843. *resp_buf_type = CIFS_NO_BUFFER;
  844. return -ENOMEM;
  845. }
  846. } else
  847. new_iov = s_iov;
  848. /* 1st iov is a RFC1001 length followed by the rest of the packet */
  849. memcpy(new_iov + 1, iov, (sizeof(struct kvec) * n_vec));
  850. new_iov[0].iov_base = new_iov[1].iov_base;
  851. new_iov[0].iov_len = 4;
  852. new_iov[1].iov_base += 4;
  853. new_iov[1].iov_len -= 4;
  854. memset(&rqst, 0, sizeof(struct smb_rqst));
  855. rqst.rq_iov = new_iov;
  856. rqst.rq_nvec = n_vec + 1;
  857. rc = cifs_send_recv(xid, ses, &rqst, resp_buf_type, flags, resp_iov);
  858. if (n_vec + 1 > CIFS_MAX_IOV_SIZE)
  859. kfree(new_iov);
  860. return rc;
  861. }
  862. int
  863. SendReceive(const unsigned int xid, struct cifs_ses *ses,
  864. struct smb_hdr *in_buf, struct smb_hdr *out_buf,
  865. int *pbytes_returned, const int timeout)
  866. {
  867. int rc = 0;
  868. struct mid_q_entry *midQ;
  869. unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
  870. struct kvec iov = { .iov_base = in_buf, .iov_len = len };
  871. struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
  872. if (ses == NULL) {
  873. cifs_dbg(VFS, "Null smb session\n");
  874. return -EIO;
  875. }
  876. if (ses->server == NULL) {
  877. cifs_dbg(VFS, "Null tcp session\n");
  878. return -EIO;
  879. }
  880. if (ses->server->tcpStatus == CifsExiting)
  881. return -ENOENT;
  882. /* Ensure that we do not send more than 50 overlapping requests
  883. to the same server. We may make this configurable later or
  884. use ses->maxReq */
  885. if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
  886. cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
  887. len);
  888. return -EIO;
  889. }
  890. rc = wait_for_free_request(ses->server, timeout, 0);
  891. if (rc)
  892. return rc;
  893. /* make sure that we sign in the same order that we send on this socket
  894. and avoid races inside tcp sendmsg code that could cause corruption
  895. of smb data */
  896. mutex_lock(&ses->server->srv_mutex);
  897. rc = allocate_mid(ses, in_buf, &midQ);
  898. if (rc) {
  899. mutex_unlock(&ses->server->srv_mutex);
  900. /* Update # of requests on wire to server */
  901. add_credits(ses->server, 1, 0);
  902. return rc;
  903. }
  904. rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
  905. if (rc) {
  906. mutex_unlock(&ses->server->srv_mutex);
  907. goto out;
  908. }
  909. midQ->mid_state = MID_REQUEST_SUBMITTED;
  910. cifs_in_send_inc(ses->server);
  911. rc = smb_send(ses->server, in_buf, len);
  912. cifs_in_send_dec(ses->server);
  913. cifs_save_when_sent(midQ);
  914. if (rc < 0)
  915. ses->server->sequence_number -= 2;
  916. mutex_unlock(&ses->server->srv_mutex);
  917. if (rc < 0)
  918. goto out;
  919. if (timeout == CIFS_ASYNC_OP)
  920. goto out;
  921. rc = wait_for_response(ses->server, midQ);
  922. if (rc != 0) {
  923. send_cancel(ses->server, &rqst, midQ);
  924. spin_lock(&GlobalMid_Lock);
  925. if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
  926. /* no longer considered to be "in-flight" */
  927. midQ->callback = DeleteMidQEntry;
  928. spin_unlock(&GlobalMid_Lock);
  929. add_credits(ses->server, 1, 0);
  930. return rc;
  931. }
  932. spin_unlock(&GlobalMid_Lock);
  933. }
  934. rc = cifs_sync_mid_result(midQ, ses->server);
  935. if (rc != 0) {
  936. add_credits(ses->server, 1, 0);
  937. return rc;
  938. }
  939. if (!midQ->resp_buf || !out_buf ||
  940. midQ->mid_state != MID_RESPONSE_RECEIVED) {
  941. rc = -EIO;
  942. cifs_dbg(VFS, "Bad MID state?\n");
  943. goto out;
  944. }
  945. *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
  946. memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
  947. rc = cifs_check_receive(midQ, ses->server, 0);
  948. out:
  949. cifs_delete_mid(midQ);
  950. add_credits(ses->server, 1, 0);
  951. return rc;
  952. }
  953. /* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
  954. blocking lock to return. */
  955. static int
  956. send_lock_cancel(const unsigned int xid, struct cifs_tcon *tcon,
  957. struct smb_hdr *in_buf,
  958. struct smb_hdr *out_buf)
  959. {
  960. int bytes_returned;
  961. struct cifs_ses *ses = tcon->ses;
  962. LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
  963. /* We just modify the current in_buf to change
  964. the type of lock from LOCKING_ANDX_SHARED_LOCK
  965. or LOCKING_ANDX_EXCLUSIVE_LOCK to
  966. LOCKING_ANDX_CANCEL_LOCK. */
  967. pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
  968. pSMB->Timeout = 0;
  969. pSMB->hdr.Mid = get_next_mid(ses->server);
  970. return SendReceive(xid, ses, in_buf, out_buf,
  971. &bytes_returned, 0);
  972. }
  973. int
  974. SendReceiveBlockingLock(const unsigned int xid, struct cifs_tcon *tcon,
  975. struct smb_hdr *in_buf, struct smb_hdr *out_buf,
  976. int *pbytes_returned)
  977. {
  978. int rc = 0;
  979. int rstart = 0;
  980. struct mid_q_entry *midQ;
  981. struct cifs_ses *ses;
  982. unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
  983. struct kvec iov = { .iov_base = in_buf, .iov_len = len };
  984. struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
  985. if (tcon == NULL || tcon->ses == NULL) {
  986. cifs_dbg(VFS, "Null smb session\n");
  987. return -EIO;
  988. }
  989. ses = tcon->ses;
  990. if (ses->server == NULL) {
  991. cifs_dbg(VFS, "Null tcp session\n");
  992. return -EIO;
  993. }
  994. if (ses->server->tcpStatus == CifsExiting)
  995. return -ENOENT;
  996. /* Ensure that we do not send more than 50 overlapping requests
  997. to the same server. We may make this configurable later or
  998. use ses->maxReq */
  999. if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
  1000. cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
  1001. len);
  1002. return -EIO;
  1003. }
  1004. rc = wait_for_free_request(ses->server, CIFS_BLOCKING_OP, 0);
  1005. if (rc)
  1006. return rc;
  1007. /* make sure that we sign in the same order that we send on this socket
  1008. and avoid races inside tcp sendmsg code that could cause corruption
  1009. of smb data */
  1010. mutex_lock(&ses->server->srv_mutex);
  1011. rc = allocate_mid(ses, in_buf, &midQ);
  1012. if (rc) {
  1013. mutex_unlock(&ses->server->srv_mutex);
  1014. return rc;
  1015. }
  1016. rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
  1017. if (rc) {
  1018. cifs_delete_mid(midQ);
  1019. mutex_unlock(&ses->server->srv_mutex);
  1020. return rc;
  1021. }
  1022. midQ->mid_state = MID_REQUEST_SUBMITTED;
  1023. cifs_in_send_inc(ses->server);
  1024. rc = smb_send(ses->server, in_buf, len);
  1025. cifs_in_send_dec(ses->server);
  1026. cifs_save_when_sent(midQ);
  1027. if (rc < 0)
  1028. ses->server->sequence_number -= 2;
  1029. mutex_unlock(&ses->server->srv_mutex);
  1030. if (rc < 0) {
  1031. cifs_delete_mid(midQ);
  1032. return rc;
  1033. }
  1034. /* Wait for a reply - allow signals to interrupt. */
  1035. rc = wait_event_interruptible(ses->server->response_q,
  1036. (!(midQ->mid_state == MID_REQUEST_SUBMITTED)) ||
  1037. ((ses->server->tcpStatus != CifsGood) &&
  1038. (ses->server->tcpStatus != CifsNew)));
  1039. /* Were we interrupted by a signal ? */
  1040. if ((rc == -ERESTARTSYS) &&
  1041. (midQ->mid_state == MID_REQUEST_SUBMITTED) &&
  1042. ((ses->server->tcpStatus == CifsGood) ||
  1043. (ses->server->tcpStatus == CifsNew))) {
  1044. if (in_buf->Command == SMB_COM_TRANSACTION2) {
  1045. /* POSIX lock. We send a NT_CANCEL SMB to cause the
  1046. blocking lock to return. */
  1047. rc = send_cancel(ses->server, &rqst, midQ);
  1048. if (rc) {
  1049. cifs_delete_mid(midQ);
  1050. return rc;
  1051. }
  1052. } else {
  1053. /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
  1054. to cause the blocking lock to return. */
  1055. rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
  1056. /* If we get -ENOLCK back the lock may have
  1057. already been removed. Don't exit in this case. */
  1058. if (rc && rc != -ENOLCK) {
  1059. cifs_delete_mid(midQ);
  1060. return rc;
  1061. }
  1062. }
  1063. rc = wait_for_response(ses->server, midQ);
  1064. if (rc) {
  1065. send_cancel(ses->server, &rqst, midQ);
  1066. spin_lock(&GlobalMid_Lock);
  1067. if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
  1068. /* no longer considered to be "in-flight" */
  1069. midQ->callback = DeleteMidQEntry;
  1070. spin_unlock(&GlobalMid_Lock);
  1071. return rc;
  1072. }
  1073. spin_unlock(&GlobalMid_Lock);
  1074. }
  1075. /* We got the response - restart system call. */
  1076. rstart = 1;
  1077. }
  1078. rc = cifs_sync_mid_result(midQ, ses->server);
  1079. if (rc != 0)
  1080. return rc;
  1081. /* rcvd frame is ok */
  1082. if (out_buf == NULL || midQ->mid_state != MID_RESPONSE_RECEIVED) {
  1083. rc = -EIO;
  1084. cifs_dbg(VFS, "Bad MID state?\n");
  1085. goto out;
  1086. }
  1087. *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
  1088. memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
  1089. rc = cifs_check_receive(midQ, ses->server, 0);
  1090. out:
  1091. cifs_delete_mid(midQ);
  1092. if (rstart && rc == -EACCES)
  1093. return -ERESTARTSYS;
  1094. return rc;
  1095. }