misc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. /*
  2. * fs/cifs/misc.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2002,2004
  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. static __u16 GlobalMid; /* multiplex id - rotating counter */
  35. /* The xid serves as a useful identifier for each incoming vfs request,
  36. in a similar way to the mid which is useful to track each sent smb,
  37. and CurrentXid can also provide a running counter (although it
  38. will eventually wrap past zero) of the total vfs operations handled
  39. since the cifs fs was mounted */
  40. unsigned int
  41. _GetXid(void)
  42. {
  43. unsigned int xid;
  44. spin_lock(&GlobalMid_Lock);
  45. GlobalTotalActiveXid++;
  46. if (GlobalTotalActiveXid > GlobalMaxActiveXid)
  47. GlobalMaxActiveXid = GlobalTotalActiveXid; /* keep high water mark for number of simultaneous vfs ops in our filesystem */
  48. xid = GlobalCurrentXid++;
  49. spin_unlock(&GlobalMid_Lock);
  50. return xid;
  51. }
  52. void
  53. _FreeXid(unsigned int xid)
  54. {
  55. spin_lock(&GlobalMid_Lock);
  56. /* if(GlobalTotalActiveXid == 0)
  57. BUG(); */
  58. GlobalTotalActiveXid--;
  59. spin_unlock(&GlobalMid_Lock);
  60. }
  61. struct cifsSesInfo *
  62. sesInfoAlloc(void)
  63. {
  64. struct cifsSesInfo *ret_buf;
  65. ret_buf =
  66. (struct cifsSesInfo *) kmalloc(sizeof (struct cifsSesInfo),
  67. GFP_KERNEL);
  68. if (ret_buf) {
  69. memset(ret_buf, 0, sizeof (struct cifsSesInfo));
  70. write_lock(&GlobalSMBSeslock);
  71. atomic_inc(&sesInfoAllocCount);
  72. ret_buf->status = CifsNew;
  73. list_add(&ret_buf->cifsSessionList, &GlobalSMBSessionList);
  74. init_MUTEX(&ret_buf->sesSem);
  75. write_unlock(&GlobalSMBSeslock);
  76. }
  77. return ret_buf;
  78. }
  79. void
  80. sesInfoFree(struct cifsSesInfo *buf_to_free)
  81. {
  82. if (buf_to_free == NULL) {
  83. cFYI(1, ("Null buffer passed to sesInfoFree"));
  84. return;
  85. }
  86. write_lock(&GlobalSMBSeslock);
  87. atomic_dec(&sesInfoAllocCount);
  88. list_del(&buf_to_free->cifsSessionList);
  89. write_unlock(&GlobalSMBSeslock);
  90. if (buf_to_free->serverOS)
  91. kfree(buf_to_free->serverOS);
  92. if (buf_to_free->serverDomain)
  93. kfree(buf_to_free->serverDomain);
  94. if (buf_to_free->serverNOS)
  95. kfree(buf_to_free->serverNOS);
  96. if (buf_to_free->password)
  97. kfree(buf_to_free->password);
  98. kfree(buf_to_free);
  99. }
  100. struct cifsTconInfo *
  101. tconInfoAlloc(void)
  102. {
  103. struct cifsTconInfo *ret_buf;
  104. ret_buf =
  105. (struct cifsTconInfo *) kmalloc(sizeof (struct cifsTconInfo),
  106. GFP_KERNEL);
  107. if (ret_buf) {
  108. memset(ret_buf, 0, sizeof (struct cifsTconInfo));
  109. write_lock(&GlobalSMBSeslock);
  110. atomic_inc(&tconInfoAllocCount);
  111. list_add(&ret_buf->cifsConnectionList,
  112. &GlobalTreeConnectionList);
  113. ret_buf->tidStatus = CifsNew;
  114. INIT_LIST_HEAD(&ret_buf->openFileList);
  115. init_MUTEX(&ret_buf->tconSem);
  116. #ifdef CONFIG_CIFS_STATS
  117. spin_lock_init(&ret_buf->stat_lock);
  118. #endif
  119. write_unlock(&GlobalSMBSeslock);
  120. }
  121. return ret_buf;
  122. }
  123. void
  124. tconInfoFree(struct cifsTconInfo *buf_to_free)
  125. {
  126. if (buf_to_free == NULL) {
  127. cFYI(1, ("Null buffer passed to tconInfoFree"));
  128. return;
  129. }
  130. write_lock(&GlobalSMBSeslock);
  131. atomic_dec(&tconInfoAllocCount);
  132. list_del(&buf_to_free->cifsConnectionList);
  133. write_unlock(&GlobalSMBSeslock);
  134. if (buf_to_free->nativeFileSystem)
  135. kfree(buf_to_free->nativeFileSystem);
  136. kfree(buf_to_free);
  137. }
  138. struct smb_hdr *
  139. cifs_buf_get(void)
  140. {
  141. struct smb_hdr *ret_buf = NULL;
  142. /* We could use negotiated size instead of max_msgsize -
  143. but it may be more efficient to always alloc same size
  144. albeit slightly larger than necessary and maxbuffersize
  145. defaults to this and can not be bigger */
  146. ret_buf =
  147. (struct smb_hdr *) mempool_alloc(cifs_req_poolp, SLAB_KERNEL | SLAB_NOFS);
  148. /* clear the first few header bytes */
  149. /* for most paths, more is cleared in header_assemble */
  150. if (ret_buf) {
  151. memset(ret_buf, 0, sizeof(struct smb_hdr) + 3);
  152. atomic_inc(&bufAllocCount);
  153. }
  154. return ret_buf;
  155. }
  156. void
  157. cifs_buf_release(void *buf_to_free)
  158. {
  159. if (buf_to_free == NULL) {
  160. /* cFYI(1, ("Null buffer passed to cifs_buf_release"));*/
  161. return;
  162. }
  163. mempool_free(buf_to_free,cifs_req_poolp);
  164. atomic_dec(&bufAllocCount);
  165. return;
  166. }
  167. struct smb_hdr *
  168. cifs_small_buf_get(void)
  169. {
  170. struct smb_hdr *ret_buf = NULL;
  171. /* We could use negotiated size instead of max_msgsize -
  172. but it may be more efficient to always alloc same size
  173. albeit slightly larger than necessary and maxbuffersize
  174. defaults to this and can not be bigger */
  175. ret_buf =
  176. (struct smb_hdr *) mempool_alloc(cifs_sm_req_poolp, SLAB_KERNEL | SLAB_NOFS);
  177. if (ret_buf) {
  178. /* No need to clear memory here, cleared in header assemble */
  179. /* memset(ret_buf, 0, sizeof(struct smb_hdr) + 27);*/
  180. atomic_inc(&smBufAllocCount);
  181. }
  182. return ret_buf;
  183. }
  184. void
  185. cifs_small_buf_release(void *buf_to_free)
  186. {
  187. if (buf_to_free == NULL) {
  188. cFYI(1, ("Null buffer passed to cifs_small_buf_release"));
  189. return;
  190. }
  191. mempool_free(buf_to_free,cifs_sm_req_poolp);
  192. atomic_dec(&smBufAllocCount);
  193. return;
  194. }
  195. void
  196. header_assemble(struct smb_hdr *buffer, char smb_command /* command */ ,
  197. const struct cifsTconInfo *treeCon, int word_count
  198. /* length of fixed section (word count) in two byte units */)
  199. {
  200. struct list_head* temp_item;
  201. struct cifsSesInfo * ses;
  202. char *temp = (char *) buffer;
  203. memset(temp,0,MAX_CIFS_HDR_SIZE);
  204. buffer->smb_buf_length =
  205. (2 * word_count) + sizeof (struct smb_hdr) -
  206. 4 /* RFC 1001 length field does not count */ +
  207. 2 /* for bcc field itself */ ;
  208. /* Note that this is the only network field that has to be converted to big endian and it is done just before we send it */
  209. buffer->Protocol[0] = 0xFF;
  210. buffer->Protocol[1] = 'S';
  211. buffer->Protocol[2] = 'M';
  212. buffer->Protocol[3] = 'B';
  213. buffer->Command = smb_command;
  214. buffer->Flags = 0x00; /* case sensitive */
  215. buffer->Flags2 = SMBFLG2_KNOWS_LONG_NAMES;
  216. buffer->Pid = cpu_to_le16((__u16)current->tgid);
  217. buffer->PidHigh = cpu_to_le16((__u16)(current->tgid >> 16));
  218. spin_lock(&GlobalMid_Lock);
  219. GlobalMid++;
  220. buffer->Mid = GlobalMid;
  221. spin_unlock(&GlobalMid_Lock);
  222. if (treeCon) {
  223. buffer->Tid = treeCon->tid;
  224. if (treeCon->ses) {
  225. if (treeCon->ses->capabilities & CAP_UNICODE)
  226. buffer->Flags2 |= SMBFLG2_UNICODE;
  227. if (treeCon->ses->capabilities & CAP_STATUS32) {
  228. buffer->Flags2 |= SMBFLG2_ERR_STATUS;
  229. }
  230. buffer->Uid = treeCon->ses->Suid; /* always in LE format */
  231. if(multiuser_mount != 0) {
  232. /* For the multiuser case, there are few obvious technically */
  233. /* possible mechanisms to match the local linux user (uid) */
  234. /* to a valid remote smb user (smb_uid): */
  235. /* 1) Query Winbind (or other local pam/nss daemon */
  236. /* for userid/password/logon_domain or credential */
  237. /* 2) Query Winbind for uid to sid to username mapping */
  238. /* and see if we have a matching password for existing*/
  239. /* session for that user perhas getting password by */
  240. /* adding a new pam_cifs module that stores passwords */
  241. /* so that the cifs vfs can get at that for all logged*/
  242. /* on users */
  243. /* 3) (Which is the mechanism we have chosen) */
  244. /* Search through sessions to the same server for a */
  245. /* a match on the uid that was passed in on mount */
  246. /* with the current processes uid (or euid?) and use */
  247. /* that smb uid. If no existing smb session for */
  248. /* that uid found, use the default smb session ie */
  249. /* the smb session for the volume mounted which is */
  250. /* the same as would be used if the multiuser mount */
  251. /* flag were disabled. */
  252. /* BB Add support for establishing new tCon and SMB Session */
  253. /* with userid/password pairs found on the smb session */
  254. /* for other target tcp/ip addresses BB */
  255. if(current->uid != treeCon->ses->linux_uid) {
  256. cFYI(1,("Multiuser mode and UID did not match tcon uid "));
  257. read_lock(&GlobalSMBSeslock);
  258. list_for_each(temp_item, &GlobalSMBSessionList) {
  259. ses = list_entry(temp_item, struct cifsSesInfo, cifsSessionList);
  260. if(ses->linux_uid == current->uid) {
  261. if(ses->server == treeCon->ses->server) {
  262. cFYI(1,("found matching uid substitute right smb_uid"));
  263. buffer->Uid = ses->Suid;
  264. break;
  265. } else {
  266. /* BB eventually call cifs_setup_session here */
  267. cFYI(1,("local UID found but smb sess with this server does not exist"));
  268. }
  269. }
  270. }
  271. read_unlock(&GlobalSMBSeslock);
  272. }
  273. }
  274. }
  275. if (treeCon->Flags & SMB_SHARE_IS_IN_DFS)
  276. buffer->Flags2 |= SMBFLG2_DFS;
  277. if((treeCon->ses) && (treeCon->ses->server))
  278. if(treeCon->ses->server->secMode &
  279. (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
  280. buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
  281. }
  282. /* endian conversion of flags is now done just before sending */
  283. buffer->WordCount = (char) word_count;
  284. return;
  285. }
  286. int
  287. checkSMBhdr(struct smb_hdr *smb, __u16 mid)
  288. {
  289. /* Make sure that this really is an SMB, that it is a response,
  290. and that the message ids match */
  291. if ((*(__le32 *) smb->Protocol == cpu_to_le32(0x424d53ff)) &&
  292. (mid == smb->Mid)) {
  293. if(smb->Flags & SMBFLG_RESPONSE)
  294. return 0;
  295. else {
  296. /* only one valid case where server sends us request */
  297. if(smb->Command == SMB_COM_LOCKING_ANDX)
  298. return 0;
  299. else
  300. cERROR(1, ("Rcvd Request not response "));
  301. }
  302. } else { /* bad signature or mid */
  303. if (*(__le32 *) smb->Protocol != cpu_to_le32(0x424d53ff))
  304. cERROR(1,
  305. ("Bad protocol string signature header %x ",
  306. *(unsigned int *) smb->Protocol));
  307. if (mid != smb->Mid)
  308. cERROR(1, ("Mids do not match"));
  309. }
  310. cERROR(1, ("bad smb detected. The Mid=%d", smb->Mid));
  311. return 1;
  312. }
  313. int
  314. checkSMB(struct smb_hdr *smb, __u16 mid, int length)
  315. {
  316. __u32 len = be32_to_cpu(smb->smb_buf_length);
  317. cFYI(0,
  318. ("Entering checkSMB with Length: %x, smb_buf_length: %x ",
  319. length, len));
  320. if (((unsigned int)length < 2 + sizeof (struct smb_hdr)) ||
  321. (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4)) {
  322. if ((unsigned int)length < 2 + sizeof (struct smb_hdr)) {
  323. if (((unsigned int)length >=
  324. sizeof (struct smb_hdr) - 1)
  325. && (smb->Status.CifsError != 0)) {
  326. smb->WordCount = 0;
  327. return 0; /* some error cases do not return wct and bcc */
  328. } else {
  329. cERROR(1, ("Length less than smb header size"));
  330. }
  331. }
  332. if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4)
  333. cERROR(1,
  334. ("smb_buf_length greater than MaxBufSize"));
  335. cERROR(1,
  336. ("bad smb detected. Illegal length. The mid=%d",
  337. smb->Mid));
  338. return 1;
  339. }
  340. if (checkSMBhdr(smb, mid))
  341. return 1;
  342. if ((4 + len != smbCalcSize(smb))
  343. || (4 + len != (unsigned int)length)) {
  344. return 0;
  345. } else {
  346. cERROR(1, ("smbCalcSize %x ", smbCalcSize(smb)));
  347. cERROR(1,
  348. ("bad smb size detected. The Mid=%d", smb->Mid));
  349. return 1;
  350. }
  351. }
  352. int
  353. is_valid_oplock_break(struct smb_hdr *buf)
  354. {
  355. struct smb_com_lock_req * pSMB = (struct smb_com_lock_req *)buf;
  356. struct list_head *tmp;
  357. struct list_head *tmp1;
  358. struct cifsTconInfo *tcon;
  359. struct cifsFileInfo *netfile;
  360. cFYI(1,("Checking for oplock break or dnotify response"));
  361. if((pSMB->hdr.Command == SMB_COM_NT_TRANSACT) &&
  362. (pSMB->hdr.Flags & SMBFLG_RESPONSE)) {
  363. struct smb_com_transaction_change_notify_rsp * pSMBr =
  364. (struct smb_com_transaction_change_notify_rsp *)buf;
  365. struct file_notify_information * pnotify;
  366. __u32 data_offset = 0;
  367. if(pSMBr->ByteCount > sizeof(struct file_notify_information)) {
  368. data_offset = le32_to_cpu(pSMBr->DataOffset);
  369. pnotify = (struct file_notify_information *)((char *)&pSMBr->hdr.Protocol
  370. + data_offset);
  371. cFYI(1,("dnotify on %s with action: 0x%x",pnotify->FileName,
  372. pnotify->Action)); /* BB removeme BB */
  373. /* cifs_dump_mem("Received notify Data is: ",buf,sizeof(struct smb_hdr)+60); */
  374. return TRUE;
  375. }
  376. if(pSMBr->hdr.Status.CifsError) {
  377. cFYI(1,("notify err 0x%d",pSMBr->hdr.Status.CifsError));
  378. return TRUE;
  379. }
  380. return FALSE;
  381. }
  382. if(pSMB->hdr.Command != SMB_COM_LOCKING_ANDX)
  383. return FALSE;
  384. if(pSMB->hdr.Flags & SMBFLG_RESPONSE) {
  385. /* no sense logging error on invalid handle on oplock
  386. break - harmless race between close request and oplock
  387. break response is expected from time to time writing out
  388. large dirty files cached on the client */
  389. if ((NT_STATUS_INVALID_HANDLE) ==
  390. le32_to_cpu(pSMB->hdr.Status.CifsError)) {
  391. cFYI(1,("invalid handle on oplock break"));
  392. return TRUE;
  393. } else if (ERRbadfid ==
  394. le16_to_cpu(pSMB->hdr.Status.DosError.Error)) {
  395. return TRUE;
  396. } else {
  397. return FALSE; /* on valid oplock brk we get "request" */
  398. }
  399. }
  400. if(pSMB->hdr.WordCount != 8)
  401. return FALSE;
  402. cFYI(1,(" oplock type 0x%d level 0x%d",pSMB->LockType,pSMB->OplockLevel));
  403. if(!(pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE))
  404. return FALSE;
  405. /* look up tcon based on tid & uid */
  406. read_lock(&GlobalSMBSeslock);
  407. list_for_each(tmp, &GlobalTreeConnectionList) {
  408. tcon = list_entry(tmp, struct cifsTconInfo, cifsConnectionList);
  409. if (tcon->tid == buf->Tid) {
  410. #ifdef CONFIG_CIFS_STATS
  411. atomic_inc(&tcon->num_oplock_brks);
  412. #endif
  413. list_for_each(tmp1,&tcon->openFileList){
  414. netfile = list_entry(tmp1,struct cifsFileInfo,
  415. tlist);
  416. if(pSMB->Fid == netfile->netfid) {
  417. struct cifsInodeInfo *pCifsInode;
  418. read_unlock(&GlobalSMBSeslock);
  419. cFYI(1,("file id match, oplock break"));
  420. pCifsInode =
  421. CIFS_I(netfile->pInode);
  422. pCifsInode->clientCanCacheAll = FALSE;
  423. if(pSMB->OplockLevel == 0)
  424. pCifsInode->clientCanCacheRead
  425. = FALSE;
  426. pCifsInode->oplockPending = TRUE;
  427. AllocOplockQEntry(netfile->pInode,
  428. netfile->netfid,
  429. tcon);
  430. cFYI(1,("about to wake up oplock thd"));
  431. if(oplockThread)
  432. wake_up_process(oplockThread);
  433. return TRUE;
  434. }
  435. }
  436. read_unlock(&GlobalSMBSeslock);
  437. cFYI(1,("No matching file for oplock break"));
  438. return TRUE;
  439. }
  440. }
  441. read_unlock(&GlobalSMBSeslock);
  442. cFYI(1,("Can not process oplock break for non-existent connection"));
  443. return TRUE;
  444. }
  445. void
  446. dump_smb(struct smb_hdr *smb_buf, int smb_buf_length)
  447. {
  448. int i, j;
  449. char debug_line[17];
  450. unsigned char *buffer;
  451. if (traceSMB == 0)
  452. return;
  453. buffer = (unsigned char *) smb_buf;
  454. for (i = 0, j = 0; i < smb_buf_length; i++, j++) {
  455. if (i % 8 == 0) { /* have reached the beginning of line */
  456. printk(KERN_DEBUG "| ");
  457. j = 0;
  458. }
  459. printk("%0#4x ", buffer[i]);
  460. debug_line[2 * j] = ' ';
  461. if (isprint(buffer[i]))
  462. debug_line[1 + (2 * j)] = buffer[i];
  463. else
  464. debug_line[1 + (2 * j)] = '_';
  465. if (i % 8 == 7) { /* reached end of line, time to print ascii */
  466. debug_line[16] = 0;
  467. printk(" | %s\n", debug_line);
  468. }
  469. }
  470. for (; j < 8; j++) {
  471. printk(" ");
  472. debug_line[2 * j] = ' ';
  473. debug_line[1 + (2 * j)] = ' ';
  474. }
  475. printk( " | %s\n", debug_line);
  476. return;
  477. }
  478. /* Windows maps these to the user defined 16 bit Unicode range since they are
  479. reserved symbols (along with \ and /), otherwise illegal to store
  480. in filenames in NTFS */
  481. #define UNI_ASTERIK (__u16) ('*' + 0xF000)
  482. #define UNI_QUESTION (__u16) ('?' + 0xF000)
  483. #define UNI_COLON (__u16) (':' + 0xF000)
  484. #define UNI_GRTRTHAN (__u16) ('>' + 0xF000)
  485. #define UNI_LESSTHAN (__u16) ('<' + 0xF000)
  486. #define UNI_PIPE (__u16) ('|' + 0xF000)
  487. #define UNI_SLASH (__u16) ('\\' + 0xF000)
  488. /* Convert 16 bit Unicode pathname from wire format to string in current code
  489. page. Conversion may involve remapping up the seven characters that are
  490. only legal in POSIX-like OS (if they are present in the string). Path
  491. names are little endian 16 bit Unicode on the wire */
  492. int
  493. cifs_convertUCSpath(char *target, const __le16 * source, int maxlen,
  494. const struct nls_table * cp)
  495. {
  496. int i,j,len;
  497. __u16 src_char;
  498. for(i = 0, j = 0; i < maxlen; i++) {
  499. src_char = le16_to_cpu(source[i]);
  500. switch (src_char) {
  501. case 0:
  502. goto cUCS_out; /* BB check this BB */
  503. case UNI_COLON:
  504. target[j] = ':';
  505. break;
  506. case UNI_ASTERIK:
  507. target[j] = '*';
  508. break;
  509. case UNI_QUESTION:
  510. target[j] = '?';
  511. break;
  512. /* BB We can not handle remapping slash until
  513. all the calls to build_path_from_dentry
  514. are modified, as they use slash as separator BB */
  515. /* case UNI_SLASH:
  516. target[j] = '\\';
  517. break;*/
  518. case UNI_PIPE:
  519. target[j] = '|';
  520. break;
  521. case UNI_GRTRTHAN:
  522. target[j] = '>';
  523. break;
  524. case UNI_LESSTHAN:
  525. target[j] = '<';
  526. break;
  527. default:
  528. len = cp->uni2char(src_char, &target[j],
  529. NLS_MAX_CHARSET_SIZE);
  530. if(len > 0) {
  531. j += len;
  532. continue;
  533. } else {
  534. target[j] = '?';
  535. }
  536. }
  537. j++;
  538. /* make sure we do not overrun callers allocated temp buffer */
  539. if(j >= (2 * NAME_MAX))
  540. break;
  541. }
  542. cUCS_out:
  543. target[j] = 0;
  544. return j;
  545. }
  546. /* Convert 16 bit Unicode pathname to wire format from string in current code
  547. page. Conversion may involve remapping up the seven characters that are
  548. only legal in POSIX-like OS (if they are present in the string). Path
  549. names are little endian 16 bit Unicode on the wire */
  550. int
  551. cifsConvertToUCS(__le16 * target, const char *source, int maxlen,
  552. const struct nls_table * cp, int mapChars)
  553. {
  554. int i,j,charlen;
  555. int len_remaining = maxlen;
  556. char src_char;
  557. if(!mapChars)
  558. return cifs_strtoUCS((wchar_t *) target, source, PATH_MAX, cp);
  559. for(i = 0, j = 0; i < maxlen; j++) {
  560. src_char = source[i];
  561. switch (src_char) {
  562. case 0:
  563. target[j] = 0;
  564. goto ctoUCS_out;
  565. case ':':
  566. target[j] = cpu_to_le16(UNI_COLON);
  567. break;
  568. case '*':
  569. target[j] = cpu_to_le16(UNI_ASTERIK);
  570. break;
  571. case '?':
  572. target[j] = cpu_to_le16(UNI_QUESTION);
  573. break;
  574. case '<':
  575. target[j] = cpu_to_le16(UNI_LESSTHAN);
  576. break;
  577. case '>':
  578. target[j] = cpu_to_le16(UNI_GRTRTHAN);
  579. break;
  580. case '|':
  581. target[j] = cpu_to_le16(UNI_PIPE);
  582. break;
  583. /* BB We can not handle remapping slash until
  584. all the calls to build_path_from_dentry
  585. are modified, as they use slash as separator BB */
  586. /* case '\\':
  587. target[j] = cpu_to_le16(UNI_SLASH);
  588. break;*/
  589. default:
  590. charlen = cp->char2uni(source+i,
  591. len_remaining, target+j);
  592. /* if no match, use question mark, which
  593. at least in some cases servers as wild card */
  594. if(charlen < 1) {
  595. target[j] = cpu_to_le16(0x003f);
  596. charlen = 1;
  597. }
  598. len_remaining -= charlen;
  599. /* character may take more than one byte in the
  600. the source string, but will take exactly two
  601. bytes in the target string */
  602. i+= charlen;
  603. continue;
  604. }
  605. i++; /* move to next char in source string */
  606. len_remaining--;
  607. }
  608. ctoUCS_out:
  609. return i;
  610. }