37 if (
likely((dst != src) && (size > 0)))
38 memmove(base + dst, base + src, size *
sizeof(*base));
51 memcpy(dstbase + dst, srcbase + src, size *
sizeof(*dstbase));
81 if (old_size == new_size)
84 new_rl = ntfs_malloc_nofs(new_size);
91 memcpy(new_rl, rl, old_size);
121 int old_size,
int new_size)
125 old_size =
PAGE_ALIGN(old_size *
sizeof(*rl));
126 new_size =
PAGE_ALIGN(new_size *
sizeof(*rl));
127 if (old_size == new_size)
130 new_rl = ntfs_malloc_nofs_nofail(new_size);
136 memcpy(new_rl, rl, old_size);
168 if ((dst->
lcn >= 0) && (src->
lcn >= 0) &&
228 if ((loc + 1) < dsize)
229 right = ntfs_are_rl_mergeable(src + ssize - 1, dst + loc + 1);
232 dst = ntfs_rl_realloc(dst, dsize, dsize + ssize - right);
242 __ntfs_rl_merge(src + ssize - 1, dst + loc + 1);
245 marker = loc + ssize + 1;
248 ntfs_rl_mm(dst, marker, loc + 1 + right, dsize - (loc + 1 + right));
249 ntfs_rl_mc(dst, loc + 1, src, 0, ssize);
256 dst[marker].
vcn = dst[marker - 1].
vcn + dst[marker - 1].
length;
300 disc = (src[0].
vcn > 0);
304 left = ntfs_are_rl_mergeable(dst + loc - 1, src);
306 merged_length = dst[loc - 1].
length;
308 merged_length += src->
length;
310 disc = (src[0].
vcn > dst[loc - 1].
vcn + merged_length);
316 dst = ntfs_rl_realloc(dst, dsize, dsize + ssize - left + disc);
324 __ntfs_rl_merge(dst + loc - 1, src);
332 marker = loc + ssize - left + disc;
335 ntfs_rl_mm(dst, marker, loc, dsize - loc);
336 ntfs_rl_mc(dst, loc + disc, src, left, ssize - left);
339 dst[marker].
vcn = dst[marker - 1].
vcn + dst[marker - 1].
length;
342 dst[marker].
length = dst[marker + 1].
vcn - dst[marker].
vcn;
394 if ((loc + 1) < dsize)
395 right = ntfs_are_rl_mergeable(src + ssize - 1, dst + loc + 1);
397 left = ntfs_are_rl_mergeable(dst + loc - 1, src);
402 delta = ssize - 1 - left -
right;
404 dst = ntfs_rl_realloc(dst, dsize, dsize + delta);
415 __ntfs_rl_merge(src + ssize - 1, dst + loc + 1);
417 __ntfs_rl_merge(dst + loc - 1, src);
426 tail = loc + right + 1;
434 marker = loc + ssize -
left;
437 ntfs_rl_mm(dst, marker, tail, dsize - tail);
438 ntfs_rl_mc(dst, loc, src, left, ssize - left);
441 if (dsize - tail > 0 && dst[marker].lcn ==
LCN_ENOENT)
442 dst[marker].
vcn = dst[marker - 1].
vcn + dst[marker - 1].
length;
477 dst = ntfs_rl_realloc(dst, dsize, dsize + ssize + 1);
486 ntfs_rl_mm(dst, loc + 1 + ssize, loc, dsize - loc);
487 ntfs_rl_mc(dst, loc + 1, src, 0, ssize);
491 dst[loc+ssize+1].
vcn = dst[loc+ssize].
vcn + dst[loc+ssize].
length;
492 dst[loc+ssize+1].
length = dst[loc+ssize+2].
vcn - dst[loc+ssize+1].
vcn;
553 if (IS_ERR(srl) || IS_ERR(drl))
565 drl = ntfs_rl_realloc(drl, dend, dend + 1);
569 ntfs_rl_mm(drl, 1, 0, dend);
594 for (; drl[di].
length; di++) {
595 if (drl[di].vcn + drl[di].length > srl[sstart].vcn)
601 if ((drl[di].vcn == srl[si].vcn) && (drl[di].lcn >= 0) &&
602 (srl[si].lcn >= 0)) {
610 for (dend = di; drl[dend].
length; dend++)
614 marker_vcn = srl[marker =
send].
vcn;
617 for (sfinal = send; sfinal >= 0 && srl[sfinal].
lcn <
LCN_HOLE; sfinal--)
619 for (dfinal = dend; dfinal >= 0 && drl[dfinal].
lcn <
LCN_HOLE; dfinal--)
626 int ss = sfinal - sstart + 1;
629 (drl[dins].vcn == srl[sstart].vcn));
631 ((drl[dins].vcn + drl[dins].length) <=
632 (srl[send - 1].
vcn + srl[send - 1].
length)));
635 if (finish && !drl[dins].length)
637 if (marker && (drl[dins].vcn + drl[dins].length > srl[send - 1].vcn))
640 ntfs_debug(
"dfinal = %i, dend = %i", dfinal, dend);
641 ntfs_debug(
"sstart = %i, sfinal = %i, send = %i", sstart, sfinal, send);
642 ntfs_debug(
"start = %i, finish = %i", start, finish);
643 ntfs_debug(
"ds = %i, ss = %i, dins = %i", ds, ss, dins);
647 drl = ntfs_rl_replace(drl, ds, srl + sstart, ss, dins);
649 drl = ntfs_rl_insert(drl, ds, srl + sstart, ss, dins);
652 drl = ntfs_rl_append(drl, ds, srl + sstart, ss, dins);
654 drl = ntfs_rl_split(drl, ds, srl + sstart, ss, dins);
663 for (ds = dend; drl[
ds].
length; ds++)
666 if (drl[ds].vcn <= marker_vcn) {
669 if (drl[ds].vcn == marker_vcn) {
689 drl = ntfs_rl_realloc_nofail(drl, ds,
706 drl = ntfs_rl_realloc_nofail(drl, ds, ds + 1);
707 drl[
ds].
vcn = marker_vcn;
765 if (!attr || !attr->non_resident || sle64_to_cpu(
766 attr->data.non_resident.lowest_vcn) < (
VCN)0) {
772 vcn = sle64_to_cpu(attr->data.non_resident.lowest_vcn);
776 attr->data.non_resident.mapping_pairs_offset);
778 if (
unlikely(buf < (
u8*)attr || buf > attr_end)) {
780 return ERR_PTR(-
EIO);
788 rl = ntfs_malloc_nofs(rlsize =
PAGE_SIZE);
798 while (buf < attr_end && *buf) {
804 if (((rlpos + 3) *
sizeof(*old_rl)) > rlsize) {
807 rl2 = ntfs_malloc_nofs(rlsize + (
int)
PAGE_SIZE);
830 for (deltaxcn = (
s8)buf[b--];
b; b--)
831 deltaxcn = (deltaxcn << 8) + buf[
b];
850 rl[rlpos].
length = deltaxcn;
863 b = b2 + ((*buf >> 4) & 0xf);
864 if (buf + b > attr_end)
866 for (deltaxcn = (
s8)buf[b--]; b > b2; b--)
867 deltaxcn = (deltaxcn << 8) + buf[
b];
888 "mapping pairs array.");
897 buf += (*buf & 0xf) + ((*buf >> 4) & 0xf) + 1;
905 deltaxcn = sle64_to_cpu(attr->data.non_resident.highest_vcn);
906 if (
unlikely(deltaxcn && vcn - 1 != deltaxcn)) {
909 "non-resident attribute.");
913 if (!attr->data.non_resident.lowest_vcn) {
916 max_cluster = ((sle64_to_cpu(
917 attr->data.non_resident.allocated_size) +
931 if (deltaxcn < max_cluster) {
932 ntfs_debug(
"More extents to follow; deltaxcn "
933 "= 0x%llx, max_cluster = "
935 (
unsigned long long)deltaxcn,
939 vcn += rl[rlpos].
length = max_cluster -
943 }
else if (
unlikely(deltaxcn > max_cluster)) {
945 "deltaxcn = 0x%llx, "
946 "max_cluster = 0x%llx",
947 (
unsigned long long)deltaxcn,
962 ntfs_debug(
"Mapping pairs array successfully decompressed:");
968 if (
likely(!IS_ERR(old_rl)))
977 return ERR_PTR(-
EIO);
1024 return rl[i].lcn + (vcn - rl[i].vcn);
1056 if (
unlikely(!rl || vcn < rl[0].vcn))
1084 static inline int ntfs_get_nr_significant_bytes(
const s64 n)
1094 }
while (l != 0 && l != -1);
1095 j = (n >> 8 * (i - 1)) & 0xff;
1097 if ((n < 0 && j >= 0) || (n > 0 && j < 0))
1137 bool the_end =
false;
1141 BUG_ON(last_vcn >= 0 && first_vcn > last_vcn);
1148 while (rl->
length && first_vcn >= rl[1].
vcn)
1151 first_vcn < rl->vcn))
1157 if (first_vcn > rl->
vcn) {
1167 if (
unlikely(last_vcn >= 0 && rl[1].vcn > last_vcn)) {
1168 s64 s1 = last_vcn + 1;
1170 length = s1 - rl->
vcn;
1173 delta = first_vcn - rl->
vcn;
1175 rls += 1 + ntfs_get_nr_significant_bytes(length - delta);
1188 rls += ntfs_get_nr_significant_bytes(prev_lcn);
1194 for (; rl->
length && !the_end; rl++) {
1203 if (
unlikely(last_vcn >= 0 && rl[1].vcn > last_vcn)) {
1204 s64 s1 = last_vcn + 1;
1206 length = s1 - rl->
vcn;
1210 rls += 1 + ntfs_get_nr_significant_bytes(length);
1220 rls += ntfs_get_nr_significant_bytes(rl->
lcn -
1252 static inline int ntfs_write_significant_bytes(
s8 *dst,
const s8 *dst_max,
1263 *dst++ = l & 0xffll;
1266 }
while (l != 0 && l != -1);
1267 j = (n >> 8 * (i - 1)) & 0xff;
1269 if (n < 0 && j >= 0) {
1274 }
else if (n > 0 && j < 0) {
1323 int ntfs_mapping_pairs_build(
const ntfs_volume *vol,
s8 *dst,
1325 const VCN first_vcn,
const VCN last_vcn,
VCN *
const stop_vcn)
1328 s8 *dst_max, *dst_next;
1330 bool the_end =
false;
1331 s8 len_len, lcn_len;
1335 BUG_ON(last_vcn >= 0 && first_vcn > last_vcn);
1347 while (rl->
length && first_vcn >= rl[1].
vcn)
1350 first_vcn < rl->vcn))
1356 dst_max = dst + dst_len - 1;
1359 if (first_vcn > rl->
vcn) {
1369 if (
unlikely(last_vcn >= 0 && rl[1].vcn > last_vcn)) {
1370 s64 s1 = last_vcn + 1;
1372 length = s1 - rl->
vcn;
1375 delta = first_vcn - rl->
vcn;
1377 len_len = ntfs_write_significant_bytes(dst + 1, dst_max,
1395 lcn_len = ntfs_write_significant_bytes(dst + 1 +
1396 len_len, dst_max, prev_lcn);
1401 dst_next = dst + len_len + lcn_len + 1;
1405 *dst = lcn_len << 4 | len_len;
1412 for (; rl->
length && !the_end; rl++) {
1421 if (
unlikely(last_vcn >= 0 && rl[1].vcn > last_vcn)) {
1422 s64 s1 = last_vcn + 1;
1424 length = s1 - rl->
vcn;
1428 len_len = ntfs_write_significant_bytes(dst + 1, dst_max,
1443 lcn_len = ntfs_write_significant_bytes(dst + 1 +
1444 len_len, dst_max, rl->
lcn - prev_lcn);
1450 dst_next = dst + len_len + lcn_len + 1;
1454 *dst = lcn_len << 4 | len_len;
1463 *stop_vcn = rl->
vcn;
1500 const s64 new_length)
1505 ntfs_debug(
"Entering for new_length 0x%llx.", (
long long)new_length);
1524 "runlist element buffer.");
1534 BUG_ON(new_length < rl->vcn);
1551 old_size = trl - runlist->
rl + 1;
1563 rl->
vcn = new_length;
1569 int new_size = rl - runlist->
rl + 1;
1570 rl = ntfs_rl_realloc(runlist->
rl, old_size, new_size);
1573 "runlist buffer. This just "
1574 "wastes a bit of memory "
1575 "temporarily so we ignore it "
1576 "and return success.");
1580 }
else if (
likely( new_length > rl->
vcn)) {
1587 if ((rl > runlist->
rl) && ((rl - 1)->lcn ==
LCN_HOLE))
1588 (rl - 1)->length = new_length - (rl - 1)->vcn;
1591 old_size = rl - runlist->
rl + 1;
1593 rl = ntfs_rl_realloc(runlist->
rl, old_size,
1597 "buffer, aborting.");
1613 rl->
vcn = new_length;
1644 int ntfs_rl_punch_nolock(
const ntfs_volume *vol, runlist *
const runlist,
1651 bool lcn_fixup =
false;
1653 ntfs_debug(
"Entering for start 0x%llx, length 0x%llx.",
1654 (
long long)start, (
long long)length);
1661 if (
likely(!start && !length))
1680 if (!rl_end->
length && end > rl_end->
vcn)
1686 rl_real_end = rl_end;
1690 old_size = rl_real_end - runlist->
rl + 1;
1697 if (end <= rl[1].vcn) {
1698 ntfs_debug(
"Done (requested hole is already sparse).");
1713 memmove(rl, rl_end, (rl_real_end - rl_end + 1) *
1716 if (end > rl->
vcn) {
1717 delta = end - rl->
vcn;
1727 rl = ntfs_rl_realloc(runlist->
rl, old_size,
1728 old_size - (rl_end - rl));
1731 "runlist buffer. This just "
1732 "wastes a bit of memory "
1733 "temporarily so we ignore it "
1734 "and return success.");
1745 if (start == rl->
vcn) {
1761 if (end >= rl[1].vcn) {
1772 trl = ntfs_rl_realloc(runlist->
rl, old_size, old_size + 1);
1776 if (runlist->
rl != trl) {
1777 rl = trl + (rl - runlist->
rl);
1778 rl_end = trl + (rl_end - runlist->
rl);
1779 rl_real_end = trl + (rl_real_end - runlist->
rl);
1784 memmove(rl + 1, rl, (rl_real_end - rl + 1) *
sizeof(*rl));
1791 if (rl->
lcn >= 0 || lcn_fixup)
1811 memmove(rl, rl_end, (rl_real_end - rl_end + 1) *
1816 goto shrink_allocation;
1829 if (end >= rl[1].vcn) {
1834 if (rl[1].length && end >= rl[2].vcn) {
1842 trl = ntfs_rl_realloc(runlist->
rl, old_size, old_size + 1);
1846 if (runlist->
rl != trl) {
1847 rl = trl + (rl - runlist->
rl);
1848 rl_end = trl + (rl_end - runlist->
rl);
1849 rl_real_end = trl + (rl_real_end - runlist->
rl);
1877 trl = ntfs_rl_realloc(runlist->
rl, old_size, old_size + 2);
1881 if (runlist->
rl != trl) {
1882 rl = trl + (rl - runlist->
rl);
1883 rl_end = trl + (rl_end - runlist->
rl);
1884 rl_real_end = trl + (rl_real_end - runlist->
rl);
1888 memmove(rl + 2, rl, (rl_real_end - rl + 1) *
sizeof(*rl));
1896 delta = end - rl->
vcn;
1903 ntfs_error(vol->
sb,
"Not enough memory to extend runlist buffer.");