compress.c 47.6 KB
Newer Older
Chao Yu's avatar
Chao Yu committed
1 2 3 4 5 6 7 8 9
// 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>
10
#include <linux/moduleparam.h>
Chao Yu's avatar
Chao Yu committed
11 12 13 14
#include <linux/writeback.h>
#include <linux/backing-dev.h>
#include <linux/lzo.h>
#include <linux/lz4.h>
15
#include <linux/zstd.h>
16
#include <linux/pagevec.h>
Chao Yu's avatar
Chao Yu committed
17 18 19

#include "f2fs.h"
#include "node.h"
20
#include "segment.h"
Chao Yu's avatar
Chao Yu committed
21 22
#include <trace/events/f2fs.h>

23 24 25
static struct kmem_cache *cic_entry_slab;
static struct kmem_cache *dic_entry_slab;

26 27 28 29 30 31
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))
32 33
		return f2fs_kmem_cache_alloc(sbi->page_array_slab,
					GFP_F2FS_ZERO, false, F2FS_I_SB(inode));
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
	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);
}

Chao Yu's avatar
Chao Yu committed
51 52 53 54
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);
55 56
	int (*init_decompress_ctx)(struct decompress_io_ctx *dic);
	void (*destroy_decompress_ctx)(struct decompress_io_ctx *dic);
Chao Yu's avatar
Chao Yu committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
	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;
81
	if (page_private_nonpointer(page))
Chao Yu's avatar
Chao Yu committed
82
		return false;
83

Chao Yu's avatar
Chao Yu committed
84 85 86 87 88 89
	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,
90
		struct inode *inode, pgoff_t index, void *data)
Chao Yu's avatar
Chao Yu committed
91
{
92
	attach_page_private(page, (void *)data);
Chao Yu's avatar
Chao Yu committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

	/* i_crypto_info and iv index */
	page->index = index;
	page->mapping = inode->i_mapping;
}

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);
}

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)
{
144
	if (cc->rpages)
Chao Yu's avatar
Chao Yu committed
145 146
		return 0;

147
	cc->rpages = page_array_alloc(cc->inode, cc->cluster_size);
Chao Yu's avatar
Chao Yu committed
148 149 150
	return cc->rpages ? 0 : -ENOMEM;
}

151
void f2fs_destroy_compress_ctx(struct compress_ctx *cc, bool reuse)
Chao Yu's avatar
Chao Yu committed
152
{
153
	page_array_free(cc->inode, cc->rpages, cc->cluster_size);
Chao Yu's avatar
Chao Yu committed
154 155 156
	cc->rpages = NULL;
	cc->nr_rpages = 0;
	cc->nr_cpages = 0;
157
	cc->valid_nr_cpages = 0;
158 159
	if (!reuse)
		cc->cluster_idx = NULL_CLUSTER;
Chao Yu's avatar
Chao Yu committed
160 161 162 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
}

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\n",
				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\n",
				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\n", 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)
{
241 242 243 244 245 246 247 248
	unsigned int size = LZ4_MEM_COMPRESS;

#ifdef CONFIG_F2FS_FS_LZ4HC
	if (F2FS_I(cc->inode)->i_compress_flag >> COMPRESS_LEVEL_OFFSET)
		size = LZ4HC_MEM_COMPRESS;
#endif

	cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode), size, GFP_NOFS);
Chao Yu's avatar
Chao Yu committed
249 250 251
	if (!cc->private)
		return -ENOMEM;

252 253 254 255 256 257
	/*
	 * 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;
Chao Yu's avatar
Chao Yu committed
258 259 260 261 262 263 264 265 266
	return 0;
}

static void lz4_destroy_compress_ctx(struct compress_ctx *cc)
{
	kvfree(cc->private);
	cc->private = NULL;
}

267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
#ifdef CONFIG_F2FS_FS_LZ4HC
static int lz4hc_compress_pages(struct compress_ctx *cc)
{
	unsigned char level = F2FS_I(cc->inode)->i_compress_flag >>
						COMPRESS_LEVEL_OFFSET;
	int len;

	if (level)
		len = LZ4_compress_HC(cc->rbuf, cc->cbuf->cdata, cc->rlen,
					cc->clen, level, cc->private);
	else
		len = LZ4_compress_default(cc->rbuf, cc->cbuf->cdata, cc->rlen,
						cc->clen, cc->private);
	if (!len)
		return -EAGAIN;

	cc->clen = len;
	return 0;
}
#endif

Chao Yu's avatar
Chao Yu committed
288 289 290 291
static int lz4_compress_pages(struct compress_ctx *cc)
{
	int len;

292 293 294
#ifdef CONFIG_F2FS_FS_LZ4HC
	return lz4hc_compress_pages(cc);
#endif
Chao Yu's avatar
Chao Yu committed
295 296
	len = LZ4_compress_default(cc->rbuf, cc->cbuf->cdata, cc->rlen,
						cc->clen, cc->private);
297 298 299
	if (!len)
		return -EAGAIN;

Chao Yu's avatar
Chao Yu committed
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
	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\n",
				KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id, ret);
		return -EIO;
	}

	if (ret != PAGE_SIZE << dic->log_cluster_size) {
317
		printk_ratelimited("%sF2FS-fs (%s): lz4 invalid ret:%d, "
Chao Yu's avatar
Chao Yu committed
318
					"expected:%lu\n", KERN_ERR,
319
					F2FS_I_SB(dic->inode)->sb->s_id, ret,
Chao Yu's avatar
Chao Yu committed
320 321 322 323 324 325 326 327 328 329 330 331 332 333
					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

334 335 336 337 338
#ifdef CONFIG_F2FS_FS_ZSTD
#define F2FS_ZSTD_DEFAULT_CLEVEL	1

static int zstd_init_compress_ctx(struct compress_ctx *cc)
{
339 340
	zstd_parameters params;
	zstd_cstream *stream;
341 342
	void *workspace;
	unsigned int workspace_size;
343 344 345 346 347
	unsigned char level = F2FS_I(cc->inode)->i_compress_flag >>
						COMPRESS_LEVEL_OFFSET;

	if (!level)
		level = F2FS_ZSTD_DEFAULT_CLEVEL;
348

349 350
	params = zstd_get_params(F2FS_ZSTD_DEFAULT_CLEVEL, cc->rlen);
	workspace_size = zstd_cstream_workspace_bound(&params.cParams);
351 352 353 354 355 356

	workspace = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
					workspace_size, GFP_NOFS);
	if (!workspace)
		return -ENOMEM;

357
	stream = zstd_init_cstream(&params, 0, workspace, workspace_size);
358
	if (!stream) {
359
		printk_ratelimited("%sF2FS-fs (%s): %s zstd_init_cstream failed\n",
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
				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)
{
382 383 384
	zstd_cstream *stream = cc->private2;
	zstd_in_buffer inbuf;
	zstd_out_buffer outbuf;
385 386 387 388 389 390 391 392 393 394 395 396
	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;

397 398 399
	ret = zstd_compress_stream(stream, &outbuf, &inbuf);
	if (zstd_is_error(ret)) {
		printk_ratelimited("%sF2FS-fs (%s): %s zstd_compress_stream failed, ret: %d\n",
400
				KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
401
				__func__, zstd_get_error_code(ret));
402 403 404
		return -EIO;
	}

405 406 407
	ret = zstd_end_stream(stream, &outbuf);
	if (zstd_is_error(ret)) {
		printk_ratelimited("%sF2FS-fs (%s): %s zstd_end_stream returned %d\n",
408
				KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
409
				__func__, zstd_get_error_code(ret));
410 411 412
		return -EIO;
	}

413 414 415 416 417 418 419
	/*
	 * there is compressed data remained in intermediate buffer due to
	 * no more space in cbuf.cdata
	 */
	if (ret)
		return -EAGAIN;

