xref: /linux/fs/f2fs/data.c (revision 4b0aaacee51eb6592a03fdefd5ce97558518e291)
1 /*
2  * fs/f2fs/data.c
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5  *             http://www.samsung.com/
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11 #include <linux/fs.h>
12 #include <linux/f2fs_fs.h>
13 #include <linux/buffer_head.h>
14 #include <linux/mpage.h>
15 #include <linux/writeback.h>
16 #include <linux/backing-dev.h>
17 #include <linux/pagevec.h>
18 #include <linux/blkdev.h>
19 #include <linux/bio.h>
20 #include <linux/prefetch.h>
21 #include <linux/uio.h>
22 #include <linux/cleancache.h>
23 #include <linux/sched/signal.h>
24 
25 #include "f2fs.h"
26 #include "node.h"
27 #include "segment.h"
28 #include "trace.h"
29 #include <trace/events/f2fs.h>
30 
31 #define NUM_PREALLOC_POST_READ_CTXS	128
32 
33 static struct kmem_cache *bio_post_read_ctx_cache;
34 static mempool_t *bio_post_read_ctx_pool;
35 
36 static bool __is_cp_guaranteed(struct page *page)
37 {
38 	struct address_space *mapping = page->mapping;
39 	struct inode *inode;
40 	struct f2fs_sb_info *sbi;
41 
42 	if (!mapping)
43 		return false;
44 
45 	inode = mapping->host;
46 	sbi = F2FS_I_SB(inode);
47 
48 	if (inode->i_ino == F2FS_META_INO(sbi) ||
49 			inode->i_ino ==  F2FS_NODE_INO(sbi) ||
50 			S_ISDIR(inode->i_mode) ||
51 			(S_ISREG(inode->i_mode) &&
52 			is_inode_flag_set(inode, FI_ATOMIC_FILE)) ||
53 			is_cold_data(page))
54 		return true;
55 	return false;
56 }
57 
58 /* postprocessing steps for read bios */
59 enum bio_post_read_step {
60 	STEP_INITIAL = 0,
61 	STEP_DECRYPT,
62 };
63 
64 struct bio_post_read_ctx {
65 	struct bio *bio;
66 	struct work_struct work;
67 	unsigned int cur_step;
68 	unsigned int enabled_steps;
69 };
70 
71 static void __read_end_io(struct bio *bio)
72 {
73 	struct page *page;
74 	struct bio_vec *bv;
75 	int i;
76 
77 	bio_for_each_segment_all(bv, bio, i) {
78 		page = bv->bv_page;
79 
80 		/* PG_error was set if any post_read step failed */
81 		if (bio->bi_status || PageError(page)) {
82 			ClearPageUptodate(page);
83 			SetPageError(page);
84 		} else {
85 			SetPageUptodate(page);
86 		}
87 		unlock_page(page);
88 	}
89 	if (bio->bi_private)
90 		mempool_free(bio->bi_private, bio_post_read_ctx_pool);
91 	bio_put(bio);
92 }
93 
94 static void bio_post_read_processing(struct bio_post_read_ctx *ctx);
95 
96 static void decrypt_work(struct work_struct *work)
97 {
98 	struct bio_post_read_ctx *ctx =
99 		container_of(work, struct bio_post_read_ctx, work);
100 
101 	fscrypt_decrypt_bio(ctx->bio);
102 
103 	bio_post_read_processing(ctx);
104 }
105 
106 static void bio_post_read_processing(struct bio_post_read_ctx *ctx)
107 {
108 	switch (++ctx->cur_step) {
109 	case STEP_DECRYPT:
110 		if (ctx->enabled_steps & (1 << STEP_DECRYPT)) {
111 			INIT_WORK(&ctx->work, decrypt_work);
112 			fscrypt_enqueue_decrypt_work(&ctx->work);
113 			return;
114 		}
115 		ctx->cur_step++;
116 		/* fall-through */
117 	default:
118 		__read_end_io(ctx->bio);
119 	}
120 }
121 
122 static bool f2fs_bio_post_read_required(struct bio *bio)
123 {
124 	return bio->bi_private && !bio->bi_status;
125 }
126 
127 static void f2fs_read_end_io(struct bio *bio)
128 {
129 	if (time_to_inject(F2FS_P_SB(bio_first_page_all(bio)), FAULT_IO)) {
130 		f2fs_show_injection_info(FAULT_IO);
131 		bio->bi_status = BLK_STS_IOERR;
132 	}
133 
134 	if (f2fs_bio_post_read_required(bio)) {
135 		struct bio_post_read_ctx *ctx = bio->bi_private;
136 
137 		ctx->cur_step = STEP_INITIAL;
138 		bio_post_read_processing(ctx);
139 		return;
140 	}
141 
142 	__read_end_io(bio);
143 }
144 
145 static void f2fs_write_end_io(struct bio *bio)
146 {
147 	struct f2fs_sb_info *sbi = bio->bi_private;
148 	struct bio_vec *bvec;
149 	int i;
150 
151 	bio_for_each_segment_all(bvec, bio, i) {
152 		struct page *page = bvec->bv_page;
153 		enum count_type type = WB_DATA_TYPE(page);
154 
155 		if (IS_DUMMY_WRITTEN_PAGE(page)) {
156 			set_page_private(page, (unsigned long)NULL);
157 			ClearPagePrivate(page);
158 			unlock_page(page);
159 			mempool_free(page, sbi->write_io_dummy);
160 
161 			if (unlikely(bio->bi_status))
162 				f2fs_stop_checkpoint(sbi, true);
163 			continue;
164 		}
165 
166 		fscrypt_pullback_bio_page(&page, true);
167 
168 		if (unlikely(bio->bi_status)) {
169 			mapping_set_error(page->mapping, -EIO);
170 			if (type == F2FS_WB_CP_DATA)
171 				f2fs_stop_checkpoint(sbi, true);
172 		}
173 
174 		f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) &&
175 					page->index != nid_of_node(page));
176 
177 		dec_page_count(sbi, type);
178 		if (f2fs_in_warm_node_list(sbi, page))
179 			f2fs_del_fsync_node_entry(sbi, page);
180 		clear_cold_data(page);
181 		end_page_writeback(page);
182 	}
183 	if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
184 				wq_has_sleeper(&sbi->cp_wait))
185 		wake_up(&sbi->cp_wait);
186 
187 	bio_put(bio);
188 }
189 
190 /*
191  * Return true, if pre_bio's bdev is same as its target device.
192  */
193 struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
194 				block_t blk_addr, struct bio *bio)
195 {
196 	struct block_device *bdev = sbi->sb->s_bdev;
197 	int i;
198 
199 	for (i = 0; i < sbi->s_ndevs; i++) {
200 		if (FDEV(i).start_blk <= blk_addr &&
201 					FDEV(i).end_blk >= blk_addr) {
202 			blk_addr -= FDEV(i).start_blk;
203 			bdev = FDEV(i).bdev;
204 			break;
205 		}
206 	}
207 	if (bio) {
208 		bio_set_dev(bio, bdev);
209 		bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
210 	}
211 	return bdev;
212 }
213 
214 int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
215 {
216 	int i;
217 
218 	for (i = 0; i < sbi->s_ndevs; i++)
219 		if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
220 			return i;
221 	return 0;
222 }
223 
224 static bool __same_bdev(struct f2fs_sb_info *sbi,
225 				block_t blk_addr, struct bio *bio)
226 {
227 	struct block_device *b = f2fs_target_device(sbi, blk_addr, NULL);
228 	return bio->bi_disk == b->bd_disk && bio->bi_partno == b->bd_partno;
229 }
230 
231 /*
232  * Low-level block read/write IO operations.
233  */
234 static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr,
235 				struct writeback_control *wbc,
236 				int npages, bool is_read,
237 				enum page_type type, enum temp_type temp)
238 {
239 	struct bio *bio;
240 
241 	bio = f2fs_bio_alloc(sbi, npages, true);
242 
243 	f2fs_target_device(sbi, blk_addr, bio);
244 	if (is_read) {
245 		bio->bi_end_io = f2fs_read_end_io;
246 		bio->bi_private = NULL;
247 	} else {
248 		bio->bi_end_io = f2fs_write_end_io;
249 		bio->bi_private = sbi;
250 		bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi, type, temp);
251 	}
252 	if (wbc)
253 		wbc_init_bio(wbc, bio);
254 
255 	return bio;
256 }
257 
258 static inline void __submit_bio(struct f2fs_sb_info *sbi,
259 				struct bio *bio, enum page_type type)
260 {
261 	if (!is_read_io(bio_op(bio))) {
262 		unsigned int start;
263 
264 		if (type != DATA && type != NODE)
265 			goto submit_io;
266 
267 		if (test_opt(sbi, LFS) && current->plug)
268 			blk_finish_plug(current->plug);
269 
270 		start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
271 		start %= F2FS_IO_SIZE(sbi);
272 
273 		if (start == 0)
274 			goto submit_io;
275 
276 		/* fill dummy pages */
277 		for (; start < F2FS_IO_SIZE(sbi); start++) {
278 			struct page *page =
279 				mempool_alloc(sbi->write_io_dummy,
280 					GFP_NOIO | __GFP_ZERO | __GFP_NOFAIL);
281 			f2fs_bug_on(sbi, !page);
282 
283 			SetPagePrivate(page);
284 			set_page_private(page, (unsigned long)DUMMY_WRITTEN_PAGE);
285 			lock_page(page);
286 			if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
287 				f2fs_bug_on(sbi, 1);
288 		}
289 		/*
290 		 * In the NODE case, we lose next block address chain. So, we
291 		 * need to do checkpoint in f2fs_sync_file.
292 		 */
293 		if (type == NODE)
294 			set_sbi_flag(sbi, SBI_NEED_CP);
295 	}
296 submit_io:
297 	if (is_read_io(bio_op(bio)))
298 		trace_f2fs_submit_read_bio(sbi->sb, type, bio);
299 	else
300 		trace_f2fs_submit_write_bio(sbi->sb, type, bio);
301 	submit_bio(bio);
302 }
303 
304 static void __submit_merged_bio(struct f2fs_bio_info *io)
305 {
306 	struct f2fs_io_info *fio = &io->fio;
307 
308 	if (!io->bio)
309 		return;
310 
311 	bio_set_op_attrs(io->bio, fio->op, fio->op_flags);
312 
313 	if (is_read_io(fio->op))
314 		trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
315 	else
316 		trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
317 
318 	__submit_bio(io->sbi, io->bio, fio->type);
319 	io->bio = NULL;
320 }
321 
322 static bool __has_merged_page(struct f2fs_bio_info *io,
323 				struct inode *inode, nid_t ino, pgoff_t idx)
324 {
325 	struct bio_vec *bvec;
326 	struct page *target;
327 	int i;
328 
329 	if (!io->bio)
330 		return false;
331 
332 	if (!inode && !ino)
333 		return true;
334 
335 	bio_for_each_segment_all(bvec, io->bio, i) {
336 
337 		if (bvec->bv_page->mapping)
338 			target = bvec->bv_page;
339 		else
340 			target = fscrypt_control_page(bvec->bv_page);
341 
342 		if (idx != target->index)
343 			continue;
344 
345 		if (inode && inode == target->mapping->host)
346 			return true;
347 		if (ino && ino == ino_of_node(target))
348 			return true;
349 	}
350 
351 	return false;
352 }
353 
354 static bool has_merged_page(struct f2fs_sb_info *sbi, struct inode *inode,
355 				nid_t ino, pgoff_t idx, enum page_type type)
356 {
357 	enum page_type btype = PAGE_TYPE_OF_BIO(type);
358 	enum temp_type temp;
359 	struct f2fs_bio_info *io;
360 	bool ret = false;
361 
362 	for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
363 		io = sbi->write_io[btype] + temp;
364 
365 		down_read(&io->io_rwsem);
366 		ret = __has_merged_page(io, inode, ino, idx);
367 		up_read(&io->io_rwsem);
368 
369 		/* TODO: use HOT temp only for meta pages now. */
370 		if (ret || btype == META)
371 			break;
372 	}
373 	return ret;
374 }
375 
376 static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
377 				enum page_type type, enum temp_type temp)
378 {
379 	enum page_type btype = PAGE_TYPE_OF_BIO(type);
380 	struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
381 
382 	down_write(&io->io_rwsem);
383 
384 	/* change META to META_FLUSH in the checkpoint procedure */
385 	if (type >= META_FLUSH) {
386 		io->fio.type = META_FLUSH;
387 		io->fio.op = REQ_OP_WRITE;
388 		io->fio.op_flags = REQ_META | REQ_PRIO | REQ_SYNC;
389 		if (!test_opt(sbi, NOBARRIER))
390 			io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
391 	}
392 	__submit_merged_bio(io);
393 	up_write(&io->io_rwsem);
394 }
395 
396 static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
397 				struct inode *inode, nid_t ino, pgoff_t idx,
398 				enum page_type type, bool force)
399 {
400 	enum temp_type temp;
401 
402 	if (!force && !has_merged_page(sbi, inode, ino, idx, type))
403 		return;
404 
405 	for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
406 
407 		__f2fs_submit_merged_write(sbi, type, temp);
408 
409 		/* TODO: use HOT temp only for meta pages now. */
410 		if (type >= META)
411 			break;
412 	}
413 }
414 
415 void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
416 {
417 	__submit_merged_write_cond(sbi, NULL, 0, 0, type, true);
418 }
419 
420 void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
421 				struct inode *inode, nid_t ino, pgoff_t idx,
422 				enum page_type type)
423 {
424 	__submit_merged_write_cond(sbi, inode, ino, idx, type, false);
425 }
426 
427 void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
428 {
429 	f2fs_submit_merged_write(sbi, DATA);
430 	f2fs_submit_merged_write(sbi, NODE);
431 	f2fs_submit_merged_write(sbi, META);
432 }
433 
434 /*
435  * Fill the locked page with data located in the block address.
436  * A caller needs to unlock the page on failure.
437  */
438 int f2fs_submit_page_bio(struct f2fs_io_info *fio)
439 {
440 	struct bio *bio;
441 	struct page *page = fio->encrypted_page ?
442 			fio->encrypted_page : fio->page;
443 
444 	if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
445 			__is_meta_io(fio) ? META_GENERIC : DATA_GENERIC))
446 		return -EFAULT;
447 
448 	trace_f2fs_submit_page_bio(page, fio);
449 	f2fs_trace_ios(fio, 0);
450 
451 	/* Allocate a new bio */
452 	bio = __bio_alloc(fio->sbi, fio->new_blkaddr, fio->io_wbc,
453 				1, is_read_io(fio->op), fio->type, fio->temp);
454 
455 	if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
456 		bio_put(bio);
457 		return -EFAULT;
458 	}
459 	bio_set_op_attrs(bio, fio->op, fio->op_flags);
460 
461 	__submit_bio(fio->sbi, bio, fio->type);
462 
463 	if (!is_read_io(fio->op))
464 		inc_page_count(fio->sbi, WB_DATA_TYPE(fio->page));
465 	return 0;
466 }
467 
468 void f2fs_submit_page_write(struct f2fs_io_info *fio)
469 {
470 	struct f2fs_sb_info *sbi = fio->sbi;
471 	enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
472 	struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
473 	struct page *bio_page;
474 
475 	f2fs_bug_on(sbi, is_read_io(fio->op));
476 
477 	down_write(&io->io_rwsem);
478 next:
479 	if (fio->in_list) {
480 		spin_lock(&io->io_lock);
481 		if (list_empty(&io->io_list)) {
482 			spin_unlock(&io->io_lock);
483 			goto out;
484 		}
485 		fio = list_first_entry(&io->io_list,
486 						struct f2fs_io_info, list);
487 		list_del(&fio->list);
488 		spin_unlock(&io->io_lock);
489 	}
490 
491 	if (__is_valid_data_blkaddr(fio->old_blkaddr))
492 		verify_block_addr(fio, fio->old_blkaddr);
493 	verify_block_addr(fio, fio->new_blkaddr);
494 
495 	bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
496 
497 	/* set submitted = true as a return value */
498 	fio->submitted = true;
499 
500 	inc_page_count(sbi, WB_DATA_TYPE(bio_page));
501 
502 	if (io->bio && (io->last_block_in_bio != fio->new_blkaddr - 1 ||
503 	    (io->fio.op != fio->op || io->fio.op_flags != fio->op_flags) ||
504 			!__same_bdev(sbi, fio->new_blkaddr, io->bio)))
505 		__submit_merged_bio(io);
506 alloc_new:
507 	if (io->bio == NULL) {
508 		if ((fio->type == DATA || fio->type == NODE) &&
509 				fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
510 			dec_page_count(sbi, WB_DATA_TYPE(bio_page));
511 			fio->retry = true;
512 			goto skip;
513 		}
514 		io->bio = __bio_alloc(sbi, fio->new_blkaddr, fio->io_wbc,
515 						BIO_MAX_PAGES, false,
516 						fio->type, fio->temp);
517 		io->fio = *fio;
518 	}
519 
520 	if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
521 		__submit_merged_bio(io);
522 		goto alloc_new;
523 	}
524 
525 	if (fio->io_wbc)
526 		wbc_account_io(fio->io_wbc, bio_page, PAGE_SIZE);
527 
528 	io->last_block_in_bio = fio->new_blkaddr;
529 	f2fs_trace_ios(fio, 0);
530 
531 	trace_f2fs_submit_page_write(fio->page, fio);
532 skip:
533 	if (fio->in_list)
534 		goto next;
535 out:
536 	up_write(&io->io_rwsem);
537 }
538 
539 static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
540 					unsigned nr_pages, unsigned op_flag)
541 {
542 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
543 	struct bio *bio;
544 	struct bio_post_read_ctx *ctx;
545 	unsigned int post_read_steps = 0;
546 
547 	if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC))
548 		return ERR_PTR(-EFAULT);
549 
550 	bio = f2fs_bio_alloc(sbi, min_t(int, nr_pages, BIO_MAX_PAGES), false);
551 	if (!bio)
552 		return ERR_PTR(-ENOMEM);
553 	f2fs_target_device(sbi, blkaddr, bio);
554 	bio->bi_end_io = f2fs_read_end_io;
555 	bio_set_op_attrs(bio, REQ_OP_READ, op_flag);
556 
557 	if (f2fs_encrypted_file(inode))
558 		post_read_steps |= 1 << STEP_DECRYPT;
559 	if (post_read_steps) {
560 		ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
561 		if (!ctx) {
562 			bio_put(bio);
563 			return ERR_PTR(-ENOMEM);
564 		}
565 		ctx->bio = bio;
566 		ctx->enabled_steps = post_read_steps;
567 		bio->bi_private = ctx;
568 
569 		/* wait the page to be moved by cleaning */
570 		f2fs_wait_on_block_writeback(sbi, blkaddr);
571 	}
572 
573 	return bio;
574 }
575 
576 /* This can handle encryption stuffs */
577 static int f2fs_submit_page_read(struct inode *inode, struct page *page,
578 							block_t blkaddr)
579 {
580 	struct bio *bio = f2fs_grab_read_bio(inode, blkaddr, 1, 0);
581 
582 	if (IS_ERR(bio))
583 		return PTR_ERR(bio);
584 
585 	if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
586 		bio_put(bio);
587 		return -EFAULT;
588 	}
589 	__submit_bio(F2FS_I_SB(inode), bio, DATA);
590 	return 0;
591 }
592 
593 static void __set_data_blkaddr(struct dnode_of_data *dn)
594 {
595 	struct f2fs_node *rn = F2FS_NODE(dn->node_page);
596 	__le32 *addr_array;
597 	int base = 0;
598 
599 	if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
600 		base = get_extra_isize(dn->inode);
601 
602 	/* Get physical address of data block */
603 	addr_array = blkaddr_in_node(rn);
604 	addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
605 }
606 
607 /*
608  * Lock ordering for the change of data block address:
609  * ->data_page
610  *  ->node_page
611  *    update block addresses in the node page
612  */
613 void f2fs_set_data_blkaddr(struct dnode_of_data *dn)
614 {
615 	f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
616 	__set_data_blkaddr(dn);
617 	if (set_page_dirty(dn->node_page))
618 		dn->node_changed = true;
619 }
620 
621 void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
622 {
623 	dn->data_blkaddr = blkaddr;
624 	f2fs_set_data_blkaddr(dn);
625 	f2fs_update_extent_cache(dn);
626 }
627 
628 /* dn->ofs_in_node will be returned with up-to-date last block pointer */
629 int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
630 {
631 	struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
632 	int err;
633 
634 	if (!count)
635 		return 0;
636 
637 	if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
638 		return -EPERM;
639 	if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
640 		return err;
641 
642 	trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
643 						dn->ofs_in_node, count);
644 
645 	f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
646 
647 	for (; count > 0; dn->ofs_in_node++) {
648 		block_t blkaddr = datablock_addr(dn->inode,
649 					dn->node_page, dn->ofs_in_node);
650 		if (blkaddr == NULL_ADDR) {
651 			dn->data_blkaddr = NEW_ADDR;
652 			__set_data_blkaddr(dn);
653 			count--;
654 		}
655 	}
656 
657 	if (set_page_dirty(dn->node_page))
658 		dn->node_changed = true;
659 	return 0;
660 }
661 
662 /* Should keep dn->ofs_in_node unchanged */
663 int f2fs_reserve_new_block(struct dnode_of_data *dn)
664 {
665 	unsigned int ofs_in_node = dn->ofs_in_node;
666 	int ret;
667 
668 	ret = f2fs_reserve_new_blocks(dn, 1);
669 	dn->ofs_in_node = ofs_in_node;
670 	return ret;
671 }
672 
673 int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
674 {
675 	bool need_put = dn->inode_page ? false : true;
676 	int err;
677 
678 	err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE);
679 	if (err)
680 		return err;
681 
682 	if (dn->data_blkaddr == NULL_ADDR)
683 		err = f2fs_reserve_new_block(dn);
684 	if (err || need_put)
685 		f2fs_put_dnode(dn);
686 	return err;
687 }
688 
689 int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
690 {
691 	struct extent_info ei  = {0,0,0};
692 	struct inode *inode = dn->inode;
693 
694 	if (f2fs_lookup_extent_cache(inode, index, &ei)) {
695 		dn->data_blkaddr = ei.blk + index - ei.fofs;
696 		return 0;
697 	}
698 
699 	return f2fs_reserve_block(dn, index);
700 }
701 
702 struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
703 						int op_flags, bool for_write)
704 {
705 	struct address_space *mapping = inode->i_mapping;
706 	struct dnode_of_data dn;
707 	struct page *page;
708 	struct extent_info ei = {0,0,0};
709 	int err;
710 
711 	page = f2fs_grab_cache_page(mapping, index, for_write);
712 	if (!page)
713 		return ERR_PTR(-ENOMEM);
714 
715 	if (f2fs_lookup_extent_cache(inode, index, &ei)) {
716 		dn.data_blkaddr = ei.blk + index - ei.fofs;
717 		goto got_it;
718 	}
719 
720 	set_new_dnode(&dn, inode, NULL, NULL, 0);
721 	err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
722 	if (err)
723 		goto put_err;
724 	f2fs_put_dnode(&dn);
725 
726 	if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
727 		err = -ENOENT;
728 		goto put_err;
729 	}
730 got_it:
731 	if (PageUptodate(page)) {
732 		unlock_page(page);
733 		return page;
734 	}
735 
736 	/*
737 	 * A new dentry page is allocated but not able to be written, since its
738 	 * new inode page couldn't be allocated due to -ENOSPC.
739 	 * In such the case, its blkaddr can be remained as NEW_ADDR.
740 	 * see, f2fs_add_link -> f2fs_get_new_data_page ->
741 	 * f2fs_init_inode_metadata.
742 	 */
743 	if (dn.data_blkaddr == NEW_ADDR) {
744 		zero_user_segment(page, 0, PAGE_SIZE);
745 		if (!PageUptodate(page))
746 			SetPageUptodate(page);
747 		unlock_page(page);
748 		return page;
749 	}
750 
751 	err = f2fs_submit_page_read(inode, page, dn.data_blkaddr);
752 	if (err)
753 		goto put_err;
754 	return page;
755 
756 put_err:
757 	f2fs_put_page(page, 1);
758 	return ERR_PTR(err);
759 }
760 
761 struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index)
762 {
763 	struct address_space *mapping = inode->i_mapping;
764 	struct page *page;
765 
766 	page = find_get_page(mapping, index);
767 	if (page && PageUptodate(page))
768 		return page;
769 	f2fs_put_page(page, 0);
770 
771 	page = f2fs_get_read_data_page(inode, index, 0, false);
772 	if (IS_ERR(page))
773 		return page;
774 
775 	if (PageUptodate(page))
776 		return page;
777 
778 	wait_on_page_locked(page);
779 	if (unlikely(!PageUptodate(page))) {
780 		f2fs_put_page(page, 0);
781 		return ERR_PTR(-EIO);
782 	}
783 	return page;
784 }
785 
786 /*
787  * If it tries to access a hole, return an error.
788  * Because, the callers, functions in dir.c and GC, should be able to know
789  * whether this page exists or not.
790  */
791 struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
792 							bool for_write)
793 {
794 	struct address_space *mapping = inode->i_mapping;
795 	struct page *page;
796 repeat:
797 	page = f2fs_get_read_data_page(inode, index, 0, for_write);
798 	if (IS_ERR(page))
799 		return page;
800 
801 	/* wait for read completion */
802 	lock_page(page);
803 	if (unlikely(page->mapping != mapping)) {
804 		f2fs_put_page(page, 1);
805 		goto repeat;
806 	}
807 	if (unlikely(!PageUptodate(page))) {
808 		f2fs_put_page(page, 1);
809 		return ERR_PTR(-EIO);
810 	}
811 	return page;
812 }
813 
814 /*
815  * Caller ensures that this data page is never allocated.
816  * A new zero-filled data page is allocated in the page cache.
817  *
818  * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
819  * f2fs_unlock_op().
820  * Note that, ipage is set only by make_empty_dir, and if any error occur,
821  * ipage should be released by this function.
822  */
823 struct page *f2fs_get_new_data_page(struct inode *inode,
824 		struct page *ipage, pgoff_t index, bool new_i_size)
825 {
826 	struct address_space *mapping = inode->i_mapping;
827 	struct page *page;
828 	struct dnode_of_data dn;
829 	int err;
830 
831 	page = f2fs_grab_cache_page(mapping, index, true);
832 	if (!page) {
833 		/*
834 		 * before exiting, we should make sure ipage will be released
835 		 * if any error occur.
836 		 */
837 		f2fs_put_page(ipage, 1);
838 		return ERR_PTR(-ENOMEM);
839 	}
840 
841 	set_new_dnode(&dn, inode, ipage, NULL, 0);
842 	err = f2fs_reserve_block(&dn, index);
843 	if (err) {
844 		f2fs_put_page(page, 1);
845 		return ERR_PTR(err);
846 	}
847 	if (!ipage)
848 		f2fs_put_dnode(&dn);
849 
850 	if (PageUptodate(page))
851 		goto got_it;
852 
853 	if (dn.data_blkaddr == NEW_ADDR) {
854 		zero_user_segment(page, 0, PAGE_SIZE);
855 		if (!PageUptodate(page))
856 			SetPageUptodate(page);
857 	} else {
858 		f2fs_put_page(page, 1);
859 
860 		/* if ipage exists, blkaddr should be NEW_ADDR */
861 		f2fs_bug_on(F2FS_I_SB(inode), ipage);
862 		page = f2fs_get_lock_data_page(inode, index, true);
863 		if (IS_ERR(page))
864 			return page;
865 	}
866 got_it:
867 	if (new_i_size && i_size_read(inode) <
868 				((loff_t)(index + 1) << PAGE_SHIFT))
869 		f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
870 	return page;
871 }
872 
873 static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
874 {
875 	struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
876 	struct f2fs_summary sum;
877 	struct node_info ni;
878 	block_t old_blkaddr;
879 	pgoff_t fofs;
880 	blkcnt_t count = 1;
881 	int err;
882 
883 	if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
884 		return -EPERM;
885 
886 	err = f2fs_get_node_info(sbi, dn->nid, &ni);
887 	if (err)
888 		return err;
889 
890 	dn->data_blkaddr = datablock_addr(dn->inode,
891 				dn->node_page, dn->ofs_in_node);
892 	if (dn->data_blkaddr == NEW_ADDR)
893 		goto alloc;
894 
895 	if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
896 		return err;
897 
898 alloc:
899 	set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
900 	old_blkaddr = dn->data_blkaddr;
901 	f2fs_allocate_data_block(sbi, NULL, old_blkaddr, &dn->data_blkaddr,
902 					&sum, seg_type, NULL, false);
903 	if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
904 		invalidate_mapping_pages(META_MAPPING(sbi),
905 					old_blkaddr, old_blkaddr);
906 	f2fs_set_data_blkaddr(dn);
907 
908 	/* update i_size */
909 	fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
910 							dn->ofs_in_node;
911 	if (i_size_read(dn->inode) < ((loff_t)(fofs + 1) << PAGE_SHIFT))
912 		f2fs_i_size_write(dn->inode,
913 				((loff_t)(fofs + 1) << PAGE_SHIFT));
914 	return 0;
915 }
916 
917 int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
918 {
919 	struct inode *inode = file_inode(iocb->ki_filp);
920 	struct f2fs_map_blocks map;
921 	int flag;
922 	int err = 0;
923 	bool direct_io = iocb->ki_flags & IOCB_DIRECT;
924 
925 	/* convert inline data for Direct I/O*/
926 	if (direct_io) {
927 		err = f2fs_convert_inline_inode(inode);
928 		if (err)
929 			return err;
930 	}
931 
932 	if (is_inode_flag_set(inode, FI_NO_PREALLOC))
933 		return 0;
934 
935 	map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
936 	map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
937 	if (map.m_len > map.m_lblk)
938 		map.m_len -= map.m_lblk;
939 	else
940 		map.m_len = 0;
941 
942 	map.m_next_pgofs = NULL;
943 	map.m_next_extent = NULL;
944 	map.m_seg_type = NO_CHECK_TYPE;
945 
946 	if (direct_io) {
947 		map.m_seg_type = f2fs_rw_hint_to_seg_type(iocb->ki_hint);
948 		flag = f2fs_force_buffered_io(inode, WRITE) ?
949 					F2FS_GET_BLOCK_PRE_AIO :
950 					F2FS_GET_BLOCK_PRE_DIO;
951 		goto map_blocks;
952 	}
953 	if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA(inode)) {
954 		err = f2fs_convert_inline_inode(inode);
955 		if (err)
956 			return err;
957 	}
958 	if (f2fs_has_inline_data(inode))
959 		return err;
960 
961 	flag = F2FS_GET_BLOCK_PRE_AIO;
962 
963 map_blocks:
964 	err = f2fs_map_blocks(inode, &map, 1, flag);
965 	if (map.m_len > 0 && err == -ENOSPC) {
966 		if (!direct_io)
967 			set_inode_flag(inode, FI_NO_PREALLOC);
968 		err = 0;
969 	}
970 	return err;
971 }
972 
973 static inline void __do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
974 {
975 	if (flag == F2FS_GET_BLOCK_PRE_AIO) {
976 		if (lock)
977 			down_read(&sbi->node_change);
978 		else
979 			up_read(&sbi->node_change);
980 	} else {
981 		if (lock)
982 			f2fs_lock_op(sbi);
983 		else
984 			f2fs_unlock_op(sbi);
985 	}
986 }
987 
988 /*
989  * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
990  * f2fs_map_blocks structure.
991  * If original data blocks are allocated, then give them to blockdev.
992  * Otherwise,
993  *     a. preallocate requested block addresses
994  *     b. do not use extent cache for better performance
995  *     c. give the block addresses to blockdev
996  */
997 int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
998 						int create, int flag)
999 {
1000 	unsigned int maxblocks = map->m_len;
1001 	struct dnode_of_data dn;
1002 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1003 	int mode = create ? ALLOC_NODE : LOOKUP_NODE;
1004 	pgoff_t pgofs, end_offset, end;
1005 	int err = 0, ofs = 1;
1006 	unsigned int ofs_in_node, last_ofs_in_node;
1007 	blkcnt_t prealloc;
1008 	struct extent_info ei = {0,0,0};
1009 	block_t blkaddr;
1010 	unsigned int start_pgofs;
1011 
1012 	if (!maxblocks)
1013 		return 0;
1014 
1015 	map->m_len = 0;
1016 	map->m_flags = 0;
1017 
1018 	/* it only supports block size == page size */
1019 	pgofs =	(pgoff_t)map->m_lblk;
1020 	end = pgofs + maxblocks;
1021 
1022 	if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
1023 		map->m_pblk = ei.blk + pgofs - ei.fofs;
1024 		map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
1025 		map->m_flags = F2FS_MAP_MAPPED;
1026 		if (map->m_next_extent)
1027 			*map->m_next_extent = pgofs + map->m_len;
1028 		goto out;
1029 	}
1030 
1031 next_dnode:
1032 	if (create)
1033 		__do_map_lock(sbi, flag, true);
1034 
1035 	/* When reading holes, we need its node page */
1036 	set_new_dnode(&dn, inode, NULL, NULL, 0);
1037 	err = f2fs_get_dnode_of_data(&dn, pgofs, mode);
1038 	if (err) {
1039 		if (flag == F2FS_GET_BLOCK_BMAP)
1040 			map->m_pblk = 0;
1041 		if (err == -ENOENT) {
1042 			err = 0;
1043 			if (map->m_next_pgofs)
1044 				*map->m_next_pgofs =
1045 					f2fs_get_next_page_offset(&dn, pgofs);
1046 			if (map->m_next_extent)
1047 				*map->m_next_extent =
1048 					f2fs_get_next_page_offset(&dn, pgofs);
1049 		}
1050 		goto unlock_out;
1051 	}
1052 
1053 	start_pgofs = pgofs;
1054 	prealloc = 0;
1055 	last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
1056 	end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1057 
1058 next_block:
1059 	blkaddr = datablock_addr(dn.inode, dn.node_page, dn.ofs_in_node);
1060 
1061 	if (__is_valid_data_blkaddr(blkaddr) &&
1062 		!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC)) {
1063 		err = -EFAULT;
1064 		goto sync_out;
1065 	}
1066 
1067 	if (!is_valid_data_blkaddr(sbi, blkaddr)) {
1068 		if (create) {
1069 			if (unlikely(f2fs_cp_error(sbi))) {
1070 				err = -EIO;
1071 				goto sync_out;
1072 			}
1073 			if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1074 				if (blkaddr == NULL_ADDR) {
1075 					prealloc++;
1076 					last_ofs_in_node = dn.ofs_in_node;
1077 				}
1078 			} else {
1079 				err = __allocate_data_block(&dn,
1080 							map->m_seg_type);
1081 				if (!err)
1082 					set_inode_flag(inode, FI_APPEND_WRITE);
1083 			}
1084 			if (err)
1085 				goto sync_out;
1086 			map->m_flags |= F2FS_MAP_NEW;
1087 			blkaddr = dn.data_blkaddr;
1088 		} else {
1089 			if (flag == F2FS_GET_BLOCK_BMAP) {
1090 				map->m_pblk = 0;
1091 				goto sync_out;
1092 			}
1093 			if (flag == F2FS_GET_BLOCK_PRECACHE)
1094 				goto sync_out;
1095 			if (flag == F2FS_GET_BLOCK_FIEMAP &&
1096 						blkaddr == NULL_ADDR) {
1097 				if (map->m_next_pgofs)
1098 					*map->m_next_pgofs = pgofs + 1;
1099 				goto sync_out;
1100 			}
1101 			if (flag != F2FS_GET_BLOCK_FIEMAP) {
1102 				/* for defragment case */
1103 				if (map->m_next_pgofs)
1104 					*map->m_next_pgofs = pgofs + 1;
1105 				goto sync_out;
1106 			}
1107 		}
1108 	}
1109 
1110 	if (flag == F2FS_GET_BLOCK_PRE_AIO)
1111 		goto skip;
1112 
1113 	if (map->m_len == 0) {
1114 		/* preallocated unwritten block should be mapped for fiemap. */
1115 		if (blkaddr == NEW_ADDR)
1116 			map->m_flags |= F2FS_MAP_UNWRITTEN;
1117 		map->m_flags |= F2FS_MAP_MAPPED;
1118 
1119 		map->m_pblk = blkaddr;
1120 		map->m_len = 1;
1121 	} else if ((map->m_pblk != NEW_ADDR &&
1122 			blkaddr == (map->m_pblk + ofs)) ||
1123 			(map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
1124 			flag == F2FS_GET_BLOCK_PRE_DIO) {
1125 		ofs++;
1126 		map->m_len++;
1127 	} else {
1128 		goto sync_out;
1129 	}
1130 
1131 skip:
1132 	dn.ofs_in_node++;
1133 	pgofs++;
1134 
1135 	/* preallocate blocks in batch for one dnode page */
1136 	if (flag == F2FS_GET_BLOCK_PRE_AIO &&
1137 			(pgofs == end || dn.ofs_in_node == end_offset)) {
1138 
1139 		dn.ofs_in_node = ofs_in_node;
1140 		err = f2fs_reserve_new_blocks(&dn, prealloc);
1141 		if (err)
1142 			goto sync_out;
1143 
1144 		map->m_len += dn.ofs_in_node - ofs_in_node;
1145 		if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
1146 			err = -ENOSPC;
1147 			goto sync_out;
1148 		}
1149 		dn.ofs_in_node = end_offset;
1150 	}
1151 
1152 	if (pgofs >= end)
1153 		goto sync_out;
1154 	else if (dn.ofs_in_node < end_offset)
1155 		goto next_block;
1156 
1157 	if (flag == F2FS_GET_BLOCK_PRECACHE) {
1158 		if (map->m_flags & F2FS_MAP_MAPPED) {
1159 			unsigned int ofs = start_pgofs - map->m_lblk;
1160 
1161 			f2fs_update_extent_cache_range(&dn,
1162 				start_pgofs, map->m_pblk + ofs,
1163 				map->m_len - ofs);
1164 		}
1165 	}
1166 
1167 	f2fs_put_dnode(&dn);
1168 
1169 	if (create) {
1170 		__do_map_lock(sbi, flag, false);
1171 		f2fs_balance_fs(sbi, dn.node_changed);
1172 	}
1173 	goto next_dnode;
1174 
1175 sync_out:
1176 	if (flag == F2FS_GET_BLOCK_PRECACHE) {
1177 		if (map->m_flags & F2FS_MAP_MAPPED) {
1178 			unsigned int ofs = start_pgofs - map->m_lblk;
1179 
1180 			f2fs_update_extent_cache_range(&dn,
1181 				start_pgofs, map->m_pblk + ofs,
1182 				map->m_len - ofs);
1183 		}
1184 		if (map->m_next_extent)
1185 			*map->m_next_extent = pgofs + 1;
1186 	}
1187 	f2fs_put_dnode(&dn);
1188 unlock_out:
1189 	if (create) {
1190 		__do_map_lock(sbi, flag, false);
1191 		f2fs_balance_fs(sbi, dn.node_changed);
1192 	}
1193 out:
1194 	trace_f2fs_map_blocks(inode, map, err);
1195 	return err;
1196 }
1197 
1198 bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
1199 {
1200 	struct f2fs_map_blocks map;
1201 	block_t last_lblk;
1202 	int err;
1203 
1204 	if (pos + len > i_size_read(inode))
1205 		return false;
1206 
1207 	map.m_lblk = F2FS_BYTES_TO_BLK(pos);
1208 	map.m_next_pgofs = NULL;
1209 	map.m_next_extent = NULL;
1210 	map.m_seg_type = NO_CHECK_TYPE;
1211 	last_lblk = F2FS_BLK_ALIGN(pos + len);
1212 
1213 	while (map.m_lblk < last_lblk) {
1214 		map.m_len = last_lblk - map.m_lblk;
1215 		err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
1216 		if (err || map.m_len == 0)
1217 			return false;
1218 		map.m_lblk += map.m_len;
1219 	}
1220 	return true;
1221 }
1222 
1223 static int __get_data_block(struct inode *inode, sector_t iblock,
1224 			struct buffer_head *bh, int create, int flag,
1225 			pgoff_t *next_pgofs, int seg_type)
1226 {
1227 	struct f2fs_map_blocks map;
1228 	int err;
1229 
1230 	map.m_lblk = iblock;
1231 	map.m_len = bh->b_size >> inode->i_blkbits;
1232 	map.m_next_pgofs = next_pgofs;
1233 	map.m_next_extent = NULL;
1234 	map.m_seg_type = seg_type;
1235 
1236 	err = f2fs_map_blocks(inode, &map, create, flag);
1237 	if (!err) {
1238 		map_bh(bh, inode->i_sb, map.m_pblk);
1239 		bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
1240 		bh->b_size = (u64)map.m_len << inode->i_blkbits;
1241 	}
1242 	return err;
1243 }
1244 
1245 static int get_data_block(struct inode *inode, sector_t iblock,
1246 			struct buffer_head *bh_result, int create, int flag,
1247 			pgoff_t *next_pgofs)
1248 {
1249 	return __get_data_block(inode, iblock, bh_result, create,
1250 							flag, next_pgofs,
1251 							NO_CHECK_TYPE);
1252 }
1253 
1254 static int get_data_block_dio(struct inode *inode, sector_t iblock,
1255 			struct buffer_head *bh_result, int create)
1256 {
1257 	return __get_data_block(inode, iblock, bh_result, create,
1258 						F2FS_GET_BLOCK_DEFAULT, NULL,
1259 						f2fs_rw_hint_to_seg_type(
1260 							inode->i_write_hint));
1261 }
1262 
1263 static int get_data_block_bmap(struct inode *inode, sector_t iblock,
1264 			struct buffer_head *bh_result, int create)
1265 {
1266 	/* Block number less than F2FS MAX BLOCKS */
1267 	if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
1268 		return -EFBIG;
1269 
1270 	return __get_data_block(inode, iblock, bh_result, create,
1271 						F2FS_GET_BLOCK_BMAP, NULL,
1272 						NO_CHECK_TYPE);
1273 }
1274 
1275 static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
1276 {
1277 	return (offset >> inode->i_blkbits);
1278 }
1279 
1280 static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
1281 {
1282 	return (blk << inode->i_blkbits);
1283 }
1284 
1285 static int f2fs_xattr_fiemap(struct inode *inode,
1286 				struct fiemap_extent_info *fieinfo)
1287 {
1288 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1289 	struct page *page;
1290 	struct node_info ni;
1291 	__u64 phys = 0, len;
1292 	__u32 flags;
1293 	nid_t xnid = F2FS_I(inode)->i_xattr_nid;
1294 	int err = 0;
1295 
1296 	if (f2fs_has_inline_xattr(inode)) {
1297 		int offset;
1298 
1299 		page = f2fs_grab_cache_page(NODE_MAPPING(sbi),
1300 						inode->i_ino, false);
1301 		if (!page)
1302 			return -ENOMEM;
1303 
1304 		err = f2fs_get_node_info(sbi, inode->i_ino, &ni);
1305 		if (err) {
1306 			f2fs_put_page(page, 1);
1307 			return err;
1308 		}
1309 
1310 		phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1311 		offset = offsetof(struct f2fs_inode, i_addr) +
1312 					sizeof(__le32) * (DEF_ADDRS_PER_INODE -
1313 					get_inline_xattr_addrs(inode));
1314 
1315 		phys += offset;
1316 		len = inline_xattr_size(inode);
1317 
1318 		f2fs_put_page(page, 1);
1319 
1320 		flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED;
1321 
1322 		if (!xnid)
1323 			flags |= FIEMAP_EXTENT_LAST;
1324 
1325 		err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1326 		if (err || err == 1)
1327 			return err;
1328 	}
1329 
1330 	if (xnid) {
1331 		page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false);
1332 		if (!page)
1333 			return -ENOMEM;
1334 
1335 		err = f2fs_get_node_info(sbi, xnid, &ni);
1336 		if (err) {
1337 			f2fs_put_page(page, 1);
1338 			return err;
1339 		}
1340 
1341 		phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1342 		len = inode->i_sb->s_blocksize;
1343 
1344 		f2fs_put_page(page, 1);
1345 
1346 		flags = FIEMAP_EXTENT_LAST;
1347 	}
1348 
1349 	if (phys)
1350 		err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1351 
1352 	return (err < 0 ? err : 0);
1353 }
1354 
1355 int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1356 		u64 start, u64 len)
1357 {
1358 	struct buffer_head map_bh;
1359 	sector_t start_blk, last_blk;
1360 	pgoff_t next_pgofs;
1361 	u64 logical = 0, phys = 0, size = 0;
1362 	u32 flags = 0;
1363 	int ret = 0;
1364 
1365 	if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
1366 		ret = f2fs_precache_extents(inode);
1367 		if (ret)
1368 			return ret;
1369 	}
1370 
1371 	ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR);
1372 	if (ret)
1373 		return ret;
1374 
1375 	inode_lock(inode);
1376 
1377 	if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1378 		ret = f2fs_xattr_fiemap(inode, fieinfo);
1379 		goto out;
1380 	}
1381 
1382 	if (f2fs_has_inline_data(inode)) {
1383 		ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1384 		if (ret != -EAGAIN)
1385 			goto out;
1386 	}
1387 
1388 	if (logical_to_blk(inode, len) == 0)
1389 		len = blk_to_logical(inode, 1);
1390 
1391 	start_blk = logical_to_blk(inode, start);
1392 	last_blk = logical_to_blk(inode, start + len - 1);
1393 
1394 next:
1395 	memset(&map_bh, 0, sizeof(struct buffer_head));
1396 	map_bh.b_size = len;
1397 
1398 	ret = get_data_block(inode, start_blk, &map_bh, 0,
1399 					F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
1400 	if (ret)
1401 		goto out;
1402 
1403 	/* HOLE */
1404 	if (!buffer_mapped(&map_bh)) {
1405 		start_blk = next_pgofs;
1406 
1407 		if (blk_to_logical(inode, start_blk) < blk_to_logical(inode,
1408 					F2FS_I_SB(inode)->max_file_blocks))
1409 			goto prep_next;
1410 
1411 		flags |= FIEMAP_EXTENT_LAST;
1412 	}
1413 
1414 	if (size) {
1415 		if (f2fs_encrypted_inode(inode))
1416 			flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1417 
1418 		ret = fiemap_fill_next_extent(fieinfo, logical,
1419 				phys, size, flags);
1420 	}
1421 
1422 	if (start_blk > last_blk || ret)
1423 		goto out;
1424 
1425 	logical = blk_to_logical(inode, start_blk);
1426 	phys = blk_to_logical(inode, map_bh.b_blocknr);
1427 	size = map_bh.b_size;
1428 	flags = 0;
1429 	if (buffer_unwritten(&map_bh))
1430 		flags = FIEMAP_EXTENT_UNWRITTEN;
1431 
1432 	start_blk += logical_to_blk(inode, size);
1433 
1434 prep_next:
1435 	cond_resched();
1436 	if (fatal_signal_pending(current))
1437 		ret = -EINTR;
1438 	else
1439 		goto next;
1440 out:
1441 	if (ret == 1)
1442 		ret = 0;
1443 
1444 	inode_unlock(inode);
1445 	return ret;
1446 }
1447 
1448 /*
1449  * This function was originally taken from fs/mpage.c, and customized for f2fs.
1450  * Major change was from block_size == page_size in f2fs by default.
1451  *
1452  * Note that the aops->readpages() function is ONLY used for read-ahead. If
1453  * this function ever deviates from doing just read-ahead, it should either
1454  * use ->readpage() or do the necessary surgery to decouple ->readpages()
1455  * from read-ahead.
1456  */
1457 static int f2fs_mpage_readpages(struct address_space *mapping,
1458 			struct list_head *pages, struct page *page,
1459 			unsigned nr_pages, bool is_readahead)
1460 {
1461 	struct bio *bio = NULL;
1462 	sector_t last_block_in_bio = 0;
1463 	struct inode *inode = mapping->host;
1464 	const unsigned blkbits = inode->i_blkbits;
1465 	const unsigned blocksize = 1 << blkbits;
1466 	sector_t block_in_file;
1467 	sector_t last_block;
1468 	sector_t last_block_in_file;
1469 	sector_t block_nr;
1470 	struct f2fs_map_blocks map;
1471 
1472 	map.m_pblk = 0;
1473 	map.m_lblk = 0;
1474 	map.m_len = 0;
1475 	map.m_flags = 0;
1476 	map.m_next_pgofs = NULL;
1477 	map.m_next_extent = NULL;
1478 	map.m_seg_type = NO_CHECK_TYPE;
1479 
1480 	for (; nr_pages; nr_pages--) {
1481 		if (pages) {
1482 			page = list_last_entry(pages, struct page, lru);
1483 
1484 			prefetchw(&page->flags);
1485 			list_del(&page->lru);
1486 			if (add_to_page_cache_lru(page, mapping,
1487 						  page->index,
1488 						  readahead_gfp_mask(mapping)))
1489 				goto next_page;
1490 		}
1491 
1492 		block_in_file = (sector_t)page->index;
1493 		last_block = block_in_file + nr_pages;
1494 		last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
1495 								blkbits;
1496 		if (last_block > last_block_in_file)
1497 			last_block = last_block_in_file;
1498 
1499 		/*
1500 		 * Map blocks using the previous result first.
1501 		 */
1502 		if ((map.m_flags & F2FS_MAP_MAPPED) &&
1503 				block_in_file > map.m_lblk &&
1504 				block_in_file < (map.m_lblk + map.m_len))
1505 			goto got_it;
1506 
1507 		/*
1508 		 * Then do more f2fs_map_blocks() calls until we are
1509 		 * done with this page.
1510 		 */
1511 		map.m_flags = 0;
1512 
1513 		if (block_in_file < last_block) {
1514 			map.m_lblk = block_in_file;
1515 			map.m_len = last_block - block_in_file;
1516 
1517 			if (f2fs_map_blocks(inode, &map, 0,
1518 						F2FS_GET_BLOCK_DEFAULT))
1519 				goto set_error_page;
1520 		}
1521 got_it:
1522 		if ((map.m_flags & F2FS_MAP_MAPPED)) {
1523 			block_nr = map.m_pblk + block_in_file - map.m_lblk;
1524 			SetPageMappedToDisk(page);
1525 
1526 			if (!PageUptodate(page) && !cleancache_get_page(page)) {
1527 				SetPageUptodate(page);
1528 				goto confused;
1529 			}
1530 
1531 			if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr,
1532 								DATA_GENERIC))
1533 				goto set_error_page;
1534 		} else {
1535 			zero_user_segment(page, 0, PAGE_SIZE);
1536 			if (!PageUptodate(page))
1537 				SetPageUptodate(page);
1538 			unlock_page(page);
1539 			goto next_page;
1540 		}
1541 
1542 		/*
1543 		 * This page will go to BIO.  Do we need to send this
1544 		 * BIO off first?
1545 		 */
1546 		if (bio && (last_block_in_bio != block_nr - 1 ||
1547 			!__same_bdev(F2FS_I_SB(inode), block_nr, bio))) {
1548 submit_and_realloc:
1549 			__submit_bio(F2FS_I_SB(inode), bio, DATA);
1550 			bio = NULL;
1551 		}
1552 		if (bio == NULL) {
1553 			bio = f2fs_grab_read_bio(inode, block_nr, nr_pages,
1554 					is_readahead ? REQ_RAHEAD : 0);
1555 			if (IS_ERR(bio)) {
1556 				bio = NULL;
1557 				goto set_error_page;
1558 			}
1559 		}
1560 
1561 		if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1562 			goto submit_and_realloc;
1563 
1564 		last_block_in_bio = block_nr;
1565 		goto next_page;
1566 set_error_page:
1567 		SetPageError(page);
1568 		zero_user_segment(page, 0, PAGE_SIZE);
1569 		unlock_page(page);
1570 		goto next_page;
1571 confused:
1572 		if (bio) {
1573 			__submit_bio(F2FS_I_SB(inode), bio, DATA);
1574 			bio = NULL;
1575 		}
1576 		unlock_page(page);
1577 next_page:
1578 		if (pages)
1579 			put_page(page);
1580 	}
1581 	BUG_ON(pages && !list_empty(pages));
1582 	if (bio)
1583 		__submit_bio(F2FS_I_SB(inode), bio, DATA);
1584 	return 0;
1585 }
1586 
1587 static int f2fs_read_data_page(struct file *file, struct page *page)
1588 {
1589 	struct inode *inode = page->mapping->host;
1590 	int ret = -EAGAIN;
1591 
1592 	trace_f2fs_readpage(page, DATA);
1593 
1594 	/* If the file has inline data, try to read it directly */
1595 	if (f2fs_has_inline_data(inode))
1596 		ret = f2fs_read_inline_data(inode, page);
1597 	if (ret == -EAGAIN)
1598 		ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1, false);
1599 	return ret;
1600 }
1601 
1602 static int f2fs_read_data_pages(struct file *file,
1603 			struct address_space *mapping,
1604 			struct list_head *pages, unsigned nr_pages)
1605 {
1606 	struct inode *inode = mapping->host;
1607 	struct page *page = list_last_entry(pages, struct page, lru);
1608 
1609 	trace_f2fs_readpages(inode, page, nr_pages);
1610 
1611 	/* If the file has inline data, skip readpages */
1612 	if (f2fs_has_inline_data(inode))
1613 		return 0;
1614 
1615 	return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages, true);
1616 }
1617 
1618 static int encrypt_one_page(struct f2fs_io_info *fio)
1619 {
1620 	struct inode *inode = fio->page->mapping->host;
1621 	struct page *mpage;
1622 	gfp_t gfp_flags = GFP_NOFS;
1623 
1624 	if (!f2fs_encrypted_file(inode))
1625 		return 0;
1626 
1627 	/* wait for GCed page writeback via META_MAPPING */
1628 	f2fs_wait_on_block_writeback(fio->sbi, fio->old_blkaddr);
1629 
1630 retry_encrypt:
1631 	fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page,
1632 			PAGE_SIZE, 0, fio->page->index, gfp_flags);
1633 	if (IS_ERR(fio->encrypted_page)) {
1634 		/* flush pending IOs and wait for a while in the ENOMEM case */
1635 		if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
1636 			f2fs_flush_merged_writes(fio->sbi);
1637 			congestion_wait(BLK_RW_ASYNC, HZ/50);
1638 			gfp_flags |= __GFP_NOFAIL;
1639 			goto retry_encrypt;
1640 		}
1641 		return PTR_ERR(fio->encrypted_page);
1642 	}
1643 
1644 	mpage = find_lock_page(META_MAPPING(fio->sbi), fio->old_blkaddr);
1645 	if (mpage) {
1646 		if (PageUptodate(mpage))
1647 			memcpy(page_address(mpage),
1648 				page_address(fio->encrypted_page), PAGE_SIZE);
1649 		f2fs_put_page(mpage, 1);
1650 	}
1651 	return 0;
1652 }
1653 
1654 static inline bool check_inplace_update_policy(struct inode *inode,
1655 				struct f2fs_io_info *fio)
1656 {
1657 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1658 	unsigned int policy = SM_I(sbi)->ipu_policy;
1659 
1660 	if (policy & (0x1 << F2FS_IPU_FORCE))
1661 		return true;
1662 	if (policy & (0x1 << F2FS_IPU_SSR) && f2fs_need_SSR(sbi))
1663 		return true;
1664 	if (policy & (0x1 << F2FS_IPU_UTIL) &&
1665 			utilization(sbi) > SM_I(sbi)->min_ipu_util)
1666 		return true;
1667 	if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && f2fs_need_SSR(sbi) &&
1668 			utilization(sbi) > SM_I(sbi)->min_ipu_util)
1669 		return true;
1670 
1671 	/*
1672 	 * IPU for rewrite async pages
1673 	 */
1674 	if (policy & (0x1 << F2FS_IPU_ASYNC) &&
1675 			fio && fio->op == REQ_OP_WRITE &&
1676 			!(fio->op_flags & REQ_SYNC) &&
1677 			!f2fs_encrypted_inode(inode))
1678 		return true;
1679 
1680 	/* this is only set during fdatasync */
1681 	if (policy & (0x1 << F2FS_IPU_FSYNC) &&
1682 			is_inode_flag_set(inode, FI_NEED_IPU))
1683 		return true;
1684 
1685 	return false;
1686 }
1687 
1688 bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio)
1689 {
1690 	if (f2fs_is_pinned_file(inode))
1691 		return true;
1692 
1693 	/* if this is cold file, we should overwrite to avoid fragmentation */
1694 	if (file_is_cold(inode))
1695 		return true;
1696 
1697 	return check_inplace_update_policy(inode, fio);
1698 }
1699 
1700 bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
1701 {
1702 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1703 
1704 	if (test_opt(sbi, LFS))
1705 		return true;
1706 	if (S_ISDIR(inode->i_mode))
1707 		return true;
1708 	if (f2fs_is_atomic_file(inode))
1709 		return true;
1710 	if (fio) {
1711 		if (is_cold_data(fio->page))
1712 			return true;
1713 		if (IS_ATOMIC_WRITTEN_PAGE(fio->page))
1714 			return true;
1715 	}
1716 	return false;
1717 }
1718 
1719 static inline bool need_inplace_update(struct f2fs_io_info *fio)
1720 {
1721 	struct inode *inode = fio->page->mapping->host;
1722 
1723 	if (f2fs_should_update_outplace(inode, fio))
1724 		return false;
1725 
1726 	return f2fs_should_update_inplace(inode, fio);
1727 }
1728 
1729 int f2fs_do_write_data_page(struct f2fs_io_info *fio)
1730 {
1731 	struct page *page = fio->page;
1732 	struct inode *inode = page->mapping->host;
1733 	struct dnode_of_data dn;
1734 	struct extent_info ei = {0,0,0};
1735 	struct node_info ni;
1736 	bool ipu_force = false;
1737 	int err = 0;
1738 
1739 	set_new_dnode(&dn, inode, NULL, NULL, 0);
1740 	if (need_inplace_update(fio) &&
1741 			f2fs_lookup_extent_cache(inode, page->index, &ei)) {
1742 		fio->old_blkaddr = ei.blk + page->index - ei.fofs;
1743 
1744 		if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
1745 							DATA_GENERIC))
1746 			return -EFAULT;
1747 
1748 		ipu_force = true;
1749 		fio->need_lock = LOCK_DONE;
1750 		goto got_it;
1751 	}
1752 
1753 	/* Deadlock due to between page->lock and f2fs_lock_op */
1754 	if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
1755 		return -EAGAIN;
1756 
1757 	err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
1758 	if (err)
1759 		goto out;
1760 
1761 	fio->old_blkaddr = dn.data_blkaddr;
1762 
1763 	/* This page is already truncated */
1764 	if (fio->old_blkaddr == NULL_ADDR) {
1765 		ClearPageUptodate(page);
1766 		goto out_writepage;
1767 	}
1768 got_it:
1769 	if (__is_valid_data_blkaddr(fio->old_blkaddr) &&
1770 		!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
1771 							DATA_GENERIC)) {
1772 		err = -EFAULT;
1773 		goto out_writepage;
1774 	}
1775 	/*
1776 	 * If current allocation needs SSR,
1777 	 * it had better in-place writes for updated data.
1778 	 */
1779 	if (ipu_force || (is_valid_data_blkaddr(fio->sbi, fio->old_blkaddr) &&
1780 					need_inplace_update(fio))) {
1781 		err = encrypt_one_page(fio);
1782 		if (err)
1783 			goto out_writepage;
1784 
1785 		set_page_writeback(page);
1786 		ClearPageError(page);
1787 		f2fs_put_dnode(&dn);
1788 		if (fio->need_lock == LOCK_REQ)
1789 			f2fs_unlock_op(fio->sbi);
1790 		err = f2fs_inplace_write_data(fio);
1791 		trace_f2fs_do_write_data_page(fio->page, IPU);
1792 		set_inode_flag(inode, FI_UPDATE_WRITE);
1793 		return err;
1794 	}
1795 
1796 	if (fio->need_lock == LOCK_RETRY) {
1797 		if (!f2fs_trylock_op(fio->sbi)) {
1798 			err = -EAGAIN;
1799 			goto out_writepage;
1800 		}
1801 		fio->need_lock = LOCK_REQ;
1802 	}
1803 
1804 	err = f2fs_get_node_info(fio->sbi, dn.nid, &ni);
1805 	if (err)
1806 		goto out_writepage;
1807 
1808 	fio->version = ni.version;
1809 
1810 	err = encrypt_one_page(fio);
1811 	if (err)
1812 		goto out_writepage;
1813 
1814 	set_page_writeback(page);
1815 	ClearPageError(page);
1816 
1817 	/* LFS mode write path */
1818 	f2fs_outplace_write_data(&dn, fio);
1819 	trace_f2fs_do_write_data_page(page, OPU);
1820 	set_inode_flag(inode, FI_APPEND_WRITE);
1821 	if (page->index == 0)
1822 		set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
1823 out_writepage:
1824 	f2fs_put_dnode(&dn);
1825 out:
1826 	if (fio->need_lock == LOCK_REQ)
1827 		f2fs_unlock_op(fio->sbi);
1828 	return err;
1829 }
1830 
1831 static int __write_data_page(struct page *page, bool *submitted,
1832 				struct writeback_control *wbc,
1833 				enum iostat_type io_type)
1834 {
1835 	struct inode *inode = page->mapping->host;
1836 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1837 	loff_t i_size = i_size_read(inode);
1838 	const pgoff_t end_index = ((unsigned long long) i_size)
1839 							>> PAGE_SHIFT;
1840 	loff_t psize = (page->index + 1) << PAGE_SHIFT;
1841 	unsigned offset = 0;
1842 	bool need_balance_fs = false;
1843 	int err = 0;
1844 	struct f2fs_io_info fio = {
1845 		.sbi = sbi,
1846 		.ino = inode->i_ino,
1847 		.type = DATA,
1848 		.op = REQ_OP_WRITE,
1849 		.op_flags = wbc_to_write_flags(wbc),
1850 		.old_blkaddr = NULL_ADDR,
1851 		.page = page,
1852 		.encrypted_page = NULL,
1853 		.submitted = false,
1854 		.need_lock = LOCK_RETRY,
1855 		.io_type = io_type,
1856 		.io_wbc = wbc,
1857 	};
1858 
1859 	trace_f2fs_writepage(page, DATA);
1860 
1861 	/* we should bypass data pages to proceed the kworkder jobs */
1862 	if (unlikely(f2fs_cp_error(sbi))) {
1863 		mapping_set_error(page->mapping, -EIO);
1864 		/*
1865 		 * don't drop any dirty dentry pages for keeping lastest
1866 		 * directory structure.
1867 		 */
1868 		if (S_ISDIR(inode->i_mode))
1869 			goto redirty_out;
1870 		goto out;
1871 	}
1872 
1873 	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1874 		goto redirty_out;
1875 
1876 	if (page->index < end_index)
1877 		goto write;
1878 
1879 	/*
1880 	 * If the offset is out-of-range of file size,
1881 	 * this page does not have to be written to disk.
1882 	 */
1883 	offset = i_size & (PAGE_SIZE - 1);
1884 	if ((page->index >= end_index + 1) || !offset)
1885 		goto out;
1886 
1887 	zero_user_segment(page, offset, PAGE_SIZE);
1888 write:
1889 	if (f2fs_is_drop_cache(inode))
1890 		goto out;
1891 	/* we should not write 0'th page having journal header */
1892 	if (f2fs_is_volatile_file(inode) && (!page->index ||
1893 			(!wbc->for_reclaim &&
1894 			f2fs_available_free_memory(sbi, BASE_CHECK))))
1895 		goto redirty_out;
1896 
1897 	/* Dentry blocks are controlled by checkpoint */
1898 	if (S_ISDIR(inode->i_mode)) {
1899 		fio.need_lock = LOCK_DONE;
1900 		err = f2fs_do_write_data_page(&fio);
1901 		goto done;
1902 	}
1903 
1904 	if (!wbc->for_reclaim)
1905 		need_balance_fs = true;
1906 	else if (has_not_enough_free_secs(sbi, 0, 0))
1907 		goto redirty_out;
1908 	else
1909 		set_inode_flag(inode, FI_HOT_DATA);
1910 
1911 	err = -EAGAIN;
1912 	if (f2fs_has_inline_data(inode)) {
1913 		err = f2fs_write_inline_data(inode, page);
1914 		if (!err)
1915 			goto out;
1916 	}
1917 
1918 	if (err == -EAGAIN) {
1919 		err = f2fs_do_write_data_page(&fio);
1920 		if (err == -EAGAIN) {
1921 			fio.need_lock = LOCK_REQ;
1922 			err = f2fs_do_write_data_page(&fio);
1923 		}
1924 	}
1925 
1926 	if (err) {
1927 		file_set_keep_isize(inode);
1928 	} else {
1929 		down_write(&F2FS_I(inode)->i_sem);
1930 		if (F2FS_I(inode)->last_disk_size < psize)
1931 			F2FS_I(inode)->last_disk_size = psize;
1932 		up_write(&F2FS_I(inode)->i_sem);
1933 	}
1934 
1935 done:
1936 	if (err && err != -ENOENT)
1937 		goto redirty_out;
1938 
1939 out:
1940 	inode_dec_dirty_pages(inode);
1941 	if (err)
1942 		ClearPageUptodate(page);
1943 
1944 	if (wbc->for_reclaim) {
1945 		f2fs_submit_merged_write_cond(sbi, inode, 0, page->index, DATA);
1946 		clear_inode_flag(inode, FI_HOT_DATA);
1947 		f2fs_remove_dirty_inode(inode);
1948 		submitted = NULL;
1949 	}
1950 
1951 	unlock_page(page);
1952 	if (!S_ISDIR(inode->i_mode))
1953 		f2fs_balance_fs(sbi, need_balance_fs);
1954 
1955 	if (unlikely(f2fs_cp_error(sbi))) {
1956 		f2fs_submit_merged_write(sbi, DATA);
1957 		submitted = NULL;
1958 	}
1959 
1960 	if (submitted)
1961 		*submitted = fio.submitted;
1962 
1963 	return 0;
1964 
1965 redirty_out:
1966 	redirty_page_for_writepage(wbc, page);
1967 	/*
1968 	 * pageout() in MM traslates EAGAIN, so calls handle_write_error()
1969 	 * -> mapping_set_error() -> set_bit(AS_EIO, ...).
1970 	 * file_write_and_wait_range() will see EIO error, which is critical
1971 	 * to return value of fsync() followed by atomic_write failure to user.
1972 	 */
1973 	if (!err || wbc->for_reclaim)
1974 		return AOP_WRITEPAGE_ACTIVATE;
1975 	unlock_page(page);
1976 	return err;
1977 }
1978 
1979 static int f2fs_write_data_page(struct page *page,
1980 					struct writeback_control *wbc)
1981 {
1982 	return __write_data_page(page, NULL, wbc, FS_DATA_IO);
1983 }
1984 
1985 /*
1986  * This function was copied from write_cche_pages from mm/page-writeback.c.
1987  * The major change is making write step of cold data page separately from
1988  * warm/hot data page.
1989  */
1990 static int f2fs_write_cache_pages(struct address_space *mapping,
1991 					struct writeback_control *wbc,
1992 					enum iostat_type io_type)
1993 {
1994 	int ret = 0;
1995 	int done = 0;
1996 	struct pagevec pvec;
1997 	struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
1998 	int nr_pages;
1999 	pgoff_t uninitialized_var(writeback_index);
2000 	pgoff_t index;
2001 	pgoff_t end;		/* Inclusive */
2002 	pgoff_t done_index;
2003 	pgoff_t last_idx = ULONG_MAX;
2004 	int cycled;
2005 	int range_whole = 0;
2006 	int tag;
2007 
2008 	pagevec_init(&pvec);
2009 
2010 	if (get_dirty_pages(mapping->host) <=
2011 				SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
2012 		set_inode_flag(mapping->host, FI_HOT_DATA);
2013 	else
2014 		clear_inode_flag(mapping->host, FI_HOT_DATA);
2015 
2016 	if (wbc->range_cyclic) {
2017 		writeback_index = mapping->writeback_index; /* prev offset */
2018 		index = writeback_index;
2019 		if (index == 0)
2020 			cycled = 1;
2021 		else
2022 			cycled = 0;
2023 		end = -1;
2024 	} else {
2025 		index = wbc->range_start >> PAGE_SHIFT;
2026 		end = wbc->range_end >> PAGE_SHIFT;
2027 		if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2028 			range_whole = 1;
2029 		cycled = 1; /* ignore range_cyclic tests */
2030 	}
2031 	if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2032 		tag = PAGECACHE_TAG_TOWRITE;
2033 	else
2034 		tag = PAGECACHE_TAG_DIRTY;
2035 retry:
2036 	if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2037 		tag_pages_for_writeback(mapping, index, end);
2038 	done_index = index;
2039 	while (!done && (index <= end)) {
2040 		int i;
2041 
2042 		nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
2043 				tag);
2044 		if (nr_pages == 0)
2045 			break;
2046 
2047 		for (i = 0; i < nr_pages; i++) {
2048 			struct page *page = pvec.pages[i];
2049 			bool submitted = false;
2050 
2051 			/* give a priority to WB_SYNC threads */
2052 			if (atomic_read(&sbi->wb_sync_req[DATA]) &&
2053 					wbc->sync_mode == WB_SYNC_NONE) {
2054 				done = 1;
2055 				break;
2056 			}
2057 
2058 			done_index = page->index;
2059 retry_write:
2060 			lock_page(page);
2061 
2062 			if (unlikely(page->mapping != mapping)) {
2063 continue_unlock:
2064 				unlock_page(page);
2065 				continue;
2066 			}
2067 
2068 			if (!PageDirty(page)) {
2069 				/* someone wrote it for us */
2070 				goto continue_unlock;
2071 			}
2072 
2073 			if (PageWriteback(page)) {
2074 				if (wbc->sync_mode != WB_SYNC_NONE)
2075 					f2fs_wait_on_page_writeback(page,
2076 								DATA, true);
2077 				else
2078 					goto continue_unlock;
2079 			}
2080 
2081 			BUG_ON(PageWriteback(page));
2082 			if (!clear_page_dirty_for_io(page))
2083 				goto continue_unlock;
2084 
2085 			ret = __write_data_page(page, &submitted, wbc, io_type);
2086 			if (unlikely(ret)) {
2087 				/*
2088 				 * keep nr_to_write, since vfs uses this to
2089 				 * get # of written pages.
2090 				 */
2091 				if (ret == AOP_WRITEPAGE_ACTIVATE) {
2092 					unlock_page(page);
2093 					ret = 0;
2094 					continue;
2095 				} else if (ret == -EAGAIN) {
2096 					ret = 0;
2097 					if (wbc->sync_mode == WB_SYNC_ALL) {
2098 						cond_resched();
2099 						congestion_wait(BLK_RW_ASYNC,
2100 									HZ/50);
2101 						goto retry_write;
2102 					}
2103 					continue;
2104 				}
2105 				done_index = page->index + 1;
2106 				done = 1;
2107 				break;
2108 			} else if (submitted) {
2109 				last_idx = page->index;
2110 			}
2111 
2112 			if (--wbc->nr_to_write <= 0 &&
2113 					wbc->sync_mode == WB_SYNC_NONE) {
2114 				done = 1;
2115 				break;
2116 			}
2117 		}
2118 		pagevec_release(&pvec);
2119 		cond_resched();
2120 	}
2121 
2122 	if (!cycled && !done) {
2123 		cycled = 1;
2124 		index = 0;
2125 		end = writeback_index - 1;
2126 		goto retry;
2127 	}
2128 	if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2129 		mapping->writeback_index = done_index;
2130 
2131 	if (last_idx != ULONG_MAX)
2132 		f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
2133 						0, last_idx, DATA);
2134 
2135 	return ret;
2136 }
2137 
2138 static inline bool __should_serialize_io(struct inode *inode,
2139 					struct writeback_control *wbc)
2140 {
2141 	if (!S_ISREG(inode->i_mode))
2142 		return false;
2143 	if (wbc->sync_mode != WB_SYNC_ALL)
2144 		return true;
2145 	if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks)
2146 		return true;
2147 	return false;
2148 }
2149 
2150 static int __f2fs_write_data_pages(struct address_space *mapping,
2151 						struct writeback_control *wbc,
2152 						enum iostat_type io_type)
2153 {
2154 	struct inode *inode = mapping->host;
2155 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2156 	struct blk_plug plug;
2157 	int ret;
2158 	bool locked = false;
2159 
2160 	/* deal with chardevs and other special file */
2161 	if (!mapping->a_ops->writepage)
2162 		return 0;
2163 
2164 	/* skip writing if there is no dirty page in this inode */
2165 	if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
2166 		return 0;
2167 
2168 	/* during POR, we don't need to trigger writepage at all. */
2169 	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2170 		goto skip_write;
2171 
2172 	if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
2173 			get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
2174 			f2fs_available_free_memory(sbi, DIRTY_DENTS))
2175 		goto skip_write;
2176 
2177 	/* skip writing during file defragment */
2178 	if (is_inode_flag_set(inode, FI_DO_DEFRAG))
2179 		goto skip_write;
2180 
2181 	trace_f2fs_writepages(mapping->host, wbc, DATA);
2182 
2183 	/* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
2184 	if (wbc->sync_mode == WB_SYNC_ALL)
2185 		atomic_inc(&sbi->wb_sync_req[DATA]);
2186 	else if (atomic_read(&sbi->wb_sync_req[DATA]))
2187 		goto skip_write;
2188 
2189 	if (__should_serialize_io(inode, wbc)) {
2190 		mutex_lock(&sbi->writepages);
2191 		locked = true;
2192 	}
2193 
2194 	blk_start_plug(&plug);
2195 	ret = f2fs_write_cache_pages(mapping, wbc, io_type);
2196 	blk_finish_plug(&plug);
2197 
2198 	if (locked)
2199 		mutex_unlock(&sbi->writepages);
2200 
2201 	if (wbc->sync_mode == WB_SYNC_ALL)
2202 		atomic_dec(&sbi->wb_sync_req[DATA]);
2203 	/*
2204 	 * if some pages were truncated, we cannot guarantee its mapping->host
2205 	 * to detect pending bios.
2206 	 */
2207 
2208 	f2fs_remove_dirty_inode(inode);
2209 	return ret;
2210 
2211 skip_write:
2212 	wbc->pages_skipped += get_dirty_pages(inode);
2213 	trace_f2fs_writepages(mapping->host, wbc, DATA);
2214 	return 0;
2215 }
2216 
2217 static int f2fs_write_data_pages(struct address_space *mapping,
2218 			    struct writeback_control *wbc)
2219 {
2220 	struct inode *inode = mapping->host;
2221 
2222 	return __f2fs_write_data_pages(mapping, wbc,
2223 			F2FS_I(inode)->cp_task == current ?
2224 			FS_CP_DATA_IO : FS_DATA_IO);
2225 }
2226 
2227 static void f2fs_write_failed(struct address_space *mapping, loff_t to)
2228 {
2229 	struct inode *inode = mapping->host;
2230 	loff_t i_size = i_size_read(inode);
2231 
2232 	if (to > i_size) {
2233 		down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
2234 		down_write(&F2FS_I(inode)->i_mmap_sem);
2235 
2236 		truncate_pagecache(inode, i_size);
2237 		f2fs_truncate_blocks(inode, i_size, true);
2238 
2239 		up_write(&F2FS_I(inode)->i_mmap_sem);
2240 		up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
2241 	}
2242 }
2243 
2244 static int prepare_write_begin(struct f2fs_sb_info *sbi,
2245 			struct page *page, loff_t pos, unsigned len,
2246 			block_t *blk_addr, bool *node_changed)
2247 {
2248 	struct inode *inode = page->mapping->host;
2249 	pgoff_t index = page->index;
2250 	struct dnode_of_data dn;
2251 	struct page *ipage;
2252 	bool locked = false;
2253 	struct extent_info ei = {0,0,0};
2254 	int err = 0;
2255 
2256 	/*
2257 	 * we already allocated all the blocks, so we don't need to get
2258 	 * the block addresses when there is no need to fill the page.
2259 	 */
2260 	if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
2261 			!is_inode_flag_set(inode, FI_NO_PREALLOC))
2262 		return 0;
2263 
2264 	if (f2fs_has_inline_data(inode) ||
2265 			(pos & PAGE_MASK) >= i_size_read(inode)) {
2266 		__do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
2267 		locked = true;
2268 	}
2269 restart:
2270 	/* check inline_data */
2271 	ipage = f2fs_get_node_page(sbi, inode->i_ino);
2272 	if (IS_ERR(ipage)) {
2273 		err = PTR_ERR(ipage);
2274 		goto unlock_out;
2275 	}
2276 
2277 	set_new_dnode(&dn, inode, ipage, ipage, 0);
2278 
2279 	if (f2fs_has_inline_data(inode)) {
2280 		if (pos + len <= MAX_INLINE_DATA(inode)) {
2281 			f2fs_do_read_inline_data(page, ipage);
2282 			set_inode_flag(inode, FI_DATA_EXIST);
2283 			if (inode->i_nlink)
2284 				set_inline_node(ipage);
2285 		} else {
2286 			err = f2fs_convert_inline_page(&dn, page);
2287 			if (err)
2288 				goto out;
2289 			if (dn.data_blkaddr == NULL_ADDR)
2290 				err = f2fs_get_block(&dn, index);
2291 		}
2292 	} else if (locked) {
2293 		err = f2fs_get_block(&dn, index);
2294 	} else {
2295 		if (f2fs_lookup_extent_cache(inode, index, &ei)) {
2296 			dn.data_blkaddr = ei.blk + index - ei.fofs;
2297 		} else {
2298 			/* hole case */
2299 			err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
2300 			if (err || dn.data_blkaddr == NULL_ADDR) {
2301 				f2fs_put_dnode(&dn);
2302 				__do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
2303 								true);
2304 				locked = true;
2305 				goto restart;
2306 			}
2307 		}
2308 	}
2309 
2310 	/* convert_inline_page can make node_changed */
2311 	*blk_addr = dn.data_blkaddr;
2312 	*node_changed = dn.node_changed;
2313 out:
2314 	f2fs_put_dnode(&dn);
2315 unlock_out:
2316 	if (locked)
2317 		__do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
2318 	return err;
2319 }
2320 
2321 static int f2fs_write_begin(struct file *file, struct address_space *mapping,
2322 		loff_t pos, unsigned len, unsigned flags,
2323 		struct page **pagep, void **fsdata)
2324 {
2325 	struct inode *inode = mapping->host;
2326 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2327 	struct page *page = NULL;
2328 	pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
2329 	bool need_balance = false, drop_atomic = false;
2330 	block_t blkaddr = NULL_ADDR;
2331 	int err = 0;
2332 
2333 	trace_f2fs_write_begin(inode, pos, len, flags);
2334 
2335 	if ((f2fs_is_atomic_file(inode) &&
2336 			!f2fs_available_free_memory(sbi, INMEM_PAGES)) ||
2337 			is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
2338 		err = -ENOMEM;
2339 		drop_atomic = true;
2340 		goto fail;
2341 	}
2342 
2343 	/*
2344 	 * We should check this at this moment to avoid deadlock on inode page
2345 	 * and #0 page. The locking rule for inline_data conversion should be:
2346 	 * lock_page(page #0) -> lock_page(inode_page)
2347 	 */
2348 	if (index != 0) {
2349 		err = f2fs_convert_inline_inode(inode);
2350 		if (err)
2351 			goto fail;
2352 	}
2353 repeat:
2354 	/*
2355 	 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
2356 	 * wait_for_stable_page. Will wait that below with our IO control.
2357 	 */
2358 	page = f2fs_pagecache_get_page(mapping, index,
2359 				FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
2360 	if (!page) {
2361 		err = -ENOMEM;
2362 		goto fail;
2363 	}
2364 
2365 	*pagep = page;
2366 
2367 	err = prepare_write_begin(sbi, page, pos, len,
2368 					&blkaddr, &need_balance);
2369 	if (err)
2370 		goto fail;
2371 
2372 	if (need_balance && has_not_enough_free_secs(sbi, 0, 0)) {
2373 		unlock_page(page);
2374 		f2fs_balance_fs(sbi, true);
2375 		lock_page(page);
2376 		if (page->mapping != mapping) {
2377 			/* The page got truncated from under us */
2378 			f2fs_put_page(page, 1);
2379 			goto repeat;
2380 		}
2381 	}
2382 
2383 	f2fs_wait_on_page_writeback(page, DATA, false);
2384 
2385 	/* wait for GCed page writeback via META_MAPPING */
2386 	if (f2fs_post_read_required(inode))
2387 		f2fs_wait_on_block_writeback(sbi, blkaddr);
2388 
2389 	if (len == PAGE_SIZE || PageUptodate(page))
2390 		return 0;
2391 
2392 	if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode)) {
2393 		zero_user_segment(page, len, PAGE_SIZE);
2394 		return 0;
2395 	}
2396 
2397 	if (blkaddr == NEW_ADDR) {
2398 		zero_user_segment(page, 0, PAGE_SIZE);
2399 		SetPageUptodate(page);
2400 	} else {
2401 		err = f2fs_submit_page_read(inode, page, blkaddr);
2402 		if (err)
2403 			goto fail;
2404 
2405 		lock_page(page);
2406 		if (unlikely(page->mapping != mapping)) {
2407 			f2fs_put_page(page, 1);
2408 			goto repeat;
2409 		}
2410 		if (unlikely(!PageUptodate(page))) {
2411 			err = -EIO;
2412 			goto fail;
2413 		}
2414 	}
2415 	return 0;
2416 
2417 fail:
2418 	f2fs_put_page(page, 1);
2419 	f2fs_write_failed(mapping, pos + len);
2420 	if (drop_atomic)
2421 		f2fs_drop_inmem_pages_all(sbi, false);
2422 	return err;
2423 }
2424 
2425 static int f2fs_write_end(struct file *file,
2426 			struct address_space *mapping,
2427 			loff_t pos, unsigned len, unsigned copied,
2428 			struct page *page, void *fsdata)
2429 {
2430 	struct inode *inode = page->mapping->host;
2431 
2432 	trace_f2fs_write_end(inode, pos, len, copied);
2433 
2434 	/*
2435 	 * This should be come from len == PAGE_SIZE, and we expect copied
2436 	 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
2437 	 * let generic_perform_write() try to copy data again through copied=0.
2438 	 */
2439 	if (!PageUptodate(page)) {
2440 		if (unlikely(copied != len))
2441 			copied = 0;
2442 		else
2443 			SetPageUptodate(page);
2444 	}
2445 	if (!copied)
2446 		goto unlock_out;
2447 
2448 	set_page_dirty(page);
2449 
2450 	if (pos + copied > i_size_read(inode))
2451 		f2fs_i_size_write(inode, pos + copied);
2452 unlock_out:
2453 	f2fs_put_page(page, 1);
2454 	f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2455 	return copied;
2456 }
2457 
2458 static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
2459 			   loff_t offset)
2460 {
2461 	unsigned i_blkbits = READ_ONCE(inode->i_blkbits);
2462 	unsigned blkbits = i_blkbits;
2463 	unsigned blocksize_mask = (1 << blkbits) - 1;
2464 	unsigned long align = offset | iov_iter_alignment(iter);
2465 	struct block_device *bdev = inode->i_sb->s_bdev;
2466 
2467 	if (align & blocksize_mask) {
2468 		if (bdev)
2469 			blkbits = blksize_bits(bdev_logical_block_size(bdev));
2470 		blocksize_mask = (1 << blkbits) - 1;
2471 		if (align & blocksize_mask)
2472 			return -EINVAL;
2473 		return 1;
2474 	}
2475 	return 0;
2476 }
2477 
2478 static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
2479 {
2480 	struct address_space *mapping = iocb->ki_filp->f_mapping;
2481 	struct inode *inode = mapping->host;
2482 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2483 	size_t count = iov_iter_count(iter);
2484 	loff_t offset = iocb->ki_pos;
2485 	int rw = iov_iter_rw(iter);
2486 	int err;
2487 	enum rw_hint hint = iocb->ki_hint;
2488 	int whint_mode = F2FS_OPTION(sbi).whint_mode;
2489 
2490 	err = check_direct_IO(inode, iter, offset);
2491 	if (err)
2492 		return err < 0 ? err : 0;
2493 
2494 	if (f2fs_force_buffered_io(inode, rw))
2495 		return 0;
2496 
2497 	trace_f2fs_direct_IO_enter(inode, offset, count, rw);
2498 
2499 	if (rw == WRITE && whint_mode == WHINT_MODE_OFF)
2500 		iocb->ki_hint = WRITE_LIFE_NOT_SET;
2501 
2502 	if (!down_read_trylock(&F2FS_I(inode)->i_gc_rwsem[rw])) {
2503 		if (iocb->ki_flags & IOCB_NOWAIT) {
2504 			iocb->ki_hint = hint;
2505 			err = -EAGAIN;
2506 			goto out;
2507 		}
2508 		down_read(&F2FS_I(inode)->i_gc_rwsem[rw]);
2509 	}
2510 
2511 	err = blockdev_direct_IO(iocb, inode, iter, get_data_block_dio);
2512 	up_read(&F2FS_I(inode)->i_gc_rwsem[rw]);
2513 
2514 	if (rw == WRITE) {
2515 		if (whint_mode == WHINT_MODE_OFF)
2516 			iocb->ki_hint = hint;
2517 		if (err > 0) {
2518 			f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
2519 									err);
2520 			set_inode_flag(inode, FI_UPDATE_WRITE);
2521 		} else if (err < 0) {
2522 			f2fs_write_failed(mapping, offset + count);
2523 		}
2524 	}
2525 
2526 out:
2527 	trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
2528 
2529 	return err;
2530 }
2531 
2532 void f2fs_invalidate_page(struct page *page, unsigned int offset,
2533 							unsigned int length)
2534 {
2535 	struct inode *inode = page->mapping->host;
2536 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2537 
2538 	if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
2539 		(offset % PAGE_SIZE || length != PAGE_SIZE))
2540 		return;
2541 
2542 	if (PageDirty(page)) {
2543 		if (inode->i_ino == F2FS_META_INO(sbi)) {
2544 			dec_page_count(sbi, F2FS_DIRTY_META);
2545 		} else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
2546 			dec_page_count(sbi, F2FS_DIRTY_NODES);
2547 		} else {
2548 			inode_dec_dirty_pages(inode);
2549 			f2fs_remove_dirty_inode(inode);
2550 		}
2551 	}
2552 
2553 	/* This is atomic written page, keep Private */
2554 	if (IS_ATOMIC_WRITTEN_PAGE(page))
2555 		return f2fs_drop_inmem_page(inode, page);
2556 
2557 	set_page_private(page, 0);
2558 	ClearPagePrivate(page);
2559 }
2560 
2561 int f2fs_release_page(struct page *page, gfp_t wait)
2562 {
2563 	/* If this is dirty page, keep PagePrivate */
2564 	if (PageDirty(page))
2565 		return 0;
2566 
2567 	/* This is atomic written page, keep Private */
2568 	if (IS_ATOMIC_WRITTEN_PAGE(page))
2569 		return 0;
2570 
2571 	set_page_private(page, 0);
2572 	ClearPagePrivate(page);
2573 	return 1;
2574 }
2575 
2576 static int f2fs_set_data_page_dirty(struct page *page)
2577 {
2578 	struct address_space *mapping = page->mapping;
2579 	struct inode *inode = mapping->host;
2580 
2581 	trace_f2fs_set_page_dirty(page, DATA);
2582 
2583 	if (!PageUptodate(page))
2584 		SetPageUptodate(page);
2585 
2586 	/* don't remain PG_checked flag which was set during GC */
2587 	if (is_cold_data(page))
2588 		clear_cold_data(page);
2589 
2590 	if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
2591 		if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
2592 			f2fs_register_inmem_page(inode, page);
2593 			return 1;
2594 		}
2595 		/*
2596 		 * Previously, this page has been registered, we just
2597 		 * return here.
2598 		 */
2599 		return 0;
2600 	}
2601 
2602 	if (!PageDirty(page)) {
2603 		__set_page_dirty_nobuffers(page);
2604 		f2fs_update_dirty_page(inode, page);
2605 		return 1;
2606 	}
2607 	return 0;
2608 }
2609 
2610 static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
2611 {
2612 	struct inode *inode = mapping->host;
2613 
2614 	if (f2fs_has_inline_data(inode))
2615 		return 0;
2616 
2617 	/* make sure allocating whole blocks */
2618 	if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
2619 		filemap_write_and_wait(mapping);
2620 
2621 	return generic_block_bmap(mapping, block, get_data_block_bmap);
2622 }
2623 
2624 #ifdef CONFIG_MIGRATION
2625 #include <linux/migrate.h>
2626 
2627 int f2fs_migrate_page(struct address_space *mapping,
2628 		struct page *newpage, struct page *page, enum migrate_mode mode)
2629 {
2630 	int rc, extra_count;
2631 	struct f2fs_inode_info *fi = F2FS_I(mapping->host);
2632 	bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
2633 
2634 	BUG_ON(PageWriteback(page));
2635 
2636 	/* migrating an atomic written page is safe with the inmem_lock hold */
2637 	if (atomic_written) {
2638 		if (mode != MIGRATE_SYNC)
2639 			return -EBUSY;
2640 		if (!mutex_trylock(&fi->inmem_lock))
2641 			return -EAGAIN;
2642 	}
2643 
2644 	/*
2645 	 * A reference is expected if PagePrivate set when move mapping,
2646 	 * however F2FS breaks this for maintaining dirty page counts when
2647 	 * truncating pages. So here adjusting the 'extra_count' make it work.
2648 	 */
2649 	extra_count = (atomic_written ? 1 : 0) - page_has_private(page);
2650 	rc = migrate_page_move_mapping(mapping, newpage,
2651 				page, NULL, mode, extra_count);
2652 	if (rc != MIGRATEPAGE_SUCCESS) {
2653 		if (atomic_written)
2654 			mutex_unlock(&fi->inmem_lock);
2655 		return rc;
2656 	}
2657 
2658 	if (atomic_written) {
2659 		struct inmem_pages *cur;
2660 		list_for_each_entry(cur, &fi->inmem_pages, list)
2661 			if (cur->page == page) {
2662 				cur->page = newpage;
2663 				break;
2664 			}
2665 		mutex_unlock(&fi->inmem_lock);
2666 		put_page(page);
2667 		get_page(newpage);
2668 	}
2669 
2670 	if (PagePrivate(page))
2671 		SetPagePrivate(newpage);
2672 	set_page_private(newpage, page_private(page));
2673 
2674 	if (mode != MIGRATE_SYNC_NO_COPY)
2675 		migrate_page_copy(newpage, page);
2676 	else
2677 		migrate_page_states(newpage, page);
2678 
2679 	return MIGRATEPAGE_SUCCESS;
2680 }
2681 #endif
2682 
2683 const struct address_space_operations f2fs_dblock_aops = {
2684 	.readpage	= f2fs_read_data_page,
2685 	.readpages	= f2fs_read_data_pages,
2686 	.writepage	= f2fs_write_data_page,
2687 	.writepages	= f2fs_write_data_pages,
2688 	.write_begin	= f2fs_write_begin,
2689 	.write_end	= f2fs_write_end,
2690 	.set_page_dirty	= f2fs_set_data_page_dirty,
2691 	.invalidatepage	= f2fs_invalidate_page,
2692 	.releasepage	= f2fs_release_page,
2693 	.direct_IO	= f2fs_direct_IO,
2694 	.bmap		= f2fs_bmap,
2695 #ifdef CONFIG_MIGRATION
2696 	.migratepage    = f2fs_migrate_page,
2697 #endif
2698 };
2699 
2700 void f2fs_clear_radix_tree_dirty_tag(struct page *page)
2701 {
2702 	struct address_space *mapping = page_mapping(page);
2703 	unsigned long flags;
2704 
2705 	xa_lock_irqsave(&mapping->i_pages, flags);
2706 	radix_tree_tag_clear(&mapping->i_pages, page_index(page),
2707 						PAGECACHE_TAG_DIRTY);
2708 	xa_unlock_irqrestore(&mapping->i_pages, flags);
2709 }
2710 
2711 int __init f2fs_init_post_read_processing(void)
2712 {
2713 	bio_post_read_ctx_cache = KMEM_CACHE(bio_post_read_ctx, 0);
2714 	if (!bio_post_read_ctx_cache)
2715 		goto fail;
2716 	bio_post_read_ctx_pool =
2717 		mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
2718 					 bio_post_read_ctx_cache);
2719 	if (!bio_post_read_ctx_pool)
2720 		goto fail_free_cache;
2721 	return 0;
2722 
2723 fail_free_cache:
2724 	kmem_cache_destroy(bio_post_read_ctx_cache);
2725 fail:
2726 	return -ENOMEM;
2727 }
2728 
2729 void __exit f2fs_destroy_post_read_processing(void)
2730 {
2731 	mempool_destroy(bio_post_read_ctx_pool);
2732 	kmem_cache_destroy(bio_post_read_ctx_cache);
2733 }
2734