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_must_be_resident
60 * Return: True if attribute must be resident.
62 static inline bool attr_must_be_resident(struct ntfs_sb_info *sbi,
65 const struct ATTR_DEF_ENTRY *de;
77 de = ntfs_query_def(sbi, type);
78 if (de && (de->flags & NTFS_ATTR_MUST_BE_RESIDENT))
85 * attr_load_runs - Load all runs stored in @attr.
87 static int attr_load_runs(struct ATTRIB *attr, struct ntfs_inode *ni,
88 struct runs_tree *run, const CLST *vcn)
91 CLST svcn = le64_to_cpu(attr->nres.svcn);
92 CLST evcn = le64_to_cpu(attr->nres.evcn);
96 if (svcn >= evcn + 1 || run_is_mapped_full(run, svcn, evcn))
99 if (vcn && (evcn < *vcn || *vcn < svcn))
102 asize = le32_to_cpu(attr->size);
103 run_off = le16_to_cpu(attr->nres.run_off);
104 err = run_unpack_ex(run, ni->mi.sbi, ni->mi.rno, svcn, evcn,
105 vcn ? *vcn : svcn, Add2Ptr(attr, run_off),
114 * run_deallocate_ex - Deallocate clusters.
116 static int run_deallocate_ex(struct ntfs_sb_info *sbi, struct runs_tree *run,
117 CLST vcn, CLST len, CLST *done, bool trim)
120 CLST vcn_next, vcn0 = vcn, lcn, clen, dn = 0;
126 if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx)) {
128 run_truncate(run, vcn0);
142 if (lcn != SPARSE_LCN) {
144 /* mark bitmap range [lcn + clen) as free and trim clusters. */
145 mark_as_free_ex(sbi, lcn, clen, trim);
154 vcn_next = vcn + clen;
155 if (!run_get_entry(run, ++idx, &vcn, &lcn, &clen) ||
157 /* Save memory - don't load entire run. */
170 * attr_allocate_clusters - Find free space, mark it as used and store in @run.
172 int attr_allocate_clusters(struct ntfs_sb_info *sbi, struct runs_tree *run,
173 CLST vcn, CLST lcn, CLST len, CLST *pre_alloc,
174 enum ALLOCATE_OPT opt, CLST *alen, const size_t fr,
178 CLST flen, vcn0 = vcn, pre = pre_alloc ? *pre_alloc : 0;
179 size_t cnt = run->count;
182 err = ntfs_look_for_free_space(sbi, lcn, len + pre, &lcn, &flen,
185 if (err == -ENOSPC && pre) {
195 if (new_lcn && vcn == vcn0)
198 /* Add new fragment into run storage. */
199 if (!run_add_entry(run, vcn, lcn, flen, opt == ALLOCATE_MFT)) {
200 /* Undo last 'ntfs_look_for_free_space' */
201 mark_as_free_ex(sbi, lcn, len, false);
208 if (flen >= len || opt == ALLOCATE_MFT ||
209 (fr && run->count - cnt >= fr)) {
218 /* Undo 'ntfs_look_for_free_space' */
220 run_deallocate_ex(sbi, run, vcn0, vcn - vcn0, NULL, false);
221 run_truncate(run, vcn0);
228 * attr_make_nonresident
230 * If page is not NULL - it is already contains resident data
231 * and locked (called from ni_write_frame()).
233 int attr_make_nonresident(struct ntfs_inode *ni, struct ATTRIB *attr,
234 struct ATTR_LIST_ENTRY *le, struct mft_inode *mi,
235 u64 new_size, struct runs_tree *run,
236 struct ATTRIB **ins_attr, struct page *page)
238 struct ntfs_sb_info *sbi;
239 struct ATTRIB *attr_s;
241 u32 used, asize, rsize, aoff, align;
255 used = le32_to_cpu(rec->used);
256 asize = le32_to_cpu(attr->size);
257 next = Add2Ptr(attr, asize);
258 aoff = PtrOffset(rec, attr);
259 rsize = le32_to_cpu(attr->res.data_size);
260 is_data = attr->type == ATTR_DATA && !attr->name_len;
262 align = sbi->cluster_size;
263 if (is_attr_compressed(attr))
264 align <<= COMPRESSION_UNIT;
265 len = (rsize + align - 1) >> sbi->cluster_bits;
269 /* Make a copy of original attribute. */
270 attr_s = kmemdup(attr, asize, GFP_NOFS);
277 /* Empty resident -> Empty nonresident. */
280 const char *data = resident_data(attr);
282 err = attr_allocate_clusters(sbi, run, 0, 0, len, NULL,
283 ALLOCATE_DEF, &alen, 0, NULL);
288 /* Empty resident -> Non empty nonresident. */
289 } else if (!is_data) {
290 err = ntfs_sb_write_run(sbi, run, 0, data, rsize, 0);
296 page = grab_cache_page(ni->vfs_inode.i_mapping, 0);
301 kaddr = kmap_atomic(page);
302 memcpy(kaddr, data, rsize);
303 memset(kaddr + rsize, 0, PAGE_SIZE - rsize);
304 kunmap_atomic(kaddr);
305 flush_dcache_page(page);
306 SetPageUptodate(page);
307 set_page_dirty(page);
313 /* Remove original attribute. */
315 memmove(attr, Add2Ptr(attr, asize), used - aoff);
316 rec->used = cpu_to_le32(used);
319 al_remove_le(ni, le);
321 err = ni_insert_nonresident(ni, attr_s->type, attr_name(attr_s),
322 attr_s->name_len, run, 0, alen,
323 attr_s->flags, &attr, NULL, NULL);
328 attr->nres.data_size = cpu_to_le64(rsize);
329 attr->nres.valid_size = attr->nres.data_size;
334 ni->ni_flags &= ~NI_FLAG_RESIDENT;
336 /* Resident attribute becomes non resident. */
340 attr = Add2Ptr(rec, aoff);
341 memmove(next, attr, used - aoff);
342 memcpy(attr, attr_s, asize);
343 rec->used = cpu_to_le32(used + asize);
346 /* Undo: do not trim new allocated clusters. */
347 run_deallocate(sbi, run, false);
356 * attr_set_size_res - Helper for attr_set_size().
358 static int attr_set_size_res(struct ntfs_inode *ni, struct ATTRIB *attr,
359 struct ATTR_LIST_ENTRY *le, struct mft_inode *mi,
360 u64 new_size, struct runs_tree *run,
361 struct ATTRIB **ins_attr)
363 struct ntfs_sb_info *sbi = mi->sbi;
364 struct MFT_REC *rec = mi->mrec;
365 u32 used = le32_to_cpu(rec->used);
366 u32 asize = le32_to_cpu(attr->size);
367 u32 aoff = PtrOffset(rec, attr);
368 u32 rsize = le32_to_cpu(attr->res.data_size);
369 u32 tail = used - aoff - asize;
370 char *next = Add2Ptr(attr, asize);
371 s64 dsize = ALIGN(new_size, 8) - ALIGN(rsize, 8);
374 memmove(next + dsize, next, tail);
375 } else if (dsize > 0) {
376 if (used + dsize > sbi->max_bytes_per_attr)
377 return attr_make_nonresident(ni, attr, le, mi, new_size,
378 run, ins_attr, NULL);
380 memmove(next + dsize, next, tail);
381 memset(next, 0, dsize);
384 if (new_size > rsize)
385 memset(Add2Ptr(resident_data(attr), rsize), 0,
388 rec->used = cpu_to_le32(used + dsize);
389 attr->size = cpu_to_le32(asize + dsize);
390 attr->res.data_size = cpu_to_le32(new_size);
398 * attr_set_size - Change the size of attribute.
401 * - Sparse/compressed: No allocated clusters.
402 * - Normal: Append allocated and preallocated new clusters.
404 * - No deallocate if @keep_prealloc is set.
406 int attr_set_size(struct ntfs_inode *ni, enum ATTR_TYPE type,
407 const __le16 *name, u8 name_len, struct runs_tree *run,
408 u64 new_size, const u64 *new_valid, bool keep_prealloc,
412 struct ntfs_sb_info *sbi = ni->mi.sbi;
413 u8 cluster_bits = sbi->cluster_bits;
415 ni->mi.rno == MFT_REC_MFT && type == ATTR_DATA && !name_len;
416 u64 old_valid, old_size, old_alloc, new_alloc, new_alloc_tmp;
417 struct ATTRIB *attr = NULL, *attr_b;
418 struct ATTR_LIST_ENTRY *le, *le_b;
419 struct mft_inode *mi, *mi_b;
420 CLST alen, vcn, lcn, new_alen, old_alen, svcn, evcn;
421 CLST next_svcn, pre_alloc = -1, done = 0;
422 bool is_ext, is_bad = false;
429 attr_b = ni_find_attr(ni, NULL, &le_b, type, name, name_len, NULL,
436 if (!attr_b->non_res) {
437 err = attr_set_size_res(ni, attr_b, le_b, mi_b, new_size, run,
442 /* Return if file is still resident. */
443 if (!attr_b->non_res)
446 /* Layout of records may be changed, so do a full search. */
450 is_ext = is_attr_ext(attr_b);
451 align = sbi->cluster_size;
453 align <<= attr_b->nres.c_unit;
455 old_valid = le64_to_cpu(attr_b->nres.valid_size);
456 old_size = le64_to_cpu(attr_b->nres.data_size);
457 old_alloc = le64_to_cpu(attr_b->nres.alloc_size);
460 old_alen = old_alloc >> cluster_bits;
462 new_alloc = (new_size + align - 1) & ~(u64)(align - 1);
463 new_alen = new_alloc >> cluster_bits;
465 if (keep_prealloc && new_size < old_size) {
466 attr_b->nres.data_size = cpu_to_le64(new_size);
473 svcn = le64_to_cpu(attr_b->nres.svcn);
474 evcn = le64_to_cpu(attr_b->nres.evcn);
476 if (svcn <= vcn && vcn <= evcn) {
485 attr = ni_find_attr(ni, attr_b, &le, type, name, name_len, &vcn,
493 svcn = le64_to_cpu(attr->nres.svcn);
494 evcn = le64_to_cpu(attr->nres.evcn);
498 * attr,mi,le - last attribute segment (containing 'vcn').
499 * attr_b,mi_b,le_b - base (primary) attribute segment.
503 err = attr_load_runs(attr, ni, run, NULL);
507 if (new_size > old_size) {
511 if (new_alloc <= old_alloc) {
512 attr_b->nres.data_size = cpu_to_le64(new_size);
518 * Add clusters. In simple case we have to:
519 * - allocate space (vcn, lcn, len)
520 * - update packed run in 'mi'
521 * - update attr->nres.evcn
522 * - update attr_b->nres.data_size/attr_b->nres.alloc_size
524 to_allocate = new_alen - old_alen;
525 add_alloc_in_same_attr_seg:
528 /* MFT allocates clusters from MFT zone. */
531 /* No preallocate for sparse/compress. */
533 } else if (pre_alloc == -1) {
535 if (type == ATTR_DATA && !name_len &&
536 sbi->options->prealloc) {
540 get_pre_allocated(new_size)) -
544 /* Get the last LCN to allocate from. */
546 !run_lookup_entry(run, vcn, &lcn, NULL, NULL)) {
550 if (lcn == SPARSE_LCN)
555 free = wnd_zeroes(&sbi->used.bitmap);
556 if (to_allocate > free) {
561 if (pre_alloc && to_allocate + pre_alloc > free)
568 if (!run_add_entry(run, vcn, SPARSE_LCN, to_allocate,
575 /* ~3 bytes per fragment. */
576 err = attr_allocate_clusters(
577 sbi, run, vcn, lcn, to_allocate, &pre_alloc,
578 is_mft ? ALLOCATE_MFT : 0, &alen,
580 : (sbi->record_size -
581 le32_to_cpu(rec->used) + 8) /
591 if (to_allocate > alen)
597 err = mi_pack_runs(mi, attr, run, vcn - svcn);
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);
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);
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;
620 if (le32_to_cpu(rec->used) < sbi->record_size) {
621 old_alen = next_svcn;
623 goto add_alloc_in_same_attr_seg;
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;
630 if (type == ATTR_LIST) {
631 err = ni_expand_list(ni);
637 /* Layout of records is changed. */
641 if (!ni->attr_list.size) {
642 err = ni_create_attr_list(ni);
643 /* In case of error layout of records is not changed. */
646 /* Layout of records is changed. */
649 if (next_svcn >= vcn) {
650 /* This is MFT data, repeat. */
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);
660 * Layout of records maybe changed.
661 * Find base attribute to update.
664 attr_b = ni_find_attr(ni, NULL, &le_b, type, name, name_len,
672 /* ni_insert_nonresident failed. */
678 run_truncate_head(run, evcn + 1);
680 svcn = le64_to_cpu(attr->nres.svcn);
681 evcn = le64_to_cpu(attr->nres.evcn);
684 * Attribute is in consistency state.
685 * Save this point to restore to if next steps fail.
687 old_valid = old_size = old_alloc = (u64)vcn << cluster_bits;
688 attr_b->nres.valid_size = attr_b->nres.data_size =
689 attr_b->nres.alloc_size = cpu_to_le64(old_size);
694 if (new_size != old_size ||
695 (new_alloc != old_alloc && !keep_prealloc)) {
697 * Truncate clusters. In simple case we have to:
698 * - update packed run in 'mi'
699 * - update attr->nres.evcn
700 * - update attr_b->nres.data_size/attr_b->nres.alloc_size
701 * - mark and trim clusters as free (vcn, lcn, len)
705 vcn = max(svcn, new_alen);
706 new_alloc_tmp = (u64)vcn << cluster_bits;
709 err = mi_pack_runs(mi, attr, run, vcn - svcn);
712 } else if (le && le->vcn) {
713 u16 le_sz = le16_to_cpu(le->size);
716 * NOTE: List entries for one attribute are always
717 * the same size. We deal with last entry (vcn==0)
718 * and it is not first in entries array
719 * (list entry for std attribute always first).
720 * So it is safe to step back.
722 mi_remove_attr(NULL, mi, attr);
724 if (!al_remove_le(ni, le)) {
729 le = (struct ATTR_LIST_ENTRY *)((u8 *)le - le_sz);
731 attr->nres.evcn = cpu_to_le64((u64)vcn - 1);
735 attr_b->nres.alloc_size = cpu_to_le64(new_alloc_tmp);
737 if (vcn == new_alen) {
738 attr_b->nres.data_size = cpu_to_le64(new_size);
739 if (new_size < old_valid)
740 attr_b->nres.valid_size =
741 attr_b->nres.data_size;
744 le64_to_cpu(attr_b->nres.data_size))
745 attr_b->nres.data_size =
746 attr_b->nres.alloc_size;
748 le64_to_cpu(attr_b->nres.valid_size))
749 attr_b->nres.valid_size =
750 attr_b->nres.alloc_size;
754 err = run_deallocate_ex(sbi, run, vcn, evcn - vcn + 1, &dlen,
760 /* dlen - really deallocated clusters. */
761 le64_sub_cpu(&attr_b->nres.total_size,
762 ((u64)dlen << cluster_bits));
765 run_truncate(run, vcn);
767 if (new_alloc_tmp <= new_alloc)
770 old_size = new_alloc_tmp;
781 if (le->type != type || le->name_len != name_len ||
782 memcmp(le_name(le), name, name_len * sizeof(short))) {
787 err = ni_load_mi(ni, le, &mi);
791 attr = mi_find_attr(mi, NULL, type, name, name_len, &le->id);
801 __le64 valid = cpu_to_le64(min(*new_valid, new_size));
803 if (attr_b->nres.valid_size != valid) {
804 attr_b->nres.valid_size = valid;
813 /* Update inode_set_bytes. */
814 if (((type == ATTR_DATA && !name_len) ||
815 (type == ATTR_ALLOC && name == I30_NAME))) {
818 if (ni->vfs_inode.i_size != new_size) {
819 ni->vfs_inode.i_size = new_size;
823 if (attr_b->non_res) {
824 new_alloc = le64_to_cpu(attr_b->nres.alloc_size);
825 if (inode_get_bytes(&ni->vfs_inode) != new_alloc) {
826 inode_set_bytes(&ni->vfs_inode, new_alloc);
832 ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
833 mark_inode_dirty(&ni->vfs_inode);
841 attr_b->nres.data_size = cpu_to_le64(old_size);
842 attr_b->nres.valid_size = cpu_to_le64(old_valid);
843 attr_b->nres.alloc_size = cpu_to_le64(old_alloc);
845 /* Restore 'attr' and 'mi'. */
849 if (le64_to_cpu(attr_b->nres.svcn) <= svcn &&
850 svcn <= le64_to_cpu(attr_b->nres.evcn)) {
859 attr = ni_find_attr(ni, attr_b, &le, type, name, name_len,
866 if (mi_pack_runs(mi, attr, run, evcn - svcn + 1))
870 run_deallocate_ex(sbi, run, vcn, alen, NULL, false);
872 run_truncate(run, vcn);
876 _ntfs_bad_inode(&ni->vfs_inode);
881 int attr_data_get_block(struct ntfs_inode *ni, CLST vcn, CLST clen, CLST *lcn,
882 CLST *len, bool *new)
885 struct runs_tree *run = &ni->file.run;
886 struct ntfs_sb_info *sbi;
888 struct ATTRIB *attr = NULL, *attr_b;
889 struct ATTR_LIST_ENTRY *le, *le_b;
890 struct mft_inode *mi, *mi_b;
891 CLST hint, svcn, to_alloc, evcn1, next_svcn, asize, end;
899 down_read(&ni->file.run_lock);
900 ok = run_lookup_entry(run, vcn, lcn, len, NULL);
901 up_read(&ni->file.run_lock);
903 if (ok && (*lcn != SPARSE_LCN || !new)) {
911 if (ok && clen > *len)
915 cluster_bits = sbi->cluster_bits;
918 down_write(&ni->file.run_lock);
921 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
927 if (!attr_b->non_res) {
933 asize = le64_to_cpu(attr_b->nres.alloc_size) >> cluster_bits;
939 clst_per_frame = 1u << attr_b->nres.c_unit;
940 to_alloc = (clen + clst_per_frame - 1) & ~(clst_per_frame - 1);
942 if (vcn + to_alloc > asize)
943 to_alloc = asize - vcn;
945 svcn = le64_to_cpu(attr_b->nres.svcn);
946 evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
952 if (le_b && (vcn < svcn || evcn1 <= vcn)) {
953 attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
959 svcn = le64_to_cpu(attr->nres.svcn);
960 evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
963 err = attr_load_runs(attr, ni, run, NULL);
968 ok = run_lookup_entry(run, vcn, lcn, len, NULL);
969 if (ok && (*lcn != SPARSE_LCN || !new)) {
981 if (ok && clen > *len) {
983 to_alloc = (clen + clst_per_frame - 1) &
984 ~(clst_per_frame - 1);
988 if (!is_attr_ext(attr_b)) {
993 /* Get the last LCN to allocate from. */
997 if (!run_add_entry(run, evcn1, SPARSE_LCN, vcn - evcn1,
1002 } else if (vcn && !run_lookup_entry(run, vcn - 1, &hint, NULL, NULL)) {
1006 err = attr_allocate_clusters(
1007 sbi, run, vcn, hint + 1, to_alloc, NULL, 0, len,
1008 (sbi->record_size - le32_to_cpu(mi->mrec->used) + 8) / 3 + 1,
1016 total_size = le64_to_cpu(attr_b->nres.total_size) +
1017 ((u64)*len << cluster_bits);
1020 err = mi_pack_runs(mi, attr, run, max(end, evcn1) - svcn);
1024 attr_b->nres.total_size = cpu_to_le64(total_size);
1025 inode_set_bytes(&ni->vfs_inode, total_size);
1026 ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
1029 mark_inode_dirty(&ni->vfs_inode);
1031 /* Stored [vcn : next_svcn) from [vcn : end). */
1032 next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
1035 if (next_svcn == evcn1) {
1036 /* Normal way. Update attribute and exit. */
1039 /* Add new segment [next_svcn : evcn1 - next_svcn). */
1040 if (!ni->attr_list.size) {
1041 err = ni_create_attr_list(ni);
1044 /* Layout of records is changed. */
1046 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL,
1062 /* Estimate next attribute. */
1063 attr = ni_find_attr(ni, attr, &le, ATTR_DATA, NULL, 0, &svcn, &mi);
1066 CLST alloc = bytes_to_cluster(
1067 sbi, le64_to_cpu(attr_b->nres.alloc_size));
1068 CLST evcn = le64_to_cpu(attr->nres.evcn);
1070 if (end < next_svcn)
1072 while (end > evcn) {
1073 /* Remove segment [svcn : evcn). */
1074 mi_remove_attr(NULL, mi, attr);
1076 if (!al_remove_le(ni, le)) {
1081 if (evcn + 1 >= alloc) {
1082 /* Last attribute segment. */
1087 if (ni_load_mi(ni, le, &mi)) {
1092 attr = mi_find_attr(mi, NULL, ATTR_DATA, NULL, 0,
1098 svcn = le64_to_cpu(attr->nres.svcn);
1099 evcn = le64_to_cpu(attr->nres.evcn);
1105 err = attr_load_runs(attr, ni, run, &end);
1110 attr->nres.svcn = cpu_to_le64(next_svcn);
1111 err = mi_pack_runs(mi, attr, run, evcn1 - next_svcn);
1115 le->vcn = cpu_to_le64(next_svcn);
1116 ni->attr_list.dirty = true;
1119 next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
1122 if (evcn1 > next_svcn) {
1123 err = ni_insert_nonresident(ni, ATTR_DATA, NULL, 0, run,
1124 next_svcn, evcn1 - next_svcn,
1125 attr_b->flags, &attr, &mi, NULL);
1130 run_truncate_around(run, vcn);
1132 up_write(&ni->file.run_lock);
1138 int attr_data_read_resident(struct ntfs_inode *ni, struct page *page)
1141 struct ATTRIB *attr;
1144 attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, NULL);
1149 return E_NTFS_NONRESIDENT;
1151 vbo = page->index << PAGE_SHIFT;
1152 data_size = le32_to_cpu(attr->res.data_size);
1153 if (vbo < data_size) {
1154 const char *data = resident_data(attr);
1155 char *kaddr = kmap_atomic(page);
1156 u32 use = data_size - vbo;
1158 if (use > PAGE_SIZE)
1161 memcpy(kaddr, data + vbo, use);
1162 memset(kaddr + use, 0, PAGE_SIZE - use);
1163 kunmap_atomic(kaddr);
1164 flush_dcache_page(page);
1165 SetPageUptodate(page);
1166 } else if (!PageUptodate(page)) {
1167 zero_user_segment(page, 0, PAGE_SIZE);
1168 SetPageUptodate(page);
1174 int attr_data_write_resident(struct ntfs_inode *ni, struct page *page)
1177 struct mft_inode *mi;
1178 struct ATTRIB *attr;
1181 attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, &mi);
1185 if (attr->non_res) {
1186 /* Return special error code to check this case. */
1187 return E_NTFS_NONRESIDENT;
1190 vbo = page->index << PAGE_SHIFT;
1191 data_size = le32_to_cpu(attr->res.data_size);
1192 if (vbo < data_size) {
1193 char *data = resident_data(attr);
1194 char *kaddr = kmap_atomic(page);
1195 u32 use = data_size - vbo;
1197 if (use > PAGE_SIZE)
1199 memcpy(data + vbo, kaddr, use);
1200 kunmap_atomic(kaddr);
1203 ni->i_valid = data_size;
1209 * attr_load_runs_vcn - Load runs with VCN.
1211 int attr_load_runs_vcn(struct ntfs_inode *ni, enum ATTR_TYPE type,
1212 const __le16 *name, u8 name_len, struct runs_tree *run,
1215 struct ATTRIB *attr;
1220 attr = ni_find_attr(ni, NULL, NULL, type, name, name_len, &vcn, NULL);
1222 /* Is record corrupted? */
1226 svcn = le64_to_cpu(attr->nres.svcn);
1227 evcn = le64_to_cpu(attr->nres.evcn);
1229 if (evcn < vcn || vcn < svcn) {
1230 /* Is record corrupted? */
1234 ro = le16_to_cpu(attr->nres.run_off);
1235 err = run_unpack_ex(run, ni->mi.sbi, ni->mi.rno, svcn, evcn, svcn,
1236 Add2Ptr(attr, ro), le32_to_cpu(attr->size) - ro);
1243 * attr_load_runs_range - Load runs for given range [from to).
1245 int attr_load_runs_range(struct ntfs_inode *ni, enum ATTR_TYPE type,
1246 const __le16 *name, u8 name_len, struct runs_tree *run,
1249 struct ntfs_sb_info *sbi = ni->mi.sbi;
1250 u8 cluster_bits = sbi->cluster_bits;
1252 CLST vcn_last = (to - 1) >> cluster_bits;
1256 for (vcn = from >> cluster_bits; vcn <= vcn_last; vcn += clen) {
1257 if (!run_lookup_entry(run, vcn, &lcn, &clen, NULL)) {
1258 err = attr_load_runs_vcn(ni, type, name, name_len, run,
1262 clen = 0; /* Next run_lookup_entry(vcn) must be success. */
1269 #ifdef CONFIG_NTFS3_LZX_XPRESS
1271 * attr_wof_frame_info
1273 * Read header of Xpress/LZX file to get info about frame.
1275 int attr_wof_frame_info(struct ntfs_inode *ni, struct ATTRIB *attr,
1276 struct runs_tree *run, u64 frame, u64 frames,
1277 u8 frame_bits, u32 *ondisk_size, u64 *vbo_data)
1279 struct ntfs_sb_info *sbi = ni->mi.sbi;
1280 u64 vbo[2], off[2], wof_size;
1289 if (ni->vfs_inode.i_size < 0x100000000ull) {
1290 /* File starts with array of 32 bit offsets. */
1291 bytes_per_off = sizeof(__le32);
1292 vbo[1] = frame << 2;
1293 *vbo_data = frames << 2;
1295 /* File starts with array of 64 bit offsets. */
1296 bytes_per_off = sizeof(__le64);
1297 vbo[1] = frame << 3;
1298 *vbo_data = frames << 3;
1302 * Read 4/8 bytes at [vbo - 4(8)] == offset where compressed frame starts.
1303 * Read 4/8 bytes at [vbo] == offset where compressed frame ends.
1305 if (!attr->non_res) {
1306 if (vbo[1] + bytes_per_off > le32_to_cpu(attr->res.data_size)) {
1307 ntfs_inode_err(&ni->vfs_inode, "is corrupted");
1310 addr = resident_data(attr);
1312 if (bytes_per_off == sizeof(__le32)) {
1313 off32 = Add2Ptr(addr, vbo[1]);
1314 off[0] = vbo[1] ? le32_to_cpu(off32[-1]) : 0;
1315 off[1] = le32_to_cpu(off32[0]);
1317 off64 = Add2Ptr(addr, vbo[1]);
1318 off[0] = vbo[1] ? le64_to_cpu(off64[-1]) : 0;
1319 off[1] = le64_to_cpu(off64[0]);
1322 *vbo_data += off[0];
1323 *ondisk_size = off[1] - off[0];
1327 wof_size = le64_to_cpu(attr->nres.data_size);
1328 down_write(&ni->file.run_lock);
1329 page = ni->file.offs_page;
1331 page = alloc_page(GFP_KERNEL);
1337 ni->file.offs_page = page;
1340 addr = page_address(page);
1343 voff = vbo[1] & (PAGE_SIZE - 1);
1344 vbo[0] = vbo[1] - bytes_per_off;
1354 pgoff_t index = vbo[i] >> PAGE_SHIFT;
1356 if (index != page->index) {
1357 u64 from = vbo[i] & ~(u64)(PAGE_SIZE - 1);
1358 u64 to = min(from + PAGE_SIZE, wof_size);
1360 err = attr_load_runs_range(ni, ATTR_DATA, WOF_NAME,
1361 ARRAY_SIZE(WOF_NAME), run,
1366 err = ntfs_bio_pages(sbi, run, &page, 1, from,
1367 to - from, REQ_OP_READ);
1372 page->index = index;
1376 if (bytes_per_off == sizeof(__le32)) {
1377 off32 = Add2Ptr(addr, voff);
1378 off[1] = le32_to_cpu(*off32);
1380 off64 = Add2Ptr(addr, voff);
1381 off[1] = le64_to_cpu(*off64);
1384 if (bytes_per_off == sizeof(__le32)) {
1385 off32 = Add2Ptr(addr, PAGE_SIZE - sizeof(u32));
1386 off[0] = le32_to_cpu(*off32);
1388 off64 = Add2Ptr(addr, PAGE_SIZE - sizeof(u64));
1389 off[0] = le64_to_cpu(*off64);
1392 /* Two values in one page. */
1393 if (bytes_per_off == sizeof(__le32)) {
1394 off32 = Add2Ptr(addr, voff);
1395 off[0] = le32_to_cpu(off32[-1]);
1396 off[1] = le32_to_cpu(off32[0]);
1398 off64 = Add2Ptr(addr, voff);
1399 off[0] = le64_to_cpu(off64[-1]);
1400 off[1] = le64_to_cpu(off64[0]);
1406 *vbo_data += off[0];
1407 *ondisk_size = off[1] - off[0];
1412 up_write(&ni->file.run_lock);
1418 * attr_is_frame_compressed - Used to detect compressed frame.
1420 int attr_is_frame_compressed(struct ntfs_inode *ni, struct ATTRIB *attr,
1421 CLST frame, CLST *clst_data)
1425 CLST clen, lcn, vcn, alen, slen, vcn_next;
1427 struct runs_tree *run;
1431 if (!is_attr_compressed(attr))
1437 clst_frame = 1u << attr->nres.c_unit;
1438 vcn = frame * clst_frame;
1439 run = &ni->file.run;
1441 if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx)) {
1442 err = attr_load_runs_vcn(ni, attr->type, attr_name(attr),
1443 attr->name_len, run, vcn);
1447 if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx))
1451 if (lcn == SPARSE_LCN) {
1452 /* Sparsed frame. */
1456 if (clen >= clst_frame) {
1458 * The frame is not compressed 'cause
1459 * it does not contain any sparse clusters.
1461 *clst_data = clst_frame;
1465 alen = bytes_to_cluster(ni->mi.sbi, le64_to_cpu(attr->nres.alloc_size));
1470 * The frame is compressed if *clst_data + slen >= clst_frame.
1471 * Check next fragments.
1473 while ((vcn += clen) < alen) {
1476 if (!run_get_entry(run, ++idx, &vcn, &lcn, &clen) ||
1478 err = attr_load_runs_vcn(ni, attr->type,
1480 attr->name_len, run, vcn_next);
1485 if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx))
1489 if (lcn == SPARSE_LCN) {
1494 * Data_clusters + sparse_clusters =
1495 * not enough for frame.
1502 if (*clst_data + slen >= clst_frame) {
1505 * There is no sparsed clusters in this frame
1506 * so it is not compressed.
1508 *clst_data = clst_frame;
1510 /* Frame is compressed. */
1520 * attr_allocate_frame - Allocate/free clusters for @frame.
1522 * Assumed: down_write(&ni->file.run_lock);
1524 int attr_allocate_frame(struct ntfs_inode *ni, CLST frame, size_t compr_size,
1528 struct runs_tree *run = &ni->file.run;
1529 struct ntfs_sb_info *sbi = ni->mi.sbi;
1530 struct ATTRIB *attr = NULL, *attr_b;
1531 struct ATTR_LIST_ENTRY *le, *le_b;
1532 struct mft_inode *mi, *mi_b;
1533 CLST svcn, evcn1, next_svcn, lcn, len;
1534 CLST vcn, end, clst_data;
1535 u64 total_size, valid_size, data_size;
1538 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
1542 if (!is_attr_ext(attr_b))
1545 vcn = frame << NTFS_LZNT_CUNIT;
1546 total_size = le64_to_cpu(attr_b->nres.total_size);
1548 svcn = le64_to_cpu(attr_b->nres.svcn);
1549 evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
1550 data_size = le64_to_cpu(attr_b->nres.data_size);
1552 if (svcn <= vcn && vcn < evcn1) {
1561 attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
1567 svcn = le64_to_cpu(attr->nres.svcn);
1568 evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
1571 err = attr_load_runs(attr, ni, run, NULL);
1575 err = attr_is_frame_compressed(ni, attr_b, frame, &clst_data);
1579 total_size -= (u64)clst_data << sbi->cluster_bits;
1581 len = bytes_to_cluster(sbi, compr_size);
1583 if (len == clst_data)
1586 if (len < clst_data) {
1587 err = run_deallocate_ex(sbi, run, vcn + len, clst_data - len,
1592 if (!run_add_entry(run, vcn + len, SPARSE_LCN, clst_data - len,
1597 end = vcn + clst_data;
1598 /* Run contains updated range [vcn + len : end). */
1600 CLST alen, hint = 0;
1601 /* Get the last LCN to allocate from. */
1602 if (vcn + clst_data &&
1603 !run_lookup_entry(run, vcn + clst_data - 1, &hint, NULL,
1608 err = attr_allocate_clusters(sbi, run, vcn + clst_data,
1609 hint + 1, len - clst_data, NULL, 0,
1615 /* Run contains updated range [vcn + clst_data : end). */
1618 total_size += (u64)len << sbi->cluster_bits;
1621 err = mi_pack_runs(mi, attr, run, max(end, evcn1) - svcn);
1625 attr_b->nres.total_size = cpu_to_le64(total_size);
1626 inode_set_bytes(&ni->vfs_inode, total_size);
1629 mark_inode_dirty(&ni->vfs_inode);
1631 /* Stored [vcn : next_svcn) from [vcn : end). */
1632 next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
1635 if (next_svcn == evcn1) {
1636 /* Normal way. Update attribute and exit. */
1639 /* Add new segment [next_svcn : evcn1 - next_svcn). */
1640 if (!ni->attr_list.size) {
1641 err = ni_create_attr_list(ni);
1644 /* Layout of records is changed. */
1646 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL,
1662 /* Estimate next attribute. */
1663 attr = ni_find_attr(ni, attr, &le, ATTR_DATA, NULL, 0, &svcn, &mi);
1666 CLST alloc = bytes_to_cluster(
1667 sbi, le64_to_cpu(attr_b->nres.alloc_size));
1668 CLST evcn = le64_to_cpu(attr->nres.evcn);
1670 if (end < next_svcn)
1672 while (end > evcn) {
1673 /* Remove segment [svcn : evcn). */
1674 mi_remove_attr(NULL, mi, attr);
1676 if (!al_remove_le(ni, le)) {
1681 if (evcn + 1 >= alloc) {
1682 /* Last attribute segment. */
1687 if (ni_load_mi(ni, le, &mi)) {
1692 attr = mi_find_attr(mi, NULL, ATTR_DATA, NULL, 0,
1698 svcn = le64_to_cpu(attr->nres.svcn);
1699 evcn = le64_to_cpu(attr->nres.evcn);
1705 err = attr_load_runs(attr, ni, run, &end);
1710 attr->nres.svcn = cpu_to_le64(next_svcn);
1711 err = mi_pack_runs(mi, attr, run, evcn1 - next_svcn);
1715 le->vcn = cpu_to_le64(next_svcn);
1716 ni->attr_list.dirty = true;
1719 next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
1722 if (evcn1 > next_svcn) {
1723 err = ni_insert_nonresident(ni, ATTR_DATA, NULL, 0, run,
1724 next_svcn, evcn1 - next_svcn,
1725 attr_b->flags, &attr, &mi, NULL);
1730 run_truncate_around(run, vcn);
1732 if (new_valid > data_size)
1733 new_valid = data_size;
1735 valid_size = le64_to_cpu(attr_b->nres.valid_size);
1736 if (new_valid != valid_size) {
1737 attr_b->nres.valid_size = cpu_to_le64(valid_size);
1745 * attr_collapse_range - Collapse range in file.
1747 int attr_collapse_range(struct ntfs_inode *ni, u64 vbo, u64 bytes)
1750 struct runs_tree *run = &ni->file.run;
1751 struct ntfs_sb_info *sbi = ni->mi.sbi;
1752 struct ATTRIB *attr = NULL, *attr_b;
1753 struct ATTR_LIST_ENTRY *le, *le_b;
1754 struct mft_inode *mi, *mi_b;
1755 CLST svcn, evcn1, len, dealloc, alen;
1757 u64 valid_size, data_size, alloc_size, total_size;
1765 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
1769 if (!attr_b->non_res) {
1770 /* Attribute is resident. Nothing to do? */
1774 data_size = le64_to_cpu(attr_b->nres.data_size);
1775 alloc_size = le64_to_cpu(attr_b->nres.alloc_size);
1776 a_flags = attr_b->flags;
1778 if (is_attr_ext(attr_b)) {
1779 total_size = le64_to_cpu(attr_b->nres.total_size);
1780 mask = (sbi->cluster_size << attr_b->nres.c_unit) - 1;
1782 total_size = alloc_size;
1783 mask = sbi->cluster_mask;
1786 if ((vbo & mask) || (bytes & mask)) {
1787 /* Allow to collapse only cluster aligned ranges. */
1791 if (vbo > data_size)
1794 down_write(&ni->file.run_lock);
1796 if (vbo + bytes >= data_size) {
1797 u64 new_valid = min(ni->i_valid, vbo);
1799 /* Simple truncate file at 'vbo'. */
1800 truncate_setsize(&ni->vfs_inode, vbo);
1801 err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, vbo,
1802 &new_valid, true, NULL);
1804 if (!err && new_valid < ni->i_valid)
1805 ni->i_valid = new_valid;
1811 * Enumerate all attribute segments and collapse.
1813 alen = alloc_size >> sbi->cluster_bits;
1814 vcn = vbo >> sbi->cluster_bits;
1815 len = bytes >> sbi->cluster_bits;
1819 svcn = le64_to_cpu(attr_b->nres.svcn);
1820 evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
1822 if (svcn <= vcn && vcn < evcn1) {
1831 attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
1838 svcn = le64_to_cpu(attr->nres.svcn);
1839 evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
1845 attr->nres.svcn = cpu_to_le64(svcn - len);
1846 attr->nres.evcn = cpu_to_le64(evcn1 - 1 - len);
1848 le->vcn = attr->nres.svcn;
1849 ni->attr_list.dirty = true;
1852 } else if (svcn < vcn || end < evcn1) {
1853 CLST vcn1, eat, next_svcn;
1855 /* Collapse a part of this attribute segment. */
1856 err = attr_load_runs(attr, ni, run, &svcn);
1859 vcn1 = max(vcn, svcn);
1860 eat = min(end, evcn1) - vcn1;
1862 err = run_deallocate_ex(sbi, run, vcn1, eat, &dealloc,
1867 if (!run_collapse_range(run, vcn1, eat)) {
1874 attr->nres.svcn = cpu_to_le64(vcn);
1876 le->vcn = attr->nres.svcn;
1877 ni->attr_list.dirty = true;
1881 err = mi_pack_runs(mi, attr, run, evcn1 - svcn - eat);
1885 next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
1886 if (next_svcn + eat < evcn1) {
1887 err = ni_insert_nonresident(
1888 ni, ATTR_DATA, NULL, 0, run, next_svcn,
1889 evcn1 - eat - next_svcn, a_flags, &attr,
1894 /* Layout of records maybe changed. */
1898 /* Free all allocated memory. */
1899 run_truncate(run, 0);
1902 u16 roff = le16_to_cpu(attr->nres.run_off);
1904 run_unpack_ex(RUN_DEALLOCATE, sbi, ni->mi.rno, svcn,
1905 evcn1 - 1, svcn, Add2Ptr(attr, roff),
1906 le32_to_cpu(attr->size) - roff);
1908 /* Delete this attribute segment. */
1909 mi_remove_attr(NULL, mi, attr);
1913 le_sz = le16_to_cpu(le->size);
1914 if (!al_remove_le(ni, le)) {
1923 /* Load next record that contains this attribute. */
1924 if (ni_load_mi(ni, le, &mi)) {
1929 /* Look for required attribute. */
1930 attr = mi_find_attr(mi, NULL, ATTR_DATA, NULL,
1938 le = (struct ATTR_LIST_ENTRY *)((u8 *)le - le_sz);
1944 attr = ni_enum_attr_ex(ni, attr, &le, &mi);
1951 svcn = le64_to_cpu(attr->nres.svcn);
1952 evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
1957 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL,
1966 valid_size = ni->i_valid;
1967 if (vbo + bytes <= valid_size)
1968 valid_size -= bytes;
1969 else if (vbo < valid_size)
1972 attr_b->nres.alloc_size = cpu_to_le64(alloc_size - bytes);
1973 attr_b->nres.data_size = cpu_to_le64(data_size);
1974 attr_b->nres.valid_size = cpu_to_le64(min(valid_size, data_size));
1975 total_size -= (u64)dealloc << sbi->cluster_bits;
1976 if (is_attr_ext(attr_b))
1977 attr_b->nres.total_size = cpu_to_le64(total_size);
1980 /* Update inode size. */
1981 ni->i_valid = valid_size;
1982 ni->vfs_inode.i_size = data_size;
1983 inode_set_bytes(&ni->vfs_inode, total_size);
1984 ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
1985 mark_inode_dirty(&ni->vfs_inode);
1988 up_write(&ni->file.run_lock);
1990 _ntfs_bad_inode(&ni->vfs_inode);
1998 * Not for normal files.
2000 int attr_punch_hole(struct ntfs_inode *ni, u64 vbo, u64 bytes, u32 *frame_size)
2003 struct runs_tree *run = &ni->file.run;
2004 struct ntfs_sb_info *sbi = ni->mi.sbi;
2005 struct ATTRIB *attr = NULL, *attr_b;
2006 struct ATTR_LIST_ENTRY *le, *le_b;
2007 struct mft_inode *mi, *mi_b;
2008 CLST svcn, evcn1, vcn, len, end, alen, hole, next_svcn;
2009 u64 total_size, alloc_size;
2012 struct runs_tree run2;
2018 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
2022 if (!attr_b->non_res) {
2023 u32 data_size = le32_to_cpu(attr->res.data_size);
2026 if (vbo > data_size)
2030 to = min_t(u64, vbo + bytes, data_size);
2031 memset(Add2Ptr(resident_data(attr_b), from), 0, to - from);
2035 if (!is_attr_ext(attr_b))
2038 alloc_size = le64_to_cpu(attr_b->nres.alloc_size);
2039 total_size = le64_to_cpu(attr_b->nres.total_size);
2041 if (vbo >= alloc_size) {
2042 /* NOTE: It is allowed. */
2046 mask = (sbi->cluster_size << attr_b->nres.c_unit) - 1;
2049 if (bytes > alloc_size)
2053 if ((vbo & mask) || (bytes & mask)) {
2054 /* We have to zero a range(s). */
2055 if (frame_size == NULL) {
2056 /* Caller insists range is aligned. */
2059 *frame_size = mask + 1;
2060 return E_NTFS_NOTALIGNED;
2063 down_write(&ni->file.run_lock);
2065 run_truncate(run, 0);
2068 * Enumerate all attribute segments and punch hole where necessary.
2070 alen = alloc_size >> sbi->cluster_bits;
2071 vcn = vbo >> sbi->cluster_bits;
2072 len = bytes >> sbi->cluster_bits;
2076 svcn = le64_to_cpu(attr_b->nres.svcn);
2077 evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
2078 a_flags = attr_b->flags;
2080 if (svcn <= vcn && vcn < evcn1) {
2089 attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
2096 svcn = le64_to_cpu(attr->nres.svcn);
2097 evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
2100 while (svcn < end) {
2101 CLST vcn1, zero, hole2 = hole;
2103 err = attr_load_runs(attr, ni, run, &svcn);
2106 vcn1 = max(vcn, svcn);
2107 zero = min(end, evcn1) - vcn1;
2110 * Check range [vcn1 + zero).
2111 * Calculate how many clusters there are.
2112 * Don't do any destructive actions.
2114 err = run_deallocate_ex(NULL, run, vcn1, zero, &hole2, false);
2118 /* Check if required range is already hole. */
2122 /* Make a clone of run to undo. */
2123 err = run_clone(run, &run2);
2127 /* Make a hole range (sparse) [vcn1 + zero). */
2128 if (!run_add_entry(run, vcn1, SPARSE_LCN, zero, false)) {
2133 /* Update run in attribute segment. */
2134 err = mi_pack_runs(mi, attr, run, evcn1 - svcn);
2137 next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
2138 if (next_svcn < evcn1) {
2139 /* Insert new attribute segment. */
2140 err = ni_insert_nonresident(ni, ATTR_DATA, NULL, 0, run,
2142 evcn1 - next_svcn, a_flags,
2147 /* Layout of records maybe changed. */
2151 /* Real deallocate. Should not fail. */
2152 run_deallocate_ex(sbi, &run2, vcn1, zero, &hole, true);
2155 /* Free all allocated memory. */
2156 run_truncate(run, 0);
2161 /* Get next attribute segment. */
2162 attr = ni_enum_attr_ex(ni, attr, &le, &mi);
2168 svcn = le64_to_cpu(attr->nres.svcn);
2169 evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
2177 attr_b = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL,
2185 total_size -= (u64)hole << sbi->cluster_bits;
2186 attr_b->nres.total_size = cpu_to_le64(total_size);
2189 /* Update inode size. */
2190 inode_set_bytes(&ni->vfs_inode, total_size);
2191 ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
2192 mark_inode_dirty(&ni->vfs_inode);
2196 up_write(&ni->file.run_lock);
2200 _ntfs_bad_inode(&ni->vfs_inode);
2205 * Restore packed runs.
2206 * 'mi_pack_runs' should not fail, cause we restore original.
2208 if (mi_pack_runs(mi, attr, &run2, evcn1 - svcn))
2215 * attr_insert_range - Insert range (hole) in file.
2216 * Not for normal files.
2218 int attr_insert_range(struct ntfs_inode *ni, u64 vbo, u64 bytes)
2221 struct runs_tree *run = &ni->file.run;
2222 struct ntfs_sb_info *sbi = ni->mi.sbi;
2223 struct ATTRIB *attr = NULL, *attr_b;
2224 struct ATTR_LIST_ENTRY *le, *le_b;
2225 struct mft_inode *mi, *mi_b;
2226 CLST vcn, svcn, evcn1, len, next_svcn;
2227 u64 data_size, alloc_size;
2235 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
2239 if (!is_attr_ext(attr_b)) {
2240 /* It was checked above. See fallocate. */
2244 if (!attr_b->non_res) {
2245 data_size = le32_to_cpu(attr_b->res.data_size);
2246 alloc_size = data_size;
2247 mask = sbi->cluster_mask; /* cluster_size - 1 */
2249 data_size = le64_to_cpu(attr_b->nres.data_size);
2250 alloc_size = le64_to_cpu(attr_b->nres.alloc_size);
2251 mask = (sbi->cluster_size << attr_b->nres.c_unit) - 1;
2254 if (vbo > data_size) {
2255 /* Insert range after the file size is not allowed. */
2259 if ((vbo & mask) || (bytes & mask)) {
2260 /* Allow to insert only frame aligned ranges. */
2265 * valid_size <= data_size <= alloc_size
2266 * Check alloc_size for maximum possible.
2268 if (bytes > sbi->maxbytes_sparse - alloc_size)
2271 vcn = vbo >> sbi->cluster_bits;
2272 len = bytes >> sbi->cluster_bits;
2274 down_write(&ni->file.run_lock);
2276 if (!attr_b->non_res) {
2277 err = attr_set_size(ni, ATTR_DATA, NULL, 0, run,
2278 data_size + bytes, NULL, false, NULL);
2281 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL,
2291 if (!attr_b->non_res) {
2292 /* Still resident. */
2293 char *data = Add2Ptr(attr_b, attr_b->res.data_off);
2295 memmove(data + bytes, data, bytes);
2296 memset(data, 0, bytes);
2300 /* Resident files becomes nonresident. */
2301 data_size = le64_to_cpu(attr_b->nres.data_size);
2302 alloc_size = le64_to_cpu(attr_b->nres.alloc_size);
2306 * Enumerate all attribute segments and shift start vcn.
2308 a_flags = attr_b->flags;
2309 svcn = le64_to_cpu(attr_b->nres.svcn);
2310 evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
2312 if (svcn <= vcn && vcn < evcn1) {
2321 attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
2328 svcn = le64_to_cpu(attr->nres.svcn);
2329 evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
2332 run_truncate(run, 0); /* clear cached values. */
2333 err = attr_load_runs(attr, ni, run, NULL);
2337 if (!run_insert_range(run, vcn, len)) {
2342 /* Try to pack in current record as much as possible. */
2343 err = mi_pack_runs(mi, attr, run, evcn1 + len - svcn);
2347 next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
2349 while ((attr = ni_enum_attr_ex(ni, attr, &le, &mi)) &&
2350 attr->type == ATTR_DATA && !attr->name_len) {
2351 le64_add_cpu(&attr->nres.svcn, len);
2352 le64_add_cpu(&attr->nres.evcn, len);
2354 le->vcn = attr->nres.svcn;
2355 ni->attr_list.dirty = true;
2360 if (next_svcn < evcn1 + len) {
2361 err = ni_insert_nonresident(ni, ATTR_DATA, NULL, 0, run,
2362 next_svcn, evcn1 + len - next_svcn,
2363 a_flags, NULL, NULL, NULL);
2366 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL,
2374 /* ni_insert_nonresident failed. Try to undo. */
2375 goto undo_insert_range;
2380 * Update primary attribute segment.
2382 if (vbo <= ni->i_valid)
2383 ni->i_valid += bytes;
2385 attr_b->nres.data_size = le64_to_cpu(data_size + bytes);
2386 attr_b->nres.alloc_size = le64_to_cpu(alloc_size + bytes);
2388 /* ni->valid may be not equal valid_size (temporary). */
2389 if (ni->i_valid > data_size + bytes)
2390 attr_b->nres.valid_size = attr_b->nres.data_size;
2392 attr_b->nres.valid_size = cpu_to_le64(ni->i_valid);
2396 ni->vfs_inode.i_size += bytes;
2397 ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
2398 mark_inode_dirty(&ni->vfs_inode);
2401 run_truncate(run, 0); /* clear cached values. */
2403 up_write(&ni->file.run_lock);
2408 _ntfs_bad_inode(&ni->vfs_inode);
2412 svcn = le64_to_cpu(attr_b->nres.svcn);
2413 evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
2415 if (svcn <= vcn && vcn < evcn1) {
2423 attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
2429 svcn = le64_to_cpu(attr->nres.svcn);
2430 evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
2433 if (attr_load_runs(attr, ni, run, NULL))
2436 if (!run_collapse_range(run, vcn, len))
2439 if (mi_pack_runs(mi, attr, run, evcn1 + len - svcn))
2442 while ((attr = ni_enum_attr_ex(ni, attr, &le, &mi)) &&
2443 attr->type == ATTR_DATA && !attr->name_len) {
2444 le64_sub_cpu(&attr->nres.svcn, len);
2445 le64_sub_cpu(&attr->nres.evcn, len);
2447 le->vcn = attr->nres.svcn;
2448 ni->attr_list.dirty = true;