420 421 422 423 424 425
	cc->clen = outbuf.pos;
	return 0;
}

static int zstd_init_decompress_ctx(struct decompress_io_ctx *dic)
{
426
	zstd_dstream *stream;
427 428
	void *workspace;
	unsigned int workspace_size;
429 430
	unsigned int max_window_size =
			MAX_COMPRESS_WINDOW_SIZE(dic->log_cluster_size);
431

432
	workspace_size = zstd_dstream_workspace_bound(max_window_size);
433 434 435 436 437 438

	workspace = f2fs_kvmalloc(F2FS_I_SB(dic->inode),
					workspace_size, GFP_NOFS);
	if (!workspace)
		return -ENOMEM;

439
	stream = zstd_init_dstream(max_window_size, workspace, workspace_size);
440
	if (!stream) {
441
		printk_ratelimited("%sF2FS-fs (%s): %s zstd_init_dstream failed\n",
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
				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)
{
463 464 465
	zstd_dstream *stream = dic->private2;
	zstd_in_buffer inbuf;
	zstd_out_buffer outbuf;
466 467 468 469 470 471 472 473 474 475
	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;

476 477 478
	ret = zstd_decompress_stream(stream, &outbuf, &inbuf);
	if (zstd_is_error(ret)) {
		printk_ratelimited("%sF2FS-fs (%s): %s zstd_decompress_stream failed, ret: %d\n",
479
				KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id,
480
				__func__, zstd_get_error_code(ret));
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
		return -EIO;
	}

	if (dic->rlen != outbuf.pos) {
		printk_ratelimited("%sF2FS-fs (%s): %s ZSTD invalid rlen:%zu, "
				"expected:%lu\n", 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

506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
#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\n",
				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

Chao Yu's avatar
Chao Yu committed
531 532 533 534 535 536 537 538 539 540 541
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
542 543 544 545 546
#ifdef CONFIG_F2FS_FS_ZSTD
	&f2fs_zstd_ops,
#else
	NULL,
#endif
547 548 549 550 551
#if defined(CONFIG_F2FS_FS_LZO) && defined(CONFIG_F2FS_FS_LZORLE)
	&f2fs_lzorle_ops,
#else
	NULL,
#endif
Chao Yu's avatar
Chao Yu committed
552 553 554 555 556 557 558 559 560
};

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];
}

Jaegeuk Kim's avatar
Jaegeuk Kim committed
561
static mempool_t *compress_page_pool;
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
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)
Chao Yu's avatar
Chao Yu committed
582 583 584
{
	struct page *page;

585
	page = mempool_alloc(compress_page_pool, GFP_NOFS);
Chao Yu's avatar
Chao Yu committed
586
	lock_page(page);
587

Chao Yu's avatar
Chao Yu committed
588 589 590
	return page;
}

591 592 593 594
static void f2fs_compress_free_page(struct page *page)
{
	if (!page)
		return;
595
	detach_page_private(page);
596 597 598 599 600
	page->mapping = NULL;
	unlock_page(page);
	mempool_free(page, compress_page_pool);
}

601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
#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;
}

Chao Yu's avatar
Chao Yu committed
617 618 619 620 621
static int f2fs_compress_pages(struct compress_ctx *cc)
{
	struct f2fs_inode_info *fi = F2FS_I(cc->inode);
	const struct f2fs_compress_ops *cops =
				f2fs_cops[fi->i_compress_algorithm];
622
	unsigned int max_len, new_nr_cpages;
Chao Yu's avatar
Chao Yu committed
623
	u32 chksum = 0;
Chao Yu's avatar
Chao Yu committed
624 625 626 627 628
	int i, ret;

	trace_f2fs_compress_pages_start(cc->inode, cc->cluster_idx,
				cc->cluster_size, fi->i_compress_algorithm);

629 630 631 632 633
	if (cops->init_compress_ctx) {
		ret = cops->init_compress_ctx(cc);
		if (ret)
			goto out;
	}
Chao Yu's avatar
Chao Yu committed
634 635 636

	max_len = COMPRESS_HEADER_SIZE + cc->clen;
	cc->nr_cpages = DIV_ROUND_UP(max_len, PAGE_SIZE);
637
	cc->valid_nr_cpages = cc->nr_cpages;
Chao Yu's avatar
Chao Yu committed
638

639
	cc->cpages = page_array_alloc(cc->inode, cc->nr_cpages);
Chao Yu's avatar
Chao Yu committed
640 641 642 643 644 645
	if (!cc->cpages) {
		ret = -ENOMEM;
		goto destroy_compress_ctx;
	}

	for (i = 0; i < cc->nr_cpages; i++) {
646
		cc->cpages[i] = f2fs_compress_alloc_page();
Chao Yu's avatar
Chao Yu committed
647 648 649 650 651 652
		if (!cc->cpages[i]) {
			ret = -ENOMEM;
			goto out_free_cpages;
		}
	}

653
	cc->rbuf = f2fs_vmap(cc->rpages, cc->cluster_size);
Chao Yu's avatar
Chao Yu committed
654 655 656 657 658
	if (!cc->rbuf) {
		ret = -ENOMEM;
		goto out_free_cpages;
	}

659
	cc->cbuf = f2fs_vmap(cc->cpages, cc->nr_cpages);
Chao Yu's avatar
Chao Yu committed
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
	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);

Chao Yu's avatar
Chao Yu committed
678 679 680 681 682
	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);

Chao Yu's avatar
Chao Yu committed
683 684 685
	for (i = 0; i < COMPRESS_DATA_RESERVED_SIZE; i++)
		cc->cbuf->reserved[i] = cpu_to_le32(0);

