namei_vfat.c 24 KB

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