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 16 #include "f2fs.h" 17 #include "node.h" 18 #include <trace/events/f2fs.h> 19 20 static struct kmem_cache *cic_entry_slab; 21 static struct kmem_cache *dic_entry_slab; 22 23 static void *page_array_alloc(struct inode *inode, int nr) 24 { 25 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 26 unsigned int size = sizeof(struct page *) * nr; 27 28 if (likely(size <= sbi->page_array_slab_size)) 29 return kmem_cache_zalloc(sbi->page_array_slab, GFP_NOFS); 30 return f2fs_kzalloc(sbi, size, GFP_NOFS); 31 } 32 33 static void page_array_free(struct inode *inode, void *pages, int nr) 34 { 35 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 36 unsigned int size = sizeof(struct page *) * nr; 37 38 if (!pages) 39 return; 40 41 if (likely(size <= sbi->page_array_slab_size)) 42 kmem_cache_free(sbi->page_array_slab, pages); 43 else 44 kfree(pages); 45 } 46 47 struct f2fs_compress_ops { 48 int (*init_compress_ctx)(struct compress_ctx *cc); 49 void (*destroy_compress_ctx)(struct compress_ctx *cc); 50 int (*compress_pages)(struct compress_ctx *cc); 51 int (*init_decompress_ctx)(struct decompress_io_ctx *dic); 52 void (*destroy_decompress_ctx)(struct decompress_io_ctx *dic); 53 int (*decompress_pages)(struct decompress_io_ctx *dic); 54 }; 55 56 static unsigned int offset_in_cluster(struct compress_ctx *cc, pgoff_t index) 57 { 58 return index & (cc->cluster_size - 1); 59 } 60 61 static pgoff_t cluster_idx(struct compress_ctx *cc, pgoff_t index) 62 { 63 return index >> cc->log_cluster_size; 64 } 65 66 static pgoff_t start_idx_of_cluster(struct compress_ctx *cc) 67 { 68 return cc->cluster_idx << cc->log_cluster_size; 69 } 70 71 bool f2fs_is_compressed_page(struct page *page) 72 { 73 if (!PagePrivate(page)) 74 return false; 75 if (!page_private(page)) 76 return false; 77 if (IS_ATOMIC_WRITTEN_PAGE(page) || IS_DUMMY_WRITTEN_PAGE(page)) 78 return false; 79 /* 80 * page->private may be set with pid. 81 * pid_max is enough to check if it is traced. 82 */ 83 if (IS_IO_TRACED_PAGE(page)) 84 return false; 85 86 f2fs_bug_on(F2FS_M_SB(page->mapping), 87 *((u32 *)page_private(page)) != F2FS_COMPRESSED_PAGE_MAGIC); 88 return true; 89 } 90 91 static void f2fs_set_compressed_page(struct page *page, 92 struct inode *inode, pgoff_t index, void *data) 93 { 94 SetPagePrivate(page); 95 set_page_private(page, (unsigned long)data); 96 97 /* i_crypto_info and iv index */ 98 page->index = index; 99 page->mapping = inode->i_mapping; 100 } 101 102 static void f2fs_drop_rpages(struct compress_ctx *cc, int len, bool unlock) 103 { 104 int i; 105 106 for (i = 0; i < len; i++) { 107 if (!cc->rpages[i]) 108 continue; 109 if (unlock) 110 unlock_page(cc->rpages[i]); 111 else 112 put_page(cc->rpages[i]); 113 } 114 } 115 116 static void f2fs_put_rpages(struct compress_ctx *cc) 117 { 118 f2fs_drop_rpages(cc, cc->cluster_size, false); 119 } 120 121 static void f2fs_unlock_rpages(struct compress_ctx *cc, int len) 122 { 123 f2fs_drop_rpages(cc, len, true); 124 } 125 126 static void f2fs_put_rpages_mapping(struct address_space *mapping, 127 pgoff_t start, int len) 128 { 129 int i; 130 131 for (i = 0; i < len; i++) { 132 struct page *page = find_get_page(mapping, start + i); 133 134 put_page(page); 135 put_page(page); 136 } 137 } 138 139 static void f2fs_put_rpages_wbc(struct compress_ctx *cc, 140 struct writeback_control *wbc, bool redirty, int unlock) 141 { 142 unsigned int i; 143 144 for (i = 0; i < cc->cluster_size; i++) { 145 if (!cc->rpages[i]) 146 continue; 147 if (redirty) 148 redirty_page_for_writepage(wbc, cc->rpages[i]); 149 f2fs_put_page(cc->rpages[i], unlock); 150 } 151 } 152 153 struct page *f2fs_compress_control_page(struct page *page) 154 { 155 return ((struct compress_io_ctx *)page_private(page))->rpages[0]; 156 } 157 158 int f2fs_init_compress_ctx(struct compress_ctx *cc) 159 { 160 if (cc->rpages) 161 return 0; 162 163 cc->rpages = page_array_alloc(cc->inode, cc->cluster_size); 164 return cc->rpages ? 0 : -ENOMEM; 165 } 166 167 void f2fs_destroy_compress_ctx(struct compress_ctx *cc) 168 { 169 page_array_free(cc->inode, cc->rpages, cc->cluster_size); 170 cc->rpages = NULL; 171 cc->nr_rpages = 0; 172 cc->nr_cpages = 0; 173 cc->cluster_idx = NULL_CLUSTER; 174 } 175 176 void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct page *page) 177 { 178 unsigned int cluster_ofs; 179 180 if (!f2fs_cluster_can_merge_page(cc, page->index)) 181 f2fs_bug_on(F2FS_I_SB(cc->inode), 1); 182 183 cluster_ofs = offset_in_cluster(cc, page->index); 184 cc->rpages[cluster_ofs] = page; 185 cc->nr_rpages++; 186 cc->cluster_idx = cluster_idx(cc, page->index); 187 } 188 189 #ifdef CONFIG_F2FS_FS_LZO 190 static int lzo_init_compress_ctx(struct compress_ctx *cc) 191 { 192 cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode), 193 LZO1X_MEM_COMPRESS, GFP_NOFS); 194 if (!cc->private) 195 return -ENOMEM; 196 197 cc->clen = lzo1x_worst_compress(PAGE_SIZE << cc->log_cluster_size); 198 return 0; 199 } 200 201 static void lzo_destroy_compress_ctx(struct compress_ctx *cc) 202 { 203 kvfree(cc->private); 204 cc->private = NULL; 205 } 206 207 static int lzo_compress_pages(struct compress_ctx *cc) 208 { 209 int ret; 210 211 ret = lzo1x_1_compress(cc->rbuf, cc->rlen, cc->cbuf->cdata, 212 &cc->clen, cc->private); 213 if (ret != LZO_E_OK) { 214 printk_ratelimited("%sF2FS-fs (%s): lzo compress failed, ret:%d\n", 215 KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id, ret); 216 return -EIO; 217 } 218 return 0; 219 } 220 221 static int lzo_decompress_pages(struct decompress_io_ctx *dic) 222 { 223 int ret; 224 225 ret = lzo1x_decompress_safe(dic->cbuf->cdata, dic->clen, 226 dic->rbuf, &dic->rlen); 227 if (ret != LZO_E_OK) { 228 printk_ratelimited("%sF2FS-fs (%s): lzo decompress failed, ret:%d\n", 229 KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id, ret); 230 return -EIO; 231 } 232 233 if (dic->rlen != PAGE_SIZE << dic->log_cluster_size) { 234 printk_ratelimited("%sF2FS-fs (%s): lzo invalid rlen:%zu, " 235 "expected:%lu\n", KERN_ERR, 236 F2FS_I_SB(dic->inode)->sb->s_id, 237 dic->rlen, 238 PAGE_SIZE << dic->log_cluster_size); 239 return -EIO; 240 } 241 return 0; 242 } 243 244 static const struct f2fs_compress_ops f2fs_lzo_ops = { 245 .init_compress_ctx = lzo_init_compress_ctx, 246 .destroy_compress_ctx = lzo_destroy_compress_ctx, 247 .compress_pages = lzo_compress_pages, 248 .decompress_pages = lzo_decompress_pages, 249 }; 250 #endif 251 252 #ifdef CONFIG_F2FS_FS_LZ4 253 static int lz4_init_compress_ctx(struct compress_ctx *cc) 254 { 255 unsigned int size = LZ4_MEM_COMPRESS; 256 257 #ifdef CONFIG_F2FS_FS_LZ4HC 258 if (F2FS_I(cc->inode)->i_compress_flag >> COMPRESS_LEVEL_OFFSET) 259 size = LZ4HC_MEM_COMPRESS; 260 #endif 261 262 cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode), size, GFP_NOFS); 263 if (!cc->private) 264 return -ENOMEM; 265 266 /* 267 * we do not change cc->clen to LZ4_compressBound(inputsize) to 268 * adapt worst compress case, because lz4 compressor can handle 269 * output budget properly. 270 */ 271 cc->clen = cc->rlen - PAGE_SIZE - COMPRESS_HEADER_SIZE; 272 return 0; 273 } 274 275 static void lz4_destroy_compress_ctx(struct compress_ctx *cc) 276 { 277 kvfree(cc->private); 278 cc->private = NULL; 279 } 280 281 #ifdef CONFIG_F2FS_FS_LZ4HC 282 static int lz4hc_compress_pages(struct compress_ctx *cc) 283 { 284 unsigned char level = F2FS_I(cc->inode)->i_compress_flag >> 285 COMPRESS_LEVEL_OFFSET; 286 int len; 287 288 if (level) 289 len = LZ4_compress_HC(cc->rbuf, cc->cbuf->cdata, cc->rlen, 290 cc->clen, level, cc->private); 291 else 292 len = LZ4_compress_default(cc->rbuf, cc->cbuf->cdata, cc->rlen, 293 cc->clen, cc->private); 294 if (!len) 295 return -EAGAIN; 296 297 cc->clen = len; 298 return 0; 299 } 300 #endif 301 302 static int lz4_compress_pages(struct compress_ctx *cc) 303 { 304 int len; 305 306 #ifdef CONFIG_F2FS_FS_LZ4HC 307 return lz4hc_compress_pages(cc); 308 #endif 309 len = LZ4_compress_default(cc->rbuf, cc->cbuf->cdata, cc->rlen, 310 cc->clen, cc->private); 311 if (!len) 312 return -EAGAIN; 313 314 cc->clen = len; 315 return 0; 316 } 317 318 static int lz4_decompress_pages(struct decompress_io_ctx *dic) 319 { 320 int ret; 321 322 ret = LZ4_decompress_safe(dic->cbuf->cdata, dic->rbuf, 323 dic->clen, dic->rlen); 324 if (ret < 0) { 325 printk_ratelimited("%sF2FS-fs (%s): lz4 decompress failed, ret:%d\n", 326 KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id, ret); 327 return -EIO; 328 } 329 330 if (ret != PAGE_SIZE << dic->log_cluster_size) { 331 printk_ratelimited("%sF2FS-fs (%s): lz4 invalid rlen:%zu, " 332 "expected:%lu\n", KERN_ERR, 333 F2FS_I_SB(dic->inode)->sb->s_id, 334 dic->rlen, 335 PAGE_SIZE << dic->log_cluster_size); 336 return -EIO; 337 } 338 return 0; 339 } 340 341 static const struct f2fs_compress_ops f2fs_lz4_ops = { 342 .init_compress_ctx = lz4_init_compress_ctx, 343 .destroy_compress_ctx = lz4_destroy_compress_ctx, 344 .compress_pages = lz4_compress_pages, 345 .decompress_pages = lz4_decompress_pages, 346 }; 347 #endif 348 349 #ifdef CONFIG_F2FS_FS_ZSTD 350 #define F2FS_ZSTD_DEFAULT_CLEVEL 1 351 352 static int zstd_init_compress_ctx(struct compress_ctx *cc) 353 { 354 ZSTD_parameters params; 355 ZSTD_CStream *stream; 356 void *workspace; 357 unsigned int workspace_size; 358 unsigned char level = F2FS_I(cc->inode)->i_compress_flag >> 359 COMPRESS_LEVEL_OFFSET; 360 361 if (!level) 362 level = F2FS_ZSTD_DEFAULT_CLEVEL; 363 364 params = ZSTD_getParams(level, cc->rlen, 0); 365 workspace_size = ZSTD_CStreamWorkspaceBound(params.cParams); 366 367 workspace = f2fs_kvmalloc(F2FS_I_SB(cc->inode), 368 workspace_size, GFP_NOFS); 369 if (!workspace) 370 return -ENOMEM; 371 372 stream = ZSTD_initCStream(params, 0, workspace, workspace_size); 373 if (!stream) { 374 printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_initCStream failed\n", 375 KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id, 376 __func__); 377 kvfree(workspace); 378 return -EIO; 379 } 380 381 cc->private = workspace; 382 cc->private2 = stream; 383 384 cc->clen = cc->rlen - PAGE_SIZE - COMPRESS_HEADER_SIZE; 385 return 0; 386 } 387 388 static void zstd_destroy_compress_ctx(struct compress_ctx *cc) 389 { 390 kvfree(cc->private); 391 cc->private = NULL; 392 cc->private2 = NULL; 393 } 394 395 static int zstd_compress_pages(struct compress_ctx *cc) 396 { 397 ZSTD_CStream *stream = cc->private2; 398 ZSTD_inBuffer inbuf; 399 ZSTD_outBuffer outbuf; 400 int src_size = cc->rlen; 401 int dst_size = src_size - PAGE_SIZE - COMPRESS_HEADER_SIZE; 402 int ret; 403 404 inbuf.pos = 0; 405 inbuf.src = cc->rbuf; 406 inbuf.size = src_size; 407 408 outbuf.pos = 0; 409 outbuf.dst = cc->cbuf->cdata; 410 outbuf.size = dst_size; 411 412 ret = ZSTD_compressStream(stream, &outbuf, &inbuf); 413 if (ZSTD_isError(ret)) { 414 printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_compressStream failed, ret: %d\n", 415 KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id, 416 __func__, ZSTD_getErrorCode(ret)); 417 return -EIO; 418 } 419 420 ret = ZSTD_endStream(stream, &outbuf); 421 if (ZSTD_isError(ret)) { 422 printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_endStream returned %d\n", 423 KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id, 424 __func__, ZSTD_getErrorCode(ret)); 425 return -EIO; 426 } 427 428 /* 429 * there is compressed data remained in intermediate buffer due to 430 * no more space in cbuf.cdata 431 */ 432 if (ret) 433 return -EAGAIN; 434 435 cc->clen = outbuf.pos; 436 return 0; 437 } 438 439 static int zstd_init_decompress_ctx(struct decompress_io_ctx *dic) 440 { 441 ZSTD_DStream *stream; 442 void *workspace; 443 unsigned int workspace_size; 444 unsigned int max_window_size = 445 MAX_COMPRESS_WINDOW_SIZE(dic->log_cluster_size); 446 447 workspace_size = ZSTD_DStreamWorkspaceBound(max_window_size); 448 449 workspace = f2fs_kvmalloc(F2FS_I_SB(dic->inode), 450 workspace_size, GFP_NOFS); 451 if (!workspace) 452 return -ENOMEM; 453 454 stream = ZSTD_initDStream(max_window_size, workspace, workspace_size); 455 if (!stream) { 456 printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_initDStream failed\n", 457 KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id, 458 __func__); 459 kvfree(workspace); 460 return -EIO; 461 } 462 463 dic->private = workspace; 464 dic->private2 = stream; 465 466 return 0; 467 } 468 469 static void zstd_destroy_decompress_ctx(struct decompress_io_ctx *dic) 470 { 471 kvfree(dic->private); 472 dic->private = NULL; 473 dic->private2 = NULL; 474 } 475 476 static int zstd_decompress_pages(struct decompress_io_ctx *dic) 477 { 478 ZSTD_DStream *stream = dic->private2; 479 ZSTD_inBuffer inbuf; 480 ZSTD_outBuffer outbuf; 481 int ret; 482 483 inbuf.pos = 0; 484 inbuf.src = dic->cbuf->cdata; 485 inbuf.size = dic->clen; 486 487 outbuf.pos = 0; 488 outbuf.dst = dic->rbuf; 489 outbuf.size = dic->rlen; 490 491 ret = ZSTD_decompressStream(stream, &outbuf, &inbuf); 492 if (ZSTD_isError(ret)) { 493 printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_compressStream failed, ret: %d\n", 494 KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id, 495 __func__, ZSTD_getErrorCode(ret)); 496 return -EIO; 497 } 498 499 if (dic->rlen != outbuf.pos) { 500 printk_ratelimited("%sF2FS-fs (%s): %s ZSTD invalid rlen:%zu, " 501 "expected:%lu\n", KERN_ERR, 502 F2FS_I_SB(dic->inode)->sb->s_id, 503 __func__, dic->rlen, 504 PAGE_SIZE << dic->log_cluster_size); 505 return -EIO; 506 } 507 508 return 0; 509 } 510 511 static const struct f2fs_compress_ops f2fs_zstd_ops = { 512 .init_compress_ctx = zstd_init_compress_ctx, 513 .destroy_compress_ctx = zstd_destroy_compress_ctx, 514 .compress_pages = zstd_compress_pages, 515 .init_decompress_ctx = zstd_init_decompress_ctx, 516 .destroy_decompress_ctx = zstd_destroy_decompress_ctx, 517 .decompress_pages = zstd_decompress_pages, 518 }; 519 #endif 520 521 #ifdef CONFIG_F2FS_FS_LZO 522 #ifdef CONFIG_F2FS_FS_LZORLE 523 static int lzorle_compress_pages(struct compress_ctx *cc) 524 { 525 int ret; 526 527 ret = lzorle1x_1_compress(cc->rbuf, cc->rlen, cc->cbuf->cdata, 528 &cc->clen, cc->private); 529 if (ret != LZO_E_OK) { 530 printk_ratelimited("%sF2FS-fs (%s): lzo-rle compress failed, ret:%d\n", 531 KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id, ret); 532 return -EIO; 533 } 534 return 0; 535 } 536 537 static const struct f2fs_compress_ops f2fs_lzorle_ops = { 538 .init_compress_ctx = lzo_init_compress_ctx, 539 .destroy_compress_ctx = lzo_destroy_compress_ctx, 540 .compress_pages = lzorle_compress_pages, 541 .decompress_pages = lzo_decompress_pages, 542 }; 543 #endif 544 #endif 545 546 static const struct f2fs_compress_ops *f2fs_cops[COMPRESS_MAX] = { 547 #ifdef CONFIG_F2FS_FS_LZO 548 &f2fs_lzo_ops, 549 #else 550 NULL, 551 #endif 552 #ifdef CONFIG_F2FS_FS_LZ4 553 &f2fs_lz4_ops, 554 #else 555 NULL, 556 #endif 557 #ifdef CONFIG_F2FS_FS_ZSTD 558 &f2fs_zstd_ops, 559 #else 560 NULL, 561 #endif 562 #if defined(CONFIG_F2FS_FS_LZO) && defined(CONFIG_F2FS_FS_LZORLE) 563 &f2fs_lzorle_ops, 564 #else 565 NULL, 566 #endif 567 }; 568 569 bool f2fs_is_compress_backend_ready(struct inode *inode) 570 { 571 if (!f2fs_compressed_file(inode)) 572 return true; 573 return f2fs_cops[F2FS_I(inode)->i_compress_algorithm]; 574 } 575 576 static mempool_t *compress_page_pool; 577 static int num_compress_pages = 512; 578 module_param(num_compress_pages, uint, 0444); 579 MODULE_PARM_DESC(num_compress_pages, 580 "Number of intermediate compress pages to preallocate"); 581 582 int f2fs_init_compress_mempool(void) 583 { 584 compress_page_pool = mempool_create_page_pool(num_compress_pages, 0); 585 if (!compress_page_pool) 586 return -ENOMEM; 587 588 return 0; 589 } 590 591 void f2fs_destroy_compress_mempool(void) 592 { 593 mempool_destroy(compress_page_pool); 594 } 595 596 static struct page *f2fs_compress_alloc_page(void) 597 { 598 struct page *page; 599 600 page = mempool_alloc(compress_page_pool, GFP_NOFS); 601 lock_page(page); 602 603 return page; 604 } 605 606 static void f2fs_compress_free_page(struct page *page) 607 { 608 if (!page) 609 return; 610 set_page_private(page, (unsigned long)NULL); 611 ClearPagePrivate(page); 612 page->mapping = NULL; 613 unlock_page(page); 614 mempool_free(page, compress_page_pool); 615 } 616 617 #define MAX_VMAP_RETRIES 3 618 619 static void *f2fs_vmap(struct page **pages, unsigned int count) 620 { 621 int i; 622 void *buf = NULL; 623 624 for (i = 0; i < MAX_VMAP_RETRIES; i++) { 625 buf = vm_map_ram(pages, count, -1); 626 if (buf) 627 break; 628 vm_unmap_aliases(); 629 } 630 return buf; 631 } 632 633 static int f2fs_compress_pages(struct compress_ctx *cc) 634 { 635 struct f2fs_inode_info *fi = F2FS_I(cc->inode); 636 const struct f2fs_compress_ops *cops = 637 f2fs_cops[fi->i_compress_algorithm]; 638 unsigned int max_len, new_nr_cpages; 639 struct page **new_cpages; 640 u32 chksum = 0; 641 int i, ret; 642 643 trace_f2fs_compress_pages_start(cc->inode, cc->cluster_idx, 644 cc->cluster_size, fi->i_compress_algorithm); 645 646 if (cops->init_compress_ctx) { 647 ret = cops->init_compress_ctx(cc); 648 if (ret) 649 goto out; 650 } 651 652 max_len = COMPRESS_HEADER_SIZE + cc->clen; 653 cc->nr_cpages = DIV_ROUND_UP(max_len, PAGE_SIZE); 654 655 cc->cpages = page_array_alloc(cc->inode, cc->nr_cpages); 656 if (!cc->cpages) { 657 ret = -ENOMEM; 658 goto destroy_compress_ctx; 659 } 660 661 for (i = 0; i < cc->nr_cpages; i++) { 662 cc->cpages[i] = f2fs_compress_alloc_page(); 663 if (!cc->cpages[i]) { 664 ret = -ENOMEM; 665 goto out_free_cpages; 666 } 667 } 668 669 cc->rbuf = f2fs_vmap(cc->rpages, cc->cluster_size); 670 if (!cc->rbuf) { 671 ret = -ENOMEM; 672 goto out_free_cpages; 673 } 674 675 cc->cbuf = f2fs_vmap(cc->cpages, cc->nr_cpages); 676 if (!cc->cbuf) { 677 ret = -ENOMEM; 678 goto out_vunmap_rbuf; 679 } 680 681 ret = cops->compress_pages(cc); 682 if (ret) 683 goto out_vunmap_cbuf; 684 685 max_len = PAGE_SIZE * (cc->cluster_size - 1) - COMPRESS_HEADER_SIZE; 686 687 if (cc->clen > max_len) { 688 ret = -EAGAIN; 689 goto out_vunmap_cbuf; 690 } 691 692 cc->cbuf->clen = cpu_to_le32(cc->clen); 693 694 if (fi->i_compress_flag & 1 << COMPRESS_CHKSUM) 695 chksum = f2fs_crc32(F2FS_I_SB(cc->inode), 696 cc->cbuf->cdata, cc->clen); 697 cc->cbuf->chksum = cpu_to_le32(chksum); 698 699 for (i = 0; i < COMPRESS_DATA_RESERVED_SIZE; i++) 700 cc->cbuf->reserved[i] = cpu_to_le32(0); 701 702 new_nr_cpages = DIV_ROUND_UP(cc->clen + COMPRESS_HEADER_SIZE, PAGE_SIZE); 703 704 /* Now we're going to cut unnecessary tail pages */ 705 new_cpages = page_array_alloc(cc->inode, new_nr_cpages); 706 if (!new_cpages) { 707 ret = -ENOMEM; 708 goto out_vunmap_cbuf; 709 } 710 711 /* zero out any unused part of the last page */ 712 memset(&cc->cbuf->cdata[cc->clen], 0, 713 (new_nr_cpages * PAGE_SIZE) - 714 (cc->clen + COMPRESS_HEADER_SIZE)); 715 716 vm_unmap_ram(cc->cbuf, cc->nr_cpages); 717 vm_unmap_ram(cc->rbuf, cc->cluster_size); 718 719 for (i = 0; i < cc->nr_cpages; i++) { 720 if (i < new_nr_cpages) { 721 new_cpages[i] = cc->cpages[i]; 722 continue; 723 } 724 f2fs_compress_free_page(cc->cpages[i]); 725 cc->cpages[i] = NULL; 726 } 727 728 if (cops->destroy_compress_ctx) 729 cops->destroy_compress_ctx(cc); 730 731 page_array_free(cc->inode, cc->cpages, cc->nr_cpages); 732 cc->cpages = new_cpages; 733 cc->nr_cpages = new_nr_cpages; 734 735 trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx, 736 cc->clen, ret); 737 return 0; 738 739 out_vunmap_cbuf: 740 vm_unmap_ram(cc->cbuf, cc->nr_cpages); 741 out_vunmap_rbuf: 742 vm_unmap_ram(cc->rbuf, cc->cluster_size); 743 out_free_cpages: 744 for (i = 0; i < cc->nr_cpages; i++) { 745 if (cc->cpages[i]) 746 f2fs_compress_free_page(cc->cpages[i]); 747 } 748 page_array_free(cc->inode, cc->cpages, cc->nr_cpages); 749 cc->cpages = NULL; 750 destroy_compress_ctx: 751 if (cops->destroy_compress_ctx) 752 cops->destroy_compress_ctx(cc); 753 out: 754 trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx, 755 cc->clen, ret); 756 return ret; 757 } 758 759 void f2fs_decompress_pages(struct bio *bio, struct page *page, bool verity) 760 { 761 struct decompress_io_ctx *dic = 762 (struct decompress_io_ctx *)page_private(page); 763 struct f2fs_sb_info *sbi = F2FS_I_SB(dic->inode); 764 struct f2fs_inode_info *fi= F2FS_I(dic->inode); 765 const struct f2fs_compress_ops *cops = 766 f2fs_cops[fi->i_compress_algorithm]; 767 int ret; 768 int i; 769 770 dec_page_count(sbi, F2FS_RD_DATA); 771 772 if (bio->bi_status || PageError(page)) 773 dic->failed = true; 774 775 if (atomic_dec_return(&dic->pending_pages)) 776 return; 777 778 trace_f2fs_decompress_pages_start(dic->inode, dic->cluster_idx, 779 dic->cluster_size, fi->i_compress_algorithm); 780 781 /* submit partial compressed pages */ 782 if (dic->failed) { 783 ret = -EIO; 784 goto out_free_dic; 785 } 786 787 dic->tpages = page_array_alloc(dic->inode, dic->cluster_size); 788 if (!dic->tpages) { 789 ret = -ENOMEM; 790 goto out_free_dic; 791 } 792 793 for (i = 0; i < dic->cluster_size; i++) { 794 if (dic->rpages[i]) { 795 dic->tpages[i] = dic->rpages[i]; 796 continue; 797 } 798 799 dic->tpages[i] = f2fs_compress_alloc_page(); 800 if (!dic->tpages[i]) { 801 ret = -ENOMEM; 802 goto out_free_dic; 803 } 804 } 805 806 if (cops->init_decompress_ctx) { 807 ret = cops->init_decompress_ctx(dic); 808 if (ret) 809 goto out_free_dic; 810 } 811 812 dic->rbuf = f2fs_vmap(dic->tpages, dic->cluster_size); 813 if (!dic->rbuf) { 814 ret = -ENOMEM; 815 goto destroy_decompress_ctx; 816 } 817 818 dic->cbuf = f2fs_vmap(dic->cpages, dic->nr_cpages); 819 if (!dic->cbuf) { 820 ret = -ENOMEM; 821 goto out_vunmap_rbuf; 822 } 823 824 dic->clen = le32_to_cpu(dic->cbuf->clen); 825 dic->rlen = PAGE_SIZE << dic->log_cluster_size; 826 827 if (dic->clen > PAGE_SIZE * dic->nr_cpages - COMPRESS_HEADER_SIZE) { 828 ret = -EFSCORRUPTED; 829 goto out_vunmap_cbuf; 830 } 831 832 ret = cops->decompress_pages(dic); 833 834 if (!ret && (fi->i_compress_flag & 1 << COMPRESS_CHKSUM)) { 835 u32 provided = le32_to_cpu(dic->cbuf->chksum); 836 u32 calculated = f2fs_crc32(sbi, dic->cbuf->cdata, dic->clen); 837 838 if (provided != calculated) { 839 if (!is_inode_flag_set(dic->inode, FI_COMPRESS_CORRUPT)) { 840 set_inode_flag(dic->inode, FI_COMPRESS_CORRUPT); 841 printk_ratelimited( 842 "%sF2FS-fs (%s): checksum invalid, nid = %lu, %x vs %x", 843 KERN_INFO, sbi->sb->s_id, dic->inode->i_ino, 844 provided, calculated); 845 } 846 set_sbi_flag(sbi, SBI_NEED_FSCK); 847 } 848 } 849 850 out_vunmap_cbuf: 851 vm_unmap_ram(dic->cbuf, dic->nr_cpages); 852 out_vunmap_rbuf: 853 vm_unmap_ram(dic->rbuf, dic->cluster_size); 854 destroy_decompress_ctx: 855 if (cops->destroy_decompress_ctx) 856 cops->destroy_decompress_ctx(dic); 857 out_free_dic: 858 if (!verity) 859 f2fs_decompress_end_io(dic->rpages, dic->cluster_size, 860 ret, false); 861 862 trace_f2fs_decompress_pages_end(dic->inode, dic->cluster_idx, 863 dic->clen, ret); 864 if (!verity) 865 f2fs_free_dic(dic); 866 } 867 868 static bool is_page_in_cluster(struct compress_ctx *cc, pgoff_t index) 869 { 870 if (cc->cluster_idx == NULL_CLUSTER) 871 return true; 872 return cc->cluster_idx == cluster_idx(cc, index); 873 } 874 875 bool f2fs_cluster_is_empty(struct compress_ctx *cc) 876 { 877 return cc->nr_rpages == 0; 878 } 879 880 static bool f2fs_cluster_is_full(struct compress_ctx *cc) 881 { 882 return cc->cluster_size == cc->nr_rpages; 883 } 884 885 bool f2fs_cluster_can_merge_page(struct compress_ctx *cc, pgoff_t index) 886 { 887 if (f2fs_cluster_is_empty(cc)) 888 return true; 889 return is_page_in_cluster(cc, index); 890 } 891 892 static bool __cluster_may_compress(struct compress_ctx *cc) 893 { 894 struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode); 895 loff_t i_size = i_size_read(cc->inode); 896 unsigned nr_pages = DIV_ROUND_UP(i_size, PAGE_SIZE); 897 int i; 898 899 for (i = 0; i < cc->cluster_size; i++) { 900 struct page *page = cc->rpages[i]; 901 902 f2fs_bug_on(sbi, !page); 903 904 if (unlikely(f2fs_cp_error(sbi))) 905 return false; 906 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) 907 return false; 908 909 /* beyond EOF */ 910 if (page->index >= nr_pages) 911 return false; 912 } 913 return true; 914 } 915 916 static int __f2fs_cluster_blocks(struct compress_ctx *cc, bool compr) 917 { 918 struct dnode_of_data dn; 919 int ret; 920 921 set_new_dnode(&dn, cc->inode, NULL, NULL, 0); 922 ret = f2fs_get_dnode_of_data(&dn, start_idx_of_cluster(cc), 923 LOOKUP_NODE); 924 if (ret) { 925 if (ret == -ENOENT) 926 ret = 0; 927 goto fail; 928 } 929 930 if (dn.data_blkaddr == COMPRESS_ADDR) { 931 int i; 932 933 ret = 1; 934 for (i = 1; i < cc->cluster_size; i++) { 935 block_t blkaddr; 936 937 blkaddr = data_blkaddr(dn.inode, 938 dn.node_page, dn.ofs_in_node + i); 939 if (compr) { 940 if (__is_valid_data_blkaddr(blkaddr)) 941 ret++; 942 } else { 943 if (blkaddr != NULL_ADDR) 944 ret++; 945 } 946 } 947 } 948 fail: 949 f2fs_put_dnode(&dn); 950 return ret; 951 } 952 953 /* return # of compressed blocks in compressed cluster */ 954 static int f2fs_compressed_blocks(struct compress_ctx *cc) 955 { 956 return __f2fs_cluster_blocks(cc, true); 957 } 958 959 /* return # of valid blocks in compressed cluster */ 960 static int f2fs_cluster_blocks(struct compress_ctx *cc) 961 { 962 return __f2fs_cluster_blocks(cc, false); 963 } 964 965 int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index) 966 { 967 struct compress_ctx cc = { 968 .inode = inode, 969 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size, 970 .cluster_size = F2FS_I(inode)->i_cluster_size, 971 .cluster_idx = index >> F2FS_I(inode)->i_log_cluster_size, 972 }; 973 974 return f2fs_cluster_blocks(&cc); 975 } 976 977 static bool cluster_may_compress(struct compress_ctx *cc) 978 { 979 if (!f2fs_need_compress_data(cc->inode)) 980 return false; 981 if (f2fs_is_atomic_file(cc->inode)) 982 return false; 983 if (f2fs_is_mmap_file(cc->inode)) 984 return false; 985 if (!f2fs_cluster_is_full(cc)) 986 return false; 987 if (unlikely(f2fs_cp_error(F2FS_I_SB(cc->inode)))) 988 return false; 989 return __cluster_may_compress(cc); 990 } 991 992 static void set_cluster_writeback(struct compress_ctx *cc) 993 { 994 int i; 995 996 for (i = 0; i < cc->cluster_size; i++) { 997 if (cc->rpages[i]) 998 set_page_writeback(cc->rpages[i]); 999 } 1000 } 1001 1002 static void set_cluster_dirty(struct compress_ctx *cc) 1003 { 1004 int i; 1005 1006 for (i = 0; i < cc->cluster_size; i++) 1007 if (cc->rpages[i]) 1008 set_page_dirty(cc->rpages[i]); 1009 } 1010 1011 static int prepare_compress_overwrite(struct compress_ctx *cc, 1012 struct page **pagep, pgoff_t index, void **fsdata) 1013 { 1014 struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode); 1015 struct address_space *mapping = cc->inode->i_mapping; 1016 struct page *page; 1017 struct dnode_of_data dn; 1018 sector_t last_block_in_bio; 1019 unsigned fgp_flag = FGP_LOCK | FGP_WRITE | FGP_CREAT; 1020 pgoff_t start_idx = start_idx_of_cluster(cc); 1021 int i, ret; 1022 bool prealloc; 1023 1024 retry: 1025 ret = f2fs_cluster_blocks(cc); 1026 if (ret <= 0) 1027 return ret; 1028 1029 /* compressed case */ 1030 prealloc = (ret < cc->cluster_size); 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 unlock_page(page); 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_destroy_compress_ctx(cc); 1057 if (ret) 1058 goto release_pages; 1059 if (bio) 1060 f2fs_submit_bio(sbi, bio, DATA); 1061 1062 ret = f2fs_init_compress_ctx(cc); 1063 if (ret) 1064 goto release_pages; 1065 } 1066 1067 for (i = 0; i < cc->cluster_size; i++) { 1068 f2fs_bug_on(sbi, cc->rpages[i]); 1069 1070 page = find_lock_page(mapping, start_idx + i); 1071 f2fs_bug_on(sbi, !page); 1072 1073 f2fs_wait_on_page_writeback(page, DATA, true, true); 1074 1075 f2fs_compress_ctx_add_page(cc, page); 1076 f2fs_put_page(page, 0); 1077 1078 if (!PageUptodate(page)) { 1079 f2fs_unlock_rpages(cc, i + 1); 1080 f2fs_put_rpages_mapping(mapping, start_idx, 1081 cc->cluster_size); 1082 f2fs_destroy_compress_ctx(cc); 1083 goto retry; 1084 } 1085 } 1086 1087 if (prealloc) { 1088 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true); 1089 1090 set_new_dnode(&dn, cc->inode, NULL, NULL, 0); 1091 1092 for (i = cc->cluster_size - 1; i > 0; i--) { 1093 ret = f2fs_get_block(&dn, start_idx + i); 1094 if (ret) { 1095 i = cc->cluster_size; 1096 break; 1097 } 1098 1099 if (dn.data_blkaddr != NEW_ADDR) 1100 break; 1101 } 1102 1103 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false); 1104 } 1105 1106 if (likely(!ret)) { 1107 *fsdata = cc->rpages; 1108 *pagep = cc->rpages[offset_in_cluster(cc, index)]; 1109 return cc->cluster_size; 1110 } 1111 1112 unlock_pages: 1113 f2fs_unlock_rpages(cc, i); 1114 release_pages: 1115 f2fs_put_rpages_mapping(mapping, start_idx, i); 1116 f2fs_destroy_compress_ctx(cc); 1117 return ret; 1118 } 1119 1120 int f2fs_prepare_compress_overwrite(struct inode *inode, 1121 struct page **pagep, pgoff_t index, void **fsdata) 1122 { 1123 struct compress_ctx cc = { 1124 .inode = inode, 1125 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size, 1126 .cluster_size = F2FS_I(inode)->i_cluster_size, 1127 .cluster_idx = index >> F2FS_I(inode)->i_log_cluster_size, 1128 .rpages = NULL, 1129 .nr_rpages = 0, 1130 }; 1131 1132 return prepare_compress_overwrite(&cc, pagep, index, fsdata); 1133 } 1134 1135 bool f2fs_compress_write_end(struct inode *inode, void *fsdata, 1136 pgoff_t index, unsigned copied) 1137 1138 { 1139 struct compress_ctx cc = { 1140 .inode = inode, 1141 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size, 1142 .cluster_size = F2FS_I(inode)->i_cluster_size, 1143 .rpages = fsdata, 1144 }; 1145 bool first_index = (index == cc.rpages[0]->index); 1146 1147 if (copied) 1148 set_cluster_dirty(&cc); 1149 1150 f2fs_put_rpages_wbc(&cc, NULL, false, 1); 1151 f2fs_destroy_compress_ctx(&cc); 1152 1153 return first_index; 1154 } 1155 1156 int f2fs_truncate_partial_cluster(struct inode *inode, u64 from, bool lock) 1157 { 1158 void *fsdata = NULL; 1159 struct page *pagep; 1160 int log_cluster_size = F2FS_I(inode)->i_log_cluster_size; 1161 pgoff_t start_idx = from >> (PAGE_SHIFT + log_cluster_size) << 1162 log_cluster_size; 1163 int err; 1164 1165 err = f2fs_is_compressed_cluster(inode, start_idx); 1166 if (err < 0) 1167 return err; 1168 1169 /* truncate normal cluster */ 1170 if (!err) 1171 return f2fs_do_truncate_blocks(inode, from, lock); 1172 1173 /* truncate compressed cluster */ 1174 err = f2fs_prepare_compress_overwrite(inode, &pagep, 1175 start_idx, &fsdata); 1176 1177 /* should not be a normal cluster */ 1178 f2fs_bug_on(F2FS_I_SB(inode), err == 0); 1179 1180 if (err <= 0) 1181 return err; 1182 1183 if (err > 0) { 1184 struct page **rpages = fsdata; 1185 int cluster_size = F2FS_I(inode)->i_cluster_size; 1186 int i; 1187 1188 for (i = cluster_size - 1; i >= 0; i--) { 1189 loff_t start = rpages[i]->index << PAGE_SHIFT; 1190 1191 if (from <= start) { 1192 zero_user_segment(rpages[i], 0, PAGE_SIZE); 1193 } else { 1194 zero_user_segment(rpages[i], from - start, 1195 PAGE_SIZE); 1196 break; 1197 } 1198 } 1199 1200 f2fs_compress_write_end(inode, fsdata, start_idx, true); 1201 } 1202 return 0; 1203 } 1204 1205 static int f2fs_write_compressed_pages(struct compress_ctx *cc, 1206 int *submitted, 1207 struct writeback_control *wbc, 1208 enum iostat_type io_type) 1209 { 1210 struct inode *inode = cc->inode; 1211 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1212 struct f2fs_inode_info *fi = F2FS_I(inode); 1213 struct f2fs_io_info fio = { 1214 .sbi = sbi, 1215 .ino = cc->inode->i_ino, 1216 .type = DATA, 1217 .op = REQ_OP_WRITE, 1218 .op_flags = wbc_to_write_flags(wbc), 1219 .old_blkaddr = NEW_ADDR, 1220 .page = NULL, 1221 .encrypted_page = NULL, 1222 .compressed_page = NULL, 1223 .submitted = false, 1224 .io_type = io_type, 1225 .io_wbc = wbc, 1226 .encrypted = fscrypt_inode_uses_fs_layer_crypto(cc->inode), 1227 }; 1228 struct dnode_of_data dn; 1229 struct node_info ni; 1230 struct compress_io_ctx *cic; 1231 pgoff_t start_idx = start_idx_of_cluster(cc); 1232 unsigned int last_index = cc->cluster_size - 1; 1233 loff_t psize; 1234 int i, err; 1235 1236 if (IS_NOQUOTA(inode)) { 1237 /* 1238 * We need to wait for node_write to avoid block allocation during 1239 * checkpoint. This can only happen to quota writes which can cause 1240 * the below discard race condition. 1241 */ 1242 down_read(&sbi->node_write); 1243 } else if (!f2fs_trylock_op(sbi)) { 1244 goto out_free; 1245 } 1246 1247 set_new_dnode(&dn, cc->inode, NULL, NULL, 0); 1248 1249 err = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE); 1250 if (err) 1251 goto out_unlock_op; 1252 1253 for (i = 0; i < cc->cluster_size; i++) { 1254 if (data_blkaddr(dn.inode, dn.node_page, 1255 dn.ofs_in_node + i) == NULL_ADDR) 1256 goto out_put_dnode; 1257 } 1258 1259 psize = (loff_t)(cc->rpages[last_index]->index + 1) << PAGE_SHIFT; 1260 1261 err = f2fs_get_node_info(fio.sbi, dn.nid, &ni); 1262 if (err) 1263 goto out_put_dnode; 1264 1265 fio.version = ni.version; 1266 1267 cic = kmem_cache_zalloc(cic_entry_slab, GFP_NOFS); 1268 if (!cic) 1269 goto out_put_dnode; 1270 1271 cic->magic = F2FS_COMPRESSED_PAGE_MAGIC; 1272 cic->inode = inode; 1273 atomic_set(&cic->pending_pages, cc->nr_cpages); 1274 cic->rpages = page_array_alloc(cc->inode, cc->cluster_size); 1275 if (!cic->rpages) 1276 goto out_put_cic; 1277 1278 cic->nr_rpages = cc->cluster_size; 1279 1280 for (i = 0; i < cc->nr_cpages; i++) { 1281 f2fs_set_compressed_page(cc->cpages[i], inode, 1282 cc->rpages[i + 1]->index, cic); 1283 fio.compressed_page = cc->cpages[i]; 1284 1285 fio.old_blkaddr = data_blkaddr(dn.inode, dn.node_page, 1286 dn.ofs_in_node + i + 1); 1287 1288 /* wait for GCed page writeback via META_MAPPING */ 1289 f2fs_wait_on_block_writeback(inode, fio.old_blkaddr); 1290 1291 if (fio.encrypted) { 1292 fio.page = cc->rpages[i + 1]; 1293 err = f2fs_encrypt_one_page(&fio); 1294 if (err) 1295 goto out_destroy_crypt; 1296 cc->cpages[i] = fio.encrypted_page; 1297 } 1298 } 1299 1300 set_cluster_writeback(cc); 1301 1302 for (i = 0; i < cc->cluster_size; i++) 1303 cic->rpages[i] = cc->rpages[i]; 1304 1305 for (i = 0; i < cc->cluster_size; i++, dn.ofs_in_node++) { 1306 block_t blkaddr; 1307 1308 blkaddr = f2fs_data_blkaddr(&dn); 1309 fio.page = cc->rpages[i]; 1310 fio.old_blkaddr = blkaddr; 1311 1312 /* cluster header */ 1313 if (i == 0) { 1314 if (blkaddr == COMPRESS_ADDR) 1315 fio.compr_blocks++; 1316 if (__is_valid_data_blkaddr(blkaddr)) 1317 f2fs_invalidate_blocks(sbi, blkaddr); 1318 f2fs_update_data_blkaddr(&dn, COMPRESS_ADDR); 1319 goto unlock_continue; 1320 } 1321 1322 if (fio.compr_blocks && __is_valid_data_blkaddr(blkaddr)) 1323 fio.compr_blocks++; 1324 1325 if (i > cc->nr_cpages) { 1326 if (__is_valid_data_blkaddr(blkaddr)) { 1327 f2fs_invalidate_blocks(sbi, blkaddr); 1328 f2fs_update_data_blkaddr(&dn, NEW_ADDR); 1329 } 1330 goto unlock_continue; 1331 } 1332 1333 f2fs_bug_on(fio.sbi, blkaddr == NULL_ADDR); 1334 1335 if (fio.encrypted) 1336 fio.encrypted_page = cc->cpages[i - 1]; 1337 else 1338 fio.compressed_page = cc->cpages[i - 1]; 1339 1340 cc->cpages[i - 1] = NULL; 1341 f2fs_outplace_write_data(&dn, &fio); 1342 (*submitted)++; 1343 unlock_continue: 1344 inode_dec_dirty_pages(cc->inode); 1345 unlock_page(fio.page); 1346 } 1347 1348 if (fio.compr_blocks) 1349 f2fs_i_compr_blocks_update(inode, fio.compr_blocks - 1, false); 1350 f2fs_i_compr_blocks_update(inode, cc->nr_cpages, true); 1351 1352 set_inode_flag(cc->inode, FI_APPEND_WRITE); 1353 if (cc->cluster_idx == 0) 1354 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN); 1355 1356 f2fs_put_dnode(&dn); 1357 if (IS_NOQUOTA(inode)) 1358 up_read(&sbi->node_write); 1359 else 1360 f2fs_unlock_op(sbi); 1361 1362 spin_lock(&fi->i_size_lock); 1363 if (fi->last_disk_size < psize) 1364 fi->last_disk_size = psize; 1365 spin_unlock(&fi->i_size_lock); 1366 1367 f2fs_put_rpages(cc); 1368 page_array_free(cc->inode, cc->cpages, cc->nr_cpages); 1369 cc->cpages = NULL; 1370 f2fs_destroy_compress_ctx(cc); 1371 return 0; 1372 1373 out_destroy_crypt: 1374 page_array_free(cc->inode, cic->rpages, cc->cluster_size); 1375 1376 for (--i; i >= 0; i--) 1377 fscrypt_finalize_bounce_page(&cc->cpages[i]); 1378 for (i = 0; i < cc->nr_cpages; i++) { 1379 if (!cc->cpages[i]) 1380 continue; 1381 f2fs_put_page(cc->cpages[i], 1); 1382 } 1383 out_put_cic: 1384 kmem_cache_free(cic_entry_slab, cic); 1385 out_put_dnode: 1386 f2fs_put_dnode(&dn); 1387 out_unlock_op: 1388 if (IS_NOQUOTA(inode)) 1389 up_read(&sbi->node_write); 1390 else 1391 f2fs_unlock_op(sbi); 1392 out_free: 1393 page_array_free(cc->inode, cc->cpages, cc->nr_cpages); 1394 cc->cpages = NULL; 1395 return -EAGAIN; 1396 } 1397 1398 void f2fs_compress_write_end_io(struct bio *bio, struct page *page) 1399 { 1400 struct f2fs_sb_info *sbi = bio->bi_private; 1401 struct compress_io_ctx *cic = 1402 (struct compress_io_ctx *)page_private(page); 1403 int i; 1404 1405 if (unlikely(bio->bi_status)) 1406 mapping_set_error(cic->inode->i_mapping, -EIO); 1407 1408 f2fs_compress_free_page(page); 1409 1410 dec_page_count(sbi, F2FS_WB_DATA); 1411 1412 if (atomic_dec_return(&cic->pending_pages)) 1413 return; 1414 1415 for (i = 0; i < cic->nr_rpages; i++) { 1416 WARN_ON(!cic->rpages[i]); 1417 clear_cold_data(cic->rpages[i]); 1418 end_page_writeback(cic->rpages[i]); 1419 } 1420 1421 page_array_free(cic->inode, cic->rpages, cic->nr_rpages); 1422 kmem_cache_free(cic_entry_slab, cic); 1423 } 1424 1425 static int f2fs_write_raw_pages(struct compress_ctx *cc, 1426 int *submitted, 1427 struct writeback_control *wbc, 1428 enum iostat_type io_type) 1429 { 1430 struct address_space *mapping = cc->inode->i_mapping; 1431 int _submitted, compr_blocks, ret; 1432 int i = -1, err = 0; 1433 1434 compr_blocks = f2fs_compressed_blocks(cc); 1435 if (compr_blocks < 0) { 1436 err = compr_blocks; 1437 goto out_err; 1438 } 1439 1440 for (i = 0; i < cc->cluster_size; i++) { 1441 if (!cc->rpages[i]) 1442 continue; 1443 retry_write: 1444 if (cc->rpages[i]->mapping != mapping) { 1445 unlock_page(cc->rpages[i]); 1446 continue; 1447 } 1448 1449 BUG_ON(!PageLocked(cc->rpages[i])); 1450 1451 ret = f2fs_write_single_data_page(cc->rpages[i], &_submitted, 1452 NULL, NULL, wbc, io_type, 1453 compr_blocks); 1454 if (ret) { 1455 if (ret == AOP_WRITEPAGE_ACTIVATE) { 1456 unlock_page(cc->rpages[i]); 1457 ret = 0; 1458 } else if (ret == -EAGAIN) { 1459 /* 1460 * for quota file, just redirty left pages to 1461 * avoid deadlock caused by cluster update race 1462 * from foreground operation. 1463 */ 1464 if (IS_NOQUOTA(cc->inode)) { 1465 err = 0; 1466 goto out_err; 1467 } 1468 ret = 0; 1469 cond_resched(); 1470 congestion_wait(BLK_RW_ASYNC, 1471 DEFAULT_IO_TIMEOUT); 1472 lock_page(cc->rpages[i]); 1473 1474 if (!PageDirty(cc->rpages[i])) { 1475 unlock_page(cc->rpages[i]); 1476 continue; 1477 } 1478 1479 clear_page_dirty_for_io(cc->rpages[i]); 1480 goto retry_write; 1481 } 1482 err = ret; 1483 goto out_err; 1484 } 1485 1486 *submitted += _submitted; 1487 } 1488 return 0; 1489 out_err: 1490 for (++i; i < cc->cluster_size; i++) { 1491 if (!cc->rpages[i]) 1492 continue; 1493 redirty_page_for_writepage(wbc, cc->rpages[i]); 1494 unlock_page(cc->rpages[i]); 1495 } 1496 return err; 1497 } 1498 1499 int f2fs_write_multi_pages(struct compress_ctx *cc, 1500 int *submitted, 1501 struct writeback_control *wbc, 1502 enum iostat_type io_type) 1503 { 1504 int err; 1505 1506 *submitted = 0; 1507 if (cluster_may_compress(cc)) { 1508 err = f2fs_compress_pages(cc); 1509 if (err == -EAGAIN) { 1510 goto write; 1511 } else if (err) { 1512 f2fs_put_rpages_wbc(cc, wbc, true, 1); 1513 goto destroy_out; 1514 } 1515 1516 err = f2fs_write_compressed_pages(cc, submitted, 1517 wbc, io_type); 1518 if (!err) 1519 return 0; 1520 f2fs_bug_on(F2FS_I_SB(cc->inode), err != -EAGAIN); 1521 } 1522 write: 1523 f2fs_bug_on(F2FS_I_SB(cc->inode), *submitted); 1524 1525 err = f2fs_write_raw_pages(cc, submitted, wbc, io_type); 1526 f2fs_put_rpages_wbc(cc, wbc, false, 0); 1527 destroy_out: 1528 f2fs_destroy_compress_ctx(cc); 1529 return err; 1530 } 1531 1532 struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc) 1533 { 1534 struct decompress_io_ctx *dic; 1535 pgoff_t start_idx = start_idx_of_cluster(cc); 1536 int i; 1537 1538 dic = kmem_cache_zalloc(dic_entry_slab, GFP_NOFS); 1539 if (!dic) 1540 return ERR_PTR(-ENOMEM); 1541 1542 dic->rpages = page_array_alloc(cc->inode, cc->cluster_size); 1543 if (!dic->rpages) { 1544 kmem_cache_free(dic_entry_slab, dic); 1545 return ERR_PTR(-ENOMEM); 1546 } 1547 1548 dic->magic = F2FS_COMPRESSED_PAGE_MAGIC; 1549 dic->inode = cc->inode; 1550 atomic_set(&dic->pending_pages, cc->nr_cpages); 1551 dic->cluster_idx = cc->cluster_idx; 1552 dic->cluster_size = cc->cluster_size; 1553 dic->log_cluster_size = cc->log_cluster_size; 1554 dic->nr_cpages = cc->nr_cpages; 1555 dic->failed = false; 1556 1557 for (i = 0; i < dic->cluster_size; i++) 1558 dic->rpages[i] = cc->rpages[i]; 1559 dic->nr_rpages = cc->cluster_size; 1560 1561 dic->cpages = page_array_alloc(dic->inode, dic->nr_cpages); 1562 if (!dic->cpages) 1563 goto out_free; 1564 1565 for (i = 0; i < dic->nr_cpages; i++) { 1566 struct page *page; 1567 1568 page = f2fs_compress_alloc_page(); 1569 if (!page) 1570 goto out_free; 1571 1572 f2fs_set_compressed_page(page, cc->inode, 1573 start_idx + i + 1, dic); 1574 dic->cpages[i] = page; 1575 } 1576 1577 return dic; 1578 1579 out_free: 1580 f2fs_free_dic(dic); 1581 return ERR_PTR(-ENOMEM); 1582 } 1583 1584 void f2fs_free_dic(struct decompress_io_ctx *dic) 1585 { 1586 int i; 1587 1588 if (dic->tpages) { 1589 for (i = 0; i < dic->cluster_size; i++) { 1590 if (dic->rpages[i]) 1591 continue; 1592 if (!dic->tpages[i]) 1593 continue; 1594 f2fs_compress_free_page(dic->tpages[i]); 1595 } 1596 page_array_free(dic->inode, dic->tpages, dic->cluster_size); 1597 } 1598 1599 if (dic->cpages) { 1600 for (i = 0; i < dic->nr_cpages; i++) { 1601 if (!dic->cpages[i]) 1602 continue; 1603 f2fs_compress_free_page(dic->cpages[i]); 1604 } 1605 page_array_free(dic->inode, dic->cpages, dic->nr_cpages); 1606 } 1607 1608 page_array_free(dic->inode, dic->rpages, dic->nr_rpages); 1609 kmem_cache_free(dic_entry_slab, dic); 1610 } 1611 1612 void f2fs_decompress_end_io(struct page **rpages, 1613 unsigned int cluster_size, bool err, bool verity) 1614 { 1615 int i; 1616 1617 for (i = 0; i < cluster_size; i++) { 1618 struct page *rpage = rpages[i]; 1619 1620 if (!rpage) 1621 continue; 1622 1623 if (err || PageError(rpage)) 1624 goto clear_uptodate; 1625 1626 if (!verity || fsverity_verify_page(rpage)) { 1627 SetPageUptodate(rpage); 1628 goto unlock; 1629 } 1630 clear_uptodate: 1631 ClearPageUptodate(rpage); 1632 ClearPageError(rpage); 1633 unlock: 1634 unlock_page(rpage); 1635 } 1636 } 1637 1638 int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi) 1639 { 1640 dev_t dev = sbi->sb->s_bdev->bd_dev; 1641 char slab_name[32]; 1642 1643 sprintf(slab_name, "f2fs_page_array_entry-%u:%u", MAJOR(dev), MINOR(dev)); 1644 1645 sbi->page_array_slab_size = sizeof(struct page *) << 1646 F2FS_OPTION(sbi).compress_log_size; 1647 1648 sbi->page_array_slab = f2fs_kmem_cache_create(slab_name, 1649 sbi->page_array_slab_size); 1650 if (!sbi->page_array_slab) 1651 return -ENOMEM; 1652 return 0; 1653 } 1654 1655 void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi) 1656 { 1657 kmem_cache_destroy(sbi->page_array_slab); 1658 } 1659 1660 static int __init f2fs_init_cic_cache(void) 1661 { 1662 cic_entry_slab = f2fs_kmem_cache_create("f2fs_cic_entry", 1663 sizeof(struct compress_io_ctx)); 1664 if (!cic_entry_slab) 1665 return -ENOMEM; 1666 return 0; 1667 } 1668 1669 static void f2fs_destroy_cic_cache(void) 1670 { 1671 kmem_cache_destroy(cic_entry_slab); 1672 } 1673 1674 static int __init f2fs_init_dic_cache(void) 1675 { 1676 dic_entry_slab = f2fs_kmem_cache_create("f2fs_dic_entry", 1677 sizeof(struct decompress_io_ctx)); 1678 if (!dic_entry_slab) 1679 return -ENOMEM; 1680 return 0; 1681 } 1682 1683 static void f2fs_destroy_dic_cache(void) 1684 { 1685 kmem_cache_destroy(dic_entry_slab); 1686 } 1687 1688 int __init f2fs_init_compress_cache(void) 1689 { 1690 int err; 1691 1692 err = f2fs_init_cic_cache(); 1693 if (err) 1694 goto out; 1695 err = f2fs_init_dic_cache(); 1696 if (err) 1697 goto free_cic; 1698 return 0; 1699 free_cic: 1700 f2fs_destroy_cic_cache(); 1701 out: 1702 return -ENOMEM; 1703 } 1704 1705 void f2fs_destroy_compress_cache(void) 1706 { 1707 f2fs_destroy_dic_cache(); 1708 f2fs_destroy_cic_cache(); 1709 } 1710