2 * compress.c - NTFS kernel compressed attributes handling.
3 * Part of the Linux-NTFS project.
5 * Copyright (c) 2001-2004 Anton Altaparmakov
6 * Copyright (c) 2002 Richard Russon
8 * This program/include file is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as published
10 * by the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program/include file is distributed in the hope that it will be
14 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program (in the main directory of the Linux-NTFS
20 * distribution in the file COPYING); if not, write to the Free Software
21 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/buffer_head.h>
26 #include <linux/blkdev.h>
31 * ntfs_compression_constants - enum of constants used in the compression code
34 /* Token types and access mask. */
35 NTFS_SYMBOL_TOKEN = 0,
36 NTFS_PHRASE_TOKEN = 1,
39 /* Compression sub-block constants. */
40 NTFS_SB_SIZE_MASK = 0x0fff,
41 NTFS_SB_SIZE = 0x1000,
42 NTFS_SB_IS_COMPRESSED = 0x8000,
45 * The maximum compression block size is by definition 16 * the cluster
46 * size, with the maximum supported cluster size being 4kiB. Thus the
47 * maximum compression buffer size is 64kiB, so we use this when
48 * initializing the compression buffer.
50 NTFS_MAX_CB_SIZE = 64 * 1024,
51 } ntfs_compression_constants;
54 * ntfs_compression_buffer - one buffer for the decompression engine
56 static u8 *ntfs_compression_buffer = NULL;
59 * ntfs_cb_lock - spinlock which protects ntfs_compression_buffer
61 static spinlock_t ntfs_cb_lock = SPIN_LOCK_UNLOCKED;
64 * allocate_compression_buffers - allocate the decompression buffers
66 * Caller has to hold the ntfs_lock semaphore.
68 * Return 0 on success or -ENOMEM if the allocations failed.
70 int allocate_compression_buffers(void)
72 BUG_ON(ntfs_compression_buffer);
74 ntfs_compression_buffer = vmalloc(NTFS_MAX_CB_SIZE);
75 if (!ntfs_compression_buffer)
81 * free_compression_buffers - free the decompression buffers
83 * Caller has to hold the ntfs_lock semaphore.
85 void free_compression_buffers(void)
87 BUG_ON(!ntfs_compression_buffer);
88 vfree(ntfs_compression_buffer);
89 ntfs_compression_buffer = NULL;
93 * zero_partial_compressed_page - zero out of bounds compressed page region
95 static void zero_partial_compressed_page(ntfs_inode *ni, struct page *page)
97 u8 *kp = page_address(page);
100 ntfs_debug("Zeroing page region outside initialized size.");
101 if (((s64)page->index << PAGE_CACHE_SHIFT) >= ni->initialized_size) {
103 * FIXME: Using clear_page() will become wrong when we get
104 * PAGE_CACHE_SIZE != PAGE_SIZE but for now there is no problem.
109 kp_ofs = ni->initialized_size & ~PAGE_CACHE_MASK;
110 memset(kp + kp_ofs, 0, PAGE_CACHE_SIZE - kp_ofs);
115 * handle_bounds_compressed_page - test for&handle out of bounds compressed page
117 static inline void handle_bounds_compressed_page(ntfs_inode *ni,
120 if ((page->index >= (ni->initialized_size >> PAGE_CACHE_SHIFT)) &&
121 (ni->initialized_size < VFS_I(ni)->i_size))
122 zero_partial_compressed_page(ni, page);
127 * ntfs_decompress - decompress a compression block into an array of pages
128 * @dest_pages: destination array of pages
129 * @dest_index: current index into @dest_pages (IN/OUT)
130 * @dest_ofs: current offset within @dest_pages[@dest_index] (IN/OUT)
131 * @dest_max_index: maximum index into @dest_pages (IN)
132 * @dest_max_ofs: maximum offset within @dest_pages[@dest_max_index] (IN)
133 * @xpage: the target page (-1 if none) (IN)
134 * @xpage_done: set to 1 if xpage was completed successfully (IN/OUT)
135 * @cb_start: compression block to decompress (IN)
136 * @cb_size: size of compression block @cb_start in bytes (IN)
138 * The caller must have disabled preemption. ntfs_decompress() reenables it when
139 * the critical section is finished.
141 * This decompresses the compression block @cb_start into the array of
142 * destination pages @dest_pages starting at index @dest_index into @dest_pages
143 * and at offset @dest_pos into the page @dest_pages[@dest_index].
145 * When the page @dest_pages[@xpage] is completed, @xpage_done is set to 1.
146 * If xpage is -1 or @xpage has not been completed, @xpage_done is not modified.
148 * @cb_start is a pointer to the compression block which needs decompressing
149 * and @cb_size is the size of @cb_start in bytes (8-64kiB).
151 * Return 0 if success or -EOVERFLOW on error in the compressed stream.
152 * @xpage_done indicates whether the target page (@dest_pages[@xpage]) was
153 * completed during the decompression of the compression block (@cb_start).
155 * Warning: This function *REQUIRES* PAGE_CACHE_SIZE >= 4096 or it will blow up
156 * unpredicatbly! You have been warned!
158 * Note to hackers: This function may not sleep until it has finished accessing
159 * the compression block @cb_start as it is a per-CPU buffer.
161 static int ntfs_decompress(struct page *dest_pages[], int *dest_index,
162 int *dest_ofs, const int dest_max_index, const int dest_max_ofs,
163 const int xpage, char *xpage_done, u8 *const cb_start,
167 * Pointers into the compressed data, i.e. the compression block (cb),
168 * and the therein contained sub-blocks (sb).
170 u8 *cb_end = cb_start + cb_size; /* End of cb. */
171 u8 *cb = cb_start; /* Current position in cb. */
172 u8 *cb_sb_start = cb; /* Beginning of the current sb in the cb. */
173 u8 *cb_sb_end; /* End of current sb / beginning of next sb. */
175 /* Variables for uncompressed data / destination. */
176 struct page *dp; /* Current destination page being worked on. */
177 u8 *dp_addr; /* Current pointer into dp. */
178 u8 *dp_sb_start; /* Start of current sub-block in dp. */
179 u8 *dp_sb_end; /* End of current sb in dp (dp_sb_start +
181 u16 do_sb_start; /* @dest_ofs when starting this sub-block. */
182 u16 do_sb_end; /* @dest_ofs of end of this sb (do_sb_start +
185 /* Variables for tag and token parsing. */
186 u8 tag; /* Current tag. */
187 int token; /* Loop counter for the eight tokens in tag. */
189 /* Need this because we can't sleep, so need two stages. */
190 int completed_pages[dest_max_index - *dest_index + 1];
191 int nr_completed_pages = 0;
193 /* Default error code. */
194 int err = -EOVERFLOW;
196 ntfs_debug("Entering, cb_size = 0x%x.", cb_size);
198 ntfs_debug("Beginning sub-block at offset = 0x%x in the cb.",
201 * Have we reached the end of the compression block or the end of the
202 * decompressed data? The latter can happen for example if the current
203 * position in the compression block is one byte before its end so the
204 * first two checks do not detect it.
206 if (cb == cb_end || !le16_to_cpup((u16*)cb) ||
207 (*dest_index == dest_max_index &&
208 *dest_ofs == dest_max_ofs)) {
211 ntfs_debug("Completed. Returning success (0).");
214 /* We can sleep from now on, so we drop lock. */
215 spin_unlock(&ntfs_cb_lock);
216 /* Second stage: finalize completed pages. */
217 if (nr_completed_pages > 0) {
218 struct page *page = dest_pages[completed_pages[0]];
219 ntfs_inode *ni = NTFS_I(page->mapping->host);
221 for (i = 0; i < nr_completed_pages; i++) {
222 int di = completed_pages[i];
226 * If we are outside the initialized size, zero
227 * the out of bounds page range.
229 handle_bounds_compressed_page(ni, dp);
230 flush_dcache_page(dp);
237 page_cache_release(dp);
238 dest_pages[di] = NULL;
244 /* Setup offsets for the current sub-block destination. */
245 do_sb_start = *dest_ofs;
246 do_sb_end = do_sb_start + NTFS_SB_SIZE;
248 /* Check that we are still within allowed boundaries. */
249 if (*dest_index == dest_max_index && do_sb_end > dest_max_ofs)
250 goto return_overflow;
252 /* Does the minimum size of a compressed sb overflow valid range? */
254 goto return_overflow;
256 /* Setup the current sub-block source pointers and validate range. */
258 cb_sb_end = cb_sb_start + (le16_to_cpup((u16*)cb) & NTFS_SB_SIZE_MASK)
260 if (cb_sb_end > cb_end)
261 goto return_overflow;
263 /* Get the current destination page. */
264 dp = dest_pages[*dest_index];
266 /* No page present. Skip decompression of this sub-block. */
269 /* Advance destination position to next sub-block. */
270 *dest_ofs = (*dest_ofs + NTFS_SB_SIZE) & ~PAGE_CACHE_MASK;
271 if (!*dest_ofs && (++*dest_index > dest_max_index))
272 goto return_overflow;
276 /* We have a valid destination page. Setup the destination pointers. */
277 dp_addr = (u8*)page_address(dp) + do_sb_start;
279 /* Now, we are ready to process the current sub-block (sb). */
280 if (!(le16_to_cpup((u16*)cb) & NTFS_SB_IS_COMPRESSED)) {
281 ntfs_debug("Found uncompressed sub-block.");
282 /* This sb is not compressed, just copy it into destination. */
284 /* Advance source position to first data byte. */
287 /* An uncompressed sb must be full size. */
288 if (cb_sb_end - cb != NTFS_SB_SIZE)
289 goto return_overflow;
291 /* Copy the block and advance the source position. */
292 memcpy(dp_addr, cb, NTFS_SB_SIZE);
295 /* Advance destination position to next sub-block. */
296 *dest_ofs += NTFS_SB_SIZE;
297 if (!(*dest_ofs &= ~PAGE_CACHE_MASK)) {
300 * First stage: add current page index to array of
303 completed_pages[nr_completed_pages++] = *dest_index;
304 if (++*dest_index > dest_max_index)
305 goto return_overflow;
309 ntfs_debug("Found compressed sub-block.");
310 /* This sb is compressed, decompress it into destination. */
312 /* Setup destination pointers. */
313 dp_sb_start = dp_addr;
314 dp_sb_end = dp_sb_start + NTFS_SB_SIZE;
316 /* Forward to the first tag in the sub-block. */
319 if (cb == cb_sb_end) {
320 /* Check if the decompressed sub-block was not full-length. */
321 if (dp_addr < dp_sb_end) {
322 int nr_bytes = do_sb_end - *dest_ofs;
324 ntfs_debug("Filling incomplete sub-block with "
326 /* Zero remainder and update destination position. */
327 memset(dp_addr, 0, nr_bytes);
328 *dest_ofs += nr_bytes;
330 /* We have finished the current sub-block. */
331 if (!(*dest_ofs &= ~PAGE_CACHE_MASK))
336 /* Check we are still in range. */
337 if (cb > cb_sb_end || dp_addr > dp_sb_end)
338 goto return_overflow;
340 /* Get the next tag and advance to first token. */
343 /* Parse the eight tokens described by the tag. */
344 for (token = 0; token < 8; token++, tag >>= 1) {
345 u16 lg, pt, length, max_non_overlap;
349 /* Check if we are done / still in range. */
350 if (cb >= cb_sb_end || dp_addr > dp_sb_end)
353 /* Determine token type and parse appropriately.*/
354 if ((tag & NTFS_TOKEN_MASK) == NTFS_SYMBOL_TOKEN) {
356 * We have a symbol token, copy the symbol across, and
357 * advance the source and destination positions.
362 /* Continue with the next token. */
367 * We have a phrase token. Make sure it is not the first tag in
368 * the sb as this is illegal and would confuse the code below.
370 if (dp_addr == dp_sb_start)
371 goto return_overflow;
374 * Determine the number of bytes to go back (p) and the number
375 * of bytes to copy (l). We use an optimized algorithm in which
376 * we first calculate log2(current destination position in sb),
377 * which allows determination of l and p in O(1) rather than
378 * O(n). We just need an arch-optimized log2() function now.
381 for (i = *dest_ofs - do_sb_start - 1; i >= 0x10; i >>= 1)
384 /* Get the phrase token into i. */
385 pt = le16_to_cpup((u16*)cb);
388 * Calculate starting position of the byte sequence in
389 * the destination using the fact that p = (pt >> (12 - lg)) + 1
390 * and make sure we don't go too far back.
392 dp_back_addr = dp_addr - (pt >> (12 - lg)) - 1;
393 if (dp_back_addr < dp_sb_start)
394 goto return_overflow;
396 /* Now calculate the length of the byte sequence. */
397 length = (pt & (0xfff >> lg)) + 3;
399 /* Advance destination position and verify it is in range. */
401 if (*dest_ofs > do_sb_end)
402 goto return_overflow;
404 /* The number of non-overlapping bytes. */
405 max_non_overlap = dp_addr - dp_back_addr;
407 if (length <= max_non_overlap) {
408 /* The byte sequence doesn't overlap, just copy it. */
409 memcpy(dp_addr, dp_back_addr, length);
411 /* Advance destination pointer. */
415 * The byte sequence does overlap, copy non-overlapping
416 * part and then do a slow byte by byte copy for the
417 * overlapping part. Also, advance the destination
420 memcpy(dp_addr, dp_back_addr, max_non_overlap);
421 dp_addr += max_non_overlap;
422 dp_back_addr += max_non_overlap;
423 length -= max_non_overlap;
425 *dp_addr++ = *dp_back_addr++;
428 /* Advance source position and continue with the next token. */
432 /* No tokens left in the current tag. Continue with the next tag. */
436 ntfs_error(NULL, "Failed. Returning -EOVERFLOW.");
441 * ntfs_read_compressed_block - read a compressed block into the page cache
442 * @page: locked page in the compression block(s) we need to read
444 * When we are called the page has already been verified to be locked and the
445 * attribute is known to be non-resident, not encrypted, but compressed.
447 * 1. Determine which compression block(s) @page is in.
448 * 2. Get hold of all pages corresponding to this/these compression block(s).
449 * 3. Read the (first) compression block.
450 * 4. Decompress it into the corresponding pages.
451 * 5. Throw the compressed data away and proceed to 3. for the next compression
452 * block or return success if no more compression blocks left.
454 * Warning: We have to be careful what we do about existing pages. They might
455 * have been written to so that we would lose data if we were to just overwrite
456 * them with the out-of-date uncompressed data.
458 * FIXME: For PAGE_CACHE_SIZE > cb_size we are not doing the Right Thing(TM) at
459 * the end of the file I think. We need to detect this case and zero the out
460 * of bounds remainder of the page in question and mark it as handled. At the
461 * moment we would just return -EIO on such a page. This bug will only become
462 * apparent if pages are above 8kiB and the NTFS volume only uses 512 byte
463 * clusters so is probably not going to be seen by anyone. Still this should
466 * FIXME: Again for PAGE_CACHE_SIZE > cb_size we are screwing up both in
467 * handling sparse and compressed cbs. (AIA)
469 * FIXME: At the moment we don't do any zeroing out in the case that
470 * initialized_size is less than data_size. This should be safe because of the
471 * nature of the compression algorithm used. Just in case we check and output
472 * an error message in read inode if the two sizes are not equal for a
473 * compressed file. (AIA)
475 int ntfs_read_compressed_block(struct page *page)
477 struct address_space *mapping = page->mapping;
478 ntfs_inode *ni = NTFS_I(mapping->host);
479 ntfs_volume *vol = ni->vol;
480 struct super_block *sb = vol->sb;
481 run_list_element *rl;
482 unsigned long block_size = sb->s_blocksize;
483 unsigned char block_size_bits = sb->s_blocksize_bits;
484 u8 *cb, *cb_pos, *cb_end;
485 struct buffer_head **bhs;
486 unsigned long offset, index = page->index;
487 u32 cb_size = ni->itype.compressed.block_size;
488 u64 cb_size_mask = cb_size - 1UL;
491 /* The first wanted vcn (minimum alignment is PAGE_CACHE_SIZE). */
492 VCN start_vcn = (((s64)index << PAGE_CACHE_SHIFT) & ~cb_size_mask) >>
493 vol->cluster_size_bits;
495 * The first vcn after the last wanted vcn (minumum alignment is again
498 VCN end_vcn = ((((s64)(index + 1UL) << PAGE_CACHE_SHIFT) + cb_size - 1)
499 & ~cb_size_mask) >> vol->cluster_size_bits;
500 /* Number of compression blocks (cbs) in the wanted vcn range. */
501 unsigned int nr_cbs = (end_vcn - start_vcn) << vol->cluster_size_bits
502 >> ni->itype.compressed.block_size_bits;
504 * Number of pages required to store the uncompressed data from all
505 * compression blocks (cbs) overlapping @page. Due to alignment
506 * guarantees of start_vcn and end_vcn, no need to round up here.
508 unsigned int nr_pages = (end_vcn - start_vcn) <<
509 vol->cluster_size_bits >> PAGE_CACHE_SHIFT;
510 unsigned int xpage, max_page, cur_page, cur_ofs, i;
511 unsigned int cb_clusters, cb_max_ofs;
512 int block, max_block, cb_max_page, bhs_size, nr_bhs, err = 0;
514 unsigned char xpage_done = 0;
516 ntfs_debug("Entering, page->index = 0x%lx, cb_size = 0x%x, nr_pages = "
517 "%i.", index, cb_size, nr_pages);
519 * Bad things happen if we get here for anything that is not an
520 * unnamed $DATA attribute.
522 BUG_ON(ni->type != AT_DATA);
523 BUG_ON(ni->name_len);
525 pages = kmalloc(nr_pages * sizeof(struct page *), GFP_NOFS);
527 /* Allocate memory to store the buffer heads we need. */
528 bhs_size = cb_size / block_size * sizeof(struct buffer_head *);
529 bhs = kmalloc(bhs_size, GFP_NOFS);
531 if (unlikely(!pages || !bhs)) {
536 ntfs_error(vol->sb, "Failed to allocate internal buffers.");
541 * We have already been given one page, this is the one we must do.
542 * Once again, the alignment guarantees keep it simple.
544 offset = start_vcn << vol->cluster_size_bits >> PAGE_CACHE_SHIFT;
545 xpage = index - offset;
548 * The remaining pages need to be allocated and inserted into the page
549 * cache, alignment guarantees keep all the below much simpler. (-8
551 max_page = ((VFS_I(ni)->i_size + PAGE_CACHE_SIZE - 1) >>
552 PAGE_CACHE_SHIFT) - offset;
553 if (nr_pages < max_page)
555 for (i = 0; i < max_page; i++, offset++) {
557 pages[i] = grab_cache_page_nowait(mapping, offset);
561 * We only (re)read the page if it isn't already read
562 * in and/or dirty or we would be losing data or at
563 * least wasting our time.
565 if (!PageDirty(page) && (!PageUptodate(page) ||
567 ClearPageError(page);
572 page_cache_release(page);
578 * We have the run list, and all the destination pages we need to fill.
579 * Now read the first compression block.
583 cb_clusters = ni->itype.compressed.block_clusters;
588 /* Read all cb buffer heads one cluster at a time. */
590 for (vcn = start_vcn, start_vcn += cb_clusters; vcn < start_vcn;
592 BOOL is_retry = FALSE;
596 down_read(&ni->run_list.lock);
597 rl = ni->run_list.rl;
599 if (likely(rl != NULL)) {
600 /* Seek to element containing target vcn. */
601 while (rl->length && rl[1].vcn <= vcn)
603 lcn = vcn_to_lcn(rl, vcn);
605 lcn = (LCN)LCN_RL_NOT_MAPPED;
606 ntfs_debug("Reading vcn = 0x%llx, lcn = 0x%llx.",
607 (unsigned long long)vcn,
608 (unsigned long long)lcn);
611 * When we reach the first sparse cluster we have
612 * finished with the cb.
616 if (is_retry || lcn != LCN_RL_NOT_MAPPED)
620 * Attempt to map run list, dropping lock for the
623 up_read(&ni->run_list.lock);
624 if (!map_run_list(ni, vcn))
625 goto lock_retry_remap;
628 block = lcn << vol->cluster_size_bits >> block_size_bits;
629 /* Read the lcn from device in chunks of block_size bytes. */
630 max_block = block + (vol->cluster_size >> block_size_bits);
632 ntfs_debug("block = 0x%x.", block);
633 if (unlikely(!(bhs[nr_bhs] = sb_getblk(sb, block))))
636 } while (++block < max_block);
639 /* Release the lock if we took it. */
641 up_read(&ni->run_list.lock);
643 /* Setup and initiate io on all buffer heads. */
644 for (i = 0; i < nr_bhs; i++) {
645 struct buffer_head *tbh = bhs[i];
647 if (unlikely(test_set_buffer_locked(tbh)))
649 if (unlikely(buffer_uptodate(tbh))) {
654 tbh->b_end_io = end_buffer_read_sync;
655 submit_bh(READ, tbh);
658 /* Wait for io completion on all buffer heads. */
659 for (i = 0; i < nr_bhs; i++) {
660 struct buffer_head *tbh = bhs[i];
662 if (buffer_uptodate(tbh))
666 * We need an optimization barrier here, otherwise we start
667 * hitting the below fixup code when accessing a loopback
668 * mounted ntfs partition. This indicates either there is a
669 * race condition in the loop driver or, more likely, gcc
670 * overoptimises the code without the barrier and it doesn't
671 * do the Right Thing(TM).
674 if (unlikely(!buffer_uptodate(tbh))) {
675 ntfs_warning(vol->sb, "Buffer is unlocked but not "
676 "uptodate! Unplugging the disk queue "
677 "and rescheduling.");
679 blk_run_address_space(mapping);
682 if (unlikely(!buffer_uptodate(tbh)))
684 ntfs_warning(vol->sb, "Buffer is now uptodate. Good.");
689 * Get the compression buffer. We must not sleep any more
690 * until we are finished with it.
692 spin_lock(&ntfs_cb_lock);
693 cb = ntfs_compression_buffer;
698 cb_end = cb + cb_size;
700 /* Copy the buffer heads into the contiguous buffer. */
701 for (i = 0; i < nr_bhs; i++) {
702 memcpy(cb_pos, bhs[i]->b_data, block_size);
703 cb_pos += block_size;
706 /* Just a precaution. */
707 if (cb_pos + 2 <= cb + cb_size)
710 /* Reset cb_pos back to the beginning. */
713 /* We now have both source (if present) and destination. */
714 ntfs_debug("Successfully read the compression block.");
716 /* The last page and maximum offset within it for the current cb. */
717 cb_max_page = (cur_page << PAGE_CACHE_SHIFT) + cur_ofs + cb_size;
718 cb_max_ofs = cb_max_page & ~PAGE_CACHE_MASK;
719 cb_max_page >>= PAGE_CACHE_SHIFT;
721 /* Catch end of file inside a compression block. */
722 if (cb_max_page > max_page)
723 cb_max_page = max_page;
725 if (vcn == start_vcn - cb_clusters) {
726 /* Sparse cb, zero out page range overlapping the cb. */
727 ntfs_debug("Found sparse compression block.");
728 /* We can sleep from now on, so we drop lock. */
729 spin_unlock(&ntfs_cb_lock);
732 for (; cur_page < cb_max_page; cur_page++) {
733 page = pages[cur_page];
736 * FIXME: Using clear_page() will become wrong
737 * when we get PAGE_CACHE_SIZE != PAGE_SIZE but
738 * for now there is no problem.
740 if (likely(!cur_ofs))
741 clear_page(page_address(page));
743 memset(page_address(page) + cur_ofs, 0,
746 flush_dcache_page(page);
748 SetPageUptodate(page);
750 if (cur_page == xpage)
753 page_cache_release(page);
754 pages[cur_page] = NULL;
756 cb_pos += PAGE_CACHE_SIZE - cur_ofs;
758 if (cb_pos >= cb_end)
761 /* If we have a partial final page, deal with it now. */
762 if (cb_max_ofs && cb_pos < cb_end) {
763 page = pages[cur_page];
765 memset(page_address(page) + cur_ofs, 0,
766 cb_max_ofs - cur_ofs);
768 * No need to update cb_pos at this stage:
769 * cb_pos += cb_max_ofs - cur_ofs;
771 cur_ofs = cb_max_ofs;
773 } else if (vcn == start_vcn) {
774 /* We can't sleep so we need two stages. */
775 unsigned int cur2_page = cur_page;
776 unsigned int cur_ofs2 = cur_ofs;
777 u8 *cb_pos2 = cb_pos;
779 ntfs_debug("Found uncompressed compression block.");
780 /* Uncompressed cb, copy it to the destination pages. */
782 * TODO: As a big optimization, we could detect this case
783 * before we read all the pages and use block_read_full_page()
784 * on all full pages instead (we still have to treat partial
785 * pages especially but at least we are getting rid of the
786 * synchronous io for the majority of pages.
787 * Or if we choose not to do the read-ahead/-behind stuff, we
788 * could just return block_read_full_page(pages[xpage]) as long
789 * as PAGE_CACHE_SIZE <= cb_size.
793 /* First stage: copy data into destination pages. */
794 for (; cur_page < cb_max_page; cur_page++) {
795 page = pages[cur_page];
797 memcpy(page_address(page) + cur_ofs, cb_pos,
798 PAGE_CACHE_SIZE - cur_ofs);
799 cb_pos += PAGE_CACHE_SIZE - cur_ofs;
801 if (cb_pos >= cb_end)
804 /* If we have a partial final page, deal with it now. */
805 if (cb_max_ofs && cb_pos < cb_end) {
806 page = pages[cur_page];
808 memcpy(page_address(page) + cur_ofs, cb_pos,
809 cb_max_ofs - cur_ofs);
810 cb_pos += cb_max_ofs - cur_ofs;
811 cur_ofs = cb_max_ofs;
813 /* We can sleep from now on, so drop lock. */
814 spin_unlock(&ntfs_cb_lock);
815 /* Second stage: finalize pages. */
816 for (; cur2_page < cb_max_page; cur2_page++) {
817 page = pages[cur2_page];
820 * If we are outside the initialized size, zero
821 * the out of bounds page range.
823 handle_bounds_compressed_page(ni, page);
824 flush_dcache_page(page);
826 SetPageUptodate(page);
828 if (cur2_page == xpage)
831 page_cache_release(page);
832 pages[cur2_page] = NULL;
834 cb_pos2 += PAGE_CACHE_SIZE - cur_ofs2;
836 if (cb_pos2 >= cb_end)
840 /* Compressed cb, decompress it into the destination page(s). */
841 unsigned int prev_cur_page = cur_page;
843 ntfs_debug("Found compressed compression block.");
844 err = ntfs_decompress(pages, &cur_page, &cur_ofs,
845 cb_max_page, cb_max_ofs, xpage, &xpage_done,
846 cb_pos, cb_size - (cb_pos - cb));
848 * We can sleep from now on, lock already dropped by
852 ntfs_error(vol->sb, "ntfs_decompress() failed in inode "
853 "0x%lx with error code %i. Skipping "
854 "this compression block.",
856 /* Release the unfinished pages. */
857 for (; prev_cur_page < cur_page; prev_cur_page++) {
858 page = pages[prev_cur_page];
860 if (prev_cur_page == xpage &&
863 flush_dcache_page(page);
866 if (prev_cur_page != xpage)
867 page_cache_release(page);
868 pages[prev_cur_page] = NULL;
874 /* Release the buffer heads. */
875 for (i = 0; i < nr_bhs; i++)
878 /* Do we have more work to do? */
882 /* We no longer need the list of buffer heads. */
885 /* Clean up if we have any pages left. Should never happen. */
886 for (cur_page = 0; cur_page < max_page; cur_page++) {
887 page = pages[cur_page];
889 ntfs_error(vol->sb, "Still have pages left! "
890 "Terminating them with extreme "
891 "prejudice. Inode 0x%lx, page index "
892 "0x%lx.", ni->mft_no, page->index);
893 if (cur_page == xpage && !xpage_done)
895 flush_dcache_page(page);
898 if (cur_page != xpage)
899 page_cache_release(page);
900 pages[cur_page] = NULL;
904 /* We no longer need the list of pages. */
907 /* If we have completed the requested page, we return success. */
908 if (likely(xpage_done))
911 ntfs_debug("Failed. Returning error code %s.", err == -EOVERFLOW ?
912 "EOVERFLOW" : (!err ? "EIO" : "unkown error"));
913 return err < 0 ? err : -EIO;
916 ntfs_error(vol->sb, "IO error while reading compressed data.");
917 /* Release the buffer heads. */
918 for (i = 0; i < nr_bhs; i++)
923 ntfs_error(vol->sb, "map_run_list() failed. Cannot read compression "
928 up_read(&ni->run_list.lock);
929 ntfs_error(vol->sb, "vcn_to_lcn() failed. Cannot read compression "
934 up_read(&ni->run_list.lock);
935 ntfs_error(vol->sb, "getblk() failed. Cannot read compression block.");
939 for (i = cur_page; i < max_page; i++) {
942 if (i == xpage && !xpage_done)
944 flush_dcache_page(page);
948 page_cache_release(page);