Blame view

fs/f2fs/compress.c 38.2 KB
4c8ff7095   Chao Yu   f2fs: support dat...
1
2
3
4
5
6
7
8
9
10
11
12
13
  // SPDX-License-Identifier: GPL-2.0
  /*
   * f2fs compress support
   *
   * Copyright (c) 2019 Chao Yu <chao@kernel.org>
   */
  
  #include <linux/fs.h>
  #include <linux/f2fs_fs.h>
  #include <linux/writeback.h>
  #include <linux/backing-dev.h>
  #include <linux/lzo.h>
  #include <linux/lz4.h>
50cfa66f0   Chao Yu   f2fs: compress: s...
14
  #include <linux/zstd.h>
4c8ff7095   Chao Yu   f2fs: support dat...
15
16
17
18
  
  #include "f2fs.h"
  #include "node.h"
  #include <trace/events/f2fs.h>
c68d6c883   Chao Yu   f2fs: compress: i...
19
20
  static struct kmem_cache *cic_entry_slab;
  static struct kmem_cache *dic_entry_slab;
310830317   Chao Yu   f2fs: compress: i...
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  static void *page_array_alloc(struct inode *inode, int nr)
  {
  	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  	unsigned int size = sizeof(struct page *) * nr;
  
  	if (likely(size <= sbi->page_array_slab_size))
  		return kmem_cache_zalloc(sbi->page_array_slab, GFP_NOFS);
  	return f2fs_kzalloc(sbi, size, GFP_NOFS);
  }
  
  static void page_array_free(struct inode *inode, void *pages, int nr)
  {
  	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  	unsigned int size = sizeof(struct page *) * nr;
  
  	if (!pages)
  		return;
  
  	if (likely(size <= sbi->page_array_slab_size))
  		kmem_cache_free(sbi->page_array_slab, pages);
  	else
  		kfree(pages);
  }
4c8ff7095   Chao Yu   f2fs: support dat...
44
45
46
47
  struct f2fs_compress_ops {
  	int (*init_compress_ctx)(struct compress_ctx *cc);
  	void (*destroy_compress_ctx)(struct compress_ctx *cc);
  	int (*compress_pages)(struct compress_ctx *cc);
23b1faaad   Chao Yu   f2fs: compress: a...
48
49
  	int (*init_decompress_ctx)(struct decompress_io_ctx *dic);
  	void (*destroy_decompress_ctx)(struct decompress_io_ctx *dic);
4c8ff7095   Chao Yu   f2fs: support dat...
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  	int (*decompress_pages)(struct decompress_io_ctx *dic);
  };
  
  static unsigned int offset_in_cluster(struct compress_ctx *cc, pgoff_t index)
  {
  	return index & (cc->cluster_size - 1);
  }
  
  static pgoff_t cluster_idx(struct compress_ctx *cc, pgoff_t index)
  {
  	return index >> cc->log_cluster_size;
  }
  
  static pgoff_t start_idx_of_cluster(struct compress_ctx *cc)
  {
  	return cc->cluster_idx << cc->log_cluster_size;
  }
  
  bool f2fs_is_compressed_page(struct page *page)
  {
  	if (!PagePrivate(page))
  		return false;
  	if (!page_private(page))
  		return false;
  	if (IS_ATOMIC_WRITTEN_PAGE(page) || IS_DUMMY_WRITTEN_PAGE(page))
  		return false;
29b993c7c   Yu Changchun   f2fs: fix an oops...
76
77
78
79
80
81
  	/*
  	 * page->private may be set with pid.
  	 * pid_max is enough to check if it is traced.
  	 */
  	if (IS_IO_TRACED_PAGE(page))
  		return false;
4c8ff7095   Chao Yu   f2fs: support dat...
82
83
84
85
86
87
  	f2fs_bug_on(F2FS_M_SB(page->mapping),
  		*((u32 *)page_private(page)) != F2FS_COMPRESSED_PAGE_MAGIC);
  	return true;
  }
  
  static void f2fs_set_compressed_page(struct page *page,
887347a09   Chao Yu   f2fs: clean up {c...
88
  		struct inode *inode, pgoff_t index, void *data)
4c8ff7095   Chao Yu   f2fs: support dat...
89
90
91
92
93
94
95
  {
  	SetPagePrivate(page);
  	set_page_private(page, (unsigned long)data);
  
  	/* i_crypto_info and iv index */
  	page->index = index;
  	page->mapping = inode->i_mapping;
4c8ff7095   Chao Yu   f2fs: support dat...
96
  }
4c8ff7095   Chao Yu   f2fs: support dat...
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
  static void f2fs_drop_rpages(struct compress_ctx *cc, int len, bool unlock)
  {
  	int i;
  
  	for (i = 0; i < len; i++) {
  		if (!cc->rpages[i])
  			continue;
  		if (unlock)
  			unlock_page(cc->rpages[i]);
  		else
  			put_page(cc->rpages[i]);
  	}
  }
  
  static void f2fs_put_rpages(struct compress_ctx *cc)
  {
  	f2fs_drop_rpages(cc, cc->cluster_size, false);
  }
  
  static void f2fs_unlock_rpages(struct compress_ctx *cc, int len)
  {
  	f2fs_drop_rpages(cc, len, true);
  }
bc67c5d0c   Chao Yu   f2fs: remove unus...
120
  static void f2fs_put_rpages_mapping(struct address_space *mapping,
4c8ff7095   Chao Yu   f2fs: support dat...
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
  				pgoff_t start, int len)
  {
  	int i;
  
  	for (i = 0; i < len; i++) {
  		struct page *page = find_get_page(mapping, start + i);
  
  		put_page(page);
  		put_page(page);
  	}
  }
  
  static void f2fs_put_rpages_wbc(struct compress_ctx *cc,
  		struct writeback_control *wbc, bool redirty, int unlock)
  {
  	unsigned int i;
  
  	for (i = 0; i < cc->cluster_size; i++) {
  		if (!cc->rpages[i])
  			continue;
  		if (redirty)
  			redirty_page_for_writepage(wbc, cc->rpages[i]);
  		f2fs_put_page(cc->rpages[i], unlock);
  	}
  }
  
  struct page *f2fs_compress_control_page(struct page *page)
  {
  	return ((struct compress_io_ctx *)page_private(page))->rpages[0];
  }
  
  int f2fs_init_compress_ctx(struct compress_ctx *cc)
  {
adfc69433   Jaegeuk Kim   f2fs: fix slab le...
154
  	if (cc->rpages)
4c8ff7095   Chao Yu   f2fs: support dat...
155
  		return 0;
310830317   Chao Yu   f2fs: compress: i...
156
  	cc->rpages = page_array_alloc(cc->inode, cc->cluster_size);
4c8ff7095   Chao Yu   f2fs: support dat...
157
158
159
160
161
  	return cc->rpages ? 0 : -ENOMEM;
  }
  
  void f2fs_destroy_compress_ctx(struct compress_ctx *cc)
  {
310830317   Chao Yu   f2fs: compress: i...
162
  	page_array_free(cc->inode, cc->rpages, cc->cluster_size);
4c8ff7095   Chao Yu   f2fs: support dat...
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
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
  	cc->rpages = NULL;
  	cc->nr_rpages = 0;
  	cc->nr_cpages = 0;
  	cc->cluster_idx = NULL_CLUSTER;
  }
  
  void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct page *page)
  {
  	unsigned int cluster_ofs;
  
  	if (!f2fs_cluster_can_merge_page(cc, page->index))
  		f2fs_bug_on(F2FS_I_SB(cc->inode), 1);
  
  	cluster_ofs = offset_in_cluster(cc, page->index);
  	cc->rpages[cluster_ofs] = page;
  	cc->nr_rpages++;
  	cc->cluster_idx = cluster_idx(cc, page->index);
  }
  
  #ifdef CONFIG_F2FS_FS_LZO
  static int lzo_init_compress_ctx(struct compress_ctx *cc)
  {
  	cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
  				LZO1X_MEM_COMPRESS, GFP_NOFS);
  	if (!cc->private)
  		return -ENOMEM;
  
  	cc->clen = lzo1x_worst_compress(PAGE_SIZE << cc->log_cluster_size);
  	return 0;
  }
  
  static void lzo_destroy_compress_ctx(struct compress_ctx *cc)
  {
  	kvfree(cc->private);
  	cc->private = NULL;
  }
  
  static int lzo_compress_pages(struct compress_ctx *cc)
  {
  	int ret;
  
  	ret = lzo1x_1_compress(cc->rbuf, cc->rlen, cc->cbuf->cdata,
  					&cc->clen, cc->private);
  	if (ret != LZO_E_OK) {
  		printk_ratelimited("%sF2FS-fs (%s): lzo compress failed, ret:%d
  ",
  				KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id, ret);
  		return -EIO;
  	}
  	return 0;
  }
  
  static int lzo_decompress_pages(struct decompress_io_ctx *dic)
  {
  	int ret;
  
  	ret = lzo1x_decompress_safe(dic->cbuf->cdata, dic->clen,
  						dic->rbuf, &dic->rlen);
  	if (ret != LZO_E_OK) {
  		printk_ratelimited("%sF2FS-fs (%s): lzo decompress failed, ret:%d
  ",
  				KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id, ret);
  		return -EIO;
  	}
  
  	if (dic->rlen != PAGE_SIZE << dic->log_cluster_size) {
  		printk_ratelimited("%sF2FS-fs (%s): lzo invalid rlen:%zu, "
  					"expected:%lu
  ", KERN_ERR,
  					F2FS_I_SB(dic->inode)->sb->s_id,
  					dic->rlen,
  					PAGE_SIZE << dic->log_cluster_size);
  		return -EIO;
  	}
  	return 0;
  }
  
  static const struct f2fs_compress_ops f2fs_lzo_ops = {
  	.init_compress_ctx	= lzo_init_compress_ctx,
  	.destroy_compress_ctx	= lzo_destroy_compress_ctx,
  	.compress_pages		= lzo_compress_pages,
  	.decompress_pages	= lzo_decompress_pages,
  };
  #endif
  
  #ifdef CONFIG_F2FS_FS_LZ4
  static int lz4_init_compress_ctx(struct compress_ctx *cc)
  {
  	cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
  				LZ4_MEM_COMPRESS, GFP_NOFS);
  	if (!cc->private)
  		return -ENOMEM;
