1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
6 * TODO: try to use extents tree (instead of array)
9 #include <linux/blkdev.h>
10 #include <linux/buffer_head.h>
12 #include <linux/log2.h>
13 #include <linux/nls.h>
19 /* runs_tree is a continues memory. Try to avoid big size. */
20 #define NTFS3_RUN_MAX_BYTES 0x10000
23 CLST vcn; /* Virtual cluster number. */
24 CLST len; /* Length in clusters. */
25 CLST lcn; /* Logical cluster number. */
29 * run_lookup - Lookup the index of a MCB entry that is first <= vcn.
31 * Case of success it will return non-zero value and set
32 * @index parameter to index of entry been found.
33 * Case of entry missing from list 'index' will be set to
34 * point to insertion position for the entry question.
36 bool run_lookup(const struct runs_tree *run, CLST vcn, size_t *index)
38 size_t min_idx, max_idx, mid_idx;
47 max_idx = run->count - 1;
49 /* Check boundary cases specially, 'cause they cover the often requests. */
56 if (vcn < r->vcn + r->len) {
62 if (vcn >= r->vcn + r->len) {
73 mid_idx = min_idx + ((max_idx - min_idx) >> 1);
74 r = run->runs + mid_idx;
77 max_idx = mid_idx - 1;
80 } else if (vcn >= r->vcn + r->len) {
81 min_idx = mid_idx + 1;
86 } while (min_idx <= max_idx);
93 * run_consolidate - Consolidate runs starting from a given one.
95 static void run_consolidate(struct runs_tree *run, size_t index)
98 struct ntfs_run *r = run->runs + index;
100 while (index + 1 < run->count) {
102 * I should merge current run with next
103 * if start of the next run lies inside one being tested.
105 struct ntfs_run *n = r + 1;
106 CLST end = r->vcn + r->len;
109 /* Stop if runs are not aligned one to another. */
116 * If range at index overlaps with next one
117 * then I will either adjust it's start position
118 * or (if completely matches) dust remove one from the list.
122 goto remove_next_range;
126 if (n->lcn != SPARSE_LCN)
132 * Stop if sparse mode does not match
133 * both current and next runs.
135 if ((n->lcn == SPARSE_LCN) != (r->lcn == SPARSE_LCN)) {
142 * Check if volume block
143 * of a next run lcn does not match
144 * last volume block of the current run.
146 if (n->lcn != SPARSE_LCN && n->lcn != r->lcn + r->len)
150 * Next and current are siblings.
153 r->len += n->len - dl;
156 i = run->count - (index + 1);
158 memmove(n, n + 1, sizeof(*n) * (i - 1));
167 * Return: True if range [svcn - evcn] is mapped.
169 bool run_is_mapped_full(const struct runs_tree *run, CLST svcn, CLST evcn)
172 const struct ntfs_run *r, *end;
175 if (!run_lookup(run, svcn, &i))
178 end = run->runs + run->count;
182 next_vcn = r->vcn + r->len;
189 if (r->vcn != next_vcn)
194 bool run_lookup_entry(const struct runs_tree *run, CLST vcn, CLST *lcn,
195 CLST *len, size_t *index)
201 /* Fail immediately if nrun was not touched yet. */
205 if (!run_lookup(run, vcn, &idx))
210 if (vcn >= r->vcn + r->len)
217 *lcn = r->lcn == SPARSE_LCN ? SPARSE_LCN : (r->lcn + gap);
228 * run_truncate_head - Decommit the range before vcn.
230 void run_truncate_head(struct runs_tree *run, CLST vcn)
235 if (run_lookup(run, vcn, &index)) {
236 r = run->runs + index;
239 CLST dlen = vcn - r->vcn;
243 if (r->lcn != SPARSE_LCN)
251 memmove(r, r + index, sizeof(*r) * (run->count - index));
263 * run_truncate - Decommit the range after vcn.
265 void run_truncate(struct runs_tree *run, CLST vcn)
270 * If I hit the range then
271 * I have to truncate one.
272 * If range to be truncated is becoming empty
273 * then it will entirely be removed.
275 if (run_lookup(run, vcn, &index)) {
276 struct ntfs_run *r = run->runs + index;
278 r->len = vcn - r->vcn;
285 * At this point 'index' is set to position that
286 * should be thrown away (including index itself)
287 * Simple one - just set the limit.
291 /* Do not reallocate array 'runs'. Only free if possible. */
300 * run_truncate_around - Trim head and tail if necessary.
302 void run_truncate_around(struct runs_tree *run, CLST vcn)
304 run_truncate_head(run, vcn);
306 if (run->count >= NTFS3_RUN_MAX_BYTES / sizeof(struct ntfs_run) / 2)
307 run_truncate(run, (run->runs + (run->count >> 1))->vcn);
313 * Sets location to known state.
314 * Run to be added may overlap with existing location.
316 * Return: false if of memory.
318 bool run_add_entry(struct runs_tree *run, CLST vcn, CLST lcn, CLST len,
324 CLST tail_vcn = 0, tail_len = 0, tail_lcn = 0;
325 bool should_add_tail = false;
328 * Lookup the insertion point.
330 * Execute bsearch for the entry containing
331 * start position question.
333 inrange = run_lookup(run, vcn, &index);
336 * Shortcut here would be case of
337 * range not been found but one been added
338 * continues previous run.
339 * This case I can directly make use of
340 * existing range as my start point.
342 if (!inrange && index > 0) {
343 struct ntfs_run *t = run->runs + index - 1;
345 if (t->vcn + t->len == vcn &&
346 (t->lcn == SPARSE_LCN) == (lcn == SPARSE_LCN) &&
347 (lcn == SPARSE_LCN || lcn == t->lcn + t->len)) {
354 * At this point 'index' either points to the range
355 * containing start position or to the insertion position
357 * So first let's check if range I'm probing is here already.
362 * Range was not found.
363 * Insert at position 'index'
365 used = run->count * sizeof(struct ntfs_run);
368 * Check allocated space.
369 * If one is not enough to get one more entry
370 * then it will be reallocated.
372 if (run->allocated < used + sizeof(struct ntfs_run)) {
374 struct ntfs_run *new_ptr;
376 /* Use power of 2 for 'bytes'. */
379 } else if (used <= 16 * PAGE_SIZE) {
380 if (is_power_of_2(run->allocated))
381 bytes = run->allocated << 1;
384 << (2 + blksize_bits(used));
386 bytes = run->allocated + (16 * PAGE_SIZE);
389 WARN_ON(!is_mft && bytes > NTFS3_RUN_MAX_BYTES);
391 new_ptr = kvmalloc(bytes, GFP_KERNEL);
397 memcpy(new_ptr, run->runs,
398 index * sizeof(struct ntfs_run));
399 memcpy(r + 1, run->runs + index,
400 sizeof(struct ntfs_run) * (run->count - index));
404 run->allocated = bytes;
407 size_t i = run->count - index;
409 r = run->runs + index;
411 /* memmove appears to be a bottle neck here... */
413 memmove(r + 1, r, sizeof(struct ntfs_run) * i);
421 r = run->runs + index;
424 * If one of ranges was not allocated then we
425 * have to split location we just matched and
426 * insert current one.
427 * A common case this requires tail to be reinserted
430 if (((lcn == SPARSE_LCN) != (r->lcn == SPARSE_LCN)) ||
431 (lcn != SPARSE_LCN && lcn != r->lcn + (vcn - r->vcn))) {
432 CLST to_eat = vcn - r->vcn;
433 CLST Tovcn = to_eat + len;
435 should_add_tail = Tovcn < r->len;
437 if (should_add_tail) {
438 tail_lcn = r->lcn == SPARSE_LCN
441 tail_vcn = r->vcn + Tovcn;
442 tail_len = r->len - Tovcn;
449 goto requires_new_range;
452 /* lcn should match one were going to add. */
457 * If existing range fits then were done.
458 * Otherwise extend found one and fall back to range jocode.
460 if (r->vcn + r->len < vcn + len)
461 r->len += len - ((r->vcn + r->len) - vcn);
465 * And normalize it starting from insertion point.
466 * It's possible that no insertion needed case if
467 * start point lies within the range of an entry
468 * that 'index' points to.
470 if (inrange && index > 0)
472 run_consolidate(run, index);
473 run_consolidate(run, index + 1);
477 * We have to add extra range a tail.
479 if (should_add_tail &&
480 !run_add_entry(run, tail_vcn, tail_lcn, tail_len, is_mft))
486 /* run_collapse_range
488 * Helper for attr_collapse_range(),
489 * which is helper for fallocate(collapse_range).
491 bool run_collapse_range(struct runs_tree *run, CLST vcn, CLST len)
494 struct ntfs_run *r, *e, *eat_start, *eat_end;
497 if (WARN_ON(!run_lookup(run, vcn, &index)))
498 return true; /* Should never be here. */
500 e = run->runs + run->count;
501 r = run->runs + index;
505 if (r->vcn + r->len <= end) {
506 /* Collapse tail of run .*/
507 r->len = vcn - r->vcn;
508 } else if (r->lcn == SPARSE_LCN) {
509 /* Collapse a middle part of sparsed run. */
512 /* Collapse a middle part of normal run, split. */
513 if (!run_add_entry(run, vcn, SPARSE_LCN, len, false))
515 return run_collapse_range(run, vcn, len);
532 if (r->vcn + r->len <= end) {
539 if (r->lcn != SPARSE_LCN)
545 eat = eat_end - eat_start;
546 memmove(eat_start, eat_end, (e - eat_end) * sizeof(*r));
553 * run_get_entry - Return index-th mapped region.
555 bool run_get_entry(const struct runs_tree *run, size_t index, CLST *vcn,
556 CLST *lcn, CLST *len)
558 const struct ntfs_run *r;
560 if (index >= run->count)
563 r = run->runs + index;
578 * run_packed_size - Calculate the size of packed int64.
581 static inline int run_packed_size(const s64 n)
583 const u8 *p = (const u8 *)&n + sizeof(n) - 1;
586 if (p[-7] || p[-6] || p[-5] || p[-4])
595 if (p[-7] != 0xff || p[-6] != 0xff || p[-5] != 0xff ||
598 if (p[-3] != 0xff || p[-2] != 0xff)
605 return (const u8 *)&n + sizeof(n) - p;
608 /* Full trusted function. It does not check 'size' for errors. */
609 static inline void run_pack_s64(u8 *run_buf, u8 size, s64 v)
611 const u8 *p = (u8 *)&v;
640 /* Full trusted function. It does not check 'size' for errors. */
641 static inline s64 run_unpack_s64(const u8 *run_buf, u8 size, s64 v)
675 static inline int run_packed_size(const s64 n)
677 const u8 *p = (const u8 *)&n;
680 if (p[7] || p[6] || p[5] || p[4])
689 if (p[7] != 0xff || p[6] != 0xff || p[5] != 0xff ||
692 if (p[3] != 0xff || p[2] != 0xff)
700 return 1 + p - (const u8 *)&n;
703 /* Full trusted function. It does not check 'size' for errors. */
704 static inline void run_pack_s64(u8 *run_buf, u8 size, s64 v)
706 const u8 *p = (u8 *)&v;
708 /* memcpy( run_buf, &v, size); Is it faster? */
736 /* full trusted function. It does not check 'size' for errors */
737 static inline s64 run_unpack_s64(const u8 *run_buf, u8 size, s64 v)
741 /* memcpy( &v, run_buf, size); Is it faster? */
772 * run_pack - Pack runs into buffer.
774 * packed_vcns - How much runs we have packed.
775 * packed_size - How much bytes we have used run_buf.
777 int run_pack(const struct runs_tree *run, CLST svcn, CLST len, u8 *run_buf,
778 u32 run_buf_size, CLST *packed_vcns)
780 CLST next_vcn, vcn, lcn;
782 CLST evcn1 = svcn + len;
787 int offset_size, size_size, tmp;
789 next_vcn = vcn = svcn;
796 ok = run_lookup_entry(run, vcn, &lcn, &len, &i);
805 next_vcn = vcn + len;
806 if (next_vcn > evcn1)
809 /* How much bytes required to pack len. */
810 size_size = run_packed_size(len);
812 /* offset_size - How much bytes is packed dlcn. */
813 if (lcn == SPARSE_LCN) {
817 /* NOTE: lcn can be less than prev_lcn! */
818 dlcn = (s64)lcn - prev_lcn;
819 offset_size = run_packed_size(dlcn);
823 tmp = run_buf_size - packed_size - 2 - offset_size;
827 /* Can we store this entire run. */
832 /* Pack run header. */
833 run_buf[0] = ((u8)(size_size | (offset_size << 4)));
836 /* Pack the length of run. */
837 run_pack_s64(run_buf, size_size, len);
839 run_buf += size_size;
840 /* Pack the offset from previous LCN. */
841 run_pack_s64(run_buf, offset_size, dlcn);
842 run_buf += offset_size;
845 packed_size += 1 + offset_size + size_size;
848 if (packed_size + 1 >= run_buf_size || next_vcn >= evcn1)
851 ok = run_get_entry(run, ++i, &vcn, &lcn, &len);
860 /* Store last zero. */
864 return packed_size + 1;
871 * run_unpack - Unpack packed runs from @run_buf.
873 * Return: Error if negative, or real used bytes.
875 int run_unpack(struct runs_tree *run, struct ntfs_sb_info *sbi, CLST ino,
876 CLST svcn, CLST evcn, CLST vcn, const u8 *run_buf,
879 u64 prev_lcn, vcn64, lcn, next_vcn;
880 const u8 *run_last, *run_0;
881 bool is_mft = ino == MFT_REC_MFT;
883 /* Check for empty. */
884 if (evcn + 1 == svcn)
891 run_last = run_buf + run_buf_size;
895 /* Read all runs the chain. */
896 /* size_size - How much bytes is packed len. */
897 while (run_buf < run_last) {
898 /* size_size - How much bytes is packed len. */
899 u8 size_size = *run_buf & 0xF;
900 /* offset_size - How much bytes is packed dlcn. */
901 u8 offset_size = *run_buf++ >> 4;
909 * NOTE: Runs are stored little endian order
910 * "len" is unsigned value, "dlcn" is signed.
911 * Large positive number requires to store 5 bytes
912 * e.g.: 05 FF 7E FF FF 00 00 00
917 len = run_unpack_s64(run_buf, size_size, 0);
918 /* Skip size_size. */
919 run_buf += size_size;
926 else if (offset_size <= 8) {
929 /* Initial value of dlcn is -1 or 0. */
930 dlcn = (run_buf[offset_size - 1] & 0x80) ? (s64)-1 : 0;
931 dlcn = run_unpack_s64(run_buf, offset_size, dlcn);
932 /* Skip offset_size. */
933 run_buf += offset_size;
937 lcn = prev_lcn + dlcn;
942 next_vcn = vcn64 + len;
943 /* Check boundary. */
944 if (next_vcn > evcn + 1)
947 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
948 if (next_vcn > 0x100000000ull || (lcn + len) > 0x100000000ull) {
951 "This driver is compiled without CONFIG_NTFS3_64BIT_CLUSTER (like windows driver).\n"
952 "Volume contains 64 bits run: vcn %llx, lcn %llx, len %llx.\n"
953 "Activate CONFIG_NTFS3_64BIT_CLUSTER to process this case",
958 if (lcn != SPARSE_LCN64 && lcn + len > sbi->used.bitmap.nbits) {
959 /* LCN range is out of volume. */
964 ; /* Called from check_attr(fslog.c) to check run. */
965 else if (run == RUN_DEALLOCATE) {
967 * Called from ni_delete_all to free clusters
968 * without storing in run.
970 if (lcn != SPARSE_LCN64)
971 mark_as_free_ex(sbi, lcn, len, true);
972 } else if (vcn64 >= vcn) {
973 if (!run_add_entry(run, vcn64, lcn, len, is_mft))
975 } else if (next_vcn > vcn) {
976 u64 dlen = vcn - vcn64;
978 if (!run_add_entry(run, vcn, lcn + dlen, len - dlen,
986 if (vcn64 != evcn + 1) {
987 /* Not expected length of unpacked runs. */
991 return run_buf - run_0;
994 #ifdef NTFS3_CHECK_FREE_CLST
996 * run_unpack_ex - Unpack packed runs from "run_buf".
998 * Checks unpacked runs to be used in bitmap.
1000 * Return: Error if negative, or real used bytes.
1002 int run_unpack_ex(struct runs_tree *run, struct ntfs_sb_info *sbi, CLST ino,
1003 CLST svcn, CLST evcn, CLST vcn, const u8 *run_buf,
1007 CLST next_vcn, lcn, len;
1010 struct wnd_bitmap *wnd;
1012 ret = run_unpack(run, sbi, ino, svcn, evcn, vcn, run_buf, run_buf_size);
1016 if (!sbi->used.bitmap.sb || !run || run == RUN_DEALLOCATE)
1019 if (ino == MFT_REC_BADCLUST)
1022 next_vcn = vcn = svcn;
1023 wnd = &sbi->used.bitmap;
1025 for (ok = run_lookup_entry(run, vcn, &lcn, &len, &index);
1027 ok = run_get_entry(run, ++index, &vcn, &lcn, &len)) {
1028 if (!ok || next_vcn != vcn)
1031 next_vcn = vcn + len;
1033 if (lcn == SPARSE_LCN)
1036 if (sbi->flags & NTFS_FLAGS_NEED_REPLAY)
1039 down_read_nested(&wnd->rw_lock, BITMAP_MUTEX_CLUSTERS);
1040 /* Check for free blocks. */
1041 ok = wnd_is_used(wnd, lcn, len);
1042 up_read(&wnd->rw_lock);
1046 /* Looks like volume is corrupted. */
1047 ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
1049 if (down_write_trylock(&wnd->rw_lock)) {
1050 /* Mark all zero bits as used in range [lcn, lcn+len). */
1051 CLST i, lcn_f = 0, len_f = 0;
1054 for (i = 0; i < len; i++) {
1055 if (wnd_is_free(wnd, lcn + i, 1)) {
1060 err = wnd_set_used(wnd, lcn_f, len_f);
1068 err = wnd_set_used(wnd, lcn_f, len_f);
1070 up_write(&wnd->rw_lock);
1081 * run_get_highest_vcn
1083 * Return the highest vcn from a mapping pairs array
1084 * it used while replaying log file.
1086 int run_get_highest_vcn(CLST vcn, const u8 *run_buf, u64 *highest_vcn)
1091 while ((size_size = *run_buf & 0xF)) {
1092 u8 offset_size = *run_buf++ >> 4;
1095 if (size_size > 8 || offset_size > 8)
1098 len = run_unpack_s64(run_buf, size_size, 0);
1102 run_buf += size_size + offset_size;
1105 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
1106 if (vcn64 > 0x100000000ull)
1111 *highest_vcn = vcn64 - 1;