transport.c 29 KB

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