f6644143c   Chao Yu   f2fs: compress: l...
255
256
257
258
259
260
  	/*
  	 * we do not change cc->clen to LZ4_compressBound(inputsize) to
  	 * adapt worst compress case, because lz4 compressor can handle
  	 * output budget properly.
  	 */
  	cc->clen = cc->rlen - PAGE_SIZE - COMPRESS_HEADER_SIZE;
4c8ff7095   Chao Yu   f2fs: support dat...
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
  	return 0;
  }
  
  static void lz4_destroy_compress_ctx(struct compress_ctx *cc)
  {
  	kvfree(cc->private);
  	cc->private = NULL;
  }
  
  static int lz4_compress_pages(struct compress_ctx *cc)
  {
  	int len;
  
  	len = LZ4_compress_default(cc->rbuf, cc->cbuf->cdata, cc->rlen,
  						cc->clen, cc->private);
f6644143c   Chao Yu   f2fs: compress: l...
276
277
  	if (!len)
  		return -EAGAIN;
4c8ff7095   Chao Yu   f2fs: support dat...
278
279
280
281
282
283
284
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
  	cc->clen = len;
  	return 0;
  }
  
  static int lz4_decompress_pages(struct decompress_io_ctx *dic)
  {
  	int ret;
  
  	ret = LZ4_decompress_safe(dic->cbuf->cdata, dic->rbuf,
  						dic->clen, dic->rlen);
  	if (ret < 0) {
  		printk_ratelimited("%sF2FS-fs (%s): lz4 decompress failed, ret:%d
  ",
  				KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id, ret);
  		return -EIO;
  	}
  
  	if (ret != PAGE_SIZE << dic->log_cluster_size) {
  		printk_ratelimited("%sF2FS-fs (%s): lz4 invalid rlen:%zu, "
  					"expected:%lu
  ", KERN_ERR,
  					F2FS_I_SB(dic->inode)->sb->s_id,
  					dic->rlen,
  					PAGE_SIZE << dic->log_cluster_size);
  		return -EIO;
  	}
  	return 0;
  }
  
  static const struct f2fs_compress_ops f2fs_lz4_ops = {
  	.init_compress_ctx	= lz4_init_compress_ctx,
  	.destroy_compress_ctx	= lz4_destroy_compress_ctx,
  	.compress_pages		= lz4_compress_pages,
  	.decompress_pages	= lz4_decompress_pages,
  };
  #endif
50cfa66f0   Chao Yu   f2fs: compress: s...
314
315
316
317
318
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
  #ifdef CONFIG_F2FS_FS_ZSTD
  #define F2FS_ZSTD_DEFAULT_CLEVEL	1
  
  static int zstd_init_compress_ctx(struct compress_ctx *cc)
  {
  	ZSTD_parameters params;
  	ZSTD_CStream *stream;
  	void *workspace;
  	unsigned int workspace_size;
  
  	params = ZSTD_getParams(F2FS_ZSTD_DEFAULT_CLEVEL, cc->rlen, 0);
  	workspace_size = ZSTD_CStreamWorkspaceBound(params.cParams);
  
  	workspace = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
  					workspace_size, GFP_NOFS);
  	if (!workspace)
  		return -ENOMEM;
  
  	stream = ZSTD_initCStream(params, 0, workspace, workspace_size);
  	if (!stream) {
  		printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_initCStream failed
  ",
  				KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
  				__func__);
  		kvfree(workspace);
  		return -EIO;
  	}
  
  	cc->private = workspace;
  	cc->private2 = stream;
  
  	cc->clen = cc->rlen - PAGE_SIZE - COMPRESS_HEADER_SIZE;
  	return 0;
  }
  
  static void zstd_destroy_compress_ctx(struct compress_ctx *cc)
  {
  	kvfree(cc->private);
  	cc->private = NULL;
  	cc->private2 = NULL;
  }
  
  static int zstd_compress_pages(struct compress_ctx *cc)
  {
  	ZSTD_CStream *stream = cc->private2;
  	ZSTD_inBuffer inbuf;
  	ZSTD_outBuffer outbuf;
  	int src_size = cc->rlen;
  	int dst_size = src_size - PAGE_SIZE - COMPRESS_HEADER_SIZE;
  	int ret;
  
  	inbuf.pos = 0;
  	inbuf.src = cc->rbuf;
  	inbuf.size = src_size;
  
  	outbuf.pos = 0;
  	outbuf.dst = cc->cbuf->cdata;
  	outbuf.size = dst_size;
  
  	ret = ZSTD_compressStream(stream, &outbuf, &inbuf);
  	if (ZSTD_isError(ret)) {
  		printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_compressStream failed, ret: %d
  ",
  				KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
  				__func__, ZSTD_getErrorCode(ret));
  		return -EIO;
  	}
  
  	ret = ZSTD_endStream(stream, &outbuf);
  	if (ZSTD_isError(ret)) {
  		printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_endStream returned %d
  ",
  				KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
  				__func__, ZSTD_getErrorCode(ret));
  		return -EIO;
  	}
1454c978e   Chao Yu   f2fs: compress: f...
390
391
392
393
394
395
  	/*
  	 * there is compressed data remained in intermediate buffer due to
  	 * no more space in cbuf.cdata
  	 */
  	if (ret)
  		return -EAGAIN;
50cfa66f0   Chao Yu   f2fs: compress: s...
396
397
398
399
400
401
402
403
404
  	cc->clen = outbuf.pos;
  	return 0;
  }
  
  static int zstd_init_decompress_ctx(struct decompress_io_ctx *dic)
  {
  	ZSTD_DStream *stream;
  	void *workspace;
  	unsigned int workspace_size;
0e2b7385c   Chao Yu   f2fs: allocate pr...
405
406
  	unsigned int max_window_size =
  			MAX_COMPRESS_WINDOW_SIZE(dic->log_cluster_size);
50cfa66f0   Chao Yu   f2fs: compress: s...
407

0e2b7385c   Chao Yu   f2fs: allocate pr...
408
  	workspace_size = ZSTD_DStreamWorkspaceBound(max_window_size);
50cfa66f0   Chao Yu   f2fs: compress: s...
409
410
411
412
413
  
  	workspace = f2fs_kvmalloc(F2FS_I_SB(dic->inode),
  					workspace_size, GFP_NOFS);
  	if (!workspace)
  		return -ENOMEM;
0e2b7385c   Chao Yu   f2fs: allocate pr...
414
  	stream = ZSTD_initDStream(max_window_size, workspace, workspace_size);
50cfa66f0   Chao Yu   f2fs: compress: s...
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
  	if (!stream) {
  		printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_initDStream failed
  ",
  				KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id,
  				__func__);
  		kvfree(workspace);
  		return -EIO;
  	}
  
  	dic->private = workspace;
  	dic->private2 = stream;
  
  	return 0;
  }
  
  static void zstd_destroy_decompress_ctx(struct decompress_io_ctx *dic)
  {
  	kvfree(dic->private);
  	dic->private = NULL;
  	dic->private2 = NULL;
  }
  
  static int zstd_decompress_pages(struct decompress_io_ctx *dic)
  {
  	ZSTD_DStream *stream = dic->private2;
  	ZSTD_inBuffer inbuf;
  	ZSTD_outBuffer outbuf;
  	int ret;
  
  	inbuf.pos = 0;
  	inbuf.src = dic->cbuf->cdata;
  	inbuf.size = dic->clen;
  
  	outbuf.pos = 0;
  	outbuf.dst = dic->rbuf;
  	outbuf.size = dic->rlen;
  
  	ret = ZSTD_decompressStream(stream, &outbuf, &inbuf);
  	if (ZSTD_isError(ret)) {
  		printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_compressStream failed, ret: %d
  ",
  				KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id,
  				__func__, ZSTD_getErrorCode(ret));
  		return -EIO;
  	}
  
  	if (dic->rlen != outbuf.pos) {
  		printk_ratelimited("%sF2FS-fs (%s): %s ZSTD invalid rlen:%zu, "
  				"expected:%lu
  ", KERN_ERR,
  				F2FS_I_SB(dic->inode)->sb->s_id,
  				__func__, dic->rlen,
  				PAGE_SIZE << dic->log_cluster_size);
  		return -EIO;
  	}
  
  	return 0;
  }
  
  static const struct f2fs_compress_ops f2fs_zstd_ops = {
  	.init_compress_ctx	= zstd_init_compress_ctx,
  	.destroy_compress_ctx	= zstd_destroy_compress_ctx,
  	.compress_pages		= zstd_compress_pages,
  	.init_decompress_ctx	= zstd_init_decompress_ctx,
  	.destroy_decompress_ctx	= zstd_destroy_decompress_ctx,
  	.decompress_pages	= zstd_decompress_pages,
  };
  #endif
