transport.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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. midQ->midState = MID_REQUEST_SUBMITTED;
  471. #ifdef CONFIG_CIFS_STATS2
  472. atomic_inc(&ses->server->inSend);
  473. #endif
  474. rc = smb_send2(ses->server, iov, n_vec,
  475. (struct sockaddr *) &(ses->server->addr.sockAddr),
  476. ses->server->noblocksnd);
  477. #ifdef CONFIG_CIFS_STATS2
  478. atomic_dec(&ses->server->inSend);
  479. midQ->when_sent = jiffies;
  480. #endif
  481. mutex_unlock(&ses->server->srv_mutex);
  482. cifs_small_buf_release(in_buf);
  483. if (rc < 0)
  484. goto out;
  485. if (long_op == CIFS_STD_OP)
  486. timeout = 15 * HZ;
  487. else if (long_op == CIFS_VLONG_OP) /* e.g. slow writes past EOF */
  488. timeout = 180 * HZ;
  489. else if (long_op == CIFS_LONG_OP)
  490. timeout = 45 * HZ; /* should be greater than
  491. servers oplock break timeout (about 43 seconds) */
  492. else if (long_op == CIFS_ASYNC_OP)
  493. goto out;
  494. else if (long_op == CIFS_BLOCKING_OP)
  495. timeout = 0x7FFFFFFF; /* large, but not so large as to wrap */
  496. else {
  497. cERROR(1, ("unknown timeout flag %d", long_op));
  498. rc = -EIO;
  499. goto out;
  500. }
  501. /* wait for 15 seconds or until woken up due to response arriving or
  502. due to last connection to this server being unmounted */
  503. if (signal_pending(current)) {
  504. /* if signal pending do not hold up user for full smb timeout
  505. but we still give response a chance to complete */
  506. timeout = 2 * HZ;
  507. }
  508. /* No user interrupts in wait - wreaks havoc with performance */
  509. wait_for_response(ses, midQ, timeout, 10 * HZ);
  510. spin_lock(&GlobalMid_Lock);
  511. if (midQ->resp_buf) {
  512. spin_unlock(&GlobalMid_Lock);
  513. receive_len = midQ->resp_buf->smb_buf_length;
  514. } else {
  515. cERROR(1, ("No response to cmd %d mid %d",
  516. midQ->command, midQ->mid));
  517. if (midQ->midState == MID_REQUEST_SUBMITTED) {
  518. if (ses->server->tcpStatus == CifsExiting)
  519. rc = -EHOSTDOWN;
  520. else {
  521. ses->server->tcpStatus = CifsNeedReconnect;
  522. midQ->midState = MID_RETRY_NEEDED;
  523. }
  524. }
  525. if (rc != -EHOSTDOWN) {
  526. if (midQ->midState == MID_RETRY_NEEDED) {
  527. rc = -EAGAIN;
  528. cFYI(1, ("marking request for retry"));
  529. } else {
  530. rc = -EIO;
  531. }
  532. }
  533. spin_unlock(&GlobalMid_Lock);
  534. DeleteMidQEntry(midQ);
  535. /* Update # of requests on wire to server */
  536. atomic_dec(&ses->server->inFlight);
  537. wake_up(&ses->server->request_q);
  538. return rc;
  539. }
  540. if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
  541. cERROR(1, ("Frame too large received. Length: %d Xid: %d",
  542. receive_len, xid));
  543. rc = -EIO;
  544. } else { /* rcvd frame is ok */
  545. if (midQ->resp_buf &&
  546. (midQ->midState == MID_RESPONSE_RECEIVED)) {
  547. iov[0].iov_base = (char *)midQ->resp_buf;
  548. if (midQ->largeBuf)
  549. *pRespBufType = CIFS_LARGE_BUFFER;
  550. else
  551. *pRespBufType = CIFS_SMALL_BUFFER;
  552. iov[0].iov_len = receive_len + 4;
  553. dump_smb(midQ->resp_buf, 80);
  554. /* convert the length into a more usable form */
  555. if ((receive_len > 24) &&
  556. (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
  557. SECMODE_SIGN_ENABLED))) {
  558. rc = cifs_verify_signature(midQ->resp_buf,
  559. &ses->server->mac_signing_key,
  560. midQ->sequence_number+1);
  561. if (rc) {
  562. cERROR(1, ("Unexpected SMB signature"));
  563. /* BB FIXME add code to kill session */
  564. }
  565. }
  566. /* BB special case reconnect tid and uid here? */
  567. rc = map_smb_to_linux_error(midQ->resp_buf,
  568. flags & CIFS_LOG_ERROR);
  569. /* convert ByteCount if necessary */
  570. if (receive_len >= sizeof(struct smb_hdr) - 4
  571. /* do not count RFC1001 header */ +
  572. (2 * midQ->resp_buf->WordCount) + 2 /* bcc */ )
  573. BCC(midQ->resp_buf) =
  574. le16_to_cpu(BCC_LE(midQ->resp_buf));
  575. if ((flags & CIFS_NO_RESP) == 0)
  576. midQ->resp_buf = NULL; /* mark it so buf will
  577. not be freed by
  578. DeleteMidQEntry */
  579. } else {
  580. rc = -EIO;
  581. cFYI(1, ("Bad MID state?"));
  582. }
  583. }
  584. out:
  585. DeleteMidQEntry(midQ);
  586. atomic_dec(&ses->server->inFlight);
  587. wake_up(&ses->server->request_q);
  588. return rc;
  589. }
  590. int
  591. SendReceive(const unsigned int xid, struct cifsSesInfo *ses,
  592. struct smb_hdr *in_buf, struct smb_hdr *out_buf,
  593. int *pbytes_returned, const int long_op)
  594. {
  595. int rc = 0;
  596. unsigned int receive_len;
  597. unsigned long timeout;
  598. struct mid_q_entry *midQ;
  599. if (ses == NULL) {
  600. cERROR(1, ("Null smb session"));
  601. return -EIO;
  602. }
  603. if (ses->server == NULL) {
  604. cERROR(1, ("Null tcp session"));
  605. return -EIO;
  606. }
  607. if (ses->server->tcpStatus == CifsExiting)
  608. return -ENOENT;
  609. /* Ensure that we do not send more than 50 overlapping requests
  610. to the same server. We may make this configurable later or
  611. use ses->maxReq */
  612. if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
  613. cERROR(1, ("Illegal length, greater than maximum frame, %d",
  614. in_buf->smb_buf_length));
  615. return -EIO;
  616. }
  617. rc = wait_for_free_request(ses, long_op);
  618. if (rc)
  619. return rc;
  620. /* make sure that we sign in the same order that we send on this socket
  621. and avoid races inside tcp sendmsg code that could cause corruption
  622. of smb data */
  623. mutex_lock(&ses->server->srv_mutex);
  624. rc = allocate_mid(ses, in_buf, &midQ);
  625. if (rc) {
  626. mutex_unlock(&ses->server->srv_mutex);
  627. /* Update # of requests on wire to server */
  628. atomic_dec(&ses->server->inFlight);
  629. wake_up(&ses->server->request_q);
  630. return rc;
  631. }
  632. rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
  633. midQ->midState = MID_REQUEST_SUBMITTED;
  634. #ifdef CONFIG_CIFS_STATS2
  635. atomic_inc(&ses->server->inSend);
  636. #endif
  637. rc = smb_send(ses->server->ssocket, in_buf, in_buf->smb_buf_length,
  638. (struct sockaddr *) &(ses->server->addr.sockAddr),
  639. ses->server->noblocksnd);
  640. #ifdef CONFIG_CIFS_STATS2
  641. atomic_dec(&ses->server->inSend);
  642. midQ->when_sent = jiffies;
  643. #endif
  644. mutex_unlock(&ses->server->srv_mutex);
  645. if (rc < 0)
  646. goto out;
  647. if (long_op == CIFS_STD_OP)
  648. timeout = 15 * HZ;
  649. /* wait for 15 seconds or until woken up due to response arriving or
  650. due to last connection to this server being unmounted */
  651. else if (long_op == CIFS_ASYNC_OP)
  652. goto out;
  653. else if (long_op == CIFS_VLONG_OP) /* writes past EOF can be slow */
  654. timeout = 180 * HZ;
  655. else if (long_op == CIFS_LONG_OP)
  656. timeout = 45 * HZ; /* should be greater than
  657. servers oplock break timeout (about 43 seconds) */
  658. else if (long_op == CIFS_BLOCKING_OP)
  659. timeout = 0x7FFFFFFF; /* large but no so large as to wrap */
  660. else {
  661. cERROR(1, ("unknown timeout flag %d", long_op));
  662. rc = -EIO;
  663. goto out;
  664. }
  665. if (signal_pending(current)) {
  666. /* if signal pending do not hold up user for full smb timeout
  667. but we still give response a chance to complete */
  668. timeout = 2 * HZ;
  669. }
  670. /* No user interrupts in wait - wreaks havoc with performance */
  671. wait_for_response(ses, midQ, timeout, 10 * HZ);
  672. spin_lock(&GlobalMid_Lock);
  673. if (midQ->resp_buf) {
  674. spin_unlock(&GlobalMid_Lock);
  675. receive_len = midQ->resp_buf->smb_buf_length;
  676. } else {
  677. cERROR(1, ("No response for cmd %d mid %d",
  678. midQ->command, midQ->mid));
  679. if (midQ->midState == MID_REQUEST_SUBMITTED) {
  680. if (ses->server->tcpStatus == CifsExiting)
  681. rc = -EHOSTDOWN;
  682. else {
  683. ses->server->tcpStatus = CifsNeedReconnect;
  684. midQ->midState = MID_RETRY_NEEDED;
  685. }
  686. }
  687. if (rc != -EHOSTDOWN) {
  688. if (midQ->midState == MID_RETRY_NEEDED) {
  689. rc = -EAGAIN;
  690. cFYI(1, ("marking request for retry"));
  691. } else {
  692. rc = -EIO;
  693. }
  694. }
  695. spin_unlock(&GlobalMid_Lock);
  696. DeleteMidQEntry(midQ);
  697. /* Update # of requests on wire to server */
  698. atomic_dec(&ses->server->inFlight);
  699. wake_up(&ses->server->request_q);
  700. return rc;
  701. }
  702. if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
  703. cERROR(1, ("Frame too large received. Length: %d Xid: %d",
  704. receive_len, xid));
  705. rc = -EIO;
  706. } else { /* rcvd frame is ok */
  707. if (midQ->resp_buf && out_buf
  708. && (midQ->midState == MID_RESPONSE_RECEIVED)) {
  709. out_buf->smb_buf_length = receive_len;
  710. memcpy((char *)out_buf + 4,
  711. (char *)midQ->resp_buf + 4,
  712. receive_len);
  713. dump_smb(out_buf, 92);
  714. /* convert the length into a more usable form */
  715. if ((receive_len > 24) &&
  716. (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
  717. SECMODE_SIGN_ENABLED))) {
  718. rc = cifs_verify_signature(out_buf,
  719. &ses->server->mac_signing_key,
  720. midQ->sequence_number+1);
  721. if (rc) {
  722. cERROR(1, ("Unexpected SMB signature"));
  723. /* BB FIXME add code to kill session */
  724. }
  725. }
  726. *pbytes_returned = out_buf->smb_buf_length;
  727. /* BB special case reconnect tid and uid here? */
  728. rc = map_smb_to_linux_error(out_buf, 0 /* no log */ );
  729. /* convert ByteCount if necessary */
  730. if (receive_len >= sizeof(struct smb_hdr) - 4
  731. /* do not count RFC1001 header */ +
  732. (2 * out_buf->WordCount) + 2 /* bcc */ )
  733. BCC(out_buf) = le16_to_cpu(BCC_LE(out_buf));
  734. } else {
  735. rc = -EIO;
  736. cERROR(1, ("Bad MID state?"));
  737. }
  738. }
  739. out:
  740. DeleteMidQEntry(midQ);
  741. atomic_dec(&ses->server->inFlight);
  742. wake_up(&ses->server->request_q);
  743. return rc;
  744. }
  745. /* Send an NT_CANCEL SMB to cause the POSIX blocking lock to return. */
  746. static int
  747. send_nt_cancel(struct cifsTconInfo *tcon, struct smb_hdr *in_buf,
  748. struct mid_q_entry *midQ)
  749. {
  750. int rc = 0;
  751. struct cifsSesInfo *ses = tcon->ses;
  752. __u16 mid = in_buf->Mid;
  753. header_assemble(in_buf, SMB_COM_NT_CANCEL, tcon, 0);
  754. in_buf->Mid = mid;
  755. mutex_lock(&ses->server->srv_mutex);
  756. rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
  757. if (rc) {
  758. mutex_unlock(&ses->server->srv_mutex);
  759. return rc;
  760. }
  761. rc = smb_send(ses->server->ssocket, in_buf, in_buf->smb_buf_length,
  762. (struct sockaddr *) &(ses->server->addr.sockAddr),
  763. ses->server->noblocksnd);
  764. mutex_unlock(&ses->server->srv_mutex);
  765. return rc;
  766. }
  767. /* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
  768. blocking lock to return. */
  769. static int
  770. send_lock_cancel(const unsigned int xid, struct cifsTconInfo *tcon,
  771. struct smb_hdr *in_buf,
  772. struct smb_hdr *out_buf)
  773. {
  774. int bytes_returned;
  775. struct cifsSesInfo *ses = tcon->ses;
  776. LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
  777. /* We just modify the current in_buf to change
  778. the type of lock from LOCKING_ANDX_SHARED_LOCK
  779. or LOCKING_ANDX_EXCLUSIVE_LOCK to
  780. LOCKING_ANDX_CANCEL_LOCK. */
  781. pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
  782. pSMB->Timeout = 0;
  783. pSMB->hdr.Mid = GetNextMid(ses->server);
  784. return SendReceive(xid, ses, in_buf, out_buf,
  785. &bytes_returned, CIFS_STD_OP);
  786. }
  787. int
  788. SendReceiveBlockingLock(const unsigned int xid, struct cifsTconInfo *tcon,
  789. struct smb_hdr *in_buf, struct smb_hdr *out_buf,
  790. int *pbytes_returned)
  791. {
  792. int rc = 0;
  793. int rstart = 0;
  794. unsigned int receive_len;
  795. struct mid_q_entry *midQ;
  796. struct cifsSesInfo *ses;
  797. if (tcon == NULL || tcon->ses == NULL) {
  798. cERROR(1, ("Null smb session"));
  799. return -EIO;
  800. }
  801. ses = tcon->ses;
  802. if (ses->server == NULL) {
  803. cERROR(1, ("Null tcp session"));
  804. return -EIO;
  805. }
  806. if (ses->server->tcpStatus == CifsExiting)
  807. return -ENOENT;
  808. /* Ensure that we do not send more than 50 overlapping requests
  809. to the same server. We may make this configurable later or
  810. use ses->maxReq */
  811. if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
  812. cERROR(1, ("Illegal length, greater than maximum frame, %d",
  813. in_buf->smb_buf_length));
  814. return -EIO;
  815. }
  816. rc = wait_for_free_request(ses, CIFS_BLOCKING_OP);
  817. if (rc)
  818. return rc;
  819. /* make sure that we sign in the same order that we send on this socket
  820. and avoid races inside tcp sendmsg code that could cause corruption
  821. of smb data */
  822. mutex_lock(&ses->server->srv_mutex);
  823. rc = allocate_mid(ses, in_buf, &midQ);
  824. if (rc) {
  825. mutex_unlock(&ses->server->srv_mutex);
  826. return rc;
  827. }
  828. rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
  829. midQ->midState = MID_REQUEST_SUBMITTED;
  830. #ifdef CONFIG_CIFS_STATS2
  831. atomic_inc(&ses->server->inSend);
  832. #endif
  833. rc = smb_send(ses->server->ssocket, in_buf, in_buf->smb_buf_length,
  834. (struct sockaddr *) &(ses->server->addr.sockAddr),
  835. ses->server->noblocksnd);
  836. #ifdef CONFIG_CIFS_STATS2
  837. atomic_dec(&ses->server->inSend);
  838. midQ->when_sent = jiffies;
  839. #endif
  840. mutex_unlock(&ses->server->srv_mutex);
  841. if (rc < 0) {
  842. DeleteMidQEntry(midQ);
  843. return rc;
  844. }
  845. /* Wait for a reply - allow signals to interrupt. */
  846. rc = wait_event_interruptible(ses->server->response_q,
  847. (!(midQ->midState == MID_REQUEST_SUBMITTED)) ||
  848. ((ses->server->tcpStatus != CifsGood) &&
  849. (ses->server->tcpStatus != CifsNew)));
  850. /* Were we interrupted by a signal ? */
  851. if ((rc == -ERESTARTSYS) &&
  852. (midQ->midState == MID_REQUEST_SUBMITTED) &&
  853. ((ses->server->tcpStatus == CifsGood) ||
  854. (ses->server->tcpStatus == CifsNew))) {
  855. if (in_buf->Command == SMB_COM_TRANSACTION2) {
  856. /* POSIX lock. We send a NT_CANCEL SMB to cause the
  857. blocking lock to return. */
  858. rc = send_nt_cancel(tcon, in_buf, midQ);
  859. if (rc) {
  860. DeleteMidQEntry(midQ);
  861. return rc;
  862. }
  863. } else {
  864. /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
  865. to cause the blocking lock to return. */
  866. rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
  867. /* If we get -ENOLCK back the lock may have
  868. already been removed. Don't exit in this case. */
  869. if (rc && rc != -ENOLCK) {
  870. DeleteMidQEntry(midQ);
  871. return rc;
  872. }
  873. }
  874. /* Wait 5 seconds for the response. */
  875. if (wait_for_response(ses, midQ, 5 * HZ, 5 * HZ) == 0) {
  876. /* We got the response - restart system call. */
  877. rstart = 1;
  878. }
  879. }
  880. spin_lock(&GlobalMid_Lock);
  881. if (midQ->resp_buf) {
  882. spin_unlock(&GlobalMid_Lock);
  883. receive_len = midQ->resp_buf->smb_buf_length;
  884. } else {
  885. cERROR(1, ("No response for cmd %d mid %d",
  886. midQ->command, midQ->mid));
  887. if (midQ->midState == MID_REQUEST_SUBMITTED) {
  888. if (ses->server->tcpStatus == CifsExiting)
  889. rc = -EHOSTDOWN;
  890. else {
  891. ses->server->tcpStatus = CifsNeedReconnect;
  892. midQ->midState = MID_RETRY_NEEDED;
  893. }
  894. }
  895. if (rc != -EHOSTDOWN) {
  896. if (midQ->midState == MID_RETRY_NEEDED) {
  897. rc = -EAGAIN;
  898. cFYI(1, ("marking request for retry"));
  899. } else {
  900. rc = -EIO;
  901. }
  902. }
  903. spin_unlock(&GlobalMid_Lock);
  904. DeleteMidQEntry(midQ);
  905. return rc;
  906. }
  907. if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
  908. cERROR(1, ("Frame too large received. Length: %d Xid: %d",
  909. receive_len, xid));
  910. rc = -EIO;
  911. } else { /* rcvd frame is ok */
  912. if (midQ->resp_buf && out_buf
  913. && (midQ->midState == MID_RESPONSE_RECEIVED)) {
  914. out_buf->smb_buf_length = receive_len;
  915. memcpy((char *)out_buf + 4,
  916. (char *)midQ->resp_buf + 4,
  917. receive_len);
  918. dump_smb(out_buf, 92);
  919. /* convert the length into a more usable form */
  920. if ((receive_len > 24) &&
  921. (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
  922. SECMODE_SIGN_ENABLED))) {
  923. rc = cifs_verify_signature(out_buf,
  924. &ses->server->mac_signing_key,
  925. midQ->sequence_number+1);
  926. if (rc) {
  927. cERROR(1, ("Unexpected SMB signature"));
  928. /* BB FIXME add code to kill session */
  929. }
  930. }
  931. *pbytes_returned = out_buf->smb_buf_length;
  932. /* BB special case reconnect tid and uid here? */
  933. rc = map_smb_to_linux_error(out_buf, 0 /* no log */ );
  934. /* convert ByteCount if necessary */
  935. if (receive_len >= sizeof(struct smb_hdr) - 4
  936. /* do not count RFC1001 header */ +
  937. (2 * out_buf->WordCount) + 2 /* bcc */ )
  938. BCC(out_buf) = le16_to_cpu(BCC_LE(out_buf));
  939. } else {
  940. rc = -EIO;
  941. cERROR(1, ("Bad MID state?"));
  942. }
  943. }
  944. DeleteMidQEntry(midQ);
  945. if (rstart && rc == -EACCES)
  946. return -ERESTARTSYS;
  947. return rc;
  948. }