686 687
	new_nr_cpages = DIV_ROUND_UP(cc->clen + COMPRESS_HEADER_SIZE, PAGE_SIZE);

688 689
	/* zero out any unused part of the last page */
	memset(&cc->cbuf->cdata[cc->clen], 0,
690 691
			(new_nr_cpages * PAGE_SIZE) -
			(cc->clen + COMPRESS_HEADER_SIZE));
692

693 694
	vm_unmap_ram(cc->cbuf, cc->nr_cpages);
	vm_unmap_ram(cc->rbuf, cc->cluster_size);
Chao Yu's avatar
Chao Yu committed
695

696
	for (i = 0; i < cc->nr_cpages; i++) {
697
		if (i < new_nr_cpages)
698
			continue;
699
		f2fs_compress_free_page(cc->cpages[i]);
Chao Yu's avatar
Chao Yu committed
700 701 702
		cc->cpages[i] = NULL;
	}

703 704
	if (cops->destroy_compress_ctx)
		cops->destroy_compress_ctx(cc);
705

706
	cc->valid_nr_cpages = new_nr_cpages;
Chao Yu's avatar
Chao Yu committed
707 708 709 710 711 712

	trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
							cc->clen, ret);
	return 0;

out_vunmap_cbuf:
713
	vm_unmap_ram(cc->cbuf, cc->nr_cpages);
Chao Yu's avatar
Chao Yu committed
714
out_vunmap_rbuf:
715
	vm_unmap_ram(cc->rbuf, cc->cluster_size);
Chao Yu's avatar
Chao Yu committed
716 717 718
out_free_cpages:
	for (i = 0; i < cc->nr_cpages; i++) {
		if (cc->cpages[i])
719
			f2fs_compress_free_page(cc->cpages[i]);
Chao Yu's avatar
Chao Yu committed
720
	}
721
	page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
Chao Yu's avatar
Chao Yu committed
722 723
	cc->cpages = NULL;
destroy_compress_ctx:
724 725
	if (cops->destroy_compress_ctx)
		cops->destroy_compress_ctx(cc);
Chao Yu's avatar
Chao Yu committed
726 727 728 729 730 731
out:
	trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
							cc->clen, ret);
	return ret;
}

732 733 734 735 736 737
static int f2fs_prepare_decomp_mem(struct decompress_io_ctx *dic,
		bool pre_alloc);
static void f2fs_release_decomp_mem(struct decompress_io_ctx *dic,
		bool bypass_destroy_callback, bool pre_alloc);

void f2fs_decompress_cluster(struct decompress_io_ctx *dic, bool in_task)
Chao Yu's avatar
Chao Yu committed
738 739
{
	struct f2fs_sb_info *sbi = F2FS_I_SB(dic->inode);
740
	struct f2fs_inode_info *fi = F2FS_I(dic->inode);
Chao Yu's avatar
Chao Yu committed
741 742
	const struct f2fs_compress_ops *cops =
			f2fs_cops[fi->i_compress_algorithm];
743
	bool bypass_callback = false;
Chao Yu's avatar
Chao Yu committed
744 745 746 747 748 749 750
	int ret;

	trace_f2fs_decompress_pages_start(dic->inode, dic->cluster_idx,
				dic->cluster_size, fi->i_compress_algorithm);

	if (dic->failed) {
		ret = -EIO;
751
		goto out_end_io;
Chao Yu's avatar
Chao Yu committed
752 753
	}

754 755 756 757
	ret = f2fs_prepare_decomp_mem(dic, false);
	if (ret) {
		bypass_callback = true;
		goto out_release;
Chao Yu's avatar
Chao Yu committed
758 759 760 761 762 763 764
	}

	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;
765
		goto out_release;
Chao Yu's avatar
Chao Yu committed
766 767 768 769
	}

	ret = cops->decompress_pages(dic);

770
	if (!ret && (fi->i_compress_flag & 1 << COMPRESS_CHKSUM)) {
Chao Yu's avatar
Chao Yu committed
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
		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);
		}
	}

786 787 788
out_release:
	f2fs_release_decomp_mem(dic, bypass_callback, false);

789
out_end_io:
Chao Yu's avatar
Chao Yu committed
790 791
	trace_f2fs_decompress_pages_end(dic->inode, dic->cluster_idx,
							dic->clen, ret);
792
	f2fs_decompress_end_io(dic, ret, in_task);
793 794 795 796 797 798 799 800
}

/*
 * This is called when a page of a compressed cluster has been read from disk
 * (or failed to be read from disk).  It checks whether this page was the last
 * page being waited on in the cluster, and if so, it decompresses the cluster
 * (or in the case of a failure, cleans up without actually decompressing).
 */
801
void f2fs_end_read_compressed_page(struct page *page, bool failed,
802
		block_t blkaddr, bool in_task)
803 804 805 806 807 808 809 810 811
{
	struct decompress_io_ctx *dic =
			(struct decompress_io_ctx *)page_private(page);
	struct f2fs_sb_info *sbi = F2FS_I_SB(dic->inode);

	dec_page_count(sbi, F2FS_RD_DATA);

	if (failed)
		WRITE_ONCE(dic->failed, true);
812
	else if (blkaddr && in_task)
813 814
		f2fs_cache_compressed_page(sbi, page,
					dic->inode->i_ino, blkaddr);
815 816

	if (atomic_dec_and_test(&dic->remaining_pages))
817
		f2fs_decompress_cluster(dic, in_task);
Chao Yu's avatar
Chao Yu committed
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
}

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);
}

844
bool f2fs_all_cluster_page_ready(struct compress_ctx *cc, struct page **pages,
845
				int index, int nr_pages, bool uptodate)
846
{
847
	unsigned long pgidx = pages[index]->index;
848
	int i = uptodate ? 0 : 1;
849

850 851 852 853 854
	/*
	 * when uptodate set to true, try to check all pages in cluster is
	 * uptodate or not.
	 */
	if (uptodate && (pgidx % cc->cluster_size))
855 856
		return false;

857 858
	if (nr_pages - index < cc->cluster_size)
		return false;
859

860
	for (; i < cc->cluster_size; i++) {
861
		if (pages[index + i]->index != pgidx + i)
862
			return false;
863
		if (uptodate && !PageUptodate(pages[index + i]))
864 865 866 867 868 869
			return false;
	}

	return true;
}

870
static bool cluster_has_invalid_data(struct compress_ctx *cc)
Chao Yu's avatar
Chao Yu committed
871 872 873 874 875 876 877 878
{
	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];

879
		f2fs_bug_on(F2FS_I_SB(cc->inode), !page);
