namei.c 24 KB

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