Blame view

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