Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

ntfs: update directory operations

Update the directory and index operations to support full read-write
functionality and use the folio API, including directory modification.

Acked-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Namjae Jeon <linkinjeon@kernel.org>

+2678 -1305
+633 -937
fs/ntfs/dir.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0-or-later 2 2 /* 3 - * dir.c - NTFS kernel directory operations. Part of the Linux-NTFS project. 3 + * NTFS kernel directory operations. 4 4 * 5 5 * Copyright (c) 2001-2007 Anton Altaparmakov 6 6 * Copyright (c) 2002 Richard Russon 7 + * Copyright (c) 2025 LG Electronics Co., Ltd. 7 8 */ 8 9 9 - #include <linux/buffer_head.h> 10 - #include <linux/slab.h> 11 10 #include <linux/blkdev.h> 12 11 13 12 #include "dir.h" 14 - #include "aops.h" 15 - #include "attrib.h" 16 13 #include "mft.h" 17 - #include "debug.h" 18 14 #include "ntfs.h" 15 + #include "index.h" 16 + #include "reparse.h" 17 + 18 + #include <linux/filelock.h> 19 19 20 20 /* 21 21 * The little endian Unicode string $I30 as a global constant. 22 22 */ 23 - ntfschar I30[5] = { cpu_to_le16('$'), cpu_to_le16('I'), 23 + __le16 I30[5] = { cpu_to_le16('$'), cpu_to_le16('I'), 24 24 cpu_to_le16('3'), cpu_to_le16('0'), 0 }; 25 25 26 - /** 26 + /* 27 27 * ntfs_lookup_inode_by_name - find an inode in a directory given its name 28 28 * @dir_ni: ntfs inode of the directory in which to search for the name 29 29 * @uname: Unicode name for which to search in the directory ··· 61 61 * locked whilst being accessed otherwise we may find a corrupt 62 62 * page due to it being under ->writepage at the moment which 63 63 * applies the mst protection fixups before writing out and then 64 - * removes them again after the write is complete after which it 64 + * removes them again after the write is complete after which it 65 65 * unlocks the page. 66 66 */ 67 - MFT_REF ntfs_lookup_inode_by_name(ntfs_inode *dir_ni, const ntfschar *uname, 68 - const int uname_len, ntfs_name **res) 67 + u64 ntfs_lookup_inode_by_name(struct ntfs_inode *dir_ni, const __le16 *uname, 68 + const int uname_len, struct ntfs_name **res) 69 69 { 70 - ntfs_volume *vol = dir_ni->vol; 70 + struct ntfs_volume *vol = dir_ni->vol; 71 71 struct super_block *sb = vol->sb; 72 - MFT_RECORD *m; 73 - INDEX_ROOT *ir; 74 - INDEX_ENTRY *ie; 75 - INDEX_ALLOCATION *ia; 72 + struct inode *ia_vi = NULL; 73 + struct mft_record *m; 74 + struct index_root *ir; 75 + struct index_entry *ie; 76 + struct index_block *ia; 76 77 u8 *index_end; 77 78 u64 mref; 78 - ntfs_attr_search_ctx *ctx; 79 + struct ntfs_attr_search_ctx *ctx; 79 80 int err, rc; 80 - VCN vcn, old_vcn; 81 + s64 vcn, old_vcn; 81 82 struct address_space *ia_mapping; 82 - struct page *page; 83 - u8 *kaddr; 84 - ntfs_name *name = NULL; 83 + struct folio *folio; 84 + u8 *kaddr = NULL; 85 + struct ntfs_name *name = NULL; 85 86 86 - BUG_ON(!S_ISDIR(VFS_I(dir_ni)->i_mode)); 87 - BUG_ON(NInoAttr(dir_ni)); 88 87 /* Get hold of the mft record for the directory. */ 89 88 m = map_mft_record(dir_ni); 90 89 if (IS_ERR(m)) { ··· 101 102 0, ctx); 102 103 if (unlikely(err)) { 103 104 if (err == -ENOENT) { 104 - ntfs_error(sb, "Index root attribute missing in " 105 - "directory inode 0x%lx.", 106 - dir_ni->mft_no); 105 + ntfs_error(sb, 106 + "Index root attribute missing in directory inode 0x%lx.", 107 + dir_ni->mft_no); 107 108 err = -EIO; 108 109 } 109 110 goto err_out; 110 111 } 111 112 /* Get to the index root value (it's been verified in read_inode). */ 112 - ir = (INDEX_ROOT*)((u8*)ctx->attr + 113 + ir = (struct index_root *)((u8 *)ctx->attr + 113 114 le16_to_cpu(ctx->attr->data.resident.value_offset)); 114 - index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length); 115 + index_end = (u8 *)&ir->index + le32_to_cpu(ir->index.index_length); 115 116 /* The first index entry. */ 116 - ie = (INDEX_ENTRY*)((u8*)&ir->index + 117 + ie = (struct index_entry *)((u8 *)&ir->index + 117 118 le32_to_cpu(ir->index.entries_offset)); 118 119 /* 119 120 * Loop until we exceed valid memory (corruption case) or until we 120 121 * reach the last entry. 121 122 */ 122 - for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) { 123 + for (;; ie = (struct index_entry *)((u8 *)ie + le16_to_cpu(ie->length))) { 123 124 /* Bounds checks. */ 124 - if ((u8*)ie < (u8*)ctx->mrec || (u8*)ie + 125 - sizeof(INDEX_ENTRY_HEADER) > index_end || 126 - (u8*)ie + le16_to_cpu(ie->key_length) > 127 - index_end) 125 + if ((u8 *)ie < (u8 *)ctx->mrec || 126 + (u8 *)ie + sizeof(struct index_entry_header) > index_end || 127 + (u8 *)ie + sizeof(struct index_entry_header) + le16_to_cpu(ie->key_length) > 128 + index_end || (u8 *)ie + le16_to_cpu(ie->length) > index_end) 128 129 goto dir_err_out; 129 130 /* 130 131 * The last entry cannot contain a name. It can however contain ··· 132 133 */ 133 134 if (ie->flags & INDEX_ENTRY_END) 134 135 break; 136 + /* Key length should not be zero if it is not last entry. */ 137 + if (!ie->key_length) 138 + goto dir_err_out; 139 + /* Check the consistency of an index entry */ 140 + if (ntfs_index_entry_inconsistent(NULL, vol, ie, COLLATION_FILE_NAME, 141 + dir_ni->mft_no)) 142 + goto dir_err_out; 135 143 /* 136 144 * We perform a case sensitive comparison and if that matches 137 145 * we are done and return the mft reference of the inode (i.e. ··· 147 141 * returning. 148 142 */ 149 143 if (ntfs_are_names_equal(uname, uname_len, 150 - (ntfschar*)&ie->key.file_name.file_name, 144 + (__le16 *)&ie->key.file_name.file_name, 151 145 ie->key.file_name.file_name_length, 152 146 CASE_SENSITIVE, vol->upcase, vol->upcase_len)) { 153 147 found_it: ··· 163 157 */ 164 158 if (ie->key.file_name.file_name_type == FILE_NAME_DOS) { 165 159 if (!name) { 166 - name = kmalloc(sizeof(ntfs_name), 160 + name = kmalloc(sizeof(struct ntfs_name), 167 161 GFP_NOFS); 168 162 if (!name) { 169 163 err = -ENOMEM; ··· 194 188 * only cache the mft reference and the file name type (we set 195 189 * the name length to zero for simplicity). 196 190 */ 197 - if (!NVolCaseSensitive(vol) && 198 - ie->key.file_name.file_name_type && 199 - ntfs_are_names_equal(uname, uname_len, 200 - (ntfschar*)&ie->key.file_name.file_name, 201 - ie->key.file_name.file_name_length, 202 - IGNORE_CASE, vol->upcase, vol->upcase_len)) { 203 - int name_size = sizeof(ntfs_name); 191 + if ((!NVolCaseSensitive(vol) || 192 + ie->key.file_name.file_name_type == FILE_NAME_DOS) && 193 + ntfs_are_names_equal(uname, uname_len, 194 + (__le16 *)&ie->key.file_name.file_name, 195 + ie->key.file_name.file_name_length, 196 + IGNORE_CASE, vol->upcase, 197 + vol->upcase_len)) { 198 + int name_size = sizeof(struct ntfs_name); 204 199 u8 type = ie->key.file_name.file_name_type; 205 200 u8 len = ie->key.file_name.file_name_length; 206 201 207 202 /* Only one case insensitive matching name allowed. */ 208 203 if (name) { 209 - ntfs_error(sb, "Found already allocated name " 210 - "in phase 1. Please run chkdsk " 211 - "and if that doesn't find any " 212 - "errors please report you saw " 213 - "this message to " 214 - "linux-ntfs-dev@lists." 215 - "sourceforge.net."); 204 + ntfs_error(sb, 205 + "Found already allocated name in phase 1. Please run chkdsk"); 216 206 goto dir_err_out; 217 207 } 218 208 219 209 if (type != FILE_NAME_DOS) 220 - name_size += len * sizeof(ntfschar); 210 + name_size += len * sizeof(__le16); 221 211 name = kmalloc(name_size, GFP_NOFS); 222 212 if (!name) { 223 213 err = -ENOMEM; ··· 224 222 if (type != FILE_NAME_DOS) { 225 223 name->len = len; 226 224 memcpy(name->name, ie->key.file_name.file_name, 227 - len * sizeof(ntfschar)); 225 + len * sizeof(__le16)); 228 226 } else 229 227 name->len = 0; 230 228 *res = name; ··· 234 232 * know which way in the B+tree we have to go. 235 233 */ 236 234 rc = ntfs_collate_names(uname, uname_len, 237 - (ntfschar*)&ie->key.file_name.file_name, 235 + (__le16 *)&ie->key.file_name.file_name, 238 236 ie->key.file_name.file_name_length, 1, 239 237 IGNORE_CASE, vol->upcase, vol->upcase_len); 240 238 /* ··· 253 251 * collation. 254 252 */ 255 253 rc = ntfs_collate_names(uname, uname_len, 256 - (ntfschar*)&ie->key.file_name.file_name, 254 + (__le16 *)&ie->key.file_name.file_name, 257 255 ie->key.file_name.file_name_length, 1, 258 256 CASE_SENSITIVE, vol->upcase, vol->upcase_len); 259 257 if (rc == -1) ··· 283 281 err = -ENOENT; 284 282 goto err_out; 285 283 } /* Child node present, descend into it. */ 286 - /* Consistency check: Verify that an index allocation exists. */ 287 - if (!NInoIndexAllocPresent(dir_ni)) { 288 - ntfs_error(sb, "No index allocation attribute but index entry " 289 - "requires one. Directory inode 0x%lx is " 290 - "corrupt or driver bug.", dir_ni->mft_no); 291 - goto err_out; 292 - } 284 + 293 285 /* Get the starting vcn of the index_block holding the child node. */ 294 - vcn = sle64_to_cpup((sle64*)((u8*)ie + le16_to_cpu(ie->length) - 8)); 295 - ia_mapping = VFS_I(dir_ni)->i_mapping; 286 + vcn = le64_to_cpup((__le64 *)((u8 *)ie + le16_to_cpu(ie->length) - 8)); 287 + 296 288 /* 297 289 * We are done with the index root and the mft record. Release them, 298 - * otherwise we deadlock with ntfs_map_page(). 290 + * otherwise we deadlock with read_mapping_folio(). 299 291 */ 300 292 ntfs_attr_put_search_ctx(ctx); 301 293 unmap_mft_record(dir_ni); 302 294 m = NULL; 303 295 ctx = NULL; 296 + 297 + ia_vi = ntfs_index_iget(VFS_I(dir_ni), I30, 4); 298 + if (IS_ERR(ia_vi)) { 299 + err = PTR_ERR(ia_vi); 300 + goto err_out; 301 + } 302 + 303 + ia_mapping = ia_vi->i_mapping; 304 304 descend_into_child_node: 305 305 /* 306 306 * Convert vcn to index into the index allocation attribute in units 307 307 * of PAGE_SIZE and map the page cache page, reading it from 308 308 * disk if necessary. 309 309 */ 310 - page = ntfs_map_page(ia_mapping, vcn << 311 - dir_ni->itype.index.vcn_size_bits >> PAGE_SHIFT); 312 - if (IS_ERR(page)) { 310 + folio = read_mapping_folio(ia_mapping, vcn << 311 + dir_ni->itype.index.vcn_size_bits >> PAGE_SHIFT, NULL); 312 + if (IS_ERR(folio)) { 313 313 ntfs_error(sb, "Failed to map directory index page, error %ld.", 314 - -PTR_ERR(page)); 315 - err = PTR_ERR(page); 314 + -PTR_ERR(folio)); 315 + err = PTR_ERR(folio); 316 316 goto err_out; 317 317 } 318 - lock_page(page); 319 - kaddr = (u8*)page_address(page); 318 + 319 + folio_lock(folio); 320 + kaddr = kmalloc(PAGE_SIZE, GFP_NOFS); 321 + if (!kaddr) { 322 + err = -ENOMEM; 323 + folio_unlock(folio); 324 + folio_put(folio); 325 + goto unm_err_out; 326 + } 327 + 328 + memcpy_from_folio(kaddr, folio, 0, PAGE_SIZE); 329 + post_read_mst_fixup((struct ntfs_record *)kaddr, PAGE_SIZE); 330 + folio_unlock(folio); 331 + folio_put(folio); 320 332 fast_descend_into_child_node: 321 333 /* Get to the index allocation block. */ 322 - ia = (INDEX_ALLOCATION*)(kaddr + ((vcn << 334 + ia = (struct index_block *)(kaddr + ((vcn << 323 335 dir_ni->itype.index.vcn_size_bits) & ~PAGE_MASK)); 324 336 /* Bounds checks. */ 325 - if ((u8*)ia < kaddr || (u8*)ia > kaddr + PAGE_SIZE) { 326 - ntfs_error(sb, "Out of bounds check failed. Corrupt directory " 327 - "inode 0x%lx or driver bug.", dir_ni->mft_no); 337 + if ((u8 *)ia < kaddr || (u8 *)ia > kaddr + PAGE_SIZE) { 338 + ntfs_error(sb, 339 + "Out of bounds check failed. Corrupt directory inode 0x%lx or driver bug.", 340 + dir_ni->mft_no); 328 341 goto unm_err_out; 329 342 } 330 343 /* Catch multi sector transfer fixup errors. */ 331 344 if (unlikely(!ntfs_is_indx_record(ia->magic))) { 332 - ntfs_error(sb, "Directory index record with vcn 0x%llx is " 333 - "corrupt. Corrupt inode 0x%lx. Run chkdsk.", 334 - (unsigned long long)vcn, dir_ni->mft_no); 345 + ntfs_error(sb, 346 + "Directory index record with vcn 0x%llx is corrupt. Corrupt inode 0x%lx. Run chkdsk.", 347 + (unsigned long long)vcn, dir_ni->mft_no); 335 348 goto unm_err_out; 336 349 } 337 - if (sle64_to_cpu(ia->index_block_vcn) != vcn) { 338 - ntfs_error(sb, "Actual VCN (0x%llx) of index buffer is " 339 - "different from expected VCN (0x%llx). " 340 - "Directory inode 0x%lx is corrupt or driver " 341 - "bug.", (unsigned long long) 342 - sle64_to_cpu(ia->index_block_vcn), 343 - (unsigned long long)vcn, dir_ni->mft_no); 350 + if (le64_to_cpu(ia->index_block_vcn) != vcn) { 351 + ntfs_error(sb, 352 + "Actual VCN (0x%llx) of index buffer is different from expected VCN (0x%llx). Directory inode 0x%lx is corrupt or driver bug.", 353 + (unsigned long long)le64_to_cpu(ia->index_block_vcn), 354 + (unsigned long long)vcn, dir_ni->mft_no); 344 355 goto unm_err_out; 345 356 } 346 357 if (le32_to_cpu(ia->index.allocated_size) + 0x18 != 347 358 dir_ni->itype.index.block_size) { 348 - ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode " 349 - "0x%lx has a size (%u) differing from the " 350 - "directory specified size (%u). Directory " 351 - "inode is corrupt or driver bug.", 352 - (unsigned long long)vcn, dir_ni->mft_no, 353 - le32_to_cpu(ia->index.allocated_size) + 0x18, 354 - dir_ni->itype.index.block_size); 359 + ntfs_error(sb, 360 + "Index buffer (VCN 0x%llx) of directory inode 0x%lx has a size (%u) differing from the directory specified size (%u). Directory inode is corrupt or driver bug.", 361 + (unsigned long long)vcn, dir_ni->mft_no, 362 + le32_to_cpu(ia->index.allocated_size) + 0x18, 363 + dir_ni->itype.index.block_size); 355 364 goto unm_err_out; 356 365 } 357 - index_end = (u8*)ia + dir_ni->itype.index.block_size; 366 + index_end = (u8 *)ia + dir_ni->itype.index.block_size; 358 367 if (index_end > kaddr + PAGE_SIZE) { 359 - ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode " 360 - "0x%lx crosses page boundary. Impossible! " 361 - "Cannot access! This is probably a bug in the " 362 - "driver.", (unsigned long long)vcn, 363 - dir_ni->mft_no); 368 + ntfs_error(sb, 369 + "Index buffer (VCN 0x%llx) of directory inode 0x%lx crosses page boundary. Impossible! Cannot access! This is probably a bug in the driver.", 370 + (unsigned long long)vcn, dir_ni->mft_no); 364 371 goto unm_err_out; 365 372 } 366 - index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length); 367 - if (index_end > (u8*)ia + dir_ni->itype.index.block_size) { 368 - ntfs_error(sb, "Size of index buffer (VCN 0x%llx) of directory " 369 - "inode 0x%lx exceeds maximum size.", 370 - (unsigned long long)vcn, dir_ni->mft_no); 373 + index_end = (u8 *)&ia->index + le32_to_cpu(ia->index.index_length); 374 + if (index_end > (u8 *)ia + dir_ni->itype.index.block_size) { 375 + ntfs_error(sb, 376 + "Size of index buffer (VCN 0x%llx) of directory inode 0x%lx exceeds maximum size.", 377 + (unsigned long long)vcn, dir_ni->mft_no); 371 378 goto unm_err_out; 372 379 } 373 380 /* The first index entry. */ 374 - ie = (INDEX_ENTRY*)((u8*)&ia->index + 381 + ie = (struct index_entry *)((u8 *)&ia->index + 375 382 le32_to_cpu(ia->index.entries_offset)); 376 383 /* 377 384 * Iterate similar to above big loop but applied to index buffer, thus 378 385 * loop until we exceed valid memory (corruption case) or until we 379 386 * reach the last entry. 380 387 */ 381 - for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) { 382 - /* Bounds check. */ 383 - if ((u8*)ie < (u8*)ia || (u8*)ie + 384 - sizeof(INDEX_ENTRY_HEADER) > index_end || 385 - (u8*)ie + le16_to_cpu(ie->key_length) > 386 - index_end) { 387 - ntfs_error(sb, "Index entry out of bounds in " 388 - "directory inode 0x%lx.", 388 + for (;; ie = (struct index_entry *)((u8 *)ie + le16_to_cpu(ie->length))) { 389 + /* Bounds checks. */ 390 + if ((u8 *)ie < (u8 *)ia || 391 + (u8 *)ie + sizeof(struct index_entry_header) > index_end || 392 + (u8 *)ie + sizeof(struct index_entry_header) + le16_to_cpu(ie->key_length) > 393 + index_end || (u8 *)ie + le16_to_cpu(ie->length) > index_end) { 394 + ntfs_error(sb, "Index entry out of bounds in directory inode 0x%lx.", 389 395 dir_ni->mft_no); 390 396 goto unm_err_out; 391 397 } ··· 403 393 */ 404 394 if (ie->flags & INDEX_ENTRY_END) 405 395 break; 396 + /* Key length should not be zero if it is not last entry. */ 397 + if (!ie->key_length) 398 + goto unm_err_out; 399 + /* Check the consistency of an index entry */ 400 + if (ntfs_index_entry_inconsistent(NULL, vol, ie, COLLATION_FILE_NAME, 401 + dir_ni->mft_no)) 402 + goto unm_err_out; 406 403 /* 407 404 * We perform a case sensitive comparison and if that matches 408 405 * we are done and return the mft reference of the inode (i.e. ··· 418 401 * returning. 419 402 */ 420 403 if (ntfs_are_names_equal(uname, uname_len, 421 - (ntfschar*)&ie->key.file_name.file_name, 404 + (__le16 *)&ie->key.file_name.file_name, 422 405 ie->key.file_name.file_name_length, 423 406 CASE_SENSITIVE, vol->upcase, vol->upcase_len)) { 424 407 found_it2: ··· 434 417 */ 435 418 if (ie->key.file_name.file_name_type == FILE_NAME_DOS) { 436 419 if (!name) { 437 - name = kmalloc(sizeof(ntfs_name), 420 + name = kmalloc(sizeof(struct ntfs_name), 438 421 GFP_NOFS); 439 422 if (!name) { 440 423 err = -ENOMEM; ··· 451 434 *res = NULL; 452 435 } 453 436 mref = le64_to_cpu(ie->data.dir.indexed_file); 454 - unlock_page(page); 455 - ntfs_unmap_page(page); 437 + kfree(kaddr); 438 + iput(ia_vi); 456 439 return mref; 457 440 } 458 441 /* ··· 465 448 * only cache the mft reference and the file name type (we set 466 449 * the name length to zero for simplicity). 467 450 */ 468 - if (!NVolCaseSensitive(vol) && 469 - ie->key.file_name.file_name_type && 470 - ntfs_are_names_equal(uname, uname_len, 471 - (ntfschar*)&ie->key.file_name.file_name, 472 - ie->key.file_name.file_name_length, 473 - IGNORE_CASE, vol->upcase, vol->upcase_len)) { 474 - int name_size = sizeof(ntfs_name); 451 + if ((!NVolCaseSensitive(vol) || 452 + ie->key.file_name.file_name_type == FILE_NAME_DOS) && 453 + ntfs_are_names_equal(uname, uname_len, 454 + (__le16 *)&ie->key.file_name.file_name, 455 + ie->key.file_name.file_name_length, 456 + IGNORE_CASE, vol->upcase, 457 + vol->upcase_len)) { 458 + int name_size = sizeof(struct ntfs_name); 475 459 u8 type = ie->key.file_name.file_name_type; 476 460 u8 len = ie->key.file_name.file_name_length; 477 461 478 462 /* Only one case insensitive matching name allowed. */ 479 463 if (name) { 480 - ntfs_error(sb, "Found already allocated name " 481 - "in phase 2. Please run chkdsk " 482 - "and if that doesn't find any " 483 - "errors please report you saw " 484 - "this message to " 485 - "linux-ntfs-dev@lists." 486 - "sourceforge.net."); 487 - unlock_page(page); 488 - ntfs_unmap_page(page); 464 + ntfs_error(sb, 465 + "Found already allocated name in phase 2. Please run chkdsk"); 466 + kfree(kaddr); 489 467 goto dir_err_out; 490 468 } 491 469 492 470 if (type != FILE_NAME_DOS) 493 - name_size += len * sizeof(ntfschar); 471 + name_size += len * sizeof(__le16); 494 472 name = kmalloc(name_size, GFP_NOFS); 495 473 if (!name) { 496 474 err = -ENOMEM; ··· 496 484 if (type != FILE_NAME_DOS) { 497 485 name->len = len; 498 486 memcpy(name->name, ie->key.file_name.file_name, 499 - len * sizeof(ntfschar)); 487 + len * sizeof(__le16)); 500 488 } else 501 489 name->len = 0; 502 490 *res = name; ··· 506 494 * know which way in the B+tree we have to go. 507 495 */ 508 496 rc = ntfs_collate_names(uname, uname_len, 509 - (ntfschar*)&ie->key.file_name.file_name, 497 + (__le16 *)&ie->key.file_name.file_name, 510 498 ie->key.file_name.file_name_length, 1, 511 499 IGNORE_CASE, vol->upcase, vol->upcase_len); 512 500 /* ··· 525 513 * collation. 526 514 */ 527 515 rc = ntfs_collate_names(uname, uname_len, 528 - (ntfschar*)&ie->key.file_name.file_name, 516 + (__le16 *)&ie->key.file_name.file_name, 529 517 ie->key.file_name.file_name_length, 1, 530 518 CASE_SENSITIVE, vol->upcase, vol->upcase_len); 531 519 if (rc == -1) ··· 545 533 */ 546 534 if (ie->flags & INDEX_ENTRY_NODE) { 547 535 if ((ia->index.flags & NODE_MASK) == LEAF_NODE) { 548 - ntfs_error(sb, "Index entry with child node found in " 549 - "a leaf node in directory inode 0x%lx.", 550 - dir_ni->mft_no); 536 + ntfs_error(sb, 537 + "Index entry with child node found in a leaf node in directory inode 0x%lx.", 538 + dir_ni->mft_no); 551 539 goto unm_err_out; 552 540 } 553 541 /* Child node present, descend into it. */ 554 542 old_vcn = vcn; 555 - vcn = sle64_to_cpup((sle64*)((u8*)ie + 543 + vcn = le64_to_cpup((__le64 *)((u8 *)ie + 556 544 le16_to_cpu(ie->length) - 8)); 557 545 if (vcn >= 0) { 558 - /* If vcn is in the same page cache page as old_vcn we 559 - * recycle the mapped page. */ 560 - if (old_vcn << vol->cluster_size_bits >> 561 - PAGE_SHIFT == vcn << 562 - vol->cluster_size_bits >> 563 - PAGE_SHIFT) 546 + /* 547 + * If vcn is in the same page cache page as old_vcn we 548 + * recycle the mapped page. 549 + */ 550 + if (ntfs_cluster_to_pidx(vol, old_vcn) == 551 + ntfs_cluster_to_pidx(vol, vcn)) 564 552 goto fast_descend_into_child_node; 565 - unlock_page(page); 566 - ntfs_unmap_page(page); 553 + kfree(kaddr); 554 + kaddr = NULL; 567 555 goto descend_into_child_node; 568 556 } 569 - ntfs_error(sb, "Negative child node vcn in directory inode " 570 - "0x%lx.", dir_ni->mft_no); 557 + ntfs_error(sb, "Negative child node vcn in directory inode 0x%lx.", 558 + dir_ni->mft_no); 571 559 goto unm_err_out; 572 560 } 573 561 /* ··· 576 564 * associated with it. 577 565 */ 578 566 if (name) { 579 - unlock_page(page); 580 - ntfs_unmap_page(page); 567 + kfree(kaddr); 568 + iput(ia_vi); 581 569 return name->mref; 582 570 } 583 571 ntfs_debug("Entry not found."); 584 572 err = -ENOENT; 585 573 unm_err_out: 586 - unlock_page(page); 587 - ntfs_unmap_page(page); 574 + kfree(kaddr); 588 575 err_out: 589 576 if (!err) 590 577 err = -EIO; ··· 591 580 ntfs_attr_put_search_ctx(ctx); 592 581 if (m) 593 582 unmap_mft_record(dir_ni); 594 - if (name) { 595 - kfree(name); 596 - *res = NULL; 597 - } 583 + kfree(name); 584 + *res = NULL; 585 + if (ia_vi && !IS_ERR(ia_vi)) 586 + iput(ia_vi); 598 587 return ERR_MREF(err); 599 588 dir_err_out: 600 589 ntfs_error(sb, "Corrupt directory. Aborting lookup."); 601 590 goto err_out; 602 591 } 603 592 604 - #if 0 605 - 606 - // TODO: (AIA) 607 - // The algorithm embedded in this code will be required for the time when we 608 - // want to support adding of entries to directories, where we require correct 609 - // collation of file names in order not to cause corruption of the filesystem. 610 - 611 - /** 612 - * ntfs_lookup_inode_by_name - find an inode in a directory given its name 613 - * @dir_ni: ntfs inode of the directory in which to search for the name 614 - * @uname: Unicode name for which to search in the directory 615 - * @uname_len: length of the name @uname in Unicode characters 616 - * 617 - * Look for an inode with name @uname in the directory with inode @dir_ni. 618 - * ntfs_lookup_inode_by_name() walks the contents of the directory looking for 619 - * the Unicode name. If the name is found in the directory, the corresponding 620 - * inode number (>= 0) is returned as a mft reference in cpu format, i.e. it 621 - * is a 64-bit number containing the sequence number. 622 - * 623 - * On error, a negative value is returned corresponding to the error code. In 624 - * particular if the inode is not found -ENOENT is returned. Note that you 625 - * can't just check the return value for being negative, you have to check the 626 - * inode number for being negative which you can extract using MREC(return 627 - * value). 628 - * 629 - * Note, @uname_len does not include the (optional) terminating NULL character. 630 - */ 631 - u64 ntfs_lookup_inode_by_name(ntfs_inode *dir_ni, const ntfschar *uname, 632 - const int uname_len) 633 - { 634 - ntfs_volume *vol = dir_ni->vol; 635 - struct super_block *sb = vol->sb; 636 - MFT_RECORD *m; 637 - INDEX_ROOT *ir; 638 - INDEX_ENTRY *ie; 639 - INDEX_ALLOCATION *ia; 640 - u8 *index_end; 641 - u64 mref; 642 - ntfs_attr_search_ctx *ctx; 643 - int err, rc; 644 - IGNORE_CASE_BOOL ic; 645 - VCN vcn, old_vcn; 646 - struct address_space *ia_mapping; 647 - struct page *page; 648 - u8 *kaddr; 649 - 650 - /* Get hold of the mft record for the directory. */ 651 - m = map_mft_record(dir_ni); 652 - if (IS_ERR(m)) { 653 - ntfs_error(sb, "map_mft_record() failed with error code %ld.", 654 - -PTR_ERR(m)); 655 - return ERR_MREF(PTR_ERR(m)); 656 - } 657 - ctx = ntfs_attr_get_search_ctx(dir_ni, m); 658 - if (!ctx) { 659 - err = -ENOMEM; 660 - goto err_out; 661 - } 662 - /* Find the index root attribute in the mft record. */ 663 - err = ntfs_attr_lookup(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 0, NULL, 664 - 0, ctx); 665 - if (unlikely(err)) { 666 - if (err == -ENOENT) { 667 - ntfs_error(sb, "Index root attribute missing in " 668 - "directory inode 0x%lx.", 669 - dir_ni->mft_no); 670 - err = -EIO; 671 - } 672 - goto err_out; 673 - } 674 - /* Get to the index root value (it's been verified in read_inode). */ 675 - ir = (INDEX_ROOT*)((u8*)ctx->attr + 676 - le16_to_cpu(ctx->attr->data.resident.value_offset)); 677 - index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length); 678 - /* The first index entry. */ 679 - ie = (INDEX_ENTRY*)((u8*)&ir->index + 680 - le32_to_cpu(ir->index.entries_offset)); 681 - /* 682 - * Loop until we exceed valid memory (corruption case) or until we 683 - * reach the last entry. 684 - */ 685 - for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) { 686 - /* Bounds checks. */ 687 - if ((u8*)ie < (u8*)ctx->mrec || (u8*)ie + 688 - sizeof(INDEX_ENTRY_HEADER) > index_end || 689 - (u8*)ie + le16_to_cpu(ie->key_length) > 690 - index_end) 691 - goto dir_err_out; 692 - /* 693 - * The last entry cannot contain a name. It can however contain 694 - * a pointer to a child node in the B+tree so we just break out. 695 - */ 696 - if (ie->flags & INDEX_ENTRY_END) 697 - break; 698 - /* 699 - * If the current entry has a name type of POSIX, the name is 700 - * case sensitive and not otherwise. This has the effect of us 701 - * not being able to access any POSIX file names which collate 702 - * after the non-POSIX one when they only differ in case, but 703 - * anyone doing screwy stuff like that deserves to burn in 704 - * hell... Doing that kind of stuff on NT4 actually causes 705 - * corruption on the partition even when using SP6a and Linux 706 - * is not involved at all. 707 - */ 708 - ic = ie->key.file_name.file_name_type ? IGNORE_CASE : 709 - CASE_SENSITIVE; 710 - /* 711 - * If the names match perfectly, we are done and return the 712 - * mft reference of the inode (i.e. the inode number together 713 - * with the sequence number for consistency checking. We 714 - * convert it to cpu format before returning. 715 - */ 716 - if (ntfs_are_names_equal(uname, uname_len, 717 - (ntfschar*)&ie->key.file_name.file_name, 718 - ie->key.file_name.file_name_length, ic, 719 - vol->upcase, vol->upcase_len)) { 720 - found_it: 721 - mref = le64_to_cpu(ie->data.dir.indexed_file); 722 - ntfs_attr_put_search_ctx(ctx); 723 - unmap_mft_record(dir_ni); 724 - return mref; 725 - } 726 - /* 727 - * Not a perfect match, need to do full blown collation so we 728 - * know which way in the B+tree we have to go. 729 - */ 730 - rc = ntfs_collate_names(uname, uname_len, 731 - (ntfschar*)&ie->key.file_name.file_name, 732 - ie->key.file_name.file_name_length, 1, 733 - IGNORE_CASE, vol->upcase, vol->upcase_len); 734 - /* 735 - * If uname collates before the name of the current entry, there 736 - * is definitely no such name in this index but we might need to 737 - * descend into the B+tree so we just break out of the loop. 738 - */ 739 - if (rc == -1) 740 - break; 741 - /* The names are not equal, continue the search. */ 742 - if (rc) 743 - continue; 744 - /* 745 - * Names match with case insensitive comparison, now try the 746 - * case sensitive comparison, which is required for proper 747 - * collation. 748 - */ 749 - rc = ntfs_collate_names(uname, uname_len, 750 - (ntfschar*)&ie->key.file_name.file_name, 751 - ie->key.file_name.file_name_length, 1, 752 - CASE_SENSITIVE, vol->upcase, vol->upcase_len); 753 - if (rc == -1) 754 - break; 755 - if (rc) 756 - continue; 757 - /* 758 - * Perfect match, this will never happen as the 759 - * ntfs_are_names_equal() call will have gotten a match but we 760 - * still treat it correctly. 761 - */ 762 - goto found_it; 763 - } 764 - /* 765 - * We have finished with this index without success. Check for the 766 - * presence of a child node. 767 - */ 768 - if (!(ie->flags & INDEX_ENTRY_NODE)) { 769 - /* No child node, return -ENOENT. */ 770 - err = -ENOENT; 771 - goto err_out; 772 - } /* Child node present, descend into it. */ 773 - /* Consistency check: Verify that an index allocation exists. */ 774 - if (!NInoIndexAllocPresent(dir_ni)) { 775 - ntfs_error(sb, "No index allocation attribute but index entry " 776 - "requires one. Directory inode 0x%lx is " 777 - "corrupt or driver bug.", dir_ni->mft_no); 778 - goto err_out; 779 - } 780 - /* Get the starting vcn of the index_block holding the child node. */ 781 - vcn = sle64_to_cpup((u8*)ie + le16_to_cpu(ie->length) - 8); 782 - ia_mapping = VFS_I(dir_ni)->i_mapping; 783 - /* 784 - * We are done with the index root and the mft record. Release them, 785 - * otherwise we deadlock with ntfs_map_page(). 786 - */ 787 - ntfs_attr_put_search_ctx(ctx); 788 - unmap_mft_record(dir_ni); 789 - m = NULL; 790 - ctx = NULL; 791 - descend_into_child_node: 792 - /* 793 - * Convert vcn to index into the index allocation attribute in units 794 - * of PAGE_SIZE and map the page cache page, reading it from 795 - * disk if necessary. 796 - */ 797 - page = ntfs_map_page(ia_mapping, vcn << 798 - dir_ni->itype.index.vcn_size_bits >> PAGE_SHIFT); 799 - if (IS_ERR(page)) { 800 - ntfs_error(sb, "Failed to map directory index page, error %ld.", 801 - -PTR_ERR(page)); 802 - err = PTR_ERR(page); 803 - goto err_out; 804 - } 805 - lock_page(page); 806 - kaddr = (u8*)page_address(page); 807 - fast_descend_into_child_node: 808 - /* Get to the index allocation block. */ 809 - ia = (INDEX_ALLOCATION*)(kaddr + ((vcn << 810 - dir_ni->itype.index.vcn_size_bits) & ~PAGE_MASK)); 811 - /* Bounds checks. */ 812 - if ((u8*)ia < kaddr || (u8*)ia > kaddr + PAGE_SIZE) { 813 - ntfs_error(sb, "Out of bounds check failed. Corrupt directory " 814 - "inode 0x%lx or driver bug.", dir_ni->mft_no); 815 - goto unm_err_out; 816 - } 817 - /* Catch multi sector transfer fixup errors. */ 818 - if (unlikely(!ntfs_is_indx_record(ia->magic))) { 819 - ntfs_error(sb, "Directory index record with vcn 0x%llx is " 820 - "corrupt. Corrupt inode 0x%lx. Run chkdsk.", 821 - (unsigned long long)vcn, dir_ni->mft_no); 822 - goto unm_err_out; 823 - } 824 - if (sle64_to_cpu(ia->index_block_vcn) != vcn) { 825 - ntfs_error(sb, "Actual VCN (0x%llx) of index buffer is " 826 - "different from expected VCN (0x%llx). " 827 - "Directory inode 0x%lx is corrupt or driver " 828 - "bug.", (unsigned long long) 829 - sle64_to_cpu(ia->index_block_vcn), 830 - (unsigned long long)vcn, dir_ni->mft_no); 831 - goto unm_err_out; 832 - } 833 - if (le32_to_cpu(ia->index.allocated_size) + 0x18 != 834 - dir_ni->itype.index.block_size) { 835 - ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode " 836 - "0x%lx has a size (%u) differing from the " 837 - "directory specified size (%u). Directory " 838 - "inode is corrupt or driver bug.", 839 - (unsigned long long)vcn, dir_ni->mft_no, 840 - le32_to_cpu(ia->index.allocated_size) + 0x18, 841 - dir_ni->itype.index.block_size); 842 - goto unm_err_out; 843 - } 844 - index_end = (u8*)ia + dir_ni->itype.index.block_size; 845 - if (index_end > kaddr + PAGE_SIZE) { 846 - ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode " 847 - "0x%lx crosses page boundary. Impossible! " 848 - "Cannot access! This is probably a bug in the " 849 - "driver.", (unsigned long long)vcn, 850 - dir_ni->mft_no); 851 - goto unm_err_out; 852 - } 853 - index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length); 854 - if (index_end > (u8*)ia + dir_ni->itype.index.block_size) { 855 - ntfs_error(sb, "Size of index buffer (VCN 0x%llx) of directory " 856 - "inode 0x%lx exceeds maximum size.", 857 - (unsigned long long)vcn, dir_ni->mft_no); 858 - goto unm_err_out; 859 - } 860 - /* The first index entry. */ 861 - ie = (INDEX_ENTRY*)((u8*)&ia->index + 862 - le32_to_cpu(ia->index.entries_offset)); 863 - /* 864 - * Iterate similar to above big loop but applied to index buffer, thus 865 - * loop until we exceed valid memory (corruption case) or until we 866 - * reach the last entry. 867 - */ 868 - for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) { 869 - /* Bounds check. */ 870 - if ((u8*)ie < (u8*)ia || (u8*)ie + 871 - sizeof(INDEX_ENTRY_HEADER) > index_end || 872 - (u8*)ie + le16_to_cpu(ie->key_length) > 873 - index_end) { 874 - ntfs_error(sb, "Index entry out of bounds in " 875 - "directory inode 0x%lx.", 876 - dir_ni->mft_no); 877 - goto unm_err_out; 878 - } 879 - /* 880 - * The last entry cannot contain a name. It can however contain 881 - * a pointer to a child node in the B+tree so we just break out. 882 - */ 883 - if (ie->flags & INDEX_ENTRY_END) 884 - break; 885 - /* 886 - * If the current entry has a name type of POSIX, the name is 887 - * case sensitive and not otherwise. This has the effect of us 888 - * not being able to access any POSIX file names which collate 889 - * after the non-POSIX one when they only differ in case, but 890 - * anyone doing screwy stuff like that deserves to burn in 891 - * hell... Doing that kind of stuff on NT4 actually causes 892 - * corruption on the partition even when using SP6a and Linux 893 - * is not involved at all. 894 - */ 895 - ic = ie->key.file_name.file_name_type ? IGNORE_CASE : 896 - CASE_SENSITIVE; 897 - /* 898 - * If the names match perfectly, we are done and return the 899 - * mft reference of the inode (i.e. the inode number together 900 - * with the sequence number for consistency checking. We 901 - * convert it to cpu format before returning. 902 - */ 903 - if (ntfs_are_names_equal(uname, uname_len, 904 - (ntfschar*)&ie->key.file_name.file_name, 905 - ie->key.file_name.file_name_length, ic, 906 - vol->upcase, vol->upcase_len)) { 907 - found_it2: 908 - mref = le64_to_cpu(ie->data.dir.indexed_file); 909 - unlock_page(page); 910 - ntfs_unmap_page(page); 911 - return mref; 912 - } 913 - /* 914 - * Not a perfect match, need to do full blown collation so we 915 - * know which way in the B+tree we have to go. 916 - */ 917 - rc = ntfs_collate_names(uname, uname_len, 918 - (ntfschar*)&ie->key.file_name.file_name, 919 - ie->key.file_name.file_name_length, 1, 920 - IGNORE_CASE, vol->upcase, vol->upcase_len); 921 - /* 922 - * If uname collates before the name of the current entry, there 923 - * is definitely no such name in this index but we might need to 924 - * descend into the B+tree so we just break out of the loop. 925 - */ 926 - if (rc == -1) 927 - break; 928 - /* The names are not equal, continue the search. */ 929 - if (rc) 930 - continue; 931 - /* 932 - * Names match with case insensitive comparison, now try the 933 - * case sensitive comparison, which is required for proper 934 - * collation. 935 - */ 936 - rc = ntfs_collate_names(uname, uname_len, 937 - (ntfschar*)&ie->key.file_name.file_name, 938 - ie->key.file_name.file_name_length, 1, 939 - CASE_SENSITIVE, vol->upcase, vol->upcase_len); 940 - if (rc == -1) 941 - break; 942 - if (rc) 943 - continue; 944 - /* 945 - * Perfect match, this will never happen as the 946 - * ntfs_are_names_equal() call will have gotten a match but we 947 - * still treat it correctly. 948 - */ 949 - goto found_it2; 950 - } 951 - /* 952 - * We have finished with this index buffer without success. Check for 953 - * the presence of a child node. 954 - */ 955 - if (ie->flags & INDEX_ENTRY_NODE) { 956 - if ((ia->index.flags & NODE_MASK) == LEAF_NODE) { 957 - ntfs_error(sb, "Index entry with child node found in " 958 - "a leaf node in directory inode 0x%lx.", 959 - dir_ni->mft_no); 960 - goto unm_err_out; 961 - } 962 - /* Child node present, descend into it. */ 963 - old_vcn = vcn; 964 - vcn = sle64_to_cpup((u8*)ie + le16_to_cpu(ie->length) - 8); 965 - if (vcn >= 0) { 966 - /* If vcn is in the same page cache page as old_vcn we 967 - * recycle the mapped page. */ 968 - if (old_vcn << vol->cluster_size_bits >> 969 - PAGE_SHIFT == vcn << 970 - vol->cluster_size_bits >> 971 - PAGE_SHIFT) 972 - goto fast_descend_into_child_node; 973 - unlock_page(page); 974 - ntfs_unmap_page(page); 975 - goto descend_into_child_node; 976 - } 977 - ntfs_error(sb, "Negative child node vcn in directory inode " 978 - "0x%lx.", dir_ni->mft_no); 979 - goto unm_err_out; 980 - } 981 - /* No child node, return -ENOENT. */ 982 - ntfs_debug("Entry not found."); 983 - err = -ENOENT; 984 - unm_err_out: 985 - unlock_page(page); 986 - ntfs_unmap_page(page); 987 - err_out: 988 - if (!err) 989 - err = -EIO; 990 - if (ctx) 991 - ntfs_attr_put_search_ctx(ctx); 992 - if (m) 993 - unmap_mft_record(dir_ni); 994 - return ERR_MREF(err); 995 - dir_err_out: 996 - ntfs_error(sb, "Corrupt directory. Aborting lookup."); 997 - goto err_out; 998 - } 999 - 1000 - #endif 1001 - 1002 - /** 593 + /* 1003 594 * ntfs_filldir - ntfs specific filldir method 1004 595 * @vol: current ntfs volume 1005 596 * @ndir: ntfs inode of current directory ··· 622 1009 * retake the lock if we are returning a non-zero value as ntfs_readdir() 623 1010 * would need to drop the lock immediately anyway. 624 1011 */ 625 - static inline int ntfs_filldir(ntfs_volume *vol, 626 - ntfs_inode *ndir, struct page *ia_page, INDEX_ENTRY *ie, 1012 + static inline int ntfs_filldir(struct ntfs_volume *vol, 1013 + struct ntfs_inode *ndir, struct page *ia_page, struct index_entry *ie, 627 1014 u8 *name, struct dir_context *actor) 628 1015 { 629 1016 unsigned long mref; 630 1017 int name_len; 631 - unsigned dt_type; 632 - FILE_NAME_TYPE_FLAGS name_type; 1018 + unsigned int dt_type; 1019 + u8 name_type; 633 1020 634 1021 name_type = ie->key.file_name.file_name_type; 635 1022 if (name_type == FILE_NAME_DOS) { ··· 645 1032 ntfs_debug("Skipping system file."); 646 1033 return 0; 647 1034 } 648 - name_len = ntfs_ucstonls(vol, (ntfschar*)&ie->key.file_name.file_name, 1035 + if (!NVolShowHiddenFiles(vol) && 1036 + (ie->key.file_name.file_attributes & FILE_ATTR_HIDDEN)) { 1037 + ntfs_debug("Skipping hidden file."); 1038 + return 0; 1039 + } 1040 + 1041 + name_len = ntfs_ucstonls(vol, (__le16 *)&ie->key.file_name.file_name, 649 1042 ie->key.file_name.file_name_length, &name, 650 1043 NTFS_MAX_NAME_LEN * NLS_MAX_CHARSET_SIZE + 1); 651 1044 if (name_len <= 0) { ··· 659 1040 (long long)MREF_LE(ie->data.dir.indexed_file)); 660 1041 return 0; 661 1042 } 1043 + 1044 + mref = MREF_LE(ie->data.dir.indexed_file); 662 1045 if (ie->key.file_name.file_attributes & 663 1046 FILE_ATTR_DUP_FILE_NAME_INDEX_PRESENT) 664 1047 dt_type = DT_DIR; 1048 + else if (ie->key.file_name.file_attributes & FILE_ATTR_REPARSE_POINT) 1049 + dt_type = ntfs_reparse_tag_dt_types(vol, mref); 665 1050 else 666 1051 dt_type = DT_REG; 667 - mref = MREF_LE(ie->data.dir.indexed_file); 1052 + 668 1053 /* 669 1054 * Drop the page lock otherwise we deadlock with NFS when it calls 670 1055 * ->lookup since ntfs_lookup() will lock the same page. 671 1056 */ 672 1057 if (ia_page) 673 1058 unlock_page(ia_page); 674 - ntfs_debug("Calling filldir for %s with len %i, fpos 0x%llx, inode " 675 - "0x%lx, DT_%s.", name, name_len, actor->pos, mref, 676 - dt_type == DT_DIR ? "DIR" : "REG"); 1059 + ntfs_debug("Calling filldir for %s with len %i, fpos 0x%llx, inode 0x%lx, DT_%s.", 1060 + name, name_len, actor->pos, mref, dt_type == DT_DIR ? "DIR" : "REG"); 677 1061 if (!dir_emit(actor, name, name_len, mref, dt_type)) 678 1062 return 1; 679 1063 /* Relock the page but not if we are aborting ->readdir. */ ··· 685 1063 return 0; 686 1064 } 687 1065 688 - /* 689 - * We use the same basic approach as the old NTFS driver, i.e. we parse the 690 - * index root entries and then the index allocation entries that are marked 691 - * as in use in the index bitmap. 692 - * 693 - * While this will return the names in random order this doesn't matter for 694 - * ->readdir but OTOH results in a faster ->readdir. 695 - * 696 - * VFS calls ->readdir without BKL but with i_mutex held. This protects the VFS 697 - * parts (e.g. ->f_pos and ->i_size, and it also protects against directory 698 - * modifications). 699 - * 700 - * Locking: - Caller must hold i_mutex on the directory. 701 - * - Each page cache page in the index allocation mapping must be 702 - * locked whilst being accessed otherwise we may find a corrupt 703 - * page due to it being under ->writepage at the moment which 704 - * applies the mst protection fixups before writing out and then 705 - * removes them again after the write is complete after which it 706 - * unlocks the page. 707 - */ 1066 + struct ntfs_file_private { 1067 + void *key; 1068 + __le16 key_length; 1069 + bool end_in_iterate; 1070 + loff_t curr_pos; 1071 + }; 1072 + 1073 + struct ntfs_index_ra { 1074 + unsigned long start_index; 1075 + unsigned int count; 1076 + struct rb_node rb_node; 1077 + }; 1078 + 1079 + static void ntfs_insert_rb(struct ntfs_index_ra *nir, struct rb_root *root) 1080 + { 1081 + struct rb_node **new = &root->rb_node, *parent = NULL; 1082 + struct ntfs_index_ra *cnir; 1083 + 1084 + while (*new) { 1085 + parent = *new; 1086 + cnir = rb_entry(parent, struct ntfs_index_ra, rb_node); 1087 + if (nir->start_index < cnir->start_index) 1088 + new = &parent->rb_left; 1089 + else if (nir->start_index >= cnir->start_index + cnir->count) 1090 + new = &parent->rb_right; 1091 + else { 1092 + pr_err("nir start index : %ld, count : %d, cnir start_index : %ld, count : %d\n", 1093 + nir->start_index, nir->count, cnir->start_index, cnir->count); 1094 + return; 1095 + } 1096 + } 1097 + 1098 + rb_link_node(&nir->rb_node, parent, new); 1099 + rb_insert_color(&nir->rb_node, root); 1100 + } 1101 + 1102 + static int ntfs_ia_blocks_readahead(struct ntfs_inode *ia_ni, loff_t pos) 1103 + { 1104 + unsigned long dir_start_index, dir_end_index; 1105 + struct inode *ia_vi = VFS_I(ia_ni); 1106 + struct file_ra_state *dir_ra; 1107 + 1108 + dir_end_index = (i_size_read(ia_vi) + PAGE_SIZE - 1) >> PAGE_SHIFT; 1109 + dir_start_index = (pos + PAGE_SIZE - 1) >> PAGE_SHIFT; 1110 + 1111 + if (dir_start_index >= dir_end_index) 1112 + return 0; 1113 + 1114 + dir_ra = kzalloc(sizeof(*dir_ra), GFP_NOFS); 1115 + if (!dir_ra) 1116 + return -ENOMEM; 1117 + 1118 + file_ra_state_init(dir_ra, ia_vi->i_mapping); 1119 + dir_end_index = (i_size_read(ia_vi) + PAGE_SIZE - 1) >> PAGE_SHIFT; 1120 + dir_start_index = (pos + PAGE_SIZE - 1) >> PAGE_SHIFT; 1121 + dir_ra->ra_pages = dir_end_index - dir_start_index; 1122 + page_cache_sync_readahead(ia_vi->i_mapping, dir_ra, NULL, 1123 + dir_start_index, dir_end_index - dir_start_index); 1124 + kfree(dir_ra); 1125 + 1126 + return 0; 1127 + } 1128 + 708 1129 static int ntfs_readdir(struct file *file, struct dir_context *actor) 709 1130 { 710 - s64 ia_pos, ia_start, prev_ia_pos, bmp_pos; 711 - loff_t i_size; 712 - struct inode *bmp_vi, *vdir = file_inode(file); 1131 + struct inode *vdir = file_inode(file); 713 1132 struct super_block *sb = vdir->i_sb; 714 - ntfs_inode *ndir = NTFS_I(vdir); 715 - ntfs_volume *vol = NTFS_SB(sb); 716 - MFT_RECORD *m; 717 - INDEX_ROOT *ir = NULL; 718 - INDEX_ENTRY *ie; 719 - INDEX_ALLOCATION *ia; 720 - u8 *name = NULL; 721 - int rc, err, ir_pos, cur_bmp_pos; 722 - struct address_space *ia_mapping, *bmp_mapping; 723 - struct page *bmp_page = NULL, *ia_page = NULL; 724 - u8 *kaddr, *bmp, *index_end; 725 - ntfs_attr_search_ctx *ctx; 1133 + struct ntfs_inode *ndir = NTFS_I(vdir); 1134 + struct ntfs_volume *vol = NTFS_SB(sb); 1135 + struct ntfs_attr_search_ctx *ctx = NULL; 1136 + struct ntfs_index_context *ictx = NULL; 1137 + u8 *name; 1138 + struct index_root *ir; 1139 + struct index_entry *next = NULL; 1140 + struct ntfs_file_private *private = NULL; 1141 + int err = 0; 1142 + loff_t ie_pos = 2; /* initialize it with dot and dotdot size */ 1143 + struct ntfs_index_ra *nir = NULL; 1144 + unsigned long index; 1145 + struct rb_root ra_root = RB_ROOT; 1146 + struct file_ra_state *ra; 726 1147 727 1148 ntfs_debug("Entering for inode 0x%lx, fpos 0x%llx.", 728 1149 vdir->i_ino, actor->pos); 729 - rc = err = 0; 730 - /* Are we at end of dir yet? */ 731 - i_size = i_size_read(vdir); 732 - if (actor->pos >= i_size + vol->mft_record_size) 733 - return 0; 1150 + 1151 + if (file->private_data) { 1152 + private = file->private_data; 1153 + 1154 + if (actor->pos != private->curr_pos) { 1155 + /* 1156 + * If actor->pos is different from the previous passed 1157 + * one, Discard the private->key and fill dirent buffer 1158 + * with linear lookup. 1159 + */ 1160 + kfree(private->key); 1161 + private->key = NULL; 1162 + private->end_in_iterate = false; 1163 + } else if (private->end_in_iterate) { 1164 + kfree(private->key); 1165 + kfree(file->private_data); 1166 + file->private_data = NULL; 1167 + return 0; 1168 + } 1169 + } 1170 + 734 1171 /* Emulate . and .. for all directories. */ 735 1172 if (!dir_emit_dots(file, actor)) 736 1173 return 0; 737 - m = NULL; 738 - ctx = NULL; 1174 + 739 1175 /* 740 1176 * Allocate a buffer to store the current name being processed 741 1177 * converted to format determined by current NLS. 742 1178 */ 743 1179 name = kmalloc(NTFS_MAX_NAME_LEN * NLS_MAX_CHARSET_SIZE + 1, GFP_NOFS); 744 - if (unlikely(!name)) { 1180 + if (unlikely(!name)) 1181 + return -ENOMEM; 1182 + 1183 + mutex_lock_nested(&ndir->mrec_lock, NTFS_INODE_MUTEX_PARENT); 1184 + ictx = ntfs_index_ctx_get(ndir, I30, 4); 1185 + if (!ictx) { 1186 + kfree(name); 1187 + mutex_unlock(&ndir->mrec_lock); 1188 + return -ENOMEM; 1189 + } 1190 + 1191 + ra = kzalloc(sizeof(struct file_ra_state), GFP_NOFS); 1192 + if (!ra) { 1193 + kfree(name); 1194 + ntfs_index_ctx_put(ictx); 1195 + mutex_unlock(&ndir->mrec_lock); 1196 + return -ENOMEM; 1197 + } 1198 + file_ra_state_init(ra, vol->mft_ino->i_mapping); 1199 + 1200 + if (private && private->key) { 1201 + /* 1202 + * Find index witk private->key using ntfs_index_lookup() 1203 + * instead of linear index lookup. 1204 + */ 1205 + err = ntfs_index_lookup(private->key, 1206 + le16_to_cpu(private->key_length), 1207 + ictx); 1208 + if (!err) { 1209 + next = ictx->entry; 1210 + /* 1211 + * Update ie_pos with private->curr_pos 1212 + * to make next d_off of dirent correct. 1213 + */ 1214 + ie_pos = private->curr_pos; 1215 + 1216 + if (actor->pos > vol->mft_record_size && ictx->ia_ni) { 1217 + err = ntfs_ia_blocks_readahead(ictx->ia_ni, actor->pos); 1218 + if (err) 1219 + goto out; 1220 + } 1221 + 1222 + goto nextdir; 1223 + } else { 1224 + goto out; 1225 + } 1226 + } else if (!private) { 1227 + private = kzalloc(sizeof(struct ntfs_file_private), GFP_KERNEL); 1228 + if (!private) { 1229 + err = -ENOMEM; 1230 + goto out; 1231 + } 1232 + file->private_data = private; 1233 + } 1234 + 1235 + ctx = ntfs_attr_get_search_ctx(ndir, NULL); 1236 + if (!ctx) { 745 1237 err = -ENOMEM; 746 - goto err_out; 1238 + goto out; 747 1239 } 748 - /* Are we jumping straight into the index allocation attribute? */ 749 - if (actor->pos >= vol->mft_record_size) 750 - goto skip_index_root; 751 - /* Get hold of the mft record for the directory. */ 752 - m = map_mft_record(ndir); 753 - if (IS_ERR(m)) { 754 - err = PTR_ERR(m); 755 - m = NULL; 756 - goto err_out; 757 - } 758 - ctx = ntfs_attr_get_search_ctx(ndir, m); 759 - if (unlikely(!ctx)) { 760 - err = -ENOMEM; 761 - goto err_out; 762 - } 763 - /* Get the offset into the index root attribute. */ 764 - ir_pos = (s64)actor->pos; 1240 + 765 1241 /* Find the index root attribute in the mft record. */ 766 - err = ntfs_attr_lookup(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 0, NULL, 767 - 0, ctx); 768 - if (unlikely(err)) { 769 - ntfs_error(sb, "Index root attribute missing in directory " 770 - "inode 0x%lx.", vdir->i_ino); 771 - goto err_out; 772 - } 773 - /* 774 - * Copy the index root attribute value to a buffer so that we can put 775 - * the search context and unmap the mft record before calling the 776 - * filldir() callback. We need to do this because of NFSd which calls 777 - * ->lookup() from its filldir callback() and this causes NTFS to 778 - * deadlock as ntfs_lookup() maps the mft record of the directory and 779 - * we have got it mapped here already. The only solution is for us to 780 - * unmap the mft record here so that a call to ntfs_lookup() is able to 781 - * map the mft record without deadlocking. 782 - */ 783 - rc = le32_to_cpu(ctx->attr->data.resident.value_length); 784 - ir = kmalloc(rc, GFP_NOFS); 785 - if (unlikely(!ir)) { 786 - err = -ENOMEM; 787 - goto err_out; 788 - } 789 - /* Copy the index root value (it has been verified in read_inode). */ 790 - memcpy(ir, (u8*)ctx->attr + 791 - le16_to_cpu(ctx->attr->data.resident.value_offset), rc); 792 - ntfs_attr_put_search_ctx(ctx); 793 - unmap_mft_record(ndir); 794 - ctx = NULL; 795 - m = NULL; 796 - index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length); 797 - /* The first index entry. */ 798 - ie = (INDEX_ENTRY*)((u8*)&ir->index + 799 - le32_to_cpu(ir->index.entries_offset)); 800 - /* 801 - * Loop until we exceed valid memory (corruption case) or until we 802 - * reach the last entry or until filldir tells us it has had enough 803 - * or signals an error (both covered by the rc test). 804 - */ 805 - for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) { 806 - ntfs_debug("In index root, offset 0x%zx.", (u8*)ie - (u8*)ir); 807 - /* Bounds checks. */ 808 - if (unlikely((u8*)ie < (u8*)ir || (u8*)ie + 809 - sizeof(INDEX_ENTRY_HEADER) > index_end || 810 - (u8*)ie + le16_to_cpu(ie->key_length) > 811 - index_end)) 812 - goto err_out; 813 - /* The last entry cannot contain a name. */ 814 - if (ie->flags & INDEX_ENTRY_END) 815 - break; 816 - /* Skip index root entry if continuing previous readdir. */ 817 - if (ir_pos > (u8*)ie - (u8*)ir) 818 - continue; 819 - /* Advance the position even if going to skip the entry. */ 820 - actor->pos = (u8*)ie - (u8*)ir; 821 - /* Submit the name to the filldir callback. */ 822 - rc = ntfs_filldir(vol, ndir, NULL, ie, name, actor); 823 - if (rc) { 824 - kfree(ir); 825 - goto abort; 826 - } 827 - } 828 - /* We are done with the index root and can free the buffer. */ 829 - kfree(ir); 830 - ir = NULL; 831 - /* If there is no index allocation attribute we are finished. */ 832 - if (!NInoIndexAllocPresent(ndir)) 833 - goto EOD; 834 - /* Advance fpos to the beginning of the index allocation. */ 835 - actor->pos = vol->mft_record_size; 836 - skip_index_root: 837 - kaddr = NULL; 838 - prev_ia_pos = -1LL; 839 - /* Get the offset into the index allocation attribute. */ 840 - ia_pos = (s64)actor->pos - vol->mft_record_size; 841 - ia_mapping = vdir->i_mapping; 842 - ntfs_debug("Inode 0x%lx, getting index bitmap.", vdir->i_ino); 843 - bmp_vi = ntfs_attr_iget(vdir, AT_BITMAP, I30, 4); 844 - if (IS_ERR(bmp_vi)) { 845 - ntfs_error(sb, "Failed to get bitmap attribute."); 846 - err = PTR_ERR(bmp_vi); 847 - goto err_out; 848 - } 849 - bmp_mapping = bmp_vi->i_mapping; 850 - /* Get the starting bitmap bit position and sanity check it. */ 851 - bmp_pos = ia_pos >> ndir->itype.index.block_size_bits; 852 - if (unlikely(bmp_pos >> 3 >= i_size_read(bmp_vi))) { 853 - ntfs_error(sb, "Current index allocation position exceeds " 854 - "index bitmap size."); 855 - goto iput_err_out; 856 - } 857 - /* Get the starting bit position in the current bitmap page. */ 858 - cur_bmp_pos = bmp_pos & ((PAGE_SIZE * 8) - 1); 859 - bmp_pos &= ~(u64)((PAGE_SIZE * 8) - 1); 860 - get_next_bmp_page: 861 - ntfs_debug("Reading bitmap with page index 0x%llx, bit ofs 0x%llx", 862 - (unsigned long long)bmp_pos >> (3 + PAGE_SHIFT), 863 - (unsigned long long)bmp_pos & 864 - (unsigned long long)((PAGE_SIZE * 8) - 1)); 865 - bmp_page = ntfs_map_page(bmp_mapping, 866 - bmp_pos >> (3 + PAGE_SHIFT)); 867 - if (IS_ERR(bmp_page)) { 868 - ntfs_error(sb, "Reading index bitmap failed."); 869 - err = PTR_ERR(bmp_page); 870 - bmp_page = NULL; 871 - goto iput_err_out; 872 - } 873 - bmp = (u8*)page_address(bmp_page); 874 - /* Find next index block in use. */ 875 - while (!(bmp[cur_bmp_pos >> 3] & (1 << (cur_bmp_pos & 7)))) { 876 - find_next_index_buffer: 877 - cur_bmp_pos++; 878 - /* 879 - * If we have reached the end of the bitmap page, get the next 880 - * page, and put away the old one. 881 - */ 882 - if (unlikely((cur_bmp_pos >> 3) >= PAGE_SIZE)) { 883 - ntfs_unmap_page(bmp_page); 884 - bmp_pos += PAGE_SIZE * 8; 885 - cur_bmp_pos = 0; 886 - goto get_next_bmp_page; 887 - } 888 - /* If we have reached the end of the bitmap, we are done. */ 889 - if (unlikely(((bmp_pos + cur_bmp_pos) >> 3) >= i_size)) 890 - goto unm_EOD; 891 - ia_pos = (bmp_pos + cur_bmp_pos) << 892 - ndir->itype.index.block_size_bits; 893 - } 894 - ntfs_debug("Handling index buffer 0x%llx.", 895 - (unsigned long long)bmp_pos + cur_bmp_pos); 896 - /* If the current index buffer is in the same page we reuse the page. */ 897 - if ((prev_ia_pos & (s64)PAGE_MASK) != 898 - (ia_pos & (s64)PAGE_MASK)) { 899 - prev_ia_pos = ia_pos; 900 - if (likely(ia_page != NULL)) { 901 - unlock_page(ia_page); 902 - ntfs_unmap_page(ia_page); 903 - } 904 - /* 905 - * Map the page cache page containing the current ia_pos, 906 - * reading it from disk if necessary. 907 - */ 908 - ia_page = ntfs_map_page(ia_mapping, ia_pos >> PAGE_SHIFT); 909 - if (IS_ERR(ia_page)) { 910 - ntfs_error(sb, "Reading index allocation data failed."); 911 - err = PTR_ERR(ia_page); 912 - ia_page = NULL; 913 - goto err_out; 914 - } 915 - lock_page(ia_page); 916 - kaddr = (u8*)page_address(ia_page); 917 - } 918 - /* Get the current index buffer. */ 919 - ia = (INDEX_ALLOCATION*)(kaddr + (ia_pos & ~PAGE_MASK & 920 - ~(s64)(ndir->itype.index.block_size - 1))); 921 - /* Bounds checks. */ 922 - if (unlikely((u8*)ia < kaddr || (u8*)ia > kaddr + PAGE_SIZE)) { 923 - ntfs_error(sb, "Out of bounds check failed. Corrupt directory " 924 - "inode 0x%lx or driver bug.", vdir->i_ino); 925 - goto err_out; 926 - } 927 - /* Catch multi sector transfer fixup errors. */ 928 - if (unlikely(!ntfs_is_indx_record(ia->magic))) { 929 - ntfs_error(sb, "Directory index record with vcn 0x%llx is " 930 - "corrupt. Corrupt inode 0x%lx. Run chkdsk.", 931 - (unsigned long long)ia_pos >> 932 - ndir->itype.index.vcn_size_bits, vdir->i_ino); 933 - goto err_out; 934 - } 935 - if (unlikely(sle64_to_cpu(ia->index_block_vcn) != (ia_pos & 936 - ~(s64)(ndir->itype.index.block_size - 1)) >> 937 - ndir->itype.index.vcn_size_bits)) { 938 - ntfs_error(sb, "Actual VCN (0x%llx) of index buffer is " 939 - "different from expected VCN (0x%llx). " 940 - "Directory inode 0x%lx is corrupt or driver " 941 - "bug. ", (unsigned long long) 942 - sle64_to_cpu(ia->index_block_vcn), 943 - (unsigned long long)ia_pos >> 944 - ndir->itype.index.vcn_size_bits, vdir->i_ino); 945 - goto err_out; 946 - } 947 - if (unlikely(le32_to_cpu(ia->index.allocated_size) + 0x18 != 948 - ndir->itype.index.block_size)) { 949 - ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode " 950 - "0x%lx has a size (%u) differing from the " 951 - "directory specified size (%u). Directory " 952 - "inode is corrupt or driver bug.", 953 - (unsigned long long)ia_pos >> 954 - ndir->itype.index.vcn_size_bits, vdir->i_ino, 955 - le32_to_cpu(ia->index.allocated_size) + 0x18, 956 - ndir->itype.index.block_size); 957 - goto err_out; 958 - } 959 - index_end = (u8*)ia + ndir->itype.index.block_size; 960 - if (unlikely(index_end > kaddr + PAGE_SIZE)) { 961 - ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode " 962 - "0x%lx crosses page boundary. Impossible! " 963 - "Cannot access! This is probably a bug in the " 964 - "driver.", (unsigned long long)ia_pos >> 965 - ndir->itype.index.vcn_size_bits, vdir->i_ino); 966 - goto err_out; 967 - } 968 - ia_start = ia_pos & ~(s64)(ndir->itype.index.block_size - 1); 969 - index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length); 970 - if (unlikely(index_end > (u8*)ia + ndir->itype.index.block_size)) { 971 - ntfs_error(sb, "Size of index buffer (VCN 0x%llx) of directory " 972 - "inode 0x%lx exceeds maximum size.", 973 - (unsigned long long)ia_pos >> 974 - ndir->itype.index.vcn_size_bits, vdir->i_ino); 975 - goto err_out; 976 - } 977 - /* The first index entry in this index buffer. */ 978 - ie = (INDEX_ENTRY*)((u8*)&ia->index + 979 - le32_to_cpu(ia->index.entries_offset)); 980 - /* 981 - * Loop until we exceed valid memory (corruption case) or until we 982 - * reach the last entry or until filldir tells us it has had enough 983 - * or signals an error (both covered by the rc test). 984 - */ 985 - for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) { 986 - ntfs_debug("In index allocation, offset 0x%llx.", 987 - (unsigned long long)ia_start + 988 - (unsigned long long)((u8*)ie - (u8*)ia)); 989 - /* Bounds checks. */ 990 - if (unlikely((u8*)ie < (u8*)ia || (u8*)ie + 991 - sizeof(INDEX_ENTRY_HEADER) > index_end || 992 - (u8*)ie + le16_to_cpu(ie->key_length) > 993 - index_end)) 994 - goto err_out; 995 - /* The last entry cannot contain a name. */ 996 - if (ie->flags & INDEX_ENTRY_END) 997 - break; 998 - /* Skip index block entry if continuing previous readdir. */ 999 - if (ia_pos - ia_start > (u8*)ie - (u8*)ia) 1000 - continue; 1001 - /* Advance the position even if going to skip the entry. */ 1002 - actor->pos = (u8*)ie - (u8*)ia + 1003 - (sle64_to_cpu(ia->index_block_vcn) << 1004 - ndir->itype.index.vcn_size_bits) + 1005 - vol->mft_record_size; 1006 - /* 1007 - * Submit the name to the @filldir callback. Note, 1008 - * ntfs_filldir() drops the lock on @ia_page but it retakes it 1009 - * before returning, unless a non-zero value is returned in 1010 - * which case the page is left unlocked. 1011 - */ 1012 - rc = ntfs_filldir(vol, ndir, ia_page, ie, name, actor); 1013 - if (rc) { 1014 - /* @ia_page is already unlocked in this case. */ 1015 - ntfs_unmap_page(ia_page); 1016 - ntfs_unmap_page(bmp_page); 1017 - iput(bmp_vi); 1018 - goto abort; 1019 - } 1020 - } 1021 - goto find_next_index_buffer; 1022 - unm_EOD: 1023 - if (ia_page) { 1024 - unlock_page(ia_page); 1025 - ntfs_unmap_page(ia_page); 1026 - } 1027 - ntfs_unmap_page(bmp_page); 1028 - iput(bmp_vi); 1029 - EOD: 1030 - /* We are finished, set fpos to EOD. */ 1031 - actor->pos = i_size + vol->mft_record_size; 1032 - abort: 1033 - kfree(name); 1034 - return 0; 1035 - err_out: 1036 - if (bmp_page) { 1037 - ntfs_unmap_page(bmp_page); 1038 - iput_err_out: 1039 - iput(bmp_vi); 1040 - } 1041 - if (ia_page) { 1042 - unlock_page(ia_page); 1043 - ntfs_unmap_page(ia_page); 1044 - } 1045 - kfree(ir); 1046 - kfree(name); 1047 - if (ctx) 1242 + if (ntfs_attr_lookup(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 0, NULL, 0, 1243 + ctx)) { 1244 + ntfs_error(sb, "Index root attribute missing in directory inode %ld", 1245 + ndir->mft_no); 1048 1246 ntfs_attr_put_search_ctx(ctx); 1049 - if (m) 1050 - unmap_mft_record(ndir); 1051 - if (!err) 1247 + err = -ENOMEM; 1248 + goto out; 1249 + } 1250 + 1251 + /* Get to the index root value. */ 1252 + ir = (struct index_root *)((u8 *)ctx->attr + 1253 + le16_to_cpu(ctx->attr->data.resident.value_offset)); 1254 + 1255 + ictx->ir = ir; 1256 + ictx->actx = ctx; 1257 + ictx->parent_vcn[ictx->pindex] = VCN_INDEX_ROOT_PARENT; 1258 + ictx->is_in_root = true; 1259 + ictx->parent_pos[ictx->pindex] = 0; 1260 + 1261 + ictx->block_size = le32_to_cpu(ir->index_block_size); 1262 + if (ictx->block_size < NTFS_BLOCK_SIZE) { 1263 + ntfs_error(sb, "Index block size (%d) is smaller than the sector size (%d)", 1264 + ictx->block_size, NTFS_BLOCK_SIZE); 1052 1265 err = -EIO; 1053 - ntfs_debug("Failed. Returning error code %i.", -err); 1266 + goto out; 1267 + } 1268 + 1269 + if (vol->cluster_size <= ictx->block_size) 1270 + ictx->vcn_size_bits = vol->cluster_size_bits; 1271 + else 1272 + ictx->vcn_size_bits = NTFS_BLOCK_SIZE_BITS; 1273 + 1274 + /* The first index entry. */ 1275 + next = (struct index_entry *)((u8 *)&ir->index + 1276 + le32_to_cpu(ir->index.entries_offset)); 1277 + 1278 + if (next->flags & INDEX_ENTRY_NODE) { 1279 + ictx->ia_ni = ntfs_ia_open(ictx, ictx->idx_ni); 1280 + if (!ictx->ia_ni) { 1281 + err = -EINVAL; 1282 + goto out; 1283 + } 1284 + 1285 + err = ntfs_ia_blocks_readahead(ictx->ia_ni, actor->pos); 1286 + if (err) 1287 + goto out; 1288 + } 1289 + 1290 + if (next->flags & INDEX_ENTRY_NODE) { 1291 + next = ntfs_index_walk_down(next, ictx); 1292 + if (!next) { 1293 + err = -EIO; 1294 + goto out; 1295 + } 1296 + } 1297 + 1298 + if (next && !(next->flags & INDEX_ENTRY_END)) 1299 + goto nextdir; 1300 + 1301 + while ((next = ntfs_index_next(next, ictx)) != NULL) { 1302 + nextdir: 1303 + /* Check the consistency of an index entry */ 1304 + if (ntfs_index_entry_inconsistent(ictx, vol, next, COLLATION_FILE_NAME, 1305 + ndir->mft_no)) { 1306 + err = -EIO; 1307 + goto out; 1308 + } 1309 + 1310 + if (ie_pos < actor->pos) { 1311 + ie_pos += le16_to_cpu(next->length); 1312 + continue; 1313 + } 1314 + 1315 + actor->pos = ie_pos; 1316 + 1317 + index = ntfs_mft_no_to_pidx(vol, 1318 + MREF_LE(next->data.dir.indexed_file)); 1319 + if (nir) { 1320 + struct ntfs_index_ra *cnir; 1321 + struct rb_node *node = ra_root.rb_node; 1322 + 1323 + if (nir->start_index <= index && 1324 + index < nir->start_index + nir->count) { 1325 + /* No behavior */ 1326 + goto filldir; 1327 + } 1328 + 1329 + while (node) { 1330 + cnir = rb_entry(node, struct ntfs_index_ra, rb_node); 1331 + if (cnir->start_index <= index && 1332 + index < cnir->start_index + cnir->count) { 1333 + goto filldir; 1334 + } else if (cnir->start_index + cnir->count == index) { 1335 + cnir->count++; 1336 + goto filldir; 1337 + } else if (!cnir->start_index && cnir->start_index - 1 == index) { 1338 + cnir->start_index = index; 1339 + goto filldir; 1340 + } 1341 + 1342 + if (index < cnir->start_index) 1343 + node = node->rb_left; 1344 + else if (index >= cnir->start_index + cnir->count) 1345 + node = node->rb_right; 1346 + } 1347 + 1348 + if (nir->start_index + nir->count == index) { 1349 + nir->count++; 1350 + } else if (!nir->start_index && nir->start_index - 1 == index) { 1351 + nir->start_index = index; 1352 + } else if (nir->count > 2) { 1353 + ntfs_insert_rb(nir, &ra_root); 1354 + nir = NULL; 1355 + } else { 1356 + nir->start_index = index; 1357 + nir->count = 1; 1358 + } 1359 + } 1360 + 1361 + if (!nir) { 1362 + nir = kzalloc(sizeof(struct ntfs_index_ra), GFP_KERNEL); 1363 + if (nir) { 1364 + nir->start_index = index; 1365 + nir->count = 1; 1366 + } 1367 + } 1368 + 1369 + filldir: 1370 + /* Submit the name to the filldir callback. */ 1371 + err = ntfs_filldir(vol, ndir, NULL, next, name, actor); 1372 + if (err) { 1373 + /* 1374 + * Store index key value to file private_data to start 1375 + * from current index offset on next round. 1376 + */ 1377 + private = file->private_data; 1378 + kfree(private->key); 1379 + private->key = kmalloc(le16_to_cpu(next->key_length), GFP_KERNEL); 1380 + if (!private->key) { 1381 + err = -ENOMEM; 1382 + goto out; 1383 + } 1384 + 1385 + memcpy(private->key, &next->key.file_name, le16_to_cpu(next->key_length)); 1386 + private->key_length = next->key_length; 1387 + break; 1388 + } 1389 + ie_pos += le16_to_cpu(next->length); 1390 + } 1391 + 1392 + if (!err) 1393 + private->end_in_iterate = true; 1394 + else 1395 + err = 0; 1396 + 1397 + private->curr_pos = actor->pos = ie_pos; 1398 + out: 1399 + while (!RB_EMPTY_ROOT(&ra_root)) { 1400 + struct ntfs_index_ra *cnir; 1401 + struct rb_node *node; 1402 + 1403 + node = rb_first(&ra_root); 1404 + cnir = rb_entry(node, struct ntfs_index_ra, rb_node); 1405 + ra->ra_pages = cnir->count; 1406 + page_cache_sync_readahead(vol->mft_ino->i_mapping, ra, NULL, 1407 + cnir->start_index, cnir->count); 1408 + rb_erase(node, &ra_root); 1409 + kfree(cnir); 1410 + } 1411 + 1412 + if (err) { 1413 + private->curr_pos = actor->pos; 1414 + private->end_in_iterate = true; 1415 + err = 0; 1416 + } 1417 + ntfs_index_ctx_put(ictx); 1418 + kfree(name); 1419 + kfree(nir); 1420 + kfree(ra); 1421 + mutex_unlock(&ndir->mrec_lock); 1054 1422 return err; 1055 1423 } 1056 1424 1057 - /** 1425 + int ntfs_check_empty_dir(struct ntfs_inode *ni, struct mft_record *ni_mrec) 1426 + { 1427 + struct ntfs_attr_search_ctx *ctx; 1428 + int ret = 0; 1429 + 1430 + if (!(ni_mrec->flags & MFT_RECORD_IS_DIRECTORY)) 1431 + return 0; 1432 + 1433 + ctx = ntfs_attr_get_search_ctx(ni, NULL); 1434 + if (!ctx) { 1435 + ntfs_error(ni->vol->sb, "Failed to get search context"); 1436 + return -ENOMEM; 1437 + } 1438 + 1439 + /* Find the index root attribute in the mft record. */ 1440 + ret = ntfs_attr_lookup(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 0, NULL, 1441 + 0, ctx); 1442 + if (ret) { 1443 + ntfs_error(ni->vol->sb, "Index root attribute missing in directory inode %lld", 1444 + (unsigned long long)ni->mft_no); 1445 + ntfs_attr_put_search_ctx(ctx); 1446 + return ret; 1447 + } 1448 + 1449 + /* Non-empty directory? */ 1450 + if (le32_to_cpu(ctx->attr->data.resident.value_length) != 1451 + sizeof(struct index_root) + sizeof(struct index_entry_header)) { 1452 + /* Both ENOTEMPTY and EEXIST are ok. We use the more common. */ 1453 + ret = -ENOTEMPTY; 1454 + ntfs_debug("Directory is not empty\n"); 1455 + } 1456 + 1457 + ntfs_attr_put_search_ctx(ctx); 1458 + 1459 + return ret; 1460 + } 1461 + 1462 + /* 1058 1463 * ntfs_dir_open - called when an inode is about to be opened 1059 1464 * @vi: inode to be opened 1060 1465 * @filp: file structure describing the inode ··· 1106 1457 return 0; 1107 1458 } 1108 1459 1109 - #ifdef NTFS_RW 1460 + static int ntfs_dir_release(struct inode *vi, struct file *filp) 1461 + { 1462 + if (filp->private_data) { 1463 + kfree(((struct ntfs_file_private *)filp->private_data)->key); 1464 + kfree(filp->private_data); 1465 + filp->private_data = NULL; 1466 + } 1467 + return 0; 1468 + } 1110 1469 1111 - /** 1470 + /* 1112 1471 * ntfs_dir_fsync - sync a directory to disk 1113 - * @filp: directory to be synced 1114 - * @start: offset in bytes of the beginning of data range to sync 1115 - * @end: offset in bytes of the end of data range (inclusive) 1472 + * @filp: file describing the directory to be synced 1473 + * @start: start offset to be synced 1474 + * @end: end offset to be synced 1116 1475 * @datasync: if non-zero only flush user data and not metadata 1117 1476 * 1118 1477 * Data integrity sync of a directory to disk. Used for fsync, fdatasync, and ··· 1136 1479 * anyway. 1137 1480 * 1138 1481 * Locking: Caller must hold i_mutex on the inode. 1139 - * 1140 - * TODO: We should probably also write all attribute/index inodes associated 1141 - * with this inode but since we have no simple way of getting to them we ignore 1142 - * this problem for now. We do write the $BITMAP attribute if it is present 1143 - * which is the important one for a directory so things are not too bad. 1144 1482 */ 1145 1483 static int ntfs_dir_fsync(struct file *filp, loff_t start, loff_t end, 1146 1484 int datasync) 1147 1485 { 1148 1486 struct inode *bmp_vi, *vi = filp->f_mapping->host; 1487 + struct ntfs_volume *vol = NTFS_I(vi)->vol; 1488 + struct ntfs_inode *ni = NTFS_I(vi); 1489 + struct ntfs_attr_search_ctx *ctx; 1490 + struct inode *parent_vi, *ia_vi; 1149 1491 int err, ret; 1150 - ntfs_attr na; 1492 + struct ntfs_attr na; 1151 1493 1152 1494 ntfs_debug("Entering for inode 0x%lx.", vi->i_ino); 1495 + 1496 + if (NVolShutdown(vol)) 1497 + return -EIO; 1498 + 1499 + ctx = ntfs_attr_get_search_ctx(ni, NULL); 1500 + if (!ctx) 1501 + return -ENOMEM; 1502 + 1503 + mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL_CHILD); 1504 + while (!(err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, 0, 0, NULL, 0, ctx))) { 1505 + struct file_name_attr *fn = (struct file_name_attr *)((u8 *)ctx->attr + 1506 + le16_to_cpu(ctx->attr->data.resident.value_offset)); 1507 + 1508 + if (MREF_LE(fn->parent_directory) == ni->mft_no) 1509 + continue; 1510 + 1511 + parent_vi = ntfs_iget(vi->i_sb, MREF_LE(fn->parent_directory)); 1512 + if (IS_ERR(parent_vi)) 1513 + continue; 1514 + mutex_lock_nested(&NTFS_I(parent_vi)->mrec_lock, NTFS_INODE_MUTEX_NORMAL); 1515 + ia_vi = ntfs_index_iget(parent_vi, I30, 4); 1516 + mutex_unlock(&NTFS_I(parent_vi)->mrec_lock); 1517 + if (IS_ERR(ia_vi)) { 1518 + iput(parent_vi); 1519 + continue; 1520 + } 1521 + write_inode_now(ia_vi, 1); 1522 + iput(ia_vi); 1523 + write_inode_now(parent_vi, 1); 1524 + iput(parent_vi); 1525 + } 1526 + mutex_unlock(&ni->mrec_lock); 1527 + ntfs_attr_put_search_ctx(ctx); 1153 1528 1154 1529 err = file_write_and_wait_range(filp, start, end); 1155 1530 if (err) 1156 1531 return err; 1157 1532 inode_lock(vi); 1158 1533 1159 - BUG_ON(!S_ISDIR(vi->i_mode)); 1160 1534 /* If the bitmap attribute inode is in memory sync it, too. */ 1161 1535 na.mft_no = vi->i_ino; 1162 1536 na.type = AT_BITMAP; ··· 1195 1507 na.name_len = 4; 1196 1508 bmp_vi = ilookup5(vi->i_sb, vi->i_ino, ntfs_test_inode, &na); 1197 1509 if (bmp_vi) { 1198 - write_inode_now(bmp_vi, !datasync); 1510 + write_inode_now(bmp_vi, !datasync); 1199 1511 iput(bmp_vi); 1200 1512 } 1201 1513 ret = __ntfs_write_inode(vi, 1); 1514 + 1202 1515 write_inode_now(vi, !datasync); 1516 + 1517 + write_inode_now(vol->mftbmp_ino, 1); 1518 + down_write(&vol->lcnbmp_lock); 1519 + write_inode_now(vol->lcnbmp_ino, 1); 1520 + up_write(&vol->lcnbmp_lock); 1521 + write_inode_now(vol->mft_ino, 1); 1522 + 1203 1523 err = sync_blockdev(vi->i_sb->s_bdev); 1204 1524 if (unlikely(err && !ret)) 1205 1525 ret = err; 1206 1526 if (likely(!ret)) 1207 1527 ntfs_debug("Done."); 1208 1528 else 1209 - ntfs_warning(vi->i_sb, "Failed to f%ssync inode 0x%lx. Error " 1210 - "%u.", datasync ? "data" : "", vi->i_ino, -ret); 1529 + ntfs_warning(vi->i_sb, 1530 + "Failed to f%ssync inode 0x%lx. Error %u.", 1531 + datasync ? "data" : "", vi->i_ino, -ret); 1211 1532 inode_unlock(vi); 1212 1533 return ret; 1213 1534 } 1214 1535 1215 - #endif /* NTFS_RW */ 1216 - 1217 - WRAP_DIR_ITER(ntfs_readdir) // FIXME! 1218 1536 const struct file_operations ntfs_dir_ops = { 1219 1537 .llseek = generic_file_llseek, /* Seek inside directory. */ 1220 1538 .read = generic_read_dir, /* Return -EISDIR. */ 1221 - .iterate_shared = shared_ntfs_readdir, /* Read directory contents. */ 1222 - #ifdef NTFS_RW 1539 + .iterate_shared = ntfs_readdir, /* Read directory contents. */ 1223 1540 .fsync = ntfs_dir_fsync, /* Sync a directory to disk. */ 1224 - #endif /* NTFS_RW */ 1225 - /*.ioctl = ,*/ /* Perform function on the 1226 - mounted filesystem. */ 1227 1541 .open = ntfs_dir_open, /* Open directory. */ 1542 + .release = ntfs_dir_release, 1543 + .unlocked_ioctl = ntfs_ioctl, 1544 + #ifdef CONFIG_COMPAT 1545 + .compat_ioctl = ntfs_compat_ioctl, 1546 + #endif 1547 + .setlease = generic_setlease, 1228 1548 };
+2045 -368
fs/ntfs/index.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0-or-later 2 2 /* 3 - * index.c - NTFS kernel index handling. Part of the Linux-NTFS project. 3 + * NTFS kernel index handling. 4 4 * 5 5 * Copyright (c) 2004-2005 Anton Altaparmakov 6 + * Copyright (c) 2025 LG Electronics Co., Ltd. 7 + * 8 + * Part of this file is based on code from the NTFS-3G. 9 + * and is copyrighted by the respective authors below: 10 + * Copyright (c) 2004-2005 Anton Altaparmakov 11 + * Copyright (c) 2004-2005 Richard Russon 12 + * Copyright (c) 2005-2006 Yura Pakhuchiy 13 + * Copyright (c) 2005-2008 Szabolcs Szakacsits 14 + * Copyright (c) 2007-2021 Jean-Pierre Andre 6 15 */ 7 16 8 - #include <linux/slab.h> 9 - 10 - #include "aops.h" 11 17 #include "collate.h" 12 - #include "debug.h" 13 18 #include "index.h" 14 19 #include "ntfs.h" 20 + #include "attrlist.h" 15 21 16 - /** 17 - * ntfs_index_ctx_get - allocate and initialize a new index context 18 - * @idx_ni: ntfs index inode with which to initialize the context 22 + /* 23 + * ntfs_index_entry_inconsistent - Check the consistency of an index entry 19 24 * 20 - * Allocate a new index context, initialize it with @idx_ni and return it. 21 - * Return NULL if allocation failed. 22 - * 23 - * Locking: Caller must hold i_mutex on the index inode. 25 + * Make sure data and key do not overflow from entry. 26 + * As a side effect, an entry with zero length is rejected. 27 + * This entry must be a full one (no INDEX_ENTRY_END flag), and its 28 + * length must have been checked beforehand to not overflow from the 29 + * index record. 24 30 */ 25 - ntfs_index_context *ntfs_index_ctx_get(ntfs_inode *idx_ni) 31 + int ntfs_index_entry_inconsistent(struct ntfs_index_context *icx, 32 + struct ntfs_volume *vol, const struct index_entry *ie, 33 + __le32 collation_rule, u64 inum) 26 34 { 27 - ntfs_index_context *ictx; 35 + if (icx) { 36 + struct index_header *ih; 37 + u8 *ie_start, *ie_end; 28 38 29 - ictx = kmem_cache_alloc(ntfs_index_ctx_cache, GFP_NOFS); 30 - if (ictx) 31 - *ictx = (ntfs_index_context){ .idx_ni = idx_ni }; 32 - return ictx; 33 - } 39 + if (icx->is_in_root) 40 + ih = &icx->ir->index; 41 + else 42 + ih = &icx->ib->index; 34 43 35 - /** 36 - * ntfs_index_ctx_put - release an index context 37 - * @ictx: index context to free 38 - * 39 - * Release the index context @ictx, releasing all associated resources. 40 - * 41 - * Locking: Caller must hold i_mutex on the index inode. 42 - */ 43 - void ntfs_index_ctx_put(ntfs_index_context *ictx) 44 - { 45 - if (ictx->entry) { 46 - if (ictx->is_in_root) { 47 - if (ictx->actx) 48 - ntfs_attr_put_search_ctx(ictx->actx); 49 - if (ictx->base_ni) 50 - unmap_mft_record(ictx->base_ni); 44 + if ((le32_to_cpu(ih->index_length) > le32_to_cpu(ih->allocated_size)) || 45 + (le32_to_cpu(ih->index_length) > icx->block_size)) { 46 + ntfs_error(vol->sb, "%s Index entry(0x%p)'s length is too big.", 47 + icx->is_in_root ? "Index root" : "Index block", 48 + (u8 *)icx->entry); 49 + return -EINVAL; 50 + } 51 + 52 + ie_start = (u8 *)ih + le32_to_cpu(ih->entries_offset); 53 + ie_end = (u8 *)ih + le32_to_cpu(ih->index_length); 54 + 55 + if (ie_start > (u8 *)ie || 56 + ie_end <= (u8 *)ie + le16_to_cpu(ie->length) || 57 + le16_to_cpu(ie->length) > le32_to_cpu(ih->allocated_size) || 58 + le16_to_cpu(ie->length) > icx->block_size) { 59 + ntfs_error(vol->sb, "Index entry(0x%p) is out of range from %s", 60 + (u8 *)icx->entry, 61 + icx->is_in_root ? "index root" : "index block"); 62 + return -EIO; 63 + } 64 + } 65 + 66 + if (ie->key_length && 67 + ((le16_to_cpu(ie->key_length) + offsetof(struct index_entry, key)) > 68 + le16_to_cpu(ie->length))) { 69 + ntfs_error(vol->sb, "Overflow from index entry in inode %lld\n", 70 + (long long)inum); 71 + return -EIO; 72 + 73 + } else { 74 + if (collation_rule == COLLATION_FILE_NAME) { 75 + if ((offsetof(struct index_entry, key.file_name.file_name) + 76 + ie->key.file_name.file_name_length * sizeof(__le16)) > 77 + le16_to_cpu(ie->length)) { 78 + ntfs_error(vol->sb, 79 + "File name overflow from index entry in inode %lld\n", 80 + (long long)inum); 81 + return -EIO; 82 + } 51 83 } else { 52 - struct page *page = ictx->page; 53 - if (page) { 54 - BUG_ON(!PageLocked(page)); 55 - unlock_page(page); 56 - ntfs_unmap_page(page); 84 + if (ie->data.vi.data_length && 85 + ((le16_to_cpu(ie->data.vi.data_offset) + 86 + le16_to_cpu(ie->data.vi.data_length)) > 87 + le16_to_cpu(ie->length))) { 88 + ntfs_error(vol->sb, 89 + "Data overflow from index entry in inode %lld\n", 90 + (long long)inum); 91 + return -EIO; 57 92 } 58 93 } 59 94 } 60 - kmem_cache_free(ntfs_index_ctx_cache, ictx); 61 - return; 95 + 96 + return 0; 62 97 } 63 98 64 - /** 65 - * ntfs_index_lookup - find a key in an index and return its index entry 66 - * @key: [IN] key for which to search in the index 67 - * @key_len: [IN] length of @key in bytes 68 - * @ictx: [IN/OUT] context describing the index and the returned entry 99 + /* 100 + * ntfs_index_entry_mark_dirty - mark an index entry dirty 101 + * @ictx: ntfs index context describing the index entry 69 102 * 70 - * Before calling ntfs_index_lookup(), @ictx must have been obtained from a 71 - * call to ntfs_index_ctx_get(). 103 + * Mark the index entry described by the index entry context @ictx dirty. 72 104 * 73 - * Look for the @key in the index specified by the index lookup context @ictx. 74 - * ntfs_index_lookup() walks the contents of the index looking for the @key. 105 + * If the index entry is in the index root attribute, simply mark the inode 106 + * containing the index root attribute dirty. This ensures the mftrecord, and 107 + * hence the index root attribute, will be written out to disk later. 75 108 * 76 - * If the @key is found in the index, 0 is returned and @ictx is setup to 77 - * describe the index entry containing the matching @key. @ictx->entry is the 78 - * index entry and @ictx->data and @ictx->data_len are the index entry data and 79 - * its length in bytes, respectively. 80 - * 81 - * If the @key is not found in the index, -ENOENT is returned and @ictx is 82 - * setup to describe the index entry whose key collates immediately after the 83 - * search @key, i.e. this is the position in the index at which an index entry 84 - * with a key of @key would need to be inserted. 85 - * 86 - * If an error occurs return the negative error code and @ictx is left 87 - * untouched. 88 - * 89 - * When finished with the entry and its data, call ntfs_index_ctx_put() to free 90 - * the context and other associated resources. 91 - * 92 - * If the index entry was modified, call flush_dcache_index_entry_page() 93 - * immediately after the modification and either ntfs_index_entry_mark_dirty() 94 - * or ntfs_index_entry_write() before the call to ntfs_index_ctx_put() to 95 - * ensure that the changes are written to disk. 96 - * 97 - * Locking: - Caller must hold i_mutex on the index inode. 98 - * - Each page cache page in the index allocation mapping must be 99 - * locked whilst being accessed otherwise we may find a corrupt 100 - * page due to it being under ->writepage at the moment which 101 - * applies the mst protection fixups before writing out and then 102 - * removes them again after the write is complete after which it 103 - * unlocks the page. 109 + * If the index entry is in an index block belonging to the index allocation 110 + * attribute, set ib_dirty to true, thus index block will be updated during 111 + * ntfs_index_ctx_put. 104 112 */ 105 - int ntfs_index_lookup(const void *key, const int key_len, 106 - ntfs_index_context *ictx) 113 + void ntfs_index_entry_mark_dirty(struct ntfs_index_context *ictx) 107 114 { 108 - VCN vcn, old_vcn; 109 - ntfs_inode *idx_ni = ictx->idx_ni; 110 - ntfs_volume *vol = idx_ni->vol; 111 - struct super_block *sb = vol->sb; 112 - ntfs_inode *base_ni = idx_ni->ext.base_ntfs_ino; 113 - MFT_RECORD *m; 114 - INDEX_ROOT *ir; 115 - INDEX_ENTRY *ie; 116 - INDEX_ALLOCATION *ia; 117 - u8 *index_end, *kaddr; 118 - ntfs_attr_search_ctx *actx; 119 - struct address_space *ia_mapping; 120 - struct page *page; 121 - int rc, err = 0; 115 + if (ictx->is_in_root) 116 + mark_mft_record_dirty(ictx->actx->ntfs_ino); 117 + else if (ictx->ib) 118 + ictx->ib_dirty = true; 119 + } 122 120 123 - ntfs_debug("Entering."); 124 - BUG_ON(!NInoAttr(idx_ni)); 125 - BUG_ON(idx_ni->type != AT_INDEX_ALLOCATION); 126 - BUG_ON(idx_ni->nr_extents != -1); 127 - BUG_ON(!base_ni); 128 - BUG_ON(!key); 129 - BUG_ON(key_len <= 0); 130 - if (!ntfs_is_collation_rule_supported( 131 - idx_ni->itype.index.collation_rule)) { 132 - ntfs_error(sb, "Index uses unsupported collation rule 0x%x. " 133 - "Aborting lookup.", le32_to_cpu( 134 - idx_ni->itype.index.collation_rule)); 135 - return -EOPNOTSUPP; 121 + static s64 ntfs_ib_vcn_to_pos(struct ntfs_index_context *icx, s64 vcn) 122 + { 123 + return vcn << icx->vcn_size_bits; 124 + } 125 + 126 + static s64 ntfs_ib_pos_to_vcn(struct ntfs_index_context *icx, s64 pos) 127 + { 128 + return pos >> icx->vcn_size_bits; 129 + } 130 + 131 + static int ntfs_ib_write(struct ntfs_index_context *icx, struct index_block *ib) 132 + { 133 + s64 ret, vcn = le64_to_cpu(ib->index_block_vcn); 134 + 135 + ntfs_debug("vcn: %lld\n", vcn); 136 + 137 + ret = pre_write_mst_fixup((struct ntfs_record *)ib, icx->block_size); 138 + if (ret) 139 + return -EIO; 140 + 141 + ret = ntfs_inode_attr_pwrite(VFS_I(icx->ia_ni), 142 + ntfs_ib_vcn_to_pos(icx, vcn), icx->block_size, 143 + (u8 *)ib, icx->sync_write); 144 + if (ret != icx->block_size) { 145 + ntfs_debug("Failed to write index block %lld, inode %llu", 146 + vcn, (unsigned long long)icx->idx_ni->mft_no); 147 + return ret; 136 148 } 137 - /* Get hold of the mft record for the index inode. */ 138 - m = map_mft_record(base_ni); 139 - if (IS_ERR(m)) { 140 - ntfs_error(sb, "map_mft_record() failed with error code %ld.", 141 - -PTR_ERR(m)); 142 - return PTR_ERR(m); 149 + 150 + return 0; 151 + } 152 + 153 + static int ntfs_icx_ib_write(struct ntfs_index_context *icx) 154 + { 155 + int err; 156 + 157 + err = ntfs_ib_write(icx, icx->ib); 158 + if (err) 159 + return err; 160 + 161 + icx->ib_dirty = false; 162 + 163 + return 0; 164 + } 165 + 166 + int ntfs_icx_ib_sync_write(struct ntfs_index_context *icx) 167 + { 168 + int ret; 169 + 170 + if (icx->ib_dirty == false) 171 + return 0; 172 + 173 + icx->sync_write = true; 174 + 175 + ret = ntfs_ib_write(icx, icx->ib); 176 + if (!ret) { 177 + kvfree(icx->ib); 178 + icx->ib = NULL; 179 + icx->ib_dirty = false; 180 + } else { 181 + post_write_mst_fixup((struct ntfs_record *)icx->ib); 182 + icx->sync_write = false; 143 183 } 144 - actx = ntfs_attr_get_search_ctx(base_ni, m); 145 - if (unlikely(!actx)) { 146 - err = -ENOMEM; 184 + 185 + return ret; 186 + } 187 + 188 + /* 189 + * ntfs_index_ctx_get - allocate and initialize a new index context 190 + * @ni: ntfs inode with which to initialize the context 191 + * @name: name of the which context describes 192 + * @name_len: length of the index name 193 + * 194 + * Allocate a new index context, initialize it with @ni and return it. 195 + * Return NULL if allocation failed. 196 + */ 197 + struct ntfs_index_context *ntfs_index_ctx_get(struct ntfs_inode *ni, 198 + __le16 *name, u32 name_len) 199 + { 200 + struct ntfs_index_context *icx; 201 + 202 + ntfs_debug("Entering\n"); 203 + 204 + if (!ni) 205 + return NULL; 206 + 207 + if (ni->nr_extents == -1) 208 + ni = ni->ext.base_ntfs_ino; 209 + 210 + icx = kmem_cache_alloc(ntfs_index_ctx_cache, GFP_NOFS); 211 + if (icx) 212 + *icx = (struct ntfs_index_context) { 213 + .idx_ni = ni, 214 + .name = name, 215 + .name_len = name_len, 216 + }; 217 + return icx; 218 + } 219 + 220 + static void ntfs_index_ctx_free(struct ntfs_index_context *icx) 221 + { 222 + ntfs_debug("Entering\n"); 223 + 224 + if (icx->actx) { 225 + ntfs_attr_put_search_ctx(icx->actx); 226 + icx->actx = NULL; 227 + } 228 + 229 + if (!icx->is_in_root) { 230 + if (icx->ib_dirty) 231 + ntfs_ib_write(icx, icx->ib); 232 + kvfree(icx->ib); 233 + icx->ib = NULL; 234 + } 235 + 236 + if (icx->ia_ni) { 237 + iput(VFS_I(icx->ia_ni)); 238 + icx->ia_ni = NULL; 239 + } 240 + } 241 + 242 + /* 243 + * ntfs_index_ctx_put - release an index context 244 + * @icx: index context to free 245 + * 246 + * Release the index context @icx, releasing all associated resources. 247 + */ 248 + void ntfs_index_ctx_put(struct ntfs_index_context *icx) 249 + { 250 + ntfs_index_ctx_free(icx); 251 + kmem_cache_free(ntfs_index_ctx_cache, icx); 252 + } 253 + 254 + /* 255 + * ntfs_index_ctx_reinit - reinitialize an index context 256 + * @icx: index context to reinitialize 257 + * 258 + * Reinitialize the index context @icx so it can be used for ntfs_index_lookup. 259 + */ 260 + void ntfs_index_ctx_reinit(struct ntfs_index_context *icx) 261 + { 262 + ntfs_debug("Entering\n"); 263 + 264 + ntfs_index_ctx_free(icx); 265 + 266 + *icx = (struct ntfs_index_context) { 267 + .idx_ni = icx->idx_ni, 268 + .name = icx->name, 269 + .name_len = icx->name_len, 270 + }; 271 + } 272 + 273 + static __le64 *ntfs_ie_get_vcn_addr(struct index_entry *ie) 274 + { 275 + return (__le64 *)((u8 *)ie + le16_to_cpu(ie->length) - sizeof(s64)); 276 + } 277 + 278 + /* 279 + * Get the subnode vcn to which the index entry refers. 280 + */ 281 + static s64 ntfs_ie_get_vcn(struct index_entry *ie) 282 + { 283 + return le64_to_cpup(ntfs_ie_get_vcn_addr(ie)); 284 + } 285 + 286 + static struct index_entry *ntfs_ie_get_first(struct index_header *ih) 287 + { 288 + return (struct index_entry *)((u8 *)ih + le32_to_cpu(ih->entries_offset)); 289 + } 290 + 291 + static struct index_entry *ntfs_ie_get_next(struct index_entry *ie) 292 + { 293 + return (struct index_entry *)((char *)ie + le16_to_cpu(ie->length)); 294 + } 295 + 296 + static u8 *ntfs_ie_get_end(struct index_header *ih) 297 + { 298 + return (u8 *)ih + le32_to_cpu(ih->index_length); 299 + } 300 + 301 + static int ntfs_ie_end(struct index_entry *ie) 302 + { 303 + return ie->flags & INDEX_ENTRY_END || !ie->length; 304 + } 305 + 306 + /* 307 + * Find the last entry in the index block 308 + */ 309 + static struct index_entry *ntfs_ie_get_last(struct index_entry *ie, char *ies_end) 310 + { 311 + ntfs_debug("Entering\n"); 312 + 313 + while ((char *)ie < ies_end && !ntfs_ie_end(ie)) 314 + ie = ntfs_ie_get_next(ie); 315 + 316 + return ie; 317 + } 318 + 319 + static struct index_entry *ntfs_ie_get_by_pos(struct index_header *ih, int pos) 320 + { 321 + struct index_entry *ie; 322 + 323 + ntfs_debug("pos: %d\n", pos); 324 + 325 + ie = ntfs_ie_get_first(ih); 326 + 327 + while (pos-- > 0) 328 + ie = ntfs_ie_get_next(ie); 329 + 330 + return ie; 331 + } 332 + 333 + static struct index_entry *ntfs_ie_prev(struct index_header *ih, struct index_entry *ie) 334 + { 335 + struct index_entry *ie_prev = NULL; 336 + struct index_entry *tmp; 337 + 338 + ntfs_debug("Entering\n"); 339 + 340 + tmp = ntfs_ie_get_first(ih); 341 + 342 + while (tmp != ie) { 343 + ie_prev = tmp; 344 + tmp = ntfs_ie_get_next(tmp); 345 + } 346 + 347 + return ie_prev; 348 + } 349 + 350 + static int ntfs_ih_numof_entries(struct index_header *ih) 351 + { 352 + int n; 353 + struct index_entry *ie; 354 + u8 *end; 355 + 356 + ntfs_debug("Entering\n"); 357 + 358 + end = ntfs_ie_get_end(ih); 359 + ie = ntfs_ie_get_first(ih); 360 + for (n = 0; !ntfs_ie_end(ie) && (u8 *)ie < end; n++) 361 + ie = ntfs_ie_get_next(ie); 362 + return n; 363 + } 364 + 365 + static int ntfs_ih_one_entry(struct index_header *ih) 366 + { 367 + return (ntfs_ih_numof_entries(ih) == 1); 368 + } 369 + 370 + static int ntfs_ih_zero_entry(struct index_header *ih) 371 + { 372 + return (ntfs_ih_numof_entries(ih) == 0); 373 + } 374 + 375 + static void ntfs_ie_delete(struct index_header *ih, struct index_entry *ie) 376 + { 377 + u32 new_size; 378 + 379 + ntfs_debug("Entering\n"); 380 + 381 + new_size = le32_to_cpu(ih->index_length) - le16_to_cpu(ie->length); 382 + ih->index_length = cpu_to_le32(new_size); 383 + memmove(ie, (u8 *)ie + le16_to_cpu(ie->length), 384 + new_size - ((u8 *)ie - (u8 *)ih)); 385 + } 386 + 387 + static void ntfs_ie_set_vcn(struct index_entry *ie, s64 vcn) 388 + { 389 + *ntfs_ie_get_vcn_addr(ie) = cpu_to_le64(vcn); 390 + } 391 + 392 + /* 393 + * Insert @ie index entry at @pos entry. Used @ih values should be ok already. 394 + */ 395 + static void ntfs_ie_insert(struct index_header *ih, struct index_entry *ie, 396 + struct index_entry *pos) 397 + { 398 + int ie_size = le16_to_cpu(ie->length); 399 + 400 + ntfs_debug("Entering\n"); 401 + 402 + ih->index_length = cpu_to_le32(le32_to_cpu(ih->index_length) + ie_size); 403 + memmove((u8 *)pos + ie_size, pos, 404 + le32_to_cpu(ih->index_length) - ((u8 *)pos - (u8 *)ih) - ie_size); 405 + memcpy(pos, ie, ie_size); 406 + } 407 + 408 + static struct index_entry *ntfs_ie_dup(struct index_entry *ie) 409 + { 410 + ntfs_debug("Entering\n"); 411 + 412 + return kmemdup(ie, le16_to_cpu(ie->length), GFP_NOFS); 413 + } 414 + 415 + static struct index_entry *ntfs_ie_dup_novcn(struct index_entry *ie) 416 + { 417 + struct index_entry *dup; 418 + int size = le16_to_cpu(ie->length); 419 + 420 + ntfs_debug("Entering\n"); 421 + 422 + if (ie->flags & INDEX_ENTRY_NODE) 423 + size -= sizeof(s64); 424 + 425 + dup = kmemdup(ie, size, GFP_NOFS); 426 + if (dup) { 427 + dup->flags &= ~INDEX_ENTRY_NODE; 428 + dup->length = cpu_to_le16(size); 429 + } 430 + return dup; 431 + } 432 + 433 + /* 434 + * Check the consistency of an index block 435 + * 436 + * Make sure the index block does not overflow from the index record. 437 + * The size of block is assumed to have been checked to be what is 438 + * defined in the index root. 439 + * 440 + * Returns 0 if no error was found -1 otherwise (with errno unchanged) 441 + * 442 + * |<--->| offsetof(struct index_block, index) 443 + * | |<--->| sizeof(struct index_header) 444 + * | | | 445 + * | | | seq index entries unused 446 + * |=====|=====|=====|===========================|==============| 447 + * | | | | | 448 + * | |<--------->| entries_offset | | 449 + * | |<---------------- index_length ------->| | 450 + * | |<--------------------- allocated_size --------------->| 451 + * |<--------------------------- block_size ------------------->| 452 + * 453 + * size(struct index_header) <= ent_offset < ind_length <= alloc_size < bk_size 454 + */ 455 + static int ntfs_index_block_inconsistent(struct ntfs_index_context *icx, 456 + struct index_block *ib, s64 vcn) 457 + { 458 + u32 ib_size = (unsigned int)le32_to_cpu(ib->index.allocated_size) + 459 + offsetof(struct index_block, index); 460 + struct super_block *sb = icx->idx_ni->vol->sb; 461 + unsigned long long inum = icx->idx_ni->mft_no; 462 + 463 + ntfs_debug("Entering\n"); 464 + 465 + if (!ntfs_is_indx_record(ib->magic)) { 466 + 467 + ntfs_error(sb, "Corrupt index block signature: vcn %lld inode %llu\n", 468 + vcn, (unsigned long long)icx->idx_ni->mft_no); 469 + return -1; 470 + } 471 + 472 + if (le64_to_cpu(ib->index_block_vcn) != vcn) { 473 + ntfs_error(sb, 474 + "Corrupt index block: s64 (%lld) is different from expected s64 (%lld) in inode %llu\n", 475 + (long long)le64_to_cpu(ib->index_block_vcn), 476 + vcn, inum); 477 + return -1; 478 + } 479 + 480 + if (ib_size != icx->block_size) { 481 + ntfs_error(sb, 482 + "Corrupt index block : s64 (%lld) of inode %llu has a size (%u) differing from the index specified size (%u)\n", 483 + vcn, inum, ib_size, icx->block_size); 484 + return -1; 485 + } 486 + 487 + if (le32_to_cpu(ib->index.entries_offset) < sizeof(struct index_header)) { 488 + ntfs_error(sb, "Invalid index entry offset in inode %lld\n", inum); 489 + return -1; 490 + } 491 + if (le32_to_cpu(ib->index.index_length) <= 492 + le32_to_cpu(ib->index.entries_offset)) { 493 + ntfs_error(sb, "No space for index entries in inode %lld\n", inum); 494 + return -1; 495 + } 496 + if (le32_to_cpu(ib->index.allocated_size) < 497 + le32_to_cpu(ib->index.index_length)) { 498 + ntfs_error(sb, "Index entries overflow in inode %lld\n", inum); 499 + return -1; 500 + } 501 + 502 + return 0; 503 + } 504 + 505 + static struct index_root *ntfs_ir_lookup(struct ntfs_inode *ni, __le16 *name, 506 + u32 name_len, struct ntfs_attr_search_ctx **ctx) 507 + { 508 + struct attr_record *a; 509 + struct index_root *ir = NULL; 510 + 511 + ntfs_debug("Entering\n"); 512 + *ctx = ntfs_attr_get_search_ctx(ni, NULL); 513 + if (!*ctx) { 514 + ntfs_error(ni->vol->sb, "%s, Failed to get search context", __func__); 515 + return NULL; 516 + } 517 + 518 + if (ntfs_attr_lookup(AT_INDEX_ROOT, name, name_len, CASE_SENSITIVE, 519 + 0, NULL, 0, *ctx)) { 520 + ntfs_error(ni->vol->sb, "Failed to lookup $INDEX_ROOT"); 147 521 goto err_out; 148 522 } 149 - /* Find the index root attribute in the mft record. */ 150 - err = ntfs_attr_lookup(AT_INDEX_ROOT, idx_ni->name, idx_ni->name_len, 151 - CASE_SENSITIVE, 0, NULL, 0, actx); 152 - if (unlikely(err)) { 153 - if (err == -ENOENT) { 154 - ntfs_error(sb, "Index root attribute missing in inode " 155 - "0x%lx.", idx_ni->mft_no); 156 - err = -EIO; 157 - } 523 + 524 + a = (*ctx)->attr; 525 + if (a->non_resident) { 526 + ntfs_error(ni->vol->sb, "Non-resident $INDEX_ROOT detected"); 158 527 goto err_out; 159 528 } 160 - /* Get to the index root value (it has been verified in read_inode). */ 161 - ir = (INDEX_ROOT*)((u8*)actx->attr + 162 - le16_to_cpu(actx->attr->data.resident.value_offset)); 163 - index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length); 164 - /* The first index entry. */ 165 - ie = (INDEX_ENTRY*)((u8*)&ir->index + 166 - le32_to_cpu(ir->index.entries_offset)); 529 + 530 + ir = (struct index_root *)((char *)a + le16_to_cpu(a->data.resident.value_offset)); 531 + err_out: 532 + if (!ir) { 533 + ntfs_attr_put_search_ctx(*ctx); 534 + *ctx = NULL; 535 + } 536 + return ir; 537 + } 538 + 539 + static struct index_root *ntfs_ir_lookup2(struct ntfs_inode *ni, __le16 *name, u32 len) 540 + { 541 + struct ntfs_attr_search_ctx *ctx; 542 + struct index_root *ir; 543 + 544 + ir = ntfs_ir_lookup(ni, name, len, &ctx); 545 + if (ir) 546 + ntfs_attr_put_search_ctx(ctx); 547 + return ir; 548 + } 549 + 550 + /* 551 + * Find a key in the index block. 552 + */ 553 + static int ntfs_ie_lookup(const void *key, const u32 key_len, 554 + struct ntfs_index_context *icx, struct index_header *ih, 555 + s64 *vcn, struct index_entry **ie_out) 556 + { 557 + struct index_entry *ie; 558 + u8 *index_end; 559 + int rc, item = 0; 560 + 561 + ntfs_debug("Entering\n"); 562 + 563 + index_end = ntfs_ie_get_end(ih); 564 + 167 565 /* 168 566 * Loop until we exceed valid memory (corruption case) or until we 169 567 * reach the last entry. 170 568 */ 171 - for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) { 569 + for (ie = ntfs_ie_get_first(ih); ; ie = ntfs_ie_get_next(ie)) { 172 570 /* Bounds checks. */ 173 - if ((u8*)ie < (u8*)actx->mrec || (u8*)ie + 174 - sizeof(INDEX_ENTRY_HEADER) > index_end || 175 - (u8*)ie + le16_to_cpu(ie->length) > index_end) 176 - goto idx_err_out; 571 + if ((u8 *)ie + sizeof(struct index_entry_header) > index_end || 572 + (u8 *)ie + le16_to_cpu(ie->length) > index_end) { 573 + ntfs_error(icx->idx_ni->vol->sb, 574 + "Index entry out of bounds in inode %llu.\n", 575 + (unsigned long long)icx->idx_ni->mft_no); 576 + return -ERANGE; 577 + } 578 + 177 579 /* 178 580 * The last entry cannot contain a key. It can however contain 179 581 * a pointer to a child node in the B+tree so we just break out. 180 582 */ 181 - if (ie->flags & INDEX_ENTRY_END) 583 + if (ntfs_ie_end(ie)) 182 584 break; 183 - /* Further bounds checks. */ 184 - if ((u32)sizeof(INDEX_ENTRY_HEADER) + 185 - le16_to_cpu(ie->key_length) > 186 - le16_to_cpu(ie->data.vi.data_offset) || 187 - (u32)le16_to_cpu(ie->data.vi.data_offset) + 188 - le16_to_cpu(ie->data.vi.data_length) > 189 - le16_to_cpu(ie->length)) 190 - goto idx_err_out; 191 - /* If the keys match perfectly, we setup @ictx and return 0. */ 192 - if ((key_len == le16_to_cpu(ie->key_length)) && !memcmp(key, 193 - &ie->key, key_len)) { 194 - ir_done: 195 - ictx->is_in_root = true; 196 - ictx->ir = ir; 197 - ictx->actx = actx; 198 - ictx->base_ni = base_ni; 199 - ictx->ia = NULL; 200 - ictx->page = NULL; 201 - done: 202 - ictx->entry = ie; 203 - ictx->data = (u8*)ie + 204 - le16_to_cpu(ie->data.vi.data_offset); 205 - ictx->data_len = le16_to_cpu(ie->data.vi.data_length); 206 - ntfs_debug("Done."); 207 - return err; 208 - } 585 + 209 586 /* 210 587 * Not a perfect match, need to do full blown collation so we 211 588 * know which way in the B+tree we have to go. 212 589 */ 213 - rc = ntfs_collate(vol, idx_ni->itype.index.collation_rule, key, 214 - key_len, &ie->key, le16_to_cpu(ie->key_length)); 590 + rc = ntfs_collate(icx->idx_ni->vol, icx->cr, key, key_len, &ie->key, 591 + le16_to_cpu(ie->key_length)); 592 + if (rc == -EINVAL) { 593 + ntfs_error(icx->idx_ni->vol->sb, 594 + "Collation error. Perhaps a filename contains invalid characters?\n"); 595 + return -ERANGE; 596 + } 215 597 /* 216 598 * If @key collates before the key of the current entry, there 217 599 * is definitely no such key in this index but we might need to ··· 601 219 */ 602 220 if (rc == -1) 603 221 break; 604 - /* 605 - * A match should never happen as the memcmp() call should have 606 - * cought it, but we still treat it correctly. 607 - */ 608 - if (!rc) 609 - goto ir_done; 610 - /* The keys are not equal, continue the search. */ 222 + 223 + if (!rc) { 224 + *ie_out = ie; 225 + icx->parent_pos[icx->pindex] = item; 226 + return 0; 227 + } 228 + 229 + item++; 611 230 } 612 231 /* 613 - * We have finished with this index without success. Check for the 614 - * presence of a child node and if not present setup @ictx and return 615 - * -ENOENT. 232 + * We have finished with this index block without success. Check for the 233 + * presence of a child node and if not present return with errno ENOENT, 234 + * otherwise we will keep searching in another index block. 616 235 */ 617 236 if (!(ie->flags & INDEX_ENTRY_NODE)) { 618 - ntfs_debug("Entry not found."); 619 - err = -ENOENT; 620 - goto ir_done; 621 - } /* Child node present, descend into it. */ 622 - /* Consistency check: Verify that an index allocation exists. */ 623 - if (!NInoIndexAllocPresent(idx_ni)) { 624 - ntfs_error(sb, "No index allocation attribute but index entry " 625 - "requires one. Inode 0x%lx is corrupt or " 626 - "driver bug.", idx_ni->mft_no); 627 - goto err_out; 237 + ntfs_debug("Index entry wasn't found.\n"); 238 + *ie_out = ie; 239 + return -ENOENT; 628 240 } 241 + 629 242 /* Get the starting vcn of the index_block holding the child node. */ 630 - vcn = sle64_to_cpup((sle64*)((u8*)ie + le16_to_cpu(ie->length) - 8)); 631 - ia_mapping = VFS_I(idx_ni)->i_mapping; 632 - /* 633 - * We are done with the index root and the mft record. Release them, 634 - * otherwise we deadlock with ntfs_map_page(). 635 - */ 636 - ntfs_attr_put_search_ctx(actx); 637 - unmap_mft_record(base_ni); 638 - m = NULL; 639 - actx = NULL; 640 - descend_into_child_node: 641 - /* 642 - * Convert vcn to index into the index allocation attribute in units 643 - * of PAGE_SIZE and map the page cache page, reading it from 644 - * disk if necessary. 645 - */ 646 - page = ntfs_map_page(ia_mapping, vcn << 647 - idx_ni->itype.index.vcn_size_bits >> PAGE_SHIFT); 648 - if (IS_ERR(page)) { 649 - ntfs_error(sb, "Failed to map index page, error %ld.", 650 - -PTR_ERR(page)); 651 - err = PTR_ERR(page); 243 + *vcn = ntfs_ie_get_vcn(ie); 244 + if (*vcn < 0) { 245 + ntfs_error(icx->idx_ni->vol->sb, "Negative vcn in inode %llu\n", 246 + (unsigned long long)icx->idx_ni->mft_no); 247 + return -EINVAL; 248 + } 249 + 250 + ntfs_debug("Parent entry number %d\n", item); 251 + icx->parent_pos[icx->pindex] = item; 252 + 253 + return -EAGAIN; 254 + } 255 + 256 + struct ntfs_inode *ntfs_ia_open(struct ntfs_index_context *icx, struct ntfs_inode *ni) 257 + { 258 + struct inode *ia_vi; 259 + 260 + ia_vi = ntfs_index_iget(VFS_I(ni), icx->name, icx->name_len); 261 + if (IS_ERR(ia_vi)) { 262 + ntfs_error(icx->idx_ni->vol->sb, 263 + "Failed to open index allocation of inode %llu", 264 + (unsigned long long)ni->mft_no); 265 + return NULL; 266 + } 267 + 268 + return NTFS_I(ia_vi); 269 + } 270 + 271 + static int ntfs_ib_read(struct ntfs_index_context *icx, s64 vcn, struct index_block *dst) 272 + { 273 + s64 pos, ret; 274 + 275 + ntfs_debug("vcn: %lld\n", vcn); 276 + 277 + pos = ntfs_ib_vcn_to_pos(icx, vcn); 278 + 279 + ret = ntfs_inode_attr_pread(VFS_I(icx->ia_ni), pos, icx->block_size, (u8 *)dst); 280 + if (ret != icx->block_size) { 281 + if (ret == -1) 282 + ntfs_error(icx->idx_ni->vol->sb, "Failed to read index block"); 283 + else 284 + ntfs_error(icx->idx_ni->vol->sb, 285 + "Failed to read full index block at %lld\n", pos); 286 + return -1; 287 + } 288 + 289 + post_read_mst_fixup((struct ntfs_record *)((u8 *)dst), icx->block_size); 290 + if (ntfs_index_block_inconsistent(icx, dst, vcn)) 291 + return -1; 292 + 293 + return 0; 294 + } 295 + 296 + static int ntfs_icx_parent_inc(struct ntfs_index_context *icx) 297 + { 298 + icx->pindex++; 299 + if (icx->pindex >= MAX_PARENT_VCN) { 300 + ntfs_error(icx->idx_ni->vol->sb, "Index is over %d level deep", MAX_PARENT_VCN); 301 + return -EOPNOTSUPP; 302 + } 303 + return 0; 304 + } 305 + 306 + static int ntfs_icx_parent_dec(struct ntfs_index_context *icx) 307 + { 308 + icx->pindex--; 309 + if (icx->pindex < 0) { 310 + ntfs_error(icx->idx_ni->vol->sb, "Corrupt index pointer (%d)", icx->pindex); 311 + return -EINVAL; 312 + } 313 + return 0; 314 + } 315 + 316 + /* 317 + * ntfs_index_lookup - find a key in an index and return its index entry 318 + * @key: key for which to search in the index 319 + * @key_len: length of @key in bytes 320 + * @icx: context describing the index and the returned entry 321 + * 322 + * Before calling ntfs_index_lookup(), @icx must have been obtained from a 323 + * call to ntfs_index_ctx_get(). 324 + * 325 + * Look for the @key in the index specified by the index lookup context @icx. 326 + * ntfs_index_lookup() walks the contents of the index looking for the @key. 327 + * 328 + * If the @key is found in the index, 0 is returned and @icx is setup to 329 + * describe the index entry containing the matching @key. @icx->entry is the 330 + * index entry and @icx->data and @icx->data_len are the index entry data and 331 + * its length in bytes, respectively. 332 + * 333 + * If the @key is not found in the index, -ENOENT is returned and 334 + * @icx is setup to describe the index entry whose key collates immediately 335 + * after the search @key, i.e. this is the position in the index at which 336 + * an index entry with a key of @key would need to be inserted. 337 + * 338 + * When finished with the entry and its data, call ntfs_index_ctx_put() to free 339 + * the context and other associated resources. 340 + * 341 + * If the index entry was modified, call ntfs_index_entry_mark_dirty() before 342 + * the call to ntfs_index_ctx_put() to ensure that the changes are written 343 + * to disk. 344 + */ 345 + int ntfs_index_lookup(const void *key, const u32 key_len, struct ntfs_index_context *icx) 346 + { 347 + s64 old_vcn, vcn; 348 + struct ntfs_inode *ni = icx->idx_ni; 349 + struct super_block *sb = ni->vol->sb; 350 + struct index_root *ir; 351 + struct index_entry *ie; 352 + struct index_block *ib = NULL; 353 + int err = 0; 354 + 355 + ntfs_debug("Entering\n"); 356 + 357 + if (!key) { 358 + ntfs_error(sb, "key: %p key_len: %d", key, key_len); 359 + return -EINVAL; 360 + } 361 + 362 + ir = ntfs_ir_lookup(ni, icx->name, icx->name_len, &icx->actx); 363 + if (!ir) 364 + return -EIO; 365 + 366 + icx->block_size = le32_to_cpu(ir->index_block_size); 367 + if (icx->block_size < NTFS_BLOCK_SIZE) { 368 + err = -EINVAL; 369 + ntfs_error(sb, 370 + "Index block size (%d) is smaller than the sector size (%d)", 371 + icx->block_size, NTFS_BLOCK_SIZE); 652 372 goto err_out; 653 373 } 654 - lock_page(page); 655 - kaddr = (u8*)page_address(page); 656 - fast_descend_into_child_node: 657 - /* Get to the index allocation block. */ 658 - ia = (INDEX_ALLOCATION*)(kaddr + ((vcn << 659 - idx_ni->itype.index.vcn_size_bits) & ~PAGE_MASK)); 660 - /* Bounds checks. */ 661 - if ((u8*)ia < kaddr || (u8*)ia > kaddr + PAGE_SIZE) { 662 - ntfs_error(sb, "Out of bounds check failed. Corrupt inode " 663 - "0x%lx or driver bug.", idx_ni->mft_no); 664 - goto unm_err_out; 374 + 375 + if (ni->vol->cluster_size <= icx->block_size) 376 + icx->vcn_size_bits = ni->vol->cluster_size_bits; 377 + else 378 + icx->vcn_size_bits = ni->vol->sector_size_bits; 379 + 380 + icx->cr = ir->collation_rule; 381 + if (!ntfs_is_collation_rule_supported(icx->cr)) { 382 + err = -EOPNOTSUPP; 383 + ntfs_error(sb, "Unknown collation rule 0x%x", 384 + (unsigned int)le32_to_cpu(icx->cr)); 385 + goto err_out; 665 386 } 666 - /* Catch multi sector transfer fixup errors. */ 667 - if (unlikely(!ntfs_is_indx_record(ia->magic))) { 668 - ntfs_error(sb, "Index record with vcn 0x%llx is corrupt. " 669 - "Corrupt inode 0x%lx. Run chkdsk.", 670 - (long long)vcn, idx_ni->mft_no); 671 - goto unm_err_out; 387 + 388 + old_vcn = VCN_INDEX_ROOT_PARENT; 389 + err = ntfs_ie_lookup(key, key_len, icx, &ir->index, &vcn, &ie); 390 + if (err == -ERANGE || err == -EINVAL) 391 + goto err_out; 392 + 393 + icx->ir = ir; 394 + if (err != -EAGAIN) { 395 + icx->is_in_root = true; 396 + icx->parent_vcn[icx->pindex] = old_vcn; 397 + goto done; 672 398 } 673 - if (sle64_to_cpu(ia->index_block_vcn) != vcn) { 674 - ntfs_error(sb, "Actual VCN (0x%llx) of index buffer is " 675 - "different from expected VCN (0x%llx). Inode " 676 - "0x%lx is corrupt or driver bug.", 677 - (unsigned long long) 678 - sle64_to_cpu(ia->index_block_vcn), 679 - (unsigned long long)vcn, idx_ni->mft_no); 680 - goto unm_err_out; 681 - } 682 - if (le32_to_cpu(ia->index.allocated_size) + 0x18 != 683 - idx_ni->itype.index.block_size) { 684 - ntfs_error(sb, "Index buffer (VCN 0x%llx) of inode 0x%lx has " 685 - "a size (%u) differing from the index " 686 - "specified size (%u). Inode is corrupt or " 687 - "driver bug.", (unsigned long long)vcn, 688 - idx_ni->mft_no, 689 - le32_to_cpu(ia->index.allocated_size) + 0x18, 690 - idx_ni->itype.index.block_size); 691 - goto unm_err_out; 692 - } 693 - index_end = (u8*)ia + idx_ni->itype.index.block_size; 694 - if (index_end > kaddr + PAGE_SIZE) { 695 - ntfs_error(sb, "Index buffer (VCN 0x%llx) of inode 0x%lx " 696 - "crosses page boundary. Impossible! Cannot " 697 - "access! This is probably a bug in the " 698 - "driver.", (unsigned long long)vcn, 699 - idx_ni->mft_no); 700 - goto unm_err_out; 701 - } 702 - index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length); 703 - if (index_end > (u8*)ia + idx_ni->itype.index.block_size) { 704 - ntfs_error(sb, "Size of index buffer (VCN 0x%llx) of inode " 705 - "0x%lx exceeds maximum size.", 706 - (unsigned long long)vcn, idx_ni->mft_no); 707 - goto unm_err_out; 708 - } 709 - /* The first index entry. */ 710 - ie = (INDEX_ENTRY*)((u8*)&ia->index + 711 - le32_to_cpu(ia->index.entries_offset)); 712 - /* 713 - * Iterate similar to above big loop but applied to index buffer, thus 714 - * loop until we exceed valid memory (corruption case) or until we 715 - * reach the last entry. 716 - */ 717 - for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) { 718 - /* Bounds checks. */ 719 - if ((u8*)ie < (u8*)ia || (u8*)ie + 720 - sizeof(INDEX_ENTRY_HEADER) > index_end || 721 - (u8*)ie + le16_to_cpu(ie->length) > index_end) { 722 - ntfs_error(sb, "Index entry out of bounds in inode " 723 - "0x%lx.", idx_ni->mft_no); 724 - goto unm_err_out; 725 - } 726 - /* 727 - * The last entry cannot contain a key. It can however contain 728 - * a pointer to a child node in the B+tree so we just break out. 729 - */ 730 - if (ie->flags & INDEX_ENTRY_END) 731 - break; 732 - /* Further bounds checks. */ 733 - if ((u32)sizeof(INDEX_ENTRY_HEADER) + 734 - le16_to_cpu(ie->key_length) > 735 - le16_to_cpu(ie->data.vi.data_offset) || 736 - (u32)le16_to_cpu(ie->data.vi.data_offset) + 737 - le16_to_cpu(ie->data.vi.data_length) > 738 - le16_to_cpu(ie->length)) { 739 - ntfs_error(sb, "Index entry out of bounds in inode " 740 - "0x%lx.", idx_ni->mft_no); 741 - goto unm_err_out; 742 - } 743 - /* If the keys match perfectly, we setup @ictx and return 0. */ 744 - if ((key_len == le16_to_cpu(ie->key_length)) && !memcmp(key, 745 - &ie->key, key_len)) { 746 - ia_done: 747 - ictx->is_in_root = false; 748 - ictx->actx = NULL; 749 - ictx->base_ni = NULL; 750 - ictx->ia = ia; 751 - ictx->page = page; 752 - goto done; 753 - } 754 - /* 755 - * Not a perfect match, need to do full blown collation so we 756 - * know which way in the B+tree we have to go. 757 - */ 758 - rc = ntfs_collate(vol, idx_ni->itype.index.collation_rule, key, 759 - key_len, &ie->key, le16_to_cpu(ie->key_length)); 760 - /* 761 - * If @key collates before the key of the current entry, there 762 - * is definitely no such key in this index but we might need to 763 - * descend into the B+tree so we just break out of the loop. 764 - */ 765 - if (rc == -1) 766 - break; 767 - /* 768 - * A match should never happen as the memcmp() call should have 769 - * cought it, but we still treat it correctly. 770 - */ 771 - if (!rc) 772 - goto ia_done; 773 - /* The keys are not equal, continue the search. */ 774 - } 775 - /* 776 - * We have finished with this index buffer without success. Check for 777 - * the presence of a child node and if not present return -ENOENT. 778 - */ 779 - if (!(ie->flags & INDEX_ENTRY_NODE)) { 780 - ntfs_debug("Entry not found."); 781 - err = -ENOENT; 782 - goto ia_done; 783 - } 784 - if ((ia->index.flags & NODE_MASK) == LEAF_NODE) { 785 - ntfs_error(sb, "Index entry with child node found in a leaf " 786 - "node in inode 0x%lx.", idx_ni->mft_no); 787 - goto unm_err_out; 788 - } 399 + 789 400 /* Child node present, descend into it. */ 790 - old_vcn = vcn; 791 - vcn = sle64_to_cpup((sle64*)((u8*)ie + le16_to_cpu(ie->length) - 8)); 792 - if (vcn >= 0) { 793 - /* 794 - * If vcn is in the same page cache page as old_vcn we recycle 795 - * the mapped page. 796 - */ 797 - if (old_vcn << vol->cluster_size_bits >> 798 - PAGE_SHIFT == vcn << 799 - vol->cluster_size_bits >> 800 - PAGE_SHIFT) 801 - goto fast_descend_into_child_node; 802 - unlock_page(page); 803 - ntfs_unmap_page(page); 804 - goto descend_into_child_node; 401 + icx->ia_ni = ntfs_ia_open(icx, ni); 402 + if (!icx->ia_ni) { 403 + err = -ENOENT; 404 + goto err_out; 805 405 } 806 - ntfs_error(sb, "Negative child node vcn in inode 0x%lx.", 807 - idx_ni->mft_no); 808 - unm_err_out: 809 - unlock_page(page); 810 - ntfs_unmap_page(page); 406 + 407 + ib = kvzalloc(icx->block_size, GFP_NOFS); 408 + if (!ib) { 409 + err = -ENOMEM; 410 + goto err_out; 411 + } 412 + 413 + descend_into_child_node: 414 + icx->parent_vcn[icx->pindex] = old_vcn; 415 + if (ntfs_icx_parent_inc(icx)) { 416 + err = -EIO; 417 + goto err_out; 418 + } 419 + old_vcn = vcn; 420 + 421 + ntfs_debug("Descend into node with s64 %lld.\n", vcn); 422 + 423 + if (ntfs_ib_read(icx, vcn, ib)) { 424 + err = -EIO; 425 + goto err_out; 426 + } 427 + err = ntfs_ie_lookup(key, key_len, icx, &ib->index, &vcn, &ie); 428 + if (err != -EAGAIN) { 429 + if (err == -EINVAL || err == -ERANGE) 430 + goto err_out; 431 + 432 + icx->is_in_root = false; 433 + icx->ib = ib; 434 + icx->parent_vcn[icx->pindex] = vcn; 435 + goto done; 436 + } 437 + 438 + if ((ib->index.flags & NODE_MASK) == LEAF_NODE) { 439 + ntfs_error(icx->idx_ni->vol->sb, 440 + "Index entry with child node found in a leaf node in inode 0x%llx.\n", 441 + (unsigned long long)ni->mft_no); 442 + goto err_out; 443 + } 444 + 445 + goto descend_into_child_node; 811 446 err_out: 447 + if (icx->actx) { 448 + ntfs_attr_put_search_ctx(icx->actx); 449 + icx->actx = NULL; 450 + } 451 + kvfree(ib); 812 452 if (!err) 813 453 err = -EIO; 814 - if (actx) 815 - ntfs_attr_put_search_ctx(actx); 816 - if (m) 817 - unmap_mft_record(base_ni); 818 454 return err; 819 - idx_err_out: 820 - ntfs_error(sb, "Corrupt index. Aborting lookup."); 455 + done: 456 + icx->entry = ie; 457 + icx->data = (u8 *)ie + offsetof(struct index_entry, key); 458 + icx->data_len = le16_to_cpu(ie->key_length); 459 + ntfs_debug("Done.\n"); 460 + return err; 461 + 462 + } 463 + 464 + static struct index_block *ntfs_ib_alloc(s64 ib_vcn, u32 ib_size, 465 + u8 node_type) 466 + { 467 + struct index_block *ib; 468 + int ih_size = sizeof(struct index_header); 469 + 470 + ntfs_debug("Entering ib_vcn = %lld ib_size = %u\n", ib_vcn, ib_size); 471 + 472 + ib = kvzalloc(ib_size, GFP_NOFS); 473 + if (!ib) 474 + return NULL; 475 + 476 + ib->magic = magic_INDX; 477 + ib->usa_ofs = cpu_to_le16(sizeof(struct index_block)); 478 + ib->usa_count = cpu_to_le16(ib_size / NTFS_BLOCK_SIZE + 1); 479 + /* Set USN to 1 */ 480 + *(__le16 *)((char *)ib + le16_to_cpu(ib->usa_ofs)) = cpu_to_le16(1); 481 + ib->lsn = 0; 482 + ib->index_block_vcn = cpu_to_le64(ib_vcn); 483 + ib->index.entries_offset = cpu_to_le32((ih_size + 484 + le16_to_cpu(ib->usa_count) * 2 + 7) & ~7); 485 + ib->index.index_length = 0; 486 + ib->index.allocated_size = cpu_to_le32(ib_size - 487 + (sizeof(struct index_block) - ih_size)); 488 + ib->index.flags = node_type; 489 + 490 + return ib; 491 + } 492 + 493 + /* 494 + * Find the median by going through all the entries 495 + */ 496 + static struct index_entry *ntfs_ie_get_median(struct index_header *ih) 497 + { 498 + struct index_entry *ie, *ie_start; 499 + u8 *ie_end; 500 + int i = 0, median; 501 + 502 + ntfs_debug("Entering\n"); 503 + 504 + ie = ie_start = ntfs_ie_get_first(ih); 505 + ie_end = (u8 *)ntfs_ie_get_end(ih); 506 + 507 + while ((u8 *)ie < ie_end && !ntfs_ie_end(ie)) { 508 + ie = ntfs_ie_get_next(ie); 509 + i++; 510 + } 511 + /* 512 + * NOTE: this could be also the entry at the half of the index block. 513 + */ 514 + median = i / 2 - 1; 515 + 516 + ntfs_debug("Entries: %d median: %d\n", i, median); 517 + 518 + for (i = 0, ie = ie_start; i <= median; i++) 519 + ie = ntfs_ie_get_next(ie); 520 + 521 + return ie; 522 + } 523 + 524 + static u64 ntfs_ibm_vcn_to_pos(struct ntfs_index_context *icx, s64 vcn) 525 + { 526 + u64 pos = ntfs_ib_vcn_to_pos(icx, vcn); 527 + 528 + do_div(pos, icx->block_size); 529 + return pos; 530 + } 531 + 532 + static s64 ntfs_ibm_pos_to_vcn(struct ntfs_index_context *icx, s64 pos) 533 + { 534 + return ntfs_ib_pos_to_vcn(icx, pos * icx->block_size); 535 + } 536 + 537 + static int ntfs_ibm_add(struct ntfs_index_context *icx) 538 + { 539 + u8 bmp[8]; 540 + 541 + ntfs_debug("Entering\n"); 542 + 543 + if (ntfs_attr_exist(icx->idx_ni, AT_BITMAP, icx->name, icx->name_len)) 544 + return 0; 545 + /* 546 + * AT_BITMAP must be at least 8 bytes. 547 + */ 548 + memset(bmp, 0, sizeof(bmp)); 549 + if (ntfs_attr_add(icx->idx_ni, AT_BITMAP, icx->name, icx->name_len, 550 + bmp, sizeof(bmp))) { 551 + ntfs_error(icx->idx_ni->vol->sb, "Failed to add AT_BITMAP"); 552 + return -EINVAL; 553 + } 554 + 555 + return 0; 556 + } 557 + 558 + static int ntfs_ibm_modify(struct ntfs_index_context *icx, s64 vcn, int set) 559 + { 560 + u8 byte; 561 + u64 pos = ntfs_ibm_vcn_to_pos(icx, vcn); 562 + u32 bpos = pos / 8; 563 + u32 bit = 1 << (pos % 8); 564 + struct ntfs_inode *bmp_ni; 565 + struct inode *bmp_vi; 566 + int ret = 0; 567 + 568 + ntfs_debug("%s vcn: %lld\n", set ? "set" : "clear", vcn); 569 + 570 + bmp_vi = ntfs_attr_iget(VFS_I(icx->idx_ni), AT_BITMAP, icx->name, icx->name_len); 571 + if (IS_ERR(bmp_vi)) { 572 + ntfs_error(icx->idx_ni->vol->sb, "Failed to open $BITMAP attribute"); 573 + return PTR_ERR(bmp_vi); 574 + } 575 + 576 + bmp_ni = NTFS_I(bmp_vi); 577 + 578 + if (set) { 579 + if (bmp_ni->data_size < bpos + 1) { 580 + ret = ntfs_attr_truncate(bmp_ni, (bmp_ni->data_size + 8) & ~7); 581 + if (ret) { 582 + ntfs_error(icx->idx_ni->vol->sb, "Failed to truncate AT_BITMAP"); 583 + goto err; 584 + } 585 + i_size_write(bmp_vi, (loff_t)bmp_ni->data_size); 586 + } 587 + } 588 + 589 + if (ntfs_inode_attr_pread(bmp_vi, bpos, 1, &byte) != 1) { 590 + ret = -EIO; 591 + ntfs_error(icx->idx_ni->vol->sb, "Failed to read $BITMAP"); 592 + goto err; 593 + } 594 + 595 + if (set) 596 + byte |= bit; 597 + else 598 + byte &= ~bit; 599 + 600 + if (ntfs_inode_attr_pwrite(bmp_vi, bpos, 1, &byte, false) != 1) { 601 + ret = -EIO; 602 + ntfs_error(icx->idx_ni->vol->sb, "Failed to write $Bitmap"); 603 + goto err; 604 + } 605 + 606 + err: 607 + iput(bmp_vi); 608 + return ret; 609 + } 610 + 611 + static int ntfs_ibm_set(struct ntfs_index_context *icx, s64 vcn) 612 + { 613 + return ntfs_ibm_modify(icx, vcn, 1); 614 + } 615 + 616 + static int ntfs_ibm_clear(struct ntfs_index_context *icx, s64 vcn) 617 + { 618 + return ntfs_ibm_modify(icx, vcn, 0); 619 + } 620 + 621 + static s64 ntfs_ibm_get_free(struct ntfs_index_context *icx) 622 + { 623 + u8 *bm; 624 + int bit; 625 + s64 vcn, byte, size; 626 + 627 + ntfs_debug("Entering\n"); 628 + 629 + bm = ntfs_attr_readall(icx->idx_ni, AT_BITMAP, icx->name, icx->name_len, 630 + &size); 631 + if (!bm) 632 + return (s64)-1; 633 + 634 + for (byte = 0; byte < size; byte++) { 635 + if (bm[byte] == 255) 636 + continue; 637 + 638 + for (bit = 0; bit < 8; bit++) { 639 + if (!(bm[byte] & (1 << bit))) { 640 + vcn = ntfs_ibm_pos_to_vcn(icx, byte * 8 + bit); 641 + goto out; 642 + } 643 + } 644 + } 645 + 646 + vcn = ntfs_ibm_pos_to_vcn(icx, size * 8); 647 + out: 648 + ntfs_debug("allocated vcn: %lld\n", vcn); 649 + 650 + if (ntfs_ibm_set(icx, vcn)) 651 + vcn = (s64)-1; 652 + 653 + kvfree(bm); 654 + return vcn; 655 + } 656 + 657 + static struct index_block *ntfs_ir_to_ib(struct index_root *ir, s64 ib_vcn) 658 + { 659 + struct index_block *ib; 660 + struct index_entry *ie_last; 661 + char *ies_start, *ies_end; 662 + int i; 663 + 664 + ntfs_debug("Entering\n"); 665 + 666 + ib = ntfs_ib_alloc(ib_vcn, le32_to_cpu(ir->index_block_size), LEAF_NODE); 667 + if (!ib) 668 + return NULL; 669 + 670 + ies_start = (char *)ntfs_ie_get_first(&ir->index); 671 + ies_end = (char *)ntfs_ie_get_end(&ir->index); 672 + ie_last = ntfs_ie_get_last((struct index_entry *)ies_start, ies_end); 673 + /* 674 + * Copy all entries, including the termination entry 675 + * as well, which can never have any data. 676 + */ 677 + i = (char *)ie_last - ies_start + le16_to_cpu(ie_last->length); 678 + memcpy(ntfs_ie_get_first(&ib->index), ies_start, i); 679 + 680 + ib->index.flags = ir->index.flags; 681 + ib->index.index_length = cpu_to_le32(i + 682 + le32_to_cpu(ib->index.entries_offset)); 683 + return ib; 684 + } 685 + 686 + static void ntfs_ir_nill(struct index_root *ir) 687 + { 688 + struct index_entry *ie_last; 689 + char *ies_start, *ies_end; 690 + 691 + ntfs_debug("Entering\n"); 692 + 693 + ies_start = (char *)ntfs_ie_get_first(&ir->index); 694 + ies_end = (char *)ntfs_ie_get_end(&ir->index); 695 + ie_last = ntfs_ie_get_last((struct index_entry *)ies_start, ies_end); 696 + /* 697 + * Move the index root termination entry forward 698 + */ 699 + if ((char *)ie_last > ies_start) { 700 + memmove((char *)ntfs_ie_get_first(&ir->index), 701 + (char *)ie_last, le16_to_cpu(ie_last->length)); 702 + ie_last = (struct index_entry *)ies_start; 703 + } 704 + } 705 + 706 + static int ntfs_ib_copy_tail(struct ntfs_index_context *icx, struct index_block *src, 707 + struct index_entry *median, s64 new_vcn) 708 + { 709 + u8 *ies_end; 710 + struct index_entry *ie_head; /* first entry after the median */ 711 + int tail_size, ret; 712 + struct index_block *dst; 713 + 714 + ntfs_debug("Entering\n"); 715 + 716 + dst = ntfs_ib_alloc(new_vcn, icx->block_size, 717 + src->index.flags & NODE_MASK); 718 + if (!dst) 719 + return -ENOMEM; 720 + 721 + ie_head = ntfs_ie_get_next(median); 722 + 723 + ies_end = (u8 *)ntfs_ie_get_end(&src->index); 724 + tail_size = ies_end - (u8 *)ie_head; 725 + memcpy(ntfs_ie_get_first(&dst->index), ie_head, tail_size); 726 + 727 + dst->index.index_length = cpu_to_le32(tail_size + 728 + le32_to_cpu(dst->index.entries_offset)); 729 + ret = ntfs_ib_write(icx, dst); 730 + 731 + kvfree(dst); 732 + return ret; 733 + } 734 + 735 + static int ntfs_ib_cut_tail(struct ntfs_index_context *icx, struct index_block *ib, 736 + struct index_entry *ie) 737 + { 738 + char *ies_start, *ies_end; 739 + struct index_entry *ie_last; 740 + int ret; 741 + 742 + ntfs_debug("Entering\n"); 743 + 744 + ies_start = (char *)ntfs_ie_get_first(&ib->index); 745 + ies_end = (char *)ntfs_ie_get_end(&ib->index); 746 + 747 + ie_last = ntfs_ie_get_last((struct index_entry *)ies_start, ies_end); 748 + if (ie_last->flags & INDEX_ENTRY_NODE) 749 + ntfs_ie_set_vcn(ie_last, ntfs_ie_get_vcn(ie)); 750 + 751 + unsafe_memcpy(ie, ie_last, le16_to_cpu(ie_last->length), 752 + /* alloc is larger than ie_last->length, see ntfs_ie_get_last() */); 753 + 754 + ib->index.index_length = cpu_to_le32(((char *)ie - ies_start) + 755 + le16_to_cpu(ie->length) + le32_to_cpu(ib->index.entries_offset)); 756 + 757 + ret = ntfs_ib_write(icx, ib); 758 + return ret; 759 + } 760 + 761 + static int ntfs_ia_add(struct ntfs_index_context *icx) 762 + { 763 + int ret; 764 + 765 + ntfs_debug("Entering\n"); 766 + 767 + ret = ntfs_ibm_add(icx); 768 + if (ret) 769 + return ret; 770 + 771 + if (!ntfs_attr_exist(icx->idx_ni, AT_INDEX_ALLOCATION, icx->name, icx->name_len)) { 772 + ret = ntfs_attr_add(icx->idx_ni, AT_INDEX_ALLOCATION, icx->name, 773 + icx->name_len, NULL, 0); 774 + if (ret) { 775 + ntfs_error(icx->idx_ni->vol->sb, "Failed to add AT_INDEX_ALLOCATION"); 776 + return ret; 777 + } 778 + } 779 + 780 + icx->ia_ni = ntfs_ia_open(icx, icx->idx_ni); 781 + if (!icx->ia_ni) 782 + return -ENOENT; 783 + 784 + return 0; 785 + } 786 + 787 + static int ntfs_ir_reparent(struct ntfs_index_context *icx) 788 + { 789 + struct ntfs_attr_search_ctx *ctx = NULL; 790 + struct index_root *ir; 791 + struct index_entry *ie; 792 + struct index_block *ib = NULL; 793 + s64 new_ib_vcn; 794 + int ix_root_size; 795 + int ret = 0; 796 + 797 + ntfs_debug("Entering\n"); 798 + 799 + ir = ntfs_ir_lookup2(icx->idx_ni, icx->name, icx->name_len); 800 + if (!ir) { 801 + ret = -ENOENT; 802 + goto out; 803 + } 804 + 805 + if ((ir->index.flags & NODE_MASK) == SMALL_INDEX) { 806 + ret = ntfs_ia_add(icx); 807 + if (ret) 808 + goto out; 809 + } 810 + 811 + new_ib_vcn = ntfs_ibm_get_free(icx); 812 + if (new_ib_vcn < 0) { 813 + ret = -EINVAL; 814 + goto out; 815 + } 816 + 817 + ir = ntfs_ir_lookup2(icx->idx_ni, icx->name, icx->name_len); 818 + if (!ir) { 819 + ret = -ENOENT; 820 + goto clear_bmp; 821 + } 822 + 823 + ib = ntfs_ir_to_ib(ir, new_ib_vcn); 824 + if (ib == NULL) { 825 + ret = -EIO; 826 + ntfs_error(icx->idx_ni->vol->sb, "Failed to move index root to index block"); 827 + goto clear_bmp; 828 + } 829 + 830 + ret = ntfs_ib_write(icx, ib); 831 + if (ret) 832 + goto clear_bmp; 833 + 834 + retry: 835 + ir = ntfs_ir_lookup(icx->idx_ni, icx->name, icx->name_len, &ctx); 836 + if (!ir) { 837 + ret = -ENOENT; 838 + goto clear_bmp; 839 + } 840 + 841 + ntfs_ir_nill(ir); 842 + 843 + ie = ntfs_ie_get_first(&ir->index); 844 + ie->flags |= INDEX_ENTRY_NODE; 845 + ie->length = cpu_to_le16(sizeof(struct index_entry_header) + sizeof(s64)); 846 + 847 + ir->index.flags = LARGE_INDEX; 848 + NInoSetIndexAllocPresent(icx->idx_ni); 849 + ir->index.index_length = cpu_to_le32(le32_to_cpu(ir->index.entries_offset) + 850 + le16_to_cpu(ie->length)); 851 + ir->index.allocated_size = ir->index.index_length; 852 + 853 + ix_root_size = sizeof(struct index_root) - sizeof(struct index_header) + 854 + le32_to_cpu(ir->index.allocated_size); 855 + ret = ntfs_resident_attr_value_resize(ctx->mrec, ctx->attr, ix_root_size); 856 + if (ret) { 857 + /* 858 + * When there is no space to build a non-resident 859 + * index, we may have to move the root to an extent 860 + */ 861 + if ((ret == -ENOSPC) && (ctx->al_entry || !ntfs_inode_add_attrlist(icx->idx_ni))) { 862 + ntfs_attr_put_search_ctx(ctx); 863 + ctx = NULL; 864 + ir = ntfs_ir_lookup(icx->idx_ni, icx->name, icx->name_len, &ctx); 865 + if (ir && !ntfs_attr_record_move_away(ctx, ix_root_size - 866 + le32_to_cpu(ctx->attr->data.resident.value_length))) { 867 + if (ntfs_attrlist_update(ctx->base_ntfs_ino ? 868 + ctx->base_ntfs_ino : ctx->ntfs_ino)) 869 + goto clear_bmp; 870 + ntfs_attr_put_search_ctx(ctx); 871 + ctx = NULL; 872 + goto retry; 873 + } 874 + } 875 + goto clear_bmp; 876 + } else { 877 + icx->idx_ni->data_size = icx->idx_ni->initialized_size = ix_root_size; 878 + icx->idx_ni->allocated_size = (ix_root_size + 7) & ~7; 879 + } 880 + ntfs_ie_set_vcn(ie, new_ib_vcn); 881 + 882 + err_out: 883 + kvfree(ib); 884 + if (ctx) 885 + ntfs_attr_put_search_ctx(ctx); 886 + out: 887 + return ret; 888 + clear_bmp: 889 + ntfs_ibm_clear(icx, new_ib_vcn); 821 890 goto err_out; 891 + } 892 + 893 + /* 894 + * ntfs_ir_truncate - Truncate index root attribute 895 + * @icx: index context 896 + * @data_size: new data size for the index root 897 + */ 898 + static int ntfs_ir_truncate(struct ntfs_index_context *icx, int data_size) 899 + { 900 + int ret; 901 + 902 + ntfs_debug("Entering\n"); 903 + 904 + /* 905 + * INDEX_ROOT must be resident and its entries can be moved to 906 + * struct index_block, so ENOSPC isn't a real error. 907 + */ 908 + ret = ntfs_attr_truncate(icx->idx_ni, data_size + offsetof(struct index_root, index)); 909 + if (!ret) { 910 + i_size_write(VFS_I(icx->idx_ni), icx->idx_ni->initialized_size); 911 + icx->ir = ntfs_ir_lookup2(icx->idx_ni, icx->name, icx->name_len); 912 + if (!icx->ir) 913 + return -ENOENT; 914 + 915 + icx->ir->index.allocated_size = cpu_to_le32(data_size); 916 + } else if (ret != -ENOSPC) 917 + ntfs_error(icx->idx_ni->vol->sb, "Failed to truncate INDEX_ROOT"); 918 + 919 + return ret; 920 + } 921 + 922 + /* 923 + * ntfs_ir_make_space - Make more space for the index root attribute 924 + * @icx: index context 925 + * @data_size: required data size for the index root 926 + */ 927 + static int ntfs_ir_make_space(struct ntfs_index_context *icx, int data_size) 928 + { 929 + int ret; 930 + 931 + ntfs_debug("Entering\n"); 932 + 933 + ret = ntfs_ir_truncate(icx, data_size); 934 + if (ret == -ENOSPC) { 935 + ret = ntfs_ir_reparent(icx); 936 + if (!ret) 937 + ret = -EAGAIN; 938 + else 939 + ntfs_error(icx->idx_ni->vol->sb, "Failed to modify INDEX_ROOT"); 940 + } 941 + 942 + return ret; 943 + } 944 + 945 + /* 946 + * NOTE: 'ie' must be a copy of a real index entry. 947 + */ 948 + static int ntfs_ie_add_vcn(struct index_entry **ie) 949 + { 950 + struct index_entry *p, *old = *ie; 951 + 952 + old->length = cpu_to_le16(le16_to_cpu(old->length) + sizeof(s64)); 953 + p = krealloc(old, le16_to_cpu(old->length), GFP_NOFS); 954 + if (!p) 955 + return -ENOMEM; 956 + 957 + p->flags |= INDEX_ENTRY_NODE; 958 + *ie = p; 959 + return 0; 960 + } 961 + 962 + static int ntfs_ih_insert(struct index_header *ih, struct index_entry *orig_ie, s64 new_vcn, 963 + int pos) 964 + { 965 + struct index_entry *ie_node, *ie; 966 + int ret = 0; 967 + s64 old_vcn; 968 + 969 + ntfs_debug("Entering\n"); 970 + ie = ntfs_ie_dup(orig_ie); 971 + if (!ie) 972 + return -ENOMEM; 973 + 974 + if (!(ie->flags & INDEX_ENTRY_NODE)) { 975 + ret = ntfs_ie_add_vcn(&ie); 976 + if (ret) 977 + goto out; 978 + } 979 + 980 + ie_node = ntfs_ie_get_by_pos(ih, pos); 981 + old_vcn = ntfs_ie_get_vcn(ie_node); 982 + ntfs_ie_set_vcn(ie_node, new_vcn); 983 + 984 + ntfs_ie_insert(ih, ie, ie_node); 985 + ntfs_ie_set_vcn(ie_node, old_vcn); 986 + out: 987 + kfree(ie); 988 + return ret; 989 + } 990 + 991 + static s64 ntfs_icx_parent_vcn(struct ntfs_index_context *icx) 992 + { 993 + return icx->parent_vcn[icx->pindex]; 994 + } 995 + 996 + static s64 ntfs_icx_parent_pos(struct ntfs_index_context *icx) 997 + { 998 + return icx->parent_pos[icx->pindex]; 999 + } 1000 + 1001 + static int ntfs_ir_insert_median(struct ntfs_index_context *icx, struct index_entry *median, 1002 + s64 new_vcn) 1003 + { 1004 + u32 new_size; 1005 + int ret; 1006 + 1007 + ntfs_debug("Entering\n"); 1008 + 1009 + icx->ir = ntfs_ir_lookup2(icx->idx_ni, icx->name, icx->name_len); 1010 + if (!icx->ir) 1011 + return -ENOENT; 1012 + 1013 + new_size = le32_to_cpu(icx->ir->index.index_length) + 1014 + le16_to_cpu(median->length); 1015 + if (!(median->flags & INDEX_ENTRY_NODE)) 1016 + new_size += sizeof(s64); 1017 + 1018 + ret = ntfs_ir_make_space(icx, new_size); 1019 + if (ret) 1020 + return ret; 1021 + 1022 + icx->ir = ntfs_ir_lookup2(icx->idx_ni, icx->name, icx->name_len); 1023 + if (!icx->ir) 1024 + return -ENOENT; 1025 + 1026 + return ntfs_ih_insert(&icx->ir->index, median, new_vcn, 1027 + ntfs_icx_parent_pos(icx)); 1028 + } 1029 + 1030 + static int ntfs_ib_split(struct ntfs_index_context *icx, struct index_block *ib); 1031 + 1032 + struct split_info { 1033 + struct list_head entry; 1034 + s64 new_vcn; 1035 + struct index_block *ib; 1036 + }; 1037 + 1038 + static int ntfs_ib_insert(struct ntfs_index_context *icx, struct index_entry *ie, s64 new_vcn, 1039 + struct split_info *si) 1040 + { 1041 + struct index_block *ib; 1042 + u32 idx_size, allocated_size; 1043 + int err; 1044 + s64 old_vcn; 1045 + 1046 + ntfs_debug("Entering\n"); 1047 + 1048 + ib = kvzalloc(icx->block_size, GFP_NOFS); 1049 + if (!ib) 1050 + return -ENOMEM; 1051 + 1052 + old_vcn = ntfs_icx_parent_vcn(icx); 1053 + 1054 + err = ntfs_ib_read(icx, old_vcn, ib); 1055 + if (err) 1056 + goto err_out; 1057 + 1058 + idx_size = le32_to_cpu(ib->index.index_length); 1059 + allocated_size = le32_to_cpu(ib->index.allocated_size); 1060 + if (idx_size + le16_to_cpu(ie->length) + sizeof(s64) > allocated_size) { 1061 + si->ib = ib; 1062 + si->new_vcn = new_vcn; 1063 + return -EAGAIN; 1064 + } 1065 + 1066 + err = ntfs_ih_insert(&ib->index, ie, new_vcn, ntfs_icx_parent_pos(icx)); 1067 + if (err) 1068 + goto err_out; 1069 + 1070 + err = ntfs_ib_write(icx, ib); 1071 + 1072 + err_out: 1073 + kvfree(ib); 1074 + return err; 1075 + } 1076 + 1077 + /* 1078 + * ntfs_ib_split - Split an index block 1079 + * @icx: index context 1080 + * @ib: index block to split 1081 + */ 1082 + static int ntfs_ib_split(struct ntfs_index_context *icx, struct index_block *ib) 1083 + { 1084 + struct index_entry *median; 1085 + s64 new_vcn; 1086 + int ret; 1087 + struct split_info *si; 1088 + LIST_HEAD(ntfs_cut_tail_list); 1089 + 1090 + ntfs_debug("Entering\n"); 1091 + 1092 + resplit: 1093 + ret = ntfs_icx_parent_dec(icx); 1094 + if (ret) 1095 + goto out; 1096 + 1097 + median = ntfs_ie_get_median(&ib->index); 1098 + new_vcn = ntfs_ibm_get_free(icx); 1099 + if (new_vcn < 0) { 1100 + ret = -EINVAL; 1101 + goto out; 1102 + } 1103 + 1104 + ret = ntfs_ib_copy_tail(icx, ib, median, new_vcn); 1105 + if (ret) { 1106 + ntfs_ibm_clear(icx, new_vcn); 1107 + goto out; 1108 + } 1109 + 1110 + if (ntfs_icx_parent_vcn(icx) == VCN_INDEX_ROOT_PARENT) { 1111 + ret = ntfs_ir_insert_median(icx, median, new_vcn); 1112 + if (ret) { 1113 + ntfs_ibm_clear(icx, new_vcn); 1114 + goto out; 1115 + } 1116 + } else { 1117 + si = kzalloc(sizeof(struct split_info), GFP_NOFS); 1118 + if (!si) { 1119 + ntfs_ibm_clear(icx, new_vcn); 1120 + ret = -ENOMEM; 1121 + goto out; 1122 + } 1123 + 1124 + ret = ntfs_ib_insert(icx, median, new_vcn, si); 1125 + if (ret == -EAGAIN) { 1126 + list_add_tail(&si->entry, &ntfs_cut_tail_list); 1127 + ib = si->ib; 1128 + goto resplit; 1129 + } else if (ret) { 1130 + kvfree(si->ib); 1131 + kfree(si); 1132 + ntfs_ibm_clear(icx, new_vcn); 1133 + goto out; 1134 + } 1135 + kfree(si); 1136 + } 1137 + 1138 + ret = ntfs_ib_cut_tail(icx, ib, median); 1139 + 1140 + out: 1141 + while (!list_empty(&ntfs_cut_tail_list)) { 1142 + si = list_last_entry(&ntfs_cut_tail_list, struct split_info, entry); 1143 + ntfs_ibm_clear(icx, si->new_vcn); 1144 + kvfree(si->ib); 1145 + list_del(&si->entry); 1146 + kfree(si); 1147 + if (!ret) 1148 + ret = -EAGAIN; 1149 + } 1150 + 1151 + return ret; 1152 + } 1153 + 1154 + int ntfs_ie_add(struct ntfs_index_context *icx, struct index_entry *ie) 1155 + { 1156 + struct index_header *ih; 1157 + int allocated_size, new_size; 1158 + int ret; 1159 + 1160 + while (1) { 1161 + ret = ntfs_index_lookup(&ie->key, le16_to_cpu(ie->key_length), icx); 1162 + if (!ret) { 1163 + ret = -EEXIST; 1164 + ntfs_error(icx->idx_ni->vol->sb, "Index already have such entry"); 1165 + goto err_out; 1166 + } 1167 + if (ret != -ENOENT) { 1168 + ntfs_error(icx->idx_ni->vol->sb, "Failed to find place for new entry"); 1169 + goto err_out; 1170 + } 1171 + ret = 0; 1172 + 1173 + if (icx->is_in_root) 1174 + ih = &icx->ir->index; 1175 + else 1176 + ih = &icx->ib->index; 1177 + 1178 + allocated_size = le32_to_cpu(ih->allocated_size); 1179 + new_size = le32_to_cpu(ih->index_length) + le16_to_cpu(ie->length); 1180 + 1181 + if (new_size <= allocated_size) 1182 + break; 1183 + 1184 + ntfs_debug("index block sizes: allocated: %d needed: %d\n", 1185 + allocated_size, new_size); 1186 + 1187 + if (icx->is_in_root) 1188 + ret = ntfs_ir_make_space(icx, new_size); 1189 + else 1190 + ret = ntfs_ib_split(icx, icx->ib); 1191 + if (ret && ret != -EAGAIN) 1192 + goto err_out; 1193 + 1194 + mark_mft_record_dirty(icx->actx->ntfs_ino); 1195 + ntfs_index_ctx_reinit(icx); 1196 + } 1197 + 1198 + ntfs_ie_insert(ih, ie, icx->entry); 1199 + ntfs_index_entry_mark_dirty(icx); 1200 + 1201 + err_out: 1202 + ntfs_debug("%s\n", ret ? "Failed" : "Done"); 1203 + return ret; 1204 + } 1205 + 1206 + /* 1207 + * ntfs_index_add_filename - add filename to directory index 1208 + * @ni: ntfs inode describing directory to which index add filename 1209 + * @fn: FILE_NAME attribute to add 1210 + * @mref: reference of the inode which @fn describes 1211 + */ 1212 + int ntfs_index_add_filename(struct ntfs_inode *ni, struct file_name_attr *fn, u64 mref) 1213 + { 1214 + struct index_entry *ie; 1215 + struct ntfs_index_context *icx; 1216 + int fn_size, ie_size, err; 1217 + 1218 + ntfs_debug("Entering\n"); 1219 + 1220 + if (!ni || !fn) 1221 + return -EINVAL; 1222 + 1223 + fn_size = (fn->file_name_length * sizeof(__le16)) + 1224 + sizeof(struct file_name_attr); 1225 + ie_size = (sizeof(struct index_entry_header) + fn_size + 7) & ~7; 1226 + 1227 + ie = kzalloc(ie_size, GFP_NOFS); 1228 + if (!ie) 1229 + return -ENOMEM; 1230 + 1231 + ie->data.dir.indexed_file = cpu_to_le64(mref); 1232 + ie->length = cpu_to_le16(ie_size); 1233 + ie->key_length = cpu_to_le16(fn_size); 1234 + 1235 + unsafe_memcpy(&ie->key, fn, fn_size, 1236 + /* "fn_size" was correctly calculated above */); 1237 + 1238 + icx = ntfs_index_ctx_get(ni, I30, 4); 1239 + if (!icx) { 1240 + err = -ENOMEM; 1241 + goto out; 1242 + } 1243 + 1244 + err = ntfs_ie_add(icx, ie); 1245 + ntfs_index_ctx_put(icx); 1246 + out: 1247 + kfree(ie); 1248 + return err; 1249 + } 1250 + 1251 + static int ntfs_ih_takeout(struct ntfs_index_context *icx, struct index_header *ih, 1252 + struct index_entry *ie, struct index_block *ib) 1253 + { 1254 + struct index_entry *ie_roam; 1255 + int freed_space; 1256 + bool full; 1257 + int ret = 0; 1258 + 1259 + ntfs_debug("Entering\n"); 1260 + 1261 + full = ih->index_length == ih->allocated_size; 1262 + ie_roam = ntfs_ie_dup_novcn(ie); 1263 + if (!ie_roam) 1264 + return -ENOMEM; 1265 + 1266 + ntfs_ie_delete(ih, ie); 1267 + 1268 + if (ntfs_icx_parent_vcn(icx) == VCN_INDEX_ROOT_PARENT) { 1269 + /* 1270 + * Recover the space which may have been freed 1271 + * while deleting an entry from root index 1272 + */ 1273 + freed_space = le32_to_cpu(ih->allocated_size) - 1274 + le32_to_cpu(ih->index_length); 1275 + if (full && (freed_space > 0) && !(freed_space & 7)) { 1276 + ntfs_ir_truncate(icx, le32_to_cpu(ih->index_length)); 1277 + /* do nothing if truncation fails */ 1278 + } 1279 + 1280 + mark_mft_record_dirty(icx->actx->ntfs_ino); 1281 + } else { 1282 + ret = ntfs_ib_write(icx, ib); 1283 + if (ret) 1284 + goto out; 1285 + } 1286 + 1287 + ntfs_index_ctx_reinit(icx); 1288 + 1289 + ret = ntfs_ie_add(icx, ie_roam); 1290 + out: 1291 + kfree(ie_roam); 1292 + return ret; 1293 + } 1294 + 1295 + /* 1296 + * Used if an empty index block to be deleted has END entry as the parent 1297 + * in the INDEX_ROOT which is the only one there. 1298 + */ 1299 + static void ntfs_ir_leafify(struct ntfs_index_context *icx, struct index_header *ih) 1300 + { 1301 + struct index_entry *ie; 1302 + 1303 + ntfs_debug("Entering\n"); 1304 + 1305 + ie = ntfs_ie_get_first(ih); 1306 + ie->flags &= ~INDEX_ENTRY_NODE; 1307 + ie->length = cpu_to_le16(le16_to_cpu(ie->length) - sizeof(s64)); 1308 + 1309 + ih->index_length = cpu_to_le32(le32_to_cpu(ih->index_length) - sizeof(s64)); 1310 + ih->flags &= ~LARGE_INDEX; 1311 + NInoClearIndexAllocPresent(icx->idx_ni); 1312 + 1313 + /* Not fatal error */ 1314 + ntfs_ir_truncate(icx, le32_to_cpu(ih->index_length)); 1315 + } 1316 + 1317 + /* 1318 + * Used if an empty index block to be deleted has END entry as the parent 1319 + * in the INDEX_ROOT which is not the only one there. 1320 + */ 1321 + static int ntfs_ih_reparent_end(struct ntfs_index_context *icx, struct index_header *ih, 1322 + struct index_block *ib) 1323 + { 1324 + struct index_entry *ie, *ie_prev; 1325 + 1326 + ntfs_debug("Entering\n"); 1327 + 1328 + ie = ntfs_ie_get_by_pos(ih, ntfs_icx_parent_pos(icx)); 1329 + ie_prev = ntfs_ie_prev(ih, ie); 1330 + if (!ie_prev) 1331 + return -EIO; 1332 + ntfs_ie_set_vcn(ie, ntfs_ie_get_vcn(ie_prev)); 1333 + 1334 + return ntfs_ih_takeout(icx, ih, ie_prev, ib); 1335 + } 1336 + 1337 + static int ntfs_index_rm_leaf(struct ntfs_index_context *icx) 1338 + { 1339 + struct index_block *ib = NULL; 1340 + struct index_header *parent_ih; 1341 + struct index_entry *ie; 1342 + int ret; 1343 + 1344 + ntfs_debug("pindex: %d\n", icx->pindex); 1345 + 1346 + ret = ntfs_icx_parent_dec(icx); 1347 + if (ret) 1348 + return ret; 1349 + 1350 + ret = ntfs_ibm_clear(icx, icx->parent_vcn[icx->pindex + 1]); 1351 + if (ret) 1352 + return ret; 1353 + 1354 + if (ntfs_icx_parent_vcn(icx) == VCN_INDEX_ROOT_PARENT) 1355 + parent_ih = &icx->ir->index; 1356 + else { 1357 + ib = kvzalloc(icx->block_size, GFP_NOFS); 1358 + if (!ib) 1359 + return -ENOMEM; 1360 + 1361 + ret = ntfs_ib_read(icx, ntfs_icx_parent_vcn(icx), ib); 1362 + if (ret) 1363 + goto out; 1364 + 1365 + parent_ih = &ib->index; 1366 + } 1367 + 1368 + ie = ntfs_ie_get_by_pos(parent_ih, ntfs_icx_parent_pos(icx)); 1369 + if (!ntfs_ie_end(ie)) { 1370 + ret = ntfs_ih_takeout(icx, parent_ih, ie, ib); 1371 + goto out; 1372 + } 1373 + 1374 + if (ntfs_ih_zero_entry(parent_ih)) { 1375 + if (ntfs_icx_parent_vcn(icx) == VCN_INDEX_ROOT_PARENT) { 1376 + ntfs_ir_leafify(icx, parent_ih); 1377 + goto out; 1378 + } 1379 + 1380 + ret = ntfs_index_rm_leaf(icx); 1381 + goto out; 1382 + } 1383 + 1384 + ret = ntfs_ih_reparent_end(icx, parent_ih, ib); 1385 + out: 1386 + kvfree(ib); 1387 + return ret; 1388 + } 1389 + 1390 + static int ntfs_index_rm_node(struct ntfs_index_context *icx) 1391 + { 1392 + int entry_pos, pindex; 1393 + s64 vcn; 1394 + struct index_block *ib = NULL; 1395 + struct index_entry *ie_succ, *ie, *entry = icx->entry; 1396 + struct index_header *ih; 1397 + u32 new_size; 1398 + int delta, ret; 1399 + 1400 + ntfs_debug("Entering\n"); 1401 + 1402 + if (!icx->ia_ni) { 1403 + icx->ia_ni = ntfs_ia_open(icx, icx->idx_ni); 1404 + if (!icx->ia_ni) 1405 + return -EINVAL; 1406 + } 1407 + 1408 + ib = kvzalloc(icx->block_size, GFP_NOFS); 1409 + if (!ib) 1410 + return -ENOMEM; 1411 + 1412 + ie_succ = ntfs_ie_get_next(icx->entry); 1413 + entry_pos = icx->parent_pos[icx->pindex]++; 1414 + pindex = icx->pindex; 1415 + descend: 1416 + vcn = ntfs_ie_get_vcn(ie_succ); 1417 + ret = ntfs_ib_read(icx, vcn, ib); 1418 + if (ret) 1419 + goto out; 1420 + 1421 + ie_succ = ntfs_ie_get_first(&ib->index); 1422 + 1423 + ret = ntfs_icx_parent_inc(icx); 1424 + if (ret) 1425 + goto out; 1426 + 1427 + icx->parent_vcn[icx->pindex] = vcn; 1428 + icx->parent_pos[icx->pindex] = 0; 1429 + 1430 + if ((ib->index.flags & NODE_MASK) == INDEX_NODE) 1431 + goto descend; 1432 + 1433 + if (ntfs_ih_zero_entry(&ib->index)) { 1434 + ret = -EIO; 1435 + ntfs_error(icx->idx_ni->vol->sb, "Empty index block"); 1436 + goto out; 1437 + } 1438 + 1439 + ie = ntfs_ie_dup(ie_succ); 1440 + if (!ie) { 1441 + ret = -ENOMEM; 1442 + goto out; 1443 + } 1444 + 1445 + ret = ntfs_ie_add_vcn(&ie); 1446 + if (ret) 1447 + goto out2; 1448 + 1449 + ntfs_ie_set_vcn(ie, ntfs_ie_get_vcn(icx->entry)); 1450 + 1451 + if (icx->is_in_root) 1452 + ih = &icx->ir->index; 1453 + else 1454 + ih = &icx->ib->index; 1455 + 1456 + delta = le16_to_cpu(ie->length) - le16_to_cpu(icx->entry->length); 1457 + new_size = le32_to_cpu(ih->index_length) + delta; 1458 + if (delta > 0) { 1459 + if (icx->is_in_root) { 1460 + ret = ntfs_ir_make_space(icx, new_size); 1461 + if (ret != 0) 1462 + goto out2; 1463 + 1464 + ih = &icx->ir->index; 1465 + entry = ntfs_ie_get_by_pos(ih, entry_pos); 1466 + 1467 + } else if (new_size > le32_to_cpu(ih->allocated_size)) { 1468 + icx->pindex = pindex; 1469 + ret = ntfs_ib_split(icx, icx->ib); 1470 + if (!ret) 1471 + ret = -EAGAIN; 1472 + goto out2; 1473 + } 1474 + } 1475 + 1476 + ntfs_ie_delete(ih, entry); 1477 + ntfs_ie_insert(ih, ie, entry); 1478 + 1479 + if (icx->is_in_root) 1480 + ret = ntfs_ir_truncate(icx, new_size); 1481 + else 1482 + ret = ntfs_icx_ib_write(icx); 1483 + if (ret) 1484 + goto out2; 1485 + 1486 + ntfs_ie_delete(&ib->index, ie_succ); 1487 + 1488 + if (ntfs_ih_zero_entry(&ib->index)) 1489 + ret = ntfs_index_rm_leaf(icx); 1490 + else 1491 + ret = ntfs_ib_write(icx, ib); 1492 + 1493 + out2: 1494 + kfree(ie); 1495 + out: 1496 + kvfree(ib); 1497 + return ret; 1498 + } 1499 + 1500 + /* 1501 + * ntfs_index_rm - remove entry from the index 1502 + * @icx: index context describing entry to delete 1503 + * 1504 + * Delete entry described by @icx from the index. Index context is always 1505 + * reinitialized after use of this function, so it can be used for index 1506 + * lookup once again. 1507 + */ 1508 + int ntfs_index_rm(struct ntfs_index_context *icx) 1509 + { 1510 + struct index_header *ih; 1511 + int ret = 0; 1512 + 1513 + ntfs_debug("Entering\n"); 1514 + 1515 + if (!icx || (!icx->ib && !icx->ir) || ntfs_ie_end(icx->entry)) { 1516 + ret = -EINVAL; 1517 + goto err_out; 1518 + } 1519 + if (icx->is_in_root) 1520 + ih = &icx->ir->index; 1521 + else 1522 + ih = &icx->ib->index; 1523 + 1524 + if (icx->entry->flags & INDEX_ENTRY_NODE) { 1525 + ret = ntfs_index_rm_node(icx); 1526 + if (ret) 1527 + goto err_out; 1528 + } else if (icx->is_in_root || !ntfs_ih_one_entry(ih)) { 1529 + ntfs_ie_delete(ih, icx->entry); 1530 + 1531 + if (icx->is_in_root) 1532 + ret = ntfs_ir_truncate(icx, le32_to_cpu(ih->index_length)); 1533 + else 1534 + ret = ntfs_icx_ib_write(icx); 1535 + if (ret) 1536 + goto err_out; 1537 + } else { 1538 + ret = ntfs_index_rm_leaf(icx); 1539 + if (ret) 1540 + goto err_out; 1541 + } 1542 + 1543 + return 0; 1544 + err_out: 1545 + return ret; 1546 + } 1547 + 1548 + int ntfs_index_remove(struct ntfs_inode *dir_ni, const void *key, const u32 keylen) 1549 + { 1550 + int ret = 0; 1551 + struct ntfs_index_context *icx; 1552 + 1553 + icx = ntfs_index_ctx_get(dir_ni, I30, 4); 1554 + if (!icx) 1555 + return -EINVAL; 1556 + 1557 + while (1) { 1558 + ret = ntfs_index_lookup(key, keylen, icx); 1559 + if (ret) 1560 + goto err_out; 1561 + 1562 + ret = ntfs_index_rm(icx); 1563 + if (ret && ret != -EAGAIN) 1564 + goto err_out; 1565 + else if (!ret) 1566 + break; 1567 + 1568 + mark_mft_record_dirty(icx->actx->ntfs_ino); 1569 + ntfs_index_ctx_reinit(icx); 1570 + } 1571 + 1572 + mark_mft_record_dirty(icx->actx->ntfs_ino); 1573 + 1574 + ntfs_index_ctx_put(icx); 1575 + return 0; 1576 + err_out: 1577 + ntfs_index_ctx_put(icx); 1578 + ntfs_error(dir_ni->vol->sb, "Delete failed"); 1579 + return ret; 1580 + } 1581 + 1582 + /* 1583 + * ntfs_index_walk_down - walk down the index tree (leaf bound) 1584 + * until there are no subnode in the first index entry returns 1585 + * the entry at the bottom left in subnode 1586 + */ 1587 + struct index_entry *ntfs_index_walk_down(struct index_entry *ie, struct ntfs_index_context *ictx) 1588 + { 1589 + struct index_entry *entry; 1590 + s64 vcn; 1591 + 1592 + entry = ie; 1593 + do { 1594 + vcn = ntfs_ie_get_vcn(entry); 1595 + if (ictx->is_in_root) { 1596 + /* down from level zero */ 1597 + ictx->ir = NULL; 1598 + ictx->ib = kvzalloc(ictx->block_size, GFP_NOFS); 1599 + ictx->pindex = 1; 1600 + ictx->is_in_root = false; 1601 + } else { 1602 + /* down from non-zero level */ 1603 + ictx->pindex++; 1604 + } 1605 + 1606 + ictx->parent_pos[ictx->pindex] = 0; 1607 + ictx->parent_vcn[ictx->pindex] = vcn; 1608 + if (!ntfs_ib_read(ictx, vcn, ictx->ib)) { 1609 + ictx->entry = ntfs_ie_get_first(&ictx->ib->index); 1610 + entry = ictx->entry; 1611 + } else 1612 + entry = NULL; 1613 + } while (entry && (entry->flags & INDEX_ENTRY_NODE)); 1614 + 1615 + return entry; 1616 + } 1617 + 1618 + /* 1619 + * ntfs_index_walk_up - walk up the index tree (root bound) until 1620 + * there is a valid data entry in parent returns the parent entry 1621 + * or NULL if no more parent. 1622 + * @ie: current index entry 1623 + * @ictx: index context 1624 + */ 1625 + static struct index_entry *ntfs_index_walk_up(struct index_entry *ie, 1626 + struct ntfs_index_context *ictx) 1627 + { 1628 + struct index_entry *entry; 1629 + s64 vcn; 1630 + 1631 + entry = ie; 1632 + if (ictx->pindex > 0) { 1633 + do { 1634 + ictx->pindex--; 1635 + if (!ictx->pindex) { 1636 + /* we have reached the root */ 1637 + kfree(ictx->ib); 1638 + ictx->ib = NULL; 1639 + ictx->is_in_root = true; 1640 + /* a new search context is to be allocated */ 1641 + if (ictx->actx) 1642 + ntfs_attr_put_search_ctx(ictx->actx); 1643 + ictx->ir = ntfs_ir_lookup(ictx->idx_ni, ictx->name, 1644 + ictx->name_len, &ictx->actx); 1645 + if (ictx->ir) 1646 + entry = ntfs_ie_get_by_pos(&ictx->ir->index, 1647 + ictx->parent_pos[ictx->pindex]); 1648 + else 1649 + entry = NULL; 1650 + } else { 1651 + /* up into non-root node */ 1652 + vcn = ictx->parent_vcn[ictx->pindex]; 1653 + if (!ntfs_ib_read(ictx, vcn, ictx->ib)) { 1654 + entry = ntfs_ie_get_by_pos(&ictx->ib->index, 1655 + ictx->parent_pos[ictx->pindex]); 1656 + } else 1657 + entry = NULL; 1658 + } 1659 + ictx->entry = entry; 1660 + } while (entry && (ictx->pindex > 0) && 1661 + (entry->flags & INDEX_ENTRY_END)); 1662 + } else 1663 + entry = NULL; 1664 + 1665 + return entry; 1666 + } 1667 + 1668 + /* 1669 + * ntfs_index_next - get next entry in an index according to collating sequence. 1670 + * Returns next entry or NULL if none. 1671 + * 1672 + * Sample layout : 1673 + * 1674 + * +---+---+---+---+---+---+---+---+ n ptrs to subnodes 1675 + * | | | 10| 25| 33| | | | n-1 keys in between 1676 + * +---+---+---+---+---+---+---+---+ no key in last entry 1677 + * | A | A 1678 + * | | | +-------------------------------+ 1679 + * +--------------------------+ | +-----+ | 1680 + * | +--+ | | 1681 + * V | V | 1682 + * +---+---+---+---+---+---+---+---+ | +---+---+---+---+---+---+---+---+ 1683 + * | 11| 12| 13| 14| 15| 16| 17| | | | 26| 27| 28| 29| 30| 31| 32| | 1684 + * +---+---+---+---+---+---+---+---+ | +---+---+---+---+---+---+---+---+ 1685 + * | | 1686 + * +-----------------------+ | 1687 + * | | 1688 + * +---+---+---+---+---+---+---+---+ 1689 + * | 18| 19| 20| 21| 22| 23| 24| | 1690 + * +---+---+---+---+---+---+---+---+ 1691 + * 1692 + * @ie: current index entry 1693 + * @ictx: index context 1694 + */ 1695 + struct index_entry *ntfs_index_next(struct index_entry *ie, struct ntfs_index_context *ictx) 1696 + { 1697 + struct index_entry *next; 1698 + __le16 flags; 1699 + 1700 + /* 1701 + * lookup() may have returned an invalid node 1702 + * when searching for a partial key 1703 + * if this happens, walk up 1704 + */ 1705 + if (ie->flags & INDEX_ENTRY_END) 1706 + next = ntfs_index_walk_up(ie, ictx); 1707 + else { 1708 + /* 1709 + * get next entry in same node 1710 + * there is always one after any entry with data 1711 + */ 1712 + next = (struct index_entry *)((char *)ie + le16_to_cpu(ie->length)); 1713 + ++ictx->parent_pos[ictx->pindex]; 1714 + flags = next->flags; 1715 + 1716 + /* walk down if it has a subnode */ 1717 + if (flags & INDEX_ENTRY_NODE) { 1718 + if (!ictx->ia_ni) 1719 + ictx->ia_ni = ntfs_ia_open(ictx, ictx->idx_ni); 1720 + 1721 + next = ntfs_index_walk_down(next, ictx); 1722 + } else { 1723 + 1724 + /* walk up it has no subnode, nor data */ 1725 + if (flags & INDEX_ENTRY_END) 1726 + next = ntfs_index_walk_up(next, ictx); 1727 + } 1728 + } 1729 + 1730 + /* return NULL if stuck at end of a block */ 1731 + if (next && (next->flags & INDEX_ENTRY_END)) 1732 + next = NULL; 1733 + 1734 + return next; 822 1735 }