transport.c 33 KB

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