xfrm: allow to accept packets with ipv6 NEXTHDR_HOP in xfrm_input
[linux/fpc-iii.git] / fs / f2fs / data.c
blobc81a1f3f0a101a11504181fa1ae60afeaaf8ca4c
1 /*
2 * fs/f2fs/data.c
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
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.
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>
25 #include "f2fs.h"
26 #include "node.h"
27 #include "segment.h"
28 #include "trace.h"
29 #include <trace/events/f2fs.h>
31 #define NUM_PREALLOC_POST_READ_CTXS 128
33 static struct kmem_cache *bio_post_read_ctx_cache;
34 static mempool_t *bio_post_read_ctx_pool;
36 static bool __is_cp_guaranteed(struct page *page)
38 struct address_space *mapping = page->mapping;
39 struct inode *inode;
40 struct f2fs_sb_info *sbi;
42 if (!mapping)
43 return false;
45 inode = mapping->host;
46 sbi = F2FS_I_SB(inode);
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;
58 /* postprocessing steps for read bios */
59 enum bio_post_read_step {
60 STEP_INITIAL = 0,
61 STEP_DECRYPT,
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;
71 static void __read_end_io(struct bio *bio)
73 struct page *page;
74 struct bio_vec *bv;
75 int i;
77 bio_for_each_segment_all(bv, bio, i) {
78 page = bv->bv_page;
80 /* PG_error was set if any post_read step failed */
81 if (bio->bi_status || PageError(page)) {
82 ClearPageUptodate(page);
83 /* will re-read again later */
84 ClearPageError(page);
85 } else {
86 SetPageUptodate(page);
88 unlock_page(page);
90 if (bio->bi_private)
91 mempool_free(bio->bi_private, bio_post_read_ctx_pool);
92 bio_put(bio);
95 static void bio_post_read_processing(struct bio_post_read_ctx *ctx);
97 static void decrypt_work(struct work_struct *work)
99 struct bio_post_read_ctx *ctx =
100 container_of(work, struct bio_post_read_ctx, work);
102 fscrypt_decrypt_bio(ctx->bio);
104 bio_post_read_processing(ctx);
107 static void bio_post_read_processing(struct bio_post_read_ctx *ctx)
109 switch (++ctx->cur_step) {
110 case STEP_DECRYPT:
111 if (ctx->enabled_steps & (1 << STEP_DECRYPT)) {
112 INIT_WORK(&ctx->work, decrypt_work);
113 fscrypt_enqueue_decrypt_work(&ctx->work);
114 return;
116 ctx->cur_step++;
117 /* fall-through */
118 default:
119 __read_end_io(ctx->bio);
123 static bool f2fs_bio_post_read_required(struct bio *bio)
125 return bio->bi_private && !bio->bi_status;
128 static void f2fs_read_end_io(struct bio *bio)
130 if (time_to_inject(F2FS_P_SB(bio_first_page_all(bio)), FAULT_IO)) {
131 f2fs_show_injection_info(FAULT_IO);
132 bio->bi_status = BLK_STS_IOERR;
135 if (f2fs_bio_post_read_required(bio)) {
136 struct bio_post_read_ctx *ctx = bio->bi_private;
138 ctx->cur_step = STEP_INITIAL;
139 bio_post_read_processing(ctx);
140 return;
143 __read_end_io(bio);
146 static void f2fs_write_end_io(struct bio *bio)
148 struct f2fs_sb_info *sbi = bio->bi_private;
149 struct bio_vec *bvec;
150 int i;
152 bio_for_each_segment_all(bvec, bio, i) {
153 struct page *page = bvec->bv_page;
154 enum count_type type = WB_DATA_TYPE(page);
156 if (IS_DUMMY_WRITTEN_PAGE(page)) {
157 set_page_private(page, (unsigned long)NULL);
158 ClearPagePrivate(page);
159 unlock_page(page);
160 mempool_free(page, sbi->write_io_dummy);
162 if (unlikely(bio->bi_status))
163 f2fs_stop_checkpoint(sbi, true);
164 continue;
167 fscrypt_pullback_bio_page(&page, true);
169 if (unlikely(bio->bi_status)) {
170 mapping_set_error(page->mapping, -EIO);
171 if (type == F2FS_WB_CP_DATA)
172 f2fs_stop_checkpoint(sbi, true);
175 f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) &&
176 page->index != nid_of_node(page));
178 dec_page_count(sbi, type);
179 if (f2fs_in_warm_node_list(sbi, page))
180 f2fs_del_fsync_node_entry(sbi, page);
181 clear_cold_data(page);
182 end_page_writeback(page);
184 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
185 wq_has_sleeper(&sbi->cp_wait))
186 wake_up(&sbi->cp_wait);
188 bio_put(bio);
192 * Return true, if pre_bio's bdev is same as its target device.
194 struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
195 block_t blk_addr, struct bio *bio)
197 struct block_device *bdev = sbi->sb->s_bdev;
198 int i;
200 if (f2fs_is_multi_device(sbi)) {
201 for (i = 0; i < sbi->s_ndevs; i++) {
202 if (FDEV(i).start_blk <= blk_addr &&
203 FDEV(i).end_blk >= blk_addr) {
204 blk_addr -= FDEV(i).start_blk;
205 bdev = FDEV(i).bdev;
206 break;
210 if (bio) {
211 bio_set_dev(bio, bdev);
212 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
214 return bdev;
217 int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
219 int i;
221 if (!f2fs_is_multi_device(sbi))
222 return 0;
224 for (i = 0; i < sbi->s_ndevs; i++)
225 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
226 return i;
227 return 0;
230 static bool __same_bdev(struct f2fs_sb_info *sbi,
231 block_t blk_addr, struct bio *bio)
233 struct block_device *b = f2fs_target_device(sbi, blk_addr, NULL);
234 return bio->bi_disk == b->bd_disk && bio->bi_partno == b->bd_partno;
238 * Low-level block read/write IO operations.
240 static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr,
241 struct writeback_control *wbc,
242 int npages, bool is_read,
243 enum page_type type, enum temp_type temp)
245 struct bio *bio;
247 bio = f2fs_bio_alloc(sbi, npages, true);
249 f2fs_target_device(sbi, blk_addr, bio);
250 if (is_read) {
251 bio->bi_end_io = f2fs_read_end_io;
252 bio->bi_private = NULL;
253 } else {
254 bio->bi_end_io = f2fs_write_end_io;
255 bio->bi_private = sbi;
256 bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi, type, temp);
258 if (wbc)
259 wbc_init_bio(wbc, bio);
261 return bio;
264 static inline void __submit_bio(struct f2fs_sb_info *sbi,
265 struct bio *bio, enum page_type type)
267 if (!is_read_io(bio_op(bio))) {
268 unsigned int start;
270 if (type != DATA && type != NODE)
271 goto submit_io;
273 if (test_opt(sbi, LFS) && current->plug)
274 blk_finish_plug(current->plug);
276 start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
277 start %= F2FS_IO_SIZE(sbi);
279 if (start == 0)
280 goto submit_io;
282 /* fill dummy pages */
283 for (; start < F2FS_IO_SIZE(sbi); start++) {
284 struct page *page =
285 mempool_alloc(sbi->write_io_dummy,
286 GFP_NOIO | __GFP_ZERO | __GFP_NOFAIL);
287 f2fs_bug_on(sbi, !page);
289 SetPagePrivate(page);
290 set_page_private(page, (unsigned long)DUMMY_WRITTEN_PAGE);
291 lock_page(page);
292 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
293 f2fs_bug_on(sbi, 1);
296 * In the NODE case, we lose next block address chain. So, we
297 * need to do checkpoint in f2fs_sync_file.
299 if (type == NODE)
300 set_sbi_flag(sbi, SBI_NEED_CP);
302 submit_io:
303 if (is_read_io(bio_op(bio)))
304 trace_f2fs_submit_read_bio(sbi->sb, type, bio);
305 else
306 trace_f2fs_submit_write_bio(sbi->sb, type, bio);
307 submit_bio(bio);
310 static void __submit_merged_bio(struct f2fs_bio_info *io)
312 struct f2fs_io_info *fio = &io->fio;
314 if (!io->bio)
315 return;
317 bio_set_op_attrs(io->bio, fio->op, fio->op_flags);
319 if (is_read_io(fio->op))
320 trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
321 else
322 trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
324 __submit_bio(io->sbi, io->bio, fio->type);
325 io->bio = NULL;
328 static bool __has_merged_page(struct f2fs_bio_info *io,
329 struct inode *inode, nid_t ino, pgoff_t idx)
331 struct bio_vec *bvec;
332 struct page *target;
333 int i;
335 if (!io->bio)
336 return false;
338 if (!inode && !ino)
339 return true;
341 bio_for_each_segment_all(bvec, io->bio, i) {
343 if (bvec->bv_page->mapping)
344 target = bvec->bv_page;
345 else
346 target = fscrypt_control_page(bvec->bv_page);
348 if (idx != target->index)
349 continue;
351 if (inode && inode == target->mapping->host)
352 return true;
353 if (ino && ino == ino_of_node(target))
354 return true;
357 return false;
360 static bool has_merged_page(struct f2fs_sb_info *sbi, struct inode *inode,
361 nid_t ino, pgoff_t idx, enum page_type type)
363 enum page_type btype = PAGE_TYPE_OF_BIO(type);
364 enum temp_type temp;
365 struct f2fs_bio_info *io;
366 bool ret = false;
368 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
369 io = sbi->write_io[btype] + temp;
371 down_read(&io->io_rwsem);
372 ret = __has_merged_page(io, inode, ino, idx);
373 up_read(&io->io_rwsem);
375 /* TODO: use HOT temp only for meta pages now. */
376 if (ret || btype == META)
377 break;
379 return ret;
382 static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
383 enum page_type type, enum temp_type temp)
385 enum page_type btype = PAGE_TYPE_OF_BIO(type);
386 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
388 down_write(&io->io_rwsem);
390 /* change META to META_FLUSH in the checkpoint procedure */
391 if (type >= META_FLUSH) {
392 io->fio.type = META_FLUSH;
393 io->fio.op = REQ_OP_WRITE;
394 io->fio.op_flags = REQ_META | REQ_PRIO | REQ_SYNC;
395 if (!test_opt(sbi, NOBARRIER))
396 io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
398 __submit_merged_bio(io);
399 up_write(&io->io_rwsem);
402 static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
403 struct inode *inode, nid_t ino, pgoff_t idx,
404 enum page_type type, bool force)
406 enum temp_type temp;
408 if (!force && !has_merged_page(sbi, inode, ino, idx, type))
409 return;
411 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
413 __f2fs_submit_merged_write(sbi, type, temp);
415 /* TODO: use HOT temp only for meta pages now. */
416 if (type >= META)
417 break;
421 void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
423 __submit_merged_write_cond(sbi, NULL, 0, 0, type, true);
426 void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
427 struct inode *inode, nid_t ino, pgoff_t idx,
428 enum page_type type)
430 __submit_merged_write_cond(sbi, inode, ino, idx, type, false);
433 void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
435 f2fs_submit_merged_write(sbi, DATA);
436 f2fs_submit_merged_write(sbi, NODE);
437 f2fs_submit_merged_write(sbi, META);
441 * Fill the locked page with data located in the block address.
442 * A caller needs to unlock the page on failure.
444 int f2fs_submit_page_bio(struct f2fs_io_info *fio)
446 struct bio *bio;
447 struct page *page = fio->encrypted_page ?
448 fio->encrypted_page : fio->page;
450 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
451 __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC))
452 return -EFSCORRUPTED;
454 trace_f2fs_submit_page_bio(page, fio);
455 f2fs_trace_ios(fio, 0);
457 /* Allocate a new bio */
458 bio = __bio_alloc(fio->sbi, fio->new_blkaddr, fio->io_wbc,
459 1, is_read_io(fio->op), fio->type, fio->temp);
461 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
462 bio_put(bio);
463 return -EFAULT;
466 if (fio->io_wbc && !is_read_io(fio->op))
467 wbc_account_io(fio->io_wbc, page, PAGE_SIZE);
469 bio_set_op_attrs(bio, fio->op, fio->op_flags);
471 if (!is_read_io(fio->op))
472 inc_page_count(fio->sbi, WB_DATA_TYPE(fio->page));
474 __submit_bio(fio->sbi, bio, fio->type);
475 return 0;
478 void f2fs_submit_page_write(struct f2fs_io_info *fio)
480 struct f2fs_sb_info *sbi = fio->sbi;
481 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
482 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
483 struct page *bio_page;
485 f2fs_bug_on(sbi, is_read_io(fio->op));
487 down_write(&io->io_rwsem);
488 next:
489 if (fio->in_list) {
490 spin_lock(&io->io_lock);
491 if (list_empty(&io->io_list)) {
492 spin_unlock(&io->io_lock);
493 goto out;
495 fio = list_first_entry(&io->io_list,
496 struct f2fs_io_info, list);
497 list_del(&fio->list);
498 spin_unlock(&io->io_lock);
501 if (__is_valid_data_blkaddr(fio->old_blkaddr))
502 verify_block_addr(fio, fio->old_blkaddr);
503 verify_block_addr(fio, fio->new_blkaddr);
505 bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
507 /* set submitted = true as a return value */
508 fio->submitted = true;
510 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
512 if (io->bio && (io->last_block_in_bio != fio->new_blkaddr - 1 ||
513 (io->fio.op != fio->op || io->fio.op_flags != fio->op_flags) ||
514 !__same_bdev(sbi, fio->new_blkaddr, io->bio)))
515 __submit_merged_bio(io);
516 alloc_new:
517 if (io->bio == NULL) {
518 if ((fio->type == DATA || fio->type == NODE) &&
519 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
520 dec_page_count(sbi, WB_DATA_TYPE(bio_page));
521 fio->retry = true;
522 goto skip;
524 io->bio = __bio_alloc(sbi, fio->new_blkaddr, fio->io_wbc,
525 BIO_MAX_PAGES, false,
526 fio->type, fio->temp);
527 io->fio = *fio;
530 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
531 __submit_merged_bio(io);
532 goto alloc_new;
535 if (fio->io_wbc)
536 wbc_account_io(fio->io_wbc, bio_page, PAGE_SIZE);
538 io->last_block_in_bio = fio->new_blkaddr;
539 f2fs_trace_ios(fio, 0);
541 trace_f2fs_submit_page_write(fio->page, fio);
542 skip:
543 if (fio->in_list)
544 goto next;
545 out:
546 if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN))
547 __submit_merged_bio(io);
548 up_write(&io->io_rwsem);
551 static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
552 unsigned nr_pages, unsigned op_flag)
554 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
555 struct bio *bio;
556 struct bio_post_read_ctx *ctx;
557 unsigned int post_read_steps = 0;
559 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC))
560 return ERR_PTR(-EFAULT);
562 bio = f2fs_bio_alloc(sbi, min_t(int, nr_pages, BIO_MAX_PAGES), false);
563 if (!bio)
564 return ERR_PTR(-ENOMEM);
565 f2fs_target_device(sbi, blkaddr, bio);
566 bio->bi_end_io = f2fs_read_end_io;
567 bio_set_op_attrs(bio, REQ_OP_READ, op_flag);
569 if (f2fs_encrypted_file(inode))
570 post_read_steps |= 1 << STEP_DECRYPT;
571 if (post_read_steps) {
572 ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
573 if (!ctx) {
574 bio_put(bio);
575 return ERR_PTR(-ENOMEM);
577 ctx->bio = bio;
578 ctx->enabled_steps = post_read_steps;
579 bio->bi_private = ctx;
582 return bio;
585 /* This can handle encryption stuffs */
586 static int f2fs_submit_page_read(struct inode *inode, struct page *page,
587 block_t blkaddr)
589 struct bio *bio = f2fs_grab_read_bio(inode, blkaddr, 1, 0);
591 if (IS_ERR(bio))
592 return PTR_ERR(bio);
594 /* wait for GCed page writeback via META_MAPPING */
595 f2fs_wait_on_block_writeback(inode, blkaddr);
597 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
598 bio_put(bio);
599 return -EFAULT;
601 ClearPageError(page);
602 __submit_bio(F2FS_I_SB(inode), bio, DATA);
603 return 0;
606 static void __set_data_blkaddr(struct dnode_of_data *dn)
608 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
609 __le32 *addr_array;
610 int base = 0;
612 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
613 base = get_extra_isize(dn->inode);
615 /* Get physical address of data block */
616 addr_array = blkaddr_in_node(rn);
617 addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
621 * Lock ordering for the change of data block address:
622 * ->data_page
623 * ->node_page
624 * update block addresses in the node page
626 void f2fs_set_data_blkaddr(struct dnode_of_data *dn)
628 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
629 __set_data_blkaddr(dn);
630 if (set_page_dirty(dn->node_page))
631 dn->node_changed = true;
634 void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
636 dn->data_blkaddr = blkaddr;
637 f2fs_set_data_blkaddr(dn);
638 f2fs_update_extent_cache(dn);
641 /* dn->ofs_in_node will be returned with up-to-date last block pointer */
642 int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
644 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
645 int err;
647 if (!count)
648 return 0;
650 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
651 return -EPERM;
652 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
653 return err;
655 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
656 dn->ofs_in_node, count);
658 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
660 for (; count > 0; dn->ofs_in_node++) {
661 block_t blkaddr = datablock_addr(dn->inode,
662 dn->node_page, dn->ofs_in_node);
663 if (blkaddr == NULL_ADDR) {
664 dn->data_blkaddr = NEW_ADDR;
665 __set_data_blkaddr(dn);
666 count--;
670 if (set_page_dirty(dn->node_page))
671 dn->node_changed = true;
672 return 0;
675 /* Should keep dn->ofs_in_node unchanged */
676 int f2fs_reserve_new_block(struct dnode_of_data *dn)
678 unsigned int ofs_in_node = dn->ofs_in_node;
679 int ret;
681 ret = f2fs_reserve_new_blocks(dn, 1);
682 dn->ofs_in_node = ofs_in_node;
683 return ret;
686 int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
688 bool need_put = dn->inode_page ? false : true;
689 int err;
691 err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE);
692 if (err)
693 return err;
695 if (dn->data_blkaddr == NULL_ADDR)
696 err = f2fs_reserve_new_block(dn);
697 if (err || need_put)
698 f2fs_put_dnode(dn);
699 return err;
702 int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
704 struct extent_info ei = {0,0,0};
705 struct inode *inode = dn->inode;
707 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
708 dn->data_blkaddr = ei.blk + index - ei.fofs;
709 return 0;
712 return f2fs_reserve_block(dn, index);
715 struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
716 int op_flags, bool for_write)
718 struct address_space *mapping = inode->i_mapping;
719 struct dnode_of_data dn;
720 struct page *page;
721 struct extent_info ei = {0,0,0};
722 int err;
724 page = f2fs_grab_cache_page(mapping, index, for_write);
725 if (!page)
726 return ERR_PTR(-ENOMEM);
728 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
729 dn.data_blkaddr = ei.blk + index - ei.fofs;
730 goto got_it;
733 set_new_dnode(&dn, inode, NULL, NULL, 0);
734 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
735 if (err)
736 goto put_err;
737 f2fs_put_dnode(&dn);
739 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
740 err = -ENOENT;
741 goto put_err;
743 got_it:
744 if (PageUptodate(page)) {
745 unlock_page(page);
746 return page;
750 * A new dentry page is allocated but not able to be written, since its
751 * new inode page couldn't be allocated due to -ENOSPC.
752 * In such the case, its blkaddr can be remained as NEW_ADDR.
753 * see, f2fs_add_link -> f2fs_get_new_data_page ->
754 * f2fs_init_inode_metadata.
756 if (dn.data_blkaddr == NEW_ADDR) {
757 zero_user_segment(page, 0, PAGE_SIZE);
758 if (!PageUptodate(page))
759 SetPageUptodate(page);
760 unlock_page(page);
761 return page;
764 err = f2fs_submit_page_read(inode, page, dn.data_blkaddr);
765 if (err)
766 goto put_err;
767 return page;
769 put_err:
770 f2fs_put_page(page, 1);
771 return ERR_PTR(err);
774 struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index)
776 struct address_space *mapping = inode->i_mapping;
777 struct page *page;
779 page = find_get_page(mapping, index);
780 if (page && PageUptodate(page))
781 return page;
782 f2fs_put_page(page, 0);
784 page = f2fs_get_read_data_page(inode, index, 0, false);
785 if (IS_ERR(page))
786 return page;
788 if (PageUptodate(page))
789 return page;
791 wait_on_page_locked(page);
792 if (unlikely(!PageUptodate(page))) {
793 f2fs_put_page(page, 0);
794 return ERR_PTR(-EIO);
796 return page;
800 * If it tries to access a hole, return an error.
801 * Because, the callers, functions in dir.c and GC, should be able to know
802 * whether this page exists or not.
804 struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
805 bool for_write)
807 struct address_space *mapping = inode->i_mapping;
808 struct page *page;
809 repeat:
810 page = f2fs_get_read_data_page(inode, index, 0, for_write);
811 if (IS_ERR(page))
812 return page;
814 /* wait for read completion */
815 lock_page(page);
816 if (unlikely(page->mapping != mapping)) {
817 f2fs_put_page(page, 1);
818 goto repeat;
820 if (unlikely(!PageUptodate(page))) {
821 f2fs_put_page(page, 1);
822 return ERR_PTR(-EIO);
824 return page;
828 * Caller ensures that this data page is never allocated.
829 * A new zero-filled data page is allocated in the page cache.
831 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
832 * f2fs_unlock_op().
833 * Note that, ipage is set only by make_empty_dir, and if any error occur,
834 * ipage should be released by this function.
836 struct page *f2fs_get_new_data_page(struct inode *inode,
837 struct page *ipage, pgoff_t index, bool new_i_size)
839 struct address_space *mapping = inode->i_mapping;
840 struct page *page;
841 struct dnode_of_data dn;
842 int err;
844 page = f2fs_grab_cache_page(mapping, index, true);
845 if (!page) {
847 * before exiting, we should make sure ipage will be released
848 * if any error occur.
850 f2fs_put_page(ipage, 1);
851 return ERR_PTR(-ENOMEM);
854 set_new_dnode(&dn, inode, ipage, NULL, 0);
855 err = f2fs_reserve_block(&dn, index);
856 if (err) {
857 f2fs_put_page(page, 1);
858 return ERR_PTR(err);
860 if (!ipage)
861 f2fs_put_dnode(&dn);
863 if (PageUptodate(page))
864 goto got_it;
866 if (dn.data_blkaddr == NEW_ADDR) {
867 zero_user_segment(page, 0, PAGE_SIZE);
868 if (!PageUptodate(page))
869 SetPageUptodate(page);
870 } else {
871 f2fs_put_page(page, 1);
873 /* if ipage exists, blkaddr should be NEW_ADDR */
874 f2fs_bug_on(F2FS_I_SB(inode), ipage);
875 page = f2fs_get_lock_data_page(inode, index, true);
876 if (IS_ERR(page))
877 return page;
879 got_it:
880 if (new_i_size && i_size_read(inode) <
881 ((loff_t)(index + 1) << PAGE_SHIFT))
882 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
883 return page;
886 static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
888 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
889 struct f2fs_summary sum;
890 struct node_info ni;
891 block_t old_blkaddr;
892 blkcnt_t count = 1;
893 int err;
895 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
896 return -EPERM;
898 err = f2fs_get_node_info(sbi, dn->nid, &ni);
899 if (err)
900 return err;
902 dn->data_blkaddr = datablock_addr(dn->inode,
903 dn->node_page, dn->ofs_in_node);
904 if (dn->data_blkaddr == NEW_ADDR)
905 goto alloc;
907 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
908 return err;
910 alloc:
911 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
912 old_blkaddr = dn->data_blkaddr;
913 f2fs_allocate_data_block(sbi, NULL, old_blkaddr, &dn->data_blkaddr,
914 &sum, seg_type, NULL, false);
915 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
916 invalidate_mapping_pages(META_MAPPING(sbi),
917 old_blkaddr, old_blkaddr);
918 f2fs_set_data_blkaddr(dn);
921 * i_size will be updated by direct_IO. Otherwise, we'll get stale
922 * data from unwritten block via dio_read.
924 return 0;
927 int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
929 struct inode *inode = file_inode(iocb->ki_filp);
930 struct f2fs_map_blocks map;
931 int flag;
932 int err = 0;
933 bool direct_io = iocb->ki_flags & IOCB_DIRECT;
935 /* convert inline data for Direct I/O*/
936 if (direct_io) {
937 err = f2fs_convert_inline_inode(inode);
938 if (err)
939 return err;
942 if (is_inode_flag_set(inode, FI_NO_PREALLOC))
943 return 0;
945 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
946 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
947 if (map.m_len > map.m_lblk)
948 map.m_len -= map.m_lblk;
949 else
950 map.m_len = 0;
952 map.m_next_pgofs = NULL;
953 map.m_next_extent = NULL;
954 map.m_seg_type = NO_CHECK_TYPE;
956 if (direct_io) {
957 map.m_seg_type = f2fs_rw_hint_to_seg_type(iocb->ki_hint);
958 flag = f2fs_force_buffered_io(inode, WRITE) ?
959 F2FS_GET_BLOCK_PRE_AIO :
960 F2FS_GET_BLOCK_PRE_DIO;
961 goto map_blocks;
963 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA(inode)) {
964 err = f2fs_convert_inline_inode(inode);
965 if (err)
966 return err;
968 if (f2fs_has_inline_data(inode))
969 return err;
971 flag = F2FS_GET_BLOCK_PRE_AIO;
973 map_blocks:
974 err = f2fs_map_blocks(inode, &map, 1, flag);
975 if (map.m_len > 0 && err == -ENOSPC) {
976 if (!direct_io)
977 set_inode_flag(inode, FI_NO_PREALLOC);
978 err = 0;
980 return err;
983 static inline void __do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
985 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
986 if (lock)
987 down_read(&sbi->node_change);
988 else
989 up_read(&sbi->node_change);
990 } else {
991 if (lock)
992 f2fs_lock_op(sbi);
993 else
994 f2fs_unlock_op(sbi);
999 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
1000 * f2fs_map_blocks structure.
1001 * If original data blocks are allocated, then give them to blockdev.
1002 * Otherwise,
1003 * a. preallocate requested block addresses
1004 * b. do not use extent cache for better performance
1005 * c. give the block addresses to blockdev
1007 int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
1008 int create, int flag)
1010 unsigned int maxblocks = map->m_len;
1011 struct dnode_of_data dn;
1012 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1013 int mode = create ? ALLOC_NODE : LOOKUP_NODE;
1014 pgoff_t pgofs, end_offset, end;
1015 int err = 0, ofs = 1;
1016 unsigned int ofs_in_node, last_ofs_in_node;
1017 blkcnt_t prealloc;
1018 struct extent_info ei = {0,0,0};
1019 block_t blkaddr;
1020 unsigned int start_pgofs;
1022 if (!maxblocks)
1023 return 0;
1025 map->m_len = 0;
1026 map->m_flags = 0;
1028 /* it only supports block size == page size */
1029 pgofs = (pgoff_t)map->m_lblk;
1030 end = pgofs + maxblocks;
1032 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
1033 map->m_pblk = ei.blk + pgofs - ei.fofs;
1034 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
1035 map->m_flags = F2FS_MAP_MAPPED;
1036 if (map->m_next_extent)
1037 *map->m_next_extent = pgofs + map->m_len;
1038 goto out;
1041 next_dnode:
1042 if (create)
1043 __do_map_lock(sbi, flag, true);
1045 /* When reading holes, we need its node page */
1046 set_new_dnode(&dn, inode, NULL, NULL, 0);
1047 err = f2fs_get_dnode_of_data(&dn, pgofs, mode);
1048 if (err) {
1049 if (flag == F2FS_GET_BLOCK_BMAP)
1050 map->m_pblk = 0;
1051 if (err == -ENOENT) {
1052 err = 0;
1053 if (map->m_next_pgofs)
1054 *map->m_next_pgofs =
1055 f2fs_get_next_page_offset(&dn, pgofs);
1056 if (map->m_next_extent)
1057 *map->m_next_extent =
1058 f2fs_get_next_page_offset(&dn, pgofs);
1060 goto unlock_out;
1063 start_pgofs = pgofs;
1064 prealloc = 0;
1065 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
1066 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1068 next_block:
1069 blkaddr = datablock_addr(dn.inode, dn.node_page, dn.ofs_in_node);
1071 if (__is_valid_data_blkaddr(blkaddr) &&
1072 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC)) {
1073 err = -EFSCORRUPTED;
1074 goto sync_out;
1077 if (!is_valid_data_blkaddr(sbi, blkaddr)) {
1078 if (create) {
1079 if (unlikely(f2fs_cp_error(sbi))) {
1080 err = -EIO;
1081 goto sync_out;
1083 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1084 if (blkaddr == NULL_ADDR) {
1085 prealloc++;
1086 last_ofs_in_node = dn.ofs_in_node;
1088 } else {
1089 WARN_ON(flag != F2FS_GET_BLOCK_PRE_DIO &&
1090 flag != F2FS_GET_BLOCK_DIO);
1091 err = __allocate_data_block(&dn,
1092 map->m_seg_type);
1093 if (!err)
1094 set_inode_flag(inode, FI_APPEND_WRITE);
1096 if (err)
1097 goto sync_out;
1098 map->m_flags |= F2FS_MAP_NEW;
1099 blkaddr = dn.data_blkaddr;
1100 } else {
1101 if (flag == F2FS_GET_BLOCK_BMAP) {
1102 map->m_pblk = 0;
1103 goto sync_out;
1105 if (flag == F2FS_GET_BLOCK_PRECACHE)
1106 goto sync_out;
1107 if (flag == F2FS_GET_BLOCK_FIEMAP &&
1108 blkaddr == NULL_ADDR) {
1109 if (map->m_next_pgofs)
1110 *map->m_next_pgofs = pgofs + 1;
1111 goto sync_out;
1113 if (flag != F2FS_GET_BLOCK_FIEMAP) {
1114 /* for defragment case */
1115 if (map->m_next_pgofs)
1116 *map->m_next_pgofs = pgofs + 1;
1117 goto sync_out;
1122 if (flag == F2FS_GET_BLOCK_PRE_AIO)
1123 goto skip;
1125 if (map->m_len == 0) {
1126 /* preallocated unwritten block should be mapped for fiemap. */
1127 if (blkaddr == NEW_ADDR)
1128 map->m_flags |= F2FS_MAP_UNWRITTEN;
1129 map->m_flags |= F2FS_MAP_MAPPED;
1131 map->m_pblk = blkaddr;
1132 map->m_len = 1;
1133 } else if ((map->m_pblk != NEW_ADDR &&
1134 blkaddr == (map->m_pblk + ofs)) ||
1135 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
1136 flag == F2FS_GET_BLOCK_PRE_DIO) {
1137 ofs++;
1138 map->m_len++;
1139 } else {
1140 goto sync_out;
1143 skip:
1144 dn.ofs_in_node++;
1145 pgofs++;
1147 /* preallocate blocks in batch for one dnode page */
1148 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
1149 (pgofs == end || dn.ofs_in_node == end_offset)) {
1151 dn.ofs_in_node = ofs_in_node;
1152 err = f2fs_reserve_new_blocks(&dn, prealloc);
1153 if (err)
1154 goto sync_out;
1156 map->m_len += dn.ofs_in_node - ofs_in_node;
1157 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
1158 err = -ENOSPC;
1159 goto sync_out;
1161 dn.ofs_in_node = end_offset;
1164 if (pgofs >= end)
1165 goto sync_out;
1166 else if (dn.ofs_in_node < end_offset)
1167 goto next_block;
1169 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1170 if (map->m_flags & F2FS_MAP_MAPPED) {
1171 unsigned int ofs = start_pgofs - map->m_lblk;
1173 f2fs_update_extent_cache_range(&dn,
1174 start_pgofs, map->m_pblk + ofs,
1175 map->m_len - ofs);
1179 f2fs_put_dnode(&dn);
1181 if (create) {
1182 __do_map_lock(sbi, flag, false);
1183 f2fs_balance_fs(sbi, dn.node_changed);
1185 goto next_dnode;
1187 sync_out:
1188 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1189 if (map->m_flags & F2FS_MAP_MAPPED) {
1190 unsigned int ofs = start_pgofs - map->m_lblk;
1192 f2fs_update_extent_cache_range(&dn,
1193 start_pgofs, map->m_pblk + ofs,
1194 map->m_len - ofs);
1196 if (map->m_next_extent)
1197 *map->m_next_extent = pgofs + 1;
1199 f2fs_put_dnode(&dn);
1200 unlock_out:
1201 if (create) {
1202 __do_map_lock(sbi, flag, false);
1203 f2fs_balance_fs(sbi, dn.node_changed);
1205 out:
1206 trace_f2fs_map_blocks(inode, map, err);
1207 return err;
1210 bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
1212 struct f2fs_map_blocks map;
1213 block_t last_lblk;
1214 int err;
1216 if (pos + len > i_size_read(inode))
1217 return false;
1219 map.m_lblk = F2FS_BYTES_TO_BLK(pos);
1220 map.m_next_pgofs = NULL;
1221 map.m_next_extent = NULL;
1222 map.m_seg_type = NO_CHECK_TYPE;
1223 last_lblk = F2FS_BLK_ALIGN(pos + len);
1225 while (map.m_lblk < last_lblk) {
1226 map.m_len = last_lblk - map.m_lblk;
1227 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
1228 if (err || map.m_len == 0)
1229 return false;
1230 map.m_lblk += map.m_len;
1232 return true;
1235 static int __get_data_block(struct inode *inode, sector_t iblock,
1236 struct buffer_head *bh, int create, int flag,
1237 pgoff_t *next_pgofs, int seg_type)
1239 struct f2fs_map_blocks map;
1240 int err;
1242 map.m_lblk = iblock;
1243 map.m_len = bh->b_size >> inode->i_blkbits;
1244 map.m_next_pgofs = next_pgofs;
1245 map.m_next_extent = NULL;
1246 map.m_seg_type = seg_type;
1248 err = f2fs_map_blocks(inode, &map, create, flag);
1249 if (!err) {
1250 map_bh(bh, inode->i_sb, map.m_pblk);
1251 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
1252 bh->b_size = (u64)map.m_len << inode->i_blkbits;
1254 return err;
1257 static int get_data_block(struct inode *inode, sector_t iblock,
1258 struct buffer_head *bh_result, int create, int flag,
1259 pgoff_t *next_pgofs)
1261 return __get_data_block(inode, iblock, bh_result, create,
1262 flag, next_pgofs,
1263 NO_CHECK_TYPE);
1266 static int get_data_block_dio(struct inode *inode, sector_t iblock,
1267 struct buffer_head *bh_result, int create)
1269 return __get_data_block(inode, iblock, bh_result, create,
1270 F2FS_GET_BLOCK_DIO, NULL,
1271 f2fs_rw_hint_to_seg_type(
1272 inode->i_write_hint));
1275 static int get_data_block_bmap(struct inode *inode, sector_t iblock,
1276 struct buffer_head *bh_result, int create)
1278 /* Block number less than F2FS MAX BLOCKS */
1279 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
1280 return -EFBIG;
1282 return __get_data_block(inode, iblock, bh_result, create,
1283 F2FS_GET_BLOCK_BMAP, NULL,
1284 NO_CHECK_TYPE);
1287 static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
1289 return (offset >> inode->i_blkbits);
1292 static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
1294 return (blk << inode->i_blkbits);
1297 static int f2fs_xattr_fiemap(struct inode *inode,
1298 struct fiemap_extent_info *fieinfo)
1300 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1301 struct page *page;
1302 struct node_info ni;
1303 __u64 phys = 0, len;
1304 __u32 flags;
1305 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
1306 int err = 0;
1308 if (f2fs_has_inline_xattr(inode)) {
1309 int offset;
1311 page = f2fs_grab_cache_page(NODE_MAPPING(sbi),
1312 inode->i_ino, false);
1313 if (!page)
1314 return -ENOMEM;
1316 err = f2fs_get_node_info(sbi, inode->i_ino, &ni);
1317 if (err) {
1318 f2fs_put_page(page, 1);
1319 return err;
1322 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1323 offset = offsetof(struct f2fs_inode, i_addr) +
1324 sizeof(__le32) * (DEF_ADDRS_PER_INODE -
1325 get_inline_xattr_addrs(inode));
1327 phys += offset;
1328 len = inline_xattr_size(inode);
1330 f2fs_put_page(page, 1);
1332 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED;
1334 if (!xnid)
1335 flags |= FIEMAP_EXTENT_LAST;
1337 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1338 if (err || err == 1)
1339 return err;
1342 if (xnid) {
1343 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false);
1344 if (!page)
1345 return -ENOMEM;
1347 err = f2fs_get_node_info(sbi, xnid, &ni);
1348 if (err) {
1349 f2fs_put_page(page, 1);
1350 return err;
1353 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1354 len = inode->i_sb->s_blocksize;
1356 f2fs_put_page(page, 1);
1358 flags = FIEMAP_EXTENT_LAST;
1361 if (phys)
1362 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1364 return (err < 0 ? err : 0);
1367 int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1368 u64 start, u64 len)
1370 struct buffer_head map_bh;
1371 sector_t start_blk, last_blk;
1372 pgoff_t next_pgofs;
1373 u64 logical = 0, phys = 0, size = 0;
1374 u32 flags = 0;
1375 int ret = 0;
1377 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
1378 ret = f2fs_precache_extents(inode);
1379 if (ret)
1380 return ret;
1383 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR);
1384 if (ret)
1385 return ret;
1387 inode_lock(inode);
1389 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1390 ret = f2fs_xattr_fiemap(inode, fieinfo);
1391 goto out;
1394 if (f2fs_has_inline_data(inode)) {
1395 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1396 if (ret != -EAGAIN)
1397 goto out;
1400 if (logical_to_blk(inode, len) == 0)
1401 len = blk_to_logical(inode, 1);
1403 start_blk = logical_to_blk(inode, start);
1404 last_blk = logical_to_blk(inode, start + len - 1);
1406 next:
1407 memset(&map_bh, 0, sizeof(struct buffer_head));
1408 map_bh.b_size = len;
1410 ret = get_data_block(inode, start_blk, &map_bh, 0,
1411 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
1412 if (ret)
1413 goto out;
1415 /* HOLE */
1416 if (!buffer_mapped(&map_bh)) {
1417 start_blk = next_pgofs;
1419 if (blk_to_logical(inode, start_blk) < blk_to_logical(inode,
1420 F2FS_I_SB(inode)->max_file_blocks))
1421 goto prep_next;
1423 flags |= FIEMAP_EXTENT_LAST;
1426 if (size) {
1427 if (f2fs_encrypted_inode(inode))
1428 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1430 ret = fiemap_fill_next_extent(fieinfo, logical,
1431 phys, size, flags);
1434 if (start_blk > last_blk || ret)
1435 goto out;
1437 logical = blk_to_logical(inode, start_blk);
1438 phys = blk_to_logical(inode, map_bh.b_blocknr);
1439 size = map_bh.b_size;
1440 flags = 0;
1441 if (buffer_unwritten(&map_bh))
1442 flags = FIEMAP_EXTENT_UNWRITTEN;
1444 start_blk += logical_to_blk(inode, size);
1446 prep_next:
1447 cond_resched();
1448 if (fatal_signal_pending(current))
1449 ret = -EINTR;
1450 else
1451 goto next;
1452 out:
1453 if (ret == 1)
1454 ret = 0;
1456 inode_unlock(inode);
1457 return ret;
1461 * This function was originally taken from fs/mpage.c, and customized for f2fs.
1462 * Major change was from block_size == page_size in f2fs by default.
1464 * Note that the aops->readpages() function is ONLY used for read-ahead. If
1465 * this function ever deviates from doing just read-ahead, it should either
1466 * use ->readpage() or do the necessary surgery to decouple ->readpages()
1467 * from read-ahead.
1469 static int f2fs_mpage_readpages(struct address_space *mapping,
1470 struct list_head *pages, struct page *page,
1471 unsigned nr_pages, bool is_readahead)
1473 struct bio *bio = NULL;
1474 sector_t last_block_in_bio = 0;
1475 struct inode *inode = mapping->host;
1476 const unsigned blkbits = inode->i_blkbits;
1477 const unsigned blocksize = 1 << blkbits;
1478 sector_t block_in_file;
1479 sector_t last_block;
1480 sector_t last_block_in_file;
1481 sector_t block_nr;
1482 struct f2fs_map_blocks map;
1484 map.m_pblk = 0;
1485 map.m_lblk = 0;
1486 map.m_len = 0;
1487 map.m_flags = 0;
1488 map.m_next_pgofs = NULL;
1489 map.m_next_extent = NULL;
1490 map.m_seg_type = NO_CHECK_TYPE;
1492 for (; nr_pages; nr_pages--) {
1493 if (pages) {
1494 page = list_last_entry(pages, struct page, lru);
1496 prefetchw(&page->flags);
1497 list_del(&page->lru);
1498 if (add_to_page_cache_lru(page, mapping,
1499 page->index,
1500 readahead_gfp_mask(mapping)))
1501 goto next_page;
1504 block_in_file = (sector_t)page->index;
1505 last_block = block_in_file + nr_pages;
1506 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
1507 blkbits;
1508 if (last_block > last_block_in_file)
1509 last_block = last_block_in_file;
1512 * Map blocks using the previous result first.
1514 if ((map.m_flags & F2FS_MAP_MAPPED) &&
1515 block_in_file > map.m_lblk &&
1516 block_in_file < (map.m_lblk + map.m_len))
1517 goto got_it;
1520 * Then do more f2fs_map_blocks() calls until we are
1521 * done with this page.
1523 map.m_flags = 0;
1525 if (block_in_file < last_block) {
1526 map.m_lblk = block_in_file;
1527 map.m_len = last_block - block_in_file;
1529 if (f2fs_map_blocks(inode, &map, 0,
1530 F2FS_GET_BLOCK_DEFAULT))
1531 goto set_error_page;
1533 got_it:
1534 if ((map.m_flags & F2FS_MAP_MAPPED)) {
1535 block_nr = map.m_pblk + block_in_file - map.m_lblk;
1536 SetPageMappedToDisk(page);
1538 if (!PageUptodate(page) && !cleancache_get_page(page)) {
1539 SetPageUptodate(page);
1540 goto confused;
1543 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr,
1544 DATA_GENERIC))
1545 goto set_error_page;
1546 } else {
1547 zero_user_segment(page, 0, PAGE_SIZE);
1548 if (!PageUptodate(page))
1549 SetPageUptodate(page);
1550 unlock_page(page);
1551 goto next_page;
1555 * This page will go to BIO. Do we need to send this
1556 * BIO off first?
1558 if (bio && (last_block_in_bio != block_nr - 1 ||
1559 !__same_bdev(F2FS_I_SB(inode), block_nr, bio))) {
1560 submit_and_realloc:
1561 __submit_bio(F2FS_I_SB(inode), bio, DATA);
1562 bio = NULL;
1564 if (bio == NULL) {
1565 bio = f2fs_grab_read_bio(inode, block_nr, nr_pages,
1566 is_readahead ? REQ_RAHEAD : 0);
1567 if (IS_ERR(bio)) {
1568 bio = NULL;
1569 goto set_error_page;
1574 * If the page is under writeback, we need to wait for
1575 * its completion to see the correct decrypted data.
1577 f2fs_wait_on_block_writeback(inode, block_nr);
1579 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1580 goto submit_and_realloc;
1582 ClearPageError(page);
1583 last_block_in_bio = block_nr;
1584 goto next_page;
1585 set_error_page:
1586 SetPageError(page);
1587 zero_user_segment(page, 0, PAGE_SIZE);
1588 unlock_page(page);
1589 goto next_page;
1590 confused:
1591 if (bio) {
1592 __submit_bio(F2FS_I_SB(inode), bio, DATA);
1593 bio = NULL;
1595 unlock_page(page);
1596 next_page:
1597 if (pages)
1598 put_page(page);
1600 BUG_ON(pages && !list_empty(pages));
1601 if (bio)
1602 __submit_bio(F2FS_I_SB(inode), bio, DATA);
1603 return 0;
1606 static int f2fs_read_data_page(struct file *file, struct page *page)
1608 struct inode *inode = page->mapping->host;
1609 int ret = -EAGAIN;
1611 trace_f2fs_readpage(page, DATA);
1613 /* If the file has inline data, try to read it directly */
1614 if (f2fs_has_inline_data(inode))
1615 ret = f2fs_read_inline_data(inode, page);
1616 if (ret == -EAGAIN)
1617 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1, false);
1618 return ret;
1621 static int f2fs_read_data_pages(struct file *file,
1622 struct address_space *mapping,
1623 struct list_head *pages, unsigned nr_pages)
1625 struct inode *inode = mapping->host;
1626 struct page *page = list_last_entry(pages, struct page, lru);
1628 trace_f2fs_readpages(inode, page, nr_pages);
1630 /* If the file has inline data, skip readpages */
1631 if (f2fs_has_inline_data(inode))
1632 return 0;
1634 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages, true);
1637 static int encrypt_one_page(struct f2fs_io_info *fio)
1639 struct inode *inode = fio->page->mapping->host;
1640 struct page *mpage;
1641 gfp_t gfp_flags = GFP_NOFS;
1643 if (!f2fs_encrypted_file(inode))
1644 return 0;
1646 /* wait for GCed page writeback via META_MAPPING */
1647 f2fs_wait_on_block_writeback(inode, fio->old_blkaddr);
1649 retry_encrypt:
1650 fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page,
1651 PAGE_SIZE, 0, fio->page->index, gfp_flags);
1652 if (IS_ERR(fio->encrypted_page)) {
1653 /* flush pending IOs and wait for a while in the ENOMEM case */
1654 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
1655 f2fs_flush_merged_writes(fio->sbi);
1656 congestion_wait(BLK_RW_ASYNC, HZ/50);
1657 gfp_flags |= __GFP_NOFAIL;
1658 goto retry_encrypt;
1660 return PTR_ERR(fio->encrypted_page);
1663 mpage = find_lock_page(META_MAPPING(fio->sbi), fio->old_blkaddr);
1664 if (mpage) {
1665 if (PageUptodate(mpage))
1666 memcpy(page_address(mpage),
1667 page_address(fio->encrypted_page), PAGE_SIZE);
1668 f2fs_put_page(mpage, 1);
1670 return 0;
1673 static inline bool check_inplace_update_policy(struct inode *inode,
1674 struct f2fs_io_info *fio)
1676 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1677 unsigned int policy = SM_I(sbi)->ipu_policy;
1679 if (policy & (0x1 << F2FS_IPU_FORCE))
1680 return true;
1681 if (policy & (0x1 << F2FS_IPU_SSR) && f2fs_need_SSR(sbi))
1682 return true;
1683 if (policy & (0x1 << F2FS_IPU_UTIL) &&
1684 utilization(sbi) > SM_I(sbi)->min_ipu_util)
1685 return true;
1686 if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && f2fs_need_SSR(sbi) &&
1687 utilization(sbi) > SM_I(sbi)->min_ipu_util)
1688 return true;
1691 * IPU for rewrite async pages
1693 if (policy & (0x1 << F2FS_IPU_ASYNC) &&
1694 fio && fio->op == REQ_OP_WRITE &&
1695 !(fio->op_flags & REQ_SYNC) &&
1696 !f2fs_encrypted_inode(inode))
1697 return true;
1699 /* this is only set during fdatasync */
1700 if (policy & (0x1 << F2FS_IPU_FSYNC) &&
1701 is_inode_flag_set(inode, FI_NEED_IPU))
1702 return true;
1704 return false;
1707 bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio)
1709 if (f2fs_is_pinned_file(inode))
1710 return true;
1712 /* if this is cold file, we should overwrite to avoid fragmentation */
1713 if (file_is_cold(inode))
1714 return true;
1716 return check_inplace_update_policy(inode, fio);
1719 bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
1721 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1723 if (test_opt(sbi, LFS))
1724 return true;
1725 if (S_ISDIR(inode->i_mode))
1726 return true;
1727 if (f2fs_is_atomic_file(inode))
1728 return true;
1729 if (fio) {
1730 if (is_cold_data(fio->page))
1731 return true;
1732 if (IS_ATOMIC_WRITTEN_PAGE(fio->page))
1733 return true;
1735 return false;
1738 static inline bool need_inplace_update(struct f2fs_io_info *fio)
1740 struct inode *inode = fio->page->mapping->host;
1742 if (f2fs_should_update_outplace(inode, fio))
1743 return false;
1745 return f2fs_should_update_inplace(inode, fio);
1748 int f2fs_do_write_data_page(struct f2fs_io_info *fio)
1750 struct page *page = fio->page;
1751 struct inode *inode = page->mapping->host;
1752 struct dnode_of_data dn;
1753 struct extent_info ei = {0,0,0};
1754 struct node_info ni;
1755 bool ipu_force = false;
1756 int err = 0;
1758 set_new_dnode(&dn, inode, NULL, NULL, 0);
1759 if (need_inplace_update(fio) &&
1760 f2fs_lookup_extent_cache(inode, page->index, &ei)) {
1761 fio->old_blkaddr = ei.blk + page->index - ei.fofs;
1763 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
1764 DATA_GENERIC))
1765 return -EFSCORRUPTED;
1767 ipu_force = true;
1768 fio->need_lock = LOCK_DONE;
1769 goto got_it;
1772 /* Deadlock due to between page->lock and f2fs_lock_op */
1773 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
1774 return -EAGAIN;
1776 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
1777 if (err)
1778 goto out;
1780 fio->old_blkaddr = dn.data_blkaddr;
1782 /* This page is already truncated */
1783 if (fio->old_blkaddr == NULL_ADDR) {
1784 ClearPageUptodate(page);
1785 clear_cold_data(page);
1786 goto out_writepage;
1788 got_it:
1789 if (__is_valid_data_blkaddr(fio->old_blkaddr) &&
1790 !f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
1791 DATA_GENERIC)) {
1792 err = -EFSCORRUPTED;
1793 goto out_writepage;
1796 * If current allocation needs SSR,
1797 * it had better in-place writes for updated data.
1799 if (ipu_force || (is_valid_data_blkaddr(fio->sbi, fio->old_blkaddr) &&
1800 need_inplace_update(fio))) {
1801 err = encrypt_one_page(fio);
1802 if (err)
1803 goto out_writepage;
1805 set_page_writeback(page);
1806 ClearPageError(page);
1807 f2fs_put_dnode(&dn);
1808 if (fio->need_lock == LOCK_REQ)
1809 f2fs_unlock_op(fio->sbi);
1810 err = f2fs_inplace_write_data(fio);
1811 trace_f2fs_do_write_data_page(fio->page, IPU);
1812 set_inode_flag(inode, FI_UPDATE_WRITE);
1813 return err;
1816 if (fio->need_lock == LOCK_RETRY) {
1817 if (!f2fs_trylock_op(fio->sbi)) {
1818 err = -EAGAIN;
1819 goto out_writepage;
1821 fio->need_lock = LOCK_REQ;
1824 err = f2fs_get_node_info(fio->sbi, dn.nid, &ni);
1825 if (err)
1826 goto out_writepage;
1828 fio->version = ni.version;
1830 err = encrypt_one_page(fio);
1831 if (err)
1832 goto out_writepage;
1834 set_page_writeback(page);
1835 ClearPageError(page);
1837 /* LFS mode write path */
1838 f2fs_outplace_write_data(&dn, fio);
1839 trace_f2fs_do_write_data_page(page, OPU);
1840 set_inode_flag(inode, FI_APPEND_WRITE);
1841 if (page->index == 0)
1842 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
1843 out_writepage:
1844 f2fs_put_dnode(&dn);
1845 out:
1846 if (fio->need_lock == LOCK_REQ)
1847 f2fs_unlock_op(fio->sbi);
1848 return err;
1851 static int __write_data_page(struct page *page, bool *submitted,
1852 struct writeback_control *wbc,
1853 enum iostat_type io_type)
1855 struct inode *inode = page->mapping->host;
1856 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1857 loff_t i_size = i_size_read(inode);
1858 const pgoff_t end_index = ((unsigned long long) i_size)
1859 >> PAGE_SHIFT;
1860 loff_t psize = (loff_t)(page->index + 1) << PAGE_SHIFT;
1861 unsigned offset = 0;
1862 bool need_balance_fs = false;
1863 int err = 0;
1864 struct f2fs_io_info fio = {
1865 .sbi = sbi,
1866 .ino = inode->i_ino,
1867 .type = DATA,
1868 .op = REQ_OP_WRITE,
1869 .op_flags = wbc_to_write_flags(wbc),
1870 .old_blkaddr = NULL_ADDR,
1871 .page = page,
1872 .encrypted_page = NULL,
1873 .submitted = false,
1874 .need_lock = LOCK_RETRY,
1875 .io_type = io_type,
1876 .io_wbc = wbc,
1879 trace_f2fs_writepage(page, DATA);
1881 /* we should bypass data pages to proceed the kworkder jobs */
1882 if (unlikely(f2fs_cp_error(sbi))) {
1883 mapping_set_error(page->mapping, -EIO);
1885 * don't drop any dirty dentry pages for keeping lastest
1886 * directory structure.
1888 if (S_ISDIR(inode->i_mode))
1889 goto redirty_out;
1890 goto out;
1893 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1894 goto redirty_out;
1896 if (page->index < end_index)
1897 goto write;
1900 * If the offset is out-of-range of file size,
1901 * this page does not have to be written to disk.
1903 offset = i_size & (PAGE_SIZE - 1);
1904 if ((page->index >= end_index + 1) || !offset)
1905 goto out;
1907 zero_user_segment(page, offset, PAGE_SIZE);
1908 write:
1909 if (f2fs_is_drop_cache(inode))
1910 goto out;
1911 /* we should not write 0'th page having journal header */
1912 if (f2fs_is_volatile_file(inode) && (!page->index ||
1913 (!wbc->for_reclaim &&
1914 f2fs_available_free_memory(sbi, BASE_CHECK))))
1915 goto redirty_out;
1917 /* Dentry blocks are controlled by checkpoint */
1918 if (S_ISDIR(inode->i_mode)) {
1919 fio.need_lock = LOCK_DONE;
1920 err = f2fs_do_write_data_page(&fio);
1921 goto done;
1924 if (!wbc->for_reclaim)
1925 need_balance_fs = true;
1926 else if (has_not_enough_free_secs(sbi, 0, 0))
1927 goto redirty_out;
1928 else
1929 set_inode_flag(inode, FI_HOT_DATA);
1931 err = -EAGAIN;
1932 if (f2fs_has_inline_data(inode)) {
1933 err = f2fs_write_inline_data(inode, page);
1934 if (!err)
1935 goto out;
1938 if (err == -EAGAIN) {
1939 err = f2fs_do_write_data_page(&fio);
1940 if (err == -EAGAIN) {
1941 fio.need_lock = LOCK_REQ;
1942 err = f2fs_do_write_data_page(&fio);
1946 if (err) {
1947 file_set_keep_isize(inode);
1948 } else {
1949 down_write(&F2FS_I(inode)->i_sem);
1950 if (F2FS_I(inode)->last_disk_size < psize)
1951 F2FS_I(inode)->last_disk_size = psize;
1952 up_write(&F2FS_I(inode)->i_sem);
1955 done:
1956 if (err && err != -ENOENT)
1957 goto redirty_out;
1959 out:
1960 inode_dec_dirty_pages(inode);
1961 if (err) {
1962 ClearPageUptodate(page);
1963 clear_cold_data(page);
1966 if (wbc->for_reclaim) {
1967 f2fs_submit_merged_write_cond(sbi, inode, 0, page->index, DATA);
1968 clear_inode_flag(inode, FI_HOT_DATA);
1969 f2fs_remove_dirty_inode(inode);
1970 submitted = NULL;
1973 unlock_page(page);
1974 if (!S_ISDIR(inode->i_mode))
1975 f2fs_balance_fs(sbi, need_balance_fs);
1977 if (unlikely(f2fs_cp_error(sbi))) {
1978 f2fs_submit_merged_write(sbi, DATA);
1979 submitted = NULL;
1982 if (submitted)
1983 *submitted = fio.submitted;
1985 return 0;
1987 redirty_out:
1988 redirty_page_for_writepage(wbc, page);
1990 * pageout() in MM traslates EAGAIN, so calls handle_write_error()
1991 * -> mapping_set_error() -> set_bit(AS_EIO, ...).
1992 * file_write_and_wait_range() will see EIO error, which is critical
1993 * to return value of fsync() followed by atomic_write failure to user.
1995 if (!err || wbc->for_reclaim)
1996 return AOP_WRITEPAGE_ACTIVATE;
1997 unlock_page(page);
1998 return err;
2001 static int f2fs_write_data_page(struct page *page,
2002 struct writeback_control *wbc)
2004 return __write_data_page(page, NULL, wbc, FS_DATA_IO);
2008 * This function was copied from write_cche_pages from mm/page-writeback.c.
2009 * The major change is making write step of cold data page separately from
2010 * warm/hot data page.
2012 static int f2fs_write_cache_pages(struct address_space *mapping,
2013 struct writeback_control *wbc,
2014 enum iostat_type io_type)
2016 int ret = 0;
2017 int done = 0;
2018 struct pagevec pvec;
2019 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
2020 int nr_pages;
2021 pgoff_t uninitialized_var(writeback_index);
2022 pgoff_t index;
2023 pgoff_t end; /* Inclusive */
2024 pgoff_t done_index;
2025 pgoff_t last_idx = ULONG_MAX;
2026 int cycled;
2027 int range_whole = 0;
2028 int tag;
2030 pagevec_init(&pvec);
2032 if (get_dirty_pages(mapping->host) <=
2033 SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
2034 set_inode_flag(mapping->host, FI_HOT_DATA);
2035 else
2036 clear_inode_flag(mapping->host, FI_HOT_DATA);
2038 if (wbc->range_cyclic) {
2039 writeback_index = mapping->writeback_index; /* prev offset */
2040 index = writeback_index;
2041 if (index == 0)
2042 cycled = 1;
2043 else
2044 cycled = 0;
2045 end = -1;
2046 } else {
2047 index = wbc->range_start >> PAGE_SHIFT;
2048 end = wbc->range_end >> PAGE_SHIFT;
2049 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2050 range_whole = 1;
2051 cycled = 1; /* ignore range_cyclic tests */
2053 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2054 tag = PAGECACHE_TAG_TOWRITE;
2055 else
2056 tag = PAGECACHE_TAG_DIRTY;
2057 retry:
2058 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2059 tag_pages_for_writeback(mapping, index, end);
2060 done_index = index;
2061 while (!done && (index <= end)) {
2062 int i;
2064 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
2065 tag);
2066 if (nr_pages == 0)
2067 break;
2069 for (i = 0; i < nr_pages; i++) {
2070 struct page *page = pvec.pages[i];
2071 bool submitted = false;
2073 /* give a priority to WB_SYNC threads */
2074 if (atomic_read(&sbi->wb_sync_req[DATA]) &&
2075 wbc->sync_mode == WB_SYNC_NONE) {
2076 done = 1;
2077 break;
2080 done_index = page->index;
2081 retry_write:
2082 lock_page(page);
2084 if (unlikely(page->mapping != mapping)) {
2085 continue_unlock:
2086 unlock_page(page);
2087 continue;
2090 if (!PageDirty(page)) {
2091 /* someone wrote it for us */
2092 goto continue_unlock;
2095 if (PageWriteback(page)) {
2096 if (wbc->sync_mode != WB_SYNC_NONE)
2097 f2fs_wait_on_page_writeback(page,
2098 DATA, true);
2099 else
2100 goto continue_unlock;
2103 BUG_ON(PageWriteback(page));
2104 if (!clear_page_dirty_for_io(page))
2105 goto continue_unlock;
2107 ret = __write_data_page(page, &submitted, wbc, io_type);
2108 if (unlikely(ret)) {
2110 * keep nr_to_write, since vfs uses this to
2111 * get # of written pages.
2113 if (ret == AOP_WRITEPAGE_ACTIVATE) {
2114 unlock_page(page);
2115 ret = 0;
2116 continue;
2117 } else if (ret == -EAGAIN) {
2118 ret = 0;
2119 if (wbc->sync_mode == WB_SYNC_ALL) {
2120 cond_resched();
2121 congestion_wait(BLK_RW_ASYNC,
2122 HZ/50);
2123 goto retry_write;
2125 continue;
2127 done_index = page->index + 1;
2128 done = 1;
2129 break;
2130 } else if (submitted) {
2131 last_idx = page->index;
2134 if (--wbc->nr_to_write <= 0 &&
2135 wbc->sync_mode == WB_SYNC_NONE) {
2136 done = 1;
2137 break;
2140 pagevec_release(&pvec);
2141 cond_resched();
2144 if (!cycled && !done) {
2145 cycled = 1;
2146 index = 0;
2147 end = writeback_index - 1;
2148 goto retry;
2150 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2151 mapping->writeback_index = done_index;
2153 if (last_idx != ULONG_MAX)
2154 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
2155 0, last_idx, DATA);
2157 return ret;
2160 static inline bool __should_serialize_io(struct inode *inode,
2161 struct writeback_control *wbc)
2163 if (!S_ISREG(inode->i_mode))
2164 return false;
2165 if (wbc->sync_mode != WB_SYNC_ALL)
2166 return true;
2167 if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks)
2168 return true;
2169 return false;
2172 static int __f2fs_write_data_pages(struct address_space *mapping,
2173 struct writeback_control *wbc,
2174 enum iostat_type io_type)
2176 struct inode *inode = mapping->host;
2177 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2178 struct blk_plug plug;
2179 int ret;
2180 bool locked = false;
2182 /* deal with chardevs and other special file */
2183 if (!mapping->a_ops->writepage)
2184 return 0;
2186 /* skip writing if there is no dirty page in this inode */
2187 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
2188 return 0;
2190 /* during POR, we don't need to trigger writepage at all. */
2191 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2192 goto skip_write;
2194 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
2195 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
2196 f2fs_available_free_memory(sbi, DIRTY_DENTS))
2197 goto skip_write;
2199 /* skip writing during file defragment */
2200 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
2201 goto skip_write;
2203 trace_f2fs_writepages(mapping->host, wbc, DATA);
2205 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
2206 if (wbc->sync_mode == WB_SYNC_ALL)
2207 atomic_inc(&sbi->wb_sync_req[DATA]);
2208 else if (atomic_read(&sbi->wb_sync_req[DATA]))
2209 goto skip_write;
2211 if (__should_serialize_io(inode, wbc)) {
2212 mutex_lock(&sbi->writepages);
2213 locked = true;
2216 blk_start_plug(&plug);
2217 ret = f2fs_write_cache_pages(mapping, wbc, io_type);
2218 blk_finish_plug(&plug);
2220 if (locked)
2221 mutex_unlock(&sbi->writepages);
2223 if (wbc->sync_mode == WB_SYNC_ALL)
2224 atomic_dec(&sbi->wb_sync_req[DATA]);
2226 * if some pages were truncated, we cannot guarantee its mapping->host
2227 * to detect pending bios.
2230 f2fs_remove_dirty_inode(inode);
2231 return ret;
2233 skip_write:
2234 wbc->pages_skipped += get_dirty_pages(inode);
2235 trace_f2fs_writepages(mapping->host, wbc, DATA);
2236 return 0;
2239 static int f2fs_write_data_pages(struct address_space *mapping,
2240 struct writeback_control *wbc)
2242 struct inode *inode = mapping->host;
2244 return __f2fs_write_data_pages(mapping, wbc,
2245 F2FS_I(inode)->cp_task == current ?
2246 FS_CP_DATA_IO : FS_DATA_IO);
2249 static void f2fs_write_failed(struct address_space *mapping, loff_t to)
2251 struct inode *inode = mapping->host;
2252 loff_t i_size = i_size_read(inode);
2254 if (to > i_size) {
2255 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
2256 down_write(&F2FS_I(inode)->i_mmap_sem);
2258 truncate_pagecache(inode, i_size);
2259 f2fs_truncate_blocks(inode, i_size, true);
2261 up_write(&F2FS_I(inode)->i_mmap_sem);
2262 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
2266 static int prepare_write_begin(struct f2fs_sb_info *sbi,
2267 struct page *page, loff_t pos, unsigned len,
2268 block_t *blk_addr, bool *node_changed)
2270 struct inode *inode = page->mapping->host;
2271 pgoff_t index = page->index;
2272 struct dnode_of_data dn;
2273 struct page *ipage;
2274 bool locked = false;
2275 struct extent_info ei = {0,0,0};
2276 int err = 0;
2277 int flag;
2280 * we already allocated all the blocks, so we don't need to get
2281 * the block addresses when there is no need to fill the page.
2283 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
2284 !is_inode_flag_set(inode, FI_NO_PREALLOC))
2285 return 0;
2287 /* f2fs_lock_op avoids race between write CP and convert_inline_page */
2288 if (f2fs_has_inline_data(inode) && pos + len > MAX_INLINE_DATA(inode))
2289 flag = F2FS_GET_BLOCK_DEFAULT;
2290 else
2291 flag = F2FS_GET_BLOCK_PRE_AIO;
2293 if (f2fs_has_inline_data(inode) ||
2294 (pos & PAGE_MASK) >= i_size_read(inode)) {
2295 __do_map_lock(sbi, flag, true);
2296 locked = true;
2298 restart:
2299 /* check inline_data */
2300 ipage = f2fs_get_node_page(sbi, inode->i_ino);
2301 if (IS_ERR(ipage)) {
2302 err = PTR_ERR(ipage);
2303 goto unlock_out;
2306 set_new_dnode(&dn, inode, ipage, ipage, 0);
2308 if (f2fs_has_inline_data(inode)) {
2309 if (pos + len <= MAX_INLINE_DATA(inode)) {
2310 f2fs_do_read_inline_data(page, ipage);
2311 set_inode_flag(inode, FI_DATA_EXIST);
2312 if (inode->i_nlink)
2313 set_inline_node(ipage);
2314 } else {
2315 err = f2fs_convert_inline_page(&dn, page);
2316 if (err)
2317 goto out;
2318 if (dn.data_blkaddr == NULL_ADDR)
2319 err = f2fs_get_block(&dn, index);
2321 } else if (locked) {
2322 err = f2fs_get_block(&dn, index);
2323 } else {
2324 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
2325 dn.data_blkaddr = ei.blk + index - ei.fofs;
2326 } else {
2327 /* hole case */
2328 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
2329 if (err || dn.data_blkaddr == NULL_ADDR) {
2330 f2fs_put_dnode(&dn);
2331 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
2332 true);
2333 WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO);
2334 locked = true;
2335 goto restart;
2340 /* convert_inline_page can make node_changed */
2341 *blk_addr = dn.data_blkaddr;
2342 *node_changed = dn.node_changed;
2343 out:
2344 f2fs_put_dnode(&dn);
2345 unlock_out:
2346 if (locked)
2347 __do_map_lock(sbi, flag, false);
2348 return err;
2351 static int f2fs_write_begin(struct file *file, struct address_space *mapping,
2352 loff_t pos, unsigned len, unsigned flags,
2353 struct page **pagep, void **fsdata)
2355 struct inode *inode = mapping->host;
2356 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2357 struct page *page = NULL;
2358 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
2359 bool need_balance = false, drop_atomic = false;
2360 block_t blkaddr = NULL_ADDR;
2361 int err = 0;
2363 trace_f2fs_write_begin(inode, pos, len, flags);
2365 if ((f2fs_is_atomic_file(inode) &&
2366 !f2fs_available_free_memory(sbi, INMEM_PAGES)) ||
2367 is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
2368 err = -ENOMEM;
2369 drop_atomic = true;
2370 goto fail;
2374 * We should check this at this moment to avoid deadlock on inode page
2375 * and #0 page. The locking rule for inline_data conversion should be:
2376 * lock_page(page #0) -> lock_page(inode_page)
2378 if (index != 0) {
2379 err = f2fs_convert_inline_inode(inode);
2380 if (err)
2381 goto fail;
2383 repeat:
2385 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
2386 * wait_for_stable_page. Will wait that below with our IO control.
2388 page = f2fs_pagecache_get_page(mapping, index,
2389 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
2390 if (!page) {
2391 err = -ENOMEM;
2392 goto fail;
2395 *pagep = page;
2397 err = prepare_write_begin(sbi, page, pos, len,
2398 &blkaddr, &need_balance);
2399 if (err)
2400 goto fail;
2402 if (need_balance && has_not_enough_free_secs(sbi, 0, 0)) {
2403 unlock_page(page);
2404 f2fs_balance_fs(sbi, true);
2405 lock_page(page);
2406 if (page->mapping != mapping) {
2407 /* The page got truncated from under us */
2408 f2fs_put_page(page, 1);
2409 goto repeat;
2413 f2fs_wait_on_page_writeback(page, DATA, false);
2415 if (len == PAGE_SIZE || PageUptodate(page))
2416 return 0;
2418 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode)) {
2419 zero_user_segment(page, len, PAGE_SIZE);
2420 return 0;
2423 if (blkaddr == NEW_ADDR) {
2424 zero_user_segment(page, 0, PAGE_SIZE);
2425 SetPageUptodate(page);
2426 } else {
2427 err = f2fs_submit_page_read(inode, page, blkaddr);
2428 if (err)
2429 goto fail;
2431 lock_page(page);
2432 if (unlikely(page->mapping != mapping)) {
2433 f2fs_put_page(page, 1);
2434 goto repeat;
2436 if (unlikely(!PageUptodate(page))) {
2437 err = -EIO;
2438 goto fail;
2441 return 0;
2443 fail:
2444 f2fs_put_page(page, 1);
2445 f2fs_write_failed(mapping, pos + len);
2446 if (drop_atomic)
2447 f2fs_drop_inmem_pages_all(sbi, false);
2448 return err;
2451 static int f2fs_write_end(struct file *file,
2452 struct address_space *mapping,
2453 loff_t pos, unsigned len, unsigned copied,
2454 struct page *page, void *fsdata)
2456 struct inode *inode = page->mapping->host;
2458 trace_f2fs_write_end(inode, pos, len, copied);
2461 * This should be come from len == PAGE_SIZE, and we expect copied
2462 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
2463 * let generic_perform_write() try to copy data again through copied=0.
2465 if (!PageUptodate(page)) {
2466 if (unlikely(copied != len))
2467 copied = 0;
2468 else
2469 SetPageUptodate(page);
2471 if (!copied)
2472 goto unlock_out;
2474 set_page_dirty(page);
2476 if (pos + copied > i_size_read(inode))
2477 f2fs_i_size_write(inode, pos + copied);
2478 unlock_out:
2479 f2fs_put_page(page, 1);
2480 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2481 return copied;
2484 static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
2485 loff_t offset)
2487 unsigned i_blkbits = READ_ONCE(inode->i_blkbits);
2488 unsigned blkbits = i_blkbits;
2489 unsigned blocksize_mask = (1 << blkbits) - 1;
2490 unsigned long align = offset | iov_iter_alignment(iter);
2491 struct block_device *bdev = inode->i_sb->s_bdev;
2493 if (align & blocksize_mask) {
2494 if (bdev)
2495 blkbits = blksize_bits(bdev_logical_block_size(bdev));
2496 blocksize_mask = (1 << blkbits) - 1;
2497 if (align & blocksize_mask)
2498 return -EINVAL;
2499 return 1;
2501 return 0;
2504 static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
2506 struct address_space *mapping = iocb->ki_filp->f_mapping;
2507 struct inode *inode = mapping->host;
2508 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2509 size_t count = iov_iter_count(iter);
2510 loff_t offset = iocb->ki_pos;
2511 int rw = iov_iter_rw(iter);
2512 int err;
2513 enum rw_hint hint = iocb->ki_hint;
2514 int whint_mode = F2FS_OPTION(sbi).whint_mode;
2516 err = check_direct_IO(inode, iter, offset);
2517 if (err)
2518 return err < 0 ? err : 0;
2520 if (f2fs_force_buffered_io(inode, rw))
2521 return 0;
2523 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
2525 if (rw == WRITE && whint_mode == WHINT_MODE_OFF)
2526 iocb->ki_hint = WRITE_LIFE_NOT_SET;
2528 if (!down_read_trylock(&F2FS_I(inode)->i_gc_rwsem[rw])) {
2529 if (iocb->ki_flags & IOCB_NOWAIT) {
2530 iocb->ki_hint = hint;
2531 err = -EAGAIN;
2532 goto out;
2534 down_read(&F2FS_I(inode)->i_gc_rwsem[rw]);
2537 err = blockdev_direct_IO(iocb, inode, iter, get_data_block_dio);
2538 up_read(&F2FS_I(inode)->i_gc_rwsem[rw]);
2540 if (rw == WRITE) {
2541 if (whint_mode == WHINT_MODE_OFF)
2542 iocb->ki_hint = hint;
2543 if (err > 0) {
2544 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
2545 err);
2546 set_inode_flag(inode, FI_UPDATE_WRITE);
2547 } else if (err < 0) {
2548 f2fs_write_failed(mapping, offset + count);
2552 out:
2553 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
2555 return err;
2558 void f2fs_invalidate_page(struct page *page, unsigned int offset,
2559 unsigned int length)
2561 struct inode *inode = page->mapping->host;
2562 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2564 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
2565 (offset % PAGE_SIZE || length != PAGE_SIZE))
2566 return;
2568 if (PageDirty(page)) {
2569 if (inode->i_ino == F2FS_META_INO(sbi)) {
2570 dec_page_count(sbi, F2FS_DIRTY_META);
2571 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
2572 dec_page_count(sbi, F2FS_DIRTY_NODES);
2573 } else {
2574 inode_dec_dirty_pages(inode);
2575 f2fs_remove_dirty_inode(inode);
2579 clear_cold_data(page);
2581 /* This is atomic written page, keep Private */
2582 if (IS_ATOMIC_WRITTEN_PAGE(page))
2583 return f2fs_drop_inmem_page(inode, page);
2585 set_page_private(page, 0);
2586 ClearPagePrivate(page);
2589 int f2fs_release_page(struct page *page, gfp_t wait)
2591 /* If this is dirty page, keep PagePrivate */
2592 if (PageDirty(page))
2593 return 0;
2595 /* This is atomic written page, keep Private */
2596 if (IS_ATOMIC_WRITTEN_PAGE(page))
2597 return 0;
2599 clear_cold_data(page);
2600 set_page_private(page, 0);
2601 ClearPagePrivate(page);
2602 return 1;
2605 static int f2fs_set_data_page_dirty(struct page *page)
2607 struct address_space *mapping = page->mapping;
2608 struct inode *inode = mapping->host;
2610 trace_f2fs_set_page_dirty(page, DATA);
2612 if (!PageUptodate(page))
2613 SetPageUptodate(page);
2615 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
2616 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
2617 f2fs_register_inmem_page(inode, page);
2618 return 1;
2621 * Previously, this page has been registered, we just
2622 * return here.
2624 return 0;
2627 if (!PageDirty(page)) {
2628 __set_page_dirty_nobuffers(page);
2629 f2fs_update_dirty_page(inode, page);
2630 return 1;
2632 return 0;
2635 static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
2637 struct inode *inode = mapping->host;
2639 if (f2fs_has_inline_data(inode))
2640 return 0;
2642 /* make sure allocating whole blocks */
2643 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
2644 filemap_write_and_wait(mapping);
2646 return generic_block_bmap(mapping, block, get_data_block_bmap);
2649 #ifdef CONFIG_MIGRATION
2650 #include <linux/migrate.h>
2652 int f2fs_migrate_page(struct address_space *mapping,
2653 struct page *newpage, struct page *page, enum migrate_mode mode)
2655 int rc, extra_count;
2656 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
2657 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
2659 BUG_ON(PageWriteback(page));
2661 /* migrating an atomic written page is safe with the inmem_lock hold */
2662 if (atomic_written) {
2663 if (mode != MIGRATE_SYNC)
2664 return -EBUSY;
2665 if (!mutex_trylock(&fi->inmem_lock))
2666 return -EAGAIN;
2670 * A reference is expected if PagePrivate set when move mapping,
2671 * however F2FS breaks this for maintaining dirty page counts when
2672 * truncating pages. So here adjusting the 'extra_count' make it work.
2674 extra_count = (atomic_written ? 1 : 0) - page_has_private(page);
2675 rc = migrate_page_move_mapping(mapping, newpage,
2676 page, NULL, mode, extra_count);
2677 if (rc != MIGRATEPAGE_SUCCESS) {
2678 if (atomic_written)
2679 mutex_unlock(&fi->inmem_lock);
2680 return rc;
2683 if (atomic_written) {
2684 struct inmem_pages *cur;
2685 list_for_each_entry(cur, &fi->inmem_pages, list)
2686 if (cur->page == page) {
2687 cur->page = newpage;
2688 break;
2690 mutex_unlock(&fi->inmem_lock);
2691 put_page(page);
2692 get_page(newpage);
2695 if (PagePrivate(page))
2696 SetPagePrivate(newpage);
2697 set_page_private(newpage, page_private(page));
2699 if (mode != MIGRATE_SYNC_NO_COPY)
2700 migrate_page_copy(newpage, page);
2701 else
2702 migrate_page_states(newpage, page);
2704 return MIGRATEPAGE_SUCCESS;
2706 #endif
2708 const struct address_space_operations f2fs_dblock_aops = {
2709 .readpage = f2fs_read_data_page,
2710 .readpages = f2fs_read_data_pages,
2711 .writepage = f2fs_write_data_page,
2712 .writepages = f2fs_write_data_pages,
2713 .write_begin = f2fs_write_begin,
2714 .write_end = f2fs_write_end,
2715 .set_page_dirty = f2fs_set_data_page_dirty,
2716 .invalidatepage = f2fs_invalidate_page,
2717 .releasepage = f2fs_release_page,
2718 .direct_IO = f2fs_direct_IO,
2719 .bmap = f2fs_bmap,
2720 #ifdef CONFIG_MIGRATION
2721 .migratepage = f2fs_migrate_page,
2722 #endif
2725 void f2fs_clear_radix_tree_dirty_tag(struct page *page)
2727 struct address_space *mapping = page_mapping(page);
2728 unsigned long flags;
2730 xa_lock_irqsave(&mapping->i_pages, flags);
2731 radix_tree_tag_clear(&mapping->i_pages, page_index(page),
2732 PAGECACHE_TAG_DIRTY);
2733 xa_unlock_irqrestore(&mapping->i_pages, flags);
2736 int __init f2fs_init_post_read_processing(void)
2738 bio_post_read_ctx_cache = KMEM_CACHE(bio_post_read_ctx, 0);
2739 if (!bio_post_read_ctx_cache)
2740 goto fail;
2741 bio_post_read_ctx_pool =
2742 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
2743 bio_post_read_ctx_cache);
2744 if (!bio_post_read_ctx_pool)
2745 goto fail_free_cache;
2746 return 0;
2748 fail_free_cache:
2749 kmem_cache_destroy(bio_post_read_ctx_cache);
2750 fail:
2751 return -ENOMEM;
2754 void __exit f2fs_destroy_post_read_processing(void)
2756 mempool_destroy(bio_post_read_ctx_pool);
2757 kmem_cache_destroy(bio_post_read_ctx_cache);