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