index.c 15 KB

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