transport.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  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. if (!server->ops->init_transform_rq) {
  356. cifs_dbg(VFS, "Encryption requested but transform callback "
  357. "is missing\n");
  358. return -EIO;
  359. }
  360. tr_hdr = kmalloc(sizeof(*tr_hdr), GFP_NOFS);
  361. if (!tr_hdr)
  362. return -ENOMEM;
  363. memset(&cur_rqst[0], 0, sizeof(cur_rqst));
  364. memset(&iov, 0, sizeof(iov));
  365. memset(tr_hdr, 0, sizeof(*tr_hdr));
  366. iov.iov_base = tr_hdr;
  367. iov.iov_len = sizeof(*tr_hdr);
  368. cur_rqst[0].rq_iov = &iov;
  369. cur_rqst[0].rq_nvec = 1;
  370. rc = server->ops->init_transform_rq(server, num_rqst + 1,
  371. &cur_rqst[0], rqst);
  372. if (rc)
  373. goto out;
  374. rc = __smb_send_rqst(server, num_rqst + 1, &cur_rqst[0]);
  375. smb3_free_compound_rqst(num_rqst, &cur_rqst[1]);
  376. out:
  377. kfree(tr_hdr);
  378. return rc;
  379. }
  380. int
  381. smb_send(struct TCP_Server_Info *server, struct smb_hdr *smb_buffer,
  382. unsigned int smb_buf_length)
  383. {
  384. struct kvec iov[2];
  385. struct smb_rqst rqst = { .rq_iov = iov,
  386. .rq_nvec = 2 };
  387. iov[0].iov_base = smb_buffer;
  388. iov[0].iov_len = 4;
  389. iov[1].iov_base = (char *)smb_buffer + 4;
  390. iov[1].iov_len = smb_buf_length;
  391. return __smb_send_rqst(server, 1, &rqst);
  392. }
  393. static int
  394. wait_for_free_credits(struct TCP_Server_Info *server, const int timeout,
  395. int *credits)
  396. {
  397. int rc;
  398. spin_lock(&server->req_lock);
  399. if (timeout == CIFS_ASYNC_OP) {
  400. /* oplock breaks must not be held up */
  401. server->in_flight++;
  402. *credits -= 1;
  403. spin_unlock(&server->req_lock);
  404. return 0;
  405. }
  406. while (1) {
  407. if (*credits <= 0) {
  408. spin_unlock(&server->req_lock);
  409. cifs_num_waiters_inc(server);
  410. rc = wait_event_killable(server->request_q,
  411. has_credits(server, credits));
  412. cifs_num_waiters_dec(server);
  413. if (rc)
  414. return rc;
  415. spin_lock(&server->req_lock);
  416. } else {
  417. if (server->tcpStatus == CifsExiting) {
  418. spin_unlock(&server->req_lock);
  419. return -ENOENT;
  420. }
  421. /*
  422. * Can not count locking commands against total
  423. * as they are allowed to block on server.
  424. */
  425. /* update # of requests on the wire to server */
  426. if (timeout != CIFS_BLOCKING_OP) {
  427. *credits -= 1;
  428. server->in_flight++;
  429. }
  430. spin_unlock(&server->req_lock);
  431. break;
  432. }
  433. }
  434. return 0;
  435. }
  436. static int
  437. wait_for_free_request(struct TCP_Server_Info *server, const int timeout,
  438. const int optype)
  439. {
  440. int *val;
  441. val = server->ops->get_credits_field(server, optype);
  442. /* Since an echo is already inflight, no need to wait to send another */
  443. if (*val <= 0 && optype == CIFS_ECHO_OP)
  444. return -EAGAIN;
  445. return wait_for_free_credits(server, timeout, val);
  446. }
  447. int
  448. cifs_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
  449. unsigned int *num, unsigned int *credits)
  450. {
  451. *num = size;
  452. *credits = 0;
  453. return 0;
  454. }
  455. static int allocate_mid(struct cifs_ses *ses, struct smb_hdr *in_buf,
  456. struct mid_q_entry **ppmidQ)
  457. {
  458. if (ses->server->tcpStatus == CifsExiting) {
  459. return -ENOENT;
  460. }
  461. if (ses->server->tcpStatus == CifsNeedReconnect) {
  462. cifs_dbg(FYI, "tcp session dead - return to caller to retry\n");
  463. return -EAGAIN;
  464. }
  465. if (ses->status == CifsNew) {
  466. if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
  467. (in_buf->Command != SMB_COM_NEGOTIATE))
  468. return -EAGAIN;
  469. /* else ok - we are setting up session */
  470. }
  471. if (ses->status == CifsExiting) {
  472. /* check if SMB session is bad because we are setting it up */
  473. if (in_buf->Command != SMB_COM_LOGOFF_ANDX)
  474. return -EAGAIN;
  475. /* else ok - we are shutting down session */
  476. }
  477. *ppmidQ = AllocMidQEntry(in_buf, ses->server);
  478. if (*ppmidQ == NULL)
  479. return -ENOMEM;
  480. spin_lock(&GlobalMid_Lock);
  481. list_add_tail(&(*ppmidQ)->qhead, &ses->server->pending_mid_q);
  482. spin_unlock(&GlobalMid_Lock);
  483. return 0;
  484. }
  485. static int
  486. wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *midQ)
  487. {
  488. int error;
  489. error = wait_event_freezekillable_unsafe(server->response_q,
  490. midQ->mid_state != MID_REQUEST_SUBMITTED);
  491. if (error < 0)
  492. return -ERESTARTSYS;
  493. return 0;
  494. }
  495. struct mid_q_entry *
  496. cifs_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
  497. {
  498. int rc;
  499. struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
  500. struct mid_q_entry *mid;
  501. if (rqst->rq_iov[0].iov_len != 4 ||
  502. rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
  503. return ERR_PTR(-EIO);
  504. /* enable signing if server requires it */
  505. if (server->sign)
  506. hdr->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
  507. mid = AllocMidQEntry(hdr, server);
  508. if (mid == NULL)
  509. return ERR_PTR(-ENOMEM);
  510. rc = cifs_sign_rqst(rqst, server, &mid->sequence_number);
  511. if (rc) {
  512. DeleteMidQEntry(mid);
  513. return ERR_PTR(rc);
  514. }
  515. return mid;
  516. }
  517. /*
  518. * Send a SMB request and set the callback function in the mid to handle
  519. * the result. Caller is responsible for dealing with timeouts.
  520. */
  521. int
  522. cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst,
  523. mid_receive_t *receive, mid_callback_t *callback,
  524. mid_handle_t *handle, void *cbdata, const int flags)
  525. {
  526. int rc, timeout, optype;
  527. struct mid_q_entry *mid;
  528. unsigned int credits = 0;
  529. timeout = flags & CIFS_TIMEOUT_MASK;
  530. optype = flags & CIFS_OP_MASK;
  531. if ((flags & CIFS_HAS_CREDITS) == 0) {
  532. rc = wait_for_free_request(server, timeout, optype);
  533. if (rc)
  534. return rc;
  535. credits = 1;
  536. }
  537. mutex_lock(&server->srv_mutex);
  538. mid = server->ops->setup_async_request(server, rqst);
  539. if (IS_ERR(mid)) {
  540. mutex_unlock(&server->srv_mutex);
  541. add_credits_and_wake_if(server, credits, optype);
  542. return PTR_ERR(mid);
  543. }
  544. mid->receive = receive;
  545. mid->callback = callback;
  546. mid->callback_data = cbdata;
  547. mid->handle = handle;
  548. mid->mid_state = MID_REQUEST_SUBMITTED;
  549. /* put it on the pending_mid_q */
  550. spin_lock(&GlobalMid_Lock);
  551. list_add_tail(&mid->qhead, &server->pending_mid_q);
  552. spin_unlock(&GlobalMid_Lock);
  553. /*
  554. * Need to store the time in mid before calling I/O. For call_async,
  555. * I/O response may come back and free the mid entry on another thread.
  556. */
  557. cifs_save_when_sent(mid);
  558. cifs_in_send_inc(server);
  559. rc = smb_send_rqst(server, 1, rqst, flags);
  560. cifs_in_send_dec(server);
  561. if (rc < 0) {
  562. revert_current_mid(server, mid->credits);
  563. server->sequence_number -= 2;
  564. cifs_delete_mid(mid);
  565. }
  566. mutex_unlock(&server->srv_mutex);
  567. if (rc == 0)
  568. return 0;
  569. add_credits_and_wake_if(server, credits, optype);
  570. return rc;
  571. }
  572. /*
  573. *
  574. * Send an SMB Request. No response info (other than return code)
  575. * needs to be parsed.
  576. *
  577. * flags indicate the type of request buffer and how long to wait
  578. * and whether to log NT STATUS code (error) before mapping it to POSIX error
  579. *
  580. */
  581. int
  582. SendReceiveNoRsp(const unsigned int xid, struct cifs_ses *ses,
  583. char *in_buf, int flags)
  584. {
  585. int rc;
  586. struct kvec iov[1];
  587. struct kvec rsp_iov;
  588. int resp_buf_type;
  589. iov[0].iov_base = in_buf;
  590. iov[0].iov_len = get_rfc1002_length(in_buf) + 4;
  591. flags |= CIFS_NO_RESP;
  592. rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
  593. cifs_dbg(NOISY, "SendRcvNoRsp flags %d rc %d\n", flags, rc);
  594. return rc;
  595. }
  596. static int
  597. cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
  598. {
  599. int rc = 0;
  600. cifs_dbg(FYI, "%s: cmd=%d mid=%llu state=%d\n",
  601. __func__, le16_to_cpu(mid->command), mid->mid, mid->mid_state);
  602. spin_lock(&GlobalMid_Lock);
  603. switch (mid->mid_state) {
  604. case MID_RESPONSE_RECEIVED:
  605. spin_unlock(&GlobalMid_Lock);
  606. return rc;
  607. case MID_RETRY_NEEDED:
  608. rc = -EAGAIN;
  609. break;
  610. case MID_RESPONSE_MALFORMED:
  611. rc = -EIO;
  612. break;
  613. case MID_SHUTDOWN:
  614. rc = -EHOSTDOWN;
  615. break;
  616. default:
  617. list_del_init(&mid->qhead);
  618. cifs_dbg(VFS, "%s: invalid mid state mid=%llu state=%d\n",
  619. __func__, mid->mid, mid->mid_state);
  620. rc = -EIO;
  621. }
  622. spin_unlock(&GlobalMid_Lock);
  623. DeleteMidQEntry(mid);
  624. return rc;
  625. }
  626. static inline int
  627. send_cancel(struct TCP_Server_Info *server, struct smb_rqst *rqst,
  628. struct mid_q_entry *mid)
  629. {
  630. return server->ops->send_cancel ?
  631. server->ops->send_cancel(server, rqst, mid) : 0;
  632. }
  633. int
  634. cifs_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
  635. bool log_error)
  636. {
  637. unsigned int len = get_rfc1002_length(mid->resp_buf) + 4;
  638. dump_smb(mid->resp_buf, min_t(u32, 92, len));
  639. /* convert the length into a more usable form */
  640. if (server->sign) {
  641. struct kvec iov[2];
  642. int rc = 0;
  643. struct smb_rqst rqst = { .rq_iov = iov,
  644. .rq_nvec = 2 };
  645. iov[0].iov_base = mid->resp_buf;
  646. iov[0].iov_len = 4;
  647. iov[1].iov_base = (char *)mid->resp_buf + 4;
  648. iov[1].iov_len = len - 4;
  649. /* FIXME: add code to kill session */
  650. rc = cifs_verify_signature(&rqst, server,
  651. mid->sequence_number);
  652. if (rc)
  653. cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
  654. rc);
  655. }
  656. /* BB special case reconnect tid and uid here? */
  657. return map_smb_to_linux_error(mid->resp_buf, log_error);
  658. }
  659. struct mid_q_entry *
  660. cifs_setup_request(struct cifs_ses *ses, struct smb_rqst *rqst)
  661. {
  662. int rc;
  663. struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
  664. struct mid_q_entry *mid;
  665. if (rqst->rq_iov[0].iov_len != 4 ||
  666. rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
  667. return ERR_PTR(-EIO);
  668. rc = allocate_mid(ses, hdr, &mid);
  669. if (rc)
  670. return ERR_PTR(rc);
  671. rc = cifs_sign_rqst(rqst, ses->server, &mid->sequence_number);
  672. if (rc) {
  673. cifs_delete_mid(mid);
  674. return ERR_PTR(rc);
  675. }
  676. return mid;
  677. }
  678. static void
  679. cifs_noop_callback(struct mid_q_entry *mid)
  680. {
  681. }
  682. int
  683. compound_send_recv(const unsigned int xid, struct cifs_ses *ses,
  684. const int flags, const int num_rqst, struct smb_rqst *rqst,
  685. int *resp_buf_type, struct kvec *resp_iov)
  686. {
  687. int i, j, rc = 0;
  688. int timeout, optype;
  689. struct mid_q_entry *midQ[MAX_COMPOUND];
  690. bool cancelled_mid[MAX_COMPOUND] = {false};
  691. unsigned int credits[MAX_COMPOUND] = {0};
  692. char *buf;
  693. timeout = flags & CIFS_TIMEOUT_MASK;
  694. optype = flags & CIFS_OP_MASK;
  695. for (i = 0; i < num_rqst; i++)
  696. resp_buf_type[i] = CIFS_NO_BUFFER; /* no response buf yet */
  697. if ((ses == NULL) || (ses->server == NULL)) {
  698. cifs_dbg(VFS, "Null session\n");
  699. return -EIO;
  700. }
  701. if (ses->server->tcpStatus == CifsExiting)
  702. return -ENOENT;
  703. /*
  704. * Ensure we obtain 1 credit per request in the compound chain.
  705. * It can be optimized further by waiting for all the credits
  706. * at once but this can wait long enough if we don't have enough
  707. * credits due to some heavy operations in progress or the server
  708. * not granting us much, so a fallback to the current approach is
  709. * needed anyway.
  710. */
  711. for (i = 0; i < num_rqst; i++) {
  712. rc = wait_for_free_request(ses->server, timeout, optype);
  713. if (rc) {
  714. /*
  715. * We haven't sent an SMB packet to the server yet but
  716. * we already obtained credits for i requests in the
  717. * compound chain - need to return those credits back
  718. * for future use. Note that we need to call add_credits
  719. * multiple times to match the way we obtained credits
  720. * in the first place and to account for in flight
  721. * requests correctly.
  722. */
  723. for (j = 0; j < i; j++)
  724. add_credits(ses->server, 1, optype);
  725. return rc;
  726. }
  727. credits[i] = 1;
  728. }
  729. /*
  730. * Make sure that we sign in the same order that we send on this socket
  731. * and avoid races inside tcp sendmsg code that could cause corruption
  732. * of smb data.
  733. */
  734. mutex_lock(&ses->server->srv_mutex);
  735. for (i = 0; i < num_rqst; i++) {
  736. midQ[i] = ses->server->ops->setup_request(ses, &rqst[i]);
  737. if (IS_ERR(midQ[i])) {
  738. revert_current_mid(ses->server, i);
  739. for (j = 0; j < i; j++)
  740. cifs_delete_mid(midQ[j]);
  741. mutex_unlock(&ses->server->srv_mutex);
  742. /* Update # of requests on wire to server */
  743. for (j = 0; j < num_rqst; j++)
  744. add_credits(ses->server, credits[j], optype);
  745. return PTR_ERR(midQ[i]);
  746. }
  747. midQ[i]->mid_state = MID_REQUEST_SUBMITTED;
  748. /*
  749. * We don't invoke the callback compounds unless it is the last
  750. * request.
  751. */
  752. if (i < num_rqst - 1)
  753. midQ[i]->callback = cifs_noop_callback;
  754. }
  755. cifs_in_send_inc(ses->server);
  756. rc = smb_send_rqst(ses->server, num_rqst, rqst, flags);
  757. cifs_in_send_dec(ses->server);
  758. for (i = 0; i < num_rqst; i++)
  759. cifs_save_when_sent(midQ[i]);
  760. if (rc < 0) {
  761. revert_current_mid(ses->server, num_rqst);
  762. ses->server->sequence_number -= 2;
  763. }
  764. mutex_unlock(&ses->server->srv_mutex);
  765. if (rc < 0)
  766. goto out;
  767. /*
  768. * Compounding is never used during session establish.
  769. */
  770. if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP)) {
  771. mutex_lock(&ses->server->srv_mutex);
  772. smb311_update_preauth_hash(ses, rqst[0].rq_iov,
  773. rqst[0].rq_nvec);
  774. mutex_unlock(&ses->server->srv_mutex);
  775. }
  776. if (timeout == CIFS_ASYNC_OP)
  777. goto out;
  778. for (i = 0; i < num_rqst; i++) {
  779. rc = wait_for_response(ses->server, midQ[i]);
  780. if (rc != 0) {
  781. cifs_dbg(FYI, "Cancelling wait for mid %llu\n",
  782. midQ[i]->mid);
  783. send_cancel(ses->server, &rqst[i], midQ[i]);
  784. spin_lock(&GlobalMid_Lock);
  785. if (midQ[i]->mid_state == MID_REQUEST_SUBMITTED) {
  786. midQ[i]->mid_flags |= MID_WAIT_CANCELLED;
  787. midQ[i]->callback = DeleteMidQEntry;
  788. cancelled_mid[i] = true;
  789. }
  790. spin_unlock(&GlobalMid_Lock);
  791. }
  792. }
  793. for (i = 0; i < num_rqst; i++)
  794. if (!cancelled_mid[i] && midQ[i]->resp_buf
  795. && (midQ[i]->mid_state == MID_RESPONSE_RECEIVED))
  796. credits[i] = ses->server->ops->get_credits(midQ[i]);
  797. for (i = 0; i < num_rqst; i++) {
  798. if (rc < 0)
  799. goto out;
  800. rc = cifs_sync_mid_result(midQ[i], ses->server);
  801. if (rc != 0) {
  802. /* mark this mid as cancelled to not free it below */
  803. cancelled_mid[i] = true;
  804. goto out;
  805. }
  806. if (!midQ[i]->resp_buf ||
  807. midQ[i]->mid_state != MID_RESPONSE_RECEIVED) {
  808. rc = -EIO;
  809. cifs_dbg(FYI, "Bad MID state?\n");
  810. goto out;
  811. }
  812. buf = (char *)midQ[i]->resp_buf;
  813. resp_iov[i].iov_base = buf;
  814. resp_iov[i].iov_len = midQ[i]->resp_buf_size +
  815. ses->server->vals->header_preamble_size;
  816. if (midQ[i]->large_buf)
  817. resp_buf_type[i] = CIFS_LARGE_BUFFER;
  818. else
  819. resp_buf_type[i] = CIFS_SMALL_BUFFER;
  820. rc = ses->server->ops->check_receive(midQ[i], ses->server,
  821. flags & CIFS_LOG_ERROR);
  822. /* mark it so buf will not be freed by cifs_delete_mid */
  823. if ((flags & CIFS_NO_RESP) == 0)
  824. midQ[i]->resp_buf = NULL;
  825. }
  826. /*
  827. * Compounding is never used during session establish.
  828. */
  829. if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP)) {
  830. struct kvec iov = {
  831. .iov_base = resp_iov[0].iov_base,
  832. .iov_len = resp_iov[0].iov_len
  833. };
  834. mutex_lock(&ses->server->srv_mutex);
  835. smb311_update_preauth_hash(ses, &iov, 1);
  836. mutex_unlock(&ses->server->srv_mutex);
  837. }
  838. out:
  839. /*
  840. * This will dequeue all mids. After this it is important that the
  841. * demultiplex_thread will not process any of these mids any futher.
  842. * This is prevented above by using a noop callback that will not
  843. * wake this thread except for the very last PDU.
  844. */
  845. for (i = 0; i < num_rqst; i++) {
  846. if (!cancelled_mid[i])
  847. cifs_delete_mid(midQ[i]);
  848. add_credits(ses->server, credits[i], optype);
  849. }
  850. return rc;
  851. }
  852. int
  853. cifs_send_recv(const unsigned int xid, struct cifs_ses *ses,
  854. struct smb_rqst *rqst, int *resp_buf_type, const int flags,
  855. struct kvec *resp_iov)
  856. {
  857. return compound_send_recv(xid, ses, flags, 1, rqst, resp_buf_type,
  858. resp_iov);
  859. }
  860. int
  861. SendReceive2(const unsigned int xid, struct cifs_ses *ses,
  862. struct kvec *iov, int n_vec, int *resp_buf_type /* ret */,
  863. const int flags, struct kvec *resp_iov)
  864. {
  865. struct smb_rqst rqst;
  866. struct kvec s_iov[CIFS_MAX_IOV_SIZE], *new_iov;
  867. int rc;
  868. if (n_vec + 1 > CIFS_MAX_IOV_SIZE) {
  869. new_iov = kmalloc_array(n_vec + 1, sizeof(struct kvec),
  870. GFP_KERNEL);
  871. if (!new_iov) {
  872. /* otherwise cifs_send_recv below sets resp_buf_type */
  873. *resp_buf_type = CIFS_NO_BUFFER;
  874. return -ENOMEM;
  875. }
  876. } else
  877. new_iov = s_iov;
  878. /* 1st iov is a RFC1001 length followed by the rest of the packet */
  879. memcpy(new_iov + 1, iov, (sizeof(struct kvec) * n_vec));
  880. new_iov[0].iov_base = new_iov[1].iov_base;
  881. new_iov[0].iov_len = 4;
  882. new_iov[1].iov_base += 4;
  883. new_iov[1].iov_len -= 4;
  884. memset(&rqst, 0, sizeof(struct smb_rqst));
  885. rqst.rq_iov = new_iov;
  886. rqst.rq_nvec = n_vec + 1;
  887. rc = cifs_send_recv(xid, ses, &rqst, resp_buf_type, flags, resp_iov);
  888. if (n_vec + 1 > CIFS_MAX_IOV_SIZE)
  889. kfree(new_iov);
  890. return rc;
  891. }
  892. int
  893. SendReceive(const unsigned int xid, struct cifs_ses *ses,
  894. struct smb_hdr *in_buf, struct smb_hdr *out_buf,
  895. int *pbytes_returned, const int timeout)
  896. {
  897. int rc = 0;
  898. struct mid_q_entry *midQ;
  899. unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
  900. struct kvec iov = { .iov_base = in_buf, .iov_len = len };
  901. struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
  902. if (ses == NULL) {
  903. cifs_dbg(VFS, "Null smb session\n");
  904. return -EIO;
  905. }
  906. if (ses->server == NULL) {
  907. cifs_dbg(VFS, "Null tcp session\n");
  908. return -EIO;
  909. }
  910. if (ses->server->tcpStatus == CifsExiting)
  911. return -ENOENT;
  912. /* Ensure that we do not send more than 50 overlapping requests
  913. to the same server. We may make this configurable later or
  914. use ses->maxReq */
  915. if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
  916. cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
  917. len);
  918. return -EIO;
  919. }
  920. rc = wait_for_free_request(ses->server, timeout, 0);
  921. if (rc)
  922. return rc;
  923. /* make sure that we sign in the same order that we send on this socket
  924. and avoid races inside tcp sendmsg code that could cause corruption
  925. of smb data */
  926. mutex_lock(&ses->server->srv_mutex);
  927. rc = allocate_mid(ses, in_buf, &midQ);
  928. if (rc) {
  929. mutex_unlock(&ses->server->srv_mutex);
  930. /* Update # of requests on wire to server */
  931. add_credits(ses->server, 1, 0);
  932. return rc;
  933. }
  934. rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
  935. if (rc) {
  936. mutex_unlock(&ses->server->srv_mutex);
  937. goto out;
  938. }
  939. midQ->mid_state = MID_REQUEST_SUBMITTED;
  940. cifs_in_send_inc(ses->server);
  941. rc = smb_send(ses->server, in_buf, len);
  942. cifs_in_send_dec(ses->server);
  943. cifs_save_when_sent(midQ);
  944. if (rc < 0)
  945. ses->server->sequence_number -= 2;
  946. mutex_unlock(&ses->server->srv_mutex);
  947. if (rc < 0)
  948. goto out;
  949. if (timeout == CIFS_ASYNC_OP)
  950. goto out;
  951. rc = wait_for_response(ses->server, midQ);
  952. if (rc != 0) {
  953. send_cancel(ses->server, &rqst, midQ);
  954. spin_lock(&GlobalMid_Lock);
  955. if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
  956. /* no longer considered to be "in-flight" */
  957. midQ->callback = DeleteMidQEntry;
  958. spin_unlock(&GlobalMid_Lock);
  959. add_credits(ses->server, 1, 0);
  960. return rc;
  961. }
  962. spin_unlock(&GlobalMid_Lock);
  963. }
  964. rc = cifs_sync_mid_result(midQ, ses->server);
  965. if (rc != 0) {
  966. add_credits(ses->server, 1, 0);
  967. return rc;
  968. }
  969. if (!midQ->resp_buf || !out_buf ||
  970. midQ->mid_state != MID_RESPONSE_RECEIVED) {
  971. rc = -EIO;
  972. cifs_dbg(VFS, "Bad MID state?\n");
  973. goto out;
  974. }
  975. *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
  976. memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
  977. rc = cifs_check_receive(midQ, ses->server, 0);
  978. out:
  979. cifs_delete_mid(midQ);
  980. add_credits(ses->server, 1, 0);
  981. return rc;
  982. }
  983. /* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
  984. blocking lock to return. */
  985. static int
  986. send_lock_cancel(const unsigned int xid, struct cifs_tcon *tcon,
  987. struct smb_hdr *in_buf,
  988. struct smb_hdr *out_buf)
  989. {
  990. int bytes_returned;
  991. struct cifs_ses *ses = tcon->ses;
  992. LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
  993. /* We just modify the current in_buf to change
  994. the type of lock from LOCKING_ANDX_SHARED_LOCK
  995. or LOCKING_ANDX_EXCLUSIVE_LOCK to
  996. LOCKING_ANDX_CANCEL_LOCK. */
  997. pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
  998. pSMB->Timeout = 0;
  999. pSMB->hdr.Mid = get_next_mid(ses->server);
  1000. return SendReceive(xid, ses, in_buf, out_buf,
  1001. &bytes_returned, 0);
  1002. }
  1003. int
  1004. SendReceiveBlockingLock(const unsigned int xid, struct cifs_tcon *tcon,
  1005. struct smb_hdr *in_buf, struct smb_hdr *out_buf,
  1006. int *pbytes_returned)
  1007. {
  1008. int rc = 0;
  1009. int rstart = 0;
  1010. struct mid_q_entry *midQ;
  1011. struct cifs_ses *ses;
  1012. unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
  1013. struct kvec iov = { .iov_base = in_buf, .iov_len = len };
  1014. struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
  1015. if (tcon == NULL || tcon->ses == NULL) {
  1016. cifs_dbg(VFS, "Null smb session\n");
  1017. return -EIO;
  1018. }
  1019. ses = tcon->ses;
  1020. if (ses->server == NULL) {
  1021. cifs_dbg(VFS, "Null tcp session\n");
  1022. return -EIO;
  1023. }
  1024. if (ses->server->tcpStatus == CifsExiting)
  1025. return -ENOENT;
  1026. /* Ensure that we do not send more than 50 overlapping requests
  1027. to the same server. We may make this configurable later or
  1028. use ses->maxReq */
  1029. if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
  1030. cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
  1031. len);
  1032. return -EIO;
  1033. }
  1034. rc = wait_for_free_request(ses->server, CIFS_BLOCKING_OP, 0);
  1035. if (rc)
  1036. return rc;
  1037. /* make sure that we sign in the same order that we send on this socket
  1038. and avoid races inside tcp sendmsg code that could cause corruption
  1039. of smb data */
  1040. mutex_lock(&ses->server->srv_mutex);
  1041. rc = allocate_mid(ses, in_buf, &midQ);
  1042. if (rc) {
  1043. mutex_unlock(&ses->server->srv_mutex);
  1044. return rc;
  1045. }
  1046. rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
  1047. if (rc) {
  1048. cifs_delete_mid(midQ);
  1049. mutex_unlock(&ses->server->srv_mutex);
  1050. return rc;
  1051. }
  1052. midQ->mid_state = MID_REQUEST_SUBMITTED;
  1053. cifs_in_send_inc(ses->server);
  1054. rc = smb_send(ses->server, in_buf, len);
  1055. cifs_in_send_dec(ses->server);
  1056. cifs_save_when_sent(midQ);
  1057. if (rc < 0)
  1058. ses->server->sequence_number -= 2;
  1059. mutex_unlock(&ses->server->srv_mutex);
  1060. if (rc < 0) {
  1061. cifs_delete_mid(midQ);
  1062. return rc;
  1063. }
  1064. /* Wait for a reply - allow signals to interrupt. */
  1065. rc = wait_event_interruptible(ses->server->response_q,
  1066. (!(midQ->mid_state == MID_REQUEST_SUBMITTED)) ||
  1067. ((ses->server->tcpStatus != CifsGood) &&
  1068. (ses->server->tcpStatus != CifsNew)));
  1069. /* Were we interrupted by a signal ? */
  1070. if ((rc == -ERESTARTSYS) &&
  1071. (midQ->mid_state == MID_REQUEST_SUBMITTED) &&
  1072. ((ses->server->tcpStatus == CifsGood) ||
  1073. (ses->server->tcpStatus == CifsNew))) {
  1074. if (in_buf->Command == SMB_COM_TRANSACTION2) {
  1075. /* POSIX lock. We send a NT_CANCEL SMB to cause the
  1076. blocking lock to return. */
  1077. rc = send_cancel(ses->server, &rqst, midQ);
  1078. if (rc) {
  1079. cifs_delete_mid(midQ);
  1080. return rc;
  1081. }
  1082. } else {
  1083. /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
  1084. to cause the blocking lock to return. */
  1085. rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
  1086. /* If we get -ENOLCK back the lock may have
  1087. already been removed. Don't exit in this case. */
  1088. if (rc && rc != -ENOLCK) {
  1089. cifs_delete_mid(midQ);
  1090. return rc;
  1091. }
  1092. }
  1093. rc = wait_for_response(ses->server, midQ);
  1094. if (rc) {
  1095. send_cancel(ses->server, &rqst, midQ);
  1096. spin_lock(&GlobalMid_Lock);
  1097. if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
  1098. /* no longer considered to be "in-flight" */
  1099. midQ->callback = DeleteMidQEntry;
  1100. spin_unlock(&GlobalMid_Lock);
  1101. return rc;
  1102. }
  1103. spin_unlock(&GlobalMid_Lock);
  1104. }
  1105. /* We got the response - restart system call. */
  1106. rstart = 1;
  1107. }
  1108. rc = cifs_sync_mid_result(midQ, ses->server);
  1109. if (rc != 0)
  1110. return rc;
  1111. /* rcvd frame is ok */
  1112. if (out_buf == NULL || midQ->mid_state != MID_RESPONSE_RECEIVED) {
  1113. rc = -EIO;
  1114. cifs_dbg(VFS, "Bad MID state?\n");
  1115. goto out;
  1116. }
  1117. *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
  1118. memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
  1119. rc = cifs_check_receive(midQ, ses->server, 0);
  1120. out:
  1121. cifs_delete_mid(midQ);
  1122. if (rstart && rc == -EACCES)
  1123. return -ERESTARTSYS;
  1124. return rc;
  1125. }