PageRenderTime 29ms CodeModel.GetById 42ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/fat/namei_vfat.c

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