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