index.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * index.c - NTFS kernel index handling. Part of the Linux-NTFS project.
  4. *
  5. * Copyright (c) 2004-2005 Anton Altaparmakov
  6. */
  7. #include <linux/slab.h>
  8. #include "aops.h"
  9. #include "collate.h"
  10. #include "debug.h"
  11. #include "index.h"
  12. #include "ntfs.h"
  13. /**
  14. * ntfs_index_ctx_get - allocate and initialize a new index context
  15. * @idx_ni: ntfs index inode with which to initialize the context
  16. *
  17. * Allocate a new index context, initialize it with @idx_ni and return it.
  18. * Return NULL if allocation failed.
  19. *
  20. * Locking: Caller must hold i_mutex on the index inode.
  21. */
  22. ntfs_index_context *ntfs_index_ctx_get(ntfs_inode *idx_ni)
  23. {
  24. ntfs_index_context *ictx;
  25. ictx = kmem_cache_alloc(ntfs_index_ctx_cache, GFP_NOFS);
  26. if (ictx)
  27. *ictx = (ntfs_index_context){ .idx_ni = idx_ni };
  28. return ictx;
  29. }
  30. /**
  31. * ntfs_index_ctx_put - release an index context
  32. * @ictx: index context to free
  33. *
  34. * Release the index context @ictx, releasing all associated resources.
  35. *
  36. * Locking: Caller must hold i_mutex on the index inode.
  37. */
  38. void ntfs_index_ctx_put(ntfs_index_context *ictx)
  39. {
  40. if (ictx->entry) {
  41. if (ictx->is_in_root) {
  42. if (ictx->actx)
  43. ntfs_attr_put_search_ctx(ictx->actx);
  44. if (ictx->base_ni)
  45. unmap_mft_record(ictx->base_ni);
  46. } else {
  47. struct page *page = ictx->page;
  48. if (page) {
  49. BUG_ON(!PageLocked(page));
  50. unlock_page(page);
  51. ntfs_unmap_page(page);
  52. }
  53. }
  54. }
  55. kmem_cache_free(ntfs_index_ctx_cache, ictx);
  56. return;
  57. }
  58. /**
  59. * ntfs_index_lookup - find a key in an index and return its index entry
  60. * @key: [IN] key for which to search in the index
  61. * @key_len: [IN] length of @key in bytes
  62. * @ictx: [IN/OUT] context describing the index and the returned entry
  63. *
  64. * Before calling ntfs_index_lookup(), @ictx must have been obtained from a
  65. * call to ntfs_index_ctx_get().
  66. *
  67. * Look for the @key in the index specified by the index lookup context @ictx.
  68. * ntfs_index_lookup() walks the contents of the index looking for the @key.
  69. *
  70. * If the @key is found in the index, 0 is returned and @ictx is setup to
  71. * describe the index entry containing the matching @key. @ictx->entry is the
  72. * index entry and @ictx->data and @ictx->data_len are the index entry data and
  73. * its length in bytes, respectively.
  74. *
  75. * If the @key is not found in the index, -ENOENT is returned and @ictx is
  76. * setup to describe the index entry whose key collates immediately after the
  77. * search @key, i.e. this is the position in the index at which an index entry
  78. * with a key of @key would need to be inserted.
  79. *
  80. * If an error occurs return the negative error code and @ictx is left
  81. * untouched.
  82. *
  83. * When finished with the entry and its data, call ntfs_index_ctx_put() to free
  84. * the context and other associated resources.
  85. *
  86. * If the index entry was modified, call flush_dcache_index_entry_page()
  87. * immediately after the modification and either ntfs_index_entry_mark_dirty()
  88. * or ntfs_index_entry_write() before the call to ntfs_index_ctx_put() to
  89. * ensure that the changes are written to disk.
  90. *
  91. * Locking: - Caller must hold i_mutex on the index inode.
  92. * - Each page cache page in the index allocation mapping must be
  93. * locked whilst being accessed otherwise we may find a corrupt
  94. * page due to it being under ->writepage at the moment which
  95. * applies the mst protection fixups before writing out and then
  96. * removes them again after the write is complete after which it
  97. * unlocks the page.
  98. */
  99. int ntfs_index_lookup(const void *key, const int key_len,
  100. ntfs_index_context *ictx)
  101. {
  102. VCN vcn, old_vcn;
  103. ntfs_inode *idx_ni = ictx->idx_ni;
  104. ntfs_volume *vol = idx_ni->vol;
  105. struct super_block *sb = vol->sb;
  106. ntfs_inode *base_ni = idx_ni->ext.base_ntfs_ino;
  107. MFT_RECORD *m;
  108. INDEX_ROOT *ir;
  109. INDEX_ENTRY *ie;
  110. INDEX_ALLOCATION *ia;
  111. u8 *index_end, *kaddr;
  112. ntfs_attr_search_ctx *actx;
  113. struct address_space *ia_mapping;
  114. struct page *page;
  115. int rc, err = 0;
  116. ntfs_debug("Entering.");
  117. BUG_ON(!NInoAttr(idx_ni));
  118. BUG_ON(idx_ni->type != AT_INDEX_ALLOCATION);
  119. BUG_ON(idx_ni->nr_extents != -1);
  120. BUG_ON(!base_ni);
  121. BUG_ON(!key);
  122. BUG_ON(key_len <= 0);
  123. if (!ntfs_is_collation_rule_supported(
  124. idx_ni->itype.index.collation_rule)) {
  125. ntfs_error(sb, "Index uses unsupported collation rule 0x%x. "
  126. "Aborting lookup.", le32_to_cpu(
  127. idx_ni->itype.index.collation_rule));
  128. return -EOPNOTSUPP;
  129. }
  130. /* Get hold of the mft record for the index inode. */
  131. m = map_mft_record(base_ni);
  132. if (IS_ERR(m)) {
  133. ntfs_error(sb, "map_mft_record() failed with error code %ld.",
  134. -PTR_ERR(m));
  135. return PTR_ERR(m);
  136. }
  137. actx = ntfs_attr_get_search_ctx(base_ni, m);
  138. if (unlikely(!actx)) {
  139. err = -ENOMEM;
  140. goto err_out;
  141. }
  142. /* Find the index root attribute in the mft record. */
  143. err = ntfs_attr_lookup(AT_INDEX_ROOT, idx_ni->name, idx_ni->name_len,
  144. CASE_SENSITIVE, 0, NULL, 0, actx);
  145. if (unlikely(err)) {
  146. if (err == -ENOENT) {
  147. ntfs_error(sb, "Index root attribute missing in inode "
  148. "0x%lx.", idx_ni->mft_no);
  149. err = -EIO;
  150. }
  151. goto err_out;
  152. }
  153. /* Get to the index root value (it has been verified in read_inode). */
  154. ir = (INDEX_ROOT*)((u8*)actx->attr +
  155. le16_to_cpu(actx->attr->data.resident.value_offset));
  156. index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length);
  157. /* The first index entry. */
  158. ie = (INDEX_ENTRY*)((u8*)&ir->index +
  159. le32_to_cpu(ir->index.entries_offset));
  160. /*
  161. * Loop until we exceed valid memory (corruption case) or until we
  162. * reach the last entry.
  163. */
  164. for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
  165. /* Bounds checks. */
  166. if ((u8*)ie < (u8*)actx->mrec || (u8*)ie +
  167. sizeof(INDEX_ENTRY_HEADER) > index_end ||
  168. (u8*)ie + le16_to_cpu(ie->length) > index_end)
  169. goto idx_err_out;
  170. /*
  171. * The last entry cannot contain a key. It can however contain
  172. * a pointer to a child node in the B+tree so we just break out.
  173. */
  174. if (ie->flags & INDEX_ENTRY_END)
  175. break;
  176. /* Further bounds checks. */
  177. if ((u32)sizeof(INDEX_ENTRY_HEADER) +
  178. le16_to_cpu(ie->key_length) >
  179. le16_to_cpu(ie->data.vi.data_offset) ||
  180. (u32)le16_to_cpu(ie->data.vi.data_offset) +
  181. le16_to_cpu(ie->data.vi.data_length) >
  182. le16_to_cpu(ie->length))
  183. goto idx_err_out;
  184. /* If the keys match perfectly, we setup @ictx and return 0. */
  185. if ((key_len == le16_to_cpu(ie->key_length)) && !memcmp(key,
  186. &ie->key, key_len)) {
  187. ir_done:
  188. ictx->is_in_root = true;
  189. ictx->ir = ir;
  190. ictx->actx = actx;
  191. ictx->base_ni = base_ni;
  192. ictx->ia = NULL;
  193. ictx->page = NULL;
  194. done:
  195. ictx->entry = ie;
  196. ictx->data = (u8*)ie +
  197. le16_to_cpu(ie->data.vi.data_offset);
  198. ictx->data_len = le16_to_cpu(ie->data.vi.data_length);
  199. ntfs_debug("Done.");
  200. return err;
  201. }
  202. /*
  203. * Not a perfect match, need to do full blown collation so we
  204. * know which way in the B+tree we have to go.
  205. */
  206. rc = ntfs_collate(vol, idx_ni->itype.index.collation_rule, key,
  207. key_len, &ie->key, le16_to_cpu(ie->key_length));
  208. /*
  209. * If @key collates before the key of the current entry, there
  210. * is definitely no such key in this index but we might need to
  211. * descend into the B+tree so we just break out of the loop.
  212. */
  213. if (rc == -1)
  214. break;
  215. /*
  216. * A match should never happen as the memcmp() call should have
  217. * cought it, but we still treat it correctly.
  218. */
  219. if (!rc)
  220. goto ir_done;
  221. /* The keys are not equal, continue the search. */
  222. }
  223. /*
  224. * We have finished with this index without success. Check for the
  225. * presence of a child node and if not present setup @ictx and return
  226. * -ENOENT.
  227. */
  228. if (!(ie->flags & INDEX_ENTRY_NODE)) {
  229. ntfs_debug("Entry not found.");
  230. err = -ENOENT;
  231. goto ir_done;
  232. } /* Child node present, descend into it. */
  233. /* Consistency check: Verify that an index allocation exists. */
  234. if (!NInoIndexAllocPresent(idx_ni)) {
  235. ntfs_error(sb, "No index allocation attribute but index entry "
  236. "requires one. Inode 0x%lx is corrupt or "
  237. "driver bug.", idx_ni->mft_no);
  238. goto err_out;
  239. }
  240. /* Get the starting vcn of the index_block holding the child node. */
  241. vcn = sle64_to_cpup((sle64*)((u8*)ie + le16_to_cpu(ie->length) - 8));
  242. ia_mapping = VFS_I(idx_ni)->i_mapping;
  243. /*
  244. * We are done with the index root and the mft record. Release them,
  245. * otherwise we deadlock with ntfs_map_page().
  246. */
  247. ntfs_attr_put_search_ctx(actx);
  248. unmap_mft_record(base_ni);
  249. m = NULL;
  250. actx = NULL;
  251. descend_into_child_node:
  252. /*
  253. * Convert vcn to index into the index allocation attribute in units
  254. * of PAGE_SIZE and map the page cache page, reading it from
  255. * disk if necessary.
  256. */
  257. page = ntfs_map_page(ia_mapping, vcn <<
  258. idx_ni->itype.index.vcn_size_bits >> PAGE_SHIFT);
  259. if (IS_ERR(page)) {
  260. ntfs_error(sb, "Failed to map index page, error %ld.",
  261. -PTR_ERR(page));
  262. err = PTR_ERR(page);
  263. goto err_out;
  264. }
  265. lock_page(page);
  266. kaddr = (u8*)page_address(page);
  267. fast_descend_into_child_node:
  268. /* Get to the index allocation block. */
  269. ia = (INDEX_ALLOCATION*)(kaddr + ((vcn <<
  270. idx_ni->itype.index.vcn_size_bits) & ~PAGE_MASK));
  271. /* Bounds checks. */
  272. if ((u8*)ia < kaddr || (u8*)ia > kaddr + PAGE_SIZE) {
  273. ntfs_error(sb, "Out of bounds check failed. Corrupt inode "
  274. "0x%lx or driver bug.", idx_ni->mft_no);
  275. goto unm_err_out;
  276. }
  277. /* Catch multi sector transfer fixup errors. */
  278. if (unlikely(!ntfs_is_indx_record(ia->magic))) {
  279. ntfs_error(sb, "Index record with vcn 0x%llx is corrupt. "
  280. "Corrupt inode 0x%lx. Run chkdsk.",
  281. (long long)vcn, idx_ni->mft_no);
  282. goto unm_err_out;
  283. }
  284. if (sle64_to_cpu(ia->index_block_vcn) != vcn) {
  285. ntfs_error(sb, "Actual VCN (0x%llx) of index buffer is "
  286. "different from expected VCN (0x%llx). Inode "
  287. "0x%lx is corrupt or driver bug.",
  288. (unsigned long long)
  289. sle64_to_cpu(ia->index_block_vcn),
  290. (unsigned long long)vcn, idx_ni->mft_no);
  291. goto unm_err_out;
  292. }
  293. if (le32_to_cpu(ia->index.allocated_size) + 0x18 !=
  294. idx_ni->itype.index.block_size) {
  295. ntfs_error(sb, "Index buffer (VCN 0x%llx) of inode 0x%lx has "
  296. "a size (%u) differing from the index "
  297. "specified size (%u). Inode is corrupt or "
  298. "driver bug.", (unsigned long long)vcn,
  299. idx_ni->mft_no,
  300. le32_to_cpu(ia->index.allocated_size) + 0x18,
  301. idx_ni->itype.index.block_size);
  302. goto unm_err_out;
  303. }
  304. index_end = (u8*)ia + idx_ni->itype.index.block_size;
  305. if (index_end > kaddr + PAGE_SIZE) {
  306. ntfs_error(sb, "Index buffer (VCN 0x%llx) of inode 0x%lx "
  307. "crosses page boundary. Impossible! Cannot "
  308. "access! This is probably a bug in the "
  309. "driver.", (unsigned long long)vcn,
  310. idx_ni->mft_no);
  311. goto unm_err_out;
  312. }
  313. index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length);
  314. if (index_end > (u8*)ia + idx_ni->itype.index.block_size) {
  315. ntfs_error(sb, "Size of index buffer (VCN 0x%llx) of inode "
  316. "0x%lx exceeds maximum size.",
  317. (unsigned long long)vcn, idx_ni->mft_no);
  318. goto unm_err_out;
  319. }
  320. /* The first index entry. */
  321. ie = (INDEX_ENTRY*)((u8*)&ia->index +
  322. le32_to_cpu(ia->index.entries_offset));
  323. /*
  324. * Iterate similar to above big loop but applied to index buffer, thus
  325. * loop until we exceed valid memory (corruption case) or until we
  326. * reach the last entry.
  327. */
  328. for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
  329. /* Bounds checks. */
  330. if ((u8*)ie < (u8*)ia || (u8*)ie +
  331. sizeof(INDEX_ENTRY_HEADER) > index_end ||
  332. (u8*)ie + le16_to_cpu(ie->length) > index_end) {
  333. ntfs_error(sb, "Index entry out of bounds in inode "
  334. "0x%lx.", idx_ni->mft_no);
  335. goto unm_err_out;
  336. }
  337. /*
  338. * The last entry cannot contain a key. It can however contain
  339. * a pointer to a child node in the B+tree so we just break out.
  340. */
  341. if (ie->flags & INDEX_ENTRY_END)
  342. break;
  343. /* Further bounds checks. */
  344. if ((u32)sizeof(INDEX_ENTRY_HEADER) +
  345. le16_to_cpu(ie->key_length) >
  346. le16_to_cpu(ie->data.vi.data_offset) ||
  347. (u32)le16_to_cpu(ie->data.vi.data_offset) +
  348. le16_to_cpu(ie->data.vi.data_length) >
  349. le16_to_cpu(ie->length)) {
  350. ntfs_error(sb, "Index entry out of bounds in inode "
  351. "0x%lx.", idx_ni->mft_no);
  352. goto unm_err_out;
  353. }
  354. /* If the keys match perfectly, we setup @ictx and return 0. */
  355. if ((key_len == le16_to_cpu(ie->key_length)) && !memcmp(key,
  356. &ie->key, key_len)) {
  357. ia_done:
  358. ictx->is_in_root = false;
  359. ictx->actx = NULL;
  360. ictx->base_ni = NULL;
  361. ictx->ia = ia;
  362. ictx->page = page;
  363. goto done;
  364. }
  365. /*
  366. * Not a perfect match, need to do full blown collation so we
  367. * know which way in the B+tree we have to go.
  368. */
  369. rc = ntfs_collate(vol, idx_ni->itype.index.collation_rule, key,
  370. key_len, &ie->key, le16_to_cpu(ie->key_length));
  371. /*
  372. * If @key collates before the key of the current entry, there
  373. * is definitely no such key in this index but we might need to
  374. * descend into the B+tree so we just break out of the loop.
  375. */
  376. if (rc == -1)
  377. break;
  378. /*
  379. * A match should never happen as the memcmp() call should have
  380. * cought it, but we still treat it correctly.
  381. */
  382. if (!rc)
  383. goto ia_done;
  384. /* The keys are not equal, continue the search. */
  385. }
  386. /*
  387. * We have finished with this index buffer without success. Check for
  388. * the presence of a child node and if not present return -ENOENT.
  389. */
  390. if (!(ie->flags & INDEX_ENTRY_NODE)) {
  391. ntfs_debug("Entry not found.");
  392. err = -ENOENT;
  393. goto ia_done;
  394. }
  395. if ((ia->index.flags & NODE_MASK) == LEAF_NODE) {
  396. ntfs_error(sb, "Index entry with child node found in a leaf "
  397. "node in inode 0x%lx.", idx_ni->mft_no);
  398. goto unm_err_out;
  399. }
  400. /* Child node present, descend into it. */
  401. old_vcn = vcn;
  402. vcn = sle64_to_cpup((sle64*)((u8*)ie + le16_to_cpu(ie->length) - 8));
  403. if (vcn >= 0) {
  404. /*
  405. * If vcn is in the same page cache page as old_vcn we recycle
  406. * the mapped page.
  407. */
  408. if (old_vcn << vol->cluster_size_bits >>
  409. PAGE_SHIFT == vcn <<
  410. vol->cluster_size_bits >>
  411. PAGE_SHIFT)
  412. goto fast_descend_into_child_node;
  413. unlock_page(page);
  414. ntfs_unmap_page(page);
  415. goto descend_into_child_node;
  416. }
  417. ntfs_error(sb, "Negative child node vcn in inode 0x%lx.",
  418. idx_ni->mft_no);
  419. unm_err_out:
  420. unlock_page(page);
  421. ntfs_unmap_page(page);
  422. err_out:
  423. if (!err)
  424. err = -EIO;
  425. if (actx)
  426. ntfs_attr_put_search_ctx(actx);
  427. if (m)
  428. unmap_mft_record(base_ni);
  429. return err;
  430. idx_err_out:
  431. ntfs_error(sb, "Corrupt index. Aborting lookup.");
  432. goto err_out;
  433. }