FILE
attrib.c
File preview with syntax highlighting.
linux-6.19.14/fs/ntfs3/attrib.c
1// SPDX-License-Identifier: GPL-2.0 2/* 3 * 4 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved. 5 * 6 * TODO: Merge attr_set_size/attr_data_get_block/attr_allocate_frame? 7 */ 8 9#include <linux/fs.h> 10#include <linux/slab.h> 11#include <linux/kernel.h> 12 13#include "debug.h" 14#include "ntfs.h" 15#include "ntfs_fs.h" 16 17/* 18 * You can set external NTFS_MIN_LOG2_OF_CLUMP/NTFS_MAX_LOG2_OF_CLUMP to manage 19 * preallocate algorithm. 20 */ 21#ifndef NTFS_MIN_LOG2_OF_CLUMP 22#define NTFS_MIN_LOG2_OF_CLUMP 16 23#endif 24 25#ifndef NTFS_MAX_LOG2_OF_CLUMP 26#define NTFS_MAX_LOG2_OF_CLUMP 26 27#endif 28 29// 16M 30#define NTFS_CLUMP_MIN (1 << (NTFS_MIN_LOG2_OF_CLUMP + 8)) 31// 16G 32#define NTFS_CLUMP_MAX (1ull << (NTFS_MAX_LOG2_OF_CLUMP + 8)) 33 34static inline u64 get_pre_allocated(u64 size) 35{ 36 u32 clump; 37 u8 align_shift; 38 u64 ret; 39 40 if (size <= NTFS_CLUMP_MIN) { 41 clump = 1 << NTFS_MIN_LOG2_OF_CLUMP; 42 align_shift = NTFS_MIN_LOG2_OF_CLUMP; 43 } else if (size >= NTFS_CLUMP_MAX) { 44 clump = 1 << NTFS_MAX_LOG2_OF_CLUMP; 45 align_shift = NTFS_MAX_LOG2_OF_CLUMP; 46 } else { 47 align_shift = NTFS_MIN_LOG2_OF_CLUMP - 1 + 48 __ffs(size >> (8 + NTFS_MIN_LOG2_OF_CLUMP)); 49 clump = 1u << align_shift; 50 } 51 52 ret = (((size + clump - 1) >> align_shift)) << align_shift; 53 54 return ret; 55} 56 57/* 58 * attr_load_runs - Load all runs stored in @attr. 59 */ 60static int attr_load_runs(struct ATTRIB *attr, struct ntfs_inode *ni, 61 struct runs_tree *run, const CLST *vcn) 62{ 63 int err; 64 CLST svcn = le64_to_cpu(attr->nres.svcn); 65 CLST evcn = le64_to_cpu(attr->nres.evcn); 66 u32 asize; 67 u16 run_off; 68 69 if (svcn >= evcn + 1 || run_is_mapped_full(run, svcn, evcn)) 70 return 0; 71 72 if (vcn && (evcn < *vcn || *vcn < svcn)) 73 return -EINVAL; 74 75 asize = le32_to_cpu(attr->size); 76 run_off = le16_to_cpu(attr->nres.run_off); 77 78 if (run_off > asize) 79 return -EINVAL; 80 81 err = run_unpack_ex(run, ni->mi.sbi, ni->mi.rno, svcn, evcn, 82 vcn ? *vcn : svcn, Add2Ptr(attr, run_off), 83 asize - run_off); 84 if (err < 0) 85 return err; 86 87 return 0; 88} 89 90/* 91 * run_deallocate_ex - Deallocate clusters. 92 */ 93static int run_deallocate_ex(struct ntfs_sb_info *sbi, struct runs_tree *run, 94 CLST vcn, CLST len, CLST *done, bool trim) 95{ 96 int err = 0; 97 CLST vcn_next, vcn0 = vcn, lcn, clen, dn = 0; 98 size_t idx; 99 100 if (!len) 101 goto out; 102 103 if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx)) { 104failed: 105 run_truncate(run, vcn0); 106 err = -EINVAL; 107 goto out; 108 } 109 110 for (;;) { 111 if (clen > len) 112 clen = len; 113 114 if (!clen) { 115 err = -EINVAL; 116 goto out; 117 } 118 119 if (lcn != SPARSE_LCN) { 120 if (sbi) { 121 /* mark bitmap range [lcn + clen) as free and trim clusters. */ 122 mark_as_free_ex(sbi, lcn, clen, trim); 123 } 124 dn += clen; 125 } 126 127 len -= clen; 128 if (!len) 129 break; 130 131 vcn_next = vcn + clen; 132 if (!run_get_entry(run, ++idx, &vcn, &lcn, &clen) || 133 vcn != vcn_next) { 134 /* Save memory - don't load entire run. */ 135 goto failed; 136 } 137 } 138 139out: 140 if (done) 141 *done += dn; 142 143 return err; 144} 145 146/* 147 * attr_allocate_clusters - Find free space, mark it as used and store in @run. 148 */ 149int attr_allocate_clusters(struct ntfs_sb_info *sbi, struct runs_tree *run, 150 CLST vcn, CLST lcn, CLST len, CLST *pre_alloc, 151 enum ALLOCATE_OPT opt, CLST *alen, const size_t fr, 152 CLST *new_lcn, CLST *new_len) 153{ 154 int err; 155 CLST flen, vcn0 = vcn, pre = pre_alloc ? *pre_alloc : 0; 156 size_t cnt = run->count; 157 158 for (;;) { 159 err = ntfs_look_for_free_space(sbi, lcn, len + pre, &lcn, &flen, 160 opt); 161 162 if (err == -ENOSPC && pre) { 163 pre = 0; 164 if (*pre_alloc) 165 *pre_alloc = 0; 166 continue; 167 } 168 169 if (err) 170 goto out; 171 172 if (vcn == vcn0) { 173 /* Return the first fragment. */ 174 if (new_lcn) 175 *new_lcn = lcn; 176 if (new_len) 177 *new_len = flen; 178 } 179 180 /* Add new fragment into run storage. */ 181 if (!run_add_entry(run, vcn, lcn, flen, opt & ALLOCATE_MFT)) { 182 /* Undo last 'ntfs_look_for_free_space' */ 183 mark_as_free_ex(sbi, lcn, len, false); 184 err = -ENOMEM; 185 goto out; 186 } 187 188 if (opt & ALLOCATE_ZERO) { 189 u8 shift = sbi->cluster_bits - SECTOR_SHIFT; 190 191 err = blkdev_issue_zeroout(sbi->sb->s_bdev, 192 (sector_t)lcn << shift, 193 (sector_t)flen << shift, 194 GFP_NOFS, 0); 195 if (err) 196 goto out; 197 } 198 199 vcn += flen; 200 201 if (flen >= len || (opt & ALLOCATE_MFT) || 202 (fr && run->count - cnt >= fr)) { 203 *alen = vcn - vcn0; 204 return 0; 205 } 206 207 len -= flen; 208 } 209 210out: 211 /* Undo 'ntfs_look_for_free_space' */ 212 if (vcn - vcn0) { 213 run_deallocate_ex(sbi, run, vcn0, vcn - vcn0, NULL, false); 214 run_truncate(run, vcn0); 215 } 216 217 return err; 218} 219 220/* 221 * attr_make_nonresident 222 * 223 * If page is not NULL - it is already contains resident data 224 * and locked (called from ni_write_frame()). 225 */ 226int attr_make_nonresident(struct ntfs_inode *ni, struct ATTRIB *attr, 227 struct ATTR_LIST_ENTRY *le, struct mft_inode *mi, 228 u64 new_size, struct runs_tree *run, 229 struct ATTRIB **ins_attr, struct page *page) 230{ 231 struct ntfs_sb_info *sbi; 232 struct ATTRIB *attr_s; 233 struct MFT_REC *rec; 234 u32 used, asize, rsize, aoff; 235 bool is_data; 236 CLST len, alen; 237 char *next; 238 int err; 239 240 if (attr->non_res) { 241 *ins_attr = attr; 242 return 0; 243 } 244 245 sbi = mi->sbi; 246 rec = mi->mrec; 247 attr_s = NULL; 248 used = le32_to_cpu(rec->used); 249 asize = le32_to_cpu(attr->size); 250 next = Add2Ptr(attr, asize); 251 aoff = PtrOffset(rec, attr); 252 rsize = le32_to_cpu(attr->res.data_size); 253 is_data = attr->type == ATTR_DATA && !attr->name_len; 254 255 /* len - how many clusters required to store 'rsize' bytes */ 256 if (is_attr_compressed(attr)) { 257 u8 shift = sbi->cluster_bits + NTFS_LZNT_CUNIT; 258 len = ((rsize + (1u << shift) - 1) >> shift) << NTFS_LZNT_CUNIT; 259 } else { 260 len = bytes_to_cluster(sbi, rsize); 261 } 262 263 run_init(run); 264 265 /* Make a copy of original attribute. */ 266 attr_s = kmemdup(attr, asize, GFP_NOFS); 267 if (!attr_s) { 268 err = -ENOMEM; 269 goto out; 270 } 271 272 if (!len) { 273 /* Empty resident -> Empty nonresident. */ 274 alen = 0; 275 } else { 276 const char *data = resident_data(attr); 277 278 err = attr_allocate_clusters(sbi, run, 0, 0, len, NULL, 279 ALLOCATE_DEF, &alen, 0, NULL, 280 NULL); 281 if (err) 282 goto out1; 283 284 if (!rsize) { 285 /* Empty resident -> Non empty nonresident. */ 286 } else if (!is_data) { 287 err = ntfs_sb_write_run(sbi, run, 0, data, rsize, 0); 288 if (err) 289 goto out2; 290 } else if (!page) { 291 struct address_space *mapping = ni->vfs_inode.i_mapping; 292 struct folio *folio; 293 294 folio = __filemap_get_folio( 295 mapping, 0, FGP_LOCK | FGP_ACCESSED | FGP_CREAT, 296 mapping_gfp_mask(mapping)); 297 if (IS_ERR(folio)) { 298 err = PTR_ERR(folio); 299 goto out2; 300 } 301 folio_fill_tail(folio, 0, data, rsize); 302 folio_mark_uptodate(folio); 303 folio_mark_dirty(folio); 304 folio_unlock(folio); 305 folio_put(folio); 306 } 307 } 308 309 /* Remove original attribute. */ 310 used -= asize; 311 memmove(attr, Add2Ptr(attr, asize), used - aoff); 312 rec->used = cpu_to_le32(used); 313 mi->dirty = true; 314 if (le) 315 al_remove_le(ni, le); 316 317 err = ni_insert_nonresident(ni, attr_s->type, attr_name(attr_s), 318 attr_s->name_len, run, 0, alen, 319 attr_s->flags, &attr, NULL, NULL); 320 if (err) 321 goto out3; 322 323 kfree(attr_s); 324 attr->nres.data_size = cpu_to_le64(rsize); 325 attr->nres.valid_size = attr->nres.data_size; 326 327 *ins_attr = attr; 328 329 if (is_data) 330 ni->ni_flags &= ~NI_FLAG_RESIDENT; 331 332 /* Resident attribute becomes non resident. */ 333 return 0; 334 335out3: 336 attr = Add2Ptr(rec, aoff); 337 memmove(next, attr, used - aoff); 338 memcpy(attr, attr_s, asize); 339 rec->used = cpu_to_le32(used + asize); 340 mi->dirty = true; 341out2: 342 /* Undo: do not trim new allocated clusters. */ 343 run_deallocate(sbi, run, false); 344 run_close(run); 345out1: 346 kfree(attr_s); 347out: 348 return err; 349} 350 351/* 352 * attr_set_size_res - Helper for attr_set_size(). 353 */ 354static int attr_set_size_res(struct ntfs_inode *ni, struct ATTRIB *attr, 355 struct ATTR_LIST_ENTRY *le, struct mft_inode *mi, 356 u64 new_size, struct runs_tree *run, 357 struct ATTRIB **ins_attr) 358{ 359 struct ntfs_sb_info *sbi = mi->sbi; 360 struct MFT_REC *rec = mi->mrec; 361 u32 used = le32_to_cpu(rec->used); 362 u32 asize = le32_to_cpu(attr->size); 363 u32 aoff = PtrOffset(rec, attr); 364 u32 rsize = le32_to_cpu(attr->res.data_size); 365 u32 tail = used - aoff - asize; 366 char *next = Add2Ptr(attr, asize); 367 s64 dsize = ALIGN(new_size, 8) - ALIGN(rsize, 8); 368 369 if (dsize < 0) { 370 memmove(next + dsize, next, tail); 371 } else if (dsize > 0) { 372 if (used + dsize > sbi->max_bytes_per_attr) 373 return attr_make_nonresident(ni, attr, le, mi, new_size, 374 run, ins_attr, NULL); 375 376 memmove(next + dsize, next, tail); 377 memset(next, 0, dsize); 378 } 379 380 if (new_size > rsize) 381 memset(Add2Ptr(resident_data(attr), rsize), 0, 382 new_size - rsize); 383 384 rec->used = cpu_to_le32(used + dsize); 385 attr->size = cpu_to_le32(asize + dsize); 386 attr->res.data_size = cpu_to_le32(new_size); 387 mi->dirty = true; 388 *ins_attr = attr; 389 390 return 0; 391} 392 393/* 394 * attr_set_size - Change the size of attribute. 395 * 396 * Extend: 397 * - Sparse/compressed: No allocated clusters. 398 * - Normal: Append allocated and preallocated new clusters. 399 * Shrink: 400 * - No deallocate if @keep_prealloc is set. 401 */ 402int attr_set_size(struct ntfs_inode *ni, enum ATTR_TYPE type, 403 const __le16 *name, u8 name_len, struct runs_tree *run, 404 u64 new_size, const u64 *new_valid, bool keep_prealloc, 405 struct ATTRIB **ret) 406{ 407 int err = 0; 408 struct ntfs_sb_info *sbi = ni->mi.sbi; 409 u8 cluster_bits = sbi->cluster_bits; 410 bool is_mft = ni->mi.rno == MFT_REC_MFT && type == ATTR_DATA && 411 !name_len; 412 u64 old_valid, old_size, old_alloc, new_alloc, new_alloc_tmp; 413 struct ATTRIB *attr = NULL, *attr_b; 414 struct ATTR_LIST_ENTRY *le, *le_b; 415 struct mft_inode *mi, *mi_b; 416 CLST alen, vcn, lcn, new_alen, old_alen, svcn, evcn; 417 CLST next_svcn, pre_alloc = -1, done = 0; 418 bool is_ext, is_bad = false; 419 bool dirty = false; 420 u32 align; 421 struct MFT_REC *rec; 422 423again: 424 alen = 0; 425 le_b = NULL; 426 attr_b = ni_find_attr(ni, NULL, &le_b, type, name, name_len, NULL, 427 &mi_b); 428 if (!attr_b) { 429 err = -ENOENT; 430 goto bad_inode; 431 } 432 433 if (!attr_b->non_res) { 434 err = attr_set_size_res(ni, attr_b, le_b, mi_b, new_size, run, 435 &attr_b); 436 if (err) 437 return err; 438 439 /* Return if file is still resident. */ 440 if (!attr_b->non_res) { 441 dirty = true; 442 goto ok1; 443 } 444 445 /* Layout of records may be changed, so do a full search. */ 446 goto again; 447 } 448 449 is_ext = is_attr_ext(attr_b); 450 align = sbi->cluster_size; 451 if (is_ext) { 452 align <<= attr_b->nres.c_unit; 453 keep_prealloc = false; 454 } 455 456 old_valid = le64_to_cpu(attr_b->nres.valid_size); 457 old_size = le64_to_cpu(attr_b->nres.data_size); 458 old_alloc = le64_to_cpu(attr_b->nres.alloc_size); 459 460again_1: 461 old_alen = old_alloc >> cluster_bits; 462 463 new_alloc = (new_size + align - 1) & ~(u64)(align - 1); 464 new_alen = new_alloc >> cluster_bits; 465 466 if (keep_prealloc && new_size < old_size) { 467 attr_b->nres.data_size = cpu_to_le64(new_size); 468 mi_b->dirty = dirty = true; 469 goto ok; 470 } 471 472 vcn = old_alen - 1; 473 474 svcn = le64_to_cpu(attr_b->nres.svcn); 475 evcn = le64_to_cpu(attr_b->nres.evcn); 476 477 if (svcn <= vcn && vcn <= evcn) { 478 attr = attr_b; 479 le = le_b; 480 mi = mi_b; 481 } else if (!le_b) { 482 err = -EINVAL; 483 goto bad_inode; 484 } else { 485 le = le_b; 486 attr = ni_find_attr(ni, attr_b, &le, type, name, name_len, &vcn, 487 &mi); 488 if (!attr) { 489 err = -EINVAL; 490 goto bad_inode; 491 } 492 493next_le_1: 494 svcn = le64_to_cpu(attr->nres.svcn); 495 evcn = le64_to_cpu(attr->nres.evcn); 496 } 497 /* 498 * Here we have: 499 * attr,mi,le - last attribute segment (containing 'vcn'). 500 * attr_b,mi_b,le_b - base (primary) attribute segment. 501 */ 502next_le: 503 rec = mi->mrec; 504 err = attr_load_runs(attr, ni, run, NULL); 505 if (err) 506 goto out; 507 508 if (new_size > old_size) { 509 CLST to_allocate; 510 size_t free; 511 512 if (new_alloc <= old_alloc) { 513 attr_b->nres.data_size = cpu_to_le64(new_size); 514 mi_b->dirty = dirty = true; 515 goto ok; 516 } 517 518 /* 519 * Add clusters. In simple case we have to: 520 * - allocate space (vcn, lcn, len) 521 * - update packed run in 'mi' 522 * - update attr->nres.evcn 523 * - update attr_b->nres.data_size/attr_b->nres.alloc_size 524 */ 525 to_allocate = new_alen - old_alen; 526add_alloc_in_same_attr_seg: 527 lcn = 0; 528 if (is_mft) { 529 /* MFT allocates clusters from MFT zone. */ 530 pre_alloc = 0; 531 } else if (is_ext) { 532 /* No preallocate for sparse/compress. */ 533 pre_alloc = 0; 534 } else if (pre_alloc == -1) { 535 pre_alloc = 0; 536 if (type == ATTR_DATA && !name_len && 537 sbi->options->prealloc) { 538 pre_alloc = bytes_to_cluster( 539 sbi, get_pre_allocated( 540 new_size)) - 541 new_alen; 542 } 543 544 /* Get the last LCN to allocate from. */ 545 if (old_alen && 546 !run_lookup_entry(run, vcn, &lcn, NULL, NULL)) { 547 lcn = SPARSE_LCN; 548 } 549 550 if (lcn == SPARSE_LCN) 551 lcn = 0; 552 else if (lcn) 553 lcn += 1; 554 555 free = wnd_zeroes(&sbi->used.bitmap); 556 if (to_allocate > free) { 557 err = -ENOSPC; 558 goto out; 559 } 560 561 if (pre_alloc && to_allocate + pre_alloc > free) 562 pre_alloc = 0; 563 } 564 565 vcn = old_alen; 566 567 if (is_ext) { 568 if (!run_add_entry(run, vcn, SPARSE_LCN, to_allocate, 569 false)) { 570 err = -ENOMEM; 571 goto out; 572 } 573 alen = to_allocate; 574 } else { 575 /* ~3 bytes per fragment. */ 576 err = attr_allocate_clusters( 577 sbi, run, vcn, lcn, to_allocate, &pre_alloc, 578 is_mft ? ALLOCATE_MFT : ALLOCATE_DEF, &alen, 579 is_mft ? 0 : 580 (sbi->record_size - 581 le32_to_cpu(rec->used) + 8) / 582 3 + 583 1, 584 NULL, NULL); 585 if (err) 586 goto out; 587 } 588 589 done += alen; 590 vcn += alen; 591 if (to_allocate > alen) 592 to_allocate -= alen; 593 else 594 to_allocate = 0; 595 596pack_runs: 597 err = mi_pack_runs(mi, attr, run, vcn - svcn); 598 if (err) 599 goto undo_1; 600 601 next_svcn = le64_to_cpu(attr->nres.evcn) + 1; 602 new_alloc_tmp = (u64)next_svcn << cluster_bits; 603 attr_b->nres.alloc_size = cpu_to_le64(new_alloc_tmp); 604 mi_b->dirty = dirty = true; 605 606 if (next_svcn >= vcn && !to_allocate) { 607 /* Normal way. Update attribute and exit. */ 608 attr_b->nres.data_size = cpu_to_le64(new_size); 609 goto ok; 610 } 611 612 /* At least two MFT to avoid recursive loop. */ 613 if (is_mft && next_svcn == vcn && 614 ((u64)done << sbi->cluster_bits) >= 2 * sbi->record_size) { 615 new_size = new_alloc_tmp; 616 attr_b->nres.data_size = attr_b->nres.alloc_size; 617 goto ok; 618 } 619 620 if (le32_to_cpu(rec->used) < sbi->record_size) { 621 old_alen = next_svcn; 622 evcn = old_alen - 1; 623 goto add_alloc_in_same_attr_seg; 624 } 625 626 attr_b->nres.data_size = attr_b->nres.alloc_size; 627 if (new_alloc_tmp < old_valid) 628 attr_b->nres.valid_size = attr_b->nres.data_size; 629 630 if (type == ATTR_LIST) { 631 err = ni_expand_list(ni); 632 if (err) 633 goto undo_2; 634 if (next_svcn < vcn) 635 goto pack_runs; 636 637 /* Layout of records is changed. */ 638 goto again; 639 } 640 641 if (!ni->attr_list.size) { 642 err = ni_create_attr_list(ni); 643 /* In case of error layout of records is not changed. */ 644 if (err) 645 goto undo_2; 646 /* Layout of records is changed. */ 647 } 648 649 if (next_svcn >= vcn) { 650 /* This is MFT data, repeat. */ 651 goto again; 652 } 653 654 /* Insert new attribute segment. */ 655 err = ni_insert_nonresident(ni, type, name, name_len, run, 656 next_svcn, vcn - next_svcn, 657 attr_b->flags, &attr, &mi, NULL); 658 659 /* 660 * Layout of records maybe changed. 661 * Find base attribute to update. 662 */ 663 le_b = NULL; 664 attr_b = ni_find_attr(ni, NULL, &le_b, type, name, name_len, 665 NULL, &mi_b); 666 if (!attr_b) { 667 err = -EINVAL; 668 goto bad_inode; 669 } 670 671 if (err) { 672 /* ni_insert_nonresident failed. */ 673 attr = NULL; 674 goto undo_2; 675 } 676 677 /* keep runs for $MFT::$ATTR_DATA and $MFT::$ATTR_BITMAP. */ 678 if (ni->mi.rno != MFT_REC_MFT) 679 run_truncate_head(run, evcn + 1); 680 681 svcn = le64_to_cpu(attr->nres.svcn); 682 evcn = le64_to_cpu(attr->nres.evcn); 683 684 /* 685 * Attribute is in consistency state. 686 * Save this point to restore to if next steps fail. 687 */ 688 old_valid = old_size = old_alloc = (u64)vcn << cluster_bits; 689 attr_b->nres.valid_size = attr_b->nres.data_size = 690 attr_b->nres.alloc_size = cpu_to_le64(old_size); 691 mi_b->dirty = dirty = true; 692 goto again_1; 693 } 694 695 if (new_size != old_size || 696 (new_alloc != old_alloc && !keep_prealloc)) { 697 /* 698 * Truncate clusters. In simple case we have to: 699 * - update packed run in 'mi' 700 * - update attr->nres.evcn 701 * - update attr_b->nres.data_size/attr_b->nres.alloc_size 702 * - mark and trim clusters as free (vcn, lcn, len) 703 */ 704 CLST dlen = 0; 705 706 vcn = max(svcn, new_alen); 707 new_alloc_tmp = (u64)vcn << cluster_bits; 708 709 if (vcn > svcn) { 710 err = mi_pack_runs(mi, attr, run, vcn - svcn); 711 if (err) 712 goto out; 713 } else if (le && le->vcn) { 714 u16 le_sz = le16_to_cpu(le->size); 715 716 /* 717 * NOTE: List entries for one attribute are always 718 * the same size. We deal with last entry (vcn==0) 719 * and it is not first in entries array 720 * (list entry for std attribute always first). 721 * So it is safe to step back. 722 */ 723 mi_remove_attr(NULL, mi, attr); 724 725 if (!al_remove_le(ni, le)) { 726 err = -EINVAL; 727 goto bad_inode; 728 } 729 730 le = (struct ATTR_LIST_ENTRY *)((u8 *)le - le_sz); 731 } else { 732 attr->nres.evcn = cpu_to_le64((u64)vcn - 1); 733 mi->dirty = true; 734 } 735 736 attr_b->nres.alloc_size = cpu_to_le64(new_alloc_tmp); 737 738 if (vcn == new_alen) { 739 attr_b->nres.data_size = cpu_to_le64(new_size); 740 if (new_size < old_valid) 741 attr_b->nres.valid_size = 742 attr_b->nres.data_size; 743 } else { 744 if (new_alloc_tmp <= 745 le64_to_cpu(attr_b->nres.data_size)) 746 attr_b->nres.data_size = 747 attr_b->nres.alloc_size; 748 if (new_alloc_tmp < 749 le64_to_cpu(attr_b->nres.valid_size)) 750 attr_b->nres.valid_size = 751 attr_b->nres.alloc_size; 752 } 753 mi_b->dirty = dirty = true; 754 755 err = run_deallocate_ex(sbi, run, vcn, evcn - vcn + 1, &dlen, 756 true); 757 if (err) 758 goto out; 759 760 if (is_ext) { 761 /* dlen - really deallocated clusters. */ 762 le64_sub_cpu(&attr_b->nres.total_size, 763 ((u64)dlen << cluster_bits)); 764 } 765 766 run_truncate(run, vcn); 767 768 if (new_alloc_tmp <= new_alloc) 769 goto ok; 770 771 old_size = new_alloc_tmp; 772 vcn = svcn - 1; 773 774 if (le == le_b) { 775 attr = attr_b; 776 mi = mi_b; 777 evcn = svcn - 1; 778 svcn = 0; 779 goto next_le; 780 } 781 782 if (le->type != type || le->name_len != name_len || 783 memcmp(le_name(le), name, name_len * sizeof(short))) { 784 err = -EINVAL; 785 goto bad_inode; 786 } 787 788 err = ni_load_mi(ni, le, &mi); 789 if (err) 790 goto out; 791 792 attr = mi_find_attr(ni, mi, NULL, type, name, name_len, 793 &le->id); 794 if (!attr) { 795 err = -EINVAL; 796 goto bad_inode; 797 } 798 goto next_le_1; 799 } 800 801ok: 802 if (new_valid) { 803 __le64 valid = cpu_to_le64(min(*new_valid, new_size)); 804 805 if (attr_b->nres.valid_size != valid) { 806 attr_b->nres.valid_size = valid; 807 mi_b->dirty = true; 808 } 809 } 810 811ok1: 812 if (ret) 813 *ret = attr_b; 814 815 if (((type == ATTR_DATA && !name_len) || 816 (type == ATTR_ALLOC && name == I30_NAME))) { 817 /* Update inode_set_bytes. */ 818 if (attr_b->non_res) { 819 new_alloc = le64_to_cpu(attr_b->nres.alloc_size); 820 if (inode_get_bytes(&ni->vfs_inode) != new_alloc) { 821 inode_set_bytes(&ni->vfs_inode, new_alloc); 822 dirty = true; 823 } 824 } 825 826 /* Don't forget to update duplicate information in parent. */ 827 if (dirty) { 828 ni->ni_flags |= NI_FLAG_UPDATE_PARENT; 829 mark_inode_dirty(&ni->vfs_inode); 830 } 831 } 832 833 return 0; 834 835undo_2: 836 vcn -= alen; 837 attr_b->nres.data_size = cpu_to_le64(old_size); 838 attr_b->nres.valid_size = cpu_to_le64(old_valid); 839 attr_b->nres.alloc_size = cpu_to_le64(old_alloc); 840 841 /* Restore 'attr' and 'mi'. */ 842 if (attr) 843 goto restore_run; 844 845 if (le64_to_cpu(attr_b->nres.svcn) <= svcn && 846 svcn <= le64_to_cpu(attr_b->nres.evcn)) { 847 attr = attr_b; 848 le = le_b; 849 mi = mi_b; 850 } else if (!le_b) { 851 err = -EINVAL; 852 goto bad_inode; 853 } else { 854 le = le_b; 855 attr = ni_find_attr(ni, attr_b, &le, type, name, name_len, 856 &svcn, &mi); 857 if (!attr) 858 goto bad_inode; 859 } 860 861restore_run: 862 if (mi_pack_runs(mi, attr, run, evcn - svcn + 1)) 863 is_bad = true; 864 865undo_1: 866 run_deallocate_ex(sbi, run, vcn, alen, NULL, false); 867 868 run_truncate(run, vcn); 869out: 870 if (is_bad) { 871bad_inode: 872 _ntfs_bad_inode(&ni->vfs_inode); 873 } 874 return err; 875} 876 877/* 878 * attr_data_get_block - Returns 'lcn' and 'len' for given 'vcn'. 879 * 880 * @new == NULL means just to get current mapping for 'vcn' 881 * @new != NULL means allocate real cluster if 'vcn' maps to hole 882 * @zero - zeroout new allocated clusters 883 * 884 * NOTE: 885 * - @new != NULL is called only for sparsed or compressed attributes. 886 * - new allocated clusters are zeroed via blkdev_issue_zeroout. 887 */ 888int attr_data_get_block(struct ntfs_inode *ni, CLST vcn, CLST clen, CLST *lcn, 889 CLST *len, bool *new, bool zero) 890{ 891 int err = 0; 892 struct runs_tree *run = &ni->file.run; 893 struct ntfs_sb_info *sbi; 894 u8 cluster_bits; 895 struct ATTRIB *attr, *attr_b; 896 struct ATTR_LIST_ENTRY *le, *le_b; 897 struct mft_inode *mi, *mi_b; 898 CLST hint, svcn, to_alloc, evcn1, next_svcn, asize, end, vcn0, alen; 899 CLST alloc, evcn; 900 unsigned fr; 901 u64 total_size, total_size0; 902 int step = 0; 903 904 if (new) 905 *new = false; 906 907 /* Try to find in cache. */ 908 down_read(&ni->file.run_lock); 909 if (!run_lookup_entry(run, vcn, lcn, len, NULL)) 910 *len = 0; 911 up_read(&ni->file.run_lock); 912 913 if (*len && (*lcn != SPARSE_LCN || !new)) 914 return 0; /* Fast normal way without allocation. */ 915 916 /* No cluster in cache or we need to allocate cluster in hole. */ 917 sbi = ni->mi.sbi; 918 cluster_bits = sbi->cluster_bits; 919 920 ni_lock(ni); 921 down_write(&ni->file.run_lock); 922 923 /* Repeat the code above (under write lock). */ 924 if (!run_lookup_entry(run, vcn, lcn, len, NULL)) 925 *len = 0; 926 927 if (*len) { 928 if (*lcn != SPARSE_LCN || !new) 929 goto out; /* normal way without allocation. */ 930 if (clen > *len) 931 clen = *len; 932 } 933 934 le_b = NULL; 935 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b); 936 if (!attr_b) { 937 err = -ENOENT; 938 goto out; 939 } 940 941 if (!attr_b->non_res) { 942 *lcn = RESIDENT_LCN; 943 *len = 1; 944 goto out; 945 } 946 947 asize = le64_to_cpu(attr_b->nres.alloc_size) >> cluster_bits; 948 if (vcn >= asize) { 949 if (new) { 950 err = -EINVAL; 951 } else { 952 *len = 1; 953 *lcn = SPARSE_LCN; 954 } 955 goto out; 956 } 957 958 svcn = le64_to_cpu(attr_b->nres.svcn); 959 evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1; 960 961 attr = attr_b; 962 le = le_b; 963 mi = mi_b; 964 965 if (le_b && (vcn < svcn || evcn1 <= vcn)) { 966 attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn, 967 &mi); 968 if (!attr) { 969 err = -EINVAL; 970 goto out; 971 } 972 svcn = le64_to_cpu(attr->nres.svcn); 973 evcn1 = le64_to_cpu(attr->nres.evcn) + 1; 974 } 975 976 /* Load in cache actual information. */ 977 err = attr_load_runs(attr, ni, run, NULL); 978 if (err) 979 goto out; 980 981 /* Check for compressed frame. */ 982 err = attr_is_frame_compressed(ni, attr_b, vcn >> NTFS_LZNT_CUNIT, 983 &hint, run); 984 if (err) 985 goto out; 986 987 if (hint) { 988 /* if frame is compressed - don't touch it. */ 989 *lcn = COMPRESSED_LCN; 990 /* length to the end of frame. */ 991 *len = NTFS_LZNT_CLUSTERS - (vcn & (NTFS_LZNT_CLUSTERS - 1)); 992 err = 0; 993 goto out; 994 } 995 996 if (!*len) { 997 if (run_lookup_entry(run, vcn, lcn, len, NULL)) { 998 if (*lcn != SPARSE_LCN || !new) 999 goto ok; /* Slow normal way without allocation. */ 1000 1001 if (clen > *len) 1002 clen = *len; 1003 } else if (!new) { 1004 /* Here we may return -ENOENT. 1005 * In any case caller gets zero length. */ 1006 goto ok; 1007 } 1008 } 1009 1010 if (!is_attr_ext(attr_b)) { 1011 /* The code below only for sparsed or compressed attributes. */ 1012 err = -EINVAL; 1013 goto out; 1014 } 1015 1016 vcn0 = vcn; 1017 to_alloc = clen; 1018 fr = (sbi->record_size - le32_to_cpu(mi->mrec->used) + 8) / 3 + 1; 1019 /* Allocate frame aligned clusters. 1020 * ntfs.sys usually uses 16 clusters per frame for sparsed or compressed. 1021 * ntfs3 uses 1 cluster per frame for new created sparsed files. */ 1022 if (attr_b->nres.c_unit) { 1023 CLST clst_per_frame = 1u << attr_b->nres.c_unit; 1024 CLST cmask = ~(clst_per_frame - 1); 1025 1026 /* Get frame aligned vcn and to_alloc. */ 1027 vcn = vcn0 & cmask; 1028 to_alloc = ((vcn0 + clen + clst_per_frame - 1) & cmask) - vcn; 1029 if (fr < clst_per_frame) 1030 fr = clst_per_frame; 1031 zero = true; 1032 1033 /* Check if 'vcn' and 'vcn0' in different attribute segments. */ 1034 if (vcn < svcn || evcn1 <= vcn) { 1035 struct ATTRIB *attr2; 1036 /* Load runs for truncated vcn. */ 1037 attr2 = ni_find_attr(ni, attr_b, &le_b, ATTR_DATA, NULL, 1038 0, &vcn, &mi); 1039 if (!attr2) { 1040 err = -EINVAL; 1041 goto out; 1042 } 1043 evcn1 = le64_to_cpu(attr2->nres.evcn) + 1; 1044 err = attr_load_runs(attr2, ni, run, NULL); 1045 if (err) 1046 goto out; 1047 } 1048 } 1049 1050 if (vcn + to_alloc > asize) 1051 to_alloc = asize - vcn; 1052 1053 /* Get the last LCN to allocate from. */ 1054 hint = 0; 1055 1056 if (vcn > evcn1) { 1057 if (!run_add_entry(run, evcn1, SPARSE_LCN, vcn - evcn1, 1058 false)) { 1059 err = -ENOMEM; 1060 goto out; 1061 } 1062 } else if (vcn && !run_lookup_entry(run, vcn - 1, &hint, NULL, NULL)) { 1063 hint = -1; 1064 } 1065 1066 /* Allocate and zeroout new clusters. */ 1067 err = attr_allocate_clusters(sbi, run, vcn, hint + 1, to_alloc, NULL, 1068 zero ? ALLOCATE_ZERO : ALLOCATE_DEF, &alen, 1069 fr, lcn, len); 1070 if (err) 1071 goto out; 1072 *new = true; 1073 step = 1; 1074 1075 end = vcn + alen; 1076 /* Save 'total_size0' to restore if error. */ 1077 total_size0 = le64_to_cpu(attr_b->nres.total_size); 1078 total_size = total_size0 + ((u64)alen << cluster_bits); 1079 1080 if (vcn != vcn0) { 1081 if (!run_lookup_entry(run, vcn0, lcn, len, NULL)) { 1082 err = -EINVAL; 1083 goto out; 1084 } 1085 if (*lcn == SPARSE_LCN) { 1086 /* Internal error. Should not happened. */ 1087 WARN_ON(1); 1088 err = -EINVAL; 1089 goto out; 1090 } 1091 /* Check case when vcn0 + len overlaps new allocated clusters. */ 1092 if (vcn0 + *len > end) 1093 *len = end - vcn0; 1094 } 1095 1096repack: 1097 err = mi_pack_runs(mi, attr, run, max(end, evcn1) - svcn); 1098 if (err) 1099 goto out; 1100 1101 attr_b->nres.total_size = cpu_to_le64(total_size); 1102 inode_set_bytes(&ni->vfs_inode, total_size); 1103 ni->ni_flags |= NI_FLAG_UPDATE_PARENT; 1104 1105 mi_b->dirty = true; 1106 mark_inode_dirty(&ni->vfs_inode); 1107 1108 /* Stored [vcn : next_svcn) from [vcn : end). */ 1109 next_svcn = le64_to_cpu(attr->nres.evcn) + 1; 1110 1111 if (end <= evcn1) { 1112 if (next_svcn == evcn1) { 1113 /* Normal way. Update attribute and exit. */ 1114 goto ok; 1115 } 1116 /* Add new segment [next_svcn : evcn1 - next_svcn). */ 1117 if (!ni->attr_list.size) { 1118 err = ni_create_attr_list(ni); 1119 if (err) 1120 goto undo1; 1121 /* Layout of records is changed. */ 1122 le_b = NULL; 1123 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 1124 0, NULL, &mi_b); 1125 if (!attr_b) { 1126 err = -ENOENT; 1127 goto out; 1128 } 1129 1130 attr = attr_b; 1131 le = le_b; 1132 mi = mi_b; 1133 goto repack; 1134 } 1135 } 1136 1137 /* 1138 * The code below may require additional cluster (to extend attribute list) 1139 * and / or one MFT record 1140 * It is too complex to undo operations if -ENOSPC occurs deep inside 1141 * in 'ni_insert_nonresident'. 1142 * Return in advance -ENOSPC here if there are no free cluster and no free MFT. 1143 */ 1144 if (!ntfs_check_for_free_space(sbi, 1, 1)) { 1145 /* Undo step 1. */ 1146 err = -ENOSPC; 1147 goto undo1; 1148 } 1149 1150 step = 2; 1151 svcn = evcn1; 1152 1153 /* Estimate next attribute. */ 1154 attr = ni_find_attr(ni, attr, &le, ATTR_DATA, NULL, 0, &svcn, &mi); 1155 1156 if (!attr) { 1157 /* Insert new attribute segment. */ 1158 goto ins_ext; 1159 } 1160 1161 /* Try to update existed attribute segment. */ 1162 alloc = bytes_to_cluster(sbi, le64_to_cpu(attr_b->nres.alloc_size)); 1163 evcn = le64_to_cpu(attr->nres.evcn); 1164 1165 if (end < next_svcn) 1166 end = next_svcn; 1167 while (end > evcn) { 1168 /* Remove segment [svcn : evcn). */ 1169 mi_remove_attr(NULL, mi, attr); 1170 1171 if (!al_remove_le(ni, le)) { 1172 err = -EINVAL; 1173 goto out; 1174 } 1175 1176 if (evcn + 1 >= alloc) { 1177 /* Last attribute segment. */ 1178 evcn1 = evcn + 1; 1179 goto ins_ext; 1180 } 1181 1182 if (ni_load_mi(ni, le, &mi)) { 1183 attr = NULL; 1184 goto out; 1185 } 1186 1187 attr = mi_find_attr(ni, mi, NULL, ATTR_DATA, NULL, 0, &le->id); 1188 if (!attr) { 1189 err = -EINVAL; 1190 goto out; 1191 } 1192 svcn = le64_to_cpu(attr->nres.svcn); 1193 evcn = le64_to_cpu(attr->nres.evcn); 1194 } 1195 1196 if (end < svcn) 1197 end = svcn; 1198 1199 err = attr_load_runs(attr, ni, run, &end); 1200 if (err) 1201 goto out; 1202 1203 evcn1 = evcn + 1; 1204 attr->nres.svcn = cpu_to_le64(next_svcn); 1205 err = mi_pack_runs(mi, attr, run, evcn1 - next_svcn); 1206 if (err) 1207 goto out; 1208 1209 le->vcn = cpu_to_le64(next_svcn); 1210 ni->attr_list.dirty = true; 1211 mi->dirty = true; 1212 next_svcn = le64_to_cpu(attr->nres.evcn) + 1; 1213 1214ins_ext: 1215 if (evcn1 > next_svcn) { 1216 err = ni_insert_nonresident(ni, ATTR_DATA, NULL, 0, run, 1217 next_svcn, evcn1 - next_svcn, 1218 attr_b->flags, &attr, &mi, NULL); 1219 if (err) 1220 goto out; 1221 } 1222ok: 1223 run_truncate_around(run, vcn); 1224out: 1225 if (err && step > 1) { 1226 /* Too complex to restore. */ 1227 _ntfs_bad_inode(&ni->vfs_inode); 1228 } 1229 up_write(&ni->file.run_lock); 1230 ni_unlock(ni); 1231 1232 return err; 1233 1234undo1: 1235 /* Undo step1. */ 1236 attr_b->nres.total_size = cpu_to_le64(total_size0); 1237 inode_set_bytes(&ni->vfs_inode, total_size0); 1238 1239 if (run_deallocate_ex(sbi, run, vcn, alen, NULL, false) || 1240 !run_add_entry(run, vcn, SPARSE_LCN, alen, false) || 1241 mi_pack_runs(mi, attr, run, max(end, evcn1) - svcn)) { 1242 _ntfs_bad_inode(&ni->vfs_inode); 1243 } 1244 goto out; 1245} 1246 1247int attr_data_read_resident(struct ntfs_inode *ni, struct folio *folio) 1248{ 1249 u64 vbo; 1250 struct ATTRIB *attr; 1251 u32 data_size; 1252 size_t len; 1253 1254 attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, NULL); 1255 if (!attr) 1256 return -EINVAL; 1257 1258 if (attr->non_res) 1259 return E_NTFS_NONRESIDENT; 1260 1261 vbo = folio->index << PAGE_SHIFT; 1262 data_size = le32_to_cpu(attr->res.data_size); 1263 if (vbo > data_size) 1264 len = 0; 1265 else 1266 len = min(data_size - vbo, folio_size(folio)); 1267 1268 folio_fill_tail(folio, 0, resident_data(attr) + vbo, len); 1269 folio_mark_uptodate(folio); 1270 1271 return 0; 1272} 1273 1274int attr_data_write_resident(struct ntfs_inode *ni, struct folio *folio) 1275{ 1276 u64 vbo; 1277 struct mft_inode *mi; 1278 struct ATTRIB *attr; 1279 u32 data_size; 1280 1281 attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, &mi); 1282 if (!attr) 1283 return -EINVAL; 1284 1285 if (attr->non_res) { 1286 /* Return special error code to check this case. */ 1287 return E_NTFS_NONRESIDENT; 1288 } 1289 1290 vbo = folio->index << PAGE_SHIFT; 1291 data_size = le32_to_cpu(attr->res.data_size); 1292 if (vbo < data_size) { 1293 char *data = resident_data(attr); 1294 size_t len = min(data_size - vbo, folio_size(folio)); 1295 1296 memcpy_from_folio(data + vbo, folio, 0, len); 1297 mi->dirty = true; 1298 } 1299 ni->i_valid = data_size; 1300 1301 return 0; 1302} 1303 1304/* 1305 * attr_load_runs_vcn - Load runs with VCN. 1306 */ 1307int attr_load_runs_vcn(struct ntfs_inode *ni, enum ATTR_TYPE type, 1308 const __le16 *name, u8 name_len, struct runs_tree *run, 1309 CLST vcn) 1310{ 1311 struct ATTRIB *attr; 1312 int err; 1313 CLST svcn, evcn; 1314 u16 ro; 1315 1316 if (!ni) { 1317 /* Is record corrupted? */ 1318 return -ENOENT; 1319 } 1320 1321 attr = ni_find_attr(ni, NULL, NULL, type, name, name_len, &vcn, NULL); 1322 if (!attr) { 1323 /* Is record corrupted? */ 1324 return -ENOENT; 1325 } 1326 1327 svcn = le64_to_cpu(attr->nres.svcn); 1328 evcn = le64_to_cpu(attr->nres.evcn); 1329 1330 if (evcn < vcn || vcn < svcn) { 1331 /* Is record corrupted? */ 1332 return -EINVAL; 1333 } 1334 1335 ro = le16_to_cpu(attr->nres.run_off); 1336 1337 if (ro > le32_to_cpu(attr->size)) 1338 return -EINVAL; 1339 1340 err = run_unpack_ex(run, ni->mi.sbi, ni->mi.rno, svcn, evcn, svcn, 1341 Add2Ptr(attr, ro), le32_to_cpu(attr->size) - ro); 1342 if (err < 0) 1343 return err; 1344 return 0; 1345} 1346 1347/* 1348 * attr_load_runs_range - Load runs for given range [from to). 1349 */ 1350int attr_load_runs_range(struct ntfs_inode *ni, enum ATTR_TYPE type, 1351 const __le16 *name, u8 name_len, struct runs_tree *run, 1352 u64 from, u64 to) 1353{ 1354 struct ntfs_sb_info *sbi = ni->mi.sbi; 1355 u8 cluster_bits = sbi->cluster_bits; 1356 CLST vcn; 1357 CLST vcn_last = (to - 1) >> cluster_bits; 1358 CLST lcn, clen; 1359 int err = 0; 1360 int retry = 0; 1361 1362 for (vcn = from >> cluster_bits; vcn <= vcn_last; vcn += clen) { 1363 if (!run_lookup_entry(run, vcn, &lcn, &clen, NULL)) { 1364 if (retry != 0) { /* Next run_lookup_entry(vcn) also failed. */ 1365 err = -EINVAL; 1366 break; 1367 } 1368 err = attr_load_runs_vcn(ni, type, name, name_len, run, 1369 vcn); 1370 if (err) 1371 break; 1372 1373 clen = 0; /* Next run_lookup_entry(vcn) must be success. */ 1374 retry++; 1375 } 1376 else 1377 retry = 0; 1378 } 1379 1380 return err; 1381} 1382 1383#ifdef CONFIG_NTFS3_LZX_XPRESS 1384/* 1385 * attr_wof_frame_info 1386 * 1387 * Read header of Xpress/LZX file to get info about frame. 1388 */ 1389int attr_wof_frame_info(struct ntfs_inode *ni, struct ATTRIB *attr, 1390 struct runs_tree *run, u64 frame, u64 frames, 1391 u8 frame_bits, u32 *ondisk_size, u64 *vbo_data) 1392{ 1393 struct ntfs_sb_info *sbi = ni->mi.sbi; 1394 u64 vbo[2], off[2], wof_size; 1395 u32 voff; 1396 u8 bytes_per_off; 1397 char *addr; 1398 struct folio *folio; 1399 int i, err; 1400 __le32 *off32; 1401 __le64 *off64; 1402 1403 if (ni->vfs_inode.i_size < 0x100000000ull) { 1404 /* File starts with array of 32 bit offsets. */ 1405 bytes_per_off = sizeof(__le32); 1406 vbo[1] = frame << 2; 1407 *vbo_data = frames << 2; 1408 } else { 1409 /* File starts with array of 64 bit offsets. */ 1410 bytes_per_off = sizeof(__le64); 1411 vbo[1] = frame << 3; 1412 *vbo_data = frames << 3; 1413 } 1414 1415 /* 1416 * Read 4/8 bytes at [vbo - 4(8)] == offset where compressed frame starts. 1417 * Read 4/8 bytes at [vbo] == offset where compressed frame ends. 1418 */ 1419 if (!attr->non_res) { 1420 if (vbo[1] + bytes_per_off > le32_to_cpu(attr->res.data_size)) { 1421 _ntfs_bad_inode(&ni->vfs_inode); 1422 return -EINVAL; 1423 } 1424 addr = resident_data(attr); 1425 1426 if (bytes_per_off == sizeof(__le32)) { 1427 off32 = Add2Ptr(addr, vbo[1]); 1428 off[0] = vbo[1] ? le32_to_cpu(off32[-1]) : 0; 1429 off[1] = le32_to_cpu(off32[0]); 1430 } else { 1431 off64 = Add2Ptr(addr, vbo[1]); 1432 off[0] = vbo[1] ? le64_to_cpu(off64[-1]) : 0; 1433 off[1] = le64_to_cpu(off64[0]); 1434 } 1435 1436 *vbo_data += off[0]; 1437 *ondisk_size = off[1] - off[0]; 1438 return 0; 1439 } 1440 1441 wof_size = le64_to_cpu(attr->nres.data_size); 1442 down_write(&ni->file.run_lock); 1443 folio = ni->file.offs_folio; 1444 if (!folio) { 1445 folio = folio_alloc(GFP_KERNEL, 0); 1446 if (!folio) { 1447 err = -ENOMEM; 1448 goto out; 1449 } 1450 folio->index = -1; 1451 ni->file.offs_folio = folio; 1452 } 1453 folio_lock(folio); 1454 addr = folio_address(folio); 1455 1456 if (vbo[1]) { 1457 voff = vbo[1] & (PAGE_SIZE - 1); 1458 vbo[0] = vbo[1] - bytes_per_off; 1459 i = 0; 1460 } else { 1461 voff = 0; 1462 vbo[0] = 0; 1463 off[0] = 0; 1464 i = 1; 1465 } 1466 1467 do { 1468 pgoff_t index = vbo[i] >> PAGE_SHIFT; 1469 1470 if (index != folio->index) { 1471 u64 from = vbo[i] & ~(u64)(PAGE_SIZE - 1); 1472 u64 to = min(from + PAGE_SIZE, wof_size); 1473 1474 err = attr_load_runs_range(ni, ATTR_DATA, WOF_NAME, 1475 ARRAY_SIZE(WOF_NAME), run, 1476 from, to); 1477 if (err) 1478 goto out1; 1479 1480 err = ntfs_read_run(sbi, run, addr, from, to - from); 1481 if (err) { 1482 folio->index = -1; 1483 goto out1; 1484 } 1485 folio->index = index; 1486 } 1487 1488 if (i) { 1489 if (bytes_per_off == sizeof(__le32)) { 1490 off32 = Add2Ptr(addr, voff); 1491 off[1] = le32_to_cpu(*off32); 1492 } else { 1493 off64 = Add2Ptr(addr, voff); 1494 off[1] = le64_to_cpu(*off64); 1495 } 1496 } else if (!voff) { 1497 if (bytes_per_off == sizeof(__le32)) { 1498 off32 = Add2Ptr(addr, PAGE_SIZE - sizeof(u32)); 1499 off[0] = le32_to_cpu(*off32); 1500 } else { 1501 off64 = Add2Ptr(addr, PAGE_SIZE - sizeof(u64)); 1502 off[0] = le64_to_cpu(*off64); 1503 } 1504 } else { 1505 /* Two values in one page. */ 1506 if (bytes_per_off == sizeof(__le32)) { 1507 off32 = Add2Ptr(addr, voff); 1508 off[0] = le32_to_cpu(off32[-1]); 1509 off[1] = le32_to_cpu(off32[0]); 1510 } else { 1511 off64 = Add2Ptr(addr, voff); 1512 off[0] = le64_to_cpu(off64[-1]); 1513 off[1] = le64_to_cpu(off64[0]); 1514 } 1515 break; 1516 } 1517 } while (++i < 2); 1518 1519 *vbo_data += off[0]; 1520 *ondisk_size = off[1] - off[0]; 1521 1522out1: 1523 folio_unlock(folio); 1524out: 1525 up_write(&ni->file.run_lock); 1526 return err; 1527} 1528#endif 1529 1530/* 1531 * attr_is_frame_compressed - Used to detect compressed frame. 1532 * 1533 * attr - base (primary) attribute segment. 1534 * run - run to use, usually == &ni->file.run. 1535 * Only base segments contains valid 'attr->nres.c_unit' 1536 */ 1537int attr_is_frame_compressed(struct ntfs_inode *ni, struct ATTRIB *attr, 1538 CLST frame, CLST *clst_data, struct runs_tree *run) 1539{ 1540 int err; 1541 u32 clst_frame; 1542 CLST clen, lcn, vcn, alen, slen, vcn_next; 1543 size_t idx; 1544 1545 *clst_data = 0; 1546 1547 if (!is_attr_compressed(attr)) 1548 return 0; 1549 1550 if (!attr->non_res) 1551 return 0; 1552 1553 clst_frame = 1u << attr->nres.c_unit; 1554 vcn = frame * clst_frame; 1555 1556 if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx)) { 1557 err = attr_load_runs_vcn(ni, attr->type, attr_name(attr), 1558 attr->name_len, run, vcn); 1559 if (err) 1560 return err; 1561 1562 if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx)) 1563 return -EINVAL; 1564 } 1565 1566 if (lcn == SPARSE_LCN) { 1567 /* Sparsed frame. */ 1568 return 0; 1569 } 1570 1571 if (clen >= clst_frame) { 1572 /* 1573 * The frame is not compressed 'cause 1574 * it does not contain any sparse clusters. 1575 */ 1576 *clst_data = clst_frame; 1577 return 0; 1578 } 1579 1580 alen = bytes_to_cluster(ni->mi.sbi, le64_to_cpu(attr->nres.alloc_size)); 1581 slen = 0; 1582 *clst_data = clen; 1583 1584 /* 1585 * The frame is compressed if *clst_data + slen >= clst_frame. 1586 * Check next fragments. 1587 */ 1588 while ((vcn += clen) < alen) { 1589 vcn_next = vcn; 1590 1591 if (!run_get_entry(run, ++idx, &vcn, &lcn, &clen) || 1592 vcn_next != vcn) { 1593 err = attr_load_runs_vcn(ni, attr->type, 1594 attr_name(attr), 1595 attr->name_len, run, vcn_next); 1596 if (err) 1597 return err; 1598 vcn = vcn_next; 1599 1600 if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx)) 1601 return -EINVAL; 1602 } 1603 1604 if (lcn == SPARSE_LCN) { 1605 slen += clen; 1606 } else { 1607 if (slen) { 1608 /* 1609 * Data_clusters + sparse_clusters = 1610 * not enough for frame. 1611 */ 1612 return -EINVAL; 1613 } 1614 *clst_data += clen; 1615 } 1616 1617 if (*clst_data + slen >= clst_frame) { 1618 if (!slen) { 1619 /* 1620 * There is no sparsed clusters in this frame 1621 * so it is not compressed. 1622 */ 1623 *clst_data = clst_frame; 1624 } else { 1625 /* Frame is compressed. */ 1626 } 1627 break; 1628 } 1629 } 1630 1631 return 0; 1632} 1633 1634/* 1635 * attr_allocate_frame - Allocate/free clusters for @frame. 1636 * 1637 * Assumed: down_write(&ni->file.run_lock); 1638 */ 1639int attr_allocate_frame(struct ntfs_inode *ni, CLST frame, size_t compr_size, 1640 u64 new_valid) 1641{ 1642 int err = 0; 1643 struct runs_tree *run = &ni->file.run; 1644 struct ntfs_sb_info *sbi = ni->mi.sbi; 1645 struct ATTRIB *attr = NULL, *attr_b; 1646 struct ATTR_LIST_ENTRY *le, *le_b; 1647 struct mft_inode *mi, *mi_b; 1648 CLST svcn, evcn1, next_svcn, len; 1649 CLST vcn, end, clst_data; 1650 u64 total_size, valid_size, data_size; 1651 1652 le_b = NULL; 1653 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b); 1654 if (!attr_b) 1655 return -ENOENT; 1656 1657 if (!is_attr_ext(attr_b)) 1658 return -EINVAL; 1659 1660 vcn = frame << NTFS_LZNT_CUNIT; 1661 total_size = le64_to_cpu(attr_b->nres.total_size); 1662 1663 svcn = le64_to_cpu(attr_b->nres.svcn); 1664 evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1; 1665 data_size = le64_to_cpu(attr_b->nres.data_size); 1666 1667 if (svcn <= vcn && vcn < evcn1) { 1668 attr = attr_b; 1669 le = le_b; 1670 mi = mi_b; 1671 } else if (!le_b) { 1672 err = -EINVAL; 1673 goto out; 1674 } else { 1675 le = le_b; 1676 attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn, 1677 &mi); 1678 if (!attr) { 1679 err = -EINVAL; 1680 goto out; 1681 } 1682 svcn = le64_to_cpu(attr->nres.svcn); 1683 evcn1 = le64_to_cpu(attr->nres.evcn) + 1; 1684 } 1685 1686 err = attr_load_runs(attr, ni, run, NULL); 1687 if (err) 1688 goto out; 1689 1690 err = attr_is_frame_compressed(ni, attr_b, frame, &clst_data, run); 1691 if (err) 1692 goto out; 1693 1694 total_size -= (u64)clst_data << sbi->cluster_bits; 1695 1696 len = bytes_to_cluster(sbi, compr_size); 1697 1698 if (len == clst_data) 1699 goto out; 1700 1701 if (len < clst_data) { 1702 err = run_deallocate_ex(sbi, run, vcn + len, clst_data - len, 1703 NULL, true); 1704 if (err) 1705 goto out; 1706 1707 if (!run_add_entry(run, vcn + len, SPARSE_LCN, clst_data - len, 1708 false)) { 1709 err = -ENOMEM; 1710 goto out; 1711 } 1712 end = vcn + clst_data; 1713 /* Run contains updated range [vcn + len : end). */ 1714 } else { 1715 CLST alen, hint = 0; 1716 /* Get the last LCN to allocate from. */ 1717 if (vcn + clst_data && 1718 !run_lookup_entry(run, vcn + clst_data - 1, &hint, NULL, 1719 NULL)) { 1720 hint = -1; 1721 } 1722 1723 err = attr_allocate_clusters(sbi, run, vcn + clst_data, 1724 hint + 1, len - clst_data, NULL, 1725 ALLOCATE_DEF, &alen, 0, NULL, 1726 NULL); 1727 if (err) 1728 goto out; 1729 1730 end = vcn + len; 1731 /* Run contains updated range [vcn + clst_data : end). */ 1732 } 1733 1734 total_size += (u64)len << sbi->cluster_bits; 1735 1736repack: 1737 err = mi_pack_runs(mi, attr, run, max(end, evcn1) - svcn); 1738 if (err) 1739 goto out; 1740 1741 attr_b->nres.total_size = cpu_to_le64(total_size); 1742 inode_set_bytes(&ni->vfs_inode, total_size); 1743 ni->ni_flags |= NI_FLAG_UPDATE_PARENT; 1744 1745 mi_b->dirty = true; 1746 mark_inode_dirty(&ni->vfs_inode); 1747 1748 /* Stored [vcn : next_svcn) from [vcn : end). */ 1749 next_svcn = le64_to_cpu(attr->nres.evcn) + 1; 1750 1751 if (end <= evcn1) { 1752 if (next_svcn == evcn1) { 1753 /* Normal way. Update attribute and exit. */ 1754 goto ok; 1755 } 1756 /* Add new segment [next_svcn : evcn1 - next_svcn). */ 1757 if (!ni->attr_list.size) { 1758 err = ni_create_attr_list(ni); 1759 if (err) 1760 goto out; 1761 /* Layout of records is changed. */ 1762 le_b = NULL; 1763 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 1764 0, NULL, &mi_b); 1765 if (!attr_b) { 1766 err = -ENOENT; 1767 goto out; 1768 } 1769 1770 attr = attr_b; 1771 le = le_b; 1772 mi = mi_b; 1773 goto repack; 1774 } 1775 } 1776 1777 svcn = evcn1; 1778 1779 /* Estimate next attribute. */ 1780 attr = ni_find_attr(ni, attr, &le, ATTR_DATA, NULL, 0, &svcn, &mi); 1781 1782 if (attr) { 1783 CLST alloc = bytes_to_cluster( 1784 sbi, le64_to_cpu(attr_b->nres.alloc_size)); 1785 CLST evcn = le64_to_cpu(attr->nres.evcn); 1786 1787 if (end < next_svcn) 1788 end = next_svcn; 1789 while (end > evcn) { 1790 /* Remove segment [svcn : evcn). */ 1791 mi_remove_attr(NULL, mi, attr); 1792 1793 if (!al_remove_le(ni, le)) { 1794 err = -EINVAL; 1795 goto out; 1796 } 1797 1798 if (evcn + 1 >= alloc) { 1799 /* Last attribute segment. */ 1800 evcn1 = evcn + 1; 1801 goto ins_ext; 1802 } 1803 1804 if (ni_load_mi(ni, le, &mi)) { 1805 attr = NULL; 1806 goto out; 1807 } 1808 1809 attr = mi_find_attr(ni, mi, NULL, ATTR_DATA, NULL, 0, 1810 &le->id); 1811 if (!attr) { 1812 err = -EINVAL; 1813 goto out; 1814 } 1815 svcn = le64_to_cpu(attr->nres.svcn); 1816 evcn = le64_to_cpu(attr->nres.evcn); 1817 } 1818 1819 if (end < svcn) 1820 end = svcn; 1821 1822 err = attr_load_runs(attr, ni, run, &end); 1823 if (err) 1824 goto out; 1825 1826 evcn1 = evcn + 1; 1827 attr->nres.svcn = cpu_to_le64(next_svcn); 1828 err = mi_pack_runs(mi, attr, run, evcn1 - next_svcn); 1829 if (err) 1830 goto out; 1831 1832 le->vcn = cpu_to_le64(next_svcn); 1833 ni->attr_list.dirty = true; 1834 mi->dirty = true; 1835 1836 next_svcn = le64_to_cpu(attr->nres.evcn) + 1; 1837 } 1838ins_ext: 1839 if (evcn1 > next_svcn) { 1840 err = ni_insert_nonresident(ni, ATTR_DATA, NULL, 0, run, 1841 next_svcn, evcn1 - next_svcn, 1842 attr_b->flags, &attr, &mi, NULL); 1843 if (err) 1844 goto out; 1845 } 1846ok: 1847 run_truncate_around(run, vcn); 1848out: 1849 if (attr_b) { 1850 if (new_valid > data_size) 1851 new_valid = data_size; 1852 1853 valid_size = le64_to_cpu(attr_b->nres.valid_size); 1854 if (new_valid != valid_size) { 1855 attr_b->nres.valid_size = cpu_to_le64(valid_size); 1856 mi_b->dirty = true; 1857 } 1858 } 1859 1860 return err; 1861} 1862 1863/* 1864 * attr_collapse_range - Collapse range in file. 1865 */ 1866int attr_collapse_range(struct ntfs_inode *ni, u64 vbo, u64 bytes) 1867{ 1868 int err = 0; 1869 struct runs_tree *run = &ni->file.run; 1870 struct ntfs_sb_info *sbi = ni->mi.sbi; 1871 struct ATTRIB *attr = NULL, *attr_b; 1872 struct ATTR_LIST_ENTRY *le, *le_b; 1873 struct mft_inode *mi, *mi_b; 1874 CLST svcn, evcn1, len, dealloc, alen, done; 1875 CLST vcn, end; 1876 u64 valid_size, data_size, alloc_size, total_size; 1877 u32 mask; 1878 __le16 a_flags; 1879 1880 if (!bytes) 1881 return 0; 1882 1883 le_b = NULL; 1884 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b); 1885 if (!attr_b) 1886 return -ENOENT; 1887 1888 if (!attr_b->non_res) { 1889 /* Attribute is resident. Nothing to do? */ 1890 return 0; 1891 } 1892 1893 data_size = le64_to_cpu(attr_b->nres.data_size); 1894 alloc_size = le64_to_cpu(attr_b->nres.alloc_size); 1895 a_flags = attr_b->flags; 1896 1897 if (is_attr_ext(attr_b)) { 1898 total_size = le64_to_cpu(attr_b->nres.total_size); 1899 mask = (sbi->cluster_size << attr_b->nres.c_unit) - 1; 1900 } else { 1901 total_size = alloc_size; 1902 mask = sbi->cluster_mask; 1903 } 1904 1905 if ((vbo & mask) || (bytes & mask)) { 1906 /* Allow to collapse only cluster aligned ranges. */ 1907 return -EINVAL; 1908 } 1909 1910 if (vbo > data_size) 1911 return -EINVAL; 1912 1913 down_write(&ni->file.run_lock); 1914 1915 if (vbo + bytes >= data_size) { 1916 u64 new_valid = min(ni->i_valid, vbo); 1917 1918 /* Simple truncate file at 'vbo'. */ 1919 truncate_setsize(&ni->vfs_inode, vbo); 1920 err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, vbo, 1921 &new_valid, true, NULL); 1922 1923 if (!err && new_valid < ni->i_valid) 1924 ni->i_valid = new_valid; 1925 1926 goto out; 1927 } 1928 1929 /* 1930 * Enumerate all attribute segments and collapse. 1931 */ 1932 alen = alloc_size >> sbi->cluster_bits; 1933 vcn = vbo >> sbi->cluster_bits; 1934 len = bytes >> sbi->cluster_bits; 1935 end = vcn + len; 1936 dealloc = 0; 1937 done = 0; 1938 1939 svcn = le64_to_cpu(attr_b->nres.svcn); 1940 evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1; 1941 1942 if (svcn <= vcn && vcn < evcn1) { 1943 attr = attr_b; 1944 le = le_b; 1945 mi = mi_b; 1946 goto check_seg; 1947 } 1948 1949 if (!le_b) { 1950 err = -EINVAL; 1951 goto out; 1952 } 1953 1954 le = le_b; 1955 attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn, &mi); 1956 if (!attr) { 1957 err = -EINVAL; 1958 goto out; 1959 } 1960 1961 for (;;) { 1962 CLST vcn1, eat, next_svcn; 1963 1964 svcn = le64_to_cpu(attr->nres.svcn); 1965 evcn1 = le64_to_cpu(attr->nres.evcn) + 1; 1966 1967check_seg: 1968 if (svcn >= end) { 1969 /* Shift VCN- */ 1970 attr->nres.svcn = cpu_to_le64(svcn - len); 1971 attr->nres.evcn = cpu_to_le64(evcn1 - 1 - len); 1972 if (le) { 1973 le->vcn = attr->nres.svcn; 1974 ni->attr_list.dirty = true; 1975 } 1976 mi->dirty = true; 1977 goto next_attr; 1978 } 1979 1980 run_truncate(run, 0); 1981 err = attr_load_runs(attr, ni, run, &svcn); 1982 if (err) 1983 goto out; 1984 1985 vcn1 = vcn + done; /* original vcn in attr/run. */ 1986 eat = min(end, evcn1) - vcn1; 1987 1988 err = run_deallocate_ex(sbi, run, vcn1, eat, &dealloc, true); 1989 if (err) 1990 goto out; 1991 1992 if (svcn + eat < evcn1) { 1993 /* Collapse a part of this attribute segment. */ 1994 1995 if (!run_collapse_range(run, vcn1, eat, done)) { 1996 err = -ENOMEM; 1997 goto out; 1998 } 1999 2000 if (svcn >= vcn) { 2001 /* Shift VCN */ 2002 attr->nres.svcn = cpu_to_le64(vcn); 2003 if (le && attr->nres.svcn != le->vcn) { 2004 le->vcn = attr->nres.svcn; 2005 ni->attr_list.dirty = true; 2006 } 2007 } 2008 2009 err = mi_pack_runs(mi, attr, run, evcn1 - svcn - eat); 2010 if (err) 2011 goto out; 2012 2013 next_svcn = le64_to_cpu(attr->nres.evcn) + 1; 2014 if (next_svcn + eat + done < evcn1) { 2015 err = ni_insert_nonresident( 2016 ni, ATTR_DATA, NULL, 0, run, next_svcn, 2017 evcn1 - eat - next_svcn, a_flags, &attr, 2018 &mi, &le); 2019 if (err) 2020 goto out; 2021 2022 /* Layout of records maybe changed. */ 2023 attr_b = NULL; 2024 } 2025 2026 /* Free all allocated memory. */ 2027 run_truncate(run, 0); 2028 done += eat; 2029 } else { 2030 u16 le_sz; 2031 2032 /* Delete this attribute segment. */ 2033 mi_remove_attr(NULL, mi, attr); 2034 if (!le) 2035 break; 2036 2037 le_sz = le16_to_cpu(le->size); 2038 if (!al_remove_le(ni, le)) { 2039 err = -EINVAL; 2040 goto out; 2041 } 2042 2043 done += evcn1 - svcn; 2044 if (evcn1 >= alen) 2045 break; 2046 2047 if (!svcn) { 2048 /* Load next record that contains this attribute. */ 2049 if (ni_load_mi(ni, le, &mi)) { 2050 err = -EINVAL; 2051 goto out; 2052 } 2053 2054 /* Look for required attribute. */ 2055 attr = mi_find_attr(ni, mi, NULL, ATTR_DATA, 2056 NULL, 0, &le->id); 2057 if (!attr) { 2058 err = -EINVAL; 2059 goto out; 2060 } 2061 continue; 2062 } 2063 le = (struct ATTR_LIST_ENTRY *)((u8 *)le - le_sz); 2064 } 2065 2066next_attr: 2067 if (evcn1 >= alen) 2068 break; 2069 2070 attr = ni_enum_attr_ex(ni, attr, &le, &mi); 2071 if (!attr) { 2072 err = -EINVAL; 2073 goto out; 2074 } 2075 } 2076 2077 if (!attr_b) { 2078 le_b = NULL; 2079 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, 2080 &mi_b); 2081 if (!attr_b) { 2082 err = -ENOENT; 2083 goto out; 2084 } 2085 } 2086 2087 data_size -= bytes; 2088 valid_size = ni->i_valid; 2089 if (vbo + bytes <= valid_size) 2090 valid_size -= bytes; 2091 else if (vbo < valid_size) 2092 valid_size = vbo; 2093 2094 attr_b->nres.alloc_size = cpu_to_le64(alloc_size - bytes); 2095 attr_b->nres.data_size = cpu_to_le64(data_size); 2096 attr_b->nres.valid_size = cpu_to_le64(min(valid_size, data_size)); 2097 total_size -= (u64)dealloc << sbi->cluster_bits; 2098 if (is_attr_ext(attr_b)) 2099 attr_b->nres.total_size = cpu_to_le64(total_size); 2100 mi_b->dirty = true; 2101 2102 /* Update inode size. */ 2103 ni->i_valid = valid_size; 2104 i_size_write(&ni->vfs_inode, data_size); 2105 inode_set_bytes(&ni->vfs_inode, total_size); 2106 ni->ni_flags |= NI_FLAG_UPDATE_PARENT; 2107 mark_inode_dirty(&ni->vfs_inode); 2108 2109out: 2110 up_write(&ni->file.run_lock); 2111 if (err) 2112 _ntfs_bad_inode(&ni->vfs_inode); 2113 2114 return err; 2115} 2116 2117/* 2118 * attr_punch_hole 2119 * 2120 * Not for normal files. 2121 */ 2122int attr_punch_hole(struct ntfs_inode *ni, u64 vbo, u64 bytes, u32 *frame_size) 2123{ 2124 int err = 0; 2125 struct runs_tree *run = &ni->file.run; 2126 struct ntfs_sb_info *sbi = ni->mi.sbi; 2127 struct ATTRIB *attr = NULL, *attr_b; 2128 struct ATTR_LIST_ENTRY *le, *le_b; 2129 struct mft_inode *mi, *mi_b; 2130 CLST svcn, evcn1, vcn, len, end, alen, hole, next_svcn; 2131 u64 total_size, alloc_size; 2132 u32 mask; 2133 __le16 a_flags; 2134 struct runs_tree run2; 2135 2136 if (!bytes) 2137 return 0; 2138 2139 le_b = NULL; 2140 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b); 2141 if (!attr_b) 2142 return -ENOENT; 2143 2144 if (!attr_b->non_res) { 2145 u32 data_size = le32_to_cpu(attr_b->res.data_size); 2146 u32 from, to; 2147 2148 if (vbo > data_size) 2149 return 0; 2150 2151 from = vbo; 2152 to = min_t(u64, vbo + bytes, data_size); 2153 memset(Add2Ptr(resident_data(attr_b), from), 0, to - from); 2154 return 0; 2155 } 2156 2157 if (!is_attr_ext(attr_b)) 2158 return -EOPNOTSUPP; 2159 2160 alloc_size = le64_to_cpu(attr_b->nres.alloc_size); 2161 total_size = le64_to_cpu(attr_b->nres.total_size); 2162 2163 if (vbo >= alloc_size) { 2164 /* NOTE: It is allowed. */ 2165 return 0; 2166 } 2167 2168 mask = (sbi->cluster_size << attr_b->nres.c_unit) - 1; 2169 2170 bytes += vbo; 2171 if (bytes > alloc_size) 2172 bytes = alloc_size; 2173 bytes -= vbo; 2174 2175 if ((vbo & mask) || (bytes & mask)) { 2176 /* We have to zero a range(s). */ 2177 if (frame_size == NULL) { 2178 /* Caller insists range is aligned. */ 2179 return -EINVAL; 2180 } 2181 *frame_size = mask + 1; 2182 return E_NTFS_NOTALIGNED; 2183 } 2184 2185 down_write(&ni->file.run_lock); 2186 run_init(&run2); 2187 run_truncate(run, 0); 2188 2189 /* 2190 * Enumerate all attribute segments and punch hole where necessary. 2191 */ 2192 alen = alloc_size >> sbi->cluster_bits; 2193 vcn = vbo >> sbi->cluster_bits; 2194 len = bytes >> sbi->cluster_bits; 2195 end = vcn + len; 2196 hole = 0; 2197 2198 svcn = le64_to_cpu(attr_b->nres.svcn); 2199 evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1; 2200 a_flags = attr_b->flags; 2201 2202 if (svcn <= vcn && vcn < evcn1) { 2203 attr = attr_b; 2204 le = le_b; 2205 mi = mi_b; 2206 } else if (!le_b) { 2207 err = -EINVAL; 2208 goto bad_inode; 2209 } else { 2210 le = le_b; 2211 attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn, 2212 &mi); 2213 if (!attr) { 2214 err = -EINVAL; 2215 goto bad_inode; 2216 } 2217 2218 svcn = le64_to_cpu(attr->nres.svcn); 2219 evcn1 = le64_to_cpu(attr->nres.evcn) + 1; 2220 } 2221 2222 while (svcn < end) { 2223 CLST vcn1, zero, hole2 = hole; 2224 2225 err = attr_load_runs(attr, ni, run, &svcn); 2226 if (err) 2227 goto done; 2228 vcn1 = max(vcn, svcn); 2229 zero = min(end, evcn1) - vcn1; 2230 2231 /* 2232 * Check range [vcn1 + zero). 2233 * Calculate how many clusters there are. 2234 * Don't do any destructive actions. 2235 */ 2236 err = run_deallocate_ex(NULL, run, vcn1, zero, &hole2, false); 2237 if (err) 2238 goto done; 2239 2240 /* Check if required range is already hole. */ 2241 if (hole2 == hole) 2242 goto next_attr; 2243 2244 /* Make a clone of run to undo. */ 2245 err = run_clone(run, &run2); 2246 if (err) 2247 goto done; 2248 2249 /* Make a hole range (sparse) [vcn1 + zero). */ 2250 if (!run_add_entry(run, vcn1, SPARSE_LCN, zero, false)) { 2251 err = -ENOMEM; 2252 goto done; 2253 } 2254 2255 /* Update run in attribute segment. */ 2256 err = mi_pack_runs(mi, attr, run, evcn1 - svcn); 2257 if (err) 2258 goto done; 2259 next_svcn = le64_to_cpu(attr->nres.evcn) + 1; 2260 if (next_svcn < evcn1) { 2261 /* Insert new attribute segment. */ 2262 err = ni_insert_nonresident(ni, ATTR_DATA, NULL, 0, run, 2263 next_svcn, 2264 evcn1 - next_svcn, a_flags, 2265 &attr, &mi, &le); 2266 if (err) 2267 goto undo_punch; 2268 2269 /* Layout of records maybe changed. */ 2270 attr_b = NULL; 2271 } 2272 2273 /* Real deallocate. Should not fail. */ 2274 run_deallocate_ex(sbi, &run2, vcn1, zero, &hole, true); 2275 2276next_attr: 2277 /* Free all allocated memory. */ 2278 run_truncate(run, 0); 2279 2280 if (evcn1 >= alen) 2281 break; 2282 2283 /* Get next attribute segment. */ 2284 attr = ni_enum_attr_ex(ni, attr, &le, &mi); 2285 if (!attr) { 2286 err = -EINVAL; 2287 goto bad_inode; 2288 } 2289 2290 svcn = le64_to_cpu(attr->nres.svcn); 2291 evcn1 = le64_to_cpu(attr->nres.evcn) + 1; 2292 } 2293 2294done: 2295 if (!hole) 2296 goto out; 2297 2298 if (!attr_b) { 2299 attr_b = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, 2300 &mi_b); 2301 if (!attr_b) { 2302 err = -EINVAL; 2303 goto bad_inode; 2304 } 2305 } 2306 2307 total_size -= (u64)hole << sbi->cluster_bits; 2308 attr_b->nres.total_size = cpu_to_le64(total_size); 2309 mi_b->dirty = true; 2310 2311 /* Update inode size. */ 2312 inode_set_bytes(&ni->vfs_inode, total_size); 2313 ni->ni_flags |= NI_FLAG_UPDATE_PARENT; 2314 mark_inode_dirty(&ni->vfs_inode); 2315 2316out: 2317 run_close(&run2); 2318 up_write(&ni->file.run_lock); 2319 return err; 2320 2321bad_inode: 2322 _ntfs_bad_inode(&ni->vfs_inode); 2323 goto out; 2324 2325undo_punch: 2326 /* 2327 * Restore packed runs. 2328 * 'mi_pack_runs' should not fail, cause we restore original. 2329 */ 2330 if (mi_pack_runs(mi, attr, &run2, evcn1 - svcn)) 2331 goto bad_inode; 2332 2333 goto done; 2334} 2335 2336/* 2337 * attr_insert_range - Insert range (hole) in file. 2338 * Not for normal files. 2339 */ 2340int attr_insert_range(struct ntfs_inode *ni, u64 vbo, u64 bytes) 2341{ 2342 int err = 0; 2343 struct runs_tree *run = &ni->file.run; 2344 struct ntfs_sb_info *sbi = ni->mi.sbi; 2345 struct ATTRIB *attr = NULL, *attr_b; 2346 struct ATTR_LIST_ENTRY *le, *le_b; 2347 struct mft_inode *mi, *mi_b; 2348 CLST vcn, svcn, evcn1, len, next_svcn; 2349 u64 data_size, alloc_size; 2350 u32 mask; 2351 __le16 a_flags; 2352 2353 if (!bytes) 2354 return 0; 2355 2356 le_b = NULL; 2357 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b); 2358 if (!attr_b) 2359 return -ENOENT; 2360 2361 if (!is_attr_ext(attr_b)) { 2362 /* It was checked above. See fallocate. */ 2363 return -EOPNOTSUPP; 2364 } 2365 2366 if (!attr_b->non_res) { 2367 data_size = le32_to_cpu(attr_b->res.data_size); 2368 alloc_size = data_size; 2369 mask = sbi->cluster_mask; /* cluster_size - 1 */ 2370 } else { 2371 data_size = le64_to_cpu(attr_b->nres.data_size); 2372 alloc_size = le64_to_cpu(attr_b->nres.alloc_size); 2373 mask = (sbi->cluster_size << attr_b->nres.c_unit) - 1; 2374 } 2375 2376 if (vbo >= data_size) { 2377 /* 2378 * Insert range after the file size is not allowed. 2379 * If the offset is equal to or greater than the end of 2380 * file, an error is returned. For such operations (i.e., inserting 2381 * a hole at the end of file), ftruncate(2) should be used. 2382 */ 2383 return -EINVAL; 2384 } 2385 2386 if ((vbo & mask) || (bytes & mask)) { 2387 /* Allow to insert only frame aligned ranges. */ 2388 return -EINVAL; 2389 } 2390 2391 /* 2392 * valid_size <= data_size <= alloc_size 2393 * Check alloc_size for maximum possible. 2394 */ 2395 if (bytes > sbi->maxbytes_sparse - alloc_size) 2396 return -EFBIG; 2397 2398 vcn = vbo >> sbi->cluster_bits; 2399 len = bytes >> sbi->cluster_bits; 2400 2401 down_write(&ni->file.run_lock); 2402 2403 if (!attr_b->non_res) { 2404 err = attr_set_size(ni, ATTR_DATA, NULL, 0, run, 2405 data_size + bytes, NULL, false, NULL); 2406 2407 le_b = NULL; 2408 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, 2409 &mi_b); 2410 if (!attr_b) { 2411 err = -EINVAL; 2412 goto bad_inode; 2413 } 2414 2415 if (err) 2416 goto out; 2417 2418 if (!attr_b->non_res) { 2419 /* Still resident. */ 2420 char *data = Add2Ptr(attr_b, 2421 le16_to_cpu(attr_b->res.data_off)); 2422 2423 memmove(data + bytes, data, bytes); 2424 memset(data, 0, bytes); 2425 goto done; 2426 } 2427 2428 /* Resident files becomes nonresident. */ 2429 data_size = le64_to_cpu(attr_b->nres.data_size); 2430 alloc_size = le64_to_cpu(attr_b->nres.alloc_size); 2431 } 2432 2433 /* 2434 * Enumerate all attribute segments and shift start vcn. 2435 */ 2436 a_flags = attr_b->flags; 2437 svcn = le64_to_cpu(attr_b->nres.svcn); 2438 evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1; 2439 2440 if (svcn <= vcn && vcn < evcn1) { 2441 attr = attr_b; 2442 le = le_b; 2443 mi = mi_b; 2444 } else if (!le_b) { 2445 err = -EINVAL; 2446 goto bad_inode; 2447 } else { 2448 le = le_b; 2449 attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn, 2450 &mi); 2451 if (!attr) { 2452 err = -EINVAL; 2453 goto bad_inode; 2454 } 2455 2456 svcn = le64_to_cpu(attr->nres.svcn); 2457 evcn1 = le64_to_cpu(attr->nres.evcn) + 1; 2458 } 2459 2460 run_truncate(run, 0); /* clear cached values. */ 2461 err = attr_load_runs(attr, ni, run, NULL); 2462 if (err) 2463 goto out; 2464 2465 if (!run_insert_range(run, vcn, len)) { 2466 err = -ENOMEM; 2467 goto out; 2468 } 2469 2470 /* Try to pack in current record as much as possible. */ 2471 err = mi_pack_runs(mi, attr, run, evcn1 + len - svcn); 2472 if (err) 2473 goto out; 2474 2475 next_svcn = le64_to_cpu(attr->nres.evcn) + 1; 2476 2477 while ((attr = ni_enum_attr_ex(ni, attr, &le, &mi)) && 2478 attr->type == ATTR_DATA && !attr->name_len) { 2479 le64_add_cpu(&attr->nres.svcn, len); 2480 le64_add_cpu(&attr->nres.evcn, len); 2481 if (le) { 2482 le->vcn = attr->nres.svcn; 2483 ni->attr_list.dirty = true; 2484 } 2485 mi->dirty = true; 2486 } 2487 2488 if (next_svcn < evcn1 + len) { 2489 err = ni_insert_nonresident(ni, ATTR_DATA, NULL, 0, run, 2490 next_svcn, evcn1 + len - next_svcn, 2491 a_flags, NULL, NULL, NULL); 2492 2493 le_b = NULL; 2494 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, 2495 &mi_b); 2496 if (!attr_b) { 2497 err = -EINVAL; 2498 goto bad_inode; 2499 } 2500 2501 if (err) { 2502 /* ni_insert_nonresident failed. Try to undo. */ 2503 goto undo_insert_range; 2504 } 2505 } 2506 2507 /* 2508 * Update primary attribute segment. 2509 */ 2510 if (vbo <= ni->i_valid) 2511 ni->i_valid += bytes; 2512 2513 attr_b->nres.data_size = cpu_to_le64(data_size + bytes); 2514 attr_b->nres.alloc_size = cpu_to_le64(alloc_size + bytes); 2515 2516 /* ni->valid may be not equal valid_size (temporary). */ 2517 if (ni->i_valid > data_size + bytes) 2518 attr_b->nres.valid_size = attr_b->nres.data_size; 2519 else 2520 attr_b->nres.valid_size = cpu_to_le64(ni->i_valid); 2521 mi_b->dirty = true; 2522 2523done: 2524 i_size_write(&ni->vfs_inode, ni->vfs_inode.i_size + bytes); 2525 ni->ni_flags |= NI_FLAG_UPDATE_PARENT; 2526 mark_inode_dirty(&ni->vfs_inode); 2527 2528out: 2529 run_truncate(run, 0); /* clear cached values. */ 2530 2531 up_write(&ni->file.run_lock); 2532 2533 return err; 2534 2535bad_inode: 2536 _ntfs_bad_inode(&ni->vfs_inode); 2537 goto out; 2538 2539undo_insert_range: 2540 svcn = le64_to_cpu(attr_b->nres.svcn); 2541 evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1; 2542 2543 if (svcn <= vcn && vcn < evcn1) { 2544 attr = attr_b; 2545 le = le_b; 2546 mi = mi_b; 2547 } else if (!le_b) { 2548 goto bad_inode; 2549 } else { 2550 le = le_b; 2551 attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn, 2552 &mi); 2553 if (!attr) { 2554 goto bad_inode; 2555 } 2556 2557 svcn = le64_to_cpu(attr->nres.svcn); 2558 evcn1 = le64_to_cpu(attr->nres.evcn) + 1; 2559 } 2560 2561 if (attr_load_runs(attr, ni, run, NULL)) 2562 goto bad_inode; 2563 2564 if (!run_collapse_range(run, vcn, len, 0)) 2565 goto bad_inode; 2566 2567 if (mi_pack_runs(mi, attr, run, evcn1 + len - svcn)) 2568 goto bad_inode; 2569 2570 while ((attr = ni_enum_attr_ex(ni, attr, &le, &mi)) && 2571 attr->type == ATTR_DATA && !attr->name_len) { 2572 le64_sub_cpu(&attr->nres.svcn, len); 2573 le64_sub_cpu(&attr->nres.evcn, len); 2574 if (le) { 2575 le->vcn = attr->nres.svcn; 2576 ni->attr_list.dirty = true; 2577 } 2578 mi->dirty = true; 2579 } 2580 2581 goto out; 2582} 2583 2584/* 2585 * attr_force_nonresident 2586 * 2587 * Convert default data attribute into non resident form. 2588 */ 2589int attr_force_nonresident(struct ntfs_inode *ni) 2590{ 2591 int err; 2592 struct ATTRIB *attr; 2593 struct ATTR_LIST_ENTRY *le = NULL; 2594 struct mft_inode *mi; 2595 2596 attr = ni_find_attr(ni, NULL, &le, ATTR_DATA, NULL, 0, NULL, &mi); 2597 if (!attr) { 2598 _ntfs_bad_inode(&ni->vfs_inode); 2599 return -ENOENT; 2600 } 2601 2602 if (attr->non_res) { 2603 /* Already non resident. */ 2604 return 0; 2605 } 2606 2607 down_write(&ni->file.run_lock); 2608 err = attr_make_nonresident(ni, attr, le, mi, 2609 le32_to_cpu(attr->res.data_size), 2610 &ni->file.run, &attr, NULL); 2611 up_write(&ni->file.run_lock); 2612 2613 return err; 2614}
~/
Read-only file preview.