misc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. /*
  2. * fs/cifs/misc.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2002,2005
  5. * Author(s): Steve French (sfrench@us.ibm.com)
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published
  9. * by the Free Software Foundation; either version 2.1 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  15. * the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/slab.h>
  22. #include <linux/ctype.h>
  23. #include <linux/mempool.h>
  24. #include "cifspdu.h"
  25. #include "cifsglob.h"
  26. #include "cifsproto.h"
  27. #include "cifs_debug.h"
  28. #include "smberr.h"
  29. #include "nterr.h"
  30. #include "cifs_unicode.h"
  31. extern mempool_t *cifs_sm_req_poolp;
  32. extern mempool_t *cifs_req_poolp;
  33. extern struct task_struct * oplockThread;
  34. /* The xid serves as a useful identifier for each incoming vfs request,
  35. in a similar way to the mid which is useful to track each sent smb,
  36. and CurrentXid can also provide a running counter (although it
  37. will eventually wrap past zero) of the total vfs operations handled
  38. since the cifs fs was mounted */
  39. unsigned int
  40. _GetXid(void)
  41. {
  42. unsigned int xid;
  43. spin_lock(&GlobalMid_Lock);
  44. GlobalTotalActiveXid++;
  45. if (GlobalTotalActiveXid > GlobalMaxActiveXid)
  46. GlobalMaxActiveXid = GlobalTotalActiveXid; /* keep high water mark for number of simultaneous vfs ops in our filesystem */
  47. if(GlobalTotalActiveXid > 65000)
  48. cFYI(1,("warning: more than 65000 requests active"));
  49. xid = GlobalCurrentXid++;
  50. spin_unlock(&GlobalMid_Lock);
  51. return xid;
  52. }
  53. void
  54. _FreeXid(unsigned int xid)
  55. {
  56. spin_lock(&GlobalMid_Lock);
  57. /* if(GlobalTotalActiveXid == 0)
  58. BUG(); */
  59. GlobalTotalActiveXid--;
  60. spin_unlock(&GlobalMid_Lock);
  61. }
  62. struct cifsSesInfo *
  63. sesInfoAlloc(void)
  64. {
  65. struct cifsSesInfo *ret_buf;
  66. ret_buf = kzalloc(sizeof (struct cifsSesInfo), GFP_KERNEL);
  67. if (ret_buf) {
  68. write_lock(&GlobalSMBSeslock);
  69. atomic_inc(&sesInfoAllocCount);
  70. ret_buf->status = CifsNew;
  71. list_add(&ret_buf->cifsSessionList, &GlobalSMBSessionList);
  72. init_MUTEX(&ret_buf->sesSem);
  73. write_unlock(&GlobalSMBSeslock);
  74. }
  75. return ret_buf;
  76. }
  77. void
  78. sesInfoFree(struct cifsSesInfo *buf_to_free)
  79. {
  80. if (buf_to_free == NULL) {
  81. cFYI(1, ("Null buffer passed to sesInfoFree"));
  82. return;
  83. }
  84. write_lock(&GlobalSMBSeslock);
  85. atomic_dec(&sesInfoAllocCount);
  86. list_del(&buf_to_free->cifsSessionList);
  87. write_unlock(&GlobalSMBSeslock);
  88. kfree(buf_to_free->serverOS);
  89. kfree(buf_to_free->serverDomain);
  90. kfree(buf_to_free->serverNOS);
  91. kfree(buf_to_free->password);
  92. kfree(buf_to_free->domainName);
  93. kfree(buf_to_free);
  94. }
  95. struct cifsTconInfo *
  96. tconInfoAlloc(void)
  97. {
  98. struct cifsTconInfo *ret_buf;
  99. ret_buf = kzalloc(sizeof (struct cifsTconInfo), GFP_KERNEL);
  100. if (ret_buf) {
  101. write_lock(&GlobalSMBSeslock);
  102. atomic_inc(&tconInfoAllocCount);
  103. list_add(&ret_buf->cifsConnectionList,
  104. &GlobalTreeConnectionList);
  105. ret_buf->tidStatus = CifsNew;
  106. INIT_LIST_HEAD(&ret_buf->openFileList);
  107. init_MUTEX(&ret_buf->tconSem);
  108. #ifdef CONFIG_CIFS_STATS
  109. spin_lock_init(&ret_buf->stat_lock);
  110. #endif
  111. write_unlock(&GlobalSMBSeslock);
  112. }
  113. return ret_buf;
  114. }
  115. void
  116. tconInfoFree(struct cifsTconInfo *buf_to_free)
  117. {
  118. if (buf_to_free == NULL) {
  119. cFYI(1, ("Null buffer passed to tconInfoFree"));
  120. return;
  121. }
  122. write_lock(&GlobalSMBSeslock);
  123. atomic_dec(&tconInfoAllocCount);
  124. list_del(&buf_to_free->cifsConnectionList);
  125. write_unlock(&GlobalSMBSeslock);
  126. kfree(buf_to_free->nativeFileSystem);
  127. kfree(buf_to_free);
  128. }
  129. struct smb_hdr *
  130. cifs_buf_get(void)
  131. {
  132. struct smb_hdr *ret_buf = NULL;
  133. /* We could use negotiated size instead of max_msgsize -
  134. but it may be more efficient to always alloc same size
  135. albeit slightly larger than necessary and maxbuffersize
  136. defaults to this and can not be bigger */
  137. ret_buf =
  138. (struct smb_hdr *) mempool_alloc(cifs_req_poolp, GFP_KERNEL | GFP_NOFS);
  139. /* clear the first few header bytes */
  140. /* for most paths, more is cleared in header_assemble */
  141. if (ret_buf) {
  142. memset(ret_buf, 0, sizeof(struct smb_hdr) + 3);
  143. atomic_inc(&bufAllocCount);
  144. #ifdef CONFIG_CIFS_STATS2
  145. atomic_inc(&totBufAllocCount);
  146. #endif /* CONFIG_CIFS_STATS2 */
  147. }
  148. return ret_buf;
  149. }
  150. void
  151. cifs_buf_release(void *buf_to_free)
  152. {
  153. if (buf_to_free == NULL) {
  154. /* cFYI(1, ("Null buffer passed to cifs_buf_release"));*/
  155. return;
  156. }
  157. mempool_free(buf_to_free,cifs_req_poolp);
  158. atomic_dec(&bufAllocCount);
  159. return;
  160. }
  161. struct smb_hdr *
  162. cifs_small_buf_get(void)
  163. {
  164. struct smb_hdr *ret_buf = NULL;
  165. /* We could use negotiated size instead of max_msgsize -
  166. but it may be more efficient to always alloc same size
  167. albeit slightly larger than necessary and maxbuffersize
  168. defaults to this and can not be bigger */
  169. ret_buf =
  170. (struct smb_hdr *) mempool_alloc(cifs_sm_req_poolp, GFP_KERNEL | GFP_NOFS);
  171. if (ret_buf) {
  172. /* No need to clear memory here, cleared in header assemble */
  173. /* memset(ret_buf, 0, sizeof(struct smb_hdr) + 27);*/
  174. atomic_inc(&smBufAllocCount);
  175. #ifdef CONFIG_CIFS_STATS2
  176. atomic_inc(&totSmBufAllocCount);
  177. #endif /* CONFIG_CIFS_STATS2 */
  178. }
  179. return ret_buf;
  180. }
  181. void
  182. cifs_small_buf_release(void *buf_to_free)
  183. {
  184. if (buf_to_free == NULL) {
  185. cFYI(1, ("Null buffer passed to cifs_small_buf_release"));
  186. return;
  187. }
  188. mempool_free(buf_to_free,cifs_sm_req_poolp);
  189. atomic_dec(&smBufAllocCount);
  190. return;
  191. }
  192. /*
  193. Find a free multiplex id (SMB mid). Otherwise there could be
  194. mid collisions which might cause problems, demultiplexing the
  195. wrong response to this request. Multiplex ids could collide if
  196. one of a series requests takes much longer than the others, or
  197. if a very large number of long lived requests (byte range
  198. locks or FindNotify requests) are pending. No more than
  199. 64K-1 requests can be outstanding at one time. If no
  200. mids are available, return zero. A future optimization
  201. could make the combination of mids and uid the key we use
  202. to demultiplex on (rather than mid alone).
  203. In addition to the above check, the cifs demultiplex
  204. code already used the command code as a secondary
  205. check of the frame and if signing is negotiated the
  206. response would be discarded if the mid were the same
  207. but the signature was wrong. Since the mid is not put in the
  208. pending queue until later (when it is about to be dispatched)
  209. we do have to limit the number of outstanding requests
  210. to somewhat less than 64K-1 although it is hard to imagine
  211. so many threads being in the vfs at one time.
  212. */
  213. __u16 GetNextMid(struct TCP_Server_Info *server)
  214. {
  215. __u16 mid = 0;
  216. __u16 last_mid;
  217. int collision;
  218. if(server == NULL)
  219. return mid;
  220. spin_lock(&GlobalMid_Lock);
  221. last_mid = server->CurrentMid; /* we do not want to loop forever */
  222. server->CurrentMid++;
  223. /* This nested loop looks more expensive than it is.
  224. In practice the list of pending requests is short,
  225. fewer than 50, and the mids are likely to be unique
  226. on the first pass through the loop unless some request
  227. takes longer than the 64 thousand requests before it
  228. (and it would also have to have been a request that
  229. did not time out) */
  230. while(server->CurrentMid != last_mid) {
  231. struct list_head *tmp;
  232. struct mid_q_entry *mid_entry;
  233. collision = 0;
  234. if(server->CurrentMid == 0)
  235. server->CurrentMid++;
  236. list_for_each(tmp, &server->pending_mid_q) {
  237. mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
  238. if ((mid_entry->mid == server->CurrentMid) &&
  239. (mid_entry->midState == MID_REQUEST_SUBMITTED)) {
  240. /* This mid is in use, try a different one */
  241. collision = 1;
  242. break;
  243. }
  244. }
  245. if(collision == 0) {
  246. mid = server->CurrentMid;
  247. break;
  248. }
  249. server->CurrentMid++;
  250. }
  251. spin_unlock(&GlobalMid_Lock);
  252. return mid;
  253. }
  254. /* NB: MID can not be set if treeCon not passed in, in that
  255. case it is responsbility of caller to set the mid */
  256. void
  257. header_assemble(struct smb_hdr *buffer, char smb_command /* command */ ,
  258. const struct cifsTconInfo *treeCon, int word_count
  259. /* length of fixed section (word count) in two byte units */)
  260. {
  261. struct list_head* temp_item;
  262. struct cifsSesInfo * ses;
  263. char *temp = (char *) buffer;
  264. memset(temp,0,256); /* bigger than MAX_CIFS_HDR_SIZE */
  265. buffer->smb_buf_length =
  266. (2 * word_count) + sizeof (struct smb_hdr) -
  267. 4 /* RFC 1001 length field does not count */ +
  268. 2 /* for bcc field itself */ ;
  269. /* Note that this is the only network field that has to be converted
  270. to big endian and it is done just before we send it */
  271. buffer->Protocol[0] = 0xFF;
  272. buffer->Protocol[1] = 'S';
  273. buffer->Protocol[2] = 'M';
  274. buffer->Protocol[3] = 'B';
  275. buffer->Command = smb_command;
  276. buffer->Flags = 0x00; /* case sensitive */
  277. buffer->Flags2 = SMBFLG2_KNOWS_LONG_NAMES;
  278. buffer->Pid = cpu_to_le16((__u16)current->tgid);
  279. buffer->PidHigh = cpu_to_le16((__u16)(current->tgid >> 16));
  280. spin_lock(&GlobalMid_Lock);
  281. spin_unlock(&GlobalMid_Lock);
  282. if (treeCon) {
  283. buffer->Tid = treeCon->tid;
  284. if (treeCon->ses) {
  285. if (treeCon->ses->capabilities & CAP_UNICODE)
  286. buffer->Flags2 |= SMBFLG2_UNICODE;
  287. if (treeCon->ses->capabilities & CAP_STATUS32) {
  288. buffer->Flags2 |= SMBFLG2_ERR_STATUS;
  289. }
  290. /* Uid is not converted */
  291. buffer->Uid = treeCon->ses->Suid;
  292. buffer->Mid = GetNextMid(treeCon->ses->server);
  293. if(multiuser_mount != 0) {
  294. /* For the multiuser case, there are few obvious technically */
  295. /* possible mechanisms to match the local linux user (uid) */
  296. /* to a valid remote smb user (smb_uid): */
  297. /* 1) Query Winbind (or other local pam/nss daemon */
  298. /* for userid/password/logon_domain or credential */
  299. /* 2) Query Winbind for uid to sid to username mapping */
  300. /* and see if we have a matching password for existing*/
  301. /* session for that user perhas getting password by */
  302. /* adding a new pam_cifs module that stores passwords */
  303. /* so that the cifs vfs can get at that for all logged*/
  304. /* on users */
  305. /* 3) (Which is the mechanism we have chosen) */
  306. /* Search through sessions to the same server for a */
  307. /* a match on the uid that was passed in on mount */
  308. /* with the current processes uid (or euid?) and use */
  309. /* that smb uid. If no existing smb session for */
  310. /* that uid found, use the default smb session ie */
  311. /* the smb session for the volume mounted which is */
  312. /* the same as would be used if the multiuser mount */
  313. /* flag were disabled. */
  314. /* BB Add support for establishing new tCon and SMB Session */
  315. /* with userid/password pairs found on the smb session */
  316. /* for other target tcp/ip addresses BB */
  317. if(current->fsuid != treeCon->ses->linux_uid) {
  318. cFYI(1,("Multiuser mode and UID did not match tcon uid"));
  319. read_lock(&GlobalSMBSeslock);
  320. list_for_each(temp_item, &GlobalSMBSessionList) {
  321. ses = list_entry(temp_item, struct cifsSesInfo, cifsSessionList);
  322. if(ses->linux_uid == current->fsuid) {
  323. if(ses->server == treeCon->ses->server) {
  324. cFYI(1,("found matching uid substitute right smb_uid"));
  325. buffer->Uid = ses->Suid;
  326. break;
  327. } else {
  328. /* BB eventually call cifs_setup_session here */
  329. cFYI(1,("local UID found but smb sess with this server does not exist"));
  330. }
  331. }
  332. }
  333. read_unlock(&GlobalSMBSeslock);
  334. }
  335. }
  336. }
  337. if (treeCon->Flags & SMB_SHARE_IS_IN_DFS)
  338. buffer->Flags2 |= SMBFLG2_DFS;
  339. if (treeCon->nocase)
  340. buffer->Flags |= SMBFLG_CASELESS;
  341. if((treeCon->ses) && (treeCon->ses->server))
  342. if(treeCon->ses->server->secMode &
  343. (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
  344. buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
  345. }
  346. /* endian conversion of flags is now done just before sending */
  347. buffer->WordCount = (char) word_count;
  348. return;
  349. }
  350. static int
  351. checkSMBhdr(struct smb_hdr *smb, __u16 mid)
  352. {
  353. /* Make sure that this really is an SMB, that it is a response,
  354. and that the message ids match */
  355. if ((*(__le32 *) smb->Protocol == cpu_to_le32(0x424d53ff)) &&
  356. (mid == smb->Mid)) {
  357. if(smb->Flags & SMBFLG_RESPONSE)
  358. return 0;
  359. else {
  360. /* only one valid case where server sends us request */
  361. if(smb->Command == SMB_COM_LOCKING_ANDX)
  362. return 0;
  363. else
  364. cERROR(1, ("Rcvd Request not response"));
  365. }
  366. } else { /* bad signature or mid */
  367. if (*(__le32 *) smb->Protocol != cpu_to_le32(0x424d53ff))
  368. cERROR(1,
  369. ("Bad protocol string signature header %x",
  370. *(unsigned int *) smb->Protocol));
  371. if (mid != smb->Mid)
  372. cERROR(1, ("Mids do not match"));
  373. }
  374. cERROR(1, ("bad smb detected. The Mid=%d", smb->Mid));
  375. return 1;
  376. }
  377. int
  378. checkSMB(struct smb_hdr *smb, __u16 mid, unsigned int length)
  379. {
  380. __u32 len = smb->smb_buf_length;
  381. __u32 clc_len; /* calculated length */
  382. cFYI(0, ("checkSMB Length: 0x%x, smb_buf_length: 0x%x", length, len));
  383. if (length < 2 + sizeof (struct smb_hdr)) {
  384. if ((length >= sizeof (struct smb_hdr) - 1)
  385. && (smb->Status.CifsError != 0)) {
  386. smb->WordCount = 0;
  387. /* some error cases do not return wct and bcc */
  388. return 0;
  389. } else if ((length == sizeof(struct smb_hdr) + 1) &&
  390. (smb->WordCount == 0)) {
  391. char * tmp = (char *)smb;
  392. /* Need to work around a bug in two servers here */
  393. /* First, check if the part of bcc they sent was zero */
  394. if (tmp[sizeof(struct smb_hdr)] == 0) {
  395. /* some servers return only half of bcc
  396. * on simple responses (wct, bcc both zero)
  397. * in particular have seen this on
  398. * ulogoffX and FindClose. This leaves
  399. * one byte of bcc potentially unitialized
  400. */
  401. /* zero rest of bcc */
  402. tmp[sizeof(struct smb_hdr)+1] = 0;
  403. return 0;
  404. }
  405. cERROR(1,("rcvd invalid byte count (bcc)"));
  406. } else {
  407. cERROR(1, ("Length less than smb header size"));
  408. }
  409. return 1;
  410. }
  411. if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
  412. cERROR(1, ("smb length greater than MaxBufSize, mid=%d",
  413. smb->Mid));
  414. return 1;
  415. }
  416. if (checkSMBhdr(smb, mid))
  417. return 1;
  418. clc_len = smbCalcSize_LE(smb);
  419. if(4 + len != length) {
  420. cERROR(1, ("Length read does not match RFC1001 length %d",len));
  421. return 1;
  422. }
  423. if (4 + len != clc_len) {
  424. /* check if bcc wrapped around for large read responses */
  425. if((len > 64 * 1024) && (len > clc_len)) {
  426. /* check if lengths match mod 64K */
  427. if(((4 + len) & 0xFFFF) == (clc_len & 0xFFFF))
  428. return 0; /* bcc wrapped */
  429. }
  430. cFYI(1, ("Calculated size %d vs length %d mismatch for mid %d",
  431. clc_len, 4 + len, smb->Mid));
  432. /* Windows XP can return a few bytes too much, presumably
  433. an illegal pad, at the end of byte range lock responses
  434. so we allow for that three byte pad, as long as actual
  435. received length is as long or longer than calculated length */
  436. /* We have now had to extend this more, since there is a
  437. case in which it needs to be bigger still to handle a
  438. malformed response to transact2 findfirst from WinXP when
  439. access denied is returned and thus bcc and wct are zero
  440. but server says length is 0x21 bytes too long as if the server
  441. forget to reset the smb rfc1001 length when it reset the
  442. wct and bcc to minimum size and drop the t2 parms and data */
  443. if((4+len > clc_len) && (len <= clc_len + 512))
  444. return 0;
  445. else {
  446. cERROR(1, ("RFC1001 size %d bigger than SMB for Mid=%d",
  447. len, smb->Mid));
  448. return 1;
  449. }
  450. }
  451. return 0;
  452. }
  453. int
  454. is_valid_oplock_break(struct smb_hdr *buf, struct TCP_Server_Info *srv)
  455. {
  456. struct smb_com_lock_req * pSMB = (struct smb_com_lock_req *)buf;
  457. struct list_head *tmp;
  458. struct list_head *tmp1;
  459. struct cifsTconInfo *tcon;
  460. struct cifsFileInfo *netfile;
  461. cFYI(1,("Checking for oplock break or dnotify response"));
  462. if((pSMB->hdr.Command == SMB_COM_NT_TRANSACT) &&
  463. (pSMB->hdr.Flags & SMBFLG_RESPONSE)) {
  464. struct smb_com_transaction_change_notify_rsp * pSMBr =
  465. (struct smb_com_transaction_change_notify_rsp *)buf;
  466. struct file_notify_information * pnotify;
  467. __u32 data_offset = 0;
  468. if(pSMBr->ByteCount > sizeof(struct file_notify_information)) {
  469. data_offset = le32_to_cpu(pSMBr->DataOffset);
  470. pnotify = (struct file_notify_information *)
  471. ((char *)&pSMBr->hdr.Protocol + data_offset);
  472. cFYI(1,("dnotify on %s Action: 0x%x",pnotify->FileName,
  473. pnotify->Action)); /* BB removeme BB */
  474. /* cifs_dump_mem("Rcvd notify Data: ",buf,
  475. sizeof(struct smb_hdr)+60); */
  476. return TRUE;
  477. }
  478. if(pSMBr->hdr.Status.CifsError) {
  479. cFYI(1,("notify err 0x%d",pSMBr->hdr.Status.CifsError));
  480. return TRUE;
  481. }
  482. return FALSE;
  483. }
  484. if(pSMB->hdr.Command != SMB_COM_LOCKING_ANDX)
  485. return FALSE;
  486. if(pSMB->hdr.Flags & SMBFLG_RESPONSE) {
  487. /* no sense logging error on invalid handle on oplock
  488. break - harmless race between close request and oplock
  489. break response is expected from time to time writing out
  490. large dirty files cached on the client */
  491. if ((NT_STATUS_INVALID_HANDLE) ==
  492. le32_to_cpu(pSMB->hdr.Status.CifsError)) {
  493. cFYI(1,("invalid handle on oplock break"));
  494. return TRUE;
  495. } else if (ERRbadfid ==
  496. le16_to_cpu(pSMB->hdr.Status.DosError.Error)) {
  497. return TRUE;
  498. } else {
  499. return FALSE; /* on valid oplock brk we get "request" */
  500. }
  501. }
  502. if(pSMB->hdr.WordCount != 8)
  503. return FALSE;
  504. cFYI(1,(" oplock type 0x%d level 0x%d",pSMB->LockType,pSMB->OplockLevel));
  505. if(!(pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE))
  506. return FALSE;
  507. /* look up tcon based on tid & uid */
  508. read_lock(&GlobalSMBSeslock);
  509. list_for_each(tmp, &GlobalTreeConnectionList) {
  510. tcon = list_entry(tmp, struct cifsTconInfo, cifsConnectionList);
  511. if ((tcon->tid == buf->Tid) && (srv == tcon->ses->server)) {
  512. cifs_stats_inc(&tcon->num_oplock_brks);
  513. list_for_each(tmp1,&tcon->openFileList){
  514. netfile = list_entry(tmp1,struct cifsFileInfo,
  515. tlist);
  516. if(pSMB->Fid == netfile->netfid) {
  517. struct cifsInodeInfo *pCifsInode;
  518. read_unlock(&GlobalSMBSeslock);
  519. cFYI(1,("file id match, oplock break"));
  520. pCifsInode =
  521. CIFS_I(netfile->pInode);
  522. pCifsInode->clientCanCacheAll = FALSE;
  523. if(pSMB->OplockLevel == 0)
  524. pCifsInode->clientCanCacheRead
  525. = FALSE;
  526. pCifsInode->oplockPending = TRUE;
  527. AllocOplockQEntry(netfile->pInode,
  528. netfile->netfid,
  529. tcon);
  530. cFYI(1,("about to wake up oplock thd"));
  531. if(oplockThread)
  532. wake_up_process(oplockThread);
  533. return TRUE;
  534. }
  535. }
  536. read_unlock(&GlobalSMBSeslock);
  537. cFYI(1,("No matching file for oplock break"));
  538. return TRUE;
  539. }
  540. }
  541. read_unlock(&GlobalSMBSeslock);
  542. cFYI(1,("Can not process oplock break for non-existent connection"));
  543. return TRUE;
  544. }
  545. void
  546. dump_smb(struct smb_hdr *smb_buf, int smb_buf_length)
  547. {
  548. int i, j;
  549. char debug_line[17];
  550. unsigned char *buffer;
  551. if (traceSMB == 0)
  552. return;
  553. buffer = (unsigned char *) smb_buf;
  554. for (i = 0, j = 0; i < smb_buf_length; i++, j++) {
  555. if (i % 8 == 0) { /* have reached the beginning of line */
  556. printk(KERN_DEBUG "| ");
  557. j = 0;
  558. }
  559. printk("%0#4x ", buffer[i]);
  560. debug_line[2 * j] = ' ';
  561. if (isprint(buffer[i]))
  562. debug_line[1 + (2 * j)] = buffer[i];
  563. else
  564. debug_line[1 + (2 * j)] = '_';
  565. if (i % 8 == 7) { /* reached end of line, time to print ascii */
  566. debug_line[16] = 0;
  567. printk(" | %s\n", debug_line);
  568. }
  569. }
  570. for (; j < 8; j++) {
  571. printk(" ");
  572. debug_line[2 * j] = ' ';
  573. debug_line[1 + (2 * j)] = ' ';
  574. }
  575. printk( " | %s\n", debug_line);
  576. return;
  577. }
  578. /* Windows maps these to the user defined 16 bit Unicode range since they are
  579. reserved symbols (along with \ and /), otherwise illegal to store
  580. in filenames in NTFS */
  581. #define UNI_ASTERIK (__u16) ('*' + 0xF000)
  582. #define UNI_QUESTION (__u16) ('?' + 0xF000)
  583. #define UNI_COLON (__u16) (':' + 0xF000)
  584. #define UNI_GRTRTHAN (__u16) ('>' + 0xF000)
  585. #define UNI_LESSTHAN (__u16) ('<' + 0xF000)
  586. #define UNI_PIPE (__u16) ('|' + 0xF000)
  587. #define UNI_SLASH (__u16) ('\\' + 0xF000)
  588. /* Convert 16 bit Unicode pathname from wire format to string in current code
  589. page. Conversion may involve remapping up the seven characters that are
  590. only legal in POSIX-like OS (if they are present in the string). Path
  591. names are little endian 16 bit Unicode on the wire */
  592. int
  593. cifs_convertUCSpath(char *target, const __le16 * source, int maxlen,
  594. const struct nls_table * cp)
  595. {
  596. int i,j,len;
  597. __u16 src_char;
  598. for(i = 0, j = 0; i < maxlen; i++) {
  599. src_char = le16_to_cpu(source[i]);
  600. switch (src_char) {
  601. case 0:
  602. goto cUCS_out; /* BB check this BB */
  603. case UNI_COLON:
  604. target[j] = ':';
  605. break;
  606. case UNI_ASTERIK:
  607. target[j] = '*';
  608. break;
  609. case UNI_QUESTION:
  610. target[j] = '?';
  611. break;
  612. /* BB We can not handle remapping slash until
  613. all the calls to build_path_from_dentry
  614. are modified, as they use slash as separator BB */
  615. /* case UNI_SLASH:
  616. target[j] = '\\';
  617. break;*/
  618. case UNI_PIPE:
  619. target[j] = '|';
  620. break;
  621. case UNI_GRTRTHAN:
  622. target[j] = '>';
  623. break;
  624. case UNI_LESSTHAN:
  625. target[j] = '<';
  626. break;
  627. default:
  628. len = cp->uni2char(src_char, &target[j],
  629. NLS_MAX_CHARSET_SIZE);
  630. if(len > 0) {
  631. j += len;
  632. continue;
  633. } else {
  634. target[j] = '?';
  635. }
  636. }
  637. j++;
  638. /* make sure we do not overrun callers allocated temp buffer */
  639. if(j >= (2 * NAME_MAX))
  640. break;
  641. }
  642. cUCS_out:
  643. target[j] = 0;
  644. return j;
  645. }
  646. /* Convert 16 bit Unicode pathname to wire format from string in current code
  647. page. Conversion may involve remapping up the seven characters that are
  648. only legal in POSIX-like OS (if they are present in the string). Path
  649. names are little endian 16 bit Unicode on the wire */
  650. int
  651. cifsConvertToUCS(__le16 * target, const char *source, int maxlen,
  652. const struct nls_table * cp, int mapChars)
  653. {
  654. int i,j,charlen;
  655. int len_remaining = maxlen;
  656. char src_char;
  657. __u16 temp;
  658. if(!mapChars)
  659. return cifs_strtoUCS(target, source, PATH_MAX, cp);
  660. for(i = 0, j = 0; i < maxlen; j++) {
  661. src_char = source[i];
  662. switch (src_char) {
  663. case 0:
  664. target[j] = 0;
  665. goto ctoUCS_out;
  666. case ':':
  667. target[j] = cpu_to_le16(UNI_COLON);
  668. break;
  669. case '*':
  670. target[j] = cpu_to_le16(UNI_ASTERIK);
  671. break;
  672. case '?':
  673. target[j] = cpu_to_le16(UNI_QUESTION);
  674. break;
  675. case '<':
  676. target[j] = cpu_to_le16(UNI_LESSTHAN);
  677. break;
  678. case '>':
  679. target[j] = cpu_to_le16(UNI_GRTRTHAN);
  680. break;
  681. case '|':
  682. target[j] = cpu_to_le16(UNI_PIPE);
  683. break;
  684. /* BB We can not handle remapping slash until
  685. all the calls to build_path_from_dentry
  686. are modified, as they use slash as separator BB */
  687. /* case '\\':
  688. target[j] = cpu_to_le16(UNI_SLASH);
  689. break;*/
  690. default:
  691. charlen = cp->char2uni(source+i,
  692. len_remaining, &temp);
  693. /* if no match, use question mark, which
  694. at least in some cases servers as wild card */
  695. if(charlen < 1) {
  696. target[j] = cpu_to_le16(0x003f);
  697. charlen = 1;
  698. } else
  699. target[j] = cpu_to_le16(temp);
  700. len_remaining -= charlen;
  701. /* character may take more than one byte in the
  702. the source string, but will take exactly two
  703. bytes in the target string */
  704. i+= charlen;
  705. continue;
  706. }
  707. i++; /* move to next char in source string */
  708. len_remaining--;
  709. }
  710. ctoUCS_out:
  711. return i;
  712. }