namei_vfat.c 29 KB

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