dir.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. *
  4. * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
  5. *
  6. * Directory handling functions for NTFS-based filesystems.
  7. *
  8. */
  9. #include <linux/fs.h>
  10. #include <linux/nls.h>
  11. #include "debug.h"
  12. #include "ntfs.h"
  13. #include "ntfs_fs.h"
  14. /* Convert little endian UTF-16 to NLS string. */
  15. int ntfs_utf16_to_nls(struct ntfs_sb_info *sbi, const __le16 *name, u32 len,
  16. u8 *buf, int buf_len)
  17. {
  18. int ret, warn;
  19. u8 *op;
  20. struct nls_table *nls = sbi->options->nls;
  21. static_assert(sizeof(wchar_t) == sizeof(__le16));
  22. if (!nls) {
  23. /* UTF-16 -> UTF-8 */
  24. ret = utf16s_to_utf8s((wchar_t *)name, len, UTF16_LITTLE_ENDIAN,
  25. buf, buf_len);
  26. buf[ret] = '\0';
  27. return ret;
  28. }
  29. op = buf;
  30. warn = 0;
  31. while (len--) {
  32. u16 ec;
  33. int charlen;
  34. char dump[5];
  35. if (buf_len < NLS_MAX_CHARSET_SIZE) {
  36. ntfs_warn(sbi->sb,
  37. "filename was truncated while converting.");
  38. break;
  39. }
  40. ec = le16_to_cpu(*name++);
  41. charlen = nls->uni2char(ec, op, buf_len);
  42. if (charlen > 0) {
  43. op += charlen;
  44. buf_len -= charlen;
  45. continue;
  46. }
  47. *op++ = '_';
  48. buf_len -= 1;
  49. if (warn)
  50. continue;
  51. warn = 1;
  52. hex_byte_pack(&dump[0], ec >> 8);
  53. hex_byte_pack(&dump[2], ec);
  54. dump[4] = 0;
  55. ntfs_err(sbi->sb, "failed to convert \"%s\" to %s", dump,
  56. nls->charset);
  57. }
  58. *op = '\0';
  59. return op - buf;
  60. }
  61. // clang-format off
  62. #define PLANE_SIZE 0x00010000
  63. #define SURROGATE_PAIR 0x0000d800
  64. #define SURROGATE_LOW 0x00000400
  65. #define SURROGATE_BITS 0x000003ff
  66. // clang-format on
  67. /*
  68. * put_utf16 - Modified version of put_utf16 from fs/nls/nls_base.c
  69. *
  70. * Function is sparse warnings free.
  71. */
  72. static inline void put_utf16(wchar_t *s, unsigned int c,
  73. enum utf16_endian endian)
  74. {
  75. static_assert(sizeof(wchar_t) == sizeof(__le16));
  76. static_assert(sizeof(wchar_t) == sizeof(__be16));
  77. switch (endian) {
  78. default:
  79. *s = (wchar_t)c;
  80. break;
  81. case UTF16_LITTLE_ENDIAN:
  82. *(__le16 *)s = __cpu_to_le16(c);
  83. break;
  84. case UTF16_BIG_ENDIAN:
  85. *(__be16 *)s = __cpu_to_be16(c);
  86. break;
  87. }
  88. }
  89. /*
  90. * _utf8s_to_utf16s
  91. *
  92. * Modified version of 'utf8s_to_utf16s' allows to
  93. * detect -ENAMETOOLONG without writing out of expected maximum.
  94. */
  95. static int _utf8s_to_utf16s(const u8 *s, int inlen, enum utf16_endian endian,
  96. wchar_t *pwcs, int maxout)
  97. {
  98. u16 *op;
  99. int size;
  100. unicode_t u;
  101. op = pwcs;
  102. while (inlen > 0 && *s) {
  103. if (*s & 0x80) {
  104. size = utf8_to_utf32(s, inlen, &u);
  105. if (size < 0)
  106. return -EINVAL;
  107. s += size;
  108. inlen -= size;
  109. if (u >= PLANE_SIZE) {
  110. if (maxout < 2)
  111. return -ENAMETOOLONG;
  112. u -= PLANE_SIZE;
  113. put_utf16(op++,
  114. SURROGATE_PAIR |
  115. ((u >> 10) & SURROGATE_BITS),
  116. endian);
  117. put_utf16(op++,
  118. SURROGATE_PAIR | SURROGATE_LOW |
  119. (u & SURROGATE_BITS),
  120. endian);
  121. maxout -= 2;
  122. } else {
  123. if (maxout < 1)
  124. return -ENAMETOOLONG;
  125. put_utf16(op++, u, endian);
  126. maxout--;
  127. }
  128. } else {
  129. if (maxout < 1)
  130. return -ENAMETOOLONG;
  131. put_utf16(op++, *s++, endian);
  132. inlen--;
  133. maxout--;
  134. }
  135. }
  136. return op - pwcs;
  137. }
  138. /*
  139. * ntfs_nls_to_utf16 - Convert input string to UTF-16.
  140. * @name: Input name.
  141. * @name_len: Input name length.
  142. * @uni: Destination memory.
  143. * @max_ulen: Destination memory.
  144. * @endian: Endian of target UTF-16 string.
  145. *
  146. * This function is called:
  147. * - to create NTFS name
  148. * - to create symlink
  149. *
  150. * Return: UTF-16 string length or error (if negative).
  151. */
  152. int ntfs_nls_to_utf16(struct ntfs_sb_info *sbi, const u8 *name, u32 name_len,
  153. struct cpu_str *uni, u32 max_ulen,
  154. enum utf16_endian endian)
  155. {
  156. int ret, slen;
  157. const u8 *end;
  158. struct nls_table *nls = sbi->options->nls;
  159. u16 *uname = uni->name;
  160. static_assert(sizeof(wchar_t) == sizeof(u16));
  161. if (!nls) {
  162. /* utf8 -> utf16 */
  163. ret = _utf8s_to_utf16s(name, name_len, endian, uname, max_ulen);
  164. uni->len = ret;
  165. return ret;
  166. }
  167. for (ret = 0, end = name + name_len; name < end; ret++, name += slen) {
  168. if (ret >= max_ulen)
  169. return -ENAMETOOLONG;
  170. slen = nls->char2uni(name, end - name, uname + ret);
  171. if (!slen)
  172. return -EINVAL;
  173. if (slen < 0)
  174. return slen;
  175. }
  176. #ifdef __BIG_ENDIAN
  177. if (endian == UTF16_LITTLE_ENDIAN) {
  178. int i = ret;
  179. while (i--) {
  180. __cpu_to_le16s(uname);
  181. uname++;
  182. }
  183. }
  184. #else
  185. if (endian == UTF16_BIG_ENDIAN) {
  186. int i = ret;
  187. while (i--) {
  188. __cpu_to_be16s(uname);
  189. uname++;
  190. }
  191. }
  192. #endif
  193. uni->len = ret;
  194. return ret;
  195. }
  196. /*
  197. * dir_search_u - Helper function.
  198. */
  199. struct inode *dir_search_u(struct inode *dir, const struct cpu_str *uni,
  200. struct ntfs_fnd *fnd)
  201. {
  202. int err = 0;
  203. struct super_block *sb = dir->i_sb;
  204. struct ntfs_sb_info *sbi = sb->s_fs_info;
  205. struct ntfs_inode *ni = ntfs_i(dir);
  206. struct NTFS_DE *e;
  207. int diff;
  208. struct inode *inode = NULL;
  209. struct ntfs_fnd *fnd_a = NULL;
  210. if (!fnd) {
  211. fnd_a = fnd_get();
  212. if (!fnd_a) {
  213. err = -ENOMEM;
  214. goto out;
  215. }
  216. fnd = fnd_a;
  217. }
  218. err = indx_find(&ni->dir, ni, NULL, uni, 0, sbi, &diff, &e, fnd);
  219. if (err)
  220. goto out;
  221. if (diff) {
  222. err = -ENOENT;
  223. goto out;
  224. }
  225. inode = ntfs_iget5(sb, &e->ref, uni);
  226. if (!IS_ERR(inode) && is_bad_inode(inode)) {
  227. iput(inode);
  228. err = -EINVAL;
  229. }
  230. out:
  231. fnd_put(fnd_a);
  232. return err == -ENOENT ? NULL : err ? ERR_PTR(err) : inode;
  233. }
  234. /*
  235. * returns false if 'ctx' if full
  236. */
  237. static inline bool ntfs_dir_emit(struct ntfs_sb_info *sbi,
  238. struct ntfs_inode *ni, const struct NTFS_DE *e,
  239. u8 *name, struct dir_context *ctx)
  240. {
  241. const struct ATTR_FILE_NAME *fname;
  242. unsigned long ino;
  243. int name_len;
  244. u32 dt_type;
  245. fname = Add2Ptr(e, sizeof(struct NTFS_DE));
  246. if (fname->type == FILE_NAME_DOS)
  247. return true;
  248. if (!mi_is_ref(&ni->mi, &fname->home))
  249. return true;
  250. ino = ino_get(&e->ref);
  251. if (ino == MFT_REC_ROOT)
  252. return true;
  253. /* Skip meta files. Unless option to show metafiles is set. */
  254. if (!sbi->options->showmeta && ntfs_is_meta_file(sbi, ino))
  255. return true;
  256. if (sbi->options->nohidden && (fname->dup.fa & FILE_ATTRIBUTE_HIDDEN))
  257. return true;
  258. if (fname->name_len + sizeof(struct NTFS_DE) > le16_to_cpu(e->size))
  259. return true;
  260. name_len = ntfs_utf16_to_nls(sbi, fname->name, fname->name_len, name,
  261. PATH_MAX);
  262. if (name_len <= 0) {
  263. ntfs_warn(sbi->sb, "failed to convert name for inode %lx.",
  264. ino);
  265. return true;
  266. }
  267. /*
  268. * NTFS: symlinks are "dir + reparse" or "file + reparse"
  269. * Unfortunately reparse attribute is used for many purposes (several dozens).
  270. * It is not possible here to know is this name symlink or not.
  271. * To get exactly the type of name we should to open inode (read mft).
  272. * getattr for opened file (fstat) correctly returns symlink.
  273. */
  274. dt_type = (fname->dup.fa & FILE_ATTRIBUTE_DIRECTORY) ? DT_DIR : DT_REG;
  275. /*
  276. * It is not reliable to detect the type of name using duplicated information
  277. * stored in parent directory.
  278. * The only correct way to get the type of name - read MFT record and find ATTR_STD.
  279. * The code below is not good idea.
  280. * It does additional locks/reads just to get the type of name.
  281. * Should we use additional mount option to enable branch below?
  282. */
  283. if (((fname->dup.fa & FILE_ATTRIBUTE_REPARSE_POINT) ||
  284. fname->dup.ea_size) &&
  285. ino != ni->mi.rno) {
  286. struct inode *inode = ntfs_iget5(sbi->sb, &e->ref, NULL);
  287. if (!IS_ERR_OR_NULL(inode)) {
  288. dt_type = fs_umode_to_dtype(inode->i_mode);
  289. iput(inode);
  290. }
  291. }
  292. return dir_emit(ctx, (s8 *)name, name_len, ino, dt_type);
  293. }
  294. /*
  295. * ntfs_read_hdr - Helper function for ntfs_readdir().
  296. *
  297. * returns 0 if ok.
  298. * returns -EINVAL if directory is corrupted.
  299. * returns +1 if 'ctx' is full.
  300. */
  301. static int ntfs_read_hdr(struct ntfs_sb_info *sbi, struct ntfs_inode *ni,
  302. const struct INDEX_HDR *hdr, u64 vbo, u64 pos,
  303. u8 *name, struct dir_context *ctx)
  304. {
  305. const struct NTFS_DE *e;
  306. u32 e_size;
  307. u32 end = le32_to_cpu(hdr->used);
  308. u32 off = le32_to_cpu(hdr->de_off);
  309. for (;; off += e_size) {
  310. if (off + sizeof(struct NTFS_DE) > end)
  311. return -EINVAL;
  312. e = Add2Ptr(hdr, off);
  313. e_size = le16_to_cpu(e->size);
  314. if (e_size < sizeof(struct NTFS_DE) || off + e_size > end)
  315. return -EINVAL;
  316. if (de_is_last(e))
  317. return 0;
  318. /* Skip already enumerated. */
  319. if (vbo + off < pos)
  320. continue;
  321. if (le16_to_cpu(e->key_size) < SIZEOF_ATTRIBUTE_FILENAME)
  322. return -EINVAL;
  323. ctx->pos = vbo + off;
  324. /* Submit the name to the filldir callback. */
  325. if (!ntfs_dir_emit(sbi, ni, e, name, ctx)) {
  326. /* ctx is full. */
  327. return +1;
  328. }
  329. }
  330. }
  331. /*
  332. * ntfs_readdir - file_operations::iterate_shared
  333. *
  334. * Use non sorted enumeration.
  335. * We have an example of broken volume where sorted enumeration
  336. * counts each name twice.
  337. */
  338. static int ntfs_readdir(struct file *file, struct dir_context *ctx)
  339. {
  340. const struct INDEX_ROOT *root;
  341. u64 vbo;
  342. size_t bit;
  343. loff_t eod;
  344. int err = 0;
  345. struct inode *dir = file_inode(file);
  346. struct ntfs_inode *ni = ntfs_i(dir);
  347. struct super_block *sb = dir->i_sb;
  348. struct ntfs_sb_info *sbi = sb->s_fs_info;
  349. loff_t i_size = i_size_read(dir);
  350. u32 pos = ctx->pos;
  351. u8 *name = NULL;
  352. struct indx_node *node = NULL;
  353. u8 index_bits = ni->dir.index_bits;
  354. /* Name is a buffer of PATH_MAX length. */
  355. static_assert(NTFS_NAME_LEN * 4 < PATH_MAX);
  356. eod = i_size + sbi->record_size;
  357. if (pos >= eod)
  358. return 0;
  359. if (!dir_emit_dots(file, ctx))
  360. return 0;
  361. /* Allocate PATH_MAX bytes. */
  362. name = __getname();
  363. if (!name)
  364. return -ENOMEM;
  365. if (!ni->mi_loaded && ni->attr_list.size) {
  366. /*
  367. * Directory inode is locked for read.
  368. * Load all subrecords to avoid 'write' access to 'ni' during
  369. * directory reading.
  370. */
  371. ni_lock(ni);
  372. if (!ni->mi_loaded && ni->attr_list.size) {
  373. err = ni_load_all_mi(ni);
  374. if (!err)
  375. ni->mi_loaded = true;
  376. }
  377. ni_unlock(ni);
  378. if (err)
  379. goto out;
  380. }
  381. root = indx_get_root(&ni->dir, ni, NULL, NULL);
  382. if (!root) {
  383. err = -EINVAL;
  384. goto out;
  385. }
  386. if (pos >= sbi->record_size) {
  387. bit = (pos - sbi->record_size) >> index_bits;
  388. } else {
  389. err = ntfs_read_hdr(sbi, ni, &root->ihdr, 0, pos, name, ctx);
  390. if (err)
  391. goto out;
  392. bit = 0;
  393. }
  394. if (!i_size) {
  395. ctx->pos = eod;
  396. goto out;
  397. }
  398. for (;;) {
  399. vbo = (u64)bit << index_bits;
  400. if (vbo >= i_size) {
  401. ctx->pos = eod;
  402. goto out;
  403. }
  404. err = indx_used_bit(&ni->dir, ni, &bit);
  405. if (err)
  406. goto out;
  407. if (bit == MINUS_ONE_T) {
  408. ctx->pos = eod;
  409. goto out;
  410. }
  411. vbo = (u64)bit << index_bits;
  412. if (vbo >= i_size) {
  413. err = -EINVAL;
  414. goto out;
  415. }
  416. err = indx_read(&ni->dir, ni, bit << ni->dir.idx2vbn_bits,
  417. &node);
  418. if (err)
  419. goto out;
  420. err = ntfs_read_hdr(sbi, ni, &node->index->ihdr,
  421. vbo + sbi->record_size, pos, name, ctx);
  422. if (err)
  423. goto out;
  424. bit += 1;
  425. }
  426. out:
  427. __putname(name);
  428. put_indx_node(node);
  429. if (err == 1) {
  430. /* 'ctx' is full. */
  431. err = 0;
  432. } else if (err == -ENOENT) {
  433. err = 0;
  434. ctx->pos = pos;
  435. } else if (err < 0) {
  436. if (err == -EINVAL)
  437. _ntfs_bad_inode(dir);
  438. ctx->pos = eod;
  439. }
  440. return err;
  441. }
  442. static int ntfs_dir_count(struct inode *dir, bool *is_empty, size_t *dirs,
  443. size_t *files)
  444. {
  445. int err = 0;
  446. struct ntfs_inode *ni = ntfs_i(dir);
  447. struct NTFS_DE *e = NULL;
  448. struct INDEX_ROOT *root;
  449. struct INDEX_HDR *hdr;
  450. const struct ATTR_FILE_NAME *fname;
  451. u32 e_size, off, end;
  452. size_t drs = 0, fles = 0, bit = 0;
  453. struct indx_node *node = NULL;
  454. size_t max_indx = i_size_read(&ni->vfs_inode) >> ni->dir.index_bits;
  455. if (is_empty)
  456. *is_empty = true;
  457. root = indx_get_root(&ni->dir, ni, NULL, NULL);
  458. if (!root)
  459. return -EINVAL;
  460. hdr = &root->ihdr;
  461. for (;;) {
  462. end = le32_to_cpu(hdr->used);
  463. off = le32_to_cpu(hdr->de_off);
  464. for (; off + sizeof(struct NTFS_DE) <= end; off += e_size) {
  465. e = Add2Ptr(hdr, off);
  466. e_size = le16_to_cpu(e->size);
  467. if (e_size < sizeof(struct NTFS_DE) ||
  468. off + e_size > end) {
  469. /* Looks like corruption. */
  470. break;
  471. }
  472. if (de_is_last(e))
  473. break;
  474. fname = de_get_fname(e);
  475. if (!fname)
  476. continue;
  477. if (fname->type == FILE_NAME_DOS)
  478. continue;
  479. if (is_empty) {
  480. *is_empty = false;
  481. if (!dirs && !files)
  482. goto out;
  483. }
  484. if (fname->dup.fa & FILE_ATTRIBUTE_DIRECTORY)
  485. drs += 1;
  486. else
  487. fles += 1;
  488. }
  489. if (bit >= max_indx)
  490. goto out;
  491. err = indx_used_bit(&ni->dir, ni, &bit);
  492. if (err)
  493. goto out;
  494. if (bit == MINUS_ONE_T)
  495. goto out;
  496. if (bit >= max_indx)
  497. goto out;
  498. err = indx_read(&ni->dir, ni, bit << ni->dir.idx2vbn_bits,
  499. &node);
  500. if (err)
  501. goto out;
  502. hdr = &node->index->ihdr;
  503. bit += 1;
  504. }
  505. out:
  506. put_indx_node(node);
  507. if (dirs)
  508. *dirs = drs;
  509. if (files)
  510. *files = fles;
  511. return err;
  512. }
  513. bool dir_is_empty(struct inode *dir)
  514. {
  515. bool is_empty = false;
  516. ntfs_dir_count(dir, &is_empty, NULL, NULL);
  517. return is_empty;
  518. }
  519. // clang-format off
  520. const struct file_operations ntfs_dir_operations = {
  521. .llseek = generic_file_llseek,
  522. .read = generic_read_dir,
  523. .iterate_shared = ntfs_readdir,
  524. .fsync = generic_file_fsync,
  525. .open = ntfs_file_open,
  526. .unlocked_ioctl = ntfs_ioctl,
  527. #ifdef CONFIG_COMPAT
  528. .compat_ioctl = ntfs_compat_ioctl,
  529. #endif
  530. };
  531. #if IS_ENABLED(CONFIG_NTFS_FS)
  532. const struct file_operations ntfs_legacy_dir_operations = {
  533. .llseek = generic_file_llseek,
  534. .read = generic_read_dir,
  535. .iterate_shared = ntfs_readdir,
  536. .open = ntfs_file_open,
  537. };
  538. #endif
  539. // clang-format on