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 ir->collation_rule, ni->mft_no)) { 1038 ntfs_error(vi->i_sb, "Directory index is corrupt."); 1039 goto unm_err_out; 1040 } 1041 1042 if (extend_sys) { 1043 if (ir->type) { 1044 ntfs_error(vi->i_sb, "Indexed attribute is not zero."); 1045 goto unm_err_out; 1046 } 1047 } else { 1048 if (ir->type != AT_FILE_NAME) { 1049 ntfs_error(vi->i_sb, "Indexed attribute is not $FILE_NAME."); 1050 goto unm_err_out; 1051 } 1052 1053 if (ir->collation_rule != COLLATION_FILE_NAME) { 1054 ntfs_error(vi->i_sb, 1055 "Index collation rule is not COLLATION_FILE_NAME."); 1056 goto unm_err_out; 1057 } 1058 } 1059 1060 ni->itype.index.collation_rule = ir->collation_rule; 1061 ni->itype.index.block_size = le32_to_cpu(ir->index_block_size); 1062 if (ni->itype.index.block_size & 1063 (ni->itype.index.block_size - 1)) { 1064 ntfs_error(vi->i_sb, "Index block size (%u) is not a power of two.", 1065 ni->itype.index.block_size); 1066 goto unm_err_out; 1067 } 1068 if (ni->itype.index.block_size > PAGE_SIZE) { 1069 ntfs_error(vi->i_sb, 1070 "Index block size (%u) > PAGE_SIZE (%ld) is not supported.", 1071 ni->itype.index.block_size, 1072 PAGE_SIZE); 1073 err = -EOPNOTSUPP; 1074 goto unm_err_out; 1075 } 1076 if (ni->itype.index.block_size < NTFS_BLOCK_SIZE) { 1077 ntfs_error(vi->i_sb, 1078 "Index block size (%u) < NTFS_BLOCK_SIZE (%i) is not supported.", 1079 ni->itype.index.block_size, 1080 NTFS_BLOCK_SIZE); 1081 err = -EOPNOTSUPP; 1082 goto unm_err_out; 1083 } 1084 ni->itype.index.block_size_bits = 1085 ffs(ni->itype.index.block_size) - 1; 1086 /* Determine the size of a vcn in the directory index. */ 1087 if (vol->cluster_size <= ni->itype.index.block_size) { 1088 ni->itype.index.vcn_size = vol->cluster_size; 1089 ni->itype.index.vcn_size_bits = vol->cluster_size_bits; 1090 } else { 1091 ni->itype.index.vcn_size = NTFS_BLOCK_SIZE; 1092 ni->itype.index.vcn_size_bits = NTFS_BLOCK_SIZE_BITS; 1093 } 1094 1095 /* Setup the index allocation attribute, even if not present. */ 1096 ni->type = AT_INDEX_ROOT; 1097 ni->name = name; 1098 ni->name_len = name_len; 1099 vi->i_size = ni->initialized_size = ni->data_size = 1100 le32_to_cpu(a->data.resident.value_length); 1101 ni->allocated_size = (ni->data_size + 7) & ~7; 1102 /* We are done with the mft record, so we release it. */ 1103 ntfs_attr_put_search_ctx(ctx); 1104 unmap_mft_record(ni); 1105 m = NULL; 1106 ctx = NULL; 1107 /* Setup the operations for this inode. */ 1108 ntfs_set_vfs_operations(vi, vi->i_mode, 0); 1109 if (ir->index.flags & LARGE_INDEX) 1110 NInoSetIndexAllocPresent(ni); 1111 } else { 1112 /* It is a file. */ 1113 ntfs_attr_reinit_search_ctx(ctx); 1114 1115 /* Setup the data attribute, even if not present. */ 1116 ni->type = AT_DATA; 1117 ni->name = AT_UNNAMED; 1118 ni->name_len = 0; 1119 1120 /* Find first extent of the unnamed data attribute. */ 1121 err = ntfs_attr_lookup(AT_DATA, NULL, 0, 0, 0, NULL, 0, ctx); 1122 if (unlikely(err)) { 1123 vi->i_size = ni->initialized_size = 1124 ni->allocated_size = 0; 1125 if (err != -ENOENT) { 1126 ntfs_error(vi->i_sb, "Failed to lookup $DATA attribute."); 1127 goto unm_err_out; 1128 } 1129 /* 1130 * FILE_Secure does not have an unnamed $DATA 1131 * attribute, so we special case it here. 1132 */ 1133 if (vi->i_ino == FILE_Secure) 1134 goto no_data_attr_special_case; 1135 /* 1136 * Most if not all the system files in the $Extend 1137 * system directory do not have unnamed data 1138 * attributes so we need to check if the parent 1139 * directory of the file is FILE_Extend and if it is 1140 * ignore this error. To do this we need to get the 1141 * name of this inode from the mft record as the name 1142 * contains the back reference to the parent directory. 1143 */ 1144 extend_sys = ntfs_is_extended_system_file(ctx); 1145 if (extend_sys > 0) { 1146 if (m->flags & MFT_RECORD_IS_VIEW_INDEX) { 1147 if (extend_sys == 2) { 1148 name = reparse_index_name; 1149 name_len = 2; 1150 goto view_index_meta; 1151 } else if (extend_sys == 3) { 1152 name = objid_index_name; 1153 name_len = 2; 1154 goto view_index_meta; 1155 } 1156 } 1157 goto no_data_attr_special_case; 1158 } 1159 1160 err = extend_sys; 1161 ntfs_error(vi->i_sb, "$DATA attribute is missing, err : %d", err); 1162 goto unm_err_out; 1163 } 1164 a = ctx->attr; 1165 /* Setup the state. */ 1166 if (a->flags & (ATTR_COMPRESSION_MASK | ATTR_IS_SPARSE)) { 1167 if (a->flags & ATTR_COMPRESSION_MASK) { 1168 if (NInoWofCompressed(ni)) { 1169 ntfs_error(vi->i_sb, 1170 "Found native compression on a WOF file."); 1171 goto unm_err_out; 1172 } 1173 NInoSetCompressed(ni); 1174 ni->flags |= FILE_ATTR_COMPRESSED; 1175 if (vol->cluster_size > 4096) { 1176 ntfs_error(vi->i_sb, 1177 "Found compressed data but compression is disabled due to cluster size (%i) > 4kiB.", 1178 vol->cluster_size); 1179 goto unm_err_out; 1180 } 1181 if ((a->flags & ATTR_COMPRESSION_MASK) 1182 != ATTR_IS_COMPRESSED) { 1183 ntfs_error(vi->i_sb, 1184 "Found unknown compression method or corrupt file."); 1185 goto unm_err_out; 1186 } 1187 } 1188 if (a->flags & ATTR_IS_SPARSE) { 1189 NInoSetSparse(ni); 1190 ni->flags |= FILE_ATTR_SPARSE_FILE; 1191 } 1192 } 1193 if (a->flags & ATTR_IS_ENCRYPTED) { 1194 if (NInoCompressed(ni)) { 1195 ntfs_error(vi->i_sb, "Found encrypted and compressed data."); 1196 goto unm_err_out; 1197 } 1198 NInoSetEncrypted(ni); 1199 ni->flags |= FILE_ATTR_ENCRYPTED; 1200 } 1201 if (a->non_resident) { 1202 NInoSetNonResident(ni); 1203 if (a->data.non_resident.lowest_vcn) { 1204 ntfs_error(vi->i_sb, 1205 "First extent of $DATA attribute has non zero lowest_vcn."); 1206 goto unm_err_out; 1207 } 1208 if (NInoCompressed(ni) || (NInoSparse(ni) && !NInoWofCompressed(ni))) { 1209 if (NInoCompressed(ni) && 1210 a->data.non_resident.compression_unit != 4) { 1211 ntfs_error(vi->i_sb, 1212 "Found non-standard compression unit (%u instead of 4). Cannot handle this.", 1213 a->data.non_resident.compression_unit); 1214 err = -EOPNOTSUPP; 1215 goto unm_err_out; 1216 } 1217 1218 /* 1219 * Windows stores the standard compression unit in 1220 * sparse attributes even when the data is not compressed. 1221 */ 1222 if (NInoSparse(ni) && 1223 a->data.non_resident.compression_unit && 1224 a->data.non_resident.compression_unit != 1225 vol->sparse_compression_unit && 1226 a->data.non_resident.compression_unit != 1227 STANDARD_COMPRESSION_UNIT) { 1228 ntfs_error(vi->i_sb, 1229 "Found non-standard compression unit (%u instead of 0, %d, or %d). Cannot handle this.", 1230 a->data.non_resident.compression_unit, 1231 vol->sparse_compression_unit, 1232 STANDARD_COMPRESSION_UNIT); 1233 err = -EOPNOTSUPP; 1234 goto unm_err_out; 1235 } 1236 1237 1238 if (a->data.non_resident.compression_unit) { 1239 ni->itype.compressed.block_size = 1U << 1240 (a->data.non_resident.compression_unit + 1241 vol->cluster_size_bits); 1242 ni->itype.compressed.block_size_bits = 1243 ffs(ni->itype.compressed.block_size) - 1; 1244 ni->itype.compressed.block_clusters = 1245 1U << a->data.non_resident.compression_unit; 1246 } else { 1247 ni->itype.compressed.block_size = 0; 1248 ni->itype.compressed.block_size_bits = 1249 0; 1250 ni->itype.compressed.block_clusters = 1251 0; 1252 } 1253 ni->itype.compressed.size = le64_to_cpu( 1254 a->data.non_resident.compressed_size); 1255 } 1256 if (ntfs_non_resident_sizes_inconsistent(vi, a)) 1257 goto unm_err_out; 1258 vi->i_size = ni->data_size = le64_to_cpu(a->data.non_resident.data_size); 1259 ni->initialized_size = le64_to_cpu(a->data.non_resident.initialized_size); 1260 ni->allocated_size = le64_to_cpu(a->data.non_resident.allocated_size); 1261 } else { /* Resident attribute. */ 1262 vi->i_size = ni->data_size = ni->initialized_size = le32_to_cpu( 1263 a->data.resident.value_length); 1264 ni->allocated_size = le32_to_cpu(a->length) - 1265 le16_to_cpu( 1266 a->data.resident.value_offset); 1267 if (vi->i_size > ni->allocated_size) { 1268 ntfs_error(vi->i_sb, 1269 "Resident data attribute is corrupt (size exceeds allocation)."); 1270 goto unm_err_out; 1271 } 1272 } 1273 no_data_attr_special_case: 1274 /* We are done with the mft record, so we release it. */ 1275 ntfs_attr_put_search_ctx(ctx); 1276 unmap_mft_record(ni); 1277 m = NULL; 1278 ctx = NULL; 1279 /* Setup the operations for this inode. */ 1280 ntfs_set_vfs_operations(vi, vi->i_mode, dev); 1281 } 1282 1283 if (NVolSysImmutable(vol) && (ni->flags & FILE_ATTR_SYSTEM) && 1284 !S_ISFIFO(vi->i_mode) && !S_ISSOCK(vi->i_mode) && !S_ISLNK(vi->i_mode)) 1285 vi->i_flags |= S_IMMUTABLE; 1286 1287 /* 1288 * System files such as $Bitmap and $MFT are maintained by the driver 1289 * itself, and writing them from userspace corrupts the volume. 1290 * Always make them immutable regardless of the sys_immutable option. 1291 * Directories are skipped so the root and $Extend stay usable. 1292 */ 1293 if (ni->mft_no < FILE_first_user && S_ISREG(vi->i_mode)) 1294 vi->i_flags |= S_IMMUTABLE; 1295 1296 /* 1297 * The number of 512-byte blocks used on disk (for stat). This is in so 1298 * far inaccurate as it doesn't account for any named streams or other 1299 * special non-resident attributes, but that is how Windows works, too, 1300 * so we are at least consistent with Windows, if not entirely 1301 * consistent with the Linux Way. Doing it the Linux Way would cause a 1302 * significant slowdown as it would involve iterating over all 1303 * attributes in the mft record and adding the allocated/compressed 1304 * sizes of all non-resident attributes present to give us the Linux 1305 * correct size that should go into i_blocks (after division by 512). 1306 */ 1307 if (S_ISREG(vi->i_mode) && 1308 (NInoCompressed(ni) || (NInoSparse(ni) && !NInoWofCompressed(ni)))) 1309 vi->i_blocks = ni->itype.compressed.size >> 9; 1310 else 1311 vi->i_blocks = ni->allocated_size >> 9; 1312 1313 if (S_ISLNK(vi->i_mode) && ni->target) 1314 vi->i_size = strlen(ni->target); 1315 1316 ntfs_debug("Done."); 1317 return 0; 1318 unm_err_out: 1319 if (!err) 1320 err = -EIO; 1321 if (ctx) 1322 ntfs_attr_put_search_ctx(ctx); 1323 if (m) 1324 unmap_mft_record(ni); 1325 err_out: 1326 if (err != -EOPNOTSUPP && err != -ENOMEM && 1327 err != -EINTR && err != -ERESTARTSYS && vol_err == true) { 1328 ntfs_error(vol->sb, 1329 "Failed with error code %i. Marking corrupt inode 0x%llx as bad. Run chkdsk.", 1330 err, ni->mft_no); 1331 NVolSetErrors(vol); 1332 } 1333 return err; 1334 } 1335 1336 /* 1337 * ntfs_read_locked_attr_inode - read an attribute inode from its base inode 1338 * @base_vi: base inode 1339 * @vi: attribute inode to read 1340 * 1341 * ntfs_read_locked_attr_inode() is called from ntfs_attr_iget() to read the 1342 * attribute inode described by @vi into memory from the base mft record 1343 * described by @base_ni. 1344 * 1345 * ntfs_read_locked_attr_inode() maps, pins and locks the base inode for 1346 * reading and looks up the attribute described by @vi before setting up the 1347 * necessary fields in @vi as well as initializing the ntfs inode. 1348 * 1349 * Q: What locks are held when the function is called? 1350 * A: i_state has I_NEW set, hence the inode is locked, also 1351 * i_count is set to 1, so it is not going to go away 1352 * 1353 * Return 0 on success and -errno on error. 1354 * 1355 * Note this cannot be called for AT_INDEX_ALLOCATION. 1356 */ 1357 static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi) 1358 { 1359 struct ntfs_volume *vol = NTFS_SB(vi->i_sb); 1360 struct ntfs_inode *ni = NTFS_I(vi), *base_ni = NTFS_I(base_vi); 1361 struct mft_record *m; 1362 struct attr_record *a; 1363 struct ntfs_attr_search_ctx *ctx; 1364 int err = 0; 1365 u32 ic; 1366 1367 ntfs_debug("Entering for i_ino 0x%llx.", ni->mft_no); 1368 1369 ntfs_init_big_inode(vi); 1370 1371 /* Just mirror the values from the base inode. */ 1372 vi->i_uid = base_vi->i_uid; 1373 vi->i_gid = base_vi->i_gid; 1374 set_nlink(vi, base_vi->i_nlink); 1375 inode_set_mtime_to_ts(vi, inode_get_mtime(base_vi)); 1376 inode_set_ctime_to_ts(vi, inode_get_ctime(base_vi)); 1377 inode_set_atime_to_ts(vi, inode_get_atime(base_vi)); 1378 vi->i_generation = ni->seq_no = base_ni->seq_no; 1379 1380 /* Named data streams are regular files throughout initialization. */ 1381 vi->i_mode = base_vi->i_mode & ~S_IFMT; 1382 if (ni->type == AT_DATA && ni->name_len) { 1383 vi->i_mode |= S_IFREG; 1384 vi->i_flags = base_vi->i_flags; 1385 } 1386 1387 m = map_mft_record(base_ni); 1388 if (IS_ERR(m)) { 1389 err = PTR_ERR(m); 1390 goto err_out; 1391 } 1392 ctx = ntfs_attr_get_search_ctx(base_ni, m); 1393 if (!ctx) { 1394 err = -ENOMEM; 1395 goto unm_err_out; 1396 } 1397 /* Find the attribute. */ 1398 ic = ntfs_inode_is_named_stream(ni) ? IGNORE_CASE : CASE_SENSITIVE; 1399 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 1400 ic, 0, NULL, 0, ctx); 1401 if (unlikely(err)) 1402 goto unm_err_out; 1403 a = ctx->attr; 1404 if (ntfs_inode_is_named_stream(ni)) { 1405 __le16 *disk_name = (__le16 *)((u8 *)a + 1406 le16_to_cpu(a->name_offset)); 1407 1408 /* Subsequent extent and mutation lookups must select this name. */ 1409 if (ni->name == I30) { 1410 ni->name = kmalloc_array(ni->name_len + 1, 1411 sizeof(__le16), GFP_NOFS); 1412 if (!ni->name) { 1413 err = -ENOMEM; 1414 goto unm_err_out; 1415 } 1416 } 1417 memcpy(ni->name, disk_name, 1418 ni->name_len * sizeof(__le16)); 1419 ni->name[ni->name_len] = 0; 1420 } 1421 if (a->flags & (ATTR_COMPRESSION_MASK | ATTR_IS_SPARSE)) { 1422 if (a->flags & ATTR_COMPRESSION_MASK) { 1423 NInoSetCompressed(ni); 1424 ni->flags |= FILE_ATTR_COMPRESSED; 1425 if ((ni->type != AT_DATA) || (ni->type == AT_DATA && 1426 ni->name_len)) { 1427 ntfs_error(vi->i_sb, 1428 "Found compressed non-data or named data attribute."); 1429 goto unm_err_out; 1430 } 1431 if (vol->cluster_size > 4096) { 1432 ntfs_error(vi->i_sb, 1433 "Found compressed attribute but compression is disabled due to cluster size (%i) > 4kiB.", 1434 vol->cluster_size); 1435 goto unm_err_out; 1436 } 1437 if ((a->flags & ATTR_COMPRESSION_MASK) != 1438 ATTR_IS_COMPRESSED) { 1439 ntfs_error(vi->i_sb, "Found unknown compression method."); 1440 goto unm_err_out; 1441 } 1442 } 1443 /* 1444 * The compressed/sparse flag set in an index root just means 1445 * to compress all files. 1446 */ 1447 if (NInoMstProtected(ni) && ni->type != AT_INDEX_ROOT) { 1448 ntfs_error(vi->i_sb, 1449 "Found mst protected attribute but the attribute is %s.", 1450 NInoCompressed(ni) ? "compressed" : "sparse"); 1451 goto unm_err_out; 1452 } 1453 if (a->flags & ATTR_IS_SPARSE) { 1454 NInoSetSparse(ni); 1455 ni->flags |= FILE_ATTR_SPARSE_FILE; 1456 } 1457 } 1458 if (a->flags & ATTR_IS_ENCRYPTED) { 1459 if (NInoCompressed(ni)) { 1460 ntfs_error(vi->i_sb, "Found encrypted and compressed data."); 1461 goto unm_err_out; 1462 } 1463 /* 1464 * The encryption flag set in an index root just means to 1465 * encrypt all files. 1466 */ 1467 if (NInoMstProtected(ni) && ni->type != AT_INDEX_ROOT) { 1468 ntfs_error(vi->i_sb, 1469 "Found mst protected attribute but the attribute is encrypted."); 1470 goto unm_err_out; 1471 } 1472 if (ni->type != AT_DATA) { 1473 ntfs_error(vi->i_sb, 1474 "Found encrypted non-data attribute."); 1475 goto unm_err_out; 1476 } 1477 NInoSetEncrypted(ni); 1478 ni->flags |= FILE_ATTR_ENCRYPTED; 1479 } 1480 if (!a->non_resident) { 1481 /* Ensure the attribute name is placed before the value. */ 1482 if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >= 1483 le16_to_cpu(a->data.resident.value_offset)))) { 1484 ntfs_error(vol->sb, 1485 "Attribute name is placed after the attribute value."); 1486 goto unm_err_out; 1487 } 1488 if (NInoMstProtected(ni)) { 1489 ntfs_error(vi->i_sb, 1490 "Found mst protected attribute but the attribute is resident."); 1491 goto unm_err_out; 1492 } 1493 vi->i_size = ni->initialized_size = ni->data_size = le32_to_cpu( 1494 a->data.resident.value_length); 1495 ni->allocated_size = le32_to_cpu(a->length) - 1496 le16_to_cpu(a->data.resident.value_offset); 1497 if (vi->i_size > ni->allocated_size) { 1498 ntfs_error(vi->i_sb, 1499 "Resident attribute is corrupt (size exceeds allocation)."); 1500 goto unm_err_out; 1501 } 1502 } else { 1503 NInoSetNonResident(ni); 1504 if (a->data.non_resident.lowest_vcn) { 1505 ntfs_error(vi->i_sb, "First extent of attribute has non-zero lowest_vcn."); 1506 goto unm_err_out; 1507 } 1508 /* 1509 * Ensure the attribute name is placed before the mapping pairs 1510 * array. 1511 */ 1512 if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >= 1513 le16_to_cpu( 1514 a->data.non_resident.mapping_pairs_offset)))) { 1515 ntfs_error(vol->sb, 1516 "Attribute name is placed after the mapping pairs array."); 1517 goto unm_err_out; 1518 } 1519 if (NInoCompressed(ni) || (NInoSparse(ni) && !NInoWofCompressed(ni))) { 1520 if (NInoCompressed(ni) && a->data.non_resident.compression_unit != 4) { 1521 ntfs_error(vi->i_sb, 1522 "Found non-standard compression unit (%u instead of 4). Cannot handle this.", 1523 a->data.non_resident.compression_unit); 1524 err = -EOPNOTSUPP; 1525 goto unm_err_out; 1526 } 1527 if (a->data.non_resident.compression_unit) { 1528 ni->itype.compressed.block_size = 1U << 1529 (a->data.non_resident.compression_unit + 1530 vol->cluster_size_bits); 1531 ni->itype.compressed.block_size_bits = 1532 ffs(ni->itype.compressed.block_size) - 1; 1533 ni->itype.compressed.block_clusters = 1U << 1534 a->data.non_resident.compression_unit; 1535 } else { 1536 ni->itype.compressed.block_size = 0; 1537 ni->itype.compressed.block_size_bits = 0; 1538 ni->itype.compressed.block_clusters = 0; 1539 } 1540 ni->itype.compressed.size = le64_to_cpu( 1541 a->data.non_resident.compressed_size); 1542 } 1543 if (ntfs_non_resident_sizes_inconsistent(vi, a)) 1544 goto unm_err_out; 1545 vi->i_size = ni->data_size = le64_to_cpu(a->data.non_resident.data_size); 1546 ni->initialized_size = le64_to_cpu(a->data.non_resident.initialized_size); 1547 ni->allocated_size = le64_to_cpu(a->data.non_resident.allocated_size); 1548 } 1549 vi->i_mapping->a_ops = &ntfs_aops; 1550 if ((NInoCompressed(ni) || NInoSparse(ni)) && ni->type != AT_INDEX_ROOT) 1551 vi->i_blocks = ni->itype.compressed.size >> 9; 1552 else 1553 vi->i_blocks = ni->allocated_size >> 9; 1554 if (ni->type == AT_DATA && ni->name_len) { 1555 vi->i_op = &ntfs_stream_inode_ops; 1556 vi->i_fop = &ntfs_stream_file_ops; 1557 } 1558 /* 1559 * Make sure the base inode does not go away and attach it to the 1560 * attribute inode. 1561 */ 1562 if (!igrab(base_vi)) { 1563 err = -ENOENT; 1564 goto unm_err_out; 1565 } 1566 ni->ext.base_ntfs_ino = base_ni; 1567 ni->nr_extents = -1; 1568 1569 ntfs_attr_put_search_ctx(ctx); 1570 unmap_mft_record(base_ni); 1571 1572 ntfs_debug("Done."); 1573 return 0; 1574 1575 unm_err_out: 1576 if (!err) 1577 err = -EIO; 1578 if (ctx) 1579 ntfs_attr_put_search_ctx(ctx); 1580 unmap_mft_record(base_ni); 1581 err_out: 1582 if (err != -ENOENT && err != -EINTR && err != -ERESTARTSYS) 1583 ntfs_error(vol->sb, 1584 "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.", 1585 err, ni->mft_no, ni->type, ni->name_len, 1586 base_ni->mft_no); 1587 if (err != -ENOENT && err != -ENOMEM && 1588 err != -EINTR && err != -ERESTARTSYS) 1589 NVolSetErrors(vol); 1590 return err; 1591 } 1592 1593 /* 1594 * ntfs_read_locked_index_inode - read an index inode from its base inode 1595 * @base_vi: base inode 1596 * @vi: index inode to read 1597 * 1598 * ntfs_read_locked_index_inode() is called from ntfs_index_iget() to read the 1599 * index inode described by @vi into memory from the base mft record described 1600 * by @base_ni. 1601 * 1602 * ntfs_read_locked_index_inode() maps, pins and locks the base inode for 1603 * reading and looks up the attributes relating to the index described by @vi 1604 * before setting up the necessary fields in @vi as well as initializing the 1605 * ntfs inode. 1606 * 1607 * Note, index inodes are essentially attribute inodes (NInoAttr() is true) 1608 * with the attribute type set to AT_INDEX_ALLOCATION. Apart from that, they 1609 * are setup like directory inodes since directories are a special case of 1610 * indices ao they need to be treated in much the same way. Most importantly, 1611 * for small indices the index allocation attribute might not actually exist. 1612 * However, the index root attribute always exists but this does not need to 1613 * have an inode associated with it and this is why we define a new inode type 1614 * index. Also, like for directories, we need to have an attribute inode for 1615 * the bitmap attribute corresponding to the index allocation attribute and we 1616 * can store this in the appropriate field of the inode, just like we do for 1617 * normal directory inodes. 1618 * 1619 * Q: What locks are held when the function is called? 1620 * A: i_state has I_NEW set, hence the inode is locked, also 1621 * i_count is set to 1, so it is not going to go away 1622 * 1623 * Return 0 on success and -errno on error. 1624 */ 1625 static int ntfs_read_locked_index_inode(struct inode *base_vi, struct inode *vi) 1626 { 1627 loff_t bvi_size; 1628 struct ntfs_volume *vol = NTFS_SB(vi->i_sb); 1629 struct ntfs_inode *ni = NTFS_I(vi), *base_ni = NTFS_I(base_vi), *bni; 1630 struct inode *bvi; 1631 struct mft_record *m; 1632 struct attr_record *a; 1633 struct ntfs_attr_search_ctx *ctx; 1634 struct index_root *ir; 1635 int err = 0; 1636 1637 ntfs_debug("Entering for i_ino 0x%llx.", ni->mft_no); 1638 lockdep_assert_held(&base_ni->mrec_lock); 1639 1640 ntfs_init_big_inode(vi); 1641 /* Just mirror the values from the base inode. */ 1642 vi->i_uid = base_vi->i_uid; 1643 vi->i_gid = base_vi->i_gid; 1644 set_nlink(vi, base_vi->i_nlink); 1645 inode_set_mtime_to_ts(vi, inode_get_mtime(base_vi)); 1646 inode_set_ctime_to_ts(vi, inode_get_ctime(base_vi)); 1647 inode_set_atime_to_ts(vi, inode_get_atime(base_vi)); 1648 vi->i_generation = ni->seq_no = base_ni->seq_no; 1649 /* Set inode type to zero but preserve permissions. */ 1650 vi->i_mode = base_vi->i_mode & ~S_IFMT; 1651 /* Map the mft record for the base inode. */ 1652 m = map_mft_record(base_ni); 1653 if (IS_ERR(m)) { 1654 err = PTR_ERR(m); 1655 goto err_out; 1656 } 1657 ctx = ntfs_attr_get_search_ctx(base_ni, m); 1658 if (!ctx) { 1659 err = -ENOMEM; 1660 goto unm_err_out; 1661 } 1662 /* Find the index root attribute. */ 1663 err = ntfs_attr_lookup(AT_INDEX_ROOT, ni->name, ni->name_len, 1664 CASE_SENSITIVE, 0, NULL, 0, ctx); 1665 if (unlikely(err)) { 1666 if (err == -ENOENT) 1667 ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is missing."); 1668 goto unm_err_out; 1669 } 1670 a = ctx->attr; 1671 /* Set up the state. */ 1672 if (unlikely(a->non_resident)) { 1673 ntfs_error(vol->sb, "$INDEX_ROOT attribute is not resident."); 1674 goto unm_err_out; 1675 } 1676 /* Ensure the attribute name is placed before the value. */ 1677 if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >= 1678 le16_to_cpu(a->data.resident.value_offset)))) { 1679 ntfs_error(vol->sb, 1680 "$INDEX_ROOT attribute name is placed after the attribute value."); 1681 goto unm_err_out; 1682 } 1683 1684 ir = (struct index_root *)((u8 *)a + le16_to_cpu(a->data.resident.value_offset)); 1685 if (ntfs_index_root_inconsistent(vol, a, ir, ni->mft_no) || 1686 ntfs_index_entries_inconsistent(vol, &ir->index, 1687 ir->collation_rule, ni->mft_no)) { 1688 ntfs_error(vi->i_sb, "Index is corrupt."); 1689 goto unm_err_out; 1690 } 1691 1692 ni->itype.index.collation_rule = ir->collation_rule; 1693 ntfs_debug("Index collation rule is 0x%x.", 1694 le32_to_cpu(ir->collation_rule)); 1695 ni->itype.index.block_size = le32_to_cpu(ir->index_block_size); 1696 if (!is_power_of_2(ni->itype.index.block_size)) { 1697 ntfs_error(vi->i_sb, "Index block size (%u) is not a power of two.", 1698 ni->itype.index.block_size); 1699 goto unm_err_out; 1700 } 1701 if (ni->itype.index.block_size > PAGE_SIZE) { 1702 ntfs_error(vi->i_sb, "Index block size (%u) > PAGE_SIZE (%ld) is not supported.", 1703 ni->itype.index.block_size, PAGE_SIZE); 1704 err = -EOPNOTSUPP; 1705 goto unm_err_out; 1706 } 1707 if (ni->itype.index.block_size < NTFS_BLOCK_SIZE) { 1708 ntfs_error(vi->i_sb, 1709 "Index block size (%u) < NTFS_BLOCK_SIZE (%i) is not supported.", 1710 ni->itype.index.block_size, NTFS_BLOCK_SIZE); 1711 err = -EOPNOTSUPP; 1712 goto unm_err_out; 1713 } 1714 ni->itype.index.block_size_bits = ffs(ni->itype.index.block_size) - 1; 1715 /* Determine the size of a vcn in the index. */ 1716 if (vol->cluster_size <= ni->itype.index.block_size) { 1717 ni->itype.index.vcn_size = vol->cluster_size; 1718 ni->itype.index.vcn_size_bits = vol->cluster_size_bits; 1719 } else { 1720 ni->itype.index.vcn_size = NTFS_BLOCK_SIZE; 1721 ni->itype.index.vcn_size_bits = NTFS_BLOCK_SIZE_BITS; 1722 } 1723 1724 /* Find index allocation attribute. */ 1725 ntfs_attr_reinit_search_ctx(ctx); 1726 err = ntfs_attr_lookup(AT_INDEX_ALLOCATION, ni->name, ni->name_len, 1727 CASE_SENSITIVE, 0, NULL, 0, ctx); 1728 if (unlikely(err)) { 1729 if (err == -ENOENT) { 1730 /* No index allocation. */ 1731 vi->i_size = ni->initialized_size = ni->allocated_size = 0; 1732 /* We are done with the mft record, so we release it. */ 1733 ntfs_attr_put_search_ctx(ctx); 1734 unmap_mft_record(base_ni); 1735 m = NULL; 1736 ctx = NULL; 1737 goto skip_large_index_stuff; 1738 } else 1739 ntfs_error(vi->i_sb, "Failed to lookup $INDEX_ALLOCATION attribute."); 1740 goto unm_err_out; 1741 } 1742 NInoSetIndexAllocPresent(ni); 1743 NInoSetNonResident(ni); 1744 ni->type = AT_INDEX_ALLOCATION; 1745 1746 a = ctx->attr; 1747 if (!a->non_resident) { 1748 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is resident."); 1749 goto unm_err_out; 1750 } 1751 /* 1752 * Ensure the attribute name is placed before the mapping pairs array. 1753 */ 1754 if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >= 1755 le16_to_cpu(a->data.non_resident.mapping_pairs_offset)))) { 1756 ntfs_error(vol->sb, 1757 "$INDEX_ALLOCATION attribute name is placed after the mapping pairs array."); 1758 goto unm_err_out; 1759 } 1760 if (a->flags & ATTR_IS_ENCRYPTED) { 1761 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is encrypted."); 1762 goto unm_err_out; 1763 } 1764 if (a->flags & ATTR_IS_SPARSE) { 1765 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is sparse."); 1766 goto unm_err_out; 1767 } 1768 if (a->flags & ATTR_COMPRESSION_MASK) { 1769 ntfs_error(vi->i_sb, 1770 "$INDEX_ALLOCATION attribute is compressed."); 1771 goto unm_err_out; 1772 } 1773 if (a->data.non_resident.lowest_vcn) { 1774 ntfs_error(vi->i_sb, 1775 "First extent of $INDEX_ALLOCATION attribute has non zero lowest_vcn."); 1776 goto unm_err_out; 1777 } 1778 if (ntfs_non_resident_sizes_inconsistent(vi, a)) 1779 goto unm_err_out; 1780 vi->i_size = ni->data_size = le64_to_cpu(a->data.non_resident.data_size); 1781 ni->initialized_size = le64_to_cpu(a->data.non_resident.initialized_size); 1782 ni->allocated_size = le64_to_cpu(a->data.non_resident.allocated_size); 1783 /* 1784 * We are done with the mft record, so we release it. Otherwise 1785 * we would deadlock in ntfs_attr_iget(). 1786 */ 1787 ntfs_attr_put_search_ctx(ctx); 1788 unmap_mft_record(base_ni); 1789 m = NULL; 1790 ctx = NULL; 1791 /* Get the index bitmap attribute inode. */ 1792 bvi = ntfs_attr_iget(base_vi, AT_BITMAP, ni->name, ni->name_len); 1793 if (IS_ERR(bvi)) { 1794 err = PTR_ERR(bvi); 1795 if (err != -EINTR && err != -ERESTARTSYS) 1796 ntfs_error(vi->i_sb, "Failed to get bitmap attribute."); 1797 goto unm_err_out; 1798 } 1799 bni = NTFS_I(bvi); 1800 if (NInoCompressed(bni) || NInoEncrypted(bni) || 1801 NInoSparse(bni)) { 1802 ntfs_error(vi->i_sb, 1803 "$BITMAP attribute is compressed and/or encrypted and/or sparse."); 1804 goto iput_unm_err_out; 1805 } 1806 /* Consistency check bitmap size vs. index allocation size. */ 1807 bvi_size = i_size_read(bvi); 1808 if ((bvi_size << 3) < (vi->i_size >> ni->itype.index.block_size_bits)) { 1809 ntfs_error(vi->i_sb, 1810 "Index bitmap too small (0x%llx) for index allocation (0x%llx).", 1811 bvi_size << 3, vi->i_size); 1812 goto iput_unm_err_out; 1813 } 1814 iput(bvi); 1815 skip_large_index_stuff: 1816 /* Setup the operations for this index inode. */ 1817 ntfs_set_vfs_operations(vi, S_IFDIR, 0); 1818 vi->i_blocks = ni->allocated_size >> 9; 1819 /* 1820 * Make sure the base inode doesn't go away and attach it to the 1821 * index inode. 1822 */ 1823 if (!igrab(base_vi)) 1824 goto unm_err_out; 1825 ni->ext.base_ntfs_ino = base_ni; 1826 ni->nr_extents = -1; 1827 1828 ntfs_debug("Done."); 1829 return 0; 1830 iput_unm_err_out: 1831 iput(bvi); 1832 unm_err_out: 1833 if (!err) 1834 err = -EIO; 1835 if (ctx) 1836 ntfs_attr_put_search_ctx(ctx); 1837 if (m) 1838 unmap_mft_record(base_ni); 1839 err_out: 1840 if (err != -EINTR && err != -ERESTARTSYS) 1841 ntfs_error(vi->i_sb, 1842 "Failed with error code %i while reading index inode (mft_no 0x%llx, name_len %i.", 1843 err, ni->mft_no, ni->name_len); 1844 if (err != -EOPNOTSUPP && err != -ENOMEM && 1845 err != -EINTR && err != -ERESTARTSYS) 1846 NVolSetErrors(vol); 1847 return err; 1848 } 1849 1850 /* 1851 * load_attribute_list_mount - load an attribute list into memory 1852 * @vol: ntfs volume from which to read 1853 * @rl: runlist of the attribute list 1854 * @al_start: destination buffer 1855 * @size: size of the destination buffer in bytes 1856 * @initialized_size: initialized size of the attribute list 1857 * 1858 * Walk the runlist @rl and load all clusters from it copying them into 1859 * the linear buffer @al. The maximum number of bytes copied to @al is @size 1860 * bytes. Note, @size does not need to be a multiple of the cluster size. If 1861 * @initialized_size is less than @size, the region in @al between 1862 * @initialized_size and @size will be zeroed and not read from disk. 1863 * 1864 * Return 0 on success or -errno on error. 1865 */ 1866 static int load_attribute_list_mount(struct ntfs_volume *vol, 1867 struct runlist_element *rl, u8 *al_start, const s64 size, 1868 const s64 initialized_size) 1869 { 1870 s64 lcn; 1871 u8 *al = al_start; 1872 u8 *al_end = al + initialized_size; 1873 struct super_block *sb; 1874 int err = 0; 1875 loff_t rl_byte_off, rl_byte_len; 1876 1877 ntfs_debug("Entering."); 1878 if (!vol || !rl || !al || size <= 0 || initialized_size < 0 || 1879 initialized_size > size) 1880 return -EINVAL; 1881 if (!initialized_size) { 1882 memset(al, 0, size); 1883 return 0; 1884 } 1885 sb = vol->sb; 1886 1887 /* Read all clusters specified by the runlist one run at a time. */ 1888 while (rl->length) { 1889 lcn = ntfs_rl_vcn_to_lcn(rl, rl->vcn); 1890 ntfs_debug("Reading vcn = 0x%llx, lcn = 0x%llx.", 1891 (unsigned long long)rl->vcn, 1892 (unsigned long long)lcn); 1893 /* The attribute list cannot be sparse. */ 1894 if (lcn < 0) { 1895 ntfs_error(sb, "ntfs_rl_vcn_to_lcn() failed. Cannot read attribute list."); 1896 return -EIO; 1897 } 1898 1899 rl_byte_off = ntfs_cluster_to_bytes(vol, lcn); 1900 rl_byte_len = ntfs_cluster_to_bytes(vol, rl->length); 1901 1902 if (al + rl_byte_len > al_end) 1903 rl_byte_len = al_end - al; 1904 1905 err = ntfs_bdev_read(sb->s_bdev, al, rl_byte_off, 1906 round_up(rl_byte_len, SECTOR_SIZE)); 1907 if (err) { 1908 ntfs_error(sb, "Cannot read attribute list."); 1909 return -EIO; 1910 } 1911 1912 if (al + rl_byte_len >= al_end) { 1913 if (initialized_size < size) 1914 goto initialize; 1915 goto done; 1916 } 1917 1918 al += rl_byte_len; 1919 rl++; 1920 } 1921 if (initialized_size < size) { 1922 initialize: 1923 memset(al_start + initialized_size, 0, size - initialized_size); 1924 } 1925 done: 1926 return err; 1927 } 1928 1929 /* 1930 * The MFT inode has special locking, so teach the lock validator 1931 * about this by splitting off the locking rules of the MFT from 1932 * the locking rules of other inodes. The MFT inode can never be 1933 * accessed from the VFS side (or even internally), only by the 1934 * map_mft functions. 1935 */ 1936 static struct lock_class_key mft_ni_runlist_lock_key, mft_ni_mrec_lock_key; 1937 1938 /* 1939 * ntfs_read_inode_mount - special read_inode for mount time use only 1940 * @vi: inode to read 1941 * 1942 * Read inode FILE_MFT at mount time, only called with super_block lock 1943 * held from within the read_super() code path. 1944 * 1945 * This function exists because when it is called the page cache for $MFT/$DATA 1946 * is not initialized and hence we cannot get at the contents of mft records 1947 * by calling map_mft_record*(). 1948 * 1949 * Further it needs to cope with the circular references problem, i.e. cannot 1950 * load any attributes other than $ATTRIBUTE_LIST until $DATA is loaded, because 1951 * we do not know where the other extent mft records are yet and again, because 1952 * we cannot call map_mft_record*() yet. Obviously this applies only when an 1953 * attribute list is actually present in $MFT inode. 1954 * 1955 * We solve these problems by starting with the $DATA attribute before anything 1956 * else and iterating using ntfs_attr_lookup($DATA) over all extents. As each 1957 * extent is found, we ntfs_mapping_pairs_decompress() including the implied 1958 * ntfs_runlists_merge(). Each step of the iteration necessarily provides 1959 * sufficient information for the next step to complete. 1960 * 1961 * This should work but there are two possible pit falls (see inline comments 1962 * below), but only time will tell if they are real pits or just smoke... 1963 */ 1964 int ntfs_read_inode_mount(struct inode *vi) 1965 { 1966 s64 next_vcn, last_vcn, highest_vcn; 1967 struct super_block *sb = vi->i_sb; 1968 struct ntfs_volume *vol = NTFS_SB(sb); 1969 struct ntfs_inode *ni = NTFS_I(vi); 1970 struct mft_record *m = NULL; 1971 struct attr_record *a; 1972 struct ntfs_attr_search_ctx *ctx; 1973 unsigned int i; 1974 int err; 1975 size_t new_rl_count; 1976 1977 ntfs_debug("Entering."); 1978 1979 /* Initialize the ntfs specific part of @vi. */ 1980 ntfs_init_big_inode(vi); 1981 1982 1983 /* Setup the data attribute. It is special as it is mst protected. */ 1984 NInoSetNonResident(ni); 1985 NInoSetMstProtected(ni); 1986 NInoSetSparseDisabled(ni); 1987 ni->type = AT_DATA; 1988 ni->name = AT_UNNAMED; 1989 ni->name_len = 0; 1990 /* 1991 * This sets up our little cheat allowing us to reuse the async read io 1992 * completion handler for directories. 1993 */ 1994 ni->itype.index.block_size = vol->mft_record_size; 1995 ni->itype.index.block_size_bits = vol->mft_record_size_bits; 1996 1997 /* Very important! Needed to be able to call map_mft_record*(). */ 1998 vol->mft_ino = vi; 1999 2000 /* Allocate enough memory to read the first mft record. */ 2001 if (vol->mft_record_size > 64 * 1024) { 2002 ntfs_error(sb, "Unsupported mft record size %i (max 64kiB).", 2003 vol->mft_record_size); 2004 goto err_out; 2005 } 2006 2007 i = vol->mft_record_size; 2008 if (i < sb->s_blocksize) 2009 i = sb->s_blocksize; 2010 2011 m = kzalloc(i, GFP_NOFS); 2012 if (!m) { 2013 ntfs_error(sb, "Failed to allocate buffer for $MFT record 0."); 2014 goto err_out; 2015 } 2016 2017 /* Load $MFT/$DATA's first mft record. */ 2018 err = ntfs_bdev_read(sb->s_bdev, (char *)m, 2019 ntfs_cluster_to_bytes(vol, vol->mft_lcn), i); 2020 if (err) { 2021 ntfs_error(sb, "Device read failed."); 2022 goto err_out; 2023 } 2024 2025 if (le32_to_cpu(m->bytes_allocated) != vol->mft_record_size) { 2026 ntfs_error(sb, "Incorrect mft record size %u in superblock, should be %u.", 2027 le32_to_cpu(m->bytes_allocated), vol->mft_record_size); 2028 goto err_out; 2029 } 2030 2031 /* Apply the mst fixups. */ 2032 if (post_read_mst_fixup((struct ntfs_record *)m, vol->mft_record_size)) { 2033 ntfs_error(sb, "MST fixup failed. $MFT is corrupt."); 2034 goto err_out; 2035 } 2036 2037 if (ntfs_mft_record_check(vol, m, FILE_MFT)) { 2038 ntfs_error(sb, "ntfs_mft_record_check failed. $MFT is corrupt."); 2039 goto err_out; 2040 } 2041 2042 /* Need this to sanity check attribute list references to $MFT. */ 2043 vi->i_generation = ni->seq_no = le16_to_cpu(m->sequence_number); 2044 2045 /* Provides read_folio() for map_mft_record(). */ 2046 vi->i_mapping->a_ops = &ntfs_mft_aops; 2047 2048 ctx = ntfs_attr_get_search_ctx(ni, m); 2049 if (!ctx) { 2050 err = -ENOMEM; 2051 goto err_out; 2052 } 2053 2054 /* Find the attribute list attribute if present. */ 2055 err = ntfs_attr_lookup(AT_ATTRIBUTE_LIST, NULL, 0, 0, 0, NULL, 0, ctx); 2056 if (err) { 2057 if (unlikely(err != -ENOENT)) { 2058 ntfs_error(sb, 2059 "Failed to lookup attribute list attribute. You should run chkdsk."); 2060 goto put_err_out; 2061 } 2062 } else /* if (!err) */ { 2063 struct attr_list_entry *al_entry, *next_al_entry; 2064 u8 *al_end; 2065 s64 attr_list_size; 2066 static const char *es = " Not allowed. $MFT is corrupt. You should run chkdsk."; 2067 2068 ntfs_debug("Attribute list attribute found in $MFT."); 2069 NInoSetAttrList(ni); 2070 a = ctx->attr; 2071 if (a->flags & ATTR_COMPRESSION_MASK) { 2072 ntfs_error(sb, 2073 "Attribute list attribute is compressed.%s", 2074 es); 2075 goto put_err_out; 2076 } 2077 if (a->flags & ATTR_IS_ENCRYPTED || 2078 a->flags & ATTR_IS_SPARSE) { 2079 if (a->non_resident) { 2080 ntfs_error(sb, 2081 "Non-resident attribute list attribute is encrypted/sparse.%s", 2082 es); 2083 goto put_err_out; 2084 } 2085 ntfs_warning(sb, 2086 "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."); 2087 } 2088 /* Now allocate memory for the attribute list. */ 2089 attr_list_size = ntfs_attr_size(a); 2090 if (attr_list_size <= 0 || 2091 attr_list_size > NTFS_MAX_ATTR_LIST_SIZE) { 2092 ntfs_error(sb, "Invalid attribute list size %lld (mft_no 0x%llx).%s", 2093 (long long)attr_list_size, ni->mft_no, es); 2094 goto put_err_out; 2095 } 2096 ni->attr_list_size = (u32)attr_list_size; 2097 ni->attr_list = kvzalloc(round_up(ni->attr_list_size, SECTOR_SIZE), 2098 GFP_NOFS); 2099 if (!ni->attr_list) { 2100 ntfs_error(sb, "Not enough memory to allocate buffer for attribute list."); 2101 goto put_err_out; 2102 } 2103 if (a->non_resident) { 2104 struct runlist_element *rl; 2105 size_t new_rl_count; 2106 2107 NInoSetAttrListNonResident(ni); 2108 if (a->data.non_resident.lowest_vcn) { 2109 ntfs_error(sb, 2110 "Attribute list has non zero lowest_vcn. $MFT is corrupt. You should run chkdsk."); 2111 goto put_err_out; 2112 } 2113 if (ntfs_non_resident_sizes_inconsistent(vi, a)) 2114 goto put_err_out; 2115 2116 rl = ntfs_mapping_pairs_decompress(vol, a, NULL, &new_rl_count); 2117 if (IS_ERR(rl)) { 2118 err = PTR_ERR(rl); 2119 ntfs_error(sb, 2120 "Mapping pairs decompression failed with error code %i.", 2121 -err); 2122 goto put_err_out; 2123 } 2124 2125 err = load_attribute_list_mount(vol, rl, ni->attr_list, ni->attr_list_size, 2126 le64_to_cpu(a->data.non_resident.initialized_size)); 2127 kvfree(rl); 2128 if (err) { 2129 ntfs_error(sb, 2130 "Failed to load attribute list with error code %i.", 2131 -err); 2132 goto put_err_out; 2133 } 2134 } else /* if (!ctx.attr->non_resident) */ { 2135 /* Now copy the attribute list. */ 2136 memcpy(ni->attr_list, (u8 *)a + le16_to_cpu( 2137 a->data.resident.value_offset), 2138 le32_to_cpu(a->data.resident.value_length)); 2139 } 2140 /* The attribute list is now setup in memory. */ 2141 al_entry = (struct attr_list_entry *)ni->attr_list; 2142 al_end = (u8 *)al_entry + ni->attr_list_size; 2143 for (;; al_entry = next_al_entry) { 2144 /* Out of bounds check. */ 2145 if ((u8 *)al_entry < ni->attr_list || 2146 (u8 *)al_entry > al_end) 2147 goto em_put_err_out; 2148 /* Catch the end of the attribute list. */ 2149 if ((u8 *)al_entry == al_end) 2150 goto em_put_err_out; 2151 if (!ntfs_attr_list_entry_is_valid(al_entry, al_end)) 2152 goto em_put_err_out; 2153 next_al_entry = (struct attr_list_entry *)((u8 *)al_entry + 2154 le16_to_cpu(al_entry->length)); 2155 if (le32_to_cpu(al_entry->type) > le32_to_cpu(AT_DATA)) 2156 goto em_put_err_out; 2157 if (al_entry->type != AT_DATA) 2158 continue; 2159 /* We want an unnamed attribute. */ 2160 if (al_entry->name_length) 2161 goto em_put_err_out; 2162 /* Want the first entry, i.e. lowest_vcn == 0. */ 2163 if (al_entry->lowest_vcn) 2164 goto em_put_err_out; 2165 /* First entry has to be in the base mft record. */ 2166 if (MREF_LE(al_entry->mft_reference) != vi->i_ino) { 2167 /* MFT references do not match, logic fails. */ 2168 ntfs_error(sb, 2169 "BUG: The first $DATA extent of $MFT is not in the base mft record."); 2170 goto put_err_out; 2171 } else { 2172 /* Sequence numbers must match. */ 2173 if (MSEQNO_LE(al_entry->mft_reference) != 2174 ni->seq_no) 2175 goto em_put_err_out; 2176 /* Got it. All is ok. We can stop now. */ 2177 break; 2178 } 2179 } 2180 } 2181 2182 ntfs_attr_reinit_search_ctx(ctx); 2183 2184 /* Now load all attribute extents. */ 2185 a = NULL; 2186 next_vcn = last_vcn = highest_vcn = 0; 2187 /* 2188 * Reading one of $MFT's own extent records in this loop can re-enter 2189 * ntfs_map_runlist_nolock() for $MFT; see the check there. 2190 */ 2191 NVolSetMftBootstrap(vol); 2192 while (!(err = ntfs_attr_lookup(AT_DATA, NULL, 0, 0, next_vcn, NULL, 0, 2193 ctx))) { 2194 struct runlist_element *nrl; 2195 2196 /* Cache the current attribute. */ 2197 a = ctx->attr; 2198 /* $MFT must be non-resident. */ 2199 if (!a->non_resident) { 2200 ntfs_error(sb, 2201 "$MFT must be non-resident but a resident extent was found. $MFT is corrupt. Run chkdsk."); 2202 goto put_err_out; 2203 } 2204 /* $MFT must be uncompressed and unencrypted. */ 2205 if (a->flags & ATTR_COMPRESSION_MASK || 2206 a->flags & ATTR_IS_ENCRYPTED || 2207 a->flags & ATTR_IS_SPARSE) { 2208 ntfs_error(sb, 2209 "$MFT must be uncompressed, non-sparse, and unencrypted but a compressed/sparse/encrypted extent was found. $MFT is corrupt. Run chkdsk."); 2210 goto put_err_out; 2211 } 2212 /* 2213 * Decompress the mapping pairs array of this extent and merge 2214 * the result into the existing runlist. No need for locking 2215 * as we have exclusive access to the inode at this time and we 2216 * are a mount in progress task, too. 2217 */ 2218 nrl = ntfs_mapping_pairs_decompress(vol, a, &ni->runlist, 2219 &new_rl_count); 2220 if (IS_ERR(nrl)) { 2221 ntfs_error(sb, 2222 "ntfs_mapping_pairs_decompress() failed with error code %ld.", 2223 PTR_ERR(nrl)); 2224 goto put_err_out; 2225 } 2226 ni->runlist.rl = nrl; 2227 ni->runlist.count = new_rl_count; 2228 2229 /* Are we in the first extent? */ 2230 if (!next_vcn) { 2231 if (a->data.non_resident.lowest_vcn) { 2232 ntfs_error(sb, 2233 "First extent of $DATA attribute has non zero lowest_vcn. $MFT is corrupt. You should run chkdsk."); 2234 goto put_err_out; 2235 } 2236 /* Get the last vcn in the $DATA attribute. */ 2237 last_vcn = ntfs_bytes_to_cluster(vol, 2238 le64_to_cpu(a->data.non_resident.allocated_size)); 2239 /* Fill in the inode size. */ 2240 vi->i_size = le64_to_cpu(a->data.non_resident.data_size); 2241 ni->initialized_size = le64_to_cpu(a->data.non_resident.initialized_size); 2242 ni->allocated_size = le64_to_cpu(a->data.non_resident.allocated_size); 2243 /* 2244 * Records between allocated_size and data_size are not 2245 * on disk, and would be read as zeros. 2246 */ 2247 if (vi->i_size > ni->allocated_size) { 2248 ntfs_error(sb, 2249 "$MFT data size %lld exceeds its allocated size %lld. $MFT is corrupt. Run chkdsk.", 2250 vi->i_size, ni->allocated_size); 2251 goto put_err_out; 2252 } 2253 /* 2254 * Verify the number of mft records does not exceed 2255 * 2^32 - 1. 2256 */ 2257 if ((vi->i_size >> vol->mft_record_size_bits) >= 2258 (1ULL << 32)) { 2259 ntfs_error(sb, "$MFT is too big! Aborting."); 2260 goto put_err_out; 2261 } 2262 /* 2263 * We have got the first extent of the runlist for 2264 * $MFT which means it is now relatively safe to call 2265 * the normal ntfs_read_inode() function. 2266 * Complete reading the inode, this will actually 2267 * re-read the mft record for $MFT, this time entering 2268 * it into the page cache with which we complete the 2269 * kick start of the volume. It should be safe to do 2270 * this now as the first extent of $MFT/$DATA is 2271 * already known and we would hope that we don't need 2272 * further extents in order to find the other 2273 * attributes belonging to $MFT. Only time will tell if 2274 * this is really the case. If not we will have to play 2275 * magic at this point, possibly duplicating a lot of 2276 * ntfs_read_inode() at this point. We will need to 2277 * ensure we do enough of its work to be able to call 2278 * ntfs_read_inode() on extents of $MFT/$DATA. But lets 2279 * hope this never happens... 2280 */ 2281 err = ntfs_read_locked_inode(vi); 2282 if (err) { 2283 ntfs_error(sb, "ntfs_read_inode() of $MFT failed.\n"); 2284 NVolClearMftBootstrap(vol); 2285 ntfs_attr_put_search_ctx(ctx); 2286 /* Revert to the safe super operations. */ 2287 kfree(m); 2288 return -1; 2289 } 2290 /* 2291 * Re-initialize some specifics about $MFT's inode as 2292 * ntfs_read_inode() will have set up the default ones. 2293 */ 2294 /* Set uid and gid to root. */ 2295 vi->i_uid = GLOBAL_ROOT_UID; 2296 vi->i_gid = GLOBAL_ROOT_GID; 2297 /* Regular file. No access for anyone. */ 2298 vi->i_mode = S_IFREG; 2299 /* No VFS initiated operations allowed for $MFT. */ 2300 vi->i_op = &ntfs_empty_inode_ops; 2301 vi->i_fop = &ntfs_empty_file_ops; 2302 } 2303 2304 /* Get the lowest vcn for the next extent. */ 2305 highest_vcn = le64_to_cpu(a->data.non_resident.highest_vcn); 2306 next_vcn = highest_vcn + 1; 2307 2308 /* Only one extent or error, which we catch below. */ 2309 if (next_vcn <= 0) 2310 break; 2311 2312 /* Avoid endless loops due to corruption. */ 2313 if (next_vcn < le64_to_cpu(a->data.non_resident.lowest_vcn)) { 2314 ntfs_error(sb, "$MFT has corrupt attribute list attribute. Run chkdsk."); 2315 goto put_err_out; 2316 } 2317 } 2318 NVolClearMftBootstrap(vol); 2319 if (err != -ENOENT) { 2320 ntfs_error(sb, "Failed to lookup $MFT/$DATA attribute extent. Run chkdsk.\n"); 2321 goto put_err_out; 2322 } 2323 if (!a) { 2324 ntfs_error(sb, "$MFT/$DATA attribute not found. $MFT is corrupt. Run chkdsk."); 2325 goto put_err_out; 2326 } 2327 if (highest_vcn && highest_vcn != last_vcn - 1) { 2328 ntfs_error(sb, "Failed to load the complete runlist for $MFT/$DATA. Run chkdsk."); 2329 ntfs_debug("highest_vcn = 0x%llx, last_vcn - 1 = 0x%llx", 2330 (unsigned long long)highest_vcn, 2331 (unsigned long long)last_vcn - 1); 2332 goto put_err_out; 2333 } 2334 ntfs_attr_put_search_ctx(ctx); 2335 ntfs_debug("Done."); 2336 kfree(m); 2337 2338 /* 2339 * Split the locking rules of the MFT inode from the 2340 * locking rules of other inodes: 2341 */ 2342 lockdep_set_class(&ni->runlist.lock, &mft_ni_runlist_lock_key); 2343 lockdep_set_class(&ni->mrec_lock, &mft_ni_mrec_lock_key); 2344 2345 return 0; 2346 2347 em_put_err_out: 2348 ntfs_error(sb, 2349 "Couldn't find first extent of $DATA attribute in attribute list. $MFT is corrupt. Run chkdsk."); 2350 put_err_out: 2351 ntfs_attr_put_search_ctx(ctx); 2352 err_out: 2353 /* Also reached from inside the $DATA loop. */ 2354 NVolClearMftBootstrap(vol); 2355 ntfs_error(sb, "Failed. Marking inode as bad."); 2356 kfree(m); 2357 return -1; 2358 } 2359 2360 static void __ntfs_clear_inode(struct ntfs_inode *ni) 2361 { 2362 /* Free all alocated memory. */ 2363 if (NInoNonResident(ni) && ni->runlist.rl) { 2364 kvfree(ni->runlist.rl); 2365 ni->runlist.rl = NULL; 2366 } 2367 2368 if (ni->attr_list) { 2369 kvfree(ni->attr_list); 2370 ni->attr_list = NULL; 2371 } 2372 2373 if (ni->name_len && ni->name != I30 && 2374 ni->name != reparse_index_name && 2375 ni->name != objid_index_name) { 2376 WARN_ON(!ni->name); 2377 kfree(ni->name); 2378 } 2379 } 2380 2381 void ntfs_clear_extent_inode(struct ntfs_inode *ni) 2382 { 2383 ntfs_debug("Entering for inode 0x%llx.", ni->mft_no); 2384 2385 WARN_ON(NInoAttr(ni)); 2386 WARN_ON(ni->nr_extents != -1); 2387 2388 __ntfs_clear_inode(ni); 2389 ntfs_destroy_extent_inode(ni); 2390 } 2391 2392 static int ntfs_delete_base_inode(struct ntfs_inode *ni) 2393 { 2394 struct super_block *sb = ni->vol->sb; 2395 int err; 2396 2397 if (NInoAttr(ni) || ni->nr_extents == -1) 2398 return 0; 2399 2400 err = ntfs_non_resident_dealloc_clusters(ni); 2401 2402 /* 2403 * Deallocate extent mft records and free extent inodes. 2404 * No need to lock as no one else has a reference. 2405 */ 2406 while (ni->nr_extents) { 2407 err = ntfs_mft_record_free(ni->vol, *(ni->ext.extent_ntfs_inos)); 2408 if (err) 2409 ntfs_error(sb, 2410 "Failed to free extent MFT record. Leaving inconsistent metadata.\n"); 2411 ntfs_inode_close(*(ni->ext.extent_ntfs_inos)); 2412 } 2413 2414 /* Deallocate base mft record */ 2415 err = ntfs_mft_record_free(ni->vol, ni); 2416 if (err) 2417 ntfs_error(sb, "Failed to free base MFT record. Leaving inconsistent metadata.\n"); 2418 return err; 2419 } 2420 2421 /* 2422 * ntfs_evict_big_inode - clean up the ntfs specific part of an inode 2423 * @vi: vfs inode pending annihilation 2424 * 2425 * When the VFS is going to remove an inode from memory, ntfs_clear_big_inode() 2426 * is called, which deallocates all memory belonging to the NTFS specific part 2427 * of the inode and returns. 2428 * 2429 * If the MFT record is dirty, we commit it before doing anything else. 2430 */ 2431 void ntfs_evict_big_inode(struct inode *vi) 2432 { 2433 struct ntfs_inode *ni = NTFS_I(vi); 2434 2435 truncate_inode_pages_final(&vi->i_data); 2436 2437 if (!vi->i_nlink) { 2438 if (!NInoAttr(ni)) { 2439 /* Never called with extent inodes */ 2440 WARN_ON(ni->nr_extents == -1); 2441 ntfs_delete_base_inode(ni); 2442 } 2443 goto release; 2444 } 2445 2446 if (NInoDirty(ni)) { 2447 /* Committing the inode also commits all extent inodes. */ 2448 ntfs_commit_inode(vi); 2449 2450 if (NInoDirty(ni)) { 2451 ntfs_debug("Failed to commit dirty inode 0x%llx. Losing data!", 2452 ni->mft_no); 2453 NInoClearAttrListDirty(ni); 2454 NInoClearDirty(ni); 2455 } 2456 } 2457 2458 /* No need to lock at this stage as no one else has a reference. */ 2459 if (ni->nr_extents > 0) { 2460 int i; 2461 2462 for (i = 0; i < ni->nr_extents; i++) { 2463 if (ni->ext.extent_ntfs_inos[i]) 2464 ntfs_clear_extent_inode(ni->ext.extent_ntfs_inos[i]); 2465 } 2466 ni->nr_extents = 0; 2467 kvfree(ni->ext.extent_ntfs_inos); 2468 } 2469 2470 release: 2471 clear_inode(vi); 2472 __ntfs_clear_inode(ni); 2473 2474 if (NInoAttr(ni)) { 2475 /* Release the base inode if we are holding it. */ 2476 if (ni->nr_extents == -1) { 2477 iput(VFS_I(ni->ext.base_ntfs_ino)); 2478 ni->nr_extents = 0; 2479 ni->ext.base_ntfs_ino = NULL; 2480 } 2481 } 2482 2483 if (!atomic_dec_and_test(&ni->count)) 2484 WARN_ON(1); 2485 if (ni->folio) 2486 folio_put(ni->folio); 2487 kfree(ni->mrec); 2488 kvfree(ni->target); 2489 } 2490 2491 /* 2492 * ntfs_show_options - show mount options in /proc/mounts 2493 * @sf: seq_file in which to write our mount options 2494 * @root: root of the mounted tree whose mount options to display 2495 * 2496 * Called by the VFS once for each mounted ntfs volume when someone reads 2497 * /proc/mounts in order to display the NTFS specific mount options of each 2498 * mount. The mount options of fs specified by @root are written to the seq file 2499 * @sf and success is returned. 2500 */ 2501 int ntfs_show_options(struct seq_file *sf, struct dentry *root) 2502 { 2503 struct ntfs_volume *vol = NTFS_SB(root->d_sb); 2504 int i; 2505 2506 if (uid_valid(vol->uid)) 2507 seq_printf(sf, ",uid=%i", from_kuid_munged(&init_user_ns, vol->uid)); 2508 if (gid_valid(vol->gid)) 2509 seq_printf(sf, ",gid=%i", from_kgid_munged(&init_user_ns, vol->gid)); 2510 if (vol->fmask == vol->dmask) 2511 seq_printf(sf, ",umask=0%o", vol->fmask); 2512 else { 2513 seq_printf(sf, ",fmask=0%o", vol->fmask); 2514 seq_printf(sf, ",dmask=0%o", vol->dmask); 2515 } 2516 seq_printf(sf, ",iocharset=%s", vol->nls_map->charset); 2517 if (NVolCaseSensitive(vol)) 2518 seq_puts(sf, ",case_sensitive"); 2519 else 2520 seq_puts(sf, ",nocase"); 2521 if (NVolShowSystemFiles(vol)) 2522 seq_puts(sf, ",show_sys_files,showmeta"); 2523 for (i = 0; on_errors_arr[i].val; i++) { 2524 if (on_errors_arr[i].val == vol->on_errors) 2525 seq_printf(sf, ",errors=%s", on_errors_arr[i].str); 2526 } 2527 seq_printf(sf, ",mft_zone_multiplier=%i", vol->mft_zone_multiplier); 2528 if (NVolSysImmutable(vol)) 2529 seq_puts(sf, ",sys_immutable"); 2530 if (!NVolShowHiddenFiles(vol)) 2531 seq_puts(sf, ",nohidden"); 2532 if (NVolHideDotFiles(vol)) 2533 seq_puts(sf, ",hide_dot_files"); 2534 if (NVolCheckWindowsNames(vol)) 2535 seq_puts(sf, ",windows_names"); 2536 if (NVolDiscard(vol)) 2537 seq_puts(sf, ",discard"); 2538 if (NVolDisableSparse(vol)) 2539 seq_puts(sf, ",disable_sparse"); 2540 if (NVolNativeSymlinkRel(vol)) 2541 seq_puts(sf, ",native_symlink=rel"); 2542 else 2543 seq_puts(sf, ",native_symlink=raw"); 2544 if (NVolSymlinkNative(vol)) 2545 seq_puts(sf, ",symlink=native"); 2546 else 2547 seq_puts(sf, ",symlink=wsl"); 2548 if (NVolStreamsWindows(vol)) 2549 seq_puts(sf, ",streams_interface=windows"); 2550 else 2551 seq_puts(sf, ",streams_interface=none"); 2552 if (vol->sb->s_flags & SB_POSIXACL) 2553 seq_puts(sf, ",acl"); 2554 return 0; 2555 } 2556 2557 int ntfs_extend_initialized_size(struct inode *vi, const loff_t offset, 2558 const loff_t new_size) 2559 { 2560 struct ntfs_inode *ni = NTFS_I(vi); 2561 struct ntfs_inode *mrec_ni = ntfs_base_inode(ni); 2562 loff_t old_init_size, clear_size; 2563 unsigned long flags; 2564 int err = 0; 2565 2566 if (!NInoNonResident(ni)) 2567 return -EINVAL; 2568 2569 filemap_invalidate_lock(vi->i_mapping); 2570 read_lock_irqsave(&ni->size_lock, flags); 2571 old_init_size = ni->initialized_size; 2572 read_unlock_irqrestore(&ni->size_lock, flags); 2573 2574 if (old_init_size >= new_size) 2575 goto out; 2576 2577 err = ntfs_attr_map_whole_runlist(ni); 2578 if (err) 2579 goto out; 2580 2581 if (!NInoCompressed(ni)) { 2582 /* 2583 * Zeroing skips holes, so discard cached uninitialized bytes 2584 * before making them visible to writes or shared mappings. 2585 */ 2586 clear_size = max_t(loff_t, i_size_read(vi), new_size); 2587 truncate_pagecache_range(vi, old_init_size, clear_size - 1); 2588 2589 if (old_init_size < offset) { 2590 err = iomap_zero_range(vi, old_init_size, 2591 offset - old_init_size, 2592 NULL, &ntfs_seek_iomap_ops, 2593 &ntfs_iomap_folio_ops, NULL); 2594 if (err) 2595 goto out; 2596 } 2597 } 2598 2599 mutex_lock(&mrec_ni->mrec_lock); 2600 err = ntfs_attr_set_initialized_size(ni, new_size); 2601 mutex_unlock(&mrec_ni->mrec_lock); 2602 if (err) 2603 truncate_setsize(vi, old_init_size); 2604 out: 2605 filemap_invalidate_unlock(vi->i_mapping); 2606 return err; 2607 } 2608 2609 int ntfs_truncate_vfs(struct inode *vi, loff_t new_size, loff_t i_size) 2610 { 2611 struct ntfs_inode *ni = NTFS_I(vi); 2612 struct ntfs_inode *mrec_ni = ntfs_base_inode(ni); 2613 int err; 2614 2615 mutex_lock(&mrec_ni->mrec_lock); 2616 err = __ntfs_attr_truncate_vfs(ni, new_size, i_size); 2617 mutex_unlock(&mrec_ni->mrec_lock); 2618 if (err < 0) 2619 return err; 2620 2621 if (ntfs_inode_is_named_stream(ni)) 2622 ntfs_stream_update_base_time(mrec_ni); 2623 else 2624 inode_set_mtime_to_ts(vi, inode_set_ctime_current(vi)); 2625 return 0; 2626 } 2627 2628 /* 2629 * ntfs_inode_sync_standard_information - update standard information attribute 2630 * @vi: inode to update standard information 2631 * @m: mft record 2632 * 2633 * Return 0 on success or -errno on error. 2634 */ 2635 static int ntfs_inode_sync_standard_information(struct inode *vi, struct mft_record *m) 2636 { 2637 struct ntfs_inode *ni = NTFS_I(vi); 2638 struct ntfs_attr_search_ctx *ctx; 2639 struct standard_information *si; 2640 __le64 nt; 2641 int err = 0; 2642 bool modified = false; 2643 2644 /* Update the access times in the standard information attribute. */ 2645 ctx = ntfs_attr_get_search_ctx(ni, m); 2646 if (unlikely(!ctx)) 2647 return -ENOMEM; 2648 err = ntfs_attr_lookup(AT_STANDARD_INFORMATION, NULL, 0, 2649 CASE_SENSITIVE, 0, NULL, 0, ctx); 2650 if (unlikely(err)) { 2651 ntfs_attr_put_search_ctx(ctx); 2652 return err; 2653 } 2654 si = (struct standard_information *)((u8 *)ctx->attr + 2655 le16_to_cpu(ctx->attr->data.resident.value_offset)); 2656 if (si->file_attributes != ni->flags) { 2657 si->file_attributes = ni->flags; 2658 modified = true; 2659 } 2660 2661 /* Update the creation times if they have changed. */ 2662 nt = utc2ntfs(ni->i_crtime); 2663 if (si->creation_time != nt) { 2664 ntfs_debug("Updating creation time for inode 0x%llx: old = 0x%llx, new = 0x%llx", 2665 ni->mft_no, le64_to_cpu(si->creation_time), 2666 le64_to_cpu(nt)); 2667 si->creation_time = nt; 2668 modified = true; 2669 } 2670 2671 /* Update the access times if they have changed. */ 2672 nt = utc2ntfs(inode_get_mtime(vi)); 2673 if (si->last_data_change_time != nt) { 2674 ntfs_debug("Updating mtime for inode 0x%llx: old = 0x%llx, new = 0x%llx", 2675 ni->mft_no, le64_to_cpu(si->last_data_change_time), 2676 le64_to_cpu(nt)); 2677 si->last_data_change_time = nt; 2678 modified = true; 2679 } 2680 2681 nt = utc2ntfs(inode_get_ctime(vi)); 2682 if (si->last_mft_change_time != nt) { 2683 ntfs_debug("Updating ctime for inode 0x%llx: old = 0x%llx, new = 0x%llx", 2684 ni->mft_no, le64_to_cpu(si->last_mft_change_time), 2685 le64_to_cpu(nt)); 2686 si->last_mft_change_time = nt; 2687 modified = true; 2688 } 2689 nt = utc2ntfs(inode_get_atime(vi)); 2690 if (si->last_access_time != nt) { 2691 ntfs_debug("Updating atime for inode 0x%llx: old = 0x%llx, new = 0x%llx", 2692 ni->mft_no, 2693 le64_to_cpu(si->last_access_time), 2694 le64_to_cpu(nt)); 2695 si->last_access_time = nt; 2696 modified = true; 2697 } 2698 2699 /* 2700 * If we just modified the standard information attribute we need to 2701 * mark the mft record it is in dirty. We do this manually so that 2702 * mark_inode_dirty() is not called which would redirty the inode and 2703 * hence result in an infinite loop of trying to write the inode. 2704 * There is no need to mark the base inode nor the base mft record 2705 * dirty, since we are going to write this mft record below in any case 2706 * and the base mft record may actually not have been modified so it 2707 * might not need to be written out. 2708 * NOTE: It is not a problem when the inode for $MFT itself is being 2709 * written out as ntfs_mft_mark_dirty() will only set I_DIRTY_PAGES 2710 * on the $MFT inode and hence ntfs_write_inode() will not be 2711 * re-invoked because of it which in turn is ok since the dirtied mft 2712 * record will be cleaned and written out to disk below, i.e. before 2713 * this function returns. 2714 */ 2715 if (modified) 2716 NInoSetDirty(ctx->ntfs_ino); 2717 ntfs_attr_put_search_ctx(ctx); 2718 2719 return err; 2720 } 2721 2722 /* 2723 * ntfs_inode_sync_filename - update FILE_NAME attributes 2724 * @ni: ntfs inode to update FILE_NAME attributes 2725 * 2726 * Update all FILE_NAME attributes for inode @ni in the index. 2727 * 2728 * Return 0 on success or error. 2729 */ 2730 int ntfs_inode_sync_filename(struct ntfs_inode *ni) 2731 { 2732 struct inode *index_vi; 2733 struct super_block *sb = VFS_I(ni)->i_sb; 2734 struct ntfs_attr_search_ctx *ctx = NULL; 2735 struct ntfs_index_context *ictx; 2736 struct ntfs_inode *index_ni; 2737 struct file_name_attr *fn; 2738 struct file_name_attr *fnx; 2739 struct reparse_point *rpp; 2740 __le32 reparse_tag; 2741 int err = 0; 2742 unsigned long flags; 2743 2744 ntfs_debug("Entering for inode %llu\n", ni->mft_no); 2745 2746 ctx = ntfs_attr_get_search_ctx(ni, NULL); 2747 if (!ctx) 2748 return -ENOMEM; 2749 2750 /* Collect the reparse tag, if any */ 2751 reparse_tag = cpu_to_le32(0); 2752 if (ni->flags & FILE_ATTR_REPARSE_POINT) { 2753 if (!ntfs_attr_lookup(AT_REPARSE_POINT, NULL, 2754 0, CASE_SENSITIVE, 0, NULL, 0, ctx)) { 2755 rpp = (struct reparse_point *)((u8 *)ctx->attr + 2756 le16_to_cpu(ctx->attr->data.resident.value_offset)); 2757 reparse_tag = rpp->reparse_tag; 2758 } 2759 ntfs_attr_reinit_search_ctx(ctx); 2760 } 2761 2762 /* Walk through all FILE_NAME attributes and update them. */ 2763 while (!(err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, 0, 0, NULL, 0, ctx))) { 2764 fn = (struct file_name_attr *)((u8 *)ctx->attr + 2765 le16_to_cpu(ctx->attr->data.resident.value_offset)); 2766 if (MREF_LE(fn->parent_directory) == ni->mft_no) 2767 continue; 2768 2769 index_vi = ntfs_iget(sb, MREF_LE(fn->parent_directory)); 2770 if (IS_ERR(index_vi)) { 2771 ntfs_error(sb, "Failed to open inode %lld with index", 2772 (long long)MREF_LE(fn->parent_directory)); 2773 continue; 2774 } 2775 2776 index_ni = NTFS_I(index_vi); 2777 2778 mutex_lock_nested(&index_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT); 2779 if (NInoBeingDeleted(ni)) { 2780 mutex_unlock(&index_ni->mrec_lock); 2781 iput(index_vi); 2782 continue; 2783 } 2784 2785 ictx = ntfs_index_ctx_get(index_ni, I30, 4); 2786 if (!ictx) { 2787 ntfs_error(sb, "Failed to get index ctx, inode %llu", 2788 index_ni->mft_no); 2789 mutex_unlock(&index_ni->mrec_lock); 2790 iput(index_vi); 2791 continue; 2792 } 2793 2794 err = ntfs_index_lookup(fn, sizeof(struct file_name_attr), ictx); 2795 if (err) { 2796 ntfs_debug("Index lookup failed, inode %llu", 2797 index_ni->mft_no); 2798 ntfs_index_ctx_put(ictx); 2799 mutex_unlock(&index_ni->mrec_lock); 2800 iput(index_vi); 2801 continue; 2802 } 2803 /* Update flags and file size. */ 2804 fnx = (struct file_name_attr *)ictx->data; 2805 fnx->file_attributes = 2806 (fnx->file_attributes & ~FILE_ATTR_VALID_FLAGS) | 2807 (ni->flags & FILE_ATTR_VALID_FLAGS); 2808 if (ctx->mrec->flags & MFT_RECORD_IS_DIRECTORY) 2809 fnx->data_size = fnx->allocated_size = 0; 2810 else { 2811 read_lock_irqsave(&ni->size_lock, flags); 2812 if (NInoSparse(ni) || NInoCompressed(ni)) 2813 fnx->allocated_size = cpu_to_le64(ni->itype.compressed.size); 2814 else 2815 fnx->allocated_size = cpu_to_le64(ni->allocated_size); 2816 fnx->data_size = cpu_to_le64(ni->data_size); 2817 2818 /* 2819 * The file name record has also to be fixed if some 2820 * attribute update implied the unnamed data to be 2821 * made non-resident 2822 */ 2823 fn->allocated_size = fnx->allocated_size; 2824 fn->data_size = fnx->data_size; 2825 read_unlock_irqrestore(&ni->size_lock, flags); 2826 } 2827 2828 /* update or clear the reparse tag in the index */ 2829 fnx->type.rp.reparse_point_tag = reparse_tag; 2830 fnx->creation_time = fn->creation_time; 2831 fnx->last_data_change_time = fn->last_data_change_time; 2832 fnx->last_mft_change_time = fn->last_mft_change_time; 2833 fnx->last_access_time = fn->last_access_time; 2834 ntfs_index_entry_mark_dirty(ictx); 2835 ntfs_icx_ib_sync_write(ictx); 2836 NInoSetDirty(ctx->ntfs_ino); 2837 ntfs_index_ctx_put(ictx); 2838 mutex_unlock(&index_ni->mrec_lock); 2839 iput(index_vi); 2840 } 2841 /* Check for real error occurred. */ 2842 if (err != -ENOENT) { 2843 ntfs_error(sb, "Attribute lookup failed, err : %d, inode %llu", err, 2844 ni->mft_no); 2845 } else 2846 err = 0; 2847 2848 ntfs_attr_put_search_ctx(ctx); 2849 return err; 2850 } 2851 2852 int ntfs_get_block_mft_record(struct ntfs_inode *mft_ni, struct ntfs_inode *ni) 2853 { 2854 s64 vcn; 2855 struct runlist_element *rl; 2856 2857 if (ni->mft_lcn[0] != LCN_RL_NOT_MAPPED) 2858 return 0; 2859 2860 vcn = (s64)ni->mft_no << mft_ni->vol->mft_record_size_bits >> 2861 mft_ni->vol->cluster_size_bits; 2862 2863 rl = mft_ni->runlist.rl; 2864 if (!rl) { 2865 ntfs_error(mft_ni->vol->sb, "$MFT runlist is not present"); 2866 return -EIO; 2867 } 2868 2869 /* Seek to element containing target vcn. */ 2870 while (rl->length && rl[1].vcn <= vcn) 2871 rl++; 2872 ni->mft_lcn[0] = ntfs_rl_vcn_to_lcn(rl, vcn); 2873 ni->mft_lcn_count = 1; 2874 2875 if (mft_ni->vol->cluster_size < mft_ni->vol->mft_record_size && 2876 (rl->length - (vcn - rl->vcn)) <= 1) { 2877 rl++; 2878 ni->mft_lcn[1] = ntfs_rl_vcn_to_lcn(rl, vcn + 1); 2879 ni->mft_lcn_count++; 2880 } 2881 return 0; 2882 } 2883 2884 /* 2885 * __ntfs_write_inode - write out a dirty inode 2886 * @vi: inode to write out 2887 * @sync: if true, write out synchronously 2888 * 2889 * Write out a dirty inode to disk including any extent inodes if present. 2890 * 2891 * If @sync is true, commit the inode to disk and wait for io completion. This 2892 * is done using write_mft_record(). 2893 * 2894 * If @sync is false, just schedule the write to happen but do not wait for i/o 2895 * completion. 2896 * 2897 * Return 0 on success and -errno on error. 2898 */ 2899 int __ntfs_write_inode(struct inode *vi, int sync) 2900 { 2901 struct ntfs_inode *ni = NTFS_I(vi); 2902 struct ntfs_inode *mft_ni = NTFS_I(ni->vol->mft_ino); 2903 struct mft_record *m; 2904 int err = 0; 2905 bool need_iput = false; 2906 2907 ntfs_debug("Entering for %sinode 0x%llx.", NInoAttr(ni) ? "attr " : "", 2908 ni->mft_no); 2909 2910 if (NVolShutdown(ni->vol)) 2911 return -EIO; 2912 2913 /* 2914 * Dirty attribute inodes are written via their real inodes so just 2915 * clean them here. Access time updates are taken care off when the 2916 * real inode is written. 2917 */ 2918 if (NInoAttr(ni) || ni->nr_extents == -1) { 2919 NInoClearDirty(ni); 2920 ntfs_debug("Done."); 2921 return 0; 2922 } 2923 2924 /* igrab prevents vi from being evicted while mrec_lock is hold. */ 2925 if (igrab(vi) != NULL) 2926 need_iput = true; 2927 2928 mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL); 2929 /* Map, pin, and lock the mft record belonging to the inode. */ 2930 m = map_mft_record(ni); 2931 if (IS_ERR(m)) { 2932 mutex_unlock(&ni->mrec_lock); 2933 err = PTR_ERR(m); 2934 goto err_out; 2935 } 2936 2937 if (NInoNonResident(ni) && NInoRunlistDirty(ni)) { 2938 down_write(&ni->runlist.lock); 2939 err = ntfs_attr_update_mapping_pairs_locked(ni, 0, ni); 2940 if (!err) 2941 NInoClearRunlistDirty(ni); 2942 up_write(&ni->runlist.lock); 2943 } 2944 2945 err = ntfs_inode_sync_standard_information(vi, m); 2946 if (err) 2947 goto unm_err_out; 2948 2949 /* 2950 * when being umounted and inodes are evicted, write_inode() 2951 * is called with all inodes being marked with I_FREEING. 2952 * then ntfs_inode_sync_filename() waits infinitly because 2953 * of ntfs_iget. This situation happens only where sync_filesysem() 2954 * from umount fails because of a disk unplug and etc. 2955 * the absent of SB_ACTIVE means umounting. 2956 */ 2957 if ((vi->i_sb->s_flags & SB_ACTIVE) && NInoTestClearFileNameDirty(ni)) 2958 ntfs_inode_sync_filename(ni); 2959 2960 /* Now the access times are updated, write the base mft record. */ 2961 if (NInoDirty(ni)) { 2962 down_read(&mft_ni->runlist.lock); 2963 err = ntfs_get_block_mft_record(mft_ni, ni); 2964 up_read(&mft_ni->runlist.lock); 2965 if (err) 2966 goto unm_err_out; 2967 2968 err = write_mft_record(ni, m, sync); 2969 if (err) 2970 ntfs_error(vi->i_sb, "write_mft_record failed, err : %d\n", err); 2971 } 2972 unmap_mft_record(ni); 2973 2974 /* Map any unmapped extent mft records with LCNs. */ 2975 down_read(&mft_ni->runlist.lock); 2976 mutex_lock(&ni->extent_lock); 2977 if (ni->nr_extents > 0) { 2978 int i; 2979 2980 for (i = 0; i < ni->nr_extents; i++) { 2981 err = ntfs_get_block_mft_record(mft_ni, 2982 ni->ext.extent_ntfs_inos[i]); 2983 if (err) { 2984 mutex_unlock(&ni->extent_lock); 2985 up_read(&mft_ni->runlist.lock); 2986 mutex_unlock(&ni->mrec_lock); 2987 goto err_out; 2988 } 2989 } 2990 } 2991 mutex_unlock(&ni->extent_lock); 2992 up_read(&mft_ni->runlist.lock); 2993 2994 /* Write all attached extent mft records. */ 2995 mutex_lock(&ni->extent_lock); 2996 if (ni->nr_extents > 0) { 2997 struct ntfs_inode **extent_nis = ni->ext.extent_ntfs_inos; 2998 int i; 2999 3000 ntfs_debug("Writing %i extent inodes.", ni->nr_extents); 3001 for (i = 0; i < ni->nr_extents; i++) { 3002 struct ntfs_inode *tni = extent_nis[i]; 3003 3004 if (NInoDirty(tni)) { 3005 struct mft_record *tm; 3006 int ret; 3007 3008 mutex_lock(&tni->mrec_lock); 3009 tm = map_mft_record(tni); 3010 if (IS_ERR(tm)) { 3011 mutex_unlock(&tni->mrec_lock); 3012 if (!err || err == -ENOMEM) 3013 err = PTR_ERR(tm); 3014 continue; 3015 } 3016 3017 ret = write_mft_record(tni, tm, sync); 3018 unmap_mft_record(tni); 3019 mutex_unlock(&tni->mrec_lock); 3020 3021 if (unlikely(ret)) { 3022 if (!err || err == -ENOMEM) 3023 err = ret; 3024 } 3025 } 3026 } 3027 } 3028 mutex_unlock(&ni->extent_lock); 3029 mutex_unlock(&ni->mrec_lock); 3030 3031 if (unlikely(err)) 3032 goto err_out; 3033 if (need_iput) 3034 iput(vi); 3035 ntfs_debug("Done."); 3036 return 0; 3037 unm_err_out: 3038 unmap_mft_record(ni); 3039 mutex_unlock(&ni->mrec_lock); 3040 err_out: 3041 if (err == -ENOMEM) 3042 mark_inode_dirty(vi); 3043 else { 3044 ntfs_error(vi->i_sb, "Failed (error %i): Run chkdsk.", -err); 3045 NVolSetErrors(ni->vol); 3046 } 3047 if (need_iput) 3048 iput(vi); 3049 return err; 3050 } 3051 3052 /* 3053 * ntfs_extent_inode_open - load an extent inode and attach it to its base 3054 * @base_ni: base ntfs inode 3055 * @mref: mft reference of the extent inode to load (in little endian) 3056 * 3057 * First check if the extent inode @mref is already attached to the base ntfs 3058 * inode @base_ni, and if so, return a pointer to the attached extent inode. 3059 * 3060 * If the extent inode is not already attached to the base inode, allocate an 3061 * ntfs_inode structure and initialize it for the given inode @mref. @mref 3062 * specifies the inode number / mft record to read, including the sequence 3063 * number, which can be 0 if no sequence number checking is to be performed. 3064 * 3065 * Then, allocate a buffer for the mft record, read the mft record from the 3066 * volume @base_ni->vol, and attach it to the ntfs_inode structure (->mrec). 3067 * The mft record is mst deprotected and sanity checked for validity and we 3068 * abort if deprotection or checks fail. 3069 * 3070 * Finally attach the ntfs inode to its base inode @base_ni and return a 3071 * pointer to the ntfs_inode structure on success or ERR_PTR() on error. 3072 * 3073 * Note, extent inodes are never closed directly. They are automatically 3074 * disposed off by the closing of the base inode. 3075 */ 3076 static struct ntfs_inode *ntfs_extent_inode_open(struct ntfs_inode *base_ni, 3077 const __le64 mref) 3078 { 3079 u64 mft_no = MREF_LE(mref); 3080 struct ntfs_inode *ni = NULL; 3081 struct ntfs_inode **extent_nis; 3082 int i; 3083 struct mft_record *ni_mrec; 3084 struct super_block *sb; 3085 3086 if (!base_ni) 3087 return ERR_PTR(-EINVAL); 3088 3089 sb = base_ni->vol->sb; 3090 ntfs_debug("Opening extent inode %llu (base mft record %llu).\n", 3091 mft_no, base_ni->mft_no); 3092 3093 /* Is the extent inode already open and attached to the base inode? */ 3094 if (base_ni->nr_extents > 0) { 3095 extent_nis = base_ni->ext.extent_ntfs_inos; 3096 for (i = 0; i < base_ni->nr_extents; i++) { 3097 u16 seq_no; 3098 3099 ni = extent_nis[i]; 3100 if (mft_no != ni->mft_no) 3101 continue; 3102 ni_mrec = map_mft_record(ni); 3103 if (IS_ERR(ni_mrec)) { 3104 ntfs_error(sb, "failed to map mft record for %llu", 3105 ni->mft_no); 3106 return ERR_CAST(ni_mrec); 3107 } 3108 /* Verify the sequence number if given. */ 3109 seq_no = MSEQNO_LE(mref); 3110 if (seq_no && 3111 seq_no != le16_to_cpu(ni_mrec->sequence_number)) { 3112 ntfs_error(sb, "Found stale extent mft reference mft=%llu", 3113 ni->mft_no); 3114 unmap_mft_record(ni); 3115 return ERR_PTR(-EIO); 3116 } 3117 unmap_mft_record(ni); 3118 goto out; 3119 } 3120 } 3121 /* Wasn't there, we need to load the extent inode. */ 3122 ni = ntfs_new_extent_inode(base_ni->vol->sb, mft_no); 3123 if (!ni) 3124 return ERR_PTR(-ENOMEM); 3125 3126 ni->seq_no = (u16)MSEQNO_LE(mref); 3127 ni->nr_extents = -1; 3128 ni->ext.base_ntfs_ino = base_ni; 3129 /* Attach extent inode to base inode, reallocating memory if needed. */ 3130 if (!(base_ni->nr_extents & 3)) { 3131 i = (base_ni->nr_extents + 4) * sizeof(struct ntfs_inode *); 3132 3133 extent_nis = kvzalloc(i, GFP_NOFS); 3134 if (!extent_nis) { 3135 ntfs_destroy_ext_inode(ni); 3136 return ERR_PTR(-ENOMEM); 3137 } 3138 if (base_ni->nr_extents) { 3139 memcpy(extent_nis, base_ni->ext.extent_ntfs_inos, 3140 i - 4 * sizeof(struct ntfs_inode *)); 3141 kvfree(base_ni->ext.extent_ntfs_inos); 3142 } 3143 base_ni->ext.extent_ntfs_inos = extent_nis; 3144 } 3145 base_ni->ext.extent_ntfs_inos[base_ni->nr_extents++] = ni; 3146 3147 out: 3148 ntfs_debug("\n"); 3149 return ni; 3150 } 3151 3152 /* 3153 * ntfs_inode_attach_all_extents - attach all extents for target inode 3154 * @ni: opened ntfs inode for which perform attach 3155 * 3156 * Return 0 on success and error. 3157 */ 3158 int ntfs_inode_attach_all_extents(struct ntfs_inode *ni) 3159 { 3160 struct attr_list_entry *ale; 3161 u64 prev_attached = 0; 3162 3163 if (!ni) { 3164 ntfs_debug("Invalid arguments.\n"); 3165 return -EINVAL; 3166 } 3167 3168 if (NInoAttr(ni)) 3169 ni = ni->ext.base_ntfs_ino; 3170 3171 ntfs_debug("Entering for inode 0x%llx.\n", ni->mft_no); 3172 3173 /* Inode haven't got attribute list, thus nothing to attach. */ 3174 if (!NInoAttrList(ni)) 3175 return 0; 3176 3177 if (!ni->attr_list) { 3178 ntfs_debug("Corrupt in-memory struct.\n"); 3179 return -EINVAL; 3180 } 3181 3182 /* Walk through attribute list and attach all extents. */ 3183 ale = (struct attr_list_entry *)ni->attr_list; 3184 while ((u8 *)ale < ni->attr_list + ni->attr_list_size) { 3185 if (ni->mft_no != MREF_LE(ale->mft_reference) && 3186 prev_attached != MREF_LE(ale->mft_reference)) { 3187 struct ntfs_inode *ext_ni; 3188 3189 ext_ni = ntfs_extent_inode_open(ni, ale->mft_reference); 3190 if (IS_ERR(ext_ni)) { 3191 ntfs_debug("Couldn't attach extent inode.\n"); 3192 return PTR_ERR(ext_ni); 3193 } 3194 prev_attached = MREF_LE(ale->mft_reference); 3195 } 3196 ale = (struct attr_list_entry *)((u8 *)ale + le16_to_cpu(ale->length)); 3197 } 3198 return 0; 3199 } 3200 3201 /* 3202 * ntfs_inode_add_attrlist - add attribute list to inode and fill it 3203 * @ni: opened ntfs inode to which add attribute list 3204 * 3205 * Return 0 on success or error. 3206 */ 3207 int ntfs_inode_add_attrlist(struct ntfs_inode *ni) 3208 { 3209 int err; 3210 struct ntfs_attr_search_ctx *ctx; 3211 u8 *al = NULL, *aln; 3212 int al_len = 0; 3213 struct attr_list_entry *ale = NULL; 3214 struct mft_record *ni_mrec; 3215 u32 attr_al_len; 3216 bool free_empty_extents = true; 3217 3218 if (!ni) 3219 return -EINVAL; 3220 3221 ntfs_debug("inode %llu\n", ni->mft_no); 3222 3223 if (NInoAttrList(ni) || ni->nr_extents) { 3224 ntfs_error(ni->vol->sb, "Inode already has attribute list"); 3225 return -EEXIST; 3226 } 3227 3228 ni_mrec = map_mft_record(ni); 3229 if (IS_ERR(ni_mrec)) 3230 return -EIO; 3231 3232 /* Form attribute list. */ 3233 ctx = ntfs_attr_get_search_ctx(ni, ni_mrec); 3234 if (!ctx) { 3235 err = -ENOMEM; 3236 goto err_out; 3237 } 3238 3239 /* Walk through all attributes. */ 3240 while (!(err = ntfs_attr_lookup(AT_UNUSED, NULL, 0, 0, 0, NULL, 0, ctx))) { 3241 int ale_size; 3242 3243 if (ctx->attr->type == AT_ATTRIBUTE_LIST) { 3244 err = -EIO; 3245 ntfs_error(ni->vol->sb, "Attribute list already present"); 3246 goto put_err_out; 3247 } 3248 3249 ale_size = (sizeof(struct attr_list_entry) + sizeof(__le16) * 3250 ctx->attr->name_length + 7) & ~7; 3251 al_len += ale_size; 3252 3253 aln = kvrealloc(al, al_len, GFP_NOFS); 3254 if (!aln) { 3255 err = -ENOMEM; 3256 ntfs_error(ni->vol->sb, "Failed to realloc %d bytes", al_len); 3257 goto put_err_out; 3258 } 3259 ale = (struct attr_list_entry *)(aln + ((u8 *)ale - al)); 3260 al = aln; 3261 3262 memset(ale, 0, ale_size); 3263 3264 /* Add attribute to attribute list. */ 3265 ale->type = ctx->attr->type; 3266 ale->length = cpu_to_le16((sizeof(struct attr_list_entry) + 3267 sizeof(__le16) * ctx->attr->name_length + 7) & ~7); 3268 ale->name_length = ctx->attr->name_length; 3269 ale->name_offset = (u8 *)ale->name - (u8 *)ale; 3270 if (ctx->attr->non_resident) 3271 ale->lowest_vcn = 3272 ctx->attr->data.non_resident.lowest_vcn; 3273 else 3274 ale->lowest_vcn = 0; 3275 ale->mft_reference = MK_LE_MREF(ni->mft_no, 3276 le16_to_cpu(ni_mrec->sequence_number)); 3277 ale->instance = ctx->attr->instance; 3278 memcpy(ale->name, (u8 *)ctx->attr + 3279 le16_to_cpu(ctx->attr->name_offset), 3280 ctx->attr->name_length * sizeof(__le16)); 3281 ale = (struct attr_list_entry *)(al + al_len); 3282 } 3283 3284 /* Check for real error occurred. */ 3285 if (err != -ENOENT) { 3286 ntfs_error(ni->vol->sb, "%s: Attribute lookup failed, inode %llu", 3287 __func__, ni->mft_no); 3288 goto put_err_out; 3289 } 3290 3291 /* Set in-memory attribute list. */ 3292 ni->attr_list = al; 3293 ni->attr_list_size = al_len; 3294 NInoSetAttrList(ni); 3295 3296 attr_al_len = offsetof(struct attr_record, data.resident.reserved) + 1 + 3297 ((al_len + 7) & ~7); 3298 /* Free space if there is not enough it for $ATTRIBUTE_LIST. */ 3299 if (le32_to_cpu(ni_mrec->bytes_allocated) - 3300 le32_to_cpu(ni_mrec->bytes_in_use) < attr_al_len) { 3301 if (ntfs_inode_free_space(ni, (int)attr_al_len)) { 3302 /* Failed to free space. */ 3303 err = -ENOSPC; 3304 ntfs_error(ni->vol->sb, "Failed to free space for attrlist"); 3305 goto rollback; 3306 } 3307 } 3308 3309 /* Add $ATTRIBUTE_LIST to mft record. */ 3310 err = ntfs_resident_attr_record_add(ni, AT_ATTRIBUTE_LIST, AT_UNNAMED, 0, 3311 NULL, al_len, 0); 3312 if (err < 0) { 3313 ntfs_error(ni->vol->sb, "Couldn't add $ATTRIBUTE_LIST to MFT"); 3314 goto rollback; 3315 } 3316 free_empty_extents = false; 3317 3318 err = ntfs_attrlist_update(ni); 3319 if (err < 0) 3320 goto remove_attrlist_record; 3321 3322 ntfs_attr_put_search_ctx(ctx); 3323 unmap_mft_record(ni); 3324 return 0; 3325 3326 remove_attrlist_record: 3327 /* Prevent ntfs_attr_recorm_rm from freeing attribute list. */ 3328 ni->attr_list = NULL; 3329 NInoClearAttrList(ni); 3330 /* Remove $ATTRIBUTE_LIST record. */ 3331 ntfs_attr_reinit_search_ctx(ctx); 3332 if (!ntfs_attr_lookup(AT_ATTRIBUTE_LIST, NULL, 0, 3333 CASE_SENSITIVE, 0, NULL, 0, ctx)) { 3334 if (ntfs_attr_record_rm(ctx)) 3335 ntfs_error(ni->vol->sb, "Rollback failed to remove attrlist"); 3336 else 3337 free_empty_extents = true; 3338 } else { 3339 ntfs_error(ni->vol->sb, "Rollback failed to find attrlist"); 3340 } 3341 3342 /* Setup back in-memory runlist. */ 3343 ni->attr_list = al; 3344 ni->attr_list_size = al_len; 3345 NInoSetAttrList(ni); 3346 rollback: 3347 /* 3348 * Scan attribute list for attributes that placed not in the base MFT 3349 * record and move them to it. 3350 */ 3351 ntfs_attr_reinit_search_ctx(ctx); 3352 ale = (struct attr_list_entry *)al; 3353 while ((u8 *)ale < al + al_len) { 3354 if (MREF_LE(ale->mft_reference) != ni->mft_no) { 3355 if (!ntfs_attr_lookup(ale->type, ale->name, 3356 ale->name_length, 3357 CASE_SENSITIVE, 3358 le64_to_cpu(ale->lowest_vcn), 3359 NULL, 0, ctx)) { 3360 if (ntfs_attr_record_move_to(ctx, ni)) 3361 ntfs_error(ni->vol->sb, 3362 "Rollback failed to move attribute"); 3363 } else { 3364 ntfs_error(ni->vol->sb, "Rollback failed to find attr"); 3365 } 3366 ntfs_attr_reinit_search_ctx(ctx); 3367 } 3368 ale = (struct attr_list_entry *)((u8 *)ale + le16_to_cpu(ale->length)); 3369 } 3370 3371 /* Remove in-memory attribute list. */ 3372 ni->attr_list = NULL; 3373 ni->attr_list_size = 0; 3374 NInoClearAttrList(ni); 3375 NInoClearAttrListDirty(ni); 3376 ntfs_attr_put_search_ctx(ctx); 3377 ctx = NULL; 3378 if (free_empty_extents && ntfs_inode_free_empty_extents(ni)) 3379 ntfs_error(ni->vol->sb, "Rollback failed to free empty extent"); 3380 goto err_out; 3381 put_err_out: 3382 ntfs_attr_put_search_ctx(ctx); 3383 err_out: 3384 kvfree(al); 3385 unmap_mft_record(ni); 3386 return err; 3387 } 3388 3389 /* 3390 * ntfs_inode_close - close an ntfs inode and free all associated memory 3391 * @ni: ntfs inode to close 3392 * 3393 * Make sure the ntfs inode @ni is clean. 3394 * 3395 * If the ntfs inode @ni is a base inode, close all associated extent inodes, 3396 * then deallocate all memory attached to it, and finally free the ntfs inode 3397 * structure itself. 3398 * 3399 * If it is an extent inode, we disconnect it from its base inode before we 3400 * destroy it. 3401 * 3402 * It is OK to pass NULL to this function, it is just noop in this case. 3403 * 3404 * Return 0 on success or error. 3405 */ 3406 int ntfs_inode_close(struct ntfs_inode *ni) 3407 { 3408 int err = -1; 3409 struct ntfs_inode **tmp_nis; 3410 struct ntfs_inode *base_ni; 3411 s32 i; 3412 3413 if (!ni) 3414 return 0; 3415 3416 ntfs_debug("Entering for inode %llu\n", ni->mft_no); 3417 3418 /* Is this a base inode with mapped extent inodes? */ 3419 /* 3420 * If the inode is an extent inode, disconnect it from the 3421 * base inode before destroying it. 3422 */ 3423 base_ni = ni->ext.base_ntfs_ino; 3424 tmp_nis = base_ni->ext.extent_ntfs_inos; 3425 if (!tmp_nis) 3426 goto out; 3427 for (i = 0; i < base_ni->nr_extents; ++i) { 3428 if (tmp_nis[i] != ni) 3429 continue; 3430 /* Found it. Disconnect. */ 3431 memmove(tmp_nis + i, tmp_nis + i + 1, 3432 (base_ni->nr_extents - i - 1) * 3433 sizeof(struct ntfs_inode *)); 3434 /* Buffer should be for multiple of four extents. */ 3435 if ((--base_ni->nr_extents) & 3) 3436 break; 3437 /* 3438 * ElectricFence is unhappy with realloc(x,0) as free(x) 3439 * thus we explicitly separate these two cases. 3440 */ 3441 if (base_ni->nr_extents) { 3442 /* Resize the memory buffer. */ 3443 tmp_nis = kvrealloc(tmp_nis, base_ni->nr_extents * 3444 sizeof(struct ntfs_inode *), GFP_NOFS); 3445 /* Ignore errors, they don't really matter. */ 3446 if (tmp_nis) 3447 base_ni->ext.extent_ntfs_inos = tmp_nis; 3448 } else if (tmp_nis) { 3449 kvfree(tmp_nis); 3450 base_ni->ext.extent_ntfs_inos = NULL; 3451 } 3452 break; 3453 } 3454 3455 out: 3456 if (NInoDirty(ni)) 3457 ntfs_error(ni->vol->sb, "Releasing dirty inode %llu!\n", 3458 ni->mft_no); 3459 if (NInoAttrList(ni) && ni->attr_list) 3460 kvfree(ni->attr_list); 3461 ntfs_destroy_ext_inode(ni); 3462 err = 0; 3463 ntfs_debug("\n"); 3464 return err; 3465 } 3466 3467 /* 3468 * ntfs_inode_free_empty_extents - free empty extent MFT records 3469 * @ni: base inode whose empty extent records should be freed 3470 * 3471 * The caller must ensure that no on-disk attribute list references an empty 3472 * extent record and must hold @ni->mrec_lock to serialize the extent array. 3473 */ 3474 int ntfs_inode_free_empty_extents(struct ntfs_inode *ni) 3475 { 3476 int err = 0, i = 0; 3477 3478 if (!ni || ni->nr_extents < 0) 3479 return -EINVAL; 3480 3481 mutex_lock(&ni->extent_lock); 3482 while (i < ni->nr_extents) { 3483 struct ntfs_inode *ext_ni = ni->ext.extent_ntfs_inos[i]; 3484 struct mft_record *m; 3485 int ret; 3486 3487 m = map_mft_record(ext_ni); 3488 if (IS_ERR(m)) { 3489 if (!err) 3490 err = PTR_ERR(m); 3491 i++; 3492 continue; 3493 } 3494 if (le32_to_cpu(m->bytes_in_use) - 3495 le16_to_cpu(m->attrs_offset) != 8) { 3496 unmap_mft_record(ext_ni); 3497 i++; 3498 continue; 3499 } 3500 unmap_mft_record(ext_ni); 3501 3502 ret = ntfs_mft_record_free(ni->vol, ext_ni); 3503 if (ret) { 3504 if (!err) 3505 err = ret; 3506 i++; 3507 continue; 3508 } 3509 ntfs_inode_close(ext_ni); 3510 /* ntfs_inode_close() removed this entry from the extent array. */ 3511 } 3512 mutex_unlock(&ni->extent_lock); 3513 return err; 3514 } 3515 3516 void ntfs_destroy_ext_inode(struct ntfs_inode *ni) 3517 { 3518 ntfs_debug("Entering."); 3519 if (ni == NULL) 3520 return; 3521 3522 ntfs_attr_close(ni); 3523 3524 if (NInoDirty(ni)) 3525 ntfs_error(ni->vol->sb, "Releasing dirty ext inode %llu!\n", 3526 ni->mft_no); 3527 if (NInoAttrList(ni) && ni->attr_list) 3528 kvfree(ni->attr_list); 3529 kfree(ni->mrec); 3530 kmem_cache_free(ntfs_inode_cache, ni); 3531 } 3532 3533 static struct ntfs_inode *ntfs_inode_base(struct ntfs_inode *ni) 3534 { 3535 if (ni->nr_extents == -1) 3536 return ni->ext.base_ntfs_ino; 3537 return ni; 3538 } 3539 3540 static int ntfs_attr_position(__le32 type, struct ntfs_attr_search_ctx *ctx) 3541 { 3542 int err; 3543 3544 err = ntfs_attr_lookup(type, NULL, 0, CASE_SENSITIVE, 0, NULL, 3545 0, ctx); 3546 if (err) { 3547 __le32 atype; 3548 3549 if (err != -ENOENT) 3550 return err; 3551 3552 atype = ctx->attr->type; 3553 if (atype == AT_END) 3554 return -ENOSPC; 3555 3556 /* 3557 * if ntfs_external_attr_lookup return -ENOENT, ctx->al_entry 3558 * could point to an attribute in an extent mft record, but 3559 * ctx->attr and ctx->ntfs_ino always points to an attibute in 3560 * a base mft record. 3561 */ 3562 if (ctx->al_entry && 3563 MREF_LE(ctx->al_entry->mft_reference) != ctx->ntfs_ino->mft_no) { 3564 ntfs_attr_reinit_search_ctx(ctx); 3565 err = ntfs_attr_lookup(atype, NULL, 0, CASE_SENSITIVE, 0, NULL, 3566 0, ctx); 3567 if (err) 3568 return err; 3569 } 3570 } 3571 return 0; 3572 } 3573 3574 /* 3575 * ntfs_inode_free_space - free space in the MFT record of inode 3576 * @ni: ntfs inode in which MFT record free space 3577 * @size: amount of space needed to free 3578 * 3579 * Return 0 on success or error. 3580 */ 3581 int ntfs_inode_free_space(struct ntfs_inode *ni, int size) 3582 { 3583 struct ntfs_attr_search_ctx *ctx; 3584 int freed, err; 3585 struct mft_record *ni_mrec; 3586 struct super_block *sb; 3587 3588 if (!ni || size < 0) 3589 return -EINVAL; 3590 ntfs_debug("Entering for inode %llu, size %d\n", ni->mft_no, size); 3591 3592 sb = ni->vol->sb; 3593 ni_mrec = map_mft_record(ni); 3594 if (IS_ERR(ni_mrec)) 3595 return -EIO; 3596 3597 freed = (le32_to_cpu(ni_mrec->bytes_allocated) - 3598 le32_to_cpu(ni_mrec->bytes_in_use)); 3599 3600 unmap_mft_record(ni); 3601 3602 if (size <= freed) 3603 return 0; 3604 3605 ctx = ntfs_attr_get_search_ctx(ni, NULL); 3606 if (!ctx) { 3607 ntfs_error(sb, "%s, Failed to get search context", __func__); 3608 return -ENOMEM; 3609 } 3610 3611 /* 3612 * Chkdsk complain if $STANDARD_INFORMATION is not in the base MFT 3613 * record. 3614 * 3615 * $INDEX_ROOT must remain resident, but its attribute record may be moved 3616 * to an extent MFT record when the base record needs room for the list. 3617 * 3618 * Also we can't move $ATTRIBUTE_LIST from base MFT_RECORD, so position 3619 * search context on first attribute after $STANDARD_INFORMATION and 3620 * $ATTRIBUTE_LIST. 3621 * 3622 * Why we reposition instead of simply skip this attributes during 3623 * enumeration? Because in case we have got only in-memory attribute 3624 * list ntfs_attr_lookup will fail when it will try to find 3625 * $ATTRIBUTE_LIST. 3626 */ 3627 err = ntfs_attr_position(AT_FILE_NAME, ctx); 3628 if (err) 3629 goto put_err_out; 3630 3631 while (1) { 3632 int record_size; 3633 3634 /* 3635 * Check whether attribute is from different MFT record. If so, 3636 * find next, because we don't need such. 3637 */ 3638 while (ctx->ntfs_ino->mft_no != ni->mft_no) { 3639 retry: 3640 err = ntfs_attr_lookup(AT_UNUSED, NULL, 0, CASE_SENSITIVE, 3641 0, NULL, 0, ctx); 3642 if (err) { 3643 if (err != -ENOENT) 3644 ntfs_error(sb, "Attr lookup failed #2"); 3645 else if (ctx->attr->type == AT_END) 3646 err = -ENOSPC; 3647 else 3648 err = 0; 3649 3650 if (err) 3651 goto put_err_out; 3652 } 3653 } 3654 3655 if (ntfs_inode_base(ctx->ntfs_ino)->mft_no == FILE_MFT && 3656 ctx->attr->type == AT_DATA) 3657 goto retry; 3658 3659 record_size = le32_to_cpu(ctx->attr->length); 3660 3661 /* Move away attribute. */ 3662 err = ntfs_attr_record_move_away(ctx, 0); 3663 if (err) { 3664 ntfs_error(sb, "Failed to move out attribute #2"); 3665 break; 3666 } 3667 freed += record_size; 3668 3669 /* Check whether we done. */ 3670 if (size <= freed) { 3671 ntfs_attr_put_search_ctx(ctx); 3672 return 0; 3673 } 3674 3675 /* 3676 * Reposition to first attribute after $STANDARD_INFORMATION and 3677 * $ATTRIBUTE_LIST (see comments upwards). 3678 */ 3679 ntfs_attr_reinit_search_ctx(ctx); 3680 err = ntfs_attr_position(AT_FILE_NAME, ctx); 3681 if (err) 3682 break; 3683 } 3684 put_err_out: 3685 ntfs_attr_put_search_ctx(ctx); 3686 if (err == -ENOSPC) 3687 ntfs_debug("No attributes left that can be moved out.\n"); 3688 return err; 3689 } 3690 3691 s64 ntfs_inode_attr_pread(struct inode *vi, s64 pos, s64 count, u8 *buf) 3692 { 3693 struct address_space *mapping = vi->i_mapping; 3694 struct folio *folio; 3695 struct ntfs_inode *ni = NTFS_I(vi); 3696 struct ntfs_inode *base_ni = ni->ext.base_ntfs_ino; 3697 struct ntfs_inode *mrec_ni = ntfs_inode_is_named_stream(ni) ? 3698 base_ni : ni; 3699 s64 isize; 3700 u32 attr_len, total = 0, offset; 3701 pgoff_t index; 3702 int err = 0; 3703 bool claimed = false; 3704 3705 WARN_ON(!NInoAttr(ni)); 3706 if (!count) 3707 return 0; 3708 3709 mutex_lock(&mrec_ni->mrec_lock); 3710 if (ntfs_inode_is_named_stream(ni)) { 3711 err = ntfs_stream_inode_validate(vi); 3712 if (err) { 3713 mutex_unlock(&mrec_ni->mrec_lock); 3714 return err; 3715 } 3716 } 3717 isize = i_size_read(vi); 3718 if (pos > isize) { 3719 mutex_unlock(&mrec_ni->mrec_lock); 3720 return -EINVAL; 3721 } 3722 if (pos + count > isize) 3723 count = isize - pos; 3724 3725 if (!NInoNonResident(ni)) { 3726 struct ntfs_attr_search_ctx *ctx; 3727 u8 *attr; 3728 3729 ctx = ntfs_attr_get_search_ctx(base_ni, NULL); 3730 if (!ctx) { 3731 ntfs_error(vi->i_sb, "Failed to get attr search ctx"); 3732 err = -ENOMEM; 3733 mutex_unlock(&mrec_ni->mrec_lock); 3734 goto out; 3735 } 3736 3737 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 3738 CASE_SENSITIVE, 3739 0, NULL, 0, ctx); 3740 if (err) { 3741 ntfs_error(vi->i_sb, "Failed to look up attr %#x", ni->type); 3742 ntfs_attr_put_search_ctx(ctx); 3743 mutex_unlock(&mrec_ni->mrec_lock); 3744 goto out; 3745 } 3746 3747 attr = (u8 *)ctx->attr + le16_to_cpu(ctx->attr->data.resident.value_offset); 3748 memcpy(buf, (u8 *)attr + pos, count); 3749 ntfs_attr_put_search_ctx(ctx); 3750 mutex_unlock(&mrec_ni->mrec_lock); 3751 return count; 3752 } 3753 if (ntfs_inode_is_named_stream(ni)) { 3754 atomic_inc(&ni->stream_open_count); 3755 claimed = true; 3756 } 3757 mutex_unlock(&mrec_ni->mrec_lock); 3758 3759 index = pos >> PAGE_SHIFT; 3760 do { 3761 /* Update @index and get the next folio. */ 3762 folio = read_mapping_folio(mapping, index, NULL); 3763 if (IS_ERR(folio)) { 3764 err = PTR_ERR(folio); 3765 break; 3766 } 3767 3768 offset = offset_in_folio(folio, pos); 3769 attr_len = min_t(size_t, (size_t)count, folio_size(folio) - offset); 3770 3771 folio_lock(folio); 3772 memcpy_from_folio(buf, folio, offset, attr_len); 3773 folio_unlock(folio); 3774 folio_put(folio); 3775 3776 total += attr_len; 3777 buf += attr_len; 3778 pos += attr_len; 3779 count -= attr_len; 3780 index++; 3781 } while (count); 3782 out: 3783 if (claimed) 3784 ntfs_stream_put(vi); 3785 return err ? (s64)err : total; 3786 } 3787 3788 static inline int ntfs_enlarge_attribute(struct inode *vi, s64 pos, s64 count, 3789 struct ntfs_attr_search_ctx *ctx) 3790 { 3791 struct ntfs_inode *ni = NTFS_I(vi); 3792 struct super_block *sb = vi->i_sb; 3793 int ret; 3794 3795 if (pos + count <= ni->initialized_size) 3796 return 0; 3797 3798 if (NInoEncrypted(ni) && NInoNonResident(ni)) 3799 return -EACCES; 3800 3801 if (NInoCompressed(ni)) 3802 return -EOPNOTSUPP; 3803 3804 if (pos + count > ni->data_size) { 3805 ret = ntfs_attr_truncate(ni, pos + count); 3806 if (ret) { 3807 ntfs_debug("Failed to truncate attribute"); 3808 return ret; 3809 } 3810 3811 ntfs_attr_reinit_search_ctx(ctx); 3812 ret = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 3813 CASE_SENSITIVE, 3814 0, NULL, 0, ctx); 3815 if (ret) { 3816 ntfs_error(sb, "Failed to look up attr %#x", ni->type); 3817 return ret; 3818 } 3819 } 3820 3821 if (!NInoNonResident(ni)) { 3822 if (likely(i_size_read(vi) < ni->data_size)) 3823 i_size_write(vi, ni->data_size); 3824 return 0; 3825 } 3826 3827 /* Named streams initialize gaps after dropping the MFT record lock. */ 3828 if (ntfs_inode_is_named_stream(ni)) { 3829 if (i_size_read(vi) < ni->data_size) 3830 i_size_write(vi, ni->data_size); 3831 return 0; 3832 } 3833 3834 if (pos + count > ni->initialized_size) { 3835 ctx->attr->data.non_resident.initialized_size = cpu_to_le64(pos + count); 3836 mark_mft_record_dirty(ctx->ntfs_ino); 3837 ni->initialized_size = pos + count; 3838 if (i_size_read(vi) < ni->initialized_size) 3839 i_size_write(vi, ni->initialized_size); 3840 } 3841 return 0; 3842 } 3843 3844 static s64 __ntfs_inode_resident_attr_pwrite(struct inode *vi, 3845 s64 pos, s64 count, u8 *buf, 3846 struct ntfs_attr_search_ctx *ctx, 3847 bool *modified) 3848 { 3849 struct ntfs_inode *ni = NTFS_I(vi); 3850 struct folio *folio; 3851 struct address_space *mapping = vi->i_mapping; 3852 u8 *addr; 3853 int err = 0; 3854 3855 WARN_ON(NInoNonResident(ni)); 3856 if (pos + count > PAGE_SIZE) { 3857 ntfs_error(vi->i_sb, "Out of write into resident attr %#x", ni->type); 3858 return -EINVAL; 3859 } 3860 3861 /* Copy to mft record page */ 3862 addr = (u8 *)ctx->attr + le16_to_cpu(ctx->attr->data.resident.value_offset); 3863 memcpy(addr + pos, buf, count); 3864 mark_mft_record_dirty(ctx->ntfs_ino); 3865 if (count) 3866 *modified = true; 3867 3868 /* Keep the first page clean and uptodate */ 3869 folio = __filemap_get_folio(mapping, 0, FGP_WRITEBEGIN | FGP_NOFS, 3870 mapping_gfp_mask(mapping)); 3871 if (IS_ERR(folio)) { 3872 err = PTR_ERR(folio); 3873 ntfs_error(vi->i_sb, "Failed to read a page 0 for attr %#x: %d", 3874 ni->type, err); 3875 goto out; 3876 } 3877 if (!folio_test_uptodate(folio)) 3878 folio_fill_tail(folio, 0, addr, 3879 le32_to_cpu(ctx->attr->data.resident.value_length)); 3880 else 3881 memcpy_to_folio(folio, offset_in_folio(folio, pos), buf, count); 3882 folio_mark_uptodate(folio); 3883 folio_unlock(folio); 3884 folio_put(folio); 3885 out: 3886 return err ? err : count; 3887 } 3888 3889 static s64 __ntfs_inode_non_resident_attr_pwrite(struct inode *vi, 3890 s64 pos, s64 count, u8 *buf, 3891 bool sync, bool *modified) 3892 { 3893 struct ntfs_inode *ni = NTFS_I(vi); 3894 struct address_space *mapping = vi->i_mapping; 3895 struct folio *folio; 3896 pgoff_t index; 3897 unsigned long offset, length; 3898 size_t attr_len; 3899 s64 ret = 0, written = 0; 3900 3901 WARN_ON(!NInoNonResident(ni)); 3902 3903 index = pos >> PAGE_SHIFT; 3904 while (count) { 3905 if (count == PAGE_SIZE && !offset_in_page(pos)) { 3906 folio = __filemap_get_folio(vi->i_mapping, index, 3907 FGP_CREAT | FGP_LOCK, 3908 mapping_gfp_mask(mapping)); 3909 if (IS_ERR(folio)) { 3910 ret = PTR_ERR(folio); 3911 break; 3912 } 3913 } else { 3914 folio = read_mapping_folio(mapping, index, NULL); 3915 if (IS_ERR(folio)) { 3916 ret = PTR_ERR(folio); 3917 ntfs_error(vi->i_sb, "Failed to read a page %lu for attr %#x: %ld", 3918 index, ni->type, PTR_ERR(folio)); 3919 break; 3920 } 3921 3922 folio_lock(folio); 3923 } 3924 3925 folio_wait_writeback(folio); 3926 3927 if (count == PAGE_SIZE && !offset_in_page(pos)) { 3928 offset = 0; 3929 attr_len = count; 3930 } else { 3931 offset = offset_in_folio(folio, pos); 3932 attr_len = min_t(size_t, (size_t)count, folio_size(folio) - offset); 3933 } 3934 memcpy_to_folio(folio, offset, buf, attr_len); 3935 *modified = true; 3936 3937 if (sync) { 3938 struct ntfs_volume *vol = ni->vol; 3939 s64 lcn, lcn_count; 3940 unsigned int lcn_folio_off = 0; 3941 struct bio *bio; 3942 u64 rl_length = 0; 3943 s64 vcn; 3944 struct runlist_element *rl; 3945 int bio_err; 3946 3947 lcn_count = max_t(s64, 1, 3948 ntfs_bytes_to_cluster(vol, attr_len + 3949 vol->cluster_size - 1)); 3950 vcn = ntfs_pidx_to_cluster(vol, folio->index); 3951 3952 do { 3953 down_write(&ni->runlist.lock); 3954 rl = ntfs_attr_vcn_to_rl(ni, vcn, &lcn); 3955 if (IS_ERR(rl)) { 3956 ret = PTR_ERR(rl); 3957 up_write(&ni->runlist.lock); 3958 goto err_unlock_folio; 3959 } 3960 3961 rl_length = rl->length - (vcn - rl->vcn); 3962 if (rl_length < lcn_count) { 3963 lcn_count -= rl_length; 3964 } else { 3965 rl_length = lcn_count; 3966 lcn_count = 0; 3967 } 3968 up_write(&ni->runlist.lock); 3969 3970 if (vol->cluster_size_bits > PAGE_SHIFT) { 3971 lcn_folio_off = folio->index << PAGE_SHIFT; 3972 lcn_folio_off &= vol->cluster_size_mask; 3973 } 3974 3975 bio = bio_alloc(vol->sb->s_bdev, 1, REQ_OP_WRITE, 3976 GFP_NOIO); 3977 bio->bi_iter.bi_sector = 3978 ntfs_bytes_to_bio_sector(ntfs_cluster_to_bytes(vol, lcn) + 3979 lcn_folio_off); 3980 3981 length = min_t(unsigned long, 3982 ntfs_cluster_to_bytes(vol, rl_length), 3983 folio_size(folio)); 3984 if (!bio_add_folio(bio, folio, length, offset)) { 3985 ret = -EIO; 3986 bio_put(bio); 3987 goto err_unlock_folio; 3988 } 3989 3990 bio_err = submit_bio_wait(bio); 3991 bio_put(bio); 3992 if (bio_err) { 3993 ntfs_error(vi->i_sb, 3994 "Synchronous attribute write failed (%d)", 3995 bio_err); 3996 ret = bio_err; 3997 goto err_unlock_folio; 3998 } 3999 vcn += rl_length; 4000 offset += length; 4001 } while (lcn_count != 0); 4002 4003 folio_mark_uptodate(folio); 4004 } else { 4005 folio_mark_uptodate(folio); 4006 folio_mark_dirty(folio); 4007 } 4008 err_unlock_folio: 4009 folio_unlock(folio); 4010 folio_put(folio); 4011 4012 if (ret) 4013 break; 4014 4015 written += attr_len; 4016 buf += attr_len; 4017 pos += attr_len; 4018 count -= attr_len; 4019 index++; 4020 4021 cond_resched(); 4022 } 4023 4024 return ret ? ret : written; 4025 } 4026 4027 s64 ntfs_inode_attr_pwrite(struct inode *vi, s64 pos, s64 count, u8 *buf, bool sync) 4028 { 4029 struct ntfs_inode *ni = NTFS_I(vi); 4030 struct ntfs_inode *base_ni = ni->ext.base_ntfs_ino; 4031 struct ntfs_inode *mrec_ni = ntfs_inode_is_named_stream(ni) ? 4032 base_ni : ni; 4033 struct ntfs_attr_search_ctx *ctx; 4034 loff_t old_data_size, old_init_size; 4035 s64 ret; 4036 bool modified = false; 4037 4038 WARN_ON(!NInoAttr(ni)); 4039 4040 if (ntfs_inode_is_named_stream(ni)) { 4041 if (NInoEncrypted(ni)) 4042 return -EACCES; 4043 if (NInoCompressed(ni)) 4044 return -EOPNOTSUPP; 4045 } 4046 4047 mutex_lock(&mrec_ni->mrec_lock); 4048 if (ntfs_inode_is_named_stream(ni)) { 4049 ret = ntfs_stream_inode_validate(vi); 4050 if (ret) 4051 goto out_unlock; 4052 } 4053 4054 ctx = ntfs_attr_get_search_ctx(base_ni, NULL); 4055 if (!ctx) { 4056 ntfs_error(vi->i_sb, "Failed to get attr search ctx"); 4057 ret = -ENOMEM; 4058 goto out_unlock; 4059 } 4060 4061 ret = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 4062 CASE_SENSITIVE, 4063 0, NULL, 0, ctx); 4064 if (ret) { 4065 ntfs_attr_put_search_ctx(ctx); 4066 ntfs_error(vi->i_sb, "Failed to look up attr %#x", ni->type); 4067 goto out_unlock; 4068 } 4069 4070 old_data_size = ni->data_size; 4071 old_init_size = ni->initialized_size; 4072 ret = ntfs_enlarge_attribute(vi, pos, count, ctx); 4073 modified = ni->data_size != old_data_size || 4074 ni->initialized_size != old_init_size; 4075 if (ret) 4076 goto out_ctx; 4077 4078 if (!NInoNonResident(ni)) { 4079 ret = __ntfs_inode_resident_attr_pwrite(vi, pos, count, buf, ctx, 4080 &modified); 4081 goto out_ctx; 4082 } 4083 4084 if (ntfs_inode_is_named_stream(ni)) 4085 atomic_inc(&ni->stream_open_count); 4086 ntfs_attr_put_search_ctx(ctx); 4087 mutex_unlock(&mrec_ni->mrec_lock); 4088 /* Attribute writes bypass iomap's delayed-allocation preparation. */ 4089 if (ntfs_inode_is_named_stream(ni)) { 4090 /* A failed write must not expose any newly initialized data. */ 4091 ret = ntfs_extend_initialized_size(vi, pos + count, pos + count); 4092 modified |= ni->initialized_size != old_init_size; 4093 if (!ret) 4094 ret = ntfs_attr_fallocate(ni, pos, count, true); 4095 } 4096 if (!ret) 4097 ret = __ntfs_inode_non_resident_attr_pwrite(vi, pos, count, 4098 buf, sync, 4099 &modified); 4100 if (ntfs_inode_is_named_stream(ni)) 4101 ntfs_stream_put(vi); 4102 if (modified && ntfs_inode_is_named_stream(ni)) 4103 ntfs_stream_update_base_time(base_ni); 4104 return ret; 4105 4106 out_ctx: 4107 ntfs_attr_put_search_ctx(ctx); 4108 out_unlock: 4109 mutex_unlock(&mrec_ni->mrec_lock); 4110 if (modified && ntfs_inode_is_named_stream(ni)) 4111 ntfs_stream_update_base_time(base_ni); 4112 return ret; 4113 } 4114 4115 struct folio *ntfs_get_locked_folio(struct address_space *mapping, 4116 pgoff_t index, pgoff_t end_index, struct file_ra_state *ra) 4117 { 4118 struct folio *folio; 4119 4120 folio = filemap_lock_folio(mapping, index); 4121 if (IS_ERR(folio)) { 4122 if (PTR_ERR(folio) != -ENOENT) 4123 return folio; 4124 4125 page_cache_sync_readahead(mapping, ra, NULL, index, 4126 end_index - index); 4127 folio = read_mapping_folio(mapping, index, NULL); 4128 if (!IS_ERR(folio)) 4129 folio_lock(folio); 4130 } 4131 4132 return folio; 4133 } 4134