namei_vfat.c 26 KB

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