Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 1 | /** |
| 2 | * cluster - Part of the Linux-NTFS project. |
| 3 | * |
| 4 | * Copyright (c) 2002-2003 Richard Russon |
Steve Kondik | 79165c3 | 2015-11-09 19:43:00 -0800 | [diff] [blame] | 5 | * Copyright (c) 2014 Jean-Pierre Andre |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 6 | * |
| 7 | * This function will locate the owner of any given sector or cluster range. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program (in the main directory of the Linux-NTFS |
| 21 | * distribution in the file COPYING); if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 23 | */ |
| 24 | |
| 25 | #include "config.h" |
| 26 | |
| 27 | #ifdef HAVE_STDIO_H |
| 28 | #include <stdio.h> |
| 29 | #endif |
| 30 | #ifdef HAVE_STDLIB_H |
| 31 | #include <stdlib.h> |
| 32 | #endif |
| 33 | #ifdef HAVE_STRING_H |
| 34 | #include <string.h> |
| 35 | #endif |
| 36 | |
| 37 | #include "cluster.h" |
| 38 | #include "utils.h" |
| 39 | #include "logging.h" |
| 40 | |
| 41 | /** |
| 42 | * cluster_find |
| 43 | */ |
| 44 | int cluster_find(ntfs_volume *vol, LCN c_begin, LCN c_end, cluster_cb *cb, void *data) |
| 45 | { |
| 46 | int j; |
| 47 | int result = -1; |
| 48 | struct mft_search_ctx *m_ctx = NULL; |
| 49 | ntfs_attr_search_ctx *a_ctx = NULL; |
Steve Kondik | 79165c3 | 2015-11-09 19:43:00 -0800 | [diff] [blame] | 50 | s64 count; |
| 51 | BOOL found; |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 52 | ATTR_RECORD *rec; |
| 53 | runlist *runs; |
| 54 | |
| 55 | if (!vol || !cb) |
| 56 | return -1; |
| 57 | |
| 58 | m_ctx = mft_get_search_ctx(vol); |
| 59 | m_ctx->flags_search = FEMR_IN_USE | FEMR_BASE_RECORD; |
Steve Kondik | 79165c3 | 2015-11-09 19:43:00 -0800 | [diff] [blame] | 60 | count = 0; |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 61 | |
| 62 | while (mft_next_record(m_ctx) == 0) { |
| 63 | |
| 64 | if (!(m_ctx->flags_match & FEMR_BASE_RECORD)) |
| 65 | continue; |
| 66 | |
| 67 | ntfs_log_verbose("Inode: %llu\n", (unsigned long long) |
| 68 | m_ctx->inode->mft_no); |
| 69 | |
| 70 | a_ctx = ntfs_attr_get_search_ctx(m_ctx->inode, NULL); |
| 71 | |
Steve Kondik | 79165c3 | 2015-11-09 19:43:00 -0800 | [diff] [blame] | 72 | found = FALSE; |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 73 | while ((rec = find_attribute(AT_UNUSED, a_ctx))) { |
| 74 | |
| 75 | if (!rec->non_resident) { |
Steve Kondik | 79165c3 | 2015-11-09 19:43:00 -0800 | [diff] [blame] | 76 | ntfs_log_verbose("0x%02x skipped - attr is resident\n", |
| 77 | (int)le32_to_cpu(a_ctx->attr->type)); |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 78 | continue; |
| 79 | } |
| 80 | |
| 81 | runs = ntfs_mapping_pairs_decompress(vol, a_ctx->attr, NULL); |
| 82 | if (!runs) { |
| 83 | ntfs_log_error("Couldn't read the data runs.\n"); |
| 84 | goto done; |
| 85 | } |
| 86 | |
Steve Kondik | 79165c3 | 2015-11-09 19:43:00 -0800 | [diff] [blame] | 87 | ntfs_log_verbose("\t[0x%02X]\n", |
| 88 | (int)le32_to_cpu(a_ctx->attr->type)); |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 89 | |
| 90 | ntfs_log_verbose("\t\tVCN\tLCN\tLength\n"); |
| 91 | for (j = 0; runs[j].length > 0; j++) { |
| 92 | LCN a_begin = runs[j].lcn; |
| 93 | LCN a_end = a_begin + runs[j].length - 1; |
| 94 | |
| 95 | if (a_begin < 0) |
| 96 | continue; // sparse, discontiguous, etc |
| 97 | |
| 98 | ntfs_log_verbose("\t\t%lld\t%lld-%lld (%lld)\n", |
| 99 | (long long)runs[j].vcn, |
| 100 | (long long)runs[j].lcn, |
| 101 | (long long)(runs[j].lcn + |
| 102 | runs[j].length - 1), |
| 103 | (long long)runs[j].length); |
| 104 | //dprint list |
| 105 | |
| 106 | if ((a_begin > c_end) || (a_end < c_begin)) |
| 107 | continue; // before or after search range |
| 108 | |
| 109 | if ((*cb) (m_ctx->inode, a_ctx->attr, runs+j, data)) |
| 110 | return 1; |
Steve Kondik | 79165c3 | 2015-11-09 19:43:00 -0800 | [diff] [blame] | 111 | found = TRUE; |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
| 115 | ntfs_attr_put_search_ctx(a_ctx); |
| 116 | a_ctx = NULL; |
Steve Kondik | 79165c3 | 2015-11-09 19:43:00 -0800 | [diff] [blame] | 117 | if (found) |
| 118 | count++; |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Steve Kondik | 79165c3 | 2015-11-09 19:43:00 -0800 | [diff] [blame] | 121 | if (count > 1) |
| 122 | ntfs_log_info("* %lld inodes found\n",(long long)count); |
| 123 | else |
| 124 | ntfs_log_info("* %s inode found\n", (count ? "one" : "no")); |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 125 | result = 0; |
| 126 | done: |
| 127 | ntfs_attr_put_search_ctx(a_ctx); |
| 128 | mft_put_search_ctx(m_ctx); |
| 129 | |
| 130 | return result; |
| 131 | } |
| 132 | |