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