1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * NTFS kernel directory operations. 4 * 5 * Copyright (c) 2001-2007 Anton Altaparmakov 6 * Copyright (c) 2002 Richard Russon 7 * Copyright (c) 2025 LG Electronics Co., Ltd. 8 */ 9 10 #include <linux/blkdev.h> 11 #include <linux/string.h> 12 13 #include "dir.h" 14 #include "mft.h" 15 #include "ntfs.h" 16 #include "index.h" 17 #include "reparse.h" 18 19 #include <linux/filelock.h> 20 21 /* 22 * The little endian Unicode string $I30 as a global constant. 23 */ 24 __le16 I30[5] = { cpu_to_le16('$'), cpu_to_le16('I'), 25 cpu_to_le16('3'), cpu_to_le16('0'), 0 }; 26 27 static inline u64 ntfs_check_mref(u64 mref) 28 { 29 if (IS_ERR_MREF(mref)) 30 return ERR_MREF(-EIO); 31 return mref; 32 } 33 34 /* 35 * ntfs_lookup_inode_by_name - find an inode in a directory given its name 36 * @dir_ni: ntfs inode of the directory in which to search for the name 37 * @uname: Unicode name for which to search in the directory 38 * @uname_len: length of the name @uname in Unicode characters 39 * @res: return the found file name if necessary (see below) 40 * 41 * Look for an inode with name @uname in the directory with inode @dir_ni. 42 * ntfs_lookup_inode_by_name() walks the contents of the directory looking for 43 * the Unicode name. If the name is found in the directory, the corresponding 44 * inode number (>= 0) is returned as a mft reference in cpu format, i.e. it 45 * is a 64-bit number containing the sequence number. 46 * 47 * On error, a negative value is returned corresponding to the error code. In 48 * particular if the inode is not found -ENOENT is returned. Note that you 49 * can't just check the return value for being negative, you have to check the 50 * inode number for being negative which you can extract using MREC(return 51 * value). 52 * 53 * Note, @uname_len does not include the (optional) terminating NULL character. 54 * 55 * Note, we look for a case sensitive match first but we also look for a case 56 * insensitive match at the same time. If we find a case insensitive match, we 57 * save that for the case that we don't find an exact match, where we return 58 * the case insensitive match and setup @res (which we allocate!) with the mft 59 * reference, the file name type, length and with a copy of the little endian 60 * Unicode file name itself. If we match a file name which is in the DOS name 61 * space, we only return the mft reference and file name type in @res. 62 * ntfs_lookup() then uses this to find the long file name in the inode itself. 63 * This is to avoid polluting the dcache with short file names. We want them to 64 * work but we don't care for how quickly one can access them. This also fixes 65 * the dcache aliasing issues. 66 * 67 * Locking: - Caller must hold i_mutex on the directory. 68 * - Each page cache page in the index allocation mapping must be 69 * locked whilst being accessed otherwise we may find a corrupt 70 * page due to it being under ->writepage at the moment which 71 * applies the mst protection fixups before writing out and then 72 * removes them again after the write is complete after which it 73 * unlocks the page. 74 */ 75 u64 ntfs_lookup_inode_by_name(struct ntfs_inode *dir_ni, const __le16 *uname, 76 const int uname_len, struct ntfs_name **res) 77 { 78 struct ntfs_volume *vol = dir_ni->vol; 79 struct super_block *sb = vol->sb; 80 struct inode *ia_vi = NULL; 81 struct mft_record *m; 82 struct index_root *ir; 83 struct index_entry *ie; 84 struct index_block *ia; 85 u8 *index_end; 86 u64 mref; 87 struct ntfs_attr_search_ctx *ctx; 88 int err, rc; 89 s64 vcn, old_vcn; 90 struct address_space *ia_mapping; 91 struct folio *folio; 92 u8 *kaddr = NULL; 93 struct ntfs_name *name = NULL; 94 95 /* Get hold of the mft record for the directory. */ 96 m = map_mft_record(dir_ni); 97 if (IS_ERR(m)) { 98 ntfs_error(sb, "map_mft_record() failed with error code %ld.", 99 -PTR_ERR(m)); 100 return ERR_MREF(PTR_ERR(m)); 101 } 102 ctx = ntfs_attr_get_search_ctx(dir_ni, m); 103 if (unlikely(!ctx)) { 104 err = -ENOMEM; 105 goto err_out; 106 } 107 /* Find the index root attribute in the mft record. */ 108 err = ntfs_attr_lookup(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 0, NULL, 109 0, ctx); 110 if (unlikely(err)) { 111 if (err == -ENOENT) { 112 ntfs_error(sb, 113 "Index root attribute missing in directory inode 0x%llx.", 114 dir_ni->mft_no); 115 err = -EIO; 116 } 117 goto err_out; 118 } 119 /* Get to the index root value (it's been verified in read_inode). */ 120 ir = (struct index_root *)((u8 *)ctx->attr + 121 le16_to_cpu(ctx->attr->data.resident.value_offset)); 122 index_end = (u8 *)&ir->index + le32_to_cpu(ir->index.index_length); 123 /* The first index entry. */ 124 ie = (struct index_entry *)((u8 *)&ir->index + 125 le32_to_cpu(ir->index.entries_offset)); 126 /* 127 * Loop until we exceed valid memory (corruption case) or until we 128 * reach the last entry. 129 */ 130 for (;; ie = (struct index_entry *)((u8 *)ie + le16_to_cpu(ie->length))) { 131 /* Bounds checks. */ 132 if ((u8 *)ie < (u8 *)ctx->mrec || 133 (u8 *)ie + sizeof(struct index_entry_header) > index_end || 134 (u8 *)ie + sizeof(struct index_entry_header) + le16_to_cpu(ie->key_length) > 135 index_end || (u8 *)ie + le16_to_cpu(ie->length) > index_end) 136 goto dir_err_out; 137 /* 138 * The last entry cannot contain a name. It can however contain 139 * a pointer to a child node in the B+tree so we just break out. 140 */ 141 if (ie->flags & INDEX_ENTRY_END) 142 break; 143 /* Key length should not be zero if it is not last entry. */ 144 if (!ie->key_length) 145 goto dir_err_out; 146 /* 147 * We perform a case sensitive comparison and if that matches 148 * we are done and return the mft reference of the inode (i.e. 149 * the inode number together with the sequence number for 150 * consistency checking). We convert it to cpu format before 151 * returning. 152 */ 153 if (ntfs_are_names_equal(uname, uname_len, 154 (__le16 *)&ie->key.file_name.file_name, 155 ie->key.file_name.file_name_length, 156 CASE_SENSITIVE, vol->upcase, vol->upcase_len)) { 157 found_it: 158 /* 159 * We have a perfect match, so we don't need to care 160 * about having matched imperfectly before, so we can 161 * free name and set *res to NULL. 162 * However, if the perfect match is a short file name, 163 * we need to signal this through *res, so that 164 * ntfs_lookup() can fix dcache aliasing issues. 165 * As an optimization we just reuse an existing 166 * allocation of *res. 167 */ 168 if (ie->key.file_name.file_name_type == FILE_NAME_DOS) { 169 if (!name) { 170 name = kmalloc_obj(struct ntfs_name, 171 GFP_NOFS); 172 if (!name) { 173 err = -ENOMEM; 174 goto err_out; 175 } 176 } 177 name->mref = le64_to_cpu( 178 ie->data.dir.indexed_file); 179 name->type = FILE_NAME_DOS; 180 name->len = 0; 181 *res = name; 182 } else { 183 kfree(name); 184 *res = NULL; 185 } 186 mref = le64_to_cpu(ie->data.dir.indexed_file); 187 ntfs_attr_put_search_ctx(ctx); 188 unmap_mft_record(dir_ni); 189 return ntfs_check_mref(mref); 190 } 191 /* 192 * For a case insensitive mount, we also perform a case 193 * insensitive comparison (provided the file name is not in the 194 * POSIX namespace). If the comparison matches, and the name is 195 * in the WIN32 namespace, we cache the filename in *res so 196 * that the caller, ntfs_lookup(), can work on it. If the 197 * comparison matches, and the name is in the DOS namespace, we 198 * only cache the mft reference and the file name type (we set 199 * the name length to zero for simplicity). 200 */ 201 if ((!NVolCaseSensitive(vol) || 202 ie->key.file_name.file_name_type == FILE_NAME_DOS) && 203 ntfs_are_names_equal(uname, uname_len, 204 (__le16 *)&ie->key.file_name.file_name, 205 ie->key.file_name.file_name_length, 206 IGNORE_CASE, vol->upcase, 207 vol->upcase_len)) { 208 int name_size = sizeof(struct ntfs_name); 209 u8 type = ie->key.file_name.file_name_type; 210 u8 len = ie->key.file_name.file_name_length; 211 212 /* Only one case insensitive matching name allowed. */ 213 if (name) { 214 ntfs_error(sb, 215 "Found already allocated name in phase 1. Please run chkdsk"); 216 goto dir_err_out; 217 } 218 219 if (type != FILE_NAME_DOS) 220 name_size += len * sizeof(__le16); 221 name = kmalloc(name_size, GFP_NOFS); 222 if (!name) { 223 err = -ENOMEM; 224 goto err_out; 225 } 226 name->mref = le64_to_cpu(ie->data.dir.indexed_file); 227 name->type = type; 228 if (type != FILE_NAME_DOS) { 229 name->len = len; 230 memcpy(name->name, ie->key.file_name.file_name, 231 len * sizeof(__le16)); 232 } else 233 name->len = 0; 234 *res = name; 235 } 236 /* 237 * Not a perfect match, need to do full blown collation so we 238 * know which way in the B+tree we have to go. 239 */ 240 rc = ntfs_collate_names(uname, uname_len, 241 (__le16 *)&ie->key.file_name.file_name, 242 ie->key.file_name.file_name_length, false, 243 IGNORE_CASE, vol->upcase, vol->upcase_len); 244 /* 245 * If uname collates before the name of the current entry, there 246 * is definitely no such name in this index but we might need to 247 * descend into the B+tree so we just break out of the loop. 248 */ 249 if (rc == -1) 250 break; 251 /* The names are not equal, continue the search. */ 252 if (rc) 253 continue; 254 /* 255 * Names match with case insensitive comparison, now try the 256 * case sensitive comparison, which is required for proper 257 * collation. 258 */ 259 rc = ntfs_collate_names(uname, uname_len, 260 (__le16 *)&ie->key.file_name.file_name, 261 ie->key.file_name.file_name_length, false, 262 CASE_SENSITIVE, vol->upcase, vol->upcase_len); 263 if (rc == -1) 264 break; 265 if (rc) 266 continue; 267 /* 268 * Perfect match, this will never happen as the 269 * ntfs_are_names_equal() call will have gotten a match but we 270 * still treat it correctly. 271 */ 272 goto found_it; 273 } 274 /* 275 * We have finished with this index without success. Check for the 276 * presence of a child node and if not present return -ENOENT, unless 277 * we have got a matching name cached in name in which case return the 278 * mft reference associated with it. 279 */ 280 if (!(ie->flags & INDEX_ENTRY_NODE)) { 281 if (name) { 282 ntfs_attr_put_search_ctx(ctx); 283 unmap_mft_record(dir_ni); 284 return ntfs_check_mref(name->mref); 285 } 286 ntfs_debug("Entry not found."); 287 err = -ENOENT; 288 goto err_out; 289 } /* Child node present, descend into it. */ 290 291 /* Get the starting vcn of the index_block holding the child node. */ 292 vcn = le64_to_cpup((__le64 *)((u8 *)ie + le16_to_cpu(ie->length) - 8)); 293 294 /* 295 * We are done with the index root and the mft record. Release them, 296 * otherwise we deadlock with read_mapping_folio(). 297 */ 298 ntfs_attr_put_search_ctx(ctx); 299 unmap_mft_record(dir_ni); 300 m = NULL; 301 ctx = NULL; 302 303 ia_vi = ntfs_index_iget(VFS_I(dir_ni), I30, 4); 304 if (IS_ERR(ia_vi)) { 305 err = PTR_ERR(ia_vi); 306 goto err_out; 307 } 308 309 ia_mapping = ia_vi->i_mapping; 310 descend_into_child_node: 311 /* 312 * The child node has to be inside $INDEX_ALLOCATION. A directory that 313 * lacks the attribute gets an empty index inode that is not 314 * non-resident, and reading a folio from it locks the mft record that 315 * is already locked here. 316 */ 317 if (!NInoNonResident(NTFS_I(ia_vi)) || vcn < 0 || 318 (u64)vcn >= (u64)i_size_read(ia_vi) >> dir_ni->itype.index.vcn_size_bits) { 319 ntfs_error(sb, 320 "Child node vcn %lld is outside $INDEX_ALLOCATION in directory inode 0x%llx.", 321 vcn, dir_ni->mft_no); 322 err = -EIO; 323 goto err_out; 324 } 325 /* 326 * Convert vcn to index into the index allocation attribute in units 327 * of PAGE_SIZE and map the page cache page, reading it from 328 * disk if necessary. 329 */ 330 folio = read_mapping_folio(ia_mapping, vcn << 331 dir_ni->itype.index.vcn_size_bits >> PAGE_SHIFT, NULL); 332 if (IS_ERR(folio)) { 333 ntfs_error(sb, "Failed to map directory index page, error %ld.", 334 -PTR_ERR(folio)); 335 err = PTR_ERR(folio); 336 goto err_out; 337 } 338 339 folio_lock(folio); 340 kaddr = kmalloc(PAGE_SIZE, GFP_NOFS); 341 if (!kaddr) { 342 err = -ENOMEM; 343 folio_unlock(folio); 344 folio_put(folio); 345 goto unm_err_out; 346 } 347 348 memcpy_from_folio(kaddr, folio, 0, PAGE_SIZE); 349 folio_unlock(folio); 350 folio_put(folio); 351 fast_descend_into_child_node: 352 /* Get to the index allocation block. */ 353 ia = (struct index_block *)(kaddr + ((vcn << 354 dir_ni->itype.index.vcn_size_bits) & ~PAGE_MASK)); 355 /* Bounds checks. */ 356 if ((u8 *)ia < kaddr || (u8 *)ia > kaddr + PAGE_SIZE) { 357 ntfs_error(sb, 358 "Out of bounds check failed. Corrupt directory inode 0x%llx or driver bug.", 359 dir_ni->mft_no); 360 goto unm_err_out; 361 } 362 index_end = (u8 *)ia + dir_ni->itype.index.block_size; 363 if (index_end > kaddr + PAGE_SIZE) { 364 ntfs_error(sb, 365 "Index buffer (VCN 0x%llx) of directory inode 0x%llx crosses page boundary. Impossible! Cannot access! This is probably a bug in the driver.", 366 vcn, dir_ni->mft_no); 367 goto unm_err_out; 368 } 369 err = post_read_mst_fixup((struct ntfs_record *)ia, 370 dir_ni->itype.index.block_size); 371 if (err) { 372 ntfs_error(sb, 373 "MST fixup failed for index block vcn %lld in directory inode 0x%llx.", 374 vcn, dir_ni->mft_no); 375 goto unm_err_out; 376 } 377 err = ntfs_index_block_inconsistent(vol, ia, 378 dir_ni->itype.index.block_size, 379 vcn, COLLATION_FILE_NAME, 380 dir_ni->mft_no); 381 if (err) 382 goto unm_err_out; 383 index_end = (u8 *)&ia->index + le32_to_cpu(ia->index.index_length); 384 /* The first index entry. */ 385 ie = (struct index_entry *)((u8 *)&ia->index + 386 le32_to_cpu(ia->index.entries_offset)); 387 /* 388 * Iterate similar to above big loop but applied to index buffer, thus 389 * loop until we exceed valid memory (corruption case) or until we 390 * reach the last entry. 391 */ 392 for (;; ie = (struct index_entry *)((u8 *)ie + le16_to_cpu(ie->length))) { 393 /* 394 * The last entry cannot contain a name. It can however contain 395 * a pointer to a child node in the B+tree so we just break out. 396 */ 397 if (ie->flags & INDEX_ENTRY_END) 398 break; 399 /* Key length should not be zero if it is not last entry. */ 400 if (!ie->key_length) 401 goto unm_err_out; 402 /* 403 * We perform a case sensitive comparison and if that matches 404 * we are done and return the mft reference of the inode (i.e. 405 * the inode number together with the sequence number for 406 * consistency checking). We convert it to cpu format before 407 * returning. 408 */ 409 if (ntfs_are_names_equal(uname, uname_len, 410 (__le16 *)&ie->key.file_name.file_name, 411 ie->key.file_name.file_name_length, 412 CASE_SENSITIVE, vol->upcase, vol->upcase_len)) { 413 found_it2: 414 /* 415 * We have a perfect match, so we don't need to care 416 * about having matched imperfectly before, so we can 417 * free name and set *res to NULL. 418 * However, if the perfect match is a short file name, 419 * we need to signal this through *res, so that 420 * ntfs_lookup() can fix dcache aliasing issues. 421 * As an optimization we just reuse an existing 422 * allocation of *res. 423 */ 424 if (ie->key.file_name.file_name_type == FILE_NAME_DOS) { 425 if (!name) { 426 name = kmalloc_obj(struct ntfs_name, 427 GFP_NOFS); 428 if (!name) { 429 err = -ENOMEM; 430 goto unm_err_out; 431 } 432 } 433 name->mref = le64_to_cpu( 434 ie->data.dir.indexed_file); 435 name->type = FILE_NAME_DOS; 436 name->len = 0; 437 *res = name; 438 } else { 439 kfree(name); 440 *res = NULL; 441 } 442 mref = le64_to_cpu(ie->data.dir.indexed_file); 443 kfree(kaddr); 444 iput(ia_vi); 445 return ntfs_check_mref(mref); 446 } 447 /* 448 * For a case insensitive mount, we also perform a case 449 * insensitive comparison (provided the file name is not in the 450 * POSIX namespace). If the comparison matches, and the name is 451 * in the WIN32 namespace, we cache the filename in *res so 452 * that the caller, ntfs_lookup(), can work on it. If the 453 * comparison matches, and the name is in the DOS namespace, we 454 * only cache the mft reference and the file name type (we set 455 * the name length to zero for simplicity). 456 */ 457 if ((!NVolCaseSensitive(vol) || 458 ie->key.file_name.file_name_type == FILE_NAME_DOS) && 459 ntfs_are_names_equal(uname, uname_len, 460 (__le16 *)&ie->key.file_name.file_name, 461 ie->key.file_name.file_name_length, 462 IGNORE_CASE, vol->upcase, 463 vol->upcase_len)) { 464 int name_size = sizeof(struct ntfs_name); 465 u8 type = ie->key.file_name.file_name_type; 466 u8 len = ie->key.file_name.file_name_length; 467 468 /* Only one case insensitive matching name allowed. */ 469 if (name) { 470 ntfs_error(sb, 471 "Found already allocated name in phase 2. Please run chkdsk"); 472 kfree(kaddr); 473 goto dir_err_out; 474 } 475 476 if (type != FILE_NAME_DOS) 477 name_size += len * sizeof(__le16); 478 name = kmalloc(name_size, GFP_NOFS); 479 if (!name) { 480 err = -ENOMEM; 481 goto unm_err_out; 482 } 483 name->mref = le64_to_cpu(ie->data.dir.indexed_file); 484 name->type = type; 485 if (type != FILE_NAME_DOS) { 486 name->len = len; 487 memcpy(name->name, ie->key.file_name.file_name, 488 len * sizeof(__le16)); 489 } else 490 name->len = 0; 491 *res = name; 492 } 493 /* 494 * Not a perfect match, need to do full blown collation so we 495 * know which way in the B+tree we have to go. 496 */ 497 rc = ntfs_collate_names(uname, uname_len, 498 (__le16 *)&ie->key.file_name.file_name, 499 ie->key.file_name.file_name_length, false, 500 IGNORE_CASE, vol->upcase, vol->upcase_len); 501 /* 502 * If uname collates before the name of the current entry, there 503 * is definitely no such name in this index but we might need to 504 * descend into the B+tree so we just break out of the loop. 505 */ 506 if (rc == -1) 507 break; 508 /* The names are not equal, continue the search. */ 509 if (rc) 510 continue; 511 /* 512 * Names match with case insensitive comparison, now try the 513 * case sensitive comparison, which is required for proper 514 * collation. 515 */ 516 rc = ntfs_collate_names(uname, uname_len, 517 (__le16 *)&ie->key.file_name.file_name, 518 ie->key.file_name.file_name_length, false, 519 CASE_SENSITIVE, vol->upcase, vol->upcase_len); 520 if (rc == -1) 521 break; 522 if (rc) 523 continue; 524 /* 525 * Perfect match, this will never happen as the 526 * ntfs_are_names_equal() call will have gotten a match but we 527 * still treat it correctly. 528 */ 529 goto found_it2; 530 } 531 /* 532 * We have finished with this index buffer without success. Check for 533 * the presence of a child node. 534 */ 535 if (ie->flags & INDEX_ENTRY_NODE) { 536 if ((ia->index.flags & NODE_MASK) == LEAF_NODE) { 537 ntfs_error(sb, 538 "Index entry with child node found in a leaf node in directory inode 0x%llx.", 539 dir_ni->mft_no); 540 goto unm_err_out; 541 } 542 /* Child node present, descend into it. */ 543 old_vcn = vcn; 544 vcn = le64_to_cpup((__le64 *)((u8 *)ie + 545 le16_to_cpu(ie->length) - 8)); 546 if (vcn >= 0) { 547 /* 548 * If vcn is in the same page cache page as old_vcn we 549 * recycle the mapped page. 550 */ 551 if (ntfs_cluster_to_pidx(vol, old_vcn) == 552 ntfs_cluster_to_pidx(vol, vcn)) 553 goto fast_descend_into_child_node; 554 kfree(kaddr); 555 kaddr = NULL; 556 goto descend_into_child_node; 557 } 558 ntfs_error(sb, "Negative child node vcn in directory inode 0x%llx.", 559 dir_ni->mft_no); 560 goto unm_err_out; 561 } 562 /* 563 * No child node present, return -ENOENT, unless we have got a matching 564 * name cached in name in which case return the mft reference 565 * associated with it. 566 */ 567 if (name) { 568 kfree(kaddr); 569 iput(ia_vi); 570 return ntfs_check_mref(name->mref); 571 } 572 ntfs_debug("Entry not found."); 573 err = -ENOENT; 574 unm_err_out: 575 kfree(kaddr); 576 err_out: 577 if (!err) 578 err = -EIO; 579 if (ctx) 580 ntfs_attr_put_search_ctx(ctx); 581 if (m) 582 unmap_mft_record(dir_ni); 583 kfree(name); 584 *res = NULL; 585 if (!IS_ERR_OR_NULL(ia_vi)) 586 iput(ia_vi); 587 return ERR_MREF(err); 588 dir_err_out: 589 ntfs_error(sb, "Corrupt directory. Aborting lookup."); 590 goto err_out; 591 } 592 593 /* 594 * ntfs_filldir - ntfs specific filldir method 595 * @vol: current ntfs volume 596 * @ndir: ntfs inode of current directory 597 * @ia_page: page in which the index allocation buffer @ie is in resides 598 * @ie: current index entry 599 * @name: buffer to use for the converted name 600 * @actor: what to feed the entries to 601 * 602 * Convert the Unicode @name to the loaded NLS and pass it to the @filldir 603 * callback. 604 * 605 * If @ia_page is not NULL it is the locked page containing the index 606 * allocation block containing the index entry @ie. 607 * 608 * Note, we drop (and then reacquire) the page lock on @ia_page across the 609 * @filldir() call otherwise we would deadlock with NFSd when it calls ->lookup 610 * since ntfs_lookup() will lock the same page. As an optimization, we do not 611 * retake the lock if we are returning a non-zero value as ntfs_readdir() 612 * would need to drop the lock immediately anyway. 613 */ 614 static inline int ntfs_filldir(struct ntfs_volume *vol, 615 struct ntfs_inode *ndir, struct page *ia_page, struct index_entry *ie, 616 u8 *name, struct dir_context *actor) 617 { 618 unsigned long mref; 619 int name_len; 620 unsigned int dt_type; 621 u8 name_type; 622 623 name_type = ie->key.file_name.file_name_type; 624 if (name_type == FILE_NAME_DOS) { 625 ntfs_debug("Skipping DOS name space entry."); 626 return 0; 627 } 628 if (MREF_LE(ie->data.dir.indexed_file) == FILE_root) { 629 ntfs_debug("Skipping root directory self reference entry."); 630 return 0; 631 } 632 if (MREF_LE(ie->data.dir.indexed_file) < FILE_first_user && 633 !NVolShowSystemFiles(vol)) { 634 ntfs_debug("Skipping system file."); 635 return 0; 636 } 637 if (!NVolShowHiddenFiles(vol) && 638 (ie->key.file_name.file_attributes & FILE_ATTR_HIDDEN)) { 639 ntfs_debug("Skipping hidden file."); 640 return 0; 641 } 642 name_len = ntfs_ucstonls(vol, (__le16 *)&ie->key.file_name.file_name, 643 ie->key.file_name.file_name_length, &name, 644 NTFS_MAX_NAME_LEN * NLS_MAX_CHARSET_SIZE + 1); 645 if (name_len <= 0) { 646 ntfs_warning(vol->sb, "Skipping unrepresentable inode 0x%llx.", 647 (long long)MREF_LE(ie->data.dir.indexed_file)); 648 return 0; 649 } 650 if (NVolStreamsWindows(vol) && strchr((char *)name, ':')) { 651 ntfs_debug("Skipping file name containing a stream separator."); 652 return 0; 653 } 654 mref = MREF_LE(ie->data.dir.indexed_file); 655 if (ie->key.file_name.file_attributes & FILE_ATTR_REPARSE_POINT) 656 dt_type = ntfs_reparse_tag_dt_types(vol, mref); 657 else if (ie->key.file_name.file_attributes & 658 FILE_ATTR_DUP_FILE_NAME_INDEX_PRESENT) 659 dt_type = DT_DIR; 660 else 661 dt_type = DT_REG; 662 663 /* 664 * Drop the page lock otherwise we deadlock with NFS when it calls 665 * ->lookup since ntfs_lookup() will lock the same page. 666 */ 667 if (ia_page) 668 unlock_page(ia_page); 669 ntfs_debug("Calling filldir for %s with len %i, fpos 0x%llx, inode 0x%lx, DT_%s.", 670 name, name_len, actor->pos, mref, dt_type == DT_DIR ? "DIR" : "REG"); 671 if (!dir_emit(actor, name, name_len, mref, dt_type)) 672 return 1; 673 /* Relock the page but not if we are aborting ->readdir. */ 674 if (ia_page) 675 lock_page(ia_page); 676 return 0; 677 } 678 679 struct ntfs_file_private { 680 void *key; 681 __le16 key_length; 682 bool end_in_iterate; 683 loff_t curr_pos; 684 }; 685 686 struct ntfs_index_ra { 687 unsigned long start_index; 688 unsigned int count; 689 struct rb_node rb_node; 690 }; 691 692 static void ntfs_insert_rb(struct ntfs_index_ra *nir, struct rb_root *root) 693 { 694 struct rb_node **new = &root->rb_node, *parent = NULL; 695 struct ntfs_index_ra *cnir; 696 697 while (*new) { 698 parent = *new; 699 cnir = rb_entry(parent, struct ntfs_index_ra, rb_node); 700 if (nir->start_index < cnir->start_index) 701 new = &parent->rb_left; 702 else if (nir->start_index >= cnir->start_index + cnir->count) 703 new = &parent->rb_right; 704 else { 705 pr_err("nir start index : %ld, count : %d, cnir start_index : %ld, count : %d\n", 706 nir->start_index, nir->count, cnir->start_index, cnir->count); 707 return; 708 } 709 } 710 711 rb_link_node(&nir->rb_node, parent, new); 712 rb_insert_color(&nir->rb_node, root); 713 } 714 715 static int ntfs_ia_blocks_readahead(struct ntfs_inode *ia_ni, loff_t pos) 716 { 717 unsigned long dir_start_index, dir_end_index; 718 struct inode *ia_vi = VFS_I(ia_ni); 719 struct file_ra_state *dir_ra; 720 721 dir_end_index = (i_size_read(ia_vi) + PAGE_SIZE - 1) >> PAGE_SHIFT; 722 dir_start_index = (pos + PAGE_SIZE - 1) >> PAGE_SHIFT; 723 724 if (dir_start_index >= dir_end_index) 725 return 0; 726 727 dir_ra = kzalloc_obj(*dir_ra, GFP_NOFS); 728 if (!dir_ra) 729 return -ENOMEM; 730 731 file_ra_state_init(dir_ra, ia_vi->i_mapping); 732 dir_end_index = (i_size_read(ia_vi) + PAGE_SIZE - 1) >> PAGE_SHIFT; 733 dir_start_index = (pos + PAGE_SIZE - 1) >> PAGE_SHIFT; 734 dir_ra->ra_pages = dir_end_index - dir_start_index; 735 page_cache_sync_readahead(ia_vi->i_mapping, dir_ra, NULL, 736 dir_start_index, dir_end_index - dir_start_index); 737 kfree(dir_ra); 738 739 return 0; 740 } 741 742 static int ntfs_readdir(struct file *file, struct dir_context *actor) 743 { 744 struct inode *vdir = file_inode(file); 745 struct super_block *sb = vdir->i_sb; 746 struct ntfs_inode *ndir = NTFS_I(vdir); 747 struct ntfs_volume *vol = NTFS_SB(sb); 748 struct ntfs_attr_search_ctx *ctx = NULL; 749 struct ntfs_index_context *ictx = NULL; 750 u8 *name; 751 struct index_root *ir; 752 struct index_entry *next = NULL; 753 struct ntfs_file_private *private = NULL; 754 int err = 0; 755 loff_t ie_pos = 2; /* initialize it with dot and dotdot size */ 756 struct ntfs_index_ra *nir = NULL; 757 unsigned long index; 758 struct rb_root ra_root = RB_ROOT; 759 struct file_ra_state *ra; 760 761 ntfs_debug("Entering for inode 0x%llx, fpos 0x%llx.", 762 ndir->mft_no, actor->pos); 763 764 if (file->private_data) { 765 private = file->private_data; 766 767 if (actor->pos != private->curr_pos) { 768 /* 769 * If actor->pos is different from the previous passed 770 * one, Discard the private->key and fill dirent buffer 771 * with linear lookup. 772 */ 773 kfree(private->key); 774 private->key = NULL; 775 private->end_in_iterate = false; 776 } else if (private->end_in_iterate) { 777 kfree(private->key); 778 kfree(file->private_data); 779 file->private_data = NULL; 780 return 0; 781 } 782 } 783 784 /* Emulate . and .. for all directories. */ 785 if (!dir_emit_dots(file, actor)) 786 return 0; 787 788 /* 789 * Allocate a buffer to store the current name being processed 790 * converted to format determined by current NLS. 791 */ 792 name = kmalloc(NTFS_MAX_NAME_LEN * NLS_MAX_CHARSET_SIZE + 1, GFP_NOFS); 793 if (unlikely(!name)) 794 return -ENOMEM; 795 796 mutex_lock_nested(&ndir->mrec_lock, NTFS_INODE_MUTEX_PARENT); 797 ictx = ntfs_index_ctx_get(ndir, I30, 4); 798 if (!ictx) { 799 kfree(name); 800 mutex_unlock(&ndir->mrec_lock); 801 return -ENOMEM; 802 } 803 804 ra = kzalloc_obj(struct file_ra_state, GFP_NOFS); 805 if (!ra) { 806 kfree(name); 807 ntfs_index_ctx_put(ictx); 808 mutex_unlock(&ndir->mrec_lock); 809 return -ENOMEM; 810 } 811 file_ra_state_init(ra, vol->mft_ino->i_mapping); 812 813 if (private && private->key) { 814 /* 815 * Find index witk private->key using ntfs_index_lookup() 816 * instead of linear index lookup. 817 */ 818 err = ntfs_index_lookup(private->key, 819 le16_to_cpu(private->key_length), 820 ictx); 821 if (!err) { 822 next = ictx->entry; 823 /* 824 * Update ie_pos with private->curr_pos 825 * to make next d_off of dirent correct. 826 */ 827 ie_pos = private->curr_pos; 828 829 if (actor->pos > vol->mft_record_size && ictx->ia_ni) { 830 err = ntfs_ia_blocks_readahead(ictx->ia_ni, actor->pos); 831 if (err) 832 goto out; 833 } 834 835 goto nextdir; 836 } else { 837 goto out; 838 } 839 } else if (!private) { 840 private = kzalloc_obj(struct ntfs_file_private); 841 if (!private) { 842 err = -ENOMEM; 843 goto out; 844 } 845 file->private_data = private; 846 } 847 848 ctx = ntfs_attr_get_search_ctx(ndir, NULL); 849 if (!ctx) { 850 err = -ENOMEM; 851 goto out; 852 } 853 854 /* Find the index root attribute in the mft record. */ 855 if (ntfs_attr_lookup(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 0, NULL, 0, 856 ctx)) { 857 ntfs_error(sb, "Index root attribute missing in directory inode %llu", 858 ndir->mft_no); 859 ntfs_attr_put_search_ctx(ctx); 860 err = -ENOMEM; 861 goto out; 862 } 863 864 /* Get to the index root value. */ 865 ir = (struct index_root *)((u8 *)ctx->attr + 866 le16_to_cpu(ctx->attr->data.resident.value_offset)); 867 868 ictx->ir = ir; 869 ictx->actx = ctx; 870 ictx->parent_vcn[ictx->pindex] = VCN_INDEX_ROOT_PARENT; 871 ictx->is_in_root = true; 872 ictx->parent_pos[ictx->pindex] = 0; 873 874 ictx->block_size = le32_to_cpu(ir->index_block_size); 875 if (ictx->block_size < NTFS_BLOCK_SIZE) { 876 ntfs_error(sb, "Index block size (%d) is smaller than the sector size (%d)", 877 ictx->block_size, NTFS_BLOCK_SIZE); 878 err = -EIO; 879 goto out; 880 } 881 882 if (vol->cluster_size <= ictx->block_size) 883 ictx->vcn_size_bits = vol->cluster_size_bits; 884 else 885 ictx->vcn_size_bits = NTFS_BLOCK_SIZE_BITS; 886 ictx->cr = ir->collation_rule; 887 888 /* The first index entry. */ 889 next = (struct index_entry *)((u8 *)&ir->index + 890 le32_to_cpu(ir->index.entries_offset)); 891 892 if (next->flags & INDEX_ENTRY_NODE) { 893 ictx->ia_ni = ntfs_ia_open(ictx, ictx->idx_ni); 894 if (!ictx->ia_ni) { 895 err = -EINVAL; 896 goto out; 897 } 898 899 err = ntfs_ia_blocks_readahead(ictx->ia_ni, actor->pos); 900 if (err) 901 goto out; 902 } 903 904 if (next->flags & INDEX_ENTRY_NODE) { 905 next = ntfs_index_walk_down(next, ictx); 906 if (IS_ERR(next)) { 907 err = PTR_ERR(next); 908 goto out; 909 } 910 } 911 912 if (next && !(next->flags & INDEX_ENTRY_END)) 913 goto nextdir; 914 915 while (1) { 916 next = ntfs_index_next(next, ictx); 917 if (IS_ERR(next)) { 918 err = PTR_ERR(next); 919 goto out; 920 } 921 if (!next) 922 break; 923 nextdir: 924 if (ie_pos < actor->pos) { 925 ie_pos += le16_to_cpu(next->length); 926 continue; 927 } 928 929 actor->pos = ie_pos; 930 931 index = ntfs_mft_no_to_pidx(vol, 932 MREF_LE(next->data.dir.indexed_file)); 933 if (nir) { 934 struct ntfs_index_ra *cnir; 935 struct rb_node *node = ra_root.rb_node; 936 937 if (nir->start_index <= index && 938 index < nir->start_index + nir->count) { 939 /* No behavior */ 940 goto filldir; 941 } 942 943 while (node) { 944 cnir = rb_entry(node, struct ntfs_index_ra, rb_node); 945 if (cnir->start_index <= index && 946 index < cnir->start_index + cnir->count) { 947 goto filldir; 948 } else if (cnir->start_index + cnir->count == index) { 949 cnir->count++; 950 goto filldir; 951 } else if (!cnir->start_index && cnir->start_index - 1 == index) { 952 cnir->start_index = index; 953 goto filldir; 954 } 955 956 if (index < cnir->start_index) 957 node = node->rb_left; 958 else if (index >= cnir->start_index + cnir->count) 959 node = node->rb_right; 960 } 961 962 if (nir->start_index + nir->count == index) { 963 nir->count++; 964 } else if (!nir->start_index && nir->start_index - 1 == index) { 965 nir->start_index = index; 966 } else if (nir->count > 2) { 967 ntfs_insert_rb(nir, &ra_root); 968 nir = NULL; 969 } else { 970 nir->start_index = index; 971 nir->count = 1; 972 } 973 } 974 975 if (!nir) { 976 nir = kzalloc_obj(struct ntfs_index_ra); 977 if (nir) { 978 nir->start_index = index; 979 nir->count = 1; 980 } 981 } 982 983 filldir: 984 /* Submit the name to the filldir callback. */ 985 err = ntfs_filldir(vol, ndir, NULL, next, name, actor); 986 if (err) { 987 /* 988 * Store index key value to file private_data to start 989 * from current index offset on next round. 990 */ 991 private = file->private_data; 992 kfree(private->key); 993 private->key = kmemdup(&next->key.file_name, 994 le16_to_cpu(next->key_length), 995 GFP_KERNEL); 996 if (!private->key) { 997 err = -ENOMEM; 998 goto out; 999 } 1000 1001 private->key_length = next->key_length; 1002 break; 1003 } 1004 ie_pos += le16_to_cpu(next->length); 1005 } 1006 1007 if (!err) 1008 private->end_in_iterate = true; 1009 else 1010 err = 0; 1011 1012 private->curr_pos = actor->pos = ie_pos; 1013 out: 1014 while (!RB_EMPTY_ROOT(&ra_root)) { 1015 struct ntfs_index_ra *cnir; 1016 struct rb_node *node; 1017 1018 node = rb_first(&ra_root); 1019 cnir = rb_entry(node, struct ntfs_index_ra, rb_node); 1020 ra->ra_pages = cnir->count; 1021 page_cache_sync_readahead(vol->mft_ino->i_mapping, ra, NULL, 1022 cnir->start_index, cnir->count); 1023 rb_erase(node, &ra_root); 1024 kfree(cnir); 1025 } 1026 1027 if (err) { 1028 if (private) { 1029 private->curr_pos = actor->pos; 1030 private->end_in_iterate = true; 1031 } 1032 err = 0; 1033 } 1034 ntfs_index_ctx_put(ictx); 1035 kfree(name); 1036 kfree(nir); 1037 kfree(ra); 1038 mutex_unlock(&ndir->mrec_lock); 1039 return err; 1040 } 1041 1042 int ntfs_check_empty_dir(struct ntfs_inode *ni, struct mft_record *ni_mrec) 1043 { 1044 struct ntfs_attr_search_ctx *ctx; 1045 int ret = 0; 1046 1047 if (!(ni_mrec->flags & MFT_RECORD_IS_DIRECTORY)) 1048 return 0; 1049 1050 ctx = ntfs_attr_get_search_ctx(ni, NULL); 1051 if (!ctx) { 1052 ntfs_error(ni->vol->sb, "Failed to get search context"); 1053 return -ENOMEM; 1054 } 1055 1056 /* Find the index root attribute in the mft record. */ 1057 ret = ntfs_attr_lookup(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 0, NULL, 1058 0, ctx); 1059 if (ret) { 1060 ntfs_error(ni->vol->sb, "Index root attribute missing in directory inode %llu", 1061 ni->mft_no); 1062 ntfs_attr_put_search_ctx(ctx); 1063 return ret; 1064 } 1065 1066 /* Non-empty directory? */ 1067 if (le32_to_cpu(ctx->attr->data.resident.value_length) != 1068 sizeof(struct index_root) + sizeof(struct index_entry_header)) { 1069 /* Both ENOTEMPTY and EEXIST are ok. We use the more common. */ 1070 ret = -ENOTEMPTY; 1071 ntfs_debug("Directory is not empty\n"); 1072 } 1073 1074 ntfs_attr_put_search_ctx(ctx); 1075 1076 return ret; 1077 } 1078 1079 /* 1080 * ntfs_dir_open - called when an inode is about to be opened 1081 * @vi: inode to be opened 1082 * @filp: file structure describing the inode 1083 * 1084 * Limit directory size to the page cache limit on architectures where unsigned 1085 * long is 32-bits. This is the most we can do for now without overflowing the 1086 * page cache page index. Doing it this way means we don't run into problems 1087 * because of existing too large directories. It would be better to allow the 1088 * user to read the accessible part of the directory but I doubt very much 1089 * anyone is going to hit this check on a 32-bit architecture, so there is no 1090 * point in adding the extra complexity required to support this. 1091 * 1092 * On 64-bit architectures, the check is hopefully optimized away by the 1093 * compiler. 1094 */ 1095 static int ntfs_dir_open(struct inode *vi, struct file *filp) 1096 { 1097 if (sizeof(unsigned long) < 8) { 1098 if (i_size_read(vi) > MAX_LFS_FILESIZE) 1099 return -EFBIG; 1100 } 1101 return 0; 1102 } 1103 1104 static int ntfs_dir_release(struct inode *vi, struct file *filp) 1105 { 1106 if (filp->private_data) { 1107 kfree(((struct ntfs_file_private *)filp->private_data)->key); 1108 kfree(filp->private_data); 1109 filp->private_data = NULL; 1110 } 1111 return 0; 1112 } 1113 1114 /* 1115 * ntfs_dir_fsync - sync a directory to disk 1116 * @filp: file describing the directory to be synced 1117 * @start: start offset to be synced 1118 * @end: end offset to be synced 1119 * @datasync: if non-zero only flush user data and not metadata 1120 * 1121 * Data integrity sync of a directory to disk. Used for fsync, fdatasync, and 1122 * msync system calls. This function is based on file.c::ntfs_file_fsync(). 1123 * 1124 * Write the mft record and all associated extent mft records as well as the 1125 * $INDEX_ALLOCATION and $BITMAP attributes and then sync the block device. 1126 * 1127 * If @datasync is true, we do not wait on the inode(s) to be written out 1128 * but we always wait on the page cache pages to be written out. 1129 * 1130 * Note: In the past @filp could be NULL so we ignore it as we don't need it 1131 * anyway. 1132 * 1133 * Locking: Caller must hold i_mutex on the inode. 1134 */ 1135 static int ntfs_dir_fsync(struct file *filp, loff_t start, loff_t end, 1136 int datasync) 1137 { 1138 struct inode *bmp_vi, *vi = filp->f_mapping->host; 1139 struct ntfs_volume *vol = NTFS_I(vi)->vol; 1140 struct ntfs_inode *ni = NTFS_I(vi); 1141 struct ntfs_attr_search_ctx *ctx; 1142 struct inode *parent_vi, *ia_vi; 1143 int err, ret; 1144 struct ntfs_attr na; 1145 1146 ntfs_debug("Entering for inode 0x%llx.", ni->mft_no); 1147 1148 if (NVolShutdown(vol)) 1149 return -EIO; 1150 1151 ctx = ntfs_attr_get_search_ctx(ni, NULL); 1152 if (!ctx) 1153 return -ENOMEM; 1154 1155 mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL_CHILD); 1156 while (!(err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, 0, 0, NULL, 0, ctx))) { 1157 struct file_name_attr *fn = (struct file_name_attr *)((u8 *)ctx->attr + 1158 le16_to_cpu(ctx->attr->data.resident.value_offset)); 1159 1160 if (MREF_LE(fn->parent_directory) == ni->mft_no) 1161 continue; 1162 1163 parent_vi = ntfs_iget(vi->i_sb, MREF_LE(fn->parent_directory)); 1164 if (IS_ERR(parent_vi)) 1165 continue; 1166 mutex_lock_nested(&NTFS_I(parent_vi)->mrec_lock, NTFS_INODE_MUTEX_NORMAL); 1167 ia_vi = ntfs_index_iget(parent_vi, I30, 4); 1168 mutex_unlock(&NTFS_I(parent_vi)->mrec_lock); 1169 if (IS_ERR(ia_vi)) { 1170 iput(parent_vi); 1171 continue; 1172 } 1173 write_inode_now(ia_vi, 1); 1174 iput(ia_vi); 1175 write_inode_now(parent_vi, 1); 1176 iput(parent_vi); 1177 } 1178 mutex_unlock(&ni->mrec_lock); 1179 ntfs_attr_put_search_ctx(ctx); 1180 1181 err = file_write_and_wait_range(filp, start, end); 1182 if (err) 1183 return err; 1184 inode_lock(vi); 1185 1186 /* If the bitmap attribute inode is in memory sync it, too. */ 1187 na.mft_no = vi->i_ino; 1188 na.type = AT_BITMAP; 1189 na.name = I30; 1190 na.name_len = 4; 1191 bmp_vi = ilookup5(vi->i_sb, vi->i_ino, ntfs_test_inode, &na); 1192 if (bmp_vi) { 1193 write_inode_now(bmp_vi, !datasync); 1194 iput(bmp_vi); 1195 } 1196 ret = __ntfs_write_inode(vi, 1); 1197 1198 write_inode_now(vi, !datasync); 1199 1200 write_inode_now(vol->mftbmp_ino, 1); 1201 down_write(&vol->lcnbmp_lock); 1202 write_inode_now(vol->lcnbmp_ino, 1); 1203 up_write(&vol->lcnbmp_lock); 1204 write_inode_now(vol->mft_ino, 1); 1205 1206 err = sync_blockdev(vi->i_sb->s_bdev); 1207 if (unlikely(err && !ret)) 1208 ret = err; 1209 if (!ret) 1210 ret = blkdev_issue_flush(vi->i_sb->s_bdev); 1211 if (likely(!ret)) 1212 ntfs_debug("Done."); 1213 else 1214 ntfs_warning(vi->i_sb, 1215 "Failed to f%ssync inode 0x%llx. Error %u.", 1216 datasync ? "data" : "", ni->mft_no, -ret); 1217 inode_unlock(vi); 1218 return ret; 1219 } 1220 1221 const struct file_operations ntfs_dir_ops = { 1222 .llseek = generic_file_llseek, /* Seek inside directory. */ 1223 .read = generic_read_dir, /* Return -EISDIR. */ 1224 .iterate_shared = ntfs_readdir, /* Read directory contents. */ 1225 .fsync = ntfs_dir_fsync, /* Sync a directory to disk. */ 1226 .open = ntfs_dir_open, /* Open directory. */ 1227 .release = ntfs_dir_release, 1228 .unlocked_ioctl = ntfs_ioctl, 1229 #ifdef CONFIG_COMPAT 1230 .compat_ioctl = ntfs_compat_ioctl, 1231 #endif 1232 .setlease = generic_setlease, 1233 }; 1234