Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 1 | /** |
| 2 | * index.c - NTFS index handling. Originated from the Linux-NTFS project. |
| 3 | * |
| 4 | * Copyright (c) 2004-2005 Anton Altaparmakov |
| 5 | * Copyright (c) 2004-2005 Richard Russon |
| 6 | * Copyright (c) 2005-2006 Yura Pakhuchiy |
| 7 | * Copyright (c) 2005-2008 Szabolcs Szakacsits |
| 8 | * Copyright (c) 2007 Jean-Pierre Andre |
| 9 | * |
| 10 | * This program/include file is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License as published |
| 12 | * by the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program/include file is distributed in the hope that it will be |
| 16 | * useful, but WITHOUT ANY WARRANTY; without even the implied warranty |
| 17 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program (in the main directory of the NTFS-3G |
| 22 | * distribution in the file COPYING); if not, write to the Free Software |
| 23 | * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 24 | */ |
| 25 | |
| 26 | #ifdef HAVE_CONFIG_H |
| 27 | #include "config.h" |
| 28 | #endif |
| 29 | |
| 30 | #ifdef HAVE_STDLIB_H |
| 31 | #include <stdlib.h> |
| 32 | #endif |
| 33 | #ifdef HAVE_STRING_H |
| 34 | #include <string.h> |
| 35 | #endif |
| 36 | #ifdef HAVE_ERRNO_H |
| 37 | #include <errno.h> |
| 38 | #endif |
| 39 | |
| 40 | #include "attrib.h" |
| 41 | #include "debug.h" |
| 42 | #include "index.h" |
| 43 | #include "collate.h" |
| 44 | #include "mst.h" |
| 45 | #include "dir.h" |
| 46 | #include "logging.h" |
| 47 | #include "bitmap.h" |
| 48 | #include "reparse.h" |
| 49 | #include "misc.h" |
| 50 | |
| 51 | /** |
| 52 | * ntfs_index_entry_mark_dirty - mark an index entry dirty |
| 53 | * @ictx: ntfs index context describing the index entry |
| 54 | * |
| 55 | * Mark the index entry described by the index entry context @ictx dirty. |
| 56 | * |
| 57 | * If the index entry is in the index root attribute, simply mark the inode |
| 58 | * containing the index root attribute dirty. This ensures the mftrecord, and |
| 59 | * hence the index root attribute, will be written out to disk later. |
| 60 | * |
| 61 | * If the index entry is in an index block belonging to the index allocation |
| 62 | * attribute, set ib_dirty to TRUE, thus index block will be updated during |
| 63 | * ntfs_index_ctx_put. |
| 64 | */ |
| 65 | void ntfs_index_entry_mark_dirty(ntfs_index_context *ictx) |
| 66 | { |
| 67 | if (ictx->is_in_root) |
| 68 | ntfs_inode_mark_dirty(ictx->actx->ntfs_ino); |
| 69 | else |
| 70 | ictx->ib_dirty = TRUE; |
| 71 | } |
| 72 | |
| 73 | static s64 ntfs_ib_vcn_to_pos(ntfs_index_context *icx, VCN vcn) |
| 74 | { |
| 75 | return vcn << icx->vcn_size_bits; |
| 76 | } |
| 77 | |
| 78 | static VCN ntfs_ib_pos_to_vcn(ntfs_index_context *icx, s64 pos) |
| 79 | { |
| 80 | return pos >> icx->vcn_size_bits; |
| 81 | } |
| 82 | |
| 83 | static int ntfs_ib_write(ntfs_index_context *icx, INDEX_BLOCK *ib) |
| 84 | { |
| 85 | s64 ret, vcn = sle64_to_cpu(ib->index_block_vcn); |
| 86 | |
| 87 | ntfs_log_trace("vcn: %lld\n", (long long)vcn); |
| 88 | |
| 89 | ret = ntfs_attr_mst_pwrite(icx->ia_na, ntfs_ib_vcn_to_pos(icx, vcn), |
| 90 | 1, icx->block_size, ib); |
| 91 | if (ret != 1) { |
| 92 | ntfs_log_perror("Failed to write index block %lld, inode %llu", |
| 93 | (long long)vcn, (unsigned long long)icx->ni->mft_no); |
| 94 | return STATUS_ERROR; |
| 95 | } |
| 96 | |
| 97 | return STATUS_OK; |
| 98 | } |
| 99 | |
| 100 | static int ntfs_icx_ib_write(ntfs_index_context *icx) |
| 101 | { |
| 102 | if (ntfs_ib_write(icx, icx->ib)) |
| 103 | return STATUS_ERROR; |
| 104 | |
| 105 | icx->ib_dirty = FALSE; |
| 106 | |
| 107 | return STATUS_OK; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * ntfs_index_ctx_get - allocate and initialize a new index context |
| 112 | * @ni: ntfs inode with which to initialize the context |
| 113 | * @name: name of the which context describes |
| 114 | * @name_len: length of the index name |
| 115 | * |
| 116 | * Allocate a new index context, initialize it with @ni and return it. |
| 117 | * Return NULL if allocation failed. |
| 118 | */ |
| 119 | ntfs_index_context *ntfs_index_ctx_get(ntfs_inode *ni, |
| 120 | ntfschar *name, u32 name_len) |
| 121 | { |
| 122 | ntfs_index_context *icx; |
| 123 | |
| 124 | ntfs_log_trace("Entering\n"); |
| 125 | |
| 126 | if (!ni) { |
| 127 | errno = EINVAL; |
| 128 | return NULL; |
| 129 | } |
| 130 | if (ni->nr_extents == -1) |
| 131 | ni = ni->base_ni; |
| 132 | icx = ntfs_calloc(sizeof(ntfs_index_context)); |
| 133 | if (icx) |
| 134 | *icx = (ntfs_index_context) { |
| 135 | .ni = ni, |
| 136 | .name = name, |
| 137 | .name_len = name_len, |
| 138 | }; |
| 139 | return icx; |
| 140 | } |
| 141 | |
| 142 | static void ntfs_index_ctx_free(ntfs_index_context *icx) |
| 143 | { |
| 144 | ntfs_log_trace("Entering\n"); |
| 145 | |
| 146 | if (!icx->entry) |
| 147 | return; |
| 148 | |
| 149 | if (icx->actx) |
| 150 | ntfs_attr_put_search_ctx(icx->actx); |
| 151 | |
| 152 | if (!icx->is_in_root) { |
| 153 | if (icx->ib_dirty) { |
| 154 | /* FIXME: Error handling!!! */ |
| 155 | ntfs_ib_write(icx, icx->ib); |
| 156 | } |
| 157 | free(icx->ib); |
| 158 | } |
| 159 | |
| 160 | ntfs_attr_close(icx->ia_na); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * ntfs_index_ctx_put - release an index context |
| 165 | * @icx: index context to free |
| 166 | * |
| 167 | * Release the index context @icx, releasing all associated resources. |
| 168 | */ |
| 169 | void ntfs_index_ctx_put(ntfs_index_context *icx) |
| 170 | { |
| 171 | ntfs_index_ctx_free(icx); |
| 172 | free(icx); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * ntfs_index_ctx_reinit - reinitialize an index context |
| 177 | * @icx: index context to reinitialize |
| 178 | * |
| 179 | * Reinitialize the index context @icx so it can be used for ntfs_index_lookup. |
| 180 | */ |
| 181 | void ntfs_index_ctx_reinit(ntfs_index_context *icx) |
| 182 | { |
| 183 | ntfs_log_trace("Entering\n"); |
| 184 | |
| 185 | ntfs_index_ctx_free(icx); |
| 186 | |
| 187 | *icx = (ntfs_index_context) { |
| 188 | .ni = icx->ni, |
| 189 | .name = icx->name, |
| 190 | .name_len = icx->name_len, |
| 191 | }; |
| 192 | } |
| 193 | |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 194 | static leVCN *ntfs_ie_get_vcn_addr(INDEX_ENTRY *ie) |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 195 | { |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 196 | return (leVCN *)((u8 *)ie + le16_to_cpu(ie->length) - sizeof(leVCN)); |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Get the subnode vcn to which the index entry refers. |
| 201 | */ |
| 202 | VCN ntfs_ie_get_vcn(INDEX_ENTRY *ie) |
| 203 | { |
| 204 | return sle64_to_cpup(ntfs_ie_get_vcn_addr(ie)); |
| 205 | } |
| 206 | |
| 207 | static INDEX_ENTRY *ntfs_ie_get_first(INDEX_HEADER *ih) |
| 208 | { |
| 209 | return (INDEX_ENTRY *)((u8 *)ih + le32_to_cpu(ih->entries_offset)); |
| 210 | } |
| 211 | |
| 212 | static INDEX_ENTRY *ntfs_ie_get_next(INDEX_ENTRY *ie) |
| 213 | { |
| 214 | return (INDEX_ENTRY *)((char *)ie + le16_to_cpu(ie->length)); |
| 215 | } |
| 216 | |
| 217 | static u8 *ntfs_ie_get_end(INDEX_HEADER *ih) |
| 218 | { |
| 219 | /* FIXME: check if it isn't overflowing the index block size */ |
| 220 | return (u8 *)ih + le32_to_cpu(ih->index_length); |
| 221 | } |
| 222 | |
| 223 | static int ntfs_ie_end(INDEX_ENTRY *ie) |
| 224 | { |
| 225 | return ie->ie_flags & INDEX_ENTRY_END || !ie->length; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Find the last entry in the index block |
| 230 | */ |
| 231 | static INDEX_ENTRY *ntfs_ie_get_last(INDEX_ENTRY *ie, char *ies_end) |
| 232 | { |
| 233 | ntfs_log_trace("Entering\n"); |
| 234 | |
| 235 | while ((char *)ie < ies_end && !ntfs_ie_end(ie)) |
| 236 | ie = ntfs_ie_get_next(ie); |
| 237 | |
| 238 | return ie; |
| 239 | } |
| 240 | |
| 241 | static INDEX_ENTRY *ntfs_ie_get_by_pos(INDEX_HEADER *ih, int pos) |
| 242 | { |
| 243 | INDEX_ENTRY *ie; |
| 244 | |
| 245 | ntfs_log_trace("pos: %d\n", pos); |
| 246 | |
| 247 | ie = ntfs_ie_get_first(ih); |
| 248 | |
| 249 | while (pos-- > 0) |
| 250 | ie = ntfs_ie_get_next(ie); |
| 251 | |
| 252 | return ie; |
| 253 | } |
| 254 | |
| 255 | static INDEX_ENTRY *ntfs_ie_prev(INDEX_HEADER *ih, INDEX_ENTRY *ie) |
| 256 | { |
| 257 | INDEX_ENTRY *ie_prev = NULL; |
| 258 | INDEX_ENTRY *tmp; |
| 259 | |
| 260 | ntfs_log_trace("Entering\n"); |
| 261 | |
| 262 | tmp = ntfs_ie_get_first(ih); |
| 263 | |
| 264 | while (tmp != ie) { |
| 265 | ie_prev = tmp; |
| 266 | tmp = ntfs_ie_get_next(tmp); |
| 267 | } |
| 268 | |
| 269 | return ie_prev; |
| 270 | } |
| 271 | |
| 272 | char *ntfs_ie_filename_get(INDEX_ENTRY *ie) |
| 273 | { |
| 274 | FILE_NAME_ATTR *fn; |
| 275 | |
| 276 | fn = (FILE_NAME_ATTR *)&ie->key; |
| 277 | return ntfs_attr_name_get(fn->file_name, fn->file_name_length); |
| 278 | } |
| 279 | |
| 280 | void ntfs_ie_filename_dump(INDEX_ENTRY *ie) |
| 281 | { |
| 282 | char *s; |
| 283 | |
| 284 | s = ntfs_ie_filename_get(ie); |
| 285 | ntfs_log_debug("'%s' ", s); |
| 286 | ntfs_attr_name_free(&s); |
| 287 | } |
| 288 | |
| 289 | void ntfs_ih_filename_dump(INDEX_HEADER *ih) |
| 290 | { |
| 291 | INDEX_ENTRY *ie; |
| 292 | |
| 293 | ntfs_log_trace("Entering\n"); |
| 294 | |
| 295 | ie = ntfs_ie_get_first(ih); |
| 296 | while (!ntfs_ie_end(ie)) { |
| 297 | ntfs_ie_filename_dump(ie); |
| 298 | ie = ntfs_ie_get_next(ie); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | static int ntfs_ih_numof_entries(INDEX_HEADER *ih) |
| 303 | { |
| 304 | int n; |
| 305 | INDEX_ENTRY *ie; |
| 306 | u8 *end; |
| 307 | |
| 308 | ntfs_log_trace("Entering\n"); |
| 309 | |
| 310 | end = ntfs_ie_get_end(ih); |
| 311 | ie = ntfs_ie_get_first(ih); |
| 312 | for (n = 0; !ntfs_ie_end(ie) && (u8 *)ie < end; n++) |
| 313 | ie = ntfs_ie_get_next(ie); |
| 314 | return n; |
| 315 | } |
| 316 | |
| 317 | static int ntfs_ih_one_entry(INDEX_HEADER *ih) |
| 318 | { |
| 319 | return (ntfs_ih_numof_entries(ih) == 1); |
| 320 | } |
| 321 | |
| 322 | static int ntfs_ih_zero_entry(INDEX_HEADER *ih) |
| 323 | { |
| 324 | return (ntfs_ih_numof_entries(ih) == 0); |
| 325 | } |
| 326 | |
| 327 | static void ntfs_ie_delete(INDEX_HEADER *ih, INDEX_ENTRY *ie) |
| 328 | { |
| 329 | u32 new_size; |
| 330 | |
| 331 | ntfs_log_trace("Entering\n"); |
| 332 | |
| 333 | new_size = le32_to_cpu(ih->index_length) - le16_to_cpu(ie->length); |
| 334 | ih->index_length = cpu_to_le32(new_size); |
| 335 | memmove(ie, (u8 *)ie + le16_to_cpu(ie->length), |
| 336 | new_size - ((u8 *)ie - (u8 *)ih)); |
| 337 | } |
| 338 | |
| 339 | static void ntfs_ie_set_vcn(INDEX_ENTRY *ie, VCN vcn) |
| 340 | { |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 341 | *ntfs_ie_get_vcn_addr(ie) = cpu_to_sle64(vcn); |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Insert @ie index entry at @pos entry. Used @ih values should be ok already. |
| 346 | */ |
| 347 | static void ntfs_ie_insert(INDEX_HEADER *ih, INDEX_ENTRY *ie, INDEX_ENTRY *pos) |
| 348 | { |
| 349 | int ie_size = le16_to_cpu(ie->length); |
| 350 | |
| 351 | ntfs_log_trace("Entering\n"); |
| 352 | |
| 353 | ih->index_length = cpu_to_le32(le32_to_cpu(ih->index_length) + ie_size); |
| 354 | memmove((u8 *)pos + ie_size, pos, |
| 355 | le32_to_cpu(ih->index_length) - ((u8 *)pos - (u8 *)ih) - ie_size); |
| 356 | memcpy(pos, ie, ie_size); |
| 357 | } |
| 358 | |
| 359 | static INDEX_ENTRY *ntfs_ie_dup(INDEX_ENTRY *ie) |
| 360 | { |
| 361 | INDEX_ENTRY *dup; |
| 362 | |
| 363 | ntfs_log_trace("Entering\n"); |
| 364 | |
| 365 | dup = ntfs_malloc(le16_to_cpu(ie->length)); |
| 366 | if (dup) |
| 367 | memcpy(dup, ie, le16_to_cpu(ie->length)); |
| 368 | |
| 369 | return dup; |
| 370 | } |
| 371 | |
| 372 | static INDEX_ENTRY *ntfs_ie_dup_novcn(INDEX_ENTRY *ie) |
| 373 | { |
| 374 | INDEX_ENTRY *dup; |
| 375 | int size = le16_to_cpu(ie->length); |
| 376 | |
| 377 | ntfs_log_trace("Entering\n"); |
| 378 | |
| 379 | if (ie->ie_flags & INDEX_ENTRY_NODE) |
| 380 | size -= sizeof(VCN); |
| 381 | |
| 382 | dup = ntfs_malloc(size); |
| 383 | if (dup) { |
| 384 | memcpy(dup, ie, size); |
| 385 | dup->ie_flags &= ~INDEX_ENTRY_NODE; |
| 386 | dup->length = cpu_to_le16(size); |
| 387 | } |
| 388 | return dup; |
| 389 | } |
| 390 | |
| 391 | static int ntfs_ia_check(ntfs_index_context *icx, INDEX_BLOCK *ib, VCN vcn) |
| 392 | { |
| 393 | u32 ib_size = (unsigned)le32_to_cpu(ib->index.allocated_size) + 0x18; |
| 394 | |
| 395 | ntfs_log_trace("Entering\n"); |
| 396 | |
| 397 | if (!ntfs_is_indx_record(ib->magic)) { |
| 398 | |
| 399 | ntfs_log_error("Corrupt index block signature: vcn %lld inode " |
| 400 | "%llu\n", (long long)vcn, |
| 401 | (unsigned long long)icx->ni->mft_no); |
| 402 | return -1; |
| 403 | } |
| 404 | |
| 405 | if (sle64_to_cpu(ib->index_block_vcn) != vcn) { |
| 406 | |
| 407 | ntfs_log_error("Corrupt index block: VCN (%lld) is different " |
| 408 | "from expected VCN (%lld) in inode %llu\n", |
| 409 | (long long)sle64_to_cpu(ib->index_block_vcn), |
| 410 | (long long)vcn, |
| 411 | (unsigned long long)icx->ni->mft_no); |
| 412 | return -1; |
| 413 | } |
| 414 | |
| 415 | if (ib_size != icx->block_size) { |
| 416 | |
| 417 | ntfs_log_error("Corrupt index block : VCN (%lld) of inode %llu " |
| 418 | "has a size (%u) differing from the index " |
| 419 | "specified size (%u)\n", (long long)vcn, |
| 420 | (unsigned long long)icx->ni->mft_no, ib_size, |
| 421 | icx->block_size); |
| 422 | return -1; |
| 423 | } |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | static INDEX_ROOT *ntfs_ir_lookup(ntfs_inode *ni, ntfschar *name, |
| 428 | u32 name_len, ntfs_attr_search_ctx **ctx) |
| 429 | { |
| 430 | ATTR_RECORD *a; |
| 431 | INDEX_ROOT *ir = NULL; |
| 432 | |
| 433 | ntfs_log_trace("Entering\n"); |
| 434 | |
| 435 | *ctx = ntfs_attr_get_search_ctx(ni, NULL); |
| 436 | if (!*ctx) |
| 437 | return NULL; |
| 438 | |
| 439 | if (ntfs_attr_lookup(AT_INDEX_ROOT, name, name_len, CASE_SENSITIVE, |
| 440 | 0, NULL, 0, *ctx)) { |
| 441 | ntfs_log_perror("Failed to lookup $INDEX_ROOT"); |
| 442 | goto err_out; |
| 443 | } |
| 444 | |
| 445 | a = (*ctx)->attr; |
| 446 | if (a->non_resident) { |
| 447 | errno = EINVAL; |
| 448 | ntfs_log_perror("Non-resident $INDEX_ROOT detected"); |
| 449 | goto err_out; |
| 450 | } |
| 451 | |
| 452 | ir = (INDEX_ROOT *)((char *)a + le16_to_cpu(a->value_offset)); |
| 453 | err_out: |
| 454 | if (!ir) { |
| 455 | ntfs_attr_put_search_ctx(*ctx); |
| 456 | *ctx = NULL; |
| 457 | } |
| 458 | return ir; |
| 459 | } |
| 460 | |
| 461 | static INDEX_ROOT *ntfs_ir_lookup2(ntfs_inode *ni, ntfschar *name, u32 len) |
| 462 | { |
| 463 | ntfs_attr_search_ctx *ctx; |
| 464 | INDEX_ROOT *ir; |
| 465 | |
| 466 | ir = ntfs_ir_lookup(ni, name, len, &ctx); |
| 467 | if (ir) |
| 468 | ntfs_attr_put_search_ctx(ctx); |
| 469 | return ir; |
| 470 | } |
| 471 | |
| 472 | /** |
| 473 | * Find a key in the index block. |
| 474 | * |
| 475 | * Return values: |
| 476 | * STATUS_OK with errno set to ESUCCESS if we know for sure that the |
| 477 | * entry exists and @ie_out points to this entry. |
| 478 | * STATUS_NOT_FOUND with errno set to ENOENT if we know for sure the |
| 479 | * entry doesn't exist and @ie_out is the insertion point. |
| 480 | * STATUS_KEEP_SEARCHING if we can't answer the above question and |
| 481 | * @vcn will contain the node index block. |
| 482 | * STATUS_ERROR with errno set if on unexpected error during lookup. |
| 483 | */ |
| 484 | static int ntfs_ie_lookup(const void *key, const int key_len, |
| 485 | ntfs_index_context *icx, INDEX_HEADER *ih, |
| 486 | VCN *vcn, INDEX_ENTRY **ie_out) |
| 487 | { |
| 488 | INDEX_ENTRY *ie; |
| 489 | u8 *index_end; |
| 490 | int rc, item = 0; |
| 491 | |
| 492 | ntfs_log_trace("Entering\n"); |
| 493 | |
| 494 | index_end = ntfs_ie_get_end(ih); |
| 495 | |
| 496 | /* |
| 497 | * Loop until we exceed valid memory (corruption case) or until we |
| 498 | * reach the last entry. |
| 499 | */ |
| 500 | for (ie = ntfs_ie_get_first(ih); ; ie = ntfs_ie_get_next(ie)) { |
| 501 | /* Bounds checks. */ |
| 502 | if ((u8 *)ie + sizeof(INDEX_ENTRY_HEADER) > index_end || |
| 503 | (u8 *)ie + le16_to_cpu(ie->length) > index_end) { |
| 504 | errno = ERANGE; |
| 505 | ntfs_log_error("Index entry out of bounds in inode " |
| 506 | "%llu.\n", |
| 507 | (unsigned long long)icx->ni->mft_no); |
| 508 | return STATUS_ERROR; |
| 509 | } |
| 510 | /* |
| 511 | * The last entry cannot contain a key. It can however contain |
| 512 | * a pointer to a child node in the B+tree so we just break out. |
| 513 | */ |
| 514 | if (ntfs_ie_end(ie)) |
| 515 | break; |
| 516 | /* |
| 517 | * Not a perfect match, need to do full blown collation so we |
| 518 | * know which way in the B+tree we have to go. |
| 519 | */ |
| 520 | if (!icx->collate) { |
| 521 | ntfs_log_error("Collation function not defined\n"); |
| 522 | errno = EOPNOTSUPP; |
| 523 | return STATUS_ERROR; |
| 524 | } |
| 525 | rc = icx->collate(icx->ni->vol, key, key_len, |
| 526 | &ie->key, le16_to_cpu(ie->key_length)); |
| 527 | if (rc == NTFS_COLLATION_ERROR) { |
| 528 | ntfs_log_error("Collation error. Perhaps a filename " |
| 529 | "contains invalid characters?\n"); |
| 530 | errno = ERANGE; |
| 531 | return STATUS_ERROR; |
| 532 | } |
| 533 | /* |
| 534 | * If @key collates before the key of the current entry, there |
| 535 | * is definitely no such key in this index but we might need to |
| 536 | * descend into the B+tree so we just break out of the loop. |
| 537 | */ |
| 538 | if (rc == -1) |
| 539 | break; |
| 540 | |
| 541 | if (!rc) { |
| 542 | *ie_out = ie; |
| 543 | errno = 0; |
| 544 | icx->parent_pos[icx->pindex] = item; |
| 545 | return STATUS_OK; |
| 546 | } |
| 547 | |
| 548 | item++; |
| 549 | } |
| 550 | /* |
| 551 | * We have finished with this index block without success. Check for the |
| 552 | * presence of a child node and if not present return with errno ENOENT, |
| 553 | * otherwise we will keep searching in another index block. |
| 554 | */ |
| 555 | if (!(ie->ie_flags & INDEX_ENTRY_NODE)) { |
| 556 | ntfs_log_debug("Index entry wasn't found.\n"); |
| 557 | *ie_out = ie; |
| 558 | errno = ENOENT; |
| 559 | return STATUS_NOT_FOUND; |
| 560 | } |
| 561 | |
| 562 | /* Get the starting vcn of the index_block holding the child node. */ |
| 563 | *vcn = ntfs_ie_get_vcn(ie); |
| 564 | if (*vcn < 0) { |
| 565 | errno = EINVAL; |
| 566 | ntfs_log_perror("Negative vcn in inode %llu", |
| 567 | (unsigned long long)icx->ni->mft_no); |
| 568 | return STATUS_ERROR; |
| 569 | } |
| 570 | |
| 571 | ntfs_log_trace("Parent entry number %d\n", item); |
| 572 | icx->parent_pos[icx->pindex] = item; |
| 573 | |
| 574 | return STATUS_KEEP_SEARCHING; |
| 575 | } |
| 576 | |
| 577 | static ntfs_attr *ntfs_ia_open(ntfs_index_context *icx, ntfs_inode *ni) |
| 578 | { |
| 579 | ntfs_attr *na; |
| 580 | |
| 581 | na = ntfs_attr_open(ni, AT_INDEX_ALLOCATION, icx->name, icx->name_len); |
| 582 | if (!na) { |
| 583 | ntfs_log_perror("Failed to open index allocation of inode " |
| 584 | "%llu", (unsigned long long)ni->mft_no); |
| 585 | return NULL; |
| 586 | } |
| 587 | |
| 588 | return na; |
| 589 | } |
| 590 | |
| 591 | static int ntfs_ib_read(ntfs_index_context *icx, VCN vcn, INDEX_BLOCK *dst) |
| 592 | { |
| 593 | s64 pos, ret; |
| 594 | |
| 595 | ntfs_log_trace("vcn: %lld\n", (long long)vcn); |
| 596 | |
| 597 | pos = ntfs_ib_vcn_to_pos(icx, vcn); |
| 598 | |
| 599 | ret = ntfs_attr_mst_pread(icx->ia_na, pos, 1, icx->block_size, (u8 *)dst); |
| 600 | if (ret != 1) { |
| 601 | if (ret == -1) |
| 602 | ntfs_log_perror("Failed to read index block"); |
| 603 | else |
| 604 | ntfs_log_error("Failed to read full index block at " |
| 605 | "%lld\n", (long long)pos); |
| 606 | return -1; |
| 607 | } |
| 608 | |
| 609 | if (ntfs_ia_check(icx, dst, vcn)) |
| 610 | return -1; |
| 611 | |
| 612 | return 0; |
| 613 | } |
| 614 | |
| 615 | static int ntfs_icx_parent_inc(ntfs_index_context *icx) |
| 616 | { |
| 617 | icx->pindex++; |
| 618 | if (icx->pindex >= MAX_PARENT_VCN) { |
| 619 | errno = EOPNOTSUPP; |
| 620 | ntfs_log_perror("Index is over %d level deep", MAX_PARENT_VCN); |
| 621 | return STATUS_ERROR; |
| 622 | } |
| 623 | return STATUS_OK; |
| 624 | } |
| 625 | |
| 626 | static int ntfs_icx_parent_dec(ntfs_index_context *icx) |
| 627 | { |
| 628 | icx->pindex--; |
| 629 | if (icx->pindex < 0) { |
| 630 | errno = EINVAL; |
| 631 | ntfs_log_perror("Corrupt index pointer (%d)", icx->pindex); |
| 632 | return STATUS_ERROR; |
| 633 | } |
| 634 | return STATUS_OK; |
| 635 | } |
| 636 | |
| 637 | /** |
| 638 | * ntfs_index_lookup - find a key in an index and return its index entry |
| 639 | * @key: [IN] key for which to search in the index |
| 640 | * @key_len: [IN] length of @key in bytes |
| 641 | * @icx: [IN/OUT] context describing the index and the returned entry |
| 642 | * |
| 643 | * Before calling ntfs_index_lookup(), @icx must have been obtained from a |
| 644 | * call to ntfs_index_ctx_get(). |
| 645 | * |
| 646 | * Look for the @key in the index specified by the index lookup context @icx. |
| 647 | * ntfs_index_lookup() walks the contents of the index looking for the @key. |
| 648 | * |
| 649 | * If the @key is found in the index, 0 is returned and @icx is setup to |
| 650 | * describe the index entry containing the matching @key. @icx->entry is the |
| 651 | * index entry and @icx->data and @icx->data_len are the index entry data and |
| 652 | * its length in bytes, respectively. |
| 653 | * |
| 654 | * If the @key is not found in the index, -1 is returned, errno = ENOENT and |
| 655 | * @icx is setup to describe the index entry whose key collates immediately |
| 656 | * after the search @key, i.e. this is the position in the index at which |
| 657 | * an index entry with a key of @key would need to be inserted. |
| 658 | * |
| 659 | * If an error occurs return -1, set errno to error code and @icx is left |
| 660 | * untouched. |
| 661 | * |
| 662 | * When finished with the entry and its data, call ntfs_index_ctx_put() to free |
| 663 | * the context and other associated resources. |
| 664 | * |
| 665 | * If the index entry was modified, call ntfs_index_entry_mark_dirty() before |
| 666 | * the call to ntfs_index_ctx_put() to ensure that the changes are written |
| 667 | * to disk. |
| 668 | */ |
| 669 | int ntfs_index_lookup(const void *key, const int key_len, ntfs_index_context *icx) |
| 670 | { |
| 671 | VCN old_vcn, vcn; |
| 672 | ntfs_inode *ni = icx->ni; |
| 673 | INDEX_ROOT *ir; |
| 674 | INDEX_ENTRY *ie; |
| 675 | INDEX_BLOCK *ib = NULL; |
| 676 | int ret, err = 0; |
| 677 | |
| 678 | ntfs_log_trace("Entering\n"); |
| 679 | |
| 680 | if (!key || key_len <= 0) { |
| 681 | errno = EINVAL; |
| 682 | ntfs_log_perror("key: %p key_len: %d", key, key_len); |
| 683 | return -1; |
| 684 | } |
| 685 | |
| 686 | ir = ntfs_ir_lookup(ni, icx->name, icx->name_len, &icx->actx); |
| 687 | if (!ir) { |
| 688 | if (errno == ENOENT) |
| 689 | errno = EIO; |
| 690 | return -1; |
| 691 | } |
| 692 | |
| 693 | icx->block_size = le32_to_cpu(ir->index_block_size); |
| 694 | if (icx->block_size < NTFS_BLOCK_SIZE) { |
| 695 | errno = EINVAL; |
| 696 | ntfs_log_perror("Index block size (%d) is smaller than the " |
| 697 | "sector size (%d)", icx->block_size, NTFS_BLOCK_SIZE); |
| 698 | goto err_out; |
| 699 | } |
| 700 | |
| 701 | if (ni->vol->cluster_size <= icx->block_size) |
| 702 | icx->vcn_size_bits = ni->vol->cluster_size_bits; |
| 703 | else |
| 704 | icx->vcn_size_bits = NTFS_BLOCK_SIZE_BITS; |
| 705 | /* get the appropriate collation function */ |
| 706 | icx->collate = ntfs_get_collate_function(ir->collation_rule); |
| 707 | if (!icx->collate) { |
| 708 | err = errno = EOPNOTSUPP; |
| 709 | ntfs_log_perror("Unknown collation rule 0x%x", |
| 710 | (unsigned)le32_to_cpu(ir->collation_rule)); |
| 711 | goto err_out; |
| 712 | } |
| 713 | |
| 714 | old_vcn = VCN_INDEX_ROOT_PARENT; |
| 715 | /* |
| 716 | * FIXME: check for both ir and ib that the first index entry is |
| 717 | * within the index block. |
| 718 | */ |
| 719 | ret = ntfs_ie_lookup(key, key_len, icx, &ir->index, &vcn, &ie); |
| 720 | if (ret == STATUS_ERROR) { |
| 721 | err = errno; |
| 722 | goto err_out; |
| 723 | } |
| 724 | |
| 725 | icx->ir = ir; |
| 726 | |
| 727 | if (ret != STATUS_KEEP_SEARCHING) { |
| 728 | /* STATUS_OK or STATUS_NOT_FOUND */ |
| 729 | err = errno; |
| 730 | icx->is_in_root = TRUE; |
| 731 | icx->parent_vcn[icx->pindex] = old_vcn; |
| 732 | goto done; |
| 733 | } |
| 734 | |
| 735 | /* Child node present, descend into it. */ |
| 736 | |
| 737 | icx->ia_na = ntfs_ia_open(icx, ni); |
| 738 | if (!icx->ia_na) |
| 739 | goto err_out; |
| 740 | |
| 741 | ib = ntfs_malloc(icx->block_size); |
| 742 | if (!ib) { |
| 743 | err = errno; |
| 744 | goto err_out; |
| 745 | } |
| 746 | |
| 747 | descend_into_child_node: |
| 748 | |
| 749 | icx->parent_vcn[icx->pindex] = old_vcn; |
| 750 | if (ntfs_icx_parent_inc(icx)) { |
| 751 | err = errno; |
| 752 | goto err_out; |
| 753 | } |
| 754 | old_vcn = vcn; |
| 755 | |
| 756 | ntfs_log_debug("Descend into node with VCN %lld\n", (long long)vcn); |
| 757 | |
| 758 | if (ntfs_ib_read(icx, vcn, ib)) |
| 759 | goto err_out; |
| 760 | |
| 761 | ret = ntfs_ie_lookup(key, key_len, icx, &ib->index, &vcn, &ie); |
| 762 | if (ret != STATUS_KEEP_SEARCHING) { |
| 763 | err = errno; |
| 764 | if (ret == STATUS_ERROR) |
| 765 | goto err_out; |
| 766 | |
| 767 | /* STATUS_OK or STATUS_NOT_FOUND */ |
| 768 | icx->is_in_root = FALSE; |
| 769 | icx->ib = ib; |
| 770 | icx->parent_vcn[icx->pindex] = vcn; |
| 771 | goto done; |
| 772 | } |
| 773 | |
| 774 | if ((ib->index.ih_flags & NODE_MASK) == LEAF_NODE) { |
| 775 | ntfs_log_error("Index entry with child node found in a leaf " |
| 776 | "node in inode 0x%llx.\n", |
| 777 | (unsigned long long)ni->mft_no); |
| 778 | goto err_out; |
| 779 | } |
| 780 | |
| 781 | goto descend_into_child_node; |
| 782 | err_out: |
| 783 | free(ib); |
| 784 | if (!err) |
| 785 | err = EIO; |
| 786 | errno = err; |
| 787 | return -1; |
| 788 | done: |
| 789 | icx->entry = ie; |
| 790 | icx->data = (u8 *)ie + offsetof(INDEX_ENTRY, key); |
| 791 | icx->data_len = le16_to_cpu(ie->key_length); |
| 792 | ntfs_log_trace("Done.\n"); |
| 793 | if (err) { |
| 794 | errno = err; |
| 795 | return -1; |
| 796 | } |
| 797 | return 0; |
| 798 | |
| 799 | } |
| 800 | |
| 801 | static INDEX_BLOCK *ntfs_ib_alloc(VCN ib_vcn, u32 ib_size, |
| 802 | INDEX_HEADER_FLAGS node_type) |
| 803 | { |
| 804 | INDEX_BLOCK *ib; |
| 805 | int ih_size = sizeof(INDEX_HEADER); |
| 806 | |
| 807 | ntfs_log_trace("ib_vcn: %lld ib_size: %u\n", (long long)ib_vcn, ib_size); |
| 808 | |
| 809 | ib = ntfs_calloc(ib_size); |
| 810 | if (!ib) |
| 811 | return NULL; |
| 812 | |
| 813 | ib->magic = magic_INDX; |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 814 | ib->usa_ofs = const_cpu_to_le16(sizeof(INDEX_BLOCK)); |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 815 | ib->usa_count = cpu_to_le16(ib_size / NTFS_BLOCK_SIZE + 1); |
| 816 | /* Set USN to 1 */ |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 817 | *(le16 *)((char *)ib + le16_to_cpu(ib->usa_ofs)) = const_cpu_to_le16(1); |
| 818 | ib->lsn = const_cpu_to_sle64(0); |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 819 | |
| 820 | ib->index_block_vcn = cpu_to_sle64(ib_vcn); |
| 821 | |
| 822 | ib->index.entries_offset = cpu_to_le32((ih_size + |
| 823 | le16_to_cpu(ib->usa_count) * 2 + 7) & ~7); |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 824 | ib->index.index_length = const_cpu_to_le32(0); |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 825 | ib->index.allocated_size = cpu_to_le32(ib_size - |
| 826 | (sizeof(INDEX_BLOCK) - ih_size)); |
| 827 | ib->index.ih_flags = node_type; |
| 828 | |
| 829 | return ib; |
| 830 | } |
| 831 | |
| 832 | /** |
| 833 | * Find the median by going through all the entries |
| 834 | */ |
| 835 | static INDEX_ENTRY *ntfs_ie_get_median(INDEX_HEADER *ih) |
| 836 | { |
| 837 | INDEX_ENTRY *ie, *ie_start; |
| 838 | u8 *ie_end; |
| 839 | int i = 0, median; |
| 840 | |
| 841 | ntfs_log_trace("Entering\n"); |
| 842 | |
| 843 | ie = ie_start = ntfs_ie_get_first(ih); |
| 844 | ie_end = (u8 *)ntfs_ie_get_end(ih); |
| 845 | |
| 846 | while ((u8 *)ie < ie_end && !ntfs_ie_end(ie)) { |
| 847 | ie = ntfs_ie_get_next(ie); |
| 848 | i++; |
| 849 | } |
| 850 | /* |
| 851 | * NOTE: this could be also the entry at the half of the index block. |
| 852 | */ |
| 853 | median = i / 2 - 1; |
| 854 | |
| 855 | ntfs_log_trace("Entries: %d median: %d\n", i, median); |
| 856 | |
| 857 | for (i = 0, ie = ie_start; i <= median; i++) |
| 858 | ie = ntfs_ie_get_next(ie); |
| 859 | |
| 860 | return ie; |
| 861 | } |
| 862 | |
| 863 | static s64 ntfs_ibm_vcn_to_pos(ntfs_index_context *icx, VCN vcn) |
| 864 | { |
| 865 | return ntfs_ib_vcn_to_pos(icx, vcn) / icx->block_size; |
| 866 | } |
| 867 | |
| 868 | static s64 ntfs_ibm_pos_to_vcn(ntfs_index_context *icx, s64 pos) |
| 869 | { |
| 870 | return ntfs_ib_pos_to_vcn(icx, pos * icx->block_size); |
| 871 | } |
| 872 | |
| 873 | static int ntfs_ibm_add(ntfs_index_context *icx) |
| 874 | { |
| 875 | u8 bmp[8]; |
| 876 | |
| 877 | ntfs_log_trace("Entering\n"); |
| 878 | |
| 879 | if (ntfs_attr_exist(icx->ni, AT_BITMAP, icx->name, icx->name_len)) |
| 880 | return STATUS_OK; |
| 881 | /* |
| 882 | * AT_BITMAP must be at least 8 bytes. |
| 883 | */ |
| 884 | memset(bmp, 0, sizeof(bmp)); |
| 885 | if (ntfs_attr_add(icx->ni, AT_BITMAP, icx->name, icx->name_len, |
| 886 | bmp, sizeof(bmp))) { |
| 887 | ntfs_log_perror("Failed to add AT_BITMAP"); |
| 888 | return STATUS_ERROR; |
| 889 | } |
| 890 | |
| 891 | return STATUS_OK; |
| 892 | } |
| 893 | |
| 894 | static int ntfs_ibm_modify(ntfs_index_context *icx, VCN vcn, int set) |
| 895 | { |
| 896 | u8 byte; |
| 897 | s64 pos = ntfs_ibm_vcn_to_pos(icx, vcn); |
| 898 | u32 bpos = pos / 8; |
| 899 | u32 bit = 1 << (pos % 8); |
| 900 | ntfs_attr *na; |
| 901 | int ret = STATUS_ERROR; |
| 902 | |
| 903 | ntfs_log_trace("%s vcn: %lld\n", set ? "set" : "clear", (long long)vcn); |
| 904 | |
| 905 | na = ntfs_attr_open(icx->ni, AT_BITMAP, icx->name, icx->name_len); |
| 906 | if (!na) { |
| 907 | ntfs_log_perror("Failed to open $BITMAP attribute"); |
| 908 | return -1; |
| 909 | } |
| 910 | |
| 911 | if (set) { |
| 912 | if (na->data_size < bpos + 1) { |
| 913 | if (ntfs_attr_truncate(na, (na->data_size + 8) & ~7)) { |
| 914 | ntfs_log_perror("Failed to truncate AT_BITMAP"); |
| 915 | goto err_na; |
| 916 | } |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | if (ntfs_attr_pread(na, bpos, 1, &byte) != 1) { |
| 921 | ntfs_log_perror("Failed to read $BITMAP"); |
| 922 | goto err_na; |
| 923 | } |
| 924 | |
| 925 | if (set) |
| 926 | byte |= bit; |
| 927 | else |
| 928 | byte &= ~bit; |
| 929 | |
| 930 | if (ntfs_attr_pwrite(na, bpos, 1, &byte) != 1) { |
| 931 | ntfs_log_perror("Failed to write $Bitmap"); |
| 932 | goto err_na; |
| 933 | } |
| 934 | |
| 935 | ret = STATUS_OK; |
| 936 | err_na: |
| 937 | ntfs_attr_close(na); |
| 938 | return ret; |
| 939 | } |
| 940 | |
| 941 | |
| 942 | static int ntfs_ibm_set(ntfs_index_context *icx, VCN vcn) |
| 943 | { |
| 944 | return ntfs_ibm_modify(icx, vcn, 1); |
| 945 | } |
| 946 | |
| 947 | static int ntfs_ibm_clear(ntfs_index_context *icx, VCN vcn) |
| 948 | { |
| 949 | return ntfs_ibm_modify(icx, vcn, 0); |
| 950 | } |
| 951 | |
| 952 | static VCN ntfs_ibm_get_free(ntfs_index_context *icx) |
| 953 | { |
| 954 | u8 *bm; |
| 955 | int bit; |
| 956 | s64 vcn, byte, size; |
| 957 | |
| 958 | ntfs_log_trace("Entering\n"); |
| 959 | |
| 960 | bm = ntfs_attr_readall(icx->ni, AT_BITMAP, icx->name, icx->name_len, |
| 961 | &size); |
| 962 | if (!bm) |
| 963 | return (VCN)-1; |
| 964 | |
| 965 | for (byte = 0; byte < size; byte++) { |
| 966 | |
| 967 | if (bm[byte] == 255) |
| 968 | continue; |
| 969 | |
| 970 | for (bit = 0; bit < 8; bit++) { |
| 971 | if (!(bm[byte] & (1 << bit))) { |
| 972 | vcn = ntfs_ibm_pos_to_vcn(icx, byte * 8 + bit); |
| 973 | goto out; |
| 974 | } |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | vcn = ntfs_ibm_pos_to_vcn(icx, size * 8); |
| 979 | out: |
| 980 | ntfs_log_trace("allocated vcn: %lld\n", (long long)vcn); |
| 981 | |
| 982 | if (ntfs_ibm_set(icx, vcn)) |
| 983 | vcn = (VCN)-1; |
| 984 | |
| 985 | free(bm); |
| 986 | return vcn; |
| 987 | } |
| 988 | |
| 989 | static INDEX_BLOCK *ntfs_ir_to_ib(INDEX_ROOT *ir, VCN ib_vcn) |
| 990 | { |
| 991 | INDEX_BLOCK *ib; |
| 992 | INDEX_ENTRY *ie_last; |
| 993 | char *ies_start, *ies_end; |
| 994 | int i; |
| 995 | |
| 996 | ntfs_log_trace("Entering\n"); |
| 997 | |
| 998 | ib = ntfs_ib_alloc(ib_vcn, le32_to_cpu(ir->index_block_size), LEAF_NODE); |
| 999 | if (!ib) |
| 1000 | return NULL; |
| 1001 | |
| 1002 | ies_start = (char *)ntfs_ie_get_first(&ir->index); |
| 1003 | ies_end = (char *)ntfs_ie_get_end(&ir->index); |
| 1004 | ie_last = ntfs_ie_get_last((INDEX_ENTRY *)ies_start, ies_end); |
| 1005 | /* |
| 1006 | * Copy all entries, including the termination entry |
| 1007 | * as well, which can never have any data. |
| 1008 | */ |
| 1009 | i = (char *)ie_last - ies_start + le16_to_cpu(ie_last->length); |
| 1010 | memcpy(ntfs_ie_get_first(&ib->index), ies_start, i); |
| 1011 | |
| 1012 | ib->index.ih_flags = ir->index.ih_flags; |
| 1013 | ib->index.index_length = cpu_to_le32(i + |
| 1014 | le32_to_cpu(ib->index.entries_offset)); |
| 1015 | return ib; |
| 1016 | } |
| 1017 | |
| 1018 | static void ntfs_ir_nill(INDEX_ROOT *ir) |
| 1019 | { |
| 1020 | INDEX_ENTRY *ie_last; |
| 1021 | char *ies_start, *ies_end; |
| 1022 | |
| 1023 | ntfs_log_trace("Entering\n"); |
| 1024 | /* |
| 1025 | * TODO: This function could be much simpler. |
| 1026 | */ |
| 1027 | ies_start = (char *)ntfs_ie_get_first(&ir->index); |
| 1028 | ies_end = (char *)ntfs_ie_get_end(&ir->index); |
| 1029 | ie_last = ntfs_ie_get_last((INDEX_ENTRY *)ies_start, ies_end); |
| 1030 | /* |
| 1031 | * Move the index root termination entry forward |
| 1032 | */ |
| 1033 | if ((char *)ie_last > ies_start) { |
| 1034 | memmove(ies_start, (char *)ie_last, le16_to_cpu(ie_last->length)); |
| 1035 | ie_last = (INDEX_ENTRY *)ies_start; |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | static int ntfs_ib_copy_tail(ntfs_index_context *icx, INDEX_BLOCK *src, |
| 1040 | INDEX_ENTRY *median, VCN new_vcn) |
| 1041 | { |
| 1042 | u8 *ies_end; |
| 1043 | INDEX_ENTRY *ie_head; /* first entry after the median */ |
| 1044 | int tail_size, ret; |
| 1045 | INDEX_BLOCK *dst; |
| 1046 | |
| 1047 | ntfs_log_trace("Entering\n"); |
| 1048 | |
| 1049 | dst = ntfs_ib_alloc(new_vcn, icx->block_size, |
| 1050 | src->index.ih_flags & NODE_MASK); |
| 1051 | if (!dst) |
| 1052 | return STATUS_ERROR; |
| 1053 | |
| 1054 | ie_head = ntfs_ie_get_next(median); |
| 1055 | |
| 1056 | ies_end = (u8 *)ntfs_ie_get_end(&src->index); |
| 1057 | tail_size = ies_end - (u8 *)ie_head; |
| 1058 | memcpy(ntfs_ie_get_first(&dst->index), ie_head, tail_size); |
| 1059 | |
| 1060 | dst->index.index_length = cpu_to_le32(tail_size + |
| 1061 | le32_to_cpu(dst->index.entries_offset)); |
| 1062 | ret = ntfs_ib_write(icx, dst); |
| 1063 | |
| 1064 | free(dst); |
| 1065 | return ret; |
| 1066 | } |
| 1067 | |
| 1068 | static int ntfs_ib_cut_tail(ntfs_index_context *icx, INDEX_BLOCK *ib, |
| 1069 | INDEX_ENTRY *ie) |
| 1070 | { |
| 1071 | char *ies_start, *ies_end; |
| 1072 | INDEX_ENTRY *ie_last; |
| 1073 | |
| 1074 | ntfs_log_trace("Entering\n"); |
| 1075 | |
| 1076 | ies_start = (char *)ntfs_ie_get_first(&ib->index); |
| 1077 | ies_end = (char *)ntfs_ie_get_end(&ib->index); |
| 1078 | |
| 1079 | ie_last = ntfs_ie_get_last((INDEX_ENTRY *)ies_start, ies_end); |
| 1080 | if (ie_last->ie_flags & INDEX_ENTRY_NODE) |
| 1081 | ntfs_ie_set_vcn(ie_last, ntfs_ie_get_vcn(ie)); |
| 1082 | |
| 1083 | memcpy(ie, ie_last, le16_to_cpu(ie_last->length)); |
| 1084 | |
| 1085 | ib->index.index_length = cpu_to_le32(((char *)ie - ies_start) + |
| 1086 | le16_to_cpu(ie->length) + le32_to_cpu(ib->index.entries_offset)); |
| 1087 | |
| 1088 | if (ntfs_ib_write(icx, ib)) |
| 1089 | return STATUS_ERROR; |
| 1090 | |
| 1091 | return STATUS_OK; |
| 1092 | } |
| 1093 | |
| 1094 | static int ntfs_ia_add(ntfs_index_context *icx) |
| 1095 | { |
| 1096 | ntfs_log_trace("Entering\n"); |
| 1097 | |
| 1098 | if (ntfs_ibm_add(icx)) |
| 1099 | return -1; |
| 1100 | |
| 1101 | if (!ntfs_attr_exist(icx->ni, AT_INDEX_ALLOCATION, icx->name, icx->name_len)) { |
| 1102 | |
| 1103 | if (ntfs_attr_add(icx->ni, AT_INDEX_ALLOCATION, icx->name, |
| 1104 | icx->name_len, NULL, 0)) { |
| 1105 | ntfs_log_perror("Failed to add AT_INDEX_ALLOCATION"); |
| 1106 | return -1; |
| 1107 | } |
| 1108 | } |
| 1109 | |
| 1110 | icx->ia_na = ntfs_ia_open(icx, icx->ni); |
| 1111 | if (!icx->ia_na) |
| 1112 | return -1; |
| 1113 | |
| 1114 | return 0; |
| 1115 | } |
| 1116 | |
| 1117 | static int ntfs_ir_reparent(ntfs_index_context *icx) |
| 1118 | { |
| 1119 | ntfs_attr_search_ctx *ctx = NULL; |
| 1120 | INDEX_ROOT *ir; |
| 1121 | INDEX_ENTRY *ie; |
| 1122 | INDEX_BLOCK *ib = NULL; |
| 1123 | VCN new_ib_vcn; |
| 1124 | int ix_root_size; |
| 1125 | int ret = STATUS_ERROR; |
| 1126 | |
| 1127 | ntfs_log_trace("Entering\n"); |
| 1128 | |
| 1129 | ir = ntfs_ir_lookup2(icx->ni, icx->name, icx->name_len); |
| 1130 | if (!ir) |
| 1131 | goto out; |
| 1132 | |
| 1133 | if ((ir->index.ih_flags & NODE_MASK) == SMALL_INDEX) |
| 1134 | if (ntfs_ia_add(icx)) |
| 1135 | goto out; |
| 1136 | |
| 1137 | new_ib_vcn = ntfs_ibm_get_free(icx); |
| 1138 | if (new_ib_vcn == -1) |
| 1139 | goto out; |
| 1140 | |
| 1141 | ir = ntfs_ir_lookup2(icx->ni, icx->name, icx->name_len); |
| 1142 | if (!ir) |
| 1143 | goto clear_bmp; |
| 1144 | |
| 1145 | ib = ntfs_ir_to_ib(ir, new_ib_vcn); |
| 1146 | if (ib == NULL) { |
| 1147 | ntfs_log_perror("Failed to move index root to index block"); |
| 1148 | goto clear_bmp; |
| 1149 | } |
| 1150 | |
| 1151 | if (ntfs_ib_write(icx, ib)) |
| 1152 | goto clear_bmp; |
| 1153 | |
| 1154 | retry : |
| 1155 | ir = ntfs_ir_lookup(icx->ni, icx->name, icx->name_len, &ctx); |
| 1156 | if (!ir) |
| 1157 | goto clear_bmp; |
| 1158 | |
| 1159 | ntfs_ir_nill(ir); |
| 1160 | |
| 1161 | ie = ntfs_ie_get_first(&ir->index); |
| 1162 | ie->ie_flags |= INDEX_ENTRY_NODE; |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 1163 | ie->length = const_cpu_to_le16(sizeof(INDEX_ENTRY_HEADER) + sizeof(VCN)); |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 1164 | |
| 1165 | ir->index.ih_flags = LARGE_INDEX; |
| 1166 | ir->index.index_length = cpu_to_le32(le32_to_cpu(ir->index.entries_offset) |
| 1167 | + le16_to_cpu(ie->length)); |
| 1168 | ir->index.allocated_size = ir->index.index_length; |
| 1169 | ix_root_size = sizeof(INDEX_ROOT) - sizeof(INDEX_HEADER) |
| 1170 | + le32_to_cpu(ir->index.allocated_size); |
| 1171 | if (ntfs_resident_attr_value_resize(ctx->mrec, ctx->attr, |
| 1172 | ix_root_size)) { |
| 1173 | /* |
| 1174 | * When there is no space to build a non-resident |
| 1175 | * index, we may have to move the root to an extent |
| 1176 | */ |
| 1177 | if ((errno == ENOSPC) |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 1178 | && (ctx->al_entry || !ntfs_inode_add_attrlist(icx->ni))) { |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 1179 | ntfs_attr_put_search_ctx(ctx); |
| 1180 | ctx = (ntfs_attr_search_ctx*)NULL; |
| 1181 | ir = ntfs_ir_lookup(icx->ni, icx->name, icx->name_len, |
| 1182 | &ctx); |
| 1183 | if (ir |
| 1184 | && !ntfs_attr_record_move_away(ctx, ix_root_size |
| 1185 | - le32_to_cpu(ctx->attr->value_length))) { |
| 1186 | ntfs_attr_put_search_ctx(ctx); |
| 1187 | ctx = (ntfs_attr_search_ctx*)NULL; |
| 1188 | goto retry; |
| 1189 | } |
| 1190 | } |
| 1191 | /* FIXME: revert index root */ |
| 1192 | goto clear_bmp; |
| 1193 | } |
| 1194 | /* |
| 1195 | * FIXME: do it earlier if we have enough space in IR (should always), |
| 1196 | * so in error case we wouldn't lose the IB. |
| 1197 | */ |
| 1198 | ntfs_ie_set_vcn(ie, new_ib_vcn); |
| 1199 | |
| 1200 | ret = STATUS_OK; |
| 1201 | err_out: |
| 1202 | free(ib); |
| 1203 | ntfs_attr_put_search_ctx(ctx); |
| 1204 | out: |
| 1205 | return ret; |
| 1206 | clear_bmp: |
| 1207 | ntfs_ibm_clear(icx, new_ib_vcn); |
| 1208 | goto err_out; |
| 1209 | } |
| 1210 | |
| 1211 | /** |
| 1212 | * ntfs_ir_truncate - Truncate index root attribute |
| 1213 | * |
| 1214 | * Returns STATUS_OK, STATUS_RESIDENT_ATTRIBUTE_FILLED_MFT or STATUS_ERROR. |
| 1215 | */ |
| 1216 | static int ntfs_ir_truncate(ntfs_index_context *icx, int data_size) |
| 1217 | { |
| 1218 | ntfs_attr *na; |
| 1219 | int ret; |
| 1220 | |
| 1221 | ntfs_log_trace("Entering\n"); |
| 1222 | |
| 1223 | na = ntfs_attr_open(icx->ni, AT_INDEX_ROOT, icx->name, icx->name_len); |
| 1224 | if (!na) { |
| 1225 | ntfs_log_perror("Failed to open INDEX_ROOT"); |
| 1226 | return STATUS_ERROR; |
| 1227 | } |
| 1228 | /* |
| 1229 | * INDEX_ROOT must be resident and its entries can be moved to |
| 1230 | * INDEX_BLOCK, so ENOSPC isn't a real error. |
| 1231 | */ |
| 1232 | ret = ntfs_attr_truncate(na, data_size + offsetof(INDEX_ROOT, index)); |
| 1233 | if (ret == STATUS_OK) { |
| 1234 | |
| 1235 | icx->ir = ntfs_ir_lookup2(icx->ni, icx->name, icx->name_len); |
| 1236 | if (!icx->ir) |
| 1237 | return STATUS_ERROR; |
| 1238 | |
| 1239 | icx->ir->index.allocated_size = cpu_to_le32(data_size); |
| 1240 | |
| 1241 | } else if (ret == STATUS_ERROR) |
| 1242 | ntfs_log_perror("Failed to truncate INDEX_ROOT"); |
| 1243 | |
| 1244 | ntfs_attr_close(na); |
| 1245 | return ret; |
| 1246 | } |
| 1247 | |
| 1248 | /** |
| 1249 | * ntfs_ir_make_space - Make more space for the index root attribute |
| 1250 | * |
| 1251 | * On success return STATUS_OK or STATUS_KEEP_SEARCHING. |
| 1252 | * On error return STATUS_ERROR. |
| 1253 | */ |
| 1254 | static int ntfs_ir_make_space(ntfs_index_context *icx, int data_size) |
| 1255 | { |
| 1256 | int ret; |
| 1257 | |
| 1258 | ntfs_log_trace("Entering\n"); |
| 1259 | |
| 1260 | ret = ntfs_ir_truncate(icx, data_size); |
| 1261 | if (ret == STATUS_RESIDENT_ATTRIBUTE_FILLED_MFT) { |
| 1262 | |
| 1263 | ret = ntfs_ir_reparent(icx); |
| 1264 | if (ret == STATUS_OK) |
| 1265 | ret = STATUS_KEEP_SEARCHING; |
| 1266 | else |
| 1267 | ntfs_log_perror("Failed to nodify INDEX_ROOT"); |
| 1268 | } |
| 1269 | |
| 1270 | return ret; |
| 1271 | } |
| 1272 | |
| 1273 | /* |
| 1274 | * NOTE: 'ie' must be a copy of a real index entry. |
| 1275 | */ |
| 1276 | static int ntfs_ie_add_vcn(INDEX_ENTRY **ie) |
| 1277 | { |
| 1278 | INDEX_ENTRY *p, *old = *ie; |
| 1279 | |
| 1280 | old->length = cpu_to_le16(le16_to_cpu(old->length) + sizeof(VCN)); |
| 1281 | p = realloc(old, le16_to_cpu(old->length)); |
| 1282 | if (!p) |
| 1283 | return STATUS_ERROR; |
| 1284 | |
| 1285 | p->ie_flags |= INDEX_ENTRY_NODE; |
| 1286 | *ie = p; |
| 1287 | |
| 1288 | return STATUS_OK; |
| 1289 | } |
| 1290 | |
| 1291 | static int ntfs_ih_insert(INDEX_HEADER *ih, INDEX_ENTRY *orig_ie, VCN new_vcn, |
| 1292 | int pos) |
| 1293 | { |
| 1294 | INDEX_ENTRY *ie_node, *ie; |
| 1295 | int ret = STATUS_ERROR; |
| 1296 | VCN old_vcn; |
| 1297 | |
| 1298 | ntfs_log_trace("Entering\n"); |
| 1299 | |
| 1300 | ie = ntfs_ie_dup(orig_ie); |
| 1301 | if (!ie) |
| 1302 | return STATUS_ERROR; |
| 1303 | |
| 1304 | if (!(ie->ie_flags & INDEX_ENTRY_NODE)) |
| 1305 | if (ntfs_ie_add_vcn(&ie)) |
| 1306 | goto out; |
| 1307 | |
| 1308 | ie_node = ntfs_ie_get_by_pos(ih, pos); |
| 1309 | old_vcn = ntfs_ie_get_vcn(ie_node); |
| 1310 | ntfs_ie_set_vcn(ie_node, new_vcn); |
| 1311 | |
| 1312 | ntfs_ie_insert(ih, ie, ie_node); |
| 1313 | ntfs_ie_set_vcn(ie_node, old_vcn); |
| 1314 | ret = STATUS_OK; |
| 1315 | out: |
| 1316 | free(ie); |
| 1317 | |
| 1318 | return ret; |
| 1319 | } |
| 1320 | |
| 1321 | static VCN ntfs_icx_parent_vcn(ntfs_index_context *icx) |
| 1322 | { |
| 1323 | return icx->parent_vcn[icx->pindex]; |
| 1324 | } |
| 1325 | |
| 1326 | static VCN ntfs_icx_parent_pos(ntfs_index_context *icx) |
| 1327 | { |
| 1328 | return icx->parent_pos[icx->pindex]; |
| 1329 | } |
| 1330 | |
| 1331 | |
| 1332 | static int ntfs_ir_insert_median(ntfs_index_context *icx, INDEX_ENTRY *median, |
| 1333 | VCN new_vcn) |
| 1334 | { |
| 1335 | u32 new_size; |
| 1336 | int ret; |
| 1337 | |
| 1338 | ntfs_log_trace("Entering\n"); |
| 1339 | |
| 1340 | icx->ir = ntfs_ir_lookup2(icx->ni, icx->name, icx->name_len); |
| 1341 | if (!icx->ir) |
| 1342 | return STATUS_ERROR; |
| 1343 | |
| 1344 | new_size = le32_to_cpu(icx->ir->index.index_length) + |
| 1345 | le16_to_cpu(median->length); |
| 1346 | if (!(median->ie_flags & INDEX_ENTRY_NODE)) |
| 1347 | new_size += sizeof(VCN); |
| 1348 | |
| 1349 | ret = ntfs_ir_make_space(icx, new_size); |
| 1350 | if (ret != STATUS_OK) |
| 1351 | return ret; |
| 1352 | |
| 1353 | icx->ir = ntfs_ir_lookup2(icx->ni, icx->name, icx->name_len); |
| 1354 | if (!icx->ir) |
| 1355 | return STATUS_ERROR; |
| 1356 | |
| 1357 | return ntfs_ih_insert(&icx->ir->index, median, new_vcn, |
| 1358 | ntfs_icx_parent_pos(icx)); |
| 1359 | } |
| 1360 | |
| 1361 | static int ntfs_ib_split(ntfs_index_context *icx, INDEX_BLOCK *ib); |
| 1362 | |
| 1363 | /** |
| 1364 | * On success return STATUS_OK or STATUS_KEEP_SEARCHING. |
| 1365 | * On error return STATUS_ERROR. |
| 1366 | */ |
| 1367 | static int ntfs_ib_insert(ntfs_index_context *icx, INDEX_ENTRY *ie, VCN new_vcn) |
| 1368 | { |
| 1369 | INDEX_BLOCK *ib; |
| 1370 | u32 idx_size, allocated_size; |
| 1371 | int err = STATUS_ERROR; |
| 1372 | VCN old_vcn; |
| 1373 | |
| 1374 | ntfs_log_trace("Entering\n"); |
| 1375 | |
| 1376 | ib = ntfs_malloc(icx->block_size); |
| 1377 | if (!ib) |
| 1378 | return -1; |
| 1379 | |
| 1380 | old_vcn = ntfs_icx_parent_vcn(icx); |
| 1381 | |
| 1382 | if (ntfs_ib_read(icx, old_vcn, ib)) |
| 1383 | goto err_out; |
| 1384 | |
| 1385 | idx_size = le32_to_cpu(ib->index.index_length); |
| 1386 | allocated_size = le32_to_cpu(ib->index.allocated_size); |
| 1387 | /* FIXME: sizeof(VCN) should be included only if ie has no VCN */ |
| 1388 | if (idx_size + le16_to_cpu(ie->length) + sizeof(VCN) > allocated_size) { |
| 1389 | err = ntfs_ib_split(icx, ib); |
| 1390 | if (err == STATUS_OK) |
| 1391 | err = STATUS_KEEP_SEARCHING; |
| 1392 | goto err_out; |
| 1393 | } |
| 1394 | |
| 1395 | if (ntfs_ih_insert(&ib->index, ie, new_vcn, ntfs_icx_parent_pos(icx))) |
| 1396 | goto err_out; |
| 1397 | |
| 1398 | if (ntfs_ib_write(icx, ib)) |
| 1399 | goto err_out; |
| 1400 | |
| 1401 | err = STATUS_OK; |
| 1402 | err_out: |
| 1403 | free(ib); |
| 1404 | return err; |
| 1405 | } |
| 1406 | |
| 1407 | /** |
| 1408 | * ntfs_ib_split - Split an index block |
| 1409 | * |
| 1410 | * On success return STATUS_OK or STATUS_KEEP_SEARCHING. |
| 1411 | * On error return is STATUS_ERROR. |
| 1412 | */ |
| 1413 | static int ntfs_ib_split(ntfs_index_context *icx, INDEX_BLOCK *ib) |
| 1414 | { |
| 1415 | INDEX_ENTRY *median; |
| 1416 | VCN new_vcn; |
| 1417 | int ret; |
| 1418 | |
| 1419 | ntfs_log_trace("Entering\n"); |
| 1420 | |
| 1421 | if (ntfs_icx_parent_dec(icx)) |
| 1422 | return STATUS_ERROR; |
| 1423 | |
| 1424 | median = ntfs_ie_get_median(&ib->index); |
| 1425 | new_vcn = ntfs_ibm_get_free(icx); |
| 1426 | if (new_vcn == -1) |
| 1427 | return STATUS_ERROR; |
| 1428 | |
| 1429 | if (ntfs_ib_copy_tail(icx, ib, median, new_vcn)) { |
| 1430 | ntfs_ibm_clear(icx, new_vcn); |
| 1431 | return STATUS_ERROR; |
| 1432 | } |
| 1433 | |
| 1434 | if (ntfs_icx_parent_vcn(icx) == VCN_INDEX_ROOT_PARENT) |
| 1435 | ret = ntfs_ir_insert_median(icx, median, new_vcn); |
| 1436 | else |
| 1437 | ret = ntfs_ib_insert(icx, median, new_vcn); |
| 1438 | |
| 1439 | if (ret != STATUS_OK) { |
| 1440 | ntfs_ibm_clear(icx, new_vcn); |
| 1441 | return ret; |
| 1442 | } |
| 1443 | |
| 1444 | ret = ntfs_ib_cut_tail(icx, ib, median); |
| 1445 | |
| 1446 | return ret; |
| 1447 | } |
| 1448 | |
| 1449 | /* JPA static */ |
| 1450 | int ntfs_ie_add(ntfs_index_context *icx, INDEX_ENTRY *ie) |
| 1451 | { |
| 1452 | INDEX_HEADER *ih; |
| 1453 | int allocated_size, new_size; |
| 1454 | int ret = STATUS_ERROR; |
| 1455 | |
| 1456 | #ifdef DEBUG |
| 1457 | /* removed by JPA to make function usable for security indexes |
| 1458 | char *fn; |
| 1459 | fn = ntfs_ie_filename_get(ie); |
| 1460 | ntfs_log_trace("file: '%s'\n", fn); |
| 1461 | ntfs_attr_name_free(&fn); |
| 1462 | */ |
| 1463 | #endif |
| 1464 | |
| 1465 | while (1) { |
| 1466 | |
| 1467 | if (!ntfs_index_lookup(&ie->key, le16_to_cpu(ie->key_length), icx)) { |
| 1468 | errno = EEXIST; |
| 1469 | ntfs_log_perror("Index already have such entry"); |
| 1470 | goto err_out; |
| 1471 | } |
| 1472 | if (errno != ENOENT) { |
| 1473 | ntfs_log_perror("Failed to find place for new entry"); |
| 1474 | goto err_out; |
| 1475 | } |
| 1476 | |
| 1477 | if (icx->is_in_root) |
| 1478 | ih = &icx->ir->index; |
| 1479 | else |
| 1480 | ih = &icx->ib->index; |
| 1481 | |
| 1482 | allocated_size = le32_to_cpu(ih->allocated_size); |
| 1483 | new_size = le32_to_cpu(ih->index_length) + le16_to_cpu(ie->length); |
| 1484 | |
| 1485 | if (new_size <= allocated_size) |
| 1486 | break; |
| 1487 | |
| 1488 | ntfs_log_trace("index block sizes: allocated: %d needed: %d\n", |
| 1489 | allocated_size, new_size); |
| 1490 | |
| 1491 | if (icx->is_in_root) { |
| 1492 | if (ntfs_ir_make_space(icx, new_size) == STATUS_ERROR) |
| 1493 | goto err_out; |
| 1494 | } else { |
| 1495 | if (ntfs_ib_split(icx, icx->ib) == STATUS_ERROR) |
| 1496 | goto err_out; |
| 1497 | } |
| 1498 | |
| 1499 | ntfs_inode_mark_dirty(icx->actx->ntfs_ino); |
| 1500 | ntfs_index_ctx_reinit(icx); |
| 1501 | } |
| 1502 | |
| 1503 | ntfs_ie_insert(ih, ie, icx->entry); |
| 1504 | ntfs_index_entry_mark_dirty(icx); |
| 1505 | |
| 1506 | ret = STATUS_OK; |
| 1507 | err_out: |
| 1508 | ntfs_log_trace("%s\n", ret ? "Failed" : "Done"); |
| 1509 | return ret; |
| 1510 | } |
| 1511 | |
| 1512 | /** |
| 1513 | * ntfs_index_add_filename - add filename to directory index |
| 1514 | * @ni: ntfs inode describing directory to which index add filename |
| 1515 | * @fn: FILE_NAME attribute to add |
| 1516 | * @mref: reference of the inode which @fn describes |
| 1517 | * |
| 1518 | * Return 0 on success or -1 on error with errno set to the error code. |
| 1519 | */ |
| 1520 | int ntfs_index_add_filename(ntfs_inode *ni, FILE_NAME_ATTR *fn, MFT_REF mref) |
| 1521 | { |
| 1522 | INDEX_ENTRY *ie; |
| 1523 | ntfs_index_context *icx; |
| 1524 | int fn_size, ie_size, err, ret = -1; |
| 1525 | |
| 1526 | ntfs_log_trace("Entering\n"); |
| 1527 | |
| 1528 | if (!ni || !fn) { |
| 1529 | ntfs_log_error("Invalid arguments.\n"); |
| 1530 | errno = EINVAL; |
| 1531 | return -1; |
| 1532 | } |
| 1533 | |
| 1534 | fn_size = (fn->file_name_length * sizeof(ntfschar)) + |
| 1535 | sizeof(FILE_NAME_ATTR); |
| 1536 | ie_size = (sizeof(INDEX_ENTRY_HEADER) + fn_size + 7) & ~7; |
| 1537 | |
| 1538 | ie = ntfs_calloc(ie_size); |
| 1539 | if (!ie) |
| 1540 | return -1; |
| 1541 | |
| 1542 | ie->indexed_file = cpu_to_le64(mref); |
| 1543 | ie->length = cpu_to_le16(ie_size); |
| 1544 | ie->key_length = cpu_to_le16(fn_size); |
| 1545 | memcpy(&ie->key, fn, fn_size); |
| 1546 | |
| 1547 | icx = ntfs_index_ctx_get(ni, NTFS_INDEX_I30, 4); |
| 1548 | if (!icx) |
| 1549 | goto out; |
| 1550 | |
| 1551 | ret = ntfs_ie_add(icx, ie); |
| 1552 | err = errno; |
| 1553 | ntfs_index_ctx_put(icx); |
| 1554 | errno = err; |
| 1555 | out: |
| 1556 | free(ie); |
| 1557 | return ret; |
| 1558 | } |
| 1559 | |
| 1560 | static int ntfs_ih_takeout(ntfs_index_context *icx, INDEX_HEADER *ih, |
| 1561 | INDEX_ENTRY *ie, INDEX_BLOCK *ib) |
| 1562 | { |
| 1563 | INDEX_ENTRY *ie_roam; |
| 1564 | int ret = STATUS_ERROR; |
| 1565 | |
| 1566 | ntfs_log_trace("Entering\n"); |
| 1567 | |
| 1568 | ie_roam = ntfs_ie_dup_novcn(ie); |
| 1569 | if (!ie_roam) |
| 1570 | return STATUS_ERROR; |
| 1571 | |
| 1572 | ntfs_ie_delete(ih, ie); |
| 1573 | |
| 1574 | if (ntfs_icx_parent_vcn(icx) == VCN_INDEX_ROOT_PARENT) |
| 1575 | ntfs_inode_mark_dirty(icx->actx->ntfs_ino); |
| 1576 | else |
| 1577 | if (ntfs_ib_write(icx, ib)) |
| 1578 | goto out; |
| 1579 | |
| 1580 | ntfs_index_ctx_reinit(icx); |
| 1581 | |
| 1582 | ret = ntfs_ie_add(icx, ie_roam); |
| 1583 | out: |
| 1584 | free(ie_roam); |
| 1585 | return ret; |
| 1586 | } |
| 1587 | |
| 1588 | /** |
| 1589 | * Used if an empty index block to be deleted has END entry as the parent |
| 1590 | * in the INDEX_ROOT which is the only one there. |
| 1591 | */ |
| 1592 | static void ntfs_ir_leafify(ntfs_index_context *icx, INDEX_HEADER *ih) |
| 1593 | { |
| 1594 | INDEX_ENTRY *ie; |
| 1595 | |
| 1596 | ntfs_log_trace("Entering\n"); |
| 1597 | |
| 1598 | ie = ntfs_ie_get_first(ih); |
| 1599 | ie->ie_flags &= ~INDEX_ENTRY_NODE; |
| 1600 | ie->length = cpu_to_le16(le16_to_cpu(ie->length) - sizeof(VCN)); |
| 1601 | |
| 1602 | ih->index_length = cpu_to_le32(le32_to_cpu(ih->index_length) - sizeof(VCN)); |
| 1603 | ih->ih_flags &= ~LARGE_INDEX; |
| 1604 | |
| 1605 | /* Not fatal error */ |
| 1606 | ntfs_ir_truncate(icx, le32_to_cpu(ih->index_length)); |
| 1607 | } |
| 1608 | |
| 1609 | /** |
| 1610 | * Used if an empty index block to be deleted has END entry as the parent |
| 1611 | * in the INDEX_ROOT which is not the only one there. |
| 1612 | */ |
| 1613 | static int ntfs_ih_reparent_end(ntfs_index_context *icx, INDEX_HEADER *ih, |
| 1614 | INDEX_BLOCK *ib) |
| 1615 | { |
| 1616 | INDEX_ENTRY *ie, *ie_prev; |
| 1617 | |
| 1618 | ntfs_log_trace("Entering\n"); |
| 1619 | |
| 1620 | ie = ntfs_ie_get_by_pos(ih, ntfs_icx_parent_pos(icx)); |
| 1621 | ie_prev = ntfs_ie_prev(ih, ie); |
| 1622 | |
| 1623 | ntfs_ie_set_vcn(ie, ntfs_ie_get_vcn(ie_prev)); |
| 1624 | |
| 1625 | return ntfs_ih_takeout(icx, ih, ie_prev, ib); |
| 1626 | } |
| 1627 | |
| 1628 | static int ntfs_index_rm_leaf(ntfs_index_context *icx) |
| 1629 | { |
| 1630 | INDEX_BLOCK *ib = NULL; |
| 1631 | INDEX_HEADER *parent_ih; |
| 1632 | INDEX_ENTRY *ie; |
| 1633 | int ret = STATUS_ERROR; |
| 1634 | |
| 1635 | ntfs_log_trace("pindex: %d\n", icx->pindex); |
| 1636 | |
| 1637 | if (ntfs_icx_parent_dec(icx)) |
| 1638 | return STATUS_ERROR; |
| 1639 | |
| 1640 | if (ntfs_ibm_clear(icx, icx->parent_vcn[icx->pindex + 1])) |
| 1641 | return STATUS_ERROR; |
| 1642 | |
| 1643 | if (ntfs_icx_parent_vcn(icx) == VCN_INDEX_ROOT_PARENT) |
| 1644 | parent_ih = &icx->ir->index; |
| 1645 | else { |
| 1646 | ib = ntfs_malloc(icx->block_size); |
| 1647 | if (!ib) |
| 1648 | return STATUS_ERROR; |
| 1649 | |
| 1650 | if (ntfs_ib_read(icx, ntfs_icx_parent_vcn(icx), ib)) |
| 1651 | goto out; |
| 1652 | |
| 1653 | parent_ih = &ib->index; |
| 1654 | } |
| 1655 | |
| 1656 | ie = ntfs_ie_get_by_pos(parent_ih, ntfs_icx_parent_pos(icx)); |
| 1657 | if (!ntfs_ie_end(ie)) { |
| 1658 | ret = ntfs_ih_takeout(icx, parent_ih, ie, ib); |
| 1659 | goto out; |
| 1660 | } |
| 1661 | |
| 1662 | if (ntfs_ih_zero_entry(parent_ih)) { |
| 1663 | |
| 1664 | if (ntfs_icx_parent_vcn(icx) == VCN_INDEX_ROOT_PARENT) { |
| 1665 | ntfs_ir_leafify(icx, parent_ih); |
| 1666 | goto ok; |
| 1667 | } |
| 1668 | |
| 1669 | ret = ntfs_index_rm_leaf(icx); |
| 1670 | goto out; |
| 1671 | } |
| 1672 | |
| 1673 | if (ntfs_ih_reparent_end(icx, parent_ih, ib)) |
| 1674 | goto out; |
| 1675 | ok: |
| 1676 | ret = STATUS_OK; |
| 1677 | out: |
| 1678 | free(ib); |
| 1679 | return ret; |
| 1680 | } |
| 1681 | |
| 1682 | static int ntfs_index_rm_node(ntfs_index_context *icx) |
| 1683 | { |
| 1684 | int entry_pos, pindex; |
| 1685 | VCN vcn; |
| 1686 | INDEX_BLOCK *ib = NULL; |
| 1687 | INDEX_ENTRY *ie_succ, *ie, *entry = icx->entry; |
| 1688 | INDEX_HEADER *ih; |
| 1689 | u32 new_size; |
| 1690 | int delta, ret = STATUS_ERROR; |
| 1691 | |
| 1692 | ntfs_log_trace("Entering\n"); |
| 1693 | |
| 1694 | if (!icx->ia_na) { |
| 1695 | icx->ia_na = ntfs_ia_open(icx, icx->ni); |
| 1696 | if (!icx->ia_na) |
| 1697 | return STATUS_ERROR; |
| 1698 | } |
| 1699 | |
| 1700 | ib = ntfs_malloc(icx->block_size); |
| 1701 | if (!ib) |
| 1702 | return STATUS_ERROR; |
| 1703 | |
| 1704 | ie_succ = ntfs_ie_get_next(icx->entry); |
| 1705 | entry_pos = icx->parent_pos[icx->pindex]++; |
| 1706 | pindex = icx->pindex; |
| 1707 | descend: |
| 1708 | vcn = ntfs_ie_get_vcn(ie_succ); |
| 1709 | if (ntfs_ib_read(icx, vcn, ib)) |
| 1710 | goto out; |
| 1711 | |
| 1712 | ie_succ = ntfs_ie_get_first(&ib->index); |
| 1713 | |
| 1714 | if (ntfs_icx_parent_inc(icx)) |
| 1715 | goto out; |
| 1716 | |
| 1717 | icx->parent_vcn[icx->pindex] = vcn; |
| 1718 | icx->parent_pos[icx->pindex] = 0; |
| 1719 | |
| 1720 | if ((ib->index.ih_flags & NODE_MASK) == INDEX_NODE) |
| 1721 | goto descend; |
| 1722 | |
| 1723 | if (ntfs_ih_zero_entry(&ib->index)) { |
| 1724 | errno = EIO; |
| 1725 | ntfs_log_perror("Empty index block"); |
| 1726 | goto out; |
| 1727 | } |
| 1728 | |
| 1729 | ie = ntfs_ie_dup(ie_succ); |
| 1730 | if (!ie) |
| 1731 | goto out; |
| 1732 | |
| 1733 | if (ntfs_ie_add_vcn(&ie)) |
| 1734 | goto out2; |
| 1735 | |
| 1736 | ntfs_ie_set_vcn(ie, ntfs_ie_get_vcn(icx->entry)); |
| 1737 | |
| 1738 | if (icx->is_in_root) |
| 1739 | ih = &icx->ir->index; |
| 1740 | else |
| 1741 | ih = &icx->ib->index; |
| 1742 | |
| 1743 | delta = le16_to_cpu(ie->length) - le16_to_cpu(icx->entry->length); |
| 1744 | new_size = le32_to_cpu(ih->index_length) + delta; |
| 1745 | if (delta > 0) { |
| 1746 | if (icx->is_in_root) { |
| 1747 | ret = ntfs_ir_make_space(icx, new_size); |
| 1748 | if (ret != STATUS_OK) |
| 1749 | goto out2; |
| 1750 | |
| 1751 | ih = &icx->ir->index; |
| 1752 | entry = ntfs_ie_get_by_pos(ih, entry_pos); |
| 1753 | |
| 1754 | } else if (new_size > le32_to_cpu(ih->allocated_size)) { |
| 1755 | icx->pindex = pindex; |
| 1756 | ret = ntfs_ib_split(icx, icx->ib); |
| 1757 | if (ret == STATUS_OK) |
| 1758 | ret = STATUS_KEEP_SEARCHING; |
| 1759 | goto out2; |
| 1760 | } |
| 1761 | } |
| 1762 | |
| 1763 | ntfs_ie_delete(ih, entry); |
| 1764 | ntfs_ie_insert(ih, ie, entry); |
| 1765 | |
| 1766 | if (icx->is_in_root) { |
| 1767 | if (ntfs_ir_truncate(icx, new_size)) |
| 1768 | goto out2; |
| 1769 | } else |
| 1770 | if (ntfs_icx_ib_write(icx)) |
| 1771 | goto out2; |
| 1772 | |
| 1773 | ntfs_ie_delete(&ib->index, ie_succ); |
| 1774 | |
| 1775 | if (ntfs_ih_zero_entry(&ib->index)) { |
| 1776 | if (ntfs_index_rm_leaf(icx)) |
| 1777 | goto out2; |
| 1778 | } else |
| 1779 | if (ntfs_ib_write(icx, ib)) |
| 1780 | goto out2; |
| 1781 | |
| 1782 | ret = STATUS_OK; |
| 1783 | out2: |
| 1784 | free(ie); |
| 1785 | out: |
| 1786 | free(ib); |
| 1787 | return ret; |
| 1788 | } |
| 1789 | |
| 1790 | /** |
| 1791 | * ntfs_index_rm - remove entry from the index |
| 1792 | * @icx: index context describing entry to delete |
| 1793 | * |
| 1794 | * Delete entry described by @icx from the index. Index context is always |
| 1795 | * reinitialized after use of this function, so it can be used for index |
| 1796 | * lookup once again. |
| 1797 | * |
| 1798 | * Return 0 on success or -1 on error with errno set to the error code. |
| 1799 | */ |
| 1800 | /*static JPA*/ |
| 1801 | int ntfs_index_rm(ntfs_index_context *icx) |
| 1802 | { |
| 1803 | INDEX_HEADER *ih; |
| 1804 | int err, ret = STATUS_OK; |
| 1805 | |
| 1806 | ntfs_log_trace("Entering\n"); |
| 1807 | |
| 1808 | if (!icx || (!icx->ib && !icx->ir) || ntfs_ie_end(icx->entry)) { |
| 1809 | ntfs_log_error("Invalid arguments.\n"); |
| 1810 | errno = EINVAL; |
| 1811 | goto err_out; |
| 1812 | } |
| 1813 | if (icx->is_in_root) |
| 1814 | ih = &icx->ir->index; |
| 1815 | else |
| 1816 | ih = &icx->ib->index; |
| 1817 | |
| 1818 | if (icx->entry->ie_flags & INDEX_ENTRY_NODE) { |
| 1819 | |
| 1820 | ret = ntfs_index_rm_node(icx); |
| 1821 | |
| 1822 | } else if (icx->is_in_root || !ntfs_ih_one_entry(ih)) { |
| 1823 | |
| 1824 | ntfs_ie_delete(ih, icx->entry); |
| 1825 | |
| 1826 | if (icx->is_in_root) { |
| 1827 | err = ntfs_ir_truncate(icx, le32_to_cpu(ih->index_length)); |
| 1828 | if (err != STATUS_OK) |
| 1829 | goto err_out; |
| 1830 | } else |
| 1831 | if (ntfs_icx_ib_write(icx)) |
| 1832 | goto err_out; |
| 1833 | } else { |
| 1834 | if (ntfs_index_rm_leaf(icx)) |
| 1835 | goto err_out; |
| 1836 | } |
| 1837 | out: |
| 1838 | return ret; |
| 1839 | err_out: |
| 1840 | ret = STATUS_ERROR; |
| 1841 | goto out; |
| 1842 | } |
| 1843 | |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 1844 | int ntfs_index_remove(ntfs_inode *dir_ni, |
| 1845 | ntfs_inode *ni __attribute__((unused)), |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 1846 | const void *key, const int keylen) |
| 1847 | { |
| 1848 | int ret = STATUS_ERROR; |
| 1849 | ntfs_index_context *icx; |
| 1850 | |
| 1851 | icx = ntfs_index_ctx_get(dir_ni, NTFS_INDEX_I30, 4); |
| 1852 | if (!icx) |
| 1853 | return -1; |
| 1854 | |
| 1855 | while (1) { |
| 1856 | |
| 1857 | if (ntfs_index_lookup(key, keylen, icx)) |
| 1858 | goto err_out; |
| 1859 | |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 1860 | ret = ntfs_index_rm(icx); |
| 1861 | if (ret == STATUS_ERROR) |
| 1862 | goto err_out; |
| 1863 | else if (ret == STATUS_OK) |
| 1864 | break; |
| 1865 | |
| 1866 | ntfs_inode_mark_dirty(icx->actx->ntfs_ino); |
| 1867 | ntfs_index_ctx_reinit(icx); |
| 1868 | } |
| 1869 | |
| 1870 | ntfs_inode_mark_dirty(icx->actx->ntfs_ino); |
| 1871 | out: |
| 1872 | ntfs_index_ctx_put(icx); |
| 1873 | return ret; |
| 1874 | err_out: |
| 1875 | ret = STATUS_ERROR; |
| 1876 | ntfs_log_perror("Delete failed"); |
| 1877 | goto out; |
| 1878 | } |
| 1879 | |
| 1880 | /** |
| 1881 | * ntfs_index_root_get - read the index root of an attribute |
| 1882 | * @ni: open ntfs inode in which the ntfs attribute resides |
| 1883 | * @attr: attribute for which we want its index root |
| 1884 | * |
| 1885 | * This function will read the related index root an ntfs attribute. |
| 1886 | * |
| 1887 | * On success a buffer is allocated with the content of the index root |
| 1888 | * and which needs to be freed when it's not needed anymore. |
| 1889 | * |
| 1890 | * On error NULL is returned with errno set to the error code. |
| 1891 | */ |
| 1892 | INDEX_ROOT *ntfs_index_root_get(ntfs_inode *ni, ATTR_RECORD *attr) |
| 1893 | { |
| 1894 | ntfs_attr_search_ctx *ctx; |
| 1895 | ntfschar *name; |
| 1896 | INDEX_ROOT *root = NULL; |
| 1897 | |
| 1898 | name = (ntfschar *)((u8 *)attr + le16_to_cpu(attr->name_offset)); |
| 1899 | |
| 1900 | if (!ntfs_ir_lookup(ni, name, attr->name_length, &ctx)) |
| 1901 | return NULL; |
| 1902 | |
| 1903 | root = ntfs_malloc(sizeof(INDEX_ROOT)); |
| 1904 | if (!root) |
| 1905 | goto out; |
| 1906 | |
| 1907 | *root = *((INDEX_ROOT *)((u8 *)ctx->attr + |
| 1908 | le16_to_cpu(ctx->attr->value_offset))); |
| 1909 | out: |
| 1910 | ntfs_attr_put_search_ctx(ctx); |
| 1911 | return root; |
| 1912 | } |
| 1913 | |
| 1914 | |
| 1915 | /* |
| 1916 | * Walk down the index tree (leaf bound) |
| 1917 | * until there are no subnode in the first index entry |
| 1918 | * returns the entry at the bottom left in subnode |
| 1919 | */ |
| 1920 | |
| 1921 | static INDEX_ENTRY *ntfs_index_walk_down(INDEX_ENTRY *ie, |
| 1922 | ntfs_index_context *ictx) |
| 1923 | { |
| 1924 | INDEX_ENTRY *entry; |
| 1925 | s64 vcn; |
| 1926 | |
| 1927 | entry = ie; |
| 1928 | do { |
| 1929 | vcn = ntfs_ie_get_vcn(entry); |
| 1930 | if (ictx->is_in_root) { |
| 1931 | |
| 1932 | /* down from level zero */ |
| 1933 | |
| 1934 | ictx->ir = (INDEX_ROOT*)NULL; |
| 1935 | ictx->ib = (INDEX_BLOCK*)ntfs_malloc(ictx->block_size); |
| 1936 | ictx->pindex = 1; |
| 1937 | ictx->is_in_root = FALSE; |
| 1938 | } else { |
| 1939 | |
| 1940 | /* down from non-zero level */ |
| 1941 | |
| 1942 | ictx->pindex++; |
| 1943 | } |
| 1944 | ictx->parent_pos[ictx->pindex] = 0; |
| 1945 | ictx->parent_vcn[ictx->pindex] = vcn; |
| 1946 | if (!ntfs_ib_read(ictx,vcn,ictx->ib)) { |
| 1947 | ictx->entry = ntfs_ie_get_first(&ictx->ib->index); |
| 1948 | entry = ictx->entry; |
| 1949 | } else |
| 1950 | entry = (INDEX_ENTRY*)NULL; |
| 1951 | } while (entry && (entry->ie_flags & INDEX_ENTRY_NODE)); |
| 1952 | return (entry); |
| 1953 | } |
| 1954 | |
| 1955 | /* |
| 1956 | * Walk up the index tree (root bound) |
| 1957 | * until there is a valid data entry in parent |
| 1958 | * returns the parent entry or NULL if no more parent |
| 1959 | */ |
| 1960 | |
| 1961 | static INDEX_ENTRY *ntfs_index_walk_up(INDEX_ENTRY *ie, |
| 1962 | ntfs_index_context *ictx) |
| 1963 | { |
| 1964 | INDEX_ENTRY *entry; |
| 1965 | s64 vcn; |
| 1966 | |
| 1967 | entry = ie; |
| 1968 | if (ictx->pindex > 0) { |
| 1969 | do { |
| 1970 | ictx->pindex--; |
| 1971 | if (!ictx->pindex) { |
| 1972 | |
| 1973 | /* we have reached the root */ |
| 1974 | |
| 1975 | free(ictx->ib); |
| 1976 | ictx->ib = (INDEX_BLOCK*)NULL; |
| 1977 | ictx->is_in_root = TRUE; |
| 1978 | /* a new search context is to be allocated */ |
| 1979 | if (ictx->actx) |
| 1980 | free(ictx->actx); |
| 1981 | ictx->ir = ntfs_ir_lookup(ictx->ni, |
| 1982 | ictx->name, ictx->name_len, |
| 1983 | &ictx->actx); |
| 1984 | if (ictx->ir) |
| 1985 | entry = ntfs_ie_get_by_pos( |
| 1986 | &ictx->ir->index, |
| 1987 | ictx->parent_pos[ictx->pindex]); |
| 1988 | else |
| 1989 | entry = (INDEX_ENTRY*)NULL; |
| 1990 | } else { |
| 1991 | /* up into non-root node */ |
| 1992 | vcn = ictx->parent_vcn[ictx->pindex]; |
| 1993 | if (!ntfs_ib_read(ictx,vcn,ictx->ib)) { |
| 1994 | entry = ntfs_ie_get_by_pos( |
| 1995 | &ictx->ib->index, |
| 1996 | ictx->parent_pos[ictx->pindex]); |
| 1997 | } else |
| 1998 | entry = (INDEX_ENTRY*)NULL; |
| 1999 | } |
| 2000 | ictx->entry = entry; |
| 2001 | } while (entry && (ictx->pindex > 0) |
| 2002 | && (entry->ie_flags & INDEX_ENTRY_END)); |
| 2003 | } else |
| 2004 | entry = (INDEX_ENTRY*)NULL; |
| 2005 | return (entry); |
| 2006 | } |
| 2007 | |
| 2008 | /* |
| 2009 | * Get next entry in an index according to collating sequence. |
| 2010 | * Must be initialized through a ntfs_index_lookup() |
| 2011 | * |
| 2012 | * Returns next entry or NULL if none |
| 2013 | * |
| 2014 | * Sample layout : |
| 2015 | * |
| 2016 | * +---+---+---+---+---+---+---+---+ n ptrs to subnodes |
| 2017 | * | | | 10| 25| 33| | | | n-1 keys in between |
| 2018 | * +---+---+---+---+---+---+---+---+ no key in last entry |
| 2019 | * | A | A |
| 2020 | * | | | +-------------------------------+ |
| 2021 | * +--------------------------+ | +-----+ | |
| 2022 | * | +--+ | | |
| 2023 | * V | V | |
| 2024 | * +---+---+---+---+---+---+---+---+ | +---+---+---+---+---+---+---+---+ |
| 2025 | * | 11| 12| 13| 14| 15| 16| 17| | | | 26| 27| 28| 29| 30| 31| 32| | |
| 2026 | * +---+---+---+---+---+---+---+---+ | +---+---+---+---+---+---+---+---+ |
| 2027 | * | | |
| 2028 | * +-----------------------+ | |
| 2029 | * | | |
| 2030 | * +---+---+---+---+---+---+---+---+ |
| 2031 | * | 18| 19| 20| 21| 22| 23| 24| | |
| 2032 | * +---+---+---+---+---+---+---+---+ |
| 2033 | */ |
| 2034 | |
| 2035 | INDEX_ENTRY *ntfs_index_next(INDEX_ENTRY *ie, ntfs_index_context *ictx) |
| 2036 | { |
| 2037 | INDEX_ENTRY *next; |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 2038 | le16 flags; |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 2039 | |
| 2040 | /* |
| 2041 | * lookup() may have returned an invalid node |
| 2042 | * when searching for a partial key |
| 2043 | * if this happens, walk up |
| 2044 | */ |
| 2045 | |
| 2046 | if (ie->ie_flags & INDEX_ENTRY_END) |
| 2047 | next = ntfs_index_walk_up(ie, ictx); |
| 2048 | else { |
| 2049 | /* |
| 2050 | * get next entry in same node |
| 2051 | * there is always one after any entry with data |
| 2052 | */ |
| 2053 | |
| 2054 | next = (INDEX_ENTRY*)((char*)ie + le16_to_cpu(ie->length)); |
| 2055 | ++ictx->parent_pos[ictx->pindex]; |
| 2056 | flags = next->ie_flags; |
| 2057 | |
| 2058 | /* walk down if it has a subnode */ |
| 2059 | |
| 2060 | if (flags & INDEX_ENTRY_NODE) { |
| 2061 | next = ntfs_index_walk_down(next,ictx); |
| 2062 | } else { |
| 2063 | |
| 2064 | /* walk up it has no subnode, nor data */ |
| 2065 | |
| 2066 | if (flags & INDEX_ENTRY_END) { |
| 2067 | next = ntfs_index_walk_up(next, ictx); |
| 2068 | } |
| 2069 | } |
| 2070 | } |
| 2071 | /* return NULL if stuck at end of a block */ |
| 2072 | |
| 2073 | if (next && (next->ie_flags & INDEX_ENTRY_END)) |
| 2074 | next = (INDEX_ENTRY*)NULL; |
| 2075 | return (next); |
| 2076 | } |
| 2077 | |
| 2078 | |