Chao Yu's avatar
Chao Yu committed
880 881 882

		/* beyond EOF */
		if (page->index >= nr_pages)
883
			return true;
Chao Yu's avatar
Chao Yu committed
884
	}
885
	return false;
Chao Yu's avatar
Chao Yu committed
886 887
}

888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
bool f2fs_sanity_check_cluster(struct dnode_of_data *dn)
{
	struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
	unsigned int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
	bool compressed = dn->data_blkaddr == COMPRESS_ADDR;
	int cluster_end = 0;
	int i;
	char *reason = "";

	if (!compressed)
		return false;

	/* [..., COMPR_ADDR, ...] */
	if (dn->ofs_in_node % cluster_size) {
		reason = "[*|C|*|*]";
		goto out;
	}

	for (i = 1; i < cluster_size; i++) {
		block_t blkaddr = data_blkaddr(dn->inode, dn->node_page,
							dn->ofs_in_node + i);

		/* [COMPR_ADDR, ..., COMPR_ADDR] */
		if (blkaddr == COMPRESS_ADDR) {
			reason = "[C|*|C|*]";
			goto out;
		}
		if (compressed) {
			if (!__is_valid_data_blkaddr(blkaddr)) {
				if (!cluster_end)
					cluster_end = i;
				continue;
			}
			/* [COMPR_ADDR, NULL_ADDR or NEW_ADDR, valid_blkaddr] */
			if (cluster_end) {
				reason = "[C|N|N|V]";
				goto out;
			}
		}
	}
	return false;
out:
	f2fs_warn(sbi, "access invalid cluster, ino:%lu, nid:%u, ofs_in_node:%u, reason:%s",
			dn->inode->i_ino, dn->nid, dn->ofs_in_node, reason);
	set_sbi_flag(sbi, SBI_NEED_FSCK);
	return true;
}

936 937
static int __f2fs_cluster_blocks(struct inode *inode,
				unsigned int cluster_idx, bool compr)
Chao Yu's avatar
Chao Yu committed
938 939
{
	struct dnode_of_data dn;
940 941 942
	unsigned int cluster_size = F2FS_I(inode)->i_cluster_size;
	unsigned int start_idx = cluster_idx <<
				F2FS_I(inode)->i_log_cluster_size;
Chao Yu's avatar
Chao Yu committed
943 944
	int ret;

945 946
	set_new_dnode(&dn, inode, NULL, NULL, 0);
	ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
Chao Yu's avatar
Chao Yu committed
947 948 949 950 951 952
	if (ret) {
		if (ret == -ENOENT)
			ret = 0;
		goto fail;
	}

953 954 955 956 957
	if (f2fs_sanity_check_cluster(&dn)) {
		ret = -EFSCORRUPTED;
		goto fail;
	}

Chao Yu's avatar
Chao Yu committed
958 959 960 961
	if (dn.data_blkaddr == COMPRESS_ADDR) {
		int i;

		ret = 1;
962
		for (i = 1; i < cluster_size; i++) {
Chao Yu's avatar
Chao Yu committed
963 964
			block_t blkaddr;

965
			blkaddr = data_blkaddr(dn.inode,
Chao Yu's avatar
Chao Yu committed
966
					dn.node_page, dn.ofs_in_node + i);
967 968 969 970 971 972 973
			if (compr) {
				if (__is_valid_data_blkaddr(blkaddr))
					ret++;
			} else {
				if (blkaddr != NULL_ADDR)
					ret++;
			}
Chao Yu's avatar
Chao Yu committed
974
		}
975 976

		f2fs_bug_on(F2FS_I_SB(inode),
977 978
			!compr && ret != cluster_size &&
			!is_inode_flag_set(inode, FI_COMPRESS_RELEASED));
Chao Yu's avatar
Chao Yu committed
979 980 981 982 983 984
	}
fail:
	f2fs_put_dnode(&dn);
	return ret;
}

985 986 987
/* return # of compressed blocks in compressed cluster */
static int f2fs_compressed_blocks(struct compress_ctx *cc)
{
988
	return __f2fs_cluster_blocks(cc->inode, cc->cluster_idx, true);
989 990 991
}

/* return # of valid blocks in compressed cluster */
Chao Yu's avatar
Chao Yu committed
992 993
int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index)
{
994 995 996
	return __f2fs_cluster_blocks(inode,
		index >> F2FS_I(inode)->i_log_cluster_size,
		false);
Chao Yu's avatar
Chao Yu committed
997 998 999 1000
}

static bool cluster_may_compress(struct compress_ctx *cc)
{
1001
	if (!f2fs_need_compress_data(cc->inode))
Chao Yu's avatar
Chao Yu committed
1002 1003 1004 1005 1006
		return false;
	if (f2fs_is_atomic_file(cc->inode))
		return false;
	if (!f2fs_cluster_is_full(cc))
		return false;
1007 1008
	if (unlikely(f2fs_cp_error(F2FS_I_SB(cc->inode))))
		return false;
1009
	return !cluster_has_invalid_data(cc);
Chao Yu's avatar
Chao Yu committed
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
}

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;
	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;

retry:
1043
	ret = f2fs_is_compressed_cluster(cc->inode, start_idx);
Chao Yu's avatar
Chao Yu committed
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
	if (ret <= 0)
		return ret;

	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))
1061
			f2fs_put_page(page, 1);
Chao Yu's avatar
Chao Yu committed
1062 1063 1064 1065 1066 1067 1068 1069
		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,
1070
					&last_block_in_bio, false, true);
1071
		f2fs_put_rpages(cc);
1072
		f2fs_destroy_compress_ctx(cc, true);
Chao Yu's avatar
Chao Yu committed
1073
		if (ret)
1074
			goto out;
Chao Yu's avatar
Chao Yu committed
1075 1076 1077 1078 1079
		if (bio)
			f2fs_submit_bio(sbi, bio, DATA);

		ret = f2fs_init_compress_ctx(cc);
		if (ret)
1080
			goto out;
Chao Yu's avatar
Chao Yu committed
1081 1082 1083 1084 1085 1086
	}

	for (i = 0; i < cc->cluster_size; i++) {
		f2fs_bug_on(sbi, cc->rpages[i]);

		page = find_lock_page(mapping, start_idx + i);
1087 1088 1089 1090
		if (!page) {
			/* page can be truncated */
			goto release_and_retry;
		}
Chao Yu's avatar
Chao Yu committed
1091 1092 1093 1094 1095

		f2fs_wait_on_page_writeback(page, DATA, true, true);
		f2fs_compress_ctx_add_page(cc, page);

		if (!PageUptodate(page)) {
1096 1097
release_and_retry:
			f2fs_put_rpages(cc);
Chao Yu's avatar
Chao Yu committed
1098
			f2fs_unlock_rpages(cc, i + 1);
1099
			f2fs_destroy_compress_ctx(cc, true);
Chao Yu's avatar
Chao Yu committed
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
			goto retry;
		}
	}

	if (likely(!ret)) {
		*fsdata = cc->rpages;
		*pagep = cc->rpages[offset_in_cluster(cc, index)];
		return cc->cluster_size;
	}

