1/** 2 * compress.c - NTFS kernel compressed attributes handling. 3 * Part of the Linux-NTFS project. 4 * 5 * Copyright (c) 2001-2004 Anton Altaparmakov 6 * Copyright (c) 2002 Richard Russon 7 * 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. 12 * 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. 17 * 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 22 */ 23 24#include <linux/fs.h> 25#include <linux/buffer_head.h> 26#include <linux/blkdev.h> 27#include <linux/vmalloc.h> 28 29#include "attrib.h" 30#include "inode.h" 31#include "debug.h" 32#include "ntfs.h" 33 34/** 35 * ntfs_compression_constants - enum of constants used in the compression code 36 */ 37typedef enum { 38 /* Token types and access mask. */ 39 NTFS_SYMBOL_TOKEN = 0, 40 NTFS_PHRASE_TOKEN = 1, 41 NTFS_TOKEN_MASK = 1, 42 43 /* Compression sub-block constants. */ 44 NTFS_SB_SIZE_MASK = 0x0fff, 45 NTFS_SB_SIZE = 0x1000, 46 NTFS_SB_IS_COMPRESSED = 0x8000, 47 48 /* 49 * The maximum compression block size is by definition 16 * the cluster 50 * size, with the maximum supported cluster size being 4kiB. Thus the 51 * maximum compression buffer size is 64kiB, so we use this when 52 * initializing the compression buffer. 53 */ 54 NTFS_MAX_CB_SIZE = 64 * 1024, 55} ntfs_compression_constants; 56 57/** 58 * ntfs_compression_buffer - one buffer for the decompression engine 59 */ 60static u8 *ntfs_compression_buffer = NULL; 61 62/** 63 * ntfs_cb_lock - spinlock which protects ntfs_compression_buffer 64 */ 65static DEFINE_SPINLOCK(ntfs_cb_lock); 66 67/** 68 * allocate_compression_buffers - allocate the decompression buffers 69 * 70 * Caller has to hold the ntfs_lock mutex. 71 * 72 * Return 0 on success or -ENOMEM if the allocations failed. 73 */ 74int allocate_compression_buffers(void) 75{ 76 BUG_ON(ntfs_compression_buffer); 77 78 ntfs_compression_buffer = vmalloc(NTFS_MAX_CB_SIZE); 79 if (!ntfs_compression_buffer) 80 return -ENOMEM; 81 return 0; 82} 83 84/** 85 * free_compression_buffers - free the decompression buffers 86 * 87 * Caller has to hold the ntfs_lock mutex. 88 */ 89void free_compression_buffers(void) 90{ 91 BUG_ON(!ntfs_compression_buffer); 92 vfree(ntfs_compression_buffer); 93 ntfs_compression_buffer = NULL; 94} 95 96/** 97 * zero_partial_compressed_page - zero out of bounds compressed page region 98 */ 99static void zero_partial_compressed_page(struct page *page, 100 const s64 initialized_size) 101{ 102 u8 *kp = page_address(page); 103 unsigned int kp_ofs; 104 105 ntfs_debug("Zeroing page region outside initialized size."); 106 if (((s64)page->index << PAGE_CACHE_SHIFT) >= initialized_size) { 107 clear_page(kp); 108 return; 109 } 110 kp_ofs = initialized_size & ~PAGE_CACHE_MASK; 111 memset(kp + kp_ofs, 0, PAGE_CACHE_SIZE - kp_ofs); 112 return; 113} 114 115/** 116 * handle_bounds_compressed_page - test for&handle out of bounds compressed page 117 */ 118static inline void handle_bounds_compressed_page(struct page *page, 119 const loff_t i_size, const s64 initialized_size) 120{ 121 if ((page->index >= (initialized_size >> PAGE_CACHE_SHIFT)) && 122 (initialized_size < i_size)) 123 zero_partial_compressed_page(page, initialized_size); 124 return; 125} 126 127/** 128 * ntfs_decompress - decompress a compression block into an array of pages 129 * @dest_pages: destination array of pages 130 * @dest_index: current index into @dest_pages (IN/OUT) 131 * @dest_ofs: current offset within @dest_pages[@dest_index] (IN/OUT) 132 * @dest_max_index: maximum index into @dest_pages (IN) 133 * @dest_max_ofs: maximum offset within @dest_pages[@dest_max_index] (IN) 134 * @xpage: the target page (-1 if none) (IN) 135 * @xpage_done: set to 1 if xpage was completed successfully (IN/OUT) 136 * @cb_start: compression block to decompress (IN) 137 * @cb_size: size of compression block @cb_start in bytes (IN) 138 * @i_size: file size when we started the read (IN) 139 * @initialized_size: initialized file size when we started the read (IN) 140 * 141 * The caller must have disabled preemption. ntfs_decompress() reenables it when 142 * the critical section is finished. 143 * 144 * This decompresses the compression block @cb_start into the array of 145 * destination pages @dest_pages starting at index @dest_index into @dest_pages 146 * and at offset @dest_pos into the page @dest_pages[@dest_index]. 147 * 148 * When the page @dest_pages[@xpage] is completed, @xpage_done is set to 1. 149 * If xpage is -1 or @xpage has not been completed, @xpage_done is not modified. 150 * 151 * @cb_start is a pointer to the compression block which needs decompressing 152 * and @cb_size is the size of @cb_start in bytes (8-64kiB). 153 * 154 * Return 0 if success or -EOVERFLOW on error in the compressed stream. 155 * @xpage_done indicates whether the target page (@dest_pages[@xpage]) was 156 * completed during the decompression of the compression block (@cb_start). 157 * 158 * Warning: This function *REQUIRES* PAGE_CACHE_SIZE >= 4096 or it will blow up 159 * unpredicatbly! You have been warned! 160 * 161 * Note to hackers: This function may not sleep until it has finished accessing 162 * the compression block @cb_start as it is a per-CPU buffer. 163 */ 164static int ntfs_decompress(struct page *dest_pages[], int *dest_index, 165 int *dest_ofs, const int dest_max_index, const int dest_max_ofs, 166 const int xpage, char *xpage_done, u8 *const cb_start, 167 const u32 cb_size, const loff_t i_size, 168 const s64 initialized_size) 169{ 170 /* 171 * Pointers into the compressed data, i.e. the compression block (cb), 172 * and the therein contained sub-blocks (sb). 173 */ 174 u8 *cb_end = cb_start + cb_size; /* End of cb. */ 175 u8 *cb = cb_start; /* Current position in cb. */ 176 u8 *cb_sb_start = cb; /* Beginning of the current sb in the cb. */ 177 u8 *cb_sb_end; /* End of current sb / beginning of next sb. */ 178 179 /* Variables for uncompressed data / destination. */ 180 struct page *dp; /* Current destination page being worked on. */ 181 u8 *dp_addr; /* Current pointer into dp. */ 182 u8 *dp_sb_start; /* Start of current sub-block in dp. */ 183 u8 *dp_sb_end; /* End of current sb in dp (dp_sb_start + 184 NTFS_SB_SIZE). */ 185 u16 do_sb_start; /* @dest_ofs when starting this sub-block. */ 186 u16 do_sb_end; /* @dest_ofs of end of this sb (do_sb_start + 187 NTFS_SB_SIZE). */ 188 189 /* Variables for tag and token parsing. */ 190 u8 tag; /* Current tag. */ 191 int token; /* Loop counter for the eight tokens in tag. */ 192 193 /* Need this because we can't sleep, so need two stages. */ 194 int completed_pages[dest_max_index - *dest_index + 1]; 195 int nr_completed_pages = 0; 196 197 /* Default error code. */ 198 int err = -EOVERFLOW; 199 200 ntfs_debug("Entering, cb_size = 0x%x.", cb_size); 201do_next_sb: 202 ntfs_debug("Beginning sub-block at offset = 0x%zx in the cb.", 203 cb - cb_start); 204 /* 205 * Have we reached the end of the compression block or the end of the 206 * decompressed data? The latter can happen for example if the current 207 * position in the compression block is one byte before its end so the 208 * first two checks do not detect it. 209 */ 210 if (cb == cb_end || !le16_to_cpup((le16*)cb) || 211 (*dest_index == dest_max_index && 212 *dest_ofs == dest_max_ofs)) { 213 int i; 214 215 ntfs_debug("Completed. Returning success (0)."); 216 err = 0; 217return_error: 218 /* We can sleep from now on, so we drop lock. */ 219 spin_unlock(&ntfs_cb_lock); 220 /* Second stage: finalize completed pages. */ 221 if (nr_completed_pages > 0) { 222 for (i = 0; i < nr_completed_pages; i++) { 223 int di = completed_pages[i]; 224 225 dp = dest_pages[di]; 226 /* 227 * If we are outside the initialized size, zero 228 * the out of bounds page range. 229 */ 230 handle_bounds_compressed_page(dp, i_size, 231 initialized_size); 232 flush_dcache_page(dp); 233 kunmap(dp); 234 SetPageUptodate(dp); 235 unlock_page(dp); 236 if (di == xpage) 237 *xpage_done = 1; 238 else 239 page_cache_release(dp); 240 dest_pages[di] = NULL; 241 } 242 } 243 return err; 244 } 245 246 /* Setup offsets for the current sub-block destination. */ 247 do_sb_start = *dest_ofs; 248 do_sb_end = do_sb_start + NTFS_SB_SIZE; 249 250 /* Check that we are still within allowed boundaries. */ 251 if (*dest_index == dest_max_index && do_sb_end > dest_max_ofs) 252 goto return_overflow; 253 254 /* Does the minimum size of a compressed sb overflow valid range? */ 255 if (cb + 6 > cb_end) 256 goto return_overflow; 257 258 /* Setup the current sub-block source pointers and validate range. */ 259 cb_sb_start = cb; 260 cb_sb_end = cb_sb_start + (le16_to_cpup((le16*)cb) & NTFS_SB_SIZE_MASK) 261 + 3; 262 if (cb_sb_end > cb_end) 263 goto return_overflow; 264 265 /* Get the current destination page. */ 266 dp = dest_pages[*dest_index]; 267 if (!dp) { 268 /* No page present. Skip decompression of this sub-block. */ 269 cb = cb_sb_end; 270 271 /* Advance destination position to next sub-block. */ 272 *dest_ofs = (*dest_ofs + NTFS_SB_SIZE) & ~PAGE_CACHE_MASK; 273 if (!*dest_ofs && (++*dest_index > dest_max_index)) 274 goto return_overflow; 275 goto do_next_sb; 276 } 277 278 /* We have a valid destination page. Setup the destination pointers. */ 279 dp_addr = (u8*)page_address(dp) + do_sb_start; 280 281 /* Now, we are ready to process the current sub-block (sb). */ 282 if (!(le16_to_cpup((le16*)cb) & NTFS_SB_IS_COMPRESSED)) { 283 ntfs_debug("Found uncompressed sub-block."); 284 /* This sb is not compressed, just copy it into destination. */ 285 286 /* Advance source position to first data byte. */ 287 cb += 2; 288 289 /* An uncompressed sb must be full size. */ 290 if (cb_sb_end - cb != NTFS_SB_SIZE) 291 goto return_overflow; 292 293 /* Copy the block and advance the source position. */ 294 memcpy(dp_addr, cb, NTFS_SB_SIZE); 295 cb += NTFS_SB_SIZE; 296 297 /* Advance destination position to next sub-block. */ 298 *dest_ofs += NTFS_SB_SIZE; 299 if (!(*dest_ofs &= ~PAGE_CACHE_MASK)) { 300finalize_page: 301 /* 302 * First stage: add current page index to array of 303 * completed pages. 304 */ 305 completed_pages[nr_completed_pages++] = *dest_index; 306 if (++*dest_index > dest_max_index) 307 goto return_overflow; 308 } 309 goto do_next_sb; 310 } 311 ntfs_debug("Found compressed sub-block."); 312 /* This sb is compressed, decompress it into destination. */ 313 314 /* Setup destination pointers. */ 315 dp_sb_start = dp_addr; 316 dp_sb_end = dp_sb_start + NTFS_SB_SIZE; 317 318 /* Forward to the first tag in the sub-block. */ 319 cb += 2; 320do_next_tag: 321 if (cb == cb_sb_end) { 322 /* Check if the decompressed sub-block was not full-length. */ 323 if (dp_addr < dp_sb_end) { 324 int nr_bytes = do_sb_end - *dest_ofs; 325 326 ntfs_debug("Filling incomplete sub-block with " 327 "zeroes."); 328 /* Zero remainder and update destination position. */ 329 memset(dp_addr, 0, nr_bytes); 330 *dest_ofs += nr_bytes; 331 } 332 /* We have finished the current sub-block. */ 333 if (!(*dest_ofs &= ~PAGE_CACHE_MASK)) 334 goto finalize_page; 335 goto do_next_sb; 336 } 337 338 /* Check we are still in range. */ 339 if (cb > cb_sb_end || dp_addr > dp_sb_end) 340 goto return_overflow; 341 342 /* Get the next tag and advance to first token. */ 343 tag = *cb++; 344 345 /* Parse the eight tokens described by the tag. */ 346 for (token = 0; token < 8; token++, tag >>= 1) { 347 u16 lg, pt, length, max_non_overlap; 348 register u16 i; 349 u8 *dp_back_addr; 350 351 /* Check if we are done / still in range. */ 352 if (cb >= cb_sb_end || dp_addr > dp_sb_end) 353 break; 354 355 /* Determine token type and parse appropriately.*/ 356 if ((tag & NTFS_TOKEN_MASK) == NTFS_SYMBOL_TOKEN) { 357 /* 358 * We have a symbol token, copy the symbol across, and 359 * advance the source and destination positions. 360 */ 361 *dp_addr++ = *cb++; 362 ++*dest_ofs; 363 364 /* Continue with the next token. */ 365 continue; 366 } 367 368 /* 369 * We have a phrase token. Make sure it is not the first tag in 370 * the sb as this is illegal and would confuse the code below. 371 */ 372 if (dp_addr == dp_sb_start) 373 goto return_overflow; 374 375 /* 376 * Determine the number of bytes to go back (p) and the number 377 * of bytes to copy (l). We use an optimized algorithm in which 378 * we first calculate log2(current destination position in sb), 379 * which allows determination of l and p in O(1) rather than 380 * O(n). We just need an arch-optimized log2() function now. 381 */ 382 lg = 0; 383 for (i = *dest_ofs - do_sb_start - 1; i >= 0x10; i >>= 1) 384 lg++; 385 386 /* Get the phrase token into i. */ 387 pt = le16_to_cpup((le16*)cb); 388 389 /* 390 * Calculate starting position of the byte sequence in 391 * the destination using the fact that p = (pt >> (12 - lg)) + 1 392 * and make sure we don't go too far back. 393 */ 394 dp_back_addr = dp_addr - (pt >> (12 - lg)) - 1; 395 if (dp_back_addr < dp_sb_start) 396 goto return_overflow; 397 398 /* Now calculate the length of the byte sequence. */ 399 length = (pt & (0xfff >> lg)) + 3; 400 401 /* Advance destination position and verify it is in range. */ 402 *dest_ofs += length; 403 if (*dest_ofs > do_sb_end) 404 goto return_overflow; 405 406 /* The number of non-overlapping bytes. */ 407 max_non_overlap = dp_addr - dp_back_addr; 408 409 if (length <= max_non_overlap) { 410 /* The byte sequence doesn't overlap, just copy it. */ 411 memcpy(dp_addr, dp_back_addr, length); 412 413 /* Advance destination pointer. */ 414 dp_addr += length; 415 } else { 416 /* 417 * The byte sequence does overlap, copy non-overlapping 418 * part and then do a slow byte by byte copy for the 419 * overlapping part. Also, advance the destination 420 * pointer. 421 */ 422 memcpy(dp_addr, dp_back_addr, max_non_overlap); 423 dp_addr += max_non_overlap; 424 dp_back_addr += max_non_overlap; 425 length -= max_non_overlap; 426 while (length--) 427 *dp_addr++ = *dp_back_addr++; 428 } 429 430 /* Advance source position and continue with the next token. */ 431 cb += 2; 432 } 433 434 /* No tokens left in the current tag. Continue with the next tag. */ 435 goto do_next_tag; 436 437return_overflow: 438 ntfs_error(NULL, "Failed. Returning -EOVERFLOW."); 439 goto return_error; 440} 441 442int ntfs_read_compressed_block(struct page *page) 443{ 444 loff_t i_size; 445 s64 initialized_size; 446 struct address_space *mapping = page->mapping; 447 ntfs_inode *ni = NTFS_I(mapping->host); 448 ntfs_volume *vol = ni->vol; 449 struct super_block *sb = vol->sb; 450 runlist_element *rl; 451 unsigned long flags, block_size = sb->s_blocksize; 452 unsigned char block_size_bits = sb->s_blocksize_bits; 453 u8 *cb, *cb_pos, *cb_end; 454 struct buffer_head **bhs; 455 unsigned long offset, index = page->index; 456 u32 cb_size = ni->itype.compressed.block_size; 457 u64 cb_size_mask = cb_size - 1UL; 458 VCN vcn; 459 LCN lcn; 460 /* The first wanted vcn (minimum alignment is PAGE_CACHE_SIZE). */ 461 VCN start_vcn = (((s64)index << PAGE_CACHE_SHIFT) & ~cb_size_mask) >> 462 vol->cluster_size_bits; 463 /* 464 * The first vcn after the last wanted vcn (minumum alignment is again 465 * PAGE_CACHE_SIZE. 466 */ 467 VCN end_vcn = ((((s64)(index + 1UL) << PAGE_CACHE_SHIFT) + cb_size - 1) 468 & ~cb_size_mask) >> vol->cluster_size_bits; 469 /* Number of compression blocks (cbs) in the wanted vcn range. */ 470 unsigned int nr_cbs = (end_vcn - start_vcn) << vol->cluster_size_bits 471 >> ni->itype.compressed.block_size_bits; 472 /* 473 * Number of pages required to store the uncompressed data from all 474 * compression blocks (cbs) overlapping @page. Due to alignment 475 * guarantees of start_vcn and end_vcn, no need to round up here. 476 */ 477 unsigned int nr_pages = (end_vcn - start_vcn) << 478 vol->cluster_size_bits >> PAGE_CACHE_SHIFT; 479 unsigned int xpage, max_page, cur_page, cur_ofs, i; 480 unsigned int cb_clusters, cb_max_ofs; 481 int block, max_block, cb_max_page, bhs_size, nr_bhs, err = 0; 482 struct page **pages; 483 unsigned char xpage_done = 0; 484 485 ntfs_debug("Entering, page->index = 0x%lx, cb_size = 0x%x, nr_pages = " 486 "%i.", index, cb_size, nr_pages); 487 /* 488 * Bad things happen if we get here for anything that is not an 489 * unnamed $DATA attribute. 490 */ 491 BUG_ON(ni->type != AT_DATA); 492 BUG_ON(ni->name_len); 493 494 pages = kmalloc(nr_pages * sizeof(struct page *), GFP_NOFS); 495 496 /* Allocate memory to store the buffer heads we need. */ 497 bhs_size = cb_size / block_size * sizeof(struct buffer_head *); 498 bhs = kmalloc(bhs_size, GFP_NOFS); 499 500 if (unlikely(!pages || !bhs)) { 501 kfree(bhs); 502 kfree(pages); 503 unlock_page(page); 504 ntfs_error(vol->sb, "Failed to allocate internal buffers."); 505 return -ENOMEM; 506 } 507 508 /* 509 * We have already been given one page, this is the one we must do. 510 * Once again, the alignment guarantees keep it simple. 511 */ 512 offset = start_vcn << vol->cluster_size_bits >> PAGE_CACHE_SHIFT; 513 xpage = index - offset; 514 pages[xpage] = page; 515 /* 516 * The remaining pages need to be allocated and inserted into the page 517 * cache, alignment guarantees keep all the below much simpler. (-8 518 */ 519 read_lock_irqsave(&ni->size_lock, flags); 520 i_size = i_size_read(VFS_I(ni)); 521 initialized_size = ni->initialized_size; 522 read_unlock_irqrestore(&ni->size_lock, flags); 523 max_page = ((i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) - 524 offset; 525 if (nr_pages < max_page) 526 max_page = nr_pages; 527 for (i = 0; i < max_page; i++, offset++) { 528 if (i != xpage) 529 pages[i] = grab_cache_page_nowait(mapping, offset); 530 page = pages[i]; 531 if (page) { 532 /* 533 * We only (re)read the page if it isn't already read 534 * in and/or dirty or we would be losing data or at 535 * least wasting our time. 536 */ 537 if (!PageDirty(page) && (!PageUptodate(page) || 538 PageError(page))) { 539 ClearPageError(page); 540 kmap(page); 541 continue; 542 } 543 unlock_page(page); 544 page_cache_release(page); 545 pages[i] = NULL; 546 } 547 } 548 549 /* 550 * We have the runlist, and all the destination pages we need to fill. 551 * Now read the first compression block. 552 */ 553 cur_page = 0; 554 cur_ofs = 0; 555 cb_clusters = ni->itype.compressed.block_clusters; 556do_next_cb: 557 nr_cbs--; 558 nr_bhs = 0; 559 560 /* Read all cb buffer heads one cluster at a time. */ 561 rl = NULL; 562 for (vcn = start_vcn, start_vcn += cb_clusters; vcn < start_vcn; 563 vcn++) { 564 bool is_retry = false; 565 566 if (!rl) { 567lock_retry_remap: 568 down_read(&ni->runlist.lock); 569 rl = ni->runlist.rl; 570 } 571 if (likely(rl != NULL)) { 572 /* Seek to element containing target vcn. */ 573 while (rl->length && rl[1].vcn <= vcn) 574 rl++; 575 lcn = ntfs_rl_vcn_to_lcn(rl, vcn); 576 } else 577 lcn = LCN_RL_NOT_MAPPED; 578 ntfs_debug("Reading vcn = 0x%llx, lcn = 0x%llx.", 579 (unsigned long long)vcn, 580 (unsigned long long)lcn); 581 if (lcn < 0) { 582 /* 583 * When we reach the first sparse cluster we have 584 * finished with the cb. 585 */ 586 if (lcn == LCN_HOLE) 587 break; 588 if (is_retry || lcn != LCN_RL_NOT_MAPPED) 589 goto rl_err; 590 is_retry = true; 591 /* 592 * Attempt to map runlist, dropping lock for the 593 * duration. 594 */ 595 up_read(&ni->runlist.lock); 596 if (!ntfs_map_runlist(ni, vcn)) 597 goto lock_retry_remap; 598 goto map_rl_err; 599 } 600 block = lcn << vol->cluster_size_bits >> block_size_bits; 601 /* Read the lcn from device in chunks of block_size bytes. */ 602 max_block = block + (vol->cluster_size >> block_size_bits); 603 do { 604 ntfs_debug("block = 0x%x.", block); 605 if (unlikely(!(bhs[nr_bhs] = sb_getblk(sb, block)))) 606 goto getblk_err; 607 nr_bhs++; 608 } while (++block < max_block); 609 } 610 611 /* Release the lock if we took it. */ 612 if (rl) 613 up_read(&ni->runlist.lock); 614 615 /* Setup and initiate io on all buffer heads. */ 616 for (i = 0; i < nr_bhs; i++) { 617 struct buffer_head *tbh = bhs[i]; 618 619 if (unlikely(test_set_buffer_locked(tbh))) 620 continue; 621 if (unlikely(buffer_uptodate(tbh))) { 622 unlock_buffer(tbh); 623 continue; 624 } 625 get_bh(tbh); 626 tbh->b_end_io = end_buffer_read_sync; 627 submit_bh(READ, tbh); 628 } 629 630 /* Wait for io completion on all buffer heads. */ 631 for (i = 0; i < nr_bhs; i++) { 632 struct buffer_head *tbh = bhs[i]; 633 634 if (buffer_uptodate(tbh)) 635 continue; 636 wait_on_buffer(tbh); 637 /* 638 * We need an optimization barrier here, otherwise we start 639 * hitting the below fixup code when accessing a loopback 640 * mounted ntfs partition. This indicates either there is a 641 * race condition in the loop driver or, more likely, gcc 642 * overoptimises the code without the barrier and it doesn't 643 * do the Right Thing(TM). 644 */ 645 barrier(); 646 if (unlikely(!buffer_uptodate(tbh))) { 647 ntfs_warning(vol->sb, "Buffer is unlocked but not " 648 "uptodate! Unplugging the disk queue " 649 "and rescheduling."); 650 get_bh(tbh); 651 blk_run_address_space(mapping); 652 schedule(); 653 put_bh(tbh); 654 if (unlikely(!buffer_uptodate(tbh))) 655 goto read_err; 656 ntfs_warning(vol->sb, "Buffer is now uptodate. Good."); 657 } 658 } 659 660 /* 661 * Get the compression buffer. We must not sleep any more 662 * until we are finished with it. 663 */ 664 spin_lock(&ntfs_cb_lock); 665 cb = ntfs_compression_buffer; 666 667 BUG_ON(!cb); 668 669 cb_pos = cb; 670 cb_end = cb + cb_size; 671 672 /* Copy the buffer heads into the contiguous buffer. */ 673 for (i = 0; i < nr_bhs; i++) { 674 memcpy(cb_pos, bhs[i]->b_data, block_size); 675 cb_pos += block_size; 676 } 677 678 /* Just a precaution. */ 679 if (cb_pos + 2 <= cb + cb_size) 680 *(u16*)cb_pos = 0; 681 682 /* Reset cb_pos back to the beginning. */ 683 cb_pos = cb; 684 685 /* We now have both source (if present) and destination. */ 686 ntfs_debug("Successfully read the compression block."); 687 688 /* The last page and maximum offset within it for the current cb. */ 689 cb_max_page = (cur_page << PAGE_CACHE_SHIFT) + cur_ofs + cb_size; 690 cb_max_ofs = cb_max_page & ~PAGE_CACHE_MASK; 691 cb_max_page >>= PAGE_CACHE_SHIFT; 692 693 /* Catch end of file inside a compression block. */ 694 if (cb_max_page > max_page) 695 cb_max_page = max_page; 696 697 if (vcn == start_vcn - cb_clusters) { 698 /* Sparse cb, zero out page range overlapping the cb. */ 699 ntfs_debug("Found sparse compression block."); 700 /* We can sleep from now on, so we drop lock. */ 701 spin_unlock(&ntfs_cb_lock); 702 if (cb_max_ofs) 703 cb_max_page--; 704 for (; cur_page < cb_max_page; cur_page++) { 705 page = pages[cur_page]; 706 if (page) { 707 if (likely(!cur_ofs)) 708 clear_page(page_address(page)); 709 else 710 memset(page_address(page) + cur_ofs, 0, 711 PAGE_CACHE_SIZE - 712 cur_ofs); 713 flush_dcache_page(page); 714 kunmap(page); 715 SetPageUptodate(page); 716 unlock_page(page); 717 if (cur_page == xpage) 718 xpage_done = 1; 719 else 720 page_cache_release(page); 721 pages[cur_page] = NULL; 722 } 723 cb_pos += PAGE_CACHE_SIZE - cur_ofs; 724 cur_ofs = 0; 725 if (cb_pos >= cb_end) 726 break; 727 } 728 /* If we have a partial final page, deal with it now. */ 729 if (cb_max_ofs && cb_pos < cb_end) { 730 page = pages[cur_page]; 731 if (page) 732 memset(page_address(page) + cur_ofs, 0, 733 cb_max_ofs - cur_ofs); 734 /* 735 * No need to update cb_pos at this stage: 736 * cb_pos += cb_max_ofs - cur_ofs; 737 */ 738 cur_ofs = cb_max_ofs; 739 } 740 } else if (vcn == start_vcn) { 741 /* We can't sleep so we need two stages. */ 742 unsigned int cur2_page = cur_page; 743 unsigned int cur_ofs2 = cur_ofs; 744 u8 *cb_pos2 = cb_pos; 745 746 ntfs_debug("Found uncompressed compression block."); 747 /* Uncompressed cb, copy it to the destination pages. */ 748 /* 749 * TODO: As a big optimization, we could detect this case 750 * before we read all the pages and use block_read_full_page() 751 * on all full pages instead (we still have to treat partial 752 * pages especially but at least we are getting rid of the 753 * synchronous io for the majority of pages. 754 * Or if we choose not to do the read-ahead/-behind stuff, we 755 * could just return block_read_full_page(pages[xpage]) as long 756 * as PAGE_CACHE_SIZE <= cb_size. 757 */ 758 if (cb_max_ofs) 759 cb_max_page--; 760 /* First stage: copy data into destination pages. */ 761 for (; cur_page < cb_max_page; cur_page++) { 762 page = pages[cur_page]; 763 if (page) 764 memcpy(page_address(page) + cur_ofs, cb_pos, 765 PAGE_CACHE_SIZE - cur_ofs); 766 cb_pos += PAGE_CACHE_SIZE - cur_ofs; 767 cur_ofs = 0; 768 if (cb_pos >= cb_end) 769 break; 770 } 771 /* If we have a partial final page, deal with it now. */ 772 if (cb_max_ofs && cb_pos < cb_end) { 773 page = pages[cur_page]; 774 if (page) 775 memcpy(page_address(page) + cur_ofs, cb_pos, 776 cb_max_ofs - cur_ofs); 777 cb_pos += cb_max_ofs - cur_ofs; 778 cur_ofs = cb_max_ofs; 779 } 780 /* We can sleep from now on, so drop lock. */ 781 spin_unlock(&ntfs_cb_lock); 782 /* Second stage: finalize pages. */ 783 for (; cur2_page < cb_max_page; cur2_page++) { 784 page = pages[cur2_page]; 785 if (page) { 786 /* 787 * If we are outside the initialized size, zero 788 * the out of bounds page range. 789 */ 790 handle_bounds_compressed_page(page, i_size, 791 initialized_size); 792 flush_dcache_page(page); 793 kunmap(page); 794 SetPageUptodate(page); 795 unlock_page(page); 796 if (cur2_page == xpage) 797 xpage_done = 1; 798 else 799 page_cache_release(page); 800 pages[cur2_page] = NULL; 801 } 802 cb_pos2 += PAGE_CACHE_SIZE - cur_ofs2; 803 cur_ofs2 = 0; 804 if (cb_pos2 >= cb_end) 805 break; 806 } 807 } else { 808 /* Compressed cb, decompress it into the destination page(s). */ 809 unsigned int prev_cur_page = cur_page; 810 811 ntfs_debug("Found compressed compression block."); 812 err = ntfs_decompress(pages, &cur_page, &cur_ofs, 813 cb_max_page, cb_max_ofs, xpage, &xpage_done, 814 cb_pos, cb_size - (cb_pos - cb), i_size, 815 initialized_size); 816 /* 817 * We can sleep from now on, lock already dropped by 818 * ntfs_decompress(). 819 */ 820 if (err) { 821 ntfs_error(vol->sb, "ntfs_decompress() failed in inode " 822 "0x%lx with error code %i. Skipping " 823 "this compression block.", 824 ni->mft_no, -err); 825 /* Release the unfinished pages. */ 826 for (; prev_cur_page < cur_page; prev_cur_page++) { 827 page = pages[prev_cur_page]; 828 if (page) { 829 flush_dcache_page(page); 830 kunmap(page); 831 unlock_page(page); 832 if (prev_cur_page != xpage) 833 page_cache_release(page); 834 pages[prev_cur_page] = NULL; 835 } 836 } 837 } 838 } 839 840 /* Release the buffer heads. */ 841 for (i = 0; i < nr_bhs; i++) 842 brelse(bhs[i]); 843 844 /* Do we have more work to do? */ 845 if (nr_cbs) 846 goto do_next_cb; 847 848 /* We no longer need the list of buffer heads. */ 849 kfree(bhs); 850 851 /* Clean up if we have any pages left. Should never happen. */ 852 for (cur_page = 0; cur_page < max_page; cur_page++) { 853 page = pages[cur_page]; 854 if (page) { 855 ntfs_error(vol->sb, "Still have pages left! " 856 "Terminating them with extreme " 857 "prejudice. Inode 0x%lx, page index " 858 "0x%lx.", ni->mft_no, page->index); 859 flush_dcache_page(page); 860 kunmap(page); 861 unlock_page(page); 862 if (cur_page != xpage) 863 page_cache_release(page); 864 pages[cur_page] = NULL; 865 } 866 } 867 868 /* We no longer need the list of pages. */ 869 kfree(pages); 870 871 /* If we have completed the requested page, we return success. */ 872 if (likely(xpage_done)) 873 return 0; 874 875 ntfs_debug("Failed. Returning error code %s.", err == -EOVERFLOW ? 876 "EOVERFLOW" : (!err ? "EIO" : "unkown error")); 877 return err < 0 ? err : -EIO; 878 879read_err: 880 ntfs_error(vol->sb, "IO error while reading compressed data."); 881 /* Release the buffer heads. */ 882 for (i = 0; i < nr_bhs; i++) 883 brelse(bhs[i]); 884 goto err_out; 885 886map_rl_err: 887 ntfs_error(vol->sb, "ntfs_map_runlist() failed. Cannot read " 888 "compression block."); 889 goto err_out; 890 891rl_err: 892 up_read(&ni->runlist.lock); 893 ntfs_error(vol->sb, "ntfs_rl_vcn_to_lcn() failed. Cannot read " 894 "compression block."); 895 goto err_out; 896 897getblk_err: 898 up_read(&ni->runlist.lock); 899 ntfs_error(vol->sb, "getblk() failed. Cannot read compression block."); 900 901err_out: 902 kfree(bhs); 903 for (i = cur_page; i < max_page; i++) { 904 page = pages[i]; 905 if (page) { 906 flush_dcache_page(page); 907 kunmap(page); 908 unlock_page(page); 909 if (i != xpage) 910 page_cache_release(page); 911 } 912 } 913 kfree(pages); 914 return -EIO; 915} 916