namei_vfat.c 30 KB

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