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.
12 #include <linux/f2fs_fs.h>
13 #include <linux/buffer_head.h>
14 #include <linux/mpage.h>
15 #include <linux/aio.h>
16 #include <linux/writeback.h>
17 #include <linux/backing-dev.h>
18 #include <linux/blkdev.h>
19 #include <linux/bio.h>
20 #include <linux/prefetch.h>
26 #include <trace/events/f2fs.h>
28 static void f2fs_read_end_io(struct bio *bio, int err)
33 bio_for_each_segment_all(bvec, bio, i) {
34 struct page *page = bvec->bv_page;
37 SetPageUptodate(page);
39 ClearPageUptodate(page);
47 static void f2fs_write_end_io(struct bio *bio, int err)
49 struct f2fs_sb_info *sbi = bio->bi_private;
53 bio_for_each_segment_all(bvec, bio, i) {
54 struct page *page = bvec->bv_page;
58 set_bit(AS_EIO, &page->mapping->flags);
59 f2fs_stop_checkpoint(sbi);
61 end_page_writeback(page);
62 dec_page_count(sbi, F2FS_WRITEBACK);
65 if (!get_pages(sbi, F2FS_WRITEBACK) &&
66 !list_empty(&sbi->cp_wait.task_list))
67 wake_up(&sbi->cp_wait);
73 * Low-level block read/write IO operations.
75 static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr,
76 int npages, bool is_read)
80 /* No failure on bio allocation */
81 bio = bio_alloc(GFP_NOIO, npages);
83 bio->bi_bdev = sbi->sb->s_bdev;
84 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
85 bio->bi_end_io = is_read ? f2fs_read_end_io : f2fs_write_end_io;
86 bio->bi_private = sbi;
91 static void __submit_merged_bio(struct f2fs_bio_info *io)
93 struct f2fs_io_info *fio = &io->fio;
98 if (is_read_io(fio->rw))
99 trace_f2fs_submit_read_bio(io->sbi->sb, fio, io->bio);
101 trace_f2fs_submit_write_bio(io->sbi->sb, fio, io->bio);
103 submit_bio(fio->rw, io->bio);
107 void f2fs_submit_merged_bio(struct f2fs_sb_info *sbi,
108 enum page_type type, int rw)
110 enum page_type btype = PAGE_TYPE_OF_BIO(type);
111 struct f2fs_bio_info *io;
113 io = is_read_io(rw) ? &sbi->read_io : &sbi->write_io[btype];
115 down_write(&io->io_rwsem);
117 /* change META to META_FLUSH in the checkpoint procedure */
118 if (type >= META_FLUSH) {
119 io->fio.type = META_FLUSH;
120 if (test_opt(sbi, NOBARRIER))
121 io->fio.rw = WRITE_FLUSH | REQ_META | REQ_PRIO;
123 io->fio.rw = WRITE_FLUSH_FUA | REQ_META | REQ_PRIO;
125 __submit_merged_bio(io);
126 up_write(&io->io_rwsem);
130 * Fill the locked page with data located in the block address.
131 * Return unlocked page.
133 int f2fs_submit_page_bio(struct f2fs_sb_info *sbi, struct page *page,
134 struct f2fs_io_info *fio)
138 trace_f2fs_submit_page_bio(page, fio);
139 f2fs_trace_ios(page, fio, 0);
141 /* Allocate a new bio */
142 bio = __bio_alloc(sbi, fio->blk_addr, 1, is_read_io(fio->rw));
144 if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) {
146 f2fs_put_page(page, 1);
150 submit_bio(fio->rw, bio);
154 void f2fs_submit_page_mbio(struct f2fs_sb_info *sbi, struct page *page,
155 struct f2fs_io_info *fio)
157 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
158 struct f2fs_bio_info *io;
159 bool is_read = is_read_io(fio->rw);
161 io = is_read ? &sbi->read_io : &sbi->write_io[btype];
163 verify_block_addr(sbi, fio->blk_addr);
165 down_write(&io->io_rwsem);
168 inc_page_count(sbi, F2FS_WRITEBACK);
170 if (io->bio && (io->last_block_in_bio != fio->blk_addr - 1 ||
171 io->fio.rw != fio->rw))
172 __submit_merged_bio(io);
174 if (io->bio == NULL) {
175 int bio_blocks = MAX_BIO_BLOCKS(sbi);
177 io->bio = __bio_alloc(sbi, fio->blk_addr, bio_blocks, is_read);
181 if (bio_add_page(io->bio, page, PAGE_CACHE_SIZE, 0) <
183 __submit_merged_bio(io);
187 io->last_block_in_bio = fio->blk_addr;
188 f2fs_trace_ios(page, fio, 0);
190 up_write(&io->io_rwsem);
191 trace_f2fs_submit_page_mbio(page, fio);
195 * Lock ordering for the change of data block address:
198 * update block addresses in the node page
200 static void __set_data_blkaddr(struct dnode_of_data *dn)
202 struct f2fs_node *rn;
204 struct page *node_page = dn->node_page;
205 unsigned int ofs_in_node = dn->ofs_in_node;
207 f2fs_wait_on_page_writeback(node_page, NODE);
209 rn = F2FS_NODE(node_page);
211 /* Get physical address of data block */
212 addr_array = blkaddr_in_node(rn);
213 addr_array[ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
214 set_page_dirty(node_page);
217 int reserve_new_block(struct dnode_of_data *dn)
219 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
221 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
223 if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
226 trace_f2fs_reserve_new_block(dn->inode, dn->nid, dn->ofs_in_node);
228 dn->data_blkaddr = NEW_ADDR;
229 __set_data_blkaddr(dn);
230 mark_inode_dirty(dn->inode);
235 int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
237 bool need_put = dn->inode_page ? false : true;
240 err = get_dnode_of_data(dn, index, ALLOC_NODE);
244 if (dn->data_blkaddr == NULL_ADDR)
245 err = reserve_new_block(dn);
251 static void f2fs_map_bh(struct super_block *sb, pgoff_t pgofs,
252 struct extent_info *ei, struct buffer_head *bh_result)
254 unsigned int blkbits = sb->s_blocksize_bits;
257 set_buffer_new(bh_result);
258 map_bh(bh_result, sb, ei->blk + pgofs - ei->fofs);
259 count = ei->fofs + ei->len - pgofs;
260 if (count < (UINT_MAX >> blkbits))
261 bh_result->b_size = (count << blkbits);
263 bh_result->b_size = UINT_MAX;
266 static bool lookup_extent_info(struct inode *inode, pgoff_t pgofs,
267 struct extent_info *ei)
269 struct f2fs_inode_info *fi = F2FS_I(inode);
270 pgoff_t start_fofs, end_fofs;
271 block_t start_blkaddr;
273 if (is_inode_flag_set(fi, FI_NO_EXTENT))
276 read_lock(&fi->ext_lock);
277 if (fi->ext.len == 0) {
278 read_unlock(&fi->ext_lock);
282 stat_inc_total_hit(inode->i_sb);
284 start_fofs = fi->ext.fofs;
285 end_fofs = fi->ext.fofs + fi->ext.len - 1;
286 start_blkaddr = fi->ext.blk;
288 if (pgofs >= start_fofs && pgofs <= end_fofs) {
290 stat_inc_read_hit(inode->i_sb);
291 read_unlock(&fi->ext_lock);
294 read_unlock(&fi->ext_lock);
298 static bool update_extent_info(struct inode *inode, pgoff_t fofs,
301 struct f2fs_inode_info *fi = F2FS_I(inode);
302 pgoff_t start_fofs, end_fofs;
303 block_t start_blkaddr, end_blkaddr;
304 int need_update = true;
306 if (is_inode_flag_set(fi, FI_NO_EXTENT))
309 write_lock(&fi->ext_lock);
311 start_fofs = fi->ext.fofs;
312 end_fofs = fi->ext.fofs + fi->ext.len - 1;
313 start_blkaddr = fi->ext.blk;
314 end_blkaddr = fi->ext.blk + fi->ext.len - 1;
316 /* Drop and initialize the matched extent */
317 if (fi->ext.len == 1 && fofs == start_fofs)
321 if (fi->ext.len == 0) {
322 if (blkaddr != NULL_ADDR) {
324 fi->ext.blk = blkaddr;
331 if (fofs == start_fofs - 1 && blkaddr == start_blkaddr - 1) {
339 if (fofs == end_fofs + 1 && blkaddr == end_blkaddr + 1) {
344 /* Split the existing extent */
345 if (fi->ext.len > 1 &&
346 fofs >= start_fofs && fofs <= end_fofs) {
347 if ((end_fofs - fofs) < (fi->ext.len >> 1)) {
348 fi->ext.len = fofs - start_fofs;
350 fi->ext.fofs = fofs + 1;
351 fi->ext.blk = start_blkaddr + fofs - start_fofs + 1;
352 fi->ext.len -= fofs - start_fofs + 1;
358 /* Finally, if the extent is very fragmented, let's drop the cache. */
359 if (fi->ext.len < F2FS_MIN_EXTENT_LEN) {
361 set_inode_flag(fi, FI_NO_EXTENT);
365 write_unlock(&fi->ext_lock);
369 static bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
370 struct extent_info *ei)
372 return lookup_extent_info(inode, pgofs, ei);
375 void f2fs_update_extent_cache(struct dnode_of_data *dn)
377 struct f2fs_inode_info *fi = F2FS_I(dn->inode);
380 f2fs_bug_on(F2FS_I_SB(dn->inode), dn->data_blkaddr == NEW_ADDR);
382 /* Update the page address in the parent node */
383 __set_data_blkaddr(dn);
385 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) +
388 if (update_extent_info(dn->inode, fofs, dn->data_blkaddr))
392 struct page *find_data_page(struct inode *inode, pgoff_t index, bool sync)
394 struct address_space *mapping = inode->i_mapping;
395 struct dnode_of_data dn;
398 struct f2fs_io_info fio = {
400 .rw = sync ? READ_SYNC : READA,
403 page = find_get_page(mapping, index);
404 if (page && PageUptodate(page))
406 f2fs_put_page(page, 0);
408 set_new_dnode(&dn, inode, NULL, NULL, 0);
409 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
414 if (dn.data_blkaddr == NULL_ADDR)
415 return ERR_PTR(-ENOENT);
417 /* By fallocate(), there is no cached page, but with NEW_ADDR */
418 if (unlikely(dn.data_blkaddr == NEW_ADDR))
419 return ERR_PTR(-EINVAL);
421 page = grab_cache_page(mapping, index);
423 return ERR_PTR(-ENOMEM);
425 if (PageUptodate(page)) {
430 fio.blk_addr = dn.data_blkaddr;
431 err = f2fs_submit_page_bio(F2FS_I_SB(inode), page, &fio);
436 wait_on_page_locked(page);
437 if (unlikely(!PageUptodate(page))) {
438 f2fs_put_page(page, 0);
439 return ERR_PTR(-EIO);
446 * If it tries to access a hole, return an error.
447 * Because, the callers, functions in dir.c and GC, should be able to know
448 * whether this page exists or not.
450 struct page *get_lock_data_page(struct inode *inode, pgoff_t index)
452 struct address_space *mapping = inode->i_mapping;
453 struct dnode_of_data dn;
456 struct f2fs_io_info fio = {
461 page = grab_cache_page(mapping, index);
463 return ERR_PTR(-ENOMEM);
465 set_new_dnode(&dn, inode, NULL, NULL, 0);
466 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
468 f2fs_put_page(page, 1);
473 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
474 f2fs_put_page(page, 1);
475 return ERR_PTR(-ENOENT);
478 if (PageUptodate(page))
482 * A new dentry page is allocated but not able to be written, since its
483 * new inode page couldn't be allocated due to -ENOSPC.
484 * In such the case, its blkaddr can be remained as NEW_ADDR.
485 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
487 if (dn.data_blkaddr == NEW_ADDR) {
488 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
489 SetPageUptodate(page);
493 fio.blk_addr = dn.data_blkaddr;
494 err = f2fs_submit_page_bio(F2FS_I_SB(inode), page, &fio);
499 if (unlikely(!PageUptodate(page))) {
500 f2fs_put_page(page, 1);
501 return ERR_PTR(-EIO);
503 if (unlikely(page->mapping != mapping)) {
504 f2fs_put_page(page, 1);
511 * Caller ensures that this data page is never allocated.
512 * A new zero-filled data page is allocated in the page cache.
514 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
516 * Note that, ipage is set only by make_empty_dir.
518 struct page *get_new_data_page(struct inode *inode,
519 struct page *ipage, pgoff_t index, bool new_i_size)
521 struct address_space *mapping = inode->i_mapping;
523 struct dnode_of_data dn;
526 set_new_dnode(&dn, inode, ipage, NULL, 0);
527 err = f2fs_reserve_block(&dn, index);
531 page = grab_cache_page(mapping, index);
537 if (PageUptodate(page))
540 if (dn.data_blkaddr == NEW_ADDR) {
541 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
542 SetPageUptodate(page);
544 struct f2fs_io_info fio = {
547 .blk_addr = dn.data_blkaddr,
549 err = f2fs_submit_page_bio(F2FS_I_SB(inode), page, &fio);
554 if (unlikely(!PageUptodate(page))) {
555 f2fs_put_page(page, 1);
559 if (unlikely(page->mapping != mapping)) {
560 f2fs_put_page(page, 1);
566 i_size_read(inode) < ((index + 1) << PAGE_CACHE_SHIFT)) {
567 i_size_write(inode, ((index + 1) << PAGE_CACHE_SHIFT));
568 /* Only the directory inode sets new_i_size */
569 set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR);
578 static int __allocate_data_block(struct dnode_of_data *dn)
580 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
581 struct f2fs_inode_info *fi = F2FS_I(dn->inode);
582 struct f2fs_summary sum;
584 int seg = CURSEG_WARM_DATA;
587 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
589 if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
592 get_node_info(sbi, dn->nid, &ni);
593 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
595 if (dn->ofs_in_node == 0 && dn->inode_page == dn->node_page)
596 seg = CURSEG_DIRECT_IO;
598 allocate_data_block(sbi, NULL, NULL_ADDR, &dn->data_blkaddr, &sum, seg);
600 /* direct IO doesn't use extent cache to maximize the performance */
601 __set_data_blkaddr(dn);
604 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) +
606 if (i_size_read(dn->inode) < ((fofs + 1) << PAGE_CACHE_SHIFT))
607 i_size_write(dn->inode, ((fofs + 1) << PAGE_CACHE_SHIFT));
612 static void __allocate_data_blocks(struct inode *inode, loff_t offset,
615 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
616 struct dnode_of_data dn;
617 u64 start = F2FS_BYTES_TO_BLK(offset);
618 u64 len = F2FS_BYTES_TO_BLK(count);
623 f2fs_balance_fs(sbi);
626 /* When reading holes, we need its node page */
627 set_new_dnode(&dn, inode, NULL, NULL, 0);
628 if (get_dnode_of_data(&dn, start, ALLOC_NODE))
632 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
634 while (dn.ofs_in_node < end_offset && len) {
635 if (dn.data_blkaddr == NULL_ADDR) {
636 if (__allocate_data_block(&dn))
646 sync_inode_page(&dn);
655 sync_inode_page(&dn);
663 * get_data_block() now supported readahead/bmap/rw direct_IO with mapped bh.
664 * If original data blocks are allocated, then give them to blockdev.
666 * a. preallocate requested block addresses
667 * b. do not use extent cache for better performance
668 * c. give the block addresses to blockdev
670 static int __get_data_block(struct inode *inode, sector_t iblock,
671 struct buffer_head *bh_result, int create, bool fiemap)
673 unsigned int blkbits = inode->i_sb->s_blocksize_bits;
674 unsigned maxblocks = bh_result->b_size >> blkbits;
675 struct dnode_of_data dn;
676 int mode = create ? ALLOC_NODE : LOOKUP_NODE_RA;
677 pgoff_t pgofs, end_offset;
678 int err = 0, ofs = 1;
679 struct extent_info ei;
680 bool allocated = false;
682 /* Get the page offset from the block offset(iblock) */
683 pgofs = (pgoff_t)(iblock >> (PAGE_CACHE_SHIFT - blkbits));
685 if (f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
686 f2fs_map_bh(inode->i_sb, pgofs, &ei, bh_result);
691 f2fs_lock_op(F2FS_I_SB(inode));
693 /* When reading holes, we need its node page */
694 set_new_dnode(&dn, inode, NULL, NULL, 0);
695 err = get_dnode_of_data(&dn, pgofs, mode);
701 if (dn.data_blkaddr == NEW_ADDR && !fiemap)
704 if (dn.data_blkaddr != NULL_ADDR) {
705 set_buffer_new(bh_result);
706 map_bh(bh_result, inode->i_sb, dn.data_blkaddr);
708 err = __allocate_data_block(&dn);
712 set_buffer_new(bh_result);
713 map_bh(bh_result, inode->i_sb, dn.data_blkaddr);
718 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
719 bh_result->b_size = (((size_t)1) << blkbits);
724 if (dn.ofs_in_node >= end_offset) {
726 sync_inode_page(&dn);
730 set_new_dnode(&dn, inode, NULL, NULL, 0);
731 err = get_dnode_of_data(&dn, pgofs, mode);
737 if (dn.data_blkaddr == NEW_ADDR && !fiemap)
740 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
743 if (maxblocks > (bh_result->b_size >> blkbits)) {
744 block_t blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
745 if (blkaddr == NULL_ADDR && create) {
746 err = __allocate_data_block(&dn);
750 blkaddr = dn.data_blkaddr;
752 /* Give more consecutive addresses for the readahead */
753 if (blkaddr == (bh_result->b_blocknr + ofs)) {
757 bh_result->b_size += (((size_t)1) << blkbits);
763 sync_inode_page(&dn);
768 f2fs_unlock_op(F2FS_I_SB(inode));
770 trace_f2fs_get_data_block(inode, iblock, bh_result, err);
774 static int get_data_block(struct inode *inode, sector_t iblock,
775 struct buffer_head *bh_result, int create)
777 return __get_data_block(inode, iblock, bh_result, create, false);
780 static int get_data_block_fiemap(struct inode *inode, sector_t iblock,
781 struct buffer_head *bh_result, int create)
783 return __get_data_block(inode, iblock, bh_result, create, true);
786 int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
789 return generic_block_fiemap(inode, fieinfo,
790 start, len, get_data_block_fiemap);
793 static int f2fs_read_data_page(struct file *file, struct page *page)
795 struct inode *inode = page->mapping->host;
798 trace_f2fs_readpage(page, DATA);
800 /* If the file has inline data, try to read it directly */
801 if (f2fs_has_inline_data(inode))
802 ret = f2fs_read_inline_data(inode, page);
804 ret = mpage_readpage(page, get_data_block);
809 static int f2fs_read_data_pages(struct file *file,
810 struct address_space *mapping,
811 struct list_head *pages, unsigned nr_pages)
813 struct inode *inode = file->f_mapping->host;
815 /* If the file has inline data, skip readpages */
816 if (f2fs_has_inline_data(inode))
819 return mpage_readpages(mapping, pages, nr_pages, get_data_block);
822 int do_write_data_page(struct page *page, struct f2fs_io_info *fio)
824 struct inode *inode = page->mapping->host;
825 struct dnode_of_data dn;
828 set_new_dnode(&dn, inode, NULL, NULL, 0);
829 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
833 fio->blk_addr = dn.data_blkaddr;
835 /* This page is already truncated */
836 if (fio->blk_addr == NULL_ADDR)
839 set_page_writeback(page);
842 * If current allocation needs SSR,
843 * it had better in-place writes for updated data.
845 if (unlikely(fio->blk_addr != NEW_ADDR &&
846 !is_cold_data(page) &&
847 need_inplace_update(inode))) {
848 rewrite_data_page(page, fio);
849 set_inode_flag(F2FS_I(inode), FI_UPDATE_WRITE);
851 write_data_page(page, &dn, fio);
852 f2fs_update_extent_cache(&dn);
853 set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
860 static int f2fs_write_data_page(struct page *page,
861 struct writeback_control *wbc)
863 struct inode *inode = page->mapping->host;
864 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
865 loff_t i_size = i_size_read(inode);
866 const pgoff_t end_index = ((unsigned long long) i_size)
869 bool need_balance_fs = false;
871 struct f2fs_io_info fio = {
873 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
876 trace_f2fs_writepage(page, DATA);
878 if (page->index < end_index)
882 * If the offset is out-of-range of file size,
883 * this page does not have to be written to disk.
885 offset = i_size & (PAGE_CACHE_SIZE - 1);
886 if ((page->index >= end_index + 1) || !offset)
889 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
891 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
893 if (f2fs_is_drop_cache(inode))
895 if (f2fs_is_volatile_file(inode) && !wbc->for_reclaim &&
896 available_free_memory(sbi, BASE_CHECK))
899 /* Dentry blocks are controlled by checkpoint */
900 if (S_ISDIR(inode->i_mode)) {
901 if (unlikely(f2fs_cp_error(sbi)))
903 err = do_write_data_page(page, &fio);
907 /* we should bypass data pages to proceed the kworkder jobs */
908 if (unlikely(f2fs_cp_error(sbi))) {
913 if (!wbc->for_reclaim)
914 need_balance_fs = true;
915 else if (has_not_enough_free_secs(sbi, 0))
920 if (f2fs_has_inline_data(inode))
921 err = f2fs_write_inline_data(inode, page);
923 err = do_write_data_page(page, &fio);
926 if (err && err != -ENOENT)
929 clear_cold_data(page);
931 inode_dec_dirty_pages(inode);
934 f2fs_balance_fs(sbi);
935 if (wbc->for_reclaim)
936 f2fs_submit_merged_bio(sbi, DATA, WRITE);
940 redirty_page_for_writepage(wbc, page);
941 return AOP_WRITEPAGE_ACTIVATE;
944 static int __f2fs_writepage(struct page *page, struct writeback_control *wbc,
947 struct address_space *mapping = data;
948 int ret = mapping->a_ops->writepage(page, wbc);
949 mapping_set_error(mapping, ret);
953 static int f2fs_write_data_pages(struct address_space *mapping,
954 struct writeback_control *wbc)
956 struct inode *inode = mapping->host;
957 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
962 trace_f2fs_writepages(mapping->host, wbc, DATA);
964 /* deal with chardevs and other special file */
965 if (!mapping->a_ops->writepage)
968 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
969 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
970 available_free_memory(sbi, DIRTY_DENTS))
973 diff = nr_pages_to_write(sbi, DATA, wbc);
975 if (!S_ISDIR(inode->i_mode)) {
976 mutex_lock(&sbi->writepages);
979 ret = write_cache_pages(mapping, wbc, __f2fs_writepage, mapping);
981 mutex_unlock(&sbi->writepages);
983 f2fs_submit_merged_bio(sbi, DATA, WRITE);
985 remove_dirty_dir_inode(inode);
987 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
991 wbc->pages_skipped += get_dirty_pages(inode);
995 static void f2fs_write_failed(struct address_space *mapping, loff_t to)
997 struct inode *inode = mapping->host;
999 if (to > inode->i_size) {
1000 truncate_pagecache(inode, inode->i_size);
1001 truncate_blocks(inode, inode->i_size, true);
1005 static int f2fs_write_begin(struct file *file, struct address_space *mapping,
1006 loff_t pos, unsigned len, unsigned flags,
1007 struct page **pagep, void **fsdata)
1009 struct inode *inode = mapping->host;
1010 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1011 struct page *page, *ipage;
1012 pgoff_t index = ((unsigned long long) pos) >> PAGE_CACHE_SHIFT;
1013 struct dnode_of_data dn;
1016 trace_f2fs_write_begin(inode, pos, len, flags);
1018 f2fs_balance_fs(sbi);
1021 * We should check this at this moment to avoid deadlock on inode page
1022 * and #0 page. The locking rule for inline_data conversion should be:
1023 * lock_page(page #0) -> lock_page(inode_page)
1026 err = f2fs_convert_inline_inode(inode);
1031 page = grab_cache_page_write_begin(mapping, index, flags);
1041 /* check inline_data */
1042 ipage = get_node_page(sbi, inode->i_ino);
1043 if (IS_ERR(ipage)) {
1044 err = PTR_ERR(ipage);
1048 set_new_dnode(&dn, inode, ipage, ipage, 0);
1050 if (f2fs_has_inline_data(inode)) {
1051 if (pos + len <= MAX_INLINE_DATA) {
1052 read_inline_data(page, ipage);
1053 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
1054 sync_inode_page(&dn);
1057 err = f2fs_convert_inline_page(&dn, page);
1061 err = f2fs_reserve_block(&dn, index);
1065 f2fs_put_dnode(&dn);
1066 f2fs_unlock_op(sbi);
1068 if ((len == PAGE_CACHE_SIZE) || PageUptodate(page))
1071 f2fs_wait_on_page_writeback(page, DATA);
1073 if ((pos & PAGE_CACHE_MASK) >= i_size_read(inode)) {
1074 unsigned start = pos & (PAGE_CACHE_SIZE - 1);
1075 unsigned end = start + len;
1077 /* Reading beyond i_size is simple: memset to zero */
1078 zero_user_segments(page, 0, start, end, PAGE_CACHE_SIZE);
1082 if (dn.data_blkaddr == NEW_ADDR) {
1083 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1085 struct f2fs_io_info fio = {
1088 .blk_addr = dn.data_blkaddr,
1090 err = f2fs_submit_page_bio(sbi, page, &fio);
1095 if (unlikely(!PageUptodate(page))) {
1096 f2fs_put_page(page, 1);
1100 if (unlikely(page->mapping != mapping)) {
1101 f2fs_put_page(page, 1);
1106 SetPageUptodate(page);
1107 clear_cold_data(page);
1111 f2fs_put_dnode(&dn);
1113 f2fs_unlock_op(sbi);
1114 f2fs_put_page(page, 1);
1116 f2fs_write_failed(mapping, pos + len);
1120 static int f2fs_write_end(struct file *file,
1121 struct address_space *mapping,
1122 loff_t pos, unsigned len, unsigned copied,
1123 struct page *page, void *fsdata)
1125 struct inode *inode = page->mapping->host;
1127 trace_f2fs_write_end(inode, pos, len, copied);
1129 set_page_dirty(page);
1131 if (pos + copied > i_size_read(inode)) {
1132 i_size_write(inode, pos + copied);
1133 mark_inode_dirty(inode);
1134 update_inode_page(inode);
1137 f2fs_put_page(page, 1);
1141 static int check_direct_IO(struct inode *inode, int rw,
1142 struct iov_iter *iter, loff_t offset)
1144 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
1149 if (offset & blocksize_mask)
1152 if (iov_iter_alignment(iter) & blocksize_mask)
1158 static ssize_t f2fs_direct_IO(int rw, struct kiocb *iocb,
1159 struct iov_iter *iter, loff_t offset)
1161 struct file *file = iocb->ki_filp;
1162 struct address_space *mapping = file->f_mapping;
1163 struct inode *inode = mapping->host;
1164 size_t count = iov_iter_count(iter);
1167 /* we don't need to use inline_data strictly */
1168 if (f2fs_has_inline_data(inode)) {
1169 err = f2fs_convert_inline_inode(inode);
1174 if (check_direct_IO(inode, rw, iter, offset))
1177 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
1180 __allocate_data_blocks(inode, offset, count);
1182 err = blockdev_direct_IO(rw, iocb, inode, iter, offset, get_data_block);
1183 if (err < 0 && (rw & WRITE))
1184 f2fs_write_failed(mapping, offset + count);
1186 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
1191 void f2fs_invalidate_page(struct page *page, unsigned int offset,
1192 unsigned int length)
1194 struct inode *inode = page->mapping->host;
1195 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1197 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
1198 (offset % PAGE_CACHE_SIZE || length != PAGE_CACHE_SIZE))
1201 if (PageDirty(page)) {
1202 if (inode->i_ino == F2FS_META_INO(sbi))
1203 dec_page_count(sbi, F2FS_DIRTY_META);
1204 else if (inode->i_ino == F2FS_NODE_INO(sbi))
1205 dec_page_count(sbi, F2FS_DIRTY_NODES);
1207 inode_dec_dirty_pages(inode);
1209 ClearPagePrivate(page);
1212 int f2fs_release_page(struct page *page, gfp_t wait)
1214 /* If this is dirty page, keep PagePrivate */
1215 if (PageDirty(page))
1218 ClearPagePrivate(page);
1222 static int f2fs_set_data_page_dirty(struct page *page)
1224 struct address_space *mapping = page->mapping;
1225 struct inode *inode = mapping->host;
1227 trace_f2fs_set_page_dirty(page, DATA);
1229 SetPageUptodate(page);
1231 if (f2fs_is_atomic_file(inode)) {
1232 register_inmem_page(inode, page);
1236 mark_inode_dirty(inode);
1238 if (!PageDirty(page)) {
1239 __set_page_dirty_nobuffers(page);
1240 update_dirty_page(inode, page);
1246 static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
1248 struct inode *inode = mapping->host;
1250 /* we don't need to use inline_data strictly */
1251 if (f2fs_has_inline_data(inode)) {
1252 int err = f2fs_convert_inline_inode(inode);
1256 return generic_block_bmap(mapping, block, get_data_block);
1259 const struct address_space_operations f2fs_dblock_aops = {
1260 .readpage = f2fs_read_data_page,
1261 .readpages = f2fs_read_data_pages,
1262 .writepage = f2fs_write_data_page,
1263 .writepages = f2fs_write_data_pages,
1264 .write_begin = f2fs_write_begin,
1265 .write_end = f2fs_write_end,
1266 .set_page_dirty = f2fs_set_data_page_dirty,
1267 .invalidatepage = f2fs_invalidate_page,
1268 .releasepage = f2fs_release_page,
1269 .direct_IO = f2fs_direct_IO,