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>
23 #include <linux/memcontrol.h>
24 #include <linux/cleancache.h>
30 #include <trace/events/f2fs.h>
32 static void f2fs_read_end_io(struct bio
*bio
)
37 #ifdef CONFIG_F2FS_FAULT_INJECTION
38 if (time_to_inject(F2FS_P_SB(bio
->bi_io_vec
->bv_page
), FAULT_IO
))
42 if (f2fs_bio_encrypted(bio
)) {
44 fscrypt_release_ctx(bio
->bi_private
);
46 fscrypt_decrypt_bio_pages(bio
->bi_private
, bio
);
51 bio_for_each_segment_all(bvec
, bio
, i
) {
52 struct page
*page
= bvec
->bv_page
;
55 if (!PageUptodate(page
))
56 SetPageUptodate(page
);
58 ClearPageUptodate(page
);
66 static void f2fs_write_end_io(struct bio
*bio
)
68 struct f2fs_sb_info
*sbi
= bio
->bi_private
;
72 bio_for_each_segment_all(bvec
, bio
, i
) {
73 struct page
*page
= bvec
->bv_page
;
75 fscrypt_pullback_bio_page(&page
, true);
77 if (unlikely(bio
->bi_error
)) {
78 mapping_set_error(page
->mapping
, -EIO
);
79 f2fs_stop_checkpoint(sbi
, true);
81 end_page_writeback(page
);
83 if (atomic_dec_and_test(&sbi
->nr_wb_bios
) &&
84 wq_has_sleeper(&sbi
->cp_wait
))
85 wake_up(&sbi
->cp_wait
);
91 * Low-level block read/write IO operations.
93 static struct bio
*__bio_alloc(struct f2fs_sb_info
*sbi
, block_t blk_addr
,
94 int npages
, bool is_read
)
98 bio
= f2fs_bio_alloc(npages
);
100 bio
->bi_bdev
= sbi
->sb
->s_bdev
;
101 bio
->bi_iter
.bi_sector
= SECTOR_FROM_BLOCK(blk_addr
);
102 bio
->bi_end_io
= is_read
? f2fs_read_end_io
: f2fs_write_end_io
;
103 bio
->bi_private
= is_read
? NULL
: sbi
;
108 static inline void __submit_bio(struct f2fs_sb_info
*sbi
,
109 struct bio
*bio
, enum page_type type
)
111 if (!is_read_io(bio_op(bio
))) {
112 atomic_inc(&sbi
->nr_wb_bios
);
113 if (f2fs_sb_mounted_hmsmr(sbi
->sb
) &&
114 current
->plug
&& (type
== DATA
|| type
== NODE
))
115 blk_finish_plug(current
->plug
);
120 static void __submit_merged_bio(struct f2fs_bio_info
*io
)
122 struct f2fs_io_info
*fio
= &io
->fio
;
127 if (is_read_io(fio
->op
))
128 trace_f2fs_submit_read_bio(io
->sbi
->sb
, fio
, io
->bio
);
130 trace_f2fs_submit_write_bio(io
->sbi
->sb
, fio
, io
->bio
);
132 bio_set_op_attrs(io
->bio
, fio
->op
, fio
->op_flags
);
134 __submit_bio(io
->sbi
, io
->bio
, fio
->type
);
138 static bool __has_merged_page(struct f2fs_bio_info
*io
, struct inode
*inode
,
139 struct page
*page
, nid_t ino
)
141 struct bio_vec
*bvec
;
148 if (!inode
&& !page
&& !ino
)
151 bio_for_each_segment_all(bvec
, io
->bio
, i
) {
153 if (bvec
->bv_page
->mapping
)
154 target
= bvec
->bv_page
;
156 target
= fscrypt_control_page(bvec
->bv_page
);
158 if (inode
&& inode
== target
->mapping
->host
)
160 if (page
&& page
== target
)
162 if (ino
&& ino
== ino_of_node(target
))
169 static bool has_merged_page(struct f2fs_sb_info
*sbi
, struct inode
*inode
,
170 struct page
*page
, nid_t ino
,
173 enum page_type btype
= PAGE_TYPE_OF_BIO(type
);
174 struct f2fs_bio_info
*io
= &sbi
->write_io
[btype
];
177 down_read(&io
->io_rwsem
);
178 ret
= __has_merged_page(io
, inode
, page
, ino
);
179 up_read(&io
->io_rwsem
);
183 static void __f2fs_submit_merged_bio(struct f2fs_sb_info
*sbi
,
184 struct inode
*inode
, struct page
*page
,
185 nid_t ino
, enum page_type type
, int rw
)
187 enum page_type btype
= PAGE_TYPE_OF_BIO(type
);
188 struct f2fs_bio_info
*io
;
190 io
= is_read_io(rw
) ? &sbi
->read_io
: &sbi
->write_io
[btype
];
192 down_write(&io
->io_rwsem
);
194 if (!__has_merged_page(io
, inode
, page
, ino
))
197 /* change META to META_FLUSH in the checkpoint procedure */
198 if (type
>= META_FLUSH
) {
199 io
->fio
.type
= META_FLUSH
;
200 io
->fio
.op
= REQ_OP_WRITE
;
201 if (test_opt(sbi
, NOBARRIER
))
202 io
->fio
.op_flags
= WRITE_FLUSH
| REQ_META
| REQ_PRIO
;
204 io
->fio
.op_flags
= WRITE_FLUSH_FUA
| REQ_META
|
207 __submit_merged_bio(io
);
209 up_write(&io
->io_rwsem
);
212 void f2fs_submit_merged_bio(struct f2fs_sb_info
*sbi
, enum page_type type
,
215 __f2fs_submit_merged_bio(sbi
, NULL
, NULL
, 0, type
, rw
);
218 void f2fs_submit_merged_bio_cond(struct f2fs_sb_info
*sbi
,
219 struct inode
*inode
, struct page
*page
,
220 nid_t ino
, enum page_type type
, int rw
)
222 if (has_merged_page(sbi
, inode
, page
, ino
, type
))
223 __f2fs_submit_merged_bio(sbi
, inode
, page
, ino
, type
, rw
);
226 void f2fs_flush_merged_bios(struct f2fs_sb_info
*sbi
)
228 f2fs_submit_merged_bio(sbi
, DATA
, WRITE
);
229 f2fs_submit_merged_bio(sbi
, NODE
, WRITE
);
230 f2fs_submit_merged_bio(sbi
, META
, WRITE
);
234 * Fill the locked page with data located in the block address.
235 * Return unlocked page.
237 int f2fs_submit_page_bio(struct f2fs_io_info
*fio
)
240 struct page
*page
= fio
->encrypted_page
?
241 fio
->encrypted_page
: fio
->page
;
243 trace_f2fs_submit_page_bio(page
, fio
);
244 f2fs_trace_ios(fio
, 0);
246 /* Allocate a new bio */
247 bio
= __bio_alloc(fio
->sbi
, fio
->new_blkaddr
, 1, is_read_io(fio
->op
));
249 if (bio_add_page(bio
, page
, PAGE_SIZE
, 0) < PAGE_SIZE
) {
253 bio_set_op_attrs(bio
, fio
->op
, fio
->op_flags
);
255 __submit_bio(fio
->sbi
, bio
, fio
->type
);
259 void f2fs_submit_page_mbio(struct f2fs_io_info
*fio
)
261 struct f2fs_sb_info
*sbi
= fio
->sbi
;
262 enum page_type btype
= PAGE_TYPE_OF_BIO(fio
->type
);
263 struct f2fs_bio_info
*io
;
264 bool is_read
= is_read_io(fio
->op
);
265 struct page
*bio_page
;
267 io
= is_read
? &sbi
->read_io
: &sbi
->write_io
[btype
];
269 if (fio
->old_blkaddr
!= NEW_ADDR
)
270 verify_block_addr(sbi
, fio
->old_blkaddr
);
271 verify_block_addr(sbi
, fio
->new_blkaddr
);
273 down_write(&io
->io_rwsem
);
275 if (io
->bio
&& (io
->last_block_in_bio
!= fio
->new_blkaddr
- 1 ||
276 (io
->fio
.op
!= fio
->op
|| io
->fio
.op_flags
!= fio
->op_flags
)))
277 __submit_merged_bio(io
);
279 if (io
->bio
== NULL
) {
280 int bio_blocks
= MAX_BIO_BLOCKS(sbi
);
282 io
->bio
= __bio_alloc(sbi
, fio
->new_blkaddr
,
283 bio_blocks
, is_read
);
287 bio_page
= fio
->encrypted_page
? fio
->encrypted_page
: fio
->page
;
289 if (bio_add_page(io
->bio
, bio_page
, PAGE_SIZE
, 0) <
291 __submit_merged_bio(io
);
295 io
->last_block_in_bio
= fio
->new_blkaddr
;
296 f2fs_trace_ios(fio
, 0);
298 up_write(&io
->io_rwsem
);
299 trace_f2fs_submit_page_mbio(fio
->page
, fio
);
302 static void __set_data_blkaddr(struct dnode_of_data
*dn
)
304 struct f2fs_node
*rn
= F2FS_NODE(dn
->node_page
);
307 /* Get physical address of data block */
308 addr_array
= blkaddr_in_node(rn
);
309 addr_array
[dn
->ofs_in_node
] = cpu_to_le32(dn
->data_blkaddr
);
313 * Lock ordering for the change of data block address:
316 * update block addresses in the node page
318 void set_data_blkaddr(struct dnode_of_data
*dn
)
320 f2fs_wait_on_page_writeback(dn
->node_page
, NODE
, true);
321 __set_data_blkaddr(dn
);
322 if (set_page_dirty(dn
->node_page
))
323 dn
->node_changed
= true;
326 void f2fs_update_data_blkaddr(struct dnode_of_data
*dn
, block_t blkaddr
)
328 dn
->data_blkaddr
= blkaddr
;
329 set_data_blkaddr(dn
);
330 f2fs_update_extent_cache(dn
);
333 /* dn->ofs_in_node will be returned with up-to-date last block pointer */
334 int reserve_new_blocks(struct dnode_of_data
*dn
, blkcnt_t count
)
336 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
341 if (unlikely(is_inode_flag_set(dn
->inode
, FI_NO_ALLOC
)))
343 if (unlikely(!inc_valid_block_count(sbi
, dn
->inode
, &count
)))
346 trace_f2fs_reserve_new_blocks(dn
->inode
, dn
->nid
,
347 dn
->ofs_in_node
, count
);
349 f2fs_wait_on_page_writeback(dn
->node_page
, NODE
, true);
351 for (; count
> 0; dn
->ofs_in_node
++) {
353 datablock_addr(dn
->node_page
, dn
->ofs_in_node
);
354 if (blkaddr
== NULL_ADDR
) {
355 dn
->data_blkaddr
= NEW_ADDR
;
356 __set_data_blkaddr(dn
);
361 if (set_page_dirty(dn
->node_page
))
362 dn
->node_changed
= true;
366 /* Should keep dn->ofs_in_node unchanged */
367 int reserve_new_block(struct dnode_of_data
*dn
)
369 unsigned int ofs_in_node
= dn
->ofs_in_node
;
372 ret
= reserve_new_blocks(dn
, 1);
373 dn
->ofs_in_node
= ofs_in_node
;
377 int f2fs_reserve_block(struct dnode_of_data
*dn
, pgoff_t index
)
379 bool need_put
= dn
->inode_page
? false : true;
382 err
= get_dnode_of_data(dn
, index
, ALLOC_NODE
);
386 if (dn
->data_blkaddr
== NULL_ADDR
)
387 err
= reserve_new_block(dn
);
393 int f2fs_get_block(struct dnode_of_data
*dn
, pgoff_t index
)
395 struct extent_info ei
;
396 struct inode
*inode
= dn
->inode
;
398 if (f2fs_lookup_extent_cache(inode
, index
, &ei
)) {
399 dn
->data_blkaddr
= ei
.blk
+ index
- ei
.fofs
;
403 return f2fs_reserve_block(dn
, index
);
406 struct page
*get_read_data_page(struct inode
*inode
, pgoff_t index
,
407 int op_flags
, bool for_write
)
409 struct address_space
*mapping
= inode
->i_mapping
;
410 struct dnode_of_data dn
;
412 struct extent_info ei
;
414 struct f2fs_io_info fio
= {
415 .sbi
= F2FS_I_SB(inode
),
418 .op_flags
= op_flags
,
419 .encrypted_page
= NULL
,
422 if (f2fs_encrypted_inode(inode
) && S_ISREG(inode
->i_mode
))
423 return read_mapping_page(mapping
, index
, NULL
);
425 page
= f2fs_grab_cache_page(mapping
, index
, for_write
);
427 return ERR_PTR(-ENOMEM
);
429 if (f2fs_lookup_extent_cache(inode
, index
, &ei
)) {
430 dn
.data_blkaddr
= ei
.blk
+ index
- ei
.fofs
;
434 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
435 err
= get_dnode_of_data(&dn
, index
, LOOKUP_NODE
);
440 if (unlikely(dn
.data_blkaddr
== NULL_ADDR
)) {
445 if (PageUptodate(page
)) {
451 * A new dentry page is allocated but not able to be written, since its
452 * new inode page couldn't be allocated due to -ENOSPC.
453 * In such the case, its blkaddr can be remained as NEW_ADDR.
454 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
456 if (dn
.data_blkaddr
== NEW_ADDR
) {
457 zero_user_segment(page
, 0, PAGE_SIZE
);
458 if (!PageUptodate(page
))
459 SetPageUptodate(page
);
464 fio
.new_blkaddr
= fio
.old_blkaddr
= dn
.data_blkaddr
;
466 err
= f2fs_submit_page_bio(&fio
);
472 f2fs_put_page(page
, 1);
476 struct page
*find_data_page(struct inode
*inode
, pgoff_t index
)
478 struct address_space
*mapping
= inode
->i_mapping
;
481 page
= find_get_page(mapping
, index
);
482 if (page
&& PageUptodate(page
))
484 f2fs_put_page(page
, 0);
486 page
= get_read_data_page(inode
, index
, READ_SYNC
, false);
490 if (PageUptodate(page
))
493 wait_on_page_locked(page
);
494 if (unlikely(!PageUptodate(page
))) {
495 f2fs_put_page(page
, 0);
496 return ERR_PTR(-EIO
);
502 * If it tries to access a hole, return an error.
503 * Because, the callers, functions in dir.c and GC, should be able to know
504 * whether this page exists or not.
506 struct page
*get_lock_data_page(struct inode
*inode
, pgoff_t index
,
509 struct address_space
*mapping
= inode
->i_mapping
;
512 page
= get_read_data_page(inode
, index
, READ_SYNC
, for_write
);
516 /* wait for read completion */
518 if (unlikely(page
->mapping
!= mapping
)) {
519 f2fs_put_page(page
, 1);
522 if (unlikely(!PageUptodate(page
))) {
523 f2fs_put_page(page
, 1);
524 return ERR_PTR(-EIO
);
530 * Caller ensures that this data page is never allocated.
531 * A new zero-filled data page is allocated in the page cache.
533 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
535 * Note that, ipage is set only by make_empty_dir, and if any error occur,
536 * ipage should be released by this function.
538 struct page
*get_new_data_page(struct inode
*inode
,
539 struct page
*ipage
, pgoff_t index
, bool new_i_size
)
541 struct address_space
*mapping
= inode
->i_mapping
;
543 struct dnode_of_data dn
;
546 page
= f2fs_grab_cache_page(mapping
, index
, true);
549 * before exiting, we should make sure ipage will be released
550 * if any error occur.
552 f2fs_put_page(ipage
, 1);
553 return ERR_PTR(-ENOMEM
);
556 set_new_dnode(&dn
, inode
, ipage
, NULL
, 0);
557 err
= f2fs_reserve_block(&dn
, index
);
559 f2fs_put_page(page
, 1);
565 if (PageUptodate(page
))
568 if (dn
.data_blkaddr
== NEW_ADDR
) {
569 zero_user_segment(page
, 0, PAGE_SIZE
);
570 if (!PageUptodate(page
))
571 SetPageUptodate(page
);
573 f2fs_put_page(page
, 1);
575 /* if ipage exists, blkaddr should be NEW_ADDR */
576 f2fs_bug_on(F2FS_I_SB(inode
), ipage
);
577 page
= get_lock_data_page(inode
, index
, true);
582 if (new_i_size
&& i_size_read(inode
) <
583 ((loff_t
)(index
+ 1) << PAGE_SHIFT
))
584 f2fs_i_size_write(inode
, ((loff_t
)(index
+ 1) << PAGE_SHIFT
));
588 static int __allocate_data_block(struct dnode_of_data
*dn
)
590 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
591 struct f2fs_summary sum
;
593 int seg
= CURSEG_WARM_DATA
;
597 if (unlikely(is_inode_flag_set(dn
->inode
, FI_NO_ALLOC
)))
600 dn
->data_blkaddr
= datablock_addr(dn
->node_page
, dn
->ofs_in_node
);
601 if (dn
->data_blkaddr
== NEW_ADDR
)
604 if (unlikely(!inc_valid_block_count(sbi
, dn
->inode
, &count
)))
608 get_node_info(sbi
, dn
->nid
, &ni
);
609 set_summary(&sum
, dn
->nid
, dn
->ofs_in_node
, ni
.version
);
611 if (dn
->ofs_in_node
== 0 && dn
->inode_page
== dn
->node_page
)
612 seg
= CURSEG_DIRECT_IO
;
614 allocate_data_block(sbi
, NULL
, dn
->data_blkaddr
, &dn
->data_blkaddr
,
616 set_data_blkaddr(dn
);
619 fofs
= start_bidx_of_node(ofs_of_node(dn
->node_page
), dn
->inode
) +
621 if (i_size_read(dn
->inode
) < ((loff_t
)(fofs
+ 1) << PAGE_SHIFT
))
622 f2fs_i_size_write(dn
->inode
,
623 ((loff_t
)(fofs
+ 1) << PAGE_SHIFT
));
627 ssize_t
f2fs_preallocate_blocks(struct kiocb
*iocb
, struct iov_iter
*from
)
629 struct inode
*inode
= file_inode(iocb
->ki_filp
);
630 struct f2fs_map_blocks map
;
633 map
.m_lblk
= F2FS_BLK_ALIGN(iocb
->ki_pos
);
634 map
.m_len
= F2FS_BYTES_TO_BLK(iocb
->ki_pos
+ iov_iter_count(from
));
635 if (map
.m_len
> map
.m_lblk
)
636 map
.m_len
-= map
.m_lblk
;
640 map
.m_next_pgofs
= NULL
;
642 if (iocb
->ki_flags
& IOCB_DIRECT
) {
643 ret
= f2fs_convert_inline_inode(inode
);
646 return f2fs_map_blocks(inode
, &map
, 1, F2FS_GET_BLOCK_PRE_DIO
);
648 if (iocb
->ki_pos
+ iov_iter_count(from
) > MAX_INLINE_DATA
) {
649 ret
= f2fs_convert_inline_inode(inode
);
653 if (!f2fs_has_inline_data(inode
))
654 return f2fs_map_blocks(inode
, &map
, 1, F2FS_GET_BLOCK_PRE_AIO
);
659 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
660 * f2fs_map_blocks structure.
661 * If original data blocks are allocated, then give them to blockdev.
663 * a. preallocate requested block addresses
664 * b. do not use extent cache for better performance
665 * c. give the block addresses to blockdev
667 int f2fs_map_blocks(struct inode
*inode
, struct f2fs_map_blocks
*map
,
668 int create
, int flag
)
670 unsigned int maxblocks
= map
->m_len
;
671 struct dnode_of_data dn
;
672 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
673 int mode
= create
? ALLOC_NODE
: LOOKUP_NODE
;
674 pgoff_t pgofs
, end_offset
, end
;
675 int err
= 0, ofs
= 1;
676 unsigned int ofs_in_node
, last_ofs_in_node
;
678 struct extent_info ei
;
679 bool allocated
= false;
688 /* it only supports block size == page size */
689 pgofs
= (pgoff_t
)map
->m_lblk
;
690 end
= pgofs
+ maxblocks
;
692 if (!create
&& f2fs_lookup_extent_cache(inode
, pgofs
, &ei
)) {
693 map
->m_pblk
= ei
.blk
+ pgofs
- ei
.fofs
;
694 map
->m_len
= min((pgoff_t
)maxblocks
, ei
.fofs
+ ei
.len
- pgofs
);
695 map
->m_flags
= F2FS_MAP_MAPPED
;
703 /* When reading holes, we need its node page */
704 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
705 err
= get_dnode_of_data(&dn
, pgofs
, mode
);
707 if (flag
== F2FS_GET_BLOCK_BMAP
)
709 if (err
== -ENOENT
) {
711 if (map
->m_next_pgofs
)
713 get_next_page_offset(&dn
, pgofs
);
719 last_ofs_in_node
= ofs_in_node
= dn
.ofs_in_node
;
720 end_offset
= ADDRS_PER_PAGE(dn
.node_page
, inode
);
723 blkaddr
= datablock_addr(dn
.node_page
, dn
.ofs_in_node
);
725 if (blkaddr
== NEW_ADDR
|| blkaddr
== NULL_ADDR
) {
727 if (unlikely(f2fs_cp_error(sbi
))) {
731 if (flag
== F2FS_GET_BLOCK_PRE_AIO
) {
732 if (blkaddr
== NULL_ADDR
) {
734 last_ofs_in_node
= dn
.ofs_in_node
;
737 err
= __allocate_data_block(&dn
);
739 set_inode_flag(inode
, FI_APPEND_WRITE
);
745 map
->m_flags
= F2FS_MAP_NEW
;
746 blkaddr
= dn
.data_blkaddr
;
748 if (flag
== F2FS_GET_BLOCK_BMAP
) {
752 if (flag
== F2FS_GET_BLOCK_FIEMAP
&&
753 blkaddr
== NULL_ADDR
) {
754 if (map
->m_next_pgofs
)
755 *map
->m_next_pgofs
= pgofs
+ 1;
757 if (flag
!= F2FS_GET_BLOCK_FIEMAP
||
763 if (flag
== F2FS_GET_BLOCK_PRE_AIO
)
766 if (map
->m_len
== 0) {
767 /* preallocated unwritten block should be mapped for fiemap. */
768 if (blkaddr
== NEW_ADDR
)
769 map
->m_flags
|= F2FS_MAP_UNWRITTEN
;
770 map
->m_flags
|= F2FS_MAP_MAPPED
;
772 map
->m_pblk
= blkaddr
;
774 } else if ((map
->m_pblk
!= NEW_ADDR
&&
775 blkaddr
== (map
->m_pblk
+ ofs
)) ||
776 (map
->m_pblk
== NEW_ADDR
&& blkaddr
== NEW_ADDR
) ||
777 flag
== F2FS_GET_BLOCK_PRE_DIO
) {
788 /* preallocate blocks in batch for one dnode page */
789 if (flag
== F2FS_GET_BLOCK_PRE_AIO
&&
790 (pgofs
== end
|| dn
.ofs_in_node
== end_offset
)) {
792 dn
.ofs_in_node
= ofs_in_node
;
793 err
= reserve_new_blocks(&dn
, prealloc
);
796 allocated
= dn
.node_changed
;
798 map
->m_len
+= dn
.ofs_in_node
- ofs_in_node
;
799 if (prealloc
&& dn
.ofs_in_node
!= last_ofs_in_node
+ 1) {
803 dn
.ofs_in_node
= end_offset
;
808 else if (dn
.ofs_in_node
< end_offset
)
815 f2fs_balance_fs(sbi
, allocated
);
825 f2fs_balance_fs(sbi
, allocated
);
828 trace_f2fs_map_blocks(inode
, map
, err
);
832 static int __get_data_block(struct inode
*inode
, sector_t iblock
,
833 struct buffer_head
*bh
, int create
, int flag
,
836 struct f2fs_map_blocks map
;
840 map
.m_len
= bh
->b_size
>> inode
->i_blkbits
;
841 map
.m_next_pgofs
= next_pgofs
;
843 ret
= f2fs_map_blocks(inode
, &map
, create
, flag
);
845 map_bh(bh
, inode
->i_sb
, map
.m_pblk
);
846 bh
->b_state
= (bh
->b_state
& ~F2FS_MAP_FLAGS
) | map
.m_flags
;
847 bh
->b_size
= (u64
)map
.m_len
<< inode
->i_blkbits
;
852 static int get_data_block(struct inode
*inode
, sector_t iblock
,
853 struct buffer_head
*bh_result
, int create
, int flag
,
856 return __get_data_block(inode
, iblock
, bh_result
, create
,
860 static int get_data_block_dio(struct inode
*inode
, sector_t iblock
,
861 struct buffer_head
*bh_result
, int create
)
863 return __get_data_block(inode
, iblock
, bh_result
, create
,
864 F2FS_GET_BLOCK_DIO
, NULL
);
867 static int get_data_block_bmap(struct inode
*inode
, sector_t iblock
,
868 struct buffer_head
*bh_result
, int create
)
870 /* Block number less than F2FS MAX BLOCKS */
871 if (unlikely(iblock
>= F2FS_I_SB(inode
)->max_file_blocks
))
874 return __get_data_block(inode
, iblock
, bh_result
, create
,
875 F2FS_GET_BLOCK_BMAP
, NULL
);
878 static inline sector_t
logical_to_blk(struct inode
*inode
, loff_t offset
)
880 return (offset
>> inode
->i_blkbits
);
883 static inline loff_t
blk_to_logical(struct inode
*inode
, sector_t blk
)
885 return (blk
<< inode
->i_blkbits
);
888 int f2fs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
891 struct buffer_head map_bh
;
892 sector_t start_blk
, last_blk
;
895 u64 logical
= 0, phys
= 0, size
= 0;
899 ret
= fiemap_check_flags(fieinfo
, FIEMAP_FLAG_SYNC
);
903 if (f2fs_has_inline_data(inode
)) {
904 ret
= f2fs_inline_data_fiemap(inode
, fieinfo
, start
, len
);
911 isize
= i_size_read(inode
);
915 if (start
+ len
> isize
)
918 if (logical_to_blk(inode
, len
) == 0)
919 len
= blk_to_logical(inode
, 1);
921 start_blk
= logical_to_blk(inode
, start
);
922 last_blk
= logical_to_blk(inode
, start
+ len
- 1);
925 memset(&map_bh
, 0, sizeof(struct buffer_head
));
928 ret
= get_data_block(inode
, start_blk
, &map_bh
, 0,
929 F2FS_GET_BLOCK_FIEMAP
, &next_pgofs
);
934 if (!buffer_mapped(&map_bh
)) {
935 start_blk
= next_pgofs
;
936 /* Go through holes util pass the EOF */
937 if (blk_to_logical(inode
, start_blk
) < isize
)
939 /* Found a hole beyond isize means no more extents.
940 * Note that the premise is that filesystems don't
941 * punch holes beyond isize and keep size unchanged.
943 flags
|= FIEMAP_EXTENT_LAST
;
947 if (f2fs_encrypted_inode(inode
))
948 flags
|= FIEMAP_EXTENT_DATA_ENCRYPTED
;
950 ret
= fiemap_fill_next_extent(fieinfo
, logical
,
954 if (start_blk
> last_blk
|| ret
)
957 logical
= blk_to_logical(inode
, start_blk
);
958 phys
= blk_to_logical(inode
, map_bh
.b_blocknr
);
959 size
= map_bh
.b_size
;
961 if (buffer_unwritten(&map_bh
))
962 flags
= FIEMAP_EXTENT_UNWRITTEN
;
964 start_blk
+= logical_to_blk(inode
, size
);
968 if (fatal_signal_pending(current
))
980 static struct bio
*f2fs_grab_bio(struct inode
*inode
, block_t blkaddr
,
983 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
984 struct fscrypt_ctx
*ctx
= NULL
;
985 struct block_device
*bdev
= sbi
->sb
->s_bdev
;
988 if (f2fs_encrypted_inode(inode
) && S_ISREG(inode
->i_mode
)) {
989 ctx
= fscrypt_get_ctx(inode
, GFP_NOFS
);
991 return ERR_CAST(ctx
);
993 /* wait the page to be moved by cleaning */
994 f2fs_wait_on_encrypted_page_writeback(sbi
, blkaddr
);
997 bio
= bio_alloc(GFP_KERNEL
, min_t(int, nr_pages
, BIO_MAX_PAGES
));
1000 fscrypt_release_ctx(ctx
);
1001 return ERR_PTR(-ENOMEM
);
1003 bio
->bi_bdev
= bdev
;
1004 bio
->bi_iter
.bi_sector
= SECTOR_FROM_BLOCK(blkaddr
);
1005 bio
->bi_end_io
= f2fs_read_end_io
;
1006 bio
->bi_private
= ctx
;
1012 * This function was originally taken from fs/mpage.c, and customized for f2fs.
1013 * Major change was from block_size == page_size in f2fs by default.
1015 static int f2fs_mpage_readpages(struct address_space
*mapping
,
1016 struct list_head
*pages
, struct page
*page
,
1019 struct bio
*bio
= NULL
;
1021 sector_t last_block_in_bio
= 0;
1022 struct inode
*inode
= mapping
->host
;
1023 const unsigned blkbits
= inode
->i_blkbits
;
1024 const unsigned blocksize
= 1 << blkbits
;
1025 sector_t block_in_file
;
1026 sector_t last_block
;
1027 sector_t last_block_in_file
;
1029 struct f2fs_map_blocks map
;
1035 map
.m_next_pgofs
= NULL
;
1037 for (page_idx
= 0; nr_pages
; page_idx
++, nr_pages
--) {
1039 prefetchw(&page
->flags
);
1041 page
= list_entry(pages
->prev
, struct page
, lru
);
1042 list_del(&page
->lru
);
1043 if (add_to_page_cache_lru(page
, mapping
,
1045 readahead_gfp_mask(mapping
)))
1049 block_in_file
= (sector_t
)page
->index
;
1050 last_block
= block_in_file
+ nr_pages
;
1051 last_block_in_file
= (i_size_read(inode
) + blocksize
- 1) >>
1053 if (last_block
> last_block_in_file
)
1054 last_block
= last_block_in_file
;
1057 * Map blocks using the previous result first.
1059 if ((map
.m_flags
& F2FS_MAP_MAPPED
) &&
1060 block_in_file
> map
.m_lblk
&&
1061 block_in_file
< (map
.m_lblk
+ map
.m_len
))
1065 * Then do more f2fs_map_blocks() calls until we are
1066 * done with this page.
1070 if (block_in_file
< last_block
) {
1071 map
.m_lblk
= block_in_file
;
1072 map
.m_len
= last_block
- block_in_file
;
1074 if (f2fs_map_blocks(inode
, &map
, 0,
1075 F2FS_GET_BLOCK_READ
))
1076 goto set_error_page
;
1079 if ((map
.m_flags
& F2FS_MAP_MAPPED
)) {
1080 block_nr
= map
.m_pblk
+ block_in_file
- map
.m_lblk
;
1081 SetPageMappedToDisk(page
);
1083 if (!PageUptodate(page
) && !cleancache_get_page(page
)) {
1084 SetPageUptodate(page
);
1088 zero_user_segment(page
, 0, PAGE_SIZE
);
1089 if (!PageUptodate(page
))
1090 SetPageUptodate(page
);
1096 * This page will go to BIO. Do we need to send this
1099 if (bio
&& (last_block_in_bio
!= block_nr
- 1)) {
1101 __submit_bio(F2FS_I_SB(inode
), bio
, DATA
);
1105 bio
= f2fs_grab_bio(inode
, block_nr
, nr_pages
);
1108 goto set_error_page
;
1110 bio_set_op_attrs(bio
, REQ_OP_READ
, 0);
1113 if (bio_add_page(bio
, page
, blocksize
, 0) < blocksize
)
1114 goto submit_and_realloc
;
1116 last_block_in_bio
= block_nr
;
1120 zero_user_segment(page
, 0, PAGE_SIZE
);
1125 __submit_bio(F2FS_I_SB(inode
), bio
, DATA
);
1133 BUG_ON(pages
&& !list_empty(pages
));
1135 __submit_bio(F2FS_I_SB(inode
), bio
, DATA
);
1139 static int f2fs_read_data_page(struct file
*file
, struct page
*page
)
1141 struct inode
*inode
= page
->mapping
->host
;
1144 trace_f2fs_readpage(page
, DATA
);
1146 /* If the file has inline data, try to read it directly */
1147 if (f2fs_has_inline_data(inode
))
1148 ret
= f2fs_read_inline_data(inode
, page
);
1150 ret
= f2fs_mpage_readpages(page
->mapping
, NULL
, page
, 1);
1154 static int f2fs_read_data_pages(struct file
*file
,
1155 struct address_space
*mapping
,
1156 struct list_head
*pages
, unsigned nr_pages
)
1158 struct inode
*inode
= file
->f_mapping
->host
;
1159 struct page
*page
= list_entry(pages
->prev
, struct page
, lru
);
1161 trace_f2fs_readpages(inode
, page
, nr_pages
);
1163 /* If the file has inline data, skip readpages */
1164 if (f2fs_has_inline_data(inode
))
1167 return f2fs_mpage_readpages(mapping
, pages
, NULL
, nr_pages
);
1170 int do_write_data_page(struct f2fs_io_info
*fio
)
1172 struct page
*page
= fio
->page
;
1173 struct inode
*inode
= page
->mapping
->host
;
1174 struct dnode_of_data dn
;
1177 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
1178 err
= get_dnode_of_data(&dn
, page
->index
, LOOKUP_NODE
);
1182 fio
->old_blkaddr
= dn
.data_blkaddr
;
1184 /* This page is already truncated */
1185 if (fio
->old_blkaddr
== NULL_ADDR
) {
1186 ClearPageUptodate(page
);
1190 if (f2fs_encrypted_inode(inode
) && S_ISREG(inode
->i_mode
)) {
1191 gfp_t gfp_flags
= GFP_NOFS
;
1193 /* wait for GCed encrypted page writeback */
1194 f2fs_wait_on_encrypted_page_writeback(F2FS_I_SB(inode
),
1197 fio
->encrypted_page
= fscrypt_encrypt_page(inode
, fio
->page
,
1199 if (IS_ERR(fio
->encrypted_page
)) {
1200 err
= PTR_ERR(fio
->encrypted_page
);
1201 if (err
== -ENOMEM
) {
1202 /* flush pending ios and wait for a while */
1203 f2fs_flush_merged_bios(F2FS_I_SB(inode
));
1204 congestion_wait(BLK_RW_ASYNC
, HZ
/50);
1205 gfp_flags
|= __GFP_NOFAIL
;
1213 set_page_writeback(page
);
1216 * If current allocation needs SSR,
1217 * it had better in-place writes for updated data.
1219 if (unlikely(fio
->old_blkaddr
!= NEW_ADDR
&&
1220 !is_cold_data(page
) &&
1221 !IS_ATOMIC_WRITTEN_PAGE(page
) &&
1222 need_inplace_update(inode
))) {
1223 rewrite_data_page(fio
);
1224 set_inode_flag(inode
, FI_UPDATE_WRITE
);
1225 trace_f2fs_do_write_data_page(page
, IPU
);
1227 write_data_page(&dn
, fio
);
1228 trace_f2fs_do_write_data_page(page
, OPU
);
1229 set_inode_flag(inode
, FI_APPEND_WRITE
);
1230 if (page
->index
== 0)
1231 set_inode_flag(inode
, FI_FIRST_BLOCK_WRITTEN
);
1234 f2fs_put_dnode(&dn
);
1238 static int f2fs_write_data_page(struct page
*page
,
1239 struct writeback_control
*wbc
)
1241 struct inode
*inode
= page
->mapping
->host
;
1242 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1243 loff_t i_size
= i_size_read(inode
);
1244 const pgoff_t end_index
= ((unsigned long long) i_size
)
1246 loff_t psize
= (page
->index
+ 1) << PAGE_SHIFT
;
1247 unsigned offset
= 0;
1248 bool need_balance_fs
= false;
1250 struct f2fs_io_info fio
= {
1254 .op_flags
= (wbc
->sync_mode
== WB_SYNC_ALL
) ? WRITE_SYNC
: 0,
1256 .encrypted_page
= NULL
,
1259 trace_f2fs_writepage(page
, DATA
);
1261 if (page
->index
< end_index
)
1265 * If the offset is out-of-range of file size,
1266 * this page does not have to be written to disk.
1268 offset
= i_size
& (PAGE_SIZE
- 1);
1269 if ((page
->index
>= end_index
+ 1) || !offset
)
1272 zero_user_segment(page
, offset
, PAGE_SIZE
);
1274 if (unlikely(is_sbi_flag_set(sbi
, SBI_POR_DOING
)))
1276 if (f2fs_is_drop_cache(inode
))
1278 /* we should not write 0'th page having journal header */
1279 if (f2fs_is_volatile_file(inode
) && (!page
->index
||
1280 (!wbc
->for_reclaim
&&
1281 available_free_memory(sbi
, BASE_CHECK
))))
1284 /* we should bypass data pages to proceed the kworkder jobs */
1285 if (unlikely(f2fs_cp_error(sbi
))) {
1286 mapping_set_error(page
->mapping
, -EIO
);
1290 /* Dentry blocks are controlled by checkpoint */
1291 if (S_ISDIR(inode
->i_mode
)) {
1292 err
= do_write_data_page(&fio
);
1296 if (!wbc
->for_reclaim
)
1297 need_balance_fs
= true;
1298 else if (has_not_enough_free_secs(sbi
, 0, 0))
1303 if (f2fs_has_inline_data(inode
))
1304 err
= f2fs_write_inline_data(inode
, page
);
1306 err
= do_write_data_page(&fio
);
1307 if (F2FS_I(inode
)->last_disk_size
< psize
)
1308 F2FS_I(inode
)->last_disk_size
= psize
;
1309 f2fs_unlock_op(sbi
);
1311 if (err
&& err
!= -ENOENT
)
1314 clear_cold_data(page
);
1316 inode_dec_dirty_pages(inode
);
1318 ClearPageUptodate(page
);
1320 if (wbc
->for_reclaim
) {
1321 f2fs_submit_merged_bio_cond(sbi
, NULL
, page
, 0, DATA
, WRITE
);
1322 remove_dirty_inode(inode
);
1326 f2fs_balance_fs(sbi
, need_balance_fs
);
1328 if (unlikely(f2fs_cp_error(sbi
)))
1329 f2fs_submit_merged_bio(sbi
, DATA
, WRITE
);
1334 redirty_page_for_writepage(wbc
, page
);
1340 * This function was copied from write_cche_pages from mm/page-writeback.c.
1341 * The major change is making write step of cold data page separately from
1342 * warm/hot data page.
1344 static int f2fs_write_cache_pages(struct address_space
*mapping
,
1345 struct writeback_control
*wbc
)
1349 struct pagevec pvec
;
1351 pgoff_t
uninitialized_var(writeback_index
);
1353 pgoff_t end
; /* Inclusive */
1356 int range_whole
= 0;
1360 pagevec_init(&pvec
, 0);
1362 if (wbc
->range_cyclic
) {
1363 writeback_index
= mapping
->writeback_index
; /* prev offset */
1364 index
= writeback_index
;
1371 index
= wbc
->range_start
>> PAGE_SHIFT
;
1372 end
= wbc
->range_end
>> PAGE_SHIFT
;
1373 if (wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
)
1375 cycled
= 1; /* ignore range_cyclic tests */
1377 if (wbc
->sync_mode
== WB_SYNC_ALL
|| wbc
->tagged_writepages
)
1378 tag
= PAGECACHE_TAG_TOWRITE
;
1380 tag
= PAGECACHE_TAG_DIRTY
;
1382 if (wbc
->sync_mode
== WB_SYNC_ALL
|| wbc
->tagged_writepages
)
1383 tag_pages_for_writeback(mapping
, index
, end
);
1385 while (!done
&& (index
<= end
)) {
1388 nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
, tag
,
1389 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
- 1) + 1);
1393 for (i
= 0; i
< nr_pages
; i
++) {
1394 struct page
*page
= pvec
.pages
[i
];
1396 if (page
->index
> end
) {
1401 done_index
= page
->index
;
1405 if (unlikely(page
->mapping
!= mapping
)) {
1411 if (!PageDirty(page
)) {
1412 /* someone wrote it for us */
1413 goto continue_unlock
;
1416 if (PageWriteback(page
)) {
1417 if (wbc
->sync_mode
!= WB_SYNC_NONE
)
1418 f2fs_wait_on_page_writeback(page
,
1421 goto continue_unlock
;
1424 BUG_ON(PageWriteback(page
));
1425 if (!clear_page_dirty_for_io(page
))
1426 goto continue_unlock
;
1428 ret
= mapping
->a_ops
->writepage(page
, wbc
);
1429 if (unlikely(ret
)) {
1430 done_index
= page
->index
+ 1;
1437 if (--wbc
->nr_to_write
<= 0 &&
1438 wbc
->sync_mode
== WB_SYNC_NONE
) {
1443 pagevec_release(&pvec
);
1447 if (!cycled
&& !done
) {
1450 end
= writeback_index
- 1;
1453 if (wbc
->range_cyclic
|| (range_whole
&& wbc
->nr_to_write
> 0))
1454 mapping
->writeback_index
= done_index
;
1457 f2fs_submit_merged_bio_cond(F2FS_M_SB(mapping
), mapping
->host
,
1458 NULL
, 0, DATA
, WRITE
);
1463 static int f2fs_write_data_pages(struct address_space
*mapping
,
1464 struct writeback_control
*wbc
)
1466 struct inode
*inode
= mapping
->host
;
1467 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1468 struct blk_plug plug
;
1471 /* deal with chardevs and other special file */
1472 if (!mapping
->a_ops
->writepage
)
1475 /* skip writing if there is no dirty page in this inode */
1476 if (!get_dirty_pages(inode
) && wbc
->sync_mode
== WB_SYNC_NONE
)
1479 if (S_ISDIR(inode
->i_mode
) && wbc
->sync_mode
== WB_SYNC_NONE
&&
1480 get_dirty_pages(inode
) < nr_pages_to_skip(sbi
, DATA
) &&
1481 available_free_memory(sbi
, DIRTY_DENTS
))
1484 /* skip writing during file defragment */
1485 if (is_inode_flag_set(inode
, FI_DO_DEFRAG
))
1488 /* during POR, we don't need to trigger writepage at all. */
1489 if (unlikely(is_sbi_flag_set(sbi
, SBI_POR_DOING
)))
1492 trace_f2fs_writepages(mapping
->host
, wbc
, DATA
);
1494 blk_start_plug(&plug
);
1495 ret
= f2fs_write_cache_pages(mapping
, wbc
);
1496 blk_finish_plug(&plug
);
1498 * if some pages were truncated, we cannot guarantee its mapping->host
1499 * to detect pending bios.
1502 remove_dirty_inode(inode
);
1506 wbc
->pages_skipped
+= get_dirty_pages(inode
);
1507 trace_f2fs_writepages(mapping
->host
, wbc
, DATA
);
1511 static void f2fs_write_failed(struct address_space
*mapping
, loff_t to
)
1513 struct inode
*inode
= mapping
->host
;
1514 loff_t i_size
= i_size_read(inode
);
1517 truncate_pagecache(inode
, i_size
);
1518 truncate_blocks(inode
, i_size
, true);
1522 static int prepare_write_begin(struct f2fs_sb_info
*sbi
,
1523 struct page
*page
, loff_t pos
, unsigned len
,
1524 block_t
*blk_addr
, bool *node_changed
)
1526 struct inode
*inode
= page
->mapping
->host
;
1527 pgoff_t index
= page
->index
;
1528 struct dnode_of_data dn
;
1530 bool locked
= false;
1531 struct extent_info ei
;
1535 * we already allocated all the blocks, so we don't need to get
1536 * the block addresses when there is no need to fill the page.
1538 if (!f2fs_has_inline_data(inode
) && len
== PAGE_SIZE
)
1541 if (f2fs_has_inline_data(inode
) ||
1542 (pos
& PAGE_MASK
) >= i_size_read(inode
)) {
1547 /* check inline_data */
1548 ipage
= get_node_page(sbi
, inode
->i_ino
);
1549 if (IS_ERR(ipage
)) {
1550 err
= PTR_ERR(ipage
);
1554 set_new_dnode(&dn
, inode
, ipage
, ipage
, 0);
1556 if (f2fs_has_inline_data(inode
)) {
1557 if (pos
+ len
<= MAX_INLINE_DATA
) {
1558 read_inline_data(page
, ipage
);
1559 set_inode_flag(inode
, FI_DATA_EXIST
);
1561 set_inline_node(ipage
);
1563 err
= f2fs_convert_inline_page(&dn
, page
);
1566 if (dn
.data_blkaddr
== NULL_ADDR
)
1567 err
= f2fs_get_block(&dn
, index
);
1569 } else if (locked
) {
1570 err
= f2fs_get_block(&dn
, index
);
1572 if (f2fs_lookup_extent_cache(inode
, index
, &ei
)) {
1573 dn
.data_blkaddr
= ei
.blk
+ index
- ei
.fofs
;
1576 err
= get_dnode_of_data(&dn
, index
, LOOKUP_NODE
);
1577 if (err
|| dn
.data_blkaddr
== NULL_ADDR
) {
1578 f2fs_put_dnode(&dn
);
1586 /* convert_inline_page can make node_changed */
1587 *blk_addr
= dn
.data_blkaddr
;
1588 *node_changed
= dn
.node_changed
;
1590 f2fs_put_dnode(&dn
);
1593 f2fs_unlock_op(sbi
);
1597 static int f2fs_write_begin(struct file
*file
, struct address_space
*mapping
,
1598 loff_t pos
, unsigned len
, unsigned flags
,
1599 struct page
**pagep
, void **fsdata
)
1601 struct inode
*inode
= mapping
->host
;
1602 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1603 struct page
*page
= NULL
;
1604 pgoff_t index
= ((unsigned long long) pos
) >> PAGE_SHIFT
;
1605 bool need_balance
= false;
1606 block_t blkaddr
= NULL_ADDR
;
1609 trace_f2fs_write_begin(inode
, pos
, len
, flags
);
1612 * We should check this at this moment to avoid deadlock on inode page
1613 * and #0 page. The locking rule for inline_data conversion should be:
1614 * lock_page(page #0) -> lock_page(inode_page)
1617 err
= f2fs_convert_inline_inode(inode
);
1623 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
1624 * wait_for_stable_page. Will wait that below with our IO control.
1626 page
= pagecache_get_page(mapping
, index
,
1627 FGP_LOCK
| FGP_WRITE
| FGP_CREAT
, GFP_NOFS
);
1635 err
= prepare_write_begin(sbi
, page
, pos
, len
,
1636 &blkaddr
, &need_balance
);
1640 if (need_balance
&& has_not_enough_free_secs(sbi
, 0, 0)) {
1642 f2fs_balance_fs(sbi
, true);
1644 if (page
->mapping
!= mapping
) {
1645 /* The page got truncated from under us */
1646 f2fs_put_page(page
, 1);
1651 f2fs_wait_on_page_writeback(page
, DATA
, false);
1653 /* wait for GCed encrypted page writeback */
1654 if (f2fs_encrypted_inode(inode
) && S_ISREG(inode
->i_mode
))
1655 f2fs_wait_on_encrypted_page_writeback(sbi
, blkaddr
);
1657 if (len
== PAGE_SIZE
|| PageUptodate(page
))
1660 if (blkaddr
== NEW_ADDR
) {
1661 zero_user_segment(page
, 0, PAGE_SIZE
);
1662 SetPageUptodate(page
);
1666 bio
= f2fs_grab_bio(inode
, blkaddr
, 1);
1671 bio_set_op_attrs(bio
, REQ_OP_READ
, READ_SYNC
);
1672 if (bio_add_page(bio
, page
, PAGE_SIZE
, 0) < PAGE_SIZE
) {
1678 __submit_bio(sbi
, bio
, DATA
);
1681 if (unlikely(page
->mapping
!= mapping
)) {
1682 f2fs_put_page(page
, 1);
1685 if (unlikely(!PageUptodate(page
))) {
1693 f2fs_put_page(page
, 1);
1694 f2fs_write_failed(mapping
, pos
+ len
);
1698 static int f2fs_write_end(struct file
*file
,
1699 struct address_space
*mapping
,
1700 loff_t pos
, unsigned len
, unsigned copied
,
1701 struct page
*page
, void *fsdata
)
1703 struct inode
*inode
= page
->mapping
->host
;
1705 trace_f2fs_write_end(inode
, pos
, len
, copied
);
1708 * This should be come from len == PAGE_SIZE, and we expect copied
1709 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
1710 * let generic_perform_write() try to copy data again through copied=0.
1712 if (!PageUptodate(page
)) {
1713 if (unlikely(copied
!= PAGE_SIZE
))
1716 SetPageUptodate(page
);
1721 set_page_dirty(page
);
1722 clear_cold_data(page
);
1724 if (pos
+ copied
> i_size_read(inode
))
1725 f2fs_i_size_write(inode
, pos
+ copied
);
1727 f2fs_put_page(page
, 1);
1728 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
1732 static int check_direct_IO(struct inode
*inode
, struct iov_iter
*iter
,
1735 unsigned blocksize_mask
= inode
->i_sb
->s_blocksize
- 1;
1737 if (offset
& blocksize_mask
)
1740 if (iov_iter_alignment(iter
) & blocksize_mask
)
1746 static ssize_t
f2fs_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
)
1748 struct address_space
*mapping
= iocb
->ki_filp
->f_mapping
;
1749 struct inode
*inode
= mapping
->host
;
1750 size_t count
= iov_iter_count(iter
);
1751 loff_t offset
= iocb
->ki_pos
;
1752 int rw
= iov_iter_rw(iter
);
1755 err
= check_direct_IO(inode
, iter
, offset
);
1759 if (f2fs_encrypted_inode(inode
) && S_ISREG(inode
->i_mode
))
1761 if (test_opt(F2FS_I_SB(inode
), LFS
))
1764 trace_f2fs_direct_IO_enter(inode
, offset
, count
, rw
);
1766 down_read(&F2FS_I(inode
)->dio_rwsem
[rw
]);
1767 err
= blockdev_direct_IO(iocb
, inode
, iter
, get_data_block_dio
);
1768 up_read(&F2FS_I(inode
)->dio_rwsem
[rw
]);
1772 set_inode_flag(inode
, FI_UPDATE_WRITE
);
1774 f2fs_write_failed(mapping
, offset
+ count
);
1777 trace_f2fs_direct_IO_exit(inode
, offset
, count
, rw
, err
);
1782 void f2fs_invalidate_page(struct page
*page
, unsigned int offset
,
1783 unsigned int length
)
1785 struct inode
*inode
= page
->mapping
->host
;
1786 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1788 if (inode
->i_ino
>= F2FS_ROOT_INO(sbi
) &&
1789 (offset
% PAGE_SIZE
|| length
!= PAGE_SIZE
))
1792 if (PageDirty(page
)) {
1793 if (inode
->i_ino
== F2FS_META_INO(sbi
))
1794 dec_page_count(sbi
, F2FS_DIRTY_META
);
1795 else if (inode
->i_ino
== F2FS_NODE_INO(sbi
))
1796 dec_page_count(sbi
, F2FS_DIRTY_NODES
);
1798 inode_dec_dirty_pages(inode
);
1801 /* This is atomic written page, keep Private */
1802 if (IS_ATOMIC_WRITTEN_PAGE(page
))
1805 set_page_private(page
, 0);
1806 ClearPagePrivate(page
);
1809 int f2fs_release_page(struct page
*page
, gfp_t wait
)
1811 /* If this is dirty page, keep PagePrivate */
1812 if (PageDirty(page
))
1815 /* This is atomic written page, keep Private */
1816 if (IS_ATOMIC_WRITTEN_PAGE(page
))
1819 set_page_private(page
, 0);
1820 ClearPagePrivate(page
);
1825 * This was copied from __set_page_dirty_buffers which gives higher performance
1826 * in very high speed storages. (e.g., pmem)
1828 void f2fs_set_page_dirty_nobuffers(struct page
*page
)
1830 struct address_space
*mapping
= page
->mapping
;
1831 unsigned long flags
;
1833 if (unlikely(!mapping
))
1836 spin_lock(&mapping
->private_lock
);
1837 lock_page_memcg(page
);
1839 spin_unlock(&mapping
->private_lock
);
1841 spin_lock_irqsave(&mapping
->tree_lock
, flags
);
1842 WARN_ON_ONCE(!PageUptodate(page
));
1843 account_page_dirtied(page
, mapping
);
1844 radix_tree_tag_set(&mapping
->page_tree
,
1845 page_index(page
), PAGECACHE_TAG_DIRTY
);
1846 spin_unlock_irqrestore(&mapping
->tree_lock
, flags
);
1847 unlock_page_memcg(page
);
1849 __mark_inode_dirty(mapping
->host
, I_DIRTY_PAGES
);
1853 static int f2fs_set_data_page_dirty(struct page
*page
)
1855 struct address_space
*mapping
= page
->mapping
;
1856 struct inode
*inode
= mapping
->host
;
1858 trace_f2fs_set_page_dirty(page
, DATA
);
1860 if (!PageUptodate(page
))
1861 SetPageUptodate(page
);
1863 if (f2fs_is_atomic_file(inode
)) {
1864 if (!IS_ATOMIC_WRITTEN_PAGE(page
)) {
1865 register_inmem_page(inode
, page
);
1869 * Previously, this page has been registered, we just
1875 if (!PageDirty(page
)) {
1876 f2fs_set_page_dirty_nobuffers(page
);
1877 update_dirty_page(inode
, page
);
1883 static sector_t
f2fs_bmap(struct address_space
*mapping
, sector_t block
)
1885 struct inode
*inode
= mapping
->host
;
1887 if (f2fs_has_inline_data(inode
))
1890 /* make sure allocating whole blocks */
1891 if (mapping_tagged(mapping
, PAGECACHE_TAG_DIRTY
))
1892 filemap_write_and_wait(mapping
);
1894 return generic_block_bmap(mapping
, block
, get_data_block_bmap
);
1897 #ifdef CONFIG_MIGRATION
1898 #include <linux/migrate.h>
1900 int f2fs_migrate_page(struct address_space
*mapping
,
1901 struct page
*newpage
, struct page
*page
, enum migrate_mode mode
)
1903 int rc
, extra_count
;
1904 struct f2fs_inode_info
*fi
= F2FS_I(mapping
->host
);
1905 bool atomic_written
= IS_ATOMIC_WRITTEN_PAGE(page
);
1907 BUG_ON(PageWriteback(page
));
1909 /* migrating an atomic written page is safe with the inmem_lock hold */
1910 if (atomic_written
&& !mutex_trylock(&fi
->inmem_lock
))
1914 * A reference is expected if PagePrivate set when move mapping,
1915 * however F2FS breaks this for maintaining dirty page counts when
1916 * truncating pages. So here adjusting the 'extra_count' make it work.
1918 extra_count
= (atomic_written
? 1 : 0) - page_has_private(page
);
1919 rc
= migrate_page_move_mapping(mapping
, newpage
,
1920 page
, NULL
, mode
, extra_count
);
1921 if (rc
!= MIGRATEPAGE_SUCCESS
) {
1923 mutex_unlock(&fi
->inmem_lock
);
1927 if (atomic_written
) {
1928 struct inmem_pages
*cur
;
1929 list_for_each_entry(cur
, &fi
->inmem_pages
, list
)
1930 if (cur
->page
== page
) {
1931 cur
->page
= newpage
;
1934 mutex_unlock(&fi
->inmem_lock
);
1939 if (PagePrivate(page
))
1940 SetPagePrivate(newpage
);
1941 set_page_private(newpage
, page_private(page
));
1943 migrate_page_copy(newpage
, page
);
1945 return MIGRATEPAGE_SUCCESS
;
1949 const struct address_space_operations f2fs_dblock_aops
= {
1950 .readpage
= f2fs_read_data_page
,
1951 .readpages
= f2fs_read_data_pages
,
1952 .writepage
= f2fs_write_data_page
,
1953 .writepages
= f2fs_write_data_pages
,
1954 .write_begin
= f2fs_write_begin
,
1955 .write_end
= f2fs_write_end
,
1956 .set_page_dirty
= f2fs_set_data_page_dirty
,
1957 .invalidatepage
= f2fs_invalidate_page
,
1958 .releasepage
= f2fs_release_page
,
1959 .direct_IO
= f2fs_direct_IO
,
1961 #ifdef CONFIG_MIGRATION
1962 .migratepage
= f2fs_migrate_page
,