blob: a345a41e211999fe386a02f7a85ae03b3220e20b [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
759void 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;
Chao Yub2f57a82020-07-25 09:17:48 +0800768 int i;
Chao Yu4c8ff702019-11-01 18:07:14 +0800769
770 dec_page_count(sbi, F2FS_RD_DATA);
771
772 if (bio->bi_status || PageError(page))
773 dic->failed = true;
774
Chao Yue6c39482020-08-10 18:39:30 +0800775 if (atomic_dec_return(&dic->pending_pages))
Chao Yu4c8ff702019-11-01 18:07:14 +0800776 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
Chao Yu31083032020-09-14 17:05:13 +0800787 dic->tpages = page_array_alloc(dic->inode, dic->cluster_size);
Chao Yub2f57a82020-07-25 09:17:48 +0800788 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
Chao Yu23b1faaad2020-03-03 16:57:07 +0800806 if (cops->init_decompress_ctx) {
807 ret = cops->init_decompress_ctx(dic);
808 if (ret)
809 goto out_free_dic;
810 }
811
Daeho Jeong6fcaeba2020-08-12 14:17:11 +0900812 dic->rbuf = f2fs_vmap(dic->tpages, dic->cluster_size);
Chao Yu4c8ff702019-11-01 18:07:14 +0800813 if (!dic->rbuf) {
814 ret = -ENOMEM;
Chao Yu23b1faaad2020-03-03 16:57:07 +0800815 goto destroy_decompress_ctx;
Chao Yu4c8ff702019-11-01 18:07:14 +0800816 }
817
Daeho Jeong6fcaeba2020-08-12 14:17:11 +0900818 dic->cbuf = f2fs_vmap(dic->cpages, dic->nr_cpages);
Chao Yu4c8ff702019-11-01 18:07:14 +0800819 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
Chao Yu3ed3a942020-12-09 16:42:14 +0800834 if (!ret && (fi->i_compress_flag & 1 << COMPRESS_CHKSUM)) {
Chao Yu1f004d62020-11-26 18:32:09 +0800835 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);
Chao Yu1f004d62020-11-26 18:32:09 +0800847 }
848 }
849
Chao Yu4c8ff702019-11-01 18:07:14 +0800850out_vunmap_cbuf:
Daeho Jeong6fcaeba2020-08-12 14:17:11 +0900851 vm_unmap_ram(dic->cbuf, dic->nr_cpages);
Chao Yu4c8ff702019-11-01 18:07:14 +0800852out_vunmap_rbuf:
Daeho Jeong6fcaeba2020-08-12 14:17:11 +0900853 vm_unmap_ram(dic->rbuf, dic->cluster_size);
Chao Yu23b1faaad2020-03-03 16:57:07 +0800854destroy_decompress_ctx:
855 if (cops->destroy_decompress_ctx)
856 cops->destroy_decompress_ctx(dic);
Chao Yu4c8ff702019-11-01 18:07:14 +0800857out_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
868static 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
875bool f2fs_cluster_is_empty(struct compress_ctx *cc)
876{
877 return cc->nr_rpages == 0;
878}
879
880static bool f2fs_cluster_is_full(struct compress_ctx *cc)
881{
882 return cc->cluster_size == cc->nr_rpages;
883}
884
885bool 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
892static 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
Chao Yu1a67cbe2020-03-12 10:45:29 +0800916static int __f2fs_cluster_blocks(struct compress_ctx *cc, bool compr)
Chao Yu4c8ff702019-11-01 18:07:14 +0800917{
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
Chao Yua2ced1c2020-02-14 17:44:10 +0800937 blkaddr = data_blkaddr(dn.inode,
Chao Yu4c8ff702019-11-01 18:07:14 +0800938 dn.node_page, dn.ofs_in_node + i);
Chao Yu1a67cbe2020-03-12 10:45:29 +0800939 if (compr) {
940 if (__is_valid_data_blkaddr(blkaddr))
941 ret++;
942 } else {
943 if (blkaddr != NULL_ADDR)
944 ret++;
945 }
Chao Yu4c8ff702019-11-01 18:07:14 +0800946 }
947 }
948fail:
949 f2fs_put_dnode(&dn);
950 return ret;
951}
952
Chao Yu1a67cbe2020-03-12 10:45:29 +0800953/* return # of compressed blocks in compressed cluster */
954static 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 */
Wang Xiaojund0783192020-06-16 10:39:24 +0800960static int f2fs_cluster_blocks(struct compress_ctx *cc)
Chao Yu1a67cbe2020-03-12 10:45:29 +0800961{
962 return __f2fs_cluster_blocks(cc, false);
963}
964
Chao Yu4c8ff702019-11-01 18:07:14 +0800965int 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
Wang Xiaojund0783192020-06-16 10:39:24 +0800974 return f2fs_cluster_blocks(&cc);
Chao Yu4c8ff702019-11-01 18:07:14 +0800975}
976
977static bool cluster_may_compress(struct compress_ctx *cc)
978{
Daeho Jeongf15a2002020-12-01 13:08:02 +0900979 if (!f2fs_need_compress_data(cc->inode))
Chao Yu4c8ff702019-11-01 18:07:14 +0800980 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;
Chao Yudc35d732020-05-26 09:55:02 +0800987 if (unlikely(f2fs_cp_error(F2FS_I_SB(cc->inode))))
988 return false;
Chao Yu4c8ff702019-11-01 18:07:14 +0800989 return __cluster_may_compress(cc);
990}
991
992static 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
1002static 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
1011static 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
1024retry:
Wang Xiaojund0783192020-06-16 10:39:24 +08001025 ret = f2fs_cluster_blocks(cc);
Chao Yu4c8ff702019-11-01 18:07:14 +08001026 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,
Chao Yu06837282020-02-18 18:21:35 +08001055 &last_block_in_bio, false, true);
Chao Yu4c8ff702019-11-01 18:07:14 +08001056 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);
Chao Yubc67c5d2020-06-08 20:03:17 +08001080 f2fs_put_rpages_mapping(mapping, start_idx,
Chao Yu4c8ff702019-11-01 18:07:14 +08001081 cc->cluster_size);
1082 f2fs_destroy_compress_ctx(cc);
1083 goto retry;
1084 }
1085 }
1086
1087 if (prealloc) {
Chao Yu0ef81832020-06-18 14:36:22 +08001088 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
Chao Yu4c8ff702019-11-01 18:07:14 +08001089
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
Chao Yu0ef81832020-06-18 14:36:22 +08001103 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
Chao Yu4c8ff702019-11-01 18:07:14 +08001104 }
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
1112unlock_pages:
1113 f2fs_unlock_rpages(cc, i);
1114release_pages:
Chao Yubc67c5d2020-06-08 20:03:17 +08001115 f2fs_put_rpages_mapping(mapping, start_idx, i);
Chao Yu4c8ff702019-11-01 18:07:14 +08001116 f2fs_destroy_compress_ctx(cc);
1117 return ret;
1118}
1119
1120int 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
1135bool f2fs_compress_write_end(struct inode *inode, void *fsdata,
1136 pgoff_t index, unsigned copied)
1137
1138{
1139 struct compress_ctx cc = {
Chao Yu31083032020-09-14 17:05:13 +08001140 .inode = inode,
Chao Yu4c8ff702019-11-01 18:07:14 +08001141 .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
Chao Yu3265d3d2020-03-18 16:22:59 +08001156int 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
Chao Yu4c8ff702019-11-01 18:07:14 +08001205static 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,
Chao Yu4c8ff702019-11-01 18:07:14 +08001224 .io_type = io_type,
1225 .io_wbc = wbc,
Satya Tangirala27aacd22020-07-02 01:56:06 +00001226 .encrypted = fscrypt_inode_uses_fs_layer_crypto(cc->inode),
Chao Yu4c8ff702019-11-01 18:07:14 +08001227 };
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
Chao Yu79963d92020-06-18 14:36:23 +08001236 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)) {
Chao Yu31083032020-09-14 17:05:13 +08001244 goto out_free;
Chao Yu79963d92020-06-18 14:36:23 +08001245 }
Chao Yu4c8ff702019-11-01 18:07:14 +08001246
Chao Yudf77fbd2020-02-24 19:20:16 +08001247 set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
Chao Yu4c8ff702019-11-01 18:07:14 +08001248
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++) {
Chao Yua2ced1c2020-02-14 17:44:10 +08001254 if (data_blkaddr(dn.inode, dn.node_page,
Chao Yu4c8ff702019-11-01 18:07:14 +08001255 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
Chao Yuc68d6c82020-09-14 17:05:14 +08001267 cic = kmem_cache_zalloc(cic_entry_slab, GFP_NOFS);
Chao Yu4c8ff702019-11-01 18:07:14 +08001268 if (!cic)
1269 goto out_put_dnode;
1270
1271 cic->magic = F2FS_COMPRESSED_PAGE_MAGIC;
1272 cic->inode = inode;
Chao Yue6c39482020-08-10 18:39:30 +08001273 atomic_set(&cic->pending_pages, cc->nr_cpages);
Chao Yu31083032020-09-14 17:05:13 +08001274 cic->rpages = page_array_alloc(cc->inode, cc->cluster_size);
Chao Yu4c8ff702019-11-01 18:07:14 +08001275 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,
Chao Yu887347a2020-03-28 17:33:23 +08001282 cc->rpages[i + 1]->index, cic);
Chao Yu4c8ff702019-11-01 18:07:14 +08001283 fio.compressed_page = cc->cpages[i];
Chao Yuf567adb2020-07-03 16:40:11 +08001284
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
Chao Yu4c8ff702019-11-01 18:07:14 +08001291 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
Chao Yua2ced1c2020-02-14 17:44:10 +08001308 blkaddr = f2fs_data_blkaddr(&dn);
Chao Yu95978ca2020-02-28 18:08:46 +08001309 fio.page = cc->rpages[i];
Chao Yu4c8ff702019-11-01 18:07:14 +08001310 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)++;
1343unlock_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);
Chao Yu79963d92020-06-18 14:36:23 +08001357 if (IS_NOQUOTA(inode))
1358 up_read(&sbi->node_write);
1359 else
Jaegeuk Kim435cbab2020-04-09 10:25:21 -07001360 f2fs_unlock_op(sbi);
Chao Yu4c8ff702019-11-01 18:07:14 +08001361
Chao Yuc10c9822020-02-27 19:30:03 +08001362 spin_lock(&fi->i_size_lock);
Chao Yu4c8ff702019-11-01 18:07:14 +08001363 if (fi->last_disk_size < psize)
1364 fi->last_disk_size = psize;
Chao Yuc10c9822020-02-27 19:30:03 +08001365 spin_unlock(&fi->i_size_lock);
Chao Yu4c8ff702019-11-01 18:07:14 +08001366
1367 f2fs_put_rpages(cc);
Chao Yu31083032020-09-14 17:05:13 +08001368 page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
1369 cc->cpages = NULL;
Chao Yu4c8ff702019-11-01 18:07:14 +08001370 f2fs_destroy_compress_ctx(cc);
1371 return 0;
1372
1373out_destroy_crypt:
Chao Yu31083032020-09-14 17:05:13 +08001374 page_array_free(cc->inode, cic->rpages, cc->cluster_size);
Chao Yu4c8ff702019-11-01 18:07:14 +08001375
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 }
1383out_put_cic:
Chao Yuc68d6c82020-09-14 17:05:14 +08001384 kmem_cache_free(cic_entry_slab, cic);
Chao Yu4c8ff702019-11-01 18:07:14 +08001385out_put_dnode:
1386 f2fs_put_dnode(&dn);
1387out_unlock_op:
Chao Yu79963d92020-06-18 14:36:23 +08001388 if (IS_NOQUOTA(inode))
1389 up_read(&sbi->node_write);
1390 else
Jaegeuk Kim435cbab2020-04-09 10:25:21 -07001391 f2fs_unlock_op(sbi);
Chao Yu31083032020-09-14 17:05:13 +08001392out_free:
1393 page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
1394 cc->cpages = NULL;
Chao Yu4c8ff702019-11-01 18:07:14 +08001395 return -EAGAIN;
1396}
1397
1398void 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
Chao Yu5e6bbde2020-04-08 19:56:05 +08001408 f2fs_compress_free_page(page);
Chao Yu4c8ff702019-11-01 18:07:14 +08001409
1410 dec_page_count(sbi, F2FS_WB_DATA);
1411
Chao Yue6c39482020-08-10 18:39:30 +08001412 if (atomic_dec_return(&cic->pending_pages))
Chao Yu4c8ff702019-11-01 18:07:14 +08001413 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
Chao Yu31083032020-09-14 17:05:13 +08001421 page_array_free(cic->inode, cic->rpages, cic->nr_rpages);
Chao Yuc68d6c82020-09-14 17:05:14 +08001422 kmem_cache_free(cic_entry_slab, cic);
Chao Yu4c8ff702019-11-01 18:07:14 +08001423}
1424
1425static 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;
1443retry_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) {
Chao Yu466357d2020-03-20 18:14:31 +08001459 /*
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 }
Chao Yu4c8ff702019-11-01 18:07:14 +08001468 ret = 0;
1469 cond_resched();
Chao Yu5df7731f2020-02-17 17:45:44 +08001470 congestion_wait(BLK_RW_ASYNC,
1471 DEFAULT_IO_TIMEOUT);
Chao Yu4c8ff702019-11-01 18:07:14 +08001472 lock_page(cc->rpages[i]);
Chao Yueb1353c2020-06-19 17:14:19 +08001473
1474 if (!PageDirty(cc->rpages[i])) {
1475 unlock_page(cc->rpages[i]);
1476 continue;
1477 }
1478
Chao Yu4c8ff702019-11-01 18:07:14 +08001479 clear_page_dirty_for_io(cc->rpages[i]);
1480 goto retry_write;
1481 }
1482 err = ret;
Chao Yu466357d2020-03-20 18:14:31 +08001483 goto out_err;
Chao Yu4c8ff702019-11-01 18:07:14 +08001484 }
1485
1486 *submitted += _submitted;
1487 }
1488 return 0;
Chao Yu4c8ff702019-11-01 18:07:14 +08001489out_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
1499int f2fs_write_multi_pages(struct compress_ctx *cc,
1500 int *submitted,
1501 struct writeback_control *wbc,
1502 enum iostat_type io_type)
1503{
Chao Yu4c8ff702019-11-01 18:07:14 +08001504 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);
Chao Yu4c8ff702019-11-01 18:07:14 +08001518 if (!err)
1519 return 0;
1520 f2fs_bug_on(F2FS_I_SB(cc->inode), err != -EAGAIN);
1521 }
1522write:
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);
1527destroy_out:
1528 f2fs_destroy_compress_ctx(cc);
1529 return err;
1530}
1531
1532struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc)
1533{
Chao Yu4c8ff702019-11-01 18:07:14 +08001534 struct decompress_io_ctx *dic;
1535 pgoff_t start_idx = start_idx_of_cluster(cc);
1536 int i;
1537
Chao Yuc68d6c82020-09-14 17:05:14 +08001538 dic = kmem_cache_zalloc(dic_entry_slab, GFP_NOFS);
Chao Yu4c8ff702019-11-01 18:07:14 +08001539 if (!dic)
1540 return ERR_PTR(-ENOMEM);
1541
Chao Yu31083032020-09-14 17:05:13 +08001542 dic->rpages = page_array_alloc(cc->inode, cc->cluster_size);
Chao Yu4c8ff702019-11-01 18:07:14 +08001543 if (!dic->rpages) {
Chao Yuc68d6c82020-09-14 17:05:14 +08001544 kmem_cache_free(dic_entry_slab, dic);
Chao Yu4c8ff702019-11-01 18:07:14 +08001545 return ERR_PTR(-ENOMEM);
1546 }
1547
1548 dic->magic = F2FS_COMPRESSED_PAGE_MAGIC;
1549 dic->inode = cc->inode;
Chao Yue6c39482020-08-10 18:39:30 +08001550 atomic_set(&dic->pending_pages, cc->nr_cpages);
Chao Yu4c8ff702019-11-01 18:07:14 +08001551 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
Chao Yu31083032020-09-14 17:05:13 +08001561 dic->cpages = page_array_alloc(dic->inode, dic->nr_cpages);
Chao Yu4c8ff702019-11-01 18:07:14 +08001562 if (!dic->cpages)
1563 goto out_free;
1564
1565 for (i = 0; i < dic->nr_cpages; i++) {
1566 struct page *page;
1567
Chao Yu5e6bbde2020-04-08 19:56:05 +08001568 page = f2fs_compress_alloc_page();
Chao Yu4c8ff702019-11-01 18:07:14 +08001569 if (!page)
1570 goto out_free;
1571
1572 f2fs_set_compressed_page(page, cc->inode,
Chao Yu887347a2020-03-28 17:33:23 +08001573 start_idx + i + 1, dic);
Chao Yu4c8ff702019-11-01 18:07:14 +08001574 dic->cpages[i] = page;
1575 }
1576
Chao Yu4c8ff702019-11-01 18:07:14 +08001577 return dic;
1578
1579out_free:
1580 f2fs_free_dic(dic);
1581 return ERR_PTR(-ENOMEM);
1582}
1583
1584void 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;
Chao Yu8908e752020-03-26 17:42:26 +08001592 if (!dic->tpages[i])
1593 continue;
Chao Yu5e6bbde2020-04-08 19:56:05 +08001594 f2fs_compress_free_page(dic->tpages[i]);
Chao Yu4c8ff702019-11-01 18:07:14 +08001595 }
Chao Yu31083032020-09-14 17:05:13 +08001596 page_array_free(dic->inode, dic->tpages, dic->cluster_size);
Chao Yu4c8ff702019-11-01 18:07:14 +08001597 }
1598
1599 if (dic->cpages) {
1600 for (i = 0; i < dic->nr_cpages; i++) {
1601 if (!dic->cpages[i])
1602 continue;
Chao Yu5e6bbde2020-04-08 19:56:05 +08001603 f2fs_compress_free_page(dic->cpages[i]);
Chao Yu4c8ff702019-11-01 18:07:14 +08001604 }
Chao Yu31083032020-09-14 17:05:13 +08001605 page_array_free(dic->inode, dic->cpages, dic->nr_cpages);
Chao Yu4c8ff702019-11-01 18:07:14 +08001606 }
1607
Chao Yu31083032020-09-14 17:05:13 +08001608 page_array_free(dic->inode, dic->rpages, dic->nr_rpages);
Chao Yuc68d6c82020-09-14 17:05:14 +08001609 kmem_cache_free(dic_entry_slab, dic);
Chao Yu4c8ff702019-11-01 18:07:14 +08001610}
1611
1612void 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
Chao Yu23c51be2020-03-21 20:24:11 +08001623 if (err || PageError(rpage))
1624 goto clear_uptodate;
1625
1626 if (!verity || fsverity_verify_page(rpage)) {
1627 SetPageUptodate(rpage);
1628 goto unlock;
Chao Yu4c8ff702019-11-01 18:07:14 +08001629 }
Chao Yu23c51be2020-03-21 20:24:11 +08001630clear_uptodate:
1631 ClearPageUptodate(rpage);
1632 ClearPageError(rpage);
1633unlock:
Chao Yu4c8ff702019-11-01 18:07:14 +08001634 unlock_page(rpage);
1635 }
1636}
Chao Yu31083032020-09-14 17:05:13 +08001637
1638int 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
1655void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi)
1656{
1657 kmem_cache_destroy(sbi->page_array_slab);
1658}
Chao Yuc68d6c82020-09-14 17:05:14 +08001659
1660static 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
1669static void f2fs_destroy_cic_cache(void)
1670{
1671 kmem_cache_destroy(cic_entry_slab);
1672}
1673
1674static 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
1683static void f2fs_destroy_dic_cache(void)
1684{
1685 kmem_cache_destroy(dic_entry_slab);
1686}
1687
1688int __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;
1699free_cic:
1700 f2fs_destroy_cic_cache();
1701out:
1702 return -ENOMEM;
1703}
1704
1705void f2fs_destroy_compress_cache(void)
1706{
1707 f2fs_destroy_dic_cache();
1708 f2fs_destroy_cic_cache();
1709}