• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * f2fs compress support
4  *
5  * Copyright (c) 2019 Chao Yu <chao@kernel.org>
6  */
7 
8 #include <linux/fs.h>
9 #include <linux/f2fs_fs.h>
10 #include <linux/writeback.h>
11 #include <linux/backing-dev.h>
12 #include <linux/lzo.h>
13 #include <linux/lz4.h>
14 #include <linux/zstd.h>
15 #include <linux/pagevec.h>
16 
17 #include "f2fs.h"
18 #include "node.h"
19 #include "segment.h"
20 #include <trace/events/f2fs.h>
21 
22 static struct kmem_cache *cic_entry_slab;
23 static struct kmem_cache *dic_entry_slab;
24 
page_array_alloc(struct inode * inode,int nr)25 static void *page_array_alloc(struct inode *inode, int nr)
26 {
27 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
28 	unsigned int size = sizeof(struct page *) * nr;
29 
30 	if (likely(size <= sbi->page_array_slab_size))
31 		return f2fs_kmem_cache_alloc(sbi->page_array_slab,
32 					GFP_F2FS_ZERO, false, F2FS_I_SB(inode));
33 	return f2fs_kzalloc(sbi, size, GFP_NOFS);
34 }
35 
page_array_free(struct inode * inode,void * pages,int nr)36 static void page_array_free(struct inode *inode, void *pages, int nr)
37 {
38 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
39 	unsigned int size = sizeof(struct page *) * nr;
40 
41 	if (!pages)
42 		return;
43 
44 	if (likely(size <= sbi->page_array_slab_size))
45 		kmem_cache_free(sbi->page_array_slab, pages);
46 	else
47 		kfree(pages);
48 }
49 
50 struct f2fs_compress_ops {
51 	int (*init_compress_ctx)(struct compress_ctx *cc);
52 	void (*destroy_compress_ctx)(struct compress_ctx *cc);
53 	int (*compress_pages)(struct compress_ctx *cc);
54 	int (*init_decompress_ctx)(struct decompress_io_ctx *dic);
55 	void (*destroy_decompress_ctx)(struct decompress_io_ctx *dic);
56 	int (*decompress_pages)(struct decompress_io_ctx *dic);
57 };
58 
offset_in_cluster(struct compress_ctx * cc,pgoff_t index)59 static unsigned int offset_in_cluster(struct compress_ctx *cc, pgoff_t index)
60 {
61 	return index & (cc->cluster_size - 1);
62 }
63 
cluster_idx(struct compress_ctx * cc,pgoff_t index)64 static pgoff_t cluster_idx(struct compress_ctx *cc, pgoff_t index)
65 {
66 	return index >> cc->log_cluster_size;
67 }
68 
start_idx_of_cluster(struct compress_ctx * cc)69 static pgoff_t start_idx_of_cluster(struct compress_ctx *cc)
70 {
71 	return cc->cluster_idx << cc->log_cluster_size;
72 }
73 
f2fs_is_compressed_page(struct page * page)74 bool f2fs_is_compressed_page(struct page *page)
75 {
76 	if (!PagePrivate(page))
77 		return false;
78 	if (!page_private(page))
79 		return false;
80 	if (page_private_nonpointer(page))
81 		return false;
82 
83 	f2fs_bug_on(F2FS_M_SB(page->mapping),
84 		*((u32 *)page_private(page)) != F2FS_COMPRESSED_PAGE_MAGIC);
85 	return true;
86 }
87 
f2fs_set_compressed_page(struct page * page,struct inode * inode,pgoff_t index,void * data)88 static void f2fs_set_compressed_page(struct page *page,
89 		struct inode *inode, pgoff_t index, void *data)
90 {
91 	attach_page_private(page, (void *)data);
92 
93 	/* i_crypto_info and iv index */
94 	page->index = index;
95 	page->mapping = inode->i_mapping;
96 }
97 
f2fs_drop_rpages(struct compress_ctx * cc,int len,bool unlock)98 static void f2fs_drop_rpages(struct compress_ctx *cc, int len, bool unlock)
99 {
100 	int i;
101 
102 	for (i = 0; i < len; i++) {
103 		if (!cc->rpages[i])
104 			continue;
105 		if (unlock)
106 			unlock_page(cc->rpages[i]);
107 		else
108 			put_page(cc->rpages[i]);
109 	}
110 }
111 
f2fs_put_rpages(struct compress_ctx * cc)112 static void f2fs_put_rpages(struct compress_ctx *cc)
113 {
114 	f2fs_drop_rpages(cc, cc->cluster_size, false);
115 }
116 
f2fs_unlock_rpages(struct compress_ctx * cc,int len)117 static void f2fs_unlock_rpages(struct compress_ctx *cc, int len)
118 {
119 	f2fs_drop_rpages(cc, len, true);
120 }
121 
f2fs_put_rpages_wbc(struct compress_ctx * cc,struct writeback_control * wbc,bool redirty,int unlock)122 static void f2fs_put_rpages_wbc(struct compress_ctx *cc,
123 		struct writeback_control *wbc, bool redirty, int unlock)
124 {
125 	unsigned int i;
126 
127 	for (i = 0; i < cc->cluster_size; i++) {
128 		if (!cc->rpages[i])
129 			continue;
130 		if (redirty)
131 			redirty_page_for_writepage(wbc, cc->rpages[i]);
132 		f2fs_put_page(cc->rpages[i], unlock);
133 	}
134 }
135 
f2fs_compress_control_page(struct page * page)136 struct page *f2fs_compress_control_page(struct page *page)
137 {
138 	return ((struct compress_io_ctx *)page_private(page))->rpages[0];
139 }
140 
f2fs_init_compress_ctx(struct compress_ctx * cc)141 int f2fs_init_compress_ctx(struct compress_ctx *cc)
142 {
143 	if (cc->rpages)
144 		return 0;
145 
146 	cc->rpages = page_array_alloc(cc->inode, cc->cluster_size);
147 	return cc->rpages ? 0 : -ENOMEM;
148 }
149 
f2fs_destroy_compress_ctx(struct compress_ctx * cc,bool reuse)150 void f2fs_destroy_compress_ctx(struct compress_ctx *cc, bool reuse)
151 {
152 	page_array_free(cc->inode, cc->rpages, cc->cluster_size);
153 	cc->rpages = NULL;
154 	cc->nr_rpages = 0;
155 	cc->nr_cpages = 0;
156 	cc->valid_nr_cpages = 0;
157 	if (!reuse)
158 		cc->cluster_idx = NULL_CLUSTER;
159 }
160 
f2fs_compress_ctx_add_page(struct compress_ctx * cc,struct page * page)161 void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct page *page)
162 {
163 	unsigned int cluster_ofs;
164 
165 	if (!f2fs_cluster_can_merge_page(cc, page->index))
166 		f2fs_bug_on(F2FS_I_SB(cc->inode), 1);
167 
168 	cluster_ofs = offset_in_cluster(cc, page->index);
169 	cc->rpages[cluster_ofs] = page;
170 	cc->nr_rpages++;
171 	cc->cluster_idx = cluster_idx(cc, page->index);
172 }
173 
174 #ifdef CONFIG_F2FS_FS_LZO
lzo_init_compress_ctx(struct compress_ctx * cc)175 static int lzo_init_compress_ctx(struct compress_ctx *cc)
176 {
177 	cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
178 				LZO1X_MEM_COMPRESS, GFP_NOFS);
179 	if (!cc->private)
180 		return -ENOMEM;
181 
182 	cc->clen = lzo1x_worst_compress(PAGE_SIZE << cc->log_cluster_size);
183 	return 0;
184 }
185 
lzo_destroy_compress_ctx(struct compress_ctx * cc)186 static void lzo_destroy_compress_ctx(struct compress_ctx *cc)
187 {
188 	kvfree(cc->private);
189 	cc->private = NULL;
190 }
191 
lzo_compress_pages(struct compress_ctx * cc)192 static int lzo_compress_pages(struct compress_ctx *cc)
193 {
194 	int ret;
195 
196 	ret = lzo1x_1_compress(cc->rbuf, cc->rlen, cc->cbuf->cdata,
197 					&cc->clen, cc->private);
198 	if (ret != LZO_E_OK) {
199 		printk_ratelimited("%sF2FS-fs (%s): lzo compress failed, ret:%d\n",
200 				KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id, ret);
201 		return -EIO;
202 	}
203 	return 0;
204 }
205 
lzo_decompress_pages(struct decompress_io_ctx * dic)206 static int lzo_decompress_pages(struct decompress_io_ctx *dic)
207 {
208 	int ret;
209 
210 	ret = lzo1x_decompress_safe(dic->cbuf->cdata, dic->clen,
211 						dic->rbuf, &dic->rlen);
212 	if (ret != LZO_E_OK) {
213 		printk_ratelimited("%sF2FS-fs (%s): lzo decompress failed, ret:%d\n",
214 				KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id, ret);
215 		return -EIO;
216 	}
217 
218 	if (dic->rlen != PAGE_SIZE << dic->log_cluster_size) {
219 		printk_ratelimited("%sF2FS-fs (%s): lzo invalid rlen:%zu, "
220 					"expected:%lu\n", KERN_ERR,
221 					F2FS_I_SB(dic->inode)->sb->s_id,
222 					dic->rlen,
223 					PAGE_SIZE << dic->log_cluster_size);
224 		return -EIO;
225 	}
226 	return 0;
227 }
228 
229 static const struct f2fs_compress_ops f2fs_lzo_ops = {
230 	.init_compress_ctx	= lzo_init_compress_ctx,
231 	.destroy_compress_ctx	= lzo_destroy_compress_ctx,
232 	.compress_pages		= lzo_compress_pages,
233 	.decompress_pages	= lzo_decompress_pages,
234 };
235 #endif
236 
237 #ifdef CONFIG_F2FS_FS_LZ4
lz4_init_compress_ctx(struct compress_ctx * cc)238 static int lz4_init_compress_ctx(struct compress_ctx *cc)
239 {
240 	unsigned int size = LZ4_MEM_COMPRESS;
241 
242 #ifdef CONFIG_F2FS_FS_LZ4HC
243 	if (F2FS_I(cc->inode)->i_compress_level)
244 		size = LZ4HC_MEM_COMPRESS;
245 #endif
246 
247 	cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode), size, GFP_NOFS);
248 	if (!cc->private)
249 		return -ENOMEM;
250 
251 	/*
252 	 * we do not change cc->clen to LZ4_compressBound(inputsize) to
253 	 * adapt worst compress case, because lz4 compressor can handle
254 	 * output budget properly.
255 	 */
256 	cc->clen = cc->rlen - PAGE_SIZE - COMPRESS_HEADER_SIZE;
257 	return 0;
258 }
259 
lz4_destroy_compress_ctx(struct compress_ctx * cc)260 static void lz4_destroy_compress_ctx(struct compress_ctx *cc)
261 {
262 	kvfree(cc->private);
263 	cc->private = NULL;
264 }
265 
lz4_compress_pages(struct compress_ctx * cc)266 static int lz4_compress_pages(struct compress_ctx *cc)
267 {
268 	int len = -EINVAL;
269 	unsigned char level = F2FS_I(cc->inode)->i_compress_level;
270 
271 	if (!level)
272 		len = LZ4_compress_default(cc->rbuf, cc->cbuf->cdata, cc->rlen,
273 						cc->clen, cc->private);
274 #ifdef CONFIG_F2FS_FS_LZ4HC
275 	else
276 		len = LZ4_compress_HC(cc->rbuf, cc->cbuf->cdata, cc->rlen,
277 					cc->clen, level, cc->private);
278 #endif
279 	if (len < 0)
280 		return len;
281 	if (!len)
282 		return -EAGAIN;
283 
284 	cc->clen = len;
285 	return 0;
286 }
287 
lz4_decompress_pages(struct decompress_io_ctx * dic)288 static int lz4_decompress_pages(struct decompress_io_ctx *dic)
289 {
290 	int ret;
291 
292 	ret = LZ4_decompress_safe(dic->cbuf->cdata, dic->rbuf,
293 						dic->clen, dic->rlen);
294 	if (ret < 0) {
295 		printk_ratelimited("%sF2FS-fs (%s): lz4 decompress failed, ret:%d\n",
296 				KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id, ret);
297 		return -EIO;
298 	}
299 
300 	if (ret != PAGE_SIZE << dic->log_cluster_size) {
301 		printk_ratelimited("%sF2FS-fs (%s): lz4 invalid ret:%d, "
302 					"expected:%lu\n", KERN_ERR,
303 					F2FS_I_SB(dic->inode)->sb->s_id, ret,
304 					PAGE_SIZE << dic->log_cluster_size);
305 		return -EIO;
306 	}
307 	return 0;
308 }
309 
310 static const struct f2fs_compress_ops f2fs_lz4_ops = {
311 	.init_compress_ctx	= lz4_init_compress_ctx,
312 	.destroy_compress_ctx	= lz4_destroy_compress_ctx,
313 	.compress_pages		= lz4_compress_pages,
314 	.decompress_pages	= lz4_decompress_pages,
315 };
316 #endif
317 
318 #ifdef CONFIG_F2FS_FS_ZSTD
319 #define F2FS_ZSTD_DEFAULT_CLEVEL	1
320 
zstd_init_compress_ctx(struct compress_ctx * cc)321 static int zstd_init_compress_ctx(struct compress_ctx *cc)
322 {
323 	ZSTD_parameters params;
324 	ZSTD_CStream *stream;
325 	void *workspace;
326 	unsigned int workspace_size;
327 	unsigned char level = F2FS_I(cc->inode)->i_compress_level;
328 
329 	if (!level)
330 		level = F2FS_ZSTD_DEFAULT_CLEVEL;
331 
332 	params = ZSTD_getParams(level, cc->rlen, 0);
333 	workspace_size = ZSTD_CStreamWorkspaceBound(params.cParams);
334 
335 	workspace = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
336 					workspace_size, GFP_NOFS);
337 	if (!workspace)
338 		return -ENOMEM;
339 
340 	stream = ZSTD_initCStream(params, 0, workspace, workspace_size);
341 	if (!stream) {
342 		printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_initCStream failed\n",
343 				KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
344 				__func__);
345 		kvfree(workspace);
346 		return -EIO;
347 	}
348 
349 	cc->private = workspace;
350 	cc->private2 = stream;
351 
352 	cc->clen = cc->rlen - PAGE_SIZE - COMPRESS_HEADER_SIZE;
353 	return 0;
354 }
355 
zstd_destroy_compress_ctx(struct compress_ctx * cc)356 static void zstd_destroy_compress_ctx(struct compress_ctx *cc)
357 {
358 	kvfree(cc->private);
359 	cc->private = NULL;
360 	cc->private2 = NULL;
361 }
362 
zstd_compress_pages(struct compress_ctx * cc)363 static int zstd_compress_pages(struct compress_ctx *cc)
364 {
365 	ZSTD_CStream *stream = cc->private2;
366 	ZSTD_inBuffer inbuf;
367 	ZSTD_outBuffer outbuf;
368 	int src_size = cc->rlen;
369 	int dst_size = src_size - PAGE_SIZE - COMPRESS_HEADER_SIZE;
370 	int ret;
371 
372 	inbuf.pos = 0;
373 	inbuf.src = cc->rbuf;
374 	inbuf.size = src_size;
375 
376 	outbuf.pos = 0;
377 	outbuf.dst = cc->cbuf->cdata;
378 	outbuf.size = dst_size;
379 
380 	ret = ZSTD_compressStream(stream, &outbuf, &inbuf);
381 	if (ZSTD_isError(ret)) {
382 		printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_compressStream failed, ret: %d\n",
383 				KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
384 				__func__, ZSTD_getErrorCode(ret));
385 		return -EIO;
386 	}
387 
388 	ret = ZSTD_endStream(stream, &outbuf);
389 	if (ZSTD_isError(ret)) {
390 		printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_endStream returned %d\n",
391 				KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
392 				__func__, ZSTD_getErrorCode(ret));
393 		return -EIO;
394 	}
395 
396 	/*
397 	 * there is compressed data remained in intermediate buffer due to
398 	 * no more space in cbuf.cdata
399 	 */
400 	if (ret)
401 		return -EAGAIN;
402 
403 	cc->clen = outbuf.pos;
404 	return 0;
405 }
406 
zstd_init_decompress_ctx(struct decompress_io_ctx * dic)407 static int zstd_init_decompress_ctx(struct decompress_io_ctx *dic)
408 {
409 	ZSTD_DStream *stream;
410 	void *workspace;
411 	unsigned int workspace_size;
412 	unsigned int max_window_size =
413 			MAX_COMPRESS_WINDOW_SIZE(dic->log_cluster_size);
414 
415 	workspace_size = ZSTD_DStreamWorkspaceBound(max_window_size);
416 
417 	workspace = f2fs_kvmalloc(F2FS_I_SB(dic->inode),
418 					workspace_size, GFP_NOFS);
419 	if (!workspace)
420 		return -ENOMEM;
421 
422 	stream = ZSTD_initDStream(max_window_size, workspace, workspace_size);
423 	if (!stream) {
424 		printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_initDStream failed\n",
425 				KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id,
426 				__func__);
427 		kvfree(workspace);
428 		return -EIO;
429 	}
430 
431 	dic->private = workspace;
432 	dic->private2 = stream;
433 
434 	return 0;
435 }
436 
zstd_destroy_decompress_ctx(struct decompress_io_ctx * dic)437 static void zstd_destroy_decompress_ctx(struct decompress_io_ctx *dic)
438 {
439 	kvfree(dic->private);
440 	dic->private = NULL;
441 	dic->private2 = NULL;
442 }
443 
zstd_decompress_pages(struct decompress_io_ctx * dic)444 static int zstd_decompress_pages(struct decompress_io_ctx *dic)
445 {
446 	ZSTD_DStream *stream = dic->private2;
447 	ZSTD_inBuffer inbuf;
448 	ZSTD_outBuffer outbuf;
449 	int ret;
450 
451 	inbuf.pos = 0;
452 	inbuf.src = dic->cbuf->cdata;
453 	inbuf.size = dic->clen;
454 
455 	outbuf.pos = 0;
456 	outbuf.dst = dic->rbuf;
457 	outbuf.size = dic->rlen;
458 
459 	ret = ZSTD_decompressStream(stream, &outbuf, &inbuf);
460 	if (ZSTD_isError(ret)) {
461 		printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_compressStream failed, ret: %d\n",
462 				KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id,
463 				__func__, ZSTD_getErrorCode(ret));
464 		return -EIO;
465 	}
466 
467 	if (dic->rlen != outbuf.pos) {
468 		printk_ratelimited("%sF2FS-fs (%s): %s ZSTD invalid rlen:%zu, "
469 				"expected:%lu\n", KERN_ERR,
470 				F2FS_I_SB(dic->inode)->sb->s_id,
471 				__func__, dic->rlen,
472 				PAGE_SIZE << dic->log_cluster_size);
473 		return -EIO;
474 	}
475 
476 	return 0;
477 }
478 
479 static const struct f2fs_compress_ops f2fs_zstd_ops = {
480 	.init_compress_ctx	= zstd_init_compress_ctx,
481 	.destroy_compress_ctx	= zstd_destroy_compress_ctx,
482 	.compress_pages		= zstd_compress_pages,
483 	.init_decompress_ctx	= zstd_init_decompress_ctx,
484 	.destroy_decompress_ctx	= zstd_destroy_decompress_ctx,
485 	.decompress_pages	= zstd_decompress_pages,
486 };
487 #endif
488 
489 #ifdef CONFIG_F2FS_FS_LZO
490 #ifdef CONFIG_F2FS_FS_LZORLE
lzorle_compress_pages(struct compress_ctx * cc)491 static int lzorle_compress_pages(struct compress_ctx *cc)
492 {
493 	int ret;
494 
495 	ret = lzorle1x_1_compress(cc->rbuf, cc->rlen, cc->cbuf->cdata,
496 					&cc->clen, cc->private);
497 	if (ret != LZO_E_OK) {
498 		printk_ratelimited("%sF2FS-fs (%s): lzo-rle compress failed, ret:%d\n",
499 				KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id, ret);
500 		return -EIO;
501 	}
502 	return 0;
503 }
504 
505 static const struct f2fs_compress_ops f2fs_lzorle_ops = {
506 	.init_compress_ctx	= lzo_init_compress_ctx,
507 	.destroy_compress_ctx	= lzo_destroy_compress_ctx,
508 	.compress_pages		= lzorle_compress_pages,
509 	.decompress_pages	= lzo_decompress_pages,
510 };
511 #endif
512 #endif
513 
514 static const struct f2fs_compress_ops *f2fs_cops[COMPRESS_MAX] = {
515 #ifdef CONFIG_F2FS_FS_LZO
516 	&f2fs_lzo_ops,
517 #else
518 	NULL,
519 #endif
520 #ifdef CONFIG_F2FS_FS_LZ4
521 	&f2fs_lz4_ops,
522 #else
523 	NULL,
524 #endif
525 #ifdef CONFIG_F2FS_FS_ZSTD
526 	&f2fs_zstd_ops,
527 #else
528 	NULL,
529 #endif
530 #if defined(CONFIG_F2FS_FS_LZO) && defined(CONFIG_F2FS_FS_LZORLE)
531 	&f2fs_lzorle_ops,
532 #else
533 	NULL,
534 #endif
535 };
536 
f2fs_is_compress_backend_ready(struct inode * inode)537 bool f2fs_is_compress_backend_ready(struct inode *inode)
538 {
539 	if (!f2fs_compressed_file(inode))
540 		return true;
541 	return f2fs_cops[F2FS_I(inode)->i_compress_algorithm];
542 }
543 
544 static mempool_t *compress_page_pool;
545 static int num_compress_pages = 512;
546 module_param(num_compress_pages, uint, 0444);
547 MODULE_PARM_DESC(num_compress_pages,
548 		"Number of intermediate compress pages to preallocate");
549 
f2fs_init_compress_mempool(void)550 int __init f2fs_init_compress_mempool(void)
551 {
552 	compress_page_pool = mempool_create_page_pool(num_compress_pages, 0);
553 	return compress_page_pool ? 0 : -ENOMEM;
554 }
555 
f2fs_destroy_compress_mempool(void)556 void f2fs_destroy_compress_mempool(void)
557 {
558 	mempool_destroy(compress_page_pool);
559 }
560 
f2fs_compress_alloc_page(void)561 static struct page *f2fs_compress_alloc_page(void)
562 {
563 	struct page *page;
564 
565 	page = mempool_alloc(compress_page_pool, GFP_NOFS);
566 	lock_page(page);
567 
568 	return page;
569 }
570 
f2fs_compress_free_page(struct page * page)571 static void f2fs_compress_free_page(struct page *page)
572 {
573 	if (!page)
574 		return;
575 	detach_page_private(page);
576 	page->mapping = NULL;
577 	unlock_page(page);
578 	mempool_free(page, compress_page_pool);
579 }
580 
581 #define MAX_VMAP_RETRIES	3
582 
f2fs_vmap(struct page ** pages,unsigned int count)583 static void *f2fs_vmap(struct page **pages, unsigned int count)
584 {
585 	int i;
586 	void *buf = NULL;
587 
588 	for (i = 0; i < MAX_VMAP_RETRIES; i++) {
589 		buf = vm_map_ram(pages, count, -1);
590 		if (buf)
591 			break;
592 		vm_unmap_aliases();
593 	}
594 	return buf;
595 }
596 
f2fs_compress_pages(struct compress_ctx * cc)597 static int f2fs_compress_pages(struct compress_ctx *cc)
598 {
599 	struct f2fs_inode_info *fi = F2FS_I(cc->inode);
600 	const struct f2fs_compress_ops *cops =
601 				f2fs_cops[fi->i_compress_algorithm];
602 	unsigned int max_len, new_nr_cpages;
603 	u32 chksum = 0;
604 	int i, ret;
605 
606 	trace_f2fs_compress_pages_start(cc->inode, cc->cluster_idx,
607 				cc->cluster_size, fi->i_compress_algorithm);
608 
609 	if (cops->init_compress_ctx) {
610 		ret = cops->init_compress_ctx(cc);
611 		if (ret)
612 			goto out;
613 	}
614 
615 	max_len = COMPRESS_HEADER_SIZE + cc->clen;
616 	cc->nr_cpages = DIV_ROUND_UP(max_len, PAGE_SIZE);
617 	cc->valid_nr_cpages = cc->nr_cpages;
618 
619 	cc->cpages = page_array_alloc(cc->inode, cc->nr_cpages);
620 	if (!cc->cpages) {
621 		ret = -ENOMEM;
622 		goto destroy_compress_ctx;
623 	}
624 
625 	for (i = 0; i < cc->nr_cpages; i++) {
626 		cc->cpages[i] = f2fs_compress_alloc_page();
627 		if (!cc->cpages[i]) {
628 			ret = -ENOMEM;
629 			goto out_free_cpages;
630 		}
631 	}
632 
633 	cc->rbuf = f2fs_vmap(cc->rpages, cc->cluster_size);
634 	if (!cc->rbuf) {
635 		ret = -ENOMEM;
636 		goto out_free_cpages;
637 	}
638 
639 	cc->cbuf = f2fs_vmap(cc->cpages, cc->nr_cpages);
640 	if (!cc->cbuf) {
641 		ret = -ENOMEM;
642 		goto out_vunmap_rbuf;
643 	}
644 
645 	ret = cops->compress_pages(cc);
646 	if (ret)
647 		goto out_vunmap_cbuf;
648 
649 	max_len = PAGE_SIZE * (cc->cluster_size - 1) - COMPRESS_HEADER_SIZE;
650 
651 	if (cc->clen > max_len) {
652 		ret = -EAGAIN;
653 		goto out_vunmap_cbuf;
654 	}
655 
656 	cc->cbuf->clen = cpu_to_le32(cc->clen);
657 
658 	if (fi->i_compress_flag & BIT(COMPRESS_CHKSUM))
659 		chksum = f2fs_crc32(F2FS_I_SB(cc->inode),
660 					cc->cbuf->cdata, cc->clen);
661 	cc->cbuf->chksum = cpu_to_le32(chksum);
662 
663 	for (i = 0; i < COMPRESS_DATA_RESERVED_SIZE; i++)
664 		cc->cbuf->reserved[i] = cpu_to_le32(0);
665 
666 	new_nr_cpages = DIV_ROUND_UP(cc->clen + COMPRESS_HEADER_SIZE, PAGE_SIZE);
667 
668 	/* zero out any unused part of the last page */
669 	memset(&cc->cbuf->cdata[cc->clen], 0,
670 			(new_nr_cpages * PAGE_SIZE) -
671 			(cc->clen + COMPRESS_HEADER_SIZE));
672 
673 	vm_unmap_ram(cc->cbuf, cc->nr_cpages);
674 	vm_unmap_ram(cc->rbuf, cc->cluster_size);
675 
676 	for (i = new_nr_cpages; i < cc->nr_cpages; i++) {
677 		f2fs_compress_free_page(cc->cpages[i]);
678 		cc->cpages[i] = NULL;
679 	}
680 
681 	if (cops->destroy_compress_ctx)
682 		cops->destroy_compress_ctx(cc);
683 
684 	cc->valid_nr_cpages = new_nr_cpages;
685 
686 	trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
687 							cc->clen, ret);
688 	return 0;
689 
690 out_vunmap_cbuf:
691 	vm_unmap_ram(cc->cbuf, cc->nr_cpages);
692 out_vunmap_rbuf:
693 	vm_unmap_ram(cc->rbuf, cc->cluster_size);
694 out_free_cpages:
695 	for (i = 0; i < cc->nr_cpages; i++) {
696 		if (cc->cpages[i])
697 			f2fs_compress_free_page(cc->cpages[i]);
698 	}
699 	page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
700 	cc->cpages = NULL;
701 destroy_compress_ctx:
702 	if (cops->destroy_compress_ctx)
703 		cops->destroy_compress_ctx(cc);
704 out:
705 	trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
706 							cc->clen, ret);
707 	return ret;
708 }
709 
710 static int f2fs_prepare_decomp_mem(struct decompress_io_ctx *dic,
711 		bool pre_alloc);
712 static void f2fs_release_decomp_mem(struct decompress_io_ctx *dic,
713 		bool bypass_destroy_callback, bool pre_alloc);
714 
f2fs_decompress_cluster(struct decompress_io_ctx * dic,bool in_task)715 void f2fs_decompress_cluster(struct decompress_io_ctx *dic, bool in_task)
716 {
717 	struct f2fs_sb_info *sbi = F2FS_I_SB(dic->inode);
718 	struct f2fs_inode_info *fi = F2FS_I(dic->inode);
719 	const struct f2fs_compress_ops *cops =
720 			f2fs_cops[fi->i_compress_algorithm];
721 	bool bypass_callback = false;
722 	int ret;
723 
724 	trace_f2fs_decompress_pages_start(dic->inode, dic->cluster_idx,
725 				dic->cluster_size, fi->i_compress_algorithm);
726 
727 	if (dic->failed) {
728 		ret = -EIO;
729 		goto out_end_io;
730 	}
731 
732 	ret = f2fs_prepare_decomp_mem(dic, false);
733 	if (ret) {
734 		bypass_callback = true;
735 		goto out_release;
736 	}
737 
738 	dic->clen = le32_to_cpu(dic->cbuf->clen);
739 	dic->rlen = PAGE_SIZE << dic->log_cluster_size;
740 
741 	if (dic->clen > PAGE_SIZE * dic->nr_cpages - COMPRESS_HEADER_SIZE) {
742 		ret = -EFSCORRUPTED;
743 
744 		/* Avoid f2fs_commit_super in irq context */
745 		if (!in_task)
746 			f2fs_save_errors(sbi, ERROR_FAIL_DECOMPRESSION);
747 		else
748 			f2fs_handle_error(sbi, ERROR_FAIL_DECOMPRESSION);
749 		goto out_release;
750 	}
751 
752 	ret = cops->decompress_pages(dic);
753 
754 	if (!ret && (fi->i_compress_flag & BIT(COMPRESS_CHKSUM))) {
755 		u32 provided = le32_to_cpu(dic->cbuf->chksum);
756 		u32 calculated = f2fs_crc32(sbi, dic->cbuf->cdata, dic->clen);
757 
758 		if (provided != calculated) {
759 			if (!is_inode_flag_set(dic->inode, FI_COMPRESS_CORRUPT)) {
760 				set_inode_flag(dic->inode, FI_COMPRESS_CORRUPT);
761 				printk_ratelimited(
762 					"%sF2FS-fs (%s): checksum invalid, nid = %lu, %x vs %x",
763 					KERN_INFO, sbi->sb->s_id, dic->inode->i_ino,
764 					provided, calculated);
765 			}
766 			set_sbi_flag(sbi, SBI_NEED_FSCK);
767 		}
768 	}
769 
770 out_release:
771 	f2fs_release_decomp_mem(dic, bypass_callback, false);
772 
773 out_end_io:
774 	trace_f2fs_decompress_pages_end(dic->inode, dic->cluster_idx,
775 							dic->clen, ret);
776 	f2fs_decompress_end_io(dic, ret, in_task);
777 }
778 
779 /*
780  * This is called when a page of a compressed cluster has been read from disk
781  * (or failed to be read from disk).  It checks whether this page was the last
782  * page being waited on in the cluster, and if so, it decompresses the cluster
783  * (or in the case of a failure, cleans up without actually decompressing).
784  */
f2fs_end_read_compressed_page(struct page * page,bool failed,block_t blkaddr,bool in_task)785 void f2fs_end_read_compressed_page(struct page *page, bool failed,
786 		block_t blkaddr, bool in_task)
787 {
788 	struct decompress_io_ctx *dic =
789 			(struct decompress_io_ctx *)page_private(page);
790 	struct f2fs_sb_info *sbi = F2FS_I_SB(dic->inode);
791 
792 	dec_page_count(sbi, F2FS_RD_DATA);
793 
794 	if (failed)
795 		WRITE_ONCE(dic->failed, true);
796 	else if (blkaddr && in_task)
797 		f2fs_cache_compressed_page(sbi, page,
798 					dic->inode->i_ino, blkaddr);
799 
800 	if (atomic_dec_and_test(&dic->remaining_pages))
801 		f2fs_decompress_cluster(dic, in_task);
802 }
803 
is_page_in_cluster(struct compress_ctx * cc,pgoff_t index)804 static bool is_page_in_cluster(struct compress_ctx *cc, pgoff_t index)
805 {
806 	if (cc->cluster_idx == NULL_CLUSTER)
807 		return true;
808 	return cc->cluster_idx == cluster_idx(cc, index);
809 }
810 
f2fs_cluster_is_empty(struct compress_ctx * cc)811 bool f2fs_cluster_is_empty(struct compress_ctx *cc)
812 {
813 	return cc->nr_rpages == 0;
814 }
815 
f2fs_cluster_is_full(struct compress_ctx * cc)816 static bool f2fs_cluster_is_full(struct compress_ctx *cc)
817 {
818 	return cc->cluster_size == cc->nr_rpages;
819 }
820 
f2fs_cluster_can_merge_page(struct compress_ctx * cc,pgoff_t index)821 bool f2fs_cluster_can_merge_page(struct compress_ctx *cc, pgoff_t index)
822 {
823 	if (f2fs_cluster_is_empty(cc))
824 		return true;
825 	return is_page_in_cluster(cc, index);
826 }
827 
f2fs_all_cluster_page_ready(struct compress_ctx * cc,struct page ** pages,int index,int nr_pages,bool uptodate)828 bool f2fs_all_cluster_page_ready(struct compress_ctx *cc, struct page **pages,
829 				int index, int nr_pages, bool uptodate)
830 {
831 	unsigned long pgidx = pages[index]->index;
832 	int i = uptodate ? 0 : 1;
833 
834 	/*
835 	 * when uptodate set to true, try to check all pages in cluster is
836 	 * uptodate or not.
837 	 */
838 	if (uptodate && (pgidx % cc->cluster_size))
839 		return false;
840 
841 	if (nr_pages - index < cc->cluster_size)
842 		return false;
843 
844 	for (; i < cc->cluster_size; i++) {
845 		if (pages[index + i]->index != pgidx + i)
846 			return false;
847 		if (uptodate && !PageUptodate(pages[index + i]))
848 			return false;
849 	}
850 
851 	return true;
852 }
853 
cluster_has_invalid_data(struct compress_ctx * cc)854 static bool cluster_has_invalid_data(struct compress_ctx *cc)
855 {
856 	loff_t i_size = i_size_read(cc->inode);
857 	unsigned nr_pages = DIV_ROUND_UP(i_size, PAGE_SIZE);
858 	int i;
859 
860 	for (i = 0; i < cc->cluster_size; i++) {
861 		struct page *page = cc->rpages[i];
862 
863 		f2fs_bug_on(F2FS_I_SB(cc->inode), !page);
864 
865 		/* beyond EOF */
866 		if (page->index >= nr_pages)
867 			return true;
868 	}
869 	return false;
870 }
871 
f2fs_sanity_check_cluster(struct dnode_of_data * dn)872 bool f2fs_sanity_check_cluster(struct dnode_of_data *dn)
873 {
874 	struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
875 	unsigned int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
876 	bool compressed = dn->data_blkaddr == COMPRESS_ADDR;
877 	int cluster_end = 0;
878 	int i;
879 	char *reason = "";
880 
881 	if (!compressed)
882 		return false;
883 
884 	/* [..., COMPR_ADDR, ...] */
885 	if (dn->ofs_in_node % cluster_size) {
886 		reason = "[*|C|*|*]";
887 		goto out;
888 	}
889 
890 	for (i = 1; i < cluster_size; i++) {
891 		block_t blkaddr = data_blkaddr(dn->inode, dn->node_page,
892 							dn->ofs_in_node + i);
893 
894 		/* [COMPR_ADDR, ..., COMPR_ADDR] */
895 		if (blkaddr == COMPRESS_ADDR) {
896 			reason = "[C|*|C|*]";
897 			goto out;
898 		}
899 		if (!__is_valid_data_blkaddr(blkaddr)) {
900 			if (!cluster_end)
901 				cluster_end = i;
902 			continue;
903 		}
904 		/* [COMPR_ADDR, NULL_ADDR or NEW_ADDR, valid_blkaddr] */
905 		if (cluster_end) {
906 			reason = "[C|N|N|V]";
907 			goto out;
908 		}
909 	}
910 	return false;
911 out:
912 	f2fs_warn(sbi, "access invalid cluster, ino:%lu, nid:%u, ofs_in_node:%u, reason:%s",
913 			dn->inode->i_ino, dn->nid, dn->ofs_in_node, reason);
914 	set_sbi_flag(sbi, SBI_NEED_FSCK);
915 	return true;
916 }
917 
__f2fs_cluster_blocks(struct inode * inode,unsigned int cluster_idx,bool compr)918 static int __f2fs_cluster_blocks(struct inode *inode,
919 				unsigned int cluster_idx, bool compr)
920 {
921 	struct dnode_of_data dn;
922 	unsigned int cluster_size = F2FS_I(inode)->i_cluster_size;
923 	unsigned int start_idx = cluster_idx <<
924 				F2FS_I(inode)->i_log_cluster_size;
925 	int ret;
926 
927 	set_new_dnode(&dn, inode, NULL, NULL, 0);
928 	ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
929 	if (ret) {
930 		if (ret == -ENOENT)
931 			ret = 0;
932 		goto fail;
933 	}
934 
935 	if (f2fs_sanity_check_cluster(&dn)) {
936 		ret = -EFSCORRUPTED;
937 		f2fs_handle_error(F2FS_I_SB(inode), ERROR_CORRUPTED_CLUSTER);
938 		goto fail;
939 	}
940 
941 	if (dn.data_blkaddr == COMPRESS_ADDR) {
942 		int i;
943 
944 		ret = 1;
945 		for (i = 1; i < cluster_size; i++) {
946 			block_t blkaddr;
947 
948 			blkaddr = data_blkaddr(dn.inode,
949 					dn.node_page, dn.ofs_in_node + i);
950 			if (compr) {
951 				if (__is_valid_data_blkaddr(blkaddr))
952 					ret++;
953 			} else {
954 				if (blkaddr != NULL_ADDR)
955 					ret++;
956 			}
957 		}
958 
959 		f2fs_bug_on(F2FS_I_SB(inode),
960 			!compr && ret != cluster_size &&
961 			!is_inode_flag_set(inode, FI_COMPRESS_RELEASED));
962 	}
963 fail:
964 	f2fs_put_dnode(&dn);
965 	return ret;
966 }
967 
968 /* return # of compressed blocks in compressed cluster */
f2fs_compressed_blocks(struct compress_ctx * cc)969 static int f2fs_compressed_blocks(struct compress_ctx *cc)
970 {
971 	return __f2fs_cluster_blocks(cc->inode, cc->cluster_idx, true);
972 }
973 
974 /* return # of valid blocks in compressed cluster */
f2fs_is_compressed_cluster(struct inode * inode,pgoff_t index)975 int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index)
976 {
977 	return __f2fs_cluster_blocks(inode,
978 		index >> F2FS_I(inode)->i_log_cluster_size,
979 		false);
980 }
981 
cluster_may_compress(struct compress_ctx * cc)982 static bool cluster_may_compress(struct compress_ctx *cc)
983 {
984 	if (!f2fs_need_compress_data(cc->inode))
985 		return false;
986 	if (f2fs_is_atomic_file(cc->inode))
987 		return false;
988 	if (!f2fs_cluster_is_full(cc))
989 		return false;
990 	if (unlikely(f2fs_cp_error(F2FS_I_SB(cc->inode))))
991 		return false;
992 	return !cluster_has_invalid_data(cc);
993 }
994 
set_cluster_writeback(struct compress_ctx * cc)995 static void set_cluster_writeback(struct compress_ctx *cc)
996 {
997 	int i;
998 
999 	for (i = 0; i < cc->cluster_size; i++) {
1000 		if (cc->rpages[i])
1001 			set_page_writeback(cc->rpages[i]);
1002 	}
1003 }
1004 
set_cluster_dirty(struct compress_ctx * cc)1005 static void set_cluster_dirty(struct compress_ctx *cc)
1006 {
1007 	int i;
1008 
1009 	for (i = 0; i < cc->cluster_size; i++)
1010 		if (cc->rpages[i]) {
1011 			set_page_dirty(cc->rpages[i]);
1012 			set_page_private_gcing(cc->rpages[i]);
1013 		}
1014 }
1015 
prepare_compress_overwrite(struct compress_ctx * cc,struct page ** pagep,pgoff_t index,void ** fsdata)1016 static int prepare_compress_overwrite(struct compress_ctx *cc,
1017 		struct page **pagep, pgoff_t index, void **fsdata)
1018 {
1019 	struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
1020 	struct address_space *mapping = cc->inode->i_mapping;
1021 	struct page *page;
1022 	sector_t last_block_in_bio;
1023 	unsigned fgp_flag = FGP_LOCK | FGP_WRITE | FGP_CREAT;
1024 	pgoff_t start_idx = start_idx_of_cluster(cc);
1025 	int i, ret;
1026 
1027 retry:
1028 	ret = f2fs_is_compressed_cluster(cc->inode, start_idx);
1029 	if (ret <= 0)
1030 		return ret;
1031 
1032 	ret = f2fs_init_compress_ctx(cc);
1033 	if (ret)
1034 		return ret;
1035 
1036 	/* keep page reference to avoid page reclaim */
1037 	for (i = 0; i < cc->cluster_size; i++) {
1038 		page = f2fs_pagecache_get_page(mapping, start_idx + i,
1039 							fgp_flag, GFP_NOFS);
1040 		if (!page) {
1041 			ret = -ENOMEM;
1042 			goto unlock_pages;
1043 		}
1044 
1045 		if (PageUptodate(page))
1046 			f2fs_put_page(page, 1);
1047 		else
1048 			f2fs_compress_ctx_add_page(cc, page);
1049 	}
1050 
1051 	if (!f2fs_cluster_is_empty(cc)) {
1052 		struct bio *bio = NULL;
1053 
1054 		ret = f2fs_read_multi_pages(cc, &bio, cc->cluster_size,
1055 					&last_block_in_bio, false, true);
1056 		f2fs_put_rpages(cc);
1057 		f2fs_destroy_compress_ctx(cc, true);
1058 		if (ret)
1059 			goto out;
1060 		if (bio)
1061 			f2fs_submit_read_bio(sbi, bio, DATA);
1062 
1063 		ret = f2fs_init_compress_ctx(cc);
1064 		if (ret)
1065 			goto out;
1066 	}
1067 
1068 	for (i = 0; i < cc->cluster_size; i++) {
1069 		f2fs_bug_on(sbi, cc->rpages[i]);
1070 
1071 		page = find_lock_page(mapping, start_idx + i);
1072 		if (!page) {
1073 			/* page can be truncated */
1074 			goto release_and_retry;
1075 		}
1076 
1077 		f2fs_wait_on_page_writeback(page, DATA, true, true);
1078 		f2fs_compress_ctx_add_page(cc, page);
1079 
1080 		if (!PageUptodate(page)) {
1081 release_and_retry:
1082 			f2fs_put_rpages(cc);
1083 			f2fs_unlock_rpages(cc, i + 1);
1084 			f2fs_destroy_compress_ctx(cc, true);
1085 			goto retry;
1086 		}
1087 	}
1088 
1089 	if (likely(!ret)) {
1090 		*fsdata = cc->rpages;
1091 		*pagep = cc->rpages[offset_in_cluster(cc, index)];
1092 		return cc->cluster_size;
1093 	}
1094 
1095 unlock_pages:
1096 	f2fs_put_rpages(cc);
1097 	f2fs_unlock_rpages(cc, i);
1098 	f2fs_destroy_compress_ctx(cc, true);
1099 out:
1100 	return ret;
1101 }
1102 
f2fs_prepare_compress_overwrite(struct inode * inode,struct page ** pagep,pgoff_t index,void ** fsdata)1103 int f2fs_prepare_compress_overwrite(struct inode *inode,
1104 		struct page **pagep, pgoff_t index, void **fsdata)
1105 {
1106 	struct compress_ctx cc = {
1107 		.inode = inode,
1108 		.log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
1109 		.cluster_size = F2FS_I(inode)->i_cluster_size,
1110 		.cluster_idx = index >> F2FS_I(inode)->i_log_cluster_size,
1111 		.rpages = NULL,
1112 		.nr_rpages = 0,
1113 	};
1114 
1115 	return prepare_compress_overwrite(&cc, pagep, index, fsdata);
1116 }
1117 
f2fs_compress_write_end(struct inode * inode,void * fsdata,pgoff_t index,unsigned copied)1118 bool f2fs_compress_write_end(struct inode *inode, void *fsdata,
1119 					pgoff_t index, unsigned copied)
1120 
1121 {
1122 	struct compress_ctx cc = {
1123 		.inode = inode,
1124 		.log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
1125 		.cluster_size = F2FS_I(inode)->i_cluster_size,
1126 		.rpages = fsdata,
1127 	};
1128 	bool first_index = (index == cc.rpages[0]->index);
1129 
1130 	if (copied)
1131 		set_cluster_dirty(&cc);
1132 
1133 	f2fs_put_rpages_wbc(&cc, NULL, false, 1);
1134 	f2fs_destroy_compress_ctx(&cc, false);
1135 
1136 	return first_index;
1137 }
1138 
f2fs_truncate_partial_cluster(struct inode * inode,u64 from,bool lock)1139 int f2fs_truncate_partial_cluster(struct inode *inode, u64 from, bool lock)
1140 {
1141 	void *fsdata = NULL;
1142 	struct page *pagep;
1143 	int log_cluster_size = F2FS_I(inode)->i_log_cluster_size;
1144 	pgoff_t start_idx = from >> (PAGE_SHIFT + log_cluster_size) <<
1145 							log_cluster_size;
1146 	int err;
1147 
1148 	err = f2fs_is_compressed_cluster(inode, start_idx);
1149 	if (err < 0)
1150 		return err;
1151 
1152 	/* truncate normal cluster */
1153 	if (!err)
1154 		return f2fs_do_truncate_blocks(inode, from, lock);
1155 
1156 	/* truncate compressed cluster */
1157 	err = f2fs_prepare_compress_overwrite(inode, &pagep,
1158 						start_idx, &fsdata);
1159 
1160 	/* should not be a normal cluster */
1161 	f2fs_bug_on(F2FS_I_SB(inode), err == 0);
1162 
1163 	if (err <= 0)
1164 		return err;
1165 
1166 	if (err > 0) {
1167 		struct page **rpages = fsdata;
1168 		int cluster_size = F2FS_I(inode)->i_cluster_size;
1169 		int i;
1170 
1171 		for (i = cluster_size - 1; i >= 0; i--) {
1172 			loff_t start = rpages[i]->index << PAGE_SHIFT;
1173 
1174 			if (from <= start) {
1175 				zero_user_segment(rpages[i], 0, PAGE_SIZE);
1176 			} else {
1177 				zero_user_segment(rpages[i], from - start,
1178 								PAGE_SIZE);
1179 				break;
1180 			}
1181 		}
1182 
1183 		f2fs_compress_write_end(inode, fsdata, start_idx, true);
1184 	}
1185 	return 0;
1186 }
1187 
f2fs_write_compressed_pages(struct compress_ctx * cc,int * submitted,struct writeback_control * wbc,enum iostat_type io_type)1188 static int f2fs_write_compressed_pages(struct compress_ctx *cc,
1189 					int *submitted,
1190 					struct writeback_control *wbc,
1191 					enum iostat_type io_type)
1192 {
1193 	struct inode *inode = cc->inode;
1194 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1195 	struct f2fs_inode_info *fi = F2FS_I(inode);
1196 	struct f2fs_io_info fio = {
1197 		.sbi = sbi,
1198 		.ino = cc->inode->i_ino,
1199 		.type = DATA,
1200 		.op = REQ_OP_WRITE,
1201 		.op_flags = wbc_to_write_flags(wbc),
1202 		.old_blkaddr = NEW_ADDR,
1203 		.page = NULL,
1204 		.encrypted_page = NULL,
1205 		.compressed_page = NULL,
1206 		.submitted = 0,
1207 		.io_type = io_type,
1208 		.io_wbc = wbc,
1209 		.encrypted = fscrypt_inode_uses_fs_layer_crypto(cc->inode) ?
1210 									1 : 0,
1211 	};
1212 	struct dnode_of_data dn;
1213 	struct node_info ni;
1214 	struct compress_io_ctx *cic;
1215 	pgoff_t start_idx = start_idx_of_cluster(cc);
1216 	unsigned int last_index = cc->cluster_size - 1;
1217 	loff_t psize;
1218 	int i, err;
1219 
1220 	/* we should bypass data pages to proceed the kworker jobs */
1221 	if (unlikely(f2fs_cp_error(sbi))) {
1222 		mapping_set_error(cc->rpages[0]->mapping, -EIO);
1223 		goto out_free;
1224 	}
1225 
1226 	if (IS_NOQUOTA(inode)) {
1227 		/*
1228 		 * We need to wait for node_write to avoid block allocation during
1229 		 * checkpoint. This can only happen to quota writes which can cause
1230 		 * the below discard race condition.
1231 		 */
1232 		f2fs_down_read(&sbi->node_write);
1233 	} else if (!f2fs_trylock_op(sbi)) {
1234 		goto out_free;
1235 	}
1236 
1237 	set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
1238 
1239 	err = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
1240 	if (err)
1241 		goto out_unlock_op;
1242 
1243 	for (i = 0; i < cc->cluster_size; i++) {
1244 		if (data_blkaddr(dn.inode, dn.node_page,
1245 					dn.ofs_in_node + i) == NULL_ADDR)
1246 			goto out_put_dnode;
1247 	}
1248 
1249 	psize = (loff_t)(cc->rpages[last_index]->index + 1) << PAGE_SHIFT;
1250 
1251 	err = f2fs_get_node_info(fio.sbi, dn.nid, &ni, false);
1252 	if (err)
1253 		goto out_put_dnode;
1254 
1255 	fio.version = ni.version;
1256 
1257 	cic = f2fs_kmem_cache_alloc(cic_entry_slab, GFP_F2FS_ZERO, false, sbi);
1258 	if (!cic)
1259 		goto out_put_dnode;
1260 
1261 	cic->magic = F2FS_COMPRESSED_PAGE_MAGIC;
1262 	cic->inode = inode;
1263 	atomic_set(&cic->pending_pages, cc->valid_nr_cpages);
1264 	cic->rpages = page_array_alloc(cc->inode, cc->cluster_size);
1265 	if (!cic->rpages)
1266 		goto out_put_cic;
1267 
1268 	cic->nr_rpages = cc->cluster_size;
1269 
1270 	for (i = 0; i < cc->valid_nr_cpages; i++) {
1271 		f2fs_set_compressed_page(cc->cpages[i], inode,
1272 					cc->rpages[i + 1]->index, cic);
1273 		fio.compressed_page = cc->cpages[i];
1274 
1275 		fio.old_blkaddr = data_blkaddr(dn.inode, dn.node_page,
1276 						dn.ofs_in_node + i + 1);
1277 
1278 		/* wait for GCed page writeback via META_MAPPING */
1279 		f2fs_wait_on_block_writeback(inode, fio.old_blkaddr);
1280 
1281 		if (fio.encrypted) {
1282 			fio.page = cc->rpages[i + 1];
1283 			err = f2fs_encrypt_one_page(&fio);
1284 			if (err)
1285 				goto out_destroy_crypt;
1286 			cc->cpages[i] = fio.encrypted_page;
1287 		}
1288 	}
1289 
1290 	set_cluster_writeback(cc);
1291 
1292 	for (i = 0; i < cc->cluster_size; i++)
1293 		cic->rpages[i] = cc->rpages[i];
1294 
1295 	for (i = 0; i < cc->cluster_size; i++, dn.ofs_in_node++) {
1296 		block_t blkaddr;
1297 
1298 		blkaddr = f2fs_data_blkaddr(&dn);
1299 		fio.page = cc->rpages[i];
1300 		fio.old_blkaddr = blkaddr;
1301 
1302 		/* cluster header */
1303 		if (i == 0) {
1304 			if (blkaddr == COMPRESS_ADDR)
1305 				fio.compr_blocks++;
1306 			if (__is_valid_data_blkaddr(blkaddr))
1307 				f2fs_invalidate_blocks(sbi, blkaddr);
1308 			f2fs_update_data_blkaddr(&dn, COMPRESS_ADDR);
1309 			goto unlock_continue;
1310 		}
1311 
1312 		if (fio.compr_blocks && __is_valid_data_blkaddr(blkaddr))
1313 			fio.compr_blocks++;
1314 
1315 		if (i > cc->valid_nr_cpages) {
1316 			if (__is_valid_data_blkaddr(blkaddr)) {
1317 				f2fs_invalidate_blocks(sbi, blkaddr);
1318 				f2fs_update_data_blkaddr(&dn, NEW_ADDR);
1319 			}
1320 			goto unlock_continue;
1321 		}
1322 
1323 		f2fs_bug_on(fio.sbi, blkaddr == NULL_ADDR);
1324 
1325 		if (fio.encrypted)
1326 			fio.encrypted_page = cc->cpages[i - 1];
1327 		else
1328 			fio.compressed_page = cc->cpages[i - 1];
1329 
1330 		cc->cpages[i - 1] = NULL;
1331 		f2fs_outplace_write_data(&dn, &fio);
1332 		(*submitted)++;
1333 unlock_continue:
1334 		inode_dec_dirty_pages(cc->inode);
1335 		unlock_page(fio.page);
1336 	}
1337 
1338 	if (fio.compr_blocks)
1339 		f2fs_i_compr_blocks_update(inode, fio.compr_blocks - 1, false);
1340 	f2fs_i_compr_blocks_update(inode, cc->valid_nr_cpages, true);
1341 	add_compr_block_stat(inode, cc->valid_nr_cpages);
1342 
1343 	set_inode_flag(cc->inode, FI_APPEND_WRITE);
1344 	if (cc->cluster_idx == 0)
1345 		set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
1346 
1347 	f2fs_put_dnode(&dn);
1348 	if (IS_NOQUOTA(inode))
1349 		f2fs_up_read(&sbi->node_write);
1350 	else
1351 		f2fs_unlock_op(sbi);
1352 
1353 	spin_lock(&fi->i_size_lock);
1354 	if (fi->last_disk_size < psize)
1355 		fi->last_disk_size = psize;
1356 	spin_unlock(&fi->i_size_lock);
1357 
1358 	f2fs_put_rpages(cc);
1359 	page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
1360 	cc->cpages = NULL;
1361 	f2fs_destroy_compress_ctx(cc, false);
1362 	return 0;
1363 
1364 out_destroy_crypt:
1365 	page_array_free(cc->inode, cic->rpages, cc->cluster_size);
1366 
1367 	for (--i; i >= 0; i--)
1368 		fscrypt_finalize_bounce_page(&cc->cpages[i]);
1369 out_put_cic:
1370 	kmem_cache_free(cic_entry_slab, cic);
1371 out_put_dnode:
1372 	f2fs_put_dnode(&dn);
1373 out_unlock_op:
1374 	if (IS_NOQUOTA(inode))
1375 		f2fs_up_read(&sbi->node_write);
1376 	else
1377 		f2fs_unlock_op(sbi);
1378 out_free:
1379 	for (i = 0; i < cc->valid_nr_cpages; i++) {
1380 		f2fs_compress_free_page(cc->cpages[i]);
1381 		cc->cpages[i] = NULL;
1382 	}
1383 	page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
1384 	cc->cpages = NULL;
1385 	return -EAGAIN;
1386 }
1387 
f2fs_compress_write_end_io(struct bio * bio,struct page * page)1388 void f2fs_compress_write_end_io(struct bio *bio, struct page *page)
1389 {
1390 	struct f2fs_sb_info *sbi = bio->bi_private;
1391 	struct compress_io_ctx *cic =
1392 			(struct compress_io_ctx *)page_private(page);
1393 	int i;
1394 
1395 	if (unlikely(bio->bi_status))
1396 		mapping_set_error(cic->inode->i_mapping, -EIO);
1397 
1398 	f2fs_compress_free_page(page);
1399 
1400 	dec_page_count(sbi, F2FS_WB_DATA);
1401 
1402 	if (atomic_dec_return(&cic->pending_pages))
1403 		return;
1404 
1405 	for (i = 0; i < cic->nr_rpages; i++) {
1406 		WARN_ON(!cic->rpages[i]);
1407 		clear_page_private_gcing(cic->rpages[i]);
1408 		end_page_writeback(cic->rpages[i]);
1409 	}
1410 
1411 	page_array_free(cic->inode, cic->rpages, cic->nr_rpages);
1412 	kmem_cache_free(cic_entry_slab, cic);
1413 }
1414 
f2fs_write_raw_pages(struct compress_ctx * cc,int * submitted,struct writeback_control * wbc,enum iostat_type io_type)1415 static int f2fs_write_raw_pages(struct compress_ctx *cc,
1416 					int *submitted,
1417 					struct writeback_control *wbc,
1418 					enum iostat_type io_type)
1419 {
1420 	struct address_space *mapping = cc->inode->i_mapping;
1421 	int _submitted, compr_blocks, ret, i;
1422 
1423 	compr_blocks = f2fs_compressed_blocks(cc);
1424 
1425 	for (i = 0; i < cc->cluster_size; i++) {
1426 		if (!cc->rpages[i])
1427 			continue;
1428 
1429 		redirty_page_for_writepage(wbc, cc->rpages[i]);
1430 		unlock_page(cc->rpages[i]);
1431 	}
1432 
1433 	if (compr_blocks < 0)
1434 		return compr_blocks;
1435 
1436 	for (i = 0; i < cc->cluster_size; i++) {
1437 		if (!cc->rpages[i])
1438 			continue;
1439 retry_write:
1440 		lock_page(cc->rpages[i]);
1441 
1442 		if (cc->rpages[i]->mapping != mapping) {
1443 continue_unlock:
1444 			unlock_page(cc->rpages[i]);
1445 			continue;
1446 		}
1447 
1448 		if (!PageDirty(cc->rpages[i]))
1449 			goto continue_unlock;
1450 
1451 		if (PageWriteback(cc->rpages[i])) {
1452 			if (wbc->sync_mode == WB_SYNC_NONE)
1453 				goto continue_unlock;
1454 			f2fs_wait_on_page_writeback(cc->rpages[i], DATA, true, true);
1455 		}
1456 
1457 		if (!clear_page_dirty_for_io(cc->rpages[i]))
1458 			goto continue_unlock;
1459 
1460 		ret = f2fs_write_single_data_page(cc->rpages[i], &_submitted,
1461 						NULL, NULL, wbc, io_type,
1462 						compr_blocks, false);
1463 		if (ret) {
1464 			if (ret == AOP_WRITEPAGE_ACTIVATE) {
1465 				unlock_page(cc->rpages[i]);
1466 				ret = 0;
1467 			} else if (ret == -EAGAIN) {
1468 				/*
1469 				 * for quota file, just redirty left pages to
1470 				 * avoid deadlock caused by cluster update race
1471 				 * from foreground operation.
1472 				 */
1473 				if (IS_NOQUOTA(cc->inode))
1474 					return 0;
1475 				ret = 0;
1476 				f2fs_io_schedule_timeout(DEFAULT_IO_TIMEOUT);
1477 				goto retry_write;
1478 			}
1479 			return ret;
1480 		}
1481 
1482 		*submitted += _submitted;
1483 	}
1484 
1485 	f2fs_balance_fs(F2FS_M_SB(mapping), true);
1486 
1487 	return 0;
1488 }
1489 
f2fs_write_multi_pages(struct compress_ctx * cc,int * submitted,struct writeback_control * wbc,enum iostat_type io_type)1490 int f2fs_write_multi_pages(struct compress_ctx *cc,
1491 					int *submitted,
1492 					struct writeback_control *wbc,
1493 					enum iostat_type io_type)
1494 {
1495 	int err;
1496 
1497 	*submitted = 0;
1498 	if (cluster_may_compress(cc)) {
1499 		err = f2fs_compress_pages(cc);
1500 		if (err == -EAGAIN) {
1501 			add_compr_block_stat(cc->inode, cc->cluster_size);
1502 			goto write;
1503 		} else if (err) {
1504 			f2fs_put_rpages_wbc(cc, wbc, true, 1);
1505 			goto destroy_out;
1506 		}
1507 
1508 		err = f2fs_write_compressed_pages(cc, submitted,
1509 							wbc, io_type);
1510 		if (!err)
1511 			return 0;
1512 		f2fs_bug_on(F2FS_I_SB(cc->inode), err != -EAGAIN);
1513 	}
1514 write:
1515 	f2fs_bug_on(F2FS_I_SB(cc->inode), *submitted);
1516 
1517 	err = f2fs_write_raw_pages(cc, submitted, wbc, io_type);
1518 	f2fs_put_rpages_wbc(cc, wbc, false, 0);
1519 destroy_out:
1520 	f2fs_destroy_compress_ctx(cc, false);
1521 	return err;
1522 }
1523 
allow_memalloc_for_decomp(struct f2fs_sb_info * sbi,bool pre_alloc)1524 static inline bool allow_memalloc_for_decomp(struct f2fs_sb_info *sbi,
1525 		bool pre_alloc)
1526 {
1527 	return pre_alloc ^ f2fs_low_mem_mode(sbi);
1528 }
1529 
f2fs_prepare_decomp_mem(struct decompress_io_ctx * dic,bool pre_alloc)1530 static int f2fs_prepare_decomp_mem(struct decompress_io_ctx *dic,
1531 		bool pre_alloc)
1532 {
1533 	const struct f2fs_compress_ops *cops =
1534 		f2fs_cops[F2FS_I(dic->inode)->i_compress_algorithm];
1535 	int i;
1536 
1537 	if (!allow_memalloc_for_decomp(F2FS_I_SB(dic->inode), pre_alloc))
1538 		return 0;
1539 
1540 	dic->tpages = page_array_alloc(dic->inode, dic->cluster_size);
1541 	if (!dic->tpages)
1542 		return -ENOMEM;
1543 
1544 	for (i = 0; i < dic->cluster_size; i++) {
1545 		if (dic->rpages[i]) {
1546 			dic->tpages[i] = dic->rpages[i];
1547 			continue;
1548 		}
1549 
1550 		dic->tpages[i] = f2fs_compress_alloc_page();
1551 		if (!dic->tpages[i])
1552 			return -ENOMEM;
1553 	}
1554 
1555 	dic->rbuf = f2fs_vmap(dic->tpages, dic->cluster_size);
1556 	if (!dic->rbuf)
1557 		return -ENOMEM;
1558 
1559 	dic->cbuf = f2fs_vmap(dic->cpages, dic->nr_cpages);
1560 	if (!dic->cbuf)
1561 		return -ENOMEM;
1562 
1563 	if (cops->init_decompress_ctx)
1564 		return cops->init_decompress_ctx(dic);
1565 
1566 	return 0;
1567 }
1568 
f2fs_release_decomp_mem(struct decompress_io_ctx * dic,bool bypass_destroy_callback,bool pre_alloc)1569 static void f2fs_release_decomp_mem(struct decompress_io_ctx *dic,
1570 		bool bypass_destroy_callback, bool pre_alloc)
1571 {
1572 	const struct f2fs_compress_ops *cops =
1573 		f2fs_cops[F2FS_I(dic->inode)->i_compress_algorithm];
1574 
1575 	if (!allow_memalloc_for_decomp(F2FS_I_SB(dic->inode), pre_alloc))
1576 		return;
1577 
1578 	if (!bypass_destroy_callback && cops->destroy_decompress_ctx)
1579 		cops->destroy_decompress_ctx(dic);
1580 
1581 	if (dic->cbuf)
1582 		vm_unmap_ram(dic->cbuf, dic->nr_cpages);
1583 
1584 	if (dic->rbuf)
1585 		vm_unmap_ram(dic->rbuf, dic->cluster_size);
1586 }
1587 
1588 static void f2fs_free_dic(struct decompress_io_ctx *dic,
1589 		bool bypass_destroy_callback);
1590 
f2fs_alloc_dic(struct compress_ctx * cc)1591 struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc)
1592 {
1593 	struct decompress_io_ctx *dic;
1594 	pgoff_t start_idx = start_idx_of_cluster(cc);
1595 	struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
1596 	int i, ret;
1597 
1598 	dic = f2fs_kmem_cache_alloc(dic_entry_slab, GFP_F2FS_ZERO, false, sbi);
1599 	if (!dic)
1600 		return ERR_PTR(-ENOMEM);
1601 
1602 	dic->rpages = page_array_alloc(cc->inode, cc->cluster_size);
1603 	if (!dic->rpages) {
1604 		kmem_cache_free(dic_entry_slab, dic);
1605 		return ERR_PTR(-ENOMEM);
1606 	}
1607 
1608 	dic->magic = F2FS_COMPRESSED_PAGE_MAGIC;
1609 	dic->inode = cc->inode;
1610 	atomic_set(&dic->remaining_pages, cc->nr_cpages);
1611 	dic->cluster_idx = cc->cluster_idx;
1612 	dic->cluster_size = cc->cluster_size;
1613 	dic->log_cluster_size = cc->log_cluster_size;
1614 	dic->nr_cpages = cc->nr_cpages;
1615 	refcount_set(&dic->refcnt, 1);
1616 	dic->failed = false;
1617 	dic->need_verity = f2fs_need_verity(cc->inode, start_idx);
1618 
1619 	for (i = 0; i < dic->cluster_size; i++)
1620 		dic->rpages[i] = cc->rpages[i];
1621 	dic->nr_rpages = cc->cluster_size;
1622 
1623 	dic->cpages = page_array_alloc(dic->inode, dic->nr_cpages);
1624 	if (!dic->cpages) {
1625 		ret = -ENOMEM;
1626 		goto out_free;
1627 	}
1628 
1629 	for (i = 0; i < dic->nr_cpages; i++) {
1630 		struct page *page;
1631 
1632 		page = f2fs_compress_alloc_page();
1633 		if (!page) {
1634 			ret = -ENOMEM;
1635 			goto out_free;
1636 		}
1637 
1638 		f2fs_set_compressed_page(page, cc->inode,
1639 					start_idx + i + 1, dic);
1640 		dic->cpages[i] = page;
1641 	}
1642 
1643 	ret = f2fs_prepare_decomp_mem(dic, true);
1644 	if (ret)
1645 		goto out_free;
1646 
1647 	return dic;
1648 
1649 out_free:
1650 	f2fs_free_dic(dic, true);
1651 	return ERR_PTR(ret);
1652 }
1653 
f2fs_free_dic(struct decompress_io_ctx * dic,bool bypass_destroy_callback)1654 static void f2fs_free_dic(struct decompress_io_ctx *dic,
1655 		bool bypass_destroy_callback)
1656 {
1657 	int i;
1658 
1659 	f2fs_release_decomp_mem(dic, bypass_destroy_callback, true);
1660 
1661 	if (dic->tpages) {
1662 		for (i = 0; i < dic->cluster_size; i++) {
1663 			if (dic->rpages[i])
1664 				continue;
1665 			if (!dic->tpages[i])
1666 				continue;
1667 			f2fs_compress_free_page(dic->tpages[i]);
1668 		}
1669 		page_array_free(dic->inode, dic->tpages, dic->cluster_size);
1670 	}
1671 
1672 	if (dic->cpages) {
1673 		for (i = 0; i < dic->nr_cpages; i++) {
1674 			if (!dic->cpages[i])
1675 				continue;
1676 			f2fs_compress_free_page(dic->cpages[i]);
1677 		}
1678 		page_array_free(dic->inode, dic->cpages, dic->nr_cpages);
1679 	}
1680 
1681 	page_array_free(dic->inode, dic->rpages, dic->nr_rpages);
1682 	kmem_cache_free(dic_entry_slab, dic);
1683 }
1684 
f2fs_late_free_dic(struct work_struct * work)1685 static void f2fs_late_free_dic(struct work_struct *work)
1686 {
1687 	struct decompress_io_ctx *dic =
1688 		container_of(work, struct decompress_io_ctx, free_work);
1689 
1690 	f2fs_free_dic(dic, false);
1691 }
1692 
f2fs_put_dic(struct decompress_io_ctx * dic,bool in_task)1693 static void f2fs_put_dic(struct decompress_io_ctx *dic, bool in_task)
1694 {
1695 	if (refcount_dec_and_test(&dic->refcnt)) {
1696 		if (in_task) {
1697 			f2fs_free_dic(dic, false);
1698 		} else {
1699 			INIT_WORK(&dic->free_work, f2fs_late_free_dic);
1700 			queue_work(F2FS_I_SB(dic->inode)->post_read_wq,
1701 					&dic->free_work);
1702 		}
1703 	}
1704 }
1705 
f2fs_verify_cluster(struct work_struct * work)1706 static void f2fs_verify_cluster(struct work_struct *work)
1707 {
1708 	struct decompress_io_ctx *dic =
1709 		container_of(work, struct decompress_io_ctx, verity_work);
1710 	int i;
1711 
1712 	/* Verify, update, and unlock the decompressed pages. */
1713 	for (i = 0; i < dic->cluster_size; i++) {
1714 		struct page *rpage = dic->rpages[i];
1715 
1716 		if (!rpage)
1717 			continue;
1718 
1719 		if (fsverity_verify_page(rpage))
1720 			SetPageUptodate(rpage);
1721 		else
1722 			ClearPageUptodate(rpage);
1723 		unlock_page(rpage);
1724 	}
1725 
1726 	f2fs_put_dic(dic, true);
1727 }
1728 
1729 /*
1730  * This is called when a compressed cluster has been decompressed
1731  * (or failed to be read and/or decompressed).
1732  */
f2fs_decompress_end_io(struct decompress_io_ctx * dic,bool failed,bool in_task)1733 void f2fs_decompress_end_io(struct decompress_io_ctx *dic, bool failed,
1734 				bool in_task)
1735 {
1736 	int i;
1737 
1738 	if (!failed && dic->need_verity) {
1739 		/*
1740 		 * Note that to avoid deadlocks, the verity work can't be done
1741 		 * on the decompression workqueue.  This is because verifying
1742 		 * the data pages can involve reading metadata pages from the
1743 		 * file, and these metadata pages may be compressed.
1744 		 */
1745 		INIT_WORK(&dic->verity_work, f2fs_verify_cluster);
1746 		fsverity_enqueue_verify_work(&dic->verity_work);
1747 		return;
1748 	}
1749 
1750 	/* Update and unlock the cluster's pagecache pages. */
1751 	for (i = 0; i < dic->cluster_size; i++) {
1752 		struct page *rpage = dic->rpages[i];
1753 
1754 		if (!rpage)
1755 			continue;
1756 
1757 		if (failed)
1758 			ClearPageUptodate(rpage);
1759 		else
1760 			SetPageUptodate(rpage);
1761 		unlock_page(rpage);
1762 	}
1763 
1764 	/*
1765 	 * Release the reference to the decompress_io_ctx that was being held
1766 	 * for I/O completion.
1767 	 */
1768 	f2fs_put_dic(dic, in_task);
1769 }
1770 
1771 /*
1772  * Put a reference to a compressed page's decompress_io_ctx.
1773  *
1774  * This is called when the page is no longer needed and can be freed.
1775  */
f2fs_put_page_dic(struct page * page,bool in_task)1776 void f2fs_put_page_dic(struct page *page, bool in_task)
1777 {
1778 	struct decompress_io_ctx *dic =
1779 			(struct decompress_io_ctx *)page_private(page);
1780 
1781 	f2fs_put_dic(dic, in_task);
1782 }
1783 
1784 /*
1785  * check whether cluster blocks are contiguous, and add extent cache entry
1786  * only if cluster blocks are logically and physically contiguous.
1787  */
f2fs_cluster_blocks_are_contiguous(struct dnode_of_data * dn)1788 unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn)
1789 {
1790 	bool compressed = f2fs_data_blkaddr(dn) == COMPRESS_ADDR;
1791 	int i = compressed ? 1 : 0;
1792 	block_t first_blkaddr = data_blkaddr(dn->inode, dn->node_page,
1793 						dn->ofs_in_node + i);
1794 
1795 	for (i += 1; i < F2FS_I(dn->inode)->i_cluster_size; i++) {
1796 		block_t blkaddr = data_blkaddr(dn->inode, dn->node_page,
1797 						dn->ofs_in_node + i);
1798 
1799 		if (!__is_valid_data_blkaddr(blkaddr))
1800 			break;
1801 		if (first_blkaddr + i - (compressed ? 1 : 0) != blkaddr)
1802 			return 0;
1803 	}
1804 
1805 	return compressed ? i - 1 : i;
1806 }
1807 
1808 const struct address_space_operations f2fs_compress_aops = {
1809 	.releasepage = f2fs_release_page,
1810 	.invalidatepage = f2fs_invalidate_page,
1811 };
1812 
COMPRESS_MAPPING(struct f2fs_sb_info * sbi)1813 struct address_space *COMPRESS_MAPPING(struct f2fs_sb_info *sbi)
1814 {
1815 	return sbi->compress_inode->i_mapping;
1816 }
1817 
f2fs_invalidate_compress_page(struct f2fs_sb_info * sbi,block_t blkaddr)1818 void f2fs_invalidate_compress_page(struct f2fs_sb_info *sbi, block_t blkaddr)
1819 {
1820 	if (!sbi->compress_inode)
1821 		return;
1822 	invalidate_mapping_pages(COMPRESS_MAPPING(sbi), blkaddr, blkaddr);
1823 }
1824 
f2fs_cache_compressed_page(struct f2fs_sb_info * sbi,struct page * page,nid_t ino,block_t blkaddr)1825 void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
1826 						nid_t ino, block_t blkaddr)
1827 {
1828 	struct page *cpage;
1829 	int ret;
1830 
1831 	if (!test_opt(sbi, COMPRESS_CACHE))
1832 		return;
1833 
1834 	if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE_READ))
1835 		return;
1836 
1837 	if (!f2fs_available_free_memory(sbi, COMPRESS_PAGE))
1838 		return;
1839 
1840 	cpage = find_get_page(COMPRESS_MAPPING(sbi), blkaddr);
1841 	if (cpage) {
1842 		f2fs_put_page(cpage, 0);
1843 		return;
1844 	}
1845 
1846 	cpage = alloc_page(__GFP_NOWARN | __GFP_IO);
1847 	if (!cpage)
1848 		return;
1849 
1850 	ret = add_to_page_cache_lru(cpage, COMPRESS_MAPPING(sbi),
1851 						blkaddr, GFP_NOFS);
1852 	if (ret) {
1853 		f2fs_put_page(cpage, 0);
1854 		return;
1855 	}
1856 
1857 	set_page_private_data(cpage, ino);
1858 
1859 	if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE_READ))
1860 		goto out;
1861 
1862 	memcpy(page_address(cpage), page_address(page), PAGE_SIZE);
1863 	SetPageUptodate(cpage);
1864 out:
1865 	f2fs_put_page(cpage, 1);
1866 }
1867 
f2fs_load_compressed_page(struct f2fs_sb_info * sbi,struct page * page,block_t blkaddr)1868 bool f2fs_load_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
1869 								block_t blkaddr)
1870 {
1871 	struct page *cpage;
1872 	bool hitted = false;
1873 
1874 	if (!test_opt(sbi, COMPRESS_CACHE))
1875 		return false;
1876 
1877 	cpage = f2fs_pagecache_get_page(COMPRESS_MAPPING(sbi),
1878 				blkaddr, FGP_LOCK | FGP_NOWAIT, GFP_NOFS);
1879 	if (cpage) {
1880 		if (PageUptodate(cpage)) {
1881 			atomic_inc(&sbi->compress_page_hit);
1882 			memcpy(page_address(page),
1883 				page_address(cpage), PAGE_SIZE);
1884 			hitted = true;
1885 		}
1886 		f2fs_put_page(cpage, 1);
1887 	}
1888 
1889 	return hitted;
1890 }
1891 
f2fs_invalidate_compress_pages(struct f2fs_sb_info * sbi,nid_t ino)1892 void f2fs_invalidate_compress_pages(struct f2fs_sb_info *sbi, nid_t ino)
1893 {
1894 	struct address_space *mapping = COMPRESS_MAPPING(sbi);
1895 	struct pagevec pvec;
1896 	pgoff_t index = 0;
1897 	pgoff_t end = MAX_BLKADDR(sbi);
1898 
1899 	if (!mapping->nrpages)
1900 		return;
1901 
1902 	pagevec_init(&pvec);
1903 
1904 	do {
1905 		unsigned int nr_pages;
1906 		int i;
1907 
1908 		nr_pages = pagevec_lookup_range(&pvec, mapping,
1909 						&index, end - 1);
1910 		if (!nr_pages)
1911 			break;
1912 
1913 		for (i = 0; i < nr_pages; i++) {
1914 			struct page *page = pvec.pages[i];
1915 
1916 			if (page->index > end)
1917 				break;
1918 
1919 			lock_page(page);
1920 			if (page->mapping != mapping) {
1921 				unlock_page(page);
1922 				continue;
1923 			}
1924 
1925 			if (ino != get_page_private_data(page)) {
1926 				unlock_page(page);
1927 				continue;
1928 			}
1929 
1930 			generic_error_remove_page(mapping, page);
1931 			unlock_page(page);
1932 		}
1933 		pagevec_release(&pvec);
1934 		cond_resched();
1935 	} while (index < end);
1936 }
1937 
f2fs_init_compress_inode(struct f2fs_sb_info * sbi)1938 int f2fs_init_compress_inode(struct f2fs_sb_info *sbi)
1939 {
1940 	struct inode *inode;
1941 
1942 	if (!test_opt(sbi, COMPRESS_CACHE))
1943 		return 0;
1944 
1945 	inode = f2fs_iget(sbi->sb, F2FS_COMPRESS_INO(sbi));
1946 	if (IS_ERR(inode))
1947 		return PTR_ERR(inode);
1948 	sbi->compress_inode = inode;
1949 
1950 	sbi->compress_percent = COMPRESS_PERCENT;
1951 	sbi->compress_watermark = COMPRESS_WATERMARK;
1952 
1953 	atomic_set(&sbi->compress_page_hit, 0);
1954 
1955 	return 0;
1956 }
1957 
f2fs_destroy_compress_inode(struct f2fs_sb_info * sbi)1958 void f2fs_destroy_compress_inode(struct f2fs_sb_info *sbi)
1959 {
1960 	if (!sbi->compress_inode)
1961 		return;
1962 	iput(sbi->compress_inode);
1963 	sbi->compress_inode = NULL;
1964 }
1965 
f2fs_init_page_array_cache(struct f2fs_sb_info * sbi)1966 int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi)
1967 {
1968 	dev_t dev = sbi->sb->s_bdev->bd_dev;
1969 	char slab_name[35];
1970 
1971 	if (!f2fs_sb_has_compression(sbi))
1972 		return 0;
1973 
1974 	sprintf(slab_name, "f2fs_page_array_entry-%u:%u", MAJOR(dev), MINOR(dev));
1975 
1976 	sbi->page_array_slab_size = sizeof(struct page *) <<
1977 					F2FS_OPTION(sbi).compress_log_size;
1978 
1979 	sbi->page_array_slab = f2fs_kmem_cache_create(slab_name,
1980 					sbi->page_array_slab_size);
1981 	return sbi->page_array_slab ? 0 : -ENOMEM;
1982 }
1983 
f2fs_destroy_page_array_cache(struct f2fs_sb_info * sbi)1984 void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi)
1985 {
1986 	kmem_cache_destroy(sbi->page_array_slab);
1987 }
1988 
f2fs_init_compress_cache(void)1989 int __init f2fs_init_compress_cache(void)
1990 {
1991 	cic_entry_slab = f2fs_kmem_cache_create("f2fs_cic_entry",
1992 					sizeof(struct compress_io_ctx));
1993 	if (!cic_entry_slab)
1994 		return -ENOMEM;
1995 	dic_entry_slab = f2fs_kmem_cache_create("f2fs_dic_entry",
1996 					sizeof(struct decompress_io_ctx));
1997 	if (!dic_entry_slab)
1998 		goto free_cic;
1999 	return 0;
2000 free_cic:
2001 	kmem_cache_destroy(cic_entry_slab);
2002 	return -ENOMEM;
2003 }
2004 
f2fs_destroy_compress_cache(void)2005 void f2fs_destroy_compress_cache(void)
2006 {
2007 	kmem_cache_destroy(dic_entry_slab);
2008 	kmem_cache_destroy(cic_entry_slab);
2009 }
2010