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>
25 #include <trace/events/f2fs.h>
27 static void f2fs_read_end_io(struct bio
*bio
, int err
)
32 bio_for_each_segment_all(bvec
, bio
, i
) {
33 struct page
*page
= bvec
->bv_page
;
36 SetPageUptodate(page
);
38 ClearPageUptodate(page
);
46 static void f2fs_write_end_io(struct bio
*bio
, int err
)
48 struct f2fs_sb_info
*sbi
= F2FS_SB(bio
->bi_io_vec
->bv_page
->mapping
->host
->i_sb
);
52 bio_for_each_segment_all(bvec
, bio
, i
) {
53 struct page
*page
= bvec
->bv_page
;
57 set_bit(AS_EIO
, &page
->mapping
->flags
);
58 set_ckpt_flags(sbi
->ckpt
, CP_ERROR_FLAG
);
59 sbi
->sb
->s_flags
|= MS_RDONLY
;
61 end_page_writeback(page
);
62 dec_page_count(sbi
, F2FS_WRITEBACK
);
66 complete(bio
->bi_private
);
68 if (!get_pages(sbi
, F2FS_WRITEBACK
) &&
69 !list_empty(&sbi
->cp_wait
.task_list
))
70 wake_up(&sbi
->cp_wait
);
76 * Low-level block read/write IO operations.
78 static struct bio
*__bio_alloc(struct f2fs_sb_info
*sbi
, block_t blk_addr
,
79 int npages
, bool is_read
)
83 /* No failure on bio allocation */
84 bio
= bio_alloc(GFP_NOIO
, npages
);
86 bio
->bi_bdev
= sbi
->sb
->s_bdev
;
87 bio
->bi_iter
.bi_sector
= SECTOR_FROM_BLOCK(sbi
, blk_addr
);
88 bio
->bi_end_io
= is_read
? f2fs_read_end_io
: f2fs_write_end_io
;
93 static void __submit_merged_bio(struct f2fs_bio_info
*io
)
95 struct f2fs_io_info
*fio
= &io
->fio
;
103 if (is_read_io(rw
)) {
104 trace_f2fs_submit_read_bio(io
->sbi
->sb
, rw
,
106 submit_bio(rw
, io
->bio
);
108 trace_f2fs_submit_write_bio(io
->sbi
->sb
, rw
,
111 * META_FLUSH is only from the checkpoint procedure, and we
112 * should wait this metadata bio for FS consistency.
114 if (fio
->type
== META_FLUSH
) {
115 DECLARE_COMPLETION_ONSTACK(wait
);
116 io
->bio
->bi_private
= &wait
;
117 submit_bio(rw
, io
->bio
);
118 wait_for_completion(&wait
);
120 submit_bio(rw
, io
->bio
);
127 void f2fs_submit_merged_bio(struct f2fs_sb_info
*sbi
,
128 enum page_type type
, int rw
)
130 enum page_type btype
= PAGE_TYPE_OF_BIO(type
);
131 struct f2fs_bio_info
*io
;
133 io
= is_read_io(rw
) ? &sbi
->read_io
: &sbi
->write_io
[btype
];
135 mutex_lock(&io
->io_mutex
);
137 /* change META to META_FLUSH in the checkpoint procedure */
138 if (type
>= META_FLUSH
) {
139 io
->fio
.type
= META_FLUSH
;
140 io
->fio
.rw
= WRITE_FLUSH_FUA
| REQ_META
| REQ_PRIO
;
142 __submit_merged_bio(io
);
143 mutex_unlock(&io
->io_mutex
);
147 * Fill the locked page with data located in the block address.
148 * Return unlocked page.
150 int f2fs_submit_page_bio(struct f2fs_sb_info
*sbi
, struct page
*page
,
151 block_t blk_addr
, int rw
)
155 trace_f2fs_submit_page_bio(page
, blk_addr
, rw
);
157 /* Allocate a new bio */
158 bio
= __bio_alloc(sbi
, blk_addr
, 1, is_read_io(rw
));
160 if (bio_add_page(bio
, page
, PAGE_CACHE_SIZE
, 0) < PAGE_CACHE_SIZE
) {
162 f2fs_put_page(page
, 1);
170 void f2fs_submit_page_mbio(struct f2fs_sb_info
*sbi
, struct page
*page
,
171 block_t blk_addr
, struct f2fs_io_info
*fio
)
173 enum page_type btype
= PAGE_TYPE_OF_BIO(fio
->type
);
174 struct f2fs_bio_info
*io
;
175 bool is_read
= is_read_io(fio
->rw
);
177 io
= is_read
? &sbi
->read_io
: &sbi
->write_io
[btype
];
179 verify_block_addr(sbi
, blk_addr
);
181 mutex_lock(&io
->io_mutex
);
184 inc_page_count(sbi
, F2FS_WRITEBACK
);
186 if (io
->bio
&& (io
->last_block_in_bio
!= blk_addr
- 1 ||
187 io
->fio
.rw
!= fio
->rw
))
188 __submit_merged_bio(io
);
190 if (io
->bio
== NULL
) {
191 int bio_blocks
= MAX_BIO_BLOCKS(max_hw_blocks(sbi
));
193 io
->bio
= __bio_alloc(sbi
, blk_addr
, bio_blocks
, is_read
);
197 if (bio_add_page(io
->bio
, page
, PAGE_CACHE_SIZE
, 0) <
199 __submit_merged_bio(io
);
203 io
->last_block_in_bio
= blk_addr
;
205 mutex_unlock(&io
->io_mutex
);
206 trace_f2fs_submit_page_mbio(page
, fio
->rw
, fio
->type
, blk_addr
);
210 * Lock ordering for the change of data block address:
213 * update block addresses in the node page
215 static void __set_data_blkaddr(struct dnode_of_data
*dn
, block_t new_addr
)
217 struct f2fs_node
*rn
;
219 struct page
*node_page
= dn
->node_page
;
220 unsigned int ofs_in_node
= dn
->ofs_in_node
;
222 f2fs_wait_on_page_writeback(node_page
, NODE
);
224 rn
= F2FS_NODE(node_page
);
226 /* Get physical address of data block */
227 addr_array
= blkaddr_in_node(rn
);
228 addr_array
[ofs_in_node
] = cpu_to_le32(new_addr
);
229 set_page_dirty(node_page
);
232 int reserve_new_block(struct dnode_of_data
*dn
)
234 struct f2fs_sb_info
*sbi
= F2FS_SB(dn
->inode
->i_sb
);
236 if (unlikely(is_inode_flag_set(F2FS_I(dn
->inode
), FI_NO_ALLOC
)))
238 if (unlikely(!inc_valid_block_count(sbi
, dn
->inode
, 1)))
241 trace_f2fs_reserve_new_block(dn
->inode
, dn
->nid
, dn
->ofs_in_node
);
243 __set_data_blkaddr(dn
, NEW_ADDR
);
244 dn
->data_blkaddr
= NEW_ADDR
;
245 mark_inode_dirty(dn
->inode
);
250 int f2fs_reserve_block(struct dnode_of_data
*dn
, pgoff_t index
)
252 bool need_put
= dn
->inode_page
? false : true;
255 /* if inode_page exists, index should be zero */
256 f2fs_bug_on(!need_put
&& index
);
258 err
= get_dnode_of_data(dn
, index
, ALLOC_NODE
);
262 if (dn
->data_blkaddr
== NULL_ADDR
)
263 err
= reserve_new_block(dn
);
269 static int check_extent_cache(struct inode
*inode
, pgoff_t pgofs
,
270 struct buffer_head
*bh_result
)
272 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
273 pgoff_t start_fofs
, end_fofs
;
274 block_t start_blkaddr
;
276 if (is_inode_flag_set(fi
, FI_NO_EXTENT
))
279 read_lock(&fi
->ext
.ext_lock
);
280 if (fi
->ext
.len
== 0) {
281 read_unlock(&fi
->ext
.ext_lock
);
285 stat_inc_total_hit(inode
->i_sb
);
287 start_fofs
= fi
->ext
.fofs
;
288 end_fofs
= fi
->ext
.fofs
+ fi
->ext
.len
- 1;
289 start_blkaddr
= fi
->ext
.blk_addr
;
291 if (pgofs
>= start_fofs
&& pgofs
<= end_fofs
) {
292 unsigned int blkbits
= inode
->i_sb
->s_blocksize_bits
;
295 clear_buffer_new(bh_result
);
296 map_bh(bh_result
, inode
->i_sb
,
297 start_blkaddr
+ pgofs
- start_fofs
);
298 count
= end_fofs
- pgofs
+ 1;
299 if (count
< (UINT_MAX
>> blkbits
))
300 bh_result
->b_size
= (count
<< blkbits
);
302 bh_result
->b_size
= UINT_MAX
;
304 stat_inc_read_hit(inode
->i_sb
);
305 read_unlock(&fi
->ext
.ext_lock
);
308 read_unlock(&fi
->ext
.ext_lock
);
312 void update_extent_cache(block_t blk_addr
, struct dnode_of_data
*dn
)
314 struct f2fs_inode_info
*fi
= F2FS_I(dn
->inode
);
315 pgoff_t fofs
, start_fofs
, end_fofs
;
316 block_t start_blkaddr
, end_blkaddr
;
317 int need_update
= true;
319 f2fs_bug_on(blk_addr
== NEW_ADDR
);
320 fofs
= start_bidx_of_node(ofs_of_node(dn
->node_page
), fi
) +
323 /* Update the page address in the parent node */
324 __set_data_blkaddr(dn
, blk_addr
);
326 if (is_inode_flag_set(fi
, FI_NO_EXTENT
))
329 write_lock(&fi
->ext
.ext_lock
);
331 start_fofs
= fi
->ext
.fofs
;
332 end_fofs
= fi
->ext
.fofs
+ fi
->ext
.len
- 1;
333 start_blkaddr
= fi
->ext
.blk_addr
;
334 end_blkaddr
= fi
->ext
.blk_addr
+ fi
->ext
.len
- 1;
336 /* Drop and initialize the matched extent */
337 if (fi
->ext
.len
== 1 && fofs
== start_fofs
)
341 if (fi
->ext
.len
== 0) {
342 if (blk_addr
!= NULL_ADDR
) {
344 fi
->ext
.blk_addr
= blk_addr
;
351 if (fofs
== start_fofs
- 1 && blk_addr
== start_blkaddr
- 1) {
359 if (fofs
== end_fofs
+ 1 && blk_addr
== end_blkaddr
+ 1) {
364 /* Split the existing extent */
365 if (fi
->ext
.len
> 1 &&
366 fofs
>= start_fofs
&& fofs
<= end_fofs
) {
367 if ((end_fofs
- fofs
) < (fi
->ext
.len
>> 1)) {
368 fi
->ext
.len
= fofs
- start_fofs
;
370 fi
->ext
.fofs
= fofs
+ 1;
371 fi
->ext
.blk_addr
= start_blkaddr
+
372 fofs
- start_fofs
+ 1;
373 fi
->ext
.len
-= fofs
- start_fofs
+ 1;
379 /* Finally, if the extent is very fragmented, let's drop the cache. */
380 if (fi
->ext
.len
< F2FS_MIN_EXTENT_LEN
) {
382 set_inode_flag(fi
, FI_NO_EXTENT
);
386 write_unlock(&fi
->ext
.ext_lock
);
392 struct page
*find_data_page(struct inode
*inode
, pgoff_t index
, bool sync
)
394 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
395 struct address_space
*mapping
= inode
->i_mapping
;
396 struct dnode_of_data dn
;
400 page
= find_get_page(mapping
, index
);
401 if (page
&& PageUptodate(page
))
403 f2fs_put_page(page
, 0);
405 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
406 err
= get_dnode_of_data(&dn
, index
, LOOKUP_NODE
);
411 if (dn
.data_blkaddr
== NULL_ADDR
)
412 return ERR_PTR(-ENOENT
);
414 /* By fallocate(), there is no cached page, but with NEW_ADDR */
415 if (unlikely(dn
.data_blkaddr
== NEW_ADDR
))
416 return ERR_PTR(-EINVAL
);
418 page
= grab_cache_page_write_begin(mapping
, index
, AOP_FLAG_NOFS
);
420 return ERR_PTR(-ENOMEM
);
422 if (PageUptodate(page
)) {
427 err
= f2fs_submit_page_bio(sbi
, page
, dn
.data_blkaddr
,
428 sync
? READ_SYNC
: READA
);
433 wait_on_page_locked(page
);
434 if (unlikely(!PageUptodate(page
))) {
435 f2fs_put_page(page
, 0);
436 return ERR_PTR(-EIO
);
443 * If it tries to access a hole, return an error.
444 * Because, the callers, functions in dir.c and GC, should be able to know
445 * whether this page exists or not.
447 struct page
*get_lock_data_page(struct inode
*inode
, pgoff_t index
)
449 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
450 struct address_space
*mapping
= inode
->i_mapping
;
451 struct dnode_of_data dn
;
456 page
= grab_cache_page_write_begin(mapping
, index
, AOP_FLAG_NOFS
);
458 return ERR_PTR(-ENOMEM
);
460 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
461 err
= get_dnode_of_data(&dn
, index
, LOOKUP_NODE
);
463 f2fs_put_page(page
, 1);
468 if (unlikely(dn
.data_blkaddr
== NULL_ADDR
)) {
469 f2fs_put_page(page
, 1);
470 return ERR_PTR(-ENOENT
);
473 if (PageUptodate(page
))
477 * A new dentry page is allocated but not able to be written, since its
478 * new inode page couldn't be allocated due to -ENOSPC.
479 * In such the case, its blkaddr can be remained as NEW_ADDR.
480 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
482 if (dn
.data_blkaddr
== NEW_ADDR
) {
483 zero_user_segment(page
, 0, PAGE_CACHE_SIZE
);
484 SetPageUptodate(page
);
488 err
= f2fs_submit_page_bio(sbi
, page
, dn
.data_blkaddr
, READ_SYNC
);
493 if (unlikely(!PageUptodate(page
))) {
494 f2fs_put_page(page
, 1);
495 return ERR_PTR(-EIO
);
497 if (unlikely(page
->mapping
!= mapping
)) {
498 f2fs_put_page(page
, 1);
505 * Caller ensures that this data page is never allocated.
506 * A new zero-filled data page is allocated in the page cache.
508 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
510 * Note that, ipage is set only by make_empty_dir.
512 struct page
*get_new_data_page(struct inode
*inode
,
513 struct page
*ipage
, pgoff_t index
, bool new_i_size
)
515 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
516 struct address_space
*mapping
= inode
->i_mapping
;
518 struct dnode_of_data dn
;
521 set_new_dnode(&dn
, inode
, ipage
, NULL
, 0);
522 err
= f2fs_reserve_block(&dn
, index
);
526 page
= grab_cache_page(mapping
, index
);
532 if (PageUptodate(page
))
535 if (dn
.data_blkaddr
== NEW_ADDR
) {
536 zero_user_segment(page
, 0, PAGE_CACHE_SIZE
);
537 SetPageUptodate(page
);
539 err
= f2fs_submit_page_bio(sbi
, page
, dn
.data_blkaddr
,
545 if (unlikely(!PageUptodate(page
))) {
546 f2fs_put_page(page
, 1);
550 if (unlikely(page
->mapping
!= mapping
)) {
551 f2fs_put_page(page
, 1);
557 i_size_read(inode
) < ((index
+ 1) << PAGE_CACHE_SHIFT
)) {
558 i_size_write(inode
, ((index
+ 1) << PAGE_CACHE_SHIFT
));
559 /* Only the directory inode sets new_i_size */
560 set_inode_flag(F2FS_I(inode
), FI_UPDATE_DIR
);
569 static int __allocate_data_block(struct dnode_of_data
*dn
)
571 struct f2fs_sb_info
*sbi
= F2FS_SB(dn
->inode
->i_sb
);
572 struct f2fs_summary sum
;
577 if (unlikely(is_inode_flag_set(F2FS_I(dn
->inode
), FI_NO_ALLOC
)))
579 if (unlikely(!inc_valid_block_count(sbi
, dn
->inode
, 1)))
582 __set_data_blkaddr(dn
, NEW_ADDR
);
583 dn
->data_blkaddr
= NEW_ADDR
;
585 get_node_info(sbi
, dn
->nid
, &ni
);
586 set_summary(&sum
, dn
->nid
, dn
->ofs_in_node
, ni
.version
);
588 type
= CURSEG_WARM_DATA
;
590 allocate_data_block(sbi
, NULL
, NULL_ADDR
, &new_blkaddr
, &sum
, type
);
592 /* direct IO doesn't use extent cache to maximize the performance */
593 set_inode_flag(F2FS_I(dn
->inode
), FI_NO_EXTENT
);
594 update_extent_cache(new_blkaddr
, dn
);
595 clear_inode_flag(F2FS_I(dn
->inode
), FI_NO_EXTENT
);
597 dn
->data_blkaddr
= new_blkaddr
;
602 * get_data_block() now supported readahead/bmap/rw direct_IO with mapped bh.
603 * If original data blocks are allocated, then give them to blockdev.
605 * a. preallocate requested block addresses
606 * b. do not use extent cache for better performance
607 * c. give the block addresses to blockdev
609 static int get_data_block(struct inode
*inode
, sector_t iblock
,
610 struct buffer_head
*bh_result
, int create
)
612 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
613 unsigned int blkbits
= inode
->i_sb
->s_blocksize_bits
;
614 unsigned maxblocks
= bh_result
->b_size
>> blkbits
;
615 struct dnode_of_data dn
;
616 int mode
= create
? ALLOC_NODE
: LOOKUP_NODE_RA
;
617 pgoff_t pgofs
, end_offset
;
618 int err
= 0, ofs
= 1;
619 bool allocated
= false;
621 /* Get the page offset from the block offset(iblock) */
622 pgofs
= (pgoff_t
)(iblock
>> (PAGE_CACHE_SHIFT
- blkbits
));
624 if (check_extent_cache(inode
, pgofs
, bh_result
))
630 /* When reading holes, we need its node page */
631 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
632 err
= get_dnode_of_data(&dn
, pgofs
, mode
);
638 if (dn
.data_blkaddr
== NEW_ADDR
)
641 if (dn
.data_blkaddr
!= NULL_ADDR
) {
642 map_bh(bh_result
, inode
->i_sb
, dn
.data_blkaddr
);
644 err
= __allocate_data_block(&dn
);
648 map_bh(bh_result
, inode
->i_sb
, dn
.data_blkaddr
);
653 end_offset
= IS_INODE(dn
.node_page
) ?
654 ADDRS_PER_INODE(F2FS_I(inode
)) : ADDRS_PER_BLOCK
;
655 bh_result
->b_size
= (((size_t)1) << blkbits
);
660 if (dn
.ofs_in_node
>= end_offset
) {
662 sync_inode_page(&dn
);
666 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
667 err
= get_dnode_of_data(&dn
, pgofs
, mode
);
673 if (dn
.data_blkaddr
== NEW_ADDR
)
676 end_offset
= IS_INODE(dn
.node_page
) ?
677 ADDRS_PER_INODE(F2FS_I(inode
)) : ADDRS_PER_BLOCK
;
680 if (maxblocks
> (bh_result
->b_size
>> blkbits
)) {
681 block_t blkaddr
= datablock_addr(dn
.node_page
, dn
.ofs_in_node
);
682 if (blkaddr
== NULL_ADDR
&& create
) {
683 err
= __allocate_data_block(&dn
);
687 blkaddr
= dn
.data_blkaddr
;
689 /* Give more consecutive addresses for the read ahead */
690 if (blkaddr
== (bh_result
->b_blocknr
+ ofs
)) {
694 bh_result
->b_size
+= (((size_t)1) << blkbits
);
700 sync_inode_page(&dn
);
707 trace_f2fs_get_data_block(inode
, iblock
, bh_result
, err
);
711 static int f2fs_read_data_page(struct file
*file
, struct page
*page
)
713 struct inode
*inode
= page
->mapping
->host
;
716 /* If the file has inline data, try to read it directlly */
717 if (f2fs_has_inline_data(inode
))
718 ret
= f2fs_read_inline_data(inode
, page
);
720 ret
= mpage_readpage(page
, get_data_block
);
725 static int f2fs_read_data_pages(struct file
*file
,
726 struct address_space
*mapping
,
727 struct list_head
*pages
, unsigned nr_pages
)
729 struct inode
*inode
= file
->f_mapping
->host
;
731 /* If the file has inline data, skip readpages */
732 if (f2fs_has_inline_data(inode
))
735 return mpage_readpages(mapping
, pages
, nr_pages
, get_data_block
);
738 int do_write_data_page(struct page
*page
, struct f2fs_io_info
*fio
)
740 struct inode
*inode
= page
->mapping
->host
;
741 block_t old_blkaddr
, new_blkaddr
;
742 struct dnode_of_data dn
;
745 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
746 err
= get_dnode_of_data(&dn
, page
->index
, LOOKUP_NODE
);
750 old_blkaddr
= dn
.data_blkaddr
;
752 /* This page is already truncated */
753 if (old_blkaddr
== NULL_ADDR
)
756 set_page_writeback(page
);
759 * If current allocation needs SSR,
760 * it had better in-place writes for updated data.
762 if (unlikely(old_blkaddr
!= NEW_ADDR
&&
763 !is_cold_data(page
) &&
764 need_inplace_update(inode
))) {
765 rewrite_data_page(page
, old_blkaddr
, fio
);
767 write_data_page(page
, &dn
, &new_blkaddr
, fio
);
768 update_extent_cache(new_blkaddr
, &dn
);
775 static int f2fs_write_data_page(struct page
*page
,
776 struct writeback_control
*wbc
)
778 struct inode
*inode
= page
->mapping
->host
;
779 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
780 loff_t i_size
= i_size_read(inode
);
781 const pgoff_t end_index
= ((unsigned long long) i_size
)
784 bool need_balance_fs
= false;
786 struct f2fs_io_info fio
= {
788 .rw
= (wbc
->sync_mode
== WB_SYNC_ALL
) ? WRITE_SYNC
: WRITE
,
791 if (page
->index
< end_index
)
795 * If the offset is out-of-range of file size,
796 * this page does not have to be written to disk.
798 offset
= i_size
& (PAGE_CACHE_SIZE
- 1);
799 if ((page
->index
>= end_index
+ 1) || !offset
) {
800 if (S_ISDIR(inode
->i_mode
)) {
801 dec_page_count(sbi
, F2FS_DIRTY_DENTS
);
802 inode_dec_dirty_dents(inode
);
807 zero_user_segment(page
, offset
, PAGE_CACHE_SIZE
);
809 if (unlikely(sbi
->por_doing
)) {
810 err
= AOP_WRITEPAGE_ACTIVATE
;
814 /* Dentry blocks are controlled by checkpoint */
815 if (S_ISDIR(inode
->i_mode
)) {
816 dec_page_count(sbi
, F2FS_DIRTY_DENTS
);
817 inode_dec_dirty_dents(inode
);
818 err
= do_write_data_page(page
, &fio
);
822 if (f2fs_has_inline_data(inode
) || f2fs_may_inline(inode
)) {
823 err
= f2fs_write_inline_data(inode
, page
, offset
);
827 err
= do_write_data_page(page
, &fio
);
831 need_balance_fs
= true;
838 if (wbc
->for_reclaim
) {
839 f2fs_submit_merged_bio(sbi
, DATA
, WRITE
);
840 need_balance_fs
= false;
843 clear_cold_data(page
);
847 f2fs_balance_fs(sbi
);
851 wbc
->pages_skipped
++;
852 set_page_dirty(page
);
856 #define MAX_DESIRED_PAGES_WP 4096
858 static int __f2fs_writepage(struct page
*page
, struct writeback_control
*wbc
,
861 struct address_space
*mapping
= data
;
862 int ret
= mapping
->a_ops
->writepage(page
, wbc
);
863 mapping_set_error(mapping
, ret
);
867 static int f2fs_write_data_pages(struct address_space
*mapping
,
868 struct writeback_control
*wbc
)
870 struct inode
*inode
= mapping
->host
;
871 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
874 long excess_nrtw
= 0, desired_nrtw
;
876 /* deal with chardevs and other special file */
877 if (!mapping
->a_ops
->writepage
)
880 if (wbc
->nr_to_write
< MAX_DESIRED_PAGES_WP
) {
881 desired_nrtw
= MAX_DESIRED_PAGES_WP
;
882 excess_nrtw
= desired_nrtw
- wbc
->nr_to_write
;
883 wbc
->nr_to_write
= desired_nrtw
;
886 if (!S_ISDIR(inode
->i_mode
)) {
887 mutex_lock(&sbi
->writepages
);
890 ret
= write_cache_pages(mapping
, wbc
, __f2fs_writepage
, mapping
);
892 mutex_unlock(&sbi
->writepages
);
894 f2fs_submit_merged_bio(sbi
, DATA
, WRITE
);
896 remove_dirty_dir_inode(inode
);
898 wbc
->nr_to_write
-= excess_nrtw
;
902 static int f2fs_write_begin(struct file
*file
, struct address_space
*mapping
,
903 loff_t pos
, unsigned len
, unsigned flags
,
904 struct page
**pagep
, void **fsdata
)
906 struct inode
*inode
= mapping
->host
;
907 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
909 pgoff_t index
= ((unsigned long long) pos
) >> PAGE_CACHE_SHIFT
;
910 struct dnode_of_data dn
;
913 f2fs_balance_fs(sbi
);
915 err
= f2fs_convert_inline_data(inode
, pos
+ len
);
919 page
= grab_cache_page_write_begin(mapping
, index
, flags
);
924 if (f2fs_has_inline_data(inode
) && (pos
+ len
) <= MAX_INLINE_DATA
)
928 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
929 err
= f2fs_reserve_block(&dn
, index
);
933 f2fs_put_page(page
, 1);
937 if ((len
== PAGE_CACHE_SIZE
) || PageUptodate(page
))
940 if ((pos
& PAGE_CACHE_MASK
) >= i_size_read(inode
)) {
941 unsigned start
= pos
& (PAGE_CACHE_SIZE
- 1);
942 unsigned end
= start
+ len
;
944 /* Reading beyond i_size is simple: memset to zero */
945 zero_user_segments(page
, 0, start
, end
, PAGE_CACHE_SIZE
);
949 if (dn
.data_blkaddr
== NEW_ADDR
) {
950 zero_user_segment(page
, 0, PAGE_CACHE_SIZE
);
952 if (f2fs_has_inline_data(inode
))
953 err
= f2fs_read_inline_data(inode
, page
);
955 err
= f2fs_submit_page_bio(sbi
, page
, dn
.data_blkaddr
,
960 if (unlikely(!PageUptodate(page
))) {
961 f2fs_put_page(page
, 1);
964 if (unlikely(page
->mapping
!= mapping
)) {
965 f2fs_put_page(page
, 1);
970 SetPageUptodate(page
);
971 clear_cold_data(page
);
975 static int f2fs_write_end(struct file
*file
,
976 struct address_space
*mapping
,
977 loff_t pos
, unsigned len
, unsigned copied
,
978 struct page
*page
, void *fsdata
)
980 struct inode
*inode
= page
->mapping
->host
;
982 SetPageUptodate(page
);
983 set_page_dirty(page
);
985 if (pos
+ copied
> i_size_read(inode
)) {
986 i_size_write(inode
, pos
+ copied
);
987 mark_inode_dirty(inode
);
988 update_inode_page(inode
);
991 f2fs_put_page(page
, 1);
995 static int check_direct_IO(struct inode
*inode
, int rw
,
996 const struct iovec
*iov
, loff_t offset
, unsigned long nr_segs
)
998 unsigned blocksize_mask
= inode
->i_sb
->s_blocksize
- 1;
1004 if (offset
& blocksize_mask
)
1007 for (i
= 0; i
< nr_segs
; i
++)
1008 if (iov
[i
].iov_len
& blocksize_mask
)
1013 static ssize_t
f2fs_direct_IO(int rw
, struct kiocb
*iocb
,
1014 const struct iovec
*iov
, loff_t offset
, unsigned long nr_segs
)
1016 struct file
*file
= iocb
->ki_filp
;
1017 struct inode
*inode
= file
->f_mapping
->host
;
1019 /* Let buffer I/O handle the inline data case. */
1020 if (f2fs_has_inline_data(inode
))
1023 if (check_direct_IO(inode
, rw
, iov
, offset
, nr_segs
))
1026 return blockdev_direct_IO(rw
, iocb
, inode
, iov
, offset
, nr_segs
,
1030 static void f2fs_invalidate_data_page(struct page
*page
, unsigned int offset
,
1031 unsigned int length
)
1033 struct inode
*inode
= page
->mapping
->host
;
1034 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
1035 if (S_ISDIR(inode
->i_mode
) && PageDirty(page
)) {
1036 dec_page_count(sbi
, F2FS_DIRTY_DENTS
);
1037 inode_dec_dirty_dents(inode
);
1039 ClearPagePrivate(page
);
1042 static int f2fs_release_data_page(struct page
*page
, gfp_t wait
)
1044 ClearPagePrivate(page
);
1048 static int f2fs_set_data_page_dirty(struct page
*page
)
1050 struct address_space
*mapping
= page
->mapping
;
1051 struct inode
*inode
= mapping
->host
;
1053 trace_f2fs_set_page_dirty(page
, DATA
);
1055 SetPageUptodate(page
);
1056 mark_inode_dirty(inode
);
1058 if (!PageDirty(page
)) {
1059 __set_page_dirty_nobuffers(page
);
1060 set_dirty_dir_page(inode
, page
);
1066 static sector_t
f2fs_bmap(struct address_space
*mapping
, sector_t block
)
1068 return generic_block_bmap(mapping
, block
, get_data_block
);
1071 const struct address_space_operations f2fs_dblock_aops
= {
1072 .readpage
= f2fs_read_data_page
,
1073 .readpages
= f2fs_read_data_pages
,
1074 .writepage
= f2fs_write_data_page
,
1075 .writepages
= f2fs_write_data_pages
,
1076 .write_begin
= f2fs_write_begin
,
1077 .write_end
= f2fs_write_end
,
1078 .set_page_dirty
= f2fs_set_data_page_dirty
,
1079 .invalidatepage
= f2fs_invalidate_data_page
,
1080 .releasepage
= f2fs_release_data_page
,
1081 .direct_IO
= f2fs_direct_IO
,