PageRenderTime 67ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/release/src/linux/linux/fs/vfat/namei.c

https://gitlab.com/envieidoc/tomato
C | 1288 lines | 1036 code | 146 blank | 106 comment | 292 complexity | 4a475f5672fcf13a5154bc65c3a454db MD5 | raw file
  1. /*
  2. * linux/fs/vfat/namei.c
  3. *
  4. * Written 1992,1993 by Werner Almesberger
  5. *
  6. * Windows95/Windows NT compatible extended MSDOS filesystem
  7. * by Gordon Chaffee Copyright (C) 1995. Send bug reports for the
  8. * VFAT filesystem to <chaffee@cs.berkeley.edu>. Specify
  9. * what file operation caused you trouble and if you can duplicate
  10. * the problem, send a script that demonstrates it.
  11. *
  12. * Short name translation 1999, 2001 by Wolfram Pienkoss <wp@bszh.de>
  13. *
  14. * Support Multibyte character and cleanup by
  15. * OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
  16. */
  17. #include <linux/module.h>
  18. #include <linux/sched.h>
  19. #include <linux/msdos_fs.h>
  20. #include <linux/nls.h>
  21. #include <linux/kernel.h>
  22. #include <linux/errno.h>
  23. #include <linux/string.h>
  24. #include <linux/ctype.h>
  25. #include <linux/stat.h>
  26. #include <linux/mm.h>
  27. #include <linux/slab.h>
  28. #define DEBUG_LEVEL 0
  29. #if (DEBUG_LEVEL >= 1)
  30. # define PRINTK1(x) printk x
  31. #else
  32. # define PRINTK1(x)
  33. #endif
  34. #if (DEBUG_LEVEL >= 2)
  35. # define PRINTK2(x) printk x
  36. #else
  37. # define PRINTK2(x)
  38. #endif
  39. #if (DEBUG_LEVEL >= 3)
  40. # define PRINTK3(x) printk x
  41. #else
  42. # define PRINTK3(x)
  43. #endif
  44. static int vfat_hashi(struct dentry *parent, struct qstr *qstr);
  45. static int vfat_hash(struct dentry *parent, struct qstr *qstr);
  46. static int vfat_cmpi(struct dentry *dentry, struct qstr *a, struct qstr *b);
  47. static int vfat_cmp(struct dentry *dentry, struct qstr *a, struct qstr *b);
  48. static int vfat_revalidate(struct dentry *dentry, int);
  49. static struct dentry_operations vfat_dentry_ops[4] = {
  50. {
  51. d_hash: vfat_hashi,
  52. d_compare: vfat_cmpi,
  53. },
  54. {
  55. d_revalidate: vfat_revalidate,
  56. d_hash: vfat_hashi,
  57. d_compare: vfat_cmpi,
  58. },
  59. {
  60. d_hash: vfat_hash,
  61. d_compare: vfat_cmp,
  62. },
  63. {
  64. d_revalidate: vfat_revalidate,
  65. d_hash: vfat_hash,
  66. d_compare: vfat_cmp,
  67. }
  68. };
  69. static int vfat_revalidate(struct dentry *dentry, int flags)
  70. {
  71. PRINTK1(("vfat_revalidate: %s\n", dentry->d_name.name));
  72. spin_lock(&dcache_lock);
  73. if (dentry->d_time == dentry->d_parent->d_inode->i_version) {
  74. spin_unlock(&dcache_lock);
  75. return 1;
  76. }
  77. spin_unlock(&dcache_lock);
  78. return 0;
  79. }
  80. static int simple_getbool(char *s, int *setval)
  81. {
  82. if (s) {
  83. if (!strcmp(s,"1") || !strcmp(s,"yes") || !strcmp(s,"true")) {
  84. *setval = 1;
  85. } else if (!strcmp(s,"0") || !strcmp(s,"no") || !strcmp(s,"false")) {
  86. *setval = 0;
  87. } else {
  88. return 0;
  89. }
  90. } else {
  91. *setval = 1;
  92. }
  93. return 1;
  94. }
  95. static int parse_options(char *options, struct fat_mount_options *opts)
  96. {
  97. char *this_char,*value,save,*savep;
  98. int ret, val;
  99. opts->unicode_xlate = opts->posixfs = 0;
  100. opts->numtail = 1;
  101. opts->utf8 = 0;
  102. opts->shortname = VFAT_SFN_DISPLAY_LOWER | VFAT_SFN_CREATE_WIN95;
  103. /* for backward compatible */
  104. if (opts->nocase) {
  105. opts->nocase = 0;
  106. opts->shortname = VFAT_SFN_DISPLAY_WIN95
  107. | VFAT_SFN_CREATE_WIN95;
  108. }
  109. if (!options) return 1;
  110. save = 0;
  111. savep = NULL;
  112. ret = 1;
  113. for (this_char = strtok(options,","); this_char; this_char = strtok(NULL,",")) {
  114. if ((value = strchr(this_char,'=')) != NULL) {
  115. save = *value;
  116. savep = value;
  117. *value++ = 0;
  118. }
  119. if (!strcmp(this_char,"utf8")) {
  120. ret = simple_getbool(value, &val);
  121. if (ret) opts->utf8 = val;
  122. } else if (!strcmp(this_char,"uni_xlate")) {
  123. ret = simple_getbool(value, &val);
  124. if (ret) opts->unicode_xlate = val;
  125. } else if (!strcmp(this_char,"posix")) {
  126. ret = simple_getbool(value, &val);
  127. if (ret) opts->posixfs = val;
  128. } else if (!strcmp(this_char,"nonumtail")) {
  129. ret = simple_getbool(value, &val);
  130. if (ret) {
  131. opts->numtail = !val;
  132. }
  133. } else if (!strcmp(this_char, "shortname")) {
  134. if (!strcmp(value, "lower"))
  135. opts->shortname = VFAT_SFN_DISPLAY_LOWER
  136. | VFAT_SFN_CREATE_WIN95;
  137. else if (!strcmp(value, "win95"))
  138. opts->shortname = VFAT_SFN_DISPLAY_WIN95
  139. | VFAT_SFN_CREATE_WIN95;
  140. else if (!strcmp(value, "winnt"))
  141. opts->shortname = VFAT_SFN_DISPLAY_WINNT
  142. | VFAT_SFN_CREATE_WINNT;
  143. else if (!strcmp(value, "mixed"))
  144. opts->shortname = VFAT_SFN_DISPLAY_WINNT
  145. | VFAT_SFN_CREATE_WIN95;
  146. else
  147. ret = 0;
  148. }
  149. if (this_char != options)
  150. *(this_char-1) = ',';
  151. if (value) {
  152. *savep = save;
  153. }
  154. if (ret == 0) {
  155. return 0;
  156. }
  157. }
  158. if (opts->unicode_xlate) {
  159. opts->utf8 = 0;
  160. }
  161. return 1;
  162. }
  163. static inline unsigned char
  164. vfat_tolower(struct nls_table *t, unsigned char c)
  165. {
  166. unsigned char nc = t->charset2lower[c];
  167. return nc ? nc : c;
  168. }
  169. static inline unsigned char
  170. vfat_toupper(struct nls_table *t, unsigned char c)
  171. {
  172. unsigned char nc = t->charset2upper[c];
  173. return nc ? nc : c;
  174. }
  175. static int
  176. vfat_strnicmp(struct nls_table *t, const unsigned char *s1,
  177. const unsigned char *s2, int len)
  178. {
  179. while(len--)
  180. if (vfat_tolower(t, *s1++) != vfat_tolower(t, *s2++))
  181. return 1;
  182. return 0;
  183. }
  184. /*
  185. * Compute the hash for the vfat name corresponding to the dentry.
  186. * Note: if the name is invalid, we leave the hash code unchanged so
  187. * that the existing dentry can be used. The vfat fs routines will
  188. * return ENOENT or EINVAL as appropriate.
  189. */
  190. static int vfat_hash(struct dentry *dentry, struct qstr *qstr)
  191. {
  192. const char *name;
  193. int len;
  194. len = qstr->len;
  195. name = qstr->name;
  196. while (len && name[len-1] == '.')
  197. len--;
  198. qstr->hash = full_name_hash(name, len);
  199. return 0;
  200. }
  201. /*
  202. * Compute the hash for the vfat name corresponding to the dentry.
  203. * Note: if the name is invalid, we leave the hash code unchanged so
  204. * that the existing dentry can be used. The vfat fs routines will
  205. * return ENOENT or EINVAL as appropriate.
  206. */
  207. static int vfat_hashi(struct dentry *dentry, struct qstr *qstr)
  208. {
  209. struct nls_table *t = MSDOS_SB(dentry->d_inode->i_sb)->nls_io;
  210. const char *name;
  211. int len;
  212. unsigned long hash;
  213. len = qstr->len;
  214. name = qstr->name;
  215. while (len && name[len-1] == '.')
  216. len--;
  217. hash = init_name_hash();
  218. while (len--)
  219. hash = partial_name_hash(vfat_tolower(t, *name++), hash);
  220. qstr->hash = end_name_hash(hash);
  221. return 0;
  222. }
  223. /*
  224. * Case insensitive compare of two vfat names.
  225. */
  226. static int vfat_cmpi(struct dentry *dentry, struct qstr *a, struct qstr *b)
  227. {
  228. struct nls_table *t = MSDOS_SB(dentry->d_inode->i_sb)->nls_io;
  229. int alen, blen;
  230. /* A filename cannot end in '.' or we treat it like it has none */
  231. alen = a->len;
  232. blen = b->len;
  233. while (alen && a->name[alen-1] == '.')
  234. alen--;
  235. while (blen && b->name[blen-1] == '.')
  236. blen--;
  237. if (alen == blen) {
  238. if (vfat_strnicmp(t, a->name, b->name, alen) == 0)
  239. return 0;
  240. }
  241. return 1;
  242. }
  243. /*
  244. * Case sensitive compare of two vfat names.
  245. */
  246. static int vfat_cmp(struct dentry *dentry, struct qstr *a, struct qstr *b)
  247. {
  248. int alen, blen;
  249. /* A filename cannot end in '.' or we treat it like it has none */
  250. alen = a->len;
  251. blen = b->len;
  252. while (alen && a->name[alen-1] == '.')
  253. alen--;
  254. while (blen && b->name[blen-1] == '.')
  255. blen--;
  256. if (alen == blen) {
  257. if (strncmp(a->name, b->name, alen) == 0)
  258. return 0;
  259. }
  260. return 1;
  261. }
  262. #ifdef DEBUG
  263. static void dump_fat(struct super_block *sb,int start)
  264. {
  265. printk("[");
  266. while (start) {
  267. printk("%d ",start);
  268. start = fat_access(sb,start,-1);
  269. if (!start) {
  270. printk("ERROR");
  271. break;
  272. }
  273. if (start == -1) break;
  274. }
  275. printk("]\n");
  276. }
  277. static void dump_de(struct msdos_dir_entry *de)
  278. {
  279. int i;
  280. unsigned char *p = (unsigned char *) de;
  281. printk("[");
  282. for (i = 0; i < 32; i++, p++) {
  283. printk("%02x ", *p);
  284. }
  285. printk("]\n");
  286. }
  287. #endif
  288. /* MS-DOS "device special files" */
  289. static const char *reserved3_names[] = {
  290. "con ", "prn ", "nul ", "aux ", NULL
  291. };
  292. static const char *reserved4_names[] = {
  293. "com1 ", "com2 ", "com3 ", "com4 ", "com5 ",
  294. "com6 ", "com7 ", "com8 ", "com9 ",
  295. "lpt1 ", "lpt2 ", "lpt3 ", "lpt4 ", "lpt5 ",
  296. "lpt6 ", "lpt7 ", "lpt8 ", "lpt9 ",
  297. NULL };
  298. /* Characters that are undesirable in an MS-DOS file name */
  299. static wchar_t bad_chars[] = {
  300. /* `*' `?' `<' `>' `|' `"' `:' `/' */
  301. 0x002A, 0x003F, 0x003C, 0x003E, 0x007C, 0x0022, 0x003A, 0x002F,
  302. /* `\' */
  303. 0x005C, 0,
  304. };
  305. #define IS_BADCHAR(uni) (vfat_unistrchr(bad_chars, (uni)) != NULL)
  306. static wchar_t replace_chars[] = {
  307. /* `[' `]' `;' `,' `+' `=' */
  308. 0x005B, 0x005D, 0x003B, 0x002C, 0x002B, 0x003D, 0,
  309. };
  310. #define IS_REPLACECHAR(uni) (vfat_unistrchr(replace_chars, (uni)) != NULL)
  311. static wchar_t skip_chars[] = {
  312. /* `.' ` ' */
  313. 0x002E, 0x0020, 0,
  314. };
  315. #define IS_SKIPCHAR(uni) \
  316. ((wchar_t)(uni) == skip_chars[0] || (wchar_t)(uni) == skip_chars[1])
  317. static inline wchar_t *vfat_unistrchr(const wchar_t *s, const wchar_t c)
  318. {
  319. for(; *s != c; ++s)
  320. if (*s == 0)
  321. return NULL;
  322. return (wchar_t *) s;
  323. }
  324. static inline int vfat_is_used_badchars(const wchar_t *s, int len)
  325. {
  326. int i;
  327. for (i = 0; i < len; i++)
  328. if (s[i] < 0x0020 || IS_BADCHAR(s[i]))
  329. return -EINVAL;
  330. return 0;
  331. }
  332. /* Checks the validity of a long MS-DOS filename */
  333. /* Returns negative number on error, 0 for a normal
  334. * return, and 1 for . or .. */
  335. static int vfat_valid_longname(const char *name, int len, int xlate)
  336. {
  337. const char **reserved, *walk;
  338. int baselen;
  339. if (len && name[len-1] == ' ') return -EINVAL;
  340. if (len >= 256) return -EINVAL;
  341. if (len < 3) return 0;
  342. for (walk = name; *walk != 0 && *walk != '.'; walk++);
  343. baselen = walk - name;
  344. if (baselen == 3) {
  345. for (reserved = reserved3_names; *reserved; reserved++) {
  346. if (!strnicmp(name,*reserved,baselen))
  347. return -EINVAL;
  348. }
  349. } else if (baselen == 4) {
  350. for (reserved = reserved4_names; *reserved; reserved++) {
  351. if (!strnicmp(name,*reserved,baselen))
  352. return -EINVAL;
  353. }
  354. }
  355. return 0;
  356. }
  357. static int vfat_find_form(struct inode *dir,char *name)
  358. {
  359. struct msdos_dir_entry *de;
  360. struct buffer_head *bh = NULL;
  361. loff_t i_pos;
  362. int res;
  363. res = fat_scan(dir, name, &bh, &de, &i_pos);
  364. fat_brelse(dir->i_sb, bh);
  365. if (res<0)
  366. return -ENOENT;
  367. return 0;
  368. }
  369. /*
  370. * 1) Valid characters for the 8.3 format alias are any combination of
  371. * letters, uppercase alphabets, digits, any of the
  372. * following special characters:
  373. * $ % ' ` - @ { } ~ ! # ( ) & _ ^
  374. * In this case Longfilename is not stored in disk.
  375. *
  376. * WinNT's Extension:
  377. * File name and extension name is contain uppercase/lowercase
  378. * only. And it is expressed by CASE_LOWER_BASE and CASE_LOWER_EXT.
  379. *
  380. * 2) File name is 8.3 format, but it contain the uppercase and
  381. * lowercase char, muliti bytes char, etc. In this case numtail is not
  382. * added, but Longfilename is stored.
  383. *
  384. * 3) When the one except for the above, or the following special
  385. * character are contained:
  386. * . [ ] ; , + =
  387. * numtail is added, and Longfilename must be stored in disk .
  388. */
  389. struct shortname_info {
  390. unsigned char lower:1,
  391. upper:1,
  392. valid:1;
  393. };
  394. #define INIT_SHORTNAME_INFO(x) do { \
  395. (x)->lower = 1; \
  396. (x)->upper = 1; \
  397. (x)->valid = 1; \
  398. } while (0)
  399. static inline unsigned char
  400. shortname_info_to_lcase(struct shortname_info *base,
  401. struct shortname_info *ext)
  402. {
  403. unsigned char lcase = 0;
  404. if (base->valid && ext->valid) {
  405. if (!base->upper && base->lower && (ext->lower || ext->upper))
  406. lcase |= CASE_LOWER_BASE;
  407. if (!ext->upper && ext->lower && (base->lower || base->upper))
  408. lcase |= CASE_LOWER_EXT;
  409. }
  410. return lcase;
  411. }
  412. static inline int to_shortname_char(struct nls_table *nls,
  413. char *buf, int buf_size, wchar_t *src,
  414. struct shortname_info *info)
  415. {
  416. int len;
  417. if (IS_SKIPCHAR(*src)) {
  418. info->valid = 0;
  419. return 0;
  420. }
  421. if (IS_REPLACECHAR(*src)) {
  422. info->valid = 0;
  423. buf[0] = '_';
  424. return 1;
  425. }
  426. len = nls->uni2char(*src, buf, buf_size);
  427. if (len <= 0) {
  428. info->valid = 0;
  429. buf[0] = '_';
  430. len = 1;
  431. } else if (len == 1) {
  432. unsigned char prev = buf[0];
  433. if (buf[0] >= 0x7F) {
  434. info->lower = 0;
  435. info->upper = 0;
  436. }
  437. buf[0] = vfat_toupper(nls, buf[0]);
  438. if (isalpha(buf[0])) {
  439. if (buf[0] == prev)
  440. info->lower = 0;
  441. else
  442. info->upper = 0;
  443. }
  444. } else {
  445. info->lower = 0;
  446. info->upper = 0;
  447. }
  448. return len;
  449. }
  450. /*
  451. * Given a valid longname, create a unique shortname. Make sure the
  452. * shortname does not exist
  453. * Returns negative number on error, 0 for a normal
  454. * return, and 1 for valid shortname
  455. */
  456. static int vfat_create_shortname(struct inode *dir, struct nls_table *nls,
  457. wchar_t *uname, int ulen,
  458. char *name_res, unsigned char *lcase)
  459. {
  460. wchar_t *ip, *ext_start, *end, *name_start;
  461. unsigned char base[9], ext[4], buf[5], *p;
  462. unsigned char charbuf[NLS_MAX_CHARSET_SIZE];
  463. int chl, chi;
  464. int sz = 0, extlen, baselen, i, numtail_baselen, numtail2_baselen;
  465. int is_shortname;
  466. struct shortname_info base_info, ext_info;
  467. unsigned short opt_shortname = MSDOS_SB(dir->i_sb)->options.shortname;
  468. is_shortname = 1;
  469. INIT_SHORTNAME_INFO(&base_info);
  470. INIT_SHORTNAME_INFO(&ext_info);
  471. /* Now, we need to create a shortname from the long name */
  472. ext_start = end = &uname[ulen];
  473. while (--ext_start >= uname) {
  474. if (*ext_start == 0x002E) { /* is `.' */
  475. if (ext_start == end - 1) {
  476. sz = ulen;
  477. ext_start = NULL;
  478. }
  479. break;
  480. }
  481. }
  482. if (ext_start == uname - 1) {
  483. sz = ulen;
  484. ext_start = NULL;
  485. } else if (ext_start) {
  486. /*
  487. * Names which start with a dot could be just
  488. * an extension eg. "...test". In this case Win95
  489. * uses the extension as the name and sets no extension.
  490. */
  491. name_start = &uname[0];
  492. while (name_start < ext_start) {
  493. if (!IS_SKIPCHAR(*name_start))
  494. break;
  495. name_start++;
  496. }
  497. if (name_start != ext_start) {
  498. sz = ext_start - uname;
  499. ext_start++;
  500. } else {
  501. sz = ulen;
  502. ext_start=NULL;
  503. }
  504. }
  505. numtail_baselen = 6;
  506. numtail2_baselen = 2;
  507. for (baselen = i = 0, p = base, ip = uname; i < sz; i++, ip++) {
  508. chl = to_shortname_char(nls, charbuf, sizeof(charbuf),
  509. ip, &base_info);
  510. if (chl == 0)
  511. continue;
  512. if (baselen < 2 && (baselen + chl) > 2)
  513. numtail2_baselen = baselen;
  514. if (baselen < 6 && (baselen + chl) > 6)
  515. numtail_baselen = baselen;
  516. for (chi = 0; chi < chl; chi++){
  517. *p++ = charbuf[chi];
  518. baselen++;
  519. if (baselen >= 8)
  520. break;
  521. }
  522. if (baselen >= 8) {
  523. if ((chi < chl - 1) || (ip + 1) - uname < sz)
  524. is_shortname = 0;
  525. break;
  526. }
  527. }
  528. if (baselen == 0) {
  529. return -EINVAL;
  530. }
  531. extlen = 0;
  532. if (ext_start) {
  533. for (p = ext, ip = ext_start; extlen < 3 && ip < end; ip++) {
  534. chl = to_shortname_char(nls, charbuf, sizeof(charbuf),
  535. ip, &ext_info);
  536. if (chl == 0)
  537. continue;
  538. if ((extlen + chl) > 3) {
  539. is_shortname = 0;
  540. break;
  541. }
  542. for (chi = 0; chi < chl; chi++) {
  543. *p++ = charbuf[chi];
  544. extlen++;
  545. }
  546. if (extlen >= 3) {
  547. if (ip + 1 != end)
  548. is_shortname = 0;
  549. break;
  550. }
  551. }
  552. }
  553. ext[extlen] = '\0';
  554. base[baselen] = '\0';
  555. /* Yes, it can happen. ".\xe5" would do it. */
  556. if (base[0] == DELETED_FLAG)
  557. base[0] = 0x05;
  558. /* OK, at this point we know that base is not longer than 8 symbols,
  559. * ext is not longer than 3, base is nonempty, both don't contain
  560. * any bad symbols (lowercase transformed to uppercase).
  561. */
  562. memset(name_res, ' ', MSDOS_NAME);
  563. memcpy(name_res, base, baselen);
  564. memcpy(name_res + 8, ext, extlen);
  565. *lcase = 0;
  566. if (is_shortname && base_info.valid && ext_info.valid) {
  567. if (vfat_find_form(dir, name_res) == 0)
  568. return -EEXIST;
  569. if (opt_shortname & VFAT_SFN_CREATE_WIN95) {
  570. return (base_info.upper && ext_info.upper);
  571. } else if (opt_shortname & VFAT_SFN_CREATE_WINNT) {
  572. if ((base_info.upper || base_info.lower)
  573. && (ext_info.upper || ext_info.lower)) {
  574. *lcase = shortname_info_to_lcase(&base_info,
  575. &ext_info);
  576. return 1;
  577. }
  578. return 0;
  579. } else {
  580. BUG();
  581. }
  582. }
  583. if (MSDOS_SB(dir->i_sb)->options.numtail == 0)
  584. if (vfat_find_form(dir, name_res) < 0)
  585. return 0;
  586. /*
  587. * Try to find a unique extension. This used to
  588. * iterate through all possibilities sequentially,
  589. * but that gave extremely bad performance. Windows
  590. * only tries a few cases before using random
  591. * values for part of the base.
  592. */
  593. if (baselen>6) {
  594. baselen = numtail_baselen;
  595. name_res[7] = ' ';
  596. }
  597. name_res[baselen] = '~';
  598. for (i = 1; i < 10; i++) {
  599. name_res[baselen+1] = i + '0';
  600. if (vfat_find_form(dir, name_res) < 0)
  601. return 0;
  602. }
  603. i = jiffies;
  604. sz = (jiffies >> 16) & 0x7;
  605. if (baselen>2) {
  606. baselen = numtail2_baselen;
  607. name_res[7] = ' ';
  608. }
  609. name_res[baselen+4] = '~';
  610. name_res[baselen+5] = '1' + sz;
  611. while (1) {
  612. snprintf(buf, sizeof(buf), "%04X", i & 0xffff);
  613. memcpy(&name_res[baselen], buf, 4);
  614. if (vfat_find_form(dir, name_res) < 0)
  615. break;
  616. i -= 11;
  617. }
  618. return 0;
  619. }
  620. /* Translate a string, including coded sequences into Unicode */
  621. static int
  622. xlate_to_uni(const char *name, int len, char *outname, int *longlen, int *outlen,
  623. int escape, int utf8, struct nls_table *nls)
  624. {
  625. const unsigned char *ip;
  626. unsigned char nc;
  627. char *op;
  628. unsigned int ec;
  629. int i, k, fill;
  630. int charlen;
  631. if (utf8) {
  632. *outlen = utf8_mbstowcs((__u16 *) outname, name, PAGE_SIZE);
  633. if (name[len-1] == '.')
  634. *outlen-=2;
  635. op = &outname[*outlen * sizeof(__u16)];
  636. } else {
  637. if (name[len-1] == '.')
  638. len--;
  639. if (nls) {
  640. for (i = 0, ip = name, op = outname, *outlen = 0;
  641. i < len && *outlen <= 260; *outlen += 1)
  642. {
  643. if (escape && (*ip == ':')) {
  644. if (i > len - 5)
  645. return -EINVAL;
  646. ec = 0;
  647. for (k = 1; k < 5; k++) {
  648. nc = ip[k];
  649. ec <<= 4;
  650. if (nc >= '0' && nc <= '9') {
  651. ec |= nc - '0';
  652. continue;
  653. }
  654. if (nc >= 'a' && nc <= 'f') {
  655. ec |= nc - ('a' - 10);
  656. continue;
  657. }
  658. if (nc >= 'A' && nc <= 'F') {
  659. ec |= nc - ('A' - 10);
  660. continue;
  661. }
  662. return -EINVAL;
  663. }
  664. *op++ = ec & 0xFF;
  665. *op++ = ec >> 8;
  666. ip += 5;
  667. i += 5;
  668. } else {
  669. if ((charlen = nls->char2uni(ip, len-i, (wchar_t *)op)) < 0)
  670. return -EINVAL;
  671. ip += charlen;
  672. i += charlen;
  673. op += 2;
  674. }
  675. }
  676. } else {
  677. for (i = 0, ip = name, op = outname, *outlen = 0;
  678. i < len && *outlen <= 260; i++, *outlen += 1)
  679. {
  680. *op++ = *ip++;
  681. *op++ = 0;
  682. }
  683. }
  684. }
  685. if (*outlen > 260)
  686. return -ENAMETOOLONG;
  687. *longlen = *outlen;
  688. if (*outlen % 13) {
  689. *op++ = 0;
  690. *op++ = 0;
  691. *outlen += 1;
  692. if (*outlen % 13) {
  693. fill = 13 - (*outlen % 13);
  694. for (i = 0; i < fill; i++) {
  695. *op++ = 0xff;
  696. *op++ = 0xff;
  697. }
  698. *outlen += fill;
  699. }
  700. }
  701. return 0;
  702. }
  703. static int
  704. vfat_fill_slots(struct inode *dir, struct msdos_dir_slot *ds, const char *name,
  705. int len, int *slots, int is_dir, int uni_xlate)
  706. {
  707. struct nls_table *nls_io, *nls_disk;
  708. wchar_t *uname;
  709. struct msdos_dir_slot *ps;
  710. struct msdos_dir_entry *de;
  711. unsigned long page;
  712. unsigned char cksum, lcase;
  713. char *uniname, msdos_name[MSDOS_NAME];
  714. int res, utf8, slot, ulen, unilen, i;
  715. loff_t offset;
  716. *slots = 0;
  717. utf8 = MSDOS_SB(dir->i_sb)->options.utf8;
  718. nls_io = MSDOS_SB(dir->i_sb)->nls_io;
  719. nls_disk = MSDOS_SB(dir->i_sb)->nls_disk;
  720. if (name[len-1] == '.')
  721. len--;
  722. if(!(page = __get_free_page(GFP_KERNEL)))
  723. return -ENOMEM;
  724. uniname = (char *) page;
  725. res = xlate_to_uni(name, len, uniname, &ulen, &unilen, uni_xlate,
  726. utf8, nls_io);
  727. if (res < 0)
  728. goto out_free;
  729. uname = (wchar_t *) page;
  730. res = vfat_is_used_badchars(uname, ulen);
  731. if (res < 0)
  732. goto out_free;
  733. res = vfat_create_shortname(dir, nls_disk, uname, ulen,
  734. msdos_name, &lcase);
  735. if (res < 0)
  736. goto out_free;
  737. else if (res == 1) {
  738. de = (struct msdos_dir_entry *)ds;
  739. res = 0;
  740. goto shortname;
  741. }
  742. /* build the entry of long file name */
  743. *slots = unilen / 13;
  744. for (cksum = i = 0; i < 11; i++) {
  745. cksum = (((cksum&1)<<7)|((cksum&0xfe)>>1)) + msdos_name[i];
  746. }
  747. PRINTK3(("vfat_fill_slots 3: slots=%d\n",*slots));
  748. for (ps = ds, slot = *slots; slot > 0; slot--, ps++) {
  749. ps->id = slot;
  750. ps->attr = ATTR_EXT;
  751. ps->reserved = 0;
  752. ps->alias_checksum = cksum;
  753. ps->start = 0;
  754. offset = (slot - 1) * 13;
  755. fatwchar_to16(ps->name0_4, uname + offset, 5);
  756. fatwchar_to16(ps->name5_10, uname + offset + 5, 6);
  757. fatwchar_to16(ps->name11_12, uname + offset + 11, 2);
  758. }
  759. ds[0].id |= 0x40;
  760. de = (struct msdos_dir_entry *) ps;
  761. shortname:
  762. PRINTK3(("vfat_fill_slots 9\n"));
  763. /* build the entry of 8.3 alias name */
  764. (*slots)++;
  765. strncpy(de->name, msdos_name, MSDOS_NAME);
  766. de->attr = is_dir ? ATTR_DIR : ATTR_ARCH;
  767. de->lcase = lcase;
  768. de->adate = de->cdate = de->date = 0;
  769. de->ctime_ms = de->ctime = de->time = 0;
  770. de->start = 0;
  771. de->starthi = 0;
  772. de->size = 0;
  773. out_free:
  774. free_page(page);
  775. return res;
  776. }
  777. /* We can't get "." or ".." here - VFS takes care of those cases */
  778. static int vfat_build_slots(struct inode *dir, const char *name, int len,
  779. struct msdos_dir_slot *ds, int *slots, int is_dir)
  780. {
  781. int res, xlate;
  782. xlate = MSDOS_SB(dir->i_sb)->options.unicode_xlate;
  783. res = vfat_valid_longname(name, len, xlate);
  784. if (res < 0)
  785. return res;
  786. return vfat_fill_slots(dir, ds, name, len, slots, is_dir, xlate);
  787. }
  788. static int vfat_add_entry(struct inode *dir,struct qstr* qname,
  789. int is_dir, struct vfat_slot_info *sinfo_out,
  790. struct buffer_head **bh, struct msdos_dir_entry **de)
  791. {
  792. struct super_block *sb = dir->i_sb;
  793. struct msdos_dir_slot *dir_slots;
  794. loff_t offset;
  795. int slots, slot;
  796. int res, len;
  797. struct msdos_dir_entry *dummy_de;
  798. struct buffer_head *dummy_bh;
  799. loff_t dummy_i_pos;
  800. loff_t dummy;
  801. dir_slots = (struct msdos_dir_slot *)
  802. kmalloc(sizeof(struct msdos_dir_slot) * MSDOS_SLOTS, GFP_KERNEL);
  803. if (dir_slots == NULL)
  804. return -ENOMEM;
  805. len = qname->len;
  806. while (len && qname->name[len-1] == '.')
  807. len--;
  808. res = fat_search_long(dir, qname->name, len,
  809. (MSDOS_SB(sb)->options.name_check != 's')
  810. || !MSDOS_SB(sb)->options.posixfs,
  811. &dummy, &dummy);
  812. if (res > 0) /* found */
  813. res = -EEXIST;
  814. if (res)
  815. goto cleanup;
  816. res = vfat_build_slots(dir, qname->name, len,
  817. dir_slots, &slots, is_dir);
  818. if (res < 0)
  819. goto cleanup;
  820. /* build the empty directory entry of number of slots */
  821. offset = fat_add_entries(dir, slots, &dummy_bh, &dummy_de, &dummy_i_pos);
  822. if (offset < 0) {
  823. res = offset;
  824. goto cleanup;
  825. }
  826. fat_brelse(sb, dummy_bh);
  827. /* Now create the new entry */
  828. *bh = NULL;
  829. for (slot = 0; slot < slots; slot++) {
  830. if (fat_get_entry(dir, &offset, bh, de, &sinfo_out->i_pos) < 0) {
  831. res = -EIO;
  832. goto cleanup;
  833. }
  834. memcpy(*de, dir_slots + slot, sizeof(struct msdos_dir_slot));
  835. fat_mark_buffer_dirty(sb, *bh);
  836. }
  837. res = 0;
  838. /* update timestamp */
  839. dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME;
  840. mark_inode_dirty(dir);
  841. fat_date_unix2dos(dir->i_mtime, &(*de)->time, &(*de)->date);
  842. (*de)->ctime = (*de)->time;
  843. (*de)->adate = (*de)->cdate = (*de)->date;
  844. fat_mark_buffer_dirty(sb, *bh);
  845. /* slots can't be less than 1 */
  846. sinfo_out->long_slots = slots - 1;
  847. sinfo_out->longname_offset =
  848. offset - sizeof(struct msdos_dir_slot) * slots;
  849. cleanup:
  850. kfree(dir_slots);
  851. return res;
  852. }
  853. static int vfat_find(struct inode *dir,struct qstr* qname,
  854. struct vfat_slot_info *sinfo, struct buffer_head **last_bh,
  855. struct msdos_dir_entry **last_de)
  856. {
  857. struct super_block *sb = dir->i_sb;
  858. loff_t offset;
  859. int res,len;
  860. len = qname->len;
  861. while (len && qname->name[len-1] == '.')
  862. len--;
  863. res = fat_search_long(dir, qname->name, len,
  864. (MSDOS_SB(sb)->options.name_check != 's'),
  865. &offset,&sinfo->longname_offset);
  866. if (res>0) {
  867. sinfo->long_slots = res-1;
  868. if (fat_get_entry(dir,&offset,last_bh,last_de,&sinfo->i_pos)>=0)
  869. return 0;
  870. res = -EIO;
  871. }
  872. return res ? res : -ENOENT;
  873. }
  874. struct dentry *vfat_lookup(struct inode *dir,struct dentry *dentry)
  875. {
  876. int res;
  877. struct vfat_slot_info sinfo;
  878. struct inode *inode;
  879. struct dentry *alias;
  880. struct buffer_head *bh = NULL;
  881. struct msdos_dir_entry *de;
  882. int table;
  883. PRINTK2(("vfat_lookup: name=%s, len=%d\n",
  884. dentry->d_name.name, dentry->d_name.len));
  885. table = (MSDOS_SB(dir->i_sb)->options.name_check == 's') ? 2 : 0;
  886. dentry->d_op = &vfat_dentry_ops[table];
  887. inode = NULL;
  888. res = vfat_find(dir,&dentry->d_name,&sinfo,&bh,&de);
  889. if (res < 0) {
  890. table++;
  891. goto error;
  892. }
  893. inode = fat_build_inode(dir->i_sb, de, sinfo.i_pos, &res);
  894. fat_brelse(dir->i_sb, bh);
  895. if (res)
  896. return ERR_PTR(res);
  897. alias = d_find_alias(inode);
  898. if (alias) {
  899. if (d_invalidate(alias)==0)
  900. dput(alias);
  901. else {
  902. iput(inode);
  903. return alias;
  904. }
  905. }
  906. error:
  907. dentry->d_op = &vfat_dentry_ops[table];
  908. dentry->d_time = dentry->d_parent->d_inode->i_version;
  909. d_add(dentry,inode);
  910. return NULL;
  911. }
  912. int vfat_create(struct inode *dir,struct dentry* dentry,int mode)
  913. {
  914. struct super_block *sb = dir->i_sb;
  915. struct inode *inode = NULL;
  916. struct buffer_head *bh = NULL;
  917. struct msdos_dir_entry *de;
  918. struct vfat_slot_info sinfo;
  919. int res;
  920. res = vfat_add_entry(dir, &dentry->d_name, 0, &sinfo, &bh, &de);
  921. if (res < 0)
  922. return res;
  923. inode = fat_build_inode(sb, de, sinfo.i_pos, &res);
  924. fat_brelse(sb, bh);
  925. if (!inode)
  926. return res;
  927. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  928. mark_inode_dirty(inode);
  929. inode->i_version = ++event;
  930. dir->i_version = event;
  931. dentry->d_time = dentry->d_parent->d_inode->i_version;
  932. d_instantiate(dentry,inode);
  933. return 0;
  934. }
  935. static void vfat_remove_entry(struct inode *dir,struct vfat_slot_info *sinfo,
  936. struct buffer_head *bh, struct msdos_dir_entry *de)
  937. {
  938. struct super_block *sb = dir->i_sb;
  939. loff_t offset, i_pos;
  940. int i;
  941. /* remove the shortname */
  942. dir->i_mtime = CURRENT_TIME;
  943. dir->i_atime = CURRENT_TIME;
  944. dir->i_version = ++event;
  945. mark_inode_dirty(dir);
  946. de->name[0] = DELETED_FLAG;
  947. fat_mark_buffer_dirty(sb, bh);
  948. /* remove the longname */
  949. offset = sinfo->longname_offset; de = NULL;
  950. for (i = sinfo->long_slots; i > 0; --i) {
  951. if (fat_get_entry(dir, &offset, &bh, &de, &i_pos) < 0)
  952. continue;
  953. de->name[0] = DELETED_FLAG;
  954. de->attr = 0;
  955. fat_mark_buffer_dirty(sb, bh);
  956. }
  957. if (bh) fat_brelse(sb, bh);
  958. }
  959. int vfat_rmdir(struct inode *dir,struct dentry* dentry)
  960. {
  961. int res;
  962. struct vfat_slot_info sinfo;
  963. struct buffer_head *bh = NULL;
  964. struct msdos_dir_entry *de;
  965. res = fat_dir_empty(dentry->d_inode);
  966. if (res)
  967. return res;
  968. res = vfat_find(dir,&dentry->d_name,&sinfo, &bh, &de);
  969. if (res<0)
  970. return res;
  971. dentry->d_inode->i_nlink = 0;
  972. dentry->d_inode->i_mtime = CURRENT_TIME;
  973. dentry->d_inode->i_atime = CURRENT_TIME;
  974. fat_detach(dentry->d_inode);
  975. mark_inode_dirty(dentry->d_inode);
  976. /* releases bh */
  977. vfat_remove_entry(dir,&sinfo,bh,de);
  978. dir->i_nlink--;
  979. return 0;
  980. }
  981. int vfat_unlink(struct inode *dir, struct dentry* dentry)
  982. {
  983. int res;
  984. struct vfat_slot_info sinfo;
  985. struct buffer_head *bh = NULL;
  986. struct msdos_dir_entry *de;
  987. PRINTK1(("vfat_unlink: %s\n", dentry->d_name.name));
  988. res = vfat_find(dir,&dentry->d_name,&sinfo,&bh,&de);
  989. if (res < 0)
  990. return res;
  991. dentry->d_inode->i_nlink = 0;
  992. dentry->d_inode->i_mtime = CURRENT_TIME;
  993. dentry->d_inode->i_atime = CURRENT_TIME;
  994. fat_detach(dentry->d_inode);
  995. mark_inode_dirty(dentry->d_inode);
  996. /* releases bh */
  997. vfat_remove_entry(dir,&sinfo,bh,de);
  998. return res;
  999. }
  1000. int vfat_mkdir(struct inode *dir,struct dentry* dentry,int mode)
  1001. {
  1002. struct super_block *sb = dir->i_sb;
  1003. struct inode *inode = NULL;
  1004. struct vfat_slot_info sinfo;
  1005. struct buffer_head *bh = NULL;
  1006. struct msdos_dir_entry *de;
  1007. int res;
  1008. res = vfat_add_entry(dir, &dentry->d_name, 1, &sinfo, &bh, &de);
  1009. if (res < 0)
  1010. return res;
  1011. inode = fat_build_inode(sb, de, sinfo.i_pos, &res);
  1012. if (!inode)
  1013. goto out;
  1014. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  1015. mark_inode_dirty(inode);
  1016. inode->i_version = ++event;
  1017. dir->i_version = event;
  1018. dir->i_nlink++;
  1019. inode->i_nlink = 2; /* no need to mark them dirty */
  1020. res = fat_new_dir(inode, dir, 1);
  1021. if (res < 0)
  1022. goto mkdir_failed;
  1023. dentry->d_time = dentry->d_parent->d_inode->i_version;
  1024. d_instantiate(dentry,inode);
  1025. out:
  1026. fat_brelse(sb, bh);
  1027. return res;
  1028. mkdir_failed:
  1029. inode->i_nlink = 0;
  1030. inode->i_mtime = CURRENT_TIME;
  1031. inode->i_atime = CURRENT_TIME;
  1032. fat_detach(inode);
  1033. mark_inode_dirty(inode);
  1034. /* releases bh */
  1035. vfat_remove_entry(dir,&sinfo,bh,de);
  1036. iput(inode);
  1037. dir->i_nlink--;
  1038. return res;
  1039. }
  1040. int vfat_rename(struct inode *old_dir,struct dentry *old_dentry,
  1041. struct inode *new_dir,struct dentry *new_dentry)
  1042. {
  1043. struct super_block *sb = old_dir->i_sb;
  1044. struct buffer_head *old_bh,*new_bh,*dotdot_bh;
  1045. struct msdos_dir_entry *old_de,*new_de,*dotdot_de;
  1046. loff_t dotdot_i_pos;
  1047. struct inode *old_inode, *new_inode;
  1048. int res, is_dir;
  1049. struct vfat_slot_info old_sinfo,sinfo;
  1050. old_bh = new_bh = dotdot_bh = NULL;
  1051. old_inode = old_dentry->d_inode;
  1052. new_inode = new_dentry->d_inode;
  1053. res = vfat_find(old_dir,&old_dentry->d_name,&old_sinfo,&old_bh,&old_de);
  1054. PRINTK3(("vfat_rename 2\n"));
  1055. if (res < 0) goto rename_done;
  1056. is_dir = S_ISDIR(old_inode->i_mode);
  1057. if (is_dir && (res = fat_scan(old_inode,MSDOS_DOTDOT,&dotdot_bh,
  1058. &dotdot_de,&dotdot_i_pos)) < 0)
  1059. goto rename_done;
  1060. if (new_dentry->d_inode) {
  1061. res = vfat_find(new_dir,&new_dentry->d_name,&sinfo,&new_bh,
  1062. &new_de);
  1063. if (res < 0 || MSDOS_I(new_inode)->i_pos != sinfo.i_pos) {
  1064. /* WTF??? Cry and fail. */
  1065. printk(KERN_WARNING "vfat_rename: fs corrupted\n");
  1066. goto rename_done;
  1067. }
  1068. if (is_dir) {
  1069. res = fat_dir_empty(new_inode);
  1070. if (res)
  1071. goto rename_done;
  1072. }
  1073. fat_detach(new_inode);
  1074. } else {
  1075. res = vfat_add_entry(new_dir,&new_dentry->d_name,is_dir,&sinfo,
  1076. &new_bh,&new_de);
  1077. if (res < 0) goto rename_done;
  1078. }
  1079. new_dir->i_version = ++event;
  1080. /* releases old_bh */
  1081. vfat_remove_entry(old_dir,&old_sinfo,old_bh,old_de);
  1082. old_bh=NULL;
  1083. fat_detach(old_inode);
  1084. fat_attach(old_inode, sinfo.i_pos);
  1085. mark_inode_dirty(old_inode);
  1086. old_dir->i_version = ++event;
  1087. old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
  1088. mark_inode_dirty(old_dir);
  1089. if (new_inode) {
  1090. new_inode->i_nlink--;
  1091. new_inode->i_ctime=CURRENT_TIME;
  1092. }
  1093. if (is_dir) {
  1094. int start = MSDOS_I(new_dir)->i_logstart;
  1095. dotdot_de->start = CT_LE_W(start);
  1096. dotdot_de->starthi = CT_LE_W(start>>16);
  1097. fat_mark_buffer_dirty(sb, dotdot_bh);
  1098. old_dir->i_nlink--;
  1099. if (new_inode) {
  1100. new_inode->i_nlink--;
  1101. } else {
  1102. new_dir->i_nlink++;
  1103. mark_inode_dirty(new_dir);
  1104. }
  1105. }
  1106. rename_done:
  1107. fat_brelse(sb, dotdot_bh);
  1108. fat_brelse(sb, old_bh);
  1109. fat_brelse(sb, new_bh);
  1110. return res;
  1111. }
  1112. /* Public inode operations for the VFAT fs */
  1113. struct inode_operations vfat_dir_inode_operations = {
  1114. create: vfat_create,
  1115. lookup: vfat_lookup,
  1116. unlink: vfat_unlink,
  1117. mkdir: vfat_mkdir,
  1118. rmdir: vfat_rmdir,
  1119. rename: vfat_rename,
  1120. setattr: fat_notify_change,
  1121. };
  1122. struct super_block *vfat_read_super(struct super_block *sb,void *data,
  1123. int silent)
  1124. {
  1125. struct super_block *res;
  1126. MSDOS_SB(sb)->options.isvfat = 1;
  1127. res = fat_read_super(sb, data, silent, &vfat_dir_inode_operations);
  1128. if (res == NULL)
  1129. return NULL;
  1130. if (parse_options((char *) data, &(MSDOS_SB(sb)->options))) {
  1131. MSDOS_SB(sb)->options.dotsOK = 0;
  1132. if (MSDOS_SB(sb)->options.posixfs) {
  1133. MSDOS_SB(sb)->options.name_check = 's';
  1134. }
  1135. if (MSDOS_SB(sb)->options.name_check != 's') {
  1136. sb->s_root->d_op = &vfat_dentry_ops[0];
  1137. } else {
  1138. sb->s_root->d_op = &vfat_dentry_ops[2];
  1139. }
  1140. }
  1141. return res;
  1142. }