namei_vfat.c 26 KB

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