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