namei_vfat.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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 | LOOKUP_RENAME_TARGET))
  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. BUG_ON(d_unhashed(alias));
  668. if (!S_ISDIR(inode->i_mode))
  669. d_move(alias, dentry);
  670. iput(inode);
  671. unlock_super(sb);
  672. return alias;
  673. }
  674. out:
  675. unlock_super(sb);
  676. dentry->d_op = sb->s_root->d_op;
  677. dentry->d_time = dentry->d_parent->d_inode->i_version;
  678. dentry = d_splice_alias(inode, dentry);
  679. if (dentry) {
  680. dentry->d_op = sb->s_root->d_op;
  681. dentry->d_time = dentry->d_parent->d_inode->i_version;
  682. }
  683. return dentry;
  684. error:
  685. unlock_super(sb);
  686. return ERR_PTR(err);
  687. }
  688. static int vfat_create(struct inode *dir, struct dentry *dentry, int mode,
  689. struct nameidata *nd)
  690. {
  691. struct super_block *sb = dir->i_sb;
  692. struct inode *inode;
  693. struct fat_slot_info sinfo;
  694. struct timespec ts;
  695. int err;
  696. lock_super(sb);
  697. ts = CURRENT_TIME_SEC;
  698. err = vfat_add_entry(dir, &dentry->d_name, 0, 0, &ts, &sinfo);
  699. if (err)
  700. goto out;
  701. dir->i_version++;
  702. inode = fat_build_inode(sb, sinfo.de, sinfo.i_pos);
  703. brelse(sinfo.bh);
  704. if (IS_ERR(inode)) {
  705. err = PTR_ERR(inode);
  706. goto out;
  707. }
  708. inode->i_version++;
  709. inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
  710. /* timestamp is already written, so mark_inode_dirty() is unneeded. */
  711. dentry->d_time = dentry->d_parent->d_inode->i_version;
  712. d_instantiate(dentry, inode);
  713. out:
  714. unlock_super(sb);
  715. return err;
  716. }
  717. static int vfat_rmdir(struct inode *dir, struct dentry *dentry)
  718. {
  719. struct inode *inode = dentry->d_inode;
  720. struct super_block *sb = dir->i_sb;
  721. struct fat_slot_info sinfo;
  722. int err;
  723. lock_super(sb);
  724. err = fat_dir_empty(inode);
  725. if (err)
  726. goto out;
  727. err = vfat_find(dir, &dentry->d_name, &sinfo);
  728. if (err)
  729. goto out;
  730. err = fat_remove_entries(dir, &sinfo); /* and releases bh */
  731. if (err)
  732. goto out;
  733. drop_nlink(dir);
  734. clear_nlink(inode);
  735. inode->i_mtime = inode->i_atime = CURRENT_TIME_SEC;
  736. fat_detach(inode);
  737. out:
  738. unlock_super(sb);
  739. return err;
  740. }
  741. static int vfat_unlink(struct inode *dir, struct dentry *dentry)
  742. {
  743. struct inode *inode = dentry->d_inode;
  744. struct super_block *sb = dir->i_sb;
  745. struct fat_slot_info sinfo;
  746. int err;
  747. lock_super(sb);
  748. err = vfat_find(dir, &dentry->d_name, &sinfo);
  749. if (err)
  750. goto out;
  751. err = fat_remove_entries(dir, &sinfo); /* and releases bh */
  752. if (err)
  753. goto out;
  754. clear_nlink(inode);
  755. inode->i_mtime = inode->i_atime = CURRENT_TIME_SEC;
  756. fat_detach(inode);
  757. out:
  758. unlock_super(sb);
  759. return err;
  760. }
  761. static int vfat_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  762. {
  763. struct super_block *sb = dir->i_sb;
  764. struct inode *inode;
  765. struct fat_slot_info sinfo;
  766. struct timespec ts;
  767. int err, cluster;
  768. lock_super(sb);
  769. ts = CURRENT_TIME_SEC;
  770. cluster = fat_alloc_new_dir(dir, &ts);
  771. if (cluster < 0) {
  772. err = cluster;
  773. goto out;
  774. }
  775. err = vfat_add_entry(dir, &dentry->d_name, 1, cluster, &ts, &sinfo);
  776. if (err)
  777. goto out_free;
  778. dir->i_version++;
  779. inc_nlink(dir);
  780. inode = fat_build_inode(sb, sinfo.de, sinfo.i_pos);
  781. brelse(sinfo.bh);
  782. if (IS_ERR(inode)) {
  783. err = PTR_ERR(inode);
  784. /* the directory was completed, just return a error */
  785. goto out;
  786. }
  787. inode->i_version++;
  788. inode->i_nlink = 2;
  789. inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
  790. /* timestamp is already written, so mark_inode_dirty() is unneeded. */
  791. dentry->d_time = dentry->d_parent->d_inode->i_version;
  792. d_instantiate(dentry, inode);
  793. unlock_super(sb);
  794. return 0;
  795. out_free:
  796. fat_free_clusters(dir, cluster);
  797. out:
  798. unlock_super(sb);
  799. return err;
  800. }
  801. static int vfat_rename(struct inode *old_dir, struct dentry *old_dentry,
  802. struct inode *new_dir, struct dentry *new_dentry)
  803. {
  804. struct buffer_head *dotdot_bh;
  805. struct msdos_dir_entry *dotdot_de;
  806. struct inode *old_inode, *new_inode;
  807. struct fat_slot_info old_sinfo, sinfo;
  808. struct timespec ts;
  809. loff_t dotdot_i_pos, new_i_pos;
  810. int err, is_dir, update_dotdot, corrupt = 0;
  811. struct super_block *sb = old_dir->i_sb;
  812. old_sinfo.bh = sinfo.bh = dotdot_bh = NULL;
  813. old_inode = old_dentry->d_inode;
  814. new_inode = new_dentry->d_inode;
  815. lock_super(sb);
  816. err = vfat_find(old_dir, &old_dentry->d_name, &old_sinfo);
  817. if (err)
  818. goto out;
  819. is_dir = S_ISDIR(old_inode->i_mode);
  820. update_dotdot = (is_dir && old_dir != new_dir);
  821. if (update_dotdot) {
  822. if (fat_get_dotdot_entry(old_inode, &dotdot_bh, &dotdot_de,
  823. &dotdot_i_pos) < 0) {
  824. err = -EIO;
  825. goto out;
  826. }
  827. }
  828. ts = CURRENT_TIME_SEC;
  829. if (new_inode) {
  830. if (is_dir) {
  831. err = fat_dir_empty(new_inode);
  832. if (err)
  833. goto out;
  834. }
  835. new_i_pos = MSDOS_I(new_inode)->i_pos;
  836. fat_detach(new_inode);
  837. } else {
  838. err = vfat_add_entry(new_dir, &new_dentry->d_name, is_dir, 0,
  839. &ts, &sinfo);
  840. if (err)
  841. goto out;
  842. new_i_pos = sinfo.i_pos;
  843. }
  844. new_dir->i_version++;
  845. fat_detach(old_inode);
  846. fat_attach(old_inode, new_i_pos);
  847. if (IS_DIRSYNC(new_dir)) {
  848. err = fat_sync_inode(old_inode);
  849. if (err)
  850. goto error_inode;
  851. } else
  852. mark_inode_dirty(old_inode);
  853. if (update_dotdot) {
  854. int start = MSDOS_I(new_dir)->i_logstart;
  855. dotdot_de->start = cpu_to_le16(start);
  856. dotdot_de->starthi = cpu_to_le16(start >> 16);
  857. mark_buffer_dirty(dotdot_bh);
  858. if (IS_DIRSYNC(new_dir)) {
  859. err = sync_dirty_buffer(dotdot_bh);
  860. if (err)
  861. goto error_dotdot;
  862. }
  863. drop_nlink(old_dir);
  864. if (!new_inode)
  865. inc_nlink(new_dir);
  866. }
  867. err = fat_remove_entries(old_dir, &old_sinfo); /* and releases bh */
  868. old_sinfo.bh = NULL;
  869. if (err)
  870. goto error_dotdot;
  871. old_dir->i_version++;
  872. old_dir->i_ctime = old_dir->i_mtime = ts;
  873. if (IS_DIRSYNC(old_dir))
  874. (void)fat_sync_inode(old_dir);
  875. else
  876. mark_inode_dirty(old_dir);
  877. if (new_inode) {
  878. drop_nlink(new_inode);
  879. if (is_dir)
  880. drop_nlink(new_inode);
  881. new_inode->i_ctime = ts;
  882. }
  883. out:
  884. brelse(sinfo.bh);
  885. brelse(dotdot_bh);
  886. brelse(old_sinfo.bh);
  887. unlock_super(sb);
  888. return err;
  889. error_dotdot:
  890. /* data cluster is shared, serious corruption */
  891. corrupt = 1;
  892. if (update_dotdot) {
  893. int start = MSDOS_I(old_dir)->i_logstart;
  894. dotdot_de->start = cpu_to_le16(start);
  895. dotdot_de->starthi = cpu_to_le16(start >> 16);
  896. mark_buffer_dirty(dotdot_bh);
  897. corrupt |= sync_dirty_buffer(dotdot_bh);
  898. }
  899. error_inode:
  900. fat_detach(old_inode);
  901. fat_attach(old_inode, old_sinfo.i_pos);
  902. if (new_inode) {
  903. fat_attach(new_inode, new_i_pos);
  904. if (corrupt)
  905. corrupt |= fat_sync_inode(new_inode);
  906. } else {
  907. /*
  908. * If new entry was not sharing the data cluster, it
  909. * shouldn't be serious corruption.
  910. */
  911. int err2 = fat_remove_entries(new_dir, &sinfo);
  912. if (corrupt)
  913. corrupt |= err2;
  914. sinfo.bh = NULL;
  915. }
  916. if (corrupt < 0) {
  917. fat_fs_panic(new_dir->i_sb,
  918. "%s: Filesystem corrupted (i_pos %lld)",
  919. __func__, sinfo.i_pos);
  920. }
  921. goto out;
  922. }
  923. static const struct inode_operations vfat_dir_inode_operations = {
  924. .create = vfat_create,
  925. .lookup = vfat_lookup,
  926. .unlink = vfat_unlink,
  927. .mkdir = vfat_mkdir,
  928. .rmdir = vfat_rmdir,
  929. .rename = vfat_rename,
  930. .setattr = fat_setattr,
  931. .getattr = fat_getattr,
  932. };
  933. static int vfat_fill_super(struct super_block *sb, void *data, int silent)
  934. {
  935. int res;
  936. res = fat_fill_super(sb, data, silent, &vfat_dir_inode_operations, 1);
  937. if (res)
  938. return res;
  939. if (MSDOS_SB(sb)->options.name_check != 's')
  940. sb->s_root->d_op = &vfat_ci_dentry_ops;
  941. else
  942. sb->s_root->d_op = &vfat_dentry_ops;
  943. return 0;
  944. }
  945. static int vfat_get_sb(struct file_system_type *fs_type,
  946. int flags, const char *dev_name,
  947. void *data, struct vfsmount *mnt)
  948. {
  949. return get_sb_bdev(fs_type, flags, dev_name, data, vfat_fill_super,
  950. mnt);
  951. }
  952. static struct file_system_type vfat_fs_type = {
  953. .owner = THIS_MODULE,
  954. .name = "vfat",
  955. .get_sb = vfat_get_sb,
  956. .kill_sb = kill_block_super,
  957. .fs_flags = FS_REQUIRES_DEV,
  958. };
  959. static int __init init_vfat_fs(void)
  960. {
  961. return register_filesystem(&vfat_fs_type);
  962. }
  963. static void __exit exit_vfat_fs(void)
  964. {
  965. unregister_filesystem(&vfat_fs_type);
  966. }
  967. MODULE_LICENSE("GPL");
  968. MODULE_DESCRIPTION("VFAT filesystem support");
  969. MODULE_AUTHOR("Gordon Chaffee");
  970. module_init(init_vfat_fs)
  971. module_exit(exit_vfat_fs)