6d92b2010   Chao Yu   f2fs: compress: s...
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
  #ifdef CONFIG_F2FS_FS_LZO
  #ifdef CONFIG_F2FS_FS_LZORLE
  static int lzorle_compress_pages(struct compress_ctx *cc)
  {
  	int ret;
  
  	ret = lzorle1x_1_compress(cc->rbuf, cc->rlen, cc->cbuf->cdata,
  					&cc->clen, cc->private);
  	if (ret != LZO_E_OK) {
  		printk_ratelimited("%sF2FS-fs (%s): lzo-rle compress failed, ret:%d
  ",
  				KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id, ret);
  		return -EIO;
  	}
  	return 0;
  }
  
  static const struct f2fs_compress_ops f2fs_lzorle_ops = {
  	.init_compress_ctx	= lzo_init_compress_ctx,
  	.destroy_compress_ctx	= lzo_destroy_compress_ctx,
  	.compress_pages		= lzorle_compress_pages,
  	.decompress_pages	= lzo_decompress_pages,
  };
  #endif
  #endif
4c8ff7095   Chao Yu   f2fs: support dat...
508
509
510
511
512
513
514
515
516
517
518
  static const struct f2fs_compress_ops *f2fs_cops[COMPRESS_MAX] = {
  #ifdef CONFIG_F2FS_FS_LZO
  	&f2fs_lzo_ops,
  #else
  	NULL,
  #endif
  #ifdef CONFIG_F2FS_FS_LZ4
  	&f2fs_lz4_ops,
  #else
  	NULL,
  #endif
50cfa66f0   Chao Yu   f2fs: compress: s...
519
520
521
522
523
  #ifdef CONFIG_F2FS_FS_ZSTD
  	&f2fs_zstd_ops,
  #else
  	NULL,
  #endif
6d92b2010   Chao Yu   f2fs: compress: s...
524
525
526
527
528
  #if defined(CONFIG_F2FS_FS_LZO) && defined(CONFIG_F2FS_FS_LZORLE)
  	&f2fs_lzorle_ops,
  #else
  	NULL,
  #endif
4c8ff7095   Chao Yu   f2fs: support dat...
529
530
531
532
533
534
535
536
  };
  
  bool f2fs_is_compress_backend_ready(struct inode *inode)
  {
  	if (!f2fs_compressed_file(inode))
  		return true;
  	return f2fs_cops[F2FS_I(inode)->i_compress_algorithm];
  }
99bbe3070   Jaegeuk Kim   f2fs: avoid check...
537
  static mempool_t *compress_page_pool;
5e6bbde95   Chao Yu   f2fs: introduce m...
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
  static int num_compress_pages = 512;
  module_param(num_compress_pages, uint, 0444);
  MODULE_PARM_DESC(num_compress_pages,
  		"Number of intermediate compress pages to preallocate");
  
  int f2fs_init_compress_mempool(void)
  {
  	compress_page_pool = mempool_create_page_pool(num_compress_pages, 0);
  	if (!compress_page_pool)
  		return -ENOMEM;
  
  	return 0;
  }
  
  void f2fs_destroy_compress_mempool(void)
  {
  	mempool_destroy(compress_page_pool);
  }
  
  static struct page *f2fs_compress_alloc_page(void)
4c8ff7095   Chao Yu   f2fs: support dat...
558
559
  {
  	struct page *page;
5e6bbde95   Chao Yu   f2fs: introduce m...
560
  	page = mempool_alloc(compress_page_pool, GFP_NOFS);
4c8ff7095   Chao Yu   f2fs: support dat...
561
  	lock_page(page);
5e6bbde95   Chao Yu   f2fs: introduce m...
562

4c8ff7095   Chao Yu   f2fs: support dat...
563
564
  	return page;
  }
5e6bbde95   Chao Yu   f2fs: introduce m...
565
566
567
568
569
570
571
572
573
574
  static void f2fs_compress_free_page(struct page *page)
  {
  	if (!page)
  		return;
  	set_page_private(page, (unsigned long)NULL);
  	ClearPagePrivate(page);
  	page->mapping = NULL;
  	unlock_page(page);
  	mempool_free(page, compress_page_pool);
  }
6fcaebac6   Daeho Jeong   f2fs: change virt...
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
  #define MAX_VMAP_RETRIES	3
  
  static void *f2fs_vmap(struct page **pages, unsigned int count)
  {
  	int i;
  	void *buf = NULL;
  
  	for (i = 0; i < MAX_VMAP_RETRIES; i++) {
  		buf = vm_map_ram(pages, count, -1);
  		if (buf)
  			break;
  		vm_unmap_aliases();
  	}
  	return buf;
  }
4c8ff7095   Chao Yu   f2fs: support dat...
590
591
  static int f2fs_compress_pages(struct compress_ctx *cc)
  {
4c8ff7095   Chao Yu   f2fs: support dat...
592
593
594
  	struct f2fs_inode_info *fi = F2FS_I(cc->inode);
  	const struct f2fs_compress_ops *cops =
  				f2fs_cops[fi->i_compress_algorithm];
310830317   Chao Yu   f2fs: compress: i...
595
596
  	unsigned int max_len, new_nr_cpages;
  	struct page **new_cpages;
1f004d62f   Chao Yu   f2fs: compress: s...
597
  	u32 chksum = 0;
4c8ff7095   Chao Yu   f2fs: support dat...
598
599
600
601
  	int i, ret;
  
  	trace_f2fs_compress_pages_start(cc->inode, cc->cluster_idx,
  				cc->cluster_size, fi->i_compress_algorithm);
23b1faaad   Chao Yu   f2fs: compress: a...
602
603
604
605
606
  	if (cops->init_compress_ctx) {
  		ret = cops->init_compress_ctx(cc);
  		if (ret)
  			goto out;
  	}
4c8ff7095   Chao Yu   f2fs: support dat...
607
608
609
  
  	max_len = COMPRESS_HEADER_SIZE + cc->clen;
  	cc->nr_cpages = DIV_ROUND_UP(max_len, PAGE_SIZE);
310830317   Chao Yu   f2fs: compress: i...
610
  	cc->cpages = page_array_alloc(cc->inode, cc->nr_cpages);
4c8ff7095   Chao Yu   f2fs: support dat...
611
612
613
614
615
616
  	if (!cc->cpages) {
  		ret = -ENOMEM;
  		goto destroy_compress_ctx;
  	}
  
  	for (i = 0; i < cc->nr_cpages; i++) {
5e6bbde95   Chao Yu   f2fs: introduce m...
617
  		cc->cpages[i] = f2fs_compress_alloc_page();
4c8ff7095   Chao Yu   f2fs: support dat...
618
619
620
621
622
  		if (!cc->cpages[i]) {
  			ret = -ENOMEM;
  			goto out_free_cpages;
  		}
  	}
6fcaebac6   Daeho Jeong   f2fs: change virt...
623
  	cc->rbuf = f2fs_vmap(cc->rpages, cc->cluster_size);
4c8ff7095   Chao Yu   f2fs: support dat...
624
625
626
627
  	if (!cc->rbuf) {
  		ret = -ENOMEM;
  		goto out_free_cpages;
  	}
6fcaebac6   Daeho Jeong   f2fs: change virt...
628
  	cc->cbuf = f2fs_vmap(cc->cpages, cc->nr_cpages);
4c8ff7095   Chao Yu   f2fs: support dat...
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
  	if (!cc->cbuf) {
  		ret = -ENOMEM;
  		goto out_vunmap_rbuf;
  	}
  
  	ret = cops->compress_pages(cc);
  	if (ret)
  		goto out_vunmap_cbuf;
  
  	max_len = PAGE_SIZE * (cc->cluster_size - 1) - COMPRESS_HEADER_SIZE;
  
  	if (cc->clen > max_len) {
  		ret = -EAGAIN;
  		goto out_vunmap_cbuf;
  	}
  
  	cc->cbuf->clen = cpu_to_le32(cc->clen);
4c8ff7095   Chao Yu   f2fs: support dat...
646

1f004d62f   Chao Yu   f2fs: compress: s...
647
648
649
650
  	if (fi->i_compress_flag & 1 << COMPRESS_CHKSUM)
  		chksum = f2fs_crc32(F2FS_I_SB(cc->inode),
  					cc->cbuf->cdata, cc->clen);
  	cc->cbuf->chksum = cpu_to_le32(chksum);
4c8ff7095   Chao Yu   f2fs: support dat...
651
652
  	for (i = 0; i < COMPRESS_DATA_RESERVED_SIZE; i++)
  		cc->cbuf->reserved[i] = cpu_to_le32(0);
310830317   Chao Yu   f2fs: compress: i...
653
654
655
656
657
658
659
660
  	new_nr_cpages = DIV_ROUND_UP(cc->clen + COMPRESS_HEADER_SIZE, PAGE_SIZE);
  
  	/* Now we're going to cut unnecessary tail pages */
  	new_cpages = page_array_alloc(cc->inode, new_nr_cpages);
  	if (!new_cpages) {
  		ret = -ENOMEM;
  		goto out_vunmap_cbuf;
  	}
7fa6d5981   Eric Biggers   f2fs: fix leaking...
661
662
663
  
  	/* zero out any unused part of the last page */
  	memset(&cc->cbuf->cdata[cc->clen], 0,
310830317   Chao Yu   f2fs: compress: i...
664
665
  			(new_nr_cpages * PAGE_SIZE) -
  			(cc->clen + COMPRESS_HEADER_SIZE));
7fa6d5981   Eric Biggers   f2fs: fix leaking...
666

6fcaebac6   Daeho Jeong   f2fs: change virt...
667
668
  	vm_unmap_ram(cc->cbuf, cc->nr_cpages);
  	vm_unmap_ram(cc->rbuf, cc->cluster_size);
4c8ff7095   Chao Yu   f2fs: support dat...
669

310830317   Chao Yu   f2fs: compress: i...
670
671
672
673
674
  	for (i = 0; i < cc->nr_cpages; i++) {
  		if (i < new_nr_cpages) {
  			new_cpages[i] = cc->cpages[i];
  			continue;
  		}
5e6bbde95   Chao Yu   f2fs: introduce m...
675
  		f2fs_compress_free_page(cc->cpages[i]);
4c8ff7095   Chao Yu   f2fs: support dat...
676
677
  		cc->cpages[i] = NULL;
  	}
23b1faaad   Chao Yu   f2fs: compress: a...
678
679
  	if (cops->destroy_compress_ctx)
  		cops->destroy_compress_ctx(cc);
09ff48011   Chao Yu   f2fs: compress: f...
680

310830317   Chao Yu   f2fs: compress: i...
681
682
683
  	page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
  	cc->cpages = new_cpages;
  	cc->nr_cpages = new_nr_cpages;
4c8ff7095   Chao Yu   f2fs: support dat...
684
685
686
687
688
689
  
  	trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
  							cc->clen, ret);
  	return 0;
  
  out_vunmap_cbuf:
