1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * NTFS kernel inode handling. 4 * 5 * Copyright (c) 2001-2014 Anton Altaparmakov and Tuxera Inc. 6 * Copyright (c) 2025 LG Electronics Co., Ltd. 7 */ 8 9 #include <linux/writeback.h> 10 #include <linux/seq_file.h> 11 #include <linux/mm.h> 12 13 #include "lcnalloc.h" 14 #include "time.h" 15 #include "ntfs.h" 16 #include "index.h" 17 #include "attrlist.h" 18 #include "reparse.h" 19 #include "ea.h" 20 #include "attrib.h" 21 #include "iomap.h" 22 #include "object_id.h" 23 #include "stream.h" 24 25 /* 26 * ntfs_test_inode - compare two (possibly fake) inodes for equality 27 * @vi: vfs inode which to test 28 * @data: data which is being tested with 29 * 30 * Compare the ntfs attribute embedded in the ntfs specific part of the vfs 31 * inode @vi for equality with the ntfs attribute @data. 32 * 33 * If searching for the normal file/directory inode, set @na->type to AT_UNUSED. 34 * @na->name and @na->name_len are then ignored. 35 * 36 * Return 1 if the attributes match and 0 if not. 37 * 38 * NOTE: This function runs with the inode_hash_lock spin lock held so it is not 39 * allowed to sleep. 40 */ 41 int ntfs_test_inode(struct inode *vi, void *data) 42 { 43 struct ntfs_attr *na = data; 44 struct ntfs_inode *ni = NTFS_I(vi); 45 46 if (vi->i_ino != na->mft_no) 47 return 0; 48 49 /* If !NInoAttr(ni), @vi is a normal file or directory inode. */ 50 if (likely(!NInoAttr(ni))) { 51 /* If not looking for a normal inode this is a mismatch. */ 52 if (unlikely(na->type != AT_UNUSED)) 53 return 0; 54 } else { 55 /* A fake inode describing an attribute. */ 56 if (ni->type != na->type) 57 return 0; 58 if (ni->name_len != na->name_len) 59 return 0; 60 if (na->name_len) { 61 if (ntfs_inode_is_named_stream(ni)) { 62 if (!ntfs_names_are_equal(ni->name, ni->name_len, 63 na->name, na->name_len, 64 IGNORE_CASE, ni->vol->upcase, 65 ni->vol->upcase_len)) 66 return 0; 67 } else if (memcmp(ni->name, na->name, 68 na->name_len * sizeof(__le16))) { 69 return 0; 70 } 71 } 72 if (!ni->ext.base_ntfs_ino) 73 return 0; 74 } 75 76 /* Match! */ 77 return 1; 78 } 79 80 /* 81 * ntfs_test_inode_rcu() - Test an inode during RCU hash lookup 82 * @vi: inode being tested 83 * @data: NTFS attribute key to match 84 * 85 * Reject inodes being initialized or destroyed, then run the normal NTFS 86 * inode match while holding @vi's inode lock. 87 * 88 * Return: 1 if the inode matches, or 0 otherwise. 89 */ 90 int ntfs_test_inode_rcu(struct inode *vi, void *data) 91 { 92 unsigned long state; 93 int ret; 94 95 spin_lock(&vi->i_lock); 96 state = inode_state_read_once(vi); 97 if (state & (I_FREEING | I_WILL_FREE | I_NEW)) { 98 ret = 0; 99 goto out; 100 } 101 ret = ntfs_test_inode(vi, data); 102 out: 103 spin_unlock(&vi->i_lock); 104 return ret; 105 } 106 107 /* 108 * ntfs_init_locked_inode - initialize an inode 109 * @vi: vfs inode to initialize 110 * @data: data which to initialize @vi to 111 * 112 * Initialize the vfs inode @vi with the values from the ntfs attribute @data in 113 * order to enable ntfs_test_inode() to do its work. 114 * 115 * If initializing the normal file/directory inode, set @na->type to AT_UNUSED. 116 * In that case, @na->name and @na->name_len should be set to NULL and 0, 117 * respectively. Although that is not strictly necessary as 118 * ntfs_read_locked_inode() will fill them in later. 119 * 120 * Return 0 on success and error. 121 * 122 * NOTE: This function runs with the inode->i_lock spin lock held so it is not 123 * allowed to sleep. (Hence the GFP_ATOMIC allocation.) 124 */ 125 static int ntfs_init_locked_inode(struct inode *vi, void *data) 126 { 127 struct ntfs_attr *na = data; 128 struct ntfs_inode *ni = NTFS_I(vi); 129 130 vi->i_ino = (unsigned long)na->mft_no; 131 132 if (na->type == AT_INDEX_ALLOCATION) 133 NInoSetMstProtected(ni); 134 else 135 ni->type = na->type; 136 137 ni->name = na->name; 138 ni->name_len = na->name_len; 139 ni->folio = NULL; 140 atomic_set(&ni->count, 1); 141 142 /* If initializing a normal inode, we are done. */ 143 if (likely(na->type == AT_UNUSED)) 144 return 0; 145 146 /* It is a fake inode. */ 147 NInoSetAttr(ni); 148 149 /* 150 * We have I30 global constant as an optimization as it is the name 151 * in >99.9% of named attributes! The other <0.1% incur a GFP_ATOMIC 152 * allocation but that is ok. And most attributes are unnamed anyway, 153 * thus the fraction of named attributes with name != I30 is actually 154 * absolutely tiny. 155 */ 156 if (na->name_len && na->name != I30) { 157 unsigned int i; 158 159 i = na->name_len * sizeof(__le16); 160 ni->name = kmalloc(i + sizeof(__le16), GFP_ATOMIC); 161 if (!ni->name) 162 return -ENOMEM; 163 memcpy(ni->name, na->name, i); 164 ni->name[na->name_len] = 0; 165 } 166 return 0; 167 } 168 169 static int ntfs_read_locked_inode(struct inode *vi); 170 static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi); 171 static int ntfs_read_locked_index_inode(struct inode *base_vi, 172 struct inode *vi); 173 174 /* 175 * ntfs_iget - obtain a struct inode corresponding to a specific normal inode 176 * @sb: super block of mounted volume 177 * @mft_no: mft record number / inode number to obtain 178 * 179 * Obtain the struct inode corresponding to a specific normal inode (i.e. a 180 * file or directory). 181 * 182 * If the inode is in the cache, it is just returned with an increased 183 * reference count. Otherwise, a new struct inode is allocated and initialized, 184 * and finally ntfs_read_locked_inode() is called to read in the inode and 185 * fill in the remainder of the inode structure. 186 * 187 * Return the struct inode on success. Check the return value with IS_ERR() and 188 * if true, the function failed and the error code is obtained from PTR_ERR(). 189 */ 190 struct inode *ntfs_iget(struct super_block *sb, u64 mft_no) 191 { 192 struct inode *vi; 193 int err; 194 struct ntfs_attr na; 195 196 na.mft_no = mft_no; 197 na.type = AT_UNUSED; 198 na.name = NULL; 199 na.name_len = 0; 200 201 vi = iget5_locked(sb, mft_no, ntfs_test_inode, 202 ntfs_init_locked_inode, &na); 203 if (unlikely(!vi)) 204 return ERR_PTR(-ENOMEM); 205 206 err = 0; 207 208 /* If this is a freshly allocated inode, need to read it now. */ 209 if (inode_state_read_once(vi) & I_NEW) { 210 err = ntfs_read_locked_inode(vi); 211 if (err) { 212 remove_inode_hash(vi); 213 discard_new_inode(vi); 214 } else 215 unlock_new_inode(vi); 216 } 217 /* 218 * There is no point in keeping bad inodes around. This also 219 * simplifies things in that we never need to check for bad inodes 220 * elsewhere. 221 */ 222 if (unlikely(err)) 223 vi = ERR_PTR(err); 224 return vi; 225 } 226 227 /* 228 * ntfs_attr_iget - obtain a struct inode corresponding to an attribute 229 * @base_vi: vfs base inode containing the attribute 230 * @type: attribute type 231 * @name: Unicode name of the attribute (NULL if unnamed) 232 * @name_len: length of @name in Unicode characters (0 if unnamed) 233 * 234 * Obtain the (fake) struct inode corresponding to the attribute specified by 235 * @type, @name, and @name_len, which is present in the base mft record 236 * specified by the vfs inode @base_vi. 237 * 238 * If the attribute inode is in the cache, it is just returned with an 239 * increased reference count. Otherwise, a new struct inode is allocated and 240 * initialized, and finally ntfs_read_locked_attr_inode() is called to read the 241 * attribute and fill in the inode structure. 242 * 243 * Note, for index allocation attributes, you need to use ntfs_index_iget() 244 * instead of ntfs_attr_iget() as working with indices is a lot more complex. 245 * 246 * Return the struct inode of the attribute inode on success. Check the return 247 * value with IS_ERR() and if true, the function failed and the error code is 248 * obtained from PTR_ERR(). 249 */ 250 struct inode *ntfs_attr_iget(struct inode *base_vi, __le32 type, 251 __le16 *name, u32 name_len) 252 { 253 struct inode *vi; 254 int err; 255 struct ntfs_attr na; 256 257 /* Make sure no one calls ntfs_attr_iget() for indices. */ 258 WARN_ON(type == AT_INDEX_ALLOCATION); 259 260 na.mft_no = base_vi->i_ino; 261 na.type = type; 262 na.name = name; 263 na.name_len = name_len; 264 265 vi = iget5_locked(base_vi->i_sb, na.mft_no, ntfs_test_inode, 266 ntfs_init_locked_inode, &na); 267 if (unlikely(!vi)) 268 return ERR_PTR(-ENOMEM); 269 err = 0; 270 271 /* If this is a freshly allocated inode, need to read it now. */ 272 if (inode_state_read_once(vi) & I_NEW) { 273 err = ntfs_read_locked_attr_inode(base_vi, vi); 274 if (err) { 275 remove_inode_hash(vi); 276 discard_new_inode(vi); 277 } else 278 unlock_new_inode(vi); 279 } 280 /* 281 * There is no point in keeping bad attribute inodes around. This also 282 * simplifies things in that we never need to check for bad attribute 283 * inodes elsewhere. 284 */ 285 if (unlikely(err)) 286 vi = ERR_PTR(err); 287 return vi; 288 } 289 290 /* 291 * ntfs_index_iget - obtain a struct inode corresponding to an index 292 * @base_vi: vfs base inode containing the index related attributes 293 * @name: Unicode name of the index 294 * @name_len: length of @name in Unicode characters 295 * 296 * Obtain the (fake) struct inode corresponding to the index specified by @name 297 * and @name_len, which is present in the base mft record specified by the vfs 298 * inode @base_vi. 299 * 300 * If the index inode is in the cache, it is just returned with an increased 301 * reference count. Otherwise, a new struct inode is allocated and 302 * initialized, and finally ntfs_read_locked_index_inode() is called to read 303 * the index related attributes and fill in the inode structure. 304 * 305 * Return the struct inode of the index inode on success. Check the return 306 * value with IS_ERR() and if true, the function failed and the error code is 307 * obtained from PTR_ERR(). 308 */ 309 struct inode *ntfs_index_iget(struct inode *base_vi, __le16 *name, 310 u32 name_len) 311 { 312 struct inode *vi; 313 int err; 314 struct ntfs_attr na; 315 316 na.mft_no = base_vi->i_ino; 317 na.type = AT_INDEX_ALLOCATION; 318 na.name = name; 319 na.name_len = name_len; 320 321 vi = iget5_locked(base_vi->i_sb, na.mft_no, ntfs_test_inode, 322 ntfs_init_locked_inode, &na); 323 if (unlikely(!vi)) 324 return ERR_PTR(-ENOMEM); 325 326 err = 0; 327 328 /* If this is a freshly allocated inode, need to read it now. */ 329 if (inode_state_read_once(vi) & I_NEW) { 330 err = ntfs_read_locked_index_inode(base_vi, vi); 331 if (err) { 332 remove_inode_hash(vi); 333 discard_new_inode(vi); 334 } else 335 unlock_new_inode(vi); 336 } 337 /* 338 * There is no point in keeping bad index inodes around. This also 339 * simplifies things in that we never need to check for bad index 340 * inodes elsewhere. 341 */ 342 if (unlikely(err)) 343 vi = ERR_PTR(err); 344 return vi; 345 } 346 347 struct inode *ntfs_alloc_big_inode(struct super_block *sb) 348 { 349 struct ntfs_inode *ni; 350 351 ntfs_debug("Entering."); 352 ni = alloc_inode_sb(sb, ntfs_big_inode_cache, GFP_NOFS); 353 if (likely(ni != NULL)) { 354 ni->state = 0; 355 ni->type = 0; 356 ni->mft_no = 0; 357 return VFS_I(ni); 358 } 359 ntfs_error(sb, "Allocation of NTFS big inode structure failed."); 360 return NULL; 361 } 362 363 void ntfs_free_big_inode(struct inode *inode) 364 { 365 kmem_cache_free(ntfs_big_inode_cache, NTFS_I(inode)); 366 } 367 368 static int ntfs_non_resident_dealloc_clusters(struct ntfs_inode *ni) 369 { 370 struct super_block *sb = ni->vol->sb; 371 struct ntfs_attr_search_ctx *actx; 372 int err = 0; 373 374 actx = ntfs_attr_get_search_ctx(ni, NULL); 375 if (!actx) 376 return -ENOMEM; 377 WARN_ON(actx->mrec->link_count != 0); 378 379 /** 380 * ntfs_truncate_vfs cannot be called in evict() context due 381 * to some limitations, which are the @ni vfs inode is marked 382 * with I_FREEING, and etc. 383 */ 384 if (NInoRunlistDirty(ni)) { 385 err = ntfs_cluster_free_from_rl(ni->vol, ni->runlist.rl); 386 if (err) 387 ntfs_error(sb, 388 "Failed to free clusters. Leaving inconsistent metadata.\n"); 389 } 390 391 while ((err = ntfs_attrs_walk(actx)) == 0) { 392 if (actx->attr->non_resident && 393 (!NInoRunlistDirty(ni) || actx->attr->type != AT_DATA)) { 394 struct runlist_element *rl; 395 size_t new_rl_count; 396 397 rl = ntfs_mapping_pairs_decompress(ni->vol, actx->attr, NULL, 398 &new_rl_count); 399 if (IS_ERR(rl)) { 400 err = PTR_ERR(rl); 401 ntfs_error(sb, 402 "Failed to decompress runlist. Leaving inconsistent metadata.\n"); 403 continue; 404 } 405 406 err = ntfs_cluster_free_from_rl(ni->vol, rl); 407 if (err) 408 ntfs_error(sb, 409 "Failed to free attribute clusters. Leaving inconsistent metadata.\n"); 410 kvfree(rl); 411 } 412 } 413 414 ntfs_release_dirty_clusters(ni->vol, ni->i_dealloc_clusters); 415 ntfs_attr_put_search_ctx(actx); 416 return err; 417 } 418 419 int ntfs_drop_big_inode(struct inode *inode) 420 { 421 struct ntfs_inode *ni = NTFS_I(inode); 422 423 if (!inode_unhashed(inode) && inode_state_read_once(inode) & I_SYNC) { 424 if (ni->type == AT_DATA || ni->type == AT_INDEX_ALLOCATION) { 425 if (!inode->i_nlink) { 426 struct ntfs_inode *ni = NTFS_I(inode); 427 428 if (ni->data_size == 0) 429 return 0; 430 431 /* To avoid evict_inode call simultaneously */ 432 atomic_inc(&inode->i_count); 433 spin_unlock(&inode->i_lock); 434 435 truncate_setsize(VFS_I(ni), 0); 436 ntfs_truncate_vfs(VFS_I(ni), 0, 1); 437 438 sb_start_intwrite(inode->i_sb); 439 i_size_write(inode, 0); 440 ni->allocated_size = ni->initialized_size = ni->data_size = 0; 441 442 truncate_inode_pages_final(inode->i_mapping); 443 sb_end_intwrite(inode->i_sb); 444 445 spin_lock(&inode->i_lock); 446 atomic_dec(&inode->i_count); 447 } 448 } 449 return 0; 450 } 451 452 return inode_generic_drop(inode); 453 } 454 455 static inline struct ntfs_inode *ntfs_alloc_extent_inode(void) 456 { 457 struct ntfs_inode *ni; 458 459 ntfs_debug("Entering."); 460 ni = kmem_cache_alloc(ntfs_inode_cache, GFP_NOFS); 461 if (likely(ni != NULL)) { 462 ni->state = 0; 463 return ni; 464 } 465 ntfs_error(NULL, "Allocation of NTFS inode structure failed."); 466 return NULL; 467 } 468 469 static void ntfs_destroy_extent_inode(struct ntfs_inode *ni) 470 { 471 ntfs_debug("Entering."); 472 473 if (!atomic_dec_and_test(&ni->count)) 474 WARN_ON(1); 475 if (ni->folio) 476 folio_put(ni->folio); 477 kfree(ni->mrec); 478 kmem_cache_free(ntfs_inode_cache, ni); 479 } 480 481 static struct lock_class_key attr_inode_mrec_lock_class; 482 static struct lock_class_key attr_list_inode_mrec_lock_class; 483 484 /* 485 * The attribute runlist lock has separate locking rules from the 486 * normal runlist lock, so split the two lock-classes: 487 */ 488 static struct lock_class_key attr_list_rl_lock_class; 489 490 /* 491 * __ntfs_init_inode - initialize ntfs specific part of an inode 492 * @sb: super block of mounted volume 493 * @ni: freshly allocated ntfs inode which to initialize 494 * 495 * Initialize an ntfs inode to defaults. 496 * 497 * NOTE: ni->mft_no, ni->state, ni->type, ni->name, and ni->name_len are left 498 * untouched. Make sure to initialize them elsewhere. 499 */ 500 void __ntfs_init_inode(struct super_block *sb, struct ntfs_inode *ni) 501 { 502 ntfs_debug("Entering."); 503 rwlock_init(&ni->size_lock); 504 ni->initialized_size = ni->allocated_size = 0; 505 ni->seq_no = 0; 506 atomic_set(&ni->count, 1); 507 ni->vol = NTFS_SB(sb); 508 atomic_set(&ni->stream_open_count, 0); 509 ntfs_init_runlist(&ni->runlist); 510 mutex_init(&ni->mrec_lock); 511 if (ni->type == AT_ATTRIBUTE_LIST) { 512 lockdep_set_class(&ni->mrec_lock, 513 &attr_list_inode_mrec_lock_class); 514 lockdep_set_class(&ni->runlist.lock, 515 &attr_list_rl_lock_class); 516 } else if (NInoAttr(ni)) { 517 lockdep_set_class(&ni->mrec_lock, 518 &attr_inode_mrec_lock_class); 519 } 520 521 ni->folio = NULL; 522 ni->folio_ofs = 0; 523 ni->mrec = NULL; 524 ni->attr_list_size = 0; 525 ni->attr_list = NULL; 526 ni->itype.index.block_size = 0; 527 ni->itype.index.vcn_size = 0; 528 ni->itype.index.collation_rule = 0; 529 ni->itype.index.block_size_bits = 0; 530 ni->itype.index.vcn_size_bits = 0; 531 mutex_init(&ni->extent_lock); 532 ni->nr_extents = 0; 533 ni->ext.base_ntfs_ino = NULL; 534 ni->flags = 0; 535 ni->mft_lcn[0] = LCN_RL_NOT_MAPPED; 536 ni->mft_lcn_count = 0; 537 ni->reparse_tag = 0; 538 ni->reparse_flags = 0; 539 ni->target = NULL; 540 ni->linuxflags = 0; 541 ni->i_dealloc_clusters = 0; 542 } 543 544 /* 545 * Extent inodes get MFT-mapped in a nested way, while the base inode 546 * is still mapped. Teach this nesting to the lock validator by creating 547 * a separate class for nested inode's mrec_lock's: 548 */ 549 static struct lock_class_key extent_inode_mrec_lock_key; 550 551 inline struct ntfs_inode *ntfs_new_extent_inode(struct super_block *sb, 552 u64 mft_no) 553 { 554 struct ntfs_inode *ni = ntfs_alloc_extent_inode(); 555 556 ntfs_debug("Entering."); 557 if (likely(ni != NULL)) { 558 __ntfs_init_inode(sb, ni); 559 lockdep_set_class(&ni->mrec_lock, &extent_inode_mrec_lock_key); 560 ni->mft_no = mft_no; 561 ni->type = AT_UNUSED; 562 ni->name = NULL; 563 ni->name_len = 0; 564 } 565 return ni; 566 } 567 568 /* 569 * ntfs_is_extended_system_file - check if a file is in the $Extend directory 570 * @ctx: initialized attribute search context 571 * 572 * Search all file name attributes in the inode described by the attribute 573 * search context @ctx and check if any of the names are in the $Extend system 574 * directory. 575 * 576 * Return values: 577 * 3: file is $ObjId in $Extend directory 578 * 2: file is $Reparse in $Extend directory 579 * 1: file is in $Extend directory 580 * 0: file is not in $Extend directory 581 * -errno: failed to determine if the file is in the $Extend directory 582 */ 583 static int ntfs_is_extended_system_file(struct ntfs_attr_search_ctx *ctx) 584 { 585 int nr_links, err; 586 587 /* Restart search. */ 588 ntfs_attr_reinit_search_ctx(ctx); 589 590 /* Get number of hard links. */ 591 nr_links = le16_to_cpu(ctx->mrec->link_count); 592 593 /* Loop through all hard links. */ 594 while (!(err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, 0, 0, NULL, 0, 595 ctx))) { 596 struct file_name_attr *file_name_attr; 597 struct attr_record *attr = ctx->attr; 598 u8 *p, *p2; 599 600 nr_links--; 601 /* 602 * Maximum sanity checking as we are called on an inode that 603 * we suspect might be corrupt. 604 */ 605 p = (u8 *)attr + le32_to_cpu(attr->length); 606 if (p < (u8 *)ctx->mrec || (u8 *)p > (u8 *)ctx->mrec + 607 le32_to_cpu(ctx->mrec->bytes_in_use)) { 608 err_corrupt_attr: 609 ntfs_error(ctx->ntfs_ino->vol->sb, 610 "Corrupt file name attribute. You should run chkdsk."); 611 return -EIO; 612 } 613 if (attr->non_resident) { 614 ntfs_error(ctx->ntfs_ino->vol->sb, 615 "Non-resident file name. You should run chkdsk."); 616 return -EIO; 617 } 618 if (attr->flags) { 619 ntfs_error(ctx->ntfs_ino->vol->sb, 620 "File name with invalid flags. You should run chkdsk."); 621 return -EIO; 622 } 623 if (!(attr->data.resident.flags & RESIDENT_ATTR_IS_INDEXED)) { 624 ntfs_error(ctx->ntfs_ino->vol->sb, 625 "Unindexed file name. You should run chkdsk."); 626 return -EIO; 627 } 628 file_name_attr = (struct file_name_attr *)((u8 *)attr + 629 le16_to_cpu(attr->data.resident.value_offset)); 630 p2 = (u8 *)file_name_attr + le32_to_cpu(attr->data.resident.value_length); 631 if (p2 < (u8 *)attr || p2 > p) 632 goto err_corrupt_attr; 633 /* This attribute is ok, but is it in the $Extend directory? */ 634 if (MREF_LE(file_name_attr->parent_directory) == FILE_Extend) { 635 unsigned char *s; 636 637 s = ntfs_attr_name_get(ctx->ntfs_ino->vol, 638 file_name_attr->file_name, 639 file_name_attr->file_name_length); 640 if (!s) 641 return 1; 642 if (!strcmp("$Reparse", s)) { 643 ntfs_attr_name_free(&s); 644 return 2; /* it's reparse point file */ 645 } 646 if (!strcmp("$ObjId", s)) { 647 ntfs_attr_name_free(&s); 648 return 3; /* it's object id file */ 649 } 650 ntfs_attr_name_free(&s); 651 return 1; /* YES, it's an extended system file. */ 652 } 653 } 654 if (unlikely(err != -ENOENT)) 655 return err; 656 if (unlikely(nr_links)) { 657 ntfs_error(ctx->ntfs_ino->vol->sb, 658 "Inode hard link count doesn't match number of name attributes. You should run chkdsk."); 659 return -EIO; 660 } 661 return 0; /* NO, it is not an extended system file. */ 662 } 663 664 static struct lock_class_key ntfs_dir_inval_lock_key; 665 666 void ntfs_set_vfs_operations(struct inode *inode, mode_t mode, dev_t dev) 667 { 668 if (S_ISDIR(mode)) { 669 if (!NInoAttr(NTFS_I(inode))) { 670 inode->i_op = &ntfs_dir_inode_ops; 671 inode->i_fop = &ntfs_dir_ops; 672 } 673 inode->i_mapping->a_ops = &ntfs_aops; 674 lockdep_set_class(&inode->i_mapping->invalidate_lock, 675 &ntfs_dir_inval_lock_key); 676 } else if (S_ISLNK(mode)) { 677 inode->i_op = &ntfs_symlink_inode_operations; 678 inode->i_mapping->a_ops = &ntfs_aops; 679 } else if (S_ISCHR(mode) || S_ISBLK(mode) || S_ISFIFO(mode) || S_ISSOCK(mode)) { 680 inode->i_op = &ntfs_special_inode_operations; 681 init_special_inode(inode, inode->i_mode, dev); 682 } else { 683 if (!NInoAttr(NTFS_I(inode))) { 684 inode->i_op = &ntfs_file_inode_ops; 685 inode->i_fop = &ntfs_file_ops; 686 } 687 if (inode->i_ino == FILE_MFT) 688 inode->i_mapping->a_ops = &ntfs_mft_aops; 689 else 690 inode->i_mapping->a_ops = &ntfs_aops; 691 } 692 } 693 694 static bool ntfs_non_resident_sizes_inconsistent(struct inode *vi, 695 const struct attr_record *a) 696 { 697 s64 allocated_size = le64_to_cpu(a->data.non_resident.allocated_size); 698 s64 data_size = le64_to_cpu(a->data.non_resident.data_size); 699 s64 initialized_size = le64_to_cpu(a->data.non_resident.initialized_size); 700 701 if (initialized_size >= 0 && initialized_size <= data_size && 702 data_size <= allocated_size && 703 !ntfs_bytes_to_cluster_off(NTFS_I(vi)->vol, allocated_size)) 704 return false; 705 706 ntfs_error(vi->i_sb, 707 "Attribute 0x%x of inode 0x%llx is corrupt (initialized size %lld, data size %lld, allocated size %lld).", 708 le32_to_cpu(a->type), NTFS_I(vi)->mft_no, initialized_size, 709 data_size, allocated_size); 710 return true; 711 } 712 713 /* 714 * ntfs_read_locked_inode - read an inode from its device 715 * @vi: inode to read 716 * 717 * ntfs_read_locked_inode() is called from ntfs_iget() to read the inode 718 * described by @vi into memory from the device. 719 * 720 * The only fields in @vi that we need to/can look at when the function is 721 * called are i_sb, pointing to the mounted device's super block, and i_ino, 722 * the number of the inode to load. 723 * 724 * ntfs_read_locked_inode() maps, pins and locks the mft record number i_ino 725 * for reading and sets up the necessary @vi fields as well as initializing 726 * the ntfs inode. 727 * 728 * Q: What locks are held when the function is called? 729 * A: i_state has I_NEW set, hence the inode is locked, also 730 * i_count is set to 1, so it is not going to go away 731 * i_flags is initialized from the persisted Linux file attributes and 732 * the driver's system-file protection policy before publishing the inode. 733 * 734 * Return 0 on success and -errno on error. 735 */ 736 static int ntfs_read_locked_inode(struct inode *vi) 737 { 738 struct ntfs_volume *vol = NTFS_SB(vi->i_sb); 739 struct ntfs_inode *ni = NTFS_I(vi); 740 struct mft_record *m; 741 struct attr_record *a; 742 struct standard_information *si; 743 struct ntfs_attr_search_ctx *ctx; 744 int err = 0; 745 __le16 *name = I30; 746 unsigned int name_len = 4, flags = 0; 747 int extend_sys = 0; 748 dev_t dev = 0; 749 bool has_lxmod = false; 750 bool vol_err = true; 751 752 ntfs_debug("Entering for i_ino 0x%llx.", ni->mft_no); 753 754 if (uid_valid(vol->uid)) { 755 vi->i_uid = vol->uid; 756 flags |= NTFS_VOL_UID; 757 } else 758 vi->i_uid = GLOBAL_ROOT_UID; 759 760 if (gid_valid(vol->gid)) { 761 vi->i_gid = vol->gid; 762 flags |= NTFS_VOL_GID; 763 } else 764 vi->i_gid = GLOBAL_ROOT_GID; 765 766 vi->i_mode = 0777; 767 768 /* 769 * Initialize the ntfs specific part of @vi special casing 770 * FILE_MFT which we need to do at mount time. 771 */ 772 if (vi->i_ino != FILE_MFT) 773 ntfs_init_big_inode(vi); 774 775 m = map_mft_record(ni); 776 if (IS_ERR(m)) { 777 err = PTR_ERR(m); 778 goto err_out; 779 } 780 781 ctx = ntfs_attr_get_search_ctx(ni, m); 782 if (!ctx) { 783 err = -ENOMEM; 784 goto unm_err_out; 785 } 786 787 if (!(m->flags & MFT_RECORD_IN_USE)) { 788 err = -ENOENT; 789 vol_err = false; 790 goto unm_err_out; 791 } 792 793 if (m->base_mft_record) { 794 ntfs_error(vi->i_sb, "Inode is an extent inode!"); 795 goto unm_err_out; 796 } 797 798 /* Transfer information from mft record into vfs and ntfs inodes. */ 799 vi->i_generation = ni->seq_no = le16_to_cpu(m->sequence_number); 800 801 if (le16_to_cpu(m->link_count) < 1) { 802 ntfs_error(vi->i_sb, "Inode link count is 0!"); 803 goto unm_err_out; 804 } 805 set_nlink(vi, le16_to_cpu(m->link_count)); 806 807 /* If read-only, no one gets write permissions. */ 808 if (IS_RDONLY(vi)) 809 vi->i_mode &= ~0222; 810 811 /* 812 * Find the standard information attribute in the mft record. At this 813 * stage we haven't setup the attribute list stuff yet, so this could 814 * in fact fail if the standard information is in an extent record, but 815 * I don't think this actually ever happens. 816 */ 817 ntfs_attr_reinit_search_ctx(ctx); 818 err = ntfs_attr_lookup(AT_STANDARD_INFORMATION, NULL, 0, 0, 0, NULL, 0, 819 ctx); 820 if (unlikely(err)) { 821 if (err == -ENOENT) 822 ntfs_error(vi->i_sb, "$STANDARD_INFORMATION attribute is missing."); 823 goto unm_err_out; 824 } 825 a = ctx->attr; 826 /* Get the standard information attribute value. */ 827 si = (struct standard_information *)((u8 *)a + 828 le16_to_cpu(a->data.resident.value_offset)); 829 830 /* Transfer information from the standard information into vi. */ 831 /* 832 * Note: The i_?times do not quite map perfectly onto the NTFS times, 833 * but they are close enough, and in the end it doesn't really matter 834 * that much... 835 */ 836 /* 837 * mtime is the last change of the data within the file. Not changed 838 * when only metadata is changed, e.g. a rename doesn't affect mtime. 839 */ 840 ni->i_crtime = ntfs2utc(si->creation_time); 841 842 inode_set_mtime_to_ts(vi, ntfs2utc(si->last_data_change_time)); 843 /* 844 * ctime is the last change of the metadata of the file. This obviously 845 * always changes, when mtime is changed. ctime can be changed on its 846 * own, mtime is then not changed, e.g. when a file is renamed. 847 */ 848 inode_set_ctime_to_ts(vi, ntfs2utc(si->last_mft_change_time)); 849 /* 850 * Last access to the data within the file. Not changed during a rename 851 * for example but changed whenever the file is written to. 852 */ 853 inode_set_atime_to_ts(vi, ntfs2utc(si->last_access_time)); 854 ni->flags = si->file_attributes; 855 856 /* Find the attribute list attribute if present. */ 857 ntfs_attr_reinit_search_ctx(ctx); 858 err = ntfs_attr_lookup(AT_ATTRIBUTE_LIST, NULL, 0, 0, 0, NULL, 0, ctx); 859 if (err) { 860 if (unlikely(err != -ENOENT)) { 861 ntfs_error(vi->i_sb, "Failed to lookup attribute list attribute."); 862 goto unm_err_out; 863 } 864 } else { 865 s64 attr_list_size; 866 867 if (vi->i_ino == FILE_MFT) 868 goto skip_attr_list_load; 869 ntfs_debug("Attribute list found in inode 0x%llx.", ni->mft_no); 870 NInoSetAttrList(ni); 871 a = ctx->attr; 872 if (a->flags & ATTR_COMPRESSION_MASK) { 873 ntfs_error(vi->i_sb, 874 "Attribute list attribute is compressed."); 875 goto unm_err_out; 876 } 877 if (a->flags & ATTR_IS_ENCRYPTED || 878 a->flags & ATTR_IS_SPARSE) { 879 if (a->non_resident) { 880 ntfs_error(vi->i_sb, 881 "Non-resident attribute list attribute is encrypted/sparse."); 882 goto unm_err_out; 883 } 884 ntfs_warning(vi->i_sb, 885 "Resident attribute list attribute in inode 0x%llx is marked encrypted/sparse which is not true. However, Windows allows this and chkdsk does not detect or correct it so we will just ignore the invalid flags and pretend they are not set.", 886 ni->mft_no); 887 } 888 /* Now allocate memory for the attribute list. */ 889 attr_list_size = ntfs_attr_size(a); 890 if (attr_list_size <= 0 || 891 attr_list_size > NTFS_MAX_ATTR_LIST_SIZE) { 892 ntfs_error(vi->i_sb, "Invalid attribute list size %lld (mft_no 0x%llx).", 893 (long long)attr_list_size, ni->mft_no); 894 goto unm_err_out; 895 } 896 ni->attr_list_size = (u32)attr_list_size; 897 ni->attr_list = kvzalloc(ni->attr_list_size, GFP_NOFS); 898 if (!ni->attr_list) { 899 ntfs_error(vi->i_sb, 900 "Not enough memory to allocate buffer for attribute list."); 901 err = -ENOMEM; 902 goto unm_err_out; 903 } 904 if (a->non_resident) { 905 NInoSetAttrListNonResident(ni); 906 if (a->data.non_resident.lowest_vcn) { 907 ntfs_error(vi->i_sb, "Attribute list has non zero lowest_vcn."); 908 goto unm_err_out; 909 } 910 911 /* Now load the attribute list. */ 912 err = load_attribute_list(ni, ni->attr_list, ni->attr_list_size); 913 if (err) { 914 ntfs_error(vi->i_sb, "Failed to load attribute list attribute."); 915 goto unm_err_out; 916 } 917 } else /* if (!a->non_resident) */ { 918 /* Now copy the attribute list. */ 919 memcpy(ni->attr_list, (u8 *)a + le16_to_cpu( 920 a->data.resident.value_offset), 921 le32_to_cpu( 922 a->data.resident.value_length)); 923 /* A resident list is not validated on load; check it now. */ 924 if (!ntfs_attr_list_is_valid(ni->attr_list, 925 ni->attr_list_size)) { 926 ntfs_error(vi->i_sb, "Corrupt attribute list."); 927 goto unm_err_out; 928 } 929 } 930 } 931 skip_attr_list_load: 932 err = ntfs_attr_lookup(AT_EA_INFORMATION, NULL, 0, 0, 0, NULL, 0, ctx); 933 if (!err) { 934 NInoSetHasEA(ni); 935 ntfs_ea_get_wsl_inode(vi, &dev, flags, &has_lxmod); 936 err = ntfs_ea_get_linuxflags(vi); 937 if (err) 938 goto unm_err_out; 939 } else if (err != -ENOENT) { 940 goto unm_err_out; 941 } 942 943 if (ni->flags & FILE_ATTR_REPARSE_POINT) { 944 unsigned int mode; 945 946 err = ntfs_parse_reparse(ni, &mode); 947 if (err) 948 goto unm_err_out; 949 if (mode) 950 vi->i_mode |= mode; 951 else { 952 vi->i_mode &= ~S_IFLNK; 953 if (m->flags & MFT_RECORD_IS_DIRECTORY) 954 vi->i_mode |= S_IFDIR; 955 else 956 vi->i_mode |= S_IFREG; 957 } 958 } else if (m->flags & MFT_RECORD_IS_DIRECTORY) { 959 vi->i_mode |= S_IFDIR; 960 } else { 961 vi->i_mode |= S_IFREG; 962 } 963 964 if (S_ISDIR(vi->i_mode)) { 965 /* 966 * Apply the directory permissions mask set in the mount options 967 * when no per-file WSL mode is present. 968 */ 969 if (!has_lxmod) 970 vi->i_mode &= ~vol->dmask; 971 /* Things break without this kludge! */ 972 if (vi->i_nlink > 1) 973 set_nlink(vi, 1); 974 } else { 975 /* Apply the file permissions mask when no WSL mode is present. */ 976 if (!has_lxmod) 977 vi->i_mode &= ~vol->fmask; 978 } 979 980 /* 981 * If an attribute list is present we now have the attribute list value 982 * in ntfs_ino->attr_list and it is ntfs_ino->attr_list_size bytes. 983 */ 984 if (m->flags & MFT_RECORD_IS_DIRECTORY) { 985 struct index_root *ir; 986 987 view_index_meta: 988 /* It is a directory, find index root attribute. */ 989 ntfs_attr_reinit_search_ctx(ctx); 990 err = ntfs_attr_lookup(AT_INDEX_ROOT, name, name_len, CASE_SENSITIVE, 991 0, NULL, 0, ctx); 992 if (unlikely(err)) { 993 if (err == -ENOENT) 994 ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is missing."); 995 goto unm_err_out; 996 } 997 a = ctx->attr; 998 /* Set up the state. */ 999 if (unlikely(a->non_resident)) { 1000 ntfs_error(vol->sb, 1001 "$INDEX_ROOT attribute is not resident."); 1002 goto unm_err_out; 1003 } 1004 /* Ensure the attribute name is placed before the value. */ 1005 if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >= 1006 le16_to_cpu(a->data.resident.value_offset)))) { 1007 ntfs_error(vol->sb, 1008 "$INDEX_ROOT attribute name is placed after the attribute value."); 1009 goto unm_err_out; 1010 } 1011 /* 1012 * Compressed/encrypted index root just means that the newly 1013 * created files in that directory should be created compressed/ 1014 * encrypted. However index root cannot be both compressed and 1015 * encrypted. 1016 */ 1017 if (a->flags & ATTR_COMPRESSION_MASK) { 1018 NInoSetCompressed(ni); 1019 ni->flags |= FILE_ATTR_COMPRESSED; 1020 } 1021 if (a->flags & ATTR_IS_ENCRYPTED) { 1022 if (a->flags & ATTR_COMPRESSION_MASK) { 1023 ntfs_error(vi->i_sb, "Found encrypted and compressed attribute."); 1024 goto unm_err_out; 1025 } 1026 NInoSetEncrypted(ni); 1027 ni->flags |= FILE_ATTR_ENCRYPTED; 1028 } 1029 if (a->flags & ATTR_IS_SPARSE) { 1030 NInoSetSparse(ni); 1031 ni->flags |= FILE_ATTR_SPARSE_FILE; 1032 } 1033 ir = (struct index_root *)((u8 *)a + 1034 le16_to_cpu(a->data.resident.value_offset)); 1035 if (ntfs_index_root_inconsistent(ni->vol, a, ir, ni->mft_no) || 1036 ntfs_index_entries_inconsistent(ni->vol, &ir->index, 1037 le32_to_cpu(ir->index_block_size), 1038 ir->collation_rule, ni->mft_no)) { 1039 ntfs_error(vi->i_sb, "Directory index is corrupt."); 1040 goto unm_err_out; 1041 } 1042 1043 if (extend_sys) { 1044 if (ir->type) { 1045 ntfs_error(vi->i_sb, "Indexed attribute is not zero."); 1046 goto unm_err_out; 1047 } 1048 } else { 1049 if (ir->type != AT_FILE_NAME) { 1050 ntfs_error(vi->i_sb, "Indexed attribute is not $FILE_NAME."); 1051 goto unm_err_out; 1052 } 1053 1054 if (ir->collation_rule != COLLATION_FILE_NAME) { 1055 ntfs_error(vi->i_sb, 1056 "Index collation rule is not COLLATION_FILE_NAME."); 1057 goto unm_err_out; 1058 } 1059 } 1060 1061 ni->itype.index.collation_rule = ir->collation_rule; 1062 ni->itype.index.block_size = le32_to_cpu(ir->index_block_size); 1063 if (ni->itype.index.block_size & 1064 (ni->itype.index.block_size - 1)) { 1065 ntfs_error(vi->i_sb, "Index block size (%u) is not a power of two.", 1066 ni->itype.index.block_size); 1067 goto unm_err_out; 1068 } 1069 if (ni->itype.index.block_size > PAGE_SIZE) { 1070 ntfs_error(vi->i_sb, 1071 "Index block size (%u) > PAGE_SIZE (%ld) is not supported.", 1072 ni->itype.index.block_size, 1073 PAGE_SIZE); 1074 err = -EOPNOTSUPP; 1075 goto unm_err_out; 1076 } 1077 if (ni->itype.index.block_size < NTFS_BLOCK_SIZE) { 1078 ntfs_error(vi->i_sb, 1079 "Index block size (%u) < NTFS_BLOCK_SIZE (%i) is not supported.", 1080 ni->itype.index.block_size, 1081 NTFS_BLOCK_SIZE); 1082 err = -EOPNOTSUPP; 1083 goto unm_err_out; 1084 } 1085 ni->itype.index.block_size_bits = 1086 ffs(ni->itype.index.block_size) - 1; 1087 /* Determine the size of a vcn in the directory index. */ 1088 if (vol->cluster_size <= ni->itype.index.block_size) { 1089 ni->itype.index.vcn_size = vol->cluster_size; 1090 ni->itype.index.vcn_size_bits = vol->cluster_size_bits; 1091 } else { 1092 ni->itype.index.vcn_size = NTFS_BLOCK_SIZE; 1093 ni->itype.index.vcn_size_bits = NTFS_BLOCK_SIZE_BITS; 1094 } 1095 1096 /* Setup the index allocation attribute, even if not present. */ 1097 ni->type = AT_INDEX_ROOT; 1098 ni->name = name; 1099 ni->name_len = name_len; 1100 vi->i_size = ni->initialized_size = ni->data_size = 1101 le32_to_cpu(a->data.resident.value_length); 1102 ni->allocated_size = (ni->data_size + 7) & ~7; 1103 /* We are done with the mft record, so we release it. */ 1104 ntfs_attr_put_search_ctx(ctx); 1105 unmap_mft_record(ni); 1106 m = NULL; 1107 ctx = NULL; 1108 /* Setup the operations for this inode. */ 1109 ntfs_set_vfs_operations(vi, vi->i_mode, 0); 1110 if (ir->index.flags & LARGE_INDEX) 1111 NInoSetIndexAllocPresent(ni); 1112 } else { 1113 /* It is a file. */ 1114 ntfs_attr_reinit_search_ctx(ctx); 1115 1116 /* Setup the data attribute, even if not present. */ 1117 ni->type = AT_DATA; 1118 ni->name = AT_UNNAMED; 1119 ni->name_len = 0; 1120 1121 /* Find first extent of the unnamed data attribute. */ 1122 err = ntfs_attr_lookup(AT_DATA, NULL, 0, 0, 0, NULL, 0, ctx); 1123 if (unlikely(err)) { 1124 vi->i_size = ni->initialized_size = 1125 ni->allocated_size = 0; 1126 if (err != -ENOENT) { 1127 ntfs_error(vi->i_sb, "Failed to lookup $DATA attribute."); 1128 goto unm_err_out; 1129 } 1130 /* 1131 * FILE_Secure does not have an unnamed $DATA 1132 * attribute, so we special case it here. 1133 */ 1134 if (vi->i_ino == FILE_Secure) 1135 goto no_data_attr_special_case; 1136 /* 1137 * Most if not all the system files in the $Extend 1138 * system directory do not have unnamed data 1139 * attributes so we need to check if the parent 1140 * directory of the file is FILE_Extend and if it is 1141 * ignore this error. To do this we need to get the 1142 * name of this inode from the mft record as the name 1143 * contains the back reference to the parent directory. 1144 */ 1145 extend_sys = ntfs_is_extended_system_file(ctx); 1146 if (extend_sys > 0) { 1147 if (m->flags & MFT_RECORD_IS_VIEW_INDEX) { 1148 if (extend_sys == 2) { 1149 name = reparse_index_name; 1150 name_len = 2; 1151 goto view_index_meta; 1152 } else if (extend_sys == 3) { 1153 name = objid_index_name; 1154 name_len = 2; 1155 goto view_index_meta; 1156 } 1157 } 1158 goto no_data_attr_special_case; 1159 } 1160 1161 err = extend_sys; 1162 ntfs_error(vi->i_sb, "$DATA attribute is missing, err : %d", err); 1163 goto unm_err_out; 1164 } 1165 a = ctx->attr; 1166 /* Setup the state. */ 1167 if (a->flags & (ATTR_COMPRESSION_MASK | ATTR_IS_SPARSE)) { 1168 if (a->flags & ATTR_COMPRESSION_MASK) { 1169 if (NInoWofCompressed(ni)) { 1170 ntfs_error(vi->i_sb, 1171 "Found native compression on a WOF file."); 1172 goto unm_err_out; 1173 } 1174 NInoSetCompressed(ni); 1175 ni->flags |= FILE_ATTR_COMPRESSED; 1176 if (vol->cluster_size > 4096) { 1177 ntfs_error(vi->i_sb, 1178 "Found compressed data but compression is disabled due to cluster size (%i) > 4kiB.", 1179 vol->cluster_size); 1180 goto unm_err_out; 1181 } 1182 if ((a->flags & ATTR_COMPRESSION_MASK) 1183 != ATTR_IS_COMPRESSED) { 1184 ntfs_error(vi->i_sb, 1185 "Found unknown compression method or corrupt file."); 1186 goto unm_err_out; 1187 } 1188 } 1189 if (a->flags & ATTR_IS_SPARSE) { 1190 NInoSetSparse(ni); 1191 ni->flags |= FILE_ATTR_SPARSE_FILE; 1192 } 1193 } 1194 if (a->flags & ATTR_IS_ENCRYPTED) { 1195 if (NInoCompressed(ni)) { 1196 ntfs_error(vi->i_sb, "Found encrypted and compressed data."); 1197 goto unm_err_out; 1198 } 1199 NInoSetEncrypted(ni); 1200 ni->flags |= FILE_ATTR_ENCRYPTED; 1201 } 1202 if (a->non_resident) { 1203 NInoSetNonResident(ni); 1204 if (a->data.non_resident.lowest_vcn) { 1205 ntfs_error(vi->i_sb, 1206 "First extent of $DATA attribute has non zero lowest_vcn."); 1207 goto unm_err_out; 1208 } 1209 if (NInoCompressed(ni) || (NInoSparse(ni) && !NInoWofCompressed(ni))) { 1210 if (NInoCompressed(ni) && 1211 a->data.non_resident.compression_unit != 4) { 1212 ntfs_error(vi->i_sb, 1213 "Found non-standard compression unit (%u instead of 4). Cannot handle this.", 1214 a->data.non_resident.compression_unit); 1215 err = -EOPNOTSUPP; 1216 goto unm_err_out; 1217 } 1218 1219 /* 1220 * Windows stores the standard compression unit in 1221 * sparse attributes even when the data is not compressed. 1222 */ 1223 if (NInoSparse(ni) && 1224 a->data.non_resident.compression_unit && 1225 a->data.non_resident.compression_unit != 1226 vol->sparse_compression_unit && 1227 a->data.non_resident.compression_unit != 1228 STANDARD_COMPRESSION_UNIT) { 1229 ntfs_error(vi->i_sb, 1230 "Found non-standard compression unit (%u instead of 0, %d, or %d). Cannot handle this.", 1231 a->data.non_resident.compression_unit, 1232 vol->sparse_compression_unit, 1233 STANDARD_COMPRESSION_UNIT); 1234 err = -EOPNOTSUPP; 1235 goto unm_err_out; 1236 } 1237 1238 1239 if (a->data.non_resident.compression_unit) { 1240 ni->itype.compressed.block_size = 1U << 1241 (a->data.non_resident.compression_unit + 1242 vol->cluster_size_bits); 1243 ni->itype.compressed.block_size_bits = 1244 ffs(ni->itype.compressed.block_size) - 1; 1245 ni->itype.compressed.block_clusters = 1246 1U << a->data.non_resident.compression_unit; 1247 } else { 1248 ni->itype.compressed.block_size = 0; 1249 ni->itype.compressed.block_size_bits = 1250 0; 1251 ni->itype.compressed.block_clusters = 1252 0; 1253 } 1254 ni->itype.compressed.size = le64_to_cpu( 1255 a->data.non_resident.compressed_size); 1256 } 1257 if (ntfs_non_resident_sizes_inconsistent(vi, a)) 1258 goto unm_err_out; 1259 vi->i_size = ni->data_size = le64_to_cpu(a->data.non_resident.data_size); 1260 ni->initialized_size = le64_to_cpu(a->data.non_resident.initialized_size); 1261 ni->allocated_size = le64_to_cpu(a->data.non_resident.allocated_size); 1262 } else { /* Resident attribute. */ 1263 vi->i_size = ni->data_size = ni->initialized_size = le32_to_cpu( 1264 a->data.resident.value_length); 1265 ni->allocated_size = le32_to_cpu(a->length) - 1266 le16_to_cpu( 1267 a->data.resident.value_offset); 1268 if (vi->i_size > ni->allocated_size) { 1269 ntfs_error(vi->i_sb, 1270 "Resident data attribute is corrupt (size exceeds allocation)."); 1271 goto unm_err_out; 1272 } 1273 } 1274 no_data_attr_special_case: 1275 /* We are done with the mft record, so we release it. */ 1276 ntfs_attr_put_search_ctx(ctx); 1277 unmap_mft_record(ni); 1278 m = NULL; 1279 ctx = NULL; 1280 /* Setup the operations for this inode. */ 1281 ntfs_set_vfs_operations(vi, vi->i_mode, dev); 1282 } 1283 1284 if (NVolSysImmutable(vol) && (ni->flags & FILE_ATTR_SYSTEM) && 1285 !S_ISFIFO(vi->i_mode) && !S_ISSOCK(vi->i_mode) && !S_ISLNK(vi->i_mode)) 1286 vi->i_flags |= S_IMMUTABLE; 1287 1288 /* 1289 * System files such as $Bitmap and $MFT are maintained by the driver 1290 * itself, and writing them from userspace corrupts the volume. 1291 * Always make them immutable regardless of the sys_immutable option. 1292 * Directories are skipped so the root and $Extend stay usable. 1293 */ 1294 if (ni->mft_no < FILE_first_user && S_ISREG(vi->i_mode)) 1295 vi->i_flags |= S_IMMUTABLE; 1296 1297 /* 1298 * The number of 512-byte blocks used on disk (for stat). This is in so 1299 * far inaccurate as it doesn't account for any named streams or other 1300 * special non-resident attributes, but that is how Windows works, too, 1301 * so we are at least consistent with Windows, if not entirely 1302 * consistent with the Linux Way. Doing it the Linux Way would cause a 1303 * significant slowdown as it would involve iterating over all 1304 * attributes in the mft record and adding the allocated/compressed 1305 * sizes of all non-resident attributes present to give us the Linux 1306 * correct size that should go into i_blocks (after division by 512). 1307 */ 1308 if (S_ISREG(vi->i_mode) && 1309 (NInoCompressed(ni) || (NInoSparse(ni) && !NInoWofCompressed(ni)))) 1310 vi->i_blocks = ni->itype.compressed.size >> 9; 1311 else 1312 vi->i_blocks = ni->allocated_size >> 9; 1313 1314 if (S_ISLNK(vi->i_mode) && ni->target) 1315 vi->i_size = strlen(ni->target); 1316 1317 ntfs_debug("Done."); 1318 return 0; 1319 unm_err_out: 1320 if (!err) 1321 err = -EIO; 1322 if (ctx) 1323 ntfs_attr_put_search_ctx(ctx); 1324 if (m) 1325 unmap_mft_record(ni); 1326 err_out: 1327 if (err != -EOPNOTSUPP && err != -ENOMEM && 1328 err != -EINTR && err != -ERESTARTSYS && vol_err == true) { 1329 ntfs_error(vol->sb, 1330 "Failed with error code %i. Marking corrupt inode 0x%llx as bad. Run chkdsk.", 1331 err, ni->mft_no); 1332 NVolSetErrors(vol); 1333 } 1334 return err; 1335 } 1336 1337 /* 1338 * ntfs_read_locked_attr_inode - read an attribute inode from its base inode 1339 * @base_vi: base inode 1340 * @vi: attribute inode to read 1341 * 1342 * ntfs_read_locked_attr_inode() is called from ntfs_attr_iget() to read the 1343 * attribute inode described by @vi into memory from the base mft record 1344 * described by @base_ni. 1345 * 1346 * ntfs_read_locked_attr_inode() maps, pins and locks the base inode for 1347 * reading and looks up the attribute described by @vi before setting up the 1348 * necessary fields in @vi as well as initializing the ntfs inode. 1349 * 1350 * Q: What locks are held when the function is called? 1351 * A: i_state has I_NEW set, hence the inode is locked, also 1352 * i_count is set to 1, so it is not going to go away 1353 * 1354 * Return 0 on success and -errno on error. 1355 * 1356 * Note this cannot be called for AT_INDEX_ALLOCATION. 1357 */ 1358 static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi) 1359 { 1360 struct ntfs_volume *vol = NTFS_SB(vi->i_sb); 1361 struct ntfs_inode *ni = NTFS_I(vi), *base_ni = NTFS_I(base_vi); 1362 struct mft_record *m; 1363 struct attr_record *a; 1364 struct ntfs_attr_search_ctx *ctx; 1365 int err = 0; 1366 u32 ic; 1367 1368 ntfs_debug("Entering for i_ino 0x%llx.", ni->mft_no); 1369 1370 ntfs_init_big_inode(vi); 1371 1372 /* Just mirror the values from the base inode. */ 1373 vi->i_uid = base_vi->i_uid; 1374 vi->i_gid = base_vi->i_gid; 1375 set_nlink(vi, base_vi->i_nlink); 1376 inode_set_mtime_to_ts(vi, inode_get_mtime(base_vi)); 1377 inode_set_ctime_to_ts(vi, inode_get_ctime(base_vi)); 1378 inode_set_atime_to_ts(vi, inode_get_atime(base_vi)); 1379 vi->i_generation = ni->seq_no = base_ni->seq_no; 1380 1381 /* Named data streams are regular files throughout initialization. */ 1382 vi->i_mode = base_vi->i_mode & ~S_IFMT; 1383 if (ni->type == AT_DATA && ni->name_len) { 1384 vi->i_mode |= S_IFREG; 1385 vi->i_flags = base_vi->i_flags; 1386 } 1387 1388 m = map_mft_record(base_ni); 1389 if (IS_ERR(m)) { 1390 err = PTR_ERR(m); 1391 goto err_out; 1392 } 1393 ctx = ntfs_attr_get_search_ctx(base_ni, m); 1394 if (!ctx) { 1395 err = -ENOMEM; 1396 goto unm_err_out; 1397 } 1398 /* Find the attribute. */ 1399 ic = ntfs_inode_is_named_stream(ni) ? IGNORE_CASE : CASE_SENSITIVE; 1400 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 1401 ic, 0, NULL, 0, ctx); 1402 if (unlikely(err)) 1403 goto unm_err_out; 1404 a = ctx->attr; 1405 if (ntfs_inode_is_named_stream(ni)) { 1406 __le16 *disk_name = (__le16 *)((u8 *)a + 1407 le16_to_cpu(a->name_offset)); 1408 1409 /* Subsequent extent and mutation lookups must select this name. */ 1410 if (ni->name == I30) { 1411 ni->name = kmalloc_array(ni->name_len + 1, 1412 sizeof(__le16), GFP_NOFS); 1413 if (!ni->name) { 1414 err = -ENOMEM; 1415 goto unm_err_out; 1416 } 1417 } 1418 memcpy(ni->name, disk_name, 1419 ni->name_len * sizeof(__le16)); 1420 ni->name[ni->name_len] = 0; 1421 } 1422 if (a->flags & (ATTR_COMPRESSION_MASK | ATTR_IS_SPARSE)) { 1423 if (a->flags & ATTR_COMPRESSION_MASK) { 1424 NInoSetCompressed(ni); 1425 ni->flags |= FILE_ATTR_COMPRESSED; 1426 if ((ni->type != AT_DATA) || (ni->type == AT_DATA && 1427 ni->name_len)) { 1428 ntfs_error(vi->i_sb, 1429 "Found compressed non-data or named data attribute."); 1430 goto unm_err_out; 1431 } 1432 if (vol->cluster_size > 4096) { 1433 ntfs_error(vi->i_sb, 1434 "Found compressed attribute but compression is disabled due to cluster size (%i) > 4kiB.", 1435 vol->cluster_size); 1436 goto unm_err_out; 1437 } 1438 if ((a->flags & ATTR_COMPRESSION_MASK) != 1439 ATTR_IS_COMPRESSED) { 1440 ntfs_error(vi->i_sb, "Found unknown compression method."); 1441 goto unm_err_out; 1442 } 1443 } 1444 /* 1445 * The compressed/sparse flag set in an index root just means 1446 * to compress all files. 1447 */ 1448 if (NInoMstProtected(ni) && ni->type != AT_INDEX_ROOT) { 1449 ntfs_error(vi->i_sb, 1450 "Found mst protected attribute but the attribute is %s.", 1451 NInoCompressed(ni) ? "compressed" : "sparse"); 1452 goto unm_err_out; 1453 } 1454 if (a->flags & ATTR_IS_SPARSE) { 1455 NInoSetSparse(ni); 1456 ni->flags |= FILE_ATTR_SPARSE_FILE; 1457 } 1458 } 1459 if (a->flags & ATTR_IS_ENCRYPTED) { 1460 if (NInoCompressed(ni)) { 1461 ntfs_error(vi->i_sb, "Found encrypted and compressed data."); 1462 goto unm_err_out; 1463 } 1464 /* 1465 * The encryption flag set in an index root just means to 1466 * encrypt all files. 1467 */ 1468 if (NInoMstProtected(ni) && ni->type != AT_INDEX_ROOT) { 1469 ntfs_error(vi->i_sb, 1470 "Found mst protected attribute but the attribute is encrypted."); 1471 goto unm_err_out; 1472 } 1473 if (ni->type != AT_DATA) { 1474 ntfs_error(vi->i_sb, 1475 "Found encrypted non-data attribute."); 1476 goto unm_err_out; 1477 } 1478 NInoSetEncrypted(ni); 1479 ni->flags |= FILE_ATTR_ENCRYPTED; 1480 } 1481 if (!a->non_resident) { 1482 /* Ensure the attribute name is placed before the value. */ 1483 if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >= 1484 le16_to_cpu(a->data.resident.value_offset)))) { 1485 ntfs_error(vol->sb, 1486 "Attribute name is placed after the attribute value."); 1487 goto unm_err_out; 1488 } 1489 if (NInoMstProtected(ni)) { 1490 ntfs_error(vi->i_sb, 1491 "Found mst protected attribute but the attribute is resident."); 1492 goto unm_err_out; 1493 } 1494 vi->i_size = ni->initialized_size = ni->data_size = le32_to_cpu( 1495 a->data.resident.value_length); 1496 ni->allocated_size = le32_to_cpu(a->length) - 1497 le16_to_cpu(a->data.resident.value_offset); 1498 if (vi->i_size > ni->allocated_size) { 1499 ntfs_error(vi->i_sb, 1500 "Resident attribute is corrupt (size exceeds allocation)."); 1501 goto unm_err_out; 1502 } 1503 } else { 1504 NInoSetNonResident(ni); 1505 if (a->data.non_resident.lowest_vcn) { 1506 ntfs_error(vi->i_sb, "First extent of attribute has non-zero lowest_vcn."); 1507 goto unm_err_out; 1508 } 1509 /* 1510 * Ensure the attribute name is placed before the mapping pairs 1511 * array. 1512 */ 1513 if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >= 1514 le16_to_cpu( 1515 a->data.non_resident.mapping_pairs_offset)))) { 1516 ntfs_error(vol->sb, 1517 "Attribute name is placed after the mapping pairs array."); 1518 goto unm_err_out; 1519 } 1520 if (NInoCompressed(ni) || (NInoSparse(ni) && !NInoWofCompressed(ni))) { 1521 if (NInoCompressed(ni) && a->data.non_resident.compression_unit != 4) { 1522 ntfs_error(vi->i_sb, 1523 "Found non-standard compression unit (%u instead of 4). Cannot handle this.", 1524 a->data.non_resident.compression_unit); 1525 err = -EOPNOTSUPP; 1526 goto unm_err_out; 1527 } 1528 if (a->data.non_resident.compression_unit) { 1529 ni->itype.compressed.block_size = 1U << 1530 (a->data.non_resident.compression_unit + 1531 vol->cluster_size_bits); 1532 ni->itype.compressed.block_size_bits = 1533 ffs(ni->itype.compressed.block_size) - 1; 1534 ni->itype.compressed.block_clusters = 1U << 1535 a->data.non_resident.compression_unit; 1536 } else { 1537 ni->itype.compressed.block_size = 0; 1538 ni->itype.compressed.block_size_bits = 0; 1539 ni->itype.compressed.block_clusters = 0; 1540 } 1541 ni->itype.compressed.size = le64_to_cpu( 1542 a->data.non_resident.compressed_size); 1543 } 1544 if (ntfs_non_resident_sizes_inconsistent(vi, a)) 1545 goto unm_err_out; 1546 vi->i_size = ni->data_size = le64_to_cpu(a->data.non_resident.data_size); 1547 ni->initialized_size = le64_to_cpu(a->data.non_resident.initialized_size); 1548 ni->allocated_size = le64_to_cpu(a->data.non_resident.allocated_size); 1549 } 1550 vi->i_mapping->a_ops = &ntfs_aops; 1551 if ((NInoCompressed(ni) || NInoSparse(ni)) && ni->type != AT_INDEX_ROOT) 1552 vi->i_blocks = ni->itype.compressed.size >> 9; 1553 else 1554 vi->i_blocks = ni->allocated_size >> 9; 1555 if (ni->type == AT_DATA && ni->name_len) { 1556 vi->i_op = &ntfs_stream_inode_ops; 1557 vi->i_fop = &ntfs_stream_file_ops; 1558 } 1559 /* 1560 * Make sure the base inode does not go away and attach it to the 1561 * attribute inode. 1562 */ 1563 if (!igrab(base_vi)) { 1564 err = -ENOENT; 1565 goto unm_err_out; 1566 } 1567 ni->ext.base_ntfs_ino = base_ni; 1568 ni->nr_extents = -1; 1569 1570 ntfs_attr_put_search_ctx(ctx); 1571 unmap_mft_record(base_ni); 1572 1573 ntfs_debug("Done."); 1574 return 0; 1575 1576 unm_err_out: 1577 if (!err) 1578 err = -EIO; 1579 if (ctx) 1580 ntfs_attr_put_search_ctx(ctx); 1581 unmap_mft_record(base_ni); 1582 err_out: 1583 if (err != -ENOENT && err != -EINTR && err != -ERESTARTSYS) 1584 ntfs_error(vol->sb, 1585 "Failed with error code %i while reading attribute inode (mft_no 0x%llx, type 0x%x, name_len %i). Marking corrupt inode and base inode 0x%llx as bad. Run chkdsk.", 1586 err, ni->mft_no, ni->type, ni->name_len, 1587 base_ni->mft_no); 1588 if (err != -ENOENT && err != -ENOMEM && 1589 err != -EINTR && err != -ERESTARTSYS) 1590 NVolSetErrors(vol); 1591 return err; 1592 } 1593 1594 /* 1595 * ntfs_read_locked_index_inode - read an index inode from its base inode 1596 * @base_vi: base inode 1597 * @vi: index inode to read 1598 * 1599 * ntfs_read_locked_index_inode() is called from ntfs_index_iget() to read the 1600 * index inode described by @vi into memory from the base mft record described 1601 * by @base_ni. 1602 * 1603 * ntfs_read_locked_index_inode() maps, pins and locks the base inode for 1604 * reading and looks up the attributes relating to the index described by @vi 1605 * before setting up the necessary fields in @vi as well as initializing the 1606 * ntfs inode. 1607 * 1608 * Note, index inodes are essentially attribute inodes (NInoAttr() is true) 1609 * with the attribute type set to AT_INDEX_ALLOCATION. Apart from that, they 1610 * are setup like directory inodes since directories are a special case of 1611 * indices ao they need to be treated in much the same way. Most importantly, 1612 * for small indices the index allocation attribute might not actually exist. 1613 * However, the index root attribute always exists but this does not need to 1614 * have an inode associated with it and this is why we define a new inode type 1615 * index. Also, like for directories, we need to have an attribute inode for 1616 * the bitmap attribute corresponding to the index allocation attribute and we 1617 * can store this in the appropriate field of the inode, just like we do for 1618 * normal directory inodes. 1619 * 1620 * Q: What locks are held when the function is called? 1621 * A: i_state has I_NEW set, hence the inode is locked, also 1622 * i_count is set to 1, so it is not going to go away 1623 * 1624 * Return 0 on success and -errno on error. 1625 */ 1626 static int ntfs_read_locked_index_inode(struct inode *base_vi, struct inode *vi) 1627 { 1628 loff_t bvi_size; 1629 struct ntfs_volume *vol = NTFS_SB(vi->i_sb); 1630 struct ntfs_inode *ni = NTFS_I(vi), *base_ni = NTFS_I(base_vi), *bni; 1631 struct inode *bvi; 1632 struct mft_record *m; 1633 struct attr_record *a; 1634 struct ntfs_attr_search_ctx *ctx; 1635 struct index_root *ir; 1636 int err = 0; 1637 1638 ntfs_debug("Entering for i_ino 0x%llx.", ni->mft_no); 1639 lockdep_assert_held(&base_ni->mrec_lock); 1640 1641 ntfs_init_big_inode(vi); 1642 /* Just mirror the values from the base inode. */ 1643 vi->i_uid = base_vi->i_uid; 1644 vi->i_gid = base_vi->i_gid; 1645 set_nlink(vi, base_vi->i_nlink); 1646 inode_set_mtime_to_ts(vi, inode_get_mtime(base_vi)); 1647 inode_set_ctime_to_ts(vi, inode_get_ctime(base_vi)); 1648 inode_set_atime_to_ts(vi, inode_get_atime(base_vi)); 1649 vi->i_generation = ni->seq_no = base_ni->seq_no; 1650 /* Set inode type to zero but preserve permissions. */ 1651 vi->i_mode = base_vi->i_mode & ~S_IFMT; 1652 /* Map the mft record for the base inode. */ 1653 m = map_mft_record(base_ni); 1654 if (IS_ERR(m)) { 1655 err = PTR_ERR(m); 1656 goto err_out; 1657 } 1658 ctx = ntfs_attr_get_search_ctx(base_ni, m); 1659 if (!ctx) { 1660 err = -ENOMEM; 1661 goto unm_err_out; 1662 } 1663 /* Find the index root attribute. */ 1664 err = ntfs_attr_lookup(AT_INDEX_ROOT, ni->name, ni->name_len, 1665 CASE_SENSITIVE, 0, NULL, 0, ctx); 1666 if (unlikely(err)) { 1667 if (err == -ENOENT) 1668 ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is missing."); 1669 goto unm_err_out; 1670 } 1671 a = ctx->attr; 1672 /* Set up the state. */ 1673 if (unlikely(a->non_resident)) { 1674 ntfs_error(vol->sb, "$INDEX_ROOT attribute is not resident."); 1675 goto unm_err_out; 1676 } 1677 /* Ensure the attribute name is placed before the value. */ 1678 if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >= 1679 le16_to_cpu(a->data.resident.value_offset)))) { 1680 ntfs_error(vol->sb, 1681 "$INDEX_ROOT attribute name is placed after the attribute value."); 1682 goto unm_err_out; 1683 } 1684 1685 ir = (struct index_root *)((u8 *)a + le16_to_cpu(a->data.resident.value_offset)); 1686 if (ntfs_index_root_inconsistent(vol, a, ir, ni->mft_no) || 1687 ntfs_index_entries_inconsistent(vol, &ir->index, 1688 le32_to_cpu(ir->index_block_size), 1689 ir->collation_rule, ni->mft_no)) { 1690 ntfs_error(vi->i_sb, "Index is corrupt."); 1691 goto unm_err_out; 1692 } 1693 1694 ni->itype.index.collation_rule = ir->collation_rule; 1695 ntfs_debug("Index collation rule is 0x%x.", 1696 le32_to_cpu(ir->collation_rule)); 1697 ni->itype.index.block_size = le32_to_cpu(ir->index_block_size); 1698 if (!is_power_of_2(ni->itype.index.block_size)) { 1699 ntfs_error(vi->i_sb, "Index block size (%u) is not a power of two.", 1700 ni->itype.index.block_size); 1701 goto unm_err_out; 1702 } 1703 if (ni->itype.index.block_size > PAGE_SIZE) { 1704 ntfs_error(vi->i_sb, "Index block size (%u) > PAGE_SIZE (%ld) is not supported.", 1705 ni->itype.index.block_size, PAGE_SIZE); 1706 err = -EOPNOTSUPP; 1707 goto unm_err_out; 1708 } 1709 if (ni->itype.index.block_size < NTFS_BLOCK_SIZE) { 1710 ntfs_error(vi->i_sb, 1711 "Index block size (%u) < NTFS_BLOCK_SIZE (%i) is not supported.", 1712 ni->itype.index.block_size, NTFS_BLOCK_SIZE); 1713 err = -EOPNOTSUPP; 1714 goto unm_err_out; 1715 } 1716 ni->itype.index.block_size_bits = ffs(ni->itype.index.block_size) - 1; 1717 /* Determine the size of a vcn in the index. */ 1718 if (vol->cluster_size <= ni->itype.index.block_size) { 1719 ni->itype.index.vcn_size = vol->cluster_size; 1720 ni->itype.index.vcn_size_bits = vol->cluster_size_bits; 1721 } else { 1722 ni->itype.index.vcn_size = NTFS_BLOCK_SIZE; 1723 ni->itype.index.vcn_size_bits = NTFS_BLOCK_SIZE_BITS; 1724 } 1725 1726 /* Find index allocation attribute. */ 1727 ntfs_attr_reinit_search_ctx(ctx); 1728 err = ntfs_attr_lookup(AT_INDEX_ALLOCATION, ni->name, ni->name_len, 1729 CASE_SENSITIVE, 0, NULL, 0, ctx); 1730 if (unlikely(err)) { 1731 if (err == -ENOENT) { 1732 /* No index allocation. */ 1733 vi->i_size = ni->initialized_size = ni->allocated_size = 0; 1734 /* We are done with the mft record, so we release it. */ 1735 ntfs_attr_put_search_ctx(ctx); 1736 unmap_mft_record(base_ni); 1737 m = NULL; 1738 ctx = NULL; 1739 goto skip_large_index_stuff; 1740 } else 1741 ntfs_error(vi->i_sb, "Failed to lookup $INDEX_ALLOCATION attribute."); 1742 goto unm_err_out; 1743 } 1744 NInoSetIndexAllocPresent(ni); 1745 NInoSetNonResident(ni); 1746 ni->type = AT_INDEX_ALLOCATION; 1747 1748 a = ctx->attr; 1749 if (!a->non_resident) { 1750 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is resident."); 1751 goto unm_err_out; 1752 } 1753 /* 1754 * Ensure the attribute name is placed before the mapping pairs array. 1755 */ 1756 if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >= 1757 le16_to_cpu(a->data.non_resident.mapping_pairs_offset)))) { 1758 ntfs_error(vol->sb, 1759 "$INDEX_ALLOCATION attribute name is placed after the mapping pairs array."); 1760 goto unm_err_out; 1761 } 1762 if (a->flags & ATTR_IS_ENCRYPTED) { 1763 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is encrypted."); 1764 goto unm_err_out; 1765 } 1766 if (a->flags & ATTR_IS_SPARSE) { 1767 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is sparse."); 1768 goto unm_err_out; 1769 } 1770 if (a->flags & ATTR_COMPRESSION_MASK) { 1771 ntfs_error(vi->i_sb, 1772 "$INDEX_ALLOCATION attribute is compressed."); 1773 goto unm_err_out; 1774 } 1775 if (a->data.non_resident.lowest_vcn) { 1776 ntfs_error(vi->i_sb, 1777 "First extent of $INDEX_ALLOCATION attribute has non zero lowest_vcn."); 1778 goto unm_err_out; 1779 } 1780 if (ntfs_non_resident_sizes_inconsistent(vi, a)) 1781 goto unm_err_out; 1782 vi->i_size = ni->data_size = le64_to_cpu(a->data.non_resident.data_size); 1783 ni->initialized_size = le64_to_cpu(a->data.non_resident.initialized_size); 1784 ni->allocated_size = le64_to_cpu(a->data.non_resident.allocated_size); 1785 /* 1786 * We are done with the mft record, so we release it. Otherwise 1787 * we would deadlock in ntfs_attr_iget(). 1788 */ 1789 ntfs_attr_put_search_ctx(ctx); 1790 unmap_mft_record(base_ni); 1791 m = NULL; 1792 ctx = NULL; 1793 /* Get the index bitmap attribute inode. */ 1794 bvi = ntfs_attr_iget(base_vi, AT_BITMAP, ni->name, ni->name_len); 1795 if (IS_ERR(bvi)) { 1796 err = PTR_ERR(bvi); 1797 if (err != -EINTR && err != -ERESTARTSYS) 1798 ntfs_error(vi->i_sb, "Failed to get bitmap attribute."); 1799 goto unm_err_out; 1800 } 1801 bni = NTFS_I(bvi); 1802 if (NInoCompressed(bni) || NInoEncrypted(bni) || 1803 NInoSparse(bni)) { 1804 ntfs_error(vi->i_sb, 1805 "$BITMAP attribute is compressed and/or encrypted and/or sparse."); 1806 goto iput_unm_err_out; 1807 } 1808 /* Consistency check bitmap size vs. index allocation size. */ 1809 bvi_size = i_size_read(bvi); 1810 if ((bvi_size << 3) < (vi->i_size >> ni->itype.index.block_size_bits)) { 1811 ntfs_error(vi->i_sb, 1812 "Index bitmap too small (0x%llx) for index allocation (0x%llx).", 1813 bvi_size << 3, vi->i_size); 1814 goto iput_unm_err_out; 1815 } 1816 iput(bvi); 1817 skip_large_index_stuff: 1818 /* Setup the operations for this index inode. */ 1819 ntfs_set_vfs_operations(vi, S_IFDIR, 0); 1820 vi->i_blocks = ni->allocated_size >> 9; 1821 /* 1822 * Make sure the base inode doesn't go away and attach it to the 1823 * index inode. 1824 */ 1825 if (!igrab(base_vi)) 1826 goto unm_err_out; 1827 ni->ext.base_ntfs_ino = base_ni; 1828 ni->nr_extents = -1; 1829 1830 ntfs_debug("Done."); 1831 return 0; 1832 iput_unm_err_out: 1833 iput(bvi); 1834 unm_err_out: 1835 if (!err) 1836 err = -EIO; 1837 if (ctx) 1838 ntfs_attr_put_search_ctx(ctx); 1839 if (m) 1840 unmap_mft_record(base_ni); 1841 err_out: 1842 if (err != -EINTR && err != -ERESTARTSYS) 1843 ntfs_error(vi->i_sb, 1844 "Failed with error code %i while reading index inode (mft_no 0x%llx, name_len %i.", 1845 err, ni->mft_no, ni->name_len); 1846 if (err != -EOPNOTSUPP && err != -ENOMEM && 1847 err != -EINTR && err != -ERESTARTSYS) 1848 NVolSetErrors(vol); 1849 return err; 1850 } 1851 1852 /* 1853 * load_attribute_list_mount - load an attribute list into memory 1854 * @vol: ntfs volume from which to read 1855 * @rl: runlist of the attribute list 1856 * @al_start: destination buffer 1857 * @size: size of the destination buffer in bytes 1858 * @initialized_size: initialized size of the attribute list 1859 * 1860 * Walk the runlist @rl and load all clusters from it copying them into 1861 * the linear buffer @al. The maximum number of bytes copied to @al is @size 1862 * bytes. Note, @size does not need to be a multiple of the cluster size. If 1863 * @initialized_size is less than @size, the region in @al between 1864 * @initialized_size and @size will be zeroed and not read from disk. 1865 * 1866 * Return 0 on success or -errno on error. 1867 */ 1868 static int load_attribute_list_mount(struct ntfs_volume *vol, 1869 struct runlist_element *rl, u8 *al_start, const s64 size, 1870 const s64 initialized_size) 1871 { 1872 s64 lcn; 1873 u8 *al = al_start; 1874 u8 *al_end = al + initialized_size; 1875 struct super_block *sb; 1876 int err = 0; 1877 loff_t rl_byte_off, rl_byte_len; 1878 1879 ntfs_debug("Entering."); 1880 if (!vol || !rl || !al || size <= 0 || initialized_size < 0 || 1881 initialized_size > size) 1882 return -EINVAL; 1883 if (!initialized_size) { 1884 memset(al, 0, size); 1885 return 0; 1886 } 1887 sb = vol->sb; 1888 1889 /* Read all clusters specified by the runlist one run at a time. */ 1890 while (rl->length) { 1891 lcn = ntfs_rl_vcn_to_lcn(rl, rl->vcn); 1892 ntfs_debug("Reading vcn = 0x%llx, lcn = 0x%llx.", 1893 (unsigned long long)rl->vcn, 1894 (unsigned long long)lcn); 1895 /* The attribute list cannot be sparse. */ 1896 if (lcn < 0) { 1897 ntfs_error(sb, "ntfs_rl_vcn_to_lcn() failed. Cannot read attribute list."); 1898 return -EIO; 1899 } 1900 1901 rl_byte_off = ntfs_cluster_to_bytes(vol, lcn); 1902 rl_byte_len = ntfs_cluster_to_bytes(vol, rl->length); 1903 1904 if (al + rl_byte_len > al_end) 1905 rl_byte_len = al_end - al; 1906 1907 err = ntfs_bdev_read(sb->s_bdev, al, rl_byte_off, 1908 round_up(rl_byte_len, SECTOR_SIZE)); 1909 if (err) { 1910 ntfs_error(sb, "Cannot read attribute list."); 1911 return -EIO; 1912 } 1913 1914 if (al + rl_byte_len >= al_end) { 1915 if (initialized_size < size) 1916 goto initialize; 1917 goto done; 1918 } 1919 1920 al += rl_byte_len; 1921 rl++; 1922 } 1923 if (initialized_size < size) { 1924 initialize: 1925 memset(al_start + initialized_size, 0, size - initialized_size); 1926 } 1927 done: 1928 return err; 1929 } 1930 1931 /* 1932 * The MFT inode has special locking, so teach the lock validator 1933 * about this by splitting off the locking rules of the MFT from 1934 * the locking rules of other inodes. The MFT inode can never be 1935 * accessed from the VFS side (or even internally), only by the 1936 * map_mft functions. 1937 */ 1938 static struct lock_class_key mft_ni_runlist_lock_key, mft_ni_mrec_lock_key; 1939 1940 /* 1941 * ntfs_read_inode_mount - special read_inode for mount time use only 1942 * @vi: inode to read 1943 * 1944 * Read inode FILE_MFT at mount time, only called with super_block lock 1945 * held from within the read_super() code path. 1946 * 1947 * This function exists because when it is called the page cache for $MFT/$DATA 1948 * is not initialized and hence we cannot get at the contents of mft records 1949 * by calling map_mft_record*(). 1950 * 1951 * Further it needs to cope with the circular references problem, i.e. cannot 1952 * load any attributes other than $ATTRIBUTE_LIST until $DATA is loaded, because 1953 * we do not know where the other extent mft records are yet and again, because 1954 * we cannot call map_mft_record*() yet. Obviously this applies only when an 1955 * attribute list is actually present in $MFT inode. 1956 * 1957 * We solve these problems by starting with the $DATA attribute before anything 1958 * else and iterating using ntfs_attr_lookup($DATA) over all extents. As each 1959 * extent is found, we ntfs_mapping_pairs_decompress() including the implied 1960 * ntfs_runlists_merge(). Each step of the iteration necessarily provides 1961 * sufficient information for the next step to complete. 1962 * 1963 * This should work but there are two possible pit falls (see inline comments 1964 * below), but only time will tell if they are real pits or just smoke... 1965 */ 1966 int ntfs_read_inode_mount(struct inode *vi) 1967 { 1968 s64 next_vcn, last_vcn, highest_vcn; 1969 struct super_block *sb = vi->i_sb; 1970 struct ntfs_volume *vol = NTFS_SB(sb); 1971 struct ntfs_inode *ni = NTFS_I(vi); 1972 struct mft_record *m = NULL; 1973 struct attr_record *a; 1974 struct ntfs_attr_search_ctx *ctx; 1975 unsigned int i; 1976 int err; 1977 size_t new_rl_count; 1978 1979 ntfs_debug("Entering."); 1980 1981 /* Initialize the ntfs specific part of @vi. */ 1982 ntfs_init_big_inode(vi); 1983 1984 1985 /* Setup the data attribute. It is special as it is mst protected. */ 1986 NInoSetNonResident(ni); 1987 NInoSetMstProtected(ni); 1988 NInoSetSparseDisabled(ni); 1989 ni->type = AT_DATA; 1990 ni->name = AT_UNNAMED; 1991 ni->name_len = 0; 1992 /* 1993 * This sets up our little cheat allowing us to reuse the async read io 1994 * completion handler for directories. 1995 */ 1996 ni->itype.index.block_size = vol->mft_record_size; 1997 ni->itype.index.block_size_bits = vol->mft_record_size_bits; 1998 1999 /* Very important! Needed to be able to call map_mft_record*(). */ 2000 vol->mft_ino = vi; 2001 2002 /* Allocate enough memory to read the first mft record. */ 2003 if (vol->mft_record_size > 64 * 1024) { 2004 ntfs_error(sb, "Unsupported mft record size %i (max 64kiB).", 2005 vol->mft_record_size); 2006 goto err_out; 2007 } 2008 2009 i = vol->mft_record_size; 2010 if (i < sb->s_blocksize) 2011 i = sb->s_blocksize; 2012 2013 m = kzalloc(i, GFP_NOFS); 2014 if (!m) { 2015 ntfs_error(sb, "Failed to allocate buffer for $MFT record 0."); 2016 goto err_out; 2017 } 2018 2019 /* Load $MFT/$DATA's first mft record. */ 2020 err = ntfs_bdev_read(sb->s_bdev, (char *)m, 2021 ntfs_cluster_to_bytes(vol, vol->mft_lcn), i); 2022 if (err) { 2023 ntfs_error(sb, "Device read failed."); 2024 goto err_out; 2025 } 2026 2027 if (le32_to_cpu(m->bytes_allocated) != vol->mft_record_size) { 2028 ntfs_error(sb, "Incorrect mft record size %u in superblock, should be %u.", 2029 le32_to_cpu(m->bytes_allocated), vol->mft_record_size); 2030 goto err_out; 2031 } 2032 2033 /* Apply the mst fixups. */ 2034 if (post_read_mst_fixup((struct ntfs_record *)m, vol->mft_record_size)) { 2035 ntfs_error(sb, "MST fixup failed. $MFT is corrupt."); 2036 goto err_out; 2037 } 2038 2039 if (ntfs_mft_record_check(vol, m, FILE_MFT)) { 2040 ntfs_error(sb, "ntfs_mft_record_check failed. $MFT is corrupt."); 2041 goto err_out; 2042 } 2043 2044 /* Need this to sanity check attribute list references to $MFT. */ 2045 vi->i_generation = ni->seq_no = le16_to_cpu(m->sequence_number); 2046 2047 /* Provides read_folio() for map_mft_record(). */ 2048 vi->i_mapping->a_ops = &ntfs_mft_aops; 2049 2050 ctx = ntfs_attr_get_search_ctx(ni, m); 2051 if (!ctx) { 2052 err = -ENOMEM; 2053 goto err_out; 2054 } 2055 2056 /* Find the attribute list attribute if present. */ 2057 err = ntfs_attr_lookup(AT_ATTRIBUTE_LIST, NULL, 0, 0, 0, NULL, 0, ctx); 2058 if (err) { 2059 if (unlikely(err != -ENOENT)) { 2060 ntfs_error(sb, 2061 "Failed to lookup attribute list attribute. You should run chkdsk."); 2062 goto put_err_out; 2063 } 2064 } else /* if (!err) */ { 2065 struct attr_list_entry *al_entry, *next_al_entry; 2066 u8 *al_end; 2067 s64 attr_list_size; 2068 static const char *es = " Not allowed. $MFT is corrupt. You should run chkdsk."; 2069 2070 ntfs_debug("Attribute list attribute found in $MFT."); 2071 NInoSetAttrList(ni); 2072 a = ctx->attr; 2073 if (a->flags & ATTR_COMPRESSION_MASK) { 2074 ntfs_error(sb, 2075 "Attribute list attribute is compressed.%s", 2076 es); 2077 goto put_err_out; 2078 } 2079 if (a->flags & ATTR_IS_ENCRYPTED || 2080 a->flags & ATTR_IS_SPARSE) { 2081 if (a->non_resident) { 2082 ntfs_error(sb, 2083 "Non-resident attribute list attribute is encrypted/sparse.%s", 2084 es); 2085 goto put_err_out; 2086 } 2087 ntfs_warning(sb, 2088 "Resident attribute list attribute in $MFT system file is marked encrypted/sparse which is not true. However, Windows allows this and chkdsk does not detect or correct it so we will just ignore the invalid flags and pretend they are not set."); 2089 } 2090 /* Now allocate memory for the attribute list. */ 2091 attr_list_size = ntfs_attr_size(a); 2092 if (attr_list_size <= 0 || 2093 attr_list_size > NTFS_MAX_ATTR_LIST_SIZE) { 2094 ntfs_error(sb, "Invalid attribute list size %lld (mft_no 0x%llx).%s", 2095 (long long)attr_list_size, ni->mft_no, es); 2096 goto put_err_out; 2097 } 2098 ni->attr_list_size = (u32)attr_list_size; 2099 ni->attr_list = kvzalloc(round_up(ni->attr_list_size, SECTOR_SIZE), 2100 GFP_NOFS); 2101 if (!ni->attr_list) { 2102 ntfs_error(sb, "Not enough memory to allocate buffer for attribute list."); 2103 goto put_err_out; 2104 } 2105 if (a->non_resident) { 2106 struct runlist_element *rl; 2107 size_t new_rl_count; 2108 2109 NInoSetAttrListNonResident(ni); 2110 if (a->data.non_resident.lowest_vcn) { 2111 ntfs_error(sb, 2112 "Attribute list has non zero lowest_vcn. $MFT is corrupt. You should run chkdsk."); 2113 goto put_err_out; 2114 } 2115 if (ntfs_non_resident_sizes_inconsistent(vi, a)) 2116 goto put_err_out; 2117 2118 rl = ntfs_mapping_pairs_decompress(vol, a, NULL, &new_rl_count); 2119 if (IS_ERR(rl)) { 2120 err = PTR_ERR(rl); 2121 ntfs_error(sb, 2122 "Mapping pairs decompression failed with error code %i.", 2123 -err); 2124 goto put_err_out; 2125 } 2126 2127 err = load_attribute_list_mount(vol, rl, ni->attr_list, ni->attr_list_size, 2128 le64_to_cpu(a->data.non_resident.initialized_size)); 2129 kvfree(rl); 2130 if (err) { 2131 ntfs_error(sb, 2132 "Failed to load attribute list with error code %i.", 2133 -err); 2134 goto put_err_out; 2135 } 2136 } else /* if (!ctx.attr->non_resident) */ { 2137 /* Now copy the attribute list. */ 2138 memcpy(ni->attr_list, (u8 *)a + le16_to_cpu( 2139 a->data.resident.value_offset), 2140 le32_to_cpu(a->data.resident.value_length)); 2141 } 2142 /* The attribute list is now setup in memory. */ 2143 al_entry = (struct attr_list_entry *)ni->attr_list; 2144 al_end = (u8 *)al_entry + ni->attr_list_size; 2145 for (;; al_entry = next_al_entry) { 2146 /* Out of bounds check. */ 2147 if ((u8 *)al_entry < ni->attr_list || 2148 (u8 *)al_entry > al_end) 2149 goto em_put_err_out; 2150 /* Catch the end of the attribute list. */ 2151 if ((u8 *)al_entry == al_end) 2152 goto em_put_err_out; 2153 if (!ntfs_attr_list_entry_is_valid(al_entry, al_end)) 2154 goto em_put_err_out; 2155 next_al_entry = (struct attr_list_entry *)((u8 *)al_entry + 2156 le16_to_cpu(al_entry->length)); 2157 if (le32_to_cpu(al_entry->type) > le32_to_cpu(AT_DATA)) 2158 goto em_put_err_out; 2159 if (al_entry->type != AT_DATA) 2160 continue; 2161 /* We want an unnamed attribute. */ 2162 if (al_entry->name_length) 2163 goto em_put_err_out; 2164 /* Want the first entry, i.e. lowest_vcn == 0. */ 2165 if (al_entry->lowest_vcn) 2166 goto em_put_err_out; 2167 /* First entry has to be in the base mft record. */ 2168 if (MREF_LE(al_entry->mft_reference) != vi->i_ino) { 2169 /* MFT references do not match, logic fails. */ 2170 ntfs_error(sb, 2171 "BUG: The first $DATA extent of $MFT is not in the base mft record."); 2172 goto put_err_out; 2173 } else { 2174 /* Sequence numbers must match. */ 2175 if (MSEQNO_LE(al_entry->mft_reference) != 2176 ni->seq_no) 2177 goto em_put_err_out; 2178 /* Got it. All is ok. We can stop now. */ 2179 break; 2180 } 2181 } 2182 } 2183 2184 ntfs_attr_reinit_search_ctx(ctx); 2185 2186 /* Now load all attribute extents. */ 2187 a = NULL; 2188 next_vcn = last_vcn = highest_vcn = 0; 2189 /* 2190 * Reading one of $MFT's own extent records in this loop can re-enter 2191 * ntfs_map_runlist_nolock() for $MFT; see the check there. 2192 */ 2193 NVolSetMftBootstrap(vol); 2194 while (!(err = ntfs_attr_lookup(AT_DATA, NULL, 0, 0, next_vcn, NULL, 0, 2195 ctx))) { 2196 struct runlist_element *nrl; 2197 2198 /* Cache the current attribute. */ 2199 a = ctx->attr; 2200 /* $MFT must be non-resident. */ 2201 if (!a->non_resident) { 2202 ntfs_error(sb, 2203 "$MFT must be non-resident but a resident extent was found. $MFT is corrupt. Run chkdsk."); 2204 goto put_err_out; 2205 } 2206 /* $MFT must be uncompressed and unencrypted. */ 2207 if (a->flags & ATTR_COMPRESSION_MASK || 2208 a->flags & ATTR_IS_ENCRYPTED || 2209 a->flags & ATTR_IS_SPARSE) { 2210 ntfs_error(sb, 2211 "$MFT must be uncompressed, non-sparse, and unencrypted but a compressed/sparse/encrypted extent was found. $MFT is corrupt. Run chkdsk."); 2212 goto put_err_out; 2213 } 2214 /* 2215 * Decompress the mapping pairs array of this extent and merge 2216 * the result into the existing runlist. No need for locking 2217 * as we have exclusive access to the inode at this time and we 2218 * are a mount in progress task, too. 2219 */ 2220 nrl = ntfs_mapping_pairs_decompress(vol, a, &ni->runlist, 2221 &new_rl_count); 2222 if (IS_ERR(nrl)) { 2223 ntfs_error(sb, 2224 "ntfs_mapping_pairs_decompress() failed with error code %ld.", 2225 PTR_ERR(nrl)); 2226 goto put_err_out; 2227 } 2228 ni->runlist.rl = nrl; 2229 ni->runlist.count = new_rl_count; 2230 2231 /* Are we in the first extent? */ 2232 if (!next_vcn) { 2233 if (a->data.non_resident.lowest_vcn) { 2234 ntfs_error(sb, 2235 "First extent of $DATA attribute has non zero lowest_vcn. $MFT is corrupt. You should run chkdsk."); 2236 goto put_err_out; 2237 } 2238 /* Get the last vcn in the $DATA attribute. */ 2239 last_vcn = ntfs_bytes_to_cluster(vol, 2240 le64_to_cpu(a->data.non_resident.allocated_size)); 2241 /* Fill in the inode size. */ 2242 vi->i_size = le64_to_cpu(a->data.non_resident.data_size); 2243 ni->initialized_size = le64_to_cpu(a->data.non_resident.initialized_size); 2244 ni->allocated_size = le64_to_cpu(a->data.non_resident.allocated_size); 2245 /* 2246 * Records between allocated_size and data_size are not 2247 * on disk, and would be read as zeros. 2248 */ 2249 if (vi->i_size > ni->allocated_size) { 2250 ntfs_error(sb, 2251 "$MFT data size %lld exceeds its allocated size %lld. $MFT is corrupt. Run chkdsk.", 2252 vi->i_size, ni->allocated_size); 2253 goto put_err_out; 2254 } 2255 /* 2256 * Verify the number of mft records does not exceed 2257 * 2^32 - 1. 2258 */ 2259 if ((vi->i_size >> vol->mft_record_size_bits) >= 2260 (1ULL << 32)) { 2261 ntfs_error(sb, "$MFT is too big! Aborting."); 2262 goto put_err_out; 2263 } 2264 /* 2265 * We have got the first extent of the runlist for 2266 * $MFT which means it is now relatively safe to call 2267 * the normal ntfs_read_inode() function. 2268 * Complete reading the inode, this will actually 2269 * re-read the mft record for $MFT, this time entering 2270 * it into the page cache with which we complete the 2271 * kick start of the volume. It should be safe to do 2272 * this now as the first extent of $MFT/$DATA is 2273 * already known and we would hope that we don't need 2274 * further extents in order to find the other 2275 * attributes belonging to $MFT. Only time will tell if 2276 * this is really the case. If not we will have to play 2277 * magic at this point, possibly duplicating a lot of 2278 * ntfs_read_inode() at this point. We will need to 2279 * ensure we do enough of its work to be able to call 2280 * ntfs_read_inode() on extents of $MFT/$DATA. But lets 2281 * hope this never happens... 2282 */ 2283 err = ntfs_read_locked_inode(vi); 2284 if (err) { 2285 ntfs_error(sb, "ntfs_read_inode() of $MFT failed.\n"); 2286 NVolClearMftBootstrap(vol); 2287 ntfs_attr_put_search_ctx(ctx); 2288 /* Revert to the safe super operations. */ 2289 kfree(m); 2290 return -1; 2291 } 2292 /* 2293 * Re-initialize some specifics about $MFT's inode as 2294 * ntfs_read_inode() will have set up the default ones. 2295 */ 2296 /* Set uid and gid to root. */ 2297 vi->i_uid = GLOBAL_ROOT_UID; 2298 vi->i_gid = GLOBAL_ROOT_GID; 2299 /* Regular file. No access for anyone. */ 2300 vi->i_mode = S_IFREG; 2301 /* No VFS initiated operations allowed for $MFT. */ 2302 vi->i_op = &ntfs_empty_inode_ops; 2303 vi->i_fop = &ntfs_empty_file_ops; 2304 } 2305 2306 /* Get the lowest vcn for the next extent. */ 2307 highest_vcn = le64_to_cpu(a->data.non_resident.highest_vcn); 2308 next_vcn = highest_vcn + 1; 2309 2310 /* Only one extent or error, which we catch below. */ 2311 if (next_vcn <= 0) 2312 break; 2313 2314 /* Avoid endless loops due to corruption. */ 2315 if (next_vcn < le64_to_cpu(a->data.non_resident.lowest_vcn)) { 2316 ntfs_error(sb, "$MFT has corrupt attribute list attribute. Run chkdsk."); 2317 goto put_err_out; 2318 } 2319 } 2320 NVolClearMftBootstrap(vol); 2321 if (err != -ENOENT) { 2322 ntfs_error(sb, "Failed to lookup $MFT/$DATA attribute extent. Run chkdsk.\n"); 2323 goto put_err_out; 2324 } 2325 if (!a) { 2326 ntfs_error(sb, "$MFT/$DATA attribute not found. $MFT is corrupt. Run chkdsk."); 2327 goto put_err_out; 2328 } 2329 if (highest_vcn && highest_vcn != last_vcn - 1) { 2330 ntfs_error(sb, "Failed to load the complete runlist for $MFT/$DATA. Run chkdsk."); 2331 ntfs_debug("highest_vcn = 0x%llx, last_vcn - 1 = 0x%llx", 2332 (unsigned long long)highest_vcn, 2333 (unsigned long long)last_vcn - 1); 2334 goto put_err_out; 2335 } 2336 ntfs_attr_put_search_ctx(ctx); 2337 ntfs_debug("Done."); 2338 kfree(m); 2339 2340 /* 2341 * Split the locking rules of the MFT inode from the 2342 * locking rules of other inodes: 2343 */ 2344 lockdep_set_class(&ni->runlist.lock, &mft_ni_runlist_lock_key); 2345 lockdep_set_class(&ni->mrec_lock, &mft_ni_mrec_lock_key); 2346 2347 return 0; 2348 2349 em_put_err_out: 2350 ntfs_error(sb, 2351 "Couldn't find first extent of $DATA attribute in attribute list. $MFT is corrupt. Run chkdsk."); 2352 put_err_out: 2353 ntfs_attr_put_search_ctx(ctx); 2354 err_out: 2355 /* Also reached from inside the $DATA loop. */ 2356 NVolClearMftBootstrap(vol); 2357 ntfs_error(sb, "Failed. Marking inode as bad."); 2358 kfree(m); 2359 return -1; 2360 } 2361 2362 static void __ntfs_clear_inode(struct ntfs_inode *ni) 2363 { 2364 /* Free all alocated memory. */ 2365 if (NInoNonResident(ni) && ni->runlist.rl) { 2366 kvfree(ni->runlist.rl); 2367 ni->runlist.rl = NULL; 2368 } 2369 2370 if (ni->attr_list) { 2371 kvfree(ni->attr_list); 2372 ni->attr_list = NULL; 2373 } 2374 2375 if (ni->name_len && ni->name != I30 && 2376 ni->name != reparse_index_name && 2377 ni->name != objid_index_name) { 2378 WARN_ON(!ni->name); 2379 kfree(ni->name); 2380 } 2381 } 2382 2383 void ntfs_clear_extent_inode(struct ntfs_inode *ni) 2384 { 2385 ntfs_debug("Entering for inode 0x%llx.", ni->mft_no); 2386 2387 WARN_ON(NInoAttr(ni)); 2388 WARN_ON(ni->nr_extents != -1); 2389 2390 __ntfs_clear_inode(ni); 2391 ntfs_destroy_extent_inode(ni); 2392 } 2393 2394 static int ntfs_delete_base_inode(struct ntfs_inode *ni) 2395 { 2396 struct super_block *sb = ni->vol->sb; 2397 int err; 2398 2399 if (NInoAttr(ni) || ni->nr_extents == -1) 2400 return 0; 2401 2402 err = ntfs_non_resident_dealloc_clusters(ni); 2403 2404 /* 2405 * Deallocate extent mft records and free extent inodes. 2406 * No need to lock as no one else has a reference. 2407 */ 2408 while (ni->nr_extents) { 2409 err = ntfs_mft_record_free(ni->vol, *(ni->ext.extent_ntfs_inos)); 2410 if (err) 2411 ntfs_error(sb, 2412 "Failed to free extent MFT record. Leaving inconsistent metadata.\n"); 2413 ntfs_inode_close(*(ni->ext.extent_ntfs_inos)); 2414 } 2415 2416 /* Deallocate base mft record */ 2417 err = ntfs_mft_record_free(ni->vol, ni); 2418 if (err) 2419 ntfs_error(sb, "Failed to free base MFT record. Leaving inconsistent metadata.\n"); 2420 return err; 2421 } 2422 2423 /* 2424 * ntfs_evict_big_inode - clean up the ntfs specific part of an inode 2425 * @vi: vfs inode pending annihilation 2426 * 2427 * When the VFS is going to remove an inode from memory, ntfs_clear_big_inode() 2428 * is called, which deallocates all memory belonging to the NTFS specific part 2429 * of the inode and returns. 2430 * 2431 * If the MFT record is dirty, we commit it before doing anything else. 2432 */ 2433 void ntfs_evict_big_inode(struct inode *vi) 2434 { 2435 struct ntfs_inode *ni = NTFS_I(vi); 2436 2437 truncate_inode_pages_final(&vi->i_data); 2438 2439 if (!vi->i_nlink) { 2440 if (!NInoAttr(ni)) { 2441 /* Never called with extent inodes */ 2442 WARN_ON(ni->nr_extents == -1); 2443 ntfs_delete_base_inode(ni); 2444 } 2445 goto release; 2446 } 2447 2448 if (NInoDirty(ni)) { 2449 /* Committing the inode also commits all extent inodes. */ 2450 ntfs_commit_inode(vi); 2451 2452 if (NInoDirty(ni)) { 2453 ntfs_debug("Failed to commit dirty inode 0x%llx. Losing data!", 2454 ni->mft_no); 2455 NInoClearAttrListDirty(ni); 2456 NInoClearDirty(ni); 2457 } 2458 } 2459 2460 /* No need to lock at this stage as no one else has a reference. */ 2461 if (ni->nr_extents > 0) { 2462 int i; 2463 2464 for (i = 0; i < ni->nr_extents; i++) { 2465 if (ni->ext.extent_ntfs_inos[i]) 2466 ntfs_clear_extent_inode(ni->ext.extent_ntfs_inos[i]); 2467 } 2468 ni->nr_extents = 0; 2469 kvfree(ni->ext.extent_ntfs_inos); 2470 } 2471 2472 release: 2473 clear_inode(vi); 2474 __ntfs_clear_inode(ni); 2475 2476 if (NInoAttr(ni)) { 2477 /* Release the base inode if we are holding it. */ 2478 if (ni->nr_extents == -1) { 2479 iput(VFS_I(ni->ext.base_ntfs_ino)); 2480 ni->nr_extents = 0; 2481 ni->ext.base_ntfs_ino = NULL; 2482 } 2483 } 2484 2485 if (!atomic_dec_and_test(&ni->count)) 2486 WARN_ON(1); 2487 if (ni->folio) 2488 folio_put(ni->folio); 2489 kfree(ni->mrec); 2490 kvfree(ni->target); 2491 } 2492 2493 /* 2494 * ntfs_show_options - show mount options in /proc/mounts 2495 * @sf: seq_file in which to write our mount options 2496 * @root: root of the mounted tree whose mount options to display 2497 * 2498 * Called by the VFS once for each mounted ntfs volume when someone reads 2499 * /proc/mounts in order to display the NTFS specific mount options of each 2500 * mount. The mount options of fs specified by @root are written to the seq file 2501 * @sf and success is returned. 2502 */ 2503 int ntfs_show_options(struct seq_file *sf, struct dentry *root) 2504 { 2505 struct ntfs_volume *vol = NTFS_SB(root->d_sb); 2506 int i; 2507 2508 if (uid_valid(vol->uid)) 2509 seq_printf(sf, ",uid=%i", from_kuid_munged(&init_user_ns, vol->uid)); 2510 if (gid_valid(vol->gid)) 2511 seq_printf(sf, ",gid=%i", from_kgid_munged(&init_user_ns, vol->gid)); 2512 if (vol->fmask == vol->dmask) 2513 seq_printf(sf, ",umask=0%o", vol->fmask); 2514 else { 2515 seq_printf(sf, ",fmask=0%o", vol->fmask); 2516 seq_printf(sf, ",dmask=0%o", vol->dmask); 2517 } 2518 seq_printf(sf, ",iocharset=%s", vol->nls_map->charset); 2519 if (NVolCaseSensitive(vol)) 2520 seq_puts(sf, ",case_sensitive"); 2521 else 2522 seq_puts(sf, ",nocase"); 2523 if (NVolShowSystemFiles(vol)) 2524 seq_puts(sf, ",show_sys_files,showmeta"); 2525 for (i = 0; on_errors_arr[i].val; i++) { 2526 if (on_errors_arr[i].val == vol->on_errors) 2527 seq_printf(sf, ",errors=%s", on_errors_arr[i].str); 2528 } 2529 seq_printf(sf, ",mft_zone_multiplier=%i", vol->mft_zone_multiplier); 2530 if (NVolSysImmutable(vol)) 2531 seq_puts(sf, ",sys_immutable"); 2532 if (!NVolShowHiddenFiles(vol)) 2533 seq_puts(sf, ",nohidden"); 2534 if (NVolHideDotFiles(vol)) 2535 seq_puts(sf, ",hide_dot_files"); 2536 if (NVolCheckWindowsNames(vol)) 2537 seq_puts(sf, ",windows_names"); 2538 if (NVolDiscard(vol)) 2539 seq_puts(sf, ",discard"); 2540 if (NVolDisableSparse(vol)) 2541 seq_puts(sf, ",disable_sparse"); 2542 if (NVolNativeSymlinkRel(vol)) 2543 seq_puts(sf, ",native_symlink=rel"); 2544 else 2545 seq_puts(sf, ",native_symlink=raw"); 2546 if (NVolSymlinkNative(vol)) 2547 seq_puts(sf, ",symlink=native"); 2548 else 2549 seq_puts(sf, ",symlink=wsl"); 2550 if (NVolStreamsWindows(vol)) 2551 seq_puts(sf, ",streams_interface=windows"); 2552 else 2553 seq_puts(sf, ",streams_interface=none"); 2554 if (vol->sb->s_flags & SB_POSIXACL) 2555 seq_puts(sf, ",acl"); 2556 return 0; 2557 } 2558 2559 /* 2560 * The caller must hold @vi's mapping invalidate lock. 2561 */ 2562 static int __ntfs_extend_initialized_size(struct inode *vi, 2563 const loff_t offset, 2564 const loff_t new_size) 2565 { 2566 struct ntfs_inode *ni = NTFS_I(vi); 2567 struct ntfs_inode *mrec_ni = ntfs_base_inode(ni); 2568 loff_t old_init_size, clear_size; 2569 unsigned long flags; 2570 int err = 0; 2571 2572 lockdep_assert_held_write(&vi->i_mapping->invalidate_lock); 2573 2574 if (!NInoNonResident(ni)) 2575 return -EINVAL; 2576 2577 read_lock_irqsave(&ni->size_lock, flags); 2578 old_init_size = ni->initialized_size; 2579 read_unlock_irqrestore(&ni->size_lock, flags); 2580 2581 if (old_init_size >= new_size) 2582 goto out; 2583 2584 err = ntfs_attr_map_whole_runlist(ni); 2585 if (err) 2586 goto out; 2587 2588 if (!NInoCompressed(ni)) { 2589 /* 2590 * Zeroing skips holes, so discard cached uninitialized bytes 2591 * before making them visible to writes or shared mappings. 2592 */ 2593 clear_size = max_t(loff_t, i_size_read(vi), new_size); 2594 truncate_pagecache_range(vi, old_init_size, clear_size - 1); 2595 2596 if (old_init_size < offset) { 2597 err = iomap_zero_range(vi, old_init_size, 2598 offset - old_init_size, 2599 NULL, ntfs_seek_iomap_next, 2600 &ntfs_iomap_folio_ops, NULL); 2601 if (err) 2602 goto out; 2603 } 2604 } 2605 2606 mutex_lock(&mrec_ni->mrec_lock); 2607 err = ntfs_attr_set_initialized_size(ni, new_size); 2608 mutex_unlock(&mrec_ni->mrec_lock); 2609 out: 2610 return err; 2611 } 2612 2613 int ntfs_extend_initialized_size(struct inode *vi, const loff_t offset, 2614 const loff_t new_size) 2615 { 2616 int err; 2617 2618 filemap_invalidate_lock(vi->i_mapping); 2619 err = __ntfs_extend_initialized_size(vi, offset, new_size); 2620 filemap_invalidate_unlock(vi->i_mapping); 2621 return err; 2622 } 2623 2624 int ntfs_truncate_vfs(struct inode *vi, loff_t new_size, loff_t i_size) 2625 { 2626 struct ntfs_inode *ni = NTFS_I(vi); 2627 struct ntfs_inode *mrec_ni = ntfs_base_inode(ni); 2628 int err; 2629 2630 mutex_lock(&mrec_ni->mrec_lock); 2631 err = __ntfs_attr_truncate_vfs(ni, new_size, i_size); 2632 mutex_unlock(&mrec_ni->mrec_lock); 2633 if (err < 0) 2634 return err; 2635 2636 if (ntfs_inode_is_named_stream(ni)) 2637 ntfs_stream_update_base_time(mrec_ni); 2638 else 2639 inode_set_mtime_to_ts(vi, inode_set_ctime_current(vi)); 2640 return 0; 2641 } 2642 2643 /* 2644 * ntfs_inode_sync_standard_information - update standard information attribute 2645 * @vi: inode to update standard information 2646 * @m: mft record 2647 * 2648 * Return 0 on success or -errno on error. 2649 */ 2650 static int ntfs_inode_sync_standard_information(struct inode *vi, struct mft_record *m) 2651 { 2652 struct ntfs_inode *ni = NTFS_I(vi); 2653 struct ntfs_attr_search_ctx *ctx; 2654 struct standard_information *si; 2655 __le64 nt; 2656 int err = 0; 2657 bool modified = false; 2658 2659 /* Update the access times in the standard information attribute. */ 2660 ctx = ntfs_attr_get_search_ctx(ni, m); 2661 if (unlikely(!ctx)) 2662 return -ENOMEM; 2663 err = ntfs_attr_lookup(AT_STANDARD_INFORMATION, NULL, 0, 2664 CASE_SENSITIVE, 0, NULL, 0, ctx); 2665 if (unlikely(err)) { 2666 ntfs_attr_put_search_ctx(ctx); 2667 return err; 2668 } 2669 si = (struct standard_information *)((u8 *)ctx->attr + 2670 le16_to_cpu(ctx->attr->data.resident.value_offset)); 2671 if (si->file_attributes != ni->flags) { 2672 si->file_attributes = ni->flags; 2673 modified = true; 2674 } 2675 2676 /* Update the creation times if they have changed. */ 2677 nt = utc2ntfs(ni->i_crtime); 2678 if (si->creation_time != nt) { 2679 ntfs_debug("Updating creation time for inode 0x%llx: old = 0x%llx, new = 0x%llx", 2680 ni->mft_no, le64_to_cpu(si->creation_time), 2681 le64_to_cpu(nt)); 2682 si->creation_time = nt; 2683 modified = true; 2684 } 2685 2686 /* Update the access times if they have changed. */ 2687 nt = utc2ntfs(inode_get_mtime(vi)); 2688 if (si->last_data_change_time != nt) { 2689 ntfs_debug("Updating mtime for inode 0x%llx: old = 0x%llx, new = 0x%llx", 2690 ni->mft_no, le64_to_cpu(si->last_data_change_time), 2691 le64_to_cpu(nt)); 2692 si->last_data_change_time = nt; 2693 modified = true; 2694 } 2695 2696 nt = utc2ntfs(inode_get_ctime(vi)); 2697 if (si->last_mft_change_time != nt) { 2698 ntfs_debug("Updating ctime for inode 0x%llx: old = 0x%llx, new = 0x%llx", 2699 ni->mft_no, le64_to_cpu(si->last_mft_change_time), 2700 le64_to_cpu(nt)); 2701 si->last_mft_change_time = nt; 2702 modified = true; 2703 } 2704 nt = utc2ntfs(inode_get_atime(vi)); 2705 if (si->last_access_time != nt) { 2706 ntfs_debug("Updating atime for inode 0x%llx: old = 0x%llx, new = 0x%llx", 2707 ni->mft_no, 2708 le64_to_cpu(si->last_access_time), 2709 le64_to_cpu(nt)); 2710 si->last_access_time = nt; 2711 modified = true; 2712 } 2713 2714 /* 2715 * If we just modified the standard information attribute we need to 2716 * mark the mft record it is in dirty. We do this manually so that 2717 * mark_inode_dirty() is not called which would redirty the inode and 2718 * hence result in an infinite loop of trying to write the inode. 2719 * There is no need to mark the base inode nor the base mft record 2720 * dirty, since we are going to write this mft record below in any case 2721 * and the base mft record may actually not have been modified so it 2722 * might not need to be written out. 2723 * NOTE: It is not a problem when the inode for $MFT itself is being 2724 * written out as ntfs_mft_mark_dirty() will only set I_DIRTY_PAGES 2725 * on the $MFT inode and hence ntfs_write_inode() will not be 2726 * re-invoked because of it which in turn is ok since the dirtied mft 2727 * record will be cleaned and written out to disk below, i.e. before 2728 * this function returns. 2729 */ 2730 if (modified) 2731 NInoSetDirty(ctx->ntfs_ino); 2732 ntfs_attr_put_search_ctx(ctx); 2733 2734 return err; 2735 } 2736 2737 /* 2738 * ntfs_inode_sync_filename - update FILE_NAME attributes 2739 * @ni: ntfs inode to update FILE_NAME attributes 2740 * 2741 * Update all FILE_NAME attributes for inode @ni in the index. 2742 * 2743 * Return 0 on success or error. 2744 */ 2745 int ntfs_inode_sync_filename(struct ntfs_inode *ni) 2746 { 2747 struct inode *index_vi; 2748 struct super_block *sb = VFS_I(ni)->i_sb; 2749 struct ntfs_attr_search_ctx *ctx = NULL; 2750 struct ntfs_index_context *ictx; 2751 struct ntfs_inode *index_ni; 2752 struct file_name_attr *fn; 2753 struct file_name_attr *fnx; 2754 struct reparse_point *rpp; 2755 __le32 reparse_tag; 2756 int err = 0; 2757 unsigned long flags; 2758 2759 ntfs_debug("Entering for inode %llu\n", ni->mft_no); 2760 2761 ctx = ntfs_attr_get_search_ctx(ni, NULL); 2762 if (!ctx) 2763 return -ENOMEM; 2764 2765 /* Collect the reparse tag, if any */ 2766 reparse_tag = cpu_to_le32(0); 2767 if (ni->flags & FILE_ATTR_REPARSE_POINT) { 2768 if (!ntfs_attr_lookup(AT_REPARSE_POINT, NULL, 2769 0, CASE_SENSITIVE, 0, NULL, 0, ctx)) { 2770 rpp = (struct reparse_point *)((u8 *)ctx->attr + 2771 le16_to_cpu(ctx->attr->data.resident.value_offset)); 2772 reparse_tag = rpp->reparse_tag; 2773 } 2774 ntfs_attr_reinit_search_ctx(ctx); 2775 } 2776 2777 /* Walk through all FILE_NAME attributes and update them. */ 2778 while (!(err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, 0, 0, NULL, 0, ctx))) { 2779 fn = (struct file_name_attr *)((u8 *)ctx->attr + 2780 le16_to_cpu(ctx->attr->data.resident.value_offset)); 2781 if (MREF_LE(fn->parent_directory) == ni->mft_no) 2782 continue; 2783 2784 index_vi = ntfs_iget(sb, MREF_LE(fn->parent_directory)); 2785 if (IS_ERR(index_vi)) { 2786 ntfs_error(sb, "Failed to open inode %lld with index", 2787 (long long)MREF_LE(fn->parent_directory)); 2788 continue; 2789 } 2790 2791 index_ni = NTFS_I(index_vi); 2792 2793 mutex_lock_nested(&index_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT); 2794 if (NInoBeingDeleted(ni)) { 2795 mutex_unlock(&index_ni->mrec_lock); 2796 iput(index_vi); 2797 continue; 2798 } 2799 2800 ictx = ntfs_index_ctx_get(index_ni, I30, 4); 2801 if (!ictx) { 2802 ntfs_error(sb, "Failed to get index ctx, inode %llu", 2803 index_ni->mft_no); 2804 mutex_unlock(&index_ni->mrec_lock); 2805 iput(index_vi); 2806 continue; 2807 } 2808 2809 err = ntfs_index_lookup(fn, sizeof(struct file_name_attr), ictx); 2810 if (err) { 2811 ntfs_debug("Index lookup failed, inode %llu", 2812 index_ni->mft_no); 2813 ntfs_index_ctx_put(ictx); 2814 mutex_unlock(&index_ni->mrec_lock); 2815 iput(index_vi); 2816 continue; 2817 } 2818 /* Update flags and file size. */ 2819 fnx = (struct file_name_attr *)ictx->data; 2820 fnx->file_attributes = 2821 (fnx->file_attributes & ~FILE_ATTR_VALID_FLAGS) | 2822 (ni->flags & FILE_ATTR_VALID_FLAGS); 2823 if (ctx->mrec->flags & MFT_RECORD_IS_DIRECTORY) 2824 fnx->data_size = fnx->allocated_size = 0; 2825 else { 2826 read_lock_irqsave(&ni->size_lock, flags); 2827 if (NInoSparse(ni) || NInoCompressed(ni)) 2828 fnx->allocated_size = cpu_to_le64(ni->itype.compressed.size); 2829 else 2830 fnx->allocated_size = cpu_to_le64(ni->allocated_size); 2831 fnx->data_size = cpu_to_le64(ni->data_size); 2832 2833 /* 2834 * The file name record has also to be fixed if some 2835 * attribute update implied the unnamed data to be 2836 * made non-resident 2837 */ 2838 fn->allocated_size = fnx->allocated_size; 2839 fn->data_size = fnx->data_size; 2840 read_unlock_irqrestore(&ni->size_lock, flags); 2841 } 2842 2843 /* update or clear the reparse tag in the index */ 2844 fnx->type.rp.reparse_point_tag = reparse_tag; 2845 fnx->creation_time = fn->creation_time; 2846 fnx->last_data_change_time = fn->last_data_change_time; 2847 fnx->last_mft_change_time = fn->last_mft_change_time; 2848 fnx->last_access_time = fn->last_access_time; 2849 ntfs_index_entry_mark_dirty(ictx); 2850 ntfs_icx_ib_sync_write(ictx); 2851 NInoSetDirty(ctx->ntfs_ino); 2852 ntfs_index_ctx_put(ictx); 2853 mutex_unlock(&index_ni->mrec_lock); 2854 iput(index_vi); 2855 } 2856 /* Check for real error occurred. */ 2857 if (err != -ENOENT) { 2858 ntfs_error(sb, "Attribute lookup failed, err : %d, inode %llu", err, 2859 ni->mft_no); 2860 } else 2861 err = 0; 2862 2863 ntfs_attr_put_search_ctx(ctx); 2864 return err; 2865 } 2866 2867 int ntfs_get_block_mft_record(struct ntfs_inode *mft_ni, struct ntfs_inode *ni) 2868 { 2869 s64 vcn; 2870 struct runlist_element *rl; 2871 2872 if (ni->mft_lcn[0] != LCN_RL_NOT_MAPPED) 2873 return 0; 2874 2875 vcn = (s64)ni->mft_no << mft_ni->vol->mft_record_size_bits >> 2876 mft_ni->vol->cluster_size_bits; 2877 2878 rl = mft_ni->runlist.rl; 2879 if (!rl) { 2880 ntfs_error(mft_ni->vol->sb, "$MFT runlist is not present"); 2881 return -EIO; 2882 } 2883 2884 /* Seek to element containing target vcn. */ 2885 while (rl->length && rl[1].vcn <= vcn) 2886 rl++; 2887 ni->mft_lcn[0] = ntfs_rl_vcn_to_lcn(rl, vcn); 2888 ni->mft_lcn_count = 1; 2889 2890 if (mft_ni->vol->cluster_size < mft_ni->vol->mft_record_size && 2891 (rl->length - (vcn - rl->vcn)) <= 1) { 2892 rl++; 2893 ni->mft_lcn[1] = ntfs_rl_vcn_to_lcn(rl, vcn + 1); 2894 ni->mft_lcn_count++; 2895 } 2896 return 0; 2897 } 2898 2899 /* 2900 * __ntfs_write_inode - write out a dirty inode 2901 * @vi: inode to write out 2902 * @sync: if true, write out synchronously 2903 * 2904 * Write out a dirty inode to disk including any extent inodes if present. 2905 * 2906 * If @sync is true, commit the inode to disk and wait for io completion. This 2907 * is done using write_mft_record(). 2908 * 2909 * If @sync is false, just schedule the write to happen but do not wait for i/o 2910 * completion. 2911 * 2912 * Return 0 on success and -errno on error. 2913 */ 2914 int __ntfs_write_inode(struct inode *vi, int sync) 2915 { 2916 struct ntfs_inode *ni = NTFS_I(vi); 2917 struct ntfs_inode *mft_ni = NTFS_I(ni->vol->mft_ino); 2918 struct mft_record *m; 2919 int err = 0; 2920 bool need_iput = false; 2921 2922 ntfs_debug("Entering for %sinode 0x%llx.", NInoAttr(ni) ? "attr " : "", 2923 ni->mft_no); 2924 2925 if (NVolShutdown(ni->vol)) 2926 return -EIO; 2927 2928 /* 2929 * Dirty attribute inodes are written via their real inodes so just 2930 * clean them here. Access time updates are taken care off when the 2931 * real inode is written. 2932 */ 2933 if (NInoAttr(ni) || ni->nr_extents == -1) { 2934 NInoClearDirty(ni); 2935 ntfs_debug("Done."); 2936 return 0; 2937 } 2938 2939 /* igrab prevents vi from being evicted while mrec_lock is hold. */ 2940 if (igrab(vi) != NULL) 2941 need_iput = true; 2942 2943 mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL); 2944 /* Map, pin, and lock the mft record belonging to the inode. */ 2945 m = map_mft_record(ni); 2946 if (IS_ERR(m)) { 2947 mutex_unlock(&ni->mrec_lock); 2948 err = PTR_ERR(m); 2949 goto err_out; 2950 } 2951 2952 if (NInoNonResident(ni) && NInoRunlistDirty(ni)) { 2953 down_write(&ni->runlist.lock); 2954 err = ntfs_attr_update_mapping_pairs_locked(ni, 0, ni); 2955 if (!err) 2956 NInoClearRunlistDirty(ni); 2957 up_write(&ni->runlist.lock); 2958 } 2959 2960 err = ntfs_inode_sync_standard_information(vi, m); 2961 if (err) 2962 goto unm_err_out; 2963 2964 /* 2965 * when being umounted and inodes are evicted, write_inode() 2966 * is called with all inodes being marked with I_FREEING. 2967 * then ntfs_inode_sync_filename() waits infinitly because 2968 * of ntfs_iget. This situation happens only where sync_filesysem() 2969 * from umount fails because of a disk unplug and etc. 2970 * the absent of SB_ACTIVE means umounting. 2971 */ 2972 if ((vi->i_sb->s_flags & SB_ACTIVE) && NInoTestClearFileNameDirty(ni)) 2973 ntfs_inode_sync_filename(ni); 2974 2975 /* Now the access times are updated, write the base mft record. */ 2976 if (NInoDirty(ni)) { 2977 down_read(&mft_ni->runlist.lock); 2978 err = ntfs_get_block_mft_record(mft_ni, ni); 2979 up_read(&mft_ni->runlist.lock); 2980 if (err) 2981 goto unm_err_out; 2982 2983 err = write_mft_record(ni, m, sync); 2984 if (err) 2985 ntfs_error(vi->i_sb, "write_mft_record failed, err : %d\n", err); 2986 } 2987 unmap_mft_record(ni); 2988 2989 /* Map any unmapped extent mft records with LCNs. */ 2990 down_read(&mft_ni->runlist.lock); 2991 mutex_lock(&ni->extent_lock); 2992 if (ni->nr_extents > 0) { 2993 int i; 2994 2995 for (i = 0; i < ni->nr_extents; i++) { 2996 err = ntfs_get_block_mft_record(mft_ni, 2997 ni->ext.extent_ntfs_inos[i]); 2998 if (err) { 2999 mutex_unlock(&ni->extent_lock); 3000 up_read(&mft_ni->runlist.lock); 3001 mutex_unlock(&ni->mrec_lock); 3002 goto err_out; 3003 } 3004 } 3005 } 3006 mutex_unlock(&ni->extent_lock); 3007 up_read(&mft_ni->runlist.lock); 3008 3009 /* Write all attached extent mft records. */ 3010 mutex_lock(&ni->extent_lock); 3011 if (ni->nr_extents > 0) { 3012 struct ntfs_inode **extent_nis = ni->ext.extent_ntfs_inos; 3013 int i; 3014 3015 ntfs_debug("Writing %i extent inodes.", ni->nr_extents); 3016 for (i = 0; i < ni->nr_extents; i++) { 3017 struct ntfs_inode *tni = extent_nis[i]; 3018 3019 if (NInoDirty(tni)) { 3020 struct mft_record *tm; 3021 int ret; 3022 3023 mutex_lock(&tni->mrec_lock); 3024 tm = map_mft_record(tni); 3025 if (IS_ERR(tm)) { 3026 mutex_unlock(&tni->mrec_lock); 3027 if (!err || err == -ENOMEM) 3028 err = PTR_ERR(tm); 3029 continue; 3030 } 3031 3032 ret = write_mft_record(tni, tm, sync); 3033 unmap_mft_record(tni); 3034 mutex_unlock(&tni->mrec_lock); 3035 3036 if (unlikely(ret)) { 3037 if (!err || err == -ENOMEM) 3038 err = ret; 3039 } 3040 } 3041 } 3042 } 3043 mutex_unlock(&ni->extent_lock); 3044 mutex_unlock(&ni->mrec_lock); 3045 3046 if (unlikely(err)) 3047 goto err_out; 3048 if (need_iput) 3049 iput(vi); 3050 ntfs_debug("Done."); 3051 return 0; 3052 unm_err_out: 3053 unmap_mft_record(ni); 3054 mutex_unlock(&ni->mrec_lock); 3055 err_out: 3056 if (err == -ENOMEM) 3057 mark_inode_dirty(vi); 3058 else { 3059 ntfs_error(vi->i_sb, "Failed (error %i): Run chkdsk.", -err); 3060 NVolSetErrors(ni->vol); 3061 } 3062 if (need_iput) 3063 iput(vi); 3064 return err; 3065 } 3066 3067 /* 3068 * ntfs_extent_inode_open - load an extent inode and attach it to its base 3069 * @base_ni: base ntfs inode 3070 * @mref: mft reference of the extent inode to load (in little endian) 3071 * 3072 * First check if the extent inode @mref is already attached to the base ntfs 3073 * inode @base_ni, and if so, return a pointer to the attached extent inode. 3074 * 3075 * If the extent inode is not already attached to the base inode, allocate an 3076 * ntfs_inode structure and initialize it for the given inode @mref. @mref 3077 * specifies the inode number / mft record to read, including the sequence 3078 * number, which can be 0 if no sequence number checking is to be performed. 3079 * 3080 * Then, allocate a buffer for the mft record, read the mft record from the 3081 * volume @base_ni->vol, and attach it to the ntfs_inode structure (->mrec). 3082 * The mft record is mst deprotected and sanity checked for validity and we 3083 * abort if deprotection or checks fail. 3084 * 3085 * Finally attach the ntfs inode to its base inode @base_ni and return a 3086 * pointer to the ntfs_inode structure on success or ERR_PTR() on error. 3087 * 3088 * Note, extent inodes are never closed directly. They are automatically 3089 * disposed off by the closing of the base inode. 3090 */ 3091 static struct ntfs_inode *ntfs_extent_inode_open(struct ntfs_inode *base_ni, 3092 const __le64 mref) 3093 { 3094 u64 mft_no = MREF_LE(mref); 3095 struct ntfs_inode *ni = NULL; 3096 struct ntfs_inode **extent_nis; 3097 int i; 3098 struct mft_record *ni_mrec; 3099 struct super_block *sb; 3100 3101 if (!base_ni) 3102 return ERR_PTR(-EINVAL); 3103 3104 sb = base_ni->vol->sb; 3105 ntfs_debug("Opening extent inode %llu (base mft record %llu).\n", 3106 mft_no, base_ni->mft_no); 3107 3108 /* Is the extent inode already open and attached to the base inode? */ 3109 if (base_ni->nr_extents > 0) { 3110 extent_nis = base_ni->ext.extent_ntfs_inos; 3111 for (i = 0; i < base_ni->nr_extents; i++) { 3112 u16 seq_no; 3113 3114 ni = extent_nis[i]; 3115 if (mft_no != ni->mft_no) 3116 continue; 3117 ni_mrec = map_mft_record(ni); 3118 if (IS_ERR(ni_mrec)) { 3119 ntfs_error(sb, "failed to map mft record for %llu", 3120 ni->mft_no); 3121 return ERR_CAST(ni_mrec); 3122 } 3123 /* Verify the sequence number if given. */ 3124 seq_no = MSEQNO_LE(mref); 3125 if (seq_no && 3126 seq_no != le16_to_cpu(ni_mrec->sequence_number)) { 3127 ntfs_error(sb, "Found stale extent mft reference mft=%llu", 3128 ni->mft_no); 3129 unmap_mft_record(ni); 3130 return ERR_PTR(-EIO); 3131 } 3132 unmap_mft_record(ni); 3133 goto out; 3134 } 3135 } 3136 /* Wasn't there, we need to load the extent inode. */ 3137 ni = ntfs_new_extent_inode(base_ni->vol->sb, mft_no); 3138 if (!ni) 3139 return ERR_PTR(-ENOMEM); 3140 3141 ni->seq_no = (u16)MSEQNO_LE(mref); 3142 ni->nr_extents = -1; 3143 ni->ext.base_ntfs_ino = base_ni; 3144 /* Attach extent inode to base inode, reallocating memory if needed. */ 3145 if (!(base_ni->nr_extents & 3)) { 3146 i = (base_ni->nr_extents + 4) * sizeof(struct ntfs_inode *); 3147 3148 extent_nis = kvzalloc(i, GFP_NOFS); 3149 if (!extent_nis) { 3150 ntfs_destroy_ext_inode(ni); 3151 return ERR_PTR(-ENOMEM); 3152 } 3153 if (base_ni->nr_extents) { 3154 memcpy(extent_nis, base_ni->ext.extent_ntfs_inos, 3155 i - 4 * sizeof(struct ntfs_inode *)); 3156 kvfree(base_ni->ext.extent_ntfs_inos); 3157 } 3158 base_ni->ext.extent_ntfs_inos = extent_nis; 3159 } 3160 base_ni->ext.extent_ntfs_inos[base_ni->nr_extents++] = ni; 3161 3162 out: 3163 ntfs_debug("\n"); 3164 return ni; 3165 } 3166 3167 /* 3168 * ntfs_inode_attach_all_extents - attach all extents for target inode 3169 * @ni: opened ntfs inode for which perform attach 3170 * 3171 * Return 0 on success and error. 3172 */ 3173 int ntfs_inode_attach_all_extents(struct ntfs_inode *ni) 3174 { 3175 struct attr_list_entry *ale; 3176 u64 prev_attached = 0; 3177 3178 if (!ni) { 3179 ntfs_debug("Invalid arguments.\n"); 3180 return -EINVAL; 3181 } 3182 3183 if (NInoAttr(ni)) 3184 ni = ni->ext.base_ntfs_ino; 3185 3186 ntfs_debug("Entering for inode 0x%llx.\n", ni->mft_no); 3187 3188 /* Inode haven't got attribute list, thus nothing to attach. */ 3189 if (!NInoAttrList(ni)) 3190 return 0; 3191 3192 if (!ni->attr_list) { 3193 ntfs_debug("Corrupt in-memory struct.\n"); 3194 return -EINVAL; 3195 } 3196 3197 /* Walk through attribute list and attach all extents. */ 3198 ale = (struct attr_list_entry *)ni->attr_list; 3199 while ((u8 *)ale < ni->attr_list + ni->attr_list_size) { 3200 if (ni->mft_no != MREF_LE(ale->mft_reference) && 3201 prev_attached != MREF_LE(ale->mft_reference)) { 3202 struct ntfs_inode *ext_ni; 3203 3204 ext_ni = ntfs_extent_inode_open(ni, ale->mft_reference); 3205 if (IS_ERR(ext_ni)) { 3206 ntfs_debug("Couldn't attach extent inode.\n"); 3207 return PTR_ERR(ext_ni); 3208 } 3209 prev_attached = MREF_LE(ale->mft_reference); 3210 } 3211 ale = (struct attr_list_entry *)((u8 *)ale + le16_to_cpu(ale->length)); 3212 } 3213 return 0; 3214 } 3215 3216 /* 3217 * ntfs_inode_add_attrlist - add attribute list to inode and fill it 3218 * @ni: opened ntfs inode to which add attribute list 3219 * 3220 * Return 0 on success or error. 3221 */ 3222 int ntfs_inode_add_attrlist(struct ntfs_inode *ni) 3223 { 3224 int err; 3225 struct ntfs_attr_search_ctx *ctx; 3226 u8 *al = NULL, *aln; 3227 int al_len = 0; 3228 struct attr_list_entry *ale = NULL; 3229 struct mft_record *ni_mrec; 3230 u32 attr_al_len; 3231 bool free_empty_extents = true; 3232 3233 if (!ni) 3234 return -EINVAL; 3235 3236 ntfs_debug("inode %llu\n", ni->mft_no); 3237 3238 if (NInoAttrList(ni) || ni->nr_extents) { 3239 ntfs_error(ni->vol->sb, "Inode already has attribute list"); 3240 return -EEXIST; 3241 } 3242 3243 ni_mrec = map_mft_record(ni); 3244 if (IS_ERR(ni_mrec)) 3245 return -EIO; 3246 3247 /* Form attribute list. */ 3248 ctx = ntfs_attr_get_search_ctx(ni, ni_mrec); 3249 if (!ctx) { 3250 err = -ENOMEM; 3251 goto err_out; 3252 } 3253 3254 /* Walk through all attributes. */ 3255 while (!(err = ntfs_attr_lookup(AT_UNUSED, NULL, 0, 0, 0, NULL, 0, ctx))) { 3256 int ale_size; 3257 3258 if (ctx->attr->type == AT_ATTRIBUTE_LIST) { 3259 err = -EIO; 3260 ntfs_error(ni->vol->sb, "Attribute list already present"); 3261 goto put_err_out; 3262 } 3263 3264 ale_size = (sizeof(struct attr_list_entry) + sizeof(__le16) * 3265 ctx->attr->name_length + 7) & ~7; 3266 al_len += ale_size; 3267 3268 aln = kvrealloc(al, al_len, GFP_NOFS); 3269 if (!aln) { 3270 err = -ENOMEM; 3271 ntfs_error(ni->vol->sb, "Failed to realloc %d bytes", al_len); 3272 goto put_err_out; 3273 } 3274 ale = (struct attr_list_entry *)(aln + ((u8 *)ale - al)); 3275 al = aln; 3276 3277 memset(ale, 0, ale_size); 3278 3279 /* Add attribute to attribute list. */ 3280 ale->type = ctx->attr->type; 3281 ale->length = cpu_to_le16((sizeof(struct attr_list_entry) + 3282 sizeof(__le16) * ctx->attr->name_length + 7) & ~7); 3283 ale->name_length = ctx->attr->name_length; 3284 ale->name_offset = (u8 *)ale->name - (u8 *)ale; 3285 if (ctx->attr->non_resident) 3286 ale->lowest_vcn = 3287 ctx->attr->data.non_resident.lowest_vcn; 3288 else 3289 ale->lowest_vcn = 0; 3290 ale->mft_reference = MK_LE_MREF(ni->mft_no, 3291 le16_to_cpu(ni_mrec->sequence_number)); 3292 ale->instance = ctx->attr->instance; 3293 memcpy(ale->name, (u8 *)ctx->attr + 3294 le16_to_cpu(ctx->attr->name_offset), 3295 ctx->attr->name_length * sizeof(__le16)); 3296 ale = (struct attr_list_entry *)(al + al_len); 3297 } 3298 3299 /* Check for real error occurred. */ 3300 if (err != -ENOENT) { 3301 ntfs_error(ni->vol->sb, "%s: Attribute lookup failed, inode %llu", 3302 __func__, ni->mft_no); 3303 goto put_err_out; 3304 } 3305 3306 /* Set in-memory attribute list. */ 3307 ni->attr_list = al; 3308 ni->attr_list_size = al_len; 3309 NInoSetAttrList(ni); 3310 3311 attr_al_len = offsetof(struct attr_record, data.resident.reserved) + 1 + 3312 ((al_len + 7) & ~7); 3313 /* Free space if there is not enough it for $ATTRIBUTE_LIST. */ 3314 if (le32_to_cpu(ni_mrec->bytes_allocated) - 3315 le32_to_cpu(ni_mrec->bytes_in_use) < attr_al_len) { 3316 if (ntfs_inode_free_space(ni, (int)attr_al_len)) { 3317 /* Failed to free space. */ 3318 err = -ENOSPC; 3319 ntfs_error(ni->vol->sb, "Failed to free space for attrlist"); 3320 goto rollback; 3321 } 3322 } 3323 3324 /* Add $ATTRIBUTE_LIST to mft record. */ 3325 err = ntfs_resident_attr_record_add(ni, AT_ATTRIBUTE_LIST, AT_UNNAMED, 0, 3326 NULL, al_len, 0); 3327 if (err < 0) { 3328 ntfs_error(ni->vol->sb, "Couldn't add $ATTRIBUTE_LIST to MFT"); 3329 goto rollback; 3330 } 3331 free_empty_extents = false; 3332 3333 err = ntfs_attrlist_update(ni); 3334 if (err < 0) 3335 goto remove_attrlist_record; 3336 3337 ntfs_attr_put_search_ctx(ctx); 3338 unmap_mft_record(ni); 3339 return 0; 3340 3341 remove_attrlist_record: 3342 /* Prevent ntfs_attr_recorm_rm from freeing attribute list. */ 3343 ni->attr_list = NULL; 3344 NInoClearAttrList(ni); 3345 /* Remove $ATTRIBUTE_LIST record. */ 3346 ntfs_attr_reinit_search_ctx(ctx); 3347 if (!ntfs_attr_lookup(AT_ATTRIBUTE_LIST, NULL, 0, 3348 CASE_SENSITIVE, 0, NULL, 0, ctx)) { 3349 if (ntfs_attr_record_rm(ctx)) 3350 ntfs_error(ni->vol->sb, "Rollback failed to remove attrlist"); 3351 else 3352 free_empty_extents = true; 3353 } else { 3354 ntfs_error(ni->vol->sb, "Rollback failed to find attrlist"); 3355 } 3356 3357 /* Setup back in-memory runlist. */ 3358 ni->attr_list = al; 3359 ni->attr_list_size = al_len; 3360 NInoSetAttrList(ni); 3361 rollback: 3362 /* 3363 * Scan attribute list for attributes that placed not in the base MFT 3364 * record and move them to it. 3365 */ 3366 ntfs_attr_reinit_search_ctx(ctx); 3367 ale = (struct attr_list_entry *)al; 3368 while ((u8 *)ale < al + al_len) { 3369 if (MREF_LE(ale->mft_reference) != ni->mft_no) { 3370 if (!ntfs_attr_lookup(ale->type, ale->name, 3371 ale->name_length, 3372 CASE_SENSITIVE, 3373 le64_to_cpu(ale->lowest_vcn), 3374 NULL, 0, ctx)) { 3375 if (ntfs_attr_record_move_to(ctx, ni)) 3376 ntfs_error(ni->vol->sb, 3377 "Rollback failed to move attribute"); 3378 } else { 3379 ntfs_error(ni->vol->sb, "Rollback failed to find attr"); 3380 } 3381 ntfs_attr_reinit_search_ctx(ctx); 3382 } 3383 ale = (struct attr_list_entry *)((u8 *)ale + le16_to_cpu(ale->length)); 3384 } 3385 3386 /* Remove in-memory attribute list. */ 3387 ni->attr_list = NULL; 3388 ni->attr_list_size = 0; 3389 NInoClearAttrList(ni); 3390 NInoClearAttrListDirty(ni); 3391 ntfs_attr_put_search_ctx(ctx); 3392 ctx = NULL; 3393 if (free_empty_extents && ntfs_inode_free_empty_extents(ni)) 3394 ntfs_error(ni->vol->sb, "Rollback failed to free empty extent"); 3395 goto err_out; 3396 put_err_out: 3397 ntfs_attr_put_search_ctx(ctx); 3398 err_out: 3399 kvfree(al); 3400 unmap_mft_record(ni); 3401 return err; 3402 } 3403 3404 /* 3405 * ntfs_inode_close - close an ntfs inode and free all associated memory 3406 * @ni: ntfs inode to close 3407 * 3408 * Make sure the ntfs inode @ni is clean. 3409 * 3410 * If the ntfs inode @ni is a base inode, close all associated extent inodes, 3411 * then deallocate all memory attached to it, and finally free the ntfs inode 3412 * structure itself. 3413 * 3414 * If it is an extent inode, we disconnect it from its base inode before we 3415 * destroy it. 3416 * 3417 * It is OK to pass NULL to this function, it is just noop in this case. 3418 * 3419 * Return 0 on success or error. 3420 */ 3421 int ntfs_inode_close(struct ntfs_inode *ni) 3422 { 3423 int err = -1; 3424 struct ntfs_inode **tmp_nis; 3425 struct ntfs_inode *base_ni; 3426 s32 i; 3427 3428 if (!ni) 3429 return 0; 3430 3431 ntfs_debug("Entering for inode %llu\n", ni->mft_no); 3432 3433 /* Is this a base inode with mapped extent inodes? */ 3434 /* 3435 * If the inode is an extent inode, disconnect it from the 3436 * base inode before destroying it. 3437 */ 3438 base_ni = ni->ext.base_ntfs_ino; 3439 tmp_nis = base_ni->ext.extent_ntfs_inos; 3440 if (!tmp_nis) 3441 goto out; 3442 for (i = 0; i < base_ni->nr_extents; ++i) { 3443 if (tmp_nis[i] != ni) 3444 continue; 3445 /* Found it. Disconnect. */ 3446 memmove(tmp_nis + i, tmp_nis + i + 1, 3447 (base_ni->nr_extents - i - 1) * 3448 sizeof(struct ntfs_inode *)); 3449 /* Buffer should be for multiple of four extents. */ 3450 if ((--base_ni->nr_extents) & 3) 3451 break; 3452 /* 3453 * ElectricFence is unhappy with realloc(x,0) as free(x) 3454 * thus we explicitly separate these two cases. 3455 */ 3456 if (base_ni->nr_extents) { 3457 /* Resize the memory buffer. */ 3458 tmp_nis = kvrealloc(tmp_nis, base_ni->nr_extents * 3459 sizeof(struct ntfs_inode *), GFP_NOFS); 3460 /* Ignore errors, they don't really matter. */ 3461 if (tmp_nis) 3462 base_ni->ext.extent_ntfs_inos = tmp_nis; 3463 } else if (tmp_nis) { 3464 kvfree(tmp_nis); 3465 base_ni->ext.extent_ntfs_inos = NULL; 3466 } 3467 break; 3468 } 3469 3470 out: 3471 if (NInoDirty(ni)) 3472 ntfs_error(ni->vol->sb, "Releasing dirty inode %llu!\n", 3473 ni->mft_no); 3474 if (NInoAttrList(ni) && ni->attr_list) 3475 kvfree(ni->attr_list); 3476 ntfs_destroy_ext_inode(ni); 3477 err = 0; 3478 ntfs_debug("\n"); 3479 return err; 3480 } 3481 3482 /* 3483 * ntfs_inode_free_empty_extents - free empty extent MFT records 3484 * @ni: base inode whose empty extent records should be freed 3485 * 3486 * The caller must ensure that no on-disk attribute list references an empty 3487 * extent record and must hold @ni->mrec_lock to serialize the extent array. 3488 */ 3489 int ntfs_inode_free_empty_extents(struct ntfs_inode *ni) 3490 { 3491 int err = 0, i = 0; 3492 3493 if (!ni || ni->nr_extents < 0) 3494 return -EINVAL; 3495 3496 mutex_lock(&ni->extent_lock); 3497 while (i < ni->nr_extents) { 3498 struct ntfs_inode *ext_ni = ni->ext.extent_ntfs_inos[i]; 3499 struct mft_record *m; 3500 int ret; 3501 3502 m = map_mft_record(ext_ni); 3503 if (IS_ERR(m)) { 3504 if (!err) 3505 err = PTR_ERR(m); 3506 i++; 3507 continue; 3508 } 3509 if (le32_to_cpu(m->bytes_in_use) - 3510 le16_to_cpu(m->attrs_offset) != 8) { 3511 unmap_mft_record(ext_ni); 3512 i++; 3513 continue; 3514 } 3515 unmap_mft_record(ext_ni); 3516 3517 ret = ntfs_mft_record_free(ni->vol, ext_ni); 3518 if (ret) { 3519 if (!err) 3520 err = ret; 3521 i++; 3522 continue; 3523 } 3524 ntfs_inode_close(ext_ni); 3525 /* ntfs_inode_close() removed this entry from the extent array. */ 3526 } 3527 mutex_unlock(&ni->extent_lock); 3528 return err; 3529 } 3530 3531 void ntfs_destroy_ext_inode(struct ntfs_inode *ni) 3532 { 3533 ntfs_debug("Entering."); 3534 if (ni == NULL) 3535 return; 3536 3537 ntfs_attr_close(ni); 3538 3539 if (NInoDirty(ni)) 3540 ntfs_error(ni->vol->sb, "Releasing dirty ext inode %llu!\n", 3541 ni->mft_no); 3542 if (NInoAttrList(ni) && ni->attr_list) 3543 kvfree(ni->attr_list); 3544 kfree(ni->mrec); 3545 kmem_cache_free(ntfs_inode_cache, ni); 3546 } 3547 3548 static struct ntfs_inode *ntfs_inode_base(struct ntfs_inode *ni) 3549 { 3550 if (ni->nr_extents == -1) 3551 return ni->ext.base_ntfs_ino; 3552 return ni; 3553 } 3554 3555 static int ntfs_attr_position(__le32 type, struct ntfs_attr_search_ctx *ctx) 3556 { 3557 int err; 3558 3559 err = ntfs_attr_lookup(type, NULL, 0, CASE_SENSITIVE, 0, NULL, 3560 0, ctx); 3561 if (err) { 3562 __le32 atype; 3563 3564 if (err != -ENOENT) 3565 return err; 3566 3567 atype = ctx->attr->type; 3568 if (atype == AT_END) 3569 return -ENOSPC; 3570 3571 /* 3572 * if ntfs_external_attr_lookup return -ENOENT, ctx->al_entry 3573 * could point to an attribute in an extent mft record, but 3574 * ctx->attr and ctx->ntfs_ino always points to an attibute in 3575 * a base mft record. 3576 */ 3577 if (ctx->al_entry && 3578 MREF_LE(ctx->al_entry->mft_reference) != ctx->ntfs_ino->mft_no) { 3579 ntfs_attr_reinit_search_ctx(ctx); 3580 err = ntfs_attr_lookup(atype, NULL, 0, CASE_SENSITIVE, 0, NULL, 3581 0, ctx); 3582 if (err) 3583 return err; 3584 } 3585 } 3586 return 0; 3587 } 3588 3589 /* 3590 * ntfs_inode_free_space - free space in the MFT record of inode 3591 * @ni: ntfs inode in which MFT record free space 3592 * @size: amount of space needed to free 3593 * 3594 * Return 0 on success or error. 3595 */ 3596 int ntfs_inode_free_space(struct ntfs_inode *ni, int size) 3597 { 3598 struct ntfs_attr_search_ctx *ctx; 3599 int freed, err; 3600 struct mft_record *ni_mrec; 3601 struct super_block *sb; 3602 3603 if (!ni || size < 0) 3604 return -EINVAL; 3605 ntfs_debug("Entering for inode %llu, size %d\n", ni->mft_no, size); 3606 3607 sb = ni->vol->sb; 3608 ni_mrec = map_mft_record(ni); 3609 if (IS_ERR(ni_mrec)) 3610 return -EIO; 3611 3612 freed = (le32_to_cpu(ni_mrec->bytes_allocated) - 3613 le32_to_cpu(ni_mrec->bytes_in_use)); 3614 3615 unmap_mft_record(ni); 3616 3617 if (size <= freed) 3618 return 0; 3619 3620 ctx = ntfs_attr_get_search_ctx(ni, NULL); 3621 if (!ctx) { 3622 ntfs_error(sb, "%s, Failed to get search context", __func__); 3623 return -ENOMEM; 3624 } 3625 3626 /* 3627 * Chkdsk complain if $STANDARD_INFORMATION is not in the base MFT 3628 * record. 3629 * 3630 * $INDEX_ROOT must remain resident, but its attribute record may be moved 3631 * to an extent MFT record when the base record needs room for the list. 3632 * 3633 * Also we can't move $ATTRIBUTE_LIST from base MFT_RECORD, so position 3634 * search context on first attribute after $STANDARD_INFORMATION and 3635 * $ATTRIBUTE_LIST. 3636 * 3637 * Why we reposition instead of simply skip this attributes during 3638 * enumeration? Because in case we have got only in-memory attribute 3639 * list ntfs_attr_lookup will fail when it will try to find 3640 * $ATTRIBUTE_LIST. 3641 */ 3642 err = ntfs_attr_position(AT_FILE_NAME, ctx); 3643 if (err) 3644 goto put_err_out; 3645 3646 while (1) { 3647 int record_size; 3648 3649 /* 3650 * Check whether attribute is from different MFT record. If so, 3651 * find next, because we don't need such. 3652 */ 3653 while (ctx->ntfs_ino->mft_no != ni->mft_no) { 3654 retry: 3655 err = ntfs_attr_lookup(AT_UNUSED, NULL, 0, CASE_SENSITIVE, 3656 0, NULL, 0, ctx); 3657 if (err) { 3658 if (err != -ENOENT) 3659 ntfs_error(sb, "Attr lookup failed #2"); 3660 else if (ctx->attr->type == AT_END) 3661 err = -ENOSPC; 3662 else 3663 err = 0; 3664 3665 if (err) 3666 goto put_err_out; 3667 } 3668 } 3669 3670 if (ntfs_inode_base(ctx->ntfs_ino)->mft_no == FILE_MFT && 3671 ctx->attr->type == AT_DATA) 3672 goto retry; 3673 3674 record_size = le32_to_cpu(ctx->attr->length); 3675 3676 /* Move away attribute. */ 3677 err = ntfs_attr_record_move_away(ctx, 0); 3678 if (err) { 3679 ntfs_error(sb, "Failed to move out attribute #2"); 3680 break; 3681 } 3682 freed += record_size; 3683 3684 /* Check whether we done. */ 3685 if (size <= freed) { 3686 ntfs_attr_put_search_ctx(ctx); 3687 return 0; 3688 } 3689 3690 /* 3691 * Reposition to first attribute after $STANDARD_INFORMATION and 3692 * $ATTRIBUTE_LIST (see comments upwards). 3693 */ 3694 ntfs_attr_reinit_search_ctx(ctx); 3695 err = ntfs_attr_position(AT_FILE_NAME, ctx); 3696 if (err) 3697 break; 3698 } 3699 put_err_out: 3700 ntfs_attr_put_search_ctx(ctx); 3701 if (err == -ENOSPC) 3702 ntfs_debug("No attributes left that can be moved out.\n"); 3703 return err; 3704 } 3705 3706 s64 ntfs_inode_attr_pread(struct inode *vi, s64 pos, s64 count, u8 *buf) 3707 { 3708 struct address_space *mapping = vi->i_mapping; 3709 struct folio *folio; 3710 struct ntfs_inode *ni = NTFS_I(vi); 3711 struct ntfs_inode *base_ni = ni->ext.base_ntfs_ino; 3712 struct ntfs_inode *mrec_ni = ntfs_inode_is_named_stream(ni) ? 3713 base_ni : ni; 3714 s64 isize; 3715 u32 attr_len, total = 0, offset; 3716 pgoff_t index; 3717 int err = 0; 3718 bool claimed = false; 3719 3720 WARN_ON(!NInoAttr(ni)); 3721 if (!count) 3722 return 0; 3723 3724 mutex_lock(&mrec_ni->mrec_lock); 3725 if (ntfs_inode_is_named_stream(ni)) { 3726 err = ntfs_stream_inode_validate(vi); 3727 if (err) { 3728 mutex_unlock(&mrec_ni->mrec_lock); 3729 return err; 3730 } 3731 } 3732 isize = i_size_read(vi); 3733 if (pos < 0 || pos > isize) { 3734 mutex_unlock(&mrec_ni->mrec_lock); 3735 return -EINVAL; 3736 } 3737 if (count > isize - pos) 3738 count = isize - pos; 3739 3740 if (!NInoNonResident(ni)) { 3741 struct ntfs_attr_search_ctx *ctx; 3742 u8 *attr; 3743 3744 ctx = ntfs_attr_get_search_ctx(base_ni, NULL); 3745 if (!ctx) { 3746 ntfs_error(vi->i_sb, "Failed to get attr search ctx"); 3747 err = -ENOMEM; 3748 mutex_unlock(&mrec_ni->mrec_lock); 3749 goto out; 3750 } 3751 3752 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 3753 CASE_SENSITIVE, 3754 0, NULL, 0, ctx); 3755 if (err) { 3756 ntfs_error(vi->i_sb, "Failed to look up attr %#x", ni->type); 3757 ntfs_attr_put_search_ctx(ctx); 3758 mutex_unlock(&mrec_ni->mrec_lock); 3759 goto out; 3760 } 3761 3762 attr = (u8 *)ctx->attr + le16_to_cpu(ctx->attr->data.resident.value_offset); 3763 memcpy(buf, (u8 *)attr + pos, count); 3764 ntfs_attr_put_search_ctx(ctx); 3765 mutex_unlock(&mrec_ni->mrec_lock); 3766 return count; 3767 } 3768 if (ntfs_inode_is_named_stream(ni)) { 3769 atomic_inc(&ni->stream_open_count); 3770 claimed = true; 3771 } 3772 mutex_unlock(&mrec_ni->mrec_lock); 3773 3774 index = pos >> PAGE_SHIFT; 3775 do { 3776 /* Update @index and get the next folio. */ 3777 folio = read_mapping_folio(mapping, index, NULL); 3778 if (IS_ERR(folio)) { 3779 err = PTR_ERR(folio); 3780 break; 3781 } 3782 3783 offset = offset_in_folio(folio, pos); 3784 attr_len = min_t(size_t, (size_t)count, folio_size(folio) - offset); 3785 3786 folio_lock(folio); 3787 memcpy_from_folio(buf, folio, offset, attr_len); 3788 folio_unlock(folio); 3789 folio_put(folio); 3790 3791 total += attr_len; 3792 buf += attr_len; 3793 pos += attr_len; 3794 count -= attr_len; 3795 index++; 3796 } while (count); 3797 out: 3798 if (claimed) 3799 ntfs_stream_put(vi); 3800 return err ? (s64)err : total; 3801 } 3802 3803 static inline int ntfs_enlarge_attribute(struct inode *vi, s64 pos, s64 count, 3804 struct ntfs_attr_search_ctx *ctx) 3805 { 3806 struct ntfs_inode *ni = NTFS_I(vi); 3807 struct super_block *sb = vi->i_sb; 3808 int ret; 3809 3810 if (pos + count <= ni->initialized_size) 3811 return 0; 3812 3813 if (NInoEncrypted(ni) && NInoNonResident(ni)) 3814 return -EACCES; 3815 3816 if (NInoCompressed(ni)) 3817 return -EOPNOTSUPP; 3818 3819 if (pos + count > ni->data_size) { 3820 ret = ntfs_attr_truncate(ni, pos + count); 3821 if (ret) { 3822 ntfs_debug("Failed to truncate attribute"); 3823 return ret; 3824 } 3825 3826 ntfs_attr_reinit_search_ctx(ctx); 3827 ret = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 3828 CASE_SENSITIVE, 3829 0, NULL, 0, ctx); 3830 if (ret) { 3831 ntfs_error(sb, "Failed to look up attr %#x", ni->type); 3832 return ret; 3833 } 3834 } 3835 3836 if (!NInoNonResident(ni)) { 3837 if (likely(i_size_read(vi) < ni->data_size)) 3838 i_size_write(vi, ni->data_size); 3839 return 0; 3840 } 3841 3842 /* Named streams initialize gaps after dropping the MFT record lock. */ 3843 if (ntfs_inode_is_named_stream(ni)) { 3844 if (i_size_read(vi) < ni->data_size) 3845 i_size_write(vi, ni->data_size); 3846 return 0; 3847 } 3848 3849 if (pos + count > ni->initialized_size) { 3850 ctx->attr->data.non_resident.initialized_size = cpu_to_le64(pos + count); 3851 mark_mft_record_dirty(ctx->ntfs_ino); 3852 ni->initialized_size = pos + count; 3853 if (i_size_read(vi) < ni->initialized_size) 3854 i_size_write(vi, ni->initialized_size); 3855 } 3856 return 0; 3857 } 3858 3859 static s64 __ntfs_inode_resident_attr_pwrite(struct inode *vi, 3860 s64 pos, s64 count, u8 *buf, 3861 struct ntfs_attr_search_ctx *ctx, 3862 bool *modified) 3863 { 3864 struct ntfs_inode *ni = NTFS_I(vi); 3865 struct folio *folio; 3866 struct address_space *mapping = vi->i_mapping; 3867 u8 *addr; 3868 int err = 0; 3869 3870 WARN_ON(NInoNonResident(ni)); 3871 if (pos + count > PAGE_SIZE) { 3872 ntfs_error(vi->i_sb, "Out of write into resident attr %#x", ni->type); 3873 return -EINVAL; 3874 } 3875 3876 /* Copy to mft record page */ 3877 addr = (u8 *)ctx->attr + le16_to_cpu(ctx->attr->data.resident.value_offset); 3878 memcpy(addr + pos, buf, count); 3879 mark_mft_record_dirty(ctx->ntfs_ino); 3880 if (count) 3881 *modified = true; 3882 3883 /* Keep the first page clean and uptodate */ 3884 folio = __filemap_get_folio(mapping, 0, FGP_WRITEBEGIN | FGP_NOFS, 3885 mapping_gfp_mask(mapping)); 3886 if (IS_ERR(folio)) { 3887 err = PTR_ERR(folio); 3888 ntfs_error(vi->i_sb, "Failed to read a page 0 for attr %#x: %d", 3889 ni->type, err); 3890 goto out; 3891 } 3892 if (!folio_test_uptodate(folio)) 3893 folio_fill_tail(folio, 0, addr, 3894 le32_to_cpu(ctx->attr->data.resident.value_length)); 3895 else 3896 memcpy_to_folio(folio, offset_in_folio(folio, pos), buf, count); 3897 folio_mark_uptodate(folio); 3898 folio_unlock(folio); 3899 folio_put(folio); 3900 out: 3901 return err ? err : count; 3902 } 3903 3904 static s64 __ntfs_inode_non_resident_attr_pwrite(struct inode *vi, 3905 s64 pos, s64 count, u8 *buf, 3906 bool sync, bool *modified) 3907 { 3908 struct ntfs_inode *ni = NTFS_I(vi); 3909 struct address_space *mapping = vi->i_mapping; 3910 struct folio *folio; 3911 pgoff_t index; 3912 unsigned long offset, length; 3913 size_t attr_len; 3914 s64 ret = 0, written = 0; 3915 3916 WARN_ON(!NInoNonResident(ni)); 3917 3918 index = pos >> PAGE_SHIFT; 3919 while (count) { 3920 if (count == PAGE_SIZE && !offset_in_page(pos)) { 3921 folio = __filemap_get_folio(vi->i_mapping, index, 3922 FGP_CREAT | FGP_LOCK, 3923 mapping_gfp_mask(mapping)); 3924 if (IS_ERR(folio)) { 3925 ret = PTR_ERR(folio); 3926 break; 3927 } 3928 } else { 3929 folio = read_mapping_folio(mapping, index, NULL); 3930 if (IS_ERR(folio)) { 3931 ret = PTR_ERR(folio); 3932 ntfs_error(vi->i_sb, "Failed to read a page %lu for attr %#x: %ld", 3933 index, ni->type, PTR_ERR(folio)); 3934 break; 3935 } 3936 3937 folio_lock(folio); 3938 } 3939 3940 folio_wait_writeback(folio); 3941 3942 if (count == PAGE_SIZE && !offset_in_page(pos)) { 3943 offset = 0; 3944 attr_len = count; 3945 } else { 3946 offset = offset_in_folio(folio, pos); 3947 attr_len = min_t(size_t, (size_t)count, folio_size(folio) - offset); 3948 } 3949 memcpy_to_folio(folio, offset, buf, attr_len); 3950 *modified = true; 3951 3952 if (sync) { 3953 struct ntfs_volume *vol = ni->vol; 3954 s64 lcn, lcn_count; 3955 unsigned int lcn_folio_off = 0; 3956 struct bio *bio; 3957 u64 rl_length = 0; 3958 s64 vcn; 3959 struct runlist_element *rl; 3960 int bio_err; 3961 3962 lcn_count = max_t(s64, 1, 3963 ntfs_bytes_to_cluster(vol, attr_len + 3964 vol->cluster_size - 1)); 3965 vcn = ntfs_pidx_to_cluster(vol, folio->index); 3966 3967 do { 3968 down_write(&ni->runlist.lock); 3969 rl = ntfs_attr_vcn_to_rl(ni, vcn, &lcn); 3970 if (IS_ERR(rl)) { 3971 ret = PTR_ERR(rl); 3972 up_write(&ni->runlist.lock); 3973 goto err_unlock_folio; 3974 } 3975 3976 rl_length = rl->length - (vcn - rl->vcn); 3977 if (rl_length < lcn_count) { 3978 lcn_count -= rl_length; 3979 } else { 3980 rl_length = lcn_count; 3981 lcn_count = 0; 3982 } 3983 up_write(&ni->runlist.lock); 3984 3985 if (vol->cluster_size_bits > PAGE_SHIFT) { 3986 lcn_folio_off = folio->index << PAGE_SHIFT; 3987 lcn_folio_off &= vol->cluster_size_mask; 3988 } 3989 3990 bio = bio_alloc(vol->sb->s_bdev, 1, REQ_OP_WRITE, 3991 GFP_NOIO); 3992 bio->bi_iter.bi_sector = 3993 ntfs_bytes_to_bio_sector(ntfs_cluster_to_bytes(vol, lcn) + 3994 lcn_folio_off); 3995 3996 length = min_t(unsigned long, 3997 ntfs_cluster_to_bytes(vol, rl_length), 3998 folio_size(folio)); 3999 if (!bio_add_folio(bio, folio, length, offset)) { 4000 ret = -EIO; 4001 bio_put(bio); 4002 goto err_unlock_folio; 4003 } 4004 4005 bio_err = submit_bio_wait(bio); 4006 bio_put(bio); 4007 if (bio_err) { 4008 ntfs_error(vi->i_sb, 4009 "Synchronous attribute write failed (%d)", 4010 bio_err); 4011 ret = bio_err; 4012 goto err_unlock_folio; 4013 } 4014 vcn += rl_length; 4015 offset += length; 4016 } while (lcn_count != 0); 4017 4018 folio_mark_uptodate(folio); 4019 } else { 4020 folio_mark_uptodate(folio); 4021 folio_mark_dirty(folio); 4022 } 4023 err_unlock_folio: 4024 folio_unlock(folio); 4025 folio_put(folio); 4026 4027 if (ret) 4028 break; 4029 4030 written += attr_len; 4031 buf += attr_len; 4032 pos += attr_len; 4033 count -= attr_len; 4034 index++; 4035 4036 cond_resched(); 4037 } 4038 4039 return ret ? ret : written; 4040 } 4041 4042 s64 ntfs_inode_attr_pwrite(struct inode *vi, s64 pos, s64 count, u8 *buf, bool sync) 4043 { 4044 struct ntfs_inode *ni = NTFS_I(vi); 4045 struct ntfs_inode *base_ni = ni->ext.base_ntfs_ino; 4046 struct ntfs_inode *mrec_ni = ntfs_inode_is_named_stream(ni) ? 4047 base_ni : ni; 4048 struct ntfs_attr_search_ctx *ctx; 4049 loff_t old_data_size, old_init_size; 4050 s64 ret; 4051 bool modified = false; 4052 4053 WARN_ON(!NInoAttr(ni)); 4054 4055 if (ntfs_inode_is_named_stream(ni)) { 4056 if (NInoEncrypted(ni)) 4057 return -EACCES; 4058 if (NInoCompressed(ni)) 4059 return -EOPNOTSUPP; 4060 } 4061 4062 mutex_lock(&mrec_ni->mrec_lock); 4063 if (ntfs_inode_is_named_stream(ni)) { 4064 ret = ntfs_stream_inode_validate(vi); 4065 if (ret) 4066 goto out_unlock; 4067 } 4068 4069 ctx = ntfs_attr_get_search_ctx(base_ni, NULL); 4070 if (!ctx) { 4071 ntfs_error(vi->i_sb, "Failed to get attr search ctx"); 4072 ret = -ENOMEM; 4073 goto out_unlock; 4074 } 4075 4076 ret = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 4077 CASE_SENSITIVE, 4078 0, NULL, 0, ctx); 4079 if (ret) { 4080 ntfs_attr_put_search_ctx(ctx); 4081 ntfs_error(vi->i_sb, "Failed to look up attr %#x", ni->type); 4082 goto out_unlock; 4083 } 4084 4085 old_data_size = ni->data_size; 4086 old_init_size = ni->initialized_size; 4087 ret = ntfs_enlarge_attribute(vi, pos, count, ctx); 4088 modified = ni->data_size != old_data_size || 4089 ni->initialized_size != old_init_size; 4090 if (ret) 4091 goto out_ctx; 4092 4093 if (!NInoNonResident(ni)) { 4094 ret = __ntfs_inode_resident_attr_pwrite(vi, pos, count, buf, ctx, 4095 &modified); 4096 goto out_ctx; 4097 } 4098 4099 if (ntfs_inode_is_named_stream(ni)) 4100 atomic_inc(&ni->stream_open_count); 4101 ntfs_attr_put_search_ctx(ctx); 4102 mutex_unlock(&mrec_ni->mrec_lock); 4103 /* Attribute writes bypass iomap's delayed-allocation preparation. */ 4104 if (ntfs_inode_is_named_stream(ni)) { 4105 /* 4106 * A failed write must not expose any newly initialized 4107 * data. The stream ioctl caller holds the mapping 4108 * invalidate lock. 4109 */ 4110 ret = __ntfs_extend_initialized_size(vi, pos + count, 4111 pos + count); 4112 modified |= ni->initialized_size != old_init_size; 4113 if (!ret) 4114 ret = ntfs_attr_fallocate(ni, pos, count, true); 4115 } 4116 if (!ret) 4117 ret = __ntfs_inode_non_resident_attr_pwrite(vi, pos, count, 4118 buf, sync, 4119 &modified); 4120 if (ntfs_inode_is_named_stream(ni)) 4121 ntfs_stream_put(vi); 4122 if (modified && ntfs_inode_is_named_stream(ni)) 4123 ntfs_stream_update_base_time(base_ni); 4124 return ret; 4125 4126 out_ctx: 4127 ntfs_attr_put_search_ctx(ctx); 4128 out_unlock: 4129 mutex_unlock(&mrec_ni->mrec_lock); 4130 if (modified && ntfs_inode_is_named_stream(ni)) 4131 ntfs_stream_update_base_time(base_ni); 4132 return ret; 4133 } 4134 4135 struct folio *ntfs_get_locked_folio(struct address_space *mapping, 4136 pgoff_t index, pgoff_t end_index, struct file_ra_state *ra) 4137 { 4138 struct folio *folio; 4139 4140 folio = filemap_lock_folio(mapping, index); 4141 if (IS_ERR(folio)) { 4142 if (PTR_ERR(folio) != -ENOENT) 4143 return folio; 4144 4145 page_cache_sync_readahead(mapping, ra, NULL, index, 4146 end_index - index); 4147 folio = read_mapping_folio(mapping, index, NULL); 4148 if (!IS_ERR(folio)) 4149 folio_lock(folio); 4150 } 4151 4152 return folio; 4153 } 4154