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