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