namei_vfat.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  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. dentry_unhash(dentry);
  738. lock_super(sb);
  739. err = fat_dir_empty(inode);
  740. if (err)
  741. goto out;
  742. err = vfat_find(dir, &dentry->d_name, &sinfo);
  743. if (err)
  744. goto out;
  745. err = fat_remove_entries(dir, &sinfo); /* and releases bh */
  746. if (err)
  747. goto out;
  748. drop_nlink(dir);
  749. clear_nlink(inode);
  750. inode->i_mtime = inode->i_atime = CURRENT_TIME_SEC;
  751. fat_detach(inode);
  752. out:
  753. unlock_super(sb);
  754. return err;
  755. }
  756. static int vfat_unlink(struct inode *dir, struct dentry *dentry)
  757. {
  758. struct inode *inode = dentry->d_inode;
  759. struct super_block *sb = dir->i_sb;
  760. struct fat_slot_info sinfo;
  761. int err;
  762. lock_super(sb);
  763. err = vfat_find(dir, &dentry->d_name, &sinfo);
  764. if (err)
  765. goto out;
  766. err = fat_remove_entries(dir, &sinfo); /* and releases bh */
  767. if (err)
  768. goto out;
  769. clear_nlink(inode);
  770. inode->i_mtime = inode->i_atime = CURRENT_TIME_SEC;
  771. fat_detach(inode);
  772. out:
  773. unlock_super(sb);
  774. return err;
  775. }
  776. static int vfat_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  777. {
  778. struct super_block *sb = dir->i_sb;
  779. struct inode *inode;
  780. struct fat_slot_info sinfo;
  781. struct timespec ts;
  782. int err, cluster;
  783. lock_super(sb);
  784. ts = CURRENT_TIME_SEC;
  785. cluster = fat_alloc_new_dir(dir, &ts);
  786. if (cluster < 0) {
  787. err = cluster;
  788. goto out;
  789. }
  790. err = vfat_add_entry(dir, &dentry->d_name, 1, cluster, &ts, &sinfo);
  791. if (err)
  792. goto out_free;
  793. dir->i_version++;
  794. inc_nlink(dir);
  795. inode = fat_build_inode(sb, sinfo.de, sinfo.i_pos);
  796. brelse(sinfo.bh);
  797. if (IS_ERR(inode)) {
  798. err = PTR_ERR(inode);
  799. /* the directory was completed, just return a error */
  800. goto out;
  801. }
  802. inode->i_version++;
  803. inode->i_nlink = 2;
  804. inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
  805. /* timestamp is already written, so mark_inode_dirty() is unneeded. */
  806. dentry->d_time = dentry->d_parent->d_inode->i_version;
  807. d_instantiate(dentry, inode);
  808. unlock_super(sb);
  809. return 0;
  810. out_free:
  811. fat_free_clusters(dir, cluster);
  812. out:
  813. unlock_super(sb);
  814. return err;
  815. }
  816. static int vfat_rename(struct inode *old_dir, struct dentry *old_dentry,
  817. struct inode *new_dir, struct dentry *new_dentry)
  818. {
  819. struct buffer_head *dotdot_bh;
  820. struct msdos_dir_entry *dotdot_de;
  821. struct inode *old_inode, *new_inode;
  822. struct fat_slot_info old_sinfo, sinfo;
  823. struct timespec ts;
  824. loff_t dotdot_i_pos, new_i_pos;
  825. int err, is_dir, update_dotdot, corrupt = 0;
  826. struct super_block *sb = old_dir->i_sb;
  827. old_sinfo.bh = sinfo.bh = dotdot_bh = NULL;
  828. old_inode = old_dentry->d_inode;
  829. new_inode = new_dentry->d_inode;
  830. lock_super(sb);
  831. err = vfat_find(old_dir, &old_dentry->d_name, &old_sinfo);
  832. if (err)
  833. goto out;
  834. is_dir = S_ISDIR(old_inode->i_mode);
  835. update_dotdot = (is_dir && old_dir != new_dir);
  836. if (update_dotdot) {
  837. if (fat_get_dotdot_entry(old_inode, &dotdot_bh, &dotdot_de,
  838. &dotdot_i_pos) < 0) {
  839. err = -EIO;
  840. goto out;
  841. }
  842. }
  843. ts = CURRENT_TIME_SEC;
  844. if (new_inode) {
  845. if (is_dir) {
  846. err = fat_dir_empty(new_inode);
  847. if (err)
  848. goto out;
  849. }
  850. new_i_pos = MSDOS_I(new_inode)->i_pos;
  851. fat_detach(new_inode);
  852. } else {
  853. err = vfat_add_entry(new_dir, &new_dentry->d_name, is_dir, 0,
  854. &ts, &sinfo);
  855. if (err)
  856. goto out;
  857. new_i_pos = sinfo.i_pos;
  858. }
  859. new_dir->i_version++;
  860. fat_detach(old_inode);
  861. fat_attach(old_inode, new_i_pos);
  862. if (IS_DIRSYNC(new_dir)) {
  863. err = fat_sync_inode(old_inode);
  864. if (err)
  865. goto error_inode;
  866. } else
  867. mark_inode_dirty(old_inode);
  868. if (update_dotdot) {
  869. int start = MSDOS_I(new_dir)->i_logstart;
  870. dotdot_de->start = cpu_to_le16(start);
  871. dotdot_de->starthi = cpu_to_le16(start >> 16);
  872. mark_buffer_dirty_inode(dotdot_bh, old_inode);
  873. if (IS_DIRSYNC(new_dir)) {
  874. err = sync_dirty_buffer(dotdot_bh);
  875. if (err)
  876. goto error_dotdot;
  877. }
  878. drop_nlink(old_dir);
  879. if (!new_inode)
  880. inc_nlink(new_dir);
  881. }
  882. err = fat_remove_entries(old_dir, &old_sinfo); /* and releases bh */
  883. old_sinfo.bh = NULL;
  884. if (err)
  885. goto error_dotdot;
  886. old_dir->i_version++;
  887. old_dir->i_ctime = old_dir->i_mtime = ts;
  888. if (IS_DIRSYNC(old_dir))
  889. (void)fat_sync_inode(old_dir);
  890. else
  891. mark_inode_dirty(old_dir);
  892. if (new_inode) {
  893. drop_nlink(new_inode);
  894. if (is_dir)
  895. drop_nlink(new_inode);
  896. new_inode->i_ctime = ts;
  897. }
  898. out:
  899. brelse(sinfo.bh);
  900. brelse(dotdot_bh);
  901. brelse(old_sinfo.bh);
  902. unlock_super(sb);
  903. return err;
  904. error_dotdot:
  905. /* data cluster is shared, serious corruption */
  906. corrupt = 1;
  907. if (update_dotdot) {
  908. int start = MSDOS_I(old_dir)->i_logstart;
  909. dotdot_de->start = cpu_to_le16(start);
  910. dotdot_de->starthi = cpu_to_le16(start >> 16);
  911. mark_buffer_dirty_inode(dotdot_bh, old_inode);
  912. corrupt |= sync_dirty_buffer(dotdot_bh);
  913. }
  914. error_inode:
  915. fat_detach(old_inode);
  916. fat_attach(old_inode, old_sinfo.i_pos);
  917. if (new_inode) {
  918. fat_attach(new_inode, new_i_pos);
  919. if (corrupt)
  920. corrupt |= fat_sync_inode(new_inode);
  921. } else {
  922. /*
  923. * If new entry was not sharing the data cluster, it
  924. * shouldn't be serious corruption.
  925. */
  926. int err2 = fat_remove_entries(new_dir, &sinfo);
  927. if (corrupt)
  928. corrupt |= err2;
  929. sinfo.bh = NULL;
  930. }
  931. if (corrupt < 0) {
  932. fat_fs_error(new_dir->i_sb,
  933. "%s: Filesystem corrupted (i_pos %lld)",
  934. __func__, sinfo.i_pos);
  935. }
  936. goto out;
  937. }
  938. static const struct inode_operations vfat_dir_inode_operations = {
  939. .create = vfat_create,
  940. .lookup = vfat_lookup,
  941. .unlink = vfat_unlink,
  942. .mkdir = vfat_mkdir,
  943. .rmdir = vfat_rmdir,
  944. .rename = vfat_rename,
  945. .setattr = fat_setattr,
  946. .getattr = fat_getattr,
  947. };
  948. static void setup(struct super_block *sb)
  949. {
  950. if (MSDOS_SB(sb)->options.name_check != 's')
  951. sb->s_d_op = &vfat_ci_dentry_ops;
  952. else
  953. sb->s_d_op = &vfat_dentry_ops;
  954. }
  955. static int vfat_fill_super(struct super_block *sb, void *data, int silent)
  956. {
  957. return fat_fill_super(sb, data, silent, &vfat_dir_inode_operations,
  958. 1, setup);
  959. }
  960. static struct dentry *vfat_mount(struct file_system_type *fs_type,
  961. int flags, const char *dev_name,
  962. void *data)
  963. {
  964. return mount_bdev(fs_type, flags, dev_name, data, vfat_fill_super);
  965. }
  966. static struct file_system_type vfat_fs_type = {
  967. .owner = THIS_MODULE,
  968. .name = "vfat",
  969. .mount = vfat_mount,
  970. .kill_sb = kill_block_super,
  971. .fs_flags = FS_REQUIRES_DEV,
  972. };
  973. static int __init init_vfat_fs(void)
  974. {
  975. return register_filesystem(&vfat_fs_type);
  976. }
  977. static void __exit exit_vfat_fs(void)
  978. {
  979. unregister_filesystem(&vfat_fs_type);
  980. }
  981. MODULE_LICENSE("GPL");
  982. MODULE_DESCRIPTION("VFAT filesystem support");
  983. MODULE_AUTHOR("Gordon Chaffee");
  984. module_init(init_vfat_fs)
  985. module_exit(exit_vfat_fs)