unlock_pages:
1111
	f2fs_put_rpages(cc);
Chao Yu's avatar
Chao Yu committed
1112
	f2fs_unlock_rpages(cc, i);
1113
	f2fs_destroy_compress_ctx(cc, true);
1114
out:
Chao Yu's avatar
Chao Yu committed
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
	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 = {
1138
		.inode = inode,
Chao Yu's avatar
Chao Yu committed
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
		.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);
1149
	f2fs_destroy_compress_ctx(&cc, false);
Chao Yu's avatar
Chao Yu committed
1150 1151 1152 1153

	return first_index;
}

1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
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;
}

Chao Yu's avatar
Chao Yu committed
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
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,
		.io_type = io_type,
		.io_wbc = wbc,
1224
		.encrypted = fscrypt_inode_uses_fs_layer_crypto(cc->inode),
Chao Yu's avatar
Chao Yu committed
1225 1226 1227 1228 1229 1230 1231 1232 1233
	};
	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;

1234 1235 1236 1237 1238 1239
	/* we should bypass data pages to proceed the kworkder jobs */
	if (unlikely(f2fs_cp_error(sbi))) {
		mapping_set_error(cc->rpages[0]->mapping, -EIO);
		goto out_free;
	}

1240 1241 1242 1243 1244 1245
	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.
		 */
1246
		f2fs_down_read(&sbi->node_write);
1247
	} else if (!f2fs_trylock_op(sbi)) {
1248
		goto out_free;
1249
	}
Chao Yu's avatar
Chao Yu committed
1250

1251
	set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
Chao Yu's avatar
Chao Yu committed
1252 1253 1254 1255 1256 1257

	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++) {
1258
		if (data_blkaddr(dn.inode, dn.node_page,
Chao Yu's avatar
Chao Yu committed
1259 1260 1261 1262 1263 1264
					dn.ofs_in_node + i) == NULL_ADDR)
			goto out_put_dnode;
	}

	psize = (loff_t)(cc->rpages[last_index]->index + 1) << PAGE_SHIFT;

1265
	err = f2fs_get_node_info(fio.sbi, dn.nid, &ni, false);
Chao Yu's avatar
Chao Yu committed
1266 1267 1268 1269 1270
	if (err)
		goto out_put_dnode;

	fio.version = ni.version;

1271
	cic = f2fs_kmem_cache_alloc(cic_entry_slab, GFP_F2FS_ZERO, false, sbi);
Chao Yu's avatar
Chao Yu committed
1272 1273 1274 1275 1276
	if (!cic)
		goto out_put_dnode;

	cic->magic = F2FS_COMPRESSED_PAGE_MAGIC;
	cic->inode = inode;
1277
	atomic_set(&cic->pending_pages, cc->valid_nr_cpages);
1278
	cic->rpages = page_array_alloc(cc->inode, cc->cluster_size);
Chao Yu's avatar
Chao Yu committed
1279 1280 1281 1282 1283
	if (!cic->rpages)
		goto out_put_cic;

	cic->nr_rpages = cc->cluster_size;

1284
	for (i = 0; i < cc->valid_nr_cpages; i++) {
Chao Yu's avatar
Chao Yu committed
1285
		f2fs_set_compressed_page(cc->cpages[i], inode,
1286
					cc->rpages[i + 1]->index, cic);
Chao Yu's avatar
Chao Yu committed
1287
		fio.compressed_page = cc->cpages[i];
1288 1289 1290 1291 1292 1293 1294

		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);

Chao Yu's avatar
Chao Yu committed
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
		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;

1312
		blkaddr = f2fs_data_blkaddr(&dn);
1313
		fio.page = cc->rpages[i];
Chao Yu's avatar
Chao Yu committed
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
		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++;

1329
		if (i > cc->valid_nr_cpages) {
Chao Yu's avatar
Chao Yu committed
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
			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);
1354 1355
	f2fs_i_compr_blocks_update(inode, cc->valid_nr_cpages, true);
	add_compr_block_stat(inode, cc->valid_nr_cpages);
Chao Yu's avatar
Chao Yu committed
1356 1357 1358 1359 1360 1361

	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);
1362
	if (IS_NOQUOTA(inode))
1363
		f2fs_up_read(&sbi->node_write);
1364
	else
1365
		f2fs_unlock_op(sbi);
Chao Yu's avatar
Chao Yu committed
1366

1367
	spin_lock(&fi->i_size_lock);
Chao Yu's avatar
Chao Yu committed
1368 1369
	if (fi->last_disk_size < psize)
		fi->last_disk_size = psize;
1370
	spin_unlock(&fi->i_size_lock);
Chao Yu's avatar
Chao Yu committed
1371 1372

	f2fs_put_rpages(cc);
1373 1374
	page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
	cc->cpages = NULL;
1375
	f2fs_destroy_compress_ctx(cc, false);
Chao Yu's avatar
Chao Yu committed
1376 1377 1378
	return 0;

out_destroy_crypt:
1379
	page_array_free(cc->inode, cic->rpages, cc->cluster_size);
Chao Yu's avatar
Chao Yu committed
1380 1381 1382 1383

	for (--i; i >= 0; i--)
		fscrypt_finalize_bounce_page(&cc->cpages[i]);
out_put_cic:
1384
	kmem_cache_free(cic_entry_slab, cic);
Chao Yu's avatar
Chao Yu committed
1385 1386 1387
out_put_dnode:
	f2fs_put_dnode(&dn);
out_unlock_op:
1388
	if (IS_NOQUOTA(inode))
1389
		f2fs_up_read(&sbi->node_write);
1390
	else
1391
		f2fs_unlock_op(sbi);
1392
out_free:
1393
	for (i = 0; i < cc->valid_nr_cpages; i++) {
1394 1395 1396
		f2fs_compress_free_page(cc->cpages[i]);
		cc->cpages[i] = NULL;
	}
1397 1398
	page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
	cc->cpages = NULL;
Chao Yu's avatar
Chao Yu committed
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411
	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);

1412
	f2fs_compress_free_page(page);
Chao Yu's avatar
Chao Yu committed
1413 1414 1415

	dec_page_count(sbi, F2FS_WB_DATA);

1416
	if (atomic_dec_return(&cic->pending_pages))
