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