blob: 7895186cc765c8ae4dfb22903b1bfc1840f17d84 [file] [log] [blame]
Chao Yu4c8ff702019-11-01 18:07:14 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * f2fs compress support
4 *
5 * Copyright (c) 2019 Chao Yu <chao@kernel.org>
6 */
7
8#include <linux/fs.h>
9#include <linux/f2fs_fs.h>
10#include <linux/writeback.h>
11#include <linux/backing-dev.h>
12#include <linux/lzo.h>
13#include <linux/lz4.h>
Chao Yu50cfa662020-03-03 17:46:02 +080014#include <linux/zstd.h>
Chao Yu4c8ff702019-11-01 18:07:14 +080015
16#include "f2fs.h"
17#include "node.h"
18#include <trace/events/f2fs.h>
19
20struct f2fs_compress_ops {
21 int (*init_compress_ctx)(struct compress_ctx *cc);
22 void (*destroy_compress_ctx)(struct compress_ctx *cc);
23 int (*compress_pages)(struct compress_ctx *cc);
Chao Yu23b1faaad2020-03-03 16:57:07 +080024 int (*init_decompress_ctx)(struct decompress_io_ctx *dic);
25 void (*destroy_decompress_ctx)(struct decompress_io_ctx *dic);
Chao Yu4c8ff702019-11-01 18:07:14 +080026 int (*decompress_pages)(struct decompress_io_ctx *dic);
27};
28
29static unsigned int offset_in_cluster(struct compress_ctx *cc, pgoff_t index)
30{
31 return index & (cc->cluster_size - 1);
32}
33
34static pgoff_t cluster_idx(struct compress_ctx *cc, pgoff_t index)
35{
36 return index >> cc->log_cluster_size;
37}
38
39static pgoff_t start_idx_of_cluster(struct compress_ctx *cc)
40{
41 return cc->cluster_idx << cc->log_cluster_size;
42}
43
44bool f2fs_is_compressed_page(struct page *page)
45{
46 if (!PagePrivate(page))
47 return false;
48 if (!page_private(page))
49 return false;
50 if (IS_ATOMIC_WRITTEN_PAGE(page) || IS_DUMMY_WRITTEN_PAGE(page))
51 return false;
Yu Changchun29b993c2020-06-20 03:58:29 -040052 /*
53 * page->private may be set with pid.
54 * pid_max is enough to check if it is traced.
55 */
56 if (IS_IO_TRACED_PAGE(page))
57 return false;
58
Chao Yu4c8ff702019-11-01 18:07:14 +080059 f2fs_bug_on(F2FS_M_SB(page->mapping),
60 *((u32 *)page_private(page)) != F2FS_COMPRESSED_PAGE_MAGIC);
61 return true;
62}
63
64static void f2fs_set_compressed_page(struct page *page,
Chao Yu887347a2020-03-28 17:33:23 +080065 struct inode *inode, pgoff_t index, void *data)
Chao Yu4c8ff702019-11-01 18:07:14 +080066{
67 SetPagePrivate(page);
68 set_page_private(page, (unsigned long)data);
69
70 /* i_crypto_info and iv index */
71 page->index = index;
72 page->mapping = inode->i_mapping;
Chao Yu4c8ff702019-11-01 18:07:14 +080073}
74
Chao Yu4c8ff702019-11-01 18:07:14 +080075static void f2fs_drop_rpages(struct compress_ctx *cc, int len, bool unlock)
76{
77 int i;
78
79 for (i = 0; i < len; i++) {
80 if (!cc->rpages[i])
81 continue;
82 if (unlock)
83 unlock_page(cc->rpages[i]);
84 else
85 put_page(cc->rpages[i]);
86 }
87}
88
89static void f2fs_put_rpages(struct compress_ctx *cc)
90{
91 f2fs_drop_rpages(cc, cc->cluster_size, false);
92}
93
94static void f2fs_unlock_rpages(struct compress_ctx *cc, int len)
95{
96 f2fs_drop_rpages(cc, len, true);
97}
98
Chao Yubc67c5d2020-06-08 20:03:17 +080099static void f2fs_put_rpages_mapping(struct address_space *mapping,
Chao Yu4c8ff702019-11-01 18:07:14 +0800100 pgoff_t start, int len)
101{
102 int i;
103
104 for (i = 0; i < len; i++) {
105 struct page *page = find_get_page(mapping, start + i);
106
107 put_page(page);
108 put_page(page);
109 }
110}
111
112static void f2fs_put_rpages_wbc(struct compress_ctx *cc,
113 struct writeback_control *wbc, bool redirty, int unlock)
114{
115 unsigned int i;
116
117 for (i = 0; i < cc->cluster_size; i++) {
118 if (!cc->rpages[i])
119 continue;
120 if (redirty)
121 redirty_page_for_writepage(wbc, cc->rpages[i]);
122 f2fs_put_page(cc->rpages[i], unlock);
123 }
124}
125
126struct page *f2fs_compress_control_page(struct page *page)
127{
128 return ((struct compress_io_ctx *)page_private(page))->rpages[0];
129}
130
131int f2fs_init_compress_ctx(struct compress_ctx *cc)
132{
133 struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
134
135 if (cc->nr_rpages)
136 return 0;
137
138 cc->rpages = f2fs_kzalloc(sbi, sizeof(struct page *) <<
139 cc->log_cluster_size, GFP_NOFS);
140 return cc->rpages ? 0 : -ENOMEM;
141}
142
143void f2fs_destroy_compress_ctx(struct compress_ctx *cc)
144{
145 kfree(cc->rpages);
146 cc->rpages = NULL;
147 cc->nr_rpages = 0;
148 cc->nr_cpages = 0;
149 cc->cluster_idx = NULL_CLUSTER;
150}
151
152void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct page *page)
153{
154 unsigned int cluster_ofs;
155
156 if (!f2fs_cluster_can_merge_page(cc, page->index))
157 f2fs_bug_on(F2FS_I_SB(cc->inode), 1);
158
159 cluster_ofs = offset_in_cluster(cc, page->index);
160 cc->rpages[cluster_ofs] = page;
161 cc->nr_rpages++;
162 cc->cluster_idx = cluster_idx(cc, page->index);
163}
164
165#ifdef CONFIG_F2FS_FS_LZO
166static int lzo_init_compress_ctx(struct compress_ctx *cc)
167{
168 cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
169 LZO1X_MEM_COMPRESS, GFP_NOFS);
170 if (!cc->private)
171 return -ENOMEM;
172
173 cc->clen = lzo1x_worst_compress(PAGE_SIZE << cc->log_cluster_size);
174 return 0;
175}
176
177static void lzo_destroy_compress_ctx(struct compress_ctx *cc)
178{
179 kvfree(cc->private);
180 cc->private = NULL;
181}
182
183static int lzo_compress_pages(struct compress_ctx *cc)
184{
185 int ret;
186
187 ret = lzo1x_1_compress(cc->rbuf, cc->rlen, cc->cbuf->cdata,
188 &cc->clen, cc->private);
189 if (ret != LZO_E_OK) {
190 printk_ratelimited("%sF2FS-fs (%s): lzo compress failed, ret:%d\n",
191 KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id, ret);
192 return -EIO;
193 }
194 return 0;
195}
196
197static int lzo_decompress_pages(struct decompress_io_ctx *dic)
198{
199 int ret;
200
201 ret = lzo1x_decompress_safe(dic->cbuf->cdata, dic->clen,
202 dic->rbuf, &dic->rlen);
203 if (ret != LZO_E_OK) {
204 printk_ratelimited("%sF2FS-fs (%s): lzo decompress failed, ret:%d\n",
205 KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id, ret);
206 return -EIO;
207 }
208
209 if (dic->rlen != PAGE_SIZE << dic->log_cluster_size) {
210 printk_ratelimited("%sF2FS-fs (%s): lzo invalid rlen:%zu, "
211 "expected:%lu\n", KERN_ERR,
212 F2FS_I_SB(dic->inode)->sb->s_id,
213 dic->rlen,
214 PAGE_SIZE << dic->log_cluster_size);
215 return -EIO;
216 }
217 return 0;
218}
219
220static const struct f2fs_compress_ops f2fs_lzo_ops = {
221 .init_compress_ctx = lzo_init_compress_ctx,
222 .destroy_compress_ctx = lzo_destroy_compress_ctx,
223 .compress_pages = lzo_compress_pages,
224 .decompress_pages = lzo_decompress_pages,
225};
226#endif
227
228#ifdef CONFIG_F2FS_FS_LZ4
229static int lz4_init_compress_ctx(struct compress_ctx *cc)
230{
231 cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
232 LZ4_MEM_COMPRESS, GFP_NOFS);
233 if (!cc->private)
234 return -ENOMEM;
235
Chao Yuf6644142020-05-09 15:01:04 +0800236 /*
237 * we do not change cc->clen to LZ4_compressBound(inputsize) to
238 * adapt worst compress case, because lz4 compressor can handle
239 * output budget properly.
240 */
241 cc->clen = cc->rlen - PAGE_SIZE - COMPRESS_HEADER_SIZE;
Chao Yu4c8ff702019-11-01 18:07:14 +0800242 return 0;
243}
244
245static void lz4_destroy_compress_ctx(struct compress_ctx *cc)
246{
247 kvfree(cc->private);
248 cc->private = NULL;
249}
250
251static int lz4_compress_pages(struct compress_ctx *cc)
252{
253 int len;
254
255 len = LZ4_compress_default(cc->rbuf, cc->cbuf->cdata, cc->rlen,
256 cc->clen, cc->private);
Chao Yuf6644142020-05-09 15:01:04 +0800257 if (!len)
258 return -EAGAIN;
259
Chao Yu4c8ff702019-11-01 18:07:14 +0800260 cc->clen = len;
261 return 0;
262}
263
264static int lz4_decompress_pages(struct decompress_io_ctx *dic)
265{
266 int ret;
267
268 ret = LZ4_decompress_safe(dic->cbuf->cdata, dic->rbuf,
269 dic->clen, dic->rlen);
270 if (ret < 0) {
271 printk_ratelimited("%sF2FS-fs (%s): lz4 decompress failed, ret:%d\n",
272 KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id, ret);
273 return -EIO;
274 }
275
276 if (ret != PAGE_SIZE << dic->log_cluster_size) {
277 printk_ratelimited("%sF2FS-fs (%s): lz4 invalid rlen:%zu, "
278 "expected:%lu\n", KERN_ERR,
279 F2FS_I_SB(dic->inode)->sb->s_id,
280 dic->rlen,
281 PAGE_SIZE << dic->log_cluster_size);
282 return -EIO;
283 }
284 return 0;
285}
286
287static const struct f2fs_compress_ops f2fs_lz4_ops = {
288 .init_compress_ctx = lz4_init_compress_ctx,
289 .destroy_compress_ctx = lz4_destroy_compress_ctx,
290 .compress_pages = lz4_compress_pages,
291 .decompress_pages = lz4_decompress_pages,
292};
293#endif
294
Chao Yu50cfa662020-03-03 17:46:02 +0800295#ifdef CONFIG_F2FS_FS_ZSTD
296#define F2FS_ZSTD_DEFAULT_CLEVEL 1
297
298static int zstd_init_compress_ctx(struct compress_ctx *cc)
299{
300 ZSTD_parameters params;
301 ZSTD_CStream *stream;
302 void *workspace;
303 unsigned int workspace_size;
304
305 params = ZSTD_getParams(F2FS_ZSTD_DEFAULT_CLEVEL, cc->rlen, 0);
306 workspace_size = ZSTD_CStreamWorkspaceBound(params.cParams);
307
308 workspace = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
309 workspace_size, GFP_NOFS);
310 if (!workspace)
311 return -ENOMEM;
312
313 stream = ZSTD_initCStream(params, 0, workspace, workspace_size);
314 if (!stream) {
315 printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_initCStream failed\n",
316 KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
317 __func__);
318 kvfree(workspace);
319 return -EIO;
320 }
321
322 cc->private = workspace;
323 cc->private2 = stream;
324
325 cc->clen = cc->rlen - PAGE_SIZE - COMPRESS_HEADER_SIZE;
326 return 0;
327}
328
329static void zstd_destroy_compress_ctx(struct compress_ctx *cc)
330{
331 kvfree(cc->private);
332 cc->private = NULL;
333 cc->private2 = NULL;
334}
335
336static int zstd_compress_pages(struct compress_ctx *cc)
337{
338 ZSTD_CStream *stream = cc->private2;
339 ZSTD_inBuffer inbuf;
340 ZSTD_outBuffer outbuf;
341 int src_size = cc->rlen;
342 int dst_size = src_size - PAGE_SIZE - COMPRESS_HEADER_SIZE;
343 int ret;
344
345 inbuf.pos = 0;
346 inbuf.src = cc->rbuf;
347 inbuf.size = src_size;
348
349 outbuf.pos = 0;
350 outbuf.dst = cc->cbuf->cdata;
351 outbuf.size = dst_size;
352
353 ret = ZSTD_compressStream(stream, &outbuf, &inbuf);
354 if (ZSTD_isError(ret)) {
355 printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_compressStream failed, ret: %d\n",
356 KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
357 __func__, ZSTD_getErrorCode(ret));
358 return -EIO;
359 }
360
361 ret = ZSTD_endStream(stream, &outbuf);
362 if (ZSTD_isError(ret)) {
363 printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_endStream returned %d\n",
364 KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
365 __func__, ZSTD_getErrorCode(ret));
366 return -EIO;
367 }
368
Chao Yu1454c972020-05-08 09:16:03 +0800369 /*
370 * there is compressed data remained in intermediate buffer due to
371 * no more space in cbuf.cdata
372 */
373 if (ret)
374 return -EAGAIN;
375
Chao Yu50cfa662020-03-03 17:46:02 +0800376 cc->clen = outbuf.pos;
377 return 0;
378}
379
380static int zstd_init_decompress_ctx(struct decompress_io_ctx *dic)
381{
382 ZSTD_DStream *stream;
383 void *workspace;
384 unsigned int workspace_size;
Chao Yu0e2b7382020-09-02 15:01:52 +0800385 unsigned int max_window_size =
386 MAX_COMPRESS_WINDOW_SIZE(dic->log_cluster_size);
Chao Yu50cfa662020-03-03 17:46:02 +0800387
Chao Yu0e2b7382020-09-02 15:01:52 +0800388 workspace_size = ZSTD_DStreamWorkspaceBound(max_window_size);
Chao Yu50cfa662020-03-03 17:46:02 +0800389
390 workspace = f2fs_kvmalloc(F2FS_I_SB(dic->inode),
391 workspace_size, GFP_NOFS);
392 if (!workspace)
393 return -ENOMEM;
394
Chao Yu0e2b7382020-09-02 15:01:52 +0800395 stream = ZSTD_initDStream(max_window_size, workspace, workspace_size);
Chao Yu50cfa662020-03-03 17:46:02 +0800396 if (!stream) {
397 printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_initDStream failed\n",
398 KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id,
399 __func__);
400 kvfree(workspace);
401 return -EIO;
402 }
403
404 dic->private = workspace;
405 dic->private2 = stream;
406
407 return 0;
408}
409
410static void zstd_destroy_decompress_ctx(struct decompress_io_ctx *dic)
411{
412 kvfree(dic->private);
413 dic->private = NULL;
414 dic->private2 = NULL;
415}
416
417static int zstd_decompress_pages(struct decompress_io_ctx *dic)
418{
419 ZSTD_DStream *stream = dic->private2;
420 ZSTD_inBuffer inbuf;
421 ZSTD_outBuffer outbuf;
422 int ret;
423
424 inbuf.pos = 0;
425 inbuf.src = dic->cbuf->cdata;
426 inbuf.size = dic->clen;
427
428 outbuf.pos = 0;
429 outbuf.dst = dic->rbuf;
430 outbuf.size = dic->rlen;
431
432 ret = ZSTD_decompressStream(stream, &outbuf, &inbuf);
433 if (ZSTD_isError(ret)) {
434 printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_compressStream failed, ret: %d\n",
435 KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id,
436 __func__, ZSTD_getErrorCode(ret));
437 return -EIO;
438 }
439
440 if (dic->rlen != outbuf.pos) {
441 printk_ratelimited("%sF2FS-fs (%s): %s ZSTD invalid rlen:%zu, "
442 "expected:%lu\n", KERN_ERR,
443 F2FS_I_SB(dic->inode)->sb->s_id,
444 __func__, dic->rlen,
445 PAGE_SIZE << dic->log_cluster_size);
446 return -EIO;
447 }
448
449 return 0;
450}
451
452static const struct f2fs_compress_ops f2fs_zstd_ops = {
453 .init_compress_ctx = zstd_init_compress_ctx,
454 .destroy_compress_ctx = zstd_destroy_compress_ctx,
455 .compress_pages = zstd_compress_pages,
456 .init_decompress_ctx = zstd_init_decompress_ctx,
457 .destroy_decompress_ctx = zstd_destroy_decompress_ctx,
458 .decompress_pages = zstd_decompress_pages,
459};
460#endif
461
Chao Yu6d92b202020-04-08 19:56:32 +0800462#ifdef CONFIG_F2FS_FS_LZO
463#ifdef CONFIG_F2FS_FS_LZORLE
464static int lzorle_compress_pages(struct compress_ctx *cc)
465{
466 int ret;
467
468 ret = lzorle1x_1_compress(cc->rbuf, cc->rlen, cc->cbuf->cdata,
469 &cc->clen, cc->private);
470 if (ret != LZO_E_OK) {
471 printk_ratelimited("%sF2FS-fs (%s): lzo-rle compress failed, ret:%d\n",
472 KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id, ret);
473 return -EIO;
474 }
475 return 0;
476}
477
478static const struct f2fs_compress_ops f2fs_lzorle_ops = {
479 .init_compress_ctx = lzo_init_compress_ctx,
480 .destroy_compress_ctx = lzo_destroy_compress_ctx,
481 .compress_pages = lzorle_compress_pages,
482 .decompress_pages = lzo_decompress_pages,
483};
484#endif
485#endif
486
Chao Yu4c8ff702019-11-01 18:07:14 +0800487static const struct f2fs_compress_ops *f2fs_cops[COMPRESS_MAX] = {
488#ifdef CONFIG_F2FS_FS_LZO
489 &f2fs_lzo_ops,
490#else
491 NULL,
492#endif
493#ifdef CONFIG_F2FS_FS_LZ4
494 &f2fs_lz4_ops,
495#else
496 NULL,
497#endif
Chao Yu50cfa662020-03-03 17:46:02 +0800498#ifdef CONFIG_F2FS_FS_ZSTD
499 &f2fs_zstd_ops,
500#else
501 NULL,
502#endif
Chao Yu6d92b202020-04-08 19:56:32 +0800503#if defined(CONFIG_F2FS_FS_LZO) && defined(CONFIG_F2FS_FS_LZORLE)
504 &f2fs_lzorle_ops,
505#else
506 NULL,
507#endif
Chao Yu4c8ff702019-11-01 18:07:14 +0800508};
509
510bool f2fs_is_compress_backend_ready(struct inode *inode)
511{
512 if (!f2fs_compressed_file(inode))
513 return true;
514 return f2fs_cops[F2FS_I(inode)->i_compress_algorithm];
515}
516
Jaegeuk Kim99bbe302020-06-16 09:56:51 -0700517static mempool_t *compress_page_pool;
Chao Yu5e6bbde2020-04-08 19:56:05 +0800518static int num_compress_pages = 512;
519module_param(num_compress_pages, uint, 0444);
520MODULE_PARM_DESC(num_compress_pages,
521 "Number of intermediate compress pages to preallocate");
522
523int f2fs_init_compress_mempool(void)
524{
525 compress_page_pool = mempool_create_page_pool(num_compress_pages, 0);
526 if (!compress_page_pool)
527 return -ENOMEM;
528
529 return 0;
530}
531
532void f2fs_destroy_compress_mempool(void)
533{
534 mempool_destroy(compress_page_pool);
535}
536
537static struct page *f2fs_compress_alloc_page(void)
Chao Yu4c8ff702019-11-01 18:07:14 +0800538{
539 struct page *page;
540
Chao Yu5e6bbde2020-04-08 19:56:05 +0800541 page = mempool_alloc(compress_page_pool, GFP_NOFS);
Chao Yu4c8ff702019-11-01 18:07:14 +0800542 lock_page(page);
Chao Yu5e6bbde2020-04-08 19:56:05 +0800543
Chao Yu4c8ff702019-11-01 18:07:14 +0800544 return page;
545}
546
Chao Yu5e6bbde2020-04-08 19:56:05 +0800547static void f2fs_compress_free_page(struct page *page)
548{
549 if (!page)
550 return;
551 set_page_private(page, (unsigned long)NULL);
552 ClearPagePrivate(page);
553 page->mapping = NULL;
554 unlock_page(page);
555 mempool_free(page, compress_page_pool);
556}
557
Daeho Jeong6fcaeba2020-08-12 14:17:11 +0900558#define MAX_VMAP_RETRIES 3
559
560static void *f2fs_vmap(struct page **pages, unsigned int count)
561{
562 int i;
563 void *buf = NULL;
564
565 for (i = 0; i < MAX_VMAP_RETRIES; i++) {
566 buf = vm_map_ram(pages, count, -1);
567 if (buf)
568 break;
569 vm_unmap_aliases();
570 }
571 return buf;
572}
573
Chao Yu4c8ff702019-11-01 18:07:14 +0800574static int f2fs_compress_pages(struct compress_ctx *cc)
575{
576 struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
577 struct f2fs_inode_info *fi = F2FS_I(cc->inode);
578 const struct f2fs_compress_ops *cops =
579 f2fs_cops[fi->i_compress_algorithm];
580 unsigned int max_len, nr_cpages;
581 int i, ret;
582
583 trace_f2fs_compress_pages_start(cc->inode, cc->cluster_idx,
584 cc->cluster_size, fi->i_compress_algorithm);
585
Chao Yu23b1faaad2020-03-03 16:57:07 +0800586 if (cops->init_compress_ctx) {
587 ret = cops->init_compress_ctx(cc);
588 if (ret)
589 goto out;
590 }
Chao Yu4c8ff702019-11-01 18:07:14 +0800591
592 max_len = COMPRESS_HEADER_SIZE + cc->clen;
593 cc->nr_cpages = DIV_ROUND_UP(max_len, PAGE_SIZE);
594
595 cc->cpages = f2fs_kzalloc(sbi, sizeof(struct page *) *
596 cc->nr_cpages, GFP_NOFS);
597 if (!cc->cpages) {
598 ret = -ENOMEM;
599 goto destroy_compress_ctx;
600 }
601
602 for (i = 0; i < cc->nr_cpages; i++) {
Chao Yu5e6bbde2020-04-08 19:56:05 +0800603 cc->cpages[i] = f2fs_compress_alloc_page();
Chao Yu4c8ff702019-11-01 18:07:14 +0800604 if (!cc->cpages[i]) {
605 ret = -ENOMEM;
606 goto out_free_cpages;
607 }
608 }
609
Daeho Jeong6fcaeba2020-08-12 14:17:11 +0900610 cc->rbuf = f2fs_vmap(cc->rpages, cc->cluster_size);
Chao Yu4c8ff702019-11-01 18:07:14 +0800611 if (!cc->rbuf) {
612 ret = -ENOMEM;
613 goto out_free_cpages;
614 }
615
Daeho Jeong6fcaeba2020-08-12 14:17:11 +0900616 cc->cbuf = f2fs_vmap(cc->cpages, cc->nr_cpages);
Chao Yu4c8ff702019-11-01 18:07:14 +0800617 if (!cc->cbuf) {
618 ret = -ENOMEM;
619 goto out_vunmap_rbuf;
620 }
621
622 ret = cops->compress_pages(cc);
623 if (ret)
624 goto out_vunmap_cbuf;
625
626 max_len = PAGE_SIZE * (cc->cluster_size - 1) - COMPRESS_HEADER_SIZE;
627
628 if (cc->clen > max_len) {
629 ret = -EAGAIN;
630 goto out_vunmap_cbuf;
631 }
632
633 cc->cbuf->clen = cpu_to_le32(cc->clen);
Chao Yu4c8ff702019-11-01 18:07:14 +0800634
635 for (i = 0; i < COMPRESS_DATA_RESERVED_SIZE; i++)
636 cc->cbuf->reserved[i] = cpu_to_le32(0);
637
Eric Biggers7fa6d592020-02-20 20:50:37 -0800638 nr_cpages = DIV_ROUND_UP(cc->clen + COMPRESS_HEADER_SIZE, PAGE_SIZE);
639
640 /* zero out any unused part of the last page */
641 memset(&cc->cbuf->cdata[cc->clen], 0,
642 (nr_cpages * PAGE_SIZE) - (cc->clen + COMPRESS_HEADER_SIZE));
643
Daeho Jeong6fcaeba2020-08-12 14:17:11 +0900644 vm_unmap_ram(cc->cbuf, cc->nr_cpages);
645 vm_unmap_ram(cc->rbuf, cc->cluster_size);
Chao Yu4c8ff702019-11-01 18:07:14 +0800646
Chao Yu4c8ff702019-11-01 18:07:14 +0800647 for (i = nr_cpages; i < cc->nr_cpages; i++) {
Chao Yu5e6bbde2020-04-08 19:56:05 +0800648 f2fs_compress_free_page(cc->cpages[i]);
Chao Yu4c8ff702019-11-01 18:07:14 +0800649 cc->cpages[i] = NULL;
650 }
651
Chao Yu23b1faaad2020-03-03 16:57:07 +0800652 if (cops->destroy_compress_ctx)
653 cops->destroy_compress_ctx(cc);
Chao Yu09ff4802020-03-03 16:57:06 +0800654
Chao Yu4c8ff702019-11-01 18:07:14 +0800655 cc->nr_cpages = nr_cpages;
656
657 trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
658 cc->clen, ret);
659 return 0;
660
661out_vunmap_cbuf:
Daeho Jeong6fcaeba2020-08-12 14:17:11 +0900662 vm_unmap_ram(cc->cbuf, cc->nr_cpages);
Chao Yu4c8ff702019-11-01 18:07:14 +0800663out_vunmap_rbuf:
Daeho Jeong6fcaeba2020-08-12 14:17:11 +0900664 vm_unmap_ram(cc->rbuf, cc->cluster_size);
Chao Yu4c8ff702019-11-01 18:07:14 +0800665out_free_cpages:
666 for (i = 0; i < cc->nr_cpages; i++) {
667 if (cc->cpages[i])
Chao Yu5e6bbde2020-04-08 19:56:05 +0800668 f2fs_compress_free_page(cc->cpages[i]);
Chao Yu4c8ff702019-11-01 18:07:14 +0800669 }
670 kfree(cc->cpages);
671 cc->cpages = NULL;
672destroy_compress_ctx:
Chao Yu23b1faaad2020-03-03 16:57:07 +0800673 if (cops->destroy_compress_ctx)
674 cops->destroy_compress_ctx(cc);
Chao Yu4c8ff702019-11-01 18:07:14 +0800675out:
676 trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
677 cc->clen, ret);
678 return ret;
679}
680
681void f2fs_decompress_pages(struct bio *bio, struct page *page, bool verity)
682{
683 struct decompress_io_ctx *dic =
684 (struct decompress_io_ctx *)page_private(page);
685 struct f2fs_sb_info *sbi = F2FS_I_SB(dic->inode);
686 struct f2fs_inode_info *fi= F2FS_I(dic->inode);
687 const struct f2fs_compress_ops *cops =
688 f2fs_cops[fi->i_compress_algorithm];
689 int ret;
Chao Yub2f57a82020-07-25 09:17:48 +0800690 int i;
Chao Yu4c8ff702019-11-01 18:07:14 +0800691
692 dec_page_count(sbi, F2FS_RD_DATA);
693
694 if (bio->bi_status || PageError(page))
695 dic->failed = true;
696
Chao Yue6c39482020-08-10 18:39:30 +0800697 if (atomic_dec_return(&dic->pending_pages))
Chao Yu4c8ff702019-11-01 18:07:14 +0800698 return;
699
700 trace_f2fs_decompress_pages_start(dic->inode, dic->cluster_idx,
701 dic->cluster_size, fi->i_compress_algorithm);
702
703 /* submit partial compressed pages */
704 if (dic->failed) {
705 ret = -EIO;
706 goto out_free_dic;
707 }
708
Chao Yub2f57a82020-07-25 09:17:48 +0800709 dic->tpages = f2fs_kzalloc(sbi, sizeof(struct page *) *
710 dic->cluster_size, GFP_NOFS);
711 if (!dic->tpages) {
712 ret = -ENOMEM;
713 goto out_free_dic;
714 }
715
716 for (i = 0; i < dic->cluster_size; i++) {
717 if (dic->rpages[i]) {
718 dic->tpages[i] = dic->rpages[i];
719 continue;
720 }
721
722 dic->tpages[i] = f2fs_compress_alloc_page();
723 if (!dic->tpages[i]) {
724 ret = -ENOMEM;
725 goto out_free_dic;
726 }
727 }
728
Chao Yu23b1faaad2020-03-03 16:57:07 +0800729 if (cops->init_decompress_ctx) {
730 ret = cops->init_decompress_ctx(dic);
731 if (ret)
732 goto out_free_dic;
733 }
734
Daeho Jeong6fcaeba2020-08-12 14:17:11 +0900735 dic->rbuf = f2fs_vmap(dic->tpages, dic->cluster_size);
Chao Yu4c8ff702019-11-01 18:07:14 +0800736 if (!dic->rbuf) {
737 ret = -ENOMEM;
Chao Yu23b1faaad2020-03-03 16:57:07 +0800738 goto destroy_decompress_ctx;
Chao Yu4c8ff702019-11-01 18:07:14 +0800739 }
740
Daeho Jeong6fcaeba2020-08-12 14:17:11 +0900741 dic->cbuf = f2fs_vmap(dic->cpages, dic->nr_cpages);
Chao Yu4c8ff702019-11-01 18:07:14 +0800742 if (!dic->cbuf) {
743 ret = -ENOMEM;
744 goto out_vunmap_rbuf;
745 }
746
747 dic->clen = le32_to_cpu(dic->cbuf->clen);
748 dic->rlen = PAGE_SIZE << dic->log_cluster_size;
749
750 if (dic->clen > PAGE_SIZE * dic->nr_cpages - COMPRESS_HEADER_SIZE) {
751 ret = -EFSCORRUPTED;
752 goto out_vunmap_cbuf;
753 }
754
755 ret = cops->decompress_pages(dic);
756
757out_vunmap_cbuf:
Daeho Jeong6fcaeba2020-08-12 14:17:11 +0900758 vm_unmap_ram(dic->cbuf, dic->nr_cpages);
Chao Yu4c8ff702019-11-01 18:07:14 +0800759out_vunmap_rbuf:
Daeho Jeong6fcaeba2020-08-12 14:17:11 +0900760 vm_unmap_ram(dic->rbuf, dic->cluster_size);
Chao Yu23b1faaad2020-03-03 16:57:07 +0800761destroy_decompress_ctx:
762 if (cops->destroy_decompress_ctx)
763 cops->destroy_decompress_ctx(dic);
Chao Yu4c8ff702019-11-01 18:07:14 +0800764out_free_dic:
Chao Yu79bbefb2020-03-23 17:43:04 +0800765 if (verity)
Chao Yue6c39482020-08-10 18:39:30 +0800766 atomic_set(&dic->pending_pages, dic->nr_cpages);
Chao Yu4c8ff702019-11-01 18:07:14 +0800767 if (!verity)
768 f2fs_decompress_end_io(dic->rpages, dic->cluster_size,
769 ret, false);
770
771 trace_f2fs_decompress_pages_end(dic->inode, dic->cluster_idx,
772 dic->clen, ret);
773 if (!verity)
774 f2fs_free_dic(dic);
775}
776
777static bool is_page_in_cluster(struct compress_ctx *cc, pgoff_t index)
778{
779 if (cc->cluster_idx == NULL_CLUSTER)
780 return true;
781 return cc->cluster_idx == cluster_idx(cc, index);
782}
783
784bool f2fs_cluster_is_empty(struct compress_ctx *cc)
785{
786 return cc->nr_rpages == 0;
787}
788
789static bool f2fs_cluster_is_full(struct compress_ctx *cc)
790{
791 return cc->cluster_size == cc->nr_rpages;
792}
793
794bool f2fs_cluster_can_merge_page(struct compress_ctx *cc, pgoff_t index)
795{
796 if (f2fs_cluster_is_empty(cc))
797 return true;
798 return is_page_in_cluster(cc, index);
799}
800
801static bool __cluster_may_compress(struct compress_ctx *cc)
802{
803 struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
804 loff_t i_size = i_size_read(cc->inode);
805 unsigned nr_pages = DIV_ROUND_UP(i_size, PAGE_SIZE);
806 int i;
807
808 for (i = 0; i < cc->cluster_size; i++) {
809 struct page *page = cc->rpages[i];
810
811 f2fs_bug_on(sbi, !page);
812
813 if (unlikely(f2fs_cp_error(sbi)))
814 return false;
815 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
816 return false;
817
818 /* beyond EOF */
819 if (page->index >= nr_pages)
820 return false;
821 }
822 return true;
823}
824
Chao Yu1a67cbe2020-03-12 10:45:29 +0800825static int __f2fs_cluster_blocks(struct compress_ctx *cc, bool compr)
Chao Yu4c8ff702019-11-01 18:07:14 +0800826{
827 struct dnode_of_data dn;
828 int ret;
829
830 set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
831 ret = f2fs_get_dnode_of_data(&dn, start_idx_of_cluster(cc),
832 LOOKUP_NODE);
833 if (ret) {
834 if (ret == -ENOENT)
835 ret = 0;
836 goto fail;
837 }
838
839 if (dn.data_blkaddr == COMPRESS_ADDR) {
840 int i;
841
842 ret = 1;
843 for (i = 1; i < cc->cluster_size; i++) {
844 block_t blkaddr;
845
Chao Yua2ced1c2020-02-14 17:44:10 +0800846 blkaddr = data_blkaddr(dn.inode,
Chao Yu4c8ff702019-11-01 18:07:14 +0800847 dn.node_page, dn.ofs_in_node + i);
Chao Yu1a67cbe2020-03-12 10:45:29 +0800848 if (compr) {
849 if (__is_valid_data_blkaddr(blkaddr))
850 ret++;
851 } else {
852 if (blkaddr != NULL_ADDR)
853 ret++;
854 }
Chao Yu4c8ff702019-11-01 18:07:14 +0800855 }
856 }
857fail:
858 f2fs_put_dnode(&dn);
859 return ret;
860}
861
Chao Yu1a67cbe2020-03-12 10:45:29 +0800862/* return # of compressed blocks in compressed cluster */
863static int f2fs_compressed_blocks(struct compress_ctx *cc)
864{
865 return __f2fs_cluster_blocks(cc, true);
866}
867
868/* return # of valid blocks in compressed cluster */
Wang Xiaojund0783192020-06-16 10:39:24 +0800869static int f2fs_cluster_blocks(struct compress_ctx *cc)
Chao Yu1a67cbe2020-03-12 10:45:29 +0800870{
871 return __f2fs_cluster_blocks(cc, false);
872}
873
Chao Yu4c8ff702019-11-01 18:07:14 +0800874int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index)
875{
876 struct compress_ctx cc = {
877 .inode = inode,
878 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
879 .cluster_size = F2FS_I(inode)->i_cluster_size,
880 .cluster_idx = index >> F2FS_I(inode)->i_log_cluster_size,
881 };
882
Wang Xiaojund0783192020-06-16 10:39:24 +0800883 return f2fs_cluster_blocks(&cc);
Chao Yu4c8ff702019-11-01 18:07:14 +0800884}
885
886static bool cluster_may_compress(struct compress_ctx *cc)
887{
888 if (!f2fs_compressed_file(cc->inode))
889 return false;
890 if (f2fs_is_atomic_file(cc->inode))
891 return false;
892 if (f2fs_is_mmap_file(cc->inode))
893 return false;
894 if (!f2fs_cluster_is_full(cc))
895 return false;
Chao Yudc35d732020-05-26 09:55:02 +0800896 if (unlikely(f2fs_cp_error(F2FS_I_SB(cc->inode))))
897 return false;
Chao Yu4c8ff702019-11-01 18:07:14 +0800898 return __cluster_may_compress(cc);
899}
900
901static void set_cluster_writeback(struct compress_ctx *cc)
902{
903 int i;
904
905 for (i = 0; i < cc->cluster_size; i++) {
906 if (cc->rpages[i])
907 set_page_writeback(cc->rpages[i]);
908 }
909}
910
911static void set_cluster_dirty(struct compress_ctx *cc)
912{
913 int i;
914
915 for (i = 0; i < cc->cluster_size; i++)
916 if (cc->rpages[i])
917 set_page_dirty(cc->rpages[i]);
918}
919
920static int prepare_compress_overwrite(struct compress_ctx *cc,
921 struct page **pagep, pgoff_t index, void **fsdata)
922{
923 struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
924 struct address_space *mapping = cc->inode->i_mapping;
925 struct page *page;
926 struct dnode_of_data dn;
927 sector_t last_block_in_bio;
928 unsigned fgp_flag = FGP_LOCK | FGP_WRITE | FGP_CREAT;
929 pgoff_t start_idx = start_idx_of_cluster(cc);
930 int i, ret;
931 bool prealloc;
932
933retry:
Wang Xiaojund0783192020-06-16 10:39:24 +0800934 ret = f2fs_cluster_blocks(cc);
Chao Yu4c8ff702019-11-01 18:07:14 +0800935 if (ret <= 0)
936 return ret;
937
938 /* compressed case */
939 prealloc = (ret < cc->cluster_size);
940
941 ret = f2fs_init_compress_ctx(cc);
942 if (ret)
943 return ret;
944
945 /* keep page reference to avoid page reclaim */
946 for (i = 0; i < cc->cluster_size; i++) {
947 page = f2fs_pagecache_get_page(mapping, start_idx + i,
948 fgp_flag, GFP_NOFS);
949 if (!page) {
950 ret = -ENOMEM;
951 goto unlock_pages;
952 }
953
954 if (PageUptodate(page))
955 unlock_page(page);
956 else
957 f2fs_compress_ctx_add_page(cc, page);
958 }
959
960 if (!f2fs_cluster_is_empty(cc)) {
961 struct bio *bio = NULL;
962
963 ret = f2fs_read_multi_pages(cc, &bio, cc->cluster_size,
Chao Yu06837282020-02-18 18:21:35 +0800964 &last_block_in_bio, false, true);
Chao Yu4c8ff702019-11-01 18:07:14 +0800965 f2fs_destroy_compress_ctx(cc);
966 if (ret)
967 goto release_pages;
968 if (bio)
969 f2fs_submit_bio(sbi, bio, DATA);
970
971 ret = f2fs_init_compress_ctx(cc);
972 if (ret)
973 goto release_pages;
974 }
975
976 for (i = 0; i < cc->cluster_size; i++) {
977 f2fs_bug_on(sbi, cc->rpages[i]);
978
979 page = find_lock_page(mapping, start_idx + i);
980 f2fs_bug_on(sbi, !page);
981
982 f2fs_wait_on_page_writeback(page, DATA, true, true);
983
984 f2fs_compress_ctx_add_page(cc, page);
985 f2fs_put_page(page, 0);
986
987 if (!PageUptodate(page)) {
988 f2fs_unlock_rpages(cc, i + 1);
Chao Yubc67c5d2020-06-08 20:03:17 +0800989 f2fs_put_rpages_mapping(mapping, start_idx,
Chao Yu4c8ff702019-11-01 18:07:14 +0800990 cc->cluster_size);
991 f2fs_destroy_compress_ctx(cc);
992 goto retry;
993 }
994 }
995
996 if (prealloc) {
Chao Yu0ef81832020-06-18 14:36:22 +0800997 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
Chao Yu4c8ff702019-11-01 18:07:14 +0800998
999 set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
1000
1001 for (i = cc->cluster_size - 1; i > 0; i--) {
1002 ret = f2fs_get_block(&dn, start_idx + i);
1003 if (ret) {
1004 i = cc->cluster_size;
1005 break;
1006 }
1007
1008 if (dn.data_blkaddr != NEW_ADDR)
1009 break;
1010 }
1011
Chao Yu0ef81832020-06-18 14:36:22 +08001012 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
Chao Yu4c8ff702019-11-01 18:07:14 +08001013 }
1014
1015 if (likely(!ret)) {
1016 *fsdata = cc->rpages;
1017 *pagep = cc->rpages[offset_in_cluster(cc, index)];
1018 return cc->cluster_size;
1019 }
1020
1021unlock_pages:
1022 f2fs_unlock_rpages(cc, i);
1023release_pages:
Chao Yubc67c5d2020-06-08 20:03:17 +08001024 f2fs_put_rpages_mapping(mapping, start_idx, i);
Chao Yu4c8ff702019-11-01 18:07:14 +08001025 f2fs_destroy_compress_ctx(cc);
1026 return ret;
1027}
1028
1029int f2fs_prepare_compress_overwrite(struct inode *inode,
1030 struct page **pagep, pgoff_t index, void **fsdata)
1031{
1032 struct compress_ctx cc = {
1033 .inode = inode,
1034 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
1035 .cluster_size = F2FS_I(inode)->i_cluster_size,
1036 .cluster_idx = index >> F2FS_I(inode)->i_log_cluster_size,
1037 .rpages = NULL,
1038 .nr_rpages = 0,
1039 };
1040
1041 return prepare_compress_overwrite(&cc, pagep, index, fsdata);
1042}
1043
1044bool f2fs_compress_write_end(struct inode *inode, void *fsdata,
1045 pgoff_t index, unsigned copied)
1046
1047{
1048 struct compress_ctx cc = {
1049 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
1050 .cluster_size = F2FS_I(inode)->i_cluster_size,
1051 .rpages = fsdata,
1052 };
1053 bool first_index = (index == cc.rpages[0]->index);
1054
1055 if (copied)
1056 set_cluster_dirty(&cc);
1057
1058 f2fs_put_rpages_wbc(&cc, NULL, false, 1);
1059 f2fs_destroy_compress_ctx(&cc);
1060
1061 return first_index;
1062}
1063
Chao Yu3265d3d2020-03-18 16:22:59 +08001064int f2fs_truncate_partial_cluster(struct inode *inode, u64 from, bool lock)
1065{
1066 void *fsdata = NULL;
1067 struct page *pagep;
1068 int log_cluster_size = F2FS_I(inode)->i_log_cluster_size;
1069 pgoff_t start_idx = from >> (PAGE_SHIFT + log_cluster_size) <<
1070 log_cluster_size;
1071 int err;
1072
1073 err = f2fs_is_compressed_cluster(inode, start_idx);
1074 if (err < 0)
1075 return err;
1076
1077 /* truncate normal cluster */
1078 if (!err)
1079 return f2fs_do_truncate_blocks(inode, from, lock);
1080
1081 /* truncate compressed cluster */
1082 err = f2fs_prepare_compress_overwrite(inode, &pagep,
1083 start_idx, &fsdata);
1084
1085 /* should not be a normal cluster */
1086 f2fs_bug_on(F2FS_I_SB(inode), err == 0);
1087
1088 if (err <= 0)
1089 return err;
1090
1091 if (err > 0) {
1092 struct page **rpages = fsdata;
1093 int cluster_size = F2FS_I(inode)->i_cluster_size;
1094 int i;
1095
1096 for (i = cluster_size - 1; i >= 0; i--) {
1097 loff_t start = rpages[i]->index << PAGE_SHIFT;
1098
1099 if (from <= start) {
1100 zero_user_segment(rpages[i], 0, PAGE_SIZE);
1101 } else {
1102 zero_user_segment(rpages[i], from - start,
1103 PAGE_SIZE);
1104 break;
1105 }
1106 }
1107
1108 f2fs_compress_write_end(inode, fsdata, start_idx, true);
1109 }
1110 return 0;
1111}
1112
Chao Yu4c8ff702019-11-01 18:07:14 +08001113static int f2fs_write_compressed_pages(struct compress_ctx *cc,
1114 int *submitted,
1115 struct writeback_control *wbc,
1116 enum iostat_type io_type)
1117{
1118 struct inode *inode = cc->inode;
1119 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1120 struct f2fs_inode_info *fi = F2FS_I(inode);
1121 struct f2fs_io_info fio = {
1122 .sbi = sbi,
1123 .ino = cc->inode->i_ino,
1124 .type = DATA,
1125 .op = REQ_OP_WRITE,
1126 .op_flags = wbc_to_write_flags(wbc),
1127 .old_blkaddr = NEW_ADDR,
1128 .page = NULL,
1129 .encrypted_page = NULL,
1130 .compressed_page = NULL,
1131 .submitted = false,
Chao Yu4c8ff702019-11-01 18:07:14 +08001132 .io_type = io_type,
1133 .io_wbc = wbc,
Satya Tangirala27aacd22020-07-02 01:56:06 +00001134 .encrypted = fscrypt_inode_uses_fs_layer_crypto(cc->inode),
Chao Yu4c8ff702019-11-01 18:07:14 +08001135 };
1136 struct dnode_of_data dn;
1137 struct node_info ni;
1138 struct compress_io_ctx *cic;
1139 pgoff_t start_idx = start_idx_of_cluster(cc);
1140 unsigned int last_index = cc->cluster_size - 1;
1141 loff_t psize;
1142 int i, err;
1143
Chao Yu79963d92020-06-18 14:36:23 +08001144 if (IS_NOQUOTA(inode)) {
1145 /*
1146 * We need to wait for node_write to avoid block allocation during
1147 * checkpoint. This can only happen to quota writes which can cause
1148 * the below discard race condition.
1149 */
1150 down_read(&sbi->node_write);
1151 } else if (!f2fs_trylock_op(sbi)) {
Chao Yudf77fbd2020-02-24 19:20:16 +08001152 return -EAGAIN;
Chao Yu79963d92020-06-18 14:36:23 +08001153 }
Chao Yu4c8ff702019-11-01 18:07:14 +08001154
Chao Yudf77fbd2020-02-24 19:20:16 +08001155 set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
Chao Yu4c8ff702019-11-01 18:07:14 +08001156
1157 err = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
1158 if (err)
1159 goto out_unlock_op;
1160
1161 for (i = 0; i < cc->cluster_size; i++) {
Chao Yua2ced1c2020-02-14 17:44:10 +08001162 if (data_blkaddr(dn.inode, dn.node_page,
Chao Yu4c8ff702019-11-01 18:07:14 +08001163 dn.ofs_in_node + i) == NULL_ADDR)
1164 goto out_put_dnode;
1165 }
1166
1167 psize = (loff_t)(cc->rpages[last_index]->index + 1) << PAGE_SHIFT;
1168
1169 err = f2fs_get_node_info(fio.sbi, dn.nid, &ni);
1170 if (err)
1171 goto out_put_dnode;
1172
1173 fio.version = ni.version;
1174
1175 cic = f2fs_kzalloc(sbi, sizeof(struct compress_io_ctx), GFP_NOFS);
1176 if (!cic)
1177 goto out_put_dnode;
1178
1179 cic->magic = F2FS_COMPRESSED_PAGE_MAGIC;
1180 cic->inode = inode;
Chao Yue6c39482020-08-10 18:39:30 +08001181 atomic_set(&cic->pending_pages, cc->nr_cpages);
Chao Yu4c8ff702019-11-01 18:07:14 +08001182 cic->rpages = f2fs_kzalloc(sbi, sizeof(struct page *) <<
1183 cc->log_cluster_size, GFP_NOFS);
1184 if (!cic->rpages)
1185 goto out_put_cic;
1186
1187 cic->nr_rpages = cc->cluster_size;
1188
1189 for (i = 0; i < cc->nr_cpages; i++) {
1190 f2fs_set_compressed_page(cc->cpages[i], inode,
Chao Yu887347a2020-03-28 17:33:23 +08001191 cc->rpages[i + 1]->index, cic);
Chao Yu4c8ff702019-11-01 18:07:14 +08001192 fio.compressed_page = cc->cpages[i];
Chao Yuf567adb2020-07-03 16:40:11 +08001193
1194 fio.old_blkaddr = data_blkaddr(dn.inode, dn.node_page,
1195 dn.ofs_in_node + i + 1);
1196
1197 /* wait for GCed page writeback via META_MAPPING */
1198 f2fs_wait_on_block_writeback(inode, fio.old_blkaddr);
1199
Chao Yu4c8ff702019-11-01 18:07:14 +08001200 if (fio.encrypted) {
1201 fio.page = cc->rpages[i + 1];
1202 err = f2fs_encrypt_one_page(&fio);
1203 if (err)
1204 goto out_destroy_crypt;
1205 cc->cpages[i] = fio.encrypted_page;
1206 }
1207 }
1208
1209 set_cluster_writeback(cc);
1210
1211 for (i = 0; i < cc->cluster_size; i++)
1212 cic->rpages[i] = cc->rpages[i];
1213
1214 for (i = 0; i < cc->cluster_size; i++, dn.ofs_in_node++) {
1215 block_t blkaddr;
1216
Chao Yua2ced1c2020-02-14 17:44:10 +08001217 blkaddr = f2fs_data_blkaddr(&dn);
Chao Yu95978ca2020-02-28 18:08:46 +08001218 fio.page = cc->rpages[i];
Chao Yu4c8ff702019-11-01 18:07:14 +08001219 fio.old_blkaddr = blkaddr;
1220
1221 /* cluster header */
1222 if (i == 0) {
1223 if (blkaddr == COMPRESS_ADDR)
1224 fio.compr_blocks++;
1225 if (__is_valid_data_blkaddr(blkaddr))
1226 f2fs_invalidate_blocks(sbi, blkaddr);
1227 f2fs_update_data_blkaddr(&dn, COMPRESS_ADDR);
1228 goto unlock_continue;
1229 }
1230
1231 if (fio.compr_blocks && __is_valid_data_blkaddr(blkaddr))
1232 fio.compr_blocks++;
1233
1234 if (i > cc->nr_cpages) {
1235 if (__is_valid_data_blkaddr(blkaddr)) {
1236 f2fs_invalidate_blocks(sbi, blkaddr);
1237 f2fs_update_data_blkaddr(&dn, NEW_ADDR);
1238 }
1239 goto unlock_continue;
1240 }
1241
1242 f2fs_bug_on(fio.sbi, blkaddr == NULL_ADDR);
1243
1244 if (fio.encrypted)
1245 fio.encrypted_page = cc->cpages[i - 1];
1246 else
1247 fio.compressed_page = cc->cpages[i - 1];
1248
1249 cc->cpages[i - 1] = NULL;
1250 f2fs_outplace_write_data(&dn, &fio);
1251 (*submitted)++;
1252unlock_continue:
1253 inode_dec_dirty_pages(cc->inode);
1254 unlock_page(fio.page);
1255 }
1256
1257 if (fio.compr_blocks)
1258 f2fs_i_compr_blocks_update(inode, fio.compr_blocks - 1, false);
1259 f2fs_i_compr_blocks_update(inode, cc->nr_cpages, true);
1260
1261 set_inode_flag(cc->inode, FI_APPEND_WRITE);
1262 if (cc->cluster_idx == 0)
1263 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
1264
1265 f2fs_put_dnode(&dn);
Chao Yu79963d92020-06-18 14:36:23 +08001266 if (IS_NOQUOTA(inode))
1267 up_read(&sbi->node_write);
1268 else
Jaegeuk Kim435cbab2020-04-09 10:25:21 -07001269 f2fs_unlock_op(sbi);
Chao Yu4c8ff702019-11-01 18:07:14 +08001270
Chao Yuc10c9822020-02-27 19:30:03 +08001271 spin_lock(&fi->i_size_lock);
Chao Yu4c8ff702019-11-01 18:07:14 +08001272 if (fi->last_disk_size < psize)
1273 fi->last_disk_size = psize;
Chao Yuc10c9822020-02-27 19:30:03 +08001274 spin_unlock(&fi->i_size_lock);
Chao Yu4c8ff702019-11-01 18:07:14 +08001275
1276 f2fs_put_rpages(cc);
1277 f2fs_destroy_compress_ctx(cc);
1278 return 0;
1279
1280out_destroy_crypt:
1281 kfree(cic->rpages);
1282
1283 for (--i; i >= 0; i--)
1284 fscrypt_finalize_bounce_page(&cc->cpages[i]);
1285 for (i = 0; i < cc->nr_cpages; i++) {
1286 if (!cc->cpages[i])
1287 continue;
1288 f2fs_put_page(cc->cpages[i], 1);
1289 }
1290out_put_cic:
1291 kfree(cic);
1292out_put_dnode:
1293 f2fs_put_dnode(&dn);
1294out_unlock_op:
Chao Yu79963d92020-06-18 14:36:23 +08001295 if (IS_NOQUOTA(inode))
1296 up_read(&sbi->node_write);
1297 else
Jaegeuk Kim435cbab2020-04-09 10:25:21 -07001298 f2fs_unlock_op(sbi);
Chao Yu4c8ff702019-11-01 18:07:14 +08001299 return -EAGAIN;
1300}
1301
1302void f2fs_compress_write_end_io(struct bio *bio, struct page *page)
1303{
1304 struct f2fs_sb_info *sbi = bio->bi_private;
1305 struct compress_io_ctx *cic =
1306 (struct compress_io_ctx *)page_private(page);
1307 int i;
1308
1309 if (unlikely(bio->bi_status))
1310 mapping_set_error(cic->inode->i_mapping, -EIO);
1311
Chao Yu5e6bbde2020-04-08 19:56:05 +08001312 f2fs_compress_free_page(page);
Chao Yu4c8ff702019-11-01 18:07:14 +08001313
1314 dec_page_count(sbi, F2FS_WB_DATA);
1315
Chao Yue6c39482020-08-10 18:39:30 +08001316 if (atomic_dec_return(&cic->pending_pages))
Chao Yu4c8ff702019-11-01 18:07:14 +08001317 return;
1318
1319 for (i = 0; i < cic->nr_rpages; i++) {
1320 WARN_ON(!cic->rpages[i]);
1321 clear_cold_data(cic->rpages[i]);
1322 end_page_writeback(cic->rpages[i]);
1323 }
1324
1325 kfree(cic->rpages);
1326 kfree(cic);
1327}
1328
1329static int f2fs_write_raw_pages(struct compress_ctx *cc,
1330 int *submitted,
1331 struct writeback_control *wbc,
1332 enum iostat_type io_type)
1333{
1334 struct address_space *mapping = cc->inode->i_mapping;
1335 int _submitted, compr_blocks, ret;
1336 int i = -1, err = 0;
1337
1338 compr_blocks = f2fs_compressed_blocks(cc);
1339 if (compr_blocks < 0) {
1340 err = compr_blocks;
1341 goto out_err;
1342 }
1343
1344 for (i = 0; i < cc->cluster_size; i++) {
1345 if (!cc->rpages[i])
1346 continue;
1347retry_write:
1348 if (cc->rpages[i]->mapping != mapping) {
1349 unlock_page(cc->rpages[i]);
1350 continue;
1351 }
1352
1353 BUG_ON(!PageLocked(cc->rpages[i]));
1354
1355 ret = f2fs_write_single_data_page(cc->rpages[i], &_submitted,
1356 NULL, NULL, wbc, io_type,
1357 compr_blocks);
1358 if (ret) {
1359 if (ret == AOP_WRITEPAGE_ACTIVATE) {
1360 unlock_page(cc->rpages[i]);
1361 ret = 0;
1362 } else if (ret == -EAGAIN) {
Chao Yu466357d2020-03-20 18:14:31 +08001363 /*
1364 * for quota file, just redirty left pages to
1365 * avoid deadlock caused by cluster update race
1366 * from foreground operation.
1367 */
1368 if (IS_NOQUOTA(cc->inode)) {
1369 err = 0;
1370 goto out_err;
1371 }
Chao Yu4c8ff702019-11-01 18:07:14 +08001372 ret = 0;
1373 cond_resched();
Chao Yu5df7731f2020-02-17 17:45:44 +08001374 congestion_wait(BLK_RW_ASYNC,
1375 DEFAULT_IO_TIMEOUT);
Chao Yu4c8ff702019-11-01 18:07:14 +08001376 lock_page(cc->rpages[i]);
Chao Yueb1353c2020-06-19 17:14:19 +08001377
1378 if (!PageDirty(cc->rpages[i])) {
1379 unlock_page(cc->rpages[i]);
1380 continue;
1381 }
1382
Chao Yu4c8ff702019-11-01 18:07:14 +08001383 clear_page_dirty_for_io(cc->rpages[i]);
1384 goto retry_write;
1385 }
1386 err = ret;
Chao Yu466357d2020-03-20 18:14:31 +08001387 goto out_err;
Chao Yu4c8ff702019-11-01 18:07:14 +08001388 }
1389
1390 *submitted += _submitted;
1391 }
1392 return 0;
Chao Yu4c8ff702019-11-01 18:07:14 +08001393out_err:
1394 for (++i; i < cc->cluster_size; i++) {
1395 if (!cc->rpages[i])
1396 continue;
1397 redirty_page_for_writepage(wbc, cc->rpages[i]);
1398 unlock_page(cc->rpages[i]);
1399 }
1400 return err;
1401}
1402
1403int f2fs_write_multi_pages(struct compress_ctx *cc,
1404 int *submitted,
1405 struct writeback_control *wbc,
1406 enum iostat_type io_type)
1407{
Chao Yu4c8ff702019-11-01 18:07:14 +08001408 int err;
1409
1410 *submitted = 0;
1411 if (cluster_may_compress(cc)) {
1412 err = f2fs_compress_pages(cc);
1413 if (err == -EAGAIN) {
1414 goto write;
1415 } else if (err) {
1416 f2fs_put_rpages_wbc(cc, wbc, true, 1);
1417 goto destroy_out;
1418 }
1419
1420 err = f2fs_write_compressed_pages(cc, submitted,
1421 wbc, io_type);
Chao Yu02772fbf2020-07-20 16:52:50 +08001422 kfree(cc->cpages);
1423 cc->cpages = NULL;
Chao Yu4c8ff702019-11-01 18:07:14 +08001424 if (!err)
1425 return 0;
1426 f2fs_bug_on(F2FS_I_SB(cc->inode), err != -EAGAIN);
1427 }
1428write:
1429 f2fs_bug_on(F2FS_I_SB(cc->inode), *submitted);
1430
1431 err = f2fs_write_raw_pages(cc, submitted, wbc, io_type);
1432 f2fs_put_rpages_wbc(cc, wbc, false, 0);
1433destroy_out:
1434 f2fs_destroy_compress_ctx(cc);
1435 return err;
1436}
1437
1438struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc)
1439{
1440 struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
1441 struct decompress_io_ctx *dic;
1442 pgoff_t start_idx = start_idx_of_cluster(cc);
1443 int i;
1444
1445 dic = f2fs_kzalloc(sbi, sizeof(struct decompress_io_ctx), GFP_NOFS);
1446 if (!dic)
1447 return ERR_PTR(-ENOMEM);
1448
1449 dic->rpages = f2fs_kzalloc(sbi, sizeof(struct page *) <<
1450 cc->log_cluster_size, GFP_NOFS);
1451 if (!dic->rpages) {
1452 kfree(dic);
1453 return ERR_PTR(-ENOMEM);
1454 }
1455
1456 dic->magic = F2FS_COMPRESSED_PAGE_MAGIC;
1457 dic->inode = cc->inode;
Chao Yue6c39482020-08-10 18:39:30 +08001458 atomic_set(&dic->pending_pages, cc->nr_cpages);
Chao Yu4c8ff702019-11-01 18:07:14 +08001459 dic->cluster_idx = cc->cluster_idx;
1460 dic->cluster_size = cc->cluster_size;
1461 dic->log_cluster_size = cc->log_cluster_size;
1462 dic->nr_cpages = cc->nr_cpages;
1463 dic->failed = false;
1464
1465 for (i = 0; i < dic->cluster_size; i++)
1466 dic->rpages[i] = cc->rpages[i];
1467 dic->nr_rpages = cc->cluster_size;
1468
1469 dic->cpages = f2fs_kzalloc(sbi, sizeof(struct page *) *
1470 dic->nr_cpages, GFP_NOFS);
1471 if (!dic->cpages)
1472 goto out_free;
1473
1474 for (i = 0; i < dic->nr_cpages; i++) {
1475 struct page *page;
1476
Chao Yu5e6bbde2020-04-08 19:56:05 +08001477 page = f2fs_compress_alloc_page();
Chao Yu4c8ff702019-11-01 18:07:14 +08001478 if (!page)
1479 goto out_free;
1480
1481 f2fs_set_compressed_page(page, cc->inode,
Chao Yu887347a2020-03-28 17:33:23 +08001482 start_idx + i + 1, dic);
Chao Yu4c8ff702019-11-01 18:07:14 +08001483 dic->cpages[i] = page;
1484 }
1485
Chao Yu4c8ff702019-11-01 18:07:14 +08001486 return dic;
1487
1488out_free:
1489 f2fs_free_dic(dic);
1490 return ERR_PTR(-ENOMEM);
1491}
1492
1493void f2fs_free_dic(struct decompress_io_ctx *dic)
1494{
1495 int i;
1496
1497 if (dic->tpages) {
1498 for (i = 0; i < dic->cluster_size; i++) {
1499 if (dic->rpages[i])
1500 continue;
Chao Yu8908e752020-03-26 17:42:26 +08001501 if (!dic->tpages[i])
1502 continue;
Chao Yu5e6bbde2020-04-08 19:56:05 +08001503 f2fs_compress_free_page(dic->tpages[i]);
Chao Yu4c8ff702019-11-01 18:07:14 +08001504 }
1505 kfree(dic->tpages);
1506 }
1507
1508 if (dic->cpages) {
1509 for (i = 0; i < dic->nr_cpages; i++) {
1510 if (!dic->cpages[i])
1511 continue;
Chao Yu5e6bbde2020-04-08 19:56:05 +08001512 f2fs_compress_free_page(dic->cpages[i]);
Chao Yu4c8ff702019-11-01 18:07:14 +08001513 }
1514 kfree(dic->cpages);
1515 }
1516
1517 kfree(dic->rpages);
1518 kfree(dic);
1519}
1520
1521void f2fs_decompress_end_io(struct page **rpages,
1522 unsigned int cluster_size, bool err, bool verity)
1523{
1524 int i;
1525
1526 for (i = 0; i < cluster_size; i++) {
1527 struct page *rpage = rpages[i];
1528
1529 if (!rpage)
1530 continue;
1531
Chao Yu23c51be2020-03-21 20:24:11 +08001532 if (err || PageError(rpage))
1533 goto clear_uptodate;
1534
1535 if (!verity || fsverity_verify_page(rpage)) {
1536 SetPageUptodate(rpage);
1537 goto unlock;
Chao Yu4c8ff702019-11-01 18:07:14 +08001538 }
Chao Yu23c51be2020-03-21 20:24:11 +08001539clear_uptodate:
1540 ClearPageUptodate(rpage);
1541 ClearPageError(rpage);
1542unlock:
Chao Yu4c8ff702019-11-01 18:07:14 +08001543 unlock_page(rpage);
1544 }
1545}