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