namei.c 25 KB

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