x86/topology: Update the 'cpu cores' field in /proc/cpuinfo correctly across CPU...
[cris-mirror.git] / fs / f2fs / data.c
blob7578ed1a85e05840f44e3a031831a4ba74fe43a7
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/mm.h>
23 #include <linux/memcontrol.h>
24 #include <linux/cleancache.h>
25 #include <linux/sched/signal.h>
27 #include "f2fs.h"
28 #include "node.h"
29 #include "segment.h"
30 #include "trace.h"
31 #include <trace/events/f2fs.h>
33 static bool __is_cp_guaranteed(struct page *page)
35 struct address_space *mapping = page->mapping;
36 struct inode *inode;
37 struct f2fs_sb_info *sbi;
39 if (!mapping)
40 return false;
42 inode = mapping->host;
43 sbi = F2FS_I_SB(inode);
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;
53 static void f2fs_read_end_io(struct bio *bio)
55 struct bio_vec *bvec;
56 int i;
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;
63 #endif
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;
74 bio_for_each_segment_all(bvec, bio, i) {
75 struct page *page = bvec->bv_page;
77 if (!bio->bi_status) {
78 if (!PageUptodate(page))
79 SetPageUptodate(page);
80 } else {
81 ClearPageUptodate(page);
82 SetPageError(page);
84 unlock_page(page);
86 bio_put(bio);
89 static void f2fs_write_end_io(struct bio *bio)
91 struct f2fs_sb_info *sbi = bio->bi_private;
92 struct bio_vec *bvec;
93 int i;
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);
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);
105 if (unlikely(bio->bi_status))
106 f2fs_stop_checkpoint(sbi, true);
107 continue;
110 fscrypt_pullback_bio_page(&page, true);
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);
118 f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) &&
119 page->index != nid_of_node(page));
121 dec_page_count(sbi, type);
122 clear_cold_data(page);
123 end_page_writeback(page);
125 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
126 wq_has_sleeper(&sbi->cp_wait))
127 wake_up(&sbi->cp_wait);
129 bio_put(bio);
133 * Return true, if pre_bio's bdev is same as its target device.
135 struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
136 block_t blk_addr, struct bio *bio)
138 struct block_device *bdev = sbi->sb->s_bdev;
139 int i;
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;
149 if (bio) {
150 bio_set_dev(bio, bdev);
151 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
153 return bdev;
156 int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
158 int i;
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;
166 static bool __same_bdev(struct f2fs_sb_info *sbi,
167 block_t blk_addr, struct bio *bio)
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;
174 * Low-level block read/write IO operations.
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)
180 struct bio *bio;
182 bio = f2fs_bio_alloc(sbi, npages, true);
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);
190 return bio;
193 static inline void __submit_bio(struct f2fs_sb_info *sbi,
194 struct bio *bio, enum page_type type)
196 if (!is_read_io(bio_op(bio))) {
197 unsigned int start;
199 if (f2fs_sb_mounted_blkzoned(sbi->sb) &&
200 current->plug && (type == DATA || type == NODE))
201 blk_finish_plug(current->plug);
203 if (type != DATA && type != NODE)
204 goto submit_io;
206 start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
207 start %= F2FS_IO_SIZE(sbi);
209 if (start == 0)
210 goto submit_io;
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);
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);
226 * In the NODE case, we lose next block address chain. So, we
227 * need to do checkpoint in f2fs_sync_file.
229 if (type == NODE)
230 set_sbi_flag(sbi, SBI_NEED_CP);
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);
240 static void __submit_merged_bio(struct f2fs_bio_info *io)
242 struct f2fs_io_info *fio = &io->fio;
244 if (!io->bio)
245 return;
247 bio_set_op_attrs(io->bio, fio->op, fio->op_flags);
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);
254 __submit_bio(io->sbi, io->bio, fio->type);
255 io->bio = NULL;
258 static bool __has_merged_page(struct f2fs_bio_info *io,
259 struct inode *inode, nid_t ino, pgoff_t idx)
261 struct bio_vec *bvec;
262 struct page *target;
263 int i;
265 if (!io->bio)
266 return false;
268 if (!inode && !ino)
269 return true;
271 bio_for_each_segment_all(bvec, io->bio, i) {
273 if (bvec->bv_page->mapping)
274 target = bvec->bv_page;
275 else
276 target = fscrypt_control_page(bvec->bv_page);
278 if (idx != target->index)
279 continue;
281 if (inode && inode == target->mapping->host)
282 return true;
283 if (ino && ino == ino_of_node(target))
284 return true;
287 return false;
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)
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;
298 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
299 io = sbi->write_io[btype] + temp;
301 down_read(&io->io_rwsem);
302 ret = __has_merged_page(io, inode, ino, idx);
303 up_read(&io->io_rwsem);
305 /* TODO: use HOT temp only for meta pages now. */
306 if (ret || btype == META)
307 break;
309 return ret;
312 static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
313 enum page_type type, enum temp_type temp)
315 enum page_type btype = PAGE_TYPE_OF_BIO(type);
316 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
318 down_write(&io->io_rwsem);
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;
328 __submit_merged_bio(io);
329 up_write(&io->io_rwsem);
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)
336 enum temp_type temp;
338 if (!force && !has_merged_page(sbi, inode, ino, idx, type))
339 return;
341 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
343 __f2fs_submit_merged_write(sbi, type, temp);
345 /* TODO: use HOT temp only for meta pages now. */
346 if (type >= META)
347 break;
351 void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
353 __submit_merged_write_cond(sbi, NULL, 0, 0, type, true);
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)
360 __submit_merged_write_cond(sbi, inode, ino, idx, type, false);
363 void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
365 f2fs_submit_merged_write(sbi, DATA);
366 f2fs_submit_merged_write(sbi, NODE);
367 f2fs_submit_merged_write(sbi, META);
371 * Fill the locked page with data located in the block address.
372 * A caller needs to unlock the page on failure.
374 int f2fs_submit_page_bio(struct f2fs_io_info *fio)
376 struct bio *bio;
377 struct page *page = fio->encrypted_page ?
378 fio->encrypted_page : fio->page;
380 trace_f2fs_submit_page_bio(page, fio);
381 f2fs_trace_ios(fio, 0);
383 /* Allocate a new bio */
384 bio = __bio_alloc(fio->sbi, fio->new_blkaddr, fio->io_wbc,
385 1, is_read_io(fio->op));
387 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
388 bio_put(bio);
389 return -EFAULT;
391 bio_set_op_attrs(bio, fio->op, fio->op_flags);
393 __submit_bio(fio->sbi, bio, fio->type);
395 if (!is_read_io(fio->op))
396 inc_page_count(fio->sbi, WB_DATA_TYPE(fio->page));
397 return 0;
400 int f2fs_submit_page_write(struct f2fs_io_info *fio)
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;
408 f2fs_bug_on(sbi, is_read_io(fio->op));
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;
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);
424 if (fio->old_blkaddr != NEW_ADDR)
425 verify_block_addr(sbi, fio->old_blkaddr);
426 verify_block_addr(sbi, fio->new_blkaddr);
428 bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
430 /* set submitted = true as a return value */
431 fio->submitted = true;
433 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
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;
447 io->bio = __bio_alloc(sbi, fio->new_blkaddr, fio->io_wbc,
448 BIO_MAX_PAGES, false);
449 io->fio = *fio;
452 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
453 __submit_merged_bio(io);
454 goto alloc_new;
457 if (fio->io_wbc)
458 wbc_account_io(fio->io_wbc, bio_page, PAGE_SIZE);
460 io->last_block_in_bio = fio->new_blkaddr;
461 f2fs_trace_ios(fio, 0);
463 trace_f2fs_submit_page_write(fio->page, fio);
465 if (fio->in_list)
466 goto next;
467 out_fail:
468 up_write(&io->io_rwsem);
469 return err;
472 static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
473 unsigned nr_pages)
475 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
476 struct fscrypt_ctx *ctx = NULL;
477 struct bio *bio;
479 if (f2fs_encrypted_file(inode)) {
480 ctx = fscrypt_get_ctx(inode, GFP_NOFS);
481 if (IS_ERR(ctx))
482 return ERR_CAST(ctx);
484 /* wait the page to be moved by cleaning */
485 f2fs_wait_on_block_writeback(sbi, blkaddr);
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);
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);
499 return bio;
502 /* This can handle encryption stuffs */
503 static int f2fs_submit_page_read(struct inode *inode, struct page *page,
504 block_t blkaddr)
506 struct bio *bio = f2fs_grab_read_bio(inode, blkaddr, 1);
508 if (IS_ERR(bio))
509 return PTR_ERR(bio);
511 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
512 bio_put(bio);
513 return -EFAULT;
515 __submit_bio(F2FS_I_SB(inode), bio, DATA);
516 return 0;
519 static void __set_data_blkaddr(struct dnode_of_data *dn)
521 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
522 __le32 *addr_array;
523 int base = 0;
525 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
526 base = get_extra_isize(dn->inode);
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);
534 * Lock ordering for the change of data block address:
535 * ->data_page
536 * ->node_page
537 * update block addresses in the node page
539 void set_data_blkaddr(struct dnode_of_data *dn)
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;
547 void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
549 dn->data_blkaddr = blkaddr;
550 set_data_blkaddr(dn);
551 f2fs_update_extent_cache(dn);
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)
557 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
558 int err;
560 if (!count)
561 return 0;
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;
568 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
569 dn->ofs_in_node, count);
571 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
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--;
583 if (set_page_dirty(dn->node_page))
584 dn->node_changed = true;
585 return 0;
588 /* Should keep dn->ofs_in_node unchanged */
589 int reserve_new_block(struct dnode_of_data *dn)
591 unsigned int ofs_in_node = dn->ofs_in_node;
592 int ret;
594 ret = reserve_new_blocks(dn, 1);
595 dn->ofs_in_node = ofs_in_node;
596 return ret;
599 int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
601 bool need_put = dn->inode_page ? false : true;
602 int err;
604 err = get_dnode_of_data(dn, index, ALLOC_NODE);
605 if (err)
606 return err;
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;
615 int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
617 struct extent_info ei = {0,0,0};
618 struct inode *inode = dn->inode;
620 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
621 dn->data_blkaddr = ei.blk + index - ei.fofs;
622 return 0;
625 return f2fs_reserve_block(dn, index);
628 struct page *get_read_data_page(struct inode *inode, pgoff_t index,
629 int op_flags, bool for_write)
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;
637 page = f2fs_grab_cache_page(mapping, index, for_write);
638 if (!page)
639 return ERR_PTR(-ENOMEM);
641 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
642 dn.data_blkaddr = ei.blk + index - ei.fofs;
643 goto got_it;
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);
652 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
653 err = -ENOENT;
654 goto put_err;
656 got_it:
657 if (PageUptodate(page)) {
658 unlock_page(page);
659 return page;
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.
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;
676 err = f2fs_submit_page_read(inode, page, dn.data_blkaddr);
677 if (err)
678 goto put_err;
679 return page;
681 put_err:
682 f2fs_put_page(page, 1);
683 return ERR_PTR(err);
686 struct page *find_data_page(struct inode *inode, pgoff_t index)
688 struct address_space *mapping = inode->i_mapping;
689 struct page *page;
691 page = find_get_page(mapping, index);
692 if (page && PageUptodate(page))
693 return page;
694 f2fs_put_page(page, 0);
696 page = get_read_data_page(inode, index, 0, false);
697 if (IS_ERR(page))
698 return page;
700 if (PageUptodate(page))
701 return page;
703 wait_on_page_locked(page);
704 if (unlikely(!PageUptodate(page))) {
705 f2fs_put_page(page, 0);
706 return ERR_PTR(-EIO);
708 return page;
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.
716 struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
717 bool for_write)
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;
726 /* wait for read completion */
727 lock_page(page);
728 if (unlikely(page->mapping != mapping)) {
729 f2fs_put_page(page, 1);
730 goto repeat;
732 if (unlikely(!PageUptodate(page))) {
733 f2fs_put_page(page, 1);
734 return ERR_PTR(-EIO);
736 return page;
740 * Caller ensures that this data page is never allocated.
741 * A new zero-filled data page is allocated in the page cache.
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.
748 struct page *get_new_data_page(struct inode *inode,
749 struct page *ipage, pgoff_t index, bool new_i_size)
751 struct address_space *mapping = inode->i_mapping;
752 struct page *page;
753 struct dnode_of_data dn;
754 int err;
756 page = f2fs_grab_cache_page(mapping, index, true);
757 if (!page) {
759 * before exiting, we should make sure ipage will be released
760 * if any error occur.
762 f2fs_put_page(ipage, 1);
763 return ERR_PTR(-ENOMEM);
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);
772 if (!ipage)
773 f2fs_put_dnode(&dn);
775 if (PageUptodate(page))
776 goto got_it;
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);
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;
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;
798 static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
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;
807 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
808 return -EPERM;
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;
815 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
816 return err;
818 alloc:
819 get_node_info(sbi, dn->nid, &ni);
820 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
822 allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr,
823 &sum, seg_type, NULL, false);
824 set_data_blkaddr(dn);
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;
835 static inline bool __force_buffered_io(struct inode *inode, int rw)
837 return (f2fs_encrypted_file(inode) ||
838 (rw == WRITE && test_opt(F2FS_I_SB(inode), LFS)) ||
839 F2FS_I_SB(inode)->s_ndevs);
842 int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
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;
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;
857 if (is_inode_flag_set(inode, FI_NO_PREALLOC))
858 return 0;
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;
867 map.m_next_pgofs = NULL;
868 map.m_next_extent = NULL;
869 map.m_seg_type = NO_CHECK_TYPE;
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;
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;
883 if (f2fs_has_inline_data(inode))
884 return err;
886 flag = F2FS_GET_BLOCK_PRE_AIO;
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;
895 return err;
898 static inline void __do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
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);
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
922 int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
923 int create, int flag)
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;
937 if (!maxblocks)
938 return 0;
940 map->m_len = 0;
941 map->m_flags = 0;
943 /* it only supports block size == page size */
944 pgofs = (pgoff_t)map->m_lblk;
945 end = pgofs + maxblocks;
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;
956 next_dnode:
957 if (create)
958 __do_map_lock(sbi, flag, true);
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);
975 goto unlock_out;
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);
983 next_block:
984 blkaddr = datablock_addr(dn.inode, dn.node_page, dn.ofs_in_node);
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;
992 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
993 if (blkaddr == NULL_ADDR) {
994 prealloc++;
995 last_ofs_in_node = dn.ofs_in_node;
997 } else {
998 err = __allocate_data_block(&dn,
999 map->m_seg_type);
1000 if (!err)
1001 set_inode_flag(inode, FI_APPEND_WRITE);
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;
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;
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;
1029 if (flag == F2FS_GET_BLOCK_PRE_AIO)
1030 goto skip;
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;
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;
1050 skip:
1051 dn.ofs_in_node++;
1052 pgofs++;
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)) {
1058 dn.ofs_in_node = ofs_in_node;
1059 err = reserve_new_blocks(&dn, prealloc);
1060 if (err)
1061 goto sync_out;
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;
1068 dn.ofs_in_node = end_offset;
1071 if (pgofs >= end)
1072 goto sync_out;
1073 else if (dn.ofs_in_node < end_offset)
1074 goto next_block;
1076 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1077 if (map->m_flags & F2FS_MAP_MAPPED) {
1078 unsigned int ofs = start_pgofs - map->m_lblk;
1080 f2fs_update_extent_cache_range(&dn,
1081 start_pgofs, map->m_pblk + ofs,
1082 map->m_len - ofs);
1086 f2fs_put_dnode(&dn);
1088 if (create) {
1089 __do_map_lock(sbi, flag, false);
1090 f2fs_balance_fs(sbi, dn.node_changed);
1092 goto next_dnode;
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;
1099 f2fs_update_extent_cache_range(&dn,
1100 start_pgofs, map->m_pblk + ofs,
1101 map->m_len - ofs);
1103 if (map->m_next_extent)
1104 *map->m_next_extent = pgofs + 1;
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);
1112 out:
1113 trace_f2fs_map_blocks(inode, map, err);
1114 return err;
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)
1121 struct f2fs_map_blocks map;
1122 int err;
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;
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;
1136 return err;
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)
1143 return __get_data_block(inode, iblock, bh_result, create,
1144 flag, next_pgofs,
1145 NO_CHECK_TYPE);
1148 static int get_data_block_dio(struct inode *inode, sector_t iblock,
1149 struct buffer_head *bh_result, int create)
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));
1157 static int get_data_block_bmap(struct inode *inode, sector_t iblock,
1158 struct buffer_head *bh_result, int create)
1160 /* Block number less than F2FS MAX BLOCKS */
1161 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
1162 return -EFBIG;
1164 return __get_data_block(inode, iblock, bh_result, create,
1165 F2FS_GET_BLOCK_BMAP, NULL,
1166 NO_CHECK_TYPE);
1169 static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
1171 return (offset >> inode->i_blkbits);
1174 static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
1176 return (blk << inode->i_blkbits);
1179 static int f2fs_xattr_fiemap(struct inode *inode,
1180 struct fiemap_extent_info *fieinfo)
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;
1190 if (f2fs_has_inline_xattr(inode)) {
1191 int offset;
1193 page = f2fs_grab_cache_page(NODE_MAPPING(sbi),
1194 inode->i_ino, false);
1195 if (!page)
1196 return -ENOMEM;
1198 get_node_info(sbi, inode->i_ino, &ni);
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));
1205 phys += offset;
1206 len = inline_xattr_size(inode);
1208 f2fs_put_page(page, 1);
1210 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED;
1212 if (!xnid)
1213 flags |= FIEMAP_EXTENT_LAST;
1215 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1216 if (err || err == 1)
1217 return err;
1220 if (xnid) {
1221 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false);
1222 if (!page)
1223 return -ENOMEM;
1225 get_node_info(sbi, xnid, &ni);
1227 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1228 len = inode->i_sb->s_blocksize;
1230 f2fs_put_page(page, 1);
1232 flags = FIEMAP_EXTENT_LAST;
1235 if (phys)
1236 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1238 return (err < 0 ? err : 0);
1241 int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1242 u64 start, u64 len)
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;
1251 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
1252 ret = f2fs_precache_extents(inode);
1253 if (ret)
1254 return ret;
1257 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR);
1258 if (ret)
1259 return ret;
1261 inode_lock(inode);
1263 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1264 ret = f2fs_xattr_fiemap(inode, fieinfo);
1265 goto out;
1268 if (f2fs_has_inline_data(inode)) {
1269 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1270 if (ret != -EAGAIN)
1271 goto out;
1274 if (logical_to_blk(inode, len) == 0)
1275 len = blk_to_logical(inode, 1);
1277 start_blk = logical_to_blk(inode, start);
1278 last_blk = logical_to_blk(inode, start + len - 1);
1280 next:
1281 memset(&map_bh, 0, sizeof(struct buffer_head));
1282 map_bh.b_size = len;
1284 ret = get_data_block(inode, start_blk, &map_bh, 0,
1285 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
1286 if (ret)
1287 goto out;
1289 /* HOLE */
1290 if (!buffer_mapped(&map_bh)) {
1291 start_blk = next_pgofs;
1293 if (blk_to_logical(inode, start_blk) < blk_to_logical(inode,
1294 F2FS_I_SB(inode)->max_file_blocks))
1295 goto prep_next;
1297 flags |= FIEMAP_EXTENT_LAST;
1300 if (size) {
1301 if (f2fs_encrypted_inode(inode))
1302 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1304 ret = fiemap_fill_next_extent(fieinfo, logical,
1305 phys, size, flags);
1308 if (start_blk > last_blk || ret)
1309 goto out;
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;
1318 start_blk += logical_to_blk(inode, size);
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;
1330 inode_unlock(inode);
1331 return ret;
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.
1338 static int f2fs_mpage_readpages(struct address_space *mapping,
1339 struct list_head *pages, struct page *page,
1340 unsigned nr_pages)
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;
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;
1361 for (; nr_pages; nr_pages--) {
1362 if (pages) {
1363 page = list_last_entry(pages, struct page, lru);
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;
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;
1381 * Map blocks using the previous result first.
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;
1389 * Then do more f2fs_map_blocks() calls until we are
1390 * done with this page.
1392 map.m_flags = 0;
1394 if (block_in_file < last_block) {
1395 map.m_lblk = block_in_file;
1396 map.m_len = last_block - block_in_file;
1398 if (f2fs_map_blocks(inode, &map, 0,
1399 F2FS_GET_BLOCK_DEFAULT))
1400 goto set_error_page;
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);
1407 if (!PageUptodate(page) && !cleancache_get_page(page)) {
1408 SetPageUptodate(page);
1409 goto confused;
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;
1420 * This page will go to BIO. Do we need to send this
1421 * BIO off first?
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;
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;
1437 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1438 goto submit_and_realloc;
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;
1452 unlock_page(page);
1453 next_page:
1454 if (pages)
1455 put_page(page);
1457 BUG_ON(pages && !list_empty(pages));
1458 if (bio)
1459 __submit_bio(F2FS_I_SB(inode), bio, DATA);
1460 return 0;
1463 static int f2fs_read_data_page(struct file *file, struct page *page)
1465 struct inode *inode = page->mapping->host;
1466 int ret = -EAGAIN;
1468 trace_f2fs_readpage(page, DATA);
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;
1478 static int f2fs_read_data_pages(struct file *file,
1479 struct address_space *mapping,
1480 struct list_head *pages, unsigned nr_pages)
1482 struct inode *inode = mapping->host;
1483 struct page *page = list_last_entry(pages, struct page, lru);
1485 trace_f2fs_readpages(inode, page, nr_pages);
1487 /* If the file has inline data, skip readpages */
1488 if (f2fs_has_inline_data(inode))
1489 return 0;
1491 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages);
1494 static int encrypt_one_page(struct f2fs_io_info *fio)
1496 struct inode *inode = fio->page->mapping->host;
1497 gfp_t gfp_flags = GFP_NOFS;
1499 if (!f2fs_encrypted_file(inode))
1500 return 0;
1502 /* wait for GCed encrypted page writeback */
1503 f2fs_wait_on_block_writeback(fio->sbi, fio->old_blkaddr);
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;
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;
1518 return PTR_ERR(fio->encrypted_page);
1521 static inline bool check_inplace_update_policy(struct inode *inode,
1522 struct f2fs_io_info *fio)
1524 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1525 unsigned int policy = SM_I(sbi)->ipu_policy;
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;
1539 * IPU for rewrite async pages
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;
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;
1552 return false;
1555 bool should_update_inplace(struct inode *inode, struct f2fs_io_info *fio)
1557 if (f2fs_is_pinned_file(inode))
1558 return true;
1560 /* if this is cold file, we should overwrite to avoid fragmentation */
1561 if (file_is_cold(inode))
1562 return true;
1564 return check_inplace_update_policy(inode, fio);
1567 bool should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
1569 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
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;
1583 return false;
1586 static inline bool need_inplace_update(struct f2fs_io_info *fio)
1588 struct inode *inode = fio->page->mapping->host;
1590 if (should_update_outplace(inode, fio))
1591 return false;
1593 return should_update_inplace(inode, fio);
1596 static inline bool valid_ipu_blkaddr(struct f2fs_io_info *fio)
1598 if (fio->old_blkaddr == NEW_ADDR)
1599 return false;
1600 if (fio->old_blkaddr == NULL_ADDR)
1601 return false;
1602 return true;
1605 int do_write_data_page(struct f2fs_io_info *fio)
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;
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;
1619 if (valid_ipu_blkaddr(fio)) {
1620 ipu_force = true;
1621 fio->need_lock = LOCK_DONE;
1622 goto got_it;
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;
1630 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
1631 if (err)
1632 goto out;
1634 fio->old_blkaddr = dn.data_blkaddr;
1636 /* This page is already truncated */
1637 if (fio->old_blkaddr == NULL_ADDR) {
1638 ClearPageUptodate(page);
1639 goto out_writepage;
1641 got_it:
1643 * If current allocation needs SSR,
1644 * it had better in-place writes for updated data.
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;
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;
1661 if (fio->need_lock == LOCK_RETRY) {
1662 if (!f2fs_trylock_op(fio->sbi)) {
1663 err = -EAGAIN;
1664 goto out_writepage;
1666 fio->need_lock = LOCK_REQ;
1669 err = encrypt_one_page(fio);
1670 if (err)
1671 goto out_writepage;
1673 set_page_writeback(page);
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;
1689 static int __write_data_page(struct page *page, bool *submitted,
1690 struct writeback_control *wbc,
1691 enum iostat_type io_type)
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,
1717 trace_f2fs_writepage(page, DATA);
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;
1725 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1726 goto redirty_out;
1728 if (page->index < end_index)
1729 goto write;
1732 * If the offset is out-of-range of file size,
1733 * this page does not have to be written to disk.
1735 offset = i_size & (PAGE_SIZE - 1);
1736 if ((page->index >= end_index + 1) || !offset)
1737 goto out;
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;
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;
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);
1763 err = -EAGAIN;
1764 if (f2fs_has_inline_data(inode)) {
1765 err = f2fs_write_inline_data(inode, page);
1766 if (!err)
1767 goto out;
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);
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);
1787 done:
1788 if (err && err != -ENOENT)
1789 goto redirty_out;
1791 out:
1792 inode_dec_dirty_pages(inode);
1793 if (err)
1794 ClearPageUptodate(page);
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;
1803 unlock_page(page);
1804 if (!S_ISDIR(inode->i_mode))
1805 f2fs_balance_fs(sbi, need_balance_fs);
1807 if (unlikely(f2fs_cp_error(sbi))) {
1808 f2fs_submit_merged_write(sbi, DATA);
1809 submitted = NULL;
1812 if (submitted)
1813 *submitted = fio.submitted;
1815 return 0;
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;
1825 static int f2fs_write_data_page(struct page *page,
1826 struct writeback_control *wbc)
1828 return __write_data_page(page, NULL, wbc, FS_DATA_IO);
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.
1836 static int f2fs_write_cache_pages(struct address_space *mapping,
1837 struct writeback_control *wbc,
1838 enum iostat_type io_type)
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;
1853 pagevec_init(&pvec);
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);
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 */
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;
1887 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
1888 tag);
1889 if (nr_pages == 0)
1890 break;
1892 for (i = 0; i < nr_pages; i++) {
1893 struct page *page = pvec.pages[i];
1894 bool submitted = false;
1896 done_index = page->index;
1897 retry_write:
1898 lock_page(page);
1900 if (unlikely(page->mapping != mapping)) {
1901 continue_unlock:
1902 unlock_page(page);
1903 continue;
1906 if (!PageDirty(page)) {
1907 /* someone wrote it for us */
1908 goto continue_unlock;
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;
1919 BUG_ON(PageWriteback(page));
1920 if (!clear_page_dirty_for_io(page))
1921 goto continue_unlock;
1923 ret = __write_data_page(page, &submitted, wbc, io_type);
1924 if (unlikely(ret)) {
1926 * keep nr_to_write, since vfs uses this to
1927 * get # of written pages.
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;
1941 continue;
1943 done_index = page->index + 1;
1944 done = 1;
1945 break;
1946 } else if (submitted) {
1947 last_idx = page->index;
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;
1958 pagevec_release(&pvec);
1959 cond_resched();
1962 if (!cycled && !done) {
1963 cycled = 1;
1964 index = 0;
1965 end = writeback_index - 1;
1966 goto retry;
1968 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1969 mapping->writeback_index = done_index;
1971 if (last_idx != ULONG_MAX)
1972 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
1973 0, last_idx, DATA);
1975 return ret;
1978 int __f2fs_write_data_pages(struct address_space *mapping,
1979 struct writeback_control *wbc,
1980 enum iostat_type io_type)
1982 struct inode *inode = mapping->host;
1983 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1984 struct blk_plug plug;
1985 int ret;
1987 /* deal with chardevs and other special file */
1988 if (!mapping->a_ops->writepage)
1989 return 0;
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;
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;
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;
2004 /* skip writing during file defragment */
2005 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
2006 goto skip_write;
2008 trace_f2fs_writepages(mapping->host, wbc, DATA);
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;
2016 blk_start_plug(&plug);
2017 ret = f2fs_write_cache_pages(mapping, wbc, io_type);
2018 blk_finish_plug(&plug);
2020 if (wbc->sync_mode == WB_SYNC_ALL)
2021 atomic_dec(&sbi->wb_sync_req);
2023 * if some pages were truncated, we cannot guarantee its mapping->host
2024 * to detect pending bios.
2027 remove_dirty_inode(inode);
2028 return ret;
2030 skip_write:
2031 wbc->pages_skipped += get_dirty_pages(inode);
2032 trace_f2fs_writepages(mapping->host, wbc, DATA);
2033 return 0;
2036 static int f2fs_write_data_pages(struct address_space *mapping,
2037 struct writeback_control *wbc)
2039 struct inode *inode = mapping->host;
2041 return __f2fs_write_data_pages(mapping, wbc,
2042 F2FS_I(inode)->cp_task == current ?
2043 FS_CP_DATA_IO : FS_DATA_IO);
2046 static void f2fs_write_failed(struct address_space *mapping, loff_t to)
2048 struct inode *inode = mapping->host;
2049 loff_t i_size = i_size_read(inode);
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);
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)
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;
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.
2075 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
2076 !is_inode_flag_set(inode, FI_NO_PREALLOC))
2077 return 0;
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;
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;
2092 set_new_dnode(&dn, inode, ipage, ipage, 0);
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);
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;
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;
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)
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;
2148 trace_f2fs_write_begin(inode, pos, len, flags);
2150 if (f2fs_is_atomic_file(inode) &&
2151 !available_free_memory(sbi, INMEM_PAGES)) {
2152 err = -ENOMEM;
2153 drop_atomic = true;
2154 goto fail;
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)
2162 if (index != 0) {
2163 err = f2fs_convert_inline_inode(inode);
2164 if (err)
2165 goto fail;
2167 repeat:
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.
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;
2179 *pagep = page;
2181 err = prepare_write_begin(sbi, page, pos, len,
2182 &blkaddr, &need_balance);
2183 if (err)
2184 goto fail;
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;
2197 f2fs_wait_on_page_writeback(page, DATA, false);
2199 /* wait for GCed encrypted page writeback */
2200 if (f2fs_encrypted_file(inode))
2201 f2fs_wait_on_block_writeback(sbi, blkaddr);
2203 if (len == PAGE_SIZE || PageUptodate(page))
2204 return 0;
2206 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode)) {
2207 zero_user_segment(page, len, PAGE_SIZE);
2208 return 0;
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;
2219 lock_page(page);
2220 if (unlikely(page->mapping != mapping)) {
2221 f2fs_put_page(page, 1);
2222 goto repeat;
2224 if (unlikely(!PageUptodate(page))) {
2225 err = -EIO;
2226 goto fail;
2229 return 0;
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;
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)
2244 struct inode *inode = page->mapping->host;
2246 trace_f2fs_write_end(inode, pos, len, copied);
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.
2253 if (!PageUptodate(page)) {
2254 if (unlikely(copied != len))
2255 copied = 0;
2256 else
2257 SetPageUptodate(page);
2259 if (!copied)
2260 goto unlock_out;
2262 set_page_dirty(page);
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;
2272 static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
2273 loff_t offset)
2275 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
2277 if (offset & blocksize_mask)
2278 return -EINVAL;
2280 if (iov_iter_alignment(iter) & blocksize_mask)
2281 return -EINVAL;
2283 return 0;
2286 static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
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;
2295 err = check_direct_IO(inode, iter, offset);
2296 if (err)
2297 return err;
2299 if (__force_buffered_io(inode, rw))
2300 return 0;
2302 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
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]);
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);
2318 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
2320 return err;
2323 void f2fs_invalidate_page(struct page *page, unsigned int offset,
2324 unsigned int length)
2326 struct inode *inode = page->mapping->host;
2327 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2329 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
2330 (offset % PAGE_SIZE || length != PAGE_SIZE))
2331 return;
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);
2344 /* This is atomic written page, keep Private */
2345 if (IS_ATOMIC_WRITTEN_PAGE(page))
2346 return drop_inmem_page(inode, page);
2348 set_page_private(page, 0);
2349 ClearPagePrivate(page);
2352 int f2fs_release_page(struct page *page, gfp_t wait)
2354 /* If this is dirty page, keep PagePrivate */
2355 if (PageDirty(page))
2356 return 0;
2358 /* This is atomic written page, keep Private */
2359 if (IS_ATOMIC_WRITTEN_PAGE(page))
2360 return 0;
2362 set_page_private(page, 0);
2363 ClearPagePrivate(page);
2364 return 1;
2368 * This was copied from __set_page_dirty_buffers which gives higher performance
2369 * in very high speed storages. (e.g., pmem)
2371 void f2fs_set_page_dirty_nobuffers(struct page *page)
2373 struct address_space *mapping = page->mapping;
2374 unsigned long flags;
2376 if (unlikely(!mapping))
2377 return;
2379 spin_lock(&mapping->private_lock);
2380 lock_page_memcg(page);
2381 SetPageDirty(page);
2382 spin_unlock(&mapping->private_lock);
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);
2392 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
2393 return;
2396 static int f2fs_set_data_page_dirty(struct page *page)
2398 struct address_space *mapping = page->mapping;
2399 struct inode *inode = mapping->host;
2401 trace_f2fs_set_page_dirty(page, DATA);
2403 if (!PageUptodate(page))
2404 SetPageUptodate(page);
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;
2412 * Previously, this page has been registered, we just
2413 * return here.
2415 return 0;
2418 if (!PageDirty(page)) {
2419 f2fs_set_page_dirty_nobuffers(page);
2420 update_dirty_page(inode, page);
2421 return 1;
2423 return 0;
2426 static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
2428 struct inode *inode = mapping->host;
2430 if (f2fs_has_inline_data(inode))
2431 return 0;
2433 /* make sure allocating whole blocks */
2434 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
2435 filemap_write_and_wait(mapping);
2437 return generic_block_bmap(mapping, block, get_data_block_bmap);
2440 #ifdef CONFIG_MIGRATION
2441 #include <linux/migrate.h>
2443 int f2fs_migrate_page(struct address_space *mapping,
2444 struct page *newpage, struct page *page, enum migrate_mode mode)
2446 int rc, extra_count;
2447 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
2448 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
2450 BUG_ON(PageWriteback(page));
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;
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.
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;
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;
2481 mutex_unlock(&fi->inmem_lock);
2482 put_page(page);
2483 get_page(newpage);
2486 if (PagePrivate(page))
2487 SetPagePrivate(newpage);
2488 set_page_private(newpage, page_private(page));
2490 if (mode != MIGRATE_SYNC_NO_COPY)
2491 migrate_page_copy(newpage, page);
2492 else
2493 migrate_page_states(newpage, page);
2495 return MIGRATEPAGE_SUCCESS;
2497 #endif
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