namei_vfat.c 26 KB

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