6fcaebac6   Daeho Jeong   f2fs: change virt...
690
  	vm_unmap_ram(cc->cbuf, cc->nr_cpages);
4c8ff7095   Chao Yu   f2fs: support dat...
691
  out_vunmap_rbuf:
6fcaebac6   Daeho Jeong   f2fs: change virt...
692
  	vm_unmap_ram(cc->rbuf, cc->cluster_size);
4c8ff7095   Chao Yu   f2fs: support dat...
693
694
695
  out_free_cpages:
  	for (i = 0; i < cc->nr_cpages; i++) {
  		if (cc->cpages[i])
5e6bbde95   Chao Yu   f2fs: introduce m...
696
  			f2fs_compress_free_page(cc->cpages[i]);
4c8ff7095   Chao Yu   f2fs: support dat...
697
  	}
310830317   Chao Yu   f2fs: compress: i...
698
  	page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
4c8ff7095   Chao Yu   f2fs: support dat...
699
700
  	cc->cpages = NULL;
  destroy_compress_ctx:
23b1faaad   Chao Yu   f2fs: compress: a...
701
702
  	if (cops->destroy_compress_ctx)
  		cops->destroy_compress_ctx(cc);
4c8ff7095   Chao Yu   f2fs: support dat...
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
  out:
  	trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
  							cc->clen, ret);
  	return ret;
  }
  
  void f2fs_decompress_pages(struct bio *bio, struct page *page, bool verity)
  {
  	struct decompress_io_ctx *dic =
  			(struct decompress_io_ctx *)page_private(page);
  	struct f2fs_sb_info *sbi = F2FS_I_SB(dic->inode);
  	struct f2fs_inode_info *fi= F2FS_I(dic->inode);
  	const struct f2fs_compress_ops *cops =
  			f2fs_cops[fi->i_compress_algorithm];
  	int ret;
b2f57a8e6   Chao Yu   f2fs: compress: d...
718
  	int i;
4c8ff7095   Chao Yu   f2fs: support dat...
719
720
721
722
723
  
  	dec_page_count(sbi, F2FS_RD_DATA);
  
  	if (bio->bi_status || PageError(page))
  		dic->failed = true;
e6c3948de   Chao Yu   f2fs: compress: u...
724
  	if (atomic_dec_return(&dic->pending_pages))
4c8ff7095   Chao Yu   f2fs: support dat...
725
726
727
728
729
730
731
732
733
734
  		return;
  
  	trace_f2fs_decompress_pages_start(dic->inode, dic->cluster_idx,
  				dic->cluster_size, fi->i_compress_algorithm);
  
  	/* submit partial compressed pages */
  	if (dic->failed) {
  		ret = -EIO;
  		goto out_free_dic;
  	}
310830317   Chao Yu   f2fs: compress: i...
735
  	dic->tpages = page_array_alloc(dic->inode, dic->cluster_size);
b2f57a8e6   Chao Yu   f2fs: compress: d...
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
  	if (!dic->tpages) {
  		ret = -ENOMEM;
  		goto out_free_dic;
  	}
  
  	for (i = 0; i < dic->cluster_size; i++) {
  		if (dic->rpages[i]) {
  			dic->tpages[i] = dic->rpages[i];
  			continue;
  		}
  
  		dic->tpages[i] = f2fs_compress_alloc_page();
  		if (!dic->tpages[i]) {
  			ret = -ENOMEM;
  			goto out_free_dic;
  		}
  	}
23b1faaad   Chao Yu   f2fs: compress: a...
753
754
755
756
757
  	if (cops->init_decompress_ctx) {
  		ret = cops->init_decompress_ctx(dic);
  		if (ret)
  			goto out_free_dic;
  	}
6fcaebac6   Daeho Jeong   f2fs: change virt...
758
  	dic->rbuf = f2fs_vmap(dic->tpages, dic->cluster_size);
4c8ff7095   Chao Yu   f2fs: support dat...
759
760
  	if (!dic->rbuf) {
  		ret = -ENOMEM;
23b1faaad   Chao Yu   f2fs: compress: a...
761
  		goto destroy_decompress_ctx;
4c8ff7095   Chao Yu   f2fs: support dat...
762
  	}
6fcaebac6   Daeho Jeong   f2fs: change virt...
763
  	dic->cbuf = f2fs_vmap(dic->cpages, dic->nr_cpages);
4c8ff7095   Chao Yu   f2fs: support dat...
764
765
766
767
768
769
770
771
772
773
774
775
776
777
  	if (!dic->cbuf) {
  		ret = -ENOMEM;
  		goto out_vunmap_rbuf;
  	}
  
  	dic->clen = le32_to_cpu(dic->cbuf->clen);
  	dic->rlen = PAGE_SIZE << dic->log_cluster_size;
  
  	if (dic->clen > PAGE_SIZE * dic->nr_cpages - COMPRESS_HEADER_SIZE) {
  		ret = -EFSCORRUPTED;
  		goto out_vunmap_cbuf;
  	}
  
  	ret = cops->decompress_pages(dic);
3ed3a9407   Chao Yu   f2fs: compress: f...
778
  	if (!ret && (fi->i_compress_flag & 1 << COMPRESS_CHKSUM)) {
1f004d62f   Chao Yu   f2fs: compress: s...
779
780
781
782
783
784
785
786
787
788
789
790
  		u32 provided = le32_to_cpu(dic->cbuf->chksum);
  		u32 calculated = f2fs_crc32(sbi, dic->cbuf->cdata, dic->clen);
  
  		if (provided != calculated) {
  			if (!is_inode_flag_set(dic->inode, FI_COMPRESS_CORRUPT)) {
  				set_inode_flag(dic->inode, FI_COMPRESS_CORRUPT);
  				printk_ratelimited(
  					"%sF2FS-fs (%s): checksum invalid, nid = %lu, %x vs %x",
  					KERN_INFO, sbi->sb->s_id, dic->inode->i_ino,
  					provided, calculated);
  			}
  			set_sbi_flag(sbi, SBI_NEED_FSCK);
1f004d62f   Chao Yu   f2fs: compress: s...
791
792
  		}
  	}
4c8ff7095   Chao Yu   f2fs: support dat...
793
  out_vunmap_cbuf:
6fcaebac6   Daeho Jeong   f2fs: change virt...
794
  	vm_unmap_ram(dic->cbuf, dic->nr_cpages);
4c8ff7095   Chao Yu   f2fs: support dat...
795
  out_vunmap_rbuf:
6fcaebac6   Daeho Jeong   f2fs: change virt...
796
  	vm_unmap_ram(dic->rbuf, dic->cluster_size);
23b1faaad   Chao Yu   f2fs: compress: a...
797
798
799
  destroy_decompress_ctx:
  	if (cops->destroy_decompress_ctx)
  		cops->destroy_decompress_ctx(dic);
4c8ff7095   Chao Yu   f2fs: support dat...
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
  out_free_dic:
  	if (!verity)
  		f2fs_decompress_end_io(dic->rpages, dic->cluster_size,
  								ret, false);
  
  	trace_f2fs_decompress_pages_end(dic->inode, dic->cluster_idx,
  							dic->clen, ret);
  	if (!verity)
  		f2fs_free_dic(dic);
  }
  
  static bool is_page_in_cluster(struct compress_ctx *cc, pgoff_t index)
  {
  	if (cc->cluster_idx == NULL_CLUSTER)
  		return true;
  	return cc->cluster_idx == cluster_idx(cc, index);
  }
  
  bool f2fs_cluster_is_empty(struct compress_ctx *cc)
  {
  	return cc->nr_rpages == 0;
  }
  
  static bool f2fs_cluster_is_full(struct compress_ctx *cc)
  {
  	return cc->cluster_size == cc->nr_rpages;
  }
  
  bool f2fs_cluster_can_merge_page(struct compress_ctx *cc, pgoff_t index)
  {
  	if (f2fs_cluster_is_empty(cc))
  		return true;
  	return is_page_in_cluster(cc, index);
  }
  
  static bool __cluster_may_compress(struct compress_ctx *cc)
  {
  	struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
  	loff_t i_size = i_size_read(cc->inode);
  	unsigned nr_pages = DIV_ROUND_UP(i_size, PAGE_SIZE);
  	int i;
  
  	for (i = 0; i < cc->cluster_size; i++) {
  		struct page *page = cc->rpages[i];
  
  		f2fs_bug_on(sbi, !page);
  
  		if (unlikely(f2fs_cp_error(sbi)))
  			return false;
  		if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
  			return false;
  
  		/* beyond EOF */
  		if (page->index >= nr_pages)
  			return false;
  	}
  	return true;
  }
