#include <linux/fs.h>
#include <linux/blkdev.h>
#include <linux/vmalloc.h>
#include <linux/slab.h>
#include "attrib.h"
#include "inode.h"
#include "debug.h"
#include "ntfs.h"
#include "lcnalloc.h"
#include "mft.h"
enum {
NTFS_SYMBOL_TOKEN = 0,
NTFS_PHRASE_TOKEN = 1,
NTFS_TOKEN_MASK = 1,
NTFS_SB_SIZE_MASK = 0x0fff,
NTFS_SB_SIZE = 0x1000,
NTFS_SB_IS_COMPRESSED = 0x8000,
NTFS_MAX_CB_SIZE = 64 * 1024,
};
static u8 *ntfs_compression_buffer;
static DEFINE_MUTEX(ntfs_cb_lock);
int allocate_compression_buffers(void)
{
if (ntfs_compression_buffer)
return 0;
ntfs_compression_buffer = vmalloc(NTFS_MAX_CB_SIZE);
if (!ntfs_compression_buffer)
return -ENOMEM;
return 0;
}
void free_compression_buffers(void)
{
mutex_lock(&ntfs_cb_lock);
if (!ntfs_compression_buffer) {
mutex_unlock(&ntfs_cb_lock);
return;
}
vfree(ntfs_compression_buffer);
ntfs_compression_buffer = NULL;
mutex_unlock(&ntfs_cb_lock);
}
static void zero_partial_compressed_page(struct page *page,
const s64 initialized_size)
{
u8 *kp = page_address(page);
unsigned int kp_ofs;
ntfs_debug("Zeroing page region outside initialized size.");
if (((s64)page->__folio_index << PAGE_SHIFT) >= initialized_size) {
clear_page(kp);
return;
}
kp_ofs = initialized_size & ~PAGE_MASK;
memset(kp + kp_ofs, 0, PAGE_SIZE - kp_ofs);
}
static inline void handle_bounds_compressed_page(struct page *page,
const loff_t i_size, const s64 initialized_size)
{
if ((page->__folio_index >= (initialized_size >> PAGE_SHIFT)) &&
(initialized_size < i_size))
zero_partial_compressed_page(page, initialized_size);
}
static int ntfs_decompress(struct page *dest_pages[], int completed_pages[],
int *dest_index, int *dest_ofs, const int dest_max_index,
const int dest_max_ofs, const int xpage, char *xpage_done,
u8 *const cb_start, const u32 cb_size, const loff_t i_size,
const s64 initialized_size)
{
u8 *cb_end = cb_start + cb_size;
u8 *cb = cb_start;
u8 *cb_sb_start = cb;
u8 *cb_sb_end;
struct page *dp;
u8 *dp_addr;
u8 *dp_sb_start;
u8 *dp_sb_end;
u16 do_sb_start;
u16 do_sb_end;
u8 tag;
int token;
int nr_completed_pages = 0;
int err = -EOVERFLOW;
ntfs_debug("Entering, cb_size = 0x%x.", cb_size);
do_next_sb:
ntfs_debug("Beginning sub-block at offset = 0x%zx in the cb.",
cb - cb_start);
if (cb == cb_end || !le16_to_cpup((__le16 *)cb) ||
(*dest_index == dest_max_index &&
*dest_ofs == dest_max_ofs)) {
int i;
ntfs_debug("Completed. Returning success (0).");
err = 0;
return_error:
mutex_unlock(&ntfs_cb_lock);
if (nr_completed_pages > 0) {
for (i = 0; i < nr_completed_pages; i++) {
int di = completed_pages[i];
dp = dest_pages[di];
handle_bounds_compressed_page(dp, i_size,
initialized_size);
flush_dcache_page(dp);
kunmap_local(page_address(dp));
SetPageUptodate(dp);
unlock_page(dp);
if (di == xpage)
*xpage_done = 1;
else
put_page(dp);
dest_pages[di] = NULL;
}
}
return err;
}
do_sb_start = *dest_ofs;
do_sb_end = do_sb_start + NTFS_SB_SIZE;
if (*dest_index == dest_max_index && do_sb_end > dest_max_ofs)
goto return_overflow;
if (cb + 6 > cb_end)
goto return_overflow;
cb_sb_start = cb;
cb_sb_end = cb_sb_start + (le16_to_cpup((__le16 *)cb) & NTFS_SB_SIZE_MASK)
+ 3;
if (cb_sb_end > cb_end)
goto return_overflow;
dp = dest_pages[*dest_index];
if (!dp) {
cb = cb_sb_end;
*dest_ofs = (*dest_ofs + NTFS_SB_SIZE) & ~PAGE_MASK;
if (!*dest_ofs && (++*dest_index > dest_max_index))
goto return_overflow;
goto do_next_sb;
}
dp_addr = (u8 *)page_address(dp) + do_sb_start;
if (!(le16_to_cpup((__le16 *)cb) & NTFS_SB_IS_COMPRESSED)) {
ntfs_debug("Found uncompressed sub-block.");
cb += 2;
if (cb_sb_end - cb != NTFS_SB_SIZE)
goto return_overflow;
memcpy(dp_addr, cb, NTFS_SB_SIZE);
cb += NTFS_SB_SIZE;
*dest_ofs += NTFS_SB_SIZE;
*dest_ofs &= ~PAGE_MASK;
if (!(*dest_ofs)) {
finalize_page:
completed_pages[nr_completed_pages++] = *dest_index;
if (++*dest_index > dest_max_index)
goto return_overflow;
}
goto do_next_sb;
}
ntfs_debug("Found compressed sub-block.");
dp_sb_start = dp_addr;
dp_sb_end = dp_sb_start + NTFS_SB_SIZE;
cb += 2;
do_next_tag:
if (cb == cb_sb_end) {
if (dp_addr < dp_sb_end) {
int nr_bytes = do_sb_end - *dest_ofs;
ntfs_debug("Filling incomplete sub-block with zeroes.");
memset(dp_addr, 0, nr_bytes);
*dest_ofs += nr_bytes;
}
*dest_ofs &= ~PAGE_MASK;
if (!(*dest_ofs))
goto finalize_page;
goto do_next_sb;
}
if (cb > cb_sb_end || dp_addr > dp_sb_end)
goto return_overflow;
tag = *cb++;
for (token = 0; token < 8; token++, tag >>= 1) {
register u16 i;
u16 lg, pt, length, max_non_overlap;
u8 *dp_back_addr;
if (cb >= cb_sb_end || dp_addr > dp_sb_end)
break;
if ((tag & NTFS_TOKEN_MASK) == NTFS_SYMBOL_TOKEN) {
*dp_addr++ = *cb++;
++*dest_ofs;
continue;
}
if (dp_addr == dp_sb_start)
goto return_overflow;
lg = 0;
for (i = *dest_ofs - do_sb_start - 1; i >= 0x10; i >>= 1)
lg++;
pt = le16_to_cpup((__le16 *)cb);
dp_back_addr = dp_addr - (pt >> (12 - lg)) - 1;
if (dp_back_addr < dp_sb_start)
goto return_overflow;
length = (pt & (0xfff >> lg)) + 3;
*dest_ofs += length;
if (*dest_ofs > do_sb_end)
goto return_overflow;
max_non_overlap = dp_addr - dp_back_addr;
if (length <= max_non_overlap) {
memcpy(dp_addr, dp_back_addr, length);
dp_addr += length;
} else {
memcpy(dp_addr, dp_back_addr, max_non_overlap);
dp_addr += max_non_overlap;
dp_back_addr += max_non_overlap;
length -= max_non_overlap;
while (length--)
*dp_addr++ = *dp_back_addr++;
}
cb += 2;
}
goto do_next_tag;
return_overflow:
ntfs_error(NULL, "Failed. Returning -EOVERFLOW.");
goto return_error;
}
int ntfs_read_compressed_block(struct folio *folio)
{
struct page *page = &folio->page;
loff_t i_size;
s64 initialized_size;
struct address_space *mapping = page->mapping;
struct ntfs_inode *ni = NTFS_I(mapping->host);
struct ntfs_volume *vol = ni->vol;
struct super_block *sb = vol->sb;
struct runlist_element *rl;
unsigned long flags;
u8 *cb, *cb_pos, *cb_end;
unsigned long offset, index = page->__folio_index;
u32 cb_size = ni->itype.compressed.block_size;
u64 cb_size_mask = cb_size - 1UL;
s64 vcn;
s64 lcn;
s64 start_vcn = (((s64)index << PAGE_SHIFT) & ~cb_size_mask) >>
vol->cluster_size_bits;
s64 end_vcn = ((((s64)(index + 1UL) << PAGE_SHIFT) + cb_size - 1)
& ~cb_size_mask) >> vol->cluster_size_bits;
unsigned int nr_cbs = ntfs_cluster_to_bytes(vol, end_vcn - start_vcn) >>
ni->itype.compressed.block_size_bits;
unsigned int nr_pages = ntfs_cluster_to_pidx(vol, end_vcn - start_vcn);
unsigned int xpage, max_page, cur_page, cur_ofs, i, page_ofs, page_index;
unsigned int cb_clusters, cb_max_ofs;
int cb_max_page, err = 0;
struct page **pages;
int *completed_pages;
unsigned char xpage_done = 0;
struct page *lpage;
ntfs_debug("Entering, page->index = 0x%lx, cb_size = 0x%x, nr_pages = %i.",
index, cb_size, nr_pages);
if (ni->type != AT_DATA || ni->name_len) {
unlock_page(page);
return -EIO;
}
pages = kmalloc_array(nr_pages, sizeof(struct page *), GFP_NOFS);
completed_pages = kmalloc_array(nr_pages + 1, sizeof(int), GFP_NOFS);
if (unlikely(!pages || !completed_pages)) {
kfree(pages);
kfree(completed_pages);
unlock_page(page);
ntfs_error(vol->sb, "Failed to allocate internal buffers.");
return -ENOMEM;
}
offset = ntfs_cluster_to_pidx(vol, start_vcn);
xpage = index - offset;
pages[xpage] = page;
read_lock_irqsave(&ni->size_lock, flags);
i_size = i_size_read(VFS_I(ni));
initialized_size = ni->initialized_size;
read_unlock_irqrestore(&ni->size_lock, flags);
max_page = ((i_size + PAGE_SIZE - 1) >> PAGE_SHIFT) -
offset;
if (xpage >= max_page) {
kfree(pages);
kfree(completed_pages);
zero_user_segments(page, 0, PAGE_SIZE, 0, 0);
ntfs_debug("Compressed read outside i_size - truncated?");
SetPageUptodate(page);
unlock_page(page);
return 0;
}
if (nr_pages < max_page)
max_page = nr_pages;
for (i = 0; i < max_page; i++, offset++) {
if (i != xpage)
pages[i] = grab_cache_page_nowait(mapping, offset);
page = pages[i];
if (page) {
if (!PageDirty(page) && (!PageUptodate(page))) {
kmap_local_page(page);
continue;
}
unlock_page(page);
put_page(page);
pages[i] = NULL;
}
}
cur_page = 0;
cur_ofs = 0;
cb_clusters = ni->itype.compressed.block_clusters;
do_next_cb:
nr_cbs--;
mutex_lock(&ntfs_cb_lock);
if (!ntfs_compression_buffer)
if (allocate_compression_buffers()) {
mutex_unlock(&ntfs_cb_lock);
goto err_out;
}
cb = ntfs_compression_buffer;
cb_pos = cb;
cb_end = cb + cb_size;
rl = NULL;
for (vcn = start_vcn, start_vcn += cb_clusters; vcn < start_vcn;
vcn++) {
bool is_retry = false;
if (!rl) {
lock_retry_remap:
down_read(&ni->runlist.lock);
rl = ni->runlist.rl;
}
if (likely(rl != NULL)) {
while (rl->length && rl[1].vcn <= vcn)
rl++;
lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
} else
lcn = LCN_RL_NOT_MAPPED;
ntfs_debug("Reading vcn = 0x%llx, lcn = 0x%llx.",
(unsigned long long)vcn,
(unsigned long long)lcn);
if (lcn < 0) {
if (lcn == LCN_HOLE)
break;
if (is_retry || lcn != LCN_RL_NOT_MAPPED) {
mutex_unlock(&ntfs_cb_lock);
goto rl_err;
}
is_retry = true;
up_read(&ni->runlist.lock);
if (!ntfs_map_runlist(ni, vcn))
goto lock_retry_remap;
mutex_unlock(&ntfs_cb_lock);
goto map_rl_err;
}
page_ofs = ntfs_cluster_to_poff(vol, lcn);
page_index = ntfs_cluster_to_pidx(vol, lcn);
lpage = read_mapping_page(sb->s_bdev->bd_mapping,
page_index, NULL);
if (IS_ERR(lpage)) {
err = PTR_ERR(lpage);
mutex_unlock(&ntfs_cb_lock);
goto read_err;
}
lock_page(lpage);
memcpy(cb_pos, page_address(lpage) + page_ofs,
vol->cluster_size);
unlock_page(lpage);
put_page(lpage);
cb_pos += vol->cluster_size;
}
if (rl)
up_read(&ni->runlist.lock);
if (cb_pos + 2 <= cb + cb_size)
*(u16 *)cb_pos = 0;
cb_pos = cb;
ntfs_debug("Successfully read the compression block.");
cb_max_page = (cur_page << PAGE_SHIFT) + cur_ofs + cb_size;
cb_max_ofs = cb_max_page & ~PAGE_MASK;
cb_max_page >>= PAGE_SHIFT;
if (cb_max_page > max_page)
cb_max_page = max_page;
if (vcn == start_vcn - cb_clusters) {
ntfs_debug("Found sparse compression block.");
mutex_unlock(&ntfs_cb_lock);
if (cb_max_ofs)
cb_max_page--;
for (; cur_page < cb_max_page; cur_page++) {
page = pages[cur_page];
if (page) {
if (likely(!cur_ofs))
clear_page(page_address(page));
else
memset(page_address(page) + cur_ofs, 0,
PAGE_SIZE -
cur_ofs);
flush_dcache_page(page);
kunmap_local(page_address(page));
SetPageUptodate(page);
unlock_page(page);
if (cur_page == xpage)
xpage_done = 1;
else
put_page(page);
pages[cur_page] = NULL;
}
cb_pos += PAGE_SIZE - cur_ofs;
cur_ofs = 0;
if (cb_pos >= cb_end)
break;
}
if (cb_max_ofs && cb_pos < cb_end) {
page = pages[cur_page];
if (page)
memset(page_address(page) + cur_ofs, 0,
cb_max_ofs - cur_ofs);
cur_ofs = cb_max_ofs;
}
} else if (vcn == start_vcn) {
unsigned int cur2_page = cur_page;
unsigned int cur_ofs2 = cur_ofs;
u8 *cb_pos2 = cb_pos;
ntfs_debug("Found uncompressed compression block.");
if (cb_max_ofs)
cb_max_page--;
for (; cur_page < cb_max_page; cur_page++) {
page = pages[cur_page];
if (page)
memcpy(page_address(page) + cur_ofs, cb_pos,
PAGE_SIZE - cur_ofs);
cb_pos += PAGE_SIZE - cur_ofs;
cur_ofs = 0;
if (cb_pos >= cb_end)
break;
}
if (cb_max_ofs && cb_pos < cb_end) {
page = pages[cur_page];
if (page)
memcpy(page_address(page) + cur_ofs, cb_pos,
cb_max_ofs - cur_ofs);
cb_pos += cb_max_ofs - cur_ofs;
cur_ofs = cb_max_ofs;
}
mutex_unlock(&ntfs_cb_lock);
for (; cur2_page < cb_max_page; cur2_page++) {
page = pages[cur2_page];
if (page) {
handle_bounds_compressed_page(page, i_size,
initialized_size);
flush_dcache_page(page);
kunmap_local(page_address(page));
SetPageUptodate(page);
unlock_page(page);
if (cur2_page == xpage)
xpage_done = 1;
else
put_page(page);
pages[cur2_page] = NULL;
}
cb_pos2 += PAGE_SIZE - cur_ofs2;
cur_ofs2 = 0;
if (cb_pos2 >= cb_end)
break;
}
} else {
unsigned int prev_cur_page = cur_page;
ntfs_debug("Found compressed compression block.");
err = ntfs_decompress(pages, completed_pages, &cur_page,
&cur_ofs, cb_max_page, cb_max_ofs, xpage,
&xpage_done, cb_pos, cb_size - (cb_pos - cb),
i_size, initialized_size);
if (err) {
ntfs_error(vol->sb,
"ntfs_decompress() failed in inode 0x%llx with error code %i. Skipping this compression block.",
ni->mft_no, -err);
for (; prev_cur_page < cur_page; prev_cur_page++) {
page = pages[prev_cur_page];
if (page) {
flush_dcache_page(page);
kunmap_local(page_address(page));
unlock_page(page);
if (prev_cur_page != xpage)
put_page(page);
pages[prev_cur_page] = NULL;
}
}
}
}
if (nr_cbs)
goto do_next_cb;
for (cur_page = 0; cur_page < max_page; cur_page++) {
page = pages[cur_page];
if (page) {
ntfs_error(vol->sb,
"Still have pages left! Terminating them with extreme prejudice. Inode 0x%llx, page index 0x%lx.",
ni->mft_no, page->__folio_index);
flush_dcache_page(page);
kunmap_local(page_address(page));
unlock_page(page);
if (cur_page != xpage)
put_page(page);
pages[cur_page] = NULL;
}
}
kfree(pages);
kfree(completed_pages);
if (likely(xpage_done))
return 0;
ntfs_debug("Failed. Returning error code %s.", err == -EOVERFLOW ?
"EOVERFLOW" : (!err ? "EIO" : "unknown error"));
return err < 0 ? err : -EIO;
map_rl_err:
ntfs_error(vol->sb, "ntfs_map_runlist() failed. Cannot read compression block.");
goto err_out;
rl_err:
up_read(&ni->runlist.lock);
ntfs_error(vol->sb, "ntfs_rl_vcn_to_lcn() failed. Cannot read compression block.");
goto err_out;
read_err:
up_read(&ni->runlist.lock);
ntfs_error(vol->sb, "IO error while reading compressed data.");
err_out:
for (i = cur_page; i < max_page; i++) {
page = pages[i];
if (page) {
flush_dcache_page(page);
kunmap_local(page_address(page));
unlock_page(page);
if (i != xpage)
put_page(page);
}
}
kfree(pages);
kfree(completed_pages);
return -EIO;
}
#define NICE_MATCH_LEN 18
#define MAX_SEARCH_DEPTH 24
#define HASH_SHIFT 14
#define HASH_MULTIPLIER 0x1E35A7BD
struct compress_context {
const unsigned char *inbuf;
int bufsize;
int size;
int rel;
int mxsz;
s16 head[1 << HASH_SHIFT];
s16 prev[NTFS_SB_SIZE];
};
static inline unsigned int ntfs_hash(const u8 *p)
{
u32 str;
u32 hash;
str = *(const u32 *)p & 0xFFFFFF;
hash = str * HASH_MULTIPLIER;
return hash >> (32 - HASH_SHIFT);
}
static void ntfs_best_match(struct compress_context *pctx, const int i,
int best_len)
{
const u8 * const inbuf = pctx->inbuf;
const u8 * const strptr = &inbuf[i];
s16 * const prev = pctx->prev;
const int max_len = min(pctx->bufsize - i, pctx->mxsz);
const int nice_len = min(NICE_MATCH_LEN, max_len);
int depth_remaining = MAX_SEARCH_DEPTH;
const u8 *best_matchptr = strptr;
unsigned int hash;
s16 cur_match;
const u8 *matchptr;
int len;
if (max_len < 4)
goto out;
hash = ntfs_hash(strptr);
cur_match = pctx->head[hash];
prev[i] = cur_match;
pctx->head[hash] = i;
if (best_len >= max_len) {
goto out;
}
for (; cur_match >= 0 && depth_remaining--; cur_match = prev[cur_match]) {
matchptr = &inbuf[cur_match];
if (matchptr[best_len] != strptr[best_len] ||
matchptr[best_len - 1] != strptr[best_len - 1] ||
matchptr[0] != strptr[0])
goto next_match;
for (len = 1; len < best_len - 1; len++)
if (matchptr[len] != strptr[len])
goto next_match;
best_matchptr = matchptr;
do {
if (++best_len >= nice_len) {
while (best_len < max_len &&
(best_matchptr[best_len] ==
strptr[best_len]))
best_len++;
goto out;
}
} while (best_matchptr[best_len] == strptr[best_len]);
next_match:
;
}
out:
pctx->size = best_len;
pctx->rel = best_matchptr - strptr;
}
static void ntfs_skip_position(struct compress_context *pctx, const int i)
{
unsigned int hash;
if (pctx->bufsize - i < 4)
return;
hash = ntfs_hash(pctx->inbuf + i);
pctx->prev[i] = pctx->head[hash];
pctx->head[hash] = i;
}
static unsigned int ntfs_compress_block(const char *inbuf, const int bufsize,
char *outbuf)
{
struct compress_context *pctx;
int i;
int j;
int k;
int offs;
int bp;
int bp_cur;
int mxoff;
unsigned int xout;
unsigned int q;
int have_match;
char *ptag;
int tag;
int ntag;
pctx = kvzalloc(sizeof(struct compress_context), GFP_NOFS);
if (!pctx)
return -ENOMEM;
memset(pctx->head, 0xFF, sizeof(pctx->head));
pctx->inbuf = (const unsigned char *)inbuf;
pctx->bufsize = bufsize;
xout = 2;
i = 0;
bp = 4;
mxoff = 1 << bp;
pctx->mxsz = (1 << (16 - bp)) + 2;
have_match = 0;
tag = 0;
ntag = 8;
ptag = &outbuf[xout++];
while ((i < bufsize) && (xout < (NTFS_SB_SIZE + 2))) {
if (!have_match) {
while (mxoff < i) {
bp++;
mxoff <<= 1;
pctx->mxsz = (pctx->mxsz + 2) >> 1;
}
ntfs_best_match(pctx, i, 2);
}
if (pctx->size >= 3) {
j = i + pctx->size;
bp_cur = bp;
offs = pctx->rel;
if (pctx->size >= NICE_MATCH_LEN) {
q = (~offs << (16 - bp_cur)) + (j - i - 3);
outbuf[xout++] = q & 255;
outbuf[xout++] = (q >> 8) & 255;
tag |= (1 << (8 - ntag));
if (j == bufsize) {
i = j;
--ntag;
break;
}
i += 1;
do {
ntfs_skip_position(pctx, i);
} while (++i != j);
have_match = 0;
} else {
if (mxoff < i + 1) {
bp++;
mxoff <<= 1;
pctx->mxsz = (pctx->mxsz + 2) >> 1;
}
ntfs_best_match(pctx, i + 1, pctx->size);
k = i + 1 + pctx->size;
if (k > (j + 1)) {
outbuf[xout++] = inbuf[i++];
have_match = 1;
} else {
q = (~offs << (16 - bp_cur)) +
(j - i - 3);
outbuf[xout++] = q & 255;
outbuf[xout++] = (q >> 8) & 255;
tag |= (1 << (8 - ntag));
i += 2;
do {
ntfs_skip_position(pctx, i);
} while (++i != j);
have_match = 0;
}
}
} else {
outbuf[xout++] = inbuf[i++];
have_match = 0;
}
if (!--ntag) {
*ptag = tag;
ntag = 8;
ptag = &outbuf[xout++];
tag = 0;
}
}
if (ntag == 8)
xout--;
else
*ptag = tag;
if ((i >= bufsize) && (xout < (NTFS_SB_SIZE + 2))) {
outbuf[0] = (xout - 3) & 255;
outbuf[1] = 0xb0 + (((xout - 3) >> 8) & 15);
} else {
memcpy(&outbuf[2], inbuf, bufsize);
if (bufsize < NTFS_SB_SIZE)
memset(&outbuf[bufsize + 2], 0, NTFS_SB_SIZE - bufsize);
outbuf[0] = 0xff;
outbuf[1] = 0x3f;
xout = NTFS_SB_SIZE + 2;
}
kvfree(pctx);
return xout;
}
static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
int pages_per_cb)
{
struct ntfs_volume *vol = ni->vol;
char *outbuf = NULL, *pbuf, *inbuf;
u32 compsz, p, insz = pages_per_cb << PAGE_SHIFT;
s32 rounded, bio_size;
unsigned int sz, bsz;
bool fail = false, allzeroes;
static char onezero[] = {0x01, 0xb0, 0x00, 0x00};
static char twozeroes[] = {0x02, 0xb0, 0x00, 0x00, 0x00};
static char morezeroes[] = {0x03, 0xb0, 0x02, 0x00};
struct page **pages_disk = NULL, *pg;
s64 bio_lcn;
struct runlist_element *rlc, *rl;
int i, err;
int pages_count = (round_up(ni->itype.compressed.block_size + 2 *
(ni->itype.compressed.block_size / NTFS_SB_SIZE) + 2, PAGE_SIZE)) / PAGE_SIZE;
size_t new_rl_count;
struct bio *bio = NULL;
loff_t new_length;
s64 new_vcn;
inbuf = vmap(pages, pages_per_cb, VM_MAP, PAGE_KERNEL_RO);
if (!inbuf)
return -ENOMEM;
pages_disk = kcalloc(pages_count, sizeof(struct page *), GFP_NOFS);
if (!pages_disk) {
vunmap(inbuf);
return -ENOMEM;
}
for (i = 0; i < pages_count; i++) {
pg = alloc_page(GFP_KERNEL);
if (!pg) {
err = -ENOMEM;
goto out;
}
pages_disk[i] = pg;
lock_page(pg);
kmap_local_page(pg);
}
outbuf = vmap(pages_disk, pages_count, VM_MAP, PAGE_KERNEL);
if (!outbuf) {
err = -ENOMEM;
goto out;
}
compsz = 0;
allzeroes = true;
for (p = 0; (p < insz) && !fail; p += NTFS_SB_SIZE) {
if ((p + NTFS_SB_SIZE) < insz)
bsz = NTFS_SB_SIZE;
else
bsz = insz - p;
pbuf = &outbuf[compsz];
sz = ntfs_compress_block(&inbuf[p], bsz, pbuf);
if (!sz || ((compsz + sz + vol->cluster_size + 2) >
ni->itype.compressed.block_size))
fail = true;
else {
if (allzeroes) {
switch (sz) {
case 4:
allzeroes = !memcmp(pbuf, onezero, 4);
break;
case 5:
allzeroes = !memcmp(pbuf, twozeroes, 5);
break;
case 6:
allzeroes = !memcmp(pbuf, morezeroes, 4);
break;
default:
allzeroes = false;
break;
}
}
compsz += sz;
}
}
if (!fail && !allzeroes) {
outbuf[compsz++] = 0;
outbuf[compsz++] = 0;
rounded = ((compsz - 1) | (vol->cluster_size - 1)) + 1;
memset(&outbuf[compsz], 0, rounded - compsz);
bio_size = rounded;
pages = pages_disk;
} else if (allzeroes) {
err = 0;
goto out;
} else {
bio_size = insz;
}
new_vcn = ntfs_bytes_to_cluster(vol,
pos & ~((loff_t)ni->itype.compressed.block_size - 1));
new_length = ntfs_bytes_to_cluster(vol, round_up(bio_size, vol->cluster_size));
err = ntfs_non_resident_attr_punch_hole(ni, new_vcn, ni->itype.compressed.block_clusters);
if (err < 0)
goto out;
rlc = ntfs_cluster_alloc(vol, new_vcn, new_length, -1, DATA_ZONE,
false, true, true);
if (IS_ERR(rlc)) {
err = PTR_ERR(rlc);
goto out;
}
bio_lcn = rlc->lcn;
down_write(&ni->runlist.lock);
rl = ntfs_runlists_merge(&ni->runlist, rlc, 0, &new_rl_count);
if (IS_ERR(rl)) {
up_write(&ni->runlist.lock);
ntfs_error(vol->sb, "Failed to merge runlists");
err = PTR_ERR(rl);
if (ntfs_cluster_free_from_rl(vol, rlc))
ntfs_error(vol->sb, "Failed to free hot clusters.");
kvfree(rlc);
goto out;
}
ni->runlist.count = new_rl_count;
ni->runlist.rl = rl;
err = ntfs_attr_update_mapping_pairs(ni, 0);
up_write(&ni->runlist.lock);
if (err) {
err = -EIO;
goto out;
}
i = 0;
while (bio_size > 0) {
int page_size;
if (bio_size >= PAGE_SIZE) {
page_size = PAGE_SIZE;
bio_size -= PAGE_SIZE;
} else {
page_size = bio_size;
bio_size = 0;
}
setup_bio:
if (!bio) {
bio = bio_alloc(vol->sb->s_bdev, 1, REQ_OP_WRITE,
GFP_NOIO);
bio->bi_iter.bi_sector =
ntfs_bytes_to_sector(vol,
ntfs_cluster_to_bytes(vol, bio_lcn + i));
}
if (!bio_add_page(bio, pages[i], page_size, 0)) {
err = submit_bio_wait(bio);
bio_put(bio);
if (err)
goto out;
bio = NULL;
goto setup_bio;
}
i++;
}
err = submit_bio_wait(bio);
bio_put(bio);
out:
vunmap(outbuf);
for (i = 0; i < pages_count; i++) {
pg = pages_disk[i];
if (pg) {
kunmap_local(page_address(pg));
unlock_page(pg);
put_page(pg);
}
}
kfree(pages_disk);
vunmap(inbuf);
NInoSetFileNameDirty(ni);
mark_mft_record_dirty(ni);
return err;
}
int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count,
struct iov_iter *from)
{
struct folio *folio;
struct page **pages = NULL, *page;
int pages_per_cb = ni->itype.compressed.block_size >> PAGE_SHIFT;
int cb_size = ni->itype.compressed.block_size, cb_off, err = 0;
int i, ip;
size_t written = 0;
struct address_space *mapping = VFS_I(ni)->i_mapping;
if (NInoCompressed(ni) && pos + count > ni->allocated_size) {
int err;
loff_t end = pos + count;
err = ntfs_attr_expand(ni, end,
round_up(end, ni->itype.compressed.block_size));
if (err)
return err;
}
pages = kmalloc_array(pages_per_cb, sizeof(struct page *), GFP_NOFS);
if (!pages)
return -ENOMEM;
while (count) {
pgoff_t index;
size_t copied, bytes;
int off;
off = pos & (cb_size - 1);
bytes = cb_size - off;
if (bytes > count)
bytes = count;
cb_off = pos & ~(cb_size - 1);
index = cb_off >> PAGE_SHIFT;
if (unlikely(fault_in_iov_iter_readable(from, bytes))) {
err = -EFAULT;
goto out;
}
for (i = 0; i < pages_per_cb; i++) {
folio = read_mapping_folio(mapping, index + i, NULL);
if (IS_ERR(folio)) {
for (ip = 0; ip < i; ip++) {
folio_unlock(page_folio(pages[ip]));
folio_put(page_folio(pages[ip]));
}
err = PTR_ERR(folio);
goto out;
}
folio_lock(folio);
pages[i] = folio_page(folio, 0);
}
WARN_ON(!bytes);
copied = 0;
ip = off >> PAGE_SHIFT;
off = offset_in_page(pos);
for (;;) {
size_t cp, tail = PAGE_SIZE - off;
page = pages[ip];
cp = copy_folio_from_iter_atomic(page_folio(page), off,
min(tail, bytes), from);
flush_dcache_page(page);
copied += cp;
bytes -= cp;
if (!bytes || !cp)
break;
if (cp < tail) {
off += cp;
} else {
ip++;
off = 0;
}
}
err = ntfs_write_cb(ni, pos, pages, pages_per_cb);
for (i = 0; i < pages_per_cb; i++) {
folio = page_folio(pages[i]);
if (i < ip) {
folio_clear_dirty(folio);
folio_mark_uptodate(folio);
}
folio_unlock(folio);
folio_put(folio);
}
if (err)
goto out;
cond_resched();
pos += copied;
written += copied;
count = iov_iter_count(from);
}
out:
kfree(pages);
if (err < 0)
written = err;
return written;
}