transport.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. /*
  2. * fs/cifs/transport.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2002,2008
  5. * Author(s): Steve French (sfrench@us.ibm.com)
  6. * Jeremy Allison (jra@samba.org) 2006.
  7. *
  8. * This library is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published
  10. * by the Free Software Foundation; either version 2.1 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  16. * the GNU Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/fs.h>
  23. #include <linux/list.h>
  24. #include <linux/wait.h>
  25. #include <linux/net.h>
  26. #include <linux/delay.h>
  27. #include <asm/uaccess.h>
  28. #include <asm/processor.h>
  29. #include <linux/mempool.h>
  30. #include "cifspdu.h"
  31. #include "cifsglob.h"
  32. #include "cifsproto.h"
  33. #include "cifs_debug.h"
  34. extern mempool_t *cifs_mid_poolp;
  35. extern struct kmem_cache *cifs_oplock_cachep;
  36. static struct mid_q_entry *
  37. AllocMidQEntry(const struct smb_hdr *smb_buffer, struct TCP_Server_Info *server)
  38. {
  39. struct mid_q_entry *temp;
  40. if (server == NULL) {
  41. cERROR(1, ("Null TCP session in AllocMidQEntry"));
  42. return NULL;
  43. }
  44. temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
  45. if (temp == NULL)
  46. return temp;
  47. else {
  48. memset(temp, 0, sizeof(struct mid_q_entry));
  49. temp->mid = smb_buffer->Mid; /* always LE */
  50. temp->pid = current->pid;
  51. temp->command = smb_buffer->Command;
  52. cFYI(1, ("For smb_command %d", temp->command));
  53. /* do_gettimeofday(&temp->when_sent);*/ /* easier to use jiffies */
  54. /* when mid allocated can be before when sent */
  55. temp->when_alloc = jiffies;
  56. temp->tsk = current;
  57. }
  58. spin_lock(&GlobalMid_Lock);
  59. list_add_tail(&temp->qhead, &server->pending_mid_q);
  60. atomic_inc(&midCount);
  61. temp->midState = MID_REQUEST_ALLOCATED;
  62. spin_unlock(&GlobalMid_Lock);
  63. return temp;
  64. }
  65. static void
  66. DeleteMidQEntry(struct mid_q_entry *midEntry)
  67. {
  68. #ifdef CONFIG_CIFS_STATS2
  69. unsigned long now;
  70. #endif
  71. spin_lock(&GlobalMid_Lock);
  72. midEntry->midState = MID_FREE;
  73. list_del(&midEntry->qhead);
  74. atomic_dec(&midCount);
  75. spin_unlock(&GlobalMid_Lock);
  76. if (midEntry->largeBuf)
  77. cifs_buf_release(midEntry->resp_buf);
  78. else
  79. cifs_small_buf_release(midEntry->resp_buf);
  80. #ifdef CONFIG_CIFS_STATS2
  81. now = jiffies;
  82. /* commands taking longer than one second are indications that
  83. something is wrong, unless it is quite a slow link or server */
  84. if ((now - midEntry->when_alloc) > HZ) {
  85. if ((cifsFYI & CIFS_TIMER) &&
  86. (midEntry->command != SMB_COM_LOCKING_ANDX)) {
  87. printk(KERN_DEBUG " CIFS slow rsp: cmd %d mid %d",
  88. midEntry->command, midEntry->mid);
  89. printk(" A: 0x%lx S: 0x%lx R: 0x%lx\n",
  90. now - midEntry->when_alloc,
  91. now - midEntry->when_sent,
  92. now - midEntry->when_received);
  93. }
  94. }
  95. #endif
  96. mempool_free(midEntry, cifs_mid_poolp);
  97. }
  98. struct oplock_q_entry *
  99. AllocOplockQEntry(struct inode *pinode, __u16 fid, struct cifsTconInfo *tcon)
  100. {
  101. struct oplock_q_entry *temp;
  102. if ((pinode == NULL) || (tcon == NULL)) {
  103. cERROR(1, ("Null parms passed to AllocOplockQEntry"));
  104. return NULL;
  105. }
  106. temp = (struct oplock_q_entry *) kmem_cache_alloc(cifs_oplock_cachep,
  107. GFP_KERNEL);
  108. if (temp == NULL)
  109. return temp;
  110. else {
  111. temp->pinode = pinode;
  112. temp->tcon = tcon;
  113. temp->netfid = fid;
  114. spin_lock(&GlobalMid_Lock);
  115. list_add_tail(&temp->qhead, &GlobalOplock_Q);
  116. spin_unlock(&GlobalMid_Lock);
  117. }
  118. return temp;
  119. }
  120. void DeleteOplockQEntry(struct oplock_q_entry *oplockEntry)
  121. {
  122. spin_lock(&GlobalMid_Lock);
  123. /* should we check if list empty first? */
  124. list_del(&oplockEntry->qhead);
  125. spin_unlock(&GlobalMid_Lock);
  126. kmem_cache_free(cifs_oplock_cachep, oplockEntry);
  127. }
  128. void DeleteTconOplockQEntries(struct cifsTconInfo *tcon)
  129. {
  130. struct oplock_q_entry *temp;
  131. if (tcon == NULL)
  132. return;
  133. spin_lock(&GlobalMid_Lock);
  134. list_for_each_entry(temp, &GlobalOplock_Q, qhead) {
  135. if ((temp->tcon) && (temp->tcon == tcon)) {
  136. list_del(&temp->qhead);
  137. kmem_cache_free(cifs_oplock_cachep, temp);
  138. }
  139. }
  140. spin_unlock(&GlobalMid_Lock);
  141. }
  142. static int
  143. smb_sendv(struct TCP_Server_Info *server, struct kvec *iov, int n_vec)
  144. {
  145. int rc = 0;
  146. int i = 0;
  147. struct msghdr smb_msg;
  148. struct smb_hdr *smb_buffer = iov[0].iov_base;
  149. unsigned int len = iov[0].iov_len;
  150. unsigned int total_len;
  151. int first_vec = 0;
  152. unsigned int smb_buf_length = smb_buffer->smb_buf_length;
  153. struct socket *ssocket = server->ssocket;
  154. if (ssocket == NULL)
  155. return -ENOTSOCK; /* BB eventually add reconnect code here */
  156. smb_msg.msg_name = (struct sockaddr *) &server->addr.sockAddr;
  157. smb_msg.msg_namelen = sizeof(struct sockaddr);
  158. smb_msg.msg_control = NULL;
  159. smb_msg.msg_controllen = 0;
  160. if (server->noblocksnd)
  161. smb_msg.msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL;
  162. else
  163. smb_msg.msg_flags = MSG_NOSIGNAL;
  164. /* smb header is converted in header_assemble. bcc and rest of SMB word
  165. area, and byte area if necessary, is converted to littleendian in
  166. cifssmb.c and RFC1001 len is converted to bigendian in smb_send
  167. Flags2 is converted in SendReceive */
  168. total_len = 0;
  169. for (i = 0; i < n_vec; i++)
  170. total_len += iov[i].iov_len;
  171. smb_buffer->smb_buf_length = cpu_to_be32(smb_buffer->smb_buf_length);
  172. cFYI(1, ("Sending smb: total_len %d", total_len));
  173. dump_smb(smb_buffer, len);
  174. i = 0;
  175. while (total_len) {
  176. rc = kernel_sendmsg(ssocket, &smb_msg, &iov[first_vec],
  177. n_vec - first_vec, total_len);
  178. if ((rc == -ENOSPC) || (rc == -EAGAIN)) {
  179. i++;
  180. /* if blocking send we try 3 times, since each can block
  181. for 5 seconds. For nonblocking we have to try more
  182. but wait increasing amounts of time allowing time for
  183. socket to clear. The overall time we wait in either
  184. case to send on the socket is about 15 seconds.
  185. Similarly we wait for 15 seconds for
  186. a response from the server in SendReceive[2]
  187. for the server to send a response back for
  188. most types of requests (except SMB Write
  189. past end of file which can be slow, and
  190. blocking lock operations). NFS waits slightly longer
  191. than CIFS, but this can make it take longer for
  192. nonresponsive servers to be detected and 15 seconds
  193. is more than enough time for modern networks to
  194. send a packet. In most cases if we fail to send
  195. after the retries we will kill the socket and
  196. reconnect which may clear the network problem.
  197. */
  198. if ((i >= 14) || (!server->noblocksnd && (i > 2))) {
  199. cERROR(1,
  200. ("sends on sock %p stuck for 15 seconds",
  201. ssocket));
  202. rc = -EAGAIN;
  203. break;
  204. }
  205. msleep(1 << i);
  206. continue;
  207. }
  208. if (rc < 0)
  209. break;
  210. if (rc == total_len) {
  211. total_len = 0;
  212. break;
  213. } else if (rc > total_len) {
  214. cERROR(1, ("sent %d requested %d", rc, total_len));
  215. break;
  216. }
  217. if (rc == 0) {
  218. /* should never happen, letting socket clear before
  219. retrying is our only obvious option here */
  220. cERROR(1, ("tcp sent no data"));
  221. msleep(500);
  222. continue;
  223. }
  224. total_len -= rc;
  225. /* the line below resets i */
  226. for (i = first_vec; i < n_vec; i++) {
  227. if (iov[i].iov_len) {
  228. if (rc > iov[i].iov_len) {
  229. rc -= iov[i].iov_len;
  230. iov[i].iov_len = 0;
  231. } else {
  232. iov[i].iov_base += rc;
  233. iov[i].iov_len -= rc;
  234. first_vec = i;
  235. break;
  236. }
  237. }
  238. }
  239. i = 0; /* in case we get ENOSPC on the next send */
  240. }
  241. if ((total_len > 0) && (total_len != smb_buf_length + 4)) {
  242. cFYI(1, ("partial send (%d remaining), terminating session",
  243. total_len));
  244. /* If we have only sent part of an SMB then the next SMB
  245. could be taken as the remainder of this one. We need
  246. to kill the socket so the server throws away the partial
  247. SMB */
  248. server->tcpStatus = CifsNeedReconnect;
  249. }
  250. if (rc < 0) {
  251. cERROR(1, ("Error %d sending data on socket to server", rc));
  252. } else
  253. rc = 0;
  254. /* Don't want to modify the buffer as a
  255. side effect of this call. */
  256. smb_buffer->smb_buf_length = smb_buf_length;
  257. return rc;
  258. }
  259. int
  260. smb_send(struct TCP_Server_Info *server, struct smb_hdr *smb_buffer,
  261. unsigned int smb_buf_length)
  262. {
  263. struct kvec iov;
  264. iov.iov_base = smb_buffer;
  265. iov.iov_len = smb_buf_length + 4;
  266. return smb_sendv(server, &iov, 1);
  267. }
  268. static int wait_for_free_request(struct cifsSesInfo *ses, const int long_op)
  269. {
  270. if (long_op == CIFS_ASYNC_OP) {
  271. /* oplock breaks must not be held up */
  272. atomic_inc(&ses->server->inFlight);
  273. return 0;
  274. }
  275. spin_lock(&GlobalMid_Lock);
  276. while (1) {
  277. if (atomic_read(&ses->server->inFlight) >=
  278. cifs_max_pending){
  279. spin_unlock(&GlobalMid_Lock);
  280. #ifdef CONFIG_CIFS_STATS2
  281. atomic_inc(&ses->server->num_waiters);
  282. #endif
  283. wait_event(ses->server->request_q,
  284. atomic_read(&ses->server->inFlight)
  285. < cifs_max_pending);
  286. #ifdef CONFIG_CIFS_STATS2
  287. atomic_dec(&ses->server->num_waiters);
  288. #endif
  289. spin_lock(&GlobalMid_Lock);
  290. } else {
  291. if (ses->server->tcpStatus == CifsExiting) {
  292. spin_unlock(&GlobalMid_Lock);
  293. return -ENOENT;
  294. }
  295. /* can not count locking commands against total
  296. as they are allowed to block on server */
  297. /* update # of requests on the wire to server */
  298. if (long_op != CIFS_BLOCKING_OP)
  299. atomic_inc(&ses->server->inFlight);
  300. spin_unlock(&GlobalMid_Lock);
  301. break;
  302. }
  303. }
  304. return 0;
  305. }
  306. static int allocate_mid(struct cifsSesInfo *ses, struct smb_hdr *in_buf,
  307. struct mid_q_entry **ppmidQ)
  308. {
  309. if (ses->server->tcpStatus == CifsExiting) {
  310. return -ENOENT;
  311. }
  312. if (ses->server->tcpStatus == CifsNeedReconnect) {
  313. cFYI(1, ("tcp session dead - return to caller to retry"));
  314. return -EAGAIN;
  315. }
  316. if (ses->status != CifsGood) {
  317. /* check if SMB session is bad because we are setting it up */
  318. if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
  319. (in_buf->Command != SMB_COM_NEGOTIATE))
  320. return -EAGAIN;
  321. /* else ok - we are setting up session */
  322. }
  323. *ppmidQ = AllocMidQEntry(in_buf, ses->server);
  324. if (*ppmidQ == NULL)
  325. return -ENOMEM;
  326. return 0;
  327. }
  328. static int wait_for_response(struct cifsSesInfo *ses,
  329. struct mid_q_entry *midQ,
  330. unsigned long timeout,
  331. unsigned long time_to_wait)
  332. {
  333. unsigned long curr_timeout;
  334. for (;;) {
  335. curr_timeout = timeout + jiffies;
  336. wait_event_timeout(ses->server->response_q,
  337. midQ->midState != MID_REQUEST_SUBMITTED, timeout);
  338. if (time_after(jiffies, curr_timeout) &&
  339. (midQ->midState == MID_REQUEST_SUBMITTED) &&
  340. ((ses->server->tcpStatus == CifsGood) ||
  341. (ses->server->tcpStatus == CifsNew))) {
  342. unsigned long lrt;
  343. /* We timed out. Is the server still
  344. sending replies ? */
  345. spin_lock(&GlobalMid_Lock);
  346. lrt = ses->server->lstrp;
  347. spin_unlock(&GlobalMid_Lock);
  348. /* Calculate time_to_wait past last receive time.
  349. Although we prefer not to time out if the
  350. server is still responding - we will time
  351. out if the server takes more than 15 (or 45
  352. or 180) seconds to respond to this request
  353. and has not responded to any request from
  354. other threads on the client within 10 seconds */
  355. lrt += time_to_wait;
  356. if (time_after(jiffies, lrt)) {
  357. /* No replies for time_to_wait. */
  358. cERROR(1, ("server not responding"));
  359. return -1;
  360. }
  361. } else {
  362. return 0;
  363. }
  364. }
  365. }
  366. /*
  367. *
  368. * Send an SMB Request. No response info (other than return code)
  369. * needs to be parsed.
  370. *
  371. * flags indicate the type of request buffer and how long to wait
  372. * and whether to log NT STATUS code (error) before mapping it to POSIX error
  373. *
  374. */
  375. int
  376. SendReceiveNoRsp(const unsigned int xid, struct cifsSesInfo *ses,
  377. struct smb_hdr *in_buf, int flags)
  378. {
  379. int rc;
  380. struct kvec iov[1];
  381. int resp_buf_type;
  382. iov[0].iov_base = (char *)in_buf;
  383. iov[0].iov_len = in_buf->smb_buf_length + 4;
  384. flags |= CIFS_NO_RESP;
  385. rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags);
  386. cFYI(DBG2, ("SendRcvNoRsp flags %d rc %d", flags, rc));
  387. return rc;
  388. }
  389. int
  390. SendReceive2(const unsigned int xid, struct cifsSesInfo *ses,
  391. struct kvec *iov, int n_vec, int *pRespBufType /* ret */,
  392. const int flags)
  393. {
  394. int rc = 0;
  395. int long_op;
  396. unsigned int receive_len;
  397. unsigned long timeout;
  398. struct mid_q_entry *midQ;
  399. struct smb_hdr *in_buf = iov[0].iov_base;
  400. long_op = flags & CIFS_TIMEOUT_MASK;
  401. *pRespBufType = CIFS_NO_BUFFER; /* no response buf yet */
  402. if ((ses == NULL) || (ses->server == NULL)) {
  403. cifs_small_buf_release(in_buf);
  404. cERROR(1, ("Null session"));
  405. return -EIO;
  406. }
  407. if (ses->server->tcpStatus == CifsExiting) {
  408. cifs_small_buf_release(in_buf);
  409. return -ENOENT;
  410. }
  411. /* Ensure that we do not send more than 50 overlapping requests
  412. to the same server. We may make this configurable later or
  413. use ses->maxReq */
  414. rc = wait_for_free_request(ses, long_op);
  415. if (rc) {
  416. cifs_small_buf_release(in_buf);
  417. return rc;
  418. }
  419. /* make sure that we sign in the same order that we send on this socket
  420. and avoid races inside tcp sendmsg code that could cause corruption
  421. of smb data */
  422. mutex_lock(&ses->server->srv_mutex);
  423. rc = allocate_mid(ses, in_buf, &midQ);
  424. if (rc) {
  425. mutex_unlock(&ses->server->srv_mutex);
  426. cifs_small_buf_release(in_buf);
  427. /* Update # of requests on wire to server */
  428. atomic_dec(&ses->server->inFlight);
  429. wake_up(&ses->server->request_q);
  430. return rc;
  431. }
  432. rc = cifs_sign_smb2(iov, n_vec, ses->server, &midQ->sequence_number);
  433. if (rc) {
  434. mutex_unlock(&ses->server->srv_mutex);
  435. cifs_small_buf_release(in_buf);
  436. goto out;
  437. }
  438. midQ->midState = MID_REQUEST_SUBMITTED;
  439. #ifdef CONFIG_CIFS_STATS2
  440. atomic_inc(&ses->server->inSend);
  441. #endif
  442. rc = smb_sendv(ses->server, iov, n_vec);
  443. #ifdef CONFIG_CIFS_STATS2
  444. atomic_dec(&ses->server->inSend);
  445. midQ->when_sent = jiffies;
  446. #endif
  447. mutex_unlock(&ses->server->srv_mutex);
  448. cifs_small_buf_release(in_buf);
  449. if (rc < 0)
  450. goto out;
  451. if (long_op == CIFS_STD_OP)
  452. timeout = 15 * HZ;
  453. else if (long_op == CIFS_VLONG_OP) /* e.g. slow writes past EOF */
  454. timeout = 180 * HZ;
  455. else if (long_op == CIFS_LONG_OP)
  456. timeout = 45 * HZ; /* should be greater than
  457. servers oplock break timeout (about 43 seconds) */
  458. else if (long_op == CIFS_ASYNC_OP)
  459. goto out;
  460. else if (long_op == CIFS_BLOCKING_OP)
  461. timeout = 0x7FFFFFFF; /* large, but not so large as to wrap */
  462. else {
  463. cERROR(1, ("unknown timeout flag %d", long_op));
  464. rc = -EIO;
  465. goto out;
  466. }
  467. /* wait for 15 seconds or until woken up due to response arriving or
  468. due to last connection to this server being unmounted */
  469. if (signal_pending(current)) {
  470. /* if signal pending do not hold up user for full smb timeout
  471. but we still give response a chance to complete */
  472. timeout = 2 * HZ;
  473. }
  474. /* No user interrupts in wait - wreaks havoc with performance */
  475. wait_for_response(ses, midQ, timeout, 10 * HZ);
  476. spin_lock(&GlobalMid_Lock);
  477. if (midQ->resp_buf == NULL) {
  478. cERROR(1, ("No response to cmd %d mid %d",
  479. midQ->command, midQ->mid));
  480. if (midQ->midState == MID_REQUEST_SUBMITTED) {
  481. if (ses->server->tcpStatus == CifsExiting)
  482. rc = -EHOSTDOWN;
  483. else {
  484. ses->server->tcpStatus = CifsNeedReconnect;
  485. midQ->midState = MID_RETRY_NEEDED;
  486. }
  487. }
  488. if (rc != -EHOSTDOWN) {
  489. if (midQ->midState == MID_RETRY_NEEDED) {
  490. rc = -EAGAIN;
  491. cFYI(1, ("marking request for retry"));
  492. } else {
  493. rc = -EIO;
  494. }
  495. }
  496. spin_unlock(&GlobalMid_Lock);
  497. DeleteMidQEntry(midQ);
  498. /* Update # of requests on wire to server */
  499. atomic_dec(&ses->server->inFlight);
  500. wake_up(&ses->server->request_q);
  501. return rc;
  502. }
  503. spin_unlock(&GlobalMid_Lock);
  504. receive_len = midQ->resp_buf->smb_buf_length;
  505. if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
  506. cERROR(1, ("Frame too large received. Length: %d Xid: %d",
  507. receive_len, xid));
  508. rc = -EIO;
  509. goto out;
  510. }
  511. /* rcvd frame is ok */
  512. if (midQ->resp_buf &&
  513. (midQ->midState == MID_RESPONSE_RECEIVED)) {
  514. iov[0].iov_base = (char *)midQ->resp_buf;
  515. if (midQ->largeBuf)
  516. *pRespBufType = CIFS_LARGE_BUFFER;
  517. else
  518. *pRespBufType = CIFS_SMALL_BUFFER;
  519. iov[0].iov_len = receive_len + 4;
  520. dump_smb(midQ->resp_buf, 80);
  521. /* convert the length into a more usable form */
  522. if ((receive_len > 24) &&
  523. (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
  524. SECMODE_SIGN_ENABLED))) {
  525. rc = cifs_verify_signature(midQ->resp_buf,
  526. &ses->server->mac_signing_key,
  527. midQ->sequence_number+1);
  528. if (rc) {
  529. cERROR(1, ("Unexpected SMB signature"));
  530. /* BB FIXME add code to kill session */
  531. }
  532. }
  533. /* BB special case reconnect tid and uid here? */
  534. rc = map_smb_to_linux_error(midQ->resp_buf,
  535. flags & CIFS_LOG_ERROR);
  536. /* convert ByteCount if necessary */
  537. if (receive_len >= sizeof(struct smb_hdr) - 4
  538. /* do not count RFC1001 header */ +
  539. (2 * midQ->resp_buf->WordCount) + 2 /* bcc */ )
  540. BCC(midQ->resp_buf) =
  541. le16_to_cpu(BCC_LE(midQ->resp_buf));
  542. if ((flags & CIFS_NO_RESP) == 0)
  543. midQ->resp_buf = NULL; /* mark it so buf will
  544. not be freed by
  545. DeleteMidQEntry */
  546. } else {
  547. rc = -EIO;
  548. cFYI(1, ("Bad MID state?"));
  549. }
  550. out:
  551. DeleteMidQEntry(midQ);
  552. atomic_dec(&ses->server->inFlight);
  553. wake_up(&ses->server->request_q);
  554. return rc;
  555. }
  556. int
  557. SendReceive(const unsigned int xid, struct cifsSesInfo *ses,
  558. struct smb_hdr *in_buf, struct smb_hdr *out_buf,
  559. int *pbytes_returned, const int long_op)
  560. {
  561. int rc = 0;
  562. unsigned int receive_len;
  563. unsigned long timeout;
  564. struct mid_q_entry *midQ;
  565. if (ses == NULL) {
  566. cERROR(1, ("Null smb session"));
  567. return -EIO;
  568. }
  569. if (ses->server == NULL) {
  570. cERROR(1, ("Null tcp session"));
  571. return -EIO;
  572. }
  573. if (ses->server->tcpStatus == CifsExiting)
  574. return -ENOENT;
  575. /* Ensure that we do not send more than 50 overlapping requests
  576. to the same server. We may make this configurable later or
  577. use ses->maxReq */
  578. if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
  579. cERROR(1, ("Illegal length, greater than maximum frame, %d",
  580. in_buf->smb_buf_length));
  581. return -EIO;
  582. }
  583. rc = wait_for_free_request(ses, long_op);
  584. if (rc)
  585. return rc;
  586. /* make sure that we sign in the same order that we send on this socket
  587. and avoid races inside tcp sendmsg code that could cause corruption
  588. of smb data */
  589. mutex_lock(&ses->server->srv_mutex);
  590. rc = allocate_mid(ses, in_buf, &midQ);
  591. if (rc) {
  592. mutex_unlock(&ses->server->srv_mutex);
  593. /* Update # of requests on wire to server */
  594. atomic_dec(&ses->server->inFlight);
  595. wake_up(&ses->server->request_q);
  596. return rc;
  597. }
  598. rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
  599. if (rc) {
  600. mutex_unlock(&ses->server->srv_mutex);
  601. goto out;
  602. }
  603. midQ->midState = MID_REQUEST_SUBMITTED;
  604. #ifdef CONFIG_CIFS_STATS2
  605. atomic_inc(&ses->server->inSend);
  606. #endif
  607. rc = smb_send(ses->server, in_buf, in_buf->smb_buf_length);
  608. #ifdef CONFIG_CIFS_STATS2
  609. atomic_dec(&ses->server->inSend);
  610. midQ->when_sent = jiffies;
  611. #endif
  612. mutex_unlock(&ses->server->srv_mutex);
  613. if (rc < 0)
  614. goto out;
  615. if (long_op == CIFS_STD_OP)
  616. timeout = 15 * HZ;
  617. /* wait for 15 seconds or until woken up due to response arriving or
  618. due to last connection to this server being unmounted */
  619. else if (long_op == CIFS_ASYNC_OP)
  620. goto out;
  621. else if (long_op == CIFS_VLONG_OP) /* writes past EOF can be slow */
  622. timeout = 180 * HZ;
  623. else if (long_op == CIFS_LONG_OP)
  624. timeout = 45 * HZ; /* should be greater than
  625. servers oplock break timeout (about 43 seconds) */
  626. else if (long_op == CIFS_BLOCKING_OP)
  627. timeout = 0x7FFFFFFF; /* large but no so large as to wrap */
  628. else {
  629. cERROR(1, ("unknown timeout flag %d", long_op));
  630. rc = -EIO;
  631. goto out;
  632. }
  633. if (signal_pending(current)) {
  634. /* if signal pending do not hold up user for full smb timeout
  635. but we still give response a chance to complete */
  636. timeout = 2 * HZ;
  637. }
  638. /* No user interrupts in wait - wreaks havoc with performance */
  639. wait_for_response(ses, midQ, timeout, 10 * HZ);
  640. spin_lock(&GlobalMid_Lock);
  641. if (midQ->resp_buf == NULL) {
  642. cERROR(1, ("No response for cmd %d mid %d",
  643. midQ->command, midQ->mid));
  644. if (midQ->midState == MID_REQUEST_SUBMITTED) {
  645. if (ses->server->tcpStatus == CifsExiting)
  646. rc = -EHOSTDOWN;
  647. else {
  648. ses->server->tcpStatus = CifsNeedReconnect;
  649. midQ->midState = MID_RETRY_NEEDED;
  650. }
  651. }
  652. if (rc != -EHOSTDOWN) {
  653. if (midQ->midState == MID_RETRY_NEEDED) {
  654. rc = -EAGAIN;
  655. cFYI(1, ("marking request for retry"));
  656. } else {
  657. rc = -EIO;
  658. }
  659. }
  660. spin_unlock(&GlobalMid_Lock);
  661. DeleteMidQEntry(midQ);
  662. /* Update # of requests on wire to server */
  663. atomic_dec(&ses->server->inFlight);
  664. wake_up(&ses->server->request_q);
  665. return rc;
  666. }
  667. spin_unlock(&GlobalMid_Lock);
  668. receive_len = midQ->resp_buf->smb_buf_length;
  669. if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
  670. cERROR(1, ("Frame too large received. Length: %d Xid: %d",
  671. receive_len, xid));
  672. rc = -EIO;
  673. goto out;
  674. }
  675. /* rcvd frame is ok */
  676. if (midQ->resp_buf && out_buf
  677. && (midQ->midState == MID_RESPONSE_RECEIVED)) {
  678. out_buf->smb_buf_length = receive_len;
  679. memcpy((char *)out_buf + 4,
  680. (char *)midQ->resp_buf + 4,
  681. receive_len);
  682. dump_smb(out_buf, 92);
  683. /* convert the length into a more usable form */
  684. if ((receive_len > 24) &&
  685. (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
  686. SECMODE_SIGN_ENABLED))) {
  687. rc = cifs_verify_signature(out_buf,
  688. &ses->server->mac_signing_key,
  689. midQ->sequence_number+1);
  690. if (rc) {
  691. cERROR(1, ("Unexpected SMB signature"));
  692. /* BB FIXME add code to kill session */
  693. }
  694. }
  695. *pbytes_returned = out_buf->smb_buf_length;
  696. /* BB special case reconnect tid and uid here? */
  697. rc = map_smb_to_linux_error(out_buf, 0 /* no log */ );
  698. /* convert ByteCount if necessary */
  699. if (receive_len >= sizeof(struct smb_hdr) - 4
  700. /* do not count RFC1001 header */ +
  701. (2 * out_buf->WordCount) + 2 /* bcc */ )
  702. BCC(out_buf) = le16_to_cpu(BCC_LE(out_buf));
  703. } else {
  704. rc = -EIO;
  705. cERROR(1, ("Bad MID state?"));
  706. }
  707. out:
  708. DeleteMidQEntry(midQ);
  709. atomic_dec(&ses->server->inFlight);
  710. wake_up(&ses->server->request_q);
  711. return rc;
  712. }
  713. /* Send an NT_CANCEL SMB to cause the POSIX blocking lock to return. */
  714. static int
  715. send_nt_cancel(struct cifsTconInfo *tcon, struct smb_hdr *in_buf,
  716. struct mid_q_entry *midQ)
  717. {
  718. int rc = 0;
  719. struct cifsSesInfo *ses = tcon->ses;
  720. __u16 mid = in_buf->Mid;
  721. header_assemble(in_buf, SMB_COM_NT_CANCEL, tcon, 0);
  722. in_buf->Mid = mid;
  723. mutex_lock(&ses->server->srv_mutex);
  724. rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
  725. if (rc) {
  726. mutex_unlock(&ses->server->srv_mutex);
  727. return rc;
  728. }
  729. rc = smb_send(ses->server, in_buf, in_buf->smb_buf_length);
  730. mutex_unlock(&ses->server->srv_mutex);
  731. return rc;
  732. }
  733. /* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
  734. blocking lock to return. */
  735. static int
  736. send_lock_cancel(const unsigned int xid, struct cifsTconInfo *tcon,
  737. struct smb_hdr *in_buf,
  738. struct smb_hdr *out_buf)
  739. {
  740. int bytes_returned;
  741. struct cifsSesInfo *ses = tcon->ses;
  742. LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
  743. /* We just modify the current in_buf to change
  744. the type of lock from LOCKING_ANDX_SHARED_LOCK
  745. or LOCKING_ANDX_EXCLUSIVE_LOCK to
  746. LOCKING_ANDX_CANCEL_LOCK. */
  747. pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
  748. pSMB->Timeout = 0;
  749. pSMB->hdr.Mid = GetNextMid(ses->server);
  750. return SendReceive(xid, ses, in_buf, out_buf,
  751. &bytes_returned, CIFS_STD_OP);
  752. }
  753. int
  754. SendReceiveBlockingLock(const unsigned int xid, struct cifsTconInfo *tcon,
  755. struct smb_hdr *in_buf, struct smb_hdr *out_buf,
  756. int *pbytes_returned)
  757. {
  758. int rc = 0;
  759. int rstart = 0;
  760. unsigned int receive_len;
  761. struct mid_q_entry *midQ;
  762. struct cifsSesInfo *ses;
  763. if (tcon == NULL || tcon->ses == NULL) {
  764. cERROR(1, ("Null smb session"));
  765. return -EIO;
  766. }
  767. ses = tcon->ses;
  768. if (ses->server == NULL) {
  769. cERROR(1, ("Null tcp session"));
  770. return -EIO;
  771. }
  772. if (ses->server->tcpStatus == CifsExiting)
  773. return -ENOENT;
  774. /* Ensure that we do not send more than 50 overlapping requests
  775. to the same server. We may make this configurable later or
  776. use ses->maxReq */
  777. if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
  778. cERROR(1, ("Illegal length, greater than maximum frame, %d",
  779. in_buf->smb_buf_length));
  780. return -EIO;
  781. }
  782. rc = wait_for_free_request(ses, CIFS_BLOCKING_OP);
  783. if (rc)
  784. return rc;
  785. /* make sure that we sign in the same order that we send on this socket
  786. and avoid races inside tcp sendmsg code that could cause corruption
  787. of smb data */
  788. mutex_lock(&ses->server->srv_mutex);
  789. rc = allocate_mid(ses, in_buf, &midQ);
  790. if (rc) {
  791. mutex_unlock(&ses->server->srv_mutex);
  792. return rc;
  793. }
  794. rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
  795. if (rc) {
  796. DeleteMidQEntry(midQ);
  797. mutex_unlock(&ses->server->srv_mutex);
  798. return rc;
  799. }
  800. midQ->midState = MID_REQUEST_SUBMITTED;
  801. #ifdef CONFIG_CIFS_STATS2
  802. atomic_inc(&ses->server->inSend);
  803. #endif
  804. rc = smb_send(ses->server, in_buf, in_buf->smb_buf_length);
  805. #ifdef CONFIG_CIFS_STATS2
  806. atomic_dec(&ses->server->inSend);
  807. midQ->when_sent = jiffies;
  808. #endif
  809. mutex_unlock(&ses->server->srv_mutex);
  810. if (rc < 0) {
  811. DeleteMidQEntry(midQ);
  812. return rc;
  813. }
  814. /* Wait for a reply - allow signals to interrupt. */
  815. rc = wait_event_interruptible(ses->server->response_q,
  816. (!(midQ->midState == MID_REQUEST_SUBMITTED)) ||
  817. ((ses->server->tcpStatus != CifsGood) &&
  818. (ses->server->tcpStatus != CifsNew)));
  819. /* Were we interrupted by a signal ? */
  820. if ((rc == -ERESTARTSYS) &&
  821. (midQ->midState == MID_REQUEST_SUBMITTED) &&
  822. ((ses->server->tcpStatus == CifsGood) ||
  823. (ses->server->tcpStatus == CifsNew))) {
  824. if (in_buf->Command == SMB_COM_TRANSACTION2) {
  825. /* POSIX lock. We send a NT_CANCEL SMB to cause the
  826. blocking lock to return. */
  827. rc = send_nt_cancel(tcon, in_buf, midQ);
  828. if (rc) {
  829. DeleteMidQEntry(midQ);
  830. return rc;
  831. }
  832. } else {
  833. /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
  834. to cause the blocking lock to return. */
  835. rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
  836. /* If we get -ENOLCK back the lock may have
  837. already been removed. Don't exit in this case. */
  838. if (rc && rc != -ENOLCK) {
  839. DeleteMidQEntry(midQ);
  840. return rc;
  841. }
  842. }
  843. /* Wait 5 seconds for the response. */
  844. if (wait_for_response(ses, midQ, 5 * HZ, 5 * HZ) == 0) {
  845. /* We got the response - restart system call. */
  846. rstart = 1;
  847. }
  848. }
  849. spin_lock(&GlobalMid_Lock);
  850. if (midQ->resp_buf) {
  851. spin_unlock(&GlobalMid_Lock);
  852. receive_len = midQ->resp_buf->smb_buf_length;
  853. } else {
  854. cERROR(1, ("No response for cmd %d mid %d",
  855. midQ->command, midQ->mid));
  856. if (midQ->midState == MID_REQUEST_SUBMITTED) {
  857. if (ses->server->tcpStatus == CifsExiting)
  858. rc = -EHOSTDOWN;
  859. else {
  860. ses->server->tcpStatus = CifsNeedReconnect;
  861. midQ->midState = MID_RETRY_NEEDED;
  862. }
  863. }
  864. if (rc != -EHOSTDOWN) {
  865. if (midQ->midState == MID_RETRY_NEEDED) {
  866. rc = -EAGAIN;
  867. cFYI(1, ("marking request for retry"));
  868. } else {
  869. rc = -EIO;
  870. }
  871. }
  872. spin_unlock(&GlobalMid_Lock);
  873. DeleteMidQEntry(midQ);
  874. return rc;
  875. }
  876. if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
  877. cERROR(1, ("Frame too large received. Length: %d Xid: %d",
  878. receive_len, xid));
  879. rc = -EIO;
  880. goto out;
  881. }
  882. /* rcvd frame is ok */
  883. if ((out_buf == NULL) || (midQ->midState != MID_RESPONSE_RECEIVED)) {
  884. rc = -EIO;
  885. cERROR(1, ("Bad MID state?"));
  886. goto out;
  887. }
  888. out_buf->smb_buf_length = receive_len;
  889. memcpy((char *)out_buf + 4,
  890. (char *)midQ->resp_buf + 4,
  891. receive_len);
  892. dump_smb(out_buf, 92);
  893. /* convert the length into a more usable form */
  894. if ((receive_len > 24) &&
  895. (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
  896. SECMODE_SIGN_ENABLED))) {
  897. rc = cifs_verify_signature(out_buf,
  898. &ses->server->mac_signing_key,
  899. midQ->sequence_number+1);
  900. if (rc) {
  901. cERROR(1, ("Unexpected SMB signature"));
  902. /* BB FIXME add code to kill session */
  903. }
  904. }
  905. *pbytes_returned = out_buf->smb_buf_length;
  906. /* BB special case reconnect tid and uid here? */
  907. rc = map_smb_to_linux_error(out_buf, 0 /* no log */ );
  908. /* convert ByteCount if necessary */
  909. if (receive_len >= sizeof(struct smb_hdr) - 4
  910. /* do not count RFC1001 header */ +
  911. (2 * out_buf->WordCount) + 2 /* bcc */ )
  912. BCC(out_buf) = le16_to_cpu(BCC_LE(out_buf));
  913. out:
  914. DeleteMidQEntry(midQ);
  915. if (rstart && rc == -EACCES)
  916. return -ERESTARTSYS;
  917. return rc;
  918. }