blob: 77fa342de38f122dde0633409035432bf32081c0 [file] [log] [blame]
Chao Yu4c8ff702019-11-01 18:07:14 +08001// 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>
Chao Yu50cfa662020-03-03 17:46:02 +080014#include <linux/zstd.h>
Chao Yu4c8ff702019-11-01 18:07:14 +080015
16#include "f2fs.h"
17#include "node.h"
18#include <trace/events/f2fs.h>
19
Chao Yuc68d6c82020-09-14 17:05:14 +080020static struct kmem_cache *cic_entry_slab;
21static struct kmem_cache *dic_entry_slab;
22
Chao Yu31083032020-09-14 17:05:13 +080023static 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
33static 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
Chao Yu4c8ff702019-11-01 18:07:14 +080047struct 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);
Chao Yu23b1faaad2020-03-03 16:57:07 +080051 int (*init_decompress_ctx)(struct decompress_io_ctx *dic);
52 void (*destroy_decompress_ctx)(struct decompress_io_ctx *dic);
Chao Yu4c8ff702019-11-01 18:07:14 +080053 int (*decompress_pages)(struct decompress_io_ctx *dic);
54};
55
56static unsigned int offset_in_cluster(struct compress_ctx *cc, pgoff_t index)
57{
58 return index & (cc->cluster_size - 1);
59}
60
61static pgoff_t cluster_idx(struct compress_ctx *cc, pgoff_t index)
62{
63 return index >> cc->log_cluster_size;
64}
65
66static pgoff_t start_idx_of_cluster(struct compress_ctx *cc)
67{
68 return cc->cluster_idx << cc->log_cluster_size;
69}
70
71bool 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;
Yu Changchun29b993c2020-06-20 03:58:29 -040079 /*
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
Chao Yu4c8ff702019-11-01 18:07:14 +080086 f2fs_bug_on(F2FS_M_SB(page->mapping),
87 *((u32 *)page_private(page)) != F2FS_COMPRESSED_PAGE_MAGIC);
88 return true;
89}
90
91static void f2fs_set_compressed_page(struct page *page,
Chao Yu887347a2020-03-28 17:33:23 +080092 struct inode *inode, pgoff_t index, void *data)
Chao Yu4c8ff702019-11-01 18:07:14 +080093{
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;
Chao Yu4c8ff702019-11-01 18:07:14 +0800100}
101
Chao Yu4c8ff702019-11-01 18:07:14 +0800102static 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
116static void f2fs_put_rpages(struct compress_ctx *cc)
117{
118 f2fs_drop_rpages(cc, cc->cluster_size, false);
119}
120
121static void f2fs_unlock_rpages(struct compress_ctx *cc, int len)
122{
123 f2fs_drop_rpages(cc, len, true);
124}
125
Chao Yubc67c5d2020-06-08 20:03:17 +0800126static void f2fs_put_rpages_mapping(struct address_space *mapping,
Chao Yu4c8ff702019-11-01 18:07:14 +0800127 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
139static 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
153struct page *f2fs_compress_control_page(struct page *page)
154{
155 return ((struct compress_io_ctx *)page_private(page))->rpages[0];
156}
157
158int f2fs_init_compress_ctx(struct compress_ctx *cc)
159{
Jaegeuk Kimadfc6942020-09-23 00:54:50 -0700160 if (cc->rpages)
Chao Yu4c8ff702019-11-01 18:07:14 +0800161 return 0;
162
Chao Yu31083032020-09-14 17:05:13 +0800163 cc->rpages = page_array_alloc(cc->inode, cc->cluster_size);
Chao Yu4c8ff702019-11-01 18:07:14 +0800164 return cc->rpages ? 0 : -ENOMEM;
165}
166
167void f2fs_destroy_compress_ctx(struct compress_ctx *cc)
168{
Chao Yu31083032020-09-14 17:05:13 +0800169 page_array_free(cc->inode, cc->rpages, cc->cluster_size);
Chao Yu4c8ff702019-11-01 18:07:14 +0800170 cc->rpages = NULL;
171 cc->nr_rpages = 0;
172 cc->nr_cpages = 0;
173 cc->cluster_idx = NULL_CLUSTER;
174}
175
176void 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
190static 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
201static void lzo_destroy_compress_ctx(struct compress_ctx *cc)
202{
203 kvfree(cc->private);
204 cc->private = NULL;
205}
206
207static 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
221static 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
244static 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
253static int lz4_init_compress_ctx(struct compress_ctx *cc)
254{
Chao Yud0d83de2021-01-22 17:46:43 +0800255 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);
Chao Yu4c8ff702019-11-01 18:07:14 +0800263 if (!cc->private)
264 return -ENOMEM;
265
Chao Yuf6644142020-05-09 15:01:04 +0800266 /*
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;
Chao Yu4c8ff702019-11-01 18:07:14 +0800272 return 0;
273}
274
275static void lz4_destroy_compress_ctx(struct compress_ctx *cc)
276{
277 kvfree(cc->private);
278 cc->private = NULL;
279}
280
Chao Yud0d83de2021-01-22 17:46:43 +0800281#ifdef CONFIG_F2FS_FS_LZ4HC
282static 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
Chao Yu4c8ff702019-11-01 18:07:14 +0800302static int lz4_compress_pages(struct compress_ctx *cc)
303{
304 int len;
305
Chao Yud0d83de2021-01-22 17:46:43 +0800306#ifdef CONFIG_F2FS_FS_LZ4HC
307 return lz4hc_compress_pages(cc);
308#endif
Chao Yu4c8ff702019-11-01 18:07:14 +0800309 len = LZ4_compress_default(cc->rbuf, cc->cbuf->cdata, cc->rlen,
310 cc->clen, cc->private);
Chao Yuf6644142020-05-09 15:01:04 +0800311 if (!len)
312 return -EAGAIN;
313
Chao Yu4c8ff702019-11-01 18:07:14 +0800314 cc->clen = len;
315 return 0;
316}
317
318static 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
341static 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
Chao Yu50cfa662020-03-03 17:46:02 +0800349#ifdef CONFIG_F2FS_FS_ZSTD
350#define F2FS_ZSTD_DEFAULT_CLEVEL 1
351
352static 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;
Chao Yud0d83de2021-01-22 17:46:43 +0800358 unsigned char level = F2FS_I(cc->inode)->i_compress_flag >>
359 COMPRESS_LEVEL_OFFSET;
Chao Yu50cfa662020-03-03 17:46:02 +0800360
Chao Yud0d83de2021-01-22 17:46:43 +0800361 if (!level)
362 level = F2FS_ZSTD_DEFAULT_CLEVEL;
363
364 params = ZSTD_getParams(level, cc->rlen, 0);
Chao Yu50cfa662020-03-03 17:46:02 +0800365 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
388static void zstd_destroy_compress_ctx(struct compress_ctx *cc)
389{
390 kvfree(cc->private);
391 cc->private = NULL;
392 cc->private2 = NULL;
393}
394
395static 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
Chao Yu1454c972020-05-08 09:16:03 +0800428 /*
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
Chao Yu50cfa662020-03-03 17:46:02 +0800435 cc->clen = outbuf.pos;
436 return 0;
437}
438
439static int zstd_init_decompress_ctx(struct decompress_io_ctx *dic)
440{
441 ZSTD_DStream *stream;
442 void *workspace;
443 unsigned int workspace_size;
Chao Yu0e2b7382020-09-02 15:01:52 +0800444 unsigned int max_window_size =
445 MAX_COMPRESS_WINDOW_SIZE(dic->log_cluster_size);
Chao Yu50cfa662020-03-03 17:46:02 +0800446
Chao Yu0e2b7382020-09-02 15:01:52 +0800447 workspace_size = ZSTD_DStreamWorkspaceBound(max_window_size);
Chao Yu50cfa662020-03-03 17:46:02 +0800448
449 workspace = f2fs_kvmalloc(F2FS_I_SB(dic->inode),
450 workspace_size, GFP_NOFS);
451 if (!workspace)
452 return -ENOMEM;
453
Chao Yu0e2b7382020-09-02 15:01:52 +0800454 stream = ZSTD_initDStream(max_window_size, workspace, workspace_size);
Chao Yu50cfa662020-03-03 17:46:02 +0800455 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
469static 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
476static 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
511static 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
Chao Yu6d92b202020-04-08 19:56:32 +0800521#ifdef CONFIG_F2FS_FS_LZO
522#ifdef CONFIG_F2FS_FS_LZORLE
523static 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
537static 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
Chao Yu4c8ff702019-11-01 18:07:14 +0800546static 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
Chao Yu50cfa662020-03-03 17:46:02 +0800557#ifdef CONFIG_F2FS_FS_ZSTD
558 &f2fs_zstd_ops,
559#else
560 NULL,
561#endif
Chao Yu6d92b202020-04-08 19:56:32 +0800562#if defined(CONFIG_F2FS_FS_LZO) && defined(CONFIG_F2FS_FS_LZORLE)
563 &f2fs_lzorle_ops,
564#else
565 NULL,
566#endif
Chao Yu4c8ff702019-11-01 18:07:14 +0800567};
568
569bool 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
Jaegeuk Kim99bbe302020-06-16 09:56:51 -0700576static mempool_t *compress_page_pool;
Chao Yu5e6bbde2020-04-08 19:56:05 +0800577static int num_compress_pages = 512;
578module_param(num_compress_pages, uint, 0444);
579MODULE_PARM_DESC(num_compress_pages,
580 "Number of intermediate compress pages to preallocate");
581
582int 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
591void f2fs_destroy_compress_mempool(void)
592{
593 mempool_destroy(compress_page_pool);
594}
595
596static struct page *f2fs_compress_alloc_page(void)
Chao Yu4c8ff702019-11-01 18:07:14 +0800597{
598 struct page *page;
599
Chao Yu5e6bbde2020-04-08 19:56:05 +0800600 page = mempool_alloc(compress_page_pool, GFP_NOFS);
Chao Yu4c8ff702019-11-01 18:07:14 +0800601 lock_page(page);
Chao Yu5e6bbde2020-04-08 19:56:05 +0800602
Chao Yu4c8ff702019-11-01 18:07:14 +0800603 return page;
604}
605
Chao Yu5e6bbde2020-04-08 19:56:05 +0800606static 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
Daeho Jeong6fcaeba2020-08-12 14:17:11 +0900617#define MAX_VMAP_RETRIES 3
618
619static 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
Chao Yu4c8ff702019-11-01 18:07:14 +0800633static int f2fs_compress_pages(struct compress_ctx *cc)
634{
Chao Yu4c8ff702019-11-01 18:07:14 +0800635 struct f2fs_inode_info *fi = F2FS_I(cc->inode);
636 const struct f2fs_compress_ops *cops =
637 f2fs_cops[fi->i_compress_algorithm];
Chao Yu31083032020-09-14 17:05:13 +0800638 unsigned int max_len, new_nr_cpages;
639 struct page **new_cpages;
Chao Yu1f004d62020-11-26 18:32:09 +0800640 u32 chksum = 0;
Chao Yu4c8ff702019-11-01 18:07:14 +0800641 int i, ret;
642
643 trace_f2fs_compress_pages_start(cc->inode, cc->cluster_idx,
644 cc->cluster_size, fi->i_compress_algorithm);
645
Chao Yu23b1faaad2020-03-03 16:57:07 +0800646 if (cops->init_compress_ctx) {
647 ret = cops->init_compress_ctx(cc);
648 if (ret)
649 goto out;
650 }
Chao Yu4c8ff702019-11-01 18:07:14 +0800651
652 max_len = COMPRESS_HEADER_SIZE + cc->clen;
653 cc->nr_cpages = DIV_ROUND_UP(max_len, PAGE_SIZE);
654
Chao Yu31083032020-09-14 17:05:13 +0800655 cc->cpages = page_array_alloc(cc->inode, cc->nr_cpages);
Chao Yu4c8ff702019-11-01 18:07:14 +0800656 if (!cc->cpages) {
657 ret = -ENOMEM;
658 goto destroy_compress_ctx;
659 }
660
661 for (i = 0; i < cc->nr_cpages; i++) {
Chao Yu5e6bbde2020-04-08 19:56:05 +0800662 cc->cpages[i] = f2fs_compress_alloc_page();
Chao Yu4c8ff702019-11-01 18:07:14 +0800663 if (!cc->cpages[i]) {
664 ret = -ENOMEM;
665 goto out_free_cpages;
666 }
667 }
668
Daeho Jeong6fcaeba2020-08-12 14:17:11 +0900669 cc->rbuf = f2fs_vmap(cc->rpages, cc->cluster_size);
Chao Yu4c8ff702019-11-01 18:07:14 +0800670 if (!cc->rbuf) {
671 ret = -ENOMEM;
672 goto out_free_cpages;
673 }
674
Daeho Jeong6fcaeba2020-08-12 14:17:11 +0900675 cc->cbuf = f2fs_vmap(cc->cpages, cc->nr_cpages);
Chao Yu4c8ff702019-11-01 18:07:14 +0800676 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);
Chao Yu4c8ff702019-11-01 18:07:14 +0800693
Chao Yu1f004d62020-11-26 18:32:09 +0800694 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
Chao Yu4c8ff702019-11-01 18:07:14 +0800699 for (i = 0; i < COMPRESS_DATA_RESERVED_SIZE; i++)
700 cc->cbuf->reserved[i] = cpu_to_le32(0);
701
Chao Yu31083032020-09-14 17:05:13 +0800702 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 }
Eric Biggers7fa6d592020-02-20 20:50:37 -0800710
711 /* zero out any unused part of the last page */
712 memset(&cc->cbuf->cdata[cc->clen], 0,
Chao Yu31083032020-09-14 17:05:13 +0800713 (new_nr_cpages * PAGE_SIZE) -
714 (cc->clen + COMPRESS_HEADER_SIZE));
Eric Biggers7fa6d592020-02-20 20:50:37 -0800715
Daeho Jeong6fcaeba2020-08-12 14:17:11 +0900716 vm_unmap_ram(cc->cbuf, cc->nr_cpages);
717 vm_unmap_ram(cc->rbuf, cc->cluster_size);
Chao Yu4c8ff702019-11-01 18:07:14 +0800718
Chao Yu31083032020-09-14 17:05:13 +0800719 for (i = 0; i < cc->nr_cpages; i++) {
720 if (i < new_nr_cpages) {
721 new_cpages[i] = cc->cpages[i];
722 continue;
723 }
Chao Yu5e6bbde2020-04-08 19:56:05 +0800724 f2fs_compress_free_page(cc->cpages[i]);
Chao Yu4c8ff702019-11-01 18:07:14 +0800725 cc->cpages[i] = NULL;
726 }
727
Chao Yu23b1faaad2020-03-03 16:57:07 +0800728 if (cops->destroy_compress_ctx)
729 cops->destroy_compress_ctx(cc);
Chao Yu09ff4802020-03-03 16:57:06 +0800730
Chao Yu31083032020-09-14 17:05:13 +0800731 page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
732 cc->cpages = new_cpages;
733 cc->nr_cpages = new_nr_cpages;
Chao Yu4c8ff702019-11-01 18:07:14 +0800734
735 trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
736 cc->clen, ret);
737 return 0;
738
739out_vunmap_cbuf:
Daeho Jeong6fcaeba2020-08-12 14:17:11 +0900740 vm_unmap_ram(cc->cbuf, cc->nr_cpages);
Chao Yu4c8ff702019-11-01 18:07:14 +0800741out_vunmap_rbuf:
Daeho Jeong6fcaeba2020-08-12 14:17:11 +0900742 vm_unmap_ram(cc->rbuf, cc->cluster_size);
Chao Yu4c8ff702019-11-01 18:07:14 +0800743out_free_cpages:
744 for (i = 0; i < cc->nr_cpages; i++) {
745 if (cc->cpages[i])
Chao Yu5e6bbde2020-04-08 19:56:05 +0800746 f2fs_compress_free_page(cc->cpages[i]);
Chao Yu4c8ff702019-11-01 18:07:14 +0800747 }
Chao Yu31083032020-09-14 17:05:13 +0800748 page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
Chao Yu4c8ff702019-11-01 18:07:14 +0800749 cc->cpages = NULL;
750destroy_compress_ctx:
Chao Yu23b1faaad2020-03-03 16:57:07 +0800751 if (cops->destroy_compress_ctx)
752 cops->destroy_compress_ctx(cc);
Chao Yu4c8ff702019-11-01 18:07:14 +0800753out:
754 trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
755 cc->clen, ret);
756 return ret;
757}
758
Eric Biggersea29e652021-01-04 22:33:02 -0800759static void f2fs_decompress_cluster(struct decompress_io_ctx *dic)
Chao Yu4c8ff702019-11-01 18:07:14 +0800760{
Chao Yu4c8ff702019-11-01 18:07:14 +0800761 struct f2fs_sb_info *sbi = F2FS_I_SB(dic->inode);
Eric Biggersea29e652021-01-04 22:33:02 -0800762 struct f2fs_inode_info *fi = F2FS_I(dic->inode);
Chao Yu4c8ff702019-11-01 18:07:14 +0800763 const struct f2fs_compress_ops *cops =
764 f2fs_cops[fi->i_compress_algorithm];
765 int ret;
Chao Yub2f57a82020-07-25 09:17:48 +0800766 int i;
Chao Yu4c8ff702019-11-01 18:07:14 +0800767
Chao Yu4c8ff702019-11-01 18:07:14 +0800768 trace_f2fs_decompress_pages_start(dic->inode, dic->cluster_idx,
769 dic->cluster_size, fi->i_compress_algorithm);
770
Chao Yu4c8ff702019-11-01 18:07:14 +0800771 if (dic->failed) {
772 ret = -EIO;
Eric Biggersea29e652021-01-04 22:33:02 -0800773 goto out_end_io;
Chao Yu4c8ff702019-11-01 18:07:14 +0800774 }
775
Chao Yu31083032020-09-14 17:05:13 +0800776 dic->tpages = page_array_alloc(dic->inode, dic->cluster_size);
Chao Yub2f57a82020-07-25 09:17:48 +0800777 if (!dic->tpages) {
778 ret = -ENOMEM;
Eric Biggersea29e652021-01-04 22:33:02 -0800779 goto out_end_io;
Chao Yub2f57a82020-07-25 09:17:48 +0800780 }
781
782 for (i = 0; i < dic->cluster_size; i++) {
783 if (dic->rpages[i]) {
784 dic->tpages[i] = dic->rpages[i];
785 continue;
786 }
787
788 dic->tpages[i] = f2fs_compress_alloc_page();
789 if (!dic->tpages[i]) {
790 ret = -ENOMEM;
Eric Biggersea29e652021-01-04 22:33:02 -0800791 goto out_end_io;
Chao Yub2f57a82020-07-25 09:17:48 +0800792 }
793 }
794
Chao Yu23b1faaad2020-03-03 16:57:07 +0800795 if (cops->init_decompress_ctx) {
796 ret = cops->init_decompress_ctx(dic);
797 if (ret)
Eric Biggersea29e652021-01-04 22:33:02 -0800798 goto out_end_io;
Chao Yu23b1faaad2020-03-03 16:57:07 +0800799 }
800
Daeho Jeong6fcaeba2020-08-12 14:17:11 +0900801 dic->rbuf = f2fs_vmap(dic->tpages, dic->cluster_size);
Chao Yu4c8ff702019-11-01 18:07:14 +0800802 if (!dic->rbuf) {
803 ret = -ENOMEM;
Eric Biggersea29e652021-01-04 22:33:02 -0800804 goto out_destroy_decompress_ctx;
Chao Yu4c8ff702019-11-01 18:07:14 +0800805 }
806
Daeho Jeong6fcaeba2020-08-12 14:17:11 +0900807 dic->cbuf = f2fs_vmap(dic->cpages, dic->nr_cpages);
Chao Yu4c8ff702019-11-01 18:07:14 +0800808 if (!dic->cbuf) {
809 ret = -ENOMEM;
810 goto out_vunmap_rbuf;
811 }
812
813 dic->clen = le32_to_cpu(dic->cbuf->clen);
814 dic->rlen = PAGE_SIZE << dic->log_cluster_size;
815
816 if (dic->clen > PAGE_SIZE * dic->nr_cpages - COMPRESS_HEADER_SIZE) {
817 ret = -EFSCORRUPTED;
818 goto out_vunmap_cbuf;
819 }
820
821 ret = cops->decompress_pages(dic);
822
Chao Yu3ed3a942020-12-09 16:42:14 +0800823 if (!ret && (fi->i_compress_flag & 1 << COMPRESS_CHKSUM)) {
Chao Yu1f004d62020-11-26 18:32:09 +0800824 u32 provided = le32_to_cpu(dic->cbuf->chksum);
825 u32 calculated = f2fs_crc32(sbi, dic->cbuf->cdata, dic->clen);
826
827 if (provided != calculated) {
828 if (!is_inode_flag_set(dic->inode, FI_COMPRESS_CORRUPT)) {
829 set_inode_flag(dic->inode, FI_COMPRESS_CORRUPT);
830 printk_ratelimited(
831 "%sF2FS-fs (%s): checksum invalid, nid = %lu, %x vs %x",
832 KERN_INFO, sbi->sb->s_id, dic->inode->i_ino,
833 provided, calculated);
834 }
835 set_sbi_flag(sbi, SBI_NEED_FSCK);
Chao Yu1f004d62020-11-26 18:32:09 +0800836 }
837 }
838
Chao Yu4c8ff702019-11-01 18:07:14 +0800839out_vunmap_cbuf:
Daeho Jeong6fcaeba2020-08-12 14:17:11 +0900840 vm_unmap_ram(dic->cbuf, dic->nr_cpages);
Chao Yu4c8ff702019-11-01 18:07:14 +0800841out_vunmap_rbuf:
Daeho Jeong6fcaeba2020-08-12 14:17:11 +0900842 vm_unmap_ram(dic->rbuf, dic->cluster_size);
Eric Biggersea29e652021-01-04 22:33:02 -0800843out_destroy_decompress_ctx:
Chao Yu23b1faaad2020-03-03 16:57:07 +0800844 if (cops->destroy_decompress_ctx)
845 cops->destroy_decompress_ctx(dic);
Eric Biggersea29e652021-01-04 22:33:02 -0800846out_end_io:
Chao Yu4c8ff702019-11-01 18:07:14 +0800847 trace_f2fs_decompress_pages_end(dic->inode, dic->cluster_idx,
848 dic->clen, ret);
Eric Biggersea29e652021-01-04 22:33:02 -0800849 f2fs_decompress_end_io(dic, ret);
850}
851
852/*
853 * This is called when a page of a compressed cluster has been read from disk
854 * (or failed to be read from disk). It checks whether this page was the last
855 * page being waited on in the cluster, and if so, it decompresses the cluster
856 * (or in the case of a failure, cleans up without actually decompressing).
857 */
858void f2fs_end_read_compressed_page(struct page *page, bool failed)
859{
860 struct decompress_io_ctx *dic =
861 (struct decompress_io_ctx *)page_private(page);
862 struct f2fs_sb_info *sbi = F2FS_I_SB(dic->inode);
863
864 dec_page_count(sbi, F2FS_RD_DATA);
865
866 if (failed)
867 WRITE_ONCE(dic->failed, true);
868
869 if (atomic_dec_and_test(&dic->remaining_pages))
870 f2fs_decompress_cluster(dic);
Chao Yu4c8ff702019-11-01 18:07:14 +0800871}
872
873static bool is_page_in_cluster(struct compress_ctx *cc, pgoff_t index)
874{
875 if (cc->cluster_idx == NULL_CLUSTER)
876 return true;
877 return cc->cluster_idx == cluster_idx(cc, index);
878}
879
880bool f2fs_cluster_is_empty(struct compress_ctx *cc)
881{
882 return cc->nr_rpages == 0;
883}
884
885static bool f2fs_cluster_is_full(struct compress_ctx *cc)
886{
887 return cc->cluster_size == cc->nr_rpages;
888}
889
890bool f2fs_cluster_can_merge_page(struct compress_ctx *cc, pgoff_t index)
891{
892 if (f2fs_cluster_is_empty(cc))
893 return true;
894 return is_page_in_cluster(cc, index);
895}
896
897static bool __cluster_may_compress(struct compress_ctx *cc)
898{
899 struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
900 loff_t i_size = i_size_read(cc->inode);
901 unsigned nr_pages = DIV_ROUND_UP(i_size, PAGE_SIZE);
902 int i;
903
904 for (i = 0; i < cc->cluster_size; i++) {
905 struct page *page = cc->rpages[i];
906
907 f2fs_bug_on(sbi, !page);
908
909 if (unlikely(f2fs_cp_error(sbi)))
910 return false;
911 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
912 return false;
913
914 /* beyond EOF */
915 if (page->index >= nr_pages)
916 return false;
917 }
918 return true;
919}
920
Chao Yu1a67cbe2020-03-12 10:45:29 +0800921static int __f2fs_cluster_blocks(struct compress_ctx *cc, bool compr)
Chao Yu4c8ff702019-11-01 18:07:14 +0800922{
923 struct dnode_of_data dn;
924 int ret;
925
926 set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
927 ret = f2fs_get_dnode_of_data(&dn, start_idx_of_cluster(cc),
928 LOOKUP_NODE);
929 if (ret) {
930 if (ret == -ENOENT)
931 ret = 0;
932 goto fail;
933 }
934
935 if (dn.data_blkaddr == COMPRESS_ADDR) {
936 int i;
937
938 ret = 1;
939 for (i = 1; i < cc->cluster_size; i++) {
940 block_t blkaddr;
941
Chao Yua2ced1c2020-02-14 17:44:10 +0800942 blkaddr = data_blkaddr(dn.inode,
Chao Yu4c8ff702019-11-01 18:07:14 +0800943 dn.node_page, dn.ofs_in_node + i);
Chao Yu1a67cbe2020-03-12 10:45:29 +0800944 if (compr) {
945 if (__is_valid_data_blkaddr(blkaddr))
946 ret++;
947 } else {
948 if (blkaddr != NULL_ADDR)
949 ret++;
950 }
Chao Yu4c8ff702019-11-01 18:07:14 +0800951 }
952 }
953fail:
954 f2fs_put_dnode(&dn);
955 return ret;
956}
957
Chao Yu1a67cbe2020-03-12 10:45:29 +0800958/* return # of compressed blocks in compressed cluster */
959static int f2fs_compressed_blocks(struct compress_ctx *cc)
960{
961 return __f2fs_cluster_blocks(cc, true);
962}
963
964/* return # of valid blocks in compressed cluster */
Wang Xiaojund0783192020-06-16 10:39:24 +0800965static int f2fs_cluster_blocks(struct compress_ctx *cc)
Chao Yu1a67cbe2020-03-12 10:45:29 +0800966{
967 return __f2fs_cluster_blocks(cc, false);
968}
969
Chao Yu4c8ff702019-11-01 18:07:14 +0800970int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index)
971{
972 struct compress_ctx cc = {
973 .inode = inode,
974 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
975 .cluster_size = F2FS_I(inode)->i_cluster_size,
976 .cluster_idx = index >> F2FS_I(inode)->i_log_cluster_size,
977 };
978
Wang Xiaojund0783192020-06-16 10:39:24 +0800979 return f2fs_cluster_blocks(&cc);
Chao Yu4c8ff702019-11-01 18:07:14 +0800980}
981
982static bool cluster_may_compress(struct compress_ctx *cc)
983{
Daeho Jeongf15a2002020-12-01 13:08:02 +0900984 if (!f2fs_need_compress_data(cc->inode))
Chao Yu4c8ff702019-11-01 18:07:14 +0800985 return false;
986 if (f2fs_is_atomic_file(cc->inode))
987 return false;
988 if (f2fs_is_mmap_file(cc->inode))
989 return false;
990 if (!f2fs_cluster_is_full(cc))
991 return false;
Chao Yudc35d732020-05-26 09:55:02 +0800992 if (unlikely(f2fs_cp_error(F2FS_I_SB(cc->inode))))
993 return false;
Chao Yu4c8ff702019-11-01 18:07:14 +0800994 return __cluster_may_compress(cc);
995}
996
997static void set_cluster_writeback(struct compress_ctx *cc)
998{
999 int i;
1000
1001 for (i = 0; i < cc->cluster_size; i++) {
1002 if (cc->rpages[i])
1003 set_page_writeback(cc->rpages[i]);
1004 }
1005}
1006
1007static void set_cluster_dirty(struct compress_ctx *cc)
1008{
1009 int i;
1010
1011 for (i = 0; i < cc->cluster_size; i++)
1012 if (cc->rpages[i])
1013 set_page_dirty(cc->rpages[i]);
1014}
1015
1016static int prepare_compress_overwrite(struct compress_ctx *cc,
1017 struct page **pagep, pgoff_t index, void **fsdata)
1018{
1019 struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
1020 struct address_space *mapping = cc->inode->i_mapping;
1021 struct page *page;
1022 struct dnode_of_data dn;
1023 sector_t last_block_in_bio;
1024 unsigned fgp_flag = FGP_LOCK | FGP_WRITE | FGP_CREAT;
1025 pgoff_t start_idx = start_idx_of_cluster(cc);
1026 int i, ret;
1027 bool prealloc;
1028
1029retry:
Wang Xiaojund0783192020-06-16 10:39:24 +08001030 ret = f2fs_cluster_blocks(cc);
Chao Yu4c8ff702019-11-01 18:07:14 +08001031 if (ret <= 0)
1032 return ret;
1033
1034 /* compressed case */
1035 prealloc = (ret < cc->cluster_size);
1036
1037 ret = f2fs_init_compress_ctx(cc);
1038 if (ret)
1039 return ret;
1040
1041 /* keep page reference to avoid page reclaim */
1042 for (i = 0; i < cc->cluster_size; i++) {
1043 page = f2fs_pagecache_get_page(mapping, start_idx + i,
1044 fgp_flag, GFP_NOFS);
1045 if (!page) {
1046 ret = -ENOMEM;
1047 goto unlock_pages;
1048 }
1049
1050 if (PageUptodate(page))
1051 unlock_page(page);
1052 else
1053 f2fs_compress_ctx_add_page(cc, page);
1054 }
1055
1056 if (!f2fs_cluster_is_empty(cc)) {
1057 struct bio *bio = NULL;
1058
1059 ret = f2fs_read_multi_pages(cc, &bio, cc->cluster_size,
Chao Yu06837282020-02-18 18:21:35 +08001060 &last_block_in_bio, false, true);
Chao Yu4c8ff702019-11-01 18:07:14 +08001061 f2fs_destroy_compress_ctx(cc);
1062 if (ret)
1063 goto release_pages;
1064 if (bio)
1065 f2fs_submit_bio(sbi, bio, DATA);
1066
1067 ret = f2fs_init_compress_ctx(cc);
1068 if (ret)
1069 goto release_pages;
1070 }
1071
1072 for (i = 0; i < cc->cluster_size; i++) {
1073 f2fs_bug_on(sbi, cc->rpages[i]);
1074
1075 page = find_lock_page(mapping, start_idx + i);
1076 f2fs_bug_on(sbi, !page);
1077
1078 f2fs_wait_on_page_writeback(page, DATA, true, true);
1079
1080 f2fs_compress_ctx_add_page(cc, page);
1081 f2fs_put_page(page, 0);
1082
1083 if (!PageUptodate(page)) {
1084 f2fs_unlock_rpages(cc, i + 1);
Chao Yubc67c5d2020-06-08 20:03:17 +08001085 f2fs_put_rpages_mapping(mapping, start_idx,
Chao Yu4c8ff702019-11-01 18:07:14 +08001086 cc->cluster_size);
1087 f2fs_destroy_compress_ctx(cc);
1088 goto retry;
1089 }
1090 }
1091
1092 if (prealloc) {
Chao Yu0ef81832020-06-18 14:36:22 +08001093 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
Chao Yu4c8ff702019-11-01 18:07:14 +08001094
1095 set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
1096
1097 for (i = cc->cluster_size - 1; i > 0; i--) {
1098 ret = f2fs_get_block(&dn, start_idx + i);
1099 if (ret) {
1100 i = cc->cluster_size;
1101 break;
1102 }
1103
1104 if (dn.data_blkaddr != NEW_ADDR)
1105 break;
1106 }
1107
Chao Yu0ef81832020-06-18 14:36:22 +08001108 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
Chao Yu4c8ff702019-11-01 18:07:14 +08001109 }
1110
1111 if (likely(!ret)) {
1112 *fsdata = cc->rpages;
1113 *pagep = cc->rpages[offset_in_cluster(cc, index)];
1114 return cc->cluster_size;
1115 }
1116
1117unlock_pages:
1118 f2fs_unlock_rpages(cc, i);
1119release_pages:
Chao Yubc67c5d2020-06-08 20:03:17 +08001120 f2fs_put_rpages_mapping(mapping, start_idx, i);
Chao Yu4c8ff702019-11-01 18:07:14 +08001121 f2fs_destroy_compress_ctx(cc);
1122 return ret;
1123}
1124
1125int f2fs_prepare_compress_overwrite(struct inode *inode,
1126 struct page **pagep, pgoff_t index, void **fsdata)
1127{
1128 struct compress_ctx cc = {
1129 .inode = inode,
1130 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
1131 .cluster_size = F2FS_I(inode)->i_cluster_size,
1132 .cluster_idx = index >> F2FS_I(inode)->i_log_cluster_size,
1133 .rpages = NULL,
1134 .nr_rpages = 0,
1135 };
1136
1137 return prepare_compress_overwrite(&cc, pagep, index, fsdata);
1138}
1139
1140bool f2fs_compress_write_end(struct inode *inode, void *fsdata,
1141 pgoff_t index, unsigned copied)
1142
1143{
1144 struct compress_ctx cc = {
Chao Yu31083032020-09-14 17:05:13 +08001145 .inode = inode,
Chao Yu4c8ff702019-11-01 18:07:14 +08001146 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
1147 .cluster_size = F2FS_I(inode)->i_cluster_size,
1148 .rpages = fsdata,
1149 };
1150 bool first_index = (index == cc.rpages[0]->index);
1151
1152 if (copied)
1153 set_cluster_dirty(&cc);
1154
1155 f2fs_put_rpages_wbc(&cc, NULL, false, 1);
1156 f2fs_destroy_compress_ctx(&cc);
1157
1158 return first_index;
1159}
1160
Chao Yu3265d3d2020-03-18 16:22:59 +08001161int f2fs_truncate_partial_cluster(struct inode *inode, u64 from, bool lock)
1162{
1163 void *fsdata = NULL;
1164 struct page *pagep;
1165 int log_cluster_size = F2FS_I(inode)->i_log_cluster_size;
1166 pgoff_t start_idx = from >> (PAGE_SHIFT + log_cluster_size) <<
1167 log_cluster_size;
1168 int err;
1169
1170 err = f2fs_is_compressed_cluster(inode, start_idx);
1171 if (err < 0)
1172 return err;
1173
1174 /* truncate normal cluster */
1175 if (!err)
1176 return f2fs_do_truncate_blocks(inode, from, lock);
1177
1178 /* truncate compressed cluster */
1179 err = f2fs_prepare_compress_overwrite(inode, &pagep,
1180 start_idx, &fsdata);
1181
1182 /* should not be a normal cluster */
1183 f2fs_bug_on(F2FS_I_SB(inode), err == 0);
1184
1185 if (err <= 0)
1186 return err;
1187
1188 if (err > 0) {
1189 struct page **rpages = fsdata;
1190 int cluster_size = F2FS_I(inode)->i_cluster_size;
1191 int i;
1192
1193 for (i = cluster_size - 1; i >= 0; i--) {
1194 loff_t start = rpages[i]->index << PAGE_SHIFT;
1195
1196 if (from <= start) {
1197 zero_user_segment(rpages[i], 0, PAGE_SIZE);
1198 } else {
1199 zero_user_segment(rpages[i], from - start,
1200 PAGE_SIZE);
1201 break;
1202 }
1203 }
1204
1205 f2fs_compress_write_end(inode, fsdata, start_idx, true);
1206 }
1207 return 0;
1208}
1209
Chao Yu4c8ff702019-11-01 18:07:14 +08001210static int f2fs_write_compressed_pages(struct compress_ctx *cc,
1211 int *submitted,
1212 struct writeback_control *wbc,
1213 enum iostat_type io_type)
1214{
1215 struct inode *inode = cc->inode;
1216 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1217 struct f2fs_inode_info *fi = F2FS_I(inode);
1218 struct f2fs_io_info fio = {
1219 .sbi = sbi,
1220 .ino = cc->inode->i_ino,
1221 .type = DATA,
1222 .op = REQ_OP_WRITE,
1223 .op_flags = wbc_to_write_flags(wbc),
1224 .old_blkaddr = NEW_ADDR,
1225 .page = NULL,
1226 .encrypted_page = NULL,
1227 .compressed_page = NULL,
1228 .submitted = false,
Chao Yu4c8ff702019-11-01 18:07:14 +08001229 .io_type = io_type,
1230 .io_wbc = wbc,
Satya Tangirala27aacd22020-07-02 01:56:06 +00001231 .encrypted = fscrypt_inode_uses_fs_layer_crypto(cc->inode),
Chao Yu4c8ff702019-11-01 18:07:14 +08001232 };
1233 struct dnode_of_data dn;
1234 struct node_info ni;
1235 struct compress_io_ctx *cic;
1236 pgoff_t start_idx = start_idx_of_cluster(cc);
1237 unsigned int last_index = cc->cluster_size - 1;
1238 loff_t psize;
1239 int i, err;
1240
Chao Yu79963d92020-06-18 14:36:23 +08001241 if (IS_NOQUOTA(inode)) {
1242 /*
1243 * We need to wait for node_write to avoid block allocation during
1244 * checkpoint. This can only happen to quota writes which can cause
1245 * the below discard race condition.
1246 */
1247 down_read(&sbi->node_write);
1248 } else if (!f2fs_trylock_op(sbi)) {
Chao Yu31083032020-09-14 17:05:13 +08001249 goto out_free;
Chao Yu79963d92020-06-18 14:36:23 +08001250 }
Chao Yu4c8ff702019-11-01 18:07:14 +08001251
Chao Yudf77fbd2020-02-24 19:20:16 +08001252 set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
Chao Yu4c8ff702019-11-01 18:07:14 +08001253
1254 err = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
1255 if (err)
1256 goto out_unlock_op;
1257
1258 for (i = 0; i < cc->cluster_size; i++) {
Chao Yua2ced1c2020-02-14 17:44:10 +08001259 if (data_blkaddr(dn.inode, dn.node_page,
Chao Yu4c8ff702019-11-01 18:07:14 +08001260 dn.ofs_in_node + i) == NULL_ADDR)
1261 goto out_put_dnode;
1262 }
1263
1264 psize = (loff_t)(cc->rpages[last_index]->index + 1) << PAGE_SHIFT;
1265
1266 err = f2fs_get_node_info(fio.sbi, dn.nid, &ni);
1267 if (err)
1268 goto out_put_dnode;
1269
1270 fio.version = ni.version;
1271
Chao Yuc68d6c82020-09-14 17:05:14 +08001272 cic = kmem_cache_zalloc(cic_entry_slab, GFP_NOFS);
Chao Yu4c8ff702019-11-01 18:07:14 +08001273 if (!cic)
1274 goto out_put_dnode;
1275
1276 cic->magic = F2FS_COMPRESSED_PAGE_MAGIC;
1277 cic->inode = inode;
Chao Yue6c39482020-08-10 18:39:30 +08001278 atomic_set(&cic->pending_pages, cc->nr_cpages);
Chao Yu31083032020-09-14 17:05:13 +08001279 cic->rpages = page_array_alloc(cc->inode, cc->cluster_size);
Chao Yu4c8ff702019-11-01 18:07:14 +08001280 if (!cic->rpages)
1281 goto out_put_cic;
1282
1283 cic->nr_rpages = cc->cluster_size;
1284
1285 for (i = 0; i < cc->nr_cpages; i++) {
1286 f2fs_set_compressed_page(cc->cpages[i], inode,
Chao Yu887347a2020-03-28 17:33:23 +08001287 cc->rpages[i + 1]->index, cic);
Chao Yu4c8ff702019-11-01 18:07:14 +08001288 fio.compressed_page = cc->cpages[i];
Chao Yuf567adb2020-07-03 16:40:11 +08001289
1290 fio.old_blkaddr = data_blkaddr(dn.inode, dn.node_page,
1291 dn.ofs_in_node + i + 1);
1292
1293 /* wait for GCed page writeback via META_MAPPING */
1294 f2fs_wait_on_block_writeback(inode, fio.old_blkaddr);
1295
Chao Yu4c8ff702019-11-01 18:07:14 +08001296 if (fio.encrypted) {
1297 fio.page = cc->rpages[i + 1];
1298 err = f2fs_encrypt_one_page(&fio);
1299 if (err)
1300 goto out_destroy_crypt;
1301 cc->cpages[i] = fio.encrypted_page;
1302 }
1303 }
1304
1305 set_cluster_writeback(cc);
1306
1307 for (i = 0; i < cc->cluster_size; i++)
1308 cic->rpages[i] = cc->rpages[i];
1309
1310 for (i = 0; i < cc->cluster_size; i++, dn.ofs_in_node++) {
1311 block_t blkaddr;
1312
Chao Yua2ced1c2020-02-14 17:44:10 +08001313 blkaddr = f2fs_data_blkaddr(&dn);
Chao Yu95978ca2020-02-28 18:08:46 +08001314 fio.page = cc->rpages[i];
Chao Yu4c8ff702019-11-01 18:07:14 +08001315 fio.old_blkaddr = blkaddr;
1316
1317 /* cluster header */
1318 if (i == 0) {
1319 if (blkaddr == COMPRESS_ADDR)
1320 fio.compr_blocks++;
1321 if (__is_valid_data_blkaddr(blkaddr))
1322 f2fs_invalidate_blocks(sbi, blkaddr);
1323 f2fs_update_data_blkaddr(&dn, COMPRESS_ADDR);
1324 goto unlock_continue;
1325 }
1326
1327 if (fio.compr_blocks && __is_valid_data_blkaddr(blkaddr))
1328 fio.compr_blocks++;
1329
1330 if (i > cc->nr_cpages) {
1331 if (__is_valid_data_blkaddr(blkaddr)) {
1332 f2fs_invalidate_blocks(sbi, blkaddr);
1333 f2fs_update_data_blkaddr(&dn, NEW_ADDR);
1334 }
1335 goto unlock_continue;
1336 }
1337
1338 f2fs_bug_on(fio.sbi, blkaddr == NULL_ADDR);
1339
1340 if (fio.encrypted)
1341 fio.encrypted_page = cc->cpages[i - 1];
1342 else
1343 fio.compressed_page = cc->cpages[i - 1];
1344
1345 cc->cpages[i - 1] = NULL;
1346 f2fs_outplace_write_data(&dn, &fio);
1347 (*submitted)++;
1348unlock_continue:
1349 inode_dec_dirty_pages(cc->inode);
1350 unlock_page(fio.page);
1351 }
1352
1353 if (fio.compr_blocks)
1354 f2fs_i_compr_blocks_update(inode, fio.compr_blocks - 1, false);
1355 f2fs_i_compr_blocks_update(inode, cc->nr_cpages, true);
1356
1357 set_inode_flag(cc->inode, FI_APPEND_WRITE);
1358 if (cc->cluster_idx == 0)
1359 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
1360
1361 f2fs_put_dnode(&dn);
Chao Yu79963d92020-06-18 14:36:23 +08001362 if (IS_NOQUOTA(inode))
1363 up_read(&sbi->node_write);
1364 else
Jaegeuk Kim435cbab2020-04-09 10:25:21 -07001365 f2fs_unlock_op(sbi);
Chao Yu4c8ff702019-11-01 18:07:14 +08001366
Chao Yuc10c9822020-02-27 19:30:03 +08001367 spin_lock(&fi->i_size_lock);
Chao Yu4c8ff702019-11-01 18:07:14 +08001368 if (fi->last_disk_size < psize)
1369 fi->last_disk_size = psize;
Chao Yuc10c9822020-02-27 19:30:03 +08001370 spin_unlock(&fi->i_size_lock);
Chao Yu4c8ff702019-11-01 18:07:14 +08001371
1372 f2fs_put_rpages(cc);
Chao Yu31083032020-09-14 17:05:13 +08001373 page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
1374 cc->cpages = NULL;
Chao Yu4c8ff702019-11-01 18:07:14 +08001375 f2fs_destroy_compress_ctx(cc);
1376 return 0;
1377
1378out_destroy_crypt:
Chao Yu31083032020-09-14 17:05:13 +08001379 page_array_free(cc->inode, cic->rpages, cc->cluster_size);
Chao Yu4c8ff702019-11-01 18:07:14 +08001380
1381 for (--i; i >= 0; i--)
1382 fscrypt_finalize_bounce_page(&cc->cpages[i]);
1383 for (i = 0; i < cc->nr_cpages; i++) {
1384 if (!cc->cpages[i])
1385 continue;
1386 f2fs_put_page(cc->cpages[i], 1);
1387 }
1388out_put_cic:
Chao Yuc68d6c82020-09-14 17:05:14 +08001389 kmem_cache_free(cic_entry_slab, cic);
Chao Yu4c8ff702019-11-01 18:07:14 +08001390out_put_dnode:
1391 f2fs_put_dnode(&dn);
1392out_unlock_op:
Chao Yu79963d92020-06-18 14:36:23 +08001393 if (IS_NOQUOTA(inode))
1394 up_read(&sbi->node_write);
1395 else
Jaegeuk Kim435cbab2020-04-09 10:25:21 -07001396 f2fs_unlock_op(sbi);
Chao Yu31083032020-09-14 17:05:13 +08001397out_free:
1398 page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
1399 cc->cpages = NULL;
Chao Yu4c8ff702019-11-01 18:07:14 +08001400 return -EAGAIN;
1401}
1402
1403void f2fs_compress_write_end_io(struct bio *bio, struct page *page)
1404{
1405 struct f2fs_sb_info *sbi = bio->bi_private;
1406 struct compress_io_ctx *cic =
1407 (struct compress_io_ctx *)page_private(page);
1408 int i;
1409
1410 if (unlikely(bio->bi_status))
1411 mapping_set_error(cic->inode->i_mapping, -EIO);
1412
Chao Yu5e6bbde2020-04-08 19:56:05 +08001413 f2fs_compress_free_page(page);
Chao Yu4c8ff702019-11-01 18:07:14 +08001414
1415 dec_page_count(sbi, F2FS_WB_DATA);
1416
Chao Yue6c39482020-08-10 18:39:30 +08001417 if (atomic_dec_return(&cic->pending_pages))
Chao Yu4c8ff702019-11-01 18:07:14 +08001418 return;
1419
1420 for (i = 0; i < cic->nr_rpages; i++) {
1421 WARN_ON(!cic->rpages[i]);
1422 clear_cold_data(cic->rpages[i]);
1423 end_page_writeback(cic->rpages[i]);
1424 }
1425
Chao Yu31083032020-09-14 17:05:13 +08001426 page_array_free(cic->inode, cic->rpages, cic->nr_rpages);
Chao Yuc68d6c82020-09-14 17:05:14 +08001427 kmem_cache_free(cic_entry_slab, cic);
Chao Yu4c8ff702019-11-01 18:07:14 +08001428}
1429
1430static int f2fs_write_raw_pages(struct compress_ctx *cc,
1431 int *submitted,
1432 struct writeback_control *wbc,
1433 enum iostat_type io_type)
1434{
1435 struct address_space *mapping = cc->inode->i_mapping;
1436 int _submitted, compr_blocks, ret;
1437 int i = -1, err = 0;
1438
1439 compr_blocks = f2fs_compressed_blocks(cc);
1440 if (compr_blocks < 0) {
1441 err = compr_blocks;
1442 goto out_err;
1443 }
1444
1445 for (i = 0; i < cc->cluster_size; i++) {
1446 if (!cc->rpages[i])
1447 continue;
1448retry_write:
1449 if (cc->rpages[i]->mapping != mapping) {
1450 unlock_page(cc->rpages[i]);
1451 continue;
1452 }
1453
1454 BUG_ON(!PageLocked(cc->rpages[i]));
1455
1456 ret = f2fs_write_single_data_page(cc->rpages[i], &_submitted,
1457 NULL, NULL, wbc, io_type,
Chao Yu77844182021-01-11 17:42:53 +08001458 compr_blocks, false);
Chao Yu4c8ff702019-11-01 18:07:14 +08001459 if (ret) {
1460 if (ret == AOP_WRITEPAGE_ACTIVATE) {
1461 unlock_page(cc->rpages[i]);
1462 ret = 0;
1463 } else if (ret == -EAGAIN) {
Chao Yu466357d2020-03-20 18:14:31 +08001464 /*
1465 * for quota file, just redirty left pages to
1466 * avoid deadlock caused by cluster update race
1467 * from foreground operation.
1468 */
1469 if (IS_NOQUOTA(cc->inode)) {
1470 err = 0;
1471 goto out_err;
1472 }
Chao Yu4c8ff702019-11-01 18:07:14 +08001473 ret = 0;
1474 cond_resched();
Chao Yu5df7731f2020-02-17 17:45:44 +08001475 congestion_wait(BLK_RW_ASYNC,
1476 DEFAULT_IO_TIMEOUT);
Chao Yu4c8ff702019-11-01 18:07:14 +08001477 lock_page(cc->rpages[i]);
Chao Yueb1353c2020-06-19 17:14:19 +08001478
1479 if (!PageDirty(cc->rpages[i])) {
1480 unlock_page(cc->rpages[i]);
1481 continue;
1482 }
1483
Chao Yu4c8ff702019-11-01 18:07:14 +08001484 clear_page_dirty_for_io(cc->rpages[i]);
1485 goto retry_write;
1486 }
1487 err = ret;
Chao Yu466357d2020-03-20 18:14:31 +08001488 goto out_err;
Chao Yu4c8ff702019-11-01 18:07:14 +08001489 }
1490
1491 *submitted += _submitted;
1492 }
Chao Yu77844182021-01-11 17:42:53 +08001493
1494 f2fs_balance_fs(F2FS_M_SB(mapping), true);
1495
Chao Yu4c8ff702019-11-01 18:07:14 +08001496 return 0;
Chao Yu4c8ff702019-11-01 18:07:14 +08001497out_err:
1498 for (++i; i < cc->cluster_size; i++) {
1499 if (!cc->rpages[i])
1500 continue;
1501 redirty_page_for_writepage(wbc, cc->rpages[i]);
1502 unlock_page(cc->rpages[i]);
1503 }
1504 return err;
1505}
1506
1507int f2fs_write_multi_pages(struct compress_ctx *cc,
1508 int *submitted,
1509 struct writeback_control *wbc,
1510 enum iostat_type io_type)
1511{
Chao Yu4c8ff702019-11-01 18:07:14 +08001512 int err;
1513
1514 *submitted = 0;
1515 if (cluster_may_compress(cc)) {
1516 err = f2fs_compress_pages(cc);
1517 if (err == -EAGAIN) {
1518 goto write;
1519 } else if (err) {
1520 f2fs_put_rpages_wbc(cc, wbc, true, 1);
1521 goto destroy_out;
1522 }
1523
1524 err = f2fs_write_compressed_pages(cc, submitted,
1525 wbc, io_type);
Chao Yu4c8ff702019-11-01 18:07:14 +08001526 if (!err)
1527 return 0;
1528 f2fs_bug_on(F2FS_I_SB(cc->inode), err != -EAGAIN);
1529 }
1530write:
1531 f2fs_bug_on(F2FS_I_SB(cc->inode), *submitted);
1532
1533 err = f2fs_write_raw_pages(cc, submitted, wbc, io_type);
1534 f2fs_put_rpages_wbc(cc, wbc, false, 0);
1535destroy_out:
1536 f2fs_destroy_compress_ctx(cc);
1537 return err;
1538}
1539
Eric Biggersea29e652021-01-04 22:33:02 -08001540static void f2fs_free_dic(struct decompress_io_ctx *dic);
1541
Chao Yu4c8ff702019-11-01 18:07:14 +08001542struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc)
1543{
Chao Yu4c8ff702019-11-01 18:07:14 +08001544 struct decompress_io_ctx *dic;
1545 pgoff_t start_idx = start_idx_of_cluster(cc);
1546 int i;
1547
Chao Yuc68d6c82020-09-14 17:05:14 +08001548 dic = kmem_cache_zalloc(dic_entry_slab, GFP_NOFS);
Chao Yu4c8ff702019-11-01 18:07:14 +08001549 if (!dic)
1550 return ERR_PTR(-ENOMEM);
1551
Chao Yu31083032020-09-14 17:05:13 +08001552 dic->rpages = page_array_alloc(cc->inode, cc->cluster_size);
Chao Yu4c8ff702019-11-01 18:07:14 +08001553 if (!dic->rpages) {
Chao Yuc68d6c82020-09-14 17:05:14 +08001554 kmem_cache_free(dic_entry_slab, dic);
Chao Yu4c8ff702019-11-01 18:07:14 +08001555 return ERR_PTR(-ENOMEM);
1556 }
1557
1558 dic->magic = F2FS_COMPRESSED_PAGE_MAGIC;
1559 dic->inode = cc->inode;
Eric Biggersea29e652021-01-04 22:33:02 -08001560 atomic_set(&dic->remaining_pages, cc->nr_cpages);
Chao Yu4c8ff702019-11-01 18:07:14 +08001561 dic->cluster_idx = cc->cluster_idx;
1562 dic->cluster_size = cc->cluster_size;
1563 dic->log_cluster_size = cc->log_cluster_size;
1564 dic->nr_cpages = cc->nr_cpages;
Eric Biggersea29e652021-01-04 22:33:02 -08001565 refcount_set(&dic->refcnt, 1);
Chao Yu4c8ff702019-11-01 18:07:14 +08001566 dic->failed = false;
Eric Biggersea29e652021-01-04 22:33:02 -08001567 dic->need_verity = f2fs_need_verity(cc->inode, start_idx);
Chao Yu4c8ff702019-11-01 18:07:14 +08001568
1569 for (i = 0; i < dic->cluster_size; i++)
1570 dic->rpages[i] = cc->rpages[i];
1571 dic->nr_rpages = cc->cluster_size;
1572
Chao Yu31083032020-09-14 17:05:13 +08001573 dic->cpages = page_array_alloc(dic->inode, dic->nr_cpages);
Chao Yu4c8ff702019-11-01 18:07:14 +08001574 if (!dic->cpages)
1575 goto out_free;
1576
1577 for (i = 0; i < dic->nr_cpages; i++) {
1578 struct page *page;
1579
Chao Yu5e6bbde2020-04-08 19:56:05 +08001580 page = f2fs_compress_alloc_page();
Chao Yu4c8ff702019-11-01 18:07:14 +08001581 if (!page)
1582 goto out_free;
1583
1584 f2fs_set_compressed_page(page, cc->inode,
Chao Yu887347a2020-03-28 17:33:23 +08001585 start_idx + i + 1, dic);
Chao Yu4c8ff702019-11-01 18:07:14 +08001586 dic->cpages[i] = page;
1587 }
1588
Chao Yu4c8ff702019-11-01 18:07:14 +08001589 return dic;
1590
1591out_free:
1592 f2fs_free_dic(dic);
1593 return ERR_PTR(-ENOMEM);
1594}
1595
Eric Biggersea29e652021-01-04 22:33:02 -08001596static void f2fs_free_dic(struct decompress_io_ctx *dic)
Chao Yu4c8ff702019-11-01 18:07:14 +08001597{
1598 int i;
1599
1600 if (dic->tpages) {
1601 for (i = 0; i < dic->cluster_size; i++) {
1602 if (dic->rpages[i])
1603 continue;
Chao Yu8908e752020-03-26 17:42:26 +08001604 if (!dic->tpages[i])
1605 continue;
Chao Yu5e6bbde2020-04-08 19:56:05 +08001606 f2fs_compress_free_page(dic->tpages[i]);
Chao Yu4c8ff702019-11-01 18:07:14 +08001607 }
Chao Yu31083032020-09-14 17:05:13 +08001608 page_array_free(dic->inode, dic->tpages, dic->cluster_size);
Chao Yu4c8ff702019-11-01 18:07:14 +08001609 }
1610
1611 if (dic->cpages) {
1612 for (i = 0; i < dic->nr_cpages; i++) {
1613 if (!dic->cpages[i])
1614 continue;
Chao Yu5e6bbde2020-04-08 19:56:05 +08001615 f2fs_compress_free_page(dic->cpages[i]);
Chao Yu4c8ff702019-11-01 18:07:14 +08001616 }
Chao Yu31083032020-09-14 17:05:13 +08001617 page_array_free(dic->inode, dic->cpages, dic->nr_cpages);
Chao Yu4c8ff702019-11-01 18:07:14 +08001618 }
1619
Chao Yu31083032020-09-14 17:05:13 +08001620 page_array_free(dic->inode, dic->rpages, dic->nr_rpages);
Chao Yuc68d6c82020-09-14 17:05:14 +08001621 kmem_cache_free(dic_entry_slab, dic);
Chao Yu4c8ff702019-11-01 18:07:14 +08001622}
1623
Eric Biggersea29e652021-01-04 22:33:02 -08001624static void f2fs_put_dic(struct decompress_io_ctx *dic)
1625{
1626 if (refcount_dec_and_test(&dic->refcnt))
1627 f2fs_free_dic(dic);
1628}
1629
1630/*
1631 * Update and unlock the cluster's pagecache pages, and release the reference to
1632 * the decompress_io_ctx that was being held for I/O completion.
1633 */
1634static void __f2fs_decompress_end_io(struct decompress_io_ctx *dic, bool failed)
Chao Yu4c8ff702019-11-01 18:07:14 +08001635{
1636 int i;
1637
Eric Biggersea29e652021-01-04 22:33:02 -08001638 for (i = 0; i < dic->cluster_size; i++) {
1639 struct page *rpage = dic->rpages[i];
Chao Yu4c8ff702019-11-01 18:07:14 +08001640
1641 if (!rpage)
1642 continue;
1643
Eric Biggersea29e652021-01-04 22:33:02 -08001644 /* PG_error was set if verity failed. */
1645 if (failed || PageError(rpage)) {
1646 ClearPageUptodate(rpage);
1647 /* will re-read again later */
1648 ClearPageError(rpage);
1649 } else {
Chao Yu23c51be2020-03-21 20:24:11 +08001650 SetPageUptodate(rpage);
Chao Yu4c8ff702019-11-01 18:07:14 +08001651 }
Chao Yu4c8ff702019-11-01 18:07:14 +08001652 unlock_page(rpage);
1653 }
Eric Biggersea29e652021-01-04 22:33:02 -08001654
1655 f2fs_put_dic(dic);
1656}
1657
1658static void f2fs_verify_cluster(struct work_struct *work)
1659{
1660 struct decompress_io_ctx *dic =
1661 container_of(work, struct decompress_io_ctx, verity_work);
1662 int i;
1663
1664 /* Verify the cluster's decompressed pages with fs-verity. */
1665 for (i = 0; i < dic->cluster_size; i++) {
1666 struct page *rpage = dic->rpages[i];
1667
1668 if (rpage && !fsverity_verify_page(rpage))
1669 SetPageError(rpage);
1670 }
1671
1672 __f2fs_decompress_end_io(dic, false);
1673}
1674
1675/*
1676 * This is called when a compressed cluster has been decompressed
1677 * (or failed to be read and/or decompressed).
1678 */
1679void f2fs_decompress_end_io(struct decompress_io_ctx *dic, bool failed)
1680{
1681 if (!failed && dic->need_verity) {
1682 /*
1683 * Note that to avoid deadlocks, the verity work can't be done
1684 * on the decompression workqueue. This is because verifying
1685 * the data pages can involve reading metadata pages from the
1686 * file, and these metadata pages may be compressed.
1687 */
1688 INIT_WORK(&dic->verity_work, f2fs_verify_cluster);
1689 fsverity_enqueue_verify_work(&dic->verity_work);
1690 } else {
1691 __f2fs_decompress_end_io(dic, failed);
1692 }
1693}
1694
1695/*
1696 * Put a reference to a compressed page's decompress_io_ctx.
1697 *
1698 * This is called when the page is no longer needed and can be freed.
1699 */
1700void f2fs_put_page_dic(struct page *page)
1701{
1702 struct decompress_io_ctx *dic =
1703 (struct decompress_io_ctx *)page_private(page);
1704
1705 f2fs_put_dic(dic);
Chao Yu4c8ff702019-11-01 18:07:14 +08001706}
Chao Yu31083032020-09-14 17:05:13 +08001707
1708int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi)
1709{
1710 dev_t dev = sbi->sb->s_bdev->bd_dev;
1711 char slab_name[32];
1712
1713 sprintf(slab_name, "f2fs_page_array_entry-%u:%u", MAJOR(dev), MINOR(dev));
1714
1715 sbi->page_array_slab_size = sizeof(struct page *) <<
1716 F2FS_OPTION(sbi).compress_log_size;
1717
1718 sbi->page_array_slab = f2fs_kmem_cache_create(slab_name,
1719 sbi->page_array_slab_size);
1720 if (!sbi->page_array_slab)
1721 return -ENOMEM;
1722 return 0;
1723}
1724
1725void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi)
1726{
1727 kmem_cache_destroy(sbi->page_array_slab);
1728}
Chao Yuc68d6c82020-09-14 17:05:14 +08001729
1730static int __init f2fs_init_cic_cache(void)
1731{
1732 cic_entry_slab = f2fs_kmem_cache_create("f2fs_cic_entry",
1733 sizeof(struct compress_io_ctx));
1734 if (!cic_entry_slab)
1735 return -ENOMEM;
1736 return 0;
1737}
1738
1739static void f2fs_destroy_cic_cache(void)
1740{
1741 kmem_cache_destroy(cic_entry_slab);
1742}
1743
1744static int __init f2fs_init_dic_cache(void)
1745{
1746 dic_entry_slab = f2fs_kmem_cache_create("f2fs_dic_entry",
1747 sizeof(struct decompress_io_ctx));
1748 if (!dic_entry_slab)
1749 return -ENOMEM;
1750 return 0;
1751}
1752
1753static void f2fs_destroy_dic_cache(void)
1754{
1755 kmem_cache_destroy(dic_entry_slab);
1756}
1757
1758int __init f2fs_init_compress_cache(void)
1759{
1760 int err;
1761
1762 err = f2fs_init_cic_cache();
1763 if (err)
1764 goto out;
1765 err = f2fs_init_dic_cache();
1766 if (err)
1767 goto free_cic;
1768 return 0;
1769free_cic:
1770 f2fs_destroy_cic_cache();
1771out:
1772 return -ENOMEM;
1773}
1774
1775void f2fs_destroy_compress_cache(void)
1776{
1777 f2fs_destroy_dic_cache();
1778 f2fs_destroy_cic_cache();
1779}