index.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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_mutex 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, GFP_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_mutex 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_mutex 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->ir = ir;
  203. ictx->actx = actx;
  204. ictx->base_ni = base_ni;
  205. ictx->ia = NULL;
  206. ictx->page = NULL;
  207. done:
  208. ictx->entry = ie;
  209. ictx->data = (u8*)ie +
  210. le16_to_cpu(ie->data.vi.data_offset);
  211. ictx->data_len = le16_to_cpu(ie->data.vi.data_length);
  212. ntfs_debug("Done.");
  213. return err;
  214. }
  215. /*
  216. * Not a perfect match, need to do full blown collation so we
  217. * know which way in the B+tree we have to go.
  218. */
  219. rc = ntfs_collate(vol, idx_ni->itype.index.collation_rule, key,
  220. key_len, &ie->key, le16_to_cpu(ie->key_length));
  221. /*
  222. * If @key collates before the key of the current entry, there
  223. * is definitely no such key in this index but we might need to
  224. * descend into the B+tree so we just break out of the loop.
  225. */
  226. if (rc == -1)
  227. break;
  228. /*
  229. * A match should never happen as the memcmp() call should have
  230. * cought it, but we still treat it correctly.
  231. */
  232. if (!rc)
  233. goto ir_done;
  234. /* The keys are not equal, continue the search. */
  235. }
  236. /*
  237. * We have finished with this index without success. Check for the
  238. * presence of a child node and if not present setup @ictx and return
  239. * -ENOENT.
  240. */
  241. if (!(ie->flags & INDEX_ENTRY_NODE)) {
  242. ntfs_debug("Entry not found.");
  243. err = -ENOENT;
  244. goto ir_done;
  245. } /* Child node present, descend into it. */
  246. /* Consistency check: Verify that an index allocation exists. */
  247. if (!NInoIndexAllocPresent(idx_ni)) {
  248. ntfs_error(sb, "No index allocation attribute but index entry "
  249. "requires one. Inode 0x%lx is corrupt or "
  250. "driver bug.", idx_ni->mft_no);
  251. goto err_out;
  252. }
  253. /* Get the starting vcn of the index_block holding the child node. */
  254. vcn = sle64_to_cpup((sle64*)((u8*)ie + le16_to_cpu(ie->length) - 8));
  255. ia_mapping = VFS_I(idx_ni)->i_mapping;
  256. /*
  257. * We are done with the index root and the mft record. Release them,
  258. * otherwise we deadlock with ntfs_map_page().
  259. */
  260. ntfs_attr_put_search_ctx(actx);
  261. unmap_mft_record(base_ni);
  262. m = NULL;
  263. actx = NULL;
  264. descend_into_child_node:
  265. /*
  266. * Convert vcn to index into the index allocation attribute in units
  267. * of PAGE_CACHE_SIZE and map the page cache page, reading it from
  268. * disk if necessary.
  269. */
  270. page = ntfs_map_page(ia_mapping, vcn <<
  271. idx_ni->itype.index.vcn_size_bits >> PAGE_CACHE_SHIFT);
  272. if (IS_ERR(page)) {
  273. ntfs_error(sb, "Failed to map index page, error %ld.",
  274. -PTR_ERR(page));
  275. err = PTR_ERR(page);
  276. goto err_out;
  277. }
  278. lock_page(page);
  279. kaddr = (u8*)page_address(page);
  280. fast_descend_into_child_node:
  281. /* Get to the index allocation block. */
  282. ia = (INDEX_ALLOCATION*)(kaddr + ((vcn <<
  283. idx_ni->itype.index.vcn_size_bits) & ~PAGE_CACHE_MASK));
  284. /* Bounds checks. */
  285. if ((u8*)ia < kaddr || (u8*)ia > kaddr + PAGE_CACHE_SIZE) {
  286. ntfs_error(sb, "Out of bounds check failed. Corrupt inode "
  287. "0x%lx or driver bug.", idx_ni->mft_no);
  288. goto unm_err_out;
  289. }
  290. /* Catch multi sector transfer fixup errors. */
  291. if (unlikely(!ntfs_is_indx_record(ia->magic))) {
  292. ntfs_error(sb, "Index record with vcn 0x%llx is corrupt. "
  293. "Corrupt inode 0x%lx. Run chkdsk.",
  294. (long long)vcn, idx_ni->mft_no);
  295. goto unm_err_out;
  296. }
  297. if (sle64_to_cpu(ia->index_block_vcn) != vcn) {
  298. ntfs_error(sb, "Actual VCN (0x%llx) of index buffer is "
  299. "different from expected VCN (0x%llx). Inode "
  300. "0x%lx is corrupt or driver bug.",
  301. (unsigned long long)
  302. sle64_to_cpu(ia->index_block_vcn),
  303. (unsigned long long)vcn, idx_ni->mft_no);
  304. goto unm_err_out;
  305. }
  306. if (le32_to_cpu(ia->index.allocated_size) + 0x18 !=
  307. idx_ni->itype.index.block_size) {
  308. ntfs_error(sb, "Index buffer (VCN 0x%llx) of inode 0x%lx has "
  309. "a size (%u) differing from the index "
  310. "specified size (%u). Inode is corrupt or "
  311. "driver bug.", (unsigned long long)vcn,
  312. idx_ni->mft_no,
  313. le32_to_cpu(ia->index.allocated_size) + 0x18,
  314. idx_ni->itype.index.block_size);
  315. goto unm_err_out;
  316. }
  317. index_end = (u8*)ia + idx_ni->itype.index.block_size;
  318. if (index_end > kaddr + PAGE_CACHE_SIZE) {
  319. ntfs_error(sb, "Index buffer (VCN 0x%llx) of inode 0x%lx "
  320. "crosses page boundary. Impossible! Cannot "
  321. "access! This is probably a bug in the "
  322. "driver.", (unsigned long long)vcn,
  323. idx_ni->mft_no);
  324. goto unm_err_out;
  325. }
  326. index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length);
  327. if (index_end > (u8*)ia + idx_ni->itype.index.block_size) {
  328. ntfs_error(sb, "Size of index buffer (VCN 0x%llx) of inode "
  329. "0x%lx exceeds maximum size.",
  330. (unsigned long long)vcn, idx_ni->mft_no);
  331. goto unm_err_out;
  332. }
  333. /* The first index entry. */
  334. ie = (INDEX_ENTRY*)((u8*)&ia->index +
  335. le32_to_cpu(ia->index.entries_offset));
  336. /*
  337. * Iterate similar to above big loop but applied to index buffer, thus
  338. * loop until we exceed valid memory (corruption case) or until we
  339. * reach the last entry.
  340. */
  341. for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
  342. /* Bounds checks. */
  343. if ((u8*)ie < (u8*)ia || (u8*)ie +
  344. sizeof(INDEX_ENTRY_HEADER) > index_end ||
  345. (u8*)ie + le16_to_cpu(ie->length) > index_end) {
  346. ntfs_error(sb, "Index entry out of bounds in inode "
  347. "0x%lx.", idx_ni->mft_no);
  348. goto unm_err_out;
  349. }
  350. /*
  351. * The last entry cannot contain a key. It can however contain
  352. * a pointer to a child node in the B+tree so we just break out.
  353. */
  354. if (ie->flags & INDEX_ENTRY_END)
  355. break;
  356. /* Further bounds checks. */
  357. if ((u32)sizeof(INDEX_ENTRY_HEADER) +
  358. le16_to_cpu(ie->key_length) >
  359. le16_to_cpu(ie->data.vi.data_offset) ||
  360. (u32)le16_to_cpu(ie->data.vi.data_offset) +
  361. le16_to_cpu(ie->data.vi.data_length) >
  362. le16_to_cpu(ie->length)) {
  363. ntfs_error(sb, "Index entry out of bounds in inode "
  364. "0x%lx.", idx_ni->mft_no);
  365. goto unm_err_out;
  366. }
  367. /* If the keys match perfectly, we setup @ictx and return 0. */
  368. if ((key_len == le16_to_cpu(ie->key_length)) && !memcmp(key,
  369. &ie->key, key_len)) {
  370. ia_done:
  371. ictx->is_in_root = false;
  372. ictx->actx = NULL;
  373. ictx->base_ni = NULL;
  374. ictx->ia = ia;
  375. ictx->page = page;
  376. goto done;
  377. }
  378. /*
  379. * Not a perfect match, need to do full blown collation so we
  380. * know which way in the B+tree we have to go.
  381. */
  382. rc = ntfs_collate(vol, idx_ni->itype.index.collation_rule, key,
  383. key_len, &ie->key, le16_to_cpu(ie->key_length));
  384. /*
  385. * If @key collates before the key of the current entry, there
  386. * is definitely no such key in this index but we might need to
  387. * descend into the B+tree so we just break out of the loop.
  388. */
  389. if (rc == -1)
  390. break;
  391. /*
  392. * A match should never happen as the memcmp() call should have
  393. * cought it, but we still treat it correctly.
  394. */
  395. if (!rc)
  396. goto ia_done;
  397. /* The keys are not equal, continue the search. */
  398. }
  399. /*
  400. * We have finished with this index buffer without success. Check for
  401. * the presence of a child node and if not present return -ENOENT.
  402. */
  403. if (!(ie->flags & INDEX_ENTRY_NODE)) {
  404. ntfs_debug("Entry not found.");
  405. err = -ENOENT;
  406. goto ia_done;
  407. }
  408. if ((ia->index.flags & NODE_MASK) == LEAF_NODE) {
  409. ntfs_error(sb, "Index entry with child node found in a leaf "
  410. "node in inode 0x%lx.", idx_ni->mft_no);
  411. goto unm_err_out;
  412. }
  413. /* Child node present, descend into it. */
  414. old_vcn = vcn;
  415. vcn = sle64_to_cpup((sle64*)((u8*)ie + le16_to_cpu(ie->length) - 8));
  416. if (vcn >= 0) {
  417. /*
  418. * If vcn is in the same page cache page as old_vcn we recycle
  419. * the mapped page.
  420. */
  421. if (old_vcn << vol->cluster_size_bits >>
  422. PAGE_CACHE_SHIFT == vcn <<
  423. vol->cluster_size_bits >>
  424. PAGE_CACHE_SHIFT)
  425. goto fast_descend_into_child_node;
  426. unlock_page(page);
  427. ntfs_unmap_page(page);
  428. goto descend_into_child_node;
  429. }
  430. ntfs_error(sb, "Negative child node vcn in inode 0x%lx.",
  431. idx_ni->mft_no);
  432. unm_err_out:
  433. unlock_page(page);
  434. ntfs_unmap_page(page);
  435. err_out:
  436. if (!err)
  437. err = -EIO;
  438. if (actx)
  439. ntfs_attr_put_search_ctx(actx);
  440. if (m)
  441. unmap_mft_record(base_ni);
  442. return err;
  443. idx_err_out:
  444. ntfs_error(sb, "Corrupt index. Aborting lookup.");
  445. goto err_out;
  446. }