namei_vfat.c 25 KB

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