namei_vfat.c 25 KB

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