namei_vfat.c 25 KB

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