Chao Yu's avatar
Chao Yu committed
1417 1418 1419 1420
		return;

	for (i = 0; i < cic->nr_rpages; i++) {
		WARN_ON(!cic->rpages[i]);
1421
		clear_page_private_gcing(cic->rpages[i]);
Chao Yu's avatar
Chao Yu committed
1422 1423 1424
		end_page_writeback(cic->rpages[i]);
	}

1425
	page_array_free(cic->inode, cic->rpages, cic->nr_rpages);
1426
	kmem_cache_free(cic_entry_slab, cic);
Chao Yu's avatar
Chao Yu committed
1427 1428 1429 1430 1431 1432 1433 1434
}

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;
1435
	int _submitted, compr_blocks, ret, i;
Chao Yu's avatar
Chao Yu committed
1436 1437

	compr_blocks = f2fs_compressed_blocks(cc);
1438 1439 1440 1441 1442 1443 1444

	for (i = 0; i < cc->cluster_size; i++) {
		if (!cc->rpages[i])
			continue;

		redirty_page_for_writepage(wbc, cc->rpages[i]);
		unlock_page(cc->rpages[i]);
Chao Yu's avatar
Chao Yu committed
1445 1446
	}

1447 1448 1449
	if (compr_blocks < 0)
		return compr_blocks;

Chao Yu's avatar
Chao Yu committed
1450 1451 1452 1453
	for (i = 0; i < cc->cluster_size; i++) {
		if (!cc->rpages[i])
			continue;
retry_write:
1454 1455
		lock_page(cc->rpages[i]);

Chao Yu's avatar
Chao Yu committed
1456
		if (cc->rpages[i]->mapping != mapping) {
1457
continue_unlock:
Chao Yu's avatar
Chao Yu committed
1458 1459 1460 1461
			unlock_page(cc->rpages[i]);
			continue;
		}

1462 1463 1464 1465 1466
		if (!PageDirty(cc->rpages[i]))
			goto continue_unlock;

		if (!clear_page_dirty_for_io(cc->rpages[i]))
			goto continue_unlock;
Chao Yu's avatar
Chao Yu committed
1467 1468 1469

		ret = f2fs_write_single_data_page(cc->rpages[i], &_submitted,
						NULL, NULL, wbc, io_type,
1470
						compr_blocks, false);
Chao Yu's avatar
Chao Yu committed
1471 1472 1473 1474 1475
		if (ret) {
			if (ret == AOP_WRITEPAGE_ACTIVATE) {
				unlock_page(cc->rpages[i]);
				ret = 0;
			} else if (ret == -EAGAIN) {
1476 1477 1478 1479 1480
				/*
				 * for quota file, just redirty left pages to
				 * avoid deadlock caused by cluster update race
				 * from foreground operation.
				 */
1481 1482
				if (IS_NOQUOTA(cc->inode))
					return 0;
Chao Yu's avatar
Chao Yu committed
1483
				ret = 0;
1484
				f2fs_io_schedule_timeout(DEFAULT_IO_TIMEOUT);
Chao Yu's avatar
Chao Yu committed
1485 1486
				goto retry_write;
			}
1487
			return ret;
Chao Yu's avatar
Chao Yu committed
1488 1489 1490 1491
		}

		*submitted += _submitted;
	}
1492 1493 1494

	f2fs_balance_fs(F2FS_M_SB(mapping), true);

Chao Yu's avatar
Chao Yu committed
1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508
	return 0;
}

int f2fs_write_multi_pages(struct compress_ctx *cc,
					int *submitted,
					struct writeback_control *wbc,
					enum iostat_type io_type)
{
	int err;

	*submitted = 0;
	if (cluster_may_compress(cc)) {
		err = f2fs_compress_pages(cc);
		if (err == -EAGAIN) {
1509
			add_compr_block_stat(cc->inode, cc->cluster_size);
Chao Yu's avatar
Chao Yu committed
1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527
			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);
		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:
1528
	f2fs_destroy_compress_ctx(cc, false);
Chao Yu's avatar
Chao Yu committed
1529 1530 1531
	return err;
}

1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570
static inline bool allow_memalloc_for_decomp(struct f2fs_sb_info *sbi,
		bool pre_alloc)
{
	return pre_alloc ^ f2fs_low_mem_mode(sbi);
}

static int f2fs_prepare_decomp_mem(struct decompress_io_ctx *dic,
		bool pre_alloc)
{
	const struct f2fs_compress_ops *cops =
		f2fs_cops[F2FS_I(dic->inode)->i_compress_algorithm];
	int i;

	if (!allow_memalloc_for_decomp(F2FS_I_SB(dic->inode), pre_alloc))
		return 0;

	dic->tpages = page_array_alloc(dic->inode, dic->cluster_size);
	if (!dic->tpages)
		return -ENOMEM;

	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])
			return -ENOMEM;
	}

	dic->rbuf = f2fs_vmap(dic->tpages, dic->cluster_size);
	if (!dic->rbuf)
		return -ENOMEM;

	dic->cbuf = f2fs_vmap(dic->cpages, dic->nr_cpages);
	if (!dic->cbuf)
		return -ENOMEM;

1571 1572
	if (cops->init_decompress_ctx)
		return cops->init_decompress_ctx(dic);
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597

	return 0;
}

static void f2fs_release_decomp_mem(struct decompress_io_ctx *dic,
		bool bypass_destroy_callback, bool pre_alloc)
{
	const struct f2fs_compress_ops *cops =
		f2fs_cops[F2FS_I(dic->inode)->i_compress_algorithm];

	if (!allow_memalloc_for_decomp(F2FS_I_SB(dic->inode), pre_alloc))
		return;

	if (!bypass_destroy_callback && cops->destroy_decompress_ctx)
		cops->destroy_decompress_ctx(dic);

	if (dic->cbuf)
		vm_unmap_ram(dic->cbuf, dic->nr_cpages);

	if (dic->rbuf)
		vm_unmap_ram(dic->rbuf, dic->cluster_size);
}

static void f2fs_free_dic(struct decompress_io_ctx *dic,
		bool bypass_destroy_callback);
1598

