namei_vfat.c 26 KB

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