index.c 15 KB

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