1a67cbe14   Chao Yu   f2fs: fix to acco...
858
  static int __f2fs_cluster_blocks(struct compress_ctx *cc, bool compr)
4c8ff7095   Chao Yu   f2fs: support dat...
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
  {
  	struct dnode_of_data dn;
  	int ret;
  
  	set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
  	ret = f2fs_get_dnode_of_data(&dn, start_idx_of_cluster(cc),
  							LOOKUP_NODE);
  	if (ret) {
  		if (ret == -ENOENT)
  			ret = 0;
  		goto fail;
  	}
  
  	if (dn.data_blkaddr == COMPRESS_ADDR) {
  		int i;
  
  		ret = 1;
  		for (i = 1; i < cc->cluster_size; i++) {
  			block_t blkaddr;
a2ced1ce1   Chao Yu   f2fs: clean up co...
878
  			blkaddr = data_blkaddr(dn.inode,
4c8ff7095   Chao Yu   f2fs: support dat...
879
  					dn.node_page, dn.ofs_in_node + i);
1a67cbe14   Chao Yu   f2fs: fix to acco...
880
881
882
883
884
885
886
  			if (compr) {
  				if (__is_valid_data_blkaddr(blkaddr))
  					ret++;
  			} else {
  				if (blkaddr != NULL_ADDR)
  					ret++;
  			}
4c8ff7095   Chao Yu   f2fs: support dat...
887
888
889
890
891
892
  		}
  	}
  fail:
  	f2fs_put_dnode(&dn);
  	return ret;
  }
1a67cbe14   Chao Yu   f2fs: fix to acco...
893
894
895
896
897
898
899
  /* return # of compressed blocks in compressed cluster */
  static int f2fs_compressed_blocks(struct compress_ctx *cc)
  {
  	return __f2fs_cluster_blocks(cc, true);
  }
  
  /* return # of valid blocks in compressed cluster */
d078319d0   Wang Xiaojun   f2fs: remove the ...
900
  static int f2fs_cluster_blocks(struct compress_ctx *cc)
1a67cbe14   Chao Yu   f2fs: fix to acco...
901
902
903
  {
  	return __f2fs_cluster_blocks(cc, false);
  }
4c8ff7095   Chao Yu   f2fs: support dat...
904
905
906
907
908
909
910
911
  int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index)
  {
  	struct compress_ctx cc = {
  		.inode = inode,
  		.log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
  		.cluster_size = F2FS_I(inode)->i_cluster_size,
  		.cluster_idx = index >> F2FS_I(inode)->i_log_cluster_size,
  	};
d078319d0   Wang Xiaojun   f2fs: remove the ...
912
  	return f2fs_cluster_blocks(&cc);
4c8ff7095   Chao Yu   f2fs: support dat...
913
914
915
916
  }
  
  static bool cluster_may_compress(struct compress_ctx *cc)
  {
f15a200ae   Daeho Jeong   f2fs: add compres...
917
  	if (!f2fs_need_compress_data(cc->inode))
4c8ff7095   Chao Yu   f2fs: support dat...
918
919
920
921
922
923
924
  		return false;
  	if (f2fs_is_atomic_file(cc->inode))
  		return false;
  	if (f2fs_is_mmap_file(cc->inode))
  		return false;
  	if (!f2fs_cluster_is_full(cc))
  		return false;
dc35d73a4   Chao Yu   f2fs: compress: d...
925
926
  	if (unlikely(f2fs_cp_error(F2FS_I_SB(cc->inode))))
  		return false;
4c8ff7095   Chao Yu   f2fs: support dat...
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
  	return __cluster_may_compress(cc);
  }
  
  static void set_cluster_writeback(struct compress_ctx *cc)
  {
  	int i;
  
  	for (i = 0; i < cc->cluster_size; i++) {
  		if (cc->rpages[i])
  			set_page_writeback(cc->rpages[i]);
  	}
  }
  
  static void set_cluster_dirty(struct compress_ctx *cc)
  {
  	int i;
  
  	for (i = 0; i < cc->cluster_size; i++)
  		if (cc->rpages[i])
  			set_page_dirty(cc->rpages[i]);
  }
  
  static int prepare_compress_overwrite(struct compress_ctx *cc,
  		struct page **pagep, pgoff_t index, void **fsdata)
  {
  	struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
  	struct address_space *mapping = cc->inode->i_mapping;
  	struct page *page;
  	struct dnode_of_data dn;
  	sector_t last_block_in_bio;
  	unsigned fgp_flag = FGP_LOCK | FGP_WRITE | FGP_CREAT;
  	pgoff_t start_idx = start_idx_of_cluster(cc);
  	int i, ret;
  	bool prealloc;
  
  retry:
d078319d0   Wang Xiaojun   f2fs: remove the ...
963
  	ret = f2fs_cluster_blocks(cc);
4c8ff7095   Chao Yu   f2fs: support dat...
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
  	if (ret <= 0)
  		return ret;
  
  	/* compressed case */
  	prealloc = (ret < cc->cluster_size);
  
  	ret = f2fs_init_compress_ctx(cc);
  	if (ret)
  		return ret;
  
  	/* keep page reference to avoid page reclaim */
  	for (i = 0; i < cc->cluster_size; i++) {
  		page = f2fs_pagecache_get_page(mapping, start_idx + i,
  							fgp_flag, GFP_NOFS);
  		if (!page) {
  			ret = -ENOMEM;
  			goto unlock_pages;
  		}
  
  		if (PageUptodate(page))
  			unlock_page(page);
  		else
  			f2fs_compress_ctx_add_page(cc, page);
  	}
  
  	if (!f2fs_cluster_is_empty(cc)) {
  		struct bio *bio = NULL;
  
  		ret = f2fs_read_multi_pages(cc, &bio, cc->cluster_size,
0683728ad   Chao Yu   f2fs: fix to avoi...
993
  					&last_block_in_bio, false, true);
4c8ff7095   Chao Yu   f2fs: support dat...
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
  		f2fs_destroy_compress_ctx(cc);
  		if (ret)
  			goto release_pages;
  		if (bio)
  			f2fs_submit_bio(sbi, bio, DATA);
  
  		ret = f2fs_init_compress_ctx(cc);
  		if (ret)
  			goto release_pages;
  	}
  
  	for (i = 0; i < cc->cluster_size; i++) {
  		f2fs_bug_on(sbi, cc->rpages[i]);
  
  		page = find_lock_page(mapping, start_idx + i);
  		f2fs_bug_on(sbi, !page);
  
  		f2fs_wait_on_page_writeback(page, DATA, true, true);
  
  		f2fs_compress_ctx_add_page(cc, page);
  		f2fs_put_page(page, 0);
  
  		if (!PageUptodate(page)) {
  			f2fs_unlock_rpages(cc, i + 1);
bc67c5d0c   Chao Yu   f2fs: remove unus...
1018
  			f2fs_put_rpages_mapping(mapping, start_idx,
4c8ff7095   Chao Yu   f2fs: support dat...
1019
1020
1021
1022
1023
1024
1025
  					cc->cluster_size);
  			f2fs_destroy_compress_ctx(cc);
  			goto retry;
  		}
  	}
  
  	if (prealloc) {
0ef818335   Chao Yu   f2fs: add prefix ...
1026
  		f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
4c8ff7095   Chao Yu   f2fs: support dat...
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
  
  		set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
  
  		for (i = cc->cluster_size - 1; i > 0; i--) {
  			ret = f2fs_get_block(&dn, start_idx + i);
  			if (ret) {
  				i = cc->cluster_size;
  				break;
  			}
  
  			if (dn.data_blkaddr != NEW_ADDR)
  				break;
  		}
0ef818335   Chao Yu   f2fs: add prefix ...
1040
  		f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
4c8ff7095   Chao Yu   f2fs: support dat...
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
  	}
  
  	if (likely(!ret)) {
  		*fsdata = cc->rpages;
  		*pagep = cc->rpages[offset_in_cluster(cc, index)];
  		return cc->cluster_size;
  	}
  
  unlock_pages:
  	f2fs_unlock_rpages(cc, i);
  release_pages:
bc67c5d0c   Chao Yu   f2fs: remove unus...
1052
  	f2fs_put_rpages_mapping(mapping, start_idx, i);
4c8ff7095   Chao Yu   f2fs: support dat...
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
  	f2fs_destroy_compress_ctx(cc);
  	return ret;
  }
  
  int f2fs_prepare_compress_overwrite(struct inode *inode,
  		struct page **pagep, pgoff_t index, void **fsdata)
  {
  	struct compress_ctx cc = {
  		.inode = inode,
  		.log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
  		.cluster_size = F2FS_I(inode)->i_cluster_size,
  		.cluster_idx = index >> F2FS_I(inode)->i_log_cluster_size,
  		.rpages = NULL,
  		.nr_rpages = 0,
  	};
  
  	return prepare_compress_overwrite(&cc, pagep, index, fsdata);
  }
  
  bool f2fs_compress_write_end(struct inode *inode, void *fsdata,
  					pgoff_t index, unsigned copied)
  
  {
  	struct compress_ctx cc = {
310830317   Chao Yu   f2fs: compress: i...
1077
  		.inode = inode,
4c8ff7095   Chao Yu   f2fs: support dat...
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
  		.log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
  		.cluster_size = F2FS_I(inode)->i_cluster_size,
  		.rpages = fsdata,
  	};
  	bool first_index = (index == cc.rpages[0]->index);
  
  	if (copied)
  		set_cluster_dirty(&cc);
  
  	f2fs_put_rpages_wbc(&cc, NULL, false, 1);
  	f2fs_destroy_compress_ctx(&cc);
  
  	return first_index;
  }
