#include <linux/overflow.h>
#include "ntfs.h"
#include "attrib.h"
static inline void ntfs_rl_mm(struct runlist_element *base, int dst, int src, int size)
{
if (likely((dst != src) && (size > 0)))
memmove(base + dst, base + src, size * sizeof(*base));
}
static inline void ntfs_rl_mc(struct runlist_element *dstbase, int dst,
struct runlist_element *srcbase, int src, int size)
{
if (likely(size > 0))
memcpy(dstbase + dst, srcbase + src, size * sizeof(*dstbase));
}
struct runlist_element *ntfs_rl_realloc(struct runlist_element *rl,
int old_size, int new_size)
{
struct runlist_element *new_rl;
old_size = old_size * sizeof(*rl);
new_size = new_size * sizeof(*rl);
if (old_size == new_size)
return rl;
new_rl = kvzalloc(new_size, GFP_NOFS);
if (unlikely(!new_rl))
return ERR_PTR(-ENOMEM);
if (likely(rl != NULL)) {
if (unlikely(old_size > new_size))
old_size = new_size;
memcpy(new_rl, rl, old_size);
kvfree(rl);
}
return new_rl;
}
static inline struct runlist_element *ntfs_rl_realloc_nofail(struct runlist_element *rl,
int old_size, int new_size)
{
struct runlist_element *new_rl;
old_size = old_size * sizeof(*rl);
new_size = new_size * sizeof(*rl);
if (old_size == new_size)
return rl;
new_rl = kvmalloc(new_size, GFP_NOFS | __GFP_NOFAIL);
if (likely(rl != NULL)) {
if (unlikely(old_size > new_size))
old_size = new_size;
memcpy(new_rl, rl, old_size);
kvfree(rl);
}
return new_rl;
}
static inline bool ntfs_are_rl_mergeable(struct runlist_element *dst,
struct runlist_element *src)
{
if ((dst->lcn == LCN_RL_NOT_MAPPED) && (src->lcn == LCN_RL_NOT_MAPPED))
return true;
if ((dst->vcn + dst->length) != src->vcn)
return false;
if ((dst->lcn >= 0) && (src->lcn >= 0) &&
((dst->lcn + dst->length) == src->lcn))
return true;
if ((dst->lcn == LCN_HOLE) && (src->lcn == LCN_HOLE))
return true;
if ((dst->lcn == LCN_DELALLOC) && (src->lcn == LCN_DELALLOC))
return true;
return false;
}
static inline void __ntfs_rl_merge(struct runlist_element *dst, struct runlist_element *src)
{
dst->length += src->length;
}
static inline struct runlist_element *ntfs_rl_append(struct runlist_element *dst,
int dsize, struct runlist_element *src, int ssize, int loc,
size_t *new_size)
{
bool right = false;
int marker;
if ((loc + 1) < dsize)
right = ntfs_are_rl_mergeable(src + ssize - 1, dst + loc + 1);
dst = ntfs_rl_realloc(dst, dsize, dsize + ssize - right);
if (IS_ERR(dst))
return dst;
*new_size = dsize + ssize - right;
if (right)
__ntfs_rl_merge(src + ssize - 1, dst + loc + 1);
marker = loc + ssize + 1;
ntfs_rl_mm(dst, marker, loc + 1 + right, dsize - (loc + 1 + right));
ntfs_rl_mc(dst, loc + 1, src, 0, ssize);
dst[loc].length = dst[loc + 1].vcn - dst[loc].vcn;
if (dst[marker].lcn == LCN_ENOENT)
dst[marker].vcn = dst[marker - 1].vcn + dst[marker - 1].length;
return dst;
}
static inline struct runlist_element *ntfs_rl_insert(struct runlist_element *dst,
int dsize, struct runlist_element *src, int ssize, int loc,
size_t *new_size)
{
bool left = false;
bool disc = false;
int marker;
if (loc == 0)
disc = (src[0].vcn > 0);
else {
s64 merged_length;
left = ntfs_are_rl_mergeable(dst + loc - 1, src);
merged_length = dst[loc - 1].length;
if (left)
merged_length += src->length;
disc = (src[0].vcn > dst[loc - 1].vcn + merged_length);
}
dst = ntfs_rl_realloc(dst, dsize, dsize + ssize - left + disc);
if (IS_ERR(dst))
return dst;
*new_size = dsize + ssize - left + disc;
if (left)
__ntfs_rl_merge(dst + loc - 1, src);
marker = loc + ssize - left + disc;
ntfs_rl_mm(dst, marker, loc, dsize - loc);
ntfs_rl_mc(dst, loc + disc, src, left, ssize - left);
dst[marker].vcn = dst[marker - 1].vcn + dst[marker - 1].length;
if (dst[marker].lcn == LCN_HOLE || dst[marker].lcn == LCN_RL_NOT_MAPPED ||
dst[marker].lcn == LCN_DELALLOC)
dst[marker].length = dst[marker + 1].vcn - dst[marker].vcn;
if (disc) {
if (loc > 0) {
dst[loc].vcn = dst[loc - 1].vcn + dst[loc - 1].length;
dst[loc].length = dst[loc + 1].vcn - dst[loc].vcn;
} else {
dst[loc].vcn = 0;
dst[loc].length = dst[loc + 1].vcn;
}
dst[loc].lcn = LCN_RL_NOT_MAPPED;
}
return dst;
}
static inline struct runlist_element *ntfs_rl_replace(struct runlist_element *dst,
int dsize, struct runlist_element *src, int ssize, int loc,
size_t *new_size)
{
int delta;
bool left = false;
bool right = false;
int tail;
int marker;
if ((loc + 1) < dsize)
right = ntfs_are_rl_mergeable(src + ssize - 1, dst + loc + 1);
if (loc > 0)
left = ntfs_are_rl_mergeable(dst + loc - 1, src);
delta = ssize - 1 - left - right;
if (delta > 0) {
dst = ntfs_rl_realloc(dst, dsize, dsize + delta);
if (IS_ERR(dst))
return dst;
}
*new_size = dsize + delta;
if (right)
__ntfs_rl_merge(src + ssize - 1, dst + loc + 1);
if (left)
__ntfs_rl_merge(dst + loc - 1, src);
tail = loc + right + 1;
marker = loc + ssize - left;
ntfs_rl_mm(dst, marker, tail, dsize - tail);
ntfs_rl_mc(dst, loc, src, left, ssize - left);
if (dsize - tail > 0 && dst[marker].lcn == LCN_ENOENT)
dst[marker].vcn = dst[marker - 1].vcn + dst[marker - 1].length;
return dst;
}
static inline struct runlist_element *ntfs_rl_split(struct runlist_element *dst, int dsize,
struct runlist_element *src, int ssize, int loc,
size_t *new_size)
{
dst = ntfs_rl_realloc(dst, dsize, dsize + ssize + 1);
if (IS_ERR(dst))
return dst;
*new_size = dsize + ssize + 1;
ntfs_rl_mm(dst, loc + 1 + ssize, loc, dsize - loc);
ntfs_rl_mc(dst, loc + 1, src, 0, ssize);
dst[loc].length = dst[loc+1].vcn - dst[loc].vcn;
dst[loc+ssize+1].vcn = dst[loc+ssize].vcn + dst[loc+ssize].length;
dst[loc+ssize+1].length = dst[loc+ssize+2].vcn - dst[loc+ssize+1].vcn;
return dst;
}
struct runlist_element *ntfs_runlists_merge(struct runlist *d_runlist,
struct runlist_element *srl, size_t s_rl_count,
size_t *new_rl_count)
{
int di, si;
int sstart;
int dins;
int dend, send;
int dfinal, sfinal;
int marker = 0;
s64 marker_vcn = 0;
struct runlist_element *drl = d_runlist->rl, *rl;
#ifdef DEBUG
ntfs_debug("dst:");
ntfs_debug_dump_runlist(drl);
ntfs_debug("src:");
ntfs_debug_dump_runlist(srl);
#endif
if (unlikely(!srl))
return drl;
if (IS_ERR(srl) || IS_ERR(drl))
return ERR_PTR(-EINVAL);
if (s_rl_count == 0) {
for (; srl[s_rl_count].length; s_rl_count++)
;
s_rl_count++;
}
if (unlikely(!drl)) {
drl = srl;
if (unlikely(drl[0].vcn)) {
drl = ntfs_rl_realloc(drl, s_rl_count, s_rl_count + 1);
if (IS_ERR(drl))
return drl;
ntfs_rl_mm(drl, 1, 0, s_rl_count);
drl[0].vcn = 0;
drl[0].lcn = LCN_RL_NOT_MAPPED;
drl[0].length = drl[1].vcn;
s_rl_count++;
}
*new_rl_count = s_rl_count;
goto finished;
}
if (d_runlist->count < 1 || s_rl_count < 2)
return ERR_PTR(-EINVAL);
si = di = 0;
while (srl[si].length && srl[si].lcn < LCN_HOLE)
si++;
WARN_ON(!srl[si].length);
sstart = si;
rl = __ntfs_attr_find_vcn_nolock(d_runlist, srl[sstart].vcn);
if (IS_ERR(rl))
di = (int)d_runlist->count - 1;
else
di = (int)(rl - d_runlist->rl);
dins = di;
if ((drl[di].vcn == srl[si].vcn) && (drl[di].lcn >= 0) &&
(srl[si].lcn >= 0)) {
ntfs_error(NULL, "Run lists overlap. Cannot merge!");
return ERR_PTR(-ERANGE);
}
send = (int)s_rl_count - 1;
dend = (int)d_runlist->count - 1;
if (srl[send].lcn == LCN_ENOENT)
marker_vcn = srl[marker = send].vcn;
for (sfinal = send; sfinal >= 0 && srl[sfinal].lcn < LCN_HOLE; sfinal--)
;
for (dfinal = dend; dfinal >= 0 && drl[dfinal].lcn < LCN_HOLE; dfinal--)
;
{
bool start;
bool finish;
int ds = dend + 1;
int ss = sfinal - sstart + 1;
start = ((drl[dins].lcn < LCN_RL_NOT_MAPPED) ||
(drl[dins].vcn == srl[sstart].vcn));
finish = ((drl[dins].lcn >= LCN_RL_NOT_MAPPED) &&
((drl[dins].vcn + drl[dins].length) <=
(srl[send - 1].vcn + srl[send - 1].length)));
if (finish && !drl[dins].length)
ss++;
if (marker && (drl[dins].vcn + drl[dins].length > srl[send - 1].vcn))
finish = false;
if (start) {
if (finish)
drl = ntfs_rl_replace(drl, ds, srl + sstart, ss, dins, new_rl_count);
else
drl = ntfs_rl_insert(drl, ds, srl + sstart, ss, dins, new_rl_count);
} else {
if (finish)
drl = ntfs_rl_append(drl, ds, srl + sstart, ss, dins, new_rl_count);
else
drl = ntfs_rl_split(drl, ds, srl + sstart, ss, dins, new_rl_count);
}
if (IS_ERR(drl)) {
ntfs_error(NULL, "Merge failed.");
return drl;
}
kvfree(srl);
if (marker) {
ntfs_debug("Triggering marker code.");
for (ds = dend; drl[ds].length; ds++)
;
if (drl[ds].vcn <= marker_vcn) {
int slots = 0;
if (drl[ds].vcn == marker_vcn) {
ntfs_debug("Old marker = 0x%llx, replacing with LCN_ENOENT.",
drl[ds].lcn);
drl[ds].lcn = LCN_ENOENT;
goto finished;
}
if (drl[ds].lcn == LCN_ENOENT) {
ds--;
slots = 1;
}
if (drl[ds].lcn != LCN_RL_NOT_MAPPED) {
if (!slots) {
drl = ntfs_rl_realloc_nofail(drl, ds,
ds + 2);
slots = 2;
*new_rl_count += 2;
}
ds++;
if (slots != 1)
drl[ds].vcn = drl[ds - 1].vcn +
drl[ds - 1].length;
drl[ds].lcn = LCN_RL_NOT_MAPPED;
slots--;
}
drl[ds].length = marker_vcn - drl[ds].vcn;
ds++;
if (!slots) {
drl = ntfs_rl_realloc_nofail(drl, ds, ds + 1);
*new_rl_count += 1;
}
drl[ds].vcn = marker_vcn;
drl[ds].lcn = LCN_ENOENT;
drl[ds].length = (s64)0;
}
}
}
finished:
ntfs_debug("Merged runlist:");
ntfs_debug_dump_runlist(drl);
return drl;
}
struct runlist_element *ntfs_mapping_pairs_decompress(const struct ntfs_volume *vol,
const struct attr_record *attr, struct runlist *old_runlist,
size_t *new_rl_count)
{
s64 vcn;
s64 lcn;
s64 deltaxcn;
struct runlist_element *rl, *new_rl;
u8 *buf;
u8 *attr_end;
int rlsize;
u16 rlpos;
u8 b;
u64 lowest_vcn;
#ifdef DEBUG
if (!attr || !attr->non_resident) {
ntfs_error(vol->sb, "Invalid arguments.");
return ERR_PTR(-EINVAL);
}
#endif
lowest_vcn = le64_to_cpu(attr->data.non_resident.lowest_vcn);
if (overflows_type(lowest_vcn, vcn)) {
ntfs_error(vol->sb, "Invalid lowest_vcn in mapping pairs.");
return ERR_PTR(-EIO);
}
vcn = lowest_vcn;
lcn = 0;
buf = (u8 *)attr +
le16_to_cpu(attr->data.non_resident.mapping_pairs_offset);
attr_end = (u8 *)attr + le32_to_cpu(attr->length);
if (unlikely(buf < (u8 *)attr || buf >= attr_end)) {
ntfs_error(vol->sb, "Corrupt attribute.");
return ERR_PTR(-EIO);
}
rlpos = 0;
rl = kvzalloc(rlsize = PAGE_SIZE, GFP_NOFS);
if (unlikely(!rl))
return ERR_PTR(-ENOMEM);
if (vcn) {
rl->vcn = 0;
rl->lcn = LCN_RL_NOT_MAPPED;
rl->length = vcn;
rlpos++;
}
while (buf < attr_end && *buf) {
if (((rlpos + 3) * sizeof(*rl)) > rlsize) {
struct runlist_element *rl2;
rl2 = kvzalloc(rlsize + PAGE_SIZE, GFP_NOFS);
if (unlikely(!rl2)) {
kvfree(rl);
return ERR_PTR(-ENOMEM);
}
memcpy(rl2, rl, rlsize);
kvfree(rl);
rl = rl2;
rlsize += PAGE_SIZE;
}
rl[rlpos].vcn = vcn;
b = *buf & 0xf;
if (b) {
if (unlikely(buf + b >= attr_end))
goto io_error;
for (deltaxcn = (s8)buf[b--]; b; b--)
deltaxcn = (deltaxcn << 8) + buf[b];
} else {
ntfs_error(vol->sb, "Missing length entry in mapping pairs array.");
deltaxcn = (s64)-1;
}
if (unlikely(deltaxcn < 0)) {
ntfs_error(vol->sb, "Invalid length in mapping pairs array.");
goto err_out;
}
rl[rlpos].length = deltaxcn;
if (unlikely(check_add_overflow(vcn, deltaxcn, &vcn))) {
ntfs_error(vol->sb, "VCN overflow in mapping pairs array.");
goto err_out;
}
if (!(*buf & 0xf0))
rl[rlpos].lcn = LCN_HOLE;
else {
u8 b2 = *buf & 0xf;
b = b2 + ((*buf >> 4) & 0xf);
if (buf + b >= attr_end)
goto io_error;
for (deltaxcn = (s8)buf[b--]; b > b2; b--)
deltaxcn = (deltaxcn << 8) + buf[b];
if (unlikely(check_add_overflow(lcn, deltaxcn, &lcn))) {
ntfs_error(vol->sb,
"LCN overflow in mapping pairs array.");
goto err_out;
}
#ifdef DEBUG
if (vol->major_ver < 3) {
if (unlikely(deltaxcn == -1))
ntfs_error(vol->sb, "lcn delta == -1");
if (unlikely(lcn == -1))
ntfs_error(vol->sb, "lcn == -1");
}
#endif
if (unlikely(lcn < -1)) {
ntfs_error(vol->sb, "Invalid s64 < -1 in mapping pairs array.");
goto err_out;
}
if ((lcn != -1) && !rl[rlpos].length) {
ntfs_error(vol->sb,
"Invalid zero-sized data run(lcn : %lld).\n",
lcn);
goto err_out;
}
rl[rlpos].lcn = lcn;
}
if (rl[rlpos].length)
rlpos++;
buf += (*buf & 0xf) + ((*buf >> 4) & 0xf) + 1;
}
if (unlikely(buf >= attr_end))
goto io_error;
deltaxcn = le64_to_cpu(attr->data.non_resident.highest_vcn);
if (unlikely(deltaxcn && vcn - 1 != deltaxcn)) {
mpa_err:
ntfs_error(vol->sb, "Corrupt mapping pairs array in non-resident attribute.");
goto err_out;
}
if (!attr->data.non_resident.lowest_vcn) {
s64 max_cluster;
max_cluster = ((le64_to_cpu(attr->data.non_resident.allocated_size) +
vol->cluster_size - 1) >>
vol->cluster_size_bits) - 1;
if (deltaxcn) {
if (deltaxcn < max_cluster) {
ntfs_debug("More extents to follow; deltaxcn = 0x%llx, max_cluster = 0x%llx",
deltaxcn, max_cluster);
rl[rlpos].vcn = vcn;
vcn += rl[rlpos].length = max_cluster -
deltaxcn;
rl[rlpos].lcn = LCN_RL_NOT_MAPPED;
rlpos++;
} else if (unlikely(deltaxcn > max_cluster)) {
ntfs_error(vol->sb,
"Corrupt attribute. deltaxcn = 0x%llx, max_cluster = 0x%llx",
deltaxcn, max_cluster);
goto mpa_err;
}
}
rl[rlpos].lcn = LCN_ENOENT;
} else
rl[rlpos].lcn = LCN_RL_NOT_MAPPED;
rl[rlpos].vcn = vcn;
rl[rlpos].length = (s64)0;
if (!old_runlist || !old_runlist->rl) {
*new_rl_count = rlpos + 1;
ntfs_debug("Mapping pairs array successfully decompressed:");
ntfs_debug_dump_runlist(rl);
return rl;
}
new_rl = ntfs_runlists_merge(old_runlist, rl, rlpos + 1, new_rl_count);
if (!IS_ERR(new_rl))
return new_rl;
kvfree(rl);
ntfs_error(vol->sb, "Failed to merge runlists.");
return new_rl;
io_error:
ntfs_error(vol->sb, "Corrupt attribute.");
err_out:
kvfree(rl);
return ERR_PTR(-EIO);
}
s64 ntfs_rl_vcn_to_lcn(const struct runlist_element *rl, const s64 vcn)
{
int i;
if (unlikely(!rl))
return LCN_RL_NOT_MAPPED;
if (unlikely(vcn < rl[0].vcn))
return LCN_ENOENT;
for (i = 0; likely(rl[i].length); i++) {
if (vcn < rl[i+1].vcn) {
if (likely(rl[i].lcn >= 0))
return rl[i].lcn + (vcn - rl[i].vcn);
return rl[i].lcn;
}
}
if (likely(rl[i].lcn < 0))
return rl[i].lcn;
return LCN_ENOENT;
}
struct runlist_element *ntfs_rl_find_vcn_nolock(struct runlist_element *rl, const s64 vcn)
{
if (unlikely(!rl || vcn < rl[0].vcn))
return NULL;
while (likely(rl->length)) {
if (unlikely(vcn < rl[1].vcn)) {
if (likely(rl->lcn >= LCN_HOLE))
return rl;
return NULL;
}
rl++;
}
if (likely(rl->lcn == LCN_ENOENT))
return rl;
return NULL;
}
static inline int ntfs_get_nr_significant_bytes(const s64 n)
{
s64 l = n;
int i;
s8 j;
i = 0;
do {
l >>= 8;
i++;
} while (l != 0 && l != -1);
j = (n >> 8 * (i - 1)) & 0xff;
if ((n < 0 && j >= 0) || (n > 0 && j < 0))
i++;
return i;
}
int ntfs_get_size_for_mapping_pairs(const struct ntfs_volume *vol,
const struct runlist_element *rl, const s64 first_vcn,
const s64 last_vcn, int max_mp_size)
{
s64 prev_lcn;
int rls;
bool the_end = false;
if (first_vcn < 0 || last_vcn < -1)
return -EINVAL;
if (last_vcn >= 0 && first_vcn > last_vcn)
return -EINVAL;
if (!rl) {
WARN_ON(first_vcn);
WARN_ON(last_vcn > 0);
return 1;
}
if (max_mp_size <= 0)
max_mp_size = INT_MAX;
while (rl->length && first_vcn >= rl[1].vcn)
rl++;
if (unlikely((!rl->length && first_vcn > rl->vcn) ||
first_vcn < rl->vcn))
return -EINVAL;
prev_lcn = 0;
rls = 1;
if (first_vcn > rl->vcn) {
s64 delta, length = rl->length;
if (unlikely(length < 0 || rl->lcn < LCN_HOLE))
goto err_out;
if (unlikely(last_vcn >= 0 && rl[1].vcn > last_vcn)) {
s64 s1 = last_vcn + 1;
if (unlikely(rl[1].vcn > s1))
length = s1 - rl->vcn;
the_end = true;
}
delta = first_vcn - rl->vcn;
rls += 1 + ntfs_get_nr_significant_bytes(length - delta);
if (likely(rl->lcn >= 0 || vol->major_ver < 3)) {
prev_lcn = rl->lcn;
if (likely(rl->lcn >= 0))
prev_lcn += delta;
rls += ntfs_get_nr_significant_bytes(prev_lcn);
}
rl++;
}
for (; rl->length && !the_end; rl++) {
s64 length = rl->length;
if (unlikely(length < 0 || rl->lcn < LCN_HOLE))
goto err_out;
if (unlikely(last_vcn >= 0 && rl[1].vcn > last_vcn)) {
s64 s1 = last_vcn + 1;
if (unlikely(rl[1].vcn > s1))
length = s1 - rl->vcn;
the_end = true;
}
rls += 1 + ntfs_get_nr_significant_bytes(length);
if (likely(rl->lcn >= 0 || vol->major_ver < 3)) {
rls += ntfs_get_nr_significant_bytes(rl->lcn -
prev_lcn);
prev_lcn = rl->lcn;
}
if (rls > max_mp_size)
break;
}
return rls;
err_out:
if (rl->lcn == LCN_RL_NOT_MAPPED)
rls = -EINVAL;
else
rls = -EIO;
return rls;
}
static inline int ntfs_write_significant_bytes(s8 *dst, const s8 *dst_max,
const s64 n)
{
s64 l = n;
int i;
s8 j;
i = 0;
do {
if (unlikely(dst > dst_max))
goto err_out;
*dst++ = l & 0xffll;
l >>= 8;
i++;
} while (l != 0 && l != -1);
j = (n >> 8 * (i - 1)) & 0xff;
if (n < 0 && j >= 0) {
if (unlikely(dst > dst_max))
goto err_out;
i++;
*dst = (s8)-1;
} else if (n > 0 && j < 0) {
if (unlikely(dst > dst_max))
goto err_out;
i++;
*dst = (s8)0;
}
return i;
err_out:
return -ENOSPC;
}
int ntfs_mapping_pairs_build(const struct ntfs_volume *vol, s8 *dst,
const int dst_len, const struct runlist_element *rl,
const s64 first_vcn, const s64 last_vcn, s64 *const stop_vcn,
struct runlist_element **stop_rl, unsigned int *de_cluster_count)
{
s64 prev_lcn;
s8 *dst_max, *dst_next;
int err = -ENOSPC;
bool the_end = false;
s8 len_len, lcn_len;
unsigned int de_cnt = 0;
if (first_vcn < 0 || last_vcn < -1 || dst_len < 1)
return -EINVAL;
if (last_vcn >= 0 && first_vcn > last_vcn)
return -EINVAL;
if (!rl) {
WARN_ON(first_vcn || last_vcn > 0);
if (stop_vcn)
*stop_vcn = 0;
*dst = 0;
return 0;
}
while (rl->length && first_vcn >= rl[1].vcn)
rl++;
if (unlikely((!rl->length && first_vcn > rl->vcn) ||
first_vcn < rl->vcn))
return -EINVAL;
dst_max = dst + dst_len - 1;
prev_lcn = 0;
if (first_vcn > rl->vcn) {
s64 delta, length = rl->length;
if (unlikely(length < 0 || rl->lcn < LCN_HOLE))
goto err_out;
if (unlikely(last_vcn >= 0 && rl[1].vcn > last_vcn)) {
s64 s1 = last_vcn + 1;
if (unlikely(rl[1].vcn > s1))
length = s1 - rl->vcn;
the_end = true;
}
delta = first_vcn - rl->vcn;
len_len = ntfs_write_significant_bytes(dst + 1, dst_max,
length - delta);
if (unlikely(len_len < 0))
goto size_err;
if (likely(rl->lcn >= 0 || vol->major_ver < 3)) {
prev_lcn = rl->lcn;
if (likely(rl->lcn >= 0))
prev_lcn += delta;
lcn_len = ntfs_write_significant_bytes(dst + 1 +
len_len, dst_max, prev_lcn);
if (unlikely(lcn_len < 0))
goto size_err;
} else
lcn_len = 0;
dst_next = dst + len_len + lcn_len + 1;
if (unlikely(dst_next > dst_max))
goto size_err;
*dst = lcn_len << 4 | len_len;
dst = dst_next;
rl++;
}
for (; rl->length && !the_end; rl++) {
s64 length = rl->length;
if (unlikely(length < 0 || rl->lcn < LCN_HOLE))
goto err_out;
if (unlikely(last_vcn >= 0 && rl[1].vcn > last_vcn)) {
s64 s1 = last_vcn + 1;
if (unlikely(rl[1].vcn > s1))
length = s1 - rl->vcn;
the_end = true;
}
len_len = ntfs_write_significant_bytes(dst + 1, dst_max,
length);
if (unlikely(len_len < 0))
goto size_err;
if (likely(rl->lcn >= 0 || vol->major_ver < 3)) {
lcn_len = ntfs_write_significant_bytes(dst + 1 +
len_len, dst_max, rl->lcn - prev_lcn);
if (unlikely(lcn_len < 0))
goto size_err;
prev_lcn = rl->lcn;
} else {
if (rl->lcn == LCN_DELALLOC)
de_cnt += rl->length;
lcn_len = 0;
}
dst_next = dst + len_len + lcn_len + 1;
if (unlikely(dst_next > dst_max))
goto size_err;
*dst = lcn_len << 4 | len_len;
dst = dst_next;
}
if (de_cluster_count)
*de_cluster_count = de_cnt;
err = 0;
size_err:
if (stop_vcn)
*stop_vcn = rl->vcn;
if (stop_rl)
*stop_rl = (struct runlist_element *)rl;
*dst = 0;
return err;
err_out:
if (rl->lcn == LCN_RL_NOT_MAPPED)
err = -EINVAL;
else
err = -EIO;
return err;
}
int ntfs_rl_truncate_nolock(const struct ntfs_volume *vol, struct runlist *const runlist,
const s64 new_length)
{
struct runlist_element *rl;
int old_size;
ntfs_debug("Entering for new_length 0x%llx.", (long long)new_length);
if (!runlist || new_length < 0)
return -EINVAL;
rl = runlist->rl;
if (new_length < rl->vcn)
return -EINVAL;
while (likely(rl->length && new_length >= rl[1].vcn))
rl++;
if (rl->length) {
struct runlist_element *trl;
bool is_end;
ntfs_debug("Shrinking runlist.");
trl = rl + 1;
while (likely(trl->length))
trl++;
old_size = trl - runlist->rl + 1;
rl->length = new_length - rl->vcn;
is_end = false;
if (rl->length) {
rl++;
if (!rl->length)
is_end = true;
rl->vcn = new_length;
rl->length = 0;
}
rl->lcn = LCN_ENOENT;
runlist->count = rl - runlist->rl + 1;
if (!is_end) {
int new_size = rl - runlist->rl + 1;
rl = ntfs_rl_realloc(runlist->rl, old_size, new_size);
if (IS_ERR(rl))
ntfs_warning(vol->sb,
"Failed to shrink runlist buffer. This just wastes a bit of memory temporarily so we ignore it and return success.");
else
runlist->rl = rl;
}
} else if (likely( new_length > rl->vcn)) {
ntfs_debug("Expanding runlist.");
if ((rl > runlist->rl) && ((rl - 1)->lcn == LCN_HOLE))
(rl - 1)->length = new_length - (rl - 1)->vcn;
else {
old_size = rl - runlist->rl + 1;
rl = ntfs_rl_realloc(runlist->rl, old_size,
old_size + 1);
if (IS_ERR(rl)) {
ntfs_error(vol->sb, "Failed to expand runlist buffer, aborting.");
return PTR_ERR(rl);
}
runlist->rl = rl;
rl += old_size - 1;
rl->lcn = LCN_HOLE;
rl->length = new_length - rl->vcn;
rl++;
rl->length = 0;
runlist->count = old_size + 1;
}
rl->vcn = new_length;
rl->lcn = LCN_ENOENT;
} else {
rl->lcn = LCN_ENOENT;
}
ntfs_debug("Done.");
return 0;
}
int ntfs_rl_sparse(struct runlist_element *rl)
{
struct runlist_element *rlc;
if (!rl)
return -EINVAL;
for (rlc = rl; rlc->length; rlc++)
if (rlc->lcn < 0) {
if (rlc->lcn != LCN_HOLE && rlc->lcn != LCN_DELALLOC) {
pr_err("%s: bad runlist\n", __func__);
return -EINVAL;
}
return 1;
}
return 0;
}
s64 ntfs_rl_get_compressed_size(struct ntfs_volume *vol, struct runlist_element *rl)
{
struct runlist_element *rlc;
s64 ret = 0;
if (!rl)
return -EINVAL;
for (rlc = rl; rlc->length; rlc++) {
if (rlc->lcn < 0) {
if (rlc->lcn != LCN_HOLE && rlc->lcn != LCN_DELALLOC) {
ntfs_error(vol->sb, "%s: bad runlist, rlc->lcn : %lld",
__func__, rlc->lcn);
return -EINVAL;
}
} else
ret += rlc->length;
}
return NTFS_CLU_TO_B(vol, ret);
}
static inline bool ntfs_rle_lcn_contiguous(struct runlist_element *left_rle,
struct runlist_element *right_rle)
{
if (left_rle->lcn > LCN_HOLE &&
left_rle->lcn + left_rle->length == right_rle->lcn)
return true;
else if (left_rle->lcn == LCN_HOLE && right_rle->lcn == LCN_HOLE)
return true;
else
return false;
}
static inline bool ntfs_rle_contain(struct runlist_element *rle, s64 vcn)
{
if (rle->length > 0 &&
vcn >= rle->vcn && vcn < rle->vcn + rle->length)
return true;
else
return false;
}
struct runlist_element *ntfs_rl_insert_range(struct runlist_element *dst_rl, int dst_cnt,
struct runlist_element *src_rl, int src_cnt,
size_t *new_rl_cnt)
{
struct runlist_element *i_rl, *new_rl, *src_rl_origin = src_rl;
struct runlist_element dst_rl_split;
s64 start_vcn;
int new_1st_cnt, new_2nd_cnt, new_3rd_cnt, new_cnt;
if (!dst_rl || !src_rl || !new_rl_cnt)
return ERR_PTR(-EINVAL);
if (dst_cnt <= 0 || src_cnt <= 0)
return ERR_PTR(-EINVAL);
if (!(dst_rl[dst_cnt - 1].lcn == LCN_ENOENT &&
dst_rl[dst_cnt - 1].length == 0) ||
src_rl[src_cnt - 1].lcn < LCN_HOLE)
return ERR_PTR(-EINVAL);
start_vcn = src_rl[0].vcn;
i_rl = ntfs_rl_find_vcn_nolock(dst_rl, start_vcn);
if (!i_rl ||
(i_rl->lcn == LCN_ENOENT && i_rl->vcn != start_vcn) ||
(i_rl->lcn != LCN_ENOENT && !ntfs_rle_contain(i_rl, start_vcn)))
return ERR_PTR(-EINVAL);
new_1st_cnt = (int)(i_rl - dst_rl);
if (new_1st_cnt > dst_cnt)
return ERR_PTR(-EINVAL);
new_3rd_cnt = dst_cnt - new_1st_cnt;
if (new_3rd_cnt < 1)
return ERR_PTR(-EINVAL);
if (i_rl[0].vcn != start_vcn) {
if (i_rl[0].lcn == LCN_HOLE && src_rl[0].lcn == LCN_HOLE)
goto merge_src_rle;
dst_rl_split.vcn = i_rl[0].vcn;
dst_rl_split.length = start_vcn - i_rl[0].vcn;
dst_rl_split.lcn = i_rl[0].lcn;
i_rl[0].vcn = start_vcn;
i_rl[0].length -= dst_rl_split.length;
i_rl[0].lcn += dst_rl_split.length;
} else {
struct runlist_element *dst_rle, *src_rle;
merge_src_rle:
dst_rl_split.lcn = LCN_ENOENT;
dst_rle = &dst_rl[new_1st_cnt - 1];
src_rle = &src_rl[0];
if (new_1st_cnt > 0 && ntfs_rle_lcn_contiguous(dst_rle, src_rle)) {
WARN_ON(dst_rle->vcn + dst_rle->length != src_rle->vcn);
dst_rle->length += src_rle->length;
src_rl++;
src_cnt--;
} else {
dst_rle = &dst_rl[new_1st_cnt];
src_rle = &src_rl[src_cnt - 1];
if (ntfs_rle_lcn_contiguous(dst_rle, src_rle)) {
dst_rle->length += src_rle->length;
src_cnt--;
}
}
}
new_2nd_cnt = src_cnt;
new_cnt = new_1st_cnt + new_2nd_cnt + new_3rd_cnt;
new_cnt += dst_rl_split.lcn >= LCN_HOLE ? 1 : 0;
new_rl = kvcalloc(new_cnt, sizeof(*new_rl), GFP_NOFS);
if (!new_rl)
return ERR_PTR(-ENOMEM);
ntfs_rl_mc(new_rl, 0, dst_rl, 0, new_1st_cnt);
if (dst_rl_split.lcn >= LCN_HOLE) {
ntfs_rl_mc(new_rl, new_1st_cnt, &dst_rl_split, 0, 1);
new_1st_cnt++;
}
ntfs_rl_mc(new_rl, new_1st_cnt, src_rl, 0, new_2nd_cnt);
if (new_3rd_cnt >= 1) {
struct runlist_element *rl, *rl_3rd;
int dst_1st_cnt = dst_rl_split.lcn >= LCN_HOLE ?
new_1st_cnt - 1 : new_1st_cnt;
ntfs_rl_mc(new_rl, new_1st_cnt + new_2nd_cnt,
dst_rl, dst_1st_cnt, new_3rd_cnt);
if (new_1st_cnt + new_2nd_cnt == 0) {
rl_3rd = &new_rl[new_1st_cnt + new_2nd_cnt + 1];
rl = &new_rl[new_1st_cnt + new_2nd_cnt];
} else {
rl_3rd = &new_rl[new_1st_cnt + new_2nd_cnt];
rl = &new_rl[new_1st_cnt + new_2nd_cnt - 1];
}
do {
rl_3rd->vcn = rl->vcn + rl->length;
if (rl_3rd->length <= 0)
break;
rl = rl_3rd;
rl_3rd++;
} while (1);
}
*new_rl_cnt = new_1st_cnt + new_2nd_cnt + new_3rd_cnt;
kvfree(dst_rl);
kvfree(src_rl_origin);
return new_rl;
}
struct runlist_element *ntfs_rl_punch_hole(struct runlist_element *dst_rl, int dst_cnt,
s64 start_vcn, s64 len,
struct runlist_element **punch_rl,
size_t *new_rl_cnt)
{
struct runlist_element *s_rl, *e_rl, *new_rl, *dst_3rd_rl, hole_rl[1];
s64 end_vcn;
int new_1st_cnt, dst_3rd_cnt, new_cnt, punch_cnt, merge_cnt;
bool begin_split, end_split, one_split_3;
if (dst_cnt < 2 ||
!(dst_rl[dst_cnt - 1].lcn == LCN_ENOENT &&
dst_rl[dst_cnt - 1].length == 0))
return ERR_PTR(-EINVAL);
end_vcn = min(start_vcn + len - 1,
dst_rl[dst_cnt - 2].vcn + dst_rl[dst_cnt - 2].length - 1);
s_rl = ntfs_rl_find_vcn_nolock(dst_rl, start_vcn);
if (!s_rl ||
s_rl->lcn <= LCN_ENOENT ||
!ntfs_rle_contain(s_rl, start_vcn))
return ERR_PTR(-EINVAL);
begin_split = s_rl->vcn != start_vcn;
e_rl = ntfs_rl_find_vcn_nolock(dst_rl, end_vcn);
if (!e_rl ||
e_rl->lcn <= LCN_ENOENT ||
!ntfs_rle_contain(e_rl, end_vcn))
return ERR_PTR(-EINVAL);
end_split = e_rl->vcn + e_rl->length - 1 != end_vcn;
one_split_3 = e_rl == s_rl && begin_split && end_split;
punch_cnt = (int)(e_rl - s_rl) + 1;
*punch_rl = kvcalloc(punch_cnt + 1, sizeof(struct runlist_element),
GFP_NOFS);
if (!*punch_rl)
return ERR_PTR(-ENOMEM);
new_cnt = dst_cnt - (int)(e_rl - s_rl + 1) + 3;
new_rl = kvcalloc(new_cnt, sizeof(struct runlist_element), GFP_NOFS);
if (!new_rl) {
kvfree(*punch_rl);
*punch_rl = NULL;
return ERR_PTR(-ENOMEM);
}
new_1st_cnt = (int)(s_rl - dst_rl) + 1;
ntfs_rl_mc(*punch_rl, 0, dst_rl, new_1st_cnt - 1, punch_cnt);
(*punch_rl)[punch_cnt].lcn = LCN_ENOENT;
(*punch_rl)[punch_cnt].length = 0;
if (!begin_split)
new_1st_cnt--;
dst_3rd_rl = e_rl;
dst_3rd_cnt = (int)(&dst_rl[dst_cnt - 1] - e_rl) + 1;
if (!end_split) {
dst_3rd_rl++;
dst_3rd_cnt--;
}
ntfs_rl_mc(new_rl, 0, dst_rl, 0, new_1st_cnt);
if (begin_split) {
s64 first_cnt = start_vcn - dst_rl[new_1st_cnt - 1].vcn;
if (new_1st_cnt)
new_rl[new_1st_cnt - 1].length = first_cnt;
(*punch_rl)[0].vcn = start_vcn;
(*punch_rl)[0].length -= first_cnt;
if ((*punch_rl)[0].lcn > LCN_HOLE)
(*punch_rl)[0].lcn += first_cnt;
}
hole_rl[0].vcn = start_vcn;
hole_rl[0].length = (s64)len;
hole_rl[0].lcn = LCN_HOLE;
ntfs_rl_mc(new_rl, new_1st_cnt, hole_rl, 0, 1);
ntfs_rl_mc(new_rl, new_1st_cnt + 1, dst_3rd_rl, 0, dst_3rd_cnt);
if (end_split) {
s64 first_cnt = end_vcn - dst_3rd_rl[0].vcn + 1;
new_rl[new_1st_cnt + 1].vcn = end_vcn + 1;
new_rl[new_1st_cnt + 1].length -= first_cnt;
if (new_rl[new_1st_cnt + 1].lcn > LCN_HOLE)
new_rl[new_1st_cnt + 1].lcn += first_cnt;
if (one_split_3)
(*punch_rl)[punch_cnt - 1].length -=
new_rl[new_1st_cnt + 1].length;
else
(*punch_rl)[punch_cnt - 1].length = first_cnt;
}
merge_cnt = 0;
if (new_1st_cnt > 0 && new_rl[new_1st_cnt - 1].lcn == LCN_HOLE) {
s_rl = &new_rl[new_1st_cnt - 1];
s_rl->length += s_rl[1].length;
merge_cnt = 1;
if (new_1st_cnt + 1 < new_cnt &&
new_rl[new_1st_cnt + 1].lcn == LCN_HOLE) {
s_rl->length += s_rl[2].length;
merge_cnt++;
}
} else if (new_1st_cnt + 1 < new_cnt &&
new_rl[new_1st_cnt + 1].lcn == LCN_HOLE) {
s_rl = &new_rl[new_1st_cnt];
s_rl->length += s_rl[1].length;
merge_cnt = 1;
}
if (merge_cnt) {
struct runlist_element *d_rl, *src_rl;
d_rl = s_rl + 1;
src_rl = s_rl + 1 + merge_cnt;
ntfs_rl_mm(new_rl, (int)(d_rl - new_rl), (int)(src_rl - new_rl),
(int)(&new_rl[new_cnt - 1] - src_rl) + 1);
}
(*punch_rl)[punch_cnt].vcn = (*punch_rl)[punch_cnt - 1].vcn +
(*punch_rl)[punch_cnt - 1].length;
*new_rl_cnt = dst_cnt - (punch_cnt - (int)begin_split - (int)end_split) +
1 - merge_cnt;
kvfree(dst_rl);
return new_rl;
}
struct runlist_element *ntfs_rl_collapse_range(struct runlist_element *dst_rl, int dst_cnt,
s64 start_vcn, s64 len,
struct runlist_element **punch_rl,
size_t *new_rl_cnt)
{
struct runlist_element *s_rl, *e_rl, *new_rl, *dst_3rd_rl;
s64 end_vcn;
int new_1st_cnt, dst_3rd_cnt, new_cnt, punch_cnt, merge_cnt, i;
bool begin_split, end_split, one_split_3;
if (dst_cnt < 2 ||
!(dst_rl[dst_cnt - 1].lcn == LCN_ENOENT &&
dst_rl[dst_cnt - 1].length == 0))
return ERR_PTR(-EINVAL);
end_vcn = min(start_vcn + len - 1,
dst_rl[dst_cnt - 1].vcn - 1);
s_rl = ntfs_rl_find_vcn_nolock(dst_rl, start_vcn);
if (!s_rl ||
s_rl->lcn <= LCN_ENOENT ||
!ntfs_rle_contain(s_rl, start_vcn))
return ERR_PTR(-EINVAL);
begin_split = s_rl->vcn != start_vcn;
e_rl = ntfs_rl_find_vcn_nolock(dst_rl, end_vcn);
if (!e_rl ||
e_rl->lcn <= LCN_ENOENT ||
!ntfs_rle_contain(e_rl, end_vcn))
return ERR_PTR(-EINVAL);
end_split = e_rl->vcn + e_rl->length - 1 != end_vcn;
one_split_3 = e_rl == s_rl && begin_split && end_split;
punch_cnt = (int)(e_rl - s_rl) + 1;
*punch_rl = kvcalloc(punch_cnt + 1, sizeof(struct runlist_element),
GFP_NOFS);
if (!*punch_rl)
return ERR_PTR(-ENOMEM);
new_cnt = dst_cnt - (int)(e_rl - s_rl + 1) + 3;
new_rl = kvcalloc(new_cnt, sizeof(struct runlist_element), GFP_NOFS);
if (!new_rl) {
kvfree(*punch_rl);
*punch_rl = NULL;
return ERR_PTR(-ENOMEM);
}
new_1st_cnt = (int)(s_rl - dst_rl) + 1;
ntfs_rl_mc(*punch_rl, 0, dst_rl, new_1st_cnt - 1, punch_cnt);
(*punch_rl)[punch_cnt].lcn = LCN_ENOENT;
(*punch_rl)[punch_cnt].length = 0;
if (!begin_split)
new_1st_cnt--;
dst_3rd_rl = e_rl;
dst_3rd_cnt = (int)(&dst_rl[dst_cnt - 1] - e_rl) + 1;
if (!end_split) {
dst_3rd_rl++;
dst_3rd_cnt--;
}
ntfs_rl_mc(new_rl, 0, dst_rl, 0, new_1st_cnt);
if (begin_split) {
s64 first_cnt = start_vcn - dst_rl[new_1st_cnt - 1].vcn;
new_rl[new_1st_cnt - 1].length = first_cnt;
(*punch_rl)[0].vcn = start_vcn;
(*punch_rl)[0].length -= first_cnt;
if ((*punch_rl)[0].lcn > LCN_HOLE)
(*punch_rl)[0].lcn += first_cnt;
}
ntfs_rl_mc(new_rl, new_1st_cnt, dst_3rd_rl, 0, dst_3rd_cnt);
if (end_split) {
s64 first_cnt = end_vcn - dst_3rd_rl[0].vcn + 1;
new_rl[new_1st_cnt].vcn = end_vcn + 1;
new_rl[new_1st_cnt].length -= first_cnt;
if (new_rl[new_1st_cnt].lcn > LCN_HOLE)
new_rl[new_1st_cnt].lcn += first_cnt;
if (one_split_3)
(*punch_rl)[punch_cnt - 1].length -=
new_rl[new_1st_cnt].length;
else
(*punch_rl)[punch_cnt - 1].length = first_cnt;
}
if (new_1st_cnt == 0)
new_rl[new_1st_cnt].vcn = 0;
for (i = new_1st_cnt == 0 ? 1 : new_1st_cnt; new_rl[i].length; i++)
new_rl[i].vcn = new_rl[i - 1].vcn + new_rl[i - 1].length;
new_rl[i].vcn = new_rl[i - 1].vcn + new_rl[i - 1].length;
merge_cnt = 0;
if (new_1st_cnt > 0 &&
ntfs_rle_lcn_contiguous(&new_rl[new_1st_cnt - 1],
&new_rl[new_1st_cnt])) {
s_rl = &new_rl[new_1st_cnt - 1];
s_rl->length += s_rl[1].length;
merge_cnt = 1;
}
if (merge_cnt) {
struct runlist_element *d_rl, *src_rl;
d_rl = s_rl + 1;
src_rl = s_rl + 1 + merge_cnt;
ntfs_rl_mm(new_rl, (int)(d_rl - new_rl), (int)(src_rl - new_rl),
(int)(&new_rl[new_cnt - 1] - src_rl) + 1);
}
(*punch_rl)[punch_cnt].vcn = (*punch_rl)[punch_cnt - 1].vcn +
(*punch_rl)[punch_cnt - 1].length;
*new_rl_cnt = dst_cnt - (punch_cnt - (int)begin_split - (int)end_split) -
merge_cnt;
kvfree(dst_rl);
return new_rl;
}