namei.c 24 KB

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