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/writeback.h>
16 #include <linux/backing-dev.h>
17 #include <linux/pagevec.h>
18 #include <linux/blkdev.h>
19 #include <linux/bio.h>
20 #include <linux/prefetch.h>
21 #include <linux/uio.h>
22 #include <linux/cleancache.h>
28 #include <trace/events/f2fs.h>
30 static void f2fs_read_end_io(struct bio
*bio
)
35 if (f2fs_bio_encrypted(bio
)) {
37 f2fs_release_crypto_ctx(bio
->bi_private
);
39 f2fs_end_io_crypto_work(bio
->bi_private
, bio
);
44 bio_for_each_segment_all(bvec
, bio
, i
) {
45 struct page
*page
= bvec
->bv_page
;
48 SetPageUptodate(page
);
50 ClearPageUptodate(page
);
58 static void f2fs_write_end_io(struct bio
*bio
)
60 struct f2fs_sb_info
*sbi
= bio
->bi_private
;
64 bio_for_each_segment_all(bvec
, bio
, i
) {
65 struct page
*page
= bvec
->bv_page
;
67 f2fs_restore_and_release_control_page(&page
);
69 if (unlikely(bio
->bi_error
)) {
71 set_bit(AS_EIO
, &page
->mapping
->flags
);
72 f2fs_stop_checkpoint(sbi
);
74 end_page_writeback(page
);
75 dec_page_count(sbi
, F2FS_WRITEBACK
);
78 if (!get_pages(sbi
, F2FS_WRITEBACK
) &&
79 !list_empty(&sbi
->cp_wait
.task_list
))
80 wake_up(&sbi
->cp_wait
);
86 * Low-level block read/write IO operations.
88 static struct bio
*__bio_alloc(struct f2fs_sb_info
*sbi
, block_t blk_addr
,
89 int npages
, bool is_read
)
93 bio
= f2fs_bio_alloc(npages
);
95 bio
->bi_bdev
= sbi
->sb
->s_bdev
;
96 bio
->bi_iter
.bi_sector
= SECTOR_FROM_BLOCK(blk_addr
);
97 bio
->bi_end_io
= is_read
? f2fs_read_end_io
: f2fs_write_end_io
;
98 bio
->bi_private
= is_read
? NULL
: sbi
;
103 static void __submit_merged_bio(struct f2fs_bio_info
*io
)
105 struct f2fs_io_info
*fio
= &io
->fio
;
110 if (is_read_io(fio
->rw
))
111 trace_f2fs_submit_read_bio(io
->sbi
->sb
, fio
, io
->bio
);
113 trace_f2fs_submit_write_bio(io
->sbi
->sb
, fio
, io
->bio
);
115 submit_bio(fio
->rw
, io
->bio
);
119 void f2fs_submit_merged_bio(struct f2fs_sb_info
*sbi
,
120 enum page_type type
, int rw
)
122 enum page_type btype
= PAGE_TYPE_OF_BIO(type
);
123 struct f2fs_bio_info
*io
;
125 io
= is_read_io(rw
) ? &sbi
->read_io
: &sbi
->write_io
[btype
];
127 down_write(&io
->io_rwsem
);
129 /* change META to META_FLUSH in the checkpoint procedure */
130 if (type
>= META_FLUSH
) {
131 io
->fio
.type
= META_FLUSH
;
132 if (test_opt(sbi
, NOBARRIER
))
133 io
->fio
.rw
= WRITE_FLUSH
| REQ_META
| REQ_PRIO
;
135 io
->fio
.rw
= WRITE_FLUSH_FUA
| REQ_META
| REQ_PRIO
;
137 __submit_merged_bio(io
);
138 up_write(&io
->io_rwsem
);
142 * Fill the locked page with data located in the block address.
143 * Return unlocked page.
145 int f2fs_submit_page_bio(struct f2fs_io_info
*fio
)
148 struct page
*page
= fio
->encrypted_page
? fio
->encrypted_page
: fio
->page
;
150 trace_f2fs_submit_page_bio(page
, fio
);
151 f2fs_trace_ios(fio
, 0);
153 /* Allocate a new bio */
154 bio
= __bio_alloc(fio
->sbi
, fio
->blk_addr
, 1, is_read_io(fio
->rw
));
156 if (bio_add_page(bio
, page
, PAGE_CACHE_SIZE
, 0) < PAGE_CACHE_SIZE
) {
161 submit_bio(fio
->rw
, bio
);
165 void f2fs_submit_page_mbio(struct f2fs_io_info
*fio
)
167 struct f2fs_sb_info
*sbi
= fio
->sbi
;
168 enum page_type btype
= PAGE_TYPE_OF_BIO(fio
->type
);
169 struct f2fs_bio_info
*io
;
170 bool is_read
= is_read_io(fio
->rw
);
171 struct page
*bio_page
;
173 io
= is_read
? &sbi
->read_io
: &sbi
->write_io
[btype
];
175 verify_block_addr(sbi
, fio
->blk_addr
);
177 down_write(&io
->io_rwsem
);
180 inc_page_count(sbi
, F2FS_WRITEBACK
);
182 if (io
->bio
&& (io
->last_block_in_bio
!= fio
->blk_addr
- 1 ||
183 io
->fio
.rw
!= fio
->rw
))
184 __submit_merged_bio(io
);
186 if (io
->bio
== NULL
) {
187 int bio_blocks
= MAX_BIO_BLOCKS(sbi
);
189 io
->bio
= __bio_alloc(sbi
, fio
->blk_addr
, bio_blocks
, is_read
);
193 bio_page
= fio
->encrypted_page
? fio
->encrypted_page
: fio
->page
;
195 if (bio_add_page(io
->bio
, bio_page
, PAGE_CACHE_SIZE
, 0) <
197 __submit_merged_bio(io
);
201 io
->last_block_in_bio
= fio
->blk_addr
;
202 f2fs_trace_ios(fio
, 0);
204 up_write(&io
->io_rwsem
);
205 trace_f2fs_submit_page_mbio(fio
->page
, fio
);
209 * Lock ordering for the change of data block address:
212 * update block addresses in the node page
214 void set_data_blkaddr(struct dnode_of_data
*dn
)
216 struct f2fs_node
*rn
;
218 struct page
*node_page
= dn
->node_page
;
219 unsigned int ofs_in_node
= dn
->ofs_in_node
;
221 f2fs_wait_on_page_writeback(node_page
, NODE
);
223 rn
= F2FS_NODE(node_page
);
225 /* Get physical address of data block */
226 addr_array
= blkaddr_in_node(rn
);
227 addr_array
[ofs_in_node
] = cpu_to_le32(dn
->data_blkaddr
);
228 set_page_dirty(node_page
);
231 int reserve_new_block(struct dnode_of_data
*dn
)
233 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
235 if (unlikely(is_inode_flag_set(F2FS_I(dn
->inode
), FI_NO_ALLOC
)))
237 if (unlikely(!inc_valid_block_count(sbi
, dn
->inode
, 1)))
240 trace_f2fs_reserve_new_block(dn
->inode
, dn
->nid
, dn
->ofs_in_node
);
242 dn
->data_blkaddr
= NEW_ADDR
;
243 set_data_blkaddr(dn
);
244 mark_inode_dirty(dn
->inode
);
249 int f2fs_reserve_block(struct dnode_of_data
*dn
, pgoff_t index
)
251 bool need_put
= dn
->inode_page
? false : true;
254 err
= get_dnode_of_data(dn
, index
, ALLOC_NODE
);
258 if (dn
->data_blkaddr
== NULL_ADDR
)
259 err
= reserve_new_block(dn
);
265 int f2fs_get_block(struct dnode_of_data
*dn
, pgoff_t index
)
267 struct extent_info ei
;
268 struct inode
*inode
= dn
->inode
;
270 if (f2fs_lookup_extent_cache(inode
, index
, &ei
)) {
271 dn
->data_blkaddr
= ei
.blk
+ index
- ei
.fofs
;
275 return f2fs_reserve_block(dn
, index
);
278 struct page
*get_read_data_page(struct inode
*inode
, pgoff_t index
,
279 int rw
, bool for_write
)
281 struct address_space
*mapping
= inode
->i_mapping
;
282 struct dnode_of_data dn
;
284 struct extent_info ei
;
286 struct f2fs_io_info fio
= {
287 .sbi
= F2FS_I_SB(inode
),
290 .encrypted_page
= NULL
,
293 if (f2fs_encrypted_inode(inode
) && S_ISREG(inode
->i_mode
))
294 return read_mapping_page(mapping
, index
, NULL
);
296 page
= f2fs_grab_cache_page(mapping
, index
, for_write
);
298 return ERR_PTR(-ENOMEM
);
300 if (f2fs_lookup_extent_cache(inode
, index
, &ei
)) {
301 dn
.data_blkaddr
= ei
.blk
+ index
- ei
.fofs
;
305 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
306 err
= get_dnode_of_data(&dn
, index
, LOOKUP_NODE
);
311 if (unlikely(dn
.data_blkaddr
== NULL_ADDR
)) {
316 if (PageUptodate(page
)) {
322 * A new dentry page is allocated but not able to be written, since its
323 * new inode page couldn't be allocated due to -ENOSPC.
324 * In such the case, its blkaddr can be remained as NEW_ADDR.
325 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
327 if (dn
.data_blkaddr
== NEW_ADDR
) {
328 zero_user_segment(page
, 0, PAGE_CACHE_SIZE
);
329 SetPageUptodate(page
);
334 fio
.blk_addr
= dn
.data_blkaddr
;
336 err
= f2fs_submit_page_bio(&fio
);
342 f2fs_put_page(page
, 1);
346 struct page
*find_data_page(struct inode
*inode
, pgoff_t index
)
348 struct address_space
*mapping
= inode
->i_mapping
;
351 page
= find_get_page(mapping
, index
);
352 if (page
&& PageUptodate(page
))
354 f2fs_put_page(page
, 0);
356 page
= get_read_data_page(inode
, index
, READ_SYNC
, false);
360 if (PageUptodate(page
))
363 wait_on_page_locked(page
);
364 if (unlikely(!PageUptodate(page
))) {
365 f2fs_put_page(page
, 0);
366 return ERR_PTR(-EIO
);
372 * If it tries to access a hole, return an error.
373 * Because, the callers, functions in dir.c and GC, should be able to know
374 * whether this page exists or not.
376 struct page
*get_lock_data_page(struct inode
*inode
, pgoff_t index
,
379 struct address_space
*mapping
= inode
->i_mapping
;
382 page
= get_read_data_page(inode
, index
, READ_SYNC
, for_write
);
386 /* wait for read completion */
388 if (unlikely(!PageUptodate(page
))) {
389 f2fs_put_page(page
, 1);
390 return ERR_PTR(-EIO
);
392 if (unlikely(page
->mapping
!= mapping
)) {
393 f2fs_put_page(page
, 1);
400 * Caller ensures that this data page is never allocated.
401 * A new zero-filled data page is allocated in the page cache.
403 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
405 * Note that, ipage is set only by make_empty_dir, and if any error occur,
406 * ipage should be released by this function.
408 struct page
*get_new_data_page(struct inode
*inode
,
409 struct page
*ipage
, pgoff_t index
, bool new_i_size
)
411 struct address_space
*mapping
= inode
->i_mapping
;
413 struct dnode_of_data dn
;
416 page
= f2fs_grab_cache_page(mapping
, index
, true);
419 * before exiting, we should make sure ipage will be released
420 * if any error occur.
422 f2fs_put_page(ipage
, 1);
423 return ERR_PTR(-ENOMEM
);
426 set_new_dnode(&dn
, inode
, ipage
, NULL
, 0);
427 err
= f2fs_reserve_block(&dn
, index
);
429 f2fs_put_page(page
, 1);
435 if (PageUptodate(page
))
438 if (dn
.data_blkaddr
== NEW_ADDR
) {
439 zero_user_segment(page
, 0, PAGE_CACHE_SIZE
);
440 SetPageUptodate(page
);
442 f2fs_put_page(page
, 1);
444 page
= get_read_data_page(inode
, index
, READ_SYNC
, true);
448 /* wait for read completion */
452 if (new_i_size
&& i_size_read(inode
) <
453 ((loff_t
)(index
+ 1) << PAGE_CACHE_SHIFT
)) {
454 i_size_write(inode
, ((loff_t
)(index
+ 1) << PAGE_CACHE_SHIFT
));
455 /* Only the directory inode sets new_i_size */
456 set_inode_flag(F2FS_I(inode
), FI_UPDATE_DIR
);
461 static int __allocate_data_block(struct dnode_of_data
*dn
)
463 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
464 struct f2fs_inode_info
*fi
= F2FS_I(dn
->inode
);
465 struct f2fs_summary sum
;
467 int seg
= CURSEG_WARM_DATA
;
470 if (unlikely(is_inode_flag_set(F2FS_I(dn
->inode
), FI_NO_ALLOC
)))
473 dn
->data_blkaddr
= datablock_addr(dn
->node_page
, dn
->ofs_in_node
);
474 if (dn
->data_blkaddr
== NEW_ADDR
)
477 if (unlikely(!inc_valid_block_count(sbi
, dn
->inode
, 1)))
481 get_node_info(sbi
, dn
->nid
, &ni
);
482 set_summary(&sum
, dn
->nid
, dn
->ofs_in_node
, ni
.version
);
484 if (dn
->ofs_in_node
== 0 && dn
->inode_page
== dn
->node_page
)
485 seg
= CURSEG_DIRECT_IO
;
487 allocate_data_block(sbi
, NULL
, dn
->data_blkaddr
, &dn
->data_blkaddr
,
489 set_data_blkaddr(dn
);
492 fofs
= start_bidx_of_node(ofs_of_node(dn
->node_page
), fi
) +
494 if (i_size_read(dn
->inode
) < ((loff_t
)(fofs
+ 1) << PAGE_CACHE_SHIFT
))
495 i_size_write(dn
->inode
,
496 ((loff_t
)(fofs
+ 1) << PAGE_CACHE_SHIFT
));
498 /* direct IO doesn't use extent cache to maximize the performance */
499 f2fs_drop_largest_extent(dn
->inode
, fofs
);
504 static void __allocate_data_blocks(struct inode
*inode
, loff_t offset
,
507 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
508 struct dnode_of_data dn
;
509 u64 start
= F2FS_BYTES_TO_BLK(offset
);
510 u64 len
= F2FS_BYTES_TO_BLK(count
);
515 f2fs_balance_fs(sbi
);
518 /* When reading holes, we need its node page */
519 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
520 if (get_dnode_of_data(&dn
, start
, ALLOC_NODE
))
524 end_offset
= ADDRS_PER_PAGE(dn
.node_page
, F2FS_I(inode
));
526 while (dn
.ofs_in_node
< end_offset
&& len
) {
529 if (unlikely(f2fs_cp_error(sbi
)))
532 blkaddr
= datablock_addr(dn
.node_page
, dn
.ofs_in_node
);
533 if (blkaddr
== NULL_ADDR
|| blkaddr
== NEW_ADDR
) {
534 if (__allocate_data_block(&dn
))
544 sync_inode_page(&dn
);
553 sync_inode_page(&dn
);
561 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
562 * f2fs_map_blocks structure.
563 * If original data blocks are allocated, then give them to blockdev.
565 * a. preallocate requested block addresses
566 * b. do not use extent cache for better performance
567 * c. give the block addresses to blockdev
569 static int f2fs_map_blocks(struct inode
*inode
, struct f2fs_map_blocks
*map
,
570 int create
, int flag
)
572 unsigned int maxblocks
= map
->m_len
;
573 struct dnode_of_data dn
;
574 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
575 int mode
= create
? ALLOC_NODE
: LOOKUP_NODE_RA
;
576 pgoff_t pgofs
, end_offset
;
577 int err
= 0, ofs
= 1;
578 struct extent_info ei
;
579 bool allocated
= false;
584 /* it only supports block size == page size */
585 pgofs
= (pgoff_t
)map
->m_lblk
;
587 if (f2fs_lookup_extent_cache(inode
, pgofs
, &ei
)) {
588 map
->m_pblk
= ei
.blk
+ pgofs
- ei
.fofs
;
589 map
->m_len
= min((pgoff_t
)maxblocks
, ei
.fofs
+ ei
.len
- pgofs
);
590 map
->m_flags
= F2FS_MAP_MAPPED
;
595 f2fs_lock_op(F2FS_I_SB(inode
));
597 /* When reading holes, we need its node page */
598 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
599 err
= get_dnode_of_data(&dn
, pgofs
, mode
);
606 if (dn
.data_blkaddr
== NEW_ADDR
|| dn
.data_blkaddr
== NULL_ADDR
) {
608 if (unlikely(f2fs_cp_error(sbi
))) {
612 err
= __allocate_data_block(&dn
);
616 map
->m_flags
= F2FS_MAP_NEW
;
618 if (flag
!= F2FS_GET_BLOCK_FIEMAP
||
619 dn
.data_blkaddr
!= NEW_ADDR
) {
620 if (flag
== F2FS_GET_BLOCK_BMAP
)
626 * preallocated unwritten block should be mapped
629 if (dn
.data_blkaddr
== NEW_ADDR
)
630 map
->m_flags
= F2FS_MAP_UNWRITTEN
;
634 map
->m_flags
|= F2FS_MAP_MAPPED
;
635 map
->m_pblk
= dn
.data_blkaddr
;
638 end_offset
= ADDRS_PER_PAGE(dn
.node_page
, F2FS_I(inode
));
643 if (dn
.ofs_in_node
>= end_offset
) {
645 sync_inode_page(&dn
);
649 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
650 err
= get_dnode_of_data(&dn
, pgofs
, mode
);
657 end_offset
= ADDRS_PER_PAGE(dn
.node_page
, F2FS_I(inode
));
660 if (maxblocks
> map
->m_len
) {
661 block_t blkaddr
= datablock_addr(dn
.node_page
, dn
.ofs_in_node
);
663 if (blkaddr
== NEW_ADDR
|| blkaddr
== NULL_ADDR
) {
665 if (unlikely(f2fs_cp_error(sbi
))) {
669 err
= __allocate_data_block(&dn
);
673 map
->m_flags
|= F2FS_MAP_NEW
;
674 blkaddr
= dn
.data_blkaddr
;
677 * we only merge preallocated unwritten blocks
680 if (flag
!= F2FS_GET_BLOCK_FIEMAP
||
686 /* Give more consecutive addresses for the readahead */
687 if ((map
->m_pblk
!= NEW_ADDR
&&
688 blkaddr
== (map
->m_pblk
+ ofs
)) ||
689 (map
->m_pblk
== NEW_ADDR
&&
690 blkaddr
== NEW_ADDR
)) {
700 sync_inode_page(&dn
);
705 f2fs_unlock_op(F2FS_I_SB(inode
));
707 trace_f2fs_map_blocks(inode
, map
, err
);
711 static int __get_data_block(struct inode
*inode
, sector_t iblock
,
712 struct buffer_head
*bh
, int create
, int flag
)
714 struct f2fs_map_blocks map
;
718 map
.m_len
= bh
->b_size
>> inode
->i_blkbits
;
720 ret
= f2fs_map_blocks(inode
, &map
, create
, flag
);
722 map_bh(bh
, inode
->i_sb
, map
.m_pblk
);
723 bh
->b_state
= (bh
->b_state
& ~F2FS_MAP_FLAGS
) | map
.m_flags
;
724 bh
->b_size
= (u64
)map
.m_len
<< inode
->i_blkbits
;
729 static int get_data_block(struct inode
*inode
, sector_t iblock
,
730 struct buffer_head
*bh_result
, int create
, int flag
)
732 return __get_data_block(inode
, iblock
, bh_result
, create
, flag
);
735 static int get_data_block_dio(struct inode
*inode
, sector_t iblock
,
736 struct buffer_head
*bh_result
, int create
)
738 return __get_data_block(inode
, iblock
, bh_result
, create
,
742 static int get_data_block_bmap(struct inode
*inode
, sector_t iblock
,
743 struct buffer_head
*bh_result
, int create
)
745 return __get_data_block(inode
, iblock
, bh_result
, create
,
746 F2FS_GET_BLOCK_BMAP
);
749 static inline sector_t
logical_to_blk(struct inode
*inode
, loff_t offset
)
751 return (offset
>> inode
->i_blkbits
);
754 static inline loff_t
blk_to_logical(struct inode
*inode
, sector_t blk
)
756 return (blk
<< inode
->i_blkbits
);
759 int f2fs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
762 struct buffer_head map_bh
;
763 sector_t start_blk
, last_blk
;
764 loff_t isize
= i_size_read(inode
);
765 u64 logical
= 0, phys
= 0, size
= 0;
767 bool past_eof
= false, whole_file
= false;
770 ret
= fiemap_check_flags(fieinfo
, FIEMAP_FLAG_SYNC
);
774 if (f2fs_has_inline_data(inode
)) {
775 ret
= f2fs_inline_data_fiemap(inode
, fieinfo
, start
, len
);
780 mutex_lock(&inode
->i_mutex
);
787 if (logical_to_blk(inode
, len
) == 0)
788 len
= blk_to_logical(inode
, 1);
790 start_blk
= logical_to_blk(inode
, start
);
791 last_blk
= logical_to_blk(inode
, start
+ len
- 1);
793 memset(&map_bh
, 0, sizeof(struct buffer_head
));
796 ret
= get_data_block(inode
, start_blk
, &map_bh
, 0,
797 F2FS_GET_BLOCK_FIEMAP
);
802 if (!buffer_mapped(&map_bh
)) {
805 if (!past_eof
&& blk_to_logical(inode
, start_blk
) >= isize
)
808 if (past_eof
&& size
) {
809 flags
|= FIEMAP_EXTENT_LAST
;
810 ret
= fiemap_fill_next_extent(fieinfo
, logical
,
813 ret
= fiemap_fill_next_extent(fieinfo
, logical
,
818 /* if we have holes up to/past EOF then we're done */
819 if (start_blk
> last_blk
|| past_eof
|| ret
)
822 if (start_blk
> last_blk
&& !whole_file
) {
823 ret
= fiemap_fill_next_extent(fieinfo
, logical
,
829 * if size != 0 then we know we already have an extent
833 ret
= fiemap_fill_next_extent(fieinfo
, logical
,
839 logical
= blk_to_logical(inode
, start_blk
);
840 phys
= blk_to_logical(inode
, map_bh
.b_blocknr
);
841 size
= map_bh
.b_size
;
843 if (buffer_unwritten(&map_bh
))
844 flags
= FIEMAP_EXTENT_UNWRITTEN
;
846 start_blk
+= logical_to_blk(inode
, size
);
849 * If we are past the EOF, then we need to make sure as
850 * soon as we find a hole that the last extent we found
851 * is marked with FIEMAP_EXTENT_LAST
853 if (!past_eof
&& logical
+ size
>= isize
)
857 if (fatal_signal_pending(current
))
865 mutex_unlock(&inode
->i_mutex
);
870 * This function was originally taken from fs/mpage.c, and customized for f2fs.
871 * Major change was from block_size == page_size in f2fs by default.
873 static int f2fs_mpage_readpages(struct address_space
*mapping
,
874 struct list_head
*pages
, struct page
*page
,
877 struct bio
*bio
= NULL
;
879 sector_t last_block_in_bio
= 0;
880 struct inode
*inode
= mapping
->host
;
881 const unsigned blkbits
= inode
->i_blkbits
;
882 const unsigned blocksize
= 1 << blkbits
;
883 sector_t block_in_file
;
885 sector_t last_block_in_file
;
887 struct block_device
*bdev
= inode
->i_sb
->s_bdev
;
888 struct f2fs_map_blocks map
;
895 for (page_idx
= 0; nr_pages
; page_idx
++, nr_pages
--) {
897 prefetchw(&page
->flags
);
899 page
= list_entry(pages
->prev
, struct page
, lru
);
900 list_del(&page
->lru
);
901 if (add_to_page_cache_lru(page
, mapping
,
902 page
->index
, GFP_KERNEL
))
906 block_in_file
= (sector_t
)page
->index
;
907 last_block
= block_in_file
+ nr_pages
;
908 last_block_in_file
= (i_size_read(inode
) + blocksize
- 1) >>
910 if (last_block
> last_block_in_file
)
911 last_block
= last_block_in_file
;
914 * Map blocks using the previous result first.
916 if ((map
.m_flags
& F2FS_MAP_MAPPED
) &&
917 block_in_file
> map
.m_lblk
&&
918 block_in_file
< (map
.m_lblk
+ map
.m_len
))
922 * Then do more f2fs_map_blocks() calls until we are
923 * done with this page.
927 if (block_in_file
< last_block
) {
928 map
.m_lblk
= block_in_file
;
929 map
.m_len
= last_block
- block_in_file
;
931 if (f2fs_map_blocks(inode
, &map
, 0,
932 F2FS_GET_BLOCK_READ
))
936 if ((map
.m_flags
& F2FS_MAP_MAPPED
)) {
937 block_nr
= map
.m_pblk
+ block_in_file
- map
.m_lblk
;
938 SetPageMappedToDisk(page
);
940 if (!PageUptodate(page
) && !cleancache_get_page(page
)) {
941 SetPageUptodate(page
);
945 zero_user_segment(page
, 0, PAGE_CACHE_SIZE
);
946 SetPageUptodate(page
);
952 * This page will go to BIO. Do we need to send this
955 if (bio
&& (last_block_in_bio
!= block_nr
- 1)) {
957 submit_bio(READ
, bio
);
961 struct f2fs_crypto_ctx
*ctx
= NULL
;
963 if (f2fs_encrypted_inode(inode
) &&
964 S_ISREG(inode
->i_mode
)) {
966 ctx
= f2fs_get_crypto_ctx(inode
);
970 /* wait the page to be moved by cleaning */
971 f2fs_wait_on_encrypted_page_writeback(
972 F2FS_I_SB(inode
), block_nr
);
975 bio
= bio_alloc(GFP_KERNEL
,
976 min_t(int, nr_pages
, BIO_MAX_PAGES
));
979 f2fs_release_crypto_ctx(ctx
);
983 bio
->bi_iter
.bi_sector
= SECTOR_FROM_BLOCK(block_nr
);
984 bio
->bi_end_io
= f2fs_read_end_io
;
985 bio
->bi_private
= ctx
;
988 if (bio_add_page(bio
, page
, blocksize
, 0) < blocksize
)
989 goto submit_and_realloc
;
991 last_block_in_bio
= block_nr
;
995 zero_user_segment(page
, 0, PAGE_CACHE_SIZE
);
1000 submit_bio(READ
, bio
);
1006 page_cache_release(page
);
1008 BUG_ON(pages
&& !list_empty(pages
));
1010 submit_bio(READ
, bio
);
1014 static int f2fs_read_data_page(struct file
*file
, struct page
*page
)
1016 struct inode
*inode
= page
->mapping
->host
;
1019 trace_f2fs_readpage(page
, DATA
);
1021 /* If the file has inline data, try to read it directly */
1022 if (f2fs_has_inline_data(inode
))
1023 ret
= f2fs_read_inline_data(inode
, page
);
1025 ret
= f2fs_mpage_readpages(page
->mapping
, NULL
, page
, 1);
1029 static int f2fs_read_data_pages(struct file
*file
,
1030 struct address_space
*mapping
,
1031 struct list_head
*pages
, unsigned nr_pages
)
1033 struct inode
*inode
= file
->f_mapping
->host
;
1034 struct page
*page
= list_entry(pages
->prev
, struct page
, lru
);
1036 trace_f2fs_readpages(inode
, page
, nr_pages
);
1038 /* If the file has inline data, skip readpages */
1039 if (f2fs_has_inline_data(inode
))
1042 return f2fs_mpage_readpages(mapping
, pages
, NULL
, nr_pages
);
1045 int do_write_data_page(struct f2fs_io_info
*fio
)
1047 struct page
*page
= fio
->page
;
1048 struct inode
*inode
= page
->mapping
->host
;
1049 struct dnode_of_data dn
;
1052 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
1053 err
= get_dnode_of_data(&dn
, page
->index
, LOOKUP_NODE
);
1057 fio
->blk_addr
= dn
.data_blkaddr
;
1059 /* This page is already truncated */
1060 if (fio
->blk_addr
== NULL_ADDR
) {
1061 ClearPageUptodate(page
);
1065 if (f2fs_encrypted_inode(inode
) && S_ISREG(inode
->i_mode
)) {
1067 /* wait for GCed encrypted page writeback */
1068 f2fs_wait_on_encrypted_page_writeback(F2FS_I_SB(inode
),
1071 fio
->encrypted_page
= f2fs_encrypt(inode
, fio
->page
);
1072 if (IS_ERR(fio
->encrypted_page
)) {
1073 err
= PTR_ERR(fio
->encrypted_page
);
1078 set_page_writeback(page
);
1081 * If current allocation needs SSR,
1082 * it had better in-place writes for updated data.
1084 if (unlikely(fio
->blk_addr
!= NEW_ADDR
&&
1085 !is_cold_data(page
) &&
1086 need_inplace_update(inode
))) {
1087 rewrite_data_page(fio
);
1088 set_inode_flag(F2FS_I(inode
), FI_UPDATE_WRITE
);
1089 trace_f2fs_do_write_data_page(page
, IPU
);
1091 write_data_page(&dn
, fio
);
1092 set_data_blkaddr(&dn
);
1093 f2fs_update_extent_cache(&dn
);
1094 trace_f2fs_do_write_data_page(page
, OPU
);
1095 set_inode_flag(F2FS_I(inode
), FI_APPEND_WRITE
);
1096 if (page
->index
== 0)
1097 set_inode_flag(F2FS_I(inode
), FI_FIRST_BLOCK_WRITTEN
);
1100 f2fs_put_dnode(&dn
);
1104 static int f2fs_write_data_page(struct page
*page
,
1105 struct writeback_control
*wbc
)
1107 struct inode
*inode
= page
->mapping
->host
;
1108 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1109 loff_t i_size
= i_size_read(inode
);
1110 const pgoff_t end_index
= ((unsigned long long) i_size
)
1111 >> PAGE_CACHE_SHIFT
;
1112 unsigned offset
= 0;
1113 bool need_balance_fs
= false;
1115 struct f2fs_io_info fio
= {
1118 .rw
= (wbc
->sync_mode
== WB_SYNC_ALL
) ? WRITE_SYNC
: WRITE
,
1120 .encrypted_page
= NULL
,
1123 trace_f2fs_writepage(page
, DATA
);
1125 if (page
->index
< end_index
)
1129 * If the offset is out-of-range of file size,
1130 * this page does not have to be written to disk.
1132 offset
= i_size
& (PAGE_CACHE_SIZE
- 1);
1133 if ((page
->index
>= end_index
+ 1) || !offset
)
1136 zero_user_segment(page
, offset
, PAGE_CACHE_SIZE
);
1138 if (unlikely(is_sbi_flag_set(sbi
, SBI_POR_DOING
)))
1140 if (f2fs_is_drop_cache(inode
))
1142 if (f2fs_is_volatile_file(inode
) && !wbc
->for_reclaim
&&
1143 available_free_memory(sbi
, BASE_CHECK
))
1146 /* Dentry blocks are controlled by checkpoint */
1147 if (S_ISDIR(inode
->i_mode
)) {
1148 if (unlikely(f2fs_cp_error(sbi
)))
1150 err
= do_write_data_page(&fio
);
1154 /* we should bypass data pages to proceed the kworkder jobs */
1155 if (unlikely(f2fs_cp_error(sbi
))) {
1160 if (!wbc
->for_reclaim
)
1161 need_balance_fs
= true;
1162 else if (has_not_enough_free_secs(sbi
, 0))
1167 if (f2fs_has_inline_data(inode
))
1168 err
= f2fs_write_inline_data(inode
, page
);
1170 err
= do_write_data_page(&fio
);
1171 f2fs_unlock_op(sbi
);
1173 if (err
&& err
!= -ENOENT
)
1176 clear_cold_data(page
);
1178 inode_dec_dirty_pages(inode
);
1180 ClearPageUptodate(page
);
1182 if (need_balance_fs
)
1183 f2fs_balance_fs(sbi
);
1184 if (wbc
->for_reclaim
)
1185 f2fs_submit_merged_bio(sbi
, DATA
, WRITE
);
1189 redirty_page_for_writepage(wbc
, page
);
1190 return AOP_WRITEPAGE_ACTIVATE
;
1193 static int __f2fs_writepage(struct page
*page
, struct writeback_control
*wbc
,
1196 struct address_space
*mapping
= data
;
1197 int ret
= mapping
->a_ops
->writepage(page
, wbc
);
1198 mapping_set_error(mapping
, ret
);
1203 * This function was copied from write_cche_pages from mm/page-writeback.c.
1204 * The major change is making write step of cold data page separately from
1205 * warm/hot data page.
1207 static int f2fs_write_cache_pages(struct address_space
*mapping
,
1208 struct writeback_control
*wbc
, writepage_t writepage
,
1213 struct pagevec pvec
;
1215 pgoff_t
uninitialized_var(writeback_index
);
1217 pgoff_t end
; /* Inclusive */
1220 int range_whole
= 0;
1224 pagevec_init(&pvec
, 0);
1226 if (wbc
->range_cyclic
) {
1227 writeback_index
= mapping
->writeback_index
; /* prev offset */
1228 index
= writeback_index
;
1235 index
= wbc
->range_start
>> PAGE_CACHE_SHIFT
;
1236 end
= wbc
->range_end
>> PAGE_CACHE_SHIFT
;
1237 if (wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
)
1239 cycled
= 1; /* ignore range_cyclic tests */
1241 if (wbc
->sync_mode
== WB_SYNC_ALL
|| wbc
->tagged_writepages
)
1242 tag
= PAGECACHE_TAG_TOWRITE
;
1244 tag
= PAGECACHE_TAG_DIRTY
;
1246 if (wbc
->sync_mode
== WB_SYNC_ALL
|| wbc
->tagged_writepages
)
1247 tag_pages_for_writeback(mapping
, index
, end
);
1249 while (!done
&& (index
<= end
)) {
1252 nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
, tag
,
1253 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
- 1) + 1);
1257 for (i
= 0; i
< nr_pages
; i
++) {
1258 struct page
*page
= pvec
.pages
[i
];
1260 if (page
->index
> end
) {
1265 done_index
= page
->index
;
1269 if (unlikely(page
->mapping
!= mapping
)) {
1275 if (!PageDirty(page
)) {
1276 /* someone wrote it for us */
1277 goto continue_unlock
;
1280 if (step
== is_cold_data(page
))
1281 goto continue_unlock
;
1283 if (PageWriteback(page
)) {
1284 if (wbc
->sync_mode
!= WB_SYNC_NONE
)
1285 f2fs_wait_on_page_writeback(page
, DATA
);
1287 goto continue_unlock
;
1290 BUG_ON(PageWriteback(page
));
1291 if (!clear_page_dirty_for_io(page
))
1292 goto continue_unlock
;
1294 ret
= (*writepage
)(page
, wbc
, data
);
1295 if (unlikely(ret
)) {
1296 if (ret
== AOP_WRITEPAGE_ACTIVATE
) {
1300 done_index
= page
->index
+ 1;
1306 if (--wbc
->nr_to_write
<= 0 &&
1307 wbc
->sync_mode
== WB_SYNC_NONE
) {
1312 pagevec_release(&pvec
);
1321 if (!cycled
&& !done
) {
1324 end
= writeback_index
- 1;
1327 if (wbc
->range_cyclic
|| (range_whole
&& wbc
->nr_to_write
> 0))
1328 mapping
->writeback_index
= done_index
;
1333 static int f2fs_write_data_pages(struct address_space
*mapping
,
1334 struct writeback_control
*wbc
)
1336 struct inode
*inode
= mapping
->host
;
1337 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1338 bool locked
= false;
1342 trace_f2fs_writepages(mapping
->host
, wbc
, DATA
);
1344 /* deal with chardevs and other special file */
1345 if (!mapping
->a_ops
->writepage
)
1348 /* skip writing if there is no dirty page in this inode */
1349 if (!get_dirty_pages(inode
) && wbc
->sync_mode
== WB_SYNC_NONE
)
1352 if (S_ISDIR(inode
->i_mode
) && wbc
->sync_mode
== WB_SYNC_NONE
&&
1353 get_dirty_pages(inode
) < nr_pages_to_skip(sbi
, DATA
) &&
1354 available_free_memory(sbi
, DIRTY_DENTS
))
1357 /* during POR, we don't need to trigger writepage at all. */
1358 if (unlikely(is_sbi_flag_set(sbi
, SBI_POR_DOING
)))
1361 diff
= nr_pages_to_write(sbi
, DATA
, wbc
);
1363 if (!S_ISDIR(inode
->i_mode
)) {
1364 mutex_lock(&sbi
->writepages
);
1367 ret
= f2fs_write_cache_pages(mapping
, wbc
, __f2fs_writepage
, mapping
);
1368 f2fs_submit_merged_bio(sbi
, DATA
, WRITE
);
1370 mutex_unlock(&sbi
->writepages
);
1372 remove_dirty_dir_inode(inode
);
1374 wbc
->nr_to_write
= max((long)0, wbc
->nr_to_write
- diff
);
1378 wbc
->pages_skipped
+= get_dirty_pages(inode
);
1382 static void f2fs_write_failed(struct address_space
*mapping
, loff_t to
)
1384 struct inode
*inode
= mapping
->host
;
1386 if (to
> inode
->i_size
) {
1387 truncate_pagecache(inode
, inode
->i_size
);
1388 truncate_blocks(inode
, inode
->i_size
, true);
1392 static int f2fs_write_begin(struct file
*file
, struct address_space
*mapping
,
1393 loff_t pos
, unsigned len
, unsigned flags
,
1394 struct page
**pagep
, void **fsdata
)
1396 struct inode
*inode
= mapping
->host
;
1397 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1398 struct page
*page
= NULL
;
1400 pgoff_t index
= ((unsigned long long) pos
) >> PAGE_CACHE_SHIFT
;
1401 struct dnode_of_data dn
;
1404 trace_f2fs_write_begin(inode
, pos
, len
, flags
);
1406 f2fs_balance_fs(sbi
);
1409 * We should check this at this moment to avoid deadlock on inode page
1410 * and #0 page. The locking rule for inline_data conversion should be:
1411 * lock_page(page #0) -> lock_page(inode_page)
1414 err
= f2fs_convert_inline_inode(inode
);
1420 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
1421 * wait_for_stable_page. Will wait that below with our IO control.
1423 page
= pagecache_get_page(mapping
, index
,
1424 FGP_LOCK
| FGP_WRITE
| FGP_CREAT
, GFP_NOFS
);
1434 /* check inline_data */
1435 ipage
= get_node_page(sbi
, inode
->i_ino
);
1436 if (IS_ERR(ipage
)) {
1437 err
= PTR_ERR(ipage
);
1441 set_new_dnode(&dn
, inode
, ipage
, ipage
, 0);
1443 if (f2fs_has_inline_data(inode
)) {
1444 if (pos
+ len
<= MAX_INLINE_DATA
) {
1445 read_inline_data(page
, ipage
);
1446 set_inode_flag(F2FS_I(inode
), FI_DATA_EXIST
);
1447 sync_inode_page(&dn
);
1450 err
= f2fs_convert_inline_page(&dn
, page
);
1455 err
= f2fs_get_block(&dn
, index
);
1459 f2fs_put_dnode(&dn
);
1460 f2fs_unlock_op(sbi
);
1462 f2fs_wait_on_page_writeback(page
, DATA
);
1464 /* wait for GCed encrypted page writeback */
1465 if (f2fs_encrypted_inode(inode
) && S_ISREG(inode
->i_mode
))
1466 f2fs_wait_on_encrypted_page_writeback(sbi
, dn
.data_blkaddr
);
1468 if (len
== PAGE_CACHE_SIZE
)
1470 if (PageUptodate(page
))
1473 if ((pos
& PAGE_CACHE_MASK
) >= i_size_read(inode
)) {
1474 unsigned start
= pos
& (PAGE_CACHE_SIZE
- 1);
1475 unsigned end
= start
+ len
;
1477 /* Reading beyond i_size is simple: memset to zero */
1478 zero_user_segments(page
, 0, start
, end
, PAGE_CACHE_SIZE
);
1482 if (dn
.data_blkaddr
== NEW_ADDR
) {
1483 zero_user_segment(page
, 0, PAGE_CACHE_SIZE
);
1485 struct f2fs_io_info fio
= {
1489 .blk_addr
= dn
.data_blkaddr
,
1491 .encrypted_page
= NULL
,
1493 err
= f2fs_submit_page_bio(&fio
);
1498 if (unlikely(!PageUptodate(page
))) {
1502 if (unlikely(page
->mapping
!= mapping
)) {
1503 f2fs_put_page(page
, 1);
1507 /* avoid symlink page */
1508 if (f2fs_encrypted_inode(inode
) && S_ISREG(inode
->i_mode
)) {
1509 err
= f2fs_decrypt_one(inode
, page
);
1515 SetPageUptodate(page
);
1517 clear_cold_data(page
);
1521 f2fs_put_dnode(&dn
);
1523 f2fs_unlock_op(sbi
);
1525 f2fs_put_page(page
, 1);
1526 f2fs_write_failed(mapping
, pos
+ len
);
1530 static int f2fs_write_end(struct file
*file
,
1531 struct address_space
*mapping
,
1532 loff_t pos
, unsigned len
, unsigned copied
,
1533 struct page
*page
, void *fsdata
)
1535 struct inode
*inode
= page
->mapping
->host
;
1537 trace_f2fs_write_end(inode
, pos
, len
, copied
);
1539 set_page_dirty(page
);
1541 if (pos
+ copied
> i_size_read(inode
)) {
1542 i_size_write(inode
, pos
+ copied
);
1543 mark_inode_dirty(inode
);
1544 update_inode_page(inode
);
1547 f2fs_put_page(page
, 1);
1551 static int check_direct_IO(struct inode
*inode
, struct iov_iter
*iter
,
1554 unsigned blocksize_mask
= inode
->i_sb
->s_blocksize
- 1;
1556 if (offset
& blocksize_mask
)
1559 if (iov_iter_alignment(iter
) & blocksize_mask
)
1565 static ssize_t
f2fs_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
,
1568 struct file
*file
= iocb
->ki_filp
;
1569 struct address_space
*mapping
= file
->f_mapping
;
1570 struct inode
*inode
= mapping
->host
;
1571 size_t count
= iov_iter_count(iter
);
1574 /* we don't need to use inline_data strictly */
1575 if (f2fs_has_inline_data(inode
)) {
1576 err
= f2fs_convert_inline_inode(inode
);
1581 if (f2fs_encrypted_inode(inode
) && S_ISREG(inode
->i_mode
))
1584 err
= check_direct_IO(inode
, iter
, offset
);
1588 trace_f2fs_direct_IO_enter(inode
, offset
, count
, iov_iter_rw(iter
));
1590 if (iov_iter_rw(iter
) == WRITE
) {
1591 __allocate_data_blocks(inode
, offset
, count
);
1592 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode
)))) {
1598 err
= blockdev_direct_IO(iocb
, inode
, iter
, offset
, get_data_block_dio
);
1600 if (err
< 0 && iov_iter_rw(iter
) == WRITE
)
1601 f2fs_write_failed(mapping
, offset
+ count
);
1603 trace_f2fs_direct_IO_exit(inode
, offset
, count
, iov_iter_rw(iter
), err
);
1608 void f2fs_invalidate_page(struct page
*page
, unsigned int offset
,
1609 unsigned int length
)
1611 struct inode
*inode
= page
->mapping
->host
;
1612 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1614 if (inode
->i_ino
>= F2FS_ROOT_INO(sbi
) &&
1615 (offset
% PAGE_CACHE_SIZE
|| length
!= PAGE_CACHE_SIZE
))
1618 if (PageDirty(page
)) {
1619 if (inode
->i_ino
== F2FS_META_INO(sbi
))
1620 dec_page_count(sbi
, F2FS_DIRTY_META
);
1621 else if (inode
->i_ino
== F2FS_NODE_INO(sbi
))
1622 dec_page_count(sbi
, F2FS_DIRTY_NODES
);
1624 inode_dec_dirty_pages(inode
);
1627 /* This is atomic written page, keep Private */
1628 if (IS_ATOMIC_WRITTEN_PAGE(page
))
1631 ClearPagePrivate(page
);
1634 int f2fs_release_page(struct page
*page
, gfp_t wait
)
1636 /* If this is dirty page, keep PagePrivate */
1637 if (PageDirty(page
))
1640 /* This is atomic written page, keep Private */
1641 if (IS_ATOMIC_WRITTEN_PAGE(page
))
1644 ClearPagePrivate(page
);
1648 static int f2fs_set_data_page_dirty(struct page
*page
)
1650 struct address_space
*mapping
= page
->mapping
;
1651 struct inode
*inode
= mapping
->host
;
1653 trace_f2fs_set_page_dirty(page
, DATA
);
1655 SetPageUptodate(page
);
1657 if (f2fs_is_atomic_file(inode
)) {
1658 if (!IS_ATOMIC_WRITTEN_PAGE(page
)) {
1659 register_inmem_page(inode
, page
);
1663 * Previously, this page has been registered, we just
1669 if (!PageDirty(page
)) {
1670 __set_page_dirty_nobuffers(page
);
1671 update_dirty_page(inode
, page
);
1677 static sector_t
f2fs_bmap(struct address_space
*mapping
, sector_t block
)
1679 struct inode
*inode
= mapping
->host
;
1681 if (f2fs_has_inline_data(inode
))
1684 /* make sure allocating whole blocks */
1685 if (mapping_tagged(mapping
, PAGECACHE_TAG_DIRTY
))
1686 filemap_write_and_wait(mapping
);
1688 return generic_block_bmap(mapping
, block
, get_data_block_bmap
);
1691 const struct address_space_operations f2fs_dblock_aops
= {
1692 .readpage
= f2fs_read_data_page
,
1693 .readpages
= f2fs_read_data_pages
,
1694 .writepage
= f2fs_write_data_page
,
1695 .writepages
= f2fs_write_data_pages
,
1696 .write_begin
= f2fs_write_begin
,
1697 .write_end
= f2fs_write_end
,
1698 .set_page_dirty
= f2fs_set_data_page_dirty
,
1699 .invalidatepage
= f2fs_invalidate_page
,
1700 .releasepage
= f2fs_release_page
,
1701 .direct_IO
= f2fs_direct_IO
,