3265d3db1   Chao Yu   f2fs: support par...
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
  int f2fs_truncate_partial_cluster(struct inode *inode, u64 from, bool lock)
  {
  	void *fsdata = NULL;
  	struct page *pagep;
  	int log_cluster_size = F2FS_I(inode)->i_log_cluster_size;
  	pgoff_t start_idx = from >> (PAGE_SHIFT + log_cluster_size) <<
  							log_cluster_size;
  	int err;
  
  	err = f2fs_is_compressed_cluster(inode, start_idx);
  	if (err < 0)
  		return err;
  
  	/* truncate normal cluster */
  	if (!err)
  		return f2fs_do_truncate_blocks(inode, from, lock);
  
  	/* truncate compressed cluster */
  	err = f2fs_prepare_compress_overwrite(inode, &pagep,
  						start_idx, &fsdata);
  
  	/* should not be a normal cluster */
  	f2fs_bug_on(F2FS_I_SB(inode), err == 0);
  
  	if (err <= 0)
  		return err;
  
  	if (err > 0) {
  		struct page **rpages = fsdata;
  		int cluster_size = F2FS_I(inode)->i_cluster_size;
  		int i;
  
  		for (i = cluster_size - 1; i >= 0; i--) {
  			loff_t start = rpages[i]->index << PAGE_SHIFT;
  
  			if (from <= start) {
  				zero_user_segment(rpages[i], 0, PAGE_SIZE);
  			} else {
  				zero_user_segment(rpages[i], from - start,
  								PAGE_SIZE);
  				break;
  			}
  		}
  
  		f2fs_compress_write_end(inode, fsdata, start_idx, true);
  	}
  	return 0;
  }
