PageRenderTime 62ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/fat/namei_vfat.c

https://github.com/dmitriy103/bravo_kernel-2.6.35
C | 1101 lines | 1013 code | 22 blank | 66 comment | 31 complexity | ff9e4b255d9a1b295d0360435338ae7b 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 characters and cleanup by
  15. * OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
  16. */
  17. #include <linux/module.h>
  18. #include <linux/jiffies.h>
  19. #include <linux/ctype.h>
  20. #include <linux/slab.h>
  21. #include <linux/buffer_head.h>
  22. #include <linux/namei.h>
  23. #include "fat.h"
  24. /*
  25. * If new entry was created in the parent, it could create the 8.3
  26. * alias (the shortname of logname). So, the parent may have the
  27. * negative-dentry which matches the created 8.3 alias.
  28. *
  29. * If it happened, the negative dentry isn't actually negative
  30. * anymore. So, drop it.
  31. */
  32. static int vfat_revalidate_shortname(struct dentry *dentry)
  33. {
  34. int ret = 1;
  35. spin_lock(&dentry->d_lock);
  36. if (dentry->d_time != dentry->d_parent->d_inode->i_version)
  37. ret = 0;
  38. spin_unlock(&dentry->d_lock);
  39. return ret;
  40. }
  41. static int vfat_revalidate(struct dentry *dentry, struct nameidata *nd)
  42. {
  43. /* This is not negative dentry. Always valid. */
  44. if (dentry->d_inode)
  45. return 1;
  46. return vfat_revalidate_shortname(dentry);
  47. }
  48. static int vfat_revalidate_ci(struct dentry *dentry, struct nameidata *nd)
  49. {
  50. /*
  51. * This is not negative dentry. Always valid.
  52. *
  53. * Note, rename() to existing directory entry will have ->d_inode,
  54. * and will use existing name which isn't specified name by user.
  55. *
  56. * We may be able to drop this positive dentry here. But dropping
  57. * positive dentry isn't good idea. So it's unsupported like
  58. * rename("filename", "FILENAME") for now.
  59. */
  60. if (dentry->d_inode)
  61. return 1;
  62. /*
  63. * This may be nfsd (or something), anyway, we can't see the
  64. * intent of this. So, since this can be for creation, drop it.
  65. */
  66. if (!nd)
  67. return 0;
  68. /*
  69. * Drop the negative dentry, in order to make sure to use the
  70. * case sensitive name which is specified by user if this is
  71. * for creation.
  72. */
  73. if (!(nd->flags & (LOOKUP_CONTINUE | LOOKUP_PARENT))) {
  74. if (nd->flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
  75. return 0;
  76. }
  77. return vfat_revalidate_shortname(dentry);
  78. }
  79. /* returns the length of a struct qstr, ignoring trailing dots */
  80. static unsigned int vfat_striptail_len(struct qstr *qstr)
  81. {
  82. unsigned int len = qstr->len;
  83. while (len && qstr->name[len - 1] == '.')
  84. len--;
  85. return len;
  86. }
  87. /*
  88. * Compute the hash for the vfat name corresponding to the dentry.
  89. * Note: if the name is invalid, we leave the hash code unchanged so
  90. * that the existing dentry can be used. The vfat fs routines will
  91. * return ENOENT or EINVAL as appropriate.
  92. */
  93. static int vfat_hash(struct dentry *dentry, struct qstr *qstr)
  94. {
  95. qstr->hash = full_name_hash(qstr->name, vfat_striptail_len(qstr));
  96. return 0;
  97. }
  98. /*
  99. * Compute the hash for the vfat name corresponding to the dentry.
  100. * Note: if the name is invalid, we leave the hash code unchanged so
  101. * that the existing dentry can be used. The vfat fs routines will
  102. * return ENOENT or EINVAL as appropriate.
  103. */
  104. static int vfat_hashi(struct dentry *dentry, struct qstr *qstr)
  105. {
  106. struct nls_table *t = MSDOS_SB(dentry->d_inode->i_sb)->nls_io;
  107. const unsigned char *name;
  108. unsigned int len;
  109. unsigned long hash;
  110. name = qstr->name;
  111. len = vfat_striptail_len(qstr);
  112. hash = init_name_hash();
  113. while (len--)
  114. hash = partial_name_hash(nls_tolower(t, *name++), hash);
  115. qstr->hash = end_name_hash(hash);
  116. return 0;
  117. }
  118. /*
  119. * Case insensitive compare of two vfat names.
  120. */
  121. static int vfat_cmpi(struct dentry *dentry, struct qstr *a, struct qstr *b)
  122. {
  123. struct nls_table *t = MSDOS_SB(dentry->d_inode->i_sb)->nls_io;
  124. unsigned int alen, blen;
  125. /* A filename cannot end in '.' or we treat it like it has none */
  126. alen = vfat_striptail_len(a);
  127. blen = vfat_striptail_len(b);
  128. if (alen == blen) {
  129. if (nls_strnicmp(t, a->name, b->name, alen) == 0)
  130. return 0;
  131. }
  132. return 1;
  133. }
  134. /*
  135. * Case sensitive compare of two vfat names.
  136. */
  137. static int vfat_cmp(struct dentry *dentry, struct qstr *a, struct qstr *b)
  138. {
  139. unsigned int alen, blen;
  140. /* A filename cannot end in '.' or we treat it like it has none */
  141. alen = vfat_striptail_len(a);
  142. blen = vfat_striptail_len(b);
  143. if (alen == blen) {
  144. if (strncmp(a->name, b->name, alen) == 0)
  145. return 0;
  146. }
  147. return 1;
  148. }
  149. static const struct dentry_operations vfat_ci_dentry_ops = {
  150. .d_revalidate = vfat_revalidate_ci,
  151. .d_hash = vfat_hashi,
  152. .d_compare = vfat_cmpi,
  153. };
  154. static const struct dentry_operations vfat_dentry_ops = {
  155. .d_revalidate = vfat_revalidate,
  156. .d_hash = vfat_hash,
  157. .d_compare = vfat_cmp,
  158. };
  159. /* Characters that are undesirable in an MS-DOS file name */
  160. static inline wchar_t vfat_bad_char(wchar_t w)
  161. {
  162. return (w < 0x0020)
  163. || (w == '*') || (w == '?') || (w == '<') || (w == '>')
  164. || (w == '|') || (w == '"') || (w == ':') || (w == '/')
  165. || (w == '\\');
  166. }
  167. static inline wchar_t vfat_replace_char(wchar_t w)
  168. {
  169. return (w == '[') || (w == ']') || (w == ';') || (w == ',')
  170. || (w == '+') || (w == '=');
  171. }
  172. static wchar_t vfat_skip_char(wchar_t w)
  173. {
  174. return (w == '.') || (w == ' ');
  175. }
  176. static inline int vfat_is_used_badchars(const wchar_t *s, int len)
  177. {
  178. int i;
  179. for (i = 0; i < len; i++)
  180. if (vfat_bad_char(s[i]))
  181. return -EINVAL;
  182. if (s[i - 1] == ' ') /* last character cannot be space */
  183. return -EINVAL;
  184. return 0;
  185. }
  186. static int vfat_find_form(struct inode *dir, unsigned char *name)
  187. {
  188. struct fat_slot_info sinfo;
  189. int err = fat_scan(dir, name, &sinfo);
  190. if (err)
  191. return -ENOENT;
  192. brelse(sinfo.bh);
  193. return 0;
  194. }
  195. /*
  196. * 1) Valid characters for the 8.3 format alias are any combination of
  197. * letters, uppercase alphabets, digits, any of the
  198. * following special characters:
  199. * $ % ' ` - @ { } ~ ! # ( ) & _ ^
  200. * In this case Longfilename is not stored in disk.
  201. *
  202. * WinNT's Extension:
  203. * File name and extension name is contain uppercase/lowercase
  204. * only. And it is expressed by CASE_LOWER_BASE and CASE_LOWER_EXT.
  205. *
  206. * 2) File name is 8.3 format, but it contain the uppercase and
  207. * lowercase char, muliti bytes char, etc. In this case numtail is not
  208. * added, but Longfilename is stored.
  209. *
  210. * 3) When the one except for the above, or the following special
  211. * character are contained:
  212. * . [ ] ; , + =
  213. * numtail is added, and Longfilename must be stored in disk .
  214. */
  215. struct shortname_info {
  216. unsigned char lower:1,
  217. upper:1,
  218. valid:1;
  219. };
  220. #define INIT_SHORTNAME_INFO(x) do { \
  221. (x)->lower = 1; \
  222. (x)->upper = 1; \
  223. (x)->valid = 1; \
  224. } while (0)
  225. static inline int to_shortname_char(struct nls_table *nls,
  226. unsigned char *buf, int buf_size,
  227. wchar_t *src, struct shortname_info *info)
  228. {
  229. int len;
  230. if (vfat_skip_char(*src)) {
  231. info->valid = 0;
  232. return 0;
  233. }
  234. if (vfat_replace_char(*src)) {
  235. info->valid = 0;
  236. buf[0] = '_';
  237. return 1;
  238. }
  239. len = nls->uni2char(*src, buf, buf_size);
  240. if (len <= 0) {
  241. info->valid = 0;
  242. buf[0] = '_';
  243. len = 1;
  244. } else if (len == 1) {
  245. unsigned char prev = buf[0];
  246. if (buf[0] >= 0x7F) {
  247. info->lower = 0;
  248. info->upper = 0;
  249. }
  250. buf[0] = nls_toupper(nls, buf[0]);
  251. if (isalpha(buf[0])) {
  252. if (buf[0] == prev)
  253. info->lower = 0;
  254. else
  255. info->upper = 0;
  256. }
  257. } else {
  258. info->lower = 0;
  259. info->upper = 0;
  260. }
  261. return len;
  262. }
  263. /*
  264. * Given a valid longname, create a unique shortname. Make sure the
  265. * shortname does not exist
  266. * Returns negative number on error, 0 for a normal
  267. * return, and 1 for valid shortname
  268. */
  269. static int vfat_create_shortname(struct inode *dir, struct nls_table *nls,
  270. wchar_t *uname, int ulen,
  271. unsigned char *name_res, unsigned char *lcase)
  272. {
  273. struct fat_mount_options *opts = &MSDOS_SB(dir->i_sb)->options;
  274. wchar_t *ip, *ext_start, *end, *name_start;
  275. unsigned char base[9], ext[4], buf[5], *p;
  276. unsigned char charbuf[NLS_MAX_CHARSET_SIZE];
  277. int chl, chi;
  278. int sz = 0, extlen, baselen, i, numtail_baselen, numtail2_baselen;
  279. int is_shortname;
  280. struct shortname_info base_info, ext_info;
  281. is_shortname = 1;
  282. INIT_SHORTNAME_INFO(&base_info);
  283. INIT_SHORTNAME_INFO(&ext_info);
  284. /* Now, we need to create a shortname from the long name */
  285. ext_start = end = &uname[ulen];
  286. while (--ext_start >= uname) {
  287. if (*ext_start == 0x002E) { /* is `.' */
  288. if (ext_start == end - 1) {
  289. sz = ulen;
  290. ext_start = NULL;
  291. }
  292. break;
  293. }
  294. }
  295. if (ext_start == uname - 1) {
  296. sz = ulen;
  297. ext_start = NULL;
  298. } else if (ext_start) {
  299. /*
  300. * Names which start with a dot could be just
  301. * an extension eg. "...test". In this case Win95
  302. * uses the extension as the name and sets no extension.
  303. */
  304. name_start = &uname[0];
  305. while (name_start < ext_start) {
  306. if (!vfat_skip_char(*name_start))
  307. break;
  308. name_start++;
  309. }
  310. if (name_start != ext_start) {
  311. sz = ext_start - uname;
  312. ext_start++;
  313. } else {
  314. sz = ulen;
  315. ext_start = NULL;
  316. }
  317. }
  318. numtail_baselen = 6;
  319. numtail2_baselen = 2;
  320. for (baselen = i = 0, p = base, ip = uname; i < sz; i++, ip++) {
  321. chl = to_shortname_char(nls, charbuf, sizeof(charbuf),
  322. ip, &base_info);
  323. if (chl == 0)
  324. continue;
  325. if (baselen < 2 && (baselen + chl) > 2)
  326. numtail2_baselen = baselen;
  327. if (baselen < 6 && (baselen + chl) > 6)
  328. numtail_baselen = baselen;
  329. for (chi = 0; chi < chl; chi++) {
  330. *p++ = charbuf[chi];
  331. baselen++;
  332. if (baselen >= 8)
  333. break;
  334. }
  335. if (baselen >= 8) {
  336. if ((chi < chl - 1) || (ip + 1) - uname < sz)
  337. is_shortname = 0;
  338. break;
  339. }
  340. }
  341. if (baselen == 0) {
  342. return -EINVAL;
  343. }
  344. extlen = 0;
  345. if (ext_start) {
  346. for (p = ext, ip = ext_start; extlen < 3 && ip < end; ip++) {
  347. chl = to_shortname_char(nls, charbuf, sizeof(charbuf),
  348. ip, &ext_info);
  349. if (chl == 0)
  350. continue;
  351. if ((extlen + chl) > 3) {
  352. is_shortname = 0;
  353. break;
  354. }
  355. for (chi = 0; chi < chl; chi++) {
  356. *p++ = charbuf[chi];
  357. extlen++;
  358. }
  359. if (extlen >= 3) {
  360. if (ip + 1 != end)
  361. is_shortname = 0;
  362. break;
  363. }
  364. }
  365. }
  366. ext[extlen] = '\0';
  367. base[baselen] = '\0';
  368. /* Yes, it can happen. ".\xe5" would do it. */
  369. if (base[0] == DELETED_FLAG)
  370. base[0] = 0x05;
  371. /* OK, at this point we know that base is not longer than 8 symbols,
  372. * ext is not longer than 3, base is nonempty, both don't contain
  373. * any bad symbols (lowercase transformed to uppercase).
  374. */
  375. memset(name_res, ' ', MSDOS_NAME);
  376. memcpy(name_res, base, baselen);
  377. memcpy(name_res + 8, ext, extlen);
  378. *lcase = 0;
  379. if (is_shortname && base_info.valid && ext_info.valid) {
  380. if (vfat_find_form(dir, name_res) == 0)
  381. return -EEXIST;
  382. if (opts->shortname & VFAT_SFN_CREATE_WIN95) {
  383. return (base_info.upper && ext_info.upper);
  384. } else if (opts->shortname & VFAT_SFN_CREATE_WINNT) {
  385. if ((base_info.upper || base_info.lower) &&
  386. (ext_info.upper || ext_info.lower)) {
  387. if (!base_info.upper && base_info.lower)
  388. *lcase |= CASE_LOWER_BASE;
  389. if (!ext_info.upper && ext_info.lower)
  390. *lcase |= CASE_LOWER_EXT;
  391. return 1;
  392. }
  393. return 0;
  394. } else {
  395. BUG();
  396. }
  397. }
  398. if (opts->numtail == 0)
  399. if (vfat_find_form(dir, name_res) < 0)
  400. return 0;
  401. /*
  402. * Try to find a unique extension. This used to
  403. * iterate through all possibilities sequentially,
  404. * but that gave extremely bad performance. Windows
  405. * only tries a few cases before using random
  406. * values for part of the base.
  407. */
  408. if (baselen > 6) {
  409. baselen = numtail_baselen;
  410. name_res[7] = ' ';
  411. }
  412. name_res[baselen] = '~';
  413. for (i = 1; i < 10; i++) {
  414. name_res[baselen + 1] = i + '0';
  415. if (vfat_find_form(dir, name_res) < 0)
  416. return 0;
  417. }
  418. i = jiffies;
  419. sz = (jiffies >> 16) & 0x7;
  420. if (baselen > 2) {
  421. baselen = numtail2_baselen;
  422. name_res[7] = ' ';
  423. }
  424. name_res[baselen + 4] = '~';
  425. name_res[baselen + 5] = '1' + sz;
  426. while (1) {
  427. snprintf(buf, sizeof(buf), "%04X", i & 0xffff);
  428. memcpy(&name_res[baselen], buf, 4);
  429. if (vfat_find_form(dir, name_res) < 0)
  430. break;
  431. i -= 11;
  432. }
  433. return 0;
  434. }
  435. /* Translate a string, including coded sequences into Unicode */
  436. static int
  437. xlate_to_uni(const unsigned char *name, int len, unsigned char *outname,
  438. int *longlen, int *outlen, int escape, int utf8,
  439. struct nls_table *nls)
  440. {
  441. const unsigned char *ip;
  442. unsigned char nc;
  443. unsigned char *op;
  444. unsigned int ec;
  445. int i, k, fill;
  446. int charlen;
  447. if (utf8) {
  448. *outlen = utf8s_to_utf16s(name, len, (wchar_t *)outname);
  449. if (*outlen < 0)
  450. return *outlen;
  451. else if (*outlen > FAT_LFN_LEN)
  452. return -ENAMETOOLONG;
  453. op = &outname[*outlen * sizeof(wchar_t)];
  454. } else {
  455. if (nls) {
  456. for (i = 0, ip = name, op = outname, *outlen = 0;
  457. i < len && *outlen <= FAT_LFN_LEN;
  458. *outlen += 1)
  459. {
  460. if (escape && (*ip == ':')) {
  461. if (i > len - 5)
  462. return -EINVAL;
  463. ec = 0;
  464. for (k = 1; k < 5; k++) {
  465. nc = ip[k];
  466. ec <<= 4;
  467. if (nc >= '0' && nc <= '9') {
  468. ec |= nc - '0';
  469. continue;
  470. }
  471. if (nc >= 'a' && nc <= 'f') {
  472. ec |= nc - ('a' - 10);
  473. continue;
  474. }
  475. if (nc >= 'A' && nc <= 'F') {
  476. ec |= nc - ('A' - 10);
  477. continue;
  478. }
  479. return -EINVAL;
  480. }
  481. *op++ = ec & 0xFF;
  482. *op++ = ec >> 8;
  483. ip += 5;
  484. i += 5;
  485. } else {
  486. if ((charlen = nls->char2uni(ip, len - i, (wchar_t *)op)) < 0)
  487. return -EINVAL;
  488. ip += charlen;
  489. i += charlen;
  490. op += 2;
  491. }
  492. }
  493. if (i < len)
  494. return -ENAMETOOLONG;
  495. } else {
  496. for (i = 0, ip = name, op = outname, *outlen = 0;
  497. i < len && *outlen <= FAT_LFN_LEN;
  498. i++, *outlen += 1)
  499. {
  500. *op++ = *ip++;
  501. *op++ = 0;
  502. }
  503. if (i < len)
  504. return -ENAMETOOLONG;
  505. }
  506. }
  507. *longlen = *outlen;
  508. if (*outlen % 13) {
  509. *op++ = 0;
  510. *op++ = 0;
  511. *outlen += 1;
  512. if (*outlen % 13) {
  513. fill = 13 - (*outlen % 13);
  514. for (i = 0; i < fill; i++) {
  515. *op++ = 0xff;
  516. *op++ = 0xff;
  517. }
  518. *outlen += fill;
  519. }
  520. }
  521. return 0;
  522. }
  523. static int vfat_build_slots(struct inode *dir, const unsigned char *name,
  524. int len, int is_dir, int cluster,
  525. struct timespec *ts,
  526. struct msdos_dir_slot *slots, int *nr_slots)
  527. {
  528. struct msdos_sb_info *sbi = MSDOS_SB(dir->i_sb);
  529. struct fat_mount_options *opts = &sbi->options;
  530. struct msdos_dir_slot *ps;
  531. struct msdos_dir_entry *de;
  532. unsigned char cksum, lcase;
  533. unsigned char msdos_name[MSDOS_NAME];
  534. wchar_t *uname;
  535. __le16 time, date;
  536. u8 time_cs;
  537. int err, ulen, usize, i;
  538. loff_t offset;
  539. *nr_slots = 0;
  540. uname = __getname();
  541. if (!uname)
  542. return -ENOMEM;
  543. err = xlate_to_uni(name, len, (unsigned char *)uname, &ulen, &usize,
  544. opts->unicode_xlate, opts->utf8, sbi->nls_io);
  545. if (err)
  546. goto out_free;
  547. err = vfat_is_used_badchars(uname, ulen);
  548. if (err)
  549. goto out_free;
  550. err = vfat_create_shortname(dir, sbi->nls_disk, uname, ulen,
  551. msdos_name, &lcase);
  552. if (err < 0)
  553. goto out_free;
  554. else if (err == 1) {
  555. de = (struct msdos_dir_entry *)slots;
  556. err = 0;
  557. goto shortname;
  558. }
  559. /* build the entry of long file name */
  560. cksum = fat_checksum(msdos_name);
  561. *nr_slots = usize / 13;
  562. for (ps = slots, i = *nr_slots; i > 0; i--, ps++) {
  563. ps->id = i;
  564. ps->attr = ATTR_EXT;
  565. ps->reserved = 0;
  566. ps->alias_checksum = cksum;
  567. ps->start = 0;
  568. offset = (i - 1) * 13;
  569. fatwchar_to16(ps->name0_4, uname + offset, 5);
  570. fatwchar_to16(ps->name5_10, uname + offset + 5, 6);
  571. fatwchar_to16(ps->name11_12, uname + offset + 11, 2);
  572. }
  573. slots[0].id |= 0x40;
  574. de = (struct msdos_dir_entry *)ps;
  575. shortname:
  576. /* build the entry of 8.3 alias name */
  577. (*nr_slots)++;
  578. memcpy(de->name, msdos_name, MSDOS_NAME);
  579. de->attr = is_dir ? ATTR_DIR : ATTR_ARCH;
  580. de->lcase = lcase;
  581. fat_time_unix2fat(sbi, ts, &time, &date, &time_cs);
  582. de->time = de->ctime = time;
  583. de->date = de->cdate = de->adate = date;
  584. de->ctime_cs = time_cs;
  585. de->start = cpu_to_le16(cluster);
  586. de->starthi = cpu_to_le16(cluster >> 16);
  587. de->size = 0;
  588. out_free:
  589. __putname(uname);
  590. return err;
  591. }
  592. static int vfat_add_entry(struct inode *dir, struct qstr *qname, int is_dir,
  593. int cluster, struct timespec *ts,
  594. struct fat_slot_info *sinfo)
  595. {
  596. struct msdos_dir_slot *slots;
  597. unsigned int len;
  598. int err, nr_slots;
  599. len = vfat_striptail_len(qname);
  600. if (len == 0)
  601. return -ENOENT;
  602. slots = kmalloc(sizeof(*slots) * MSDOS_SLOTS, GFP_NOFS);
  603. if (slots == NULL)
  604. return -ENOMEM;
  605. err = vfat_build_slots(dir, qname->name, len, is_dir, cluster, ts,
  606. slots, &nr_slots);
  607. if (err)
  608. goto cleanup;
  609. err = fat_add_entries(dir, slots, nr_slots, sinfo);
  610. if (err)
  611. goto cleanup;
  612. /* update timestamp */
  613. dir->i_ctime = dir->i_mtime = dir->i_atime = *ts;
  614. if (IS_DIRSYNC(dir))
  615. (void)fat_sync_inode(dir);
  616. else
  617. mark_inode_dirty(dir);
  618. cleanup:
  619. kfree(slots);
  620. return err;
  621. }
  622. static int vfat_find(struct inode *dir, struct qstr *qname,
  623. struct fat_slot_info *sinfo)
  624. {
  625. unsigned int len = vfat_striptail_len(qname);
  626. if (len == 0)
  627. return -ENOENT;
  628. return fat_search_long(dir, qname->name, len, sinfo);
  629. }
  630. /*
  631. * (nfsd's) anonymous disconnected dentry?
  632. * NOTE: !IS_ROOT() is not anonymous (I.e. d_splice_alias() did the job).
  633. */
  634. static int vfat_d_anon_disconn(struct dentry *dentry)
  635. {
  636. return IS_ROOT(dentry) && (dentry->d_flags & DCACHE_DISCONNECTED);
  637. }
  638. static struct dentry *vfat_lookup(struct inode *dir, struct dentry *dentry,
  639. struct nameidata *nd)
  640. {
  641. struct super_block *sb = dir->i_sb;
  642. struct fat_slot_info sinfo;
  643. struct inode *inode;
  644. struct dentry *alias;
  645. int err;
  646. lock_super(sb);
  647. err = vfat_find(dir, &dentry->d_name, &sinfo);
  648. if (err) {
  649. if (err == -ENOENT) {
  650. inode = NULL;
  651. goto out;
  652. }
  653. goto error;
  654. }
  655. inode = fat_build_inode(sb, sinfo.de, sinfo.i_pos);
  656. brelse(sinfo.bh);
  657. if (IS_ERR(inode)) {
  658. err = PTR_ERR(inode);
  659. goto error;
  660. }
  661. alias = d_find_alias(inode);
  662. if (alias && !vfat_d_anon_disconn(alias)) {
  663. /*
  664. * This inode has non anonymous-DCACHE_DISCONNECTED
  665. * dentry. This means, the user did ->lookup() by an
  666. * another name (longname vs 8.3 alias of it) in past.
  667. *
  668. * Switch to new one for reason of locality if possible.
  669. */
  670. BUG_ON(d_unhashed(alias));
  671. if (!S_ISDIR(inode->i_mode))
  672. d_move(alias, dentry);
  673. iput(inode);
  674. unlock_super(sb);
  675. return alias;
  676. } else
  677. dput(alias);
  678. out:
  679. unlock_super(sb);
  680. dentry->d_op = sb->s_root->d_op;
  681. dentry->d_time = dentry->d_parent->d_inode->i_version;
  682. dentry = d_splice_alias(inode, dentry);
  683. if (dentry) {
  684. dentry->d_op = sb->s_root->d_op;
  685. dentry->d_time = dentry->d_parent->d_inode->i_version;
  686. }
  687. return dentry;
  688. error:
  689. unlock_super(sb);
  690. return ERR_PTR(err);
  691. }
  692. static int vfat_create(struct inode *dir, struct dentry *dentry, int mode,
  693. struct nameidata *nd)
  694. {
  695. struct super_block *sb = dir->i_sb;
  696. struct inode *inode;
  697. struct fat_slot_info sinfo;
  698. struct timespec ts;
  699. int err;
  700. lock_super(sb);
  701. ts = CURRENT_TIME_SEC;
  702. err = vfat_add_entry(dir, &dentry->d_name, 0, 0, &ts, &sinfo);
  703. if (err)
  704. goto out;
  705. dir->i_version++;
  706. inode = fat_build_inode(sb, sinfo.de, sinfo.i_pos);
  707. brelse(sinfo.bh);
  708. if (IS_ERR(inode)) {
  709. err = PTR_ERR(inode);
  710. goto out;
  711. }
  712. inode->i_version++;
  713. inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
  714. /* timestamp is already written, so mark_inode_dirty() is unneeded. */
  715. dentry->d_time = dentry->d_parent->d_inode->i_version;
  716. d_instantiate(dentry, inode);
  717. out:
  718. unlock_super(sb);
  719. return err;
  720. }
  721. static int vfat_rmdir(struct inode *dir, struct dentry *dentry)
  722. {
  723. struct inode *inode = dentry->d_inode;
  724. struct super_block *sb = dir->i_sb;
  725. struct fat_slot_info sinfo;
  726. int err;
  727. lock_super(sb);
  728. err = fat_dir_empty(inode);
  729. if (err)
  730. goto out;
  731. err = vfat_find(dir, &dentry->d_name, &sinfo);
  732. if (err)
  733. goto out;
  734. err = fat_remove_entries(dir, &sinfo); /* and releases bh */
  735. if (err)
  736. goto out;
  737. drop_nlink(dir);
  738. clear_nlink(inode);
  739. inode->i_mtime = inode->i_atime = CURRENT_TIME_SEC;
  740. fat_detach(inode);
  741. out:
  742. unlock_super(sb);
  743. return err;
  744. }
  745. static int vfat_unlink(struct inode *dir, struct dentry *dentry)
  746. {
  747. struct inode *inode = dentry->d_inode;
  748. struct super_block *sb = dir->i_sb;
  749. struct fat_slot_info sinfo;
  750. int err;
  751. lock_super(sb);
  752. err = vfat_find(dir, &dentry->d_name, &sinfo);
  753. if (err)
  754. goto out;
  755. err = fat_remove_entries(dir, &sinfo); /* and releases bh */
  756. if (err)
  757. goto out;
  758. clear_nlink(inode);
  759. inode->i_mtime = inode->i_atime = CURRENT_TIME_SEC;
  760. fat_detach(inode);
  761. out:
  762. unlock_super(sb);
  763. return err;
  764. }
  765. static int vfat_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  766. {
  767. struct super_block *sb = dir->i_sb;
  768. struct inode *inode;
  769. struct fat_slot_info sinfo;
  770. struct timespec ts;
  771. int err, cluster;
  772. lock_super(sb);
  773. ts = CURRENT_TIME_SEC;
  774. cluster = fat_alloc_new_dir(dir, &ts);
  775. if (cluster < 0) {
  776. err = cluster;
  777. goto out;
  778. }
  779. err = vfat_add_entry(dir, &dentry->d_name, 1, cluster, &ts, &sinfo);
  780. if (err)
  781. goto out_free;
  782. dir->i_version++;
  783. inc_nlink(dir);
  784. inode = fat_build_inode(sb, sinfo.de, sinfo.i_pos);
  785. brelse(sinfo.bh);
  786. if (IS_ERR(inode)) {
  787. err = PTR_ERR(inode);
  788. /* the directory was completed, just return a error */
  789. goto out;
  790. }
  791. inode->i_version++;
  792. inode->i_nlink = 2;
  793. inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
  794. /* timestamp is already written, so mark_inode_dirty() is unneeded. */
  795. dentry->d_time = dentry->d_parent->d_inode->i_version;
  796. d_instantiate(dentry, inode);
  797. unlock_super(sb);
  798. return 0;
  799. out_free:
  800. fat_free_clusters(dir, cluster);
  801. out:
  802. unlock_super(sb);
  803. return err;
  804. }
  805. static int vfat_rename(struct inode *old_dir, struct dentry *old_dentry,
  806. struct inode *new_dir, struct dentry *new_dentry)
  807. {
  808. struct buffer_head *dotdot_bh;
  809. struct msdos_dir_entry *dotdot_de;
  810. struct inode *old_inode, *new_inode;
  811. struct fat_slot_info old_sinfo, sinfo;
  812. struct timespec ts;
  813. loff_t dotdot_i_pos, new_i_pos;
  814. int err, is_dir, update_dotdot, corrupt = 0;
  815. struct super_block *sb = old_dir->i_sb;
  816. old_sinfo.bh = sinfo.bh = dotdot_bh = NULL;
  817. old_inode = old_dentry->d_inode;
  818. new_inode = new_dentry->d_inode;
  819. lock_super(sb);
  820. err = vfat_find(old_dir, &old_dentry->d_name, &old_sinfo);
  821. if (err)
  822. goto out;
  823. is_dir = S_ISDIR(old_inode->i_mode);
  824. update_dotdot = (is_dir && old_dir != new_dir);
  825. if (update_dotdot) {
  826. if (fat_get_dotdot_entry(old_inode, &dotdot_bh, &dotdot_de,
  827. &dotdot_i_pos) < 0) {
  828. err = -EIO;
  829. goto out;
  830. }
  831. }
  832. ts = CURRENT_TIME_SEC;
  833. if (new_inode) {
  834. if (is_dir) {
  835. err = fat_dir_empty(new_inode);
  836. if (err)
  837. goto out;
  838. }
  839. new_i_pos = MSDOS_I(new_inode)->i_pos;
  840. fat_detach(new_inode);
  841. } else {
  842. err = vfat_add_entry(new_dir, &new_dentry->d_name, is_dir, 0,
  843. &ts, &sinfo);
  844. if (err)
  845. goto out;
  846. new_i_pos = sinfo.i_pos;
  847. }
  848. new_dir->i_version++;
  849. fat_detach(old_inode);
  850. fat_attach(old_inode, new_i_pos);
  851. if (IS_DIRSYNC(new_dir)) {
  852. err = fat_sync_inode(old_inode);
  853. if (err)
  854. goto error_inode;
  855. } else
  856. mark_inode_dirty(old_inode);
  857. if (update_dotdot) {
  858. int start = MSDOS_I(new_dir)->i_logstart;
  859. dotdot_de->start = cpu_to_le16(start);
  860. dotdot_de->starthi = cpu_to_le16(start >> 16);
  861. mark_buffer_dirty_inode(dotdot_bh, old_inode);
  862. if (IS_DIRSYNC(new_dir)) {
  863. err = sync_dirty_buffer(dotdot_bh);
  864. if (err)
  865. goto error_dotdot;
  866. }
  867. drop_nlink(old_dir);
  868. if (!new_inode)
  869. inc_nlink(new_dir);
  870. }
  871. err = fat_remove_entries(old_dir, &old_sinfo); /* and releases bh */
  872. old_sinfo.bh = NULL;
  873. if (err)
  874. goto error_dotdot;
  875. old_dir->i_version++;
  876. old_dir->i_ctime = old_dir->i_mtime = ts;
  877. if (IS_DIRSYNC(old_dir))
  878. (void)fat_sync_inode(old_dir);
  879. else
  880. mark_inode_dirty(old_dir);
  881. if (new_inode) {
  882. drop_nlink(new_inode);
  883. if (is_dir)
  884. drop_nlink(new_inode);
  885. new_inode->i_ctime = ts;
  886. }
  887. out:
  888. brelse(sinfo.bh);
  889. brelse(dotdot_bh);
  890. brelse(old_sinfo.bh);
  891. unlock_super(sb);
  892. return err;
  893. error_dotdot:
  894. /* data cluster is shared, serious corruption */
  895. corrupt = 1;
  896. if (update_dotdot) {
  897. int start = MSDOS_I(old_dir)->i_logstart;
  898. dotdot_de->start = cpu_to_le16(start);
  899. dotdot_de->starthi = cpu_to_le16(start >> 16);
  900. mark_buffer_dirty_inode(dotdot_bh, old_inode);
  901. corrupt |= sync_dirty_buffer(dotdot_bh);
  902. }
  903. error_inode:
  904. fat_detach(old_inode);
  905. fat_attach(old_inode, old_sinfo.i_pos);
  906. if (new_inode) {
  907. fat_attach(new_inode, new_i_pos);
  908. if (corrupt)
  909. corrupt |= fat_sync_inode(new_inode);
  910. } else {
  911. /*
  912. * If new entry was not sharing the data cluster, it
  913. * shouldn't be serious corruption.
  914. */
  915. int err2 = fat_remove_entries(new_dir, &sinfo);
  916. if (corrupt)
  917. corrupt |= err2;
  918. sinfo.bh = NULL;
  919. }
  920. if (corrupt < 0) {
  921. fat_fs_error(new_dir->i_sb,
  922. "%s: Filesystem corrupted (i_pos %lld)",
  923. __func__, sinfo.i_pos);
  924. }
  925. goto out;
  926. }
  927. static const struct inode_operations vfat_dir_inode_operations = {
  928. .create = vfat_create,
  929. .lookup = vfat_lookup,
  930. .unlink = vfat_unlink,
  931. .mkdir = vfat_mkdir,
  932. .rmdir = vfat_rmdir,
  933. .rename = vfat_rename,
  934. .setattr = fat_setattr,
  935. .getattr = fat_getattr,
  936. };
  937. static int vfat_fill_super(struct super_block *sb, void *data, int silent)
  938. {
  939. int res;
  940. res = fat_fill_super(sb, data, silent, &vfat_dir_inode_operations, 1);
  941. if (res)
  942. return res;
  943. if (MSDOS_SB(sb)->options.name_check != 's')
  944. sb->s_root->d_op = &vfat_ci_dentry_ops;
  945. else
  946. sb->s_root->d_op = &vfat_dentry_ops;
  947. return 0;
  948. }
  949. static int vfat_get_sb(struct file_system_type *fs_type,
  950. int flags, const char *dev_name,
  951. void *data, struct vfsmount *mnt)
  952. {
  953. return get_sb_bdev(fs_type, flags, dev_name, data, vfat_fill_super,
  954. mnt);
  955. }
  956. static struct file_system_type vfat_fs_type = {
  957. .owner = THIS_MODULE,
  958. .name = "vfat",
  959. .get_sb = vfat_get_sb,
  960. .kill_sb = kill_block_super,
  961. .fs_flags = FS_REQUIRES_DEV,
  962. };
  963. static int __init init_vfat_fs(void)
  964. {
  965. return register_filesystem(&vfat_fs_type);
  966. }
  967. static void __exit exit_vfat_fs(void)
  968. {
  969. unregister_filesystem(&vfat_fs_type);
  970. }
  971. MODULE_LICENSE("GPL");
  972. MODULE_DESCRIPTION("VFAT filesystem support");
  973. MODULE_AUTHOR("Gordon Chaffee");
  974. module_init(init_vfat_fs)
  975. module_exit(exit_vfat_fs)