162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * compress.c - NTFS kernel compressed attributes handling.
462306a36Sopenharmony_ci *		Part of the Linux-NTFS project.
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * Copyright (c) 2001-2004 Anton Altaparmakov
762306a36Sopenharmony_ci * Copyright (c) 2002 Richard Russon
862306a36Sopenharmony_ci */
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include <linux/fs.h>
1162306a36Sopenharmony_ci#include <linux/buffer_head.h>
1262306a36Sopenharmony_ci#include <linux/blkdev.h>
1362306a36Sopenharmony_ci#include <linux/vmalloc.h>
1462306a36Sopenharmony_ci#include <linux/slab.h>
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci#include "attrib.h"
1762306a36Sopenharmony_ci#include "inode.h"
1862306a36Sopenharmony_ci#include "debug.h"
1962306a36Sopenharmony_ci#include "ntfs.h"
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci/**
2262306a36Sopenharmony_ci * ntfs_compression_constants - enum of constants used in the compression code
2362306a36Sopenharmony_ci */
2462306a36Sopenharmony_citypedef enum {
2562306a36Sopenharmony_ci	/* Token types and access mask. */
2662306a36Sopenharmony_ci	NTFS_SYMBOL_TOKEN	=	0,
2762306a36Sopenharmony_ci	NTFS_PHRASE_TOKEN	=	1,
2862306a36Sopenharmony_ci	NTFS_TOKEN_MASK		=	1,
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci	/* Compression sub-block constants. */
3162306a36Sopenharmony_ci	NTFS_SB_SIZE_MASK	=	0x0fff,
3262306a36Sopenharmony_ci	NTFS_SB_SIZE		=	0x1000,
3362306a36Sopenharmony_ci	NTFS_SB_IS_COMPRESSED	=	0x8000,
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci	/*
3662306a36Sopenharmony_ci	 * The maximum compression block size is by definition 16 * the cluster
3762306a36Sopenharmony_ci	 * size, with the maximum supported cluster size being 4kiB. Thus the
3862306a36Sopenharmony_ci	 * maximum compression buffer size is 64kiB, so we use this when
3962306a36Sopenharmony_ci	 * initializing the compression buffer.
4062306a36Sopenharmony_ci	 */
4162306a36Sopenharmony_ci	NTFS_MAX_CB_SIZE	= 64 * 1024,
4262306a36Sopenharmony_ci} ntfs_compression_constants;
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci/*
4562306a36Sopenharmony_ci * ntfs_compression_buffer - one buffer for the decompression engine
4662306a36Sopenharmony_ci */
4762306a36Sopenharmony_cistatic u8 *ntfs_compression_buffer;
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci/*
5062306a36Sopenharmony_ci * ntfs_cb_lock - spinlock which protects ntfs_compression_buffer
5162306a36Sopenharmony_ci */
5262306a36Sopenharmony_cistatic DEFINE_SPINLOCK(ntfs_cb_lock);
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci/**
5562306a36Sopenharmony_ci * allocate_compression_buffers - allocate the decompression buffers
5662306a36Sopenharmony_ci *
5762306a36Sopenharmony_ci * Caller has to hold the ntfs_lock mutex.
5862306a36Sopenharmony_ci *
5962306a36Sopenharmony_ci * Return 0 on success or -ENOMEM if the allocations failed.
6062306a36Sopenharmony_ci */
6162306a36Sopenharmony_ciint allocate_compression_buffers(void)
6262306a36Sopenharmony_ci{
6362306a36Sopenharmony_ci	BUG_ON(ntfs_compression_buffer);
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci	ntfs_compression_buffer = vmalloc(NTFS_MAX_CB_SIZE);
6662306a36Sopenharmony_ci	if (!ntfs_compression_buffer)
6762306a36Sopenharmony_ci		return -ENOMEM;
6862306a36Sopenharmony_ci	return 0;
6962306a36Sopenharmony_ci}
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci/**
7262306a36Sopenharmony_ci * free_compression_buffers - free the decompression buffers
7362306a36Sopenharmony_ci *
7462306a36Sopenharmony_ci * Caller has to hold the ntfs_lock mutex.
7562306a36Sopenharmony_ci */
7662306a36Sopenharmony_civoid free_compression_buffers(void)
7762306a36Sopenharmony_ci{
7862306a36Sopenharmony_ci	BUG_ON(!ntfs_compression_buffer);
7962306a36Sopenharmony_ci	vfree(ntfs_compression_buffer);
8062306a36Sopenharmony_ci	ntfs_compression_buffer = NULL;
8162306a36Sopenharmony_ci}
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci/**
8462306a36Sopenharmony_ci * zero_partial_compressed_page - zero out of bounds compressed page region
8562306a36Sopenharmony_ci */
8662306a36Sopenharmony_cistatic void zero_partial_compressed_page(struct page *page,
8762306a36Sopenharmony_ci		const s64 initialized_size)
8862306a36Sopenharmony_ci{
8962306a36Sopenharmony_ci	u8 *kp = page_address(page);
9062306a36Sopenharmony_ci	unsigned int kp_ofs;
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci	ntfs_debug("Zeroing page region outside initialized size.");
9362306a36Sopenharmony_ci	if (((s64)page->index << PAGE_SHIFT) >= initialized_size) {
9462306a36Sopenharmony_ci		clear_page(kp);
9562306a36Sopenharmony_ci		return;
9662306a36Sopenharmony_ci	}
9762306a36Sopenharmony_ci	kp_ofs = initialized_size & ~PAGE_MASK;
9862306a36Sopenharmony_ci	memset(kp + kp_ofs, 0, PAGE_SIZE - kp_ofs);
9962306a36Sopenharmony_ci	return;
10062306a36Sopenharmony_ci}
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci/**
10362306a36Sopenharmony_ci * handle_bounds_compressed_page - test for&handle out of bounds compressed page
10462306a36Sopenharmony_ci */
10562306a36Sopenharmony_cistatic inline void handle_bounds_compressed_page(struct page *page,
10662306a36Sopenharmony_ci		const loff_t i_size, const s64 initialized_size)
10762306a36Sopenharmony_ci{
10862306a36Sopenharmony_ci	if ((page->index >= (initialized_size >> PAGE_SHIFT)) &&
10962306a36Sopenharmony_ci			(initialized_size < i_size))
11062306a36Sopenharmony_ci		zero_partial_compressed_page(page, initialized_size);
11162306a36Sopenharmony_ci	return;
11262306a36Sopenharmony_ci}
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci/**
11562306a36Sopenharmony_ci * ntfs_decompress - decompress a compression block into an array of pages
11662306a36Sopenharmony_ci * @dest_pages:		destination array of pages
11762306a36Sopenharmony_ci * @completed_pages:	scratch space to track completed pages
11862306a36Sopenharmony_ci * @dest_index:		current index into @dest_pages (IN/OUT)
11962306a36Sopenharmony_ci * @dest_ofs:		current offset within @dest_pages[@dest_index] (IN/OUT)
12062306a36Sopenharmony_ci * @dest_max_index:	maximum index into @dest_pages (IN)
12162306a36Sopenharmony_ci * @dest_max_ofs:	maximum offset within @dest_pages[@dest_max_index] (IN)
12262306a36Sopenharmony_ci * @xpage:		the target page (-1 if none) (IN)
12362306a36Sopenharmony_ci * @xpage_done:		set to 1 if xpage was completed successfully (IN/OUT)
12462306a36Sopenharmony_ci * @cb_start:		compression block to decompress (IN)
12562306a36Sopenharmony_ci * @cb_size:		size of compression block @cb_start in bytes (IN)
12662306a36Sopenharmony_ci * @i_size:		file size when we started the read (IN)
12762306a36Sopenharmony_ci * @initialized_size:	initialized file size when we started the read (IN)
12862306a36Sopenharmony_ci *
12962306a36Sopenharmony_ci * The caller must have disabled preemption. ntfs_decompress() reenables it when
13062306a36Sopenharmony_ci * the critical section is finished.
13162306a36Sopenharmony_ci *
13262306a36Sopenharmony_ci * This decompresses the compression block @cb_start into the array of
13362306a36Sopenharmony_ci * destination pages @dest_pages starting at index @dest_index into @dest_pages
13462306a36Sopenharmony_ci * and at offset @dest_pos into the page @dest_pages[@dest_index].
13562306a36Sopenharmony_ci *
13662306a36Sopenharmony_ci * When the page @dest_pages[@xpage] is completed, @xpage_done is set to 1.
13762306a36Sopenharmony_ci * If xpage is -1 or @xpage has not been completed, @xpage_done is not modified.
13862306a36Sopenharmony_ci *
13962306a36Sopenharmony_ci * @cb_start is a pointer to the compression block which needs decompressing
14062306a36Sopenharmony_ci * and @cb_size is the size of @cb_start in bytes (8-64kiB).
14162306a36Sopenharmony_ci *
14262306a36Sopenharmony_ci * Return 0 if success or -EOVERFLOW on error in the compressed stream.
14362306a36Sopenharmony_ci * @xpage_done indicates whether the target page (@dest_pages[@xpage]) was
14462306a36Sopenharmony_ci * completed during the decompression of the compression block (@cb_start).
14562306a36Sopenharmony_ci *
14662306a36Sopenharmony_ci * Warning: This function *REQUIRES* PAGE_SIZE >= 4096 or it will blow up
14762306a36Sopenharmony_ci * unpredicatbly! You have been warned!
14862306a36Sopenharmony_ci *
14962306a36Sopenharmony_ci * Note to hackers: This function may not sleep until it has finished accessing
15062306a36Sopenharmony_ci * the compression block @cb_start as it is a per-CPU buffer.
15162306a36Sopenharmony_ci */
15262306a36Sopenharmony_cistatic int ntfs_decompress(struct page *dest_pages[], int completed_pages[],
15362306a36Sopenharmony_ci		int *dest_index, int *dest_ofs, const int dest_max_index,
15462306a36Sopenharmony_ci		const int dest_max_ofs, const int xpage, char *xpage_done,
15562306a36Sopenharmony_ci		u8 *const cb_start, const u32 cb_size, const loff_t i_size,
15662306a36Sopenharmony_ci		const s64 initialized_size)
15762306a36Sopenharmony_ci{
15862306a36Sopenharmony_ci	/*
15962306a36Sopenharmony_ci	 * Pointers into the compressed data, i.e. the compression block (cb),
16062306a36Sopenharmony_ci	 * and the therein contained sub-blocks (sb).
16162306a36Sopenharmony_ci	 */
16262306a36Sopenharmony_ci	u8 *cb_end = cb_start + cb_size; /* End of cb. */
16362306a36Sopenharmony_ci	u8 *cb = cb_start;	/* Current position in cb. */
16462306a36Sopenharmony_ci	u8 *cb_sb_start;	/* Beginning of the current sb in the cb. */
16562306a36Sopenharmony_ci	u8 *cb_sb_end;		/* End of current sb / beginning of next sb. */
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci	/* Variables for uncompressed data / destination. */
16862306a36Sopenharmony_ci	struct page *dp;	/* Current destination page being worked on. */
16962306a36Sopenharmony_ci	u8 *dp_addr;		/* Current pointer into dp. */
17062306a36Sopenharmony_ci	u8 *dp_sb_start;	/* Start of current sub-block in dp. */
17162306a36Sopenharmony_ci	u8 *dp_sb_end;		/* End of current sb in dp (dp_sb_start +
17262306a36Sopenharmony_ci				   NTFS_SB_SIZE). */
17362306a36Sopenharmony_ci	u16 do_sb_start;	/* @dest_ofs when starting this sub-block. */
17462306a36Sopenharmony_ci	u16 do_sb_end;		/* @dest_ofs of end of this sb (do_sb_start +
17562306a36Sopenharmony_ci				   NTFS_SB_SIZE). */
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci	/* Variables for tag and token parsing. */
17862306a36Sopenharmony_ci	u8 tag;			/* Current tag. */
17962306a36Sopenharmony_ci	int token;		/* Loop counter for the eight tokens in tag. */
18062306a36Sopenharmony_ci	int nr_completed_pages = 0;
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci	/* Default error code. */
18362306a36Sopenharmony_ci	int err = -EOVERFLOW;
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_ci	ntfs_debug("Entering, cb_size = 0x%x.", cb_size);
18662306a36Sopenharmony_cido_next_sb:
18762306a36Sopenharmony_ci	ntfs_debug("Beginning sub-block at offset = 0x%zx in the cb.",
18862306a36Sopenharmony_ci			cb - cb_start);
18962306a36Sopenharmony_ci	/*
19062306a36Sopenharmony_ci	 * Have we reached the end of the compression block or the end of the
19162306a36Sopenharmony_ci	 * decompressed data?  The latter can happen for example if the current
19262306a36Sopenharmony_ci	 * position in the compression block is one byte before its end so the
19362306a36Sopenharmony_ci	 * first two checks do not detect it.
19462306a36Sopenharmony_ci	 */
19562306a36Sopenharmony_ci	if (cb == cb_end || !le16_to_cpup((le16*)cb) ||
19662306a36Sopenharmony_ci			(*dest_index == dest_max_index &&
19762306a36Sopenharmony_ci			*dest_ofs == dest_max_ofs)) {
19862306a36Sopenharmony_ci		int i;
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ci		ntfs_debug("Completed. Returning success (0).");
20162306a36Sopenharmony_ci		err = 0;
20262306a36Sopenharmony_cireturn_error:
20362306a36Sopenharmony_ci		/* We can sleep from now on, so we drop lock. */
20462306a36Sopenharmony_ci		spin_unlock(&ntfs_cb_lock);
20562306a36Sopenharmony_ci		/* Second stage: finalize completed pages. */
20662306a36Sopenharmony_ci		if (nr_completed_pages > 0) {
20762306a36Sopenharmony_ci			for (i = 0; i < nr_completed_pages; i++) {
20862306a36Sopenharmony_ci				int di = completed_pages[i];
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ci				dp = dest_pages[di];
21162306a36Sopenharmony_ci				/*
21262306a36Sopenharmony_ci				 * If we are outside the initialized size, zero
21362306a36Sopenharmony_ci				 * the out of bounds page range.
21462306a36Sopenharmony_ci				 */
21562306a36Sopenharmony_ci				handle_bounds_compressed_page(dp, i_size,
21662306a36Sopenharmony_ci						initialized_size);
21762306a36Sopenharmony_ci				flush_dcache_page(dp);
21862306a36Sopenharmony_ci				kunmap(dp);
21962306a36Sopenharmony_ci				SetPageUptodate(dp);
22062306a36Sopenharmony_ci				unlock_page(dp);
22162306a36Sopenharmony_ci				if (di == xpage)
22262306a36Sopenharmony_ci					*xpage_done = 1;
22362306a36Sopenharmony_ci				else
22462306a36Sopenharmony_ci					put_page(dp);
22562306a36Sopenharmony_ci				dest_pages[di] = NULL;
22662306a36Sopenharmony_ci			}
22762306a36Sopenharmony_ci		}
22862306a36Sopenharmony_ci		return err;
22962306a36Sopenharmony_ci	}
23062306a36Sopenharmony_ci
23162306a36Sopenharmony_ci	/* Setup offsets for the current sub-block destination. */
23262306a36Sopenharmony_ci	do_sb_start = *dest_ofs;
23362306a36Sopenharmony_ci	do_sb_end = do_sb_start + NTFS_SB_SIZE;
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_ci	/* Check that we are still within allowed boundaries. */
23662306a36Sopenharmony_ci	if (*dest_index == dest_max_index && do_sb_end > dest_max_ofs)
23762306a36Sopenharmony_ci		goto return_overflow;
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ci	/* Does the minimum size of a compressed sb overflow valid range? */
24062306a36Sopenharmony_ci	if (cb + 6 > cb_end)
24162306a36Sopenharmony_ci		goto return_overflow;
24262306a36Sopenharmony_ci
24362306a36Sopenharmony_ci	/* Setup the current sub-block source pointers and validate range. */
24462306a36Sopenharmony_ci	cb_sb_start = cb;
24562306a36Sopenharmony_ci	cb_sb_end = cb_sb_start + (le16_to_cpup((le16*)cb) & NTFS_SB_SIZE_MASK)
24662306a36Sopenharmony_ci			+ 3;
24762306a36Sopenharmony_ci	if (cb_sb_end > cb_end)
24862306a36Sopenharmony_ci		goto return_overflow;
24962306a36Sopenharmony_ci
25062306a36Sopenharmony_ci	/* Get the current destination page. */
25162306a36Sopenharmony_ci	dp = dest_pages[*dest_index];
25262306a36Sopenharmony_ci	if (!dp) {
25362306a36Sopenharmony_ci		/* No page present. Skip decompression of this sub-block. */
25462306a36Sopenharmony_ci		cb = cb_sb_end;
25562306a36Sopenharmony_ci
25662306a36Sopenharmony_ci		/* Advance destination position to next sub-block. */
25762306a36Sopenharmony_ci		*dest_ofs = (*dest_ofs + NTFS_SB_SIZE) & ~PAGE_MASK;
25862306a36Sopenharmony_ci		if (!*dest_ofs && (++*dest_index > dest_max_index))
25962306a36Sopenharmony_ci			goto return_overflow;
26062306a36Sopenharmony_ci		goto do_next_sb;
26162306a36Sopenharmony_ci	}
26262306a36Sopenharmony_ci
26362306a36Sopenharmony_ci	/* We have a valid destination page. Setup the destination pointers. */
26462306a36Sopenharmony_ci	dp_addr = (u8*)page_address(dp) + do_sb_start;
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_ci	/* Now, we are ready to process the current sub-block (sb). */
26762306a36Sopenharmony_ci	if (!(le16_to_cpup((le16*)cb) & NTFS_SB_IS_COMPRESSED)) {
26862306a36Sopenharmony_ci		ntfs_debug("Found uncompressed sub-block.");
26962306a36Sopenharmony_ci		/* This sb is not compressed, just copy it into destination. */
27062306a36Sopenharmony_ci
27162306a36Sopenharmony_ci		/* Advance source position to first data byte. */
27262306a36Sopenharmony_ci		cb += 2;
27362306a36Sopenharmony_ci
27462306a36Sopenharmony_ci		/* An uncompressed sb must be full size. */
27562306a36Sopenharmony_ci		if (cb_sb_end - cb != NTFS_SB_SIZE)
27662306a36Sopenharmony_ci			goto return_overflow;
27762306a36Sopenharmony_ci
27862306a36Sopenharmony_ci		/* Copy the block and advance the source position. */
27962306a36Sopenharmony_ci		memcpy(dp_addr, cb, NTFS_SB_SIZE);
28062306a36Sopenharmony_ci		cb += NTFS_SB_SIZE;
28162306a36Sopenharmony_ci
28262306a36Sopenharmony_ci		/* Advance destination position to next sub-block. */
28362306a36Sopenharmony_ci		*dest_ofs += NTFS_SB_SIZE;
28462306a36Sopenharmony_ci		if (!(*dest_ofs &= ~PAGE_MASK)) {
28562306a36Sopenharmony_cifinalize_page:
28662306a36Sopenharmony_ci			/*
28762306a36Sopenharmony_ci			 * First stage: add current page index to array of
28862306a36Sopenharmony_ci			 * completed pages.
28962306a36Sopenharmony_ci			 */
29062306a36Sopenharmony_ci			completed_pages[nr_completed_pages++] = *dest_index;
29162306a36Sopenharmony_ci			if (++*dest_index > dest_max_index)
29262306a36Sopenharmony_ci				goto return_overflow;
29362306a36Sopenharmony_ci		}
29462306a36Sopenharmony_ci		goto do_next_sb;
29562306a36Sopenharmony_ci	}
29662306a36Sopenharmony_ci	ntfs_debug("Found compressed sub-block.");
29762306a36Sopenharmony_ci	/* This sb is compressed, decompress it into destination. */
29862306a36Sopenharmony_ci
29962306a36Sopenharmony_ci	/* Setup destination pointers. */
30062306a36Sopenharmony_ci	dp_sb_start = dp_addr;
30162306a36Sopenharmony_ci	dp_sb_end = dp_sb_start + NTFS_SB_SIZE;
30262306a36Sopenharmony_ci
30362306a36Sopenharmony_ci	/* Forward to the first tag in the sub-block. */
30462306a36Sopenharmony_ci	cb += 2;
30562306a36Sopenharmony_cido_next_tag:
30662306a36Sopenharmony_ci	if (cb == cb_sb_end) {
30762306a36Sopenharmony_ci		/* Check if the decompressed sub-block was not full-length. */
30862306a36Sopenharmony_ci		if (dp_addr < dp_sb_end) {
30962306a36Sopenharmony_ci			int nr_bytes = do_sb_end - *dest_ofs;
31062306a36Sopenharmony_ci
31162306a36Sopenharmony_ci			ntfs_debug("Filling incomplete sub-block with "
31262306a36Sopenharmony_ci					"zeroes.");
31362306a36Sopenharmony_ci			/* Zero remainder and update destination position. */
31462306a36Sopenharmony_ci			memset(dp_addr, 0, nr_bytes);
31562306a36Sopenharmony_ci			*dest_ofs += nr_bytes;
31662306a36Sopenharmony_ci		}
31762306a36Sopenharmony_ci		/* We have finished the current sub-block. */
31862306a36Sopenharmony_ci		if (!(*dest_ofs &= ~PAGE_MASK))
31962306a36Sopenharmony_ci			goto finalize_page;
32062306a36Sopenharmony_ci		goto do_next_sb;
32162306a36Sopenharmony_ci	}
32262306a36Sopenharmony_ci
32362306a36Sopenharmony_ci	/* Check we are still in range. */
32462306a36Sopenharmony_ci	if (cb > cb_sb_end || dp_addr > dp_sb_end)
32562306a36Sopenharmony_ci		goto return_overflow;
32662306a36Sopenharmony_ci
32762306a36Sopenharmony_ci	/* Get the next tag and advance to first token. */
32862306a36Sopenharmony_ci	tag = *cb++;
32962306a36Sopenharmony_ci
33062306a36Sopenharmony_ci	/* Parse the eight tokens described by the tag. */
33162306a36Sopenharmony_ci	for (token = 0; token < 8; token++, tag >>= 1) {
33262306a36Sopenharmony_ci		u16 lg, pt, length, max_non_overlap;
33362306a36Sopenharmony_ci		register u16 i;
33462306a36Sopenharmony_ci		u8 *dp_back_addr;
33562306a36Sopenharmony_ci
33662306a36Sopenharmony_ci		/* Check if we are done / still in range. */
33762306a36Sopenharmony_ci		if (cb >= cb_sb_end || dp_addr > dp_sb_end)
33862306a36Sopenharmony_ci			break;
33962306a36Sopenharmony_ci
34062306a36Sopenharmony_ci		/* Determine token type and parse appropriately.*/
34162306a36Sopenharmony_ci		if ((tag & NTFS_TOKEN_MASK) == NTFS_SYMBOL_TOKEN) {
34262306a36Sopenharmony_ci			/*
34362306a36Sopenharmony_ci			 * We have a symbol token, copy the symbol across, and
34462306a36Sopenharmony_ci			 * advance the source and destination positions.
34562306a36Sopenharmony_ci			 */
34662306a36Sopenharmony_ci			*dp_addr++ = *cb++;
34762306a36Sopenharmony_ci			++*dest_ofs;
34862306a36Sopenharmony_ci
34962306a36Sopenharmony_ci			/* Continue with the next token. */
35062306a36Sopenharmony_ci			continue;
35162306a36Sopenharmony_ci		}
35262306a36Sopenharmony_ci
35362306a36Sopenharmony_ci		/*
35462306a36Sopenharmony_ci		 * We have a phrase token. Make sure it is not the first tag in
35562306a36Sopenharmony_ci		 * the sb as this is illegal and would confuse the code below.
35662306a36Sopenharmony_ci		 */
35762306a36Sopenharmony_ci		if (dp_addr == dp_sb_start)
35862306a36Sopenharmony_ci			goto return_overflow;
35962306a36Sopenharmony_ci
36062306a36Sopenharmony_ci		/*
36162306a36Sopenharmony_ci		 * Determine the number of bytes to go back (p) and the number
36262306a36Sopenharmony_ci		 * of bytes to copy (l). We use an optimized algorithm in which
36362306a36Sopenharmony_ci		 * we first calculate log2(current destination position in sb),
36462306a36Sopenharmony_ci		 * which allows determination of l and p in O(1) rather than
36562306a36Sopenharmony_ci		 * O(n). We just need an arch-optimized log2() function now.
36662306a36Sopenharmony_ci		 */
36762306a36Sopenharmony_ci		lg = 0;
36862306a36Sopenharmony_ci		for (i = *dest_ofs - do_sb_start - 1; i >= 0x10; i >>= 1)
36962306a36Sopenharmony_ci			lg++;
37062306a36Sopenharmony_ci
37162306a36Sopenharmony_ci		/* Get the phrase token into i. */
37262306a36Sopenharmony_ci		pt = le16_to_cpup((le16*)cb);
37362306a36Sopenharmony_ci
37462306a36Sopenharmony_ci		/*
37562306a36Sopenharmony_ci		 * Calculate starting position of the byte sequence in
37662306a36Sopenharmony_ci		 * the destination using the fact that p = (pt >> (12 - lg)) + 1
37762306a36Sopenharmony_ci		 * and make sure we don't go too far back.
37862306a36Sopenharmony_ci		 */
37962306a36Sopenharmony_ci		dp_back_addr = dp_addr - (pt >> (12 - lg)) - 1;
38062306a36Sopenharmony_ci		if (dp_back_addr < dp_sb_start)
38162306a36Sopenharmony_ci			goto return_overflow;
38262306a36Sopenharmony_ci
38362306a36Sopenharmony_ci		/* Now calculate the length of the byte sequence. */
38462306a36Sopenharmony_ci		length = (pt & (0xfff >> lg)) + 3;
38562306a36Sopenharmony_ci
38662306a36Sopenharmony_ci		/* Advance destination position and verify it is in range. */
38762306a36Sopenharmony_ci		*dest_ofs += length;
38862306a36Sopenharmony_ci		if (*dest_ofs > do_sb_end)
38962306a36Sopenharmony_ci			goto return_overflow;
39062306a36Sopenharmony_ci
39162306a36Sopenharmony_ci		/* The number of non-overlapping bytes. */
39262306a36Sopenharmony_ci		max_non_overlap = dp_addr - dp_back_addr;
39362306a36Sopenharmony_ci
39462306a36Sopenharmony_ci		if (length <= max_non_overlap) {
39562306a36Sopenharmony_ci			/* The byte sequence doesn't overlap, just copy it. */
39662306a36Sopenharmony_ci			memcpy(dp_addr, dp_back_addr, length);
39762306a36Sopenharmony_ci
39862306a36Sopenharmony_ci			/* Advance destination pointer. */
39962306a36Sopenharmony_ci			dp_addr += length;
40062306a36Sopenharmony_ci		} else {
40162306a36Sopenharmony_ci			/*
40262306a36Sopenharmony_ci			 * The byte sequence does overlap, copy non-overlapping
40362306a36Sopenharmony_ci			 * part and then do a slow byte by byte copy for the
40462306a36Sopenharmony_ci			 * overlapping part. Also, advance the destination
40562306a36Sopenharmony_ci			 * pointer.
40662306a36Sopenharmony_ci			 */
40762306a36Sopenharmony_ci			memcpy(dp_addr, dp_back_addr, max_non_overlap);
40862306a36Sopenharmony_ci			dp_addr += max_non_overlap;
40962306a36Sopenharmony_ci			dp_back_addr += max_non_overlap;
41062306a36Sopenharmony_ci			length -= max_non_overlap;
41162306a36Sopenharmony_ci			while (length--)
41262306a36Sopenharmony_ci				*dp_addr++ = *dp_back_addr++;
41362306a36Sopenharmony_ci		}
41462306a36Sopenharmony_ci
41562306a36Sopenharmony_ci		/* Advance source position and continue with the next token. */
41662306a36Sopenharmony_ci		cb += 2;
41762306a36Sopenharmony_ci	}
41862306a36Sopenharmony_ci
41962306a36Sopenharmony_ci	/* No tokens left in the current tag. Continue with the next tag. */
42062306a36Sopenharmony_ci	goto do_next_tag;
42162306a36Sopenharmony_ci
42262306a36Sopenharmony_cireturn_overflow:
42362306a36Sopenharmony_ci	ntfs_error(NULL, "Failed. Returning -EOVERFLOW.");
42462306a36Sopenharmony_ci	goto return_error;
42562306a36Sopenharmony_ci}
42662306a36Sopenharmony_ci
42762306a36Sopenharmony_ci/**
42862306a36Sopenharmony_ci * ntfs_read_compressed_block - read a compressed block into the page cache
42962306a36Sopenharmony_ci * @page:	locked page in the compression block(s) we need to read
43062306a36Sopenharmony_ci *
43162306a36Sopenharmony_ci * When we are called the page has already been verified to be locked and the
43262306a36Sopenharmony_ci * attribute is known to be non-resident, not encrypted, but compressed.
43362306a36Sopenharmony_ci *
43462306a36Sopenharmony_ci * 1. Determine which compression block(s) @page is in.
43562306a36Sopenharmony_ci * 2. Get hold of all pages corresponding to this/these compression block(s).
43662306a36Sopenharmony_ci * 3. Read the (first) compression block.
43762306a36Sopenharmony_ci * 4. Decompress it into the corresponding pages.
43862306a36Sopenharmony_ci * 5. Throw the compressed data away and proceed to 3. for the next compression
43962306a36Sopenharmony_ci *    block or return success if no more compression blocks left.
44062306a36Sopenharmony_ci *
44162306a36Sopenharmony_ci * Warning: We have to be careful what we do about existing pages. They might
44262306a36Sopenharmony_ci * have been written to so that we would lose data if we were to just overwrite
44362306a36Sopenharmony_ci * them with the out-of-date uncompressed data.
44462306a36Sopenharmony_ci *
44562306a36Sopenharmony_ci * FIXME: For PAGE_SIZE > cb_size we are not doing the Right Thing(TM) at
44662306a36Sopenharmony_ci * the end of the file I think. We need to detect this case and zero the out
44762306a36Sopenharmony_ci * of bounds remainder of the page in question and mark it as handled. At the
44862306a36Sopenharmony_ci * moment we would just return -EIO on such a page. This bug will only become
44962306a36Sopenharmony_ci * apparent if pages are above 8kiB and the NTFS volume only uses 512 byte
45062306a36Sopenharmony_ci * clusters so is probably not going to be seen by anyone. Still this should
45162306a36Sopenharmony_ci * be fixed. (AIA)
45262306a36Sopenharmony_ci *
45362306a36Sopenharmony_ci * FIXME: Again for PAGE_SIZE > cb_size we are screwing up both in
45462306a36Sopenharmony_ci * handling sparse and compressed cbs. (AIA)
45562306a36Sopenharmony_ci *
45662306a36Sopenharmony_ci * FIXME: At the moment we don't do any zeroing out in the case that
45762306a36Sopenharmony_ci * initialized_size is less than data_size. This should be safe because of the
45862306a36Sopenharmony_ci * nature of the compression algorithm used. Just in case we check and output
45962306a36Sopenharmony_ci * an error message in read inode if the two sizes are not equal for a
46062306a36Sopenharmony_ci * compressed file. (AIA)
46162306a36Sopenharmony_ci */
46262306a36Sopenharmony_ciint ntfs_read_compressed_block(struct page *page)
46362306a36Sopenharmony_ci{
46462306a36Sopenharmony_ci	loff_t i_size;
46562306a36Sopenharmony_ci	s64 initialized_size;
46662306a36Sopenharmony_ci	struct address_space *mapping = page->mapping;
46762306a36Sopenharmony_ci	ntfs_inode *ni = NTFS_I(mapping->host);
46862306a36Sopenharmony_ci	ntfs_volume *vol = ni->vol;
46962306a36Sopenharmony_ci	struct super_block *sb = vol->sb;
47062306a36Sopenharmony_ci	runlist_element *rl;
47162306a36Sopenharmony_ci	unsigned long flags, block_size = sb->s_blocksize;
47262306a36Sopenharmony_ci	unsigned char block_size_bits = sb->s_blocksize_bits;
47362306a36Sopenharmony_ci	u8 *cb, *cb_pos, *cb_end;
47462306a36Sopenharmony_ci	struct buffer_head **bhs;
47562306a36Sopenharmony_ci	unsigned long offset, index = page->index;
47662306a36Sopenharmony_ci	u32 cb_size = ni->itype.compressed.block_size;
47762306a36Sopenharmony_ci	u64 cb_size_mask = cb_size - 1UL;
47862306a36Sopenharmony_ci	VCN vcn;
47962306a36Sopenharmony_ci	LCN lcn;
48062306a36Sopenharmony_ci	/* The first wanted vcn (minimum alignment is PAGE_SIZE). */
48162306a36Sopenharmony_ci	VCN start_vcn = (((s64)index << PAGE_SHIFT) & ~cb_size_mask) >>
48262306a36Sopenharmony_ci			vol->cluster_size_bits;
48362306a36Sopenharmony_ci	/*
48462306a36Sopenharmony_ci	 * The first vcn after the last wanted vcn (minimum alignment is again
48562306a36Sopenharmony_ci	 * PAGE_SIZE.
48662306a36Sopenharmony_ci	 */
48762306a36Sopenharmony_ci	VCN end_vcn = ((((s64)(index + 1UL) << PAGE_SHIFT) + cb_size - 1)
48862306a36Sopenharmony_ci			& ~cb_size_mask) >> vol->cluster_size_bits;
48962306a36Sopenharmony_ci	/* Number of compression blocks (cbs) in the wanted vcn range. */
49062306a36Sopenharmony_ci	unsigned int nr_cbs = (end_vcn - start_vcn) << vol->cluster_size_bits
49162306a36Sopenharmony_ci			>> ni->itype.compressed.block_size_bits;
49262306a36Sopenharmony_ci	/*
49362306a36Sopenharmony_ci	 * Number of pages required to store the uncompressed data from all
49462306a36Sopenharmony_ci	 * compression blocks (cbs) overlapping @page. Due to alignment
49562306a36Sopenharmony_ci	 * guarantees of start_vcn and end_vcn, no need to round up here.
49662306a36Sopenharmony_ci	 */
49762306a36Sopenharmony_ci	unsigned int nr_pages = (end_vcn - start_vcn) <<
49862306a36Sopenharmony_ci			vol->cluster_size_bits >> PAGE_SHIFT;
49962306a36Sopenharmony_ci	unsigned int xpage, max_page, cur_page, cur_ofs, i;
50062306a36Sopenharmony_ci	unsigned int cb_clusters, cb_max_ofs;
50162306a36Sopenharmony_ci	int block, max_block, cb_max_page, bhs_size, nr_bhs, err = 0;
50262306a36Sopenharmony_ci	struct page **pages;
50362306a36Sopenharmony_ci	int *completed_pages;
50462306a36Sopenharmony_ci	unsigned char xpage_done = 0;
50562306a36Sopenharmony_ci
50662306a36Sopenharmony_ci	ntfs_debug("Entering, page->index = 0x%lx, cb_size = 0x%x, nr_pages = "
50762306a36Sopenharmony_ci			"%i.", index, cb_size, nr_pages);
50862306a36Sopenharmony_ci	/*
50962306a36Sopenharmony_ci	 * Bad things happen if we get here for anything that is not an
51062306a36Sopenharmony_ci	 * unnamed $DATA attribute.
51162306a36Sopenharmony_ci	 */
51262306a36Sopenharmony_ci	BUG_ON(ni->type != AT_DATA);
51362306a36Sopenharmony_ci	BUG_ON(ni->name_len);
51462306a36Sopenharmony_ci
51562306a36Sopenharmony_ci	pages = kmalloc_array(nr_pages, sizeof(struct page *), GFP_NOFS);
51662306a36Sopenharmony_ci	completed_pages = kmalloc_array(nr_pages + 1, sizeof(int), GFP_NOFS);
51762306a36Sopenharmony_ci
51862306a36Sopenharmony_ci	/* Allocate memory to store the buffer heads we need. */
51962306a36Sopenharmony_ci	bhs_size = cb_size / block_size * sizeof(struct buffer_head *);
52062306a36Sopenharmony_ci	bhs = kmalloc(bhs_size, GFP_NOFS);
52162306a36Sopenharmony_ci
52262306a36Sopenharmony_ci	if (unlikely(!pages || !bhs || !completed_pages)) {
52362306a36Sopenharmony_ci		kfree(bhs);
52462306a36Sopenharmony_ci		kfree(pages);
52562306a36Sopenharmony_ci		kfree(completed_pages);
52662306a36Sopenharmony_ci		unlock_page(page);
52762306a36Sopenharmony_ci		ntfs_error(vol->sb, "Failed to allocate internal buffers.");
52862306a36Sopenharmony_ci		return -ENOMEM;
52962306a36Sopenharmony_ci	}
53062306a36Sopenharmony_ci
53162306a36Sopenharmony_ci	/*
53262306a36Sopenharmony_ci	 * We have already been given one page, this is the one we must do.
53362306a36Sopenharmony_ci	 * Once again, the alignment guarantees keep it simple.
53462306a36Sopenharmony_ci	 */
53562306a36Sopenharmony_ci	offset = start_vcn << vol->cluster_size_bits >> PAGE_SHIFT;
53662306a36Sopenharmony_ci	xpage = index - offset;
53762306a36Sopenharmony_ci	pages[xpage] = page;
53862306a36Sopenharmony_ci	/*
53962306a36Sopenharmony_ci	 * The remaining pages need to be allocated and inserted into the page
54062306a36Sopenharmony_ci	 * cache, alignment guarantees keep all the below much simpler. (-8
54162306a36Sopenharmony_ci	 */
54262306a36Sopenharmony_ci	read_lock_irqsave(&ni->size_lock, flags);
54362306a36Sopenharmony_ci	i_size = i_size_read(VFS_I(ni));
54462306a36Sopenharmony_ci	initialized_size = ni->initialized_size;
54562306a36Sopenharmony_ci	read_unlock_irqrestore(&ni->size_lock, flags);
54662306a36Sopenharmony_ci	max_page = ((i_size + PAGE_SIZE - 1) >> PAGE_SHIFT) -
54762306a36Sopenharmony_ci			offset;
54862306a36Sopenharmony_ci	/* Is the page fully outside i_size? (truncate in progress) */
54962306a36Sopenharmony_ci	if (xpage >= max_page) {
55062306a36Sopenharmony_ci		kfree(bhs);
55162306a36Sopenharmony_ci		kfree(pages);
55262306a36Sopenharmony_ci		kfree(completed_pages);
55362306a36Sopenharmony_ci		zero_user(page, 0, PAGE_SIZE);
55462306a36Sopenharmony_ci		ntfs_debug("Compressed read outside i_size - truncated?");
55562306a36Sopenharmony_ci		SetPageUptodate(page);
55662306a36Sopenharmony_ci		unlock_page(page);
55762306a36Sopenharmony_ci		return 0;
55862306a36Sopenharmony_ci	}
55962306a36Sopenharmony_ci	if (nr_pages < max_page)
56062306a36Sopenharmony_ci		max_page = nr_pages;
56162306a36Sopenharmony_ci	for (i = 0; i < max_page; i++, offset++) {
56262306a36Sopenharmony_ci		if (i != xpage)
56362306a36Sopenharmony_ci			pages[i] = grab_cache_page_nowait(mapping, offset);
56462306a36Sopenharmony_ci		page = pages[i];
56562306a36Sopenharmony_ci		if (page) {
56662306a36Sopenharmony_ci			/*
56762306a36Sopenharmony_ci			 * We only (re)read the page if it isn't already read
56862306a36Sopenharmony_ci			 * in and/or dirty or we would be losing data or at
56962306a36Sopenharmony_ci			 * least wasting our time.
57062306a36Sopenharmony_ci			 */
57162306a36Sopenharmony_ci			if (!PageDirty(page) && (!PageUptodate(page) ||
57262306a36Sopenharmony_ci					PageError(page))) {
57362306a36Sopenharmony_ci				ClearPageError(page);
57462306a36Sopenharmony_ci				kmap(page);
57562306a36Sopenharmony_ci				continue;
57662306a36Sopenharmony_ci			}
57762306a36Sopenharmony_ci			unlock_page(page);
57862306a36Sopenharmony_ci			put_page(page);
57962306a36Sopenharmony_ci			pages[i] = NULL;
58062306a36Sopenharmony_ci		}
58162306a36Sopenharmony_ci	}
58262306a36Sopenharmony_ci
58362306a36Sopenharmony_ci	/*
58462306a36Sopenharmony_ci	 * We have the runlist, and all the destination pages we need to fill.
58562306a36Sopenharmony_ci	 * Now read the first compression block.
58662306a36Sopenharmony_ci	 */
58762306a36Sopenharmony_ci	cur_page = 0;
58862306a36Sopenharmony_ci	cur_ofs = 0;
58962306a36Sopenharmony_ci	cb_clusters = ni->itype.compressed.block_clusters;
59062306a36Sopenharmony_cido_next_cb:
59162306a36Sopenharmony_ci	nr_cbs--;
59262306a36Sopenharmony_ci	nr_bhs = 0;
59362306a36Sopenharmony_ci
59462306a36Sopenharmony_ci	/* Read all cb buffer heads one cluster at a time. */
59562306a36Sopenharmony_ci	rl = NULL;
59662306a36Sopenharmony_ci	for (vcn = start_vcn, start_vcn += cb_clusters; vcn < start_vcn;
59762306a36Sopenharmony_ci			vcn++) {
59862306a36Sopenharmony_ci		bool is_retry = false;
59962306a36Sopenharmony_ci
60062306a36Sopenharmony_ci		if (!rl) {
60162306a36Sopenharmony_cilock_retry_remap:
60262306a36Sopenharmony_ci			down_read(&ni->runlist.lock);
60362306a36Sopenharmony_ci			rl = ni->runlist.rl;
60462306a36Sopenharmony_ci		}
60562306a36Sopenharmony_ci		if (likely(rl != NULL)) {
60662306a36Sopenharmony_ci			/* Seek to element containing target vcn. */
60762306a36Sopenharmony_ci			while (rl->length && rl[1].vcn <= vcn)
60862306a36Sopenharmony_ci				rl++;
60962306a36Sopenharmony_ci			lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
61062306a36Sopenharmony_ci		} else
61162306a36Sopenharmony_ci			lcn = LCN_RL_NOT_MAPPED;
61262306a36Sopenharmony_ci		ntfs_debug("Reading vcn = 0x%llx, lcn = 0x%llx.",
61362306a36Sopenharmony_ci				(unsigned long long)vcn,
61462306a36Sopenharmony_ci				(unsigned long long)lcn);
61562306a36Sopenharmony_ci		if (lcn < 0) {
61662306a36Sopenharmony_ci			/*
61762306a36Sopenharmony_ci			 * When we reach the first sparse cluster we have
61862306a36Sopenharmony_ci			 * finished with the cb.
61962306a36Sopenharmony_ci			 */
62062306a36Sopenharmony_ci			if (lcn == LCN_HOLE)
62162306a36Sopenharmony_ci				break;
62262306a36Sopenharmony_ci			if (is_retry || lcn != LCN_RL_NOT_MAPPED)
62362306a36Sopenharmony_ci				goto rl_err;
62462306a36Sopenharmony_ci			is_retry = true;
62562306a36Sopenharmony_ci			/*
62662306a36Sopenharmony_ci			 * Attempt to map runlist, dropping lock for the
62762306a36Sopenharmony_ci			 * duration.
62862306a36Sopenharmony_ci			 */
62962306a36Sopenharmony_ci			up_read(&ni->runlist.lock);
63062306a36Sopenharmony_ci			if (!ntfs_map_runlist(ni, vcn))
63162306a36Sopenharmony_ci				goto lock_retry_remap;
63262306a36Sopenharmony_ci			goto map_rl_err;
63362306a36Sopenharmony_ci		}
63462306a36Sopenharmony_ci		block = lcn << vol->cluster_size_bits >> block_size_bits;
63562306a36Sopenharmony_ci		/* Read the lcn from device in chunks of block_size bytes. */
63662306a36Sopenharmony_ci		max_block = block + (vol->cluster_size >> block_size_bits);
63762306a36Sopenharmony_ci		do {
63862306a36Sopenharmony_ci			ntfs_debug("block = 0x%x.", block);
63962306a36Sopenharmony_ci			if (unlikely(!(bhs[nr_bhs] = sb_getblk(sb, block))))
64062306a36Sopenharmony_ci				goto getblk_err;
64162306a36Sopenharmony_ci			nr_bhs++;
64262306a36Sopenharmony_ci		} while (++block < max_block);
64362306a36Sopenharmony_ci	}
64462306a36Sopenharmony_ci
64562306a36Sopenharmony_ci	/* Release the lock if we took it. */
64662306a36Sopenharmony_ci	if (rl)
64762306a36Sopenharmony_ci		up_read(&ni->runlist.lock);
64862306a36Sopenharmony_ci
64962306a36Sopenharmony_ci	/* Setup and initiate io on all buffer heads. */
65062306a36Sopenharmony_ci	for (i = 0; i < nr_bhs; i++) {
65162306a36Sopenharmony_ci		struct buffer_head *tbh = bhs[i];
65262306a36Sopenharmony_ci
65362306a36Sopenharmony_ci		if (!trylock_buffer(tbh))
65462306a36Sopenharmony_ci			continue;
65562306a36Sopenharmony_ci		if (unlikely(buffer_uptodate(tbh))) {
65662306a36Sopenharmony_ci			unlock_buffer(tbh);
65762306a36Sopenharmony_ci			continue;
65862306a36Sopenharmony_ci		}
65962306a36Sopenharmony_ci		get_bh(tbh);
66062306a36Sopenharmony_ci		tbh->b_end_io = end_buffer_read_sync;
66162306a36Sopenharmony_ci		submit_bh(REQ_OP_READ, tbh);
66262306a36Sopenharmony_ci	}
66362306a36Sopenharmony_ci
66462306a36Sopenharmony_ci	/* Wait for io completion on all buffer heads. */
66562306a36Sopenharmony_ci	for (i = 0; i < nr_bhs; i++) {
66662306a36Sopenharmony_ci		struct buffer_head *tbh = bhs[i];
66762306a36Sopenharmony_ci
66862306a36Sopenharmony_ci		if (buffer_uptodate(tbh))
66962306a36Sopenharmony_ci			continue;
67062306a36Sopenharmony_ci		wait_on_buffer(tbh);
67162306a36Sopenharmony_ci		/*
67262306a36Sopenharmony_ci		 * We need an optimization barrier here, otherwise we start
67362306a36Sopenharmony_ci		 * hitting the below fixup code when accessing a loopback
67462306a36Sopenharmony_ci		 * mounted ntfs partition. This indicates either there is a
67562306a36Sopenharmony_ci		 * race condition in the loop driver or, more likely, gcc
67662306a36Sopenharmony_ci		 * overoptimises the code without the barrier and it doesn't
67762306a36Sopenharmony_ci		 * do the Right Thing(TM).
67862306a36Sopenharmony_ci		 */
67962306a36Sopenharmony_ci		barrier();
68062306a36Sopenharmony_ci		if (unlikely(!buffer_uptodate(tbh))) {
68162306a36Sopenharmony_ci			ntfs_warning(vol->sb, "Buffer is unlocked but not "
68262306a36Sopenharmony_ci					"uptodate! Unplugging the disk queue "
68362306a36Sopenharmony_ci					"and rescheduling.");
68462306a36Sopenharmony_ci			get_bh(tbh);
68562306a36Sopenharmony_ci			io_schedule();
68662306a36Sopenharmony_ci			put_bh(tbh);
68762306a36Sopenharmony_ci			if (unlikely(!buffer_uptodate(tbh)))
68862306a36Sopenharmony_ci				goto read_err;
68962306a36Sopenharmony_ci			ntfs_warning(vol->sb, "Buffer is now uptodate. Good.");
69062306a36Sopenharmony_ci		}
69162306a36Sopenharmony_ci	}
69262306a36Sopenharmony_ci
69362306a36Sopenharmony_ci	/*
69462306a36Sopenharmony_ci	 * Get the compression buffer. We must not sleep any more
69562306a36Sopenharmony_ci	 * until we are finished with it.
69662306a36Sopenharmony_ci	 */
69762306a36Sopenharmony_ci	spin_lock(&ntfs_cb_lock);
69862306a36Sopenharmony_ci	cb = ntfs_compression_buffer;
69962306a36Sopenharmony_ci
70062306a36Sopenharmony_ci	BUG_ON(!cb);
70162306a36Sopenharmony_ci
70262306a36Sopenharmony_ci	cb_pos = cb;
70362306a36Sopenharmony_ci	cb_end = cb + cb_size;
70462306a36Sopenharmony_ci
70562306a36Sopenharmony_ci	/* Copy the buffer heads into the contiguous buffer. */
70662306a36Sopenharmony_ci	for (i = 0; i < nr_bhs; i++) {
70762306a36Sopenharmony_ci		memcpy(cb_pos, bhs[i]->b_data, block_size);
70862306a36Sopenharmony_ci		cb_pos += block_size;
70962306a36Sopenharmony_ci	}
71062306a36Sopenharmony_ci
71162306a36Sopenharmony_ci	/* Just a precaution. */
71262306a36Sopenharmony_ci	if (cb_pos + 2 <= cb + cb_size)
71362306a36Sopenharmony_ci		*(u16*)cb_pos = 0;
71462306a36Sopenharmony_ci
71562306a36Sopenharmony_ci	/* Reset cb_pos back to the beginning. */
71662306a36Sopenharmony_ci	cb_pos = cb;
71762306a36Sopenharmony_ci
71862306a36Sopenharmony_ci	/* We now have both source (if present) and destination. */
71962306a36Sopenharmony_ci	ntfs_debug("Successfully read the compression block.");
72062306a36Sopenharmony_ci
72162306a36Sopenharmony_ci	/* The last page and maximum offset within it for the current cb. */
72262306a36Sopenharmony_ci	cb_max_page = (cur_page << PAGE_SHIFT) + cur_ofs + cb_size;
72362306a36Sopenharmony_ci	cb_max_ofs = cb_max_page & ~PAGE_MASK;
72462306a36Sopenharmony_ci	cb_max_page >>= PAGE_SHIFT;
72562306a36Sopenharmony_ci
72662306a36Sopenharmony_ci	/* Catch end of file inside a compression block. */
72762306a36Sopenharmony_ci	if (cb_max_page > max_page)
72862306a36Sopenharmony_ci		cb_max_page = max_page;
72962306a36Sopenharmony_ci
73062306a36Sopenharmony_ci	if (vcn == start_vcn - cb_clusters) {
73162306a36Sopenharmony_ci		/* Sparse cb, zero out page range overlapping the cb. */
73262306a36Sopenharmony_ci		ntfs_debug("Found sparse compression block.");
73362306a36Sopenharmony_ci		/* We can sleep from now on, so we drop lock. */
73462306a36Sopenharmony_ci		spin_unlock(&ntfs_cb_lock);
73562306a36Sopenharmony_ci		if (cb_max_ofs)
73662306a36Sopenharmony_ci			cb_max_page--;
73762306a36Sopenharmony_ci		for (; cur_page < cb_max_page; cur_page++) {
73862306a36Sopenharmony_ci			page = pages[cur_page];
73962306a36Sopenharmony_ci			if (page) {
74062306a36Sopenharmony_ci				if (likely(!cur_ofs))
74162306a36Sopenharmony_ci					clear_page(page_address(page));
74262306a36Sopenharmony_ci				else
74362306a36Sopenharmony_ci					memset(page_address(page) + cur_ofs, 0,
74462306a36Sopenharmony_ci							PAGE_SIZE -
74562306a36Sopenharmony_ci							cur_ofs);
74662306a36Sopenharmony_ci				flush_dcache_page(page);
74762306a36Sopenharmony_ci				kunmap(page);
74862306a36Sopenharmony_ci				SetPageUptodate(page);
74962306a36Sopenharmony_ci				unlock_page(page);
75062306a36Sopenharmony_ci				if (cur_page == xpage)
75162306a36Sopenharmony_ci					xpage_done = 1;
75262306a36Sopenharmony_ci				else
75362306a36Sopenharmony_ci					put_page(page);
75462306a36Sopenharmony_ci				pages[cur_page] = NULL;
75562306a36Sopenharmony_ci			}
75662306a36Sopenharmony_ci			cb_pos += PAGE_SIZE - cur_ofs;
75762306a36Sopenharmony_ci			cur_ofs = 0;
75862306a36Sopenharmony_ci			if (cb_pos >= cb_end)
75962306a36Sopenharmony_ci				break;
76062306a36Sopenharmony_ci		}
76162306a36Sopenharmony_ci		/* If we have a partial final page, deal with it now. */
76262306a36Sopenharmony_ci		if (cb_max_ofs && cb_pos < cb_end) {
76362306a36Sopenharmony_ci			page = pages[cur_page];
76462306a36Sopenharmony_ci			if (page)
76562306a36Sopenharmony_ci				memset(page_address(page) + cur_ofs, 0,
76662306a36Sopenharmony_ci						cb_max_ofs - cur_ofs);
76762306a36Sopenharmony_ci			/*
76862306a36Sopenharmony_ci			 * No need to update cb_pos at this stage:
76962306a36Sopenharmony_ci			 *	cb_pos += cb_max_ofs - cur_ofs;
77062306a36Sopenharmony_ci			 */
77162306a36Sopenharmony_ci			cur_ofs = cb_max_ofs;
77262306a36Sopenharmony_ci		}
77362306a36Sopenharmony_ci	} else if (vcn == start_vcn) {
77462306a36Sopenharmony_ci		/* We can't sleep so we need two stages. */
77562306a36Sopenharmony_ci		unsigned int cur2_page = cur_page;
77662306a36Sopenharmony_ci		unsigned int cur_ofs2 = cur_ofs;
77762306a36Sopenharmony_ci		u8 *cb_pos2 = cb_pos;
77862306a36Sopenharmony_ci
77962306a36Sopenharmony_ci		ntfs_debug("Found uncompressed compression block.");
78062306a36Sopenharmony_ci		/* Uncompressed cb, copy it to the destination pages. */
78162306a36Sopenharmony_ci		/*
78262306a36Sopenharmony_ci		 * TODO: As a big optimization, we could detect this case
78362306a36Sopenharmony_ci		 * before we read all the pages and use block_read_full_folio()
78462306a36Sopenharmony_ci		 * on all full pages instead (we still have to treat partial
78562306a36Sopenharmony_ci		 * pages especially but at least we are getting rid of the
78662306a36Sopenharmony_ci		 * synchronous io for the majority of pages.
78762306a36Sopenharmony_ci		 * Or if we choose not to do the read-ahead/-behind stuff, we
78862306a36Sopenharmony_ci		 * could just return block_read_full_folio(pages[xpage]) as long
78962306a36Sopenharmony_ci		 * as PAGE_SIZE <= cb_size.
79062306a36Sopenharmony_ci		 */
79162306a36Sopenharmony_ci		if (cb_max_ofs)
79262306a36Sopenharmony_ci			cb_max_page--;
79362306a36Sopenharmony_ci		/* First stage: copy data into destination pages. */
79462306a36Sopenharmony_ci		for (; cur_page < cb_max_page; cur_page++) {
79562306a36Sopenharmony_ci			page = pages[cur_page];
79662306a36Sopenharmony_ci			if (page)
79762306a36Sopenharmony_ci				memcpy(page_address(page) + cur_ofs, cb_pos,
79862306a36Sopenharmony_ci						PAGE_SIZE - cur_ofs);
79962306a36Sopenharmony_ci			cb_pos += PAGE_SIZE - cur_ofs;
80062306a36Sopenharmony_ci			cur_ofs = 0;
80162306a36Sopenharmony_ci			if (cb_pos >= cb_end)
80262306a36Sopenharmony_ci				break;
80362306a36Sopenharmony_ci		}
80462306a36Sopenharmony_ci		/* If we have a partial final page, deal with it now. */
80562306a36Sopenharmony_ci		if (cb_max_ofs && cb_pos < cb_end) {
80662306a36Sopenharmony_ci			page = pages[cur_page];
80762306a36Sopenharmony_ci			if (page)
80862306a36Sopenharmony_ci				memcpy(page_address(page) + cur_ofs, cb_pos,
80962306a36Sopenharmony_ci						cb_max_ofs - cur_ofs);
81062306a36Sopenharmony_ci			cb_pos += cb_max_ofs - cur_ofs;
81162306a36Sopenharmony_ci			cur_ofs = cb_max_ofs;
81262306a36Sopenharmony_ci		}
81362306a36Sopenharmony_ci		/* We can sleep from now on, so drop lock. */
81462306a36Sopenharmony_ci		spin_unlock(&ntfs_cb_lock);
81562306a36Sopenharmony_ci		/* Second stage: finalize pages. */
81662306a36Sopenharmony_ci		for (; cur2_page < cb_max_page; cur2_page++) {
81762306a36Sopenharmony_ci			page = pages[cur2_page];
81862306a36Sopenharmony_ci			if (page) {
81962306a36Sopenharmony_ci				/*
82062306a36Sopenharmony_ci				 * If we are outside the initialized size, zero
82162306a36Sopenharmony_ci				 * the out of bounds page range.
82262306a36Sopenharmony_ci				 */
82362306a36Sopenharmony_ci				handle_bounds_compressed_page(page, i_size,
82462306a36Sopenharmony_ci						initialized_size);
82562306a36Sopenharmony_ci				flush_dcache_page(page);
82662306a36Sopenharmony_ci				kunmap(page);
82762306a36Sopenharmony_ci				SetPageUptodate(page);
82862306a36Sopenharmony_ci				unlock_page(page);
82962306a36Sopenharmony_ci				if (cur2_page == xpage)
83062306a36Sopenharmony_ci					xpage_done = 1;
83162306a36Sopenharmony_ci				else
83262306a36Sopenharmony_ci					put_page(page);
83362306a36Sopenharmony_ci				pages[cur2_page] = NULL;
83462306a36Sopenharmony_ci			}
83562306a36Sopenharmony_ci			cb_pos2 += PAGE_SIZE - cur_ofs2;
83662306a36Sopenharmony_ci			cur_ofs2 = 0;
83762306a36Sopenharmony_ci			if (cb_pos2 >= cb_end)
83862306a36Sopenharmony_ci				break;
83962306a36Sopenharmony_ci		}
84062306a36Sopenharmony_ci	} else {
84162306a36Sopenharmony_ci		/* Compressed cb, decompress it into the destination page(s). */
84262306a36Sopenharmony_ci		unsigned int prev_cur_page = cur_page;
84362306a36Sopenharmony_ci
84462306a36Sopenharmony_ci		ntfs_debug("Found compressed compression block.");
84562306a36Sopenharmony_ci		err = ntfs_decompress(pages, completed_pages, &cur_page,
84662306a36Sopenharmony_ci				&cur_ofs, cb_max_page, cb_max_ofs, xpage,
84762306a36Sopenharmony_ci				&xpage_done, cb_pos, cb_size - (cb_pos - cb),
84862306a36Sopenharmony_ci				i_size, initialized_size);
84962306a36Sopenharmony_ci		/*
85062306a36Sopenharmony_ci		 * We can sleep from now on, lock already dropped by
85162306a36Sopenharmony_ci		 * ntfs_decompress().
85262306a36Sopenharmony_ci		 */
85362306a36Sopenharmony_ci		if (err) {
85462306a36Sopenharmony_ci			ntfs_error(vol->sb, "ntfs_decompress() failed in inode "
85562306a36Sopenharmony_ci					"0x%lx with error code %i. Skipping "
85662306a36Sopenharmony_ci					"this compression block.",
85762306a36Sopenharmony_ci					ni->mft_no, -err);
85862306a36Sopenharmony_ci			/* Release the unfinished pages. */
85962306a36Sopenharmony_ci			for (; prev_cur_page < cur_page; prev_cur_page++) {
86062306a36Sopenharmony_ci				page = pages[prev_cur_page];
86162306a36Sopenharmony_ci				if (page) {
86262306a36Sopenharmony_ci					flush_dcache_page(page);
86362306a36Sopenharmony_ci					kunmap(page);
86462306a36Sopenharmony_ci					unlock_page(page);
86562306a36Sopenharmony_ci					if (prev_cur_page != xpage)
86662306a36Sopenharmony_ci						put_page(page);
86762306a36Sopenharmony_ci					pages[prev_cur_page] = NULL;
86862306a36Sopenharmony_ci				}
86962306a36Sopenharmony_ci			}
87062306a36Sopenharmony_ci		}
87162306a36Sopenharmony_ci	}
87262306a36Sopenharmony_ci
87362306a36Sopenharmony_ci	/* Release the buffer heads. */
87462306a36Sopenharmony_ci	for (i = 0; i < nr_bhs; i++)
87562306a36Sopenharmony_ci		brelse(bhs[i]);
87662306a36Sopenharmony_ci
87762306a36Sopenharmony_ci	/* Do we have more work to do? */
87862306a36Sopenharmony_ci	if (nr_cbs)
87962306a36Sopenharmony_ci		goto do_next_cb;
88062306a36Sopenharmony_ci
88162306a36Sopenharmony_ci	/* We no longer need the list of buffer heads. */
88262306a36Sopenharmony_ci	kfree(bhs);
88362306a36Sopenharmony_ci
88462306a36Sopenharmony_ci	/* Clean up if we have any pages left. Should never happen. */
88562306a36Sopenharmony_ci	for (cur_page = 0; cur_page < max_page; cur_page++) {
88662306a36Sopenharmony_ci		page = pages[cur_page];
88762306a36Sopenharmony_ci		if (page) {
88862306a36Sopenharmony_ci			ntfs_error(vol->sb, "Still have pages left! "
88962306a36Sopenharmony_ci					"Terminating them with extreme "
89062306a36Sopenharmony_ci					"prejudice.  Inode 0x%lx, page index "
89162306a36Sopenharmony_ci					"0x%lx.", ni->mft_no, page->index);
89262306a36Sopenharmony_ci			flush_dcache_page(page);
89362306a36Sopenharmony_ci			kunmap(page);
89462306a36Sopenharmony_ci			unlock_page(page);
89562306a36Sopenharmony_ci			if (cur_page != xpage)
89662306a36Sopenharmony_ci				put_page(page);
89762306a36Sopenharmony_ci			pages[cur_page] = NULL;
89862306a36Sopenharmony_ci		}
89962306a36Sopenharmony_ci	}
90062306a36Sopenharmony_ci
90162306a36Sopenharmony_ci	/* We no longer need the list of pages. */
90262306a36Sopenharmony_ci	kfree(pages);
90362306a36Sopenharmony_ci	kfree(completed_pages);
90462306a36Sopenharmony_ci
90562306a36Sopenharmony_ci	/* If we have completed the requested page, we return success. */
90662306a36Sopenharmony_ci	if (likely(xpage_done))
90762306a36Sopenharmony_ci		return 0;
90862306a36Sopenharmony_ci
90962306a36Sopenharmony_ci	ntfs_debug("Failed. Returning error code %s.", err == -EOVERFLOW ?
91062306a36Sopenharmony_ci			"EOVERFLOW" : (!err ? "EIO" : "unknown error"));
91162306a36Sopenharmony_ci	return err < 0 ? err : -EIO;
91262306a36Sopenharmony_ci
91362306a36Sopenharmony_ciread_err:
91462306a36Sopenharmony_ci	ntfs_error(vol->sb, "IO error while reading compressed data.");
91562306a36Sopenharmony_ci	/* Release the buffer heads. */
91662306a36Sopenharmony_ci	for (i = 0; i < nr_bhs; i++)
91762306a36Sopenharmony_ci		brelse(bhs[i]);
91862306a36Sopenharmony_ci	goto err_out;
91962306a36Sopenharmony_ci
92062306a36Sopenharmony_cimap_rl_err:
92162306a36Sopenharmony_ci	ntfs_error(vol->sb, "ntfs_map_runlist() failed. Cannot read "
92262306a36Sopenharmony_ci			"compression block.");
92362306a36Sopenharmony_ci	goto err_out;
92462306a36Sopenharmony_ci
92562306a36Sopenharmony_cirl_err:
92662306a36Sopenharmony_ci	up_read(&ni->runlist.lock);
92762306a36Sopenharmony_ci	ntfs_error(vol->sb, "ntfs_rl_vcn_to_lcn() failed. Cannot read "
92862306a36Sopenharmony_ci			"compression block.");
92962306a36Sopenharmony_ci	goto err_out;
93062306a36Sopenharmony_ci
93162306a36Sopenharmony_cigetblk_err:
93262306a36Sopenharmony_ci	up_read(&ni->runlist.lock);
93362306a36Sopenharmony_ci	ntfs_error(vol->sb, "getblk() failed. Cannot read compression block.");
93462306a36Sopenharmony_ci
93562306a36Sopenharmony_cierr_out:
93662306a36Sopenharmony_ci	kfree(bhs);
93762306a36Sopenharmony_ci	for (i = cur_page; i < max_page; i++) {
93862306a36Sopenharmony_ci		page = pages[i];
93962306a36Sopenharmony_ci		if (page) {
94062306a36Sopenharmony_ci			flush_dcache_page(page);
94162306a36Sopenharmony_ci			kunmap(page);
94262306a36Sopenharmony_ci			unlock_page(page);
94362306a36Sopenharmony_ci			if (i != xpage)
94462306a36Sopenharmony_ci				put_page(page);
94562306a36Sopenharmony_ci		}
94662306a36Sopenharmony_ci	}
94762306a36Sopenharmony_ci	kfree(pages);
94862306a36Sopenharmony_ci	kfree(completed_pages);
94962306a36Sopenharmony_ci	return -EIO;
95062306a36Sopenharmony_ci}
951