Chao Yu's avatar
Chao Yu committed
1599 1600 1601 1602
struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc)
{
	struct decompress_io_ctx *dic;
	pgoff_t start_idx = start_idx_of_cluster(cc);
1603 1604
	struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
	int i, ret;
Chao Yu's avatar
Chao Yu committed
1605

1606
	dic = f2fs_kmem_cache_alloc(dic_entry_slab, GFP_F2FS_ZERO, false, sbi);
Chao Yu's avatar
Chao Yu committed
1607 1608 1609
	if (!dic)
		return ERR_PTR(-ENOMEM);

1610
	dic->rpages = page_array_alloc(cc->inode, cc->cluster_size);
Chao Yu's avatar
Chao Yu committed
1611
	if (!dic->rpages) {
1612
		kmem_cache_free(dic_entry_slab, dic);
Chao Yu's avatar
Chao Yu committed
1613 1614 1615 1616 1617
		return ERR_PTR(-ENOMEM);
	}

	dic->magic = F2FS_COMPRESSED_PAGE_MAGIC;
	dic->inode = cc->inode;
1618
	atomic_set(&dic->remaining_pages, cc->nr_cpages);
Chao Yu's avatar
Chao Yu committed
1619 1620 1621 1622
	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;
1623
	refcount_set(&dic->refcnt, 1);
Chao Yu's avatar
Chao Yu committed
1624
	dic->failed = false;
1625
	dic->need_verity = f2fs_need_verity(cc->inode, start_idx);
Chao Yu's avatar
Chao Yu committed
1626 1627 1628 1629 1630

	for (i = 0; i < dic->cluster_size; i++)
		dic->rpages[i] = cc->rpages[i];
	dic->nr_rpages = cc->cluster_size;

1631
	dic->cpages = page_array_alloc(dic->inode, dic->nr_cpages);
1632 1633
	if (!dic->cpages) {
		ret = -ENOMEM;
Chao Yu's avatar
Chao Yu committed
1634
		goto out_free;
1635
	}
Chao Yu's avatar
Chao Yu committed
1636 1637 1638 1639

	for (i = 0; i < dic->nr_cpages; i++) {
		struct page *page;

1640
		page = f2fs_compress_alloc_page();
1641 1642
		if (!page) {
			ret = -ENOMEM;
Chao Yu's avatar
Chao Yu committed
1643
			goto out_free;
1644
		}
Chao Yu's avatar
Chao Yu committed
1645 1646

		f2fs_set_compressed_page(page, cc->inode,
1647
					start_idx + i + 1, dic);
Chao Yu's avatar
Chao Yu committed
1648 1649 1650
		dic->cpages[i] = page;
	}

1651 1652 1653 1654
	ret = f2fs_prepare_decomp_mem(dic, true);
	if (ret)
		goto out_free;

Chao Yu's avatar
Chao Yu committed
1655 1656 1657
	return dic;

out_free:
1658 1659
	f2fs_free_dic(dic, true);
	return ERR_PTR(ret);
Chao Yu's avatar
Chao Yu committed
1660 1661
}

1662 1663
static void f2fs_free_dic(struct decompress_io_ctx *dic,
		bool bypass_destroy_callback)
Chao Yu's avatar
Chao Yu committed
1664 1665 1666
{
	int i;

1667 1668
	f2fs_release_decomp_mem(dic, bypass_destroy_callback, true);

Chao Yu's avatar
Chao Yu committed
1669 1670 1671 1672
	if (dic->tpages) {
		for (i = 0; i < dic->cluster_size; i++) {
			if (dic->rpages[i])
				continue;
1673 1674
			if (!dic->tpages[i])
				continue;
1675
			f2fs_compress_free_page(dic->tpages[i]);
Chao Yu's avatar
Chao Yu committed
1676
		}
1677
		page_array_free(dic->inode, dic->tpages, dic->cluster_size);
Chao Yu's avatar
Chao Yu committed
1678 1679 1680 1681 1682 1683
	}

	if (dic->cpages) {
		for (i = 0; i < dic->nr_cpages; i++) {
			if (!dic->cpages[i])
				continue;
1684
			f2fs_compress_free_page(dic->cpages[i]);
Chao Yu's avatar
Chao Yu committed
1685
		}
1686
		page_array_free(dic->inode, dic->cpages, dic->nr_cpages);
Chao Yu's avatar
Chao Yu committed
1687 1688
	}

1689
	page_array_free(dic->inode, dic->rpages, dic->nr_rpages);
1690
	kmem_cache_free(dic_entry_slab, dic);
Chao Yu's avatar
Chao Yu committed
1691 1692
}

1693
static void f2fs_late_free_dic(struct work_struct *work)
1694
{
1695 1696 1697 1698 1699 1700 1701
	struct decompress_io_ctx *dic =
		container_of(work, struct decompress_io_ctx, free_work);

	f2fs_free_dic(dic, false);
}

static void f2fs_put_dic(struct decompress_io_ctx *dic, bool in_task)
1702
{
1703 1704 1705 1706 1707 1708 1709 1710 1711
	if (refcount_dec_and_test(&dic->refcnt)) {
		if (in_task) {
			f2fs_free_dic(dic, false);
		} else {
			INIT_WORK(&dic->free_work, f2fs_late_free_dic);
			queue_work(F2FS_I_SB(dic->inode)->post_read_wq,
					&dic->free_work);
		}
	}
1712 1713 1714 1715 1716 1717
}

/*
 * Update and unlock the cluster's pagecache pages, and release the reference to
 * the decompress_io_ctx that was being held for I/O completion.
 */
1718 1719
static void __f2fs_decompress_end_io(struct decompress_io_ctx *dic, bool failed,
				bool in_task)
Chao Yu's avatar
Chao Yu committed
1720 1721 1722
{
	int i;

1723 1724
	for (i = 0; i < dic->cluster_size; i++) {
		struct page *rpage = dic->rpages[i];
Chao Yu's avatar
Chao Yu committed
1725 1726 1727 1728

		if (!rpage)
			continue;

1729 1730 1731 1732 1733 1734
		/* PG_error was set if verity failed. */
		if (failed || PageError(rpage)) {
			ClearPageUptodate(rpage);
			/* will re-read again later */
			ClearPageError(rpage);
		} else {
1735
			SetPageUptodate(rpage);
Chao Yu's avatar
Chao Yu committed
1736 1737 1738
		}
		unlock_page(rpage);
	}
1739

1740
	f2fs_put_dic(dic, in_task);
1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
}

static void f2fs_verify_cluster(struct work_struct *work)
{
	struct decompress_io_ctx *dic =
		container_of(work, struct decompress_io_ctx, verity_work);
	int i;

	/* Verify the cluster's decompressed pages with fs-verity. */
	for (i = 0; i < dic->cluster_size; i++) {
		struct page *rpage = dic->rpages[i];

		if (rpage && !fsverity_verify_page(rpage))
			SetPageError(rpage);
	}

1757
	__f2fs_decompress_end_io(dic, false, true);
1758 1759 1760 1761 1762 1763
}

/*
 * This is called when a compressed cluster has been decompressed
 * (or failed to be read and/or decompressed).
 */
1764 1765
void f2fs_decompress_end_io(struct decompress_io_ctx *dic, bool failed,
				bool in_task)
