blob: 14262e0f1cd60e03b2ce2de7fbaea64baf5d3133 [file] [log] [blame]
Chao Yu4c8ff702019-11-01 18:07:14 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * f2fs compress support
4 *
5 * Copyright (c) 2019 Chao Yu <chao@kernel.org>
6 */
7
8#include <linux/fs.h>
9#include <linux/f2fs_fs.h>
10#include <linux/writeback.h>
11#include <linux/backing-dev.h>
12#include <linux/lzo.h>
13#include <linux/lz4.h>
Chao Yu50cfa662020-03-03 17:46:02 +080014#include <linux/zstd.h>
Chao Yu4c8ff702019-11-01 18:07:14 +080015
16#include "f2fs.h"
17#include "node.h"
18#include <trace/events/f2fs.h>
19
Chao Yuc68d6c82020-09-14 17:05:14 +080020static struct kmem_cache *cic_entry_slab;
21static struct kmem_cache *dic_entry_slab;
22
Chao Yu31083032020-09-14 17:05:13 +080023static void *page_array_alloc(struct inode *inode, int nr)
24{
25 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
26 unsigned int size = sizeof(struct page *) * nr;
27
28 if (likely(size <= sbi->page_array_slab_size))
29 return kmem_cache_zalloc(sbi->page_array_slab, GFP_NOFS);
30 return f2fs_kzalloc(sbi, size, GFP_NOFS);
31}
32
33static void page_array_free(struct inode *inode, void *pages, int nr)
34{
35 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
36 unsigned int size = sizeof(struct page *) * nr;
37
38 if (!pages)
39 return;
40
41 if (likely(size <= sbi->page_array_slab_size))
42 kmem_cache_free(sbi->page_array_slab, pages);
43 else
44 kfree(pages);
45}
46
Chao Yu4c8ff702019-11-01 18:07:14 +080047struct f2fs_compress_ops {
48 int (*init_compress_ctx)(struct compress_ctx *cc);
49 void (*destroy_compress_ctx)(struct compress_ctx *cc);
50 int (*compress_pages)(struct compress_ctx *cc);
Chao Yu23b1faaad2020-03-03 16:57:07 +080051 int (*init_decompress_ctx)(struct decompress_io_ctx *dic);
52 void (*destroy_decompress_ctx)(struct decompress_io_ctx *dic);
Chao Yu4c8ff702019-11-01 18:07:14 +080053 int (*decompress_pages)(struct decompress_io_ctx *dic);
54};
55
56static unsigned int offset_in_cluster(struct compress_ctx *cc, pgoff_t index)
57{
58 return index & (cc->cluster_size - 1);
59}
60
61static pgoff_t cluster_idx(struct compress_ctx *cc, pgoff_t index)
62{
63 return index >> cc->log_cluster_size;
64}
65
66static pgoff_t start_idx_of_cluster(struct compress_ctx *cc)
67{
68 return cc->cluster_idx << cc->log_cluster_size;
69}
70
71bool f2fs_is_compressed_page(struct page *page)
72{
73 if (!PagePrivate(page))
74 return false;
75 if (!page_private(page))
76 return false;
77 if (IS_ATOMIC_WRITTEN_PAGE(page) || IS_DUMMY_WRITTEN_PAGE(page))
78 return false;
Yu Changchun29b993c2020-06-20 03:58:29 -040079 /*
80 * page->private may be set with pid.
81 * pid_max is enough to check if it is traced.
82 */
83 if (IS_IO_TRACED_PAGE(page))
84 return false;
85
Chao Yu4c8ff702019-11-01 18:07:14 +080086 f2fs_bug_on(F2FS_M_SB(page->mapping),
87 *((u32 *)page_private(page)) != F2FS_COMPRESSED_PAGE_MAGIC);
88 return true;
89}
90
91static void f2fs_set_compressed_page(struct page *page,
Chao Yu887347a2020-03-28 17:33:23 +080092 struct inode *inode, pgoff_t index, void *data)
Chao Yu4c8ff702019-11-01 18:07:14 +080093{
94 SetPagePrivate(page);
95 set_page_private(page, (unsigned long)data);
96
97 /* i_crypto_info and iv index */
98 page->index = index;
99 page->mapping = inode->i_mapping;
Chao Yu4c8ff702019-11-01 18:07:14 +0800100}
101
Chao Yu4c8ff702019-11-01 18:07:14 +0800102static void f2fs_drop_rpages(struct compress_ctx *cc, int len, bool unlock)
103{
104 int i;
105
106 for (i = 0; i < len; i++) {
107 if (!cc->rpages[i])
108 continue;
109 if (unlock)
110 unlock_page(cc->rpages[i]);
111 else
112 put_page(cc->rpages[i]);
113 }
114}
115
116static void f2fs_put_rpages(struct compress_ctx *cc)
117{
118 f2fs_drop_rpages(cc, cc->cluster_size, false);
119}
120
121static void f2fs_unlock_rpages(struct compress_ctx *cc, int len)
122{
123 f2fs_drop_rpages(cc, len, true);
124}
125
Chao Yubc67c5d2020-06-08 20:03:17 +0800126static void f2fs_put_rpages_mapping(struct address_space *mapping,
Chao Yu4c8ff702019-11-01 18:07:14 +0800127 pgoff_t start, int len)
128{
129 int i;
130
131 for (i = 0; i < len; i++) {
132 struct page *page = find_get_page(mapping, start + i);
133
134 put_page(page);
135 put_page(page);
136 }
137}
138
139static void f2fs_put_rpages_wbc(struct compress_ctx *cc,
140 struct writeback_control *wbc, bool redirty, int unlock)
141{
142 unsigned int i;
143
144 for (i = 0; i < cc->cluster_size; i++) {
145 if (!cc->rpages[i])
146 continue;
147 if (redirty)
148 redirty_page_for_writepage(wbc, cc->rpages[i]);
149 f2fs_put_page(cc->rpages[i], unlock);
150 }
151}
152
153struct page *f2fs_compress_control_page(struct page *page)
154{
155 return ((struct compress_io_ctx *)page_private(page))->rpages[0];
156}
157
158int f2fs_init_compress_ctx(struct compress_ctx *cc)
159{
Jaegeuk Kimadfc6942020-09-23 00:54:50 -0700160 if (cc->rpages)
Chao Yu4c8ff702019-11-01 18:07:14 +0800161 return 0;
162
Chao Yu31083032020-09-14 17:05:13 +0800163 cc->rpages = page_array_alloc(cc->inode, cc->cluster_size);
Chao Yu4c8ff702019-11-01 18:07:14 +0800164 return cc->rpages ? 0 : -ENOMEM;
165}
166
167void f2fs_destroy_compress_ctx(struct compress_ctx *cc)
168{
Chao Yu31083032020-09-14 17:05:13 +0800169 page_array_free(cc->inode, cc->rpages, cc->cluster_size);
Chao Yu4c8ff702019-11-01 18:07:14 +0800170 cc->rpages = NULL;
171 cc->nr_rpages = 0;
172 cc->nr_cpages = 0;
173 cc->cluster_idx = NULL_CLUSTER;
174}
175
176void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct page *page)
177{
178 unsigned int cluster_ofs;
179
180 if (!f2fs_cluster_can_merge_page(cc, page->index))
181 f2fs_bug_on(F2FS_I_SB(cc->inode), 1);
182
183 cluster_ofs = offset_in_cluster(cc, page->index);
184 cc->rpages[cluster_ofs] = page;
185 cc->nr_rpages++;
186 cc->cluster_idx = cluster_idx(cc, page->index);
187}
188
189#ifdef CONFIG_F2FS_FS_LZO
190static int lzo_init_compress_ctx(struct compress_ctx *cc)
191{
192 cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
193 LZO1X_MEM_COMPRESS, GFP_NOFS);
194 if (!cc->private)
195 return -ENOMEM;
196
197 cc->clen = lzo1x_worst_compress(PAGE_SIZE << cc->log_cluster_size);
198 return 0;
199}
200
201static void lzo_destroy_compress_ctx(struct compress_ctx *cc)
202{
203 kvfree(cc->private);
204 cc->private = NULL;
205}
206
207static int lzo_compress_pages(struct compress_ctx *cc)
208{
209 int ret;
210
211 ret = lzo1x_1_compress(cc->rbuf, cc->rlen, cc->cbuf->cdata,
212 &cc->clen, cc->private);
213 if (ret != LZO_E_OK) {
214 printk_ratelimited("%sF2FS-fs (%s): lzo compress failed, ret:%d\n",
215 KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id, ret);
216 return -EIO;
217 }
218 return 0;
219}
220
221static int lzo_decompress_pages(struct decompress_io_ctx *dic)
222{
223 int ret;
224
225 ret = lzo1x_decompress_safe(dic->cbuf->cdata, dic->clen,
226 dic->rbuf, &dic->rlen);
227 if (ret != LZO_E_OK) {
228 printk_ratelimited("%sF2FS-fs (%s): lzo decompress failed, ret:%d\n",
229 KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id, ret);
230 return -EIO;
231 }
232
233 if (dic->rlen != PAGE_SIZE << dic->log_cluster_size) {
234 printk_ratelimited("%sF2FS-fs (%s): lzo invalid rlen:%zu, "
235 "expected:%lu\n", KERN_ERR,
236 F2FS_I_SB(dic->inode)->sb->s_id,
237 dic->rlen,
238 PAGE_SIZE << dic->log_cluster_size);
239 return -EIO;
240 }
241 return 0;
242}
243
244static const struct f2fs_compress_ops f2fs_lzo_ops = {
245 .init_compress_ctx = lzo_init_compress_ctx,
246 .destroy_compress_ctx = lzo_destroy_compress_ctx,
247 .compress_pages = lzo_compress_pages,
248 .decompress_pages = lzo_decompress_pages,
249};
250#endif
251
252#ifdef CONFIG_F2FS_FS_LZ4
253static int lz4_init_compress_ctx(struct compress_ctx *cc)
254{
255 cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
256 LZ4_MEM_COMPRESS, GFP_NOFS);
257 if (!cc->private)
258 return -ENOMEM;
259
Chao Yuf6644142020-05-09 15:01:04 +0800260 /*
261 * we do not change cc->clen to LZ4_compressBound(inputsize) to
262 * adapt worst compress case, because lz4 compressor can handle
263 * output budget properly.
264 */
265 cc->clen = cc->rlen - PAGE_SIZE - COMPRESS_HEADER_SIZE;
Chao Yu4c8ff702019-11-01 18:07:14 +0800266 return 0;
267}
268
269static void lz4_destroy_compress_ctx(struct compress_ctx *cc)
270{
271 kvfree(cc->private);
272 cc->private = NULL;
273}
274
275static int lz4_compress_pages(struct compress_ctx *cc)
276{
277 int len;
278
279 len = LZ4_compress_default(cc->rbuf, cc->cbuf->cdata, cc->rlen,
280 cc->clen, cc->private);
Chao Yuf6644142020-05-09 15:01:04 +0800281 if (!len)
282 return -EAGAIN;
283
Chao Yu4c8ff702019-11-01 18:07:14 +0800284 cc->clen = len;
285 return 0;
286}
287
288static int lz4_decompress_pages(struct decompress_io_ctx *dic)
289{
290 int ret;
291
292 ret = LZ4_decompress_safe(dic->cbuf->cdata, dic->rbuf,
293 dic->clen, dic->rlen);
294 if (ret < 0) {
295 printk_ratelimited("%sF2FS-fs (%s): lz4 decompress failed, ret:%d\n",
296 KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id, ret);
297 return -EIO;
298 }
299
300 if (ret != PAGE_SIZE << dic->log_cluster_size) {
301 printk_ratelimited("%sF2FS-fs (%s): lz4 invalid rlen:%zu, "
302 "expected:%lu\n", KERN_ERR,
303 F2FS_I_SB(dic->inode)->sb->s_id,
304 dic->rlen,
305 PAGE_SIZE << dic->log_cluster_size);
306 return -EIO;
307 }
308 return 0;
309}
310
311static const struct f2fs_compress_ops f2fs_lz4_ops = {
312 .init_compress_ctx = lz4_init_compress_ctx,
313 .destroy_compress_ctx = lz4_destroy_compress_ctx,
314 .compress_pages = lz4_compress_pages,
315 .decompress_pages = lz4_decompress_pages,
316};
317#endif
318
Chao Yu50cfa662020-03-03 17:46:02 +0800319#ifdef CONFIG_F2FS_FS_ZSTD
320#define F2FS_ZSTD_DEFAULT_CLEVEL 1
321
322static int zstd_init_compress_ctx(struct compress_ctx *cc)
323{
324 ZSTD_parameters params;
325 ZSTD_CStream *stream;
326 void *workspace;
327 unsigned int workspace_size;
328
329 params = ZSTD_getParams(F2FS_ZSTD_DEFAULT_CLEVEL, cc->rlen, 0);
330 workspace_size = ZSTD_CStreamWorkspaceBound(params.cParams);
331
332 workspace = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
333 workspace_size, GFP_NOFS);
334 if (!workspace)
335 return -ENOMEM;
336
337 stream = ZSTD_initCStream(params, 0, workspace, workspace_size);
338 if (!stream) {
339 printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_initCStream failed\n",
340 KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
341 __func__);
342 kvfree(workspace);
343 return -EIO;
344 }
345
346 cc->private = workspace;
347 cc->private2 = stream;
348
349 cc->clen = cc->rlen - PAGE_SIZE - COMPRESS_HEADER_SIZE;
350 return 0;
351}
352
353static void zstd_destroy_compress_ctx(struct compress_ctx *cc)
354{
355 kvfree(cc->private);
356 cc->private = NULL;
357 cc->private2 = NULL;
358}
359
360static int zstd_compress_pages(struct compress_ctx *cc)
361{
362 ZSTD_CStream *stream = cc->private2;
363 ZSTD_inBuffer inbuf;
364 ZSTD_outBuffer outbuf;
365 int src_size = cc->rlen;
366 int dst_size = src_size - PAGE_SIZE - COMPRESS_HEADER_SIZE;
367 int ret;
368
369 inbuf.pos = 0;
370 inbuf.src = cc->rbuf;
371 inbuf.size = src_size;
372
373 outbuf.pos = 0;
374 outbuf.dst = cc->cbuf->cdata;
375 outbuf.size = dst_size;
376
377 ret = ZSTD_compressStream(stream, &outbuf, &inbuf);
378 if (ZSTD_isError(ret)) {
379 printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_compressStream failed, ret: %d\n",
380 KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
381 __func__, ZSTD_getErrorCode(ret));
382 return -EIO;
383 }
384
385 ret = ZSTD_endStream(stream, &outbuf);
386 if (ZSTD_isError(ret)) {
387 printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_endStream returned %d\n",
388 KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
389 __func__, ZSTD_getErrorCode(ret));
390 return -EIO;
391 }
392
Chao Yu1454c972020-05-08 09:16:03 +0800393 /*
394 * there is compressed data remained in intermediate buffer due to
395 * no more space in cbuf.cdata
396 */
397 if (ret)
398 return -EAGAIN;
399
Chao Yu50cfa662020-03-03 17:46:02 +0800400 cc->clen = outbuf.pos;
401 return 0;
402}
403
404static int zstd_init_decompress_ctx(struct decompress_io_ctx *dic)
405{
406 ZSTD_DStream *stream;
407 void *workspace;
408 unsigned int workspace_size;
Chao Yu0e2b7382020-09-02 15:01:52 +0800409 unsigned int max_window_size =
410 MAX_COMPRESS_WINDOW_SIZE(dic->log_cluster_size);
Chao Yu50cfa662020-03-03 17:46:02 +0800411
Chao Yu0e2b7382020-09-02 15:01:52 +0800412 workspace_size = ZSTD_DStreamWorkspaceBound(max_window_size);
Chao Yu50cfa662020-03-03 17:46:02 +0800413
414 workspace = f2fs_kvmalloc(F2FS_I_SB(dic->inode),
415 workspace_size, GFP_NOFS);
416 if (!workspace)
417 return -ENOMEM;
418
Chao Yu0e2b7382020-09-02 15:01:52 +0800419 stream = ZSTD_initDStream(max_window_size, workspace, workspace_size);
Chao Yu50cfa662020-03-03 17:46:02 +0800420 if (!stream) {
421 printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_initDStream failed\n",
422 KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id,
423 __func__);
424 kvfree(workspace);
425 return -EIO;
426 }
427
428 dic->private = workspace;
429 dic->private2 = stream;
430
431 return 0;
432}
433
434static void zstd_destroy_decompress_ctx(struct decompress_io_ctx *dic)
435{
436 kvfree(dic->private);
437 dic->private = NULL;
438 dic->private2 = NULL;
439}
440
441static int zstd_decompress_pages(struct decompress_io_ctx *dic)
442{
443 ZSTD_DStream *stream = dic->private2;
444 ZSTD_inBuffer inbuf;
445 ZSTD_outBuffer outbuf;
446 int ret;
447
448 inbuf.pos = 0;
449 inbuf.src = dic->cbuf->cdata;
450 inbuf.size = dic->clen;
451
452 outbuf.pos = 0;
453 outbuf.dst = dic->rbuf;
454 outbuf.size = dic->rlen;
455
456 ret = ZSTD_decompressStream(stream, &outbuf, &inbuf);
457 if (ZSTD_isError(ret)) {
458 printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_compressStream failed, ret: %d\n",
459 KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id,
460 __func__, ZSTD_getErrorCode(ret));
461 return -EIO;
462 }
463
464 if (dic->rlen != outbuf.pos) {
465 printk_ratelimited("%sF2FS-fs (%s): %s ZSTD invalid rlen:%zu, "
466 "expected:%lu\n", KERN_ERR,
467 F2FS_I_SB(dic->inode)->sb->s_id,
468 __func__, dic->rlen,
469 PAGE_SIZE << dic->log_cluster_size);
470 return -EIO;
471 }
472
473 return 0;
474}
475
476static const struct f2fs_compress_ops f2fs_zstd_ops = {
477 .init_compress_ctx = zstd_init_compress_ctx,
478 .destroy_compress_ctx = zstd_destroy_compress_ctx,
479 .compress_pages = zstd_compress_pages,
480 .init_decompress_ctx = zstd_init_decompress_ctx,
481 .destroy_decompress_ctx = zstd_destroy_decompress_ctx,
482 .decompress_pages = zstd_decompress_pages,
483};
484#endif
485
Chao Yu6d92b202020-04-08 19:56:32 +0800486#ifdef CONFIG_F2FS_FS_LZO
487#ifdef CONFIG_F2FS_FS_LZORLE
488static int lzorle_compress_pages(struct compress_ctx *cc)
489{
490 int ret;
491
492 ret = lzorle1x_1_compress(cc->rbuf, cc->rlen, cc->cbuf->cdata,
493 &cc->clen, cc->private);
494 if (ret != LZO_E_OK) {
495 printk_ratelimited("%sF2FS-fs (%s): lzo-rle compress failed, ret:%d\n",
496 KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id, ret);
497 return -EIO;
498 }
499 return 0;
500}
501
502static const struct f2fs_compress_ops f2fs_lzorle_ops = {
503 .init_compress_ctx = lzo_init_compress_ctx,
504 .destroy_compress_ctx = lzo_destroy_compress_ctx,
505 .compress_pages = lzorle_compress_pages,
506 .decompress_pages = lzo_decompress_pages,
507};
508#endif
509#endif
510
Chao Yu4c8ff702019-11-01 18:07:14 +0800511static const struct f2fs_compress_ops *f2fs_cops[COMPRESS_MAX] = {
512#ifdef CONFIG_F2FS_FS_LZO
513 &f2fs_lzo_ops,
514#else
515 NULL,
516#endif
517#ifdef CONFIG_F2FS_FS_LZ4
518 &f2fs_lz4_ops,
519#else
520 NULL,
521#endif
Chao Yu50cfa662020-03-03 17:46:02 +0800522#ifdef CONFIG_F2FS_FS_ZSTD
523 &f2fs_zstd_ops,
524#else
525 NULL,
526#endif
Chao Yu6d92b202020-04-08 19:56:32 +0800527#if defined(CONFIG_F2FS_FS_LZO) && defined(CONFIG_F2FS_FS_LZORLE)
528 &f2fs_lzorle_ops,
529#else
530 NULL,
531#endif
Chao Yu4c8ff702019-11-01 18:07:14 +0800532};
533
534bool f2fs_is_compress_backend_ready(struct inode *inode)
535{
536 if (!f2fs_compressed_file(inode))
537 return true;
538 return f2fs_cops[F2FS_I(inode)->i_compress_algorithm];
539}
540
Jaegeuk Kim99bbe302020-06-16 09:56:51 -0700541static mempool_t *compress_page_pool;
Chao Yu5e6bbde2020-04-08 19:56:05 +0800542static int num_compress_pages = 512;
543module_param(num_compress_pages, uint, 0444);
544MODULE_PARM_DESC(num_compress_pages,
545 "Number of intermediate compress pages to preallocate");
546
547int f2fs_init_compress_mempool(void)
548{
549 compress_page_pool = mempool_create_page_pool(num_compress_pages, 0);
550 if (!compress_page_pool)
551 return -ENOMEM;
552
553 return 0;
554}
555
556void f2fs_destroy_compress_mempool(void)
557{
558 mempool_destroy(compress_page_pool);
559}
560
561static struct page *f2fs_compress_alloc_page(void)
Chao Yu4c8ff702019-11-01 18:07:14 +0800562{
563 struct page *page;
564
Chao Yu5e6bbde2020-04-08 19:56:05 +0800565 page = mempool_alloc(compress_page_pool, GFP_NOFS);
Chao Yu4c8ff702019-11-01 18:07:14 +0800566 lock_page(page);
Chao Yu5e6bbde2020-04-08 19:56:05 +0800567
Chao Yu4c8ff702019-11-01 18:07:14 +0800568 return page;
569}
570
Chao Yu5e6bbde2020-04-08 19:56:05 +0800571static void f2fs_compress_free_page(struct page *page)
572{
573 if (!page)
574 return;
575 set_page_private(page, (unsigned long)NULL);
576 ClearPagePrivate(page);
577 page->mapping = NULL;
578 unlock_page(page);
579 mempool_free(page, compress_page_pool);
580}
581
Daeho Jeong6fcaeba2020-08-12 14:17:11 +0900582#define MAX_VMAP_RETRIES 3
583
584static void *f2fs_vmap(struct page **pages, unsigned int count)
585{
586 int i;
587 void *buf = NULL;
588
589 for (i = 0; i < MAX_VMAP_RETRIES; i++) {
590 buf = vm_map_ram(pages, count, -1);
591 if (buf)
592 break;
593 vm_unmap_aliases();
594 }
595 return buf;
596}
597
Chao Yu4c8ff702019-11-01 18:07:14 +0800598static int f2fs_compress_pages(struct compress_ctx *cc)
599{
Chao Yu4c8ff702019-11-01 18:07:14 +0800600 struct f2fs_inode_info *fi = F2FS_I(cc->inode);
601 const struct f2fs_compress_ops *cops =
602 f2fs_cops[fi->i_compress_algorithm];
Chao Yu31083032020-09-14 17:05:13 +0800603 unsigned int max_len, new_nr_cpages;
604 struct page **new_cpages;
Chao Yu4c8ff702019-11-01 18:07:14 +0800605 int i, ret;
606
607 trace_f2fs_compress_pages_start(cc->inode, cc->cluster_idx,
608 cc->cluster_size, fi->i_compress_algorithm);
609
Chao Yu23b1faaad2020-03-03 16:57:07 +0800610 if (cops->init_compress_ctx) {
611 ret = cops->init_compress_ctx(cc);
612 if (ret)
613 goto out;
614 }
Chao Yu4c8ff702019-11-01 18:07:14 +0800615
616 max_len = COMPRESS_HEADER_SIZE + cc->clen;
617 cc->nr_cpages = DIV_ROUND_UP(max_len, PAGE_SIZE);
618
Chao Yu31083032020-09-14 17:05:13 +0800619 cc->cpages = page_array_alloc(cc->inode, cc->nr_cpages);
Chao Yu4c8ff702019-11-01 18:07:14 +0800620 if (!cc->cpages) {
621 ret = -ENOMEM;
622 goto destroy_compress_ctx;
623 }
624
625 for (i = 0; i < cc->nr_cpages; i++) {
Chao Yu5e6bbde2020-04-08 19:56:05 +0800626 cc->cpages[i] = f2fs_compress_alloc_page();
Chao Yu4c8ff702019-11-01 18:07:14 +0800627 if (!cc->cpages[i]) {
628 ret = -ENOMEM;
629 goto out_free_cpages;
630 }
631 }
632
Daeho Jeong6fcaeba2020-08-12 14:17:11 +0900633 cc->rbuf = f2fs_vmap(cc->rpages, cc->cluster_size);
Chao Yu4c8ff702019-11-01 18:07:14 +0800634 if (!cc->rbuf) {
635 ret = -ENOMEM;
636 goto out_free_cpages;
637 }
638
Daeho Jeong6fcaeba2020-08-12 14:17:11 +0900639 cc->cbuf = f2fs_vmap(cc->cpages, cc->nr_cpages);
Chao Yu4c8ff702019-11-01 18:07:14 +0800640 if (!cc->cbuf) {
641 ret = -ENOMEM;
642 goto out_vunmap_rbuf;
643 }
644
645 ret = cops->compress_pages(cc);
646 if (ret)
647 goto out_vunmap_cbuf;
648
649 max_len = PAGE_SIZE * (cc->cluster_size - 1) - COMPRESS_HEADER_SIZE;
650
651 if (cc->clen > max_len) {
652 ret = -EAGAIN;
653 goto out_vunmap_cbuf;
654 }
655
656 cc->cbuf->clen = cpu_to_le32(cc->clen);
Chao Yu4c8ff702019-11-01 18:07:14 +0800657
658 for (i = 0; i < COMPRESS_DATA_RESERVED_SIZE; i++)
659 cc->cbuf->reserved[i] = cpu_to_le32(0);
660
Chao Yu31083032020-09-14 17:05:13 +0800661 new_nr_cpages = DIV_ROUND_UP(cc->clen + COMPRESS_HEADER_SIZE, PAGE_SIZE);
662
663 /* Now we're going to cut unnecessary tail pages */
664 new_cpages = page_array_alloc(cc->inode, new_nr_cpages);
665 if (!new_cpages) {
666 ret = -ENOMEM;
667 goto out_vunmap_cbuf;
668 }
Eric Biggers7fa6d592020-02-20 20:50:37 -0800669
670 /* zero out any unused part of the last page */
671 memset(&cc->cbuf->cdata[cc->clen], 0,
Chao Yu31083032020-09-14 17:05:13 +0800672 (new_nr_cpages * PAGE_SIZE) -
673 (cc->clen + COMPRESS_HEADER_SIZE));
Eric Biggers7fa6d592020-02-20 20:50:37 -0800674
Daeho Jeong6fcaeba2020-08-12 14:17:11 +0900675 vm_unmap_ram(cc->cbuf, cc->nr_cpages);
676 vm_unmap_ram(cc->rbuf, cc->cluster_size);
Chao Yu4c8ff702019-11-01 18:07:14 +0800677
Chao Yu31083032020-09-14 17:05:13 +0800678 for (i = 0; i < cc->nr_cpages; i++) {
679 if (i < new_nr_cpages) {
680 new_cpages[i] = cc->cpages[i];
681 continue;
682 }
Chao Yu5e6bbde2020-04-08 19:56:05 +0800683 f2fs_compress_free_page(cc->cpages[i]);
Chao Yu4c8ff702019-11-01 18:07:14 +0800684 cc->cpages[i] = NULL;
685 }
686
Chao Yu23b1faaad2020-03-03 16:57:07 +0800687 if (cops->destroy_compress_ctx)
688 cops->destroy_compress_ctx(cc);
Chao Yu09ff4802020-03-03 16:57:06 +0800689
Chao Yu31083032020-09-14 17:05:13 +0800690 page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
691 cc->cpages = new_cpages;
692 cc->nr_cpages = new_nr_cpages;
Chao Yu4c8ff702019-11-01 18:07:14 +0800693
694 trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
695 cc->clen, ret);
696 return 0;
697
698out_vunmap_cbuf:
Daeho Jeong6fcaeba2020-08-12 14:17:11 +0900699 vm_unmap_ram(cc->cbuf, cc->nr_cpages);
Chao Yu4c8ff702019-11-01 18:07:14 +0800700out_vunmap_rbuf:
Daeho Jeong6fcaeba2020-08-12 14:17:11 +0900701 vm_unmap_ram(cc->rbuf, cc->cluster_size);
Chao Yu4c8ff702019-11-01 18:07:14 +0800702out_free_cpages:
703 for (i = 0; i < cc->nr_cpages; i++) {
704 if (cc->cpages[i])
Chao Yu5e6bbde2020-04-08 19:56:05 +0800705 f2fs_compress_free_page(cc->cpages[i]);
Chao Yu4c8ff702019-11-01 18:07:14 +0800706 }
Chao Yu31083032020-09-14 17:05:13 +0800707 page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
Chao Yu4c8ff702019-11-01 18:07:14 +0800708 cc->cpages = NULL;
709destroy_compress_ctx:
Chao Yu23b1faaad2020-03-03 16:57:07 +0800710 if (cops->destroy_compress_ctx)
711 cops->destroy_compress_ctx(cc);
Chao Yu4c8ff702019-11-01 18:07:14 +0800712out:
713 trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
714 cc->clen, ret);
715 return ret;
716}
717
718void f2fs_decompress_pages(struct bio *bio, struct page *page, bool verity)
719{
720 struct decompress_io_ctx *dic =
721 (struct decompress_io_ctx *)page_private(page);
722 struct f2fs_sb_info *sbi = F2FS_I_SB(dic->inode);
723 struct f2fs_inode_info *fi= F2FS_I(dic->inode);
724 const struct f2fs_compress_ops *cops =
725 f2fs_cops[fi->i_compress_algorithm];
726 int ret;
Chao Yub2f57a82020-07-25 09:17:48 +0800727 int i;
Chao Yu4c8ff702019-11-01 18:07:14 +0800728
729 dec_page_count(sbi, F2FS_RD_DATA);
730
731 if (bio->bi_status || PageError(page))
732 dic->failed = true;
733
Chao Yue6c39482020-08-10 18:39:30 +0800734 if (atomic_dec_return(&dic->pending_pages))
Chao Yu4c8ff702019-11-01 18:07:14 +0800735 return;
736
737 trace_f2fs_decompress_pages_start(dic->inode, dic->cluster_idx,
738 dic->cluster_size, fi->i_compress_algorithm);
739
740 /* submit partial compressed pages */
741 if (dic->failed) {
742 ret = -EIO;
743 goto out_free_dic;
744 }
745
Chao Yu31083032020-09-14 17:05:13 +0800746 dic->tpages = page_array_alloc(dic->inode, dic->cluster_size);
Chao Yub2f57a82020-07-25 09:17:48 +0800747 if (!dic->tpages) {
748 ret = -ENOMEM;
749 goto out_free_dic;
750 }
751
752 for (i = 0; i < dic->cluster_size; i++) {
753 if (dic->rpages[i]) {
754 dic->tpages[i] = dic->rpages[i];
755 continue;
756 }
757
758 dic->tpages[i] = f2fs_compress_alloc_page();
759 if (!dic->tpages[i]) {
760 ret = -ENOMEM;
761 goto out_free_dic;
762 }
763 }
764
Chao Yu23b1faaad2020-03-03 16:57:07 +0800765 if (cops->init_decompress_ctx) {
766 ret = cops->init_decompress_ctx(dic);
767 if (ret)
768 goto out_free_dic;
769 }
770
Daeho Jeong6fcaeba2020-08-12 14:17:11 +0900771 dic->rbuf = f2fs_vmap(dic->tpages, dic->cluster_size);
Chao Yu4c8ff702019-11-01 18:07:14 +0800772 if (!dic->rbuf) {
773 ret = -ENOMEM;
Chao Yu23b1faaad2020-03-03 16:57:07 +0800774 goto destroy_decompress_ctx;
Chao Yu4c8ff702019-11-01 18:07:14 +0800775 }
776
Daeho Jeong6fcaeba2020-08-12 14:17:11 +0900777 dic->cbuf = f2fs_vmap(dic->cpages, dic->nr_cpages);
Chao Yu4c8ff702019-11-01 18:07:14 +0800778 if (!dic->cbuf) {
779 ret = -ENOMEM;
780 goto out_vunmap_rbuf;
781 }
782
783 dic->clen = le32_to_cpu(dic->cbuf->clen);
784 dic->rlen = PAGE_SIZE << dic->log_cluster_size;
785
786 if (dic->clen > PAGE_SIZE * dic->nr_cpages - COMPRESS_HEADER_SIZE) {
787 ret = -EFSCORRUPTED;
788 goto out_vunmap_cbuf;
789 }
790
791 ret = cops->decompress_pages(dic);
792
793out_vunmap_cbuf:
Daeho Jeong6fcaeba2020-08-12 14:17:11 +0900794 vm_unmap_ram(dic->cbuf, dic->nr_cpages);
Chao Yu4c8ff702019-11-01 18:07:14 +0800795out_vunmap_rbuf:
Daeho Jeong6fcaeba2020-08-12 14:17:11 +0900796 vm_unmap_ram(dic->rbuf, dic->cluster_size);
Chao Yu23b1faaad2020-03-03 16:57:07 +0800797destroy_decompress_ctx:
798 if (cops->destroy_decompress_ctx)
799 cops->destroy_decompress_ctx(dic);
Chao Yu4c8ff702019-11-01 18:07:14 +0800800out_free_dic:
Chao Yu79bbefb2020-03-23 17:43:04 +0800801 if (verity)
Chao Yue6c39482020-08-10 18:39:30 +0800802 atomic_set(&dic->pending_pages, dic->nr_cpages);
Chao Yu4c8ff702019-11-01 18:07:14 +0800803 if (!verity)
804 f2fs_decompress_end_io(dic->rpages, dic->cluster_size,
805 ret, false);
806
807 trace_f2fs_decompress_pages_end(dic->inode, dic->cluster_idx,
808 dic->clen, ret);
809 if (!verity)
810 f2fs_free_dic(dic);
811}
812
813static bool is_page_in_cluster(struct compress_ctx *cc, pgoff_t index)
814{
815 if (cc->cluster_idx == NULL_CLUSTER)
816 return true;
817 return cc->cluster_idx == cluster_idx(cc, index);
818}
819
820bool f2fs_cluster_is_empty(struct compress_ctx *cc)
821{
822 return cc->nr_rpages == 0;
823}
824
825static bool f2fs_cluster_is_full(struct compress_ctx *cc)
826{
827 return cc->cluster_size == cc->nr_rpages;
828}
829
830bool f2fs_cluster_can_merge_page(struct compress_ctx *cc, pgoff_t index)
831{
832 if (f2fs_cluster_is_empty(cc))
833 return true;
834 return is_page_in_cluster(cc, index);
835}
836
837static bool __cluster_may_compress(struct compress_ctx *cc)
838{
839 struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
840 loff_t i_size = i_size_read(cc->inode);
841 unsigned nr_pages = DIV_ROUND_UP(i_size, PAGE_SIZE);
842 int i;
843
844 for (i = 0; i < cc->cluster_size; i++) {
845 struct page *page = cc->rpages[i];
846
847 f2fs_bug_on(sbi, !page);
848
849 if (unlikely(f2fs_cp_error(sbi)))
850 return false;
851 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
852 return false;
853
854 /* beyond EOF */
855 if (page->index >= nr_pages)
856 return false;
857 }
858 return true;
859}
860
Chao Yu1a67cbe2020-03-12 10:45:29 +0800861static int __f2fs_cluster_blocks(struct compress_ctx *cc, bool compr)
Chao Yu4c8ff702019-11-01 18:07:14 +0800862{
863 struct dnode_of_data dn;
864 int ret;
865
866 set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
867 ret = f2fs_get_dnode_of_data(&dn, start_idx_of_cluster(cc),
868 LOOKUP_NODE);
869 if (ret) {
870 if (ret == -ENOENT)
871 ret = 0;
872 goto fail;
873 }
874
875 if (dn.data_blkaddr == COMPRESS_ADDR) {
876 int i;
877
878 ret = 1;
879 for (i = 1; i < cc->cluster_size; i++) {
880 block_t blkaddr;
881
Chao Yua2ced1c2020-02-14 17:44:10 +0800882 blkaddr = data_blkaddr(dn.inode,
Chao Yu4c8ff702019-11-01 18:07:14 +0800883 dn.node_page, dn.ofs_in_node + i);
Chao Yu1a67cbe2020-03-12 10:45:29 +0800884 if (compr) {
885 if (__is_valid_data_blkaddr(blkaddr))
886 ret++;
887 } else {
888 if (blkaddr != NULL_ADDR)
889 ret++;
890 }
Chao Yu4c8ff702019-11-01 18:07:14 +0800891 }
892 }
893fail:
894 f2fs_put_dnode(&dn);
895 return ret;
896}
897
Chao Yu1a67cbe2020-03-12 10:45:29 +0800898/* return # of compressed blocks in compressed cluster */
899static int f2fs_compressed_blocks(struct compress_ctx *cc)
900{
901 return __f2fs_cluster_blocks(cc, true);
902}
903
904/* return # of valid blocks in compressed cluster */
Wang Xiaojund0783192020-06-16 10:39:24 +0800905static int f2fs_cluster_blocks(struct compress_ctx *cc)
Chao Yu1a67cbe2020-03-12 10:45:29 +0800906{
907 return __f2fs_cluster_blocks(cc, false);
908}
909
Chao Yu4c8ff702019-11-01 18:07:14 +0800910int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index)
911{
912 struct compress_ctx cc = {
913 .inode = inode,
914 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
915 .cluster_size = F2FS_I(inode)->i_cluster_size,
916 .cluster_idx = index >> F2FS_I(inode)->i_log_cluster_size,
917 };
918
Wang Xiaojund0783192020-06-16 10:39:24 +0800919 return f2fs_cluster_blocks(&cc);
Chao Yu4c8ff702019-11-01 18:07:14 +0800920}
921
922static bool cluster_may_compress(struct compress_ctx *cc)
923{
924 if (!f2fs_compressed_file(cc->inode))
925 return false;
926 if (f2fs_is_atomic_file(cc->inode))
927 return false;
928 if (f2fs_is_mmap_file(cc->inode))
929 return false;
930 if (!f2fs_cluster_is_full(cc))
931 return false;
Chao Yudc35d732020-05-26 09:55:02 +0800932 if (unlikely(f2fs_cp_error(F2FS_I_SB(cc->inode))))
933 return false;
Chao Yu4c8ff702019-11-01 18:07:14 +0800934 return __cluster_may_compress(cc);
935}
936
937static void set_cluster_writeback(struct compress_ctx *cc)
938{
939 int i;
940
941 for (i = 0; i < cc->cluster_size; i++) {
942 if (cc->rpages[i])
943 set_page_writeback(cc->rpages[i]);
944 }
945}
946
947static void set_cluster_dirty(struct compress_ctx *cc)
948{
949 int i;
950
951 for (i = 0; i < cc->cluster_size; i++)
952 if (cc->rpages[i])
953 set_page_dirty(cc->rpages[i]);
954}
955
956static int prepare_compress_overwrite(struct compress_ctx *cc,
957 struct page **pagep, pgoff_t index, void **fsdata)
958{
959 struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
960 struct address_space *mapping = cc->inode->i_mapping;
961 struct page *page;
962 struct dnode_of_data dn;
963 sector_t last_block_in_bio;
964 unsigned fgp_flag = FGP_LOCK | FGP_WRITE | FGP_CREAT;
965 pgoff_t start_idx = start_idx_of_cluster(cc);
966 int i, ret;
967 bool prealloc;
968
969retry:
Wang Xiaojund0783192020-06-16 10:39:24 +0800970 ret = f2fs_cluster_blocks(cc);
Chao Yu4c8ff702019-11-01 18:07:14 +0800971 if (ret <= 0)
972 return ret;
973
974 /* compressed case */
975 prealloc = (ret < cc->cluster_size);
976
977 ret = f2fs_init_compress_ctx(cc);
978 if (ret)
979 return ret;
980
981 /* keep page reference to avoid page reclaim */
982 for (i = 0; i < cc->cluster_size; i++) {
983 page = f2fs_pagecache_get_page(mapping, start_idx + i,
984 fgp_flag, GFP_NOFS);
985 if (!page) {
986 ret = -ENOMEM;
987 goto unlock_pages;
988 }
989
990 if (PageUptodate(page))
991 unlock_page(page);
992 else
993 f2fs_compress_ctx_add_page(cc, page);
994 }
995
996 if (!f2fs_cluster_is_empty(cc)) {
997 struct bio *bio = NULL;
998
999 ret = f2fs_read_multi_pages(cc, &bio, cc->cluster_size,
Chao Yu06837282020-02-18 18:21:35 +08001000 &last_block_in_bio, false, true);
Chao Yu4c8ff702019-11-01 18:07:14 +08001001 f2fs_destroy_compress_ctx(cc);
1002 if (ret)
1003 goto release_pages;
1004 if (bio)
1005 f2fs_submit_bio(sbi, bio, DATA);
1006
1007 ret = f2fs_init_compress_ctx(cc);
1008 if (ret)
1009 goto release_pages;
1010 }
1011
1012 for (i = 0; i < cc->cluster_size; i++) {
1013 f2fs_bug_on(sbi, cc->rpages[i]);
1014
1015 page = find_lock_page(mapping, start_idx + i);
1016 f2fs_bug_on(sbi, !page);
1017
1018 f2fs_wait_on_page_writeback(page, DATA, true, true);
1019
1020 f2fs_compress_ctx_add_page(cc, page);
1021 f2fs_put_page(page, 0);
1022
1023 if (!PageUptodate(page)) {
1024 f2fs_unlock_rpages(cc, i + 1);
Chao Yubc67c5d2020-06-08 20:03:17 +08001025 f2fs_put_rpages_mapping(mapping, start_idx,
Chao Yu4c8ff702019-11-01 18:07:14 +08001026 cc->cluster_size);
1027 f2fs_destroy_compress_ctx(cc);
1028 goto retry;
1029 }
1030 }
1031
1032 if (prealloc) {
Chao Yu0ef81832020-06-18 14:36:22 +08001033 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
Chao Yu4c8ff702019-11-01 18:07:14 +08001034
1035 set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
1036
1037 for (i = cc->cluster_size - 1; i > 0; i--) {
1038 ret = f2fs_get_block(&dn, start_idx + i);
1039 if (ret) {
1040 i = cc->cluster_size;
1041 break;
1042 }
1043
1044 if (dn.data_blkaddr != NEW_ADDR)
1045 break;
1046 }
1047
Chao Yu0ef81832020-06-18 14:36:22 +08001048 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
Chao Yu4c8ff702019-11-01 18:07:14 +08001049 }
1050
1051 if (likely(!ret)) {
1052 *fsdata = cc->rpages;
1053 *pagep = cc->rpages[offset_in_cluster(cc, index)];
1054 return cc->cluster_size;
1055 }
1056
1057unlock_pages:
1058 f2fs_unlock_rpages(cc, i);
1059release_pages:
Chao Yubc67c5d2020-06-08 20:03:17 +08001060 f2fs_put_rpages_mapping(mapping, start_idx, i);
Chao Yu4c8ff702019-11-01 18:07:14 +08001061 f2fs_destroy_compress_ctx(cc);
1062 return ret;
1063}
1064
1065int f2fs_prepare_compress_overwrite(struct inode *inode,
1066 struct page **pagep, pgoff_t index, void **fsdata)
1067{
1068 struct compress_ctx cc = {
1069 .inode = inode,
1070 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
1071 .cluster_size = F2FS_I(inode)->i_cluster_size,
1072 .cluster_idx = index >> F2FS_I(inode)->i_log_cluster_size,
1073 .rpages = NULL,
1074 .nr_rpages = 0,
1075 };
1076
1077 return prepare_compress_overwrite(&cc, pagep, index, fsdata);
1078}
1079
1080bool f2fs_compress_write_end(struct inode *inode, void *fsdata,
1081 pgoff_t index, unsigned copied)
1082
1083{
1084 struct compress_ctx cc = {
Chao Yu31083032020-09-14 17:05:13 +08001085 .inode = inode,
Chao Yu4c8ff702019-11-01 18:07:14 +08001086 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
1087 .cluster_size = F2FS_I(inode)->i_cluster_size,
1088 .rpages = fsdata,
1089 };
1090 bool first_index = (index == cc.rpages[0]->index);
1091
1092 if (copied)
1093 set_cluster_dirty(&cc);
1094
1095 f2fs_put_rpages_wbc(&cc, NULL, false, 1);
1096 f2fs_destroy_compress_ctx(&cc);
1097
1098 return first_index;
1099}
1100
Chao Yu3265d3d2020-03-18 16:22:59 +08001101int f2fs_truncate_partial_cluster(struct inode *inode, u64 from, bool lock)
1102{
1103 void *fsdata = NULL;
1104 struct page *pagep;
1105 int log_cluster_size = F2FS_I(inode)->i_log_cluster_size;
1106 pgoff_t start_idx = from >> (PAGE_SHIFT + log_cluster_size) <<
1107 log_cluster_size;
1108 int err;
1109
1110 err = f2fs_is_compressed_cluster(inode, start_idx);
1111 if (err < 0)
1112 return err;
1113
1114 /* truncate normal cluster */
1115 if (!err)
1116 return f2fs_do_truncate_blocks(inode, from, lock);
1117
1118 /* truncate compressed cluster */
1119 err = f2fs_prepare_compress_overwrite(inode, &pagep,
1120 start_idx, &fsdata);
1121
1122 /* should not be a normal cluster */
1123 f2fs_bug_on(F2FS_I_SB(inode), err == 0);
1124
1125 if (err <= 0)
1126 return err;
1127
1128 if (err > 0) {
1129 struct page **rpages = fsdata;
1130 int cluster_size = F2FS_I(inode)->i_cluster_size;
1131 int i;
1132
1133 for (i = cluster_size - 1; i >= 0; i--) {
1134 loff_t start = rpages[i]->index << PAGE_SHIFT;
1135
1136 if (from <= start) {
1137 zero_user_segment(rpages[i], 0, PAGE_SIZE);
1138 } else {
1139 zero_user_segment(rpages[i], from - start,
1140 PAGE_SIZE);
1141 break;
1142 }
1143 }
1144
1145 f2fs_compress_write_end(inode, fsdata, start_idx, true);
1146 }
1147 return 0;
1148}
1149
Chao Yu4c8ff702019-11-01 18:07:14 +08001150static int f2fs_write_compressed_pages(struct compress_ctx *cc,
1151 int *submitted,
1152 struct writeback_control *wbc,
1153 enum iostat_type io_type)
1154{
1155 struct inode *inode = cc->inode;
1156 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1157 struct f2fs_inode_info *fi = F2FS_I(inode);
1158 struct f2fs_io_info fio = {
1159 .sbi = sbi,
1160 .ino = cc->inode->i_ino,
1161 .type = DATA,
1162 .op = REQ_OP_WRITE,
1163 .op_flags = wbc_to_write_flags(wbc),
1164 .old_blkaddr = NEW_ADDR,
1165 .page = NULL,
1166 .encrypted_page = NULL,
1167 .compressed_page = NULL,
1168 .submitted = false,
Chao Yu4c8ff702019-11-01 18:07:14 +08001169 .io_type = io_type,
1170 .io_wbc = wbc,
Satya Tangirala27aacd22020-07-02 01:56:06 +00001171 .encrypted = fscrypt_inode_uses_fs_layer_crypto(cc->inode),
Chao Yu4c8ff702019-11-01 18:07:14 +08001172 };
1173 struct dnode_of_data dn;
1174 struct node_info ni;
1175 struct compress_io_ctx *cic;
1176 pgoff_t start_idx = start_idx_of_cluster(cc);
1177 unsigned int last_index = cc->cluster_size - 1;
1178 loff_t psize;
1179 int i, err;
1180
Chao Yu79963d92020-06-18 14:36:23 +08001181 if (IS_NOQUOTA(inode)) {
1182 /*
1183 * We need to wait for node_write to avoid block allocation during
1184 * checkpoint. This can only happen to quota writes which can cause
1185 * the below discard race condition.
1186 */
1187 down_read(&sbi->node_write);
1188 } else if (!f2fs_trylock_op(sbi)) {
Chao Yu31083032020-09-14 17:05:13 +08001189 goto out_free;
Chao Yu79963d92020-06-18 14:36:23 +08001190 }
Chao Yu4c8ff702019-11-01 18:07:14 +08001191
Chao Yudf77fbd2020-02-24 19:20:16 +08001192 set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
Chao Yu4c8ff702019-11-01 18:07:14 +08001193
1194 err = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
1195 if (err)
1196 goto out_unlock_op;
1197
1198 for (i = 0; i < cc->cluster_size; i++) {
Chao Yua2ced1c2020-02-14 17:44:10 +08001199 if (data_blkaddr(dn.inode, dn.node_page,
Chao Yu4c8ff702019-11-01 18:07:14 +08001200 dn.ofs_in_node + i) == NULL_ADDR)
1201 goto out_put_dnode;
1202 }
1203
1204 psize = (loff_t)(cc->rpages[last_index]->index + 1) << PAGE_SHIFT;
1205
1206 err = f2fs_get_node_info(fio.sbi, dn.nid, &ni);
1207 if (err)
1208 goto out_put_dnode;
1209
1210 fio.version = ni.version;
1211
Chao Yuc68d6c82020-09-14 17:05:14 +08001212 cic = kmem_cache_zalloc(cic_entry_slab, GFP_NOFS);
Chao Yu4c8ff702019-11-01 18:07:14 +08001213 if (!cic)
1214 goto out_put_dnode;
1215
1216 cic->magic = F2FS_COMPRESSED_PAGE_MAGIC;
1217 cic->inode = inode;
Chao Yue6c39482020-08-10 18:39:30 +08001218 atomic_set(&cic->pending_pages, cc->nr_cpages);
Chao Yu31083032020-09-14 17:05:13 +08001219 cic->rpages = page_array_alloc(cc->inode, cc->cluster_size);
Chao Yu4c8ff702019-11-01 18:07:14 +08001220 if (!cic->rpages)
1221 goto out_put_cic;
1222
1223 cic->nr_rpages = cc->cluster_size;
1224
1225 for (i = 0; i < cc->nr_cpages; i++) {
1226 f2fs_set_compressed_page(cc->cpages[i], inode,
Chao Yu887347a2020-03-28 17:33:23 +08001227 cc->rpages[i + 1]->index, cic);
Chao Yu4c8ff702019-11-01 18:07:14 +08001228 fio.compressed_page = cc->cpages[i];
Chao Yuf567adb2020-07-03 16:40:11 +08001229
1230 fio.old_blkaddr = data_blkaddr(dn.inode, dn.node_page,
1231 dn.ofs_in_node + i + 1);
1232
1233 /* wait for GCed page writeback via META_MAPPING */
1234 f2fs_wait_on_block_writeback(inode, fio.old_blkaddr);
1235
Chao Yu4c8ff702019-11-01 18:07:14 +08001236 if (fio.encrypted) {
1237 fio.page = cc->rpages[i + 1];
1238 err = f2fs_encrypt_one_page(&fio);
1239 if (err)
1240 goto out_destroy_crypt;
1241 cc->cpages[i] = fio.encrypted_page;
1242 }
1243 }
1244
1245 set_cluster_writeback(cc);
1246
1247 for (i = 0; i < cc->cluster_size; i++)
1248 cic->rpages[i] = cc->rpages[i];
1249
1250 for (i = 0; i < cc->cluster_size; i++, dn.ofs_in_node++) {
1251 block_t blkaddr;
1252
Chao Yua2ced1c2020-02-14 17:44:10 +08001253 blkaddr = f2fs_data_blkaddr(&dn);
Chao Yu95978ca2020-02-28 18:08:46 +08001254 fio.page = cc->rpages[i];
Chao Yu4c8ff702019-11-01 18:07:14 +08001255 fio.old_blkaddr = blkaddr;
1256
1257 /* cluster header */
1258 if (i == 0) {
1259 if (blkaddr == COMPRESS_ADDR)
1260 fio.compr_blocks++;
1261 if (__is_valid_data_blkaddr(blkaddr))
1262 f2fs_invalidate_blocks(sbi, blkaddr);
1263 f2fs_update_data_blkaddr(&dn, COMPRESS_ADDR);
1264 goto unlock_continue;
1265 }
1266
1267 if (fio.compr_blocks && __is_valid_data_blkaddr(blkaddr))
1268 fio.compr_blocks++;
1269
1270 if (i > cc->nr_cpages) {
1271 if (__is_valid_data_blkaddr(blkaddr)) {
1272 f2fs_invalidate_blocks(sbi, blkaddr);
1273 f2fs_update_data_blkaddr(&dn, NEW_ADDR);
1274 }
1275 goto unlock_continue;
1276 }
1277
1278 f2fs_bug_on(fio.sbi, blkaddr == NULL_ADDR);
1279
1280 if (fio.encrypted)
1281 fio.encrypted_page = cc->cpages[i - 1];
1282 else
1283 fio.compressed_page = cc->cpages[i - 1];
1284
1285 cc->cpages[i - 1] = NULL;
1286 f2fs_outplace_write_data(&dn, &fio);
1287 (*submitted)++;
1288unlock_continue:
1289 inode_dec_dirty_pages(cc->inode);
1290 unlock_page(fio.page);
1291 }
1292
1293 if (fio.compr_blocks)
1294 f2fs_i_compr_blocks_update(inode, fio.compr_blocks - 1, false);
1295 f2fs_i_compr_blocks_update(inode, cc->nr_cpages, true);
1296
1297 set_inode_flag(cc->inode, FI_APPEND_WRITE);
1298 if (cc->cluster_idx == 0)
1299 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
1300
1301 f2fs_put_dnode(&dn);
Chao Yu79963d92020-06-18 14:36:23 +08001302 if (IS_NOQUOTA(inode))
1303 up_read(&sbi->node_write);
1304 else
Jaegeuk Kim435cbab2020-04-09 10:25:21 -07001305 f2fs_unlock_op(sbi);
Chao Yu4c8ff702019-11-01 18:07:14 +08001306
Chao Yuc10c9822020-02-27 19:30:03 +08001307 spin_lock(&fi->i_size_lock);
Chao Yu4c8ff702019-11-01 18:07:14 +08001308 if (fi->last_disk_size < psize)
1309 fi->last_disk_size = psize;
Chao Yuc10c9822020-02-27 19:30:03 +08001310 spin_unlock(&fi->i_size_lock);
Chao Yu4c8ff702019-11-01 18:07:14 +08001311
1312 f2fs_put_rpages(cc);
Chao Yu31083032020-09-14 17:05:13 +08001313 page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
1314 cc->cpages = NULL;
Chao Yu4c8ff702019-11-01 18:07:14 +08001315 f2fs_destroy_compress_ctx(cc);
1316 return 0;
1317
1318out_destroy_crypt:
Chao Yu31083032020-09-14 17:05:13 +08001319 page_array_free(cc->inode, cic->rpages, cc->cluster_size);
Chao Yu4c8ff702019-11-01 18:07:14 +08001320
1321 for (--i; i >= 0; i--)
1322 fscrypt_finalize_bounce_page(&cc->cpages[i]);
1323 for (i = 0; i < cc->nr_cpages; i++) {
1324 if (!cc->cpages[i])
1325 continue;
1326 f2fs_put_page(cc->cpages[i], 1);
1327 }
1328out_put_cic:
Chao Yuc68d6c82020-09-14 17:05:14 +08001329 kmem_cache_free(cic_entry_slab, cic);
Chao Yu4c8ff702019-11-01 18:07:14 +08001330out_put_dnode:
1331 f2fs_put_dnode(&dn);
1332out_unlock_op:
Chao Yu79963d92020-06-18 14:36:23 +08001333 if (IS_NOQUOTA(inode))
1334 up_read(&sbi->node_write);
1335 else
Jaegeuk Kim435cbab2020-04-09 10:25:21 -07001336 f2fs_unlock_op(sbi);
Chao Yu31083032020-09-14 17:05:13 +08001337out_free:
1338 page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
1339 cc->cpages = NULL;
Chao Yu4c8ff702019-11-01 18:07:14 +08001340 return -EAGAIN;
1341}
1342
1343void f2fs_compress_write_end_io(struct bio *bio, struct page *page)
1344{
1345 struct f2fs_sb_info *sbi = bio->bi_private;
1346 struct compress_io_ctx *cic =
1347 (struct compress_io_ctx *)page_private(page);
1348 int i;
1349
1350 if (unlikely(bio->bi_status))
1351 mapping_set_error(cic->inode->i_mapping, -EIO);
1352
Chao Yu5e6bbde2020-04-08 19:56:05 +08001353 f2fs_compress_free_page(page);
Chao Yu4c8ff702019-11-01 18:07:14 +08001354
1355 dec_page_count(sbi, F2FS_WB_DATA);
1356
Chao Yue6c39482020-08-10 18:39:30 +08001357 if (atomic_dec_return(&cic->pending_pages))
Chao Yu4c8ff702019-11-01 18:07:14 +08001358 return;
1359
1360 for (i = 0; i < cic->nr_rpages; i++) {
1361 WARN_ON(!cic->rpages[i]);
1362 clear_cold_data(cic->rpages[i]);
1363 end_page_writeback(cic->rpages[i]);
1364 }
1365
Chao Yu31083032020-09-14 17:05:13 +08001366 page_array_free(cic->inode, cic->rpages, cic->nr_rpages);
Chao Yuc68d6c82020-09-14 17:05:14 +08001367 kmem_cache_free(cic_entry_slab, cic);
Chao Yu4c8ff702019-11-01 18:07:14 +08001368}
1369
1370static int f2fs_write_raw_pages(struct compress_ctx *cc,
1371 int *submitted,
1372 struct writeback_control *wbc,
1373 enum iostat_type io_type)
1374{
1375 struct address_space *mapping = cc->inode->i_mapping;
1376 int _submitted, compr_blocks, ret;
1377 int i = -1, err = 0;
1378
1379 compr_blocks = f2fs_compressed_blocks(cc);
1380 if (compr_blocks < 0) {
1381 err = compr_blocks;
1382 goto out_err;
1383 }
1384
1385 for (i = 0; i < cc->cluster_size; i++) {
1386 if (!cc->rpages[i])
1387 continue;
1388retry_write:
1389 if (cc->rpages[i]->mapping != mapping) {
1390 unlock_page(cc->rpages[i]);
1391 continue;
1392 }
1393
1394 BUG_ON(!PageLocked(cc->rpages[i]));
1395
1396 ret = f2fs_write_single_data_page(cc->rpages[i], &_submitted,
1397 NULL, NULL, wbc, io_type,
1398 compr_blocks);
1399 if (ret) {
1400 if (ret == AOP_WRITEPAGE_ACTIVATE) {
1401 unlock_page(cc->rpages[i]);
1402 ret = 0;
1403 } else if (ret == -EAGAIN) {
Chao Yu466357d2020-03-20 18:14:31 +08001404 /*
1405 * for quota file, just redirty left pages to
1406 * avoid deadlock caused by cluster update race
1407 * from foreground operation.
1408 */
1409 if (IS_NOQUOTA(cc->inode)) {
1410 err = 0;
1411 goto out_err;
1412 }
Chao Yu4c8ff702019-11-01 18:07:14 +08001413 ret = 0;
1414 cond_resched();
Chao Yu5df7731f2020-02-17 17:45:44 +08001415 congestion_wait(BLK_RW_ASYNC,
1416 DEFAULT_IO_TIMEOUT);
Chao Yu4c8ff702019-11-01 18:07:14 +08001417 lock_page(cc->rpages[i]);
Chao Yueb1353c2020-06-19 17:14:19 +08001418
1419 if (!PageDirty(cc->rpages[i])) {
1420 unlock_page(cc->rpages[i]);
1421 continue;
1422 }
1423
Chao Yu4c8ff702019-11-01 18:07:14 +08001424 clear_page_dirty_for_io(cc->rpages[i]);
1425 goto retry_write;
1426 }
1427 err = ret;
Chao Yu466357d2020-03-20 18:14:31 +08001428 goto out_err;
Chao Yu4c8ff702019-11-01 18:07:14 +08001429 }
1430
1431 *submitted += _submitted;
1432 }
1433 return 0;
Chao Yu4c8ff702019-11-01 18:07:14 +08001434out_err:
1435 for (++i; i < cc->cluster_size; i++) {
1436 if (!cc->rpages[i])
1437 continue;
1438 redirty_page_for_writepage(wbc, cc->rpages[i]);
1439 unlock_page(cc->rpages[i]);
1440 }
1441 return err;
1442}
1443
1444int f2fs_write_multi_pages(struct compress_ctx *cc,
1445 int *submitted,
1446 struct writeback_control *wbc,
1447 enum iostat_type io_type)
1448{
Chao Yu4c8ff702019-11-01 18:07:14 +08001449 int err;
1450
1451 *submitted = 0;
1452 if (cluster_may_compress(cc)) {
1453 err = f2fs_compress_pages(cc);
1454 if (err == -EAGAIN) {
1455 goto write;
1456 } else if (err) {
1457 f2fs_put_rpages_wbc(cc, wbc, true, 1);
1458 goto destroy_out;
1459 }
1460
1461 err = f2fs_write_compressed_pages(cc, submitted,
1462 wbc, io_type);
Chao Yu4c8ff702019-11-01 18:07:14 +08001463 if (!err)
1464 return 0;
1465 f2fs_bug_on(F2FS_I_SB(cc->inode), err != -EAGAIN);
1466 }
1467write:
1468 f2fs_bug_on(F2FS_I_SB(cc->inode), *submitted);
1469
1470 err = f2fs_write_raw_pages(cc, submitted, wbc, io_type);
1471 f2fs_put_rpages_wbc(cc, wbc, false, 0);
1472destroy_out:
1473 f2fs_destroy_compress_ctx(cc);
1474 return err;
1475}
1476
1477struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc)
1478{
Chao Yu4c8ff702019-11-01 18:07:14 +08001479 struct decompress_io_ctx *dic;
1480 pgoff_t start_idx = start_idx_of_cluster(cc);
1481 int i;
1482
Chao Yuc68d6c82020-09-14 17:05:14 +08001483 dic = kmem_cache_zalloc(dic_entry_slab, GFP_NOFS);
Chao Yu4c8ff702019-11-01 18:07:14 +08001484 if (!dic)
1485 return ERR_PTR(-ENOMEM);
1486
Chao Yu31083032020-09-14 17:05:13 +08001487 dic->rpages = page_array_alloc(cc->inode, cc->cluster_size);
Chao Yu4c8ff702019-11-01 18:07:14 +08001488 if (!dic->rpages) {
Chao Yuc68d6c82020-09-14 17:05:14 +08001489 kmem_cache_free(dic_entry_slab, dic);
Chao Yu4c8ff702019-11-01 18:07:14 +08001490 return ERR_PTR(-ENOMEM);
1491 }
1492
1493 dic->magic = F2FS_COMPRESSED_PAGE_MAGIC;
1494 dic->inode = cc->inode;
Chao Yue6c39482020-08-10 18:39:30 +08001495 atomic_set(&dic->pending_pages, cc->nr_cpages);
Chao Yu4c8ff702019-11-01 18:07:14 +08001496 dic->cluster_idx = cc->cluster_idx;
1497 dic->cluster_size = cc->cluster_size;
1498 dic->log_cluster_size = cc->log_cluster_size;
1499 dic->nr_cpages = cc->nr_cpages;
1500 dic->failed = false;
1501
1502 for (i = 0; i < dic->cluster_size; i++)
1503 dic->rpages[i] = cc->rpages[i];
1504 dic->nr_rpages = cc->cluster_size;
1505
Chao Yu31083032020-09-14 17:05:13 +08001506 dic->cpages = page_array_alloc(dic->inode, dic->nr_cpages);
Chao Yu4c8ff702019-11-01 18:07:14 +08001507 if (!dic->cpages)
1508 goto out_free;
1509
1510 for (i = 0; i < dic->nr_cpages; i++) {
1511 struct page *page;
1512
Chao Yu5e6bbde2020-04-08 19:56:05 +08001513 page = f2fs_compress_alloc_page();
Chao Yu4c8ff702019-11-01 18:07:14 +08001514 if (!page)
1515 goto out_free;
1516
1517 f2fs_set_compressed_page(page, cc->inode,
Chao Yu887347a2020-03-28 17:33:23 +08001518 start_idx + i + 1, dic);
Chao Yu4c8ff702019-11-01 18:07:14 +08001519 dic->cpages[i] = page;
1520 }
1521
Chao Yu4c8ff702019-11-01 18:07:14 +08001522 return dic;
1523
1524out_free:
1525 f2fs_free_dic(dic);
1526 return ERR_PTR(-ENOMEM);
1527}
1528
1529void f2fs_free_dic(struct decompress_io_ctx *dic)
1530{
1531 int i;
1532
1533 if (dic->tpages) {
1534 for (i = 0; i < dic->cluster_size; i++) {
1535 if (dic->rpages[i])
1536 continue;
Chao Yu8908e752020-03-26 17:42:26 +08001537 if (!dic->tpages[i])
1538 continue;
Chao Yu5e6bbde2020-04-08 19:56:05 +08001539 f2fs_compress_free_page(dic->tpages[i]);
Chao Yu4c8ff702019-11-01 18:07:14 +08001540 }
Chao Yu31083032020-09-14 17:05:13 +08001541 page_array_free(dic->inode, dic->tpages, dic->cluster_size);
Chao Yu4c8ff702019-11-01 18:07:14 +08001542 }
1543
1544 if (dic->cpages) {
1545 for (i = 0; i < dic->nr_cpages; i++) {
1546 if (!dic->cpages[i])
1547 continue;
Chao Yu5e6bbde2020-04-08 19:56:05 +08001548 f2fs_compress_free_page(dic->cpages[i]);
Chao Yu4c8ff702019-11-01 18:07:14 +08001549 }
Chao Yu31083032020-09-14 17:05:13 +08001550 page_array_free(dic->inode, dic->cpages, dic->nr_cpages);
Chao Yu4c8ff702019-11-01 18:07:14 +08001551 }
1552
Chao Yu31083032020-09-14 17:05:13 +08001553 page_array_free(dic->inode, dic->rpages, dic->nr_rpages);
Chao Yuc68d6c82020-09-14 17:05:14 +08001554 kmem_cache_free(dic_entry_slab, dic);
Chao Yu4c8ff702019-11-01 18:07:14 +08001555}
1556
1557void f2fs_decompress_end_io(struct page **rpages,
1558 unsigned int cluster_size, bool err, bool verity)
1559{
1560 int i;
1561
1562 for (i = 0; i < cluster_size; i++) {
1563 struct page *rpage = rpages[i];
1564
1565 if (!rpage)
1566 continue;
1567
Chao Yu23c51be2020-03-21 20:24:11 +08001568 if (err || PageError(rpage))
1569 goto clear_uptodate;
1570
1571 if (!verity || fsverity_verify_page(rpage)) {
1572 SetPageUptodate(rpage);
1573 goto unlock;
Chao Yu4c8ff702019-11-01 18:07:14 +08001574 }
Chao Yu23c51be2020-03-21 20:24:11 +08001575clear_uptodate:
1576 ClearPageUptodate(rpage);
1577 ClearPageError(rpage);
1578unlock:
Chao Yu4c8ff702019-11-01 18:07:14 +08001579 unlock_page(rpage);
1580 }
1581}
Chao Yu31083032020-09-14 17:05:13 +08001582
1583int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi)
1584{
1585 dev_t dev = sbi->sb->s_bdev->bd_dev;
1586 char slab_name[32];
1587
1588 sprintf(slab_name, "f2fs_page_array_entry-%u:%u", MAJOR(dev), MINOR(dev));
1589
1590 sbi->page_array_slab_size = sizeof(struct page *) <<
1591 F2FS_OPTION(sbi).compress_log_size;
1592
1593 sbi->page_array_slab = f2fs_kmem_cache_create(slab_name,
1594 sbi->page_array_slab_size);
1595 if (!sbi->page_array_slab)
1596 return -ENOMEM;
1597 return 0;
1598}
1599
1600void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi)
1601{
1602 kmem_cache_destroy(sbi->page_array_slab);
1603}
Chao Yuc68d6c82020-09-14 17:05:14 +08001604
1605static int __init f2fs_init_cic_cache(void)
1606{
1607 cic_entry_slab = f2fs_kmem_cache_create("f2fs_cic_entry",
1608 sizeof(struct compress_io_ctx));
1609 if (!cic_entry_slab)
1610 return -ENOMEM;
1611 return 0;
1612}
1613
1614static void f2fs_destroy_cic_cache(void)
1615{
1616 kmem_cache_destroy(cic_entry_slab);
1617}
1618
1619static int __init f2fs_init_dic_cache(void)
1620{
1621 dic_entry_slab = f2fs_kmem_cache_create("f2fs_dic_entry",
1622 sizeof(struct decompress_io_ctx));
1623 if (!dic_entry_slab)
1624 return -ENOMEM;
1625 return 0;
1626}
1627
1628static void f2fs_destroy_dic_cache(void)
1629{
1630 kmem_cache_destroy(dic_entry_slab);
1631}
1632
1633int __init f2fs_init_compress_cache(void)
1634{
1635 int err;
1636
1637 err = f2fs_init_cic_cache();
1638 if (err)
1639 goto out;
1640 err = f2fs_init_dic_cache();
1641 if (err)
1642 goto free_cic;
1643 return 0;
1644free_cic:
1645 f2fs_destroy_cic_cache();
1646out:
1647 return -ENOMEM;
1648}
1649
1650void f2fs_destroy_compress_cache(void)
1651{
1652 f2fs_destroy_dic_cache();
1653 f2fs_destroy_cic_cache();
1654}