1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * 4 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved. 5 * 6 */ 7 8 #include <linux/fiemap.h> 9 #include <linux/fs.h> 10 #include <linux/minmax.h> 11 #include <linux/vmalloc.h> 12 13 #include "debug.h" 14 #include "ntfs.h" 15 #include "ntfs_fs.h" 16 #ifdef CONFIG_NTFS3_LZX_XPRESS 17 #include "lib/lib.h" 18 #endif 19 20 static struct mft_inode *ni_ins_mi(struct ntfs_inode *ni, struct rb_root *tree, 21 CLST ino, struct rb_node *ins) 22 { 23 struct rb_node **p = &tree->rb_node; 24 struct rb_node *pr = NULL; 25 26 while (*p) { 27 struct mft_inode *mi; 28 29 pr = *p; 30 mi = rb_entry(pr, struct mft_inode, node); 31 if (mi->rno > ino) 32 p = &pr->rb_left; 33 else if (mi->rno < ino) 34 p = &pr->rb_right; 35 else 36 return mi; 37 } 38 39 if (!ins) 40 return NULL; 41 42 rb_link_node(ins, pr, p); 43 rb_insert_color(ins, tree); 44 return rb_entry(ins, struct mft_inode, node); 45 } 46 47 /* 48 * ni_find_mi - Find mft_inode by record number. 49 */ 50 static struct mft_inode *ni_find_mi(struct ntfs_inode *ni, CLST rno) 51 { 52 return ni_ins_mi(ni, &ni->mi_tree, rno, NULL); 53 } 54 55 /* 56 * ni_add_mi - Add new mft_inode into ntfs_inode. 57 */ 58 static void ni_add_mi(struct ntfs_inode *ni, struct mft_inode *mi) 59 { 60 ni_ins_mi(ni, &ni->mi_tree, mi->rno, &mi->node); 61 } 62 63 /* 64 * ni_remove_mi - Remove mft_inode from ntfs_inode. 65 */ 66 void ni_remove_mi(struct ntfs_inode *ni, struct mft_inode *mi) 67 { 68 rb_erase(&mi->node, &ni->mi_tree); 69 } 70 71 /* 72 * ni_std - Return: Pointer into std_info from primary record. 73 */ 74 struct ATTR_STD_INFO *ni_std(struct ntfs_inode *ni) 75 { 76 const struct ATTRIB *attr; 77 78 attr = mi_find_attr(ni, &ni->mi, NULL, ATTR_STD, NULL, 0, NULL); 79 return attr ? resident_data_ex(attr, sizeof(struct ATTR_STD_INFO)) : 80 NULL; 81 } 82 83 /* 84 * ni_std5 85 * 86 * Return: Pointer into std_info from primary record. 87 */ 88 struct ATTR_STD_INFO5 *ni_std5(struct ntfs_inode *ni) 89 { 90 const struct ATTRIB *attr; 91 92 attr = mi_find_attr(ni, &ni->mi, NULL, ATTR_STD, NULL, 0, NULL); 93 94 return attr ? resident_data_ex(attr, sizeof(struct ATTR_STD_INFO5)) : 95 NULL; 96 } 97 98 /* 99 * ni_clear - Clear resources allocated by ntfs_inode. 100 */ 101 void ni_clear(struct ntfs_inode *ni) 102 { 103 struct rb_node *node; 104 105 if (!ni->vfs_inode.i_nlink && ni->mi.mrec && 106 is_rec_inuse(ni->mi.mrec) && 107 !(ni->mi.sbi->flags & NTFS_FLAGS_LOG_REPLAYING)) 108 ni_delete_all(ni); 109 110 al_destroy(ni); 111 112 for (node = rb_first(&ni->mi_tree); node;) { 113 struct rb_node *next = rb_next(node); 114 struct mft_inode *mi = rb_entry(node, struct mft_inode, node); 115 116 rb_erase(node, &ni->mi_tree); 117 mi_put(mi); 118 node = next; 119 } 120 121 /* Bad inode always has mode == S_IFREG. */ 122 if (ni->ni_flags & NI_FLAG_DIR) 123 indx_clear(&ni->dir); 124 else { 125 run_close(&ni->file.run); 126 #ifdef CONFIG_NTFS3_LZX_XPRESS 127 if (ni->file.offs_folio) { 128 /* On-demand allocated page for offsets. */ 129 folio_put(ni->file.offs_folio); 130 ni->file.offs_folio = NULL; 131 } 132 #endif 133 } 134 135 mi_clear(&ni->mi); 136 } 137 138 /* 139 * ni_load_mi_ex - Find mft_inode by record number. 140 */ 141 int ni_load_mi_ex(struct ntfs_inode *ni, CLST rno, struct mft_inode **mi) 142 { 143 int err; 144 struct mft_inode *r; 145 146 r = ni_find_mi(ni, rno); 147 if (r) 148 goto out; 149 150 err = mi_get(ni->mi.sbi, rno, &r); 151 if (err) { 152 _ntfs_bad_inode(&ni->vfs_inode); 153 return err; 154 } 155 156 ni_add_mi(ni, r); 157 158 out: 159 if (mi) 160 *mi = r; 161 return 0; 162 } 163 164 /* 165 * ni_load_mi - Load mft_inode corresponded list_entry. 166 */ 167 int ni_load_mi(struct ntfs_inode *ni, const struct ATTR_LIST_ENTRY *le, 168 struct mft_inode **mi) 169 { 170 CLST rno; 171 172 if (!le) { 173 *mi = &ni->mi; 174 return 0; 175 } 176 177 rno = ino_get(&le->ref); 178 if (rno == ni->mi.rno) { 179 *mi = &ni->mi; 180 return 0; 181 } 182 return ni_load_mi_ex(ni, rno, mi); 183 } 184 185 /* 186 * ni_find_attr 187 * 188 * Return: Attribute and record this attribute belongs to. 189 */ 190 struct ATTRIB *ni_find_attr(struct ntfs_inode *ni, struct ATTRIB *attr, 191 struct ATTR_LIST_ENTRY **le_o, enum ATTR_TYPE type, 192 const __le16 *name, u8 name_len, const CLST *vcn, 193 struct mft_inode **mi) 194 { 195 struct ATTR_LIST_ENTRY *le; 196 struct mft_inode *m; 197 198 if (!ni->attr_list.size || 199 (!name_len && (type == ATTR_LIST || type == ATTR_STD))) { 200 if (le_o) 201 *le_o = NULL; 202 if (mi) 203 *mi = &ni->mi; 204 205 /* Look for required attribute in primary record. */ 206 return mi_find_attr(ni, &ni->mi, attr, type, name, name_len, 207 NULL); 208 } 209 210 /* First look for list entry of required type. */ 211 le = al_find_ex(ni, le_o ? *le_o : NULL, type, name, name_len, vcn); 212 if (!le) 213 return NULL; 214 215 if (le_o) 216 *le_o = le; 217 218 /* Load record that contains this attribute. */ 219 if (ni_load_mi(ni, le, &m)) 220 return NULL; 221 222 /* Look for required attribute. */ 223 attr = mi_find_attr(ni, m, NULL, type, name, name_len, &le->id); 224 225 if (!attr) 226 goto out; 227 228 if (!attr->non_res) { 229 if (vcn && *vcn) 230 goto out; 231 } else if (!vcn) { 232 if (attr->nres.svcn) 233 goto out; 234 } else if (le64_to_cpu(attr->nres.svcn) > *vcn || 235 *vcn > le64_to_cpu(attr->nres.evcn)) { 236 goto out; 237 } 238 239 if (mi) 240 *mi = m; 241 return attr; 242 243 out: 244 _ntfs_bad_inode(&ni->vfs_inode); 245 return NULL; 246 } 247 248 /* 249 * ni_enum_attr_ex - Enumerates attributes in ntfs_inode. 250 */ 251 struct ATTRIB *ni_enum_attr_ex(struct ntfs_inode *ni, struct ATTRIB *attr, 252 struct ATTR_LIST_ENTRY **le, 253 struct mft_inode **mi) 254 { 255 struct mft_inode *mi2; 256 struct ATTR_LIST_ENTRY *le2; 257 258 /* Do we have an attribute list? */ 259 if (!ni->attr_list.size) { 260 *le = NULL; 261 if (mi) 262 *mi = &ni->mi; 263 /* Enum attributes in primary record. */ 264 return mi_enum_attr(ni, &ni->mi, attr); 265 } 266 267 /* Get next list entry. */ 268 le2 = *le = al_enumerate(ni, attr ? *le : NULL); 269 if (!le2) 270 return NULL; 271 272 /* Load record that contains the required attribute. */ 273 if (ni_load_mi(ni, le2, &mi2)) 274 return NULL; 275 276 if (mi) 277 *mi = mi2; 278 279 /* Find attribute in loaded record. */ 280 return rec_find_attr_le(ni, mi2, le2); 281 } 282 283 /* 284 * ni_load_all_mi - Load all subrecords. 285 */ 286 int ni_load_all_mi(struct ntfs_inode *ni) 287 { 288 int err; 289 struct ATTR_LIST_ENTRY *le; 290 291 if (!ni->attr_list.size) 292 return 0; 293 294 le = NULL; 295 296 while ((le = al_enumerate(ni, le))) { 297 CLST rno = ino_get(&le->ref); 298 299 if (rno == ni->mi.rno) 300 continue; 301 302 err = ni_load_mi_ex(ni, rno, NULL); 303 if (err) 304 return err; 305 } 306 307 return 0; 308 } 309 310 /* 311 * ni_add_subrecord - Allocate + format + attach a new subrecord. 312 */ 313 bool ni_add_subrecord(struct ntfs_inode *ni, CLST rno, struct mft_inode **mi) 314 { 315 struct mft_inode *m; 316 317 m = kzalloc(sizeof(struct mft_inode), GFP_NOFS); 318 if (!m) 319 return false; 320 321 if (mi_format_new(m, ni->mi.sbi, rno, 0, ni->mi.rno == MFT_REC_MFT)) { 322 mi_put(m); 323 return false; 324 } 325 326 mi_get_ref(&ni->mi, &m->mrec->parent_ref); 327 328 ni_add_mi(ni, m); 329 *mi = m; 330 return true; 331 } 332 333 /* 334 * ni_remove_attr - Remove all attributes for the given type/name/id. 335 */ 336 int ni_remove_attr(struct ntfs_inode *ni, enum ATTR_TYPE type, 337 const __le16 *name, u8 name_len, bool base_only, 338 const __le16 *id) 339 { 340 int err; 341 struct ATTRIB *attr; 342 struct ATTR_LIST_ENTRY *le; 343 struct mft_inode *mi; 344 u32 type_in; 345 int diff; 346 347 if (base_only || type == ATTR_LIST || !ni->attr_list.size) { 348 attr = mi_find_attr(ni, &ni->mi, NULL, type, name, name_len, 349 id); 350 if (!attr) 351 return -ENOENT; 352 353 mi_remove_attr(ni, &ni->mi, attr); 354 return 0; 355 } 356 357 type_in = le32_to_cpu(type); 358 le = NULL; 359 360 for (;;) { 361 le = al_enumerate(ni, le); 362 if (!le) 363 return 0; 364 365 next_le2: 366 diff = le32_to_cpu(le->type) - type_in; 367 if (diff < 0) 368 continue; 369 370 if (diff > 0) 371 return 0; 372 373 if (le->name_len != name_len) 374 continue; 375 376 if (name_len && 377 memcmp(le_name(le), name, name_len * sizeof(short))) 378 continue; 379 380 if (id && le->id != *id) 381 continue; 382 err = ni_load_mi(ni, le, &mi); 383 if (err) 384 return err; 385 386 al_remove_le(ni, le); 387 388 attr = mi_find_attr(ni, mi, NULL, type, name, name_len, id); 389 if (!attr) 390 return -ENOENT; 391 392 mi_remove_attr(ni, mi, attr); 393 394 if (PtrOffset(ni->attr_list.le, le) >= ni->attr_list.size) 395 return 0; 396 goto next_le2; 397 } 398 } 399 400 /* 401 * ni_ins_new_attr - Insert the attribute into record. 402 * 403 * Return: Not full constructed attribute or NULL if not possible to create. 404 */ 405 static struct ATTRIB * 406 ni_ins_new_attr(struct ntfs_inode *ni, struct mft_inode *mi, 407 struct ATTR_LIST_ENTRY *le, enum ATTR_TYPE type, 408 const __le16 *name, u8 name_len, u32 asize, u16 name_off, 409 CLST svcn, struct ATTR_LIST_ENTRY **ins_le) 410 { 411 int err; 412 struct ATTRIB *attr; 413 bool le_added = false; 414 struct MFT_REF ref; 415 416 mi_get_ref(mi, &ref); 417 418 if (type != ATTR_LIST && !le && ni->attr_list.size) { 419 err = al_add_le(ni, type, name, name_len, svcn, cpu_to_le16(-1), 420 &ref, &le); 421 if (err) { 422 /* No memory or no space. */ 423 return ERR_PTR(err); 424 } 425 le_added = true; 426 427 /* 428 * al_add_le -> attr_set_size (list) -> ni_expand_list 429 * which moves some attributes out of primary record 430 * this means that name may point into moved memory 431 * reinit 'name' from le. 432 */ 433 name = le->name; 434 } 435 436 attr = mi_insert_attr(ni, mi, type, name, name_len, asize, name_off); 437 if (!attr) { 438 if (le_added) 439 al_remove_le(ni, le); 440 return NULL; 441 } 442 443 if (type == ATTR_LIST) { 444 /* Attr list is not in list entry array. */ 445 goto out; 446 } 447 448 if (!le) 449 goto out; 450 451 /* Update ATTRIB Id and record reference. */ 452 le->id = attr->id; 453 ni->attr_list.dirty = true; 454 le->ref = ref; 455 456 out: 457 if (ins_le) 458 *ins_le = le; 459 return attr; 460 } 461 462 /* 463 * ni_repack 464 * 465 * Random write access to sparsed or compressed file may result to 466 * not optimized packed runs. 467 * Here is the place to optimize it. 468 */ 469 static int ni_repack(struct ntfs_inode *ni) 470 { 471 #if 1 472 return 0; 473 #else 474 int err = 0; 475 struct ntfs_sb_info *sbi = ni->mi.sbi; 476 struct mft_inode *mi, *mi_p = NULL; 477 struct ATTRIB *attr = NULL, *attr_p; 478 struct ATTR_LIST_ENTRY *le = NULL, *le_p; 479 CLST alloc = 0; 480 u8 cluster_bits = sbi->cluster_bits; 481 CLST svcn, evcn = 0, svcn_p, evcn_p, next_svcn; 482 u32 roff, rs = sbi->record_size; 483 struct runs_tree run; 484 485 run_init(&run); 486 487 while ((attr = ni_enum_attr_ex(ni, attr, &le, &mi))) { 488 if (!attr->non_res) 489 continue; 490 491 svcn = le64_to_cpu(attr->nres.svcn); 492 if (svcn != le64_to_cpu(le->vcn)) { 493 err = -EINVAL; 494 break; 495 } 496 497 if (!svcn) { 498 alloc = le64_to_cpu(attr->nres.alloc_size) >> 499 cluster_bits; 500 mi_p = NULL; 501 } else if (svcn != evcn + 1) { 502 err = -EINVAL; 503 break; 504 } 505 506 evcn = le64_to_cpu(attr->nres.evcn); 507 508 if (svcn > evcn + 1) { 509 err = -EINVAL; 510 break; 511 } 512 513 if (!mi_p) { 514 /* Do not try if not enough free space. */ 515 if (le32_to_cpu(mi->mrec->used) + 8 >= rs) 516 continue; 517 518 /* Do not try if last attribute segment. */ 519 if (evcn + 1 == alloc) 520 continue; 521 run_close(&run); 522 } 523 524 roff = le16_to_cpu(attr->nres.run_off); 525 526 if (roff > le32_to_cpu(attr->size)) { 527 err = -EINVAL; 528 break; 529 } 530 531 err = run_unpack(&run, sbi, ni->mi.rno, svcn, evcn, svcn, 532 Add2Ptr(attr, roff), 533 le32_to_cpu(attr->size) - roff); 534 if (err < 0) 535 break; 536 537 if (!mi_p) { 538 mi_p = mi; 539 attr_p = attr; 540 svcn_p = svcn; 541 evcn_p = evcn; 542 le_p = le; 543 err = 0; 544 continue; 545 } 546 547 /* 548 * Run contains data from two records: mi_p and mi 549 * Try to pack in one. 550 */ 551 err = mi_pack_runs(mi_p, attr_p, &run, evcn + 1 - svcn_p); 552 if (err) 553 break; 554 555 next_svcn = le64_to_cpu(attr_p->nres.evcn) + 1; 556 557 if (next_svcn >= evcn + 1) { 558 /* We can remove this attribute segment. */ 559 al_remove_le(ni, le); 560 mi_remove_attr(NULL, mi, attr); 561 le = le_p; 562 continue; 563 } 564 565 attr->nres.svcn = le->vcn = cpu_to_le64(next_svcn); 566 mi->dirty = true; 567 ni->attr_list.dirty = true; 568 569 if (evcn + 1 == alloc) { 570 err = mi_pack_runs(mi, attr, &run, 571 evcn + 1 - next_svcn); 572 if (err) 573 break; 574 mi_p = NULL; 575 } else { 576 mi_p = mi; 577 attr_p = attr; 578 svcn_p = next_svcn; 579 evcn_p = evcn; 580 le_p = le; 581 run_truncate_head(&run, next_svcn); 582 } 583 } 584 585 if (err) { 586 ntfs_inode_warn(&ni->vfs_inode, "repack problem"); 587 ntfs_set_state(sbi, NTFS_DIRTY_ERROR); 588 589 /* Pack loaded but not packed runs. */ 590 if (mi_p) 591 mi_pack_runs(mi_p, attr_p, &run, evcn_p + 1 - svcn_p); 592 } 593 594 run_close(&run); 595 return err; 596 #endif 597 } 598 599 /* 600 * ni_try_remove_attr_list 601 * 602 * Can we remove attribute list? 603 * Check the case when primary record contains enough space for all attributes. 604 */ 605 static int ni_try_remove_attr_list(struct ntfs_inode *ni) 606 { 607 int err = 0; 608 struct ntfs_sb_info *sbi = ni->mi.sbi; 609 struct ATTRIB *attr, *attr_list, *attr_ins; 610 struct ATTR_LIST_ENTRY *le; 611 struct mft_inode *mi; 612 u32 asize, free; 613 struct MFT_REF ref; 614 struct MFT_REC *mrec; 615 __le16 id; 616 617 if (!ni->attr_list.dirty) 618 return 0; 619 620 err = ni_repack(ni); 621 if (err) 622 return err; 623 624 attr_list = mi_find_attr(ni, &ni->mi, NULL, ATTR_LIST, NULL, 0, NULL); 625 if (!attr_list) 626 return 0; 627 628 asize = le32_to_cpu(attr_list->size); 629 630 /* Free space in primary record without attribute list. */ 631 free = sbi->record_size - le32_to_cpu(ni->mi.mrec->used) + asize; 632 mi_get_ref(&ni->mi, &ref); 633 634 le = NULL; 635 while ((le = al_enumerate(ni, le))) { 636 if (!memcmp(&le->ref, &ref, sizeof(ref))) 637 continue; 638 639 if (le->vcn) 640 return 0; 641 642 mi = ni_find_mi(ni, ino_get(&le->ref)); 643 if (!mi) 644 return 0; 645 646 attr = mi_find_attr(ni, mi, NULL, le->type, le_name(le), 647 le->name_len, &le->id); 648 if (!attr) 649 return 0; 650 651 asize = le32_to_cpu(attr->size); 652 if (asize > free) 653 return 0; 654 655 free -= asize; 656 } 657 658 /* Make a copy of primary record to restore if error. */ 659 mrec = kmemdup(ni->mi.mrec, sbi->record_size, GFP_NOFS); 660 if (!mrec) 661 return 0; /* Not critical. */ 662 663 /* It seems that attribute list can be removed from primary record. */ 664 mi_remove_attr(NULL, &ni->mi, attr_list); 665 666 /* 667 * Repeat the cycle above and copy all attributes to primary record. 668 * Do not remove original attributes from subrecords! 669 * It should be success! 670 */ 671 le = NULL; 672 while ((le = al_enumerate(ni, le))) { 673 if (!memcmp(&le->ref, &ref, sizeof(ref))) 674 continue; 675 676 mi = ni_find_mi(ni, ino_get(&le->ref)); 677 if (!mi) { 678 /* Should never happened, 'cause already checked. */ 679 goto out; 680 } 681 682 attr = mi_find_attr(ni, mi, NULL, le->type, le_name(le), 683 le->name_len, &le->id); 684 if (!attr) { 685 /* Should never happened, 'cause already checked. */ 686 goto out; 687 } 688 asize = le32_to_cpu(attr->size); 689 690 /* Insert into primary record. */ 691 attr_ins = mi_insert_attr(ni, &ni->mi, le->type, le_name(le), 692 le->name_len, asize, 693 le16_to_cpu(attr->name_off)); 694 if (!attr_ins) { 695 /* 696 * No space in primary record (already checked). 697 */ 698 goto out; 699 } 700 701 /* Copy all except id. */ 702 id = attr_ins->id; 703 memcpy(attr_ins, attr, asize); 704 attr_ins->id = id; 705 } 706 707 /* 708 * Repeat the cycle above and remove all attributes from subrecords. 709 */ 710 le = NULL; 711 while ((le = al_enumerate(ni, le))) { 712 if (!memcmp(&le->ref, &ref, sizeof(ref))) 713 continue; 714 715 mi = ni_find_mi(ni, ino_get(&le->ref)); 716 if (!mi) 717 continue; 718 719 attr = mi_find_attr(ni, mi, NULL, le->type, le_name(le), 720 le->name_len, &le->id); 721 if (!attr) 722 continue; 723 724 /* Remove from original record. */ 725 mi_remove_attr(NULL, mi, attr); 726 } 727 728 run_deallocate(sbi, &ni->attr_list.run, true); 729 run_close(&ni->attr_list.run); 730 ni->attr_list.size = 0; 731 kvfree(ni->attr_list.le); 732 ni->attr_list.le = NULL; 733 ni->attr_list.dirty = false; 734 735 kfree(mrec); 736 return 0; 737 out: 738 /* Restore primary record. */ 739 swap(mrec, ni->mi.mrec); 740 kfree(mrec); 741 return 0; 742 } 743 744 /* 745 * ni_create_attr_list - Generates an attribute list for this primary record. 746 */ 747 int ni_create_attr_list(struct ntfs_inode *ni) 748 { 749 struct ntfs_sb_info *sbi = ni->mi.sbi; 750 int err; 751 u32 lsize; 752 struct ATTRIB *attr; 753 struct ATTRIB *arr_move[7]; 754 struct ATTR_LIST_ENTRY *le, *le_b[7]; 755 struct MFT_REC *rec; 756 bool is_mft; 757 CLST rno = 0; 758 struct mft_inode *mi; 759 u32 free_b, nb, to_free, rs; 760 u16 sz; 761 762 is_mft = ni->mi.rno == MFT_REC_MFT; 763 rec = ni->mi.mrec; 764 rs = sbi->record_size; 765 766 /* 767 * Skip estimating exact memory requirement. 768 * Looks like one record_size is always enough. 769 */ 770 le = kmalloc(al_aligned(rs), GFP_NOFS); 771 if (!le) 772 return -ENOMEM; 773 774 mi_get_ref(&ni->mi, &le->ref); 775 ni->attr_list.le = le; 776 777 attr = NULL; 778 nb = 0; 779 free_b = 0; 780 attr = NULL; 781 782 for (; (attr = mi_enum_attr(ni, &ni->mi, attr)); le = Add2Ptr(le, sz)) { 783 sz = le_size(attr->name_len); 784 le->type = attr->type; 785 le->size = cpu_to_le16(sz); 786 le->name_len = attr->name_len; 787 le->name_off = offsetof(struct ATTR_LIST_ENTRY, name); 788 le->vcn = 0; 789 if (le != ni->attr_list.le) 790 le->ref = ni->attr_list.le->ref; 791 le->id = attr->id; 792 793 if (attr->name_len) 794 memcpy(le->name, attr_name(attr), 795 sizeof(short) * attr->name_len); 796 else if (attr->type == ATTR_STD) 797 continue; 798 else if (attr->type == ATTR_LIST) 799 continue; 800 else if (is_mft && attr->type == ATTR_DATA) 801 continue; 802 803 if (!nb || nb < ARRAY_SIZE(arr_move)) { 804 le_b[nb] = le; 805 arr_move[nb++] = attr; 806 free_b += le32_to_cpu(attr->size); 807 } 808 } 809 810 lsize = PtrOffset(ni->attr_list.le, le); 811 ni->attr_list.size = lsize; 812 813 to_free = le32_to_cpu(rec->used) + lsize + SIZEOF_RESIDENT; 814 if (to_free <= rs) { 815 to_free = 0; 816 } else { 817 to_free -= rs; 818 819 if (to_free > free_b) { 820 err = -EINVAL; 821 goto out; 822 } 823 } 824 825 /* Allocate child MFT. */ 826 err = ntfs_look_free_mft(sbi, &rno, is_mft, ni, &mi); 827 if (err) 828 goto out; 829 830 err = -EINVAL; 831 /* Call mi_remove_attr() in reverse order to keep pointers 'arr_move' valid. */ 832 while (to_free > 0) { 833 struct ATTRIB *b = arr_move[--nb]; 834 u32 asize = le32_to_cpu(b->size); 835 u16 name_off = le16_to_cpu(b->name_off); 836 837 attr = mi_insert_attr(ni, mi, b->type, Add2Ptr(b, name_off), 838 b->name_len, asize, name_off); 839 if (!attr) 840 goto out; 841 842 mi_get_ref(mi, &le_b[nb]->ref); 843 le_b[nb]->id = attr->id; 844 845 /* Copy all except id. */ 846 memcpy(attr, b, asize); 847 attr->id = le_b[nb]->id; 848 849 /* Remove from primary record. */ 850 if (!mi_remove_attr(NULL, &ni->mi, b)) 851 goto out; 852 853 if (to_free <= asize) 854 break; 855 to_free -= asize; 856 if (!nb) 857 goto out; 858 } 859 860 attr = mi_insert_attr(ni, &ni->mi, ATTR_LIST, NULL, 0, 861 lsize + SIZEOF_RESIDENT, SIZEOF_RESIDENT); 862 if (!attr) 863 goto out; 864 865 attr->non_res = 0; 866 attr->flags = 0; 867 attr->res.data_size = cpu_to_le32(lsize); 868 attr->res.data_off = SIZEOF_RESIDENT_LE; 869 attr->res.flags = 0; 870 attr->res.res = 0; 871 872 memcpy(resident_data_ex(attr, lsize), ni->attr_list.le, lsize); 873 874 ni->attr_list.dirty = false; 875 876 mark_inode_dirty(&ni->vfs_inode); 877 return 0; 878 879 out: 880 kvfree(ni->attr_list.le); 881 ni->attr_list.le = NULL; 882 ni->attr_list.size = 0; 883 return err; 884 } 885 886 /* 887 * ni_ins_attr_ext - Add an external attribute to the ntfs_inode. 888 */ 889 static int ni_ins_attr_ext(struct ntfs_inode *ni, struct ATTR_LIST_ENTRY *le, 890 enum ATTR_TYPE type, const __le16 *name, u8 name_len, 891 u32 asize, CLST svcn, u16 name_off, bool force_ext, 892 struct ATTRIB **ins_attr, struct mft_inode **ins_mi, 893 struct ATTR_LIST_ENTRY **ins_le) 894 { 895 struct ATTRIB *attr; 896 struct mft_inode *mi; 897 CLST rno; 898 u64 vbo; 899 struct rb_node *node; 900 int err; 901 bool is_mft, is_mft_data; 902 struct ntfs_sb_info *sbi = ni->mi.sbi; 903 904 is_mft = ni->mi.rno == MFT_REC_MFT; 905 is_mft_data = is_mft && type == ATTR_DATA && !name_len; 906 907 if (asize > sbi->max_bytes_per_attr) { 908 err = -EINVAL; 909 goto out; 910 } 911 912 /* 913 * Standard information and attr_list cannot be made external. 914 * The Log File cannot have any external attributes. 915 */ 916 if (type == ATTR_STD || type == ATTR_LIST || 917 ni->mi.rno == MFT_REC_LOG) { 918 err = -EINVAL; 919 goto out; 920 } 921 922 /* Create attribute list if it is not already existed. */ 923 if (!ni->attr_list.size) { 924 err = ni_create_attr_list(ni); 925 if (err) 926 goto out; 927 } 928 929 vbo = is_mft_data ? ((u64)svcn << sbi->cluster_bits) : 0; 930 931 if (force_ext) 932 goto insert_ext; 933 934 /* Load all subrecords into memory. */ 935 err = ni_load_all_mi(ni); 936 if (err) 937 goto out; 938 939 /* Check each of loaded subrecord. */ 940 for (node = rb_first(&ni->mi_tree); node; node = rb_next(node)) { 941 mi = rb_entry(node, struct mft_inode, node); 942 943 if (is_mft_data && 944 (mi_enum_attr(ni, mi, NULL) || 945 vbo <= ((u64)mi->rno << sbi->record_bits))) { 946 /* We can't accept this record 'cause MFT's bootstrapping. */ 947 continue; 948 } 949 if (is_mft && 950 mi_find_attr(ni, mi, NULL, ATTR_DATA, NULL, 0, NULL)) { 951 /* 952 * This child record already has a ATTR_DATA. 953 * So it can't accept any other records. 954 */ 955 continue; 956 } 957 958 if ((type != ATTR_NAME || name_len) && 959 mi_find_attr(ni, mi, NULL, type, name, name_len, NULL)) { 960 /* Only indexed attributes can share same record. */ 961 continue; 962 } 963 964 /* 965 * Do not try to insert this attribute 966 * if there is no room in record. 967 */ 968 if (le32_to_cpu(mi->mrec->used) + asize > sbi->record_size) 969 continue; 970 971 /* Try to insert attribute into this subrecord. */ 972 attr = ni_ins_new_attr(ni, mi, le, type, name, name_len, asize, 973 name_off, svcn, ins_le); 974 if (!attr) 975 continue; 976 if (IS_ERR(attr)) 977 return PTR_ERR(attr); 978 979 if (ins_attr) 980 *ins_attr = attr; 981 if (ins_mi) 982 *ins_mi = mi; 983 return 0; 984 } 985 986 insert_ext: 987 /* We have to allocate a new child subrecord. */ 988 err = ntfs_look_free_mft(sbi, &rno, is_mft_data, ni, &mi); 989 if (err) 990 goto out; 991 992 if (is_mft_data && vbo <= ((u64)rno << sbi->record_bits)) { 993 err = -EINVAL; 994 goto out1; 995 } 996 997 attr = ni_ins_new_attr(ni, mi, le, type, name, name_len, asize, 998 name_off, svcn, ins_le); 999 if (!attr) { 1000 err = -EINVAL; 1001 goto out2; 1002 } 1003 1004 if (IS_ERR(attr)) { 1005 err = PTR_ERR(attr); 1006 goto out2; 1007 } 1008 1009 if (ins_attr) 1010 *ins_attr = attr; 1011 if (ins_mi) 1012 *ins_mi = mi; 1013 1014 return 0; 1015 1016 out2: 1017 ni_remove_mi(ni, mi); 1018 mi_put(mi); 1019 1020 out1: 1021 ntfs_mark_rec_free(sbi, rno, is_mft); 1022 1023 out: 1024 return err; 1025 } 1026 1027 /* 1028 * ni_insert_attr - Insert an attribute into the file. 1029 * 1030 * If the primary record has room, it will just insert the attribute. 1031 * If not, it may make the attribute external. 1032 * For $MFT::Data it may make room for the attribute by 1033 * making other attributes external. 1034 * 1035 * NOTE: 1036 * The ATTR_LIST and ATTR_STD cannot be made external. 1037 * This function does not fill new attribute full. 1038 * It only fills 'size'/'type'/'id'/'name_len' fields. 1039 */ 1040 static int ni_insert_attr(struct ntfs_inode *ni, enum ATTR_TYPE type, 1041 const __le16 *name, u8 name_len, u32 asize, 1042 u16 name_off, CLST svcn, struct ATTRIB **ins_attr, 1043 struct mft_inode **ins_mi, 1044 struct ATTR_LIST_ENTRY **ins_le) 1045 { 1046 struct ntfs_sb_info *sbi = ni->mi.sbi; 1047 int err; 1048 struct ATTRIB *attr, *eattr; 1049 struct MFT_REC *rec; 1050 bool is_mft; 1051 struct ATTR_LIST_ENTRY *le; 1052 u32 list_reserve, max_free, free, used, t32; 1053 __le16 id; 1054 u16 t16; 1055 1056 is_mft = ni->mi.rno == MFT_REC_MFT; 1057 rec = ni->mi.mrec; 1058 1059 list_reserve = SIZEOF_NONRESIDENT + 3 * (1 + 2 * sizeof(u32)); 1060 used = le32_to_cpu(rec->used); 1061 free = sbi->record_size - used; 1062 1063 if (is_mft && type != ATTR_LIST) { 1064 /* Reserve space for the ATTRIB list. */ 1065 if (free < list_reserve) 1066 free = 0; 1067 else 1068 free -= list_reserve; 1069 } 1070 1071 if (asize <= free) { 1072 attr = ni_ins_new_attr(ni, &ni->mi, NULL, type, name, name_len, 1073 asize, name_off, svcn, ins_le); 1074 if (IS_ERR(attr)) { 1075 err = PTR_ERR(attr); 1076 goto out; 1077 } 1078 1079 if (attr) { 1080 if (ins_attr) 1081 *ins_attr = attr; 1082 if (ins_mi) 1083 *ins_mi = &ni->mi; 1084 err = 0; 1085 goto out; 1086 } 1087 } 1088 1089 if (!is_mft || type != ATTR_DATA || svcn) { 1090 /* This ATTRIB will be external. */ 1091 err = ni_ins_attr_ext(ni, NULL, type, name, name_len, asize, 1092 svcn, name_off, false, ins_attr, ins_mi, 1093 ins_le); 1094 goto out; 1095 } 1096 1097 /* 1098 * Here we have: "is_mft && type == ATTR_DATA && !svcn" 1099 * 1100 * The first chunk of the $MFT::Data ATTRIB must be the base record. 1101 * Evict as many other attributes as possible. 1102 */ 1103 max_free = free; 1104 1105 /* Estimate the result of moving all possible attributes away. */ 1106 attr = NULL; 1107 1108 while ((attr = mi_enum_attr(ni, &ni->mi, attr))) { 1109 if (attr->type == ATTR_STD) 1110 continue; 1111 if (attr->type == ATTR_LIST) 1112 continue; 1113 max_free += le32_to_cpu(attr->size); 1114 } 1115 1116 if (max_free < asize + list_reserve) { 1117 /* Impossible to insert this attribute into primary record. */ 1118 err = -EINVAL; 1119 goto out; 1120 } 1121 1122 /* Start real attribute moving. */ 1123 attr = NULL; 1124 1125 for (;;) { 1126 attr = mi_enum_attr(ni, &ni->mi, attr); 1127 if (!attr) { 1128 /* We should never be here 'cause we have already check this case. */ 1129 err = -EINVAL; 1130 goto out; 1131 } 1132 1133 /* Skip attributes that MUST be primary record. */ 1134 if (attr->type == ATTR_STD || attr->type == ATTR_LIST) 1135 continue; 1136 1137 le = NULL; 1138 if (ni->attr_list.size) { 1139 le = al_find_le(ni, NULL, attr); 1140 if (!le) { 1141 /* Really this is a serious bug. */ 1142 err = -EINVAL; 1143 goto out; 1144 } 1145 } 1146 1147 t32 = le32_to_cpu(attr->size); 1148 t16 = le16_to_cpu(attr->name_off); 1149 err = ni_ins_attr_ext(ni, le, attr->type, Add2Ptr(attr, t16), 1150 attr->name_len, t32, attr_svcn(attr), t16, 1151 false, &eattr, NULL, NULL); 1152 if (err) 1153 return err; 1154 1155 id = eattr->id; 1156 memcpy(eattr, attr, t32); 1157 eattr->id = id; 1158 1159 /* Remove from primary record. */ 1160 mi_remove_attr(NULL, &ni->mi, attr); 1161 1162 /* attr now points to next attribute. */ 1163 if (attr->type == ATTR_END) 1164 goto out; 1165 } 1166 while (asize + list_reserve > sbi->record_size - le32_to_cpu(rec->used)) 1167 ; 1168 1169 attr = ni_ins_new_attr(ni, &ni->mi, NULL, type, name, name_len, asize, 1170 name_off, svcn, ins_le); 1171 if (!attr) { 1172 err = -EINVAL; 1173 goto out; 1174 } 1175 1176 if (IS_ERR(attr)) { 1177 err = PTR_ERR(attr); 1178 goto out; 1179 } 1180 1181 if (ins_attr) 1182 *ins_attr = attr; 1183 if (ins_mi) 1184 *ins_mi = &ni->mi; 1185 1186 out: 1187 return err; 1188 } 1189 1190 /* ni_expand_mft_list - Split ATTR_DATA of $MFT. */ 1191 static int ni_expand_mft_list(struct ntfs_inode *ni) 1192 { 1193 int err = 0; 1194 struct runs_tree *run = &ni->file.run; 1195 u32 asize, run_size, done = 0; 1196 struct ATTRIB *attr; 1197 struct rb_node *node; 1198 CLST mft_min, mft_new, svcn, evcn, plen; 1199 struct mft_inode *mi, *mi_min, *mi_new; 1200 struct ntfs_sb_info *sbi = ni->mi.sbi; 1201 1202 /* Find the nearest MFT. */ 1203 mft_min = 0; 1204 mft_new = 0; 1205 mi_min = NULL; 1206 1207 for (node = rb_first(&ni->mi_tree); node; node = rb_next(node)) { 1208 mi = rb_entry(node, struct mft_inode, node); 1209 1210 attr = mi_enum_attr(ni, mi, NULL); 1211 1212 if (!attr) { 1213 mft_min = mi->rno; 1214 mi_min = mi; 1215 break; 1216 } 1217 } 1218 1219 if (ntfs_look_free_mft(sbi, &mft_new, true, ni, &mi_new)) { 1220 mft_new = 0; 1221 /* Really this is not critical. */ 1222 } else if (mft_min > mft_new) { 1223 mft_min = mft_new; 1224 mi_min = mi_new; 1225 } else { 1226 ntfs_mark_rec_free(sbi, mft_new, true); 1227 mft_new = 0; 1228 ni_remove_mi(ni, mi_new); 1229 } 1230 1231 attr = mi_find_attr(ni, &ni->mi, NULL, ATTR_DATA, NULL, 0, NULL); 1232 if (!attr) { 1233 err = -EINVAL; 1234 goto out; 1235 } 1236 1237 asize = le32_to_cpu(attr->size); 1238 1239 evcn = le64_to_cpu(attr->nres.evcn); 1240 svcn = bytes_to_cluster(sbi, (u64)(mft_min + 1) << sbi->record_bits); 1241 if (evcn + 1 >= svcn) { 1242 err = -EINVAL; 1243 goto out; 1244 } 1245 1246 /* 1247 * Split primary attribute [0 evcn] in two parts [0 svcn) + [svcn evcn]. 1248 * 1249 * Update first part of ATTR_DATA in 'primary MFT. 1250 */ 1251 err = run_pack(run, 0, svcn, Add2Ptr(attr, SIZEOF_NONRESIDENT), 1252 asize - SIZEOF_NONRESIDENT, &plen); 1253 if (err < 0) 1254 goto out; 1255 1256 run_size = ALIGN(err, 8); 1257 err = 0; 1258 1259 if (plen < svcn) { 1260 err = -EINVAL; 1261 goto out; 1262 } 1263 1264 attr->nres.evcn = cpu_to_le64(svcn - 1); 1265 attr->size = cpu_to_le32(run_size + SIZEOF_NONRESIDENT); 1266 /* 'done' - How many bytes of primary MFT becomes free. */ 1267 done = asize - run_size - SIZEOF_NONRESIDENT; 1268 le32_sub_cpu(&ni->mi.mrec->used, done); 1269 1270 /* Estimate packed size (run_buf=NULL). */ 1271 err = run_pack(run, svcn, evcn + 1 - svcn, NULL, sbi->record_size, 1272 &plen); 1273 if (err < 0) 1274 goto out; 1275 1276 run_size = ALIGN(err, 8); 1277 err = 0; 1278 1279 if (plen < evcn + 1 - svcn) { 1280 err = -EINVAL; 1281 goto out; 1282 } 1283 1284 /* 1285 * This function may implicitly call expand attr_list. 1286 * Insert second part of ATTR_DATA in 'mi_min'. 1287 */ 1288 attr = ni_ins_new_attr(ni, mi_min, NULL, ATTR_DATA, NULL, 0, 1289 SIZEOF_NONRESIDENT + run_size, 1290 SIZEOF_NONRESIDENT, svcn, NULL); 1291 if (!attr) { 1292 err = -EINVAL; 1293 goto out; 1294 } 1295 1296 if (IS_ERR(attr)) { 1297 err = PTR_ERR(attr); 1298 goto out; 1299 } 1300 1301 attr->non_res = 1; 1302 attr->name_off = SIZEOF_NONRESIDENT_LE; 1303 attr->flags = 0; 1304 1305 /* This function can't fail - cause already checked above. */ 1306 run_pack(run, svcn, evcn + 1 - svcn, Add2Ptr(attr, SIZEOF_NONRESIDENT), 1307 run_size, &plen); 1308 1309 attr->nres.svcn = cpu_to_le64(svcn); 1310 attr->nres.evcn = cpu_to_le64(evcn); 1311 attr->nres.run_off = cpu_to_le16(SIZEOF_NONRESIDENT); 1312 1313 out: 1314 if (mft_new) { 1315 ntfs_mark_rec_free(sbi, mft_new, true); 1316 ni_remove_mi(ni, mi_new); 1317 } 1318 1319 return !err && !done ? -EOPNOTSUPP : err; 1320 } 1321 1322 /* 1323 * ni_expand_list - Move all possible attributes out of primary record. 1324 */ 1325 int ni_expand_list(struct ntfs_inode *ni) 1326 { 1327 int err = 0; 1328 u32 asize, done = 0; 1329 struct ATTRIB *attr, *ins_attr; 1330 struct ATTR_LIST_ENTRY *le; 1331 bool is_mft = ni->mi.rno == MFT_REC_MFT; 1332 struct MFT_REF ref; 1333 1334 mi_get_ref(&ni->mi, &ref); 1335 le = NULL; 1336 1337 while ((le = al_enumerate(ni, le))) { 1338 if (le->type == ATTR_STD) 1339 continue; 1340 1341 if (memcmp(&ref, &le->ref, sizeof(struct MFT_REF))) 1342 continue; 1343 1344 if (is_mft && le->type == ATTR_DATA) 1345 continue; 1346 1347 /* Find attribute in primary record. */ 1348 attr = rec_find_attr_le(ni, &ni->mi, le); 1349 if (!attr) { 1350 err = -EINVAL; 1351 goto out; 1352 } 1353 1354 asize = le32_to_cpu(attr->size); 1355 1356 /* Always insert into new record to avoid collisions (deep recursive). */ 1357 err = ni_ins_attr_ext(ni, le, attr->type, attr_name(attr), 1358 attr->name_len, asize, attr_svcn(attr), 1359 le16_to_cpu(attr->name_off), true, 1360 &ins_attr, NULL, NULL); 1361 1362 if (err) 1363 goto out; 1364 1365 memcpy(ins_attr, attr, asize); 1366 ins_attr->id = le->id; 1367 /* Remove from primary record. */ 1368 mi_remove_attr(NULL, &ni->mi, attr); 1369 1370 done += asize; 1371 goto out; 1372 } 1373 1374 if (!is_mft) { 1375 err = -EFBIG; /* Attr list is too big(?) */ 1376 goto out; 1377 } 1378 1379 /* Split MFT data as much as possible. */ 1380 err = ni_expand_mft_list(ni); 1381 1382 out: 1383 return !err && !done ? -EOPNOTSUPP : err; 1384 } 1385 1386 /* 1387 * ni_insert_nonresident - Insert new nonresident attribute. 1388 */ 1389 int ni_insert_nonresident(struct ntfs_inode *ni, enum ATTR_TYPE type, 1390 const __le16 *name, u8 name_len, 1391 const struct runs_tree *run, CLST svcn, CLST len, 1392 __le16 flags, struct ATTRIB **new_attr, 1393 struct mft_inode **mi, struct ATTR_LIST_ENTRY **le) 1394 { 1395 int err; 1396 CLST plen; 1397 struct ATTRIB *attr; 1398 bool is_ext = (flags & (ATTR_FLAG_SPARSED | ATTR_FLAG_COMPRESSED)) && 1399 !svcn; 1400 u32 name_size = ALIGN(name_len * sizeof(short), 8); 1401 u32 name_off = is_ext ? SIZEOF_NONRESIDENT_EX : SIZEOF_NONRESIDENT; 1402 u32 run_off = name_off + name_size; 1403 u32 run_size, asize; 1404 struct ntfs_sb_info *sbi = ni->mi.sbi; 1405 1406 /* Estimate packed size (run_buf=NULL). */ 1407 err = run_pack(run, svcn, len, NULL, sbi->max_bytes_per_attr - run_off, 1408 &plen); 1409 if (err < 0) 1410 goto out; 1411 1412 run_size = ALIGN(err, 8); 1413 1414 if (plen < len) { 1415 err = -EINVAL; 1416 goto out; 1417 } 1418 1419 asize = run_off + run_size; 1420 1421 if (asize > sbi->max_bytes_per_attr) { 1422 err = -EINVAL; 1423 goto out; 1424 } 1425 1426 err = ni_insert_attr(ni, type, name, name_len, asize, name_off, svcn, 1427 &attr, mi, le); 1428 1429 if (err) 1430 goto out; 1431 1432 attr->non_res = 1; 1433 attr->name_off = cpu_to_le16(name_off); 1434 attr->flags = flags; 1435 1436 /* This function can't fail - cause already checked above. */ 1437 run_pack(run, svcn, len, Add2Ptr(attr, run_off), run_size, &plen); 1438 1439 attr->nres.svcn = cpu_to_le64(svcn); 1440 attr->nres.evcn = cpu_to_le64((u64)svcn + len - 1); 1441 1442 if (new_attr) 1443 *new_attr = attr; 1444 1445 *(__le64 *)&attr->nres.run_off = cpu_to_le64(run_off); 1446 1447 attr->nres.alloc_size = 1448 svcn ? 0 : cpu_to_le64((u64)len << ni->mi.sbi->cluster_bits); 1449 attr->nres.data_size = attr->nres.alloc_size; 1450 attr->nres.valid_size = attr->nres.alloc_size; 1451 1452 if (is_ext) { 1453 if (flags & ATTR_FLAG_COMPRESSED) 1454 attr->nres.c_unit = NTFS_LZNT_CUNIT; 1455 attr->nres.total_size = attr->nres.alloc_size; 1456 } 1457 1458 out: 1459 return err; 1460 } 1461 1462 /* 1463 * ni_insert_resident - Inserts new resident attribute. 1464 */ 1465 int ni_insert_resident(struct ntfs_inode *ni, u32 data_size, 1466 enum ATTR_TYPE type, const __le16 *name, u8 name_len, 1467 struct ATTRIB **new_attr, struct mft_inode **mi, 1468 struct ATTR_LIST_ENTRY **le) 1469 { 1470 int err; 1471 u32 name_size = ALIGN(name_len * sizeof(short), 8); 1472 u32 asize = SIZEOF_RESIDENT + name_size + ALIGN(data_size, 8); 1473 struct ATTRIB *attr; 1474 1475 err = ni_insert_attr(ni, type, name, name_len, asize, SIZEOF_RESIDENT, 1476 0, &attr, mi, le); 1477 if (err) 1478 return err; 1479 1480 attr->non_res = 0; 1481 attr->flags = 0; 1482 1483 attr->res.data_size = cpu_to_le32(data_size); 1484 attr->res.data_off = cpu_to_le16(SIZEOF_RESIDENT + name_size); 1485 if (type == ATTR_NAME) { 1486 attr->res.flags = RESIDENT_FLAG_INDEXED; 1487 1488 /* is_attr_indexed(attr)) == true */ 1489 le16_add_cpu(&ni->mi.mrec->hard_links, 1); 1490 ni->mi.dirty = true; 1491 } 1492 attr->res.res = 0; 1493 1494 if (new_attr) 1495 *new_attr = attr; 1496 1497 return 0; 1498 } 1499 1500 /* 1501 * ni_remove_attr_le - Remove attribute from record. 1502 */ 1503 void ni_remove_attr_le(struct ntfs_inode *ni, struct ATTRIB *attr, 1504 struct mft_inode *mi, struct ATTR_LIST_ENTRY *le) 1505 { 1506 mi_remove_attr(ni, mi, attr); 1507 1508 if (le) 1509 al_remove_le(ni, le); 1510 } 1511 1512 /* 1513 * ni_delete_all - Remove all attributes and frees allocates space. 1514 * 1515 * ntfs_evict_inode->ntfs_clear_inode->ni_delete_all (if no links). 1516 */ 1517 int ni_delete_all(struct ntfs_inode *ni) 1518 { 1519 int err; 1520 struct ATTR_LIST_ENTRY *le = NULL; 1521 struct ATTRIB *attr = NULL; 1522 struct rb_node *node; 1523 u16 roff; 1524 u32 asize; 1525 CLST svcn, evcn; 1526 struct ntfs_sb_info *sbi = ni->mi.sbi; 1527 bool nt3 = is_ntfs3(sbi); 1528 struct MFT_REF ref; 1529 1530 while ((attr = ni_enum_attr_ex(ni, attr, &le, NULL))) { 1531 if (!nt3 || attr->name_len) { 1532 ; 1533 } else if (attr->type == ATTR_REPARSE) { 1534 mi_get_ref(&ni->mi, &ref); 1535 ntfs_remove_reparse(sbi, 0, &ref); 1536 } else if (attr->type == ATTR_ID && !attr->non_res && 1537 le32_to_cpu(attr->res.data_size) >= 1538 sizeof(struct GUID)) { 1539 ntfs_objid_remove(sbi, resident_data(attr)); 1540 } 1541 1542 if (!attr->non_res) 1543 continue; 1544 1545 svcn = le64_to_cpu(attr->nres.svcn); 1546 evcn = le64_to_cpu(attr->nres.evcn); 1547 1548 if (evcn + 1 <= svcn) 1549 continue; 1550 1551 asize = le32_to_cpu(attr->size); 1552 roff = le16_to_cpu(attr->nres.run_off); 1553 1554 if (roff > asize) { 1555 /* ni_enum_attr_ex checks this case. */ 1556 continue; 1557 } 1558 1559 /* run==1 means unpack and deallocate. */ 1560 run_unpack_ex(RUN_DEALLOCATE, sbi, ni->mi.rno, svcn, evcn, svcn, 1561 Add2Ptr(attr, roff), asize - roff); 1562 } 1563 1564 if (ni->attr_list.size) { 1565 run_deallocate(ni->mi.sbi, &ni->attr_list.run, true); 1566 al_destroy(ni); 1567 } 1568 1569 /* Free all subrecords. */ 1570 for (node = rb_first(&ni->mi_tree); node;) { 1571 struct rb_node *next = rb_next(node); 1572 struct mft_inode *mi = rb_entry(node, struct mft_inode, node); 1573 1574 clear_rec_inuse(mi->mrec); 1575 mi->dirty = true; 1576 mi_write(mi, 0); 1577 1578 ntfs_mark_rec_free(sbi, mi->rno, false); 1579 ni_remove_mi(ni, mi); 1580 mi_put(mi); 1581 node = next; 1582 } 1583 1584 /* Free base record. */ 1585 clear_rec_inuse(ni->mi.mrec); 1586 ni->mi.dirty = true; 1587 err = mi_write(&ni->mi, 0); 1588 1589 ntfs_mark_rec_free(sbi, ni->mi.rno, false); 1590 1591 return err; 1592 } 1593 1594 /* ni_fname_name 1595 * 1596 * Return: File name attribute by its value. 1597 */ 1598 struct ATTR_FILE_NAME *ni_fname_name(struct ntfs_inode *ni, 1599 const struct le_str *uni, 1600 const struct MFT_REF *home_dir, 1601 struct mft_inode **mi, 1602 struct ATTR_LIST_ENTRY **le) 1603 { 1604 struct ATTRIB *attr = NULL; 1605 struct ATTR_FILE_NAME *fname; 1606 1607 if (le) 1608 *le = NULL; 1609 1610 /* Enumerate all names. */ 1611 next: 1612 attr = ni_find_attr(ni, attr, le, ATTR_NAME, NULL, 0, NULL, mi); 1613 if (!attr) 1614 return NULL; 1615 1616 fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME); 1617 if (!fname) 1618 goto next; 1619 1620 if (home_dir && memcmp(home_dir, &fname->home, sizeof(*home_dir))) 1621 goto next; 1622 1623 if (!uni) 1624 return fname; 1625 1626 if (uni->len != fname->name_len) 1627 goto next; 1628 1629 if (ntfs_cmp_names(uni->name, uni->len, fname->name, uni->len, NULL, 1630 false)) 1631 goto next; 1632 return fname; 1633 } 1634 1635 /* 1636 * ni_fname_type 1637 * 1638 * Return: File name attribute with given type. 1639 */ 1640 struct ATTR_FILE_NAME *ni_fname_type(struct ntfs_inode *ni, u8 name_type, 1641 struct mft_inode **mi, 1642 struct ATTR_LIST_ENTRY **le) 1643 { 1644 struct ATTRIB *attr = NULL; 1645 struct ATTR_FILE_NAME *fname; 1646 1647 *le = NULL; 1648 1649 if (name_type == FILE_NAME_POSIX) 1650 return NULL; 1651 1652 /* Enumerate all names. */ 1653 for (;;) { 1654 attr = ni_find_attr(ni, attr, le, ATTR_NAME, NULL, 0, NULL, mi); 1655 if (!attr) 1656 return NULL; 1657 1658 fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME); 1659 if (fname && name_type == fname->type) 1660 return fname; 1661 } 1662 } 1663 1664 /* 1665 * ni_new_attr_flags 1666 * 1667 * Process compressed/sparsed in special way. 1668 * NOTE: You need to set ni->std_fa = new_fa 1669 * after this function to keep internal structures in consistency. 1670 */ 1671 int ni_new_attr_flags(struct ntfs_inode *ni, enum FILE_ATTRIBUTE new_fa) 1672 { 1673 struct ATTRIB *attr; 1674 struct mft_inode *mi; 1675 __le16 new_aflags; 1676 u32 new_asize; 1677 1678 attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, &mi); 1679 if (!attr) 1680 return -EINVAL; 1681 1682 new_aflags = attr->flags; 1683 1684 if (new_fa & FILE_ATTRIBUTE_SPARSE_FILE) 1685 new_aflags |= ATTR_FLAG_SPARSED; 1686 else 1687 new_aflags &= ~ATTR_FLAG_SPARSED; 1688 1689 if (new_fa & FILE_ATTRIBUTE_COMPRESSED) 1690 new_aflags |= ATTR_FLAG_COMPRESSED; 1691 else 1692 new_aflags &= ~ATTR_FLAG_COMPRESSED; 1693 1694 if (new_aflags == attr->flags) 1695 return 0; 1696 1697 if ((new_aflags & (ATTR_FLAG_COMPRESSED | ATTR_FLAG_SPARSED)) == 1698 (ATTR_FLAG_COMPRESSED | ATTR_FLAG_SPARSED)) { 1699 ntfs_inode_warn(&ni->vfs_inode, 1700 "file can't be sparsed and compressed"); 1701 return -EOPNOTSUPP; 1702 } 1703 1704 if (!attr->non_res) 1705 goto out; 1706 1707 if (attr->nres.data_size) { 1708 ntfs_inode_warn( 1709 &ni->vfs_inode, 1710 "one can change sparsed/compressed only for empty files"); 1711 return -EOPNOTSUPP; 1712 } 1713 1714 /* Resize nonresident empty attribute in-place only. */ 1715 new_asize = (new_aflags & (ATTR_FLAG_COMPRESSED | ATTR_FLAG_SPARSED)) ? 1716 (SIZEOF_NONRESIDENT_EX + 8) : 1717 (SIZEOF_NONRESIDENT + 8); 1718 1719 if (!mi_resize_attr(mi, attr, new_asize - le32_to_cpu(attr->size))) 1720 return -EOPNOTSUPP; 1721 1722 if (new_aflags & ATTR_FLAG_SPARSED) { 1723 attr->name_off = SIZEOF_NONRESIDENT_EX_LE; 1724 /* Windows uses 16 clusters per frame but supports one cluster per frame too. */ 1725 attr->nres.c_unit = 0; 1726 ni->vfs_inode.i_mapping->a_ops = &ntfs_aops; 1727 } else if (new_aflags & ATTR_FLAG_COMPRESSED) { 1728 attr->name_off = SIZEOF_NONRESIDENT_EX_LE; 1729 /* The only allowed: 16 clusters per frame. */ 1730 attr->nres.c_unit = NTFS_LZNT_CUNIT; 1731 ni->vfs_inode.i_mapping->a_ops = &ntfs_aops_cmpr; 1732 } else { 1733 attr->name_off = SIZEOF_NONRESIDENT_LE; 1734 /* Normal files. */ 1735 attr->nres.c_unit = 0; 1736 ni->vfs_inode.i_mapping->a_ops = &ntfs_aops; 1737 } 1738 attr->nres.run_off = attr->name_off; 1739 out: 1740 attr->flags = new_aflags; 1741 mi->dirty = true; 1742 1743 return 0; 1744 } 1745 1746 /* 1747 * ni_parse_reparse 1748 * 1749 * buffer - memory for reparse buffer header 1750 */ 1751 enum REPARSE_SIGN ni_parse_reparse(struct ntfs_inode *ni, struct ATTRIB *attr, 1752 struct REPARSE_DATA_BUFFER *buffer) 1753 { 1754 const struct REPARSE_DATA_BUFFER *rp = NULL; 1755 u8 bits; 1756 u16 len; 1757 typeof(rp->CompressReparseBuffer) *cmpr; 1758 1759 /* Try to estimate reparse point. */ 1760 if (!attr->non_res) { 1761 rp = resident_data_ex(attr, sizeof(struct REPARSE_DATA_BUFFER)); 1762 } else if (le64_to_cpu(attr->nres.data_size) >= 1763 sizeof(struct REPARSE_DATA_BUFFER)) { 1764 struct runs_tree run; 1765 1766 run_init(&run); 1767 1768 if (!attr_load_runs_vcn(ni, ATTR_REPARSE, NULL, 0, &run, 0) && 1769 !ntfs_read_run_nb(ni->mi.sbi, &run, 0, buffer, 1770 sizeof(struct REPARSE_DATA_BUFFER), 1771 NULL)) { 1772 rp = buffer; 1773 } 1774 1775 run_close(&run); 1776 } 1777 1778 if (!rp) 1779 return REPARSE_NONE; 1780 1781 len = le16_to_cpu(rp->ReparseDataLength); 1782 switch (rp->ReparseTag) { 1783 case (IO_REPARSE_TAG_MICROSOFT | IO_REPARSE_TAG_SYMBOLIC_LINK): 1784 break; /* Symbolic link. */ 1785 case IO_REPARSE_TAG_MOUNT_POINT: 1786 break; /* Mount points and junctions. */ 1787 case IO_REPARSE_TAG_SYMLINK: 1788 break; 1789 case IO_REPARSE_TAG_COMPRESS: 1790 /* 1791 * WOF - Windows Overlay Filter - Used to compress files with 1792 * LZX/Xpress. 1793 * 1794 * Unlike native NTFS file compression, the Windows 1795 * Overlay Filter supports only read operations. This means 1796 * that it doesn't need to sector-align each compressed chunk, 1797 * so the compressed data can be packed more tightly together. 1798 * If you open the file for writing, the WOF just decompresses 1799 * the entire file, turning it back into a plain file. 1800 * 1801 * Ntfs3 driver decompresses the entire file only on write or 1802 * change size requests. 1803 */ 1804 1805 cmpr = &rp->CompressReparseBuffer; 1806 if (len < sizeof(*cmpr) || 1807 cmpr->WofVersion != WOF_CURRENT_VERSION || 1808 cmpr->WofProvider != WOF_PROVIDER_SYSTEM || 1809 cmpr->ProviderVer != WOF_PROVIDER_CURRENT_VERSION) { 1810 return REPARSE_NONE; 1811 } 1812 1813 switch (cmpr->CompressionFormat) { 1814 case WOF_COMPRESSION_XPRESS4K: 1815 bits = 0xc; // 4k 1816 break; 1817 case WOF_COMPRESSION_XPRESS8K: 1818 bits = 0xd; // 8k 1819 break; 1820 case WOF_COMPRESSION_XPRESS16K: 1821 bits = 0xe; // 16k 1822 break; 1823 case WOF_COMPRESSION_LZX32K: 1824 bits = 0xf; // 32k 1825 break; 1826 default: 1827 bits = 0x10; // 64k 1828 break; 1829 } 1830 ni_set_ext_compress_bits(ni, bits); 1831 return REPARSE_COMPRESSED; 1832 1833 case IO_REPARSE_TAG_DEDUP: 1834 ni->ni_flags |= NI_FLAG_DEDUPLICATED; 1835 return REPARSE_DEDUPLICATED; 1836 1837 default: 1838 if (rp->ReparseTag & IO_REPARSE_TAG_NAME_SURROGATE) 1839 break; 1840 1841 return REPARSE_NONE; 1842 } 1843 1844 if (buffer != rp) 1845 memcpy(buffer, rp, sizeof(struct REPARSE_DATA_BUFFER)); 1846 1847 /* Looks like normal symlink. */ 1848 return REPARSE_LINK; 1849 } 1850 1851 /* 1852 * ni_fiemap - Helper for file_fiemap(). 1853 * 1854 * Assumed ni_lock. 1855 * TODO: Less aggressive locks. 1856 */ 1857 int ni_fiemap(struct ntfs_inode *ni, struct fiemap_extent_info *fieinfo, 1858 __u64 vbo, __u64 len) 1859 { 1860 int err = 0; 1861 struct ntfs_sb_info *sbi = ni->mi.sbi; 1862 u8 cluster_bits = sbi->cluster_bits; 1863 struct runs_tree run; 1864 struct ATTRIB *attr; 1865 CLST vcn = vbo >> cluster_bits; 1866 CLST lcn, clen; 1867 u64 valid = ni->i_valid; 1868 u64 lbo, bytes; 1869 u64 end, alloc_size; 1870 size_t idx = -1; 1871 u32 flags; 1872 bool ok; 1873 1874 run_init(&run); 1875 if (S_ISDIR(ni->vfs_inode.i_mode)) { 1876 attr = ni_find_attr(ni, NULL, NULL, ATTR_ALLOC, I30_NAME, 1877 ARRAY_SIZE(I30_NAME), NULL, NULL); 1878 } else { 1879 attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, 1880 NULL); 1881 if (!attr) { 1882 err = -EINVAL; 1883 goto out; 1884 } 1885 if (is_attr_compressed(attr)) { 1886 /* Unfortunately cp -r incorrectly treats compressed clusters. */ 1887 err = -EOPNOTSUPP; 1888 ntfs_inode_warn( 1889 &ni->vfs_inode, 1890 "fiemap is not supported for compressed file (cp -r)"); 1891 goto out; 1892 } 1893 } 1894 1895 if (!attr || !attr->non_res) { 1896 err = fiemap_fill_next_extent( 1897 fieinfo, 0, 0, 1898 attr ? le32_to_cpu(attr->res.data_size) : 0, 1899 FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_LAST | 1900 FIEMAP_EXTENT_MERGED); 1901 goto out; 1902 } 1903 1904 end = vbo + len; 1905 alloc_size = le64_to_cpu(attr->nres.alloc_size); 1906 if (end > alloc_size) 1907 end = alloc_size; 1908 1909 while (vbo < end) { 1910 if (idx == -1) { 1911 ok = run_lookup_entry(&run, vcn, &lcn, &clen, &idx); 1912 } else { 1913 CLST vcn_next = vcn; 1914 1915 ok = run_get_entry(&run, ++idx, &vcn, &lcn, &clen) && 1916 vcn == vcn_next; 1917 if (!ok) 1918 vcn = vcn_next; 1919 } 1920 1921 if (!ok) { 1922 err = attr_load_runs_vcn(ni, attr->type, 1923 attr_name(attr), 1924 attr->name_len, &run, vcn); 1925 1926 if (err) 1927 break; 1928 1929 ok = run_lookup_entry(&run, vcn, &lcn, &clen, &idx); 1930 1931 if (!ok) { 1932 err = -EINVAL; 1933 break; 1934 } 1935 } 1936 1937 if (!clen) { 1938 err = -EINVAL; // ? 1939 break; 1940 } 1941 1942 if (lcn == SPARSE_LCN) { 1943 vcn += clen; 1944 vbo = (u64)vcn << cluster_bits; 1945 continue; 1946 } 1947 1948 flags = FIEMAP_EXTENT_MERGED; 1949 if (S_ISDIR(ni->vfs_inode.i_mode)) { 1950 ; 1951 } else if (is_attr_compressed(attr)) { 1952 CLST clst_data; 1953 1954 err = attr_is_frame_compressed(ni, attr, 1955 vcn >> attr->nres.c_unit, 1956 &clst_data, &run); 1957 if (err) 1958 break; 1959 if (clst_data < NTFS_LZNT_CLUSTERS) 1960 flags |= FIEMAP_EXTENT_ENCODED; 1961 } else if (is_attr_encrypted(attr)) { 1962 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED; 1963 } 1964 1965 vbo = (u64)vcn << cluster_bits; 1966 bytes = (u64)clen << cluster_bits; 1967 lbo = (u64)lcn << cluster_bits; 1968 1969 vcn += clen; 1970 1971 if (vbo + bytes >= end) 1972 bytes = end - vbo; 1973 1974 if (vbo + bytes <= valid) { 1975 ; 1976 } else if (vbo >= valid) { 1977 flags |= FIEMAP_EXTENT_UNWRITTEN; 1978 } else { 1979 /* vbo < valid && valid < vbo + bytes */ 1980 u64 dlen = valid - vbo; 1981 1982 if (vbo + dlen >= end) 1983 flags |= FIEMAP_EXTENT_LAST; 1984 1985 err = fiemap_fill_next_extent(fieinfo, vbo, lbo, dlen, 1986 flags); 1987 1988 if (err < 0) 1989 break; 1990 if (err == 1) { 1991 err = 0; 1992 break; 1993 } 1994 1995 vbo = valid; 1996 bytes -= dlen; 1997 if (!bytes) 1998 continue; 1999 2000 lbo += dlen; 2001 flags |= FIEMAP_EXTENT_UNWRITTEN; 2002 } 2003 2004 if (vbo + bytes >= end) 2005 flags |= FIEMAP_EXTENT_LAST; 2006 2007 err = fiemap_fill_next_extent(fieinfo, vbo, lbo, bytes, flags); 2008 if (err < 0) 2009 break; 2010 if (err == 1) { 2011 err = 0; 2012 break; 2013 } 2014 2015 vbo += bytes; 2016 } 2017 2018 out: 2019 run_close(&run); 2020 return err; 2021 } 2022 2023 /* 2024 * ni_readpage_cmpr 2025 * 2026 * When decompressing, we typically obtain more than one page per reference. 2027 * We inject the additional pages into the page cache. 2028 */ 2029 int ni_readpage_cmpr(struct ntfs_inode *ni, struct folio *folio) 2030 { 2031 int err; 2032 struct ntfs_sb_info *sbi = ni->mi.sbi; 2033 struct address_space *mapping = folio->mapping; 2034 pgoff_t index = folio->index; 2035 u64 frame_vbo, vbo = (u64)index << PAGE_SHIFT; 2036 struct page **pages = NULL; /* Array of at most 16 pages. stack? */ 2037 u8 frame_bits; 2038 CLST frame; 2039 u32 i, idx, frame_size, pages_per_frame; 2040 gfp_t gfp_mask; 2041 struct page *pg; 2042 2043 if (vbo >= i_size_read(&ni->vfs_inode)) { 2044 folio_zero_range(folio, 0, folio_size(folio)); 2045 folio_mark_uptodate(folio); 2046 err = 0; 2047 goto out; 2048 } 2049 2050 if (ni->ni_flags & NI_FLAG_COMPRESSED_MASK) { 2051 /* Xpress or LZX. */ 2052 frame_bits = ni_ext_compress_bits(ni); 2053 } else { 2054 /* LZNT compression. */ 2055 frame_bits = NTFS_LZNT_CUNIT + sbi->cluster_bits; 2056 } 2057 frame_size = 1u << frame_bits; 2058 frame = vbo >> frame_bits; 2059 frame_vbo = (u64)frame << frame_bits; 2060 idx = (vbo - frame_vbo) >> PAGE_SHIFT; 2061 2062 pages_per_frame = frame_size >> PAGE_SHIFT; 2063 pages = kcalloc(pages_per_frame, sizeof(struct page *), GFP_NOFS); 2064 if (!pages) { 2065 err = -ENOMEM; 2066 goto out; 2067 } 2068 2069 pages[idx] = &folio->page; 2070 index = frame_vbo >> PAGE_SHIFT; 2071 gfp_mask = mapping_gfp_mask(mapping); 2072 2073 for (i = 0; i < pages_per_frame; i++, index++) { 2074 if (i == idx) 2075 continue; 2076 2077 pg = find_or_create_page(mapping, index, gfp_mask); 2078 if (!pg) { 2079 err = -ENOMEM; 2080 goto out1; 2081 } 2082 pages[i] = pg; 2083 } 2084 2085 err = ni_read_frame(ni, frame_vbo, pages, pages_per_frame); 2086 2087 out1: 2088 for (i = 0; i < pages_per_frame; i++) { 2089 pg = pages[i]; 2090 if (i == idx || !pg) 2091 continue; 2092 unlock_page(pg); 2093 put_page(pg); 2094 } 2095 2096 out: 2097 /* At this point, err contains 0 or -EIO depending on the "critical" page. */ 2098 kfree(pages); 2099 folio_unlock(folio); 2100 2101 return err; 2102 } 2103 2104 #ifdef CONFIG_NTFS3_LZX_XPRESS 2105 /* 2106 * ni_decompress_file - Decompress LZX/Xpress compressed file. 2107 * 2108 * Remove ATTR_DATA::WofCompressedData. 2109 * Remove ATTR_REPARSE. 2110 */ 2111 int ni_decompress_file(struct ntfs_inode *ni) 2112 { 2113 struct ntfs_sb_info *sbi = ni->mi.sbi; 2114 struct inode *inode = &ni->vfs_inode; 2115 loff_t i_size = i_size_read(inode); 2116 struct address_space *mapping = inode->i_mapping; 2117 gfp_t gfp_mask = mapping_gfp_mask(mapping); 2118 struct page **pages = NULL; 2119 struct ATTR_LIST_ENTRY *le; 2120 struct ATTRIB *attr; 2121 CLST vcn, cend, lcn, clen, end; 2122 pgoff_t index; 2123 u64 vbo; 2124 u8 frame_bits; 2125 u32 i, frame_size, pages_per_frame, bytes; 2126 struct mft_inode *mi; 2127 int err; 2128 2129 /* Clusters for decompressed data. */ 2130 cend = bytes_to_cluster(sbi, i_size); 2131 2132 if (!i_size) 2133 goto remove_wof; 2134 2135 /* Check in advance. */ 2136 if (cend > wnd_zeroes(&sbi->used.bitmap)) { 2137 err = -ENOSPC; 2138 goto out; 2139 } 2140 2141 frame_bits = ni_ext_compress_bits(ni); 2142 frame_size = 1u << frame_bits; 2143 pages_per_frame = frame_size >> PAGE_SHIFT; 2144 pages = kcalloc(pages_per_frame, sizeof(struct page *), GFP_NOFS); 2145 if (!pages) { 2146 err = -ENOMEM; 2147 goto out; 2148 } 2149 2150 /* 2151 * Step 1: Decompress data and copy to new allocated clusters. 2152 */ 2153 index = 0; 2154 for (vbo = 0; vbo < i_size; vbo += bytes) { 2155 u32 nr_pages; 2156 bool new; 2157 2158 if (vbo + frame_size > i_size) { 2159 bytes = i_size - vbo; 2160 nr_pages = (bytes + PAGE_SIZE - 1) >> PAGE_SHIFT; 2161 } else { 2162 nr_pages = pages_per_frame; 2163 bytes = frame_size; 2164 } 2165 2166 end = bytes_to_cluster(sbi, vbo + bytes); 2167 2168 for (vcn = vbo >> sbi->cluster_bits; vcn < end; vcn += clen) { 2169 err = attr_data_get_block(ni, vcn, cend - vcn, &lcn, 2170 &clen, &new, false); 2171 if (err) 2172 goto out; 2173 } 2174 2175 for (i = 0; i < pages_per_frame; i++, index++) { 2176 struct page *pg; 2177 2178 pg = find_or_create_page(mapping, index, gfp_mask); 2179 if (!pg) { 2180 while (i--) { 2181 unlock_page(pages[i]); 2182 put_page(pages[i]); 2183 } 2184 err = -ENOMEM; 2185 goto out; 2186 } 2187 pages[i] = pg; 2188 } 2189 2190 err = ni_read_frame(ni, vbo, pages, pages_per_frame); 2191 2192 if (!err) { 2193 down_read(&ni->file.run_lock); 2194 err = ntfs_bio_pages(sbi, &ni->file.run, pages, 2195 nr_pages, vbo, bytes, 2196 REQ_OP_WRITE); 2197 up_read(&ni->file.run_lock); 2198 } 2199 2200 for (i = 0; i < pages_per_frame; i++) { 2201 unlock_page(pages[i]); 2202 put_page(pages[i]); 2203 } 2204 2205 if (err) 2206 goto out; 2207 2208 cond_resched(); 2209 } 2210 2211 remove_wof: 2212 /* 2213 * Step 2: Deallocate attributes ATTR_DATA::WofCompressedData 2214 * and ATTR_REPARSE. 2215 */ 2216 attr = NULL; 2217 le = NULL; 2218 while ((attr = ni_enum_attr_ex(ni, attr, &le, NULL))) { 2219 CLST svcn, evcn; 2220 u32 asize, roff; 2221 2222 if (attr->type == ATTR_REPARSE) { 2223 struct MFT_REF ref; 2224 2225 mi_get_ref(&ni->mi, &ref); 2226 ntfs_remove_reparse(sbi, 0, &ref); 2227 } 2228 2229 if (!attr->non_res) 2230 continue; 2231 2232 if (attr->type != ATTR_REPARSE && 2233 (attr->type != ATTR_DATA || 2234 attr->name_len != ARRAY_SIZE(WOF_NAME) || 2235 memcmp(attr_name(attr), WOF_NAME, sizeof(WOF_NAME)))) 2236 continue; 2237 2238 svcn = le64_to_cpu(attr->nres.svcn); 2239 evcn = le64_to_cpu(attr->nres.evcn); 2240 2241 if (evcn + 1 <= svcn) 2242 continue; 2243 2244 asize = le32_to_cpu(attr->size); 2245 roff = le16_to_cpu(attr->nres.run_off); 2246 2247 if (roff > asize) { 2248 err = -EINVAL; 2249 goto out; 2250 } 2251 2252 /*run==1 Means unpack and deallocate. */ 2253 run_unpack_ex(RUN_DEALLOCATE, sbi, ni->mi.rno, svcn, evcn, svcn, 2254 Add2Ptr(attr, roff), asize - roff); 2255 } 2256 2257 /* 2258 * Step 3: Remove attribute ATTR_DATA::WofCompressedData. 2259 */ 2260 err = ni_remove_attr(ni, ATTR_DATA, WOF_NAME, ARRAY_SIZE(WOF_NAME), 2261 false, NULL); 2262 if (err) 2263 goto out; 2264 2265 /* 2266 * Step 4: Remove ATTR_REPARSE. 2267 */ 2268 err = ni_remove_attr(ni, ATTR_REPARSE, NULL, 0, false, NULL); 2269 if (err) 2270 goto out; 2271 2272 /* 2273 * Step 5: Remove sparse flag from data attribute. 2274 */ 2275 attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, &mi); 2276 if (!attr) { 2277 err = -EINVAL; 2278 goto out; 2279 } 2280 2281 if (attr->non_res && is_attr_sparsed(attr)) { 2282 /* Sparsed attribute header is 8 bytes bigger than normal. */ 2283 struct MFT_REC *rec = mi->mrec; 2284 u32 used = le32_to_cpu(rec->used); 2285 u32 asize = le32_to_cpu(attr->size); 2286 u16 roff = le16_to_cpu(attr->nres.run_off); 2287 char *rbuf = Add2Ptr(attr, roff); 2288 2289 memmove(rbuf - 8, rbuf, used - PtrOffset(rec, rbuf)); 2290 attr->size = cpu_to_le32(asize - 8); 2291 attr->flags &= ~ATTR_FLAG_SPARSED; 2292 attr->nres.run_off = cpu_to_le16(roff - 8); 2293 attr->nres.c_unit = 0; 2294 rec->used = cpu_to_le32(used - 8); 2295 mi->dirty = true; 2296 ni->std_fa &= ~(FILE_ATTRIBUTE_SPARSE_FILE | 2297 FILE_ATTRIBUTE_REPARSE_POINT); 2298 2299 mark_inode_dirty(inode); 2300 } 2301 2302 /* Clear cached flag. */ 2303 ni->ni_flags &= ~NI_FLAG_COMPRESSED_MASK; 2304 if (ni->file.offs_folio) { 2305 folio_put(ni->file.offs_folio); 2306 ni->file.offs_folio = NULL; 2307 } 2308 mapping->a_ops = &ntfs_aops; 2309 2310 out: 2311 kfree(pages); 2312 if (err) 2313 _ntfs_bad_inode(inode); 2314 2315 return err; 2316 } 2317 2318 /* 2319 * decompress_lzx_xpress - External compression LZX/Xpress. 2320 */ 2321 static int decompress_lzx_xpress(struct ntfs_sb_info *sbi, const char *cmpr, 2322 size_t cmpr_size, void *unc, size_t unc_size, 2323 u32 frame_size) 2324 { 2325 int err; 2326 void *ctx; 2327 2328 if (cmpr_size == unc_size) { 2329 /* Frame not compressed. */ 2330 memcpy(unc, cmpr, unc_size); 2331 return 0; 2332 } 2333 2334 err = 0; 2335 if (frame_size == 0x8000) { 2336 mutex_lock(&sbi->compress.mtx_lzx); 2337 /* LZX: Frame compressed. */ 2338 ctx = sbi->compress.lzx; 2339 if (!ctx) { 2340 /* Lazy initialize LZX decompress context. */ 2341 ctx = lzx_allocate_decompressor(); 2342 if (!ctx) { 2343 err = -ENOMEM; 2344 goto out1; 2345 } 2346 2347 sbi->compress.lzx = ctx; 2348 } 2349 2350 if (lzx_decompress(ctx, cmpr, cmpr_size, unc, unc_size)) { 2351 /* Treat all errors as "invalid argument". */ 2352 err = -EINVAL; 2353 } 2354 out1: 2355 mutex_unlock(&sbi->compress.mtx_lzx); 2356 } else { 2357 /* XPRESS: Frame compressed. */ 2358 mutex_lock(&sbi->compress.mtx_xpress); 2359 ctx = sbi->compress.xpress; 2360 if (!ctx) { 2361 /* Lazy initialize Xpress decompress context. */ 2362 ctx = xpress_allocate_decompressor(); 2363 if (!ctx) { 2364 err = -ENOMEM; 2365 goto out2; 2366 } 2367 2368 sbi->compress.xpress = ctx; 2369 } 2370 2371 if (xpress_decompress(ctx, cmpr, cmpr_size, unc, unc_size)) { 2372 /* Treat all errors as "invalid argument". */ 2373 err = -EINVAL; 2374 } 2375 out2: 2376 mutex_unlock(&sbi->compress.mtx_xpress); 2377 } 2378 return err; 2379 } 2380 #endif 2381 2382 /* 2383 * ni_read_frame 2384 * 2385 * Pages - Array of locked pages. 2386 */ 2387 int ni_read_frame(struct ntfs_inode *ni, u64 frame_vbo, struct page **pages, 2388 u32 pages_per_frame) 2389 { 2390 int err; 2391 struct ntfs_sb_info *sbi = ni->mi.sbi; 2392 u8 cluster_bits = sbi->cluster_bits; 2393 char *frame_ondisk = NULL; 2394 char *frame_mem = NULL; 2395 struct page **pages_disk = NULL; 2396 struct ATTR_LIST_ENTRY *le = NULL; 2397 struct runs_tree *run = &ni->file.run; 2398 u64 valid_size = ni->i_valid; 2399 u64 vbo_disk; 2400 size_t unc_size; 2401 u32 frame_size, i, npages_disk, ondisk_size; 2402 struct page *pg; 2403 struct ATTRIB *attr; 2404 CLST frame, clst_data; 2405 2406 /* 2407 * To simplify decompress algorithm do vmap for source 2408 * and target pages. 2409 */ 2410 for (i = 0; i < pages_per_frame; i++) 2411 kmap(pages[i]); 2412 2413 frame_size = pages_per_frame << PAGE_SHIFT; 2414 frame_mem = vmap(pages, pages_per_frame, VM_MAP, PAGE_KERNEL); 2415 if (!frame_mem) { 2416 err = -ENOMEM; 2417 goto out; 2418 } 2419 2420 attr = ni_find_attr(ni, NULL, &le, ATTR_DATA, NULL, 0, NULL, NULL); 2421 if (!attr) { 2422 err = -ENOENT; 2423 goto out1; 2424 } 2425 2426 if (!attr->non_res) { 2427 u32 data_size = le32_to_cpu(attr->res.data_size); 2428 2429 memset(frame_mem, 0, frame_size); 2430 if (frame_vbo < data_size) { 2431 ondisk_size = data_size - frame_vbo; 2432 memcpy(frame_mem, resident_data(attr) + frame_vbo, 2433 min(ondisk_size, frame_size)); 2434 } 2435 err = 0; 2436 goto out1; 2437 } 2438 2439 if (frame_vbo >= valid_size) { 2440 memset(frame_mem, 0, frame_size); 2441 err = 0; 2442 goto out1; 2443 } 2444 2445 if (ni->ni_flags & NI_FLAG_COMPRESSED_MASK) { 2446 #ifndef CONFIG_NTFS3_LZX_XPRESS 2447 err = -EOPNOTSUPP; 2448 goto out1; 2449 #else 2450 loff_t i_size = i_size_read(&ni->vfs_inode); 2451 u32 frame_bits = ni_ext_compress_bits(ni); 2452 u64 frame64 = frame_vbo >> frame_bits; 2453 u64 frames, vbo_data; 2454 2455 if (frame_size != (1u << frame_bits)) { 2456 err = -EINVAL; 2457 goto out1; 2458 } 2459 switch (frame_size) { 2460 case 0x1000: 2461 case 0x2000: 2462 case 0x4000: 2463 case 0x8000: 2464 break; 2465 default: 2466 /* Unknown compression. */ 2467 err = -EOPNOTSUPP; 2468 goto out1; 2469 } 2470 2471 attr = ni_find_attr(ni, attr, &le, ATTR_DATA, WOF_NAME, 2472 ARRAY_SIZE(WOF_NAME), NULL, NULL); 2473 if (!attr) { 2474 ntfs_inode_err( 2475 &ni->vfs_inode, 2476 "external compressed file should contains data attribute \"WofCompressedData\""); 2477 err = -EINVAL; 2478 goto out1; 2479 } 2480 2481 if (!attr->non_res) { 2482 run = NULL; 2483 } else { 2484 run = run_alloc(); 2485 if (!run) { 2486 err = -ENOMEM; 2487 goto out1; 2488 } 2489 } 2490 2491 frames = (i_size - 1) >> frame_bits; 2492 2493 err = attr_wof_frame_info(ni, attr, run, frame64, frames, 2494 frame_bits, &ondisk_size, &vbo_data); 2495 if (err) 2496 goto out2; 2497 2498 if (frame64 == frames) { 2499 unc_size = 1 + ((i_size - 1) & (frame_size - 1)); 2500 ondisk_size = attr_size(attr) - vbo_data; 2501 } else { 2502 unc_size = frame_size; 2503 } 2504 2505 if (ondisk_size > frame_size) { 2506 err = -EINVAL; 2507 goto out2; 2508 } 2509 2510 if (!attr->non_res) { 2511 if (vbo_data + ondisk_size > 2512 le32_to_cpu(attr->res.data_size)) { 2513 err = -EINVAL; 2514 goto out1; 2515 } 2516 2517 err = decompress_lzx_xpress( 2518 sbi, Add2Ptr(resident_data(attr), vbo_data), 2519 ondisk_size, frame_mem, unc_size, frame_size); 2520 goto out1; 2521 } 2522 vbo_disk = vbo_data; 2523 /* Load all runs to read [vbo_disk-vbo_to). */ 2524 err = attr_load_runs_range(ni, ATTR_DATA, WOF_NAME, 2525 ARRAY_SIZE(WOF_NAME), run, vbo_disk, 2526 vbo_data + ondisk_size); 2527 if (err) 2528 goto out2; 2529 npages_disk = (ondisk_size + (vbo_disk & (PAGE_SIZE - 1)) + 2530 PAGE_SIZE - 1) >> 2531 PAGE_SHIFT; 2532 #endif 2533 } else if (is_attr_compressed(attr)) { 2534 /* LZNT compression. */ 2535 if (sbi->cluster_size > NTFS_LZNT_MAX_CLUSTER) { 2536 err = -EOPNOTSUPP; 2537 goto out1; 2538 } 2539 2540 if (attr->nres.c_unit != NTFS_LZNT_CUNIT) { 2541 err = -EOPNOTSUPP; 2542 goto out1; 2543 } 2544 2545 down_write(&ni->file.run_lock); 2546 run_truncate_around(run, le64_to_cpu(attr->nres.svcn)); 2547 frame = frame_vbo >> (cluster_bits + NTFS_LZNT_CUNIT); 2548 err = attr_is_frame_compressed(ni, attr, frame, &clst_data, 2549 run); 2550 up_write(&ni->file.run_lock); 2551 if (err) 2552 goto out1; 2553 2554 if (!clst_data) { 2555 memset(frame_mem, 0, frame_size); 2556 goto out1; 2557 } 2558 2559 frame_size = sbi->cluster_size << NTFS_LZNT_CUNIT; 2560 ondisk_size = clst_data << cluster_bits; 2561 2562 if (clst_data >= NTFS_LZNT_CLUSTERS) { 2563 /* Frame is not compressed. */ 2564 down_read(&ni->file.run_lock); 2565 err = ntfs_bio_pages(sbi, run, pages, pages_per_frame, 2566 frame_vbo, ondisk_size, 2567 REQ_OP_READ); 2568 up_read(&ni->file.run_lock); 2569 goto out1; 2570 } 2571 vbo_disk = frame_vbo; 2572 npages_disk = (ondisk_size + PAGE_SIZE - 1) >> PAGE_SHIFT; 2573 } else { 2574 __builtin_unreachable(); 2575 err = -EINVAL; 2576 goto out1; 2577 } 2578 2579 pages_disk = kcalloc(npages_disk, sizeof(*pages_disk), GFP_NOFS); 2580 if (!pages_disk) { 2581 err = -ENOMEM; 2582 goto out2; 2583 } 2584 2585 for (i = 0; i < npages_disk; i++) { 2586 pg = alloc_page(GFP_KERNEL); 2587 if (!pg) { 2588 err = -ENOMEM; 2589 goto out3; 2590 } 2591 pages_disk[i] = pg; 2592 lock_page(pg); 2593 kmap(pg); 2594 } 2595 2596 /* Read 'ondisk_size' bytes from disk. */ 2597 down_read(&ni->file.run_lock); 2598 err = ntfs_bio_pages(sbi, run, pages_disk, npages_disk, vbo_disk, 2599 ondisk_size, REQ_OP_READ); 2600 up_read(&ni->file.run_lock); 2601 if (err) 2602 goto out3; 2603 2604 /* 2605 * To simplify decompress algorithm do vmap for source and target pages. 2606 */ 2607 frame_ondisk = vmap(pages_disk, npages_disk, VM_MAP, PAGE_KERNEL_RO); 2608 if (!frame_ondisk) { 2609 err = -ENOMEM; 2610 goto out3; 2611 } 2612 2613 /* Decompress: Frame_ondisk -> frame_mem. */ 2614 #ifdef CONFIG_NTFS3_LZX_XPRESS 2615 if (run != &ni->file.run) { 2616 /* LZX or XPRESS */ 2617 err = decompress_lzx_xpress( 2618 sbi, frame_ondisk + (vbo_disk & (PAGE_SIZE - 1)), 2619 ondisk_size, frame_mem, unc_size, frame_size); 2620 } else 2621 #endif 2622 { 2623 /* LZNT - Native NTFS compression. */ 2624 unc_size = decompress_lznt(frame_ondisk, ondisk_size, frame_mem, 2625 frame_size); 2626 if ((ssize_t)unc_size < 0) 2627 err = unc_size; 2628 else if (!unc_size || unc_size > frame_size) 2629 err = -EINVAL; 2630 } 2631 if (!err && valid_size < frame_vbo + frame_size) { 2632 size_t ok = valid_size - frame_vbo; 2633 2634 memset(frame_mem + ok, 0, frame_size - ok); 2635 } 2636 2637 vunmap(frame_ondisk); 2638 2639 out3: 2640 for (i = 0; i < npages_disk; i++) { 2641 pg = pages_disk[i]; 2642 if (pg) { 2643 kunmap(pg); 2644 unlock_page(pg); 2645 put_page(pg); 2646 } 2647 } 2648 kfree(pages_disk); 2649 2650 out2: 2651 #ifdef CONFIG_NTFS3_LZX_XPRESS 2652 if (run != &ni->file.run) 2653 run_free(run); 2654 #endif 2655 out1: 2656 vunmap(frame_mem); 2657 out: 2658 for (i = 0; i < pages_per_frame; i++) { 2659 pg = pages[i]; 2660 kunmap(pg); 2661 SetPageUptodate(pg); 2662 } 2663 2664 return err; 2665 } 2666 2667 /* 2668 * ni_write_frame 2669 * 2670 * Pages - Array of locked pages. 2671 */ 2672 int ni_write_frame(struct ntfs_inode *ni, struct page **pages, 2673 u32 pages_per_frame) 2674 { 2675 int err; 2676 struct ntfs_sb_info *sbi = ni->mi.sbi; 2677 struct folio *folio = page_folio(pages[0]); 2678 u8 frame_bits = NTFS_LZNT_CUNIT + sbi->cluster_bits; 2679 u32 frame_size = sbi->cluster_size << NTFS_LZNT_CUNIT; 2680 u64 frame_vbo = folio_pos(folio); 2681 CLST frame = frame_vbo >> frame_bits; 2682 char *frame_ondisk = NULL; 2683 struct page **pages_disk = NULL; 2684 struct ATTR_LIST_ENTRY *le = NULL; 2685 char *frame_mem; 2686 struct ATTRIB *attr; 2687 struct mft_inode *mi; 2688 u32 i; 2689 struct page *pg; 2690 size_t compr_size, ondisk_size; 2691 struct lznt *lznt; 2692 2693 attr = ni_find_attr(ni, NULL, &le, ATTR_DATA, NULL, 0, NULL, &mi); 2694 if (!attr) { 2695 err = -ENOENT; 2696 goto out; 2697 } 2698 2699 if (WARN_ON(!is_attr_compressed(attr))) { 2700 err = -EINVAL; 2701 goto out; 2702 } 2703 2704 if (sbi->cluster_size > NTFS_LZNT_MAX_CLUSTER) { 2705 err = -EOPNOTSUPP; 2706 goto out; 2707 } 2708 2709 if (!attr->non_res) { 2710 down_write(&ni->file.run_lock); 2711 err = attr_make_nonresident(ni, attr, le, mi, 2712 le32_to_cpu(attr->res.data_size), 2713 &ni->file.run, &attr, pages[0]); 2714 up_write(&ni->file.run_lock); 2715 if (err) 2716 goto out; 2717 } 2718 2719 if (attr->nres.c_unit != NTFS_LZNT_CUNIT) { 2720 err = -EOPNOTSUPP; 2721 goto out; 2722 } 2723 2724 pages_disk = kcalloc(pages_per_frame, sizeof(struct page *), GFP_NOFS); 2725 if (!pages_disk) { 2726 err = -ENOMEM; 2727 goto out; 2728 } 2729 2730 for (i = 0; i < pages_per_frame; i++) { 2731 pg = alloc_page(GFP_KERNEL); 2732 if (!pg) { 2733 err = -ENOMEM; 2734 goto out1; 2735 } 2736 pages_disk[i] = pg; 2737 lock_page(pg); 2738 kmap(pg); 2739 } 2740 2741 /* To simplify compress algorithm do vmap for source and target pages. */ 2742 frame_ondisk = vmap(pages_disk, pages_per_frame, VM_MAP, PAGE_KERNEL); 2743 if (!frame_ondisk) { 2744 err = -ENOMEM; 2745 goto out1; 2746 } 2747 2748 for (i = 0; i < pages_per_frame; i++) 2749 kmap(pages[i]); 2750 2751 /* Map in-memory frame for read-only. */ 2752 frame_mem = vmap(pages, pages_per_frame, VM_MAP, PAGE_KERNEL_RO); 2753 if (!frame_mem) { 2754 err = -ENOMEM; 2755 goto out2; 2756 } 2757 2758 mutex_lock(&sbi->compress.mtx_lznt); 2759 lznt = NULL; 2760 if (!sbi->compress.lznt) { 2761 /* 2762 * LZNT implements two levels of compression: 2763 * 0 - Standard compression 2764 * 1 - Best compression, requires a lot of cpu 2765 * use mount option? 2766 */ 2767 lznt = get_lznt_ctx(0); 2768 if (!lznt) { 2769 mutex_unlock(&sbi->compress.mtx_lznt); 2770 err = -ENOMEM; 2771 goto out3; 2772 } 2773 2774 sbi->compress.lznt = lznt; 2775 lznt = NULL; 2776 } 2777 2778 /* Compress: frame_mem -> frame_ondisk */ 2779 compr_size = compress_lznt(frame_mem, frame_size, frame_ondisk, 2780 frame_size, sbi->compress.lznt); 2781 mutex_unlock(&sbi->compress.mtx_lznt); 2782 kfree(lznt); 2783 2784 if (compr_size + sbi->cluster_size > frame_size) { 2785 /* Frame is not compressed. */ 2786 compr_size = frame_size; 2787 ondisk_size = frame_size; 2788 } else if (compr_size) { 2789 /* Frame is compressed. */ 2790 ondisk_size = ntfs_up_cluster(sbi, compr_size); 2791 memset(frame_ondisk + compr_size, 0, ondisk_size - compr_size); 2792 } else { 2793 /* Frame is sparsed. */ 2794 ondisk_size = 0; 2795 } 2796 2797 down_write(&ni->file.run_lock); 2798 run_truncate_around(&ni->file.run, le64_to_cpu(attr->nres.svcn)); 2799 err = attr_allocate_frame(ni, frame, compr_size, ni->i_valid); 2800 up_write(&ni->file.run_lock); 2801 if (err) 2802 goto out2; 2803 2804 if (!ondisk_size) 2805 goto out2; 2806 2807 down_read(&ni->file.run_lock); 2808 err = ntfs_bio_pages(sbi, &ni->file.run, 2809 ondisk_size < frame_size ? pages_disk : pages, 2810 pages_per_frame, frame_vbo, ondisk_size, 2811 REQ_OP_WRITE); 2812 up_read(&ni->file.run_lock); 2813 2814 out3: 2815 vunmap(frame_mem); 2816 2817 out2: 2818 for (i = 0; i < pages_per_frame; i++) 2819 kunmap(pages[i]); 2820 2821 vunmap(frame_ondisk); 2822 out1: 2823 for (i = 0; i < pages_per_frame; i++) { 2824 pg = pages_disk[i]; 2825 if (pg) { 2826 kunmap(pg); 2827 unlock_page(pg); 2828 put_page(pg); 2829 } 2830 } 2831 kfree(pages_disk); 2832 out: 2833 return err; 2834 } 2835 2836 /* 2837 * ni_remove_name - Removes name 'de' from MFT and from directory. 2838 * 'de2' and 'undo_step' are used to restore MFT/dir, if error occurs. 2839 */ 2840 int ni_remove_name(struct ntfs_inode *dir_ni, struct ntfs_inode *ni, 2841 struct NTFS_DE *de, struct NTFS_DE **de2, int *undo_step) 2842 { 2843 int err; 2844 struct ntfs_sb_info *sbi = ni->mi.sbi; 2845 struct ATTR_FILE_NAME *de_name = (struct ATTR_FILE_NAME *)(de + 1); 2846 struct ATTR_FILE_NAME *fname; 2847 struct ATTR_LIST_ENTRY *le; 2848 struct mft_inode *mi; 2849 u16 de_key_size = le16_to_cpu(de->key_size); 2850 u8 name_type; 2851 2852 *undo_step = 0; 2853 2854 /* Find name in record. */ 2855 mi_get_ref(&dir_ni->mi, &de_name->home); 2856 2857 fname = ni_fname_name(ni, (struct le_str *)&de_name->name_len, 2858 &de_name->home, &mi, &le); 2859 if (!fname) 2860 return -ENOENT; 2861 2862 memcpy(&de_name->dup, &fname->dup, sizeof(struct NTFS_DUP_INFO)); 2863 name_type = paired_name(fname->type); 2864 2865 /* Mark ntfs as dirty. It will be cleared at umount. */ 2866 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY); 2867 2868 /* Step 1: Remove name from directory. */ 2869 err = indx_delete_entry(&dir_ni->dir, dir_ni, fname, de_key_size, sbi); 2870 if (err) 2871 return err; 2872 2873 /* Step 2: Remove name from MFT. */ 2874 ni_remove_attr_le(ni, attr_from_name(fname), mi, le); 2875 2876 *undo_step = 2; 2877 2878 /* Get paired name. */ 2879 fname = ni_fname_type(ni, name_type, &mi, &le); 2880 if (fname) { 2881 u16 de2_key_size = fname_full_size(fname); 2882 2883 *de2 = Add2Ptr(de, 1024); 2884 (*de2)->key_size = cpu_to_le16(de2_key_size); 2885 2886 memcpy(*de2 + 1, fname, de2_key_size); 2887 2888 /* Step 3: Remove paired name from directory. */ 2889 err = indx_delete_entry(&dir_ni->dir, dir_ni, fname, 2890 de2_key_size, sbi); 2891 if (err) 2892 return err; 2893 2894 /* Step 4: Remove paired name from MFT. */ 2895 ni_remove_attr_le(ni, attr_from_name(fname), mi, le); 2896 2897 *undo_step = 4; 2898 } 2899 return 0; 2900 } 2901 2902 /* 2903 * ni_remove_name_undo - Paired function for ni_remove_name. 2904 * 2905 * Return: True if ok 2906 */ 2907 bool ni_remove_name_undo(struct ntfs_inode *dir_ni, struct ntfs_inode *ni, 2908 struct NTFS_DE *de, struct NTFS_DE *de2, int undo_step) 2909 { 2910 struct ntfs_sb_info *sbi = ni->mi.sbi; 2911 struct ATTRIB *attr; 2912 u16 de_key_size; 2913 2914 switch (undo_step) { 2915 case 4: 2916 de_key_size = le16_to_cpu(de2->key_size); 2917 if (ni_insert_resident(ni, de_key_size, ATTR_NAME, NULL, 0, 2918 &attr, NULL, NULL)) 2919 return false; 2920 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), de2 + 1, de_key_size); 2921 2922 mi_get_ref(&ni->mi, &de2->ref); 2923 de2->size = cpu_to_le16(ALIGN(de_key_size, 8) + 2924 sizeof(struct NTFS_DE)); 2925 de2->flags = 0; 2926 de2->res = 0; 2927 2928 if (indx_insert_entry(&dir_ni->dir, dir_ni, de2, sbi, NULL, 1)) 2929 return false; 2930 fallthrough; 2931 2932 case 2: 2933 de_key_size = le16_to_cpu(de->key_size); 2934 2935 if (ni_insert_resident(ni, de_key_size, ATTR_NAME, NULL, 0, 2936 &attr, NULL, NULL)) 2937 return false; 2938 2939 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), de + 1, de_key_size); 2940 mi_get_ref(&ni->mi, &de->ref); 2941 2942 if (indx_insert_entry(&dir_ni->dir, dir_ni, de, sbi, NULL, 1)) 2943 return false; 2944 } 2945 2946 return true; 2947 } 2948 2949 /* 2950 * ni_add_name - Add new name into MFT and into directory. 2951 */ 2952 int ni_add_name(struct ntfs_inode *dir_ni, struct ntfs_inode *ni, 2953 struct NTFS_DE *de) 2954 { 2955 int err; 2956 struct ntfs_sb_info *sbi = ni->mi.sbi; 2957 struct ATTRIB *attr; 2958 struct ATTR_LIST_ENTRY *le; 2959 struct mft_inode *mi; 2960 struct ATTR_FILE_NAME *fname; 2961 struct ATTR_FILE_NAME *de_name = (struct ATTR_FILE_NAME *)(de + 1); 2962 u16 de_key_size = le16_to_cpu(de->key_size); 2963 2964 if (sbi->options->windows_names && 2965 !valid_windows_name(sbi, (struct le_str *)&de_name->name_len)) 2966 return -EINVAL; 2967 2968 /* If option "hide_dot_files" then set hidden attribute for dot files. */ 2969 if (ni->mi.sbi->options->hide_dot_files) { 2970 if (de_name->name_len > 0 && 2971 le16_to_cpu(de_name->name[0]) == '.') 2972 ni->std_fa |= FILE_ATTRIBUTE_HIDDEN; 2973 else 2974 ni->std_fa &= ~FILE_ATTRIBUTE_HIDDEN; 2975 } 2976 2977 mi_get_ref(&ni->mi, &de->ref); 2978 mi_get_ref(&dir_ni->mi, &de_name->home); 2979 2980 /* Fill duplicate from any ATTR_NAME. */ 2981 fname = ni_fname_name(ni, NULL, NULL, NULL, NULL); 2982 if (fname) 2983 memcpy(&de_name->dup, &fname->dup, sizeof(fname->dup)); 2984 de_name->dup.fa = ni->std_fa; 2985 2986 /* Insert new name into MFT. */ 2987 err = ni_insert_resident(ni, de_key_size, ATTR_NAME, NULL, 0, &attr, 2988 &mi, &le); 2989 if (err) 2990 return err; 2991 2992 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), de_name, de_key_size); 2993 2994 /* Insert new name into directory. */ 2995 err = indx_insert_entry(&dir_ni->dir, dir_ni, de, sbi, NULL, 0); 2996 if (err) 2997 ni_remove_attr_le(ni, attr, mi, le); 2998 2999 return err; 3000 } 3001 3002 /* 3003 * ni_rename - Remove one name and insert new name. 3004 */ 3005 int ni_rename(struct ntfs_inode *dir_ni, struct ntfs_inode *new_dir_ni, 3006 struct ntfs_inode *ni, struct NTFS_DE *de, struct NTFS_DE *new_de, 3007 bool *is_bad) 3008 { 3009 int err; 3010 struct NTFS_DE *de2 = NULL; 3011 int undo = 0; 3012 3013 /* 3014 * There are two possible ways to rename: 3015 * 1) Add new name and remove old name. 3016 * 2) Remove old name and add new name. 3017 * 3018 * In most cases (not all!) adding new name into MFT and into directory can 3019 * allocate additional cluster(s). 3020 * Second way may result to bad inode if we can't add new name 3021 * and then can't restore (add) old name. 3022 */ 3023 3024 /* 3025 * Way 1 - Add new + remove old. 3026 */ 3027 err = ni_add_name(new_dir_ni, ni, new_de); 3028 if (!err) { 3029 err = ni_remove_name(dir_ni, ni, de, &de2, &undo); 3030 if (err && ni_remove_name(new_dir_ni, ni, new_de, &de2, &undo)) 3031 *is_bad = true; 3032 } 3033 3034 /* 3035 * Way 2 - Remove old + add new. 3036 */ 3037 /* 3038 * err = ni_remove_name(dir_ni, ni, de, &de2, &undo); 3039 * if (!err) { 3040 * err = ni_add_name(new_dir_ni, ni, new_de); 3041 * if (err && !ni_remove_name_undo(dir_ni, ni, de, de2, undo)) 3042 * *is_bad = true; 3043 * } 3044 */ 3045 3046 return err; 3047 } 3048 3049 /* 3050 * ni_is_dirty - Return: True if 'ni' requires ni_write_inode. 3051 */ 3052 bool ni_is_dirty(struct inode *inode) 3053 { 3054 struct ntfs_inode *ni = ntfs_i(inode); 3055 struct rb_node *node; 3056 3057 if (ni->mi.dirty || ni->attr_list.dirty || 3058 (ni->ni_flags & NI_FLAG_UPDATE_PARENT)) 3059 return true; 3060 3061 for (node = rb_first(&ni->mi_tree); node; node = rb_next(node)) { 3062 if (rb_entry(node, struct mft_inode, node)->dirty) 3063 return true; 3064 } 3065 3066 return false; 3067 } 3068 3069 /* 3070 * ni_update_parent 3071 * 3072 * Update duplicate info of ATTR_FILE_NAME in MFT and in parent directories. 3073 */ 3074 static bool ni_update_parent(struct ntfs_inode *ni, struct NTFS_DUP_INFO *dup, 3075 int sync) 3076 { 3077 struct ATTRIB *attr; 3078 struct mft_inode *mi; 3079 struct ATTR_LIST_ENTRY *le = NULL; 3080 struct ntfs_sb_info *sbi = ni->mi.sbi; 3081 struct super_block *sb = sbi->sb; 3082 bool re_dirty = false; 3083 3084 if (ni->mi.mrec->flags & RECORD_FLAG_DIR) { 3085 dup->fa |= FILE_ATTRIBUTE_DIRECTORY; 3086 attr = NULL; 3087 dup->alloc_size = 0; 3088 dup->data_size = 0; 3089 } else { 3090 dup->fa &= ~FILE_ATTRIBUTE_DIRECTORY; 3091 3092 attr = ni_find_attr(ni, NULL, &le, ATTR_DATA, NULL, 0, NULL, 3093 &mi); 3094 if (!attr) { 3095 dup->alloc_size = dup->data_size = 0; 3096 } else if (!attr->non_res) { 3097 u32 data_size = le32_to_cpu(attr->res.data_size); 3098 3099 dup->alloc_size = cpu_to_le64(ALIGN(data_size, 8)); 3100 dup->data_size = cpu_to_le64(data_size); 3101 } else { 3102 u64 new_valid = ni->i_valid; 3103 u64 data_size = le64_to_cpu(attr->nres.data_size); 3104 __le64 valid_le; 3105 3106 dup->alloc_size = is_attr_ext(attr) ? 3107 attr->nres.total_size : 3108 attr->nres.alloc_size; 3109 dup->data_size = attr->nres.data_size; 3110 3111 if (new_valid > data_size) 3112 new_valid = data_size; 3113 3114 valid_le = cpu_to_le64(new_valid); 3115 if (valid_le != attr->nres.valid_size) { 3116 attr->nres.valid_size = valid_le; 3117 mi->dirty = true; 3118 } 3119 } 3120 } 3121 3122 /* TODO: Fill reparse info. */ 3123 dup->reparse = 0; 3124 dup->ea_size = 0; 3125 3126 if (ni->ni_flags & NI_FLAG_EA) { 3127 attr = ni_find_attr(ni, attr, &le, ATTR_EA_INFO, NULL, 0, NULL, 3128 NULL); 3129 if (attr) { 3130 const struct EA_INFO *info; 3131 3132 info = resident_data_ex(attr, sizeof(struct EA_INFO)); 3133 /* If ATTR_EA_INFO exists 'info' can't be NULL. */ 3134 if (info) 3135 dup->ea_size = info->size_pack; 3136 } 3137 } 3138 3139 attr = NULL; 3140 le = NULL; 3141 3142 while ((attr = ni_find_attr(ni, attr, &le, ATTR_NAME, NULL, 0, NULL, 3143 &mi))) { 3144 struct inode *dir; 3145 struct ATTR_FILE_NAME *fname; 3146 3147 fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME); 3148 if (!fname || !memcmp(&fname->dup, dup, sizeof(fname->dup))) 3149 continue; 3150 3151 /* Check simple case when parent inode equals current inode. */ 3152 if (ino_get(&fname->home) == ni->vfs_inode.i_ino) { 3153 ntfs_set_state(sbi, NTFS_DIRTY_ERROR); 3154 continue; 3155 } 3156 3157 /* ntfs_iget5 may sleep. */ 3158 dir = ntfs_iget5(sb, &fname->home, NULL); 3159 if (IS_ERR(dir)) { 3160 ntfs_inode_warn( 3161 &ni->vfs_inode, 3162 "failed to open parent directory r=%lx to update", 3163 (long)ino_get(&fname->home)); 3164 continue; 3165 } 3166 3167 if (!is_bad_inode(dir)) { 3168 struct ntfs_inode *dir_ni = ntfs_i(dir); 3169 3170 if (!ni_trylock(dir_ni)) { 3171 re_dirty = true; 3172 } else { 3173 indx_update_dup(dir_ni, sbi, fname, dup, sync); 3174 ni_unlock(dir_ni); 3175 memcpy(&fname->dup, dup, sizeof(fname->dup)); 3176 mi->dirty = true; 3177 } 3178 } 3179 iput(dir); 3180 } 3181 3182 return re_dirty; 3183 } 3184 3185 /* 3186 * ni_write_inode - Write MFT base record and all subrecords to disk. 3187 */ 3188 int ni_write_inode(struct inode *inode, int sync, const char *hint) 3189 { 3190 int err = 0, err2; 3191 struct ntfs_inode *ni = ntfs_i(inode); 3192 struct super_block *sb = inode->i_sb; 3193 struct ntfs_sb_info *sbi = sb->s_fs_info; 3194 bool re_dirty = false; 3195 struct ATTR_STD_INFO *std; 3196 struct rb_node *node, *next; 3197 struct NTFS_DUP_INFO dup; 3198 3199 if (is_bad_inode(inode) || sb_rdonly(sb)) 3200 return 0; 3201 3202 if (unlikely(ntfs3_forced_shutdown(sb))) 3203 return -EIO; 3204 3205 if (!ni_trylock(ni)) { 3206 /* 'ni' is under modification, skip for now. */ 3207 mark_inode_dirty_sync(inode); 3208 return 0; 3209 } 3210 3211 if (!ni->mi.mrec) 3212 goto out; 3213 3214 if (is_rec_inuse(ni->mi.mrec) && 3215 !(sbi->flags & NTFS_FLAGS_LOG_REPLAYING) && inode->i_nlink) { 3216 bool modified = false; 3217 struct timespec64 ts; 3218 3219 /* Update times in standard attribute. */ 3220 std = ni_std(ni); 3221 if (!std) { 3222 err = -EINVAL; 3223 goto out; 3224 } 3225 3226 /* Update the access times if they have changed. */ 3227 ts = inode_get_mtime(inode); 3228 dup.m_time = kernel2nt(&ts); 3229 if (std->m_time != dup.m_time) { 3230 std->m_time = dup.m_time; 3231 modified = true; 3232 } 3233 3234 ts = inode_get_ctime(inode); 3235 dup.c_time = kernel2nt(&ts); 3236 if (std->c_time != dup.c_time) { 3237 std->c_time = dup.c_time; 3238 modified = true; 3239 } 3240 3241 ts = inode_get_atime(inode); 3242 dup.a_time = kernel2nt(&ts); 3243 if (std->a_time != dup.a_time) { 3244 std->a_time = dup.a_time; 3245 modified = true; 3246 } 3247 3248 dup.fa = ni->std_fa; 3249 if (std->fa != dup.fa) { 3250 std->fa = dup.fa; 3251 modified = true; 3252 } 3253 3254 /* std attribute is always in primary MFT record. */ 3255 if (modified) 3256 ni->mi.dirty = true; 3257 3258 if (!ntfs_is_meta_file(sbi, inode->i_ino) && 3259 (modified || (ni->ni_flags & NI_FLAG_UPDATE_PARENT)) 3260 /* Avoid __wait_on_freeing_inode(inode). */ 3261 && (sb->s_flags & SB_ACTIVE)) { 3262 dup.cr_time = std->cr_time; 3263 /* Not critical if this function fail. */ 3264 re_dirty = ni_update_parent(ni, &dup, sync); 3265 3266 if (re_dirty) 3267 ni->ni_flags |= NI_FLAG_UPDATE_PARENT; 3268 else 3269 ni->ni_flags &= ~NI_FLAG_UPDATE_PARENT; 3270 } 3271 3272 /* Update attribute list. */ 3273 if (ni->attr_list.size && ni->attr_list.dirty) { 3274 if (inode->i_ino != MFT_REC_MFT || sync) { 3275 err = ni_try_remove_attr_list(ni); 3276 if (err) 3277 goto out; 3278 } 3279 3280 err = al_update(ni, sync); 3281 if (err) 3282 goto out; 3283 } 3284 } 3285 3286 for (node = rb_first(&ni->mi_tree); node; node = next) { 3287 struct mft_inode *mi = rb_entry(node, struct mft_inode, node); 3288 bool is_empty; 3289 3290 next = rb_next(node); 3291 3292 if (!mi->dirty) 3293 continue; 3294 3295 is_empty = !mi_enum_attr(ni, mi, NULL); 3296 3297 if (is_empty) 3298 clear_rec_inuse(mi->mrec); 3299 3300 err2 = mi_write(mi, sync); 3301 if (!err && err2) 3302 err = err2; 3303 3304 if (is_empty) { 3305 ntfs_mark_rec_free(sbi, mi->rno, false); 3306 rb_erase(node, &ni->mi_tree); 3307 mi_put(mi); 3308 } 3309 } 3310 3311 if (ni->mi.dirty) { 3312 err2 = mi_write(&ni->mi, sync); 3313 if (!err && err2) 3314 err = err2; 3315 } 3316 out: 3317 ni_unlock(ni); 3318 3319 if (err) { 3320 ntfs_inode_err(inode, "%s failed, %d.", hint, err); 3321 ntfs_set_state(sbi, NTFS_DIRTY_ERROR); 3322 return err; 3323 } 3324 3325 if (re_dirty) 3326 mark_inode_dirty_sync(inode); 3327 3328 return 0; 3329 } 3330