namei.c 24 KB

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