4c8ff7095   Chao Yu   f2fs: support dat...
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
  static int f2fs_write_compressed_pages(struct compress_ctx *cc,
  					int *submitted,
  					struct writeback_control *wbc,
  					enum iostat_type io_type)
  {
  	struct inode *inode = cc->inode;
  	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  	struct f2fs_inode_info *fi = F2FS_I(inode);
  	struct f2fs_io_info fio = {
  		.sbi = sbi,
  		.ino = cc->inode->i_ino,
  		.type = DATA,
  		.op = REQ_OP_WRITE,
  		.op_flags = wbc_to_write_flags(wbc),
  		.old_blkaddr = NEW_ADDR,
  		.page = NULL,
  		.encrypted_page = NULL,
  		.compressed_page = NULL,
  		.submitted = false,
4c8ff7095   Chao Yu   f2fs: support dat...
1159
1160
  		.io_type = io_type,
  		.io_wbc = wbc,
27aacd28e   Satya Tangirala   f2fs: add inline ...
1161
  		.encrypted = fscrypt_inode_uses_fs_layer_crypto(cc->inode),
4c8ff7095   Chao Yu   f2fs: support dat...
1162
1163
1164
1165
1166
1167
1168
1169
  	};
  	struct dnode_of_data dn;
  	struct node_info ni;
  	struct compress_io_ctx *cic;
  	pgoff_t start_idx = start_idx_of_cluster(cc);
  	unsigned int last_index = cc->cluster_size - 1;
  	loff_t psize;
  	int i, err;
79963d967   Chao Yu   f2fs: shrink node...
1170
1171
1172
1173
1174
1175
1176
1177
  	if (IS_NOQUOTA(inode)) {
  		/*
  		 * We need to wait for node_write to avoid block allocation during
  		 * checkpoint. This can only happen to quota writes which can cause
  		 * the below discard race condition.
  		 */
  		down_read(&sbi->node_write);
  	} else if (!f2fs_trylock_op(sbi)) {
310830317   Chao Yu   f2fs: compress: i...
1178
  		goto out_free;
79963d967   Chao Yu   f2fs: shrink node...
1179
  	}
4c8ff7095   Chao Yu   f2fs: support dat...
1180

df77fbd8c   Chao Yu   f2fs: fix to avoi...
1181
  	set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
4c8ff7095   Chao Yu   f2fs: support dat...
1182
1183
1184
1185
1186
1187
  
  	err = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
  	if (err)
  		goto out_unlock_op;
  
  	for (i = 0; i < cc->cluster_size; i++) {
a2ced1ce1   Chao Yu   f2fs: clean up co...
1188
  		if (data_blkaddr(dn.inode, dn.node_page,
4c8ff7095   Chao Yu   f2fs: support dat...
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
  					dn.ofs_in_node + i) == NULL_ADDR)
  			goto out_put_dnode;
  	}
  
  	psize = (loff_t)(cc->rpages[last_index]->index + 1) << PAGE_SHIFT;
  
  	err = f2fs_get_node_info(fio.sbi, dn.nid, &ni);
  	if (err)
  		goto out_put_dnode;
  
  	fio.version = ni.version;
c68d6c883   Chao Yu   f2fs: compress: i...
1200
  	cic = kmem_cache_zalloc(cic_entry_slab, GFP_NOFS);
4c8ff7095   Chao Yu   f2fs: support dat...
1201
1202
1203
1204
1205
  	if (!cic)
  		goto out_put_dnode;
  
  	cic->magic = F2FS_COMPRESSED_PAGE_MAGIC;
  	cic->inode = inode;
e6c3948de   Chao Yu   f2fs: compress: u...
1206
  	atomic_set(&cic->pending_pages, cc->nr_cpages);
310830317   Chao Yu   f2fs: compress: i...
1207
  	cic->rpages = page_array_alloc(cc->inode, cc->cluster_size);
4c8ff7095   Chao Yu   f2fs: support dat...
1208
1209
1210
1211
1212
1213
1214
  	if (!cic->rpages)
  		goto out_put_cic;
  
  	cic->nr_rpages = cc->cluster_size;
  
  	for (i = 0; i < cc->nr_cpages; i++) {
  		f2fs_set_compressed_page(cc->cpages[i], inode,
887347a09   Chao Yu   f2fs: clean up {c...
1215
  					cc->rpages[i + 1]->index, cic);
4c8ff7095   Chao Yu   f2fs: support dat...
1216
  		fio.compressed_page = cc->cpages[i];
f567adb03   Chao Yu   f2fs: fix to wait...
1217
1218
1219
1220
1221
1222
  
  		fio.old_blkaddr = data_blkaddr(dn.inode, dn.node_page,
  						dn.ofs_in_node + i + 1);
  
  		/* wait for GCed page writeback via META_MAPPING */
  		f2fs_wait_on_block_writeback(inode, fio.old_blkaddr);
4c8ff7095   Chao Yu   f2fs: support dat...
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
  		if (fio.encrypted) {
  			fio.page = cc->rpages[i + 1];
  			err = f2fs_encrypt_one_page(&fio);
  			if (err)
  				goto out_destroy_crypt;
  			cc->cpages[i] = fio.encrypted_page;
  		}
  	}
  
  	set_cluster_writeback(cc);
  
  	for (i = 0; i < cc->cluster_size; i++)
  		cic->rpages[i] = cc->rpages[i];
  
  	for (i = 0; i < cc->cluster_size; i++, dn.ofs_in_node++) {
  		block_t blkaddr;
a2ced1ce1   Chao Yu   f2fs: clean up co...
1239
  		blkaddr = f2fs_data_blkaddr(&dn);
95978caa1   Chao Yu   f2fs: fix to avoi...
1240
  		fio.page = cc->rpages[i];
4c8ff7095   Chao Yu   f2fs: support dat...
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
  		fio.old_blkaddr = blkaddr;
  
  		/* cluster header */
  		if (i == 0) {
  			if (blkaddr == COMPRESS_ADDR)
  				fio.compr_blocks++;
  			if (__is_valid_data_blkaddr(blkaddr))
  				f2fs_invalidate_blocks(sbi, blkaddr);
  			f2fs_update_data_blkaddr(&dn, COMPRESS_ADDR);
  			goto unlock_continue;
  		}
  
  		if (fio.compr_blocks && __is_valid_data_blkaddr(blkaddr))
  			fio.compr_blocks++;
  
  		if (i > cc->nr_cpages) {
  			if (__is_valid_data_blkaddr(blkaddr)) {
  				f2fs_invalidate_blocks(sbi, blkaddr);
  				f2fs_update_data_blkaddr(&dn, NEW_ADDR);
  			}
  			goto unlock_continue;
  		}
  
  		f2fs_bug_on(fio.sbi, blkaddr == NULL_ADDR);
  
  		if (fio.encrypted)
  			fio.encrypted_page = cc->cpages[i - 1];
  		else
  			fio.compressed_page = cc->cpages[i - 1];
  
  		cc->cpages[i - 1] = NULL;
  		f2fs_outplace_write_data(&dn, &fio);
  		(*submitted)++;
  unlock_continue:
  		inode_dec_dirty_pages(cc->inode);
  		unlock_page(fio.page);
  	}
  
  	if (fio.compr_blocks)
  		f2fs_i_compr_blocks_update(inode, fio.compr_blocks - 1, false);
  	f2fs_i_compr_blocks_update(inode, cc->nr_cpages, true);
  
  	set_inode_flag(cc->inode, FI_APPEND_WRITE);
  	if (cc->cluster_idx == 0)
  		set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
  
  	f2fs_put_dnode(&dn);
79963d967   Chao Yu   f2fs: shrink node...
1288
1289
1290
  	if (IS_NOQUOTA(inode))
  		up_read(&sbi->node_write);
  	else
435cbab95   Jaegeuk Kim   f2fs: fix quota_s...
1291
  		f2fs_unlock_op(sbi);
4c8ff7095   Chao Yu   f2fs: support dat...
1292

c10c98203   Chao Yu   f2fs: cover last_...
1293
  	spin_lock(&fi->i_size_lock);
4c8ff7095   Chao Yu   f2fs: support dat...
1294
1295
  	if (fi->last_disk_size < psize)
  		fi->last_disk_size = psize;
c10c98203   Chao Yu   f2fs: cover last_...
1296
  	spin_unlock(&fi->i_size_lock);
4c8ff7095   Chao Yu   f2fs: support dat...
1297
1298
  
  	f2fs_put_rpages(cc);
310830317   Chao Yu   f2fs: compress: i...
1299
1300
  	page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
  	cc->cpages = NULL;
4c8ff7095   Chao Yu   f2fs: support dat...
1301
1302
1303
1304
  	f2fs_destroy_compress_ctx(cc);
  	return 0;
  
  out_destroy_crypt:
310830317   Chao Yu   f2fs: compress: i...
1305
  	page_array_free(cc->inode, cic->rpages, cc->cluster_size);
4c8ff7095   Chao Yu   f2fs: support dat...
1306
1307
1308
1309
1310
1311
1312
1313
1314
  
  	for (--i; i >= 0; i--)
  		fscrypt_finalize_bounce_page(&cc->cpages[i]);
  	for (i = 0; i < cc->nr_cpages; i++) {
  		if (!cc->cpages[i])
  			continue;
  		f2fs_put_page(cc->cpages[i], 1);
  	}
  out_put_cic:
c68d6c883   Chao Yu   f2fs: compress: i...
1315
  	kmem_cache_free(cic_entry_slab, cic);
4c8ff7095   Chao Yu   f2fs: support dat...
1316
1317
1318
  out_put_dnode:
  	f2fs_put_dnode(&dn);
  out_unlock_op:
79963d967   Chao Yu   f2fs: shrink node...
1319
1320
1321
  	if (IS_NOQUOTA(inode))
  		up_read(&sbi->node_write);
  	else
435cbab95   Jaegeuk Kim   f2fs: fix quota_s...
1322
  		f2fs_unlock_op(sbi);
310830317   Chao Yu   f2fs: compress: i...
1323
1324
1325
  out_free:
  	page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
  	cc->cpages = NULL;
4c8ff7095   Chao Yu   f2fs: support dat...
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
  	return -EAGAIN;
  }
  
  void f2fs_compress_write_end_io(struct bio *bio, struct page *page)
  {
  	struct f2fs_sb_info *sbi = bio->bi_private;
  	struct compress_io_ctx *cic =
  			(struct compress_io_ctx *)page_private(page);
  	int i;
  
  	if (unlikely(bio->bi_status))
  		mapping_set_error(cic->inode->i_mapping, -EIO);
5e6bbde95   Chao Yu   f2fs: introduce m...
1338
  	f2fs_compress_free_page(page);
4c8ff7095   Chao Yu   f2fs: support dat...
1339
1340
  
  	dec_page_count(sbi, F2FS_WB_DATA);
e6c3948de   Chao Yu   f2fs: compress: u...
1341
  	if (atomic_dec_return(&cic->pending_pages))
4c8ff7095   Chao Yu   f2fs: support dat...
1342
1343
1344
1345
1346
1347
1348
  		return;
  
  	for (i = 0; i < cic->nr_rpages; i++) {
  		WARN_ON(!cic->rpages[i]);
  		clear_cold_data(cic->rpages[i]);
  		end_page_writeback(cic->rpages[i]);
  	}
310830317   Chao Yu   f2fs: compress: i...
1349
  	page_array_free(cic->inode, cic->rpages, cic->nr_rpages);
c68d6c883   Chao Yu   f2fs: compress: i...
1350
  	kmem_cache_free(cic_entry_slab, cic);
4c8ff7095   Chao Yu   f2fs: support dat...
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
  }
  
  static int f2fs_write_raw_pages(struct compress_ctx *cc,
  					int *submitted,
  					struct writeback_control *wbc,
  					enum iostat_type io_type)
  {
  	struct address_space *mapping = cc->inode->i_mapping;
  	int _submitted, compr_blocks, ret;
  	int i = -1, err = 0;
  
  	compr_blocks = f2fs_compressed_blocks(cc);
  	if (compr_blocks < 0) {
  		err = compr_blocks;
  		goto out_err;
  	}
  
  	for (i = 0; i < cc->cluster_size; i++) {
  		if (!cc->rpages[i])
  			continue;
  retry_write:
  		if (cc->rpages[i]->mapping != mapping) {
  			unlock_page(cc->rpages[i]);
  			continue;
  		}
  
  		BUG_ON(!PageLocked(cc->rpages[i]));
  
  		ret = f2fs_write_single_data_page(cc->rpages[i], &_submitted,
  						NULL, NULL, wbc, io_type,
  						compr_blocks);
  		if (ret) {
  			if (ret == AOP_WRITEPAGE_ACTIVATE) {
  				unlock_page(cc->rpages[i]);
  				ret = 0;
  			} else if (ret == -EAGAIN) {
466357dc9   Chao Yu   f2fs: fix potenti...
1387
1388
1389
1390
1391
1392
1393
1394
1395
  				/*
  				 * for quota file, just redirty left pages to
  				 * avoid deadlock caused by cluster update race
  				 * from foreground operation.
  				 */
  				if (IS_NOQUOTA(cc->inode)) {
  					err = 0;
  					goto out_err;
  				}
4c8ff7095   Chao Yu   f2fs: support dat...
1396
1397
  				ret = 0;
  				cond_resched();
5df7731f6   Chao Yu   f2fs: introduce D...
1398
1399
  				congestion_wait(BLK_RW_ASYNC,
  						DEFAULT_IO_TIMEOUT);
4c8ff7095   Chao Yu   f2fs: support dat...
1400
  				lock_page(cc->rpages[i]);
eb1353cfa   Chao Yu   f2fs: fix to chec...
1401
1402
1403
1404
1405
  
  				if (!PageDirty(cc->rpages[i])) {
  					unlock_page(cc->rpages[i]);
  					continue;
  				}
4c8ff7095   Chao Yu   f2fs: support dat...
1406
1407
1408
1409
  				clear_page_dirty_for_io(cc->rpages[i]);
  				goto retry_write;
  			}
  			err = ret;
466357dc9   Chao Yu   f2fs: fix potenti...
1410
  			goto out_err;
4c8ff7095   Chao Yu   f2fs: support dat...
1411
1412
1413
1414
1415
  		}
  
  		*submitted += _submitted;
  	}
  	return 0;
4c8ff7095   Chao Yu   f2fs: support dat...
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
  out_err:
  	for (++i; i < cc->cluster_size; i++) {
  		if (!cc->rpages[i])
  			continue;
  		redirty_page_for_writepage(wbc, cc->rpages[i]);
  		unlock_page(cc->rpages[i]);
  	}
  	return err;
  }
  
  int f2fs_write_multi_pages(struct compress_ctx *cc,
  					int *submitted,
  					struct writeback_control *wbc,
  					enum iostat_type io_type)
  {
4c8ff7095   Chao Yu   f2fs: support dat...
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
  	int err;
  
  	*submitted = 0;
  	if (cluster_may_compress(cc)) {
  		err = f2fs_compress_pages(cc);
  		if (err == -EAGAIN) {
  			goto write;
  		} else if (err) {
  			f2fs_put_rpages_wbc(cc, wbc, true, 1);
  			goto destroy_out;
  		}
  
  		err = f2fs_write_compressed_pages(cc, submitted,
  							wbc, io_type);
4c8ff7095   Chao Yu   f2fs: support dat...
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
  		if (!err)
  			return 0;
  		f2fs_bug_on(F2FS_I_SB(cc->inode), err != -EAGAIN);
  	}
  write:
  	f2fs_bug_on(F2FS_I_SB(cc->inode), *submitted);
  
  	err = f2fs_write_raw_pages(cc, submitted, wbc, io_type);
  	f2fs_put_rpages_wbc(cc, wbc, false, 0);
  destroy_out:
  	f2fs_destroy_compress_ctx(cc);
  	return err;
  }
  
  struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc)
  {
4c8ff7095   Chao Yu   f2fs: support dat...
1461
1462
1463
  	struct decompress_io_ctx *dic;
  	pgoff_t start_idx = start_idx_of_cluster(cc);
  	int i;
c68d6c883   Chao Yu   f2fs: compress: i...
1464
  	dic = kmem_cache_zalloc(dic_entry_slab, GFP_NOFS);
4c8ff7095   Chao Yu   f2fs: support dat...
1465
1466
  	if (!dic)
  		return ERR_PTR(-ENOMEM);
310830317   Chao Yu   f2fs: compress: i...
1467
  	dic->rpages = page_array_alloc(cc->inode, cc->cluster_size);
4c8ff7095   Chao Yu   f2fs: support dat...
1468
  	if (!dic->rpages) {
c68d6c883   Chao Yu   f2fs: compress: i...
1469
  		kmem_cache_free(dic_entry_slab, dic);
4c8ff7095   Chao Yu   f2fs: support dat...
1470
1471
1472
1473
1474
  		return ERR_PTR(-ENOMEM);
  	}
  
  	dic->magic = F2FS_COMPRESSED_PAGE_MAGIC;
  	dic->inode = cc->inode;
e6c3948de   Chao Yu   f2fs: compress: u...
1475
  	atomic_set(&dic->pending_pages, cc->nr_cpages);
4c8ff7095   Chao Yu   f2fs: support dat...
1476
1477
1478
1479
1480
1481
1482
1483
1484
  	dic->cluster_idx = cc->cluster_idx;
  	dic->cluster_size = cc->cluster_size;
  	dic->log_cluster_size = cc->log_cluster_size;
  	dic->nr_cpages = cc->nr_cpages;
  	dic->failed = false;
  
  	for (i = 0; i < dic->cluster_size; i++)
  		dic->rpages[i] = cc->rpages[i];
  	dic->nr_rpages = cc->cluster_size;
310830317   Chao Yu   f2fs: compress: i...
1485
  	dic->cpages = page_array_alloc(dic->inode, dic->nr_cpages);
4c8ff7095   Chao Yu   f2fs: support dat...
1486
1487
1488
1489
1490
  	if (!dic->cpages)
  		goto out_free;
  
  	for (i = 0; i < dic->nr_cpages; i++) {
  		struct page *page;
5e6bbde95   Chao Yu   f2fs: introduce m...
1491
  		page = f2fs_compress_alloc_page();
4c8ff7095   Chao Yu   f2fs: support dat...
1492
1493
1494
1495
  		if (!page)
  			goto out_free;
  
  		f2fs_set_compressed_page(page, cc->inode,
887347a09   Chao Yu   f2fs: clean up {c...
1496
  					start_idx + i + 1, dic);
4c8ff7095   Chao Yu   f2fs: support dat...
1497
1498
  		dic->cpages[i] = page;
  	}
4c8ff7095   Chao Yu   f2fs: support dat...
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
  	return dic;
  
  out_free:
  	f2fs_free_dic(dic);
  	return ERR_PTR(-ENOMEM);
  }
  
  void f2fs_free_dic(struct decompress_io_ctx *dic)
  {
  	int i;
  
  	if (dic->tpages) {
  		for (i = 0; i < dic->cluster_size; i++) {
  			if (dic->rpages[i])
  				continue;
8908e7531   Chao Yu   f2fs: fix to veri...
1514
1515
  			if (!dic->tpages[i])
  				continue;
5e6bbde95   Chao Yu   f2fs: introduce m...
1516
  			f2fs_compress_free_page(dic->tpages[i]);
4c8ff7095   Chao Yu   f2fs: support dat...
1517
  		}
310830317   Chao Yu   f2fs: compress: i...
1518
  		page_array_free(dic->inode, dic->tpages, dic->cluster_size);
4c8ff7095   Chao Yu   f2fs: support dat...
1519
1520
1521
1522
1523
1524
  	}
  
  	if (dic->cpages) {
  		for (i = 0; i < dic->nr_cpages; i++) {
  			if (!dic->cpages[i])
  				continue;
5e6bbde95   Chao Yu   f2fs: introduce m...
1525
  			f2fs_compress_free_page(dic->cpages[i]);
4c8ff7095   Chao Yu   f2fs: support dat...
1526
  		}
310830317   Chao Yu   f2fs: compress: i...
1527
  		page_array_free(dic->inode, dic->cpages, dic->nr_cpages);
4c8ff7095   Chao Yu   f2fs: support dat...
1528
  	}
310830317   Chao Yu   f2fs: compress: i...
1529
  	page_array_free(dic->inode, dic->rpages, dic->nr_rpages);
c68d6c883   Chao Yu   f2fs: compress: i...
1530
  	kmem_cache_free(dic_entry_slab, dic);
4c8ff7095   Chao Yu   f2fs: support dat...
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
  }
  
  void f2fs_decompress_end_io(struct page **rpages,
  			unsigned int cluster_size, bool err, bool verity)
  {
  	int i;
  
  	for (i = 0; i < cluster_size; i++) {
  		struct page *rpage = rpages[i];
  
  		if (!rpage)
  			continue;
23c51bed6   Chao Yu   f2fs: fix to clea...
1543
1544
1545
1546
1547
1548
  		if (err || PageError(rpage))
  			goto clear_uptodate;
  
  		if (!verity || fsverity_verify_page(rpage)) {
  			SetPageUptodate(rpage);
  			goto unlock;
4c8ff7095   Chao Yu   f2fs: support dat...
1549
  		}
23c51bed6   Chao Yu   f2fs: fix to clea...
1550
1551
1552
1553
  clear_uptodate:
  		ClearPageUptodate(rpage);
  		ClearPageError(rpage);
  unlock:
4c8ff7095   Chao Yu   f2fs: support dat...
1554
1555
1556
  		unlock_page(rpage);
  	}
  }
310830317   Chao Yu   f2fs: compress: i...
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
  
  int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi)
  {
  	dev_t dev = sbi->sb->s_bdev->bd_dev;
  	char slab_name[32];
  
  	sprintf(slab_name, "f2fs_page_array_entry-%u:%u", MAJOR(dev), MINOR(dev));
  
  	sbi->page_array_slab_size = sizeof(struct page *) <<
  					F2FS_OPTION(sbi).compress_log_size;
  
  	sbi->page_array_slab = f2fs_kmem_cache_create(slab_name,
  					sbi->page_array_slab_size);
  	if (!sbi->page_array_slab)
  		return -ENOMEM;
  	return 0;
  }
  
  void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi)
  {
  	kmem_cache_destroy(sbi->page_array_slab);
  }
