1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
6 * TODO: Merge attr_set_size/attr_data_get_block/attr_allocate_frame?
10 #include <linux/slab.h>
11 #include <linux/kernel.h>
18 * You can set external NTFS_MIN_LOG2_OF_CLUMP/NTFS_MAX_LOG2_OF_CLUMP to manage
19 * preallocate algorithm.
21 #ifndef NTFS_MIN_LOG2_OF_CLUMP
22 #define NTFS_MIN_LOG2_OF_CLUMP 16
25 #ifndef NTFS_MAX_LOG2_OF_CLUMP
26 #define NTFS_MAX_LOG2_OF_CLUMP 26
30 #define NTFS_CLUMP_MIN (1 << (NTFS_MIN_LOG2_OF_CLUMP + 8))
32 #define NTFS_CLUMP_MAX (1ull << (NTFS_MAX_LOG2_OF_CLUMP + 8))
34 static inline u64 get_pre_allocated(u64 size)
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;
47 align_shift = NTFS_MIN_LOG2_OF_CLUMP - 1 +
48 __ffs(size >> (8 + NTFS_MIN_LOG2_OF_CLUMP));
49 clump = 1u << align_shift;
52 ret = (((size + clump - 1) >> align_shift)) << align_shift;
58 * attr_load_runs - Load all runs stored in @attr.
60 static int attr_load_runs(struct ATTRIB *attr, struct ntfs_inode *ni,
61 struct runs_tree *run, const CLST *vcn)
64 CLST svcn = le64_to_cpu(attr->nres.svcn);
65 CLST evcn = le64_to_cpu(attr->nres.evcn);
69 if (svcn >= evcn + 1 || run_is_mapped_full(run, svcn, evcn))
72 if (vcn && (evcn < *vcn || *vcn < svcn))
75 asize = le32_to_cpu(attr->size);
76 run_off = le16_to_cpu(attr->nres.run_off);
81 err = run_unpack_ex(run, ni->mi.sbi, ni->mi.rno, svcn, evcn,
82 vcn ? *vcn : svcn, Add2Ptr(attr, run_off),
91 * run_deallocate_ex - Deallocate clusters.
93 static int run_deallocate_ex(struct ntfs_sb_info *sbi, struct runs_tree *run,
94 CLST vcn, CLST len, CLST *done, bool trim)
97 CLST vcn_next, vcn0 = vcn, lcn, clen, dn = 0;
103 if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx)) {
105 run_truncate(run, vcn0);
119 if (lcn != SPARSE_LCN) {
121 /* mark bitmap range [lcn + clen) as free and trim clusters. */
122 mark_as_free_ex(sbi, lcn, clen, trim);
131 vcn_next = vcn + clen;
132 if (!run_get_entry(run, ++idx, &vcn, &lcn, &clen) ||
134 /* Save memory - don't load entire run. */
147 * attr_allocate_clusters - Find free space, mark it as used and store in @run.
149 int 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)
155 CLST flen, vcn0 = vcn, pre = pre_alloc ? *pre_alloc : 0;
156 size_t cnt = run->count;
159 err = ntfs_look_for_free_space(sbi, lcn, len + pre, &lcn, &flen,
162 if (err == -ENOSPC && pre) {
173 /* Return the first fragment. */
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);
188 if (opt & ALLOCATE_ZERO) {
189 u8 shift = sbi->cluster_bits - SECTOR_SHIFT;
191 err = blkdev_issue_zeroout(sbi->sb->s_bdev,
192 (sector_t)lcn << shift,
193 (sector_t)flen << shift,
201 if (flen >= len || (opt & ALLOCATE_MFT) ||
202 (fr && run->count - cnt >= fr)) {
211 /* Undo 'ntfs_look_for_free_space' */
213 run_deallocate_ex(sbi, run, vcn0, vcn - vcn0, NULL, false);
214 run_truncate(run, vcn0);
221 * attr_make_nonresident
223 * If page is not NULL - it is already contains resident data
224 * and locked (called from ni_write_frame()).
226 int 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)
231 struct ntfs_sb_info *sbi;
232 struct ATTRIB *attr_s;
234 u32 used, asize, rsize, aoff, align;
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;
255 align = sbi->cluster_size;
256 if (is_attr_compressed(attr))
257 align <<= COMPRESSION_UNIT;
258 len = (rsize + align - 1) >> sbi->cluster_bits;
262 /* Make a copy of original attribute. */
263 attr_s = kmemdup(attr, asize, GFP_NOFS);
270 /* Empty resident -> Empty nonresident. */
273 const char *data = resident_data(attr);
275 err = attr_allocate_clusters(sbi, run, 0, 0, len, NULL,
276 ALLOCATE_DEF, &alen, 0, NULL,
282 /* Empty resident -> Non empty nonresident. */
283 } else if (!is_data) {
284 err = ntfs_sb_write_run(sbi, run, 0, data, rsize, 0);
290 page = grab_cache_page(ni->vfs_inode.i_mapping, 0);
295 kaddr = kmap_atomic(page);
296 memcpy(kaddr, data, rsize);
297 memset(kaddr + rsize, 0, PAGE_SIZE - rsize);
298 kunmap_atomic(kaddr);
299 flush_dcache_page(page);
300 SetPageUptodate(page);
301 set_page_dirty(page);
307 /* Remove original attribute. */
309 memmove(attr, Add2Ptr(attr, asize), used - aoff);
310 rec->used = cpu_to_le32(used);
313 al_remove_le(ni, le);
315 err = ni_insert_nonresident(ni, attr_s->type, attr_name(attr_s),
316 attr_s->name_len, run, 0, alen,
317 attr_s->flags, &attr, NULL, NULL);
322 attr->nres.data_size = cpu_to_le64(rsize);
323 attr->nres.valid_size = attr->nres.data_size;
328 ni->ni_flags &= ~NI_FLAG_RESIDENT;
330 /* Resident attribute becomes non resident. */
334 attr = Add2Ptr(rec, aoff);
335 memmove(next, attr, used - aoff);
336 memcpy(attr, attr_s, asize);
337 rec->used = cpu_to_le32(used + asize);
340 /* Undo: do not trim new allocated clusters. */
341 run_deallocate(sbi, run, false);
350 * attr_set_size_res - Helper for attr_set_size().
352 static int attr_set_size_res(struct ntfs_inode *ni, struct ATTRIB *attr,
353 struct ATTR_LIST_ENTRY *le, struct mft_inode *mi,
354 u64 new_size, struct runs_tree *run,
355 struct ATTRIB **ins_attr)
357 struct ntfs_sb_info *sbi = mi->sbi;
358 struct MFT_REC *rec = mi->mrec;
359 u32 used = le32_to_cpu(rec->used);
360 u32 asize = le32_to_cpu(attr->size);
361 u32 aoff = PtrOffset(rec, attr);
362 u32 rsize = le32_to_cpu(attr->res.data_size);
363 u32 tail = used - aoff - asize;
364 char *next = Add2Ptr(attr, asize);
365 s64 dsize = ALIGN(new_size, 8) - ALIGN(rsize, 8);
368 memmove(next + dsize, next, tail);
369 } else if (dsize > 0) {
370 if (used + dsize > sbi->max_bytes_per_attr)
371 return attr_make_nonresident(ni, attr, le, mi, new_size,
372 run, ins_attr, NULL);
374 memmove(next + dsize, next, tail);
375 memset(next, 0, dsize);
378 if (new_size > rsize)
379 memset(Add2Ptr(resident_data(attr), rsize), 0,
382 rec->used = cpu_to_le32(used + dsize);
383 attr->size = cpu_to_le32(asize + dsize);
384 attr->res.data_size = cpu_to_le32(new_size);
392 * attr_set_size - Change the size of attribute.
395 * - Sparse/compressed: No allocated clusters.
396 * - Normal: Append allocated and preallocated new clusters.
398 * - No deallocate if @keep_prealloc is set.
400 int attr_set_size(struct ntfs_inode *ni, enum ATTR_TYPE type,
401 const __le16 *name, u8 name_len, struct runs_tree *run,
402 u64 new_size, const u64 *new_valid, bool keep_prealloc,
406 struct ntfs_sb_info *sbi = ni->mi.sbi;
407 u8 cluster_bits = sbi->cluster_bits;
409 ni->mi.rno == MFT_REC_MFT && type == ATTR_DATA && !name_len;
410 u64 old_valid, old_size, old_alloc, new_alloc, new_alloc_tmp;
411 struct ATTRIB *attr = NULL, *attr_b;
412 struct ATTR_LIST_ENTRY *le, *le_b;
413 struct mft_inode *mi, *mi_b;
414 CLST alen, vcn, lcn, new_alen, old_alen, svcn, evcn;
415 CLST next_svcn, pre_alloc = -1, done = 0;
416 bool is_ext, is_bad = false;
423 attr_b = ni_find_attr(ni, NULL, &le_b, type, name, name_len, NULL,
430 if (!attr_b->non_res) {
431 err = attr_set_size_res(ni, attr_b, le_b, mi_b, new_size, run,
436 /* Return if file is still resident. */
437 if (!attr_b->non_res)
440 /* Layout of records may be changed, so do a full search. */
444 is_ext = is_attr_ext(attr_b);
445 align = sbi->cluster_size;
447 align <<= attr_b->nres.c_unit;
449 old_valid = le64_to_cpu(attr_b->nres.valid_size);
450 old_size = le64_to_cpu(attr_b->nres.data_size);
451 old_alloc = le64_to_cpu(attr_b->nres.alloc_size);
454 old_alen = old_alloc >> cluster_bits;
456 new_alloc = (new_size + align - 1) & ~(u64)(align - 1);
457 new_alen = new_alloc >> cluster_bits;
459 if (keep_prealloc && new_size < old_size) {
460 attr_b->nres.data_size = cpu_to_le64(new_size);
467 svcn = le64_to_cpu(attr_b->nres.svcn);
468 evcn = le64_to_cpu(attr_b->nres.evcn);
470 if (svcn <= vcn && vcn <= evcn) {
479 attr = ni_find_attr(ni, attr_b, &le, type, name, name_len, &vcn,
487 svcn = le64_to_cpu(attr->nres.svcn);
488 evcn = le64_to_cpu(attr->nres.evcn);
492 * attr,mi,le - last attribute segment (containing 'vcn').
493 * attr_b,mi_b,le_b - base (primary) attribute segment.
497 err = attr_load_runs(attr, ni, run, NULL);
501 if (new_size > old_size) {
505 if (new_alloc <= old_alloc) {
506 attr_b->nres.data_size = cpu_to_le64(new_size);
512 * Add clusters. In simple case we have to:
513 * - allocate space (vcn, lcn, len)
514 * - update packed run in 'mi'
515 * - update attr->nres.evcn
516 * - update attr_b->nres.data_size/attr_b->nres.alloc_size
518 to_allocate = new_alen - old_alen;
519 add_alloc_in_same_attr_seg:
522 /* MFT allocates clusters from MFT zone. */
525 /* No preallocate for sparse/compress. */
527 } else if (pre_alloc == -1) {
529 if (type == ATTR_DATA && !name_len &&
530 sbi->options->prealloc) {
534 get_pre_allocated(new_size)) -
538 /* Get the last LCN to allocate from. */
540 !run_lookup_entry(run, vcn, &lcn, NULL, NULL)) {
544 if (lcn == SPARSE_LCN)
549 free = wnd_zeroes(&sbi->used.bitmap);
550 if (to_allocate > free) {
555 if (pre_alloc && to_allocate + pre_alloc > free)
562 if (!run_add_entry(run, vcn, SPARSE_LCN, to_allocate,
569 /* ~3 bytes per fragment. */
570 err = attr_allocate_clusters(
571 sbi, run, vcn, lcn, to_allocate, &pre_alloc,
572 is_mft ? ALLOCATE_MFT : ALLOCATE_DEF, &alen,
574 : (sbi->record_size -
575 le32_to_cpu(rec->used) + 8) /
585 if (to_allocate > alen)
591 err = mi_pack_runs(mi, attr, run, vcn - svcn);
595 next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
596 new_alloc_tmp = (u64)next_svcn << cluster_bits;
597 attr_b->nres.alloc_size = cpu_to_le64(new_alloc_tmp);
600 if (next_svcn >= vcn && !to_allocate) {
601 /* Normal way. Update attribute and exit. */
602 attr_b->nres.data_size = cpu_to_le64(new_size);
606 /* At least two MFT to avoid recursive loop. */
607 if (is_mft && next_svcn == vcn &&
608 ((u64)done << sbi->cluster_bits) >= 2 * sbi->record_size) {
609 new_size = new_alloc_tmp;
610 attr_b->nres.data_size = attr_b->nres.alloc_size;
614 if (le32_to_cpu(rec->used) < sbi->record_size) {
615 old_alen = next_svcn;
617 goto add_alloc_in_same_attr_seg;
620 attr_b->nres.data_size = attr_b->nres.alloc_size;
621 if (new_alloc_tmp < old_valid)
622 attr_b->nres.valid_size = attr_b->nres.data_size;
624 if (type == ATTR_LIST) {
625 err = ni_expand_list(ni);
631 /* Layout of records is changed. */
635 if (!ni->attr_list.size) {
636 err = ni_create_attr_list(ni);
637 /* In case of error layout of records is not changed. */
640 /* Layout of records is changed. */
643 if (next_svcn >= vcn) {
644 /* This is MFT data, repeat. */
648 /* Insert new attribute segment. */
649 err = ni_insert_nonresident(ni, type, name, name_len, run,
650 next_svcn, vcn - next_svcn,
651 attr_b->flags, &attr, &mi, NULL);
654 * Layout of records maybe changed.
655 * Find base attribute to update.
658 attr_b = ni_find_attr(ni, NULL, &le_b, type, name, name_len,
666 /* ni_insert_nonresident failed. */
672 run_truncate_head(run, evcn + 1);
674 svcn = le64_to_cpu(attr->nres.svcn);
675 evcn = le64_to_cpu(attr->nres.evcn);
678 * Attribute is in consistency state.
679 * Save this point to restore to if next steps fail.
681 old_valid = old_size = old_alloc = (u64)vcn << cluster_bits;
682 attr_b->nres.valid_size = attr_b->nres.data_size =
683 attr_b->nres.alloc_size = cpu_to_le64(old_size);
688 if (new_size != old_size ||
689 (new_alloc != old_alloc && !keep_prealloc)) {
691 * Truncate clusters. In simple case we have to:
692 * - update packed run in 'mi'
693 * - update attr->nres.evcn
694 * - update attr_b->nres.data_size/attr_b->nres.alloc_size
695 * - mark and trim clusters as free (vcn, lcn, len)
699 vcn = max(svcn, new_alen);
700 new_alloc_tmp = (u64)vcn << cluster_bits;
703 err = mi_pack_runs(mi, attr, run, vcn - svcn);
706 } else if (le && le->vcn) {
707 u16 le_sz = le16_to_cpu(le->size);
710 * NOTE: List entries for one attribute are always
711 * the same size. We deal with last entry (vcn==0)
712 * and it is not first in entries array
713 * (list entry for std attribute always first).
714 * So it is safe to step back.
716 mi_remove_attr(NULL, mi, attr);
718 if (!al_remove_le(ni, le)) {
723 le = (struct ATTR_LIST_ENTRY *)((u8 *)le - le_sz);
725 attr->nres.evcn = cpu_to_le64((u64)vcn - 1);
729 attr_b->nres.alloc_size = cpu_to_le64(new_alloc_tmp);
731 if (vcn == new_alen) {
732 attr_b->nres.data_size = cpu_to_le64(new_size);
733 if (new_size < old_valid)
734 attr_b->nres.valid_size =
735 attr_b->nres.data_size;
738 le64_to_cpu(attr_b->nres.data_size))
739 attr_b->nres.data_size =
740 attr_b->nres.alloc_size;
742 le64_to_cpu(attr_b->nres.valid_size))
743 attr_b->nres.valid_size =
744 attr_b->nres.alloc_size;
748 err = run_deallocate_ex(sbi, run, vcn, evcn - vcn + 1, &dlen,
754 /* dlen - really deallocated clusters. */
755 le64_sub_cpu(&attr_b->nres.total_size,
756 ((u64)dlen << cluster_bits));
759 run_truncate(run, vcn);
761 if (new_alloc_tmp <= new_alloc)
764 old_size = new_alloc_tmp;
775 if (le->type != type || le->name_len != name_len ||
776 memcmp(le_name(le), name, name_len * sizeof(short))) {
781 err = ni_load_mi(ni, le, &mi);
785 attr = mi_find_attr(mi, NULL, type, name, name_len, &le->id);
795 __le64 valid = cpu_to_le64(min(*new_valid, new_size));
797 if (attr_b->nres.valid_size != valid) {
798 attr_b->nres.valid_size = valid;
807 /* Update inode_set_bytes. */
808 if (((type == ATTR_DATA && !name_len) ||
809 (type == ATTR_ALLOC && name == I30_NAME))) {
812 if (ni->vfs_inode.i_size != new_size) {
813 ni->vfs_inode.i_size = new_size;
817 if (attr_b->non_res) {
818 new_alloc = le64_to_cpu(attr_b->nres.alloc_size);
819 if (inode_get_bytes(&ni->vfs_inode) != new_alloc) {
820 inode_set_bytes(&ni->vfs_inode, new_alloc);
826 ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
827 mark_inode_dirty(&ni->vfs_inode);
835 attr_b->nres.data_size = cpu_to_le64(old_size);
836 attr_b->nres.valid_size = cpu_to_le64(old_valid);
837 attr_b->nres.alloc_size = cpu_to_le64(old_alloc);
839 /* Restore 'attr' and 'mi'. */
843 if (le64_to_cpu(attr_b->nres.svcn) <= svcn &&
844 svcn <= le64_to_cpu(attr_b->nres.evcn)) {
853 attr = ni_find_attr(ni, attr_b, &le, type, name, name_len,
860 if (mi_pack_runs(mi, attr, run, evcn - svcn + 1))
864 run_deallocate_ex(sbi, run, vcn, alen, NULL, false);
866 run_truncate(run, vcn);
870 _ntfs_bad_inode(&ni->vfs_inode);
876 * attr_data_get_block - Returns 'lcn' and 'len' for given 'vcn'.
878 * @new == NULL means just to get current mapping for 'vcn'
879 * @new != NULL means allocate real cluster if 'vcn' maps to hole
880 * @zero - zeroout new allocated clusters
883 * - @new != NULL is called only for sparsed or compressed attributes.
884 * - new allocated clusters are zeroed via blkdev_issue_zeroout.
886 int attr_data_get_block(struct ntfs_inode *ni, CLST vcn, CLST clen, CLST *lcn,
887 CLST *len, bool *new, bool zero)
890 struct runs_tree *run = &ni->file.run;
891 struct ntfs_sb_info *sbi;
893 struct ATTRIB *attr = NULL, *attr_b;
894 struct ATTR_LIST_ENTRY *le, *le_b;
895 struct mft_inode *mi, *mi_b;
896 CLST hint, svcn, to_alloc, evcn1, next_svcn, asize, end, vcn0, alen;
903 /* Try to find in cache. */
904 down_read(&ni->file.run_lock);
905 if (!run_lookup_entry(run, vcn, lcn, len, NULL))
907 up_read(&ni->file.run_lock);
910 if (*lcn != SPARSE_LCN || !new)
911 return 0; /* Fast normal way without allocation. */
912 else if (clen > *len)
916 /* No cluster in cache or we need to allocate cluster in hole. */
918 cluster_bits = sbi->cluster_bits;
921 down_write(&ni->file.run_lock);
924 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
930 if (!attr_b->non_res) {
936 asize = le64_to_cpu(attr_b->nres.alloc_size) >> cluster_bits;
942 svcn = le64_to_cpu(attr_b->nres.svcn);
943 evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
949 if (le_b && (vcn < svcn || evcn1 <= vcn)) {
950 attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
956 svcn = le64_to_cpu(attr->nres.svcn);
957 evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
960 /* Load in cache actual information. */
961 err = attr_load_runs(attr, ni, run, NULL);
966 if (run_lookup_entry(run, vcn, lcn, len, NULL)) {
967 if (*lcn != SPARSE_LCN || !new)
968 goto ok; /* Slow normal way without allocation. */
973 /* Here we may return -ENOENT.
974 * In any case caller gets zero length. */
979 if (!is_attr_ext(attr_b)) {
980 /* The code below only for sparsed or compressed attributes. */
987 fr = (sbi->record_size - le32_to_cpu(mi->mrec->used) + 8) / 3 + 1;
988 /* Allocate frame aligned clusters.
989 * ntfs.sys usually uses 16 clusters per frame for sparsed or compressed.
990 * ntfs3 uses 1 cluster per frame for new created sparsed files. */
991 if (attr_b->nres.c_unit) {
992 CLST clst_per_frame = 1u << attr_b->nres.c_unit;
993 CLST cmask = ~(clst_per_frame - 1);
995 /* Get frame aligned vcn and to_alloc. */
997 to_alloc = ((vcn0 + clen + clst_per_frame - 1) & cmask) - vcn;
998 if (fr < clst_per_frame)
1002 /* Check if 'vcn' and 'vcn0' in different attribute segments. */
1003 if (vcn < svcn || evcn1 <= vcn) {
1004 /* Load attribute for truncated vcn. */
1005 attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0,
1011 svcn = le64_to_cpu(attr->nres.svcn);
1012 evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
1013 err = attr_load_runs(attr, ni, run, NULL);
1019 if (vcn + to_alloc > asize)
1020 to_alloc = asize - vcn;
1022 /* Get the last LCN to allocate from. */
1026 if (!run_add_entry(run, evcn1, SPARSE_LCN, vcn - evcn1,
1031 } else if (vcn && !run_lookup_entry(run, vcn - 1, &hint, NULL, NULL)) {
1035 /* Allocate and zeroout new clusters. */
1036 err = attr_allocate_clusters(sbi, run, vcn, hint + 1, to_alloc, NULL,
1037 zero ? ALLOCATE_ZERO : ALLOCATE_DEF, &alen,
1044 total_size = le64_to_cpu(attr_b->nres.total_size) +
1045 ((u64)alen << cluster_bits);
1048 if (!run_lookup_entry(run, vcn0, lcn, len, NULL)) {
1052 if (*lcn == SPARSE_LCN) {
1053 /* Internal error. Should not happened. */
1058 /* Check case when vcn0 + len overlaps new allocated clusters. */
1059 if (vcn0 + *len > end)
1064 err = mi_pack_runs(mi, attr, run, max(end, evcn1) - svcn);
1068 attr_b->nres.total_size = cpu_to_le64(total_size);
1069 inode_set_bytes(&ni->vfs_inode, total_size);
1070 ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
1073 mark_inode_dirty(&ni->vfs_inode);
1075 /* Stored [vcn : next_svcn) from [vcn : end). */
1076 next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
1079 if (next_svcn == evcn1) {
1080 /* Normal way. Update attribute and exit. */
1083 /* Add new segment [next_svcn : evcn1 - next_svcn). */
1084 if (!ni->attr_list.size) {
1085 err = ni_create_attr_list(ni);
1088 /* Layout of records is changed. */
1090 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL,
1106 /* Estimate next attribute. */
1107 attr = ni_find_attr(ni, attr, &le, ATTR_DATA, NULL, 0, &svcn, &mi);
1110 CLST alloc = bytes_to_cluster(
1111 sbi, le64_to_cpu(attr_b->nres.alloc_size));
1112 CLST evcn = le64_to_cpu(attr->nres.evcn);
1114 if (end < next_svcn)
1116 while (end > evcn) {
1117 /* Remove segment [svcn : evcn). */
1118 mi_remove_attr(NULL, mi, attr);
1120 if (!al_remove_le(ni, le)) {
1125 if (evcn + 1 >= alloc) {
1126 /* Last attribute segment. */
1131 if (ni_load_mi(ni, le, &mi)) {
1136 attr = mi_find_attr(mi, NULL, ATTR_DATA, NULL, 0,
1142 svcn = le64_to_cpu(attr->nres.svcn);
1143 evcn = le64_to_cpu(attr->nres.evcn);
1149 err = attr_load_runs(attr, ni, run, &end);
1154 attr->nres.svcn = cpu_to_le64(next_svcn);
1155 err = mi_pack_runs(mi, attr, run, evcn1 - next_svcn);
1159 le->vcn = cpu_to_le64(next_svcn);
1160 ni->attr_list.dirty = true;
1163 next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
1166 if (evcn1 > next_svcn) {
1167 err = ni_insert_nonresident(ni, ATTR_DATA, NULL, 0, run,
1168 next_svcn, evcn1 - next_svcn,
1169 attr_b->flags, &attr, &mi, NULL);
1174 run_truncate_around(run, vcn);
1176 up_write(&ni->file.run_lock);
1182 int attr_data_read_resident(struct ntfs_inode *ni, struct page *page)
1185 struct ATTRIB *attr;
1188 attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, NULL);
1193 return E_NTFS_NONRESIDENT;
1195 vbo = page->index << PAGE_SHIFT;
1196 data_size = le32_to_cpu(attr->res.data_size);
1197 if (vbo < data_size) {
1198 const char *data = resident_data(attr);
1199 char *kaddr = kmap_atomic(page);
1200 u32 use = data_size - vbo;
1202 if (use > PAGE_SIZE)
1205 memcpy(kaddr, data + vbo, use);
1206 memset(kaddr + use, 0, PAGE_SIZE - use);
1207 kunmap_atomic(kaddr);
1208 flush_dcache_page(page);
1209 SetPageUptodate(page);
1210 } else if (!PageUptodate(page)) {
1211 zero_user_segment(page, 0, PAGE_SIZE);
1212 SetPageUptodate(page);
1218 int attr_data_write_resident(struct ntfs_inode *ni, struct page *page)
1221 struct mft_inode *mi;
1222 struct ATTRIB *attr;
1225 attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, &mi);
1229 if (attr->non_res) {
1230 /* Return special error code to check this case. */
1231 return E_NTFS_NONRESIDENT;
1234 vbo = page->index << PAGE_SHIFT;
1235 data_size = le32_to_cpu(attr->res.data_size);
1236 if (vbo < data_size) {
1237 char *data = resident_data(attr);
1238 char *kaddr = kmap_atomic(page);
1239 u32 use = data_size - vbo;
1241 if (use > PAGE_SIZE)
1243 memcpy(data + vbo, kaddr, use);
1244 kunmap_atomic(kaddr);
1247 ni->i_valid = data_size;
1253 * attr_load_runs_vcn - Load runs with VCN.
1255 int attr_load_runs_vcn(struct ntfs_inode *ni, enum ATTR_TYPE type,
1256 const __le16 *name, u8 name_len, struct runs_tree *run,
1259 struct ATTRIB *attr;
1265 /* Is record corrupted? */
1269 attr = ni_find_attr(ni, NULL, NULL, type, name, name_len, &vcn, NULL);
1271 /* Is record corrupted? */
1275 svcn = le64_to_cpu(attr->nres.svcn);
1276 evcn = le64_to_cpu(attr->nres.evcn);
1278 if (evcn < vcn || vcn < svcn) {
1279 /* Is record corrupted? */
1283 ro = le16_to_cpu(attr->nres.run_off);
1285 if (ro > le32_to_cpu(attr->size))
1288 err = run_unpack_ex(run, ni->mi.sbi, ni->mi.rno, svcn, evcn, svcn,
1289 Add2Ptr(attr, ro), le32_to_cpu(attr->size) - ro);
1296 * attr_load_runs_range - Load runs for given range [from to).
1298 int attr_load_runs_range(struct ntfs_inode *ni, enum ATTR_TYPE type,
1299 const __le16 *name, u8 name_len, struct runs_tree *run,
1302 struct ntfs_sb_info *sbi = ni->mi.sbi;
1303 u8 cluster_bits = sbi->cluster_bits;
1305 CLST vcn_last = (to - 1) >> cluster_bits;
1309 for (vcn = from >> cluster_bits; vcn <= vcn_last; vcn += clen) {
1310 if (!run_lookup_entry(run, vcn, &lcn, &clen, NULL)) {
1311 err = attr_load_runs_vcn(ni, type, name, name_len, run,
1315 clen = 0; /* Next run_lookup_entry(vcn) must be success. */
1322 #ifdef CONFIG_NTFS3_LZX_XPRESS
1324 * attr_wof_frame_info
1326 * Read header of Xpress/LZX file to get info about frame.
1328 int attr_wof_frame_info(struct ntfs_inode *ni, struct ATTRIB *attr,
1329 struct runs_tree *run, u64 frame, u64 frames,
1330 u8 frame_bits, u32 *ondisk_size, u64 *vbo_data)
1332 struct ntfs_sb_info *sbi = ni->mi.sbi;
1333 u64 vbo[2], off[2], wof_size;
1342 if (ni->vfs_inode.i_size < 0x100000000ull) {
1343 /* File starts with array of 32 bit offsets. */
1344 bytes_per_off = sizeof(__le32);
1345 vbo[1] = frame << 2;
1346 *vbo_data = frames << 2;
1348 /* File starts with array of 64 bit offsets. */
1349 bytes_per_off = sizeof(__le64);
1350 vbo[1] = frame << 3;
1351 *vbo_data = frames << 3;
1355 * Read 4/8 bytes at [vbo - 4(8)] == offset where compressed frame starts.
1356 * Read 4/8 bytes at [vbo] == offset where compressed frame ends.
1358 if (!attr->non_res) {
1359 if (vbo[1] + bytes_per_off > le32_to_cpu(attr->res.data_size)) {
1360 ntfs_inode_err(&ni->vfs_inode, "is corrupted");
1363 addr = resident_data(attr);
1365 if (bytes_per_off == sizeof(__le32)) {
1366 off32 = Add2Ptr(addr, vbo[1]);
1367 off[0] = vbo[1] ? le32_to_cpu(off32[-1]) : 0;
1368 off[1] = le32_to_cpu(off32[0]);
1370 off64 = Add2Ptr(addr, vbo[1]);
1371 off[0] = vbo[1] ? le64_to_cpu(off64[-1]) : 0;
1372 off[1] = le64_to_cpu(off64[0]);
1375 *vbo_data += off[0];
1376 *ondisk_size = off[1] - off[0];
1380 wof_size = le64_to_cpu(attr->nres.data_size);
1381 down_write(&ni->file.run_lock);
1382 page = ni->file.offs_page;
1384 page = alloc_page(GFP_KERNEL);
1390 ni->file.offs_page = page;
1393 addr = page_address(page);
1396 voff = vbo[1] & (PAGE_SIZE - 1);
1397 vbo[0] = vbo[1] - bytes_per_off;
1407 pgoff_t index = vbo[i] >> PAGE_SHIFT;
1409 if (index != page->index) {
1410 u64 from = vbo[i] & ~(u64)(PAGE_SIZE - 1);
1411 u64 to = min(from + PAGE_SIZE, wof_size);
1413 err = attr_load_runs_range(ni, ATTR_DATA, WOF_NAME,
1414 ARRAY_SIZE(WOF_NAME), run,
1419 err = ntfs_bio_pages(sbi, run, &page, 1, from,
1420 to - from, REQ_OP_READ);
1425 page->index = index;
1429 if (bytes_per_off == sizeof(__le32)) {
1430 off32 = Add2Ptr(addr, voff);
1431 off[1] = le32_to_cpu(*off32);
1433 off64 = Add2Ptr(addr, voff);
1434 off[1] = le64_to_cpu(*off64);
1437 if (bytes_per_off == sizeof(__le32)) {
1438 off32 = Add2Ptr(addr, PAGE_SIZE - sizeof(u32));
1439 off[0] = le32_to_cpu(*off32);
1441 off64 = Add2Ptr(addr, PAGE_SIZE - sizeof(u64));
1442 off[0] = le64_to_cpu(*off64);
1445 /* Two values in one page. */
1446 if (bytes_per_off == sizeof(__le32)) {
1447 off32 = Add2Ptr(addr, voff);
1448 off[0] = le32_to_cpu(off32[-1]);
1449 off[1] = le32_to_cpu(off32[0]);
1451 off64 = Add2Ptr(addr, voff);
1452 off[0] = le64_to_cpu(off64[-1]);
1453 off[1] = le64_to_cpu(off64[0]);
1459 *vbo_data += off[0];
1460 *ondisk_size = off[1] - off[0];
1465 up_write(&ni->file.run_lock);
1471 * attr_is_frame_compressed - Used to detect compressed frame.
1473 int attr_is_frame_compressed(struct ntfs_inode *ni, struct ATTRIB *attr,
1474 CLST frame, CLST *clst_data)
1478 CLST clen, lcn, vcn, alen, slen, vcn_next;
1480 struct runs_tree *run;
1484 if (!is_attr_compressed(attr))
1490 clst_frame = 1u << attr->nres.c_unit;
1491 vcn = frame * clst_frame;
1492 run = &ni->file.run;
1494 if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx)) {
1495 err = attr_load_runs_vcn(ni, attr->type, attr_name(attr),
1496 attr->name_len, run, vcn);
1500 if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx))
1504 if (lcn == SPARSE_LCN) {
1505 /* Sparsed frame. */
1509 if (clen >= clst_frame) {
1511 * The frame is not compressed 'cause
1512 * it does not contain any sparse clusters.
1514 *clst_data = clst_frame;
1518 alen = bytes_to_cluster(ni->mi.sbi, le64_to_cpu(attr->nres.alloc_size));
1523 * The frame is compressed if *clst_data + slen >= clst_frame.
1524 * Check next fragments.
1526 while ((vcn += clen) < alen) {
1529 if (!run_get_entry(run, ++idx, &vcn, &lcn, &clen) ||
1531 err = attr_load_runs_vcn(ni, attr->type,
1533 attr->name_len, run, vcn_next);
1538 if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx))
1542 if (lcn == SPARSE_LCN) {
1547 * Data_clusters + sparse_clusters =
1548 * not enough for frame.
1555 if (*clst_data + slen >= clst_frame) {
1558 * There is no sparsed clusters in this frame
1559 * so it is not compressed.
1561 *clst_data = clst_frame;
1563 /* Frame is compressed. */
1573 * attr_allocate_frame - Allocate/free clusters for @frame.
1575 * Assumed: down_write(&ni->file.run_lock);
1577 int attr_allocate_frame(struct ntfs_inode *ni, CLST frame, size_t compr_size,
1581 struct runs_tree *run = &ni->file.run;
1582 struct ntfs_sb_info *sbi = ni->mi.sbi;
1583 struct ATTRIB *attr = NULL, *attr_b;
1584 struct ATTR_LIST_ENTRY *le, *le_b;
1585 struct mft_inode *mi, *mi_b;
1586 CLST svcn, evcn1, next_svcn, len;
1587 CLST vcn, end, clst_data;
1588 u64 total_size, valid_size, data_size;
1591 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
1595 if (!is_attr_ext(attr_b))
1598 vcn = frame << NTFS_LZNT_CUNIT;
1599 total_size = le64_to_cpu(attr_b->nres.total_size);
1601 svcn = le64_to_cpu(attr_b->nres.svcn);
1602 evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
1603 data_size = le64_to_cpu(attr_b->nres.data_size);
1605 if (svcn <= vcn && vcn < evcn1) {
1614 attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
1620 svcn = le64_to_cpu(attr->nres.svcn);
1621 evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
1624 err = attr_load_runs(attr, ni, run, NULL);
1628 err = attr_is_frame_compressed(ni, attr_b, frame, &clst_data);
1632 total_size -= (u64)clst_data << sbi->cluster_bits;
1634 len = bytes_to_cluster(sbi, compr_size);
1636 if (len == clst_data)
1639 if (len < clst_data) {
1640 err = run_deallocate_ex(sbi, run, vcn + len, clst_data - len,
1645 if (!run_add_entry(run, vcn + len, SPARSE_LCN, clst_data - len,
1650 end = vcn + clst_data;
1651 /* Run contains updated range [vcn + len : end). */
1653 CLST alen, hint = 0;
1654 /* Get the last LCN to allocate from. */
1655 if (vcn + clst_data &&
1656 !run_lookup_entry(run, vcn + clst_data - 1, &hint, NULL,
1661 err = attr_allocate_clusters(sbi, run, vcn + clst_data,
1662 hint + 1, len - clst_data, NULL,
1663 ALLOCATE_DEF, &alen, 0, NULL,
1669 /* Run contains updated range [vcn + clst_data : end). */
1672 total_size += (u64)len << sbi->cluster_bits;
1675 err = mi_pack_runs(mi, attr, run, max(end, evcn1) - svcn);
1679 attr_b->nres.total_size = cpu_to_le64(total_size);
1680 inode_set_bytes(&ni->vfs_inode, total_size);
1683 mark_inode_dirty(&ni->vfs_inode);
1685 /* Stored [vcn : next_svcn) from [vcn : end). */
1686 next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
1689 if (next_svcn == evcn1) {
1690 /* Normal way. Update attribute and exit. */
1693 /* Add new segment [next_svcn : evcn1 - next_svcn). */
1694 if (!ni->attr_list.size) {
1695 err = ni_create_attr_list(ni);
1698 /* Layout of records is changed. */
1700 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL,
1716 /* Estimate next attribute. */
1717 attr = ni_find_attr(ni, attr, &le, ATTR_DATA, NULL, 0, &svcn, &mi);
1720 CLST alloc = bytes_to_cluster(
1721 sbi, le64_to_cpu(attr_b->nres.alloc_size));
1722 CLST evcn = le64_to_cpu(attr->nres.evcn);
1724 if (end < next_svcn)
1726 while (end > evcn) {
1727 /* Remove segment [svcn : evcn). */
1728 mi_remove_attr(NULL, mi, attr);
1730 if (!al_remove_le(ni, le)) {
1735 if (evcn + 1 >= alloc) {
1736 /* Last attribute segment. */
1741 if (ni_load_mi(ni, le, &mi)) {
1746 attr = mi_find_attr(mi, NULL, ATTR_DATA, NULL, 0,
1752 svcn = le64_to_cpu(attr->nres.svcn);
1753 evcn = le64_to_cpu(attr->nres.evcn);
1759 err = attr_load_runs(attr, ni, run, &end);
1764 attr->nres.svcn = cpu_to_le64(next_svcn);
1765 err = mi_pack_runs(mi, attr, run, evcn1 - next_svcn);
1769 le->vcn = cpu_to_le64(next_svcn);
1770 ni->attr_list.dirty = true;
1773 next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
1776 if (evcn1 > next_svcn) {
1777 err = ni_insert_nonresident(ni, ATTR_DATA, NULL, 0, run,
1778 next_svcn, evcn1 - next_svcn,
1779 attr_b->flags, &attr, &mi, NULL);
1784 run_truncate_around(run, vcn);
1786 if (new_valid > data_size)
1787 new_valid = data_size;
1789 valid_size = le64_to_cpu(attr_b->nres.valid_size);
1790 if (new_valid != valid_size) {
1791 attr_b->nres.valid_size = cpu_to_le64(valid_size);
1799 * attr_collapse_range - Collapse range in file.
1801 int attr_collapse_range(struct ntfs_inode *ni, u64 vbo, u64 bytes)
1804 struct runs_tree *run = &ni->file.run;
1805 struct ntfs_sb_info *sbi = ni->mi.sbi;
1806 struct ATTRIB *attr = NULL, *attr_b;
1807 struct ATTR_LIST_ENTRY *le, *le_b;
1808 struct mft_inode *mi, *mi_b;
1809 CLST svcn, evcn1, len, dealloc, alen;
1811 u64 valid_size, data_size, alloc_size, total_size;
1819 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
1823 if (!attr_b->non_res) {
1824 /* Attribute is resident. Nothing to do? */
1828 data_size = le64_to_cpu(attr_b->nres.data_size);
1829 alloc_size = le64_to_cpu(attr_b->nres.alloc_size);
1830 a_flags = attr_b->flags;
1832 if (is_attr_ext(attr_b)) {
1833 total_size = le64_to_cpu(attr_b->nres.total_size);
1834 mask = (sbi->cluster_size << attr_b->nres.c_unit) - 1;
1836 total_size = alloc_size;
1837 mask = sbi->cluster_mask;
1840 if ((vbo & mask) || (bytes & mask)) {
1841 /* Allow to collapse only cluster aligned ranges. */
1845 if (vbo > data_size)
1848 down_write(&ni->file.run_lock);
1850 if (vbo + bytes >= data_size) {
1851 u64 new_valid = min(ni->i_valid, vbo);
1853 /* Simple truncate file at 'vbo'. */
1854 truncate_setsize(&ni->vfs_inode, vbo);
1855 err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, vbo,
1856 &new_valid, true, NULL);
1858 if (!err && new_valid < ni->i_valid)
1859 ni->i_valid = new_valid;
1865 * Enumerate all attribute segments and collapse.
1867 alen = alloc_size >> sbi->cluster_bits;
1868 vcn = vbo >> sbi->cluster_bits;
1869 len = bytes >> sbi->cluster_bits;
1873 svcn = le64_to_cpu(attr_b->nres.svcn);
1874 evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
1876 if (svcn <= vcn && vcn < evcn1) {
1885 attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
1892 svcn = le64_to_cpu(attr->nres.svcn);
1893 evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
1899 attr->nres.svcn = cpu_to_le64(svcn - len);
1900 attr->nres.evcn = cpu_to_le64(evcn1 - 1 - len);
1902 le->vcn = attr->nres.svcn;
1903 ni->attr_list.dirty = true;
1906 } else if (svcn < vcn || end < evcn1) {
1907 CLST vcn1, eat, next_svcn;
1909 /* Collapse a part of this attribute segment. */
1910 err = attr_load_runs(attr, ni, run, &svcn);
1913 vcn1 = max(vcn, svcn);
1914 eat = min(end, evcn1) - vcn1;
1916 err = run_deallocate_ex(sbi, run, vcn1, eat, &dealloc,
1921 if (!run_collapse_range(run, vcn1, eat)) {
1928 attr->nres.svcn = cpu_to_le64(vcn);
1930 le->vcn = attr->nres.svcn;
1931 ni->attr_list.dirty = true;
1935 err = mi_pack_runs(mi, attr, run, evcn1 - svcn - eat);
1939 next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
1940 if (next_svcn + eat < evcn1) {
1941 err = ni_insert_nonresident(
1942 ni, ATTR_DATA, NULL, 0, run, next_svcn,
1943 evcn1 - eat - next_svcn, a_flags, &attr,
1948 /* Layout of records maybe changed. */
1952 /* Free all allocated memory. */
1953 run_truncate(run, 0);
1956 u16 roff = le16_to_cpu(attr->nres.run_off);
1958 if (roff > le32_to_cpu(attr->size)) {
1963 run_unpack_ex(RUN_DEALLOCATE, sbi, ni->mi.rno, svcn,
1964 evcn1 - 1, svcn, Add2Ptr(attr, roff),
1965 le32_to_cpu(attr->size) - roff);
1967 /* Delete this attribute segment. */
1968 mi_remove_attr(NULL, mi, attr);
1972 le_sz = le16_to_cpu(le->size);
1973 if (!al_remove_le(ni, le)) {
1982 /* Load next record that contains this attribute. */
1983 if (ni_load_mi(ni, le, &mi)) {
1988 /* Look for required attribute. */
1989 attr = mi_find_attr(mi, NULL, ATTR_DATA, NULL,
1997 le = (struct ATTR_LIST_ENTRY *)((u8 *)le - le_sz);
2003 attr = ni_enum_attr_ex(ni, attr, &le, &mi);
2010 svcn = le64_to_cpu(attr->nres.svcn);
2011 evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
2016 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL,
2025 valid_size = ni->i_valid;
2026 if (vbo + bytes <= valid_size)
2027 valid_size -= bytes;
2028 else if (vbo < valid_size)
2031 attr_b->nres.alloc_size = cpu_to_le64(alloc_size - bytes);
2032 attr_b->nres.data_size = cpu_to_le64(data_size);
2033 attr_b->nres.valid_size = cpu_to_le64(min(valid_size, data_size));
2034 total_size -= (u64)dealloc << sbi->cluster_bits;
2035 if (is_attr_ext(attr_b))
2036 attr_b->nres.total_size = cpu_to_le64(total_size);
2039 /* Update inode size. */
2040 ni->i_valid = valid_size;
2041 ni->vfs_inode.i_size = data_size;
2042 inode_set_bytes(&ni->vfs_inode, total_size);
2043 ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
2044 mark_inode_dirty(&ni->vfs_inode);
2047 up_write(&ni->file.run_lock);
2049 _ntfs_bad_inode(&ni->vfs_inode);
2057 * Not for normal files.
2059 int attr_punch_hole(struct ntfs_inode *ni, u64 vbo, u64 bytes, u32 *frame_size)
2062 struct runs_tree *run = &ni->file.run;
2063 struct ntfs_sb_info *sbi = ni->mi.sbi;
2064 struct ATTRIB *attr = NULL, *attr_b;
2065 struct ATTR_LIST_ENTRY *le, *le_b;
2066 struct mft_inode *mi, *mi_b;
2067 CLST svcn, evcn1, vcn, len, end, alen, hole, next_svcn;
2068 u64 total_size, alloc_size;
2071 struct runs_tree run2;
2077 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
2081 if (!attr_b->non_res) {
2082 u32 data_size = le32_to_cpu(attr_b->res.data_size);
2085 if (vbo > data_size)
2089 to = min_t(u64, vbo + bytes, data_size);
2090 memset(Add2Ptr(resident_data(attr_b), from), 0, to - from);
2094 if (!is_attr_ext(attr_b))
2097 alloc_size = le64_to_cpu(attr_b->nres.alloc_size);
2098 total_size = le64_to_cpu(attr_b->nres.total_size);
2100 if (vbo >= alloc_size) {
2101 /* NOTE: It is allowed. */
2105 mask = (sbi->cluster_size << attr_b->nres.c_unit) - 1;
2108 if (bytes > alloc_size)
2112 if ((vbo & mask) || (bytes & mask)) {
2113 /* We have to zero a range(s). */
2114 if (frame_size == NULL) {
2115 /* Caller insists range is aligned. */
2118 *frame_size = mask + 1;
2119 return E_NTFS_NOTALIGNED;
2122 down_write(&ni->file.run_lock);
2124 run_truncate(run, 0);
2127 * Enumerate all attribute segments and punch hole where necessary.
2129 alen = alloc_size >> sbi->cluster_bits;
2130 vcn = vbo >> sbi->cluster_bits;
2131 len = bytes >> sbi->cluster_bits;
2135 svcn = le64_to_cpu(attr_b->nres.svcn);
2136 evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
2137 a_flags = attr_b->flags;
2139 if (svcn <= vcn && vcn < evcn1) {
2148 attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
2155 svcn = le64_to_cpu(attr->nres.svcn);
2156 evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
2159 while (svcn < end) {
2160 CLST vcn1, zero, hole2 = hole;
2162 err = attr_load_runs(attr, ni, run, &svcn);
2165 vcn1 = max(vcn, svcn);
2166 zero = min(end, evcn1) - vcn1;
2169 * Check range [vcn1 + zero).
2170 * Calculate how many clusters there are.
2171 * Don't do any destructive actions.
2173 err = run_deallocate_ex(NULL, run, vcn1, zero, &hole2, false);
2177 /* Check if required range is already hole. */
2181 /* Make a clone of run to undo. */
2182 err = run_clone(run, &run2);
2186 /* Make a hole range (sparse) [vcn1 + zero). */
2187 if (!run_add_entry(run, vcn1, SPARSE_LCN, zero, false)) {
2192 /* Update run in attribute segment. */
2193 err = mi_pack_runs(mi, attr, run, evcn1 - svcn);
2196 next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
2197 if (next_svcn < evcn1) {
2198 /* Insert new attribute segment. */
2199 err = ni_insert_nonresident(ni, ATTR_DATA, NULL, 0, run,
2201 evcn1 - next_svcn, a_flags,
2206 /* Layout of records maybe changed. */
2210 /* Real deallocate. Should not fail. */
2211 run_deallocate_ex(sbi, &run2, vcn1, zero, &hole, true);
2214 /* Free all allocated memory. */
2215 run_truncate(run, 0);
2220 /* Get next attribute segment. */
2221 attr = ni_enum_attr_ex(ni, attr, &le, &mi);
2227 svcn = le64_to_cpu(attr->nres.svcn);
2228 evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
2236 attr_b = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL,
2244 total_size -= (u64)hole << sbi->cluster_bits;
2245 attr_b->nres.total_size = cpu_to_le64(total_size);
2248 /* Update inode size. */
2249 inode_set_bytes(&ni->vfs_inode, total_size);
2250 ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
2251 mark_inode_dirty(&ni->vfs_inode);
2255 up_write(&ni->file.run_lock);
2259 _ntfs_bad_inode(&ni->vfs_inode);
2264 * Restore packed runs.
2265 * 'mi_pack_runs' should not fail, cause we restore original.
2267 if (mi_pack_runs(mi, attr, &run2, evcn1 - svcn))
2274 * attr_insert_range - Insert range (hole) in file.
2275 * Not for normal files.
2277 int attr_insert_range(struct ntfs_inode *ni, u64 vbo, u64 bytes)
2280 struct runs_tree *run = &ni->file.run;
2281 struct ntfs_sb_info *sbi = ni->mi.sbi;
2282 struct ATTRIB *attr = NULL, *attr_b;
2283 struct ATTR_LIST_ENTRY *le, *le_b;
2284 struct mft_inode *mi, *mi_b;
2285 CLST vcn, svcn, evcn1, len, next_svcn;
2286 u64 data_size, alloc_size;
2294 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
2298 if (!is_attr_ext(attr_b)) {
2299 /* It was checked above. See fallocate. */
2303 if (!attr_b->non_res) {
2304 data_size = le32_to_cpu(attr_b->res.data_size);
2305 alloc_size = data_size;
2306 mask = sbi->cluster_mask; /* cluster_size - 1 */
2308 data_size = le64_to_cpu(attr_b->nres.data_size);
2309 alloc_size = le64_to_cpu(attr_b->nres.alloc_size);
2310 mask = (sbi->cluster_size << attr_b->nres.c_unit) - 1;
2313 if (vbo > data_size) {
2314 /* Insert range after the file size is not allowed. */
2318 if ((vbo & mask) || (bytes & mask)) {
2319 /* Allow to insert only frame aligned ranges. */
2324 * valid_size <= data_size <= alloc_size
2325 * Check alloc_size for maximum possible.
2327 if (bytes > sbi->maxbytes_sparse - alloc_size)
2330 vcn = vbo >> sbi->cluster_bits;
2331 len = bytes >> sbi->cluster_bits;
2333 down_write(&ni->file.run_lock);
2335 if (!attr_b->non_res) {
2336 err = attr_set_size(ni, ATTR_DATA, NULL, 0, run,
2337 data_size + bytes, NULL, false, NULL);
2340 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL,
2350 if (!attr_b->non_res) {
2351 /* Still resident. */
2352 char *data = Add2Ptr(attr_b,
2353 le16_to_cpu(attr_b->res.data_off));
2355 memmove(data + bytes, data, bytes);
2356 memset(data, 0, bytes);
2360 /* Resident files becomes nonresident. */
2361 data_size = le64_to_cpu(attr_b->nres.data_size);
2362 alloc_size = le64_to_cpu(attr_b->nres.alloc_size);
2366 * Enumerate all attribute segments and shift start vcn.
2368 a_flags = attr_b->flags;
2369 svcn = le64_to_cpu(attr_b->nres.svcn);
2370 evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
2372 if (svcn <= vcn && vcn < evcn1) {
2381 attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
2388 svcn = le64_to_cpu(attr->nres.svcn);
2389 evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
2392 run_truncate(run, 0); /* clear cached values. */
2393 err = attr_load_runs(attr, ni, run, NULL);
2397 if (!run_insert_range(run, vcn, len)) {
2402 /* Try to pack in current record as much as possible. */
2403 err = mi_pack_runs(mi, attr, run, evcn1 + len - svcn);
2407 next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
2409 while ((attr = ni_enum_attr_ex(ni, attr, &le, &mi)) &&
2410 attr->type == ATTR_DATA && !attr->name_len) {
2411 le64_add_cpu(&attr->nres.svcn, len);
2412 le64_add_cpu(&attr->nres.evcn, len);
2414 le->vcn = attr->nres.svcn;
2415 ni->attr_list.dirty = true;
2420 if (next_svcn < evcn1 + len) {
2421 err = ni_insert_nonresident(ni, ATTR_DATA, NULL, 0, run,
2422 next_svcn, evcn1 + len - next_svcn,
2423 a_flags, NULL, NULL, NULL);
2426 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL,
2434 /* ni_insert_nonresident failed. Try to undo. */
2435 goto undo_insert_range;
2440 * Update primary attribute segment.
2442 if (vbo <= ni->i_valid)
2443 ni->i_valid += bytes;
2445 attr_b->nres.data_size = cpu_to_le64(data_size + bytes);
2446 attr_b->nres.alloc_size = cpu_to_le64(alloc_size + bytes);
2448 /* ni->valid may be not equal valid_size (temporary). */
2449 if (ni->i_valid > data_size + bytes)
2450 attr_b->nres.valid_size = attr_b->nres.data_size;
2452 attr_b->nres.valid_size = cpu_to_le64(ni->i_valid);
2456 ni->vfs_inode.i_size += bytes;
2457 ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
2458 mark_inode_dirty(&ni->vfs_inode);
2461 run_truncate(run, 0); /* clear cached values. */
2463 up_write(&ni->file.run_lock);
2468 _ntfs_bad_inode(&ni->vfs_inode);
2472 svcn = le64_to_cpu(attr_b->nres.svcn);
2473 evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
2475 if (svcn <= vcn && vcn < evcn1) {
2483 attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
2489 svcn = le64_to_cpu(attr->nres.svcn);
2490 evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
2493 if (attr_load_runs(attr, ni, run, NULL))
2496 if (!run_collapse_range(run, vcn, len))
2499 if (mi_pack_runs(mi, attr, run, evcn1 + len - svcn))
2502 while ((attr = ni_enum_attr_ex(ni, attr, &le, &mi)) &&
2503 attr->type == ATTR_DATA && !attr->name_len) {
2504 le64_sub_cpu(&attr->nres.svcn, len);
2505 le64_sub_cpu(&attr->nres.evcn, len);
2507 le->vcn = attr->nres.svcn;
2508 ni->attr_list.dirty = true;