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