namei_vfat.c 25 KB

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