Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * fs/f2fs/data.c
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
7 */
8#include <linux/fs.h>
9#include <linux/f2fs_fs.h>
10#include <linux/sched/mm.h>
11#include <linux/mpage.h>
12#include <linux/writeback.h>
13#include <linux/pagevec.h>
14#include <linux/blkdev.h>
15#include <linux/bio.h>
16#include <linux/blk-crypto.h>
17#include <linux/swap.h>
18#include <linux/prefetch.h>
19#include <linux/uio.h>
20#include <linux/sched/signal.h>
21#include <linux/fiemap.h>
22#include <linux/iomap.h>
23
24#include "f2fs.h"
25#include "node.h"
26#include "segment.h"
27#include "iostat.h"
28#include <trace/events/f2fs.h>
29
30#define NUM_PREALLOC_POST_READ_CTXS 128
31
32static struct kmem_cache *bio_post_read_ctx_cache;
33static struct kmem_cache *bio_entry_slab;
34static mempool_t *bio_post_read_ctx_pool;
35static struct bio_set f2fs_bioset;
36
37#define F2FS_BIO_POOL_SIZE NR_CURSEG_TYPE
38
39int __init f2fs_init_bioset(void)
40{
41 return bioset_init(&f2fs_bioset, F2FS_BIO_POOL_SIZE,
42 0, BIOSET_NEED_BVECS);
43}
44
45void f2fs_destroy_bioset(void)
46{
47 bioset_exit(&f2fs_bioset);
48}
49
50bool f2fs_is_cp_guaranteed(struct page *page)
51{
52 struct address_space *mapping = page->mapping;
53 struct inode *inode;
54 struct f2fs_sb_info *sbi;
55
56 if (!mapping)
57 return false;
58
59 inode = mapping->host;
60 sbi = F2FS_I_SB(inode);
61
62 if (inode->i_ino == F2FS_META_INO(sbi) ||
63 inode->i_ino == F2FS_NODE_INO(sbi) ||
64 S_ISDIR(inode->i_mode))
65 return true;
66
67 if ((S_ISREG(inode->i_mode) && IS_NOQUOTA(inode)) ||
68 page_private_gcing(page))
69 return true;
70 return false;
71}
72
73static enum count_type __read_io_type(struct folio *folio)
74{
75 struct address_space *mapping = folio->mapping;
76
77 if (mapping) {
78 struct inode *inode = mapping->host;
79 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
80
81 if (inode->i_ino == F2FS_META_INO(sbi))
82 return F2FS_RD_META;
83
84 if (inode->i_ino == F2FS_NODE_INO(sbi))
85 return F2FS_RD_NODE;
86 }
87 return F2FS_RD_DATA;
88}
89
90/* postprocessing steps for read bios */
91enum bio_post_read_step {
92#ifdef CONFIG_FS_ENCRYPTION
93 STEP_DECRYPT = BIT(0),
94#else
95 STEP_DECRYPT = 0, /* compile out the decryption-related code */
96#endif
97#ifdef CONFIG_F2FS_FS_COMPRESSION
98 STEP_DECOMPRESS = BIT(1),
99#else
100 STEP_DECOMPRESS = 0, /* compile out the decompression-related code */
101#endif
102#ifdef CONFIG_FS_VERITY
103 STEP_VERITY = BIT(2),
104#else
105 STEP_VERITY = 0, /* compile out the verity-related code */
106#endif
107};
108
109struct bio_post_read_ctx {
110 struct bio *bio;
111 struct f2fs_sb_info *sbi;
112 struct work_struct work;
113 unsigned int enabled_steps;
114 /*
115 * decompression_attempted keeps track of whether
116 * f2fs_end_read_compressed_page() has been called on the pages in the
117 * bio that belong to a compressed cluster yet.
118 */
119 bool decompression_attempted;
120 block_t fs_blkaddr;
121};
122
123/*
124 * Update and unlock a bio's pages, and free the bio.
125 *
126 * This marks pages up-to-date only if there was no error in the bio (I/O error,
127 * decryption error, or verity error), as indicated by bio->bi_status.
128 *
129 * "Compressed pages" (pagecache pages backed by a compressed cluster on-disk)
130 * aren't marked up-to-date here, as decompression is done on a per-compression-
131 * cluster basis rather than a per-bio basis. Instead, we only must do two
132 * things for each compressed page here: call f2fs_end_read_compressed_page()
133 * with failed=true if an error occurred before it would have normally gotten
134 * called (i.e., I/O error or decryption error, but *not* verity error), and
135 * release the bio's reference to the decompress_io_ctx of the page's cluster.
136 */
137static void f2fs_finish_read_bio(struct bio *bio, bool in_task)
138{
139 struct folio_iter fi;
140 struct bio_post_read_ctx *ctx = bio->bi_private;
141
142 bio_for_each_folio_all(fi, bio) {
143 struct folio *folio = fi.folio;
144
145 if (f2fs_is_compressed_page(&folio->page)) {
146 if (ctx && !ctx->decompression_attempted)
147 f2fs_end_read_compressed_page(&folio->page, true, 0,
148 in_task);
149 f2fs_put_page_dic(&folio->page, in_task);
150 continue;
151 }
152
153 dec_page_count(F2FS_F_SB(folio), __read_io_type(folio));
154 folio_end_read(folio, bio->bi_status == 0);
155 }
156
157 if (ctx)
158 mempool_free(ctx, bio_post_read_ctx_pool);
159 bio_put(bio);
160}
161
162static void f2fs_verify_bio(struct work_struct *work)
163{
164 struct bio_post_read_ctx *ctx =
165 container_of(work, struct bio_post_read_ctx, work);
166 struct bio *bio = ctx->bio;
167 bool may_have_compressed_pages = (ctx->enabled_steps & STEP_DECOMPRESS);
168
169 /*
170 * fsverity_verify_bio() may call readahead() again, and while verity
171 * will be disabled for this, decryption and/or decompression may still
172 * be needed, resulting in another bio_post_read_ctx being allocated.
173 * So to prevent deadlocks we need to release the current ctx to the
174 * mempool first. This assumes that verity is the last post-read step.
175 */
176 mempool_free(ctx, bio_post_read_ctx_pool);
177 bio->bi_private = NULL;
178
179 /*
180 * Verify the bio's pages with fs-verity. Exclude compressed pages,
181 * as those were handled separately by f2fs_end_read_compressed_page().
182 */
183 if (may_have_compressed_pages) {
184 struct bio_vec *bv;
185 struct bvec_iter_all iter_all;
186
187 bio_for_each_segment_all(bv, bio, iter_all) {
188 struct page *page = bv->bv_page;
189
190 if (!f2fs_is_compressed_page(page) &&
191 !fsverity_verify_page(page)) {
192 bio->bi_status = BLK_STS_IOERR;
193 break;
194 }
195 }
196 } else {
197 fsverity_verify_bio(bio);
198 }
199
200 f2fs_finish_read_bio(bio, true);
201}
202
203/*
204 * If the bio's data needs to be verified with fs-verity, then enqueue the
205 * verity work for the bio. Otherwise finish the bio now.
206 *
207 * Note that to avoid deadlocks, the verity work can't be done on the
208 * decryption/decompression workqueue. This is because verifying the data pages
209 * can involve reading verity metadata pages from the file, and these verity
210 * metadata pages may be encrypted and/or compressed.
211 */
212static void f2fs_verify_and_finish_bio(struct bio *bio, bool in_task)
213{
214 struct bio_post_read_ctx *ctx = bio->bi_private;
215
216 if (ctx && (ctx->enabled_steps & STEP_VERITY)) {
217 INIT_WORK(&ctx->work, f2fs_verify_bio);
218 fsverity_enqueue_verify_work(&ctx->work);
219 } else {
220 f2fs_finish_read_bio(bio, in_task);
221 }
222}
223
224/*
225 * Handle STEP_DECOMPRESS by decompressing any compressed clusters whose last
226 * remaining page was read by @ctx->bio.
227 *
228 * Note that a bio may span clusters (even a mix of compressed and uncompressed
229 * clusters) or be for just part of a cluster. STEP_DECOMPRESS just indicates
230 * that the bio includes at least one compressed page. The actual decompression
231 * is done on a per-cluster basis, not a per-bio basis.
232 */
233static void f2fs_handle_step_decompress(struct bio_post_read_ctx *ctx,
234 bool in_task)
235{
236 struct bio_vec *bv;
237 struct bvec_iter_all iter_all;
238 bool all_compressed = true;
239 block_t blkaddr = ctx->fs_blkaddr;
240
241 bio_for_each_segment_all(bv, ctx->bio, iter_all) {
242 struct page *page = bv->bv_page;
243
244 if (f2fs_is_compressed_page(page))
245 f2fs_end_read_compressed_page(page, false, blkaddr,
246 in_task);
247 else
248 all_compressed = false;
249
250 blkaddr++;
251 }
252
253 ctx->decompression_attempted = true;
254
255 /*
256 * Optimization: if all the bio's pages are compressed, then scheduling
257 * the per-bio verity work is unnecessary, as verity will be fully
258 * handled at the compression cluster level.
259 */
260 if (all_compressed)
261 ctx->enabled_steps &= ~STEP_VERITY;
262}
263
264static void f2fs_post_read_work(struct work_struct *work)
265{
266 struct bio_post_read_ctx *ctx =
267 container_of(work, struct bio_post_read_ctx, work);
268 struct bio *bio = ctx->bio;
269
270 if ((ctx->enabled_steps & STEP_DECRYPT) && !fscrypt_decrypt_bio(bio)) {
271 f2fs_finish_read_bio(bio, true);
272 return;
273 }
274
275 if (ctx->enabled_steps & STEP_DECOMPRESS)
276 f2fs_handle_step_decompress(ctx, true);
277
278 f2fs_verify_and_finish_bio(bio, true);
279}
280
281static void f2fs_read_end_io(struct bio *bio)
282{
283 struct f2fs_sb_info *sbi = F2FS_P_SB(bio_first_page_all(bio));
284 struct bio_post_read_ctx *ctx;
285 bool intask = in_task();
286
287 iostat_update_and_unbind_ctx(bio);
288 ctx = bio->bi_private;
289
290 if (time_to_inject(sbi, FAULT_READ_IO))
291 bio->bi_status = BLK_STS_IOERR;
292
293 if (bio->bi_status) {
294 f2fs_finish_read_bio(bio, intask);
295 return;
296 }
297
298 if (ctx) {
299 unsigned int enabled_steps = ctx->enabled_steps &
300 (STEP_DECRYPT | STEP_DECOMPRESS);
301
302 /*
303 * If we have only decompression step between decompression and
304 * decrypt, we don't need post processing for this.
305 */
306 if (enabled_steps == STEP_DECOMPRESS &&
307 !f2fs_low_mem_mode(sbi)) {
308 f2fs_handle_step_decompress(ctx, intask);
309 } else if (enabled_steps) {
310 INIT_WORK(&ctx->work, f2fs_post_read_work);
311 queue_work(ctx->sbi->post_read_wq, &ctx->work);
312 return;
313 }
314 }
315
316 f2fs_verify_and_finish_bio(bio, intask);
317}
318
319static void f2fs_write_end_io(struct bio *bio)
320{
321 struct f2fs_sb_info *sbi;
322 struct bio_vec *bvec;
323 struct bvec_iter_all iter_all;
324
325 iostat_update_and_unbind_ctx(bio);
326 sbi = bio->bi_private;
327
328 if (time_to_inject(sbi, FAULT_WRITE_IO))
329 bio->bi_status = BLK_STS_IOERR;
330
331 bio_for_each_segment_all(bvec, bio, iter_all) {
332 struct page *page = bvec->bv_page;
333 enum count_type type = WB_DATA_TYPE(page, false);
334
335 fscrypt_finalize_bounce_page(&page);
336
337#ifdef CONFIG_F2FS_FS_COMPRESSION
338 if (f2fs_is_compressed_page(page)) {
339 f2fs_compress_write_end_io(bio, page);
340 continue;
341 }
342#endif
343
344 if (unlikely(bio->bi_status)) {
345 mapping_set_error(page->mapping, -EIO);
346 if (type == F2FS_WB_CP_DATA)
347 f2fs_stop_checkpoint(sbi, true,
348 STOP_CP_REASON_WRITE_FAIL);
349 }
350
351 f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) &&
352 page_folio(page)->index != nid_of_node(page));
353
354 dec_page_count(sbi, type);
355 if (f2fs_in_warm_node_list(sbi, page))
356 f2fs_del_fsync_node_entry(sbi, page);
357 clear_page_private_gcing(page);
358 end_page_writeback(page);
359 }
360 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
361 wq_has_sleeper(&sbi->cp_wait))
362 wake_up(&sbi->cp_wait);
363
364 bio_put(bio);
365}
366
367#ifdef CONFIG_BLK_DEV_ZONED
368static void f2fs_zone_write_end_io(struct bio *bio)
369{
370 struct f2fs_bio_info *io = (struct f2fs_bio_info *)bio->bi_private;
371
372 bio->bi_private = io->bi_private;
373 complete(&io->zone_wait);
374 f2fs_write_end_io(bio);
375}
376#endif
377
378struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
379 block_t blk_addr, sector_t *sector)
380{
381 struct block_device *bdev = sbi->sb->s_bdev;
382 int i;
383
384 if (f2fs_is_multi_device(sbi)) {
385 for (i = 0; i < sbi->s_ndevs; i++) {
386 if (FDEV(i).start_blk <= blk_addr &&
387 FDEV(i).end_blk >= blk_addr) {
388 blk_addr -= FDEV(i).start_blk;
389 bdev = FDEV(i).bdev;
390 break;
391 }
392 }
393 }
394
395 if (sector)
396 *sector = SECTOR_FROM_BLOCK(blk_addr);
397 return bdev;
398}
399
400int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
401{
402 int i;
403
404 if (!f2fs_is_multi_device(sbi))
405 return 0;
406
407 for (i = 0; i < sbi->s_ndevs; i++)
408 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
409 return i;
410 return 0;
411}
412
413static blk_opf_t f2fs_io_flags(struct f2fs_io_info *fio)
414{
415 unsigned int temp_mask = GENMASK(NR_TEMP_TYPE - 1, 0);
416 unsigned int fua_flag, meta_flag, io_flag;
417 blk_opf_t op_flags = 0;
418
419 if (fio->op != REQ_OP_WRITE)
420 return 0;
421 if (fio->type == DATA)
422 io_flag = fio->sbi->data_io_flag;
423 else if (fio->type == NODE)
424 io_flag = fio->sbi->node_io_flag;
425 else
426 return 0;
427
428 fua_flag = io_flag & temp_mask;
429 meta_flag = (io_flag >> NR_TEMP_TYPE) & temp_mask;
430
431 /*
432 * data/node io flag bits per temp:
433 * REQ_META | REQ_FUA |
434 * 5 | 4 | 3 | 2 | 1 | 0 |
435 * Cold | Warm | Hot | Cold | Warm | Hot |
436 */
437 if (BIT(fio->temp) & meta_flag)
438 op_flags |= REQ_META;
439 if (BIT(fio->temp) & fua_flag)
440 op_flags |= REQ_FUA;
441 return op_flags;
442}
443
444static struct bio *__bio_alloc(struct f2fs_io_info *fio, int npages)
445{
446 struct f2fs_sb_info *sbi = fio->sbi;
447 struct block_device *bdev;
448 sector_t sector;
449 struct bio *bio;
450
451 bdev = f2fs_target_device(sbi, fio->new_blkaddr, §or);
452 bio = bio_alloc_bioset(bdev, npages,
453 fio->op | fio->op_flags | f2fs_io_flags(fio),
454 GFP_NOIO, &f2fs_bioset);
455 bio->bi_iter.bi_sector = sector;
456 if (is_read_io(fio->op)) {
457 bio->bi_end_io = f2fs_read_end_io;
458 bio->bi_private = NULL;
459 } else {
460 bio->bi_end_io = f2fs_write_end_io;
461 bio->bi_private = sbi;
462 bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi,
463 fio->type, fio->temp);
464 }
465 iostat_alloc_and_bind_ctx(sbi, bio, NULL);
466
467 if (fio->io_wbc)
468 wbc_init_bio(fio->io_wbc, bio);
469
470 return bio;
471}
472
473static void f2fs_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode,
474 pgoff_t first_idx,
475 const struct f2fs_io_info *fio,
476 gfp_t gfp_mask)
477{
478 /*
479 * The f2fs garbage collector sets ->encrypted_page when it wants to
480 * read/write raw data without encryption.
481 */
482 if (!fio || !fio->encrypted_page)
483 fscrypt_set_bio_crypt_ctx(bio, inode, first_idx, gfp_mask);
484}
485
486static bool f2fs_crypt_mergeable_bio(struct bio *bio, const struct inode *inode,
487 pgoff_t next_idx,
488 const struct f2fs_io_info *fio)
489{
490 /*
491 * The f2fs garbage collector sets ->encrypted_page when it wants to
492 * read/write raw data without encryption.
493 */
494 if (fio && fio->encrypted_page)
495 return !bio_has_crypt_ctx(bio);
496
497 return fscrypt_mergeable_bio(bio, inode, next_idx);
498}
499
500void f2fs_submit_read_bio(struct f2fs_sb_info *sbi, struct bio *bio,
501 enum page_type type)
502{
503 WARN_ON_ONCE(!is_read_io(bio_op(bio)));
504 trace_f2fs_submit_read_bio(sbi->sb, type, bio);
505
506 iostat_update_submit_ctx(bio, type);
507 submit_bio(bio);
508}
509
510static void f2fs_submit_write_bio(struct f2fs_sb_info *sbi, struct bio *bio,
511 enum page_type type)
512{
513 WARN_ON_ONCE(is_read_io(bio_op(bio)));
514 trace_f2fs_submit_write_bio(sbi->sb, type, bio);
515 iostat_update_submit_ctx(bio, type);
516 submit_bio(bio);
517}
518
519static void __submit_merged_bio(struct f2fs_bio_info *io)
520{
521 struct f2fs_io_info *fio = &io->fio;
522
523 if (!io->bio)
524 return;
525
526 if (is_read_io(fio->op)) {
527 trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
528 f2fs_submit_read_bio(io->sbi, io->bio, fio->type);
529 } else {
530 trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
531 f2fs_submit_write_bio(io->sbi, io->bio, fio->type);
532 }
533 io->bio = NULL;
534}
535
536static bool __has_merged_page(struct bio *bio, struct inode *inode,
537 struct page *page, nid_t ino)
538{
539 struct bio_vec *bvec;
540 struct bvec_iter_all iter_all;
541
542 if (!bio)
543 return false;
544
545 if (!inode && !page && !ino)
546 return true;
547
548 bio_for_each_segment_all(bvec, bio, iter_all) {
549 struct page *target = bvec->bv_page;
550
551 if (fscrypt_is_bounce_page(target)) {
552 target = fscrypt_pagecache_page(target);
553 if (IS_ERR(target))
554 continue;
555 }
556 if (f2fs_is_compressed_page(target)) {
557 target = f2fs_compress_control_page(target);
558 if (IS_ERR(target))
559 continue;
560 }
561
562 if (inode && inode == target->mapping->host)
563 return true;
564 if (page && page == target)
565 return true;
566 if (ino && ino == ino_of_node(target))
567 return true;
568 }
569
570 return false;
571}
572
573int f2fs_init_write_merge_io(struct f2fs_sb_info *sbi)
574{
575 int i;
576
577 for (i = 0; i < NR_PAGE_TYPE; i++) {
578 int n = (i == META) ? 1 : NR_TEMP_TYPE;
579 int j;
580
581 sbi->write_io[i] = f2fs_kmalloc(sbi,
582 array_size(n, sizeof(struct f2fs_bio_info)),
583 GFP_KERNEL);
584 if (!sbi->write_io[i])
585 return -ENOMEM;
586
587 for (j = HOT; j < n; j++) {
588 struct f2fs_bio_info *io = &sbi->write_io[i][j];
589
590 init_f2fs_rwsem(&io->io_rwsem);
591 io->sbi = sbi;
592 io->bio = NULL;
593 io->last_block_in_bio = 0;
594 spin_lock_init(&io->io_lock);
595 INIT_LIST_HEAD(&io->io_list);
596 INIT_LIST_HEAD(&io->bio_list);
597 init_f2fs_rwsem(&io->bio_list_lock);
598#ifdef CONFIG_BLK_DEV_ZONED
599 init_completion(&io->zone_wait);
600 io->zone_pending_bio = NULL;
601 io->bi_private = NULL;
602#endif
603 }
604 }
605
606 return 0;
607}
608
609static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
610 enum page_type type, enum temp_type temp)
611{
612 enum page_type btype = PAGE_TYPE_OF_BIO(type);
613 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
614
615 f2fs_down_write(&io->io_rwsem);
616
617 if (!io->bio)
618 goto unlock_out;
619
620 /* change META to META_FLUSH in the checkpoint procedure */
621 if (type >= META_FLUSH) {
622 io->fio.type = META_FLUSH;
623 io->bio->bi_opf |= REQ_META | REQ_PRIO | REQ_SYNC;
624 if (!test_opt(sbi, NOBARRIER))
625 io->bio->bi_opf |= REQ_PREFLUSH | REQ_FUA;
626 }
627 __submit_merged_bio(io);
628unlock_out:
629 f2fs_up_write(&io->io_rwsem);
630}
631
632static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
633 struct inode *inode, struct page *page,
634 nid_t ino, enum page_type type, bool force)
635{
636 enum temp_type temp;
637 bool ret = true;
638
639 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
640 if (!force) {
641 enum page_type btype = PAGE_TYPE_OF_BIO(type);
642 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
643
644 f2fs_down_read(&io->io_rwsem);
645 ret = __has_merged_page(io->bio, inode, page, ino);
646 f2fs_up_read(&io->io_rwsem);
647 }
648 if (ret)
649 __f2fs_submit_merged_write(sbi, type, temp);
650
651 /* TODO: use HOT temp only for meta pages now. */
652 if (type >= META)
653 break;
654 }
655}
656
657void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
658{
659 __submit_merged_write_cond(sbi, NULL, NULL, 0, type, true);
660}
661
662void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
663 struct inode *inode, struct page *page,
664 nid_t ino, enum page_type type)
665{
666 __submit_merged_write_cond(sbi, inode, page, ino, type, false);
667}
668
669void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
670{
671 f2fs_submit_merged_write(sbi, DATA);
672 f2fs_submit_merged_write(sbi, NODE);
673 f2fs_submit_merged_write(sbi, META);
674}
675
676/*
677 * Fill the locked page with data located in the block address.
678 * A caller needs to unlock the page on failure.
679 */
680int f2fs_submit_page_bio(struct f2fs_io_info *fio)
681{
682 struct bio *bio;
683 struct folio *fio_folio = page_folio(fio->page);
684 struct folio *data_folio = fio->encrypted_page ?
685 page_folio(fio->encrypted_page) : fio_folio;
686
687 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
688 fio->is_por ? META_POR : (__is_meta_io(fio) ?
689 META_GENERIC : DATA_GENERIC_ENHANCE)))
690 return -EFSCORRUPTED;
691
692 trace_f2fs_submit_folio_bio(data_folio, fio);
693
694 /* Allocate a new bio */
695 bio = __bio_alloc(fio, 1);
696
697 f2fs_set_bio_crypt_ctx(bio, fio_folio->mapping->host,
698 fio_folio->index, fio, GFP_NOIO);
699 bio_add_folio_nofail(bio, data_folio, folio_size(data_folio), 0);
700
701 if (fio->io_wbc && !is_read_io(fio->op))
702 wbc_account_cgroup_owner(fio->io_wbc, fio_folio, PAGE_SIZE);
703
704 inc_page_count(fio->sbi, is_read_io(fio->op) ?
705 __read_io_type(data_folio) : WB_DATA_TYPE(fio->page, false));
706
707 if (is_read_io(bio_op(bio)))
708 f2fs_submit_read_bio(fio->sbi, bio, fio->type);
709 else
710 f2fs_submit_write_bio(fio->sbi, bio, fio->type);
711 return 0;
712}
713
714static bool page_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
715 block_t last_blkaddr, block_t cur_blkaddr)
716{
717 if (unlikely(sbi->max_io_bytes &&
718 bio->bi_iter.bi_size >= sbi->max_io_bytes))
719 return false;
720 if (last_blkaddr + 1 != cur_blkaddr)
721 return false;
722 return bio->bi_bdev == f2fs_target_device(sbi, cur_blkaddr, NULL);
723}
724
725static bool io_type_is_mergeable(struct f2fs_bio_info *io,
726 struct f2fs_io_info *fio)
727{
728 if (io->fio.op != fio->op)
729 return false;
730 return io->fio.op_flags == fio->op_flags;
731}
732
733static bool io_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
734 struct f2fs_bio_info *io,
735 struct f2fs_io_info *fio,
736 block_t last_blkaddr,
737 block_t cur_blkaddr)
738{
739 if (!page_is_mergeable(sbi, bio, last_blkaddr, cur_blkaddr))
740 return false;
741 return io_type_is_mergeable(io, fio);
742}
743
744static void add_bio_entry(struct f2fs_sb_info *sbi, struct bio *bio,
745 struct page *page, enum temp_type temp)
746{
747 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
748 struct bio_entry *be;
749
750 be = f2fs_kmem_cache_alloc(bio_entry_slab, GFP_NOFS, true, NULL);
751 be->bio = bio;
752 bio_get(bio);
753
754 if (bio_add_page(bio, page, PAGE_SIZE, 0) != PAGE_SIZE)
755 f2fs_bug_on(sbi, 1);
756
757 f2fs_down_write(&io->bio_list_lock);
758 list_add_tail(&be->list, &io->bio_list);
759 f2fs_up_write(&io->bio_list_lock);
760}
761
762static void del_bio_entry(struct bio_entry *be)
763{
764 list_del(&be->list);
765 kmem_cache_free(bio_entry_slab, be);
766}
767
768static int add_ipu_page(struct f2fs_io_info *fio, struct bio **bio,
769 struct page *page)
770{
771 struct f2fs_sb_info *sbi = fio->sbi;
772 enum temp_type temp;
773 bool found = false;
774 int ret = -EAGAIN;
775
776 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
777 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
778 struct list_head *head = &io->bio_list;
779 struct bio_entry *be;
780
781 f2fs_down_write(&io->bio_list_lock);
782 list_for_each_entry(be, head, list) {
783 if (be->bio != *bio)
784 continue;
785
786 found = true;
787
788 f2fs_bug_on(sbi, !page_is_mergeable(sbi, *bio,
789 *fio->last_block,
790 fio->new_blkaddr));
791 if (f2fs_crypt_mergeable_bio(*bio,
792 fio->page->mapping->host,
793 page_folio(fio->page)->index, fio) &&
794 bio_add_page(*bio, page, PAGE_SIZE, 0) ==
795 PAGE_SIZE) {
796 ret = 0;
797 break;
798 }
799
800 /* page can't be merged into bio; submit the bio */
801 del_bio_entry(be);
802 f2fs_submit_write_bio(sbi, *bio, DATA);
803 break;
804 }
805 f2fs_up_write(&io->bio_list_lock);
806 }
807
808 if (ret) {
809 bio_put(*bio);
810 *bio = NULL;
811 }
812
813 return ret;
814}
815
816void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi,
817 struct bio **bio, struct page *page)
818{
819 enum temp_type temp;
820 bool found = false;
821 struct bio *target = bio ? *bio : NULL;
822
823 f2fs_bug_on(sbi, !target && !page);
824
825 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
826 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
827 struct list_head *head = &io->bio_list;
828 struct bio_entry *be;
829
830 if (list_empty(head))
831 continue;
832
833 f2fs_down_read(&io->bio_list_lock);
834 list_for_each_entry(be, head, list) {
835 if (target)
836 found = (target == be->bio);
837 else
838 found = __has_merged_page(be->bio, NULL,
839 page, 0);
840 if (found)
841 break;
842 }
843 f2fs_up_read(&io->bio_list_lock);
844
845 if (!found)
846 continue;
847
848 found = false;
849
850 f2fs_down_write(&io->bio_list_lock);
851 list_for_each_entry(be, head, list) {
852 if (target)
853 found = (target == be->bio);
854 else
855 found = __has_merged_page(be->bio, NULL,
856 page, 0);
857 if (found) {
858 target = be->bio;
859 del_bio_entry(be);
860 break;
861 }
862 }
863 f2fs_up_write(&io->bio_list_lock);
864 }
865
866 if (found)
867 f2fs_submit_write_bio(sbi, target, DATA);
868 if (bio && *bio) {
869 bio_put(*bio);
870 *bio = NULL;
871 }
872}
873
874int f2fs_merge_page_bio(struct f2fs_io_info *fio)
875{
876 struct bio *bio = *fio->bio;
877 struct page *page = fio->encrypted_page ?
878 fio->encrypted_page : fio->page;
879
880 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
881 __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC))
882 return -EFSCORRUPTED;
883
884 trace_f2fs_submit_folio_bio(page_folio(page), fio);
885
886 if (bio && !page_is_mergeable(fio->sbi, bio, *fio->last_block,
887 fio->new_blkaddr))
888 f2fs_submit_merged_ipu_write(fio->sbi, &bio, NULL);
889alloc_new:
890 if (!bio) {
891 bio = __bio_alloc(fio, BIO_MAX_VECS);
892 f2fs_set_bio_crypt_ctx(bio, fio->page->mapping->host,
893 page_folio(fio->page)->index, fio, GFP_NOIO);
894
895 add_bio_entry(fio->sbi, bio, page, fio->temp);
896 } else {
897 if (add_ipu_page(fio, &bio, page))
898 goto alloc_new;
899 }
900
901 if (fio->io_wbc)
902 wbc_account_cgroup_owner(fio->io_wbc, page_folio(fio->page),
903 PAGE_SIZE);
904
905 inc_page_count(fio->sbi, WB_DATA_TYPE(page, false));
906
907 *fio->last_block = fio->new_blkaddr;
908 *fio->bio = bio;
909
910 return 0;
911}
912
913#ifdef CONFIG_BLK_DEV_ZONED
914static bool is_end_zone_blkaddr(struct f2fs_sb_info *sbi, block_t blkaddr)
915{
916 struct block_device *bdev = sbi->sb->s_bdev;
917 int devi = 0;
918
919 if (f2fs_is_multi_device(sbi)) {
920 devi = f2fs_target_device_index(sbi, blkaddr);
921 if (blkaddr < FDEV(devi).start_blk ||
922 blkaddr > FDEV(devi).end_blk) {
923 f2fs_err(sbi, "Invalid block %x", blkaddr);
924 return false;
925 }
926 blkaddr -= FDEV(devi).start_blk;
927 bdev = FDEV(devi).bdev;
928 }
929 return bdev_is_zoned(bdev) &&
930 f2fs_blkz_is_seq(sbi, devi, blkaddr) &&
931 (blkaddr % sbi->blocks_per_blkz == sbi->blocks_per_blkz - 1);
932}
933#endif
934
935void f2fs_submit_page_write(struct f2fs_io_info *fio)
936{
937 struct f2fs_sb_info *sbi = fio->sbi;
938 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
939 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
940 struct page *bio_page;
941 enum count_type type;
942
943 f2fs_bug_on(sbi, is_read_io(fio->op));
944
945 f2fs_down_write(&io->io_rwsem);
946next:
947#ifdef CONFIG_BLK_DEV_ZONED
948 if (f2fs_sb_has_blkzoned(sbi) && btype < META && io->zone_pending_bio) {
949 wait_for_completion_io(&io->zone_wait);
950 bio_put(io->zone_pending_bio);
951 io->zone_pending_bio = NULL;
952 io->bi_private = NULL;
953 }
954#endif
955
956 if (fio->in_list) {
957 spin_lock(&io->io_lock);
958 if (list_empty(&io->io_list)) {
959 spin_unlock(&io->io_lock);
960 goto out;
961 }
962 fio = list_first_entry(&io->io_list,
963 struct f2fs_io_info, list);
964 list_del(&fio->list);
965 spin_unlock(&io->io_lock);
966 }
967
968 verify_fio_blkaddr(fio);
969
970 if (fio->encrypted_page)
971 bio_page = fio->encrypted_page;
972 else if (fio->compressed_page)
973 bio_page = fio->compressed_page;
974 else
975 bio_page = fio->page;
976
977 /* set submitted = true as a return value */
978 fio->submitted = 1;
979
980 type = WB_DATA_TYPE(bio_page, fio->compressed_page);
981 inc_page_count(sbi, type);
982
983 if (io->bio &&
984 (!io_is_mergeable(sbi, io->bio, io, fio, io->last_block_in_bio,
985 fio->new_blkaddr) ||
986 !f2fs_crypt_mergeable_bio(io->bio, fio->page->mapping->host,
987 page_folio(bio_page)->index, fio)))
988 __submit_merged_bio(io);
989alloc_new:
990 if (io->bio == NULL) {
991 io->bio = __bio_alloc(fio, BIO_MAX_VECS);
992 f2fs_set_bio_crypt_ctx(io->bio, fio->page->mapping->host,
993 page_folio(bio_page)->index, fio, GFP_NOIO);
994 io->fio = *fio;
995 }
996
997 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
998 __submit_merged_bio(io);
999 goto alloc_new;
1000 }
1001
1002 if (fio->io_wbc)
1003 wbc_account_cgroup_owner(fio->io_wbc, page_folio(fio->page),
1004 PAGE_SIZE);
1005
1006 io->last_block_in_bio = fio->new_blkaddr;
1007
1008 trace_f2fs_submit_folio_write(page_folio(fio->page), fio);
1009#ifdef CONFIG_BLK_DEV_ZONED
1010 if (f2fs_sb_has_blkzoned(sbi) && btype < META &&
1011 is_end_zone_blkaddr(sbi, fio->new_blkaddr)) {
1012 bio_get(io->bio);
1013 reinit_completion(&io->zone_wait);
1014 io->bi_private = io->bio->bi_private;
1015 io->bio->bi_private = io;
1016 io->bio->bi_end_io = f2fs_zone_write_end_io;
1017 io->zone_pending_bio = io->bio;
1018 __submit_merged_bio(io);
1019 }
1020#endif
1021 if (fio->in_list)
1022 goto next;
1023out:
1024 if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) ||
1025 !f2fs_is_checkpoint_ready(sbi))
1026 __submit_merged_bio(io);
1027 f2fs_up_write(&io->io_rwsem);
1028}
1029
1030static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
1031 unsigned nr_pages, blk_opf_t op_flag,
1032 pgoff_t first_idx, bool for_write)
1033{
1034 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1035 struct bio *bio;
1036 struct bio_post_read_ctx *ctx = NULL;
1037 unsigned int post_read_steps = 0;
1038 sector_t sector;
1039 struct block_device *bdev = f2fs_target_device(sbi, blkaddr, §or);
1040
1041 bio = bio_alloc_bioset(bdev, bio_max_segs(nr_pages),
1042 REQ_OP_READ | op_flag,
1043 for_write ? GFP_NOIO : GFP_KERNEL, &f2fs_bioset);
1044 if (!bio)
1045 return ERR_PTR(-ENOMEM);
1046 bio->bi_iter.bi_sector = sector;
1047 f2fs_set_bio_crypt_ctx(bio, inode, first_idx, NULL, GFP_NOFS);
1048 bio->bi_end_io = f2fs_read_end_io;
1049
1050 if (fscrypt_inode_uses_fs_layer_crypto(inode))
1051 post_read_steps |= STEP_DECRYPT;
1052
1053 if (f2fs_need_verity(inode, first_idx))
1054 post_read_steps |= STEP_VERITY;
1055
1056 /*
1057 * STEP_DECOMPRESS is handled specially, since a compressed file might
1058 * contain both compressed and uncompressed clusters. We'll allocate a
1059 * bio_post_read_ctx if the file is compressed, but the caller is
1060 * responsible for enabling STEP_DECOMPRESS if it's actually needed.
1061 */
1062
1063 if (post_read_steps || f2fs_compressed_file(inode)) {
1064 /* Due to the mempool, this never fails. */
1065 ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
1066 ctx->bio = bio;
1067 ctx->sbi = sbi;
1068 ctx->enabled_steps = post_read_steps;
1069 ctx->fs_blkaddr = blkaddr;
1070 ctx->decompression_attempted = false;
1071 bio->bi_private = ctx;
1072 }
1073 iostat_alloc_and_bind_ctx(sbi, bio, ctx);
1074
1075 return bio;
1076}
1077
1078/* This can handle encryption stuffs */
1079static int f2fs_submit_page_read(struct inode *inode, struct folio *folio,
1080 block_t blkaddr, blk_opf_t op_flags,
1081 bool for_write)
1082{
1083 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1084 struct bio *bio;
1085
1086 bio = f2fs_grab_read_bio(inode, blkaddr, 1, op_flags,
1087 folio->index, for_write);
1088 if (IS_ERR(bio))
1089 return PTR_ERR(bio);
1090
1091 /* wait for GCed page writeback via META_MAPPING */
1092 f2fs_wait_on_block_writeback(inode, blkaddr);
1093
1094 if (!bio_add_folio(bio, folio, PAGE_SIZE, 0)) {
1095 iostat_update_and_unbind_ctx(bio);
1096 if (bio->bi_private)
1097 mempool_free(bio->bi_private, bio_post_read_ctx_pool);
1098 bio_put(bio);
1099 return -EFAULT;
1100 }
1101 inc_page_count(sbi, F2FS_RD_DATA);
1102 f2fs_update_iostat(sbi, NULL, FS_DATA_READ_IO, F2FS_BLKSIZE);
1103 f2fs_submit_read_bio(sbi, bio, DATA);
1104 return 0;
1105}
1106
1107static void __set_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
1108{
1109 __le32 *addr = get_dnode_addr(dn->inode, dn->node_page);
1110
1111 dn->data_blkaddr = blkaddr;
1112 addr[dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
1113}
1114
1115/*
1116 * Lock ordering for the change of data block address:
1117 * ->data_page
1118 * ->node_page
1119 * update block addresses in the node page
1120 */
1121void f2fs_set_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
1122{
1123 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
1124 __set_data_blkaddr(dn, blkaddr);
1125 if (set_page_dirty(dn->node_page))
1126 dn->node_changed = true;
1127}
1128
1129void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
1130{
1131 f2fs_set_data_blkaddr(dn, blkaddr);
1132 f2fs_update_read_extent_cache(dn);
1133}
1134
1135/* dn->ofs_in_node will be returned with up-to-date last block pointer */
1136int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
1137{
1138 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1139 int err;
1140
1141 if (!count)
1142 return 0;
1143
1144 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
1145 return -EPERM;
1146 err = inc_valid_block_count(sbi, dn->inode, &count, true);
1147 if (unlikely(err))
1148 return err;
1149
1150 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
1151 dn->ofs_in_node, count);
1152
1153 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
1154
1155 for (; count > 0; dn->ofs_in_node++) {
1156 block_t blkaddr = f2fs_data_blkaddr(dn);
1157
1158 if (blkaddr == NULL_ADDR) {
1159 __set_data_blkaddr(dn, NEW_ADDR);
1160 count--;
1161 }
1162 }
1163
1164 if (set_page_dirty(dn->node_page))
1165 dn->node_changed = true;
1166 return 0;
1167}
1168
1169/* Should keep dn->ofs_in_node unchanged */
1170int f2fs_reserve_new_block(struct dnode_of_data *dn)
1171{
1172 unsigned int ofs_in_node = dn->ofs_in_node;
1173 int ret;
1174
1175 ret = f2fs_reserve_new_blocks(dn, 1);
1176 dn->ofs_in_node = ofs_in_node;
1177 return ret;
1178}
1179
1180int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
1181{
1182 bool need_put = dn->inode_page ? false : true;
1183 int err;
1184
1185 err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE);
1186 if (err)
1187 return err;
1188
1189 if (dn->data_blkaddr == NULL_ADDR)
1190 err = f2fs_reserve_new_block(dn);
1191 if (err || need_put)
1192 f2fs_put_dnode(dn);
1193 return err;
1194}
1195
1196struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
1197 blk_opf_t op_flags, bool for_write,
1198 pgoff_t *next_pgofs)
1199{
1200 struct address_space *mapping = inode->i_mapping;
1201 struct dnode_of_data dn;
1202 struct page *page;
1203 int err;
1204
1205 page = f2fs_grab_cache_page(mapping, index, for_write);
1206 if (!page)
1207 return ERR_PTR(-ENOMEM);
1208
1209 if (f2fs_lookup_read_extent_cache_block(inode, index,
1210 &dn.data_blkaddr)) {
1211 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), dn.data_blkaddr,
1212 DATA_GENERIC_ENHANCE_READ)) {
1213 err = -EFSCORRUPTED;
1214 goto put_err;
1215 }
1216 goto got_it;
1217 }
1218
1219 set_new_dnode(&dn, inode, NULL, NULL, 0);
1220 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
1221 if (err) {
1222 if (err == -ENOENT && next_pgofs)
1223 *next_pgofs = f2fs_get_next_page_offset(&dn, index);
1224 goto put_err;
1225 }
1226 f2fs_put_dnode(&dn);
1227
1228 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
1229 err = -ENOENT;
1230 if (next_pgofs)
1231 *next_pgofs = index + 1;
1232 goto put_err;
1233 }
1234 if (dn.data_blkaddr != NEW_ADDR &&
1235 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
1236 dn.data_blkaddr,
1237 DATA_GENERIC_ENHANCE)) {
1238 err = -EFSCORRUPTED;
1239 goto put_err;
1240 }
1241got_it:
1242 if (PageUptodate(page)) {
1243 unlock_page(page);
1244 return page;
1245 }
1246
1247 /*
1248 * A new dentry page is allocated but not able to be written, since its
1249 * new inode page couldn't be allocated due to -ENOSPC.
1250 * In such the case, its blkaddr can be remained as NEW_ADDR.
1251 * see, f2fs_add_link -> f2fs_get_new_data_page ->
1252 * f2fs_init_inode_metadata.
1253 */
1254 if (dn.data_blkaddr == NEW_ADDR) {
1255 zero_user_segment(page, 0, PAGE_SIZE);
1256 if (!PageUptodate(page))
1257 SetPageUptodate(page);
1258 unlock_page(page);
1259 return page;
1260 }
1261
1262 err = f2fs_submit_page_read(inode, page_folio(page), dn.data_blkaddr,
1263 op_flags, for_write);
1264 if (err)
1265 goto put_err;
1266 return page;
1267
1268put_err:
1269 f2fs_put_page(page, 1);
1270 return ERR_PTR(err);
1271}
1272
1273struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index,
1274 pgoff_t *next_pgofs)
1275{
1276 struct address_space *mapping = inode->i_mapping;
1277 struct page *page;
1278
1279 page = find_get_page_flags(mapping, index, FGP_ACCESSED);
1280 if (page && PageUptodate(page))
1281 return page;
1282 f2fs_put_page(page, 0);
1283
1284 page = f2fs_get_read_data_page(inode, index, 0, false, next_pgofs);
1285 if (IS_ERR(page))
1286 return page;
1287
1288 if (PageUptodate(page))
1289 return page;
1290
1291 wait_on_page_locked(page);
1292 if (unlikely(!PageUptodate(page))) {
1293 f2fs_put_page(page, 0);
1294 return ERR_PTR(-EIO);
1295 }
1296 return page;
1297}
1298
1299/*
1300 * If it tries to access a hole, return an error.
1301 * Because, the callers, functions in dir.c and GC, should be able to know
1302 * whether this page exists or not.
1303 */
1304struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
1305 bool for_write)
1306{
1307 struct address_space *mapping = inode->i_mapping;
1308 struct page *page;
1309
1310 page = f2fs_get_read_data_page(inode, index, 0, for_write, NULL);
1311 if (IS_ERR(page))
1312 return page;
1313
1314 /* wait for read completion */
1315 lock_page(page);
1316 if (unlikely(page->mapping != mapping || !PageUptodate(page))) {
1317 f2fs_put_page(page, 1);
1318 return ERR_PTR(-EIO);
1319 }
1320 return page;
1321}
1322
1323/*
1324 * Caller ensures that this data page is never allocated.
1325 * A new zero-filled data page is allocated in the page cache.
1326 *
1327 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
1328 * f2fs_unlock_op().
1329 * Note that, ipage is set only by make_empty_dir, and if any error occur,
1330 * ipage should be released by this function.
1331 */
1332struct page *f2fs_get_new_data_page(struct inode *inode,
1333 struct page *ipage, pgoff_t index, bool new_i_size)
1334{
1335 struct address_space *mapping = inode->i_mapping;
1336 struct page *page;
1337 struct dnode_of_data dn;
1338 int err;
1339
1340 page = f2fs_grab_cache_page(mapping, index, true);
1341 if (!page) {
1342 /*
1343 * before exiting, we should make sure ipage will be released
1344 * if any error occur.
1345 */
1346 f2fs_put_page(ipage, 1);
1347 return ERR_PTR(-ENOMEM);
1348 }
1349
1350 set_new_dnode(&dn, inode, ipage, NULL, 0);
1351 err = f2fs_reserve_block(&dn, index);
1352 if (err) {
1353 f2fs_put_page(page, 1);
1354 return ERR_PTR(err);
1355 }
1356 if (!ipage)
1357 f2fs_put_dnode(&dn);
1358
1359 if (PageUptodate(page))
1360 goto got_it;
1361
1362 if (dn.data_blkaddr == NEW_ADDR) {
1363 zero_user_segment(page, 0, PAGE_SIZE);
1364 if (!PageUptodate(page))
1365 SetPageUptodate(page);
1366 } else {
1367 f2fs_put_page(page, 1);
1368
1369 /* if ipage exists, blkaddr should be NEW_ADDR */
1370 f2fs_bug_on(F2FS_I_SB(inode), ipage);
1371 page = f2fs_get_lock_data_page(inode, index, true);
1372 if (IS_ERR(page))
1373 return page;
1374 }
1375got_it:
1376 if (new_i_size && i_size_read(inode) <
1377 ((loff_t)(index + 1) << PAGE_SHIFT))
1378 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
1379 return page;
1380}
1381
1382static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
1383{
1384 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1385 struct f2fs_summary sum;
1386 struct node_info ni;
1387 block_t old_blkaddr;
1388 blkcnt_t count = 1;
1389 int err;
1390
1391 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
1392 return -EPERM;
1393
1394 err = f2fs_get_node_info(sbi, dn->nid, &ni, false);
1395 if (err)
1396 return err;
1397
1398 dn->data_blkaddr = f2fs_data_blkaddr(dn);
1399 if (dn->data_blkaddr == NULL_ADDR) {
1400 err = inc_valid_block_count(sbi, dn->inode, &count, true);
1401 if (unlikely(err))
1402 return err;
1403 }
1404
1405 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1406 old_blkaddr = dn->data_blkaddr;
1407 err = f2fs_allocate_data_block(sbi, NULL, old_blkaddr,
1408 &dn->data_blkaddr, &sum, seg_type, NULL);
1409 if (err)
1410 return err;
1411
1412 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
1413 f2fs_invalidate_internal_cache(sbi, old_blkaddr, 1);
1414
1415 f2fs_update_data_blkaddr(dn, dn->data_blkaddr);
1416 return 0;
1417}
1418
1419static void f2fs_map_lock(struct f2fs_sb_info *sbi, int flag)
1420{
1421 if (flag == F2FS_GET_BLOCK_PRE_AIO)
1422 f2fs_down_read(&sbi->node_change);
1423 else
1424 f2fs_lock_op(sbi);
1425}
1426
1427static void f2fs_map_unlock(struct f2fs_sb_info *sbi, int flag)
1428{
1429 if (flag == F2FS_GET_BLOCK_PRE_AIO)
1430 f2fs_up_read(&sbi->node_change);
1431 else
1432 f2fs_unlock_op(sbi);
1433}
1434
1435int f2fs_get_block_locked(struct dnode_of_data *dn, pgoff_t index)
1436{
1437 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1438 int err = 0;
1439
1440 f2fs_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO);
1441 if (!f2fs_lookup_read_extent_cache_block(dn->inode, index,
1442 &dn->data_blkaddr))
1443 err = f2fs_reserve_block(dn, index);
1444 f2fs_map_unlock(sbi, F2FS_GET_BLOCK_PRE_AIO);
1445
1446 return err;
1447}
1448
1449static int f2fs_map_no_dnode(struct inode *inode,
1450 struct f2fs_map_blocks *map, struct dnode_of_data *dn,
1451 pgoff_t pgoff)
1452{
1453 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1454
1455 /*
1456 * There is one exceptional case that read_node_page() may return
1457 * -ENOENT due to filesystem has been shutdown or cp_error, return
1458 * -EIO in that case.
1459 */
1460 if (map->m_may_create &&
1461 (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) || f2fs_cp_error(sbi)))
1462 return -EIO;
1463
1464 if (map->m_next_pgofs)
1465 *map->m_next_pgofs = f2fs_get_next_page_offset(dn, pgoff);
1466 if (map->m_next_extent)
1467 *map->m_next_extent = f2fs_get_next_page_offset(dn, pgoff);
1468 return 0;
1469}
1470
1471static bool f2fs_map_blocks_cached(struct inode *inode,
1472 struct f2fs_map_blocks *map, int flag)
1473{
1474 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1475 unsigned int maxblocks = map->m_len;
1476 pgoff_t pgoff = (pgoff_t)map->m_lblk;
1477 struct extent_info ei = {};
1478
1479 if (!f2fs_lookup_read_extent_cache(inode, pgoff, &ei))
1480 return false;
1481
1482 map->m_pblk = ei.blk + pgoff - ei.fofs;
1483 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgoff);
1484 map->m_flags = F2FS_MAP_MAPPED;
1485 if (map->m_next_extent)
1486 *map->m_next_extent = pgoff + map->m_len;
1487
1488 /* for hardware encryption, but to avoid potential issue in future */
1489 if (flag == F2FS_GET_BLOCK_DIO)
1490 f2fs_wait_on_block_writeback_range(inode,
1491 map->m_pblk, map->m_len);
1492
1493 if (f2fs_allow_multi_device_dio(sbi, flag)) {
1494 int bidx = f2fs_target_device_index(sbi, map->m_pblk);
1495 struct f2fs_dev_info *dev = &sbi->devs[bidx];
1496
1497 map->m_bdev = dev->bdev;
1498 map->m_pblk -= dev->start_blk;
1499 map->m_len = min(map->m_len, dev->end_blk + 1 - map->m_pblk);
1500 } else {
1501 map->m_bdev = inode->i_sb->s_bdev;
1502 }
1503 return true;
1504}
1505
1506static bool map_is_mergeable(struct f2fs_sb_info *sbi,
1507 struct f2fs_map_blocks *map,
1508 block_t blkaddr, int flag, int bidx,
1509 int ofs)
1510{
1511 if (map->m_multidev_dio && map->m_bdev != FDEV(bidx).bdev)
1512 return false;
1513 if (map->m_pblk != NEW_ADDR && blkaddr == (map->m_pblk + ofs))
1514 return true;
1515 if (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR)
1516 return true;
1517 if (flag == F2FS_GET_BLOCK_PRE_DIO)
1518 return true;
1519 if (flag == F2FS_GET_BLOCK_DIO &&
1520 map->m_pblk == NULL_ADDR && blkaddr == NULL_ADDR)
1521 return true;
1522 return false;
1523}
1524
1525/*
1526 * f2fs_map_blocks() tries to find or build mapping relationship which
1527 * maps continuous logical blocks to physical blocks, and return such
1528 * info via f2fs_map_blocks structure.
1529 */
1530int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, int flag)
1531{
1532 unsigned int maxblocks = map->m_len;
1533 struct dnode_of_data dn;
1534 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1535 int mode = map->m_may_create ? ALLOC_NODE : LOOKUP_NODE;
1536 pgoff_t pgofs, end_offset, end;
1537 int err = 0, ofs = 1;
1538 unsigned int ofs_in_node, last_ofs_in_node;
1539 blkcnt_t prealloc;
1540 block_t blkaddr;
1541 unsigned int start_pgofs;
1542 int bidx = 0;
1543 bool is_hole;
1544
1545 if (!maxblocks)
1546 return 0;
1547
1548 if (!map->m_may_create && f2fs_map_blocks_cached(inode, map, flag))
1549 goto out;
1550
1551 map->m_bdev = inode->i_sb->s_bdev;
1552 map->m_multidev_dio =
1553 f2fs_allow_multi_device_dio(F2FS_I_SB(inode), flag);
1554
1555 map->m_len = 0;
1556 map->m_flags = 0;
1557
1558 /* it only supports block size == page size */
1559 pgofs = (pgoff_t)map->m_lblk;
1560 end = pgofs + maxblocks;
1561
1562next_dnode:
1563 if (map->m_may_create)
1564 f2fs_map_lock(sbi, flag);
1565
1566 /* When reading holes, we need its node page */
1567 set_new_dnode(&dn, inode, NULL, NULL, 0);
1568 err = f2fs_get_dnode_of_data(&dn, pgofs, mode);
1569 if (err) {
1570 if (flag == F2FS_GET_BLOCK_BMAP)
1571 map->m_pblk = 0;
1572 if (err == -ENOENT)
1573 err = f2fs_map_no_dnode(inode, map, &dn, pgofs);
1574 goto unlock_out;
1575 }
1576
1577 start_pgofs = pgofs;
1578 prealloc = 0;
1579 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
1580 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1581
1582next_block:
1583 blkaddr = f2fs_data_blkaddr(&dn);
1584 is_hole = !__is_valid_data_blkaddr(blkaddr);
1585 if (!is_hole &&
1586 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE)) {
1587 err = -EFSCORRUPTED;
1588 goto sync_out;
1589 }
1590
1591 /* use out-place-update for direct IO under LFS mode */
1592 if (map->m_may_create && (is_hole ||
1593 (flag == F2FS_GET_BLOCK_DIO && f2fs_lfs_mode(sbi) &&
1594 !f2fs_is_pinned_file(inode)))) {
1595 if (unlikely(f2fs_cp_error(sbi))) {
1596 err = -EIO;
1597 goto sync_out;
1598 }
1599
1600 switch (flag) {
1601 case F2FS_GET_BLOCK_PRE_AIO:
1602 if (blkaddr == NULL_ADDR) {
1603 prealloc++;
1604 last_ofs_in_node = dn.ofs_in_node;
1605 }
1606 break;
1607 case F2FS_GET_BLOCK_PRE_DIO:
1608 case F2FS_GET_BLOCK_DIO:
1609 err = __allocate_data_block(&dn, map->m_seg_type);
1610 if (err)
1611 goto sync_out;
1612 if (flag == F2FS_GET_BLOCK_PRE_DIO)
1613 file_need_truncate(inode);
1614 set_inode_flag(inode, FI_APPEND_WRITE);
1615 break;
1616 default:
1617 WARN_ON_ONCE(1);
1618 err = -EIO;
1619 goto sync_out;
1620 }
1621
1622 blkaddr = dn.data_blkaddr;
1623 if (is_hole)
1624 map->m_flags |= F2FS_MAP_NEW;
1625 } else if (is_hole) {
1626 if (f2fs_compressed_file(inode) &&
1627 f2fs_sanity_check_cluster(&dn)) {
1628 err = -EFSCORRUPTED;
1629 f2fs_handle_error(sbi,
1630 ERROR_CORRUPTED_CLUSTER);
1631 goto sync_out;
1632 }
1633
1634 switch (flag) {
1635 case F2FS_GET_BLOCK_PRECACHE:
1636 goto sync_out;
1637 case F2FS_GET_BLOCK_BMAP:
1638 map->m_pblk = 0;
1639 goto sync_out;
1640 case F2FS_GET_BLOCK_FIEMAP:
1641 if (blkaddr == NULL_ADDR) {
1642 if (map->m_next_pgofs)
1643 *map->m_next_pgofs = pgofs + 1;
1644 goto sync_out;
1645 }
1646 break;
1647 case F2FS_GET_BLOCK_DIO:
1648 if (map->m_next_pgofs)
1649 *map->m_next_pgofs = pgofs + 1;
1650 break;
1651 default:
1652 /* for defragment case */
1653 if (map->m_next_pgofs)
1654 *map->m_next_pgofs = pgofs + 1;
1655 goto sync_out;
1656 }
1657 }
1658
1659 if (flag == F2FS_GET_BLOCK_PRE_AIO)
1660 goto skip;
1661
1662 if (map->m_multidev_dio)
1663 bidx = f2fs_target_device_index(sbi, blkaddr);
1664
1665 if (map->m_len == 0) {
1666 /* reserved delalloc block should be mapped for fiemap. */
1667 if (blkaddr == NEW_ADDR)
1668 map->m_flags |= F2FS_MAP_DELALLOC;
1669 /* DIO READ and hole case, should not map the blocks. */
1670 if (!(flag == F2FS_GET_BLOCK_DIO && is_hole && !map->m_may_create))
1671 map->m_flags |= F2FS_MAP_MAPPED;
1672
1673 map->m_pblk = blkaddr;
1674 map->m_len = 1;
1675
1676 if (map->m_multidev_dio)
1677 map->m_bdev = FDEV(bidx).bdev;
1678 } else if (map_is_mergeable(sbi, map, blkaddr, flag, bidx, ofs)) {
1679 ofs++;
1680 map->m_len++;
1681 } else {
1682 goto sync_out;
1683 }
1684
1685skip:
1686 dn.ofs_in_node++;
1687 pgofs++;
1688
1689 /* preallocate blocks in batch for one dnode page */
1690 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
1691 (pgofs == end || dn.ofs_in_node == end_offset)) {
1692
1693 dn.ofs_in_node = ofs_in_node;
1694 err = f2fs_reserve_new_blocks(&dn, prealloc);
1695 if (err)
1696 goto sync_out;
1697
1698 map->m_len += dn.ofs_in_node - ofs_in_node;
1699 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
1700 err = -ENOSPC;
1701 goto sync_out;
1702 }
1703 dn.ofs_in_node = end_offset;
1704 }
1705
1706 if (flag == F2FS_GET_BLOCK_DIO && f2fs_lfs_mode(sbi) &&
1707 map->m_may_create) {
1708 /* the next block to be allocated may not be contiguous. */
1709 if (GET_SEGOFF_FROM_SEG0(sbi, blkaddr) % BLKS_PER_SEC(sbi) ==
1710 CAP_BLKS_PER_SEC(sbi) - 1)
1711 goto sync_out;
1712 }
1713
1714 if (pgofs >= end)
1715 goto sync_out;
1716 else if (dn.ofs_in_node < end_offset)
1717 goto next_block;
1718
1719 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1720 if (map->m_flags & F2FS_MAP_MAPPED) {
1721 unsigned int ofs = start_pgofs - map->m_lblk;
1722
1723 f2fs_update_read_extent_cache_range(&dn,
1724 start_pgofs, map->m_pblk + ofs,
1725 map->m_len - ofs);
1726 }
1727 }
1728
1729 f2fs_put_dnode(&dn);
1730
1731 if (map->m_may_create) {
1732 f2fs_map_unlock(sbi, flag);
1733 f2fs_balance_fs(sbi, dn.node_changed);
1734 }
1735 goto next_dnode;
1736
1737sync_out:
1738
1739 if (flag == F2FS_GET_BLOCK_DIO && map->m_flags & F2FS_MAP_MAPPED) {
1740 /*
1741 * for hardware encryption, but to avoid potential issue
1742 * in future
1743 */
1744 f2fs_wait_on_block_writeback_range(inode,
1745 map->m_pblk, map->m_len);
1746
1747 if (map->m_multidev_dio) {
1748 block_t blk_addr = map->m_pblk;
1749
1750 bidx = f2fs_target_device_index(sbi, map->m_pblk);
1751
1752 map->m_bdev = FDEV(bidx).bdev;
1753 map->m_pblk -= FDEV(bidx).start_blk;
1754
1755 if (map->m_may_create)
1756 f2fs_update_device_state(sbi, inode->i_ino,
1757 blk_addr, map->m_len);
1758
1759 f2fs_bug_on(sbi, blk_addr + map->m_len >
1760 FDEV(bidx).end_blk + 1);
1761 }
1762 }
1763
1764 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1765 if (map->m_flags & F2FS_MAP_MAPPED) {
1766 unsigned int ofs = start_pgofs - map->m_lblk;
1767
1768 f2fs_update_read_extent_cache_range(&dn,
1769 start_pgofs, map->m_pblk + ofs,
1770 map->m_len - ofs);
1771 }
1772 if (map->m_next_extent)
1773 *map->m_next_extent = pgofs + 1;
1774 }
1775 f2fs_put_dnode(&dn);
1776unlock_out:
1777 if (map->m_may_create) {
1778 f2fs_map_unlock(sbi, flag);
1779 f2fs_balance_fs(sbi, dn.node_changed);
1780 }
1781out:
1782 trace_f2fs_map_blocks(inode, map, flag, err);
1783 return err;
1784}
1785
1786bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
1787{
1788 struct f2fs_map_blocks map;
1789 block_t last_lblk;
1790 int err;
1791
1792 if (pos + len > i_size_read(inode))
1793 return false;
1794
1795 map.m_lblk = F2FS_BYTES_TO_BLK(pos);
1796 map.m_next_pgofs = NULL;
1797 map.m_next_extent = NULL;
1798 map.m_seg_type = NO_CHECK_TYPE;
1799 map.m_may_create = false;
1800 last_lblk = F2FS_BLK_ALIGN(pos + len);
1801
1802 while (map.m_lblk < last_lblk) {
1803 map.m_len = last_lblk - map.m_lblk;
1804 err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_DEFAULT);
1805 if (err || map.m_len == 0)
1806 return false;
1807 map.m_lblk += map.m_len;
1808 }
1809 return true;
1810}
1811
1812static int f2fs_xattr_fiemap(struct inode *inode,
1813 struct fiemap_extent_info *fieinfo)
1814{
1815 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1816 struct page *page;
1817 struct node_info ni;
1818 __u64 phys = 0, len;
1819 __u32 flags;
1820 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
1821 int err = 0;
1822
1823 if (f2fs_has_inline_xattr(inode)) {
1824 int offset;
1825
1826 page = f2fs_grab_cache_page(NODE_MAPPING(sbi),
1827 inode->i_ino, false);
1828 if (!page)
1829 return -ENOMEM;
1830
1831 err = f2fs_get_node_info(sbi, inode->i_ino, &ni, false);
1832 if (err) {
1833 f2fs_put_page(page, 1);
1834 return err;
1835 }
1836
1837 phys = F2FS_BLK_TO_BYTES(ni.blk_addr);
1838 offset = offsetof(struct f2fs_inode, i_addr) +
1839 sizeof(__le32) * (DEF_ADDRS_PER_INODE -
1840 get_inline_xattr_addrs(inode));
1841
1842 phys += offset;
1843 len = inline_xattr_size(inode);
1844
1845 f2fs_put_page(page, 1);
1846
1847 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED;
1848
1849 if (!xnid)
1850 flags |= FIEMAP_EXTENT_LAST;
1851
1852 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1853 trace_f2fs_fiemap(inode, 0, phys, len, flags, err);
1854 if (err)
1855 return err;
1856 }
1857
1858 if (xnid) {
1859 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false);
1860 if (!page)
1861 return -ENOMEM;
1862
1863 err = f2fs_get_node_info(sbi, xnid, &ni, false);
1864 if (err) {
1865 f2fs_put_page(page, 1);
1866 return err;
1867 }
1868
1869 phys = F2FS_BLK_TO_BYTES(ni.blk_addr);
1870 len = inode->i_sb->s_blocksize;
1871
1872 f2fs_put_page(page, 1);
1873
1874 flags = FIEMAP_EXTENT_LAST;
1875 }
1876
1877 if (phys) {
1878 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1879 trace_f2fs_fiemap(inode, 0, phys, len, flags, err);
1880 }
1881
1882 return (err < 0 ? err : 0);
1883}
1884
1885int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1886 u64 start, u64 len)
1887{
1888 struct f2fs_map_blocks map;
1889 sector_t start_blk, last_blk, blk_len, max_len;
1890 pgoff_t next_pgofs;
1891 u64 logical = 0, phys = 0, size = 0;
1892 u32 flags = 0;
1893 int ret = 0;
1894 bool compr_cluster = false, compr_appended;
1895 unsigned int cluster_size = F2FS_I(inode)->i_cluster_size;
1896 unsigned int count_in_cluster = 0;
1897 loff_t maxbytes;
1898
1899 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
1900 ret = f2fs_precache_extents(inode);
1901 if (ret)
1902 return ret;
1903 }
1904
1905 ret = fiemap_prep(inode, fieinfo, start, &len, FIEMAP_FLAG_XATTR);
1906 if (ret)
1907 return ret;
1908
1909 inode_lock_shared(inode);
1910
1911 maxbytes = F2FS_BLK_TO_BYTES(max_file_blocks(inode));
1912 if (start > maxbytes) {
1913 ret = -EFBIG;
1914 goto out;
1915 }
1916
1917 if (len > maxbytes || (maxbytes - len) < start)
1918 len = maxbytes - start;
1919
1920 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1921 ret = f2fs_xattr_fiemap(inode, fieinfo);
1922 goto out;
1923 }
1924
1925 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
1926 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1927 if (ret != -EAGAIN)
1928 goto out;
1929 }
1930
1931 start_blk = F2FS_BYTES_TO_BLK(start);
1932 last_blk = F2FS_BYTES_TO_BLK(start + len - 1);
1933 blk_len = last_blk - start_blk + 1;
1934 max_len = F2FS_BYTES_TO_BLK(maxbytes) - start_blk;
1935
1936next:
1937 memset(&map, 0, sizeof(map));
1938 map.m_lblk = start_blk;
1939 map.m_len = blk_len;
1940 map.m_next_pgofs = &next_pgofs;
1941 map.m_seg_type = NO_CHECK_TYPE;
1942
1943 if (compr_cluster) {
1944 map.m_lblk += 1;
1945 map.m_len = cluster_size - count_in_cluster;
1946 }
1947
1948 ret = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_FIEMAP);
1949 if (ret)
1950 goto out;
1951
1952 /* HOLE */
1953 if (!compr_cluster && !(map.m_flags & F2FS_MAP_FLAGS)) {
1954 start_blk = next_pgofs;
1955
1956 if (F2FS_BLK_TO_BYTES(start_blk) < maxbytes)
1957 goto prep_next;
1958
1959 flags |= FIEMAP_EXTENT_LAST;
1960 }
1961
1962 /*
1963 * current extent may cross boundary of inquiry, increase len to
1964 * requery.
1965 */
1966 if (!compr_cluster && (map.m_flags & F2FS_MAP_MAPPED) &&
1967 map.m_lblk + map.m_len - 1 == last_blk &&
1968 blk_len != max_len) {
1969 blk_len = max_len;
1970 goto next;
1971 }
1972
1973 compr_appended = false;
1974 /* In a case of compressed cluster, append this to the last extent */
1975 if (compr_cluster && ((map.m_flags & F2FS_MAP_DELALLOC) ||
1976 !(map.m_flags & F2FS_MAP_FLAGS))) {
1977 compr_appended = true;
1978 goto skip_fill;
1979 }
1980
1981 if (size) {
1982 flags |= FIEMAP_EXTENT_MERGED;
1983 if (IS_ENCRYPTED(inode))
1984 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1985
1986 ret = fiemap_fill_next_extent(fieinfo, logical,
1987 phys, size, flags);
1988 trace_f2fs_fiemap(inode, logical, phys, size, flags, ret);
1989 if (ret)
1990 goto out;
1991 size = 0;
1992 }
1993
1994 if (start_blk > last_blk)
1995 goto out;
1996
1997skip_fill:
1998 if (map.m_pblk == COMPRESS_ADDR) {
1999 compr_cluster = true;
2000 count_in_cluster = 1;
2001 } else if (compr_appended) {
2002 unsigned int appended_blks = cluster_size -
2003 count_in_cluster + 1;
2004 size += F2FS_BLK_TO_BYTES(appended_blks);
2005 start_blk += appended_blks;
2006 compr_cluster = false;
2007 } else {
2008 logical = F2FS_BLK_TO_BYTES(start_blk);
2009 phys = __is_valid_data_blkaddr(map.m_pblk) ?
2010 F2FS_BLK_TO_BYTES(map.m_pblk) : 0;
2011 size = F2FS_BLK_TO_BYTES(map.m_len);
2012 flags = 0;
2013
2014 if (compr_cluster) {
2015 flags = FIEMAP_EXTENT_ENCODED;
2016 count_in_cluster += map.m_len;
2017 if (count_in_cluster == cluster_size) {
2018 compr_cluster = false;
2019 size += F2FS_BLKSIZE;
2020 }
2021 } else if (map.m_flags & F2FS_MAP_DELALLOC) {
2022 flags = FIEMAP_EXTENT_UNWRITTEN;
2023 }
2024
2025 start_blk += F2FS_BYTES_TO_BLK(size);
2026 }
2027
2028prep_next:
2029 cond_resched();
2030 if (fatal_signal_pending(current))
2031 ret = -EINTR;
2032 else
2033 goto next;
2034out:
2035 if (ret == 1)
2036 ret = 0;
2037
2038 inode_unlock_shared(inode);
2039 return ret;
2040}
2041
2042static inline loff_t f2fs_readpage_limit(struct inode *inode)
2043{
2044 if (IS_ENABLED(CONFIG_FS_VERITY) && IS_VERITY(inode))
2045 return F2FS_BLK_TO_BYTES(max_file_blocks(inode));
2046
2047 return i_size_read(inode);
2048}
2049
2050static inline blk_opf_t f2fs_ra_op_flags(struct readahead_control *rac)
2051{
2052 return rac ? REQ_RAHEAD : 0;
2053}
2054
2055static int f2fs_read_single_page(struct inode *inode, struct folio *folio,
2056 unsigned nr_pages,
2057 struct f2fs_map_blocks *map,
2058 struct bio **bio_ret,
2059 sector_t *last_block_in_bio,
2060 struct readahead_control *rac)
2061{
2062 struct bio *bio = *bio_ret;
2063 const unsigned int blocksize = F2FS_BLKSIZE;
2064 sector_t block_in_file;
2065 sector_t last_block;
2066 sector_t last_block_in_file;
2067 sector_t block_nr;
2068 pgoff_t index = folio_index(folio);
2069 int ret = 0;
2070
2071 block_in_file = (sector_t)index;
2072 last_block = block_in_file + nr_pages;
2073 last_block_in_file = F2FS_BYTES_TO_BLK(f2fs_readpage_limit(inode) +
2074 blocksize - 1);
2075 if (last_block > last_block_in_file)
2076 last_block = last_block_in_file;
2077
2078 /* just zeroing out page which is beyond EOF */
2079 if (block_in_file >= last_block)
2080 goto zero_out;
2081 /*
2082 * Map blocks using the previous result first.
2083 */
2084 if ((map->m_flags & F2FS_MAP_MAPPED) &&
2085 block_in_file > map->m_lblk &&
2086 block_in_file < (map->m_lblk + map->m_len))
2087 goto got_it;
2088
2089 /*
2090 * Then do more f2fs_map_blocks() calls until we are
2091 * done with this page.
2092 */
2093 map->m_lblk = block_in_file;
2094 map->m_len = last_block - block_in_file;
2095
2096 ret = f2fs_map_blocks(inode, map, F2FS_GET_BLOCK_DEFAULT);
2097 if (ret)
2098 goto out;
2099got_it:
2100 if ((map->m_flags & F2FS_MAP_MAPPED)) {
2101 block_nr = map->m_pblk + block_in_file - map->m_lblk;
2102 folio_set_mappedtodisk(folio);
2103
2104 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr,
2105 DATA_GENERIC_ENHANCE_READ)) {
2106 ret = -EFSCORRUPTED;
2107 goto out;
2108 }
2109 } else {
2110zero_out:
2111 folio_zero_segment(folio, 0, folio_size(folio));
2112 if (f2fs_need_verity(inode, index) &&
2113 !fsverity_verify_folio(folio)) {
2114 ret = -EIO;
2115 goto out;
2116 }
2117 if (!folio_test_uptodate(folio))
2118 folio_mark_uptodate(folio);
2119 folio_unlock(folio);
2120 goto out;
2121 }
2122
2123 /*
2124 * This page will go to BIO. Do we need to send this
2125 * BIO off first?
2126 */
2127 if (bio && (!page_is_mergeable(F2FS_I_SB(inode), bio,
2128 *last_block_in_bio, block_nr) ||
2129 !f2fs_crypt_mergeable_bio(bio, inode, index, NULL))) {
2130submit_and_realloc:
2131 f2fs_submit_read_bio(F2FS_I_SB(inode), bio, DATA);
2132 bio = NULL;
2133 }
2134 if (bio == NULL) {
2135 bio = f2fs_grab_read_bio(inode, block_nr, nr_pages,
2136 f2fs_ra_op_flags(rac), index,
2137 false);
2138 if (IS_ERR(bio)) {
2139 ret = PTR_ERR(bio);
2140 bio = NULL;
2141 goto out;
2142 }
2143 }
2144
2145 /*
2146 * If the page is under writeback, we need to wait for
2147 * its completion to see the correct decrypted data.
2148 */
2149 f2fs_wait_on_block_writeback(inode, block_nr);
2150
2151 if (!bio_add_folio(bio, folio, blocksize, 0))
2152 goto submit_and_realloc;
2153
2154 inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA);
2155 f2fs_update_iostat(F2FS_I_SB(inode), NULL, FS_DATA_READ_IO,
2156 F2FS_BLKSIZE);
2157 *last_block_in_bio = block_nr;
2158out:
2159 *bio_ret = bio;
2160 return ret;
2161}
2162
2163#ifdef CONFIG_F2FS_FS_COMPRESSION
2164int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
2165 unsigned nr_pages, sector_t *last_block_in_bio,
2166 struct readahead_control *rac, bool for_write)
2167{
2168 struct dnode_of_data dn;
2169 struct inode *inode = cc->inode;
2170 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2171 struct bio *bio = *bio_ret;
2172 unsigned int start_idx = cc->cluster_idx << cc->log_cluster_size;
2173 sector_t last_block_in_file;
2174 const unsigned int blocksize = F2FS_BLKSIZE;
2175 struct decompress_io_ctx *dic = NULL;
2176 struct extent_info ei = {};
2177 bool from_dnode = true;
2178 int i;
2179 int ret = 0;
2180
2181 f2fs_bug_on(sbi, f2fs_cluster_is_empty(cc));
2182
2183 last_block_in_file = F2FS_BYTES_TO_BLK(f2fs_readpage_limit(inode) +
2184 blocksize - 1);
2185
2186 /* get rid of pages beyond EOF */
2187 for (i = 0; i < cc->cluster_size; i++) {
2188 struct page *page = cc->rpages[i];
2189 struct folio *folio;
2190
2191 if (!page)
2192 continue;
2193
2194 folio = page_folio(page);
2195 if ((sector_t)folio->index >= last_block_in_file) {
2196 folio_zero_segment(folio, 0, folio_size(folio));
2197 if (!folio_test_uptodate(folio))
2198 folio_mark_uptodate(folio);
2199 } else if (!folio_test_uptodate(folio)) {
2200 continue;
2201 }
2202 folio_unlock(folio);
2203 if (for_write)
2204 folio_put(folio);
2205 cc->rpages[i] = NULL;
2206 cc->nr_rpages--;
2207 }
2208
2209 /* we are done since all pages are beyond EOF */
2210 if (f2fs_cluster_is_empty(cc))
2211 goto out;
2212
2213 if (f2fs_lookup_read_extent_cache(inode, start_idx, &ei))
2214 from_dnode = false;
2215
2216 if (!from_dnode)
2217 goto skip_reading_dnode;
2218
2219 set_new_dnode(&dn, inode, NULL, NULL, 0);
2220 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
2221 if (ret)
2222 goto out;
2223
2224 if (unlikely(f2fs_cp_error(sbi))) {
2225 ret = -EIO;
2226 goto out_put_dnode;
2227 }
2228 f2fs_bug_on(sbi, dn.data_blkaddr != COMPRESS_ADDR);
2229
2230skip_reading_dnode:
2231 for (i = 1; i < cc->cluster_size; i++) {
2232 block_t blkaddr;
2233
2234 blkaddr = from_dnode ? data_blkaddr(dn.inode, dn.node_page,
2235 dn.ofs_in_node + i) :
2236 ei.blk + i - 1;
2237
2238 if (!__is_valid_data_blkaddr(blkaddr))
2239 break;
2240
2241 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC)) {
2242 ret = -EFAULT;
2243 goto out_put_dnode;
2244 }
2245 cc->nr_cpages++;
2246
2247 if (!from_dnode && i >= ei.c_len)
2248 break;
2249 }
2250
2251 /* nothing to decompress */
2252 if (cc->nr_cpages == 0) {
2253 ret = 0;
2254 goto out_put_dnode;
2255 }
2256
2257 dic = f2fs_alloc_dic(cc);
2258 if (IS_ERR(dic)) {
2259 ret = PTR_ERR(dic);
2260 goto out_put_dnode;
2261 }
2262
2263 for (i = 0; i < cc->nr_cpages; i++) {
2264 struct folio *folio = page_folio(dic->cpages[i]);
2265 block_t blkaddr;
2266 struct bio_post_read_ctx *ctx;
2267
2268 blkaddr = from_dnode ? data_blkaddr(dn.inode, dn.node_page,
2269 dn.ofs_in_node + i + 1) :
2270 ei.blk + i;
2271
2272 f2fs_wait_on_block_writeback(inode, blkaddr);
2273
2274 if (f2fs_load_compressed_page(sbi, folio_page(folio, 0),
2275 blkaddr)) {
2276 if (atomic_dec_and_test(&dic->remaining_pages)) {
2277 f2fs_decompress_cluster(dic, true);
2278 break;
2279 }
2280 continue;
2281 }
2282
2283 if (bio && (!page_is_mergeable(sbi, bio,
2284 *last_block_in_bio, blkaddr) ||
2285 !f2fs_crypt_mergeable_bio(bio, inode, folio->index, NULL))) {
2286submit_and_realloc:
2287 f2fs_submit_read_bio(sbi, bio, DATA);
2288 bio = NULL;
2289 }
2290
2291 if (!bio) {
2292 bio = f2fs_grab_read_bio(inode, blkaddr, nr_pages,
2293 f2fs_ra_op_flags(rac),
2294 folio->index, for_write);
2295 if (IS_ERR(bio)) {
2296 ret = PTR_ERR(bio);
2297 f2fs_decompress_end_io(dic, ret, true);
2298 f2fs_put_dnode(&dn);
2299 *bio_ret = NULL;
2300 return ret;
2301 }
2302 }
2303
2304 if (!bio_add_folio(bio, folio, blocksize, 0))
2305 goto submit_and_realloc;
2306
2307 ctx = get_post_read_ctx(bio);
2308 ctx->enabled_steps |= STEP_DECOMPRESS;
2309 refcount_inc(&dic->refcnt);
2310
2311 inc_page_count(sbi, F2FS_RD_DATA);
2312 f2fs_update_iostat(sbi, inode, FS_DATA_READ_IO, F2FS_BLKSIZE);
2313 *last_block_in_bio = blkaddr;
2314 }
2315
2316 if (from_dnode)
2317 f2fs_put_dnode(&dn);
2318
2319 *bio_ret = bio;
2320 return 0;
2321
2322out_put_dnode:
2323 if (from_dnode)
2324 f2fs_put_dnode(&dn);
2325out:
2326 for (i = 0; i < cc->cluster_size; i++) {
2327 if (cc->rpages[i]) {
2328 ClearPageUptodate(cc->rpages[i]);
2329 unlock_page(cc->rpages[i]);
2330 }
2331 }
2332 *bio_ret = bio;
2333 return ret;
2334}
2335#endif
2336
2337/*
2338 * This function was originally taken from fs/mpage.c, and customized for f2fs.
2339 * Major change was from block_size == page_size in f2fs by default.
2340 */
2341static int f2fs_mpage_readpages(struct inode *inode,
2342 struct readahead_control *rac, struct folio *folio)
2343{
2344 struct bio *bio = NULL;
2345 sector_t last_block_in_bio = 0;
2346 struct f2fs_map_blocks map;
2347#ifdef CONFIG_F2FS_FS_COMPRESSION
2348 struct compress_ctx cc = {
2349 .inode = inode,
2350 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2351 .cluster_size = F2FS_I(inode)->i_cluster_size,
2352 .cluster_idx = NULL_CLUSTER,
2353 .rpages = NULL,
2354 .cpages = NULL,
2355 .nr_rpages = 0,
2356 .nr_cpages = 0,
2357 };
2358 pgoff_t nc_cluster_idx = NULL_CLUSTER;
2359 pgoff_t index;
2360#endif
2361 unsigned nr_pages = rac ? readahead_count(rac) : 1;
2362 unsigned max_nr_pages = nr_pages;
2363 int ret = 0;
2364
2365 map.m_pblk = 0;
2366 map.m_lblk = 0;
2367 map.m_len = 0;
2368 map.m_flags = 0;
2369 map.m_next_pgofs = NULL;
2370 map.m_next_extent = NULL;
2371 map.m_seg_type = NO_CHECK_TYPE;
2372 map.m_may_create = false;
2373
2374 for (; nr_pages; nr_pages--) {
2375 if (rac) {
2376 folio = readahead_folio(rac);
2377 prefetchw(&folio->flags);
2378 }
2379
2380#ifdef CONFIG_F2FS_FS_COMPRESSION
2381 index = folio_index(folio);
2382
2383 if (!f2fs_compressed_file(inode))
2384 goto read_single_page;
2385
2386 /* there are remained compressed pages, submit them */
2387 if (!f2fs_cluster_can_merge_page(&cc, index)) {
2388 ret = f2fs_read_multi_pages(&cc, &bio,
2389 max_nr_pages,
2390 &last_block_in_bio,
2391 rac, false);
2392 f2fs_destroy_compress_ctx(&cc, false);
2393 if (ret)
2394 goto set_error_page;
2395 }
2396 if (cc.cluster_idx == NULL_CLUSTER) {
2397 if (nc_cluster_idx == index >> cc.log_cluster_size)
2398 goto read_single_page;
2399
2400 ret = f2fs_is_compressed_cluster(inode, index);
2401 if (ret < 0)
2402 goto set_error_page;
2403 else if (!ret) {
2404 nc_cluster_idx =
2405 index >> cc.log_cluster_size;
2406 goto read_single_page;
2407 }
2408
2409 nc_cluster_idx = NULL_CLUSTER;
2410 }
2411 ret = f2fs_init_compress_ctx(&cc);
2412 if (ret)
2413 goto set_error_page;
2414
2415 f2fs_compress_ctx_add_page(&cc, folio);
2416
2417 goto next_page;
2418read_single_page:
2419#endif
2420
2421 ret = f2fs_read_single_page(inode, folio, max_nr_pages, &map,
2422 &bio, &last_block_in_bio, rac);
2423 if (ret) {
2424#ifdef CONFIG_F2FS_FS_COMPRESSION
2425set_error_page:
2426#endif
2427 folio_zero_segment(folio, 0, folio_size(folio));
2428 folio_unlock(folio);
2429 }
2430#ifdef CONFIG_F2FS_FS_COMPRESSION
2431next_page:
2432#endif
2433
2434#ifdef CONFIG_F2FS_FS_COMPRESSION
2435 if (f2fs_compressed_file(inode)) {
2436 /* last page */
2437 if (nr_pages == 1 && !f2fs_cluster_is_empty(&cc)) {
2438 ret = f2fs_read_multi_pages(&cc, &bio,
2439 max_nr_pages,
2440 &last_block_in_bio,
2441 rac, false);
2442 f2fs_destroy_compress_ctx(&cc, false);
2443 }
2444 }
2445#endif
2446 }
2447 if (bio)
2448 f2fs_submit_read_bio(F2FS_I_SB(inode), bio, DATA);
2449 return ret;
2450}
2451
2452static int f2fs_read_data_folio(struct file *file, struct folio *folio)
2453{
2454 struct inode *inode = folio->mapping->host;
2455 int ret = -EAGAIN;
2456
2457 trace_f2fs_readpage(folio, DATA);
2458
2459 if (!f2fs_is_compress_backend_ready(inode)) {
2460 folio_unlock(folio);
2461 return -EOPNOTSUPP;
2462 }
2463
2464 /* If the file has inline data, try to read it directly */
2465 if (f2fs_has_inline_data(inode))
2466 ret = f2fs_read_inline_data(inode, folio);
2467 if (ret == -EAGAIN)
2468 ret = f2fs_mpage_readpages(inode, NULL, folio);
2469 return ret;
2470}
2471
2472static void f2fs_readahead(struct readahead_control *rac)
2473{
2474 struct inode *inode = rac->mapping->host;
2475
2476 trace_f2fs_readpages(inode, readahead_index(rac), readahead_count(rac));
2477
2478 if (!f2fs_is_compress_backend_ready(inode))
2479 return;
2480
2481 /* If the file has inline data, skip readahead */
2482 if (f2fs_has_inline_data(inode))
2483 return;
2484
2485 f2fs_mpage_readpages(inode, rac, NULL);
2486}
2487
2488int f2fs_encrypt_one_page(struct f2fs_io_info *fio)
2489{
2490 struct inode *inode = fio->page->mapping->host;
2491 struct page *mpage, *page;
2492 gfp_t gfp_flags = GFP_NOFS;
2493
2494 if (!f2fs_encrypted_file(inode))
2495 return 0;
2496
2497 page = fio->compressed_page ? fio->compressed_page : fio->page;
2498
2499 if (fscrypt_inode_uses_inline_crypto(inode))
2500 return 0;
2501
2502retry_encrypt:
2503 fio->encrypted_page = fscrypt_encrypt_pagecache_blocks(page,
2504 PAGE_SIZE, 0, gfp_flags);
2505 if (IS_ERR(fio->encrypted_page)) {
2506 /* flush pending IOs and wait for a while in the ENOMEM case */
2507 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
2508 f2fs_flush_merged_writes(fio->sbi);
2509 memalloc_retry_wait(GFP_NOFS);
2510 gfp_flags |= __GFP_NOFAIL;
2511 goto retry_encrypt;
2512 }
2513 return PTR_ERR(fio->encrypted_page);
2514 }
2515
2516 mpage = find_lock_page(META_MAPPING(fio->sbi), fio->old_blkaddr);
2517 if (mpage) {
2518 if (PageUptodate(mpage))
2519 memcpy(page_address(mpage),
2520 page_address(fio->encrypted_page), PAGE_SIZE);
2521 f2fs_put_page(mpage, 1);
2522 }
2523 return 0;
2524}
2525
2526static inline bool check_inplace_update_policy(struct inode *inode,
2527 struct f2fs_io_info *fio)
2528{
2529 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2530
2531 if (IS_F2FS_IPU_HONOR_OPU_WRITE(sbi) &&
2532 is_inode_flag_set(inode, FI_OPU_WRITE))
2533 return false;
2534 if (IS_F2FS_IPU_FORCE(sbi))
2535 return true;
2536 if (IS_F2FS_IPU_SSR(sbi) && f2fs_need_SSR(sbi))
2537 return true;
2538 if (IS_F2FS_IPU_UTIL(sbi) && utilization(sbi) > SM_I(sbi)->min_ipu_util)
2539 return true;
2540 if (IS_F2FS_IPU_SSR_UTIL(sbi) && f2fs_need_SSR(sbi) &&
2541 utilization(sbi) > SM_I(sbi)->min_ipu_util)
2542 return true;
2543
2544 /*
2545 * IPU for rewrite async pages
2546 */
2547 if (IS_F2FS_IPU_ASYNC(sbi) && fio && fio->op == REQ_OP_WRITE &&
2548 !(fio->op_flags & REQ_SYNC) && !IS_ENCRYPTED(inode))
2549 return true;
2550
2551 /* this is only set during fdatasync */
2552 if (IS_F2FS_IPU_FSYNC(sbi) && is_inode_flag_set(inode, FI_NEED_IPU))
2553 return true;
2554
2555 if (unlikely(fio && is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2556 !f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2557 return true;
2558
2559 return false;
2560}
2561
2562bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio)
2563{
2564 /* swap file is migrating in aligned write mode */
2565 if (is_inode_flag_set(inode, FI_ALIGNED_WRITE))
2566 return false;
2567
2568 if (f2fs_is_pinned_file(inode))
2569 return true;
2570
2571 /* if this is cold file, we should overwrite to avoid fragmentation */
2572 if (file_is_cold(inode) && !is_inode_flag_set(inode, FI_OPU_WRITE))
2573 return true;
2574
2575 return check_inplace_update_policy(inode, fio);
2576}
2577
2578bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
2579{
2580 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2581
2582 /* The below cases were checked when setting it. */
2583 if (f2fs_is_pinned_file(inode))
2584 return false;
2585 if (fio && is_sbi_flag_set(sbi, SBI_NEED_FSCK))
2586 return true;
2587 if (f2fs_lfs_mode(sbi))
2588 return true;
2589 if (S_ISDIR(inode->i_mode))
2590 return true;
2591 if (IS_NOQUOTA(inode))
2592 return true;
2593 if (f2fs_used_in_atomic_write(inode))
2594 return true;
2595 /* rewrite low ratio compress data w/ OPU mode to avoid fragmentation */
2596 if (f2fs_compressed_file(inode) &&
2597 F2FS_OPTION(sbi).compress_mode == COMPR_MODE_USER &&
2598 is_inode_flag_set(inode, FI_ENABLE_COMPRESS))
2599 return true;
2600
2601 /* swap file is migrating in aligned write mode */
2602 if (is_inode_flag_set(inode, FI_ALIGNED_WRITE))
2603 return true;
2604
2605 if (is_inode_flag_set(inode, FI_OPU_WRITE))
2606 return true;
2607
2608 if (fio) {
2609 if (page_private_gcing(fio->page))
2610 return true;
2611 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2612 f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2613 return true;
2614 }
2615 return false;
2616}
2617
2618static inline bool need_inplace_update(struct f2fs_io_info *fio)
2619{
2620 struct inode *inode = fio->page->mapping->host;
2621
2622 if (f2fs_should_update_outplace(inode, fio))
2623 return false;
2624
2625 return f2fs_should_update_inplace(inode, fio);
2626}
2627
2628int f2fs_do_write_data_page(struct f2fs_io_info *fio)
2629{
2630 struct folio *folio = page_folio(fio->page);
2631 struct inode *inode = folio->mapping->host;
2632 struct dnode_of_data dn;
2633 struct node_info ni;
2634 bool ipu_force = false;
2635 bool atomic_commit;
2636 int err = 0;
2637
2638 /* Use COW inode to make dnode_of_data for atomic write */
2639 atomic_commit = f2fs_is_atomic_file(inode) &&
2640 page_private_atomic(folio_page(folio, 0));
2641 if (atomic_commit)
2642 set_new_dnode(&dn, F2FS_I(inode)->cow_inode, NULL, NULL, 0);
2643 else
2644 set_new_dnode(&dn, inode, NULL, NULL, 0);
2645
2646 if (need_inplace_update(fio) &&
2647 f2fs_lookup_read_extent_cache_block(inode, folio->index,
2648 &fio->old_blkaddr)) {
2649 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
2650 DATA_GENERIC_ENHANCE))
2651 return -EFSCORRUPTED;
2652
2653 ipu_force = true;
2654 fio->need_lock = LOCK_DONE;
2655 goto got_it;
2656 }
2657
2658 /* Deadlock due to between page->lock and f2fs_lock_op */
2659 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
2660 return -EAGAIN;
2661
2662 err = f2fs_get_dnode_of_data(&dn, folio->index, LOOKUP_NODE);
2663 if (err)
2664 goto out;
2665
2666 fio->old_blkaddr = dn.data_blkaddr;
2667
2668 /* This page is already truncated */
2669 if (fio->old_blkaddr == NULL_ADDR) {
2670 folio_clear_uptodate(folio);
2671 clear_page_private_gcing(folio_page(folio, 0));
2672 goto out_writepage;
2673 }
2674got_it:
2675 if (__is_valid_data_blkaddr(fio->old_blkaddr) &&
2676 !f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
2677 DATA_GENERIC_ENHANCE)) {
2678 err = -EFSCORRUPTED;
2679 goto out_writepage;
2680 }
2681
2682 /* wait for GCed page writeback via META_MAPPING */
2683 if (fio->meta_gc)
2684 f2fs_wait_on_block_writeback(inode, fio->old_blkaddr);
2685
2686 /*
2687 * If current allocation needs SSR,
2688 * it had better in-place writes for updated data.
2689 */
2690 if (ipu_force ||
2691 (__is_valid_data_blkaddr(fio->old_blkaddr) &&
2692 need_inplace_update(fio))) {
2693 err = f2fs_encrypt_one_page(fio);
2694 if (err)
2695 goto out_writepage;
2696
2697 folio_start_writeback(folio);
2698 f2fs_put_dnode(&dn);
2699 if (fio->need_lock == LOCK_REQ)
2700 f2fs_unlock_op(fio->sbi);
2701 err = f2fs_inplace_write_data(fio);
2702 if (err) {
2703 if (fscrypt_inode_uses_fs_layer_crypto(inode))
2704 fscrypt_finalize_bounce_page(&fio->encrypted_page);
2705 folio_end_writeback(folio);
2706 } else {
2707 set_inode_flag(inode, FI_UPDATE_WRITE);
2708 }
2709 trace_f2fs_do_write_data_page(folio, IPU);
2710 return err;
2711 }
2712
2713 if (fio->need_lock == LOCK_RETRY) {
2714 if (!f2fs_trylock_op(fio->sbi)) {
2715 err = -EAGAIN;
2716 goto out_writepage;
2717 }
2718 fio->need_lock = LOCK_REQ;
2719 }
2720
2721 err = f2fs_get_node_info(fio->sbi, dn.nid, &ni, false);
2722 if (err)
2723 goto out_writepage;
2724
2725 fio->version = ni.version;
2726
2727 err = f2fs_encrypt_one_page(fio);
2728 if (err)
2729 goto out_writepage;
2730
2731 folio_start_writeback(folio);
2732
2733 if (fio->compr_blocks && fio->old_blkaddr == COMPRESS_ADDR)
2734 f2fs_i_compr_blocks_update(inode, fio->compr_blocks - 1, false);
2735
2736 /* LFS mode write path */
2737 f2fs_outplace_write_data(&dn, fio);
2738 trace_f2fs_do_write_data_page(folio, OPU);
2739 set_inode_flag(inode, FI_APPEND_WRITE);
2740 if (atomic_commit)
2741 clear_page_private_atomic(folio_page(folio, 0));
2742out_writepage:
2743 f2fs_put_dnode(&dn);
2744out:
2745 if (fio->need_lock == LOCK_REQ)
2746 f2fs_unlock_op(fio->sbi);
2747 return err;
2748}
2749
2750int f2fs_write_single_data_page(struct folio *folio, int *submitted,
2751 struct bio **bio,
2752 sector_t *last_block,
2753 struct writeback_control *wbc,
2754 enum iostat_type io_type,
2755 int compr_blocks,
2756 bool allow_balance)
2757{
2758 struct inode *inode = folio->mapping->host;
2759 struct page *page = folio_page(folio, 0);
2760 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2761 loff_t i_size = i_size_read(inode);
2762 const pgoff_t end_index = ((unsigned long long)i_size)
2763 >> PAGE_SHIFT;
2764 loff_t psize = (loff_t)(folio->index + 1) << PAGE_SHIFT;
2765 unsigned offset = 0;
2766 bool need_balance_fs = false;
2767 bool quota_inode = IS_NOQUOTA(inode);
2768 int err = 0;
2769 struct f2fs_io_info fio = {
2770 .sbi = sbi,
2771 .ino = inode->i_ino,
2772 .type = DATA,
2773 .op = REQ_OP_WRITE,
2774 .op_flags = wbc_to_write_flags(wbc),
2775 .old_blkaddr = NULL_ADDR,
2776 .page = page,
2777 .encrypted_page = NULL,
2778 .submitted = 0,
2779 .compr_blocks = compr_blocks,
2780 .need_lock = compr_blocks ? LOCK_DONE : LOCK_RETRY,
2781 .meta_gc = f2fs_meta_inode_gc_required(inode) ? 1 : 0,
2782 .io_type = io_type,
2783 .io_wbc = wbc,
2784 .bio = bio,
2785 .last_block = last_block,
2786 };
2787
2788 trace_f2fs_writepage(folio, DATA);
2789
2790 /* we should bypass data pages to proceed the kworker jobs */
2791 if (unlikely(f2fs_cp_error(sbi))) {
2792 mapping_set_error(folio->mapping, -EIO);
2793 /*
2794 * don't drop any dirty dentry pages for keeping lastest
2795 * directory structure.
2796 */
2797 if (S_ISDIR(inode->i_mode) &&
2798 !is_sbi_flag_set(sbi, SBI_IS_CLOSE))
2799 goto redirty_out;
2800
2801 /* keep data pages in remount-ro mode */
2802 if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_READONLY)
2803 goto redirty_out;
2804 goto out;
2805 }
2806
2807 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2808 goto redirty_out;
2809
2810 if (folio->index < end_index ||
2811 f2fs_verity_in_progress(inode) ||
2812 compr_blocks)
2813 goto write;
2814
2815 /*
2816 * If the offset is out-of-range of file size,
2817 * this page does not have to be written to disk.
2818 */
2819 offset = i_size & (PAGE_SIZE - 1);
2820 if ((folio->index >= end_index + 1) || !offset)
2821 goto out;
2822
2823 folio_zero_segment(folio, offset, folio_size(folio));
2824write:
2825 /* Dentry/quota blocks are controlled by checkpoint */
2826 if (S_ISDIR(inode->i_mode) || quota_inode) {
2827 /*
2828 * We need to wait for node_write to avoid block allocation during
2829 * checkpoint. This can only happen to quota writes which can cause
2830 * the below discard race condition.
2831 */
2832 if (quota_inode)
2833 f2fs_down_read(&sbi->node_write);
2834
2835 fio.need_lock = LOCK_DONE;
2836 err = f2fs_do_write_data_page(&fio);
2837
2838 if (quota_inode)
2839 f2fs_up_read(&sbi->node_write);
2840
2841 goto done;
2842 }
2843
2844 if (!wbc->for_reclaim)
2845 need_balance_fs = true;
2846 else if (has_not_enough_free_secs(sbi, 0, 0))
2847 goto redirty_out;
2848 else
2849 set_inode_flag(inode, FI_HOT_DATA);
2850
2851 err = -EAGAIN;
2852 if (f2fs_has_inline_data(inode)) {
2853 err = f2fs_write_inline_data(inode, folio);
2854 if (!err)
2855 goto out;
2856 }
2857
2858 if (err == -EAGAIN) {
2859 err = f2fs_do_write_data_page(&fio);
2860 if (err == -EAGAIN) {
2861 f2fs_bug_on(sbi, compr_blocks);
2862 fio.need_lock = LOCK_REQ;
2863 err = f2fs_do_write_data_page(&fio);
2864 }
2865 }
2866
2867 if (err) {
2868 file_set_keep_isize(inode);
2869 } else {
2870 spin_lock(&F2FS_I(inode)->i_size_lock);
2871 if (F2FS_I(inode)->last_disk_size < psize)
2872 F2FS_I(inode)->last_disk_size = psize;
2873 spin_unlock(&F2FS_I(inode)->i_size_lock);
2874 }
2875
2876done:
2877 if (err && err != -ENOENT)
2878 goto redirty_out;
2879
2880out:
2881 inode_dec_dirty_pages(inode);
2882 if (err) {
2883 folio_clear_uptodate(folio);
2884 clear_page_private_gcing(page);
2885 }
2886
2887 if (wbc->for_reclaim) {
2888 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, DATA);
2889 clear_inode_flag(inode, FI_HOT_DATA);
2890 f2fs_remove_dirty_inode(inode);
2891 submitted = NULL;
2892 }
2893 folio_unlock(folio);
2894 if (!S_ISDIR(inode->i_mode) && !IS_NOQUOTA(inode) &&
2895 !F2FS_I(inode)->wb_task && allow_balance)
2896 f2fs_balance_fs(sbi, need_balance_fs);
2897
2898 if (unlikely(f2fs_cp_error(sbi))) {
2899 f2fs_submit_merged_write(sbi, DATA);
2900 if (bio && *bio)
2901 f2fs_submit_merged_ipu_write(sbi, bio, NULL);
2902 submitted = NULL;
2903 }
2904
2905 if (submitted)
2906 *submitted = fio.submitted;
2907
2908 return 0;
2909
2910redirty_out:
2911 folio_redirty_for_writepage(wbc, folio);
2912 /*
2913 * pageout() in MM translates EAGAIN, so calls handle_write_error()
2914 * -> mapping_set_error() -> set_bit(AS_EIO, ...).
2915 * file_write_and_wait_range() will see EIO error, which is critical
2916 * to return value of fsync() followed by atomic_write failure to user.
2917 */
2918 if (!err || wbc->for_reclaim)
2919 return AOP_WRITEPAGE_ACTIVATE;
2920 folio_unlock(folio);
2921 return err;
2922}
2923
2924static int f2fs_write_data_page(struct page *page,
2925 struct writeback_control *wbc)
2926{
2927 struct folio *folio = page_folio(page);
2928#ifdef CONFIG_F2FS_FS_COMPRESSION
2929 struct inode *inode = folio->mapping->host;
2930
2931 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
2932 goto out;
2933
2934 if (f2fs_compressed_file(inode)) {
2935 if (f2fs_is_compressed_cluster(inode, folio->index)) {
2936 folio_redirty_for_writepage(wbc, folio);
2937 return AOP_WRITEPAGE_ACTIVATE;
2938 }
2939 }
2940out:
2941#endif
2942
2943 return f2fs_write_single_data_page(folio, NULL, NULL, NULL,
2944 wbc, FS_DATA_IO, 0, true);
2945}
2946
2947/*
2948 * This function was copied from write_cache_pages from mm/page-writeback.c.
2949 * The major change is making write step of cold data page separately from
2950 * warm/hot data page.
2951 */
2952static int f2fs_write_cache_pages(struct address_space *mapping,
2953 struct writeback_control *wbc,
2954 enum iostat_type io_type)
2955{
2956 int ret = 0;
2957 int done = 0, retry = 0;
2958 struct page *pages_local[F2FS_ONSTACK_PAGES];
2959 struct page **pages = pages_local;
2960 struct folio_batch fbatch;
2961 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
2962 struct bio *bio = NULL;
2963 sector_t last_block;
2964#ifdef CONFIG_F2FS_FS_COMPRESSION
2965 struct inode *inode = mapping->host;
2966 struct compress_ctx cc = {
2967 .inode = inode,
2968 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2969 .cluster_size = F2FS_I(inode)->i_cluster_size,
2970 .cluster_idx = NULL_CLUSTER,
2971 .rpages = NULL,
2972 .nr_rpages = 0,
2973 .cpages = NULL,
2974 .valid_nr_cpages = 0,
2975 .rbuf = NULL,
2976 .cbuf = NULL,
2977 .rlen = PAGE_SIZE * F2FS_I(inode)->i_cluster_size,
2978 .private = NULL,
2979 };
2980#endif
2981 int nr_folios, p, idx;
2982 int nr_pages;
2983 unsigned int max_pages = F2FS_ONSTACK_PAGES;
2984 pgoff_t index;
2985 pgoff_t end; /* Inclusive */
2986 pgoff_t done_index;
2987 int range_whole = 0;
2988 xa_mark_t tag;
2989 int nwritten = 0;
2990 int submitted = 0;
2991 int i;
2992
2993#ifdef CONFIG_F2FS_FS_COMPRESSION
2994 if (f2fs_compressed_file(inode) &&
2995 1 << cc.log_cluster_size > F2FS_ONSTACK_PAGES) {
2996 pages = f2fs_kzalloc(sbi, sizeof(struct page *) <<
2997 cc.log_cluster_size, GFP_NOFS | __GFP_NOFAIL);
2998 max_pages = 1 << cc.log_cluster_size;
2999 }
3000#endif
3001
3002 folio_batch_init(&fbatch);
3003
3004 if (get_dirty_pages(mapping->host) <=
3005 SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
3006 set_inode_flag(mapping->host, FI_HOT_DATA);
3007 else
3008 clear_inode_flag(mapping->host, FI_HOT_DATA);
3009
3010 if (wbc->range_cyclic) {
3011 index = mapping->writeback_index; /* prev offset */
3012 end = -1;
3013 } else {
3014 index = wbc->range_start >> PAGE_SHIFT;
3015 end = wbc->range_end >> PAGE_SHIFT;
3016 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
3017 range_whole = 1;
3018 }
3019 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
3020 tag = PAGECACHE_TAG_TOWRITE;
3021 else
3022 tag = PAGECACHE_TAG_DIRTY;
3023retry:
3024 retry = 0;
3025 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
3026 tag_pages_for_writeback(mapping, index, end);
3027 done_index = index;
3028 while (!done && !retry && (index <= end)) {
3029 nr_pages = 0;
3030again:
3031 nr_folios = filemap_get_folios_tag(mapping, &index, end,
3032 tag, &fbatch);
3033 if (nr_folios == 0) {
3034 if (nr_pages)
3035 goto write;
3036 break;
3037 }
3038
3039 for (i = 0; i < nr_folios; i++) {
3040 struct folio *folio = fbatch.folios[i];
3041
3042 idx = 0;
3043 p = folio_nr_pages(folio);
3044add_more:
3045 pages[nr_pages] = folio_page(folio, idx);
3046 folio_get(folio);
3047 if (++nr_pages == max_pages) {
3048 index = folio->index + idx + 1;
3049 folio_batch_release(&fbatch);
3050 goto write;
3051 }
3052 if (++idx < p)
3053 goto add_more;
3054 }
3055 folio_batch_release(&fbatch);
3056 goto again;
3057write:
3058 for (i = 0; i < nr_pages; i++) {
3059 struct page *page = pages[i];
3060 struct folio *folio = page_folio(page);
3061 bool need_readd;
3062readd:
3063 need_readd = false;
3064#ifdef CONFIG_F2FS_FS_COMPRESSION
3065 if (f2fs_compressed_file(inode)) {
3066 void *fsdata = NULL;
3067 struct page *pagep;
3068 int ret2;
3069
3070 ret = f2fs_init_compress_ctx(&cc);
3071 if (ret) {
3072 done = 1;
3073 break;
3074 }
3075
3076 if (!f2fs_cluster_can_merge_page(&cc,
3077 folio->index)) {
3078 ret = f2fs_write_multi_pages(&cc,
3079 &submitted, wbc, io_type);
3080 if (!ret)
3081 need_readd = true;
3082 goto result;
3083 }
3084
3085 if (unlikely(f2fs_cp_error(sbi)))
3086 goto lock_folio;
3087
3088 if (!f2fs_cluster_is_empty(&cc))
3089 goto lock_folio;
3090
3091 if (f2fs_all_cluster_page_ready(&cc,
3092 pages, i, nr_pages, true))
3093 goto lock_folio;
3094
3095 ret2 = f2fs_prepare_compress_overwrite(
3096 inode, &pagep,
3097 folio->index, &fsdata);
3098 if (ret2 < 0) {
3099 ret = ret2;
3100 done = 1;
3101 break;
3102 } else if (ret2 &&
3103 (!f2fs_compress_write_end(inode,
3104 fsdata, folio->index, 1) ||
3105 !f2fs_all_cluster_page_ready(&cc,
3106 pages, i, nr_pages,
3107 false))) {
3108 retry = 1;
3109 break;
3110 }
3111 }
3112#endif
3113 /* give a priority to WB_SYNC threads */
3114 if (atomic_read(&sbi->wb_sync_req[DATA]) &&
3115 wbc->sync_mode == WB_SYNC_NONE) {
3116 done = 1;
3117 break;
3118 }
3119#ifdef CONFIG_F2FS_FS_COMPRESSION
3120lock_folio:
3121#endif
3122 done_index = folio->index;
3123retry_write:
3124 folio_lock(folio);
3125
3126 if (unlikely(folio->mapping != mapping)) {
3127continue_unlock:
3128 folio_unlock(folio);
3129 continue;
3130 }
3131
3132 if (!folio_test_dirty(folio)) {
3133 /* someone wrote it for us */
3134 goto continue_unlock;
3135 }
3136
3137 if (folio_test_writeback(folio)) {
3138 if (wbc->sync_mode == WB_SYNC_NONE)
3139 goto continue_unlock;
3140 f2fs_wait_on_page_writeback(&folio->page, DATA, true, true);
3141 }
3142
3143 if (!folio_clear_dirty_for_io(folio))
3144 goto continue_unlock;
3145
3146#ifdef CONFIG_F2FS_FS_COMPRESSION
3147 if (f2fs_compressed_file(inode)) {
3148 folio_get(folio);
3149 f2fs_compress_ctx_add_page(&cc, folio);
3150 continue;
3151 }
3152#endif
3153 submitted = 0;
3154 ret = f2fs_write_single_data_page(folio,
3155 &submitted, &bio, &last_block,
3156 wbc, io_type, 0, true);
3157 if (ret == AOP_WRITEPAGE_ACTIVATE)
3158 folio_unlock(folio);
3159#ifdef CONFIG_F2FS_FS_COMPRESSION
3160result:
3161#endif
3162 nwritten += submitted;
3163 wbc->nr_to_write -= submitted;
3164
3165 if (unlikely(ret)) {
3166 /*
3167 * keep nr_to_write, since vfs uses this to
3168 * get # of written pages.
3169 */
3170 if (ret == AOP_WRITEPAGE_ACTIVATE) {
3171 ret = 0;
3172 goto next;
3173 } else if (ret == -EAGAIN) {
3174 ret = 0;
3175 if (wbc->sync_mode == WB_SYNC_ALL) {
3176 f2fs_io_schedule_timeout(
3177 DEFAULT_IO_TIMEOUT);
3178 goto retry_write;
3179 }
3180 goto next;
3181 }
3182 done_index = folio_next_index(folio);
3183 done = 1;
3184 break;
3185 }
3186
3187 if (wbc->nr_to_write <= 0 &&
3188 wbc->sync_mode == WB_SYNC_NONE) {
3189 done = 1;
3190 break;
3191 }
3192next:
3193 if (need_readd)
3194 goto readd;
3195 }
3196 release_pages(pages, nr_pages);
3197 cond_resched();
3198 }
3199#ifdef CONFIG_F2FS_FS_COMPRESSION
3200 /* flush remained pages in compress cluster */
3201 if (f2fs_compressed_file(inode) && !f2fs_cluster_is_empty(&cc)) {
3202 ret = f2fs_write_multi_pages(&cc, &submitted, wbc, io_type);
3203 nwritten += submitted;
3204 wbc->nr_to_write -= submitted;
3205 if (ret) {
3206 done = 1;
3207 retry = 0;
3208 }
3209 }
3210 if (f2fs_compressed_file(inode))
3211 f2fs_destroy_compress_ctx(&cc, false);
3212#endif
3213 if (retry) {
3214 index = 0;
3215 end = -1;
3216 goto retry;
3217 }
3218 if (wbc->range_cyclic && !done)
3219 done_index = 0;
3220 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
3221 mapping->writeback_index = done_index;
3222
3223 if (nwritten)
3224 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
3225 NULL, 0, DATA);
3226 /* submit cached bio of IPU write */
3227 if (bio)
3228 f2fs_submit_merged_ipu_write(sbi, &bio, NULL);
3229
3230#ifdef CONFIG_F2FS_FS_COMPRESSION
3231 if (pages != pages_local)
3232 kfree(pages);
3233#endif
3234
3235 return ret;
3236}
3237
3238static inline bool __should_serialize_io(struct inode *inode,
3239 struct writeback_control *wbc)
3240{
3241 /* to avoid deadlock in path of data flush */
3242 if (F2FS_I(inode)->wb_task)
3243 return false;
3244
3245 if (!S_ISREG(inode->i_mode))
3246 return false;
3247 if (IS_NOQUOTA(inode))
3248 return false;
3249
3250 if (f2fs_need_compress_data(inode))
3251 return true;
3252 if (wbc->sync_mode != WB_SYNC_ALL)
3253 return true;
3254 if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks)
3255 return true;
3256 return false;
3257}
3258
3259static int __f2fs_write_data_pages(struct address_space *mapping,
3260 struct writeback_control *wbc,
3261 enum iostat_type io_type)
3262{
3263 struct inode *inode = mapping->host;
3264 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3265 struct blk_plug plug;
3266 int ret;
3267 bool locked = false;
3268
3269 /* deal with chardevs and other special file */
3270 if (!mapping->a_ops->writepage)
3271 return 0;
3272
3273 /* skip writing if there is no dirty page in this inode */
3274 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
3275 return 0;
3276
3277 /* during POR, we don't need to trigger writepage at all. */
3278 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
3279 goto skip_write;
3280
3281 if ((S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) &&
3282 wbc->sync_mode == WB_SYNC_NONE &&
3283 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
3284 f2fs_available_free_memory(sbi, DIRTY_DENTS))
3285 goto skip_write;
3286
3287 /* skip writing in file defragment preparing stage */
3288 if (is_inode_flag_set(inode, FI_SKIP_WRITES))
3289 goto skip_write;
3290
3291 trace_f2fs_writepages(mapping->host, wbc, DATA);
3292
3293 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
3294 if (wbc->sync_mode == WB_SYNC_ALL)
3295 atomic_inc(&sbi->wb_sync_req[DATA]);
3296 else if (atomic_read(&sbi->wb_sync_req[DATA])) {
3297 /* to avoid potential deadlock */
3298 if (current->plug)
3299 blk_finish_plug(current->plug);
3300 goto skip_write;
3301 }
3302
3303 if (__should_serialize_io(inode, wbc)) {
3304 mutex_lock(&sbi->writepages);
3305 locked = true;
3306 }
3307
3308 blk_start_plug(&plug);
3309 ret = f2fs_write_cache_pages(mapping, wbc, io_type);
3310 blk_finish_plug(&plug);
3311
3312 if (locked)
3313 mutex_unlock(&sbi->writepages);
3314
3315 if (wbc->sync_mode == WB_SYNC_ALL)
3316 atomic_dec(&sbi->wb_sync_req[DATA]);
3317 /*
3318 * if some pages were truncated, we cannot guarantee its mapping->host
3319 * to detect pending bios.
3320 */
3321
3322 f2fs_remove_dirty_inode(inode);
3323 return ret;
3324
3325skip_write:
3326 wbc->pages_skipped += get_dirty_pages(inode);
3327 trace_f2fs_writepages(mapping->host, wbc, DATA);
3328 return 0;
3329}
3330
3331static int f2fs_write_data_pages(struct address_space *mapping,
3332 struct writeback_control *wbc)
3333{
3334 struct inode *inode = mapping->host;
3335
3336 return __f2fs_write_data_pages(mapping, wbc,
3337 F2FS_I(inode)->cp_task == current ?
3338 FS_CP_DATA_IO : FS_DATA_IO);
3339}
3340
3341void f2fs_write_failed(struct inode *inode, loff_t to)
3342{
3343 loff_t i_size = i_size_read(inode);
3344
3345 if (IS_NOQUOTA(inode))
3346 return;
3347
3348 /* In the fs-verity case, f2fs_end_enable_verity() does the truncate */
3349 if (to > i_size && !f2fs_verity_in_progress(inode)) {
3350 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3351 filemap_invalidate_lock(inode->i_mapping);
3352
3353 truncate_pagecache(inode, i_size);
3354 f2fs_truncate_blocks(inode, i_size, true);
3355
3356 filemap_invalidate_unlock(inode->i_mapping);
3357 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3358 }
3359}
3360
3361static int prepare_write_begin(struct f2fs_sb_info *sbi,
3362 struct folio *folio, loff_t pos, unsigned int len,
3363 block_t *blk_addr, bool *node_changed)
3364{
3365 struct inode *inode = folio->mapping->host;
3366 pgoff_t index = folio->index;
3367 struct dnode_of_data dn;
3368 struct page *ipage;
3369 bool locked = false;
3370 int flag = F2FS_GET_BLOCK_PRE_AIO;
3371 int err = 0;
3372
3373 /*
3374 * If a whole page is being written and we already preallocated all the
3375 * blocks, then there is no need to get a block address now.
3376 */
3377 if (len == PAGE_SIZE && is_inode_flag_set(inode, FI_PREALLOCATED_ALL))
3378 return 0;
3379
3380 /* f2fs_lock_op avoids race between write CP and convert_inline_page */
3381 if (f2fs_has_inline_data(inode)) {
3382 if (pos + len > MAX_INLINE_DATA(inode))
3383 flag = F2FS_GET_BLOCK_DEFAULT;
3384 f2fs_map_lock(sbi, flag);
3385 locked = true;
3386 } else if ((pos & PAGE_MASK) >= i_size_read(inode)) {
3387 f2fs_map_lock(sbi, flag);
3388 locked = true;
3389 }
3390
3391restart:
3392 /* check inline_data */
3393 ipage = f2fs_get_node_page(sbi, inode->i_ino);
3394 if (IS_ERR(ipage)) {
3395 err = PTR_ERR(ipage);
3396 goto unlock_out;
3397 }
3398
3399 set_new_dnode(&dn, inode, ipage, ipage, 0);
3400
3401 if (f2fs_has_inline_data(inode)) {
3402 if (pos + len <= MAX_INLINE_DATA(inode)) {
3403 f2fs_do_read_inline_data(folio, ipage);
3404 set_inode_flag(inode, FI_DATA_EXIST);
3405 if (inode->i_nlink)
3406 set_page_private_inline(ipage);
3407 goto out;
3408 }
3409 err = f2fs_convert_inline_page(&dn, folio_page(folio, 0));
3410 if (err || dn.data_blkaddr != NULL_ADDR)
3411 goto out;
3412 }
3413
3414 if (!f2fs_lookup_read_extent_cache_block(inode, index,
3415 &dn.data_blkaddr)) {
3416 if (IS_DEVICE_ALIASING(inode)) {
3417 err = -ENODATA;
3418 goto out;
3419 }
3420
3421 if (locked) {
3422 err = f2fs_reserve_block(&dn, index);
3423 goto out;
3424 }
3425
3426 /* hole case */
3427 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
3428 if (!err && dn.data_blkaddr != NULL_ADDR)
3429 goto out;
3430 f2fs_put_dnode(&dn);
3431 f2fs_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO);
3432 WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO);
3433 locked = true;
3434 goto restart;
3435 }
3436out:
3437 if (!err) {
3438 /* convert_inline_page can make node_changed */
3439 *blk_addr = dn.data_blkaddr;
3440 *node_changed = dn.node_changed;
3441 }
3442 f2fs_put_dnode(&dn);
3443unlock_out:
3444 if (locked)
3445 f2fs_map_unlock(sbi, flag);
3446 return err;
3447}
3448
3449static int __find_data_block(struct inode *inode, pgoff_t index,
3450 block_t *blk_addr)
3451{
3452 struct dnode_of_data dn;
3453 struct page *ipage;
3454 int err = 0;
3455
3456 ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
3457 if (IS_ERR(ipage))
3458 return PTR_ERR(ipage);
3459
3460 set_new_dnode(&dn, inode, ipage, ipage, 0);
3461
3462 if (!f2fs_lookup_read_extent_cache_block(inode, index,
3463 &dn.data_blkaddr)) {
3464 /* hole case */
3465 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
3466 if (err) {
3467 dn.data_blkaddr = NULL_ADDR;
3468 err = 0;
3469 }
3470 }
3471 *blk_addr = dn.data_blkaddr;
3472 f2fs_put_dnode(&dn);
3473 return err;
3474}
3475
3476static int __reserve_data_block(struct inode *inode, pgoff_t index,
3477 block_t *blk_addr, bool *node_changed)
3478{
3479 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3480 struct dnode_of_data dn;
3481 struct page *ipage;
3482 int err = 0;
3483
3484 f2fs_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO);
3485
3486 ipage = f2fs_get_node_page(sbi, inode->i_ino);
3487 if (IS_ERR(ipage)) {
3488 err = PTR_ERR(ipage);
3489 goto unlock_out;
3490 }
3491 set_new_dnode(&dn, inode, ipage, ipage, 0);
3492
3493 if (!f2fs_lookup_read_extent_cache_block(dn.inode, index,
3494 &dn.data_blkaddr))
3495 err = f2fs_reserve_block(&dn, index);
3496
3497 *blk_addr = dn.data_blkaddr;
3498 *node_changed = dn.node_changed;
3499 f2fs_put_dnode(&dn);
3500
3501unlock_out:
3502 f2fs_map_unlock(sbi, F2FS_GET_BLOCK_PRE_AIO);
3503 return err;
3504}
3505
3506static int prepare_atomic_write_begin(struct f2fs_sb_info *sbi,
3507 struct folio *folio, loff_t pos, unsigned int len,
3508 block_t *blk_addr, bool *node_changed, bool *use_cow)
3509{
3510 struct inode *inode = folio->mapping->host;
3511 struct inode *cow_inode = F2FS_I(inode)->cow_inode;
3512 pgoff_t index = folio->index;
3513 int err = 0;
3514 block_t ori_blk_addr = NULL_ADDR;
3515
3516 /* If pos is beyond the end of file, reserve a new block in COW inode */
3517 if ((pos & PAGE_MASK) >= i_size_read(inode))
3518 goto reserve_block;
3519
3520 /* Look for the block in COW inode first */
3521 err = __find_data_block(cow_inode, index, blk_addr);
3522 if (err) {
3523 return err;
3524 } else if (*blk_addr != NULL_ADDR) {
3525 *use_cow = true;
3526 return 0;
3527 }
3528
3529 if (is_inode_flag_set(inode, FI_ATOMIC_REPLACE))
3530 goto reserve_block;
3531
3532 /* Look for the block in the original inode */
3533 err = __find_data_block(inode, index, &ori_blk_addr);
3534 if (err)
3535 return err;
3536
3537reserve_block:
3538 /* Finally, we should reserve a new block in COW inode for the update */
3539 err = __reserve_data_block(cow_inode, index, blk_addr, node_changed);
3540 if (err)
3541 return err;
3542 inc_atomic_write_cnt(inode);
3543
3544 if (ori_blk_addr != NULL_ADDR)
3545 *blk_addr = ori_blk_addr;
3546 return 0;
3547}
3548
3549static int f2fs_write_begin(struct file *file, struct address_space *mapping,
3550 loff_t pos, unsigned len, struct folio **foliop, void **fsdata)
3551{
3552 struct inode *inode = mapping->host;
3553 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3554 struct folio *folio;
3555 pgoff_t index = pos >> PAGE_SHIFT;
3556 bool need_balance = false;
3557 bool use_cow = false;
3558 block_t blkaddr = NULL_ADDR;
3559 int err = 0;
3560
3561 trace_f2fs_write_begin(inode, pos, len);
3562
3563 if (!f2fs_is_checkpoint_ready(sbi)) {
3564 err = -ENOSPC;
3565 goto fail;
3566 }
3567
3568 /*
3569 * We should check this at this moment to avoid deadlock on inode page
3570 * and #0 page. The locking rule for inline_data conversion should be:
3571 * folio_lock(folio #0) -> folio_lock(inode_page)
3572 */
3573 if (index != 0) {
3574 err = f2fs_convert_inline_inode(inode);
3575 if (err)
3576 goto fail;
3577 }
3578
3579#ifdef CONFIG_F2FS_FS_COMPRESSION
3580 if (f2fs_compressed_file(inode)) {
3581 int ret;
3582 struct page *page;
3583
3584 *fsdata = NULL;
3585
3586 if (len == PAGE_SIZE && !(f2fs_is_atomic_file(inode)))
3587 goto repeat;
3588
3589 ret = f2fs_prepare_compress_overwrite(inode, &page,
3590 index, fsdata);
3591 if (ret < 0) {
3592 err = ret;
3593 goto fail;
3594 } else if (ret) {
3595 *foliop = page_folio(page);
3596 return 0;
3597 }
3598 }
3599#endif
3600
3601repeat:
3602 /*
3603 * Do not use FGP_STABLE to avoid deadlock.
3604 * Will wait that below with our IO control.
3605 */
3606 folio = __filemap_get_folio(mapping, index,
3607 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
3608 if (IS_ERR(folio)) {
3609 err = PTR_ERR(folio);
3610 goto fail;
3611 }
3612
3613 /* TODO: cluster can be compressed due to race with .writepage */
3614
3615 *foliop = folio;
3616
3617 if (f2fs_is_atomic_file(inode))
3618 err = prepare_atomic_write_begin(sbi, folio, pos, len,
3619 &blkaddr, &need_balance, &use_cow);
3620 else
3621 err = prepare_write_begin(sbi, folio, pos, len,
3622 &blkaddr, &need_balance);
3623 if (err)
3624 goto put_folio;
3625
3626 if (need_balance && !IS_NOQUOTA(inode) &&
3627 has_not_enough_free_secs(sbi, 0, 0)) {
3628 folio_unlock(folio);
3629 f2fs_balance_fs(sbi, true);
3630 folio_lock(folio);
3631 if (folio->mapping != mapping) {
3632 /* The folio got truncated from under us */
3633 folio_unlock(folio);
3634 folio_put(folio);
3635 goto repeat;
3636 }
3637 }
3638
3639 f2fs_wait_on_page_writeback(&folio->page, DATA, false, true);
3640
3641 if (len == folio_size(folio) || folio_test_uptodate(folio))
3642 return 0;
3643
3644 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode) &&
3645 !f2fs_verity_in_progress(inode)) {
3646 folio_zero_segment(folio, len, folio_size(folio));
3647 return 0;
3648 }
3649
3650 if (blkaddr == NEW_ADDR) {
3651 folio_zero_segment(folio, 0, folio_size(folio));
3652 folio_mark_uptodate(folio);
3653 } else {
3654 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
3655 DATA_GENERIC_ENHANCE_READ)) {
3656 err = -EFSCORRUPTED;
3657 goto put_folio;
3658 }
3659 err = f2fs_submit_page_read(use_cow ?
3660 F2FS_I(inode)->cow_inode : inode,
3661 folio, blkaddr, 0, true);
3662 if (err)
3663 goto put_folio;
3664
3665 folio_lock(folio);
3666 if (unlikely(folio->mapping != mapping)) {
3667 folio_unlock(folio);
3668 folio_put(folio);
3669 goto repeat;
3670 }
3671 if (unlikely(!folio_test_uptodate(folio))) {
3672 err = -EIO;
3673 goto put_folio;
3674 }
3675 }
3676 return 0;
3677
3678put_folio:
3679 folio_unlock(folio);
3680 folio_put(folio);
3681fail:
3682 f2fs_write_failed(inode, pos + len);
3683 return err;
3684}
3685
3686static int f2fs_write_end(struct file *file,
3687 struct address_space *mapping,
3688 loff_t pos, unsigned len, unsigned copied,
3689 struct folio *folio, void *fsdata)
3690{
3691 struct inode *inode = folio->mapping->host;
3692
3693 trace_f2fs_write_end(inode, pos, len, copied);
3694
3695 /*
3696 * This should be come from len == PAGE_SIZE, and we expect copied
3697 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
3698 * let generic_perform_write() try to copy data again through copied=0.
3699 */
3700 if (!folio_test_uptodate(folio)) {
3701 if (unlikely(copied != len))
3702 copied = 0;
3703 else
3704 folio_mark_uptodate(folio);
3705 }
3706
3707#ifdef CONFIG_F2FS_FS_COMPRESSION
3708 /* overwrite compressed file */
3709 if (f2fs_compressed_file(inode) && fsdata) {
3710 f2fs_compress_write_end(inode, fsdata, folio->index, copied);
3711 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3712
3713 if (pos + copied > i_size_read(inode) &&
3714 !f2fs_verity_in_progress(inode))
3715 f2fs_i_size_write(inode, pos + copied);
3716 return copied;
3717 }
3718#endif
3719
3720 if (!copied)
3721 goto unlock_out;
3722
3723 folio_mark_dirty(folio);
3724
3725 if (f2fs_is_atomic_file(inode))
3726 set_page_private_atomic(folio_page(folio, 0));
3727
3728 if (pos + copied > i_size_read(inode) &&
3729 !f2fs_verity_in_progress(inode)) {
3730 f2fs_i_size_write(inode, pos + copied);
3731 if (f2fs_is_atomic_file(inode))
3732 f2fs_i_size_write(F2FS_I(inode)->cow_inode,
3733 pos + copied);
3734 }
3735unlock_out:
3736 folio_unlock(folio);
3737 folio_put(folio);
3738 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3739 return copied;
3740}
3741
3742void f2fs_invalidate_folio(struct folio *folio, size_t offset, size_t length)
3743{
3744 struct inode *inode = folio->mapping->host;
3745 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3746
3747 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
3748 (offset || length != folio_size(folio)))
3749 return;
3750
3751 if (folio_test_dirty(folio)) {
3752 if (inode->i_ino == F2FS_META_INO(sbi)) {
3753 dec_page_count(sbi, F2FS_DIRTY_META);
3754 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
3755 dec_page_count(sbi, F2FS_DIRTY_NODES);
3756 } else {
3757 inode_dec_dirty_pages(inode);
3758 f2fs_remove_dirty_inode(inode);
3759 }
3760 }
3761 clear_page_private_all(&folio->page);
3762}
3763
3764bool f2fs_release_folio(struct folio *folio, gfp_t wait)
3765{
3766 /* If this is dirty folio, keep private data */
3767 if (folio_test_dirty(folio))
3768 return false;
3769
3770 clear_page_private_all(&folio->page);
3771 return true;
3772}
3773
3774static bool f2fs_dirty_data_folio(struct address_space *mapping,
3775 struct folio *folio)
3776{
3777 struct inode *inode = mapping->host;
3778
3779 trace_f2fs_set_page_dirty(folio, DATA);
3780
3781 if (!folio_test_uptodate(folio))
3782 folio_mark_uptodate(folio);
3783 BUG_ON(folio_test_swapcache(folio));
3784
3785 if (filemap_dirty_folio(mapping, folio)) {
3786 f2fs_update_dirty_folio(inode, folio);
3787 return true;
3788 }
3789 return false;
3790}
3791
3792
3793static sector_t f2fs_bmap_compress(struct inode *inode, sector_t block)
3794{
3795#ifdef CONFIG_F2FS_FS_COMPRESSION
3796 struct dnode_of_data dn;
3797 sector_t start_idx, blknr = 0;
3798 int ret;
3799
3800 start_idx = round_down(block, F2FS_I(inode)->i_cluster_size);
3801
3802 set_new_dnode(&dn, inode, NULL, NULL, 0);
3803 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
3804 if (ret)
3805 return 0;
3806
3807 if (dn.data_blkaddr != COMPRESS_ADDR) {
3808 dn.ofs_in_node += block - start_idx;
3809 blknr = f2fs_data_blkaddr(&dn);
3810 if (!__is_valid_data_blkaddr(blknr))
3811 blknr = 0;
3812 }
3813
3814 f2fs_put_dnode(&dn);
3815 return blknr;
3816#else
3817 return 0;
3818#endif
3819}
3820
3821
3822static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
3823{
3824 struct inode *inode = mapping->host;
3825 sector_t blknr = 0;
3826
3827 if (f2fs_has_inline_data(inode))
3828 goto out;
3829
3830 /* make sure allocating whole blocks */
3831 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
3832 filemap_write_and_wait(mapping);
3833
3834 /* Block number less than F2FS MAX BLOCKS */
3835 if (unlikely(block >= max_file_blocks(inode)))
3836 goto out;
3837
3838 if (f2fs_compressed_file(inode)) {
3839 blknr = f2fs_bmap_compress(inode, block);
3840 } else {
3841 struct f2fs_map_blocks map;
3842
3843 memset(&map, 0, sizeof(map));
3844 map.m_lblk = block;
3845 map.m_len = 1;
3846 map.m_next_pgofs = NULL;
3847 map.m_seg_type = NO_CHECK_TYPE;
3848
3849 if (!f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_BMAP))
3850 blknr = map.m_pblk;
3851 }
3852out:
3853 trace_f2fs_bmap(inode, block, blknr);
3854 return blknr;
3855}
3856
3857#ifdef CONFIG_SWAP
3858static int f2fs_migrate_blocks(struct inode *inode, block_t start_blk,
3859 unsigned int blkcnt)
3860{
3861 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3862 unsigned int blkofs;
3863 unsigned int blk_per_sec = BLKS_PER_SEC(sbi);
3864 unsigned int end_blk = start_blk + blkcnt - 1;
3865 unsigned int secidx = start_blk / blk_per_sec;
3866 unsigned int end_sec;
3867 int ret = 0;
3868
3869 if (!blkcnt)
3870 return 0;
3871 end_sec = end_blk / blk_per_sec;
3872
3873 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3874 filemap_invalidate_lock(inode->i_mapping);
3875
3876 set_inode_flag(inode, FI_ALIGNED_WRITE);
3877 set_inode_flag(inode, FI_OPU_WRITE);
3878
3879 for (; secidx <= end_sec; secidx++) {
3880 unsigned int blkofs_end = secidx == end_sec ?
3881 end_blk % blk_per_sec : blk_per_sec - 1;
3882
3883 f2fs_down_write(&sbi->pin_sem);
3884
3885 ret = f2fs_allocate_pinning_section(sbi);
3886 if (ret) {
3887 f2fs_up_write(&sbi->pin_sem);
3888 break;
3889 }
3890
3891 set_inode_flag(inode, FI_SKIP_WRITES);
3892
3893 for (blkofs = 0; blkofs <= blkofs_end; blkofs++) {
3894 struct page *page;
3895 unsigned int blkidx = secidx * blk_per_sec + blkofs;
3896
3897 page = f2fs_get_lock_data_page(inode, blkidx, true);
3898 if (IS_ERR(page)) {
3899 f2fs_up_write(&sbi->pin_sem);
3900 ret = PTR_ERR(page);
3901 goto done;
3902 }
3903
3904 set_page_dirty(page);
3905 f2fs_put_page(page, 1);
3906 }
3907
3908 clear_inode_flag(inode, FI_SKIP_WRITES);
3909
3910 ret = filemap_fdatawrite(inode->i_mapping);
3911
3912 f2fs_up_write(&sbi->pin_sem);
3913
3914 if (ret)
3915 break;
3916 }
3917
3918done:
3919 clear_inode_flag(inode, FI_SKIP_WRITES);
3920 clear_inode_flag(inode, FI_OPU_WRITE);
3921 clear_inode_flag(inode, FI_ALIGNED_WRITE);
3922
3923 filemap_invalidate_unlock(inode->i_mapping);
3924 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3925
3926 return ret;
3927}
3928
3929static int check_swap_activate(struct swap_info_struct *sis,
3930 struct file *swap_file, sector_t *span)
3931{
3932 struct address_space *mapping = swap_file->f_mapping;
3933 struct inode *inode = mapping->host;
3934 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3935 block_t cur_lblock;
3936 block_t last_lblock;
3937 block_t pblock;
3938 block_t lowest_pblock = -1;
3939 block_t highest_pblock = 0;
3940 int nr_extents = 0;
3941 unsigned int nr_pblocks;
3942 unsigned int blks_per_sec = BLKS_PER_SEC(sbi);
3943 unsigned int not_aligned = 0;
3944 int ret = 0;
3945
3946 /*
3947 * Map all the blocks into the extent list. This code doesn't try
3948 * to be very smart.
3949 */
3950 cur_lblock = 0;
3951 last_lblock = F2FS_BYTES_TO_BLK(i_size_read(inode));
3952
3953 while (cur_lblock < last_lblock && cur_lblock < sis->max) {
3954 struct f2fs_map_blocks map;
3955retry:
3956 cond_resched();
3957
3958 memset(&map, 0, sizeof(map));
3959 map.m_lblk = cur_lblock;
3960 map.m_len = last_lblock - cur_lblock;
3961 map.m_next_pgofs = NULL;
3962 map.m_next_extent = NULL;
3963 map.m_seg_type = NO_CHECK_TYPE;
3964 map.m_may_create = false;
3965
3966 ret = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_FIEMAP);
3967 if (ret)
3968 goto out;
3969
3970 /* hole */
3971 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
3972 f2fs_err(sbi, "Swapfile has holes");
3973 ret = -EINVAL;
3974 goto out;
3975 }
3976
3977 pblock = map.m_pblk;
3978 nr_pblocks = map.m_len;
3979
3980 if ((pblock - SM_I(sbi)->main_blkaddr) % blks_per_sec ||
3981 nr_pblocks % blks_per_sec ||
3982 !f2fs_valid_pinned_area(sbi, pblock)) {
3983 bool last_extent = false;
3984
3985 not_aligned++;
3986
3987 nr_pblocks = roundup(nr_pblocks, blks_per_sec);
3988 if (cur_lblock + nr_pblocks > sis->max)
3989 nr_pblocks -= blks_per_sec;
3990
3991 /* this extent is last one */
3992 if (!nr_pblocks) {
3993 nr_pblocks = last_lblock - cur_lblock;
3994 last_extent = true;
3995 }
3996
3997 ret = f2fs_migrate_blocks(inode, cur_lblock,
3998 nr_pblocks);
3999 if (ret) {
4000 if (ret == -ENOENT)
4001 ret = -EINVAL;
4002 goto out;
4003 }
4004
4005 if (!last_extent)
4006 goto retry;
4007 }
4008
4009 if (cur_lblock + nr_pblocks >= sis->max)
4010 nr_pblocks = sis->max - cur_lblock;
4011
4012 if (cur_lblock) { /* exclude the header page */
4013 if (pblock < lowest_pblock)
4014 lowest_pblock = pblock;
4015 if (pblock + nr_pblocks - 1 > highest_pblock)
4016 highest_pblock = pblock + nr_pblocks - 1;
4017 }
4018
4019 /*
4020 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
4021 */
4022 ret = add_swap_extent(sis, cur_lblock, nr_pblocks, pblock);
4023 if (ret < 0)
4024 goto out;
4025 nr_extents += ret;
4026 cur_lblock += nr_pblocks;
4027 }
4028 ret = nr_extents;
4029 *span = 1 + highest_pblock - lowest_pblock;
4030 if (cur_lblock == 0)
4031 cur_lblock = 1; /* force Empty message */
4032 sis->max = cur_lblock;
4033 sis->pages = cur_lblock - 1;
4034out:
4035 if (not_aligned)
4036 f2fs_warn(sbi, "Swapfile (%u) is not align to section: 1) creat(), 2) ioctl(F2FS_IOC_SET_PIN_FILE), 3) fallocate(%lu * N)",
4037 not_aligned, blks_per_sec * F2FS_BLKSIZE);
4038 return ret;
4039}
4040
4041static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
4042 sector_t *span)
4043{
4044 struct inode *inode = file_inode(file);
4045 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4046 int ret;
4047
4048 if (!S_ISREG(inode->i_mode))
4049 return -EINVAL;
4050
4051 if (f2fs_readonly(sbi->sb))
4052 return -EROFS;
4053
4054 if (f2fs_lfs_mode(sbi) && !f2fs_sb_has_blkzoned(sbi)) {
4055 f2fs_err(sbi, "Swapfile not supported in LFS mode");
4056 return -EINVAL;
4057 }
4058
4059 ret = f2fs_convert_inline_inode(inode);
4060 if (ret)
4061 return ret;
4062
4063 if (!f2fs_disable_compressed_file(inode))
4064 return -EINVAL;
4065
4066 ret = filemap_fdatawrite(inode->i_mapping);
4067 if (ret < 0)
4068 return ret;
4069
4070 f2fs_precache_extents(inode);
4071
4072 ret = check_swap_activate(sis, file, span);
4073 if (ret < 0)
4074 return ret;
4075
4076 stat_inc_swapfile_inode(inode);
4077 set_inode_flag(inode, FI_PIN_FILE);
4078 f2fs_update_time(sbi, REQ_TIME);
4079 return ret;
4080}
4081
4082static void f2fs_swap_deactivate(struct file *file)
4083{
4084 struct inode *inode = file_inode(file);
4085
4086 stat_dec_swapfile_inode(inode);
4087 clear_inode_flag(inode, FI_PIN_FILE);
4088}
4089#else
4090static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
4091 sector_t *span)
4092{
4093 return -EOPNOTSUPP;
4094}
4095
4096static void f2fs_swap_deactivate(struct file *file)
4097{
4098}
4099#endif
4100
4101const struct address_space_operations f2fs_dblock_aops = {
4102 .read_folio = f2fs_read_data_folio,
4103 .readahead = f2fs_readahead,
4104 .writepage = f2fs_write_data_page,
4105 .writepages = f2fs_write_data_pages,
4106 .write_begin = f2fs_write_begin,
4107 .write_end = f2fs_write_end,
4108 .dirty_folio = f2fs_dirty_data_folio,
4109 .migrate_folio = filemap_migrate_folio,
4110 .invalidate_folio = f2fs_invalidate_folio,
4111 .release_folio = f2fs_release_folio,
4112 .bmap = f2fs_bmap,
4113 .swap_activate = f2fs_swap_activate,
4114 .swap_deactivate = f2fs_swap_deactivate,
4115};
4116
4117void f2fs_clear_page_cache_dirty_tag(struct folio *folio)
4118{
4119 struct address_space *mapping = folio->mapping;
4120 unsigned long flags;
4121
4122 xa_lock_irqsave(&mapping->i_pages, flags);
4123 __xa_clear_mark(&mapping->i_pages, folio->index,
4124 PAGECACHE_TAG_DIRTY);
4125 xa_unlock_irqrestore(&mapping->i_pages, flags);
4126}
4127
4128int __init f2fs_init_post_read_processing(void)
4129{
4130 bio_post_read_ctx_cache =
4131 kmem_cache_create("f2fs_bio_post_read_ctx",
4132 sizeof(struct bio_post_read_ctx), 0, 0, NULL);
4133 if (!bio_post_read_ctx_cache)
4134 goto fail;
4135 bio_post_read_ctx_pool =
4136 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
4137 bio_post_read_ctx_cache);
4138 if (!bio_post_read_ctx_pool)
4139 goto fail_free_cache;
4140 return 0;
4141
4142fail_free_cache:
4143 kmem_cache_destroy(bio_post_read_ctx_cache);
4144fail:
4145 return -ENOMEM;
4146}
4147
4148void f2fs_destroy_post_read_processing(void)
4149{
4150 mempool_destroy(bio_post_read_ctx_pool);
4151 kmem_cache_destroy(bio_post_read_ctx_cache);
4152}
4153
4154int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi)
4155{
4156 if (!f2fs_sb_has_encrypt(sbi) &&
4157 !f2fs_sb_has_verity(sbi) &&
4158 !f2fs_sb_has_compression(sbi))
4159 return 0;
4160
4161 sbi->post_read_wq = alloc_workqueue("f2fs_post_read_wq",
4162 WQ_UNBOUND | WQ_HIGHPRI,
4163 num_online_cpus());
4164 return sbi->post_read_wq ? 0 : -ENOMEM;
4165}
4166
4167void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi)
4168{
4169 if (sbi->post_read_wq)
4170 destroy_workqueue(sbi->post_read_wq);
4171}
4172
4173int __init f2fs_init_bio_entry_cache(void)
4174{
4175 bio_entry_slab = f2fs_kmem_cache_create("f2fs_bio_entry_slab",
4176 sizeof(struct bio_entry));
4177 return bio_entry_slab ? 0 : -ENOMEM;
4178}
4179
4180void f2fs_destroy_bio_entry_cache(void)
4181{
4182 kmem_cache_destroy(bio_entry_slab);
4183}
4184
4185static int f2fs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
4186 unsigned int flags, struct iomap *iomap,
4187 struct iomap *srcmap)
4188{
4189 struct f2fs_map_blocks map = {};
4190 pgoff_t next_pgofs = 0;
4191 int err;
4192
4193 map.m_lblk = F2FS_BYTES_TO_BLK(offset);
4194 map.m_len = F2FS_BYTES_TO_BLK(offset + length - 1) - map.m_lblk + 1;
4195 map.m_next_pgofs = &next_pgofs;
4196 map.m_seg_type = f2fs_rw_hint_to_seg_type(F2FS_I_SB(inode),
4197 inode->i_write_hint);
4198 if (flags & IOMAP_WRITE)
4199 map.m_may_create = true;
4200
4201 err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_DIO);
4202 if (err)
4203 return err;
4204
4205 iomap->offset = F2FS_BLK_TO_BYTES(map.m_lblk);
4206
4207 /*
4208 * When inline encryption is enabled, sometimes I/O to an encrypted file
4209 * has to be broken up to guarantee DUN contiguity. Handle this by
4210 * limiting the length of the mapping returned.
4211 */
4212 map.m_len = fscrypt_limit_io_blocks(inode, map.m_lblk, map.m_len);
4213
4214 /*
4215 * We should never see delalloc or compressed extents here based on
4216 * prior flushing and checks.
4217 */
4218 if (WARN_ON_ONCE(map.m_pblk == COMPRESS_ADDR))
4219 return -EINVAL;
4220
4221 if (map.m_flags & F2FS_MAP_MAPPED) {
4222 if (WARN_ON_ONCE(map.m_pblk == NEW_ADDR))
4223 return -EINVAL;
4224
4225 iomap->length = F2FS_BLK_TO_BYTES(map.m_len);
4226 iomap->type = IOMAP_MAPPED;
4227 iomap->flags |= IOMAP_F_MERGED;
4228 iomap->bdev = map.m_bdev;
4229 iomap->addr = F2FS_BLK_TO_BYTES(map.m_pblk);
4230 } else {
4231 if (flags & IOMAP_WRITE)
4232 return -ENOTBLK;
4233
4234 if (map.m_pblk == NULL_ADDR) {
4235 iomap->length = F2FS_BLK_TO_BYTES(next_pgofs) -
4236 iomap->offset;
4237 iomap->type = IOMAP_HOLE;
4238 } else if (map.m_pblk == NEW_ADDR) {
4239 iomap->length = F2FS_BLK_TO_BYTES(map.m_len);
4240 iomap->type = IOMAP_UNWRITTEN;
4241 } else {
4242 f2fs_bug_on(F2FS_I_SB(inode), 1);
4243 }
4244 iomap->addr = IOMAP_NULL_ADDR;
4245 }
4246
4247 if (map.m_flags & F2FS_MAP_NEW)
4248 iomap->flags |= IOMAP_F_NEW;
4249 if ((inode->i_state & I_DIRTY_DATASYNC) ||
4250 offset + length > i_size_read(inode))
4251 iomap->flags |= IOMAP_F_DIRTY;
4252
4253 return 0;
4254}
4255
4256const struct iomap_ops f2fs_iomap_ops = {
4257 .iomap_begin = f2fs_iomap_begin,
4258};