c68d6c883   Chao Yu   f2fs: compress: i...
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
  
  static int __init f2fs_init_cic_cache(void)
  {
  	cic_entry_slab = f2fs_kmem_cache_create("f2fs_cic_entry",
  					sizeof(struct compress_io_ctx));
  	if (!cic_entry_slab)
  		return -ENOMEM;
  	return 0;
  }
  
  static void f2fs_destroy_cic_cache(void)
  {
  	kmem_cache_destroy(cic_entry_slab);
  }
  
  static int __init f2fs_init_dic_cache(void)
  {
  	dic_entry_slab = f2fs_kmem_cache_create("f2fs_dic_entry",
  					sizeof(struct decompress_io_ctx));
  	if (!dic_entry_slab)
  		return -ENOMEM;
  	return 0;
  }
  
  static void f2fs_destroy_dic_cache(void)
  {
  	kmem_cache_destroy(dic_entry_slab);
  }
  
  int __init f2fs_init_compress_cache(void)
  {
  	int err;
  
  	err = f2fs_init_cic_cache();
  	if (err)
  		goto out;
  	err = f2fs_init_dic_cache();
  	if (err)
  		goto free_cic;
  	return 0;
  free_cic:
  	f2fs_destroy_cic_cache();
  out:
  	return -ENOMEM;
  }
  
  void f2fs_destroy_compress_cache(void)
  {
  	f2fs_destroy_dic_cache();
  	f2fs_destroy_cic_cache();
  }