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