]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - fs/f2fs/compress.c
f2fs: fix to avoid triggering IO in write path
[mirror_ubuntu-jammy-kernel.git] / fs / f2fs / compress.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * f2fs compress support
4 *
5 * Copyright (c) 2019 Chao Yu <chao@kernel.org>
6 */
7
8 #include <linux/fs.h>
9 #include <linux/f2fs_fs.h>
10 #include <linux/writeback.h>
11 #include <linux/backing-dev.h>
12 #include <linux/lzo.h>
13 #include <linux/lz4.h>
14
15 #include "f2fs.h"
16 #include "node.h"
17 #include <trace/events/f2fs.h>
18
19 struct f2fs_compress_ops {
20 int (*init_compress_ctx)(struct compress_ctx *cc);
21 void (*destroy_compress_ctx)(struct compress_ctx *cc);
22 int (*compress_pages)(struct compress_ctx *cc);
23 int (*decompress_pages)(struct decompress_io_ctx *dic);
24 };
25
26 static unsigned int offset_in_cluster(struct compress_ctx *cc, pgoff_t index)
27 {
28 return index & (cc->cluster_size - 1);
29 }
30
31 static pgoff_t cluster_idx(struct compress_ctx *cc, pgoff_t index)
32 {
33 return index >> cc->log_cluster_size;
34 }
35
36 static pgoff_t start_idx_of_cluster(struct compress_ctx *cc)
37 {
38 return cc->cluster_idx << cc->log_cluster_size;
39 }
40
41 bool f2fs_is_compressed_page(struct page *page)
42 {
43 if (!PagePrivate(page))
44 return false;
45 if (!page_private(page))
46 return false;
47 if (IS_ATOMIC_WRITTEN_PAGE(page) || IS_DUMMY_WRITTEN_PAGE(page))
48 return false;
49 f2fs_bug_on(F2FS_M_SB(page->mapping),
50 *((u32 *)page_private(page)) != F2FS_COMPRESSED_PAGE_MAGIC);
51 return true;
52 }
53
54 static void f2fs_set_compressed_page(struct page *page,
55 struct inode *inode, pgoff_t index, void *data, refcount_t *r)
56 {
57 SetPagePrivate(page);
58 set_page_private(page, (unsigned long)data);
59
60 /* i_crypto_info and iv index */
61 page->index = index;
62 page->mapping = inode->i_mapping;
63 if (r)
64 refcount_inc(r);
65 }
66
67 static void f2fs_put_compressed_page(struct page *page)
68 {
69 set_page_private(page, (unsigned long)NULL);
70 ClearPagePrivate(page);
71 page->mapping = NULL;
72 unlock_page(page);
73 put_page(page);
74 }
75
76 static void f2fs_drop_rpages(struct compress_ctx *cc, int len, bool unlock)
77 {
78 int i;
79
80 for (i = 0; i < len; i++) {
81 if (!cc->rpages[i])
82 continue;
83 if (unlock)
84 unlock_page(cc->rpages[i]);
85 else
86 put_page(cc->rpages[i]);
87 }
88 }
89
90 static void f2fs_put_rpages(struct compress_ctx *cc)
91 {
92 f2fs_drop_rpages(cc, cc->cluster_size, false);
93 }
94
95 static void f2fs_unlock_rpages(struct compress_ctx *cc, int len)
96 {
97 f2fs_drop_rpages(cc, len, true);
98 }
99
100 static void f2fs_put_rpages_mapping(struct compress_ctx *cc,
101 struct address_space *mapping,
102 pgoff_t start, int len)
103 {
104 int i;
105
106 for (i = 0; i < len; i++) {
107 struct page *page = find_get_page(mapping, start + i);
108
109 put_page(page);
110 put_page(page);
111 }
112 }
113
114 static void f2fs_put_rpages_wbc(struct compress_ctx *cc,
115 struct writeback_control *wbc, bool redirty, int unlock)
116 {
117 unsigned int i;
118
119 for (i = 0; i < cc->cluster_size; i++) {
120 if (!cc->rpages[i])
121 continue;
122 if (redirty)
123 redirty_page_for_writepage(wbc, cc->rpages[i]);
124 f2fs_put_page(cc->rpages[i], unlock);
125 }
126 }
127
128 struct page *f2fs_compress_control_page(struct page *page)
129 {
130 return ((struct compress_io_ctx *)page_private(page))->rpages[0];
131 }
132
133 int f2fs_init_compress_ctx(struct compress_ctx *cc)
134 {
135 struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
136
137 if (cc->nr_rpages)
138 return 0;
139
140 cc->rpages = f2fs_kzalloc(sbi, sizeof(struct page *) <<
141 cc->log_cluster_size, GFP_NOFS);
142 return cc->rpages ? 0 : -ENOMEM;
143 }
144
145 void f2fs_destroy_compress_ctx(struct compress_ctx *cc)
146 {
147 kfree(cc->rpages);
148 cc->rpages = NULL;
149 cc->nr_rpages = 0;
150 cc->nr_cpages = 0;
151 cc->cluster_idx = NULL_CLUSTER;
152 }
153
154 void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct page *page)
155 {
156 unsigned int cluster_ofs;
157
158 if (!f2fs_cluster_can_merge_page(cc, page->index))
159 f2fs_bug_on(F2FS_I_SB(cc->inode), 1);
160
161 cluster_ofs = offset_in_cluster(cc, page->index);
162 cc->rpages[cluster_ofs] = page;
163 cc->nr_rpages++;
164 cc->cluster_idx = cluster_idx(cc, page->index);
165 }
166
167 #ifdef CONFIG_F2FS_FS_LZO
168 static int lzo_init_compress_ctx(struct compress_ctx *cc)
169 {
170 cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
171 LZO1X_MEM_COMPRESS, GFP_NOFS);
172 if (!cc->private)
173 return -ENOMEM;
174
175 cc->clen = lzo1x_worst_compress(PAGE_SIZE << cc->log_cluster_size);
176 return 0;
177 }
178
179 static void lzo_destroy_compress_ctx(struct compress_ctx *cc)
180 {
181 kvfree(cc->private);
182 cc->private = NULL;
183 }
184
185 static int lzo_compress_pages(struct compress_ctx *cc)
186 {
187 int ret;
188
189 ret = lzo1x_1_compress(cc->rbuf, cc->rlen, cc->cbuf->cdata,
190 &cc->clen, cc->private);
191 if (ret != LZO_E_OK) {
192 printk_ratelimited("%sF2FS-fs (%s): lzo compress failed, ret:%d\n",
193 KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id, ret);
194 return -EIO;
195 }
196 return 0;
197 }
198
199 static int lzo_decompress_pages(struct decompress_io_ctx *dic)
200 {
201 int ret;
202
203 ret = lzo1x_decompress_safe(dic->cbuf->cdata, dic->clen,
204 dic->rbuf, &dic->rlen);
205 if (ret != LZO_E_OK) {
206 printk_ratelimited("%sF2FS-fs (%s): lzo decompress failed, ret:%d\n",
207 KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id, ret);
208 return -EIO;
209 }
210
211 if (dic->rlen != PAGE_SIZE << dic->log_cluster_size) {
212 printk_ratelimited("%sF2FS-fs (%s): lzo invalid rlen:%zu, "
213 "expected:%lu\n", KERN_ERR,
214 F2FS_I_SB(dic->inode)->sb->s_id,
215 dic->rlen,
216 PAGE_SIZE << dic->log_cluster_size);
217 return -EIO;
218 }
219 return 0;
220 }
221
222 static const struct f2fs_compress_ops f2fs_lzo_ops = {
223 .init_compress_ctx = lzo_init_compress_ctx,
224 .destroy_compress_ctx = lzo_destroy_compress_ctx,
225 .compress_pages = lzo_compress_pages,
226 .decompress_pages = lzo_decompress_pages,
227 };
228 #endif
229
230 #ifdef CONFIG_F2FS_FS_LZ4
231 static int lz4_init_compress_ctx(struct compress_ctx *cc)
232 {
233 cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
234 LZ4_MEM_COMPRESS, GFP_NOFS);
235 if (!cc->private)
236 return -ENOMEM;
237
238 cc->clen = LZ4_compressBound(PAGE_SIZE << cc->log_cluster_size);
239 return 0;
240 }
241
242 static void lz4_destroy_compress_ctx(struct compress_ctx *cc)
243 {
244 kvfree(cc->private);
245 cc->private = NULL;
246 }
247
248 static int lz4_compress_pages(struct compress_ctx *cc)
249 {
250 int len;
251
252 len = LZ4_compress_default(cc->rbuf, cc->cbuf->cdata, cc->rlen,
253 cc->clen, cc->private);
254 if (!len) {
255 printk_ratelimited("%sF2FS-fs (%s): lz4 compress failed\n",
256 KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id);
257 return -EIO;
258 }
259 cc->clen = len;
260 return 0;
261 }
262
263 static int lz4_decompress_pages(struct decompress_io_ctx *dic)
264 {
265 int ret;
266
267 ret = LZ4_decompress_safe(dic->cbuf->cdata, dic->rbuf,
268 dic->clen, dic->rlen);
269 if (ret < 0) {
270 printk_ratelimited("%sF2FS-fs (%s): lz4 decompress failed, ret:%d\n",
271 KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id, ret);
272 return -EIO;
273 }
274
275 if (ret != PAGE_SIZE << dic->log_cluster_size) {
276 printk_ratelimited("%sF2FS-fs (%s): lz4 invalid rlen:%zu, "
277 "expected:%lu\n", KERN_ERR,
278 F2FS_I_SB(dic->inode)->sb->s_id,
279 dic->rlen,
280 PAGE_SIZE << dic->log_cluster_size);
281 return -EIO;
282 }
283 return 0;
284 }
285
286 static const struct f2fs_compress_ops f2fs_lz4_ops = {
287 .init_compress_ctx = lz4_init_compress_ctx,
288 .destroy_compress_ctx = lz4_destroy_compress_ctx,
289 .compress_pages = lz4_compress_pages,
290 .decompress_pages = lz4_decompress_pages,
291 };
292 #endif
293
294 static const struct f2fs_compress_ops *f2fs_cops[COMPRESS_MAX] = {
295 #ifdef CONFIG_F2FS_FS_LZO
296 &f2fs_lzo_ops,
297 #else
298 NULL,
299 #endif
300 #ifdef CONFIG_F2FS_FS_LZ4
301 &f2fs_lz4_ops,
302 #else
303 NULL,
304 #endif
305 };
306
307 bool f2fs_is_compress_backend_ready(struct inode *inode)
308 {
309 if (!f2fs_compressed_file(inode))
310 return true;
311 return f2fs_cops[F2FS_I(inode)->i_compress_algorithm];
312 }
313
314 static struct page *f2fs_grab_page(void)
315 {
316 struct page *page;
317
318 page = alloc_page(GFP_NOFS);
319 if (!page)
320 return NULL;
321 lock_page(page);
322 return page;
323 }
324
325 static int f2fs_compress_pages(struct compress_ctx *cc)
326 {
327 struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
328 struct f2fs_inode_info *fi = F2FS_I(cc->inode);
329 const struct f2fs_compress_ops *cops =
330 f2fs_cops[fi->i_compress_algorithm];
331 unsigned int max_len, nr_cpages;
332 int i, ret;
333
334 trace_f2fs_compress_pages_start(cc->inode, cc->cluster_idx,
335 cc->cluster_size, fi->i_compress_algorithm);
336
337 ret = cops->init_compress_ctx(cc);
338 if (ret)
339 goto out;
340
341 max_len = COMPRESS_HEADER_SIZE + cc->clen;
342 cc->nr_cpages = DIV_ROUND_UP(max_len, PAGE_SIZE);
343
344 cc->cpages = f2fs_kzalloc(sbi, sizeof(struct page *) *
345 cc->nr_cpages, GFP_NOFS);
346 if (!cc->cpages) {
347 ret = -ENOMEM;
348 goto destroy_compress_ctx;
349 }
350
351 for (i = 0; i < cc->nr_cpages; i++) {
352 cc->cpages[i] = f2fs_grab_page();
353 if (!cc->cpages[i]) {
354 ret = -ENOMEM;
355 goto out_free_cpages;
356 }
357 }
358
359 cc->rbuf = vmap(cc->rpages, cc->cluster_size, VM_MAP, PAGE_KERNEL_RO);
360 if (!cc->rbuf) {
361 ret = -ENOMEM;
362 goto out_free_cpages;
363 }
364
365 cc->cbuf = vmap(cc->cpages, cc->nr_cpages, VM_MAP, PAGE_KERNEL);
366 if (!cc->cbuf) {
367 ret = -ENOMEM;
368 goto out_vunmap_rbuf;
369 }
370
371 ret = cops->compress_pages(cc);
372 if (ret)
373 goto out_vunmap_cbuf;
374
375 max_len = PAGE_SIZE * (cc->cluster_size - 1) - COMPRESS_HEADER_SIZE;
376
377 if (cc->clen > max_len) {
378 ret = -EAGAIN;
379 goto out_vunmap_cbuf;
380 }
381
382 cc->cbuf->clen = cpu_to_le32(cc->clen);
383
384 for (i = 0; i < COMPRESS_DATA_RESERVED_SIZE; i++)
385 cc->cbuf->reserved[i] = cpu_to_le32(0);
386
387 nr_cpages = DIV_ROUND_UP(cc->clen + COMPRESS_HEADER_SIZE, PAGE_SIZE);
388
389 /* zero out any unused part of the last page */
390 memset(&cc->cbuf->cdata[cc->clen], 0,
391 (nr_cpages * PAGE_SIZE) - (cc->clen + COMPRESS_HEADER_SIZE));
392
393 vunmap(cc->cbuf);
394 vunmap(cc->rbuf);
395
396 for (i = nr_cpages; i < cc->nr_cpages; i++) {
397 f2fs_put_compressed_page(cc->cpages[i]);
398 cc->cpages[i] = NULL;
399 }
400
401 cc->nr_cpages = nr_cpages;
402
403 trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
404 cc->clen, ret);
405 return 0;
406
407 out_vunmap_cbuf:
408 vunmap(cc->cbuf);
409 out_vunmap_rbuf:
410 vunmap(cc->rbuf);
411 out_free_cpages:
412 for (i = 0; i < cc->nr_cpages; i++) {
413 if (cc->cpages[i])
414 f2fs_put_compressed_page(cc->cpages[i]);
415 }
416 kfree(cc->cpages);
417 cc->cpages = NULL;
418 destroy_compress_ctx:
419 cops->destroy_compress_ctx(cc);
420 out:
421 trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
422 cc->clen, ret);
423 return ret;
424 }
425
426 void f2fs_decompress_pages(struct bio *bio, struct page *page, bool verity)
427 {
428 struct decompress_io_ctx *dic =
429 (struct decompress_io_ctx *)page_private(page);
430 struct f2fs_sb_info *sbi = F2FS_I_SB(dic->inode);
431 struct f2fs_inode_info *fi= F2FS_I(dic->inode);
432 const struct f2fs_compress_ops *cops =
433 f2fs_cops[fi->i_compress_algorithm];
434 int ret;
435
436 dec_page_count(sbi, F2FS_RD_DATA);
437
438 if (bio->bi_status || PageError(page))
439 dic->failed = true;
440
441 if (refcount_dec_not_one(&dic->ref))
442 return;
443
444 trace_f2fs_decompress_pages_start(dic->inode, dic->cluster_idx,
445 dic->cluster_size, fi->i_compress_algorithm);
446
447 /* submit partial compressed pages */
448 if (dic->failed) {
449 ret = -EIO;
450 goto out_free_dic;
451 }
452
453 dic->rbuf = vmap(dic->tpages, dic->cluster_size, VM_MAP, PAGE_KERNEL);
454 if (!dic->rbuf) {
455 ret = -ENOMEM;
456 goto out_free_dic;
457 }
458
459 dic->cbuf = vmap(dic->cpages, dic->nr_cpages, VM_MAP, PAGE_KERNEL_RO);
460 if (!dic->cbuf) {
461 ret = -ENOMEM;
462 goto out_vunmap_rbuf;
463 }
464
465 dic->clen = le32_to_cpu(dic->cbuf->clen);
466 dic->rlen = PAGE_SIZE << dic->log_cluster_size;
467
468 if (dic->clen > PAGE_SIZE * dic->nr_cpages - COMPRESS_HEADER_SIZE) {
469 ret = -EFSCORRUPTED;
470 goto out_vunmap_cbuf;
471 }
472
473 ret = cops->decompress_pages(dic);
474
475 out_vunmap_cbuf:
476 vunmap(dic->cbuf);
477 out_vunmap_rbuf:
478 vunmap(dic->rbuf);
479 out_free_dic:
480 if (!verity)
481 f2fs_decompress_end_io(dic->rpages, dic->cluster_size,
482 ret, false);
483
484 trace_f2fs_decompress_pages_end(dic->inode, dic->cluster_idx,
485 dic->clen, ret);
486 if (!verity)
487 f2fs_free_dic(dic);
488 }
489
490 static bool is_page_in_cluster(struct compress_ctx *cc, pgoff_t index)
491 {
492 if (cc->cluster_idx == NULL_CLUSTER)
493 return true;
494 return cc->cluster_idx == cluster_idx(cc, index);
495 }
496
497 bool f2fs_cluster_is_empty(struct compress_ctx *cc)
498 {
499 return cc->nr_rpages == 0;
500 }
501
502 static bool f2fs_cluster_is_full(struct compress_ctx *cc)
503 {
504 return cc->cluster_size == cc->nr_rpages;
505 }
506
507 bool f2fs_cluster_can_merge_page(struct compress_ctx *cc, pgoff_t index)
508 {
509 if (f2fs_cluster_is_empty(cc))
510 return true;
511 return is_page_in_cluster(cc, index);
512 }
513
514 static bool __cluster_may_compress(struct compress_ctx *cc)
515 {
516 struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
517 loff_t i_size = i_size_read(cc->inode);
518 unsigned nr_pages = DIV_ROUND_UP(i_size, PAGE_SIZE);
519 int i;
520
521 for (i = 0; i < cc->cluster_size; i++) {
522 struct page *page = cc->rpages[i];
523
524 f2fs_bug_on(sbi, !page);
525
526 if (unlikely(f2fs_cp_error(sbi)))
527 return false;
528 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
529 return false;
530
531 /* beyond EOF */
532 if (page->index >= nr_pages)
533 return false;
534 }
535 return true;
536 }
537
538 /* return # of compressed block addresses */
539 static int f2fs_compressed_blocks(struct compress_ctx *cc)
540 {
541 struct dnode_of_data dn;
542 int ret;
543
544 set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
545 ret = f2fs_get_dnode_of_data(&dn, start_idx_of_cluster(cc),
546 LOOKUP_NODE);
547 if (ret) {
548 if (ret == -ENOENT)
549 ret = 0;
550 goto fail;
551 }
552
553 if (dn.data_blkaddr == COMPRESS_ADDR) {
554 int i;
555
556 ret = 1;
557 for (i = 1; i < cc->cluster_size; i++) {
558 block_t blkaddr;
559
560 blkaddr = data_blkaddr(dn.inode,
561 dn.node_page, dn.ofs_in_node + i);
562 if (blkaddr != NULL_ADDR)
563 ret++;
564 }
565 }
566 fail:
567 f2fs_put_dnode(&dn);
568 return ret;
569 }
570
571 int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index)
572 {
573 struct compress_ctx cc = {
574 .inode = inode,
575 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
576 .cluster_size = F2FS_I(inode)->i_cluster_size,
577 .cluster_idx = index >> F2FS_I(inode)->i_log_cluster_size,
578 };
579
580 return f2fs_compressed_blocks(&cc);
581 }
582
583 static bool cluster_may_compress(struct compress_ctx *cc)
584 {
585 if (!f2fs_compressed_file(cc->inode))
586 return false;
587 if (f2fs_is_atomic_file(cc->inode))
588 return false;
589 if (f2fs_is_mmap_file(cc->inode))
590 return false;
591 if (!f2fs_cluster_is_full(cc))
592 return false;
593 return __cluster_may_compress(cc);
594 }
595
596 static void set_cluster_writeback(struct compress_ctx *cc)
597 {
598 int i;
599
600 for (i = 0; i < cc->cluster_size; i++) {
601 if (cc->rpages[i])
602 set_page_writeback(cc->rpages[i]);
603 }
604 }
605
606 static void set_cluster_dirty(struct compress_ctx *cc)
607 {
608 int i;
609
610 for (i = 0; i < cc->cluster_size; i++)
611 if (cc->rpages[i])
612 set_page_dirty(cc->rpages[i]);
613 }
614
615 static int prepare_compress_overwrite(struct compress_ctx *cc,
616 struct page **pagep, pgoff_t index, void **fsdata)
617 {
618 struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
619 struct address_space *mapping = cc->inode->i_mapping;
620 struct page *page;
621 struct dnode_of_data dn;
622 sector_t last_block_in_bio;
623 unsigned fgp_flag = FGP_LOCK | FGP_WRITE | FGP_CREAT;
624 pgoff_t start_idx = start_idx_of_cluster(cc);
625 int i, ret;
626 bool prealloc;
627
628 retry:
629 ret = f2fs_compressed_blocks(cc);
630 if (ret <= 0)
631 return ret;
632
633 /* compressed case */
634 prealloc = (ret < cc->cluster_size);
635
636 ret = f2fs_init_compress_ctx(cc);
637 if (ret)
638 return ret;
639
640 /* keep page reference to avoid page reclaim */
641 for (i = 0; i < cc->cluster_size; i++) {
642 page = f2fs_pagecache_get_page(mapping, start_idx + i,
643 fgp_flag, GFP_NOFS);
644 if (!page) {
645 ret = -ENOMEM;
646 goto unlock_pages;
647 }
648
649 if (PageUptodate(page))
650 unlock_page(page);
651 else
652 f2fs_compress_ctx_add_page(cc, page);
653 }
654
655 if (!f2fs_cluster_is_empty(cc)) {
656 struct bio *bio = NULL;
657
658 ret = f2fs_read_multi_pages(cc, &bio, cc->cluster_size,
659 &last_block_in_bio, false, true);
660 f2fs_destroy_compress_ctx(cc);
661 if (ret)
662 goto release_pages;
663 if (bio)
664 f2fs_submit_bio(sbi, bio, DATA);
665
666 ret = f2fs_init_compress_ctx(cc);
667 if (ret)
668 goto release_pages;
669 }
670
671 for (i = 0; i < cc->cluster_size; i++) {
672 f2fs_bug_on(sbi, cc->rpages[i]);
673
674 page = find_lock_page(mapping, start_idx + i);
675 f2fs_bug_on(sbi, !page);
676
677 f2fs_wait_on_page_writeback(page, DATA, true, true);
678
679 f2fs_compress_ctx_add_page(cc, page);
680 f2fs_put_page(page, 0);
681
682 if (!PageUptodate(page)) {
683 f2fs_unlock_rpages(cc, i + 1);
684 f2fs_put_rpages_mapping(cc, mapping, start_idx,
685 cc->cluster_size);
686 f2fs_destroy_compress_ctx(cc);
687 goto retry;
688 }
689 }
690
691 if (prealloc) {
692 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
693
694 set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
695
696 for (i = cc->cluster_size - 1; i > 0; i--) {
697 ret = f2fs_get_block(&dn, start_idx + i);
698 if (ret) {
699 i = cc->cluster_size;
700 break;
701 }
702
703 if (dn.data_blkaddr != NEW_ADDR)
704 break;
705 }
706
707 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
708 }
709
710 if (likely(!ret)) {
711 *fsdata = cc->rpages;
712 *pagep = cc->rpages[offset_in_cluster(cc, index)];
713 return cc->cluster_size;
714 }
715
716 unlock_pages:
717 f2fs_unlock_rpages(cc, i);
718 release_pages:
719 f2fs_put_rpages_mapping(cc, mapping, start_idx, i);
720 f2fs_destroy_compress_ctx(cc);
721 return ret;
722 }
723
724 int f2fs_prepare_compress_overwrite(struct inode *inode,
725 struct page **pagep, pgoff_t index, void **fsdata)
726 {
727 struct compress_ctx cc = {
728 .inode = inode,
729 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
730 .cluster_size = F2FS_I(inode)->i_cluster_size,
731 .cluster_idx = index >> F2FS_I(inode)->i_log_cluster_size,
732 .rpages = NULL,
733 .nr_rpages = 0,
734 };
735
736 return prepare_compress_overwrite(&cc, pagep, index, fsdata);
737 }
738
739 bool f2fs_compress_write_end(struct inode *inode, void *fsdata,
740 pgoff_t index, unsigned copied)
741
742 {
743 struct compress_ctx cc = {
744 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
745 .cluster_size = F2FS_I(inode)->i_cluster_size,
746 .rpages = fsdata,
747 };
748 bool first_index = (index == cc.rpages[0]->index);
749
750 if (copied)
751 set_cluster_dirty(&cc);
752
753 f2fs_put_rpages_wbc(&cc, NULL, false, 1);
754 f2fs_destroy_compress_ctx(&cc);
755
756 return first_index;
757 }
758
759 static int f2fs_write_compressed_pages(struct compress_ctx *cc,
760 int *submitted,
761 struct writeback_control *wbc,
762 enum iostat_type io_type)
763 {
764 struct inode *inode = cc->inode;
765 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
766 struct f2fs_inode_info *fi = F2FS_I(inode);
767 struct f2fs_io_info fio = {
768 .sbi = sbi,
769 .ino = cc->inode->i_ino,
770 .type = DATA,
771 .op = REQ_OP_WRITE,
772 .op_flags = wbc_to_write_flags(wbc),
773 .old_blkaddr = NEW_ADDR,
774 .page = NULL,
775 .encrypted_page = NULL,
776 .compressed_page = NULL,
777 .submitted = false,
778 .io_type = io_type,
779 .io_wbc = wbc,
780 .encrypted = f2fs_encrypted_file(cc->inode),
781 };
782 struct dnode_of_data dn;
783 struct node_info ni;
784 struct compress_io_ctx *cic;
785 pgoff_t start_idx = start_idx_of_cluster(cc);
786 unsigned int last_index = cc->cluster_size - 1;
787 loff_t psize;
788 int i, err;
789
790 if (!f2fs_trylock_op(sbi))
791 return -EAGAIN;
792
793 set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
794
795 err = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
796 if (err)
797 goto out_unlock_op;
798
799 for (i = 0; i < cc->cluster_size; i++) {
800 if (data_blkaddr(dn.inode, dn.node_page,
801 dn.ofs_in_node + i) == NULL_ADDR)
802 goto out_put_dnode;
803 }
804
805 psize = (loff_t)(cc->rpages[last_index]->index + 1) << PAGE_SHIFT;
806
807 err = f2fs_get_node_info(fio.sbi, dn.nid, &ni);
808 if (err)
809 goto out_put_dnode;
810
811 fio.version = ni.version;
812
813 cic = f2fs_kzalloc(sbi, sizeof(struct compress_io_ctx), GFP_NOFS);
814 if (!cic)
815 goto out_put_dnode;
816
817 cic->magic = F2FS_COMPRESSED_PAGE_MAGIC;
818 cic->inode = inode;
819 refcount_set(&cic->ref, 1);
820 cic->rpages = f2fs_kzalloc(sbi, sizeof(struct page *) <<
821 cc->log_cluster_size, GFP_NOFS);
822 if (!cic->rpages)
823 goto out_put_cic;
824
825 cic->nr_rpages = cc->cluster_size;
826
827 for (i = 0; i < cc->nr_cpages; i++) {
828 f2fs_set_compressed_page(cc->cpages[i], inode,
829 cc->rpages[i + 1]->index,
830 cic, i ? &cic->ref : NULL);
831 fio.compressed_page = cc->cpages[i];
832 if (fio.encrypted) {
833 fio.page = cc->rpages[i + 1];
834 err = f2fs_encrypt_one_page(&fio);
835 if (err)
836 goto out_destroy_crypt;
837 cc->cpages[i] = fio.encrypted_page;
838 }
839 }
840
841 set_cluster_writeback(cc);
842
843 for (i = 0; i < cc->cluster_size; i++)
844 cic->rpages[i] = cc->rpages[i];
845
846 for (i = 0; i < cc->cluster_size; i++, dn.ofs_in_node++) {
847 block_t blkaddr;
848
849 blkaddr = f2fs_data_blkaddr(&dn);
850 fio.page = cc->rpages[i];
851 fio.old_blkaddr = blkaddr;
852
853 /* cluster header */
854 if (i == 0) {
855 if (blkaddr == COMPRESS_ADDR)
856 fio.compr_blocks++;
857 if (__is_valid_data_blkaddr(blkaddr))
858 f2fs_invalidate_blocks(sbi, blkaddr);
859 f2fs_update_data_blkaddr(&dn, COMPRESS_ADDR);
860 goto unlock_continue;
861 }
862
863 if (fio.compr_blocks && __is_valid_data_blkaddr(blkaddr))
864 fio.compr_blocks++;
865
866 if (i > cc->nr_cpages) {
867 if (__is_valid_data_blkaddr(blkaddr)) {
868 f2fs_invalidate_blocks(sbi, blkaddr);
869 f2fs_update_data_blkaddr(&dn, NEW_ADDR);
870 }
871 goto unlock_continue;
872 }
873
874 f2fs_bug_on(fio.sbi, blkaddr == NULL_ADDR);
875
876 if (fio.encrypted)
877 fio.encrypted_page = cc->cpages[i - 1];
878 else
879 fio.compressed_page = cc->cpages[i - 1];
880
881 cc->cpages[i - 1] = NULL;
882 f2fs_outplace_write_data(&dn, &fio);
883 (*submitted)++;
884 unlock_continue:
885 inode_dec_dirty_pages(cc->inode);
886 unlock_page(fio.page);
887 }
888
889 if (fio.compr_blocks)
890 f2fs_i_compr_blocks_update(inode, fio.compr_blocks - 1, false);
891 f2fs_i_compr_blocks_update(inode, cc->nr_cpages, true);
892
893 set_inode_flag(cc->inode, FI_APPEND_WRITE);
894 if (cc->cluster_idx == 0)
895 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
896
897 f2fs_put_dnode(&dn);
898 f2fs_unlock_op(sbi);
899
900 spin_lock(&fi->i_size_lock);
901 if (fi->last_disk_size < psize)
902 fi->last_disk_size = psize;
903 spin_unlock(&fi->i_size_lock);
904
905 f2fs_put_rpages(cc);
906 f2fs_destroy_compress_ctx(cc);
907 return 0;
908
909 out_destroy_crypt:
910 kfree(cic->rpages);
911
912 for (--i; i >= 0; i--)
913 fscrypt_finalize_bounce_page(&cc->cpages[i]);
914 for (i = 0; i < cc->nr_cpages; i++) {
915 if (!cc->cpages[i])
916 continue;
917 f2fs_put_page(cc->cpages[i], 1);
918 }
919 out_put_cic:
920 kfree(cic);
921 out_put_dnode:
922 f2fs_put_dnode(&dn);
923 out_unlock_op:
924 f2fs_unlock_op(sbi);
925 return -EAGAIN;
926 }
927
928 void f2fs_compress_write_end_io(struct bio *bio, struct page *page)
929 {
930 struct f2fs_sb_info *sbi = bio->bi_private;
931 struct compress_io_ctx *cic =
932 (struct compress_io_ctx *)page_private(page);
933 int i;
934
935 if (unlikely(bio->bi_status))
936 mapping_set_error(cic->inode->i_mapping, -EIO);
937
938 f2fs_put_compressed_page(page);
939
940 dec_page_count(sbi, F2FS_WB_DATA);
941
942 if (refcount_dec_not_one(&cic->ref))
943 return;
944
945 for (i = 0; i < cic->nr_rpages; i++) {
946 WARN_ON(!cic->rpages[i]);
947 clear_cold_data(cic->rpages[i]);
948 end_page_writeback(cic->rpages[i]);
949 }
950
951 kfree(cic->rpages);
952 kfree(cic);
953 }
954
955 static int f2fs_write_raw_pages(struct compress_ctx *cc,
956 int *submitted,
957 struct writeback_control *wbc,
958 enum iostat_type io_type)
959 {
960 struct address_space *mapping = cc->inode->i_mapping;
961 int _submitted, compr_blocks, ret;
962 int i = -1, err = 0;
963
964 compr_blocks = f2fs_compressed_blocks(cc);
965 if (compr_blocks < 0) {
966 err = compr_blocks;
967 goto out_err;
968 }
969
970 for (i = 0; i < cc->cluster_size; i++) {
971 if (!cc->rpages[i])
972 continue;
973 retry_write:
974 if (cc->rpages[i]->mapping != mapping) {
975 unlock_page(cc->rpages[i]);
976 continue;
977 }
978
979 BUG_ON(!PageLocked(cc->rpages[i]));
980
981 ret = f2fs_write_single_data_page(cc->rpages[i], &_submitted,
982 NULL, NULL, wbc, io_type,
983 compr_blocks);
984 if (ret) {
985 if (ret == AOP_WRITEPAGE_ACTIVATE) {
986 unlock_page(cc->rpages[i]);
987 ret = 0;
988 } else if (ret == -EAGAIN) {
989 ret = 0;
990 cond_resched();
991 congestion_wait(BLK_RW_ASYNC,
992 DEFAULT_IO_TIMEOUT);
993 lock_page(cc->rpages[i]);
994 clear_page_dirty_for_io(cc->rpages[i]);
995 goto retry_write;
996 }
997 err = ret;
998 goto out_fail;
999 }
1000
1001 *submitted += _submitted;
1002 }
1003 return 0;
1004
1005 out_fail:
1006 /* TODO: revoke partially updated block addresses */
1007 BUG_ON(compr_blocks);
1008 out_err:
1009 for (++i; i < cc->cluster_size; i++) {
1010 if (!cc->rpages[i])
1011 continue;
1012 redirty_page_for_writepage(wbc, cc->rpages[i]);
1013 unlock_page(cc->rpages[i]);
1014 }
1015 return err;
1016 }
1017
1018 int f2fs_write_multi_pages(struct compress_ctx *cc,
1019 int *submitted,
1020 struct writeback_control *wbc,
1021 enum iostat_type io_type)
1022 {
1023 struct f2fs_inode_info *fi = F2FS_I(cc->inode);
1024 const struct f2fs_compress_ops *cops =
1025 f2fs_cops[fi->i_compress_algorithm];
1026 int err;
1027
1028 *submitted = 0;
1029 if (cluster_may_compress(cc)) {
1030 err = f2fs_compress_pages(cc);
1031 if (err == -EAGAIN) {
1032 goto write;
1033 } else if (err) {
1034 f2fs_put_rpages_wbc(cc, wbc, true, 1);
1035 goto destroy_out;
1036 }
1037
1038 err = f2fs_write_compressed_pages(cc, submitted,
1039 wbc, io_type);
1040 cops->destroy_compress_ctx(cc);
1041 if (!err)
1042 return 0;
1043 f2fs_bug_on(F2FS_I_SB(cc->inode), err != -EAGAIN);
1044 }
1045 write:
1046 f2fs_bug_on(F2FS_I_SB(cc->inode), *submitted);
1047
1048 err = f2fs_write_raw_pages(cc, submitted, wbc, io_type);
1049 f2fs_put_rpages_wbc(cc, wbc, false, 0);
1050 destroy_out:
1051 f2fs_destroy_compress_ctx(cc);
1052 return err;
1053 }
1054
1055 struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc)
1056 {
1057 struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
1058 struct decompress_io_ctx *dic;
1059 pgoff_t start_idx = start_idx_of_cluster(cc);
1060 int i;
1061
1062 dic = f2fs_kzalloc(sbi, sizeof(struct decompress_io_ctx), GFP_NOFS);
1063 if (!dic)
1064 return ERR_PTR(-ENOMEM);
1065
1066 dic->rpages = f2fs_kzalloc(sbi, sizeof(struct page *) <<
1067 cc->log_cluster_size, GFP_NOFS);
1068 if (!dic->rpages) {
1069 kfree(dic);
1070 return ERR_PTR(-ENOMEM);
1071 }
1072
1073 dic->magic = F2FS_COMPRESSED_PAGE_MAGIC;
1074 dic->inode = cc->inode;
1075 refcount_set(&dic->ref, 1);
1076 dic->cluster_idx = cc->cluster_idx;
1077 dic->cluster_size = cc->cluster_size;
1078 dic->log_cluster_size = cc->log_cluster_size;
1079 dic->nr_cpages = cc->nr_cpages;
1080 dic->failed = false;
1081
1082 for (i = 0; i < dic->cluster_size; i++)
1083 dic->rpages[i] = cc->rpages[i];
1084 dic->nr_rpages = cc->cluster_size;
1085
1086 dic->cpages = f2fs_kzalloc(sbi, sizeof(struct page *) *
1087 dic->nr_cpages, GFP_NOFS);
1088 if (!dic->cpages)
1089 goto out_free;
1090
1091 for (i = 0; i < dic->nr_cpages; i++) {
1092 struct page *page;
1093
1094 page = f2fs_grab_page();
1095 if (!page)
1096 goto out_free;
1097
1098 f2fs_set_compressed_page(page, cc->inode,
1099 start_idx + i + 1,
1100 dic, i ? &dic->ref : NULL);
1101 dic->cpages[i] = page;
1102 }
1103
1104 dic->tpages = f2fs_kzalloc(sbi, sizeof(struct page *) *
1105 dic->cluster_size, GFP_NOFS);
1106 if (!dic->tpages)
1107 goto out_free;
1108
1109 for (i = 0; i < dic->cluster_size; i++) {
1110 if (cc->rpages[i])
1111 continue;
1112
1113 dic->tpages[i] = f2fs_grab_page();
1114 if (!dic->tpages[i])
1115 goto out_free;
1116 }
1117
1118 for (i = 0; i < dic->cluster_size; i++) {
1119 if (dic->tpages[i])
1120 continue;
1121 dic->tpages[i] = cc->rpages[i];
1122 }
1123
1124 return dic;
1125
1126 out_free:
1127 f2fs_free_dic(dic);
1128 return ERR_PTR(-ENOMEM);
1129 }
1130
1131 void f2fs_free_dic(struct decompress_io_ctx *dic)
1132 {
1133 int i;
1134
1135 if (dic->tpages) {
1136 for (i = 0; i < dic->cluster_size; i++) {
1137 if (dic->rpages[i])
1138 continue;
1139 unlock_page(dic->tpages[i]);
1140 put_page(dic->tpages[i]);
1141 }
1142 kfree(dic->tpages);
1143 }
1144
1145 if (dic->cpages) {
1146 for (i = 0; i < dic->nr_cpages; i++) {
1147 if (!dic->cpages[i])
1148 continue;
1149 f2fs_put_compressed_page(dic->cpages[i]);
1150 }
1151 kfree(dic->cpages);
1152 }
1153
1154 kfree(dic->rpages);
1155 kfree(dic);
1156 }
1157
1158 void f2fs_decompress_end_io(struct page **rpages,
1159 unsigned int cluster_size, bool err, bool verity)
1160 {
1161 int i;
1162
1163 for (i = 0; i < cluster_size; i++) {
1164 struct page *rpage = rpages[i];
1165
1166 if (!rpage)
1167 continue;
1168
1169 if (err || PageError(rpage)) {
1170 ClearPageUptodate(rpage);
1171 ClearPageError(rpage);
1172 } else {
1173 if (!verity || fsverity_verify_page(rpage))
1174 SetPageUptodate(rpage);
1175 else
1176 SetPageError(rpage);
1177 }
1178 unlock_page(rpage);
1179 }
1180 }