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