1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776
{
	if (!failed && dic->need_verity) {
		/*
		 * Note that to avoid deadlocks, the verity work can't be done
		 * on the decompression workqueue.  This is because verifying
		 * the data pages can involve reading metadata pages from the
		 * file, and these metadata pages may be compressed.
		 */
		INIT_WORK(&dic->verity_work, f2fs_verify_cluster);
		fsverity_enqueue_verify_work(&dic->verity_work);
	} else {
1777
		__f2fs_decompress_end_io(dic, failed, in_task);
1778 1779 1780 1781 1782 1783 1784 1785
	}
}

/*
 * Put a reference to a compressed page's decompress_io_ctx.
 *
 * This is called when the page is no longer needed and can be freed.
 */
1786
void f2fs_put_page_dic(struct page *page, bool in_task)
1787 1788 1789 1790
{
	struct decompress_io_ctx *dic =
			(struct decompress_io_ctx *)page_private(page);

1791
	f2fs_put_dic(dic, in_task);
Chao Yu's avatar
Chao Yu committed
1792
}
1793

1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817
/*
 * check whether cluster blocks are contiguous, and add extent cache entry
 * only if cluster blocks are logically and physically contiguous.
 */
unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn)
{
	bool compressed = f2fs_data_blkaddr(dn) == COMPRESS_ADDR;
	int i = compressed ? 1 : 0;
	block_t first_blkaddr = data_blkaddr(dn->inode, dn->node_page,
						dn->ofs_in_node + i);

	for (i += 1; i < F2FS_I(dn->inode)->i_cluster_size; i++) {
		block_t blkaddr = data_blkaddr(dn->inode, dn->node_page,
						dn->ofs_in_node + i);

		if (!__is_valid_data_blkaddr(blkaddr))
			break;
		if (first_blkaddr + i - (compressed ? 1 : 0) != blkaddr)
			return 0;
	}

	return compressed ? i - 1 : i;
}

1818
const struct address_space_operations f2fs_compress_aops = {
1819
	.release_folio = f2fs_release_folio,
1820
	.invalidate_folio = f2fs_invalidate_folio,
1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903
};

struct address_space *COMPRESS_MAPPING(struct f2fs_sb_info *sbi)
{
	return sbi->compress_inode->i_mapping;
}

void f2fs_invalidate_compress_page(struct f2fs_sb_info *sbi, block_t blkaddr)
{
	if (!sbi->compress_inode)
		return;
	invalidate_mapping_pages(COMPRESS_MAPPING(sbi), blkaddr, blkaddr);
}

void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
						nid_t ino, block_t blkaddr)
{
	struct page *cpage;
	int ret;

	if (!test_opt(sbi, COMPRESS_CACHE))
		return;

	if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE_READ))
		return;

	if (!f2fs_available_free_memory(sbi, COMPRESS_PAGE))
		return;

	cpage = find_get_page(COMPRESS_MAPPING(sbi), blkaddr);
	if (cpage) {
		f2fs_put_page(cpage, 0);
		return;
	}

	cpage = alloc_page(__GFP_NOWARN | __GFP_IO);
	if (!cpage)
		return;

	ret = add_to_page_cache_lru(cpage, COMPRESS_MAPPING(sbi),
						blkaddr, GFP_NOFS);
	if (ret) {
		f2fs_put_page(cpage, 0);
		return;
	}

	set_page_private_data(cpage, ino);

	if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE_READ))
		goto out;

	memcpy(page_address(cpage), page_address(page), PAGE_SIZE);
	SetPageUptodate(cpage);
out:
	f2fs_put_page(cpage, 1);
}

bool f2fs_load_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
								block_t blkaddr)
{
	struct page *cpage;
	bool hitted = false;

	if (!test_opt(sbi, COMPRESS_CACHE))
		return false;

	cpage = f2fs_pagecache_get_page(COMPRESS_MAPPING(sbi),
				blkaddr, FGP_LOCK | FGP_NOWAIT, GFP_NOFS);
	if (cpage) {
		if (PageUptodate(cpage)) {
			atomic_inc(&sbi->compress_page_hit);
			memcpy(page_address(page),
				page_address(cpage), PAGE_SIZE);
			hitted = true;
		}
		f2fs_put_page(cpage, 1);
	}

	return hitted;
}

void f2fs_invalidate_compress_pages(struct f2fs_sb_info *sbi, nid_t ino)
{
1904
	struct address_space *mapping = COMPRESS_MAPPING(sbi);
1905
	struct folio_batch fbatch;
1906 1907 1908 1909 1910 1911
	pgoff_t index = 0;
	pgoff_t end = MAX_BLKADDR(sbi);

	if (!mapping->nrpages)
		return;

1912
	folio_batch_init(&fbatch);
1913 1914

	do {
1915
		unsigned int nr, i;
1916

1917 1918
		nr = filemap_get_folios(mapping, &index, end - 1, &fbatch);
		if (!nr)
1919 1920
			break;

1921 1922
		for (i = 0; i < nr; i++) {
			struct folio *folio = fbatch.folios[i];
1923

1924 1925 1926
			folio_lock(folio);
			if (folio->mapping != mapping) {
				folio_unlock(folio);
1927 1928 1929
				continue;
			}

1930 1931
			if (ino != get_page_private_data(&folio->page)) {
				folio_unlock(folio);
1932 1933 1934
				continue;
			}

1935 1936
			generic_error_remove_page(mapping, &folio->page);
			folio_unlock(folio);
1937
		}
1938
		folio_batch_release(&fbatch);
1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970
		cond_resched();
	} while (index < end);
}

int f2fs_init_compress_inode(struct f2fs_sb_info *sbi)
{
	struct inode *inode;

	if (!test_opt(sbi, COMPRESS_CACHE))
		return 0;

	inode = f2fs_iget(sbi->sb, F2FS_COMPRESS_INO(sbi));
	if (IS_ERR(inode))
		return PTR_ERR(inode);
	sbi->compress_inode = inode;

	sbi->compress_percent = COMPRESS_PERCENT;
	sbi->compress_watermark = COMPRESS_WATERMARK;

	atomic_set(&sbi->compress_page_hit, 0);

	return 0;
}

void f2fs_destroy_compress_inode(struct f2fs_sb_info *sbi)
{
	if (!sbi->compress_inode)
		return;
	iput(sbi->compress_inode);
	sbi->compress_inode = NULL;
}

1971 1972 1973 1974 1975
int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi)
{
	dev_t dev = sbi->sb->s_bdev->bd_dev;
	char slab_name[32];

1976 1977 1978
	if (!f2fs_sb_has_compression(sbi))
		return 0;

1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994
	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);
}
1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045

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();
}