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