index.c 15 KB

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