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 if (!f2fs_is_valid_blkaddr(fio
->sbi
, fio
->new_blkaddr
,
244 __is_meta_io(fio
) ? META_GENERIC
: DATA_GENERIC
))
247 trace_f2fs_submit_page_bio(page
, fio
);
248 f2fs_trace_ios(fio
, 0);
250 /* Allocate a new bio */
251 bio
= __bio_alloc(fio
->sbi
, fio
->new_blkaddr
, 1, is_read_io(fio
->op
));
253 if (bio_add_page(bio
, page
, PAGE_SIZE
, 0) < PAGE_SIZE
) {
257 bio_set_op_attrs(bio
, fio
->op
, fio
->op_flags
);
259 __submit_bio(fio
->sbi
, bio
, fio
->type
);
263 void f2fs_submit_page_mbio(struct f2fs_io_info
*fio
)
265 struct f2fs_sb_info
*sbi
= fio
->sbi
;
266 enum page_type btype
= PAGE_TYPE_OF_BIO(fio
->type
);
267 struct f2fs_bio_info
*io
;
268 bool is_read
= is_read_io(fio
->op
);
269 struct page
*bio_page
;
271 io
= is_read
? &sbi
->read_io
: &sbi
->write_io
[btype
];
273 if (__is_valid_data_blkaddr(fio
->old_blkaddr
))
274 verify_block_addr(fio
, fio
->old_blkaddr
);
275 verify_block_addr(fio
, fio
->new_blkaddr
);
277 down_write(&io
->io_rwsem
);
279 if (io
->bio
&& (io
->last_block_in_bio
!= fio
->new_blkaddr
- 1 ||
280 (io
->fio
.op
!= fio
->op
|| io
->fio
.op_flags
!= fio
->op_flags
)))
281 __submit_merged_bio(io
);
283 if (io
->bio
== NULL
) {
284 int bio_blocks
= MAX_BIO_BLOCKS(sbi
);
286 io
->bio
= __bio_alloc(sbi
, fio
->new_blkaddr
,
287 bio_blocks
, is_read
);
291 bio_page
= fio
->encrypted_page
? fio
->encrypted_page
: fio
->page
;
293 if (bio_add_page(io
->bio
, bio_page
, PAGE_SIZE
, 0) <
295 __submit_merged_bio(io
);
299 io
->last_block_in_bio
= fio
->new_blkaddr
;
300 f2fs_trace_ios(fio
, 0);
302 up_write(&io
->io_rwsem
);
303 trace_f2fs_submit_page_mbio(fio
->page
, fio
);
306 static void __set_data_blkaddr(struct dnode_of_data
*dn
)
308 struct f2fs_node
*rn
= F2FS_NODE(dn
->node_page
);
311 /* Get physical address of data block */
312 addr_array
= blkaddr_in_node(rn
);
313 addr_array
[dn
->ofs_in_node
] = cpu_to_le32(dn
->data_blkaddr
);
317 * Lock ordering for the change of data block address:
320 * update block addresses in the node page
322 void set_data_blkaddr(struct dnode_of_data
*dn
)
324 f2fs_wait_on_page_writeback(dn
->node_page
, NODE
, true);
325 __set_data_blkaddr(dn
);
326 if (set_page_dirty(dn
->node_page
))
327 dn
->node_changed
= true;
330 void f2fs_update_data_blkaddr(struct dnode_of_data
*dn
, block_t blkaddr
)
332 dn
->data_blkaddr
= blkaddr
;
333 set_data_blkaddr(dn
);
334 f2fs_update_extent_cache(dn
);
337 /* dn->ofs_in_node will be returned with up-to-date last block pointer */
338 int reserve_new_blocks(struct dnode_of_data
*dn
, blkcnt_t count
)
340 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
345 if (unlikely(is_inode_flag_set(dn
->inode
, FI_NO_ALLOC
)))
347 if (unlikely(!inc_valid_block_count(sbi
, dn
->inode
, &count
)))
350 trace_f2fs_reserve_new_blocks(dn
->inode
, dn
->nid
,
351 dn
->ofs_in_node
, count
);
353 f2fs_wait_on_page_writeback(dn
->node_page
, NODE
, true);
355 for (; count
> 0; dn
->ofs_in_node
++) {
357 datablock_addr(dn
->node_page
, dn
->ofs_in_node
);
358 if (blkaddr
== NULL_ADDR
) {
359 dn
->data_blkaddr
= NEW_ADDR
;
360 __set_data_blkaddr(dn
);
365 if (set_page_dirty(dn
->node_page
))
366 dn
->node_changed
= true;
370 /* Should keep dn->ofs_in_node unchanged */
371 int reserve_new_block(struct dnode_of_data
*dn
)
373 unsigned int ofs_in_node
= dn
->ofs_in_node
;
376 ret
= reserve_new_blocks(dn
, 1);
377 dn
->ofs_in_node
= ofs_in_node
;
381 int f2fs_reserve_block(struct dnode_of_data
*dn
, pgoff_t index
)
383 bool need_put
= dn
->inode_page
? false : true;
386 err
= get_dnode_of_data(dn
, index
, ALLOC_NODE
);
390 if (dn
->data_blkaddr
== NULL_ADDR
)
391 err
= reserve_new_block(dn
);
397 int f2fs_get_block(struct dnode_of_data
*dn
, pgoff_t index
)
399 struct extent_info ei
;
400 struct inode
*inode
= dn
->inode
;
402 if (f2fs_lookup_extent_cache(inode
, index
, &ei
)) {
403 dn
->data_blkaddr
= ei
.blk
+ index
- ei
.fofs
;
407 return f2fs_reserve_block(dn
, index
);
410 struct page
*get_read_data_page(struct inode
*inode
, pgoff_t index
,
411 int op_flags
, bool for_write
)
413 struct address_space
*mapping
= inode
->i_mapping
;
414 struct dnode_of_data dn
;
416 struct extent_info ei
;
418 struct f2fs_io_info fio
= {
419 .sbi
= F2FS_I_SB(inode
),
422 .op_flags
= op_flags
,
423 .encrypted_page
= NULL
,
426 if (f2fs_encrypted_inode(inode
) && S_ISREG(inode
->i_mode
))
427 return read_mapping_page(mapping
, index
, NULL
);
429 page
= f2fs_grab_cache_page(mapping
, index
, for_write
);
431 return ERR_PTR(-ENOMEM
);
433 if (f2fs_lookup_extent_cache(inode
, index
, &ei
)) {
434 dn
.data_blkaddr
= ei
.blk
+ index
- ei
.fofs
;
438 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
439 err
= get_dnode_of_data(&dn
, index
, LOOKUP_NODE
);
444 if (unlikely(dn
.data_blkaddr
== NULL_ADDR
)) {
449 if (PageUptodate(page
)) {
455 * A new dentry page is allocated but not able to be written, since its
456 * new inode page couldn't be allocated due to -ENOSPC.
457 * In such the case, its blkaddr can be remained as NEW_ADDR.
458 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
460 if (dn
.data_blkaddr
== NEW_ADDR
) {
461 zero_user_segment(page
, 0, PAGE_SIZE
);
462 if (!PageUptodate(page
))
463 SetPageUptodate(page
);
468 fio
.new_blkaddr
= fio
.old_blkaddr
= dn
.data_blkaddr
;
470 err
= f2fs_submit_page_bio(&fio
);
476 f2fs_put_page(page
, 1);
480 struct page
*find_data_page(struct inode
*inode
, pgoff_t index
)
482 struct address_space
*mapping
= inode
->i_mapping
;
485 page
= find_get_page(mapping
, index
);
486 if (page
&& PageUptodate(page
))
488 f2fs_put_page(page
, 0);
490 page
= get_read_data_page(inode
, index
, READ_SYNC
, false);
494 if (PageUptodate(page
))
497 wait_on_page_locked(page
);
498 if (unlikely(!PageUptodate(page
))) {
499 f2fs_put_page(page
, 0);
500 return ERR_PTR(-EIO
);
506 * If it tries to access a hole, return an error.
507 * Because, the callers, functions in dir.c and GC, should be able to know
508 * whether this page exists or not.
510 struct page
*get_lock_data_page(struct inode
*inode
, pgoff_t index
,
513 struct address_space
*mapping
= inode
->i_mapping
;
516 page
= get_read_data_page(inode
, index
, READ_SYNC
, for_write
);
520 /* wait for read completion */
522 if (unlikely(page
->mapping
!= mapping
)) {
523 f2fs_put_page(page
, 1);
526 if (unlikely(!PageUptodate(page
))) {
527 f2fs_put_page(page
, 1);
528 return ERR_PTR(-EIO
);
534 * Caller ensures that this data page is never allocated.
535 * A new zero-filled data page is allocated in the page cache.
537 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
539 * Note that, ipage is set only by make_empty_dir, and if any error occur,
540 * ipage should be released by this function.
542 struct page
*get_new_data_page(struct inode
*inode
,
543 struct page
*ipage
, pgoff_t index
, bool new_i_size
)
545 struct address_space
*mapping
= inode
->i_mapping
;
547 struct dnode_of_data dn
;
550 page
= f2fs_grab_cache_page(mapping
, index
, true);
553 * before exiting, we should make sure ipage will be released
554 * if any error occur.
556 f2fs_put_page(ipage
, 1);
557 return ERR_PTR(-ENOMEM
);
560 set_new_dnode(&dn
, inode
, ipage
, NULL
, 0);
561 err
= f2fs_reserve_block(&dn
, index
);
563 f2fs_put_page(page
, 1);
569 if (PageUptodate(page
))
572 if (dn
.data_blkaddr
== NEW_ADDR
) {
573 zero_user_segment(page
, 0, PAGE_SIZE
);
574 if (!PageUptodate(page
))
575 SetPageUptodate(page
);
577 f2fs_put_page(page
, 1);
579 /* if ipage exists, blkaddr should be NEW_ADDR */
580 f2fs_bug_on(F2FS_I_SB(inode
), ipage
);
581 page
= get_lock_data_page(inode
, index
, true);
586 if (new_i_size
&& i_size_read(inode
) <
587 ((loff_t
)(index
+ 1) << PAGE_SHIFT
))
588 f2fs_i_size_write(inode
, ((loff_t
)(index
+ 1) << PAGE_SHIFT
));
592 static int __allocate_data_block(struct dnode_of_data
*dn
)
594 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
595 struct f2fs_summary sum
;
597 int seg
= CURSEG_WARM_DATA
;
601 if (unlikely(is_inode_flag_set(dn
->inode
, FI_NO_ALLOC
)))
604 dn
->data_blkaddr
= datablock_addr(dn
->node_page
, dn
->ofs_in_node
);
605 if (dn
->data_blkaddr
== NEW_ADDR
)
608 if (unlikely(!inc_valid_block_count(sbi
, dn
->inode
, &count
)))
612 get_node_info(sbi
, dn
->nid
, &ni
);
613 set_summary(&sum
, dn
->nid
, dn
->ofs_in_node
, ni
.version
);
615 if (dn
->ofs_in_node
== 0 && dn
->inode_page
== dn
->node_page
)
616 seg
= CURSEG_DIRECT_IO
;
618 allocate_data_block(sbi
, NULL
, dn
->data_blkaddr
, &dn
->data_blkaddr
,
620 set_data_blkaddr(dn
);
623 fofs
= start_bidx_of_node(ofs_of_node(dn
->node_page
), dn
->inode
) +
625 if (i_size_read(dn
->inode
) < ((loff_t
)(fofs
+ 1) << PAGE_SHIFT
))
626 f2fs_i_size_write(dn
->inode
,
627 ((loff_t
)(fofs
+ 1) << PAGE_SHIFT
));
631 ssize_t
f2fs_preallocate_blocks(struct kiocb
*iocb
, struct iov_iter
*from
)
633 struct inode
*inode
= file_inode(iocb
->ki_filp
);
634 struct f2fs_map_blocks map
;
637 map
.m_lblk
= F2FS_BLK_ALIGN(iocb
->ki_pos
);
638 map
.m_len
= F2FS_BYTES_TO_BLK(iocb
->ki_pos
+ iov_iter_count(from
));
639 if (map
.m_len
> map
.m_lblk
)
640 map
.m_len
-= map
.m_lblk
;
644 map
.m_next_pgofs
= NULL
;
646 if (iocb
->ki_flags
& IOCB_DIRECT
) {
647 ret
= f2fs_convert_inline_inode(inode
);
650 return f2fs_map_blocks(inode
, &map
, 1, F2FS_GET_BLOCK_PRE_DIO
);
652 if (iocb
->ki_pos
+ iov_iter_count(from
) > MAX_INLINE_DATA
) {
653 ret
= f2fs_convert_inline_inode(inode
);
657 if (!f2fs_has_inline_data(inode
))
658 return f2fs_map_blocks(inode
, &map
, 1, F2FS_GET_BLOCK_PRE_AIO
);
663 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
664 * f2fs_map_blocks structure.
665 * If original data blocks are allocated, then give them to blockdev.
667 * a. preallocate requested block addresses
668 * b. do not use extent cache for better performance
669 * c. give the block addresses to blockdev
671 int f2fs_map_blocks(struct inode
*inode
, struct f2fs_map_blocks
*map
,
672 int create
, int flag
)
674 unsigned int maxblocks
= map
->m_len
;
675 struct dnode_of_data dn
;
676 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
677 int mode
= create
? ALLOC_NODE
: LOOKUP_NODE
;
678 pgoff_t pgofs
, end_offset
, end
;
679 int err
= 0, ofs
= 1;
680 unsigned int ofs_in_node
, last_ofs_in_node
;
682 struct extent_info ei
;
683 bool allocated
= false;
692 /* it only supports block size == page size */
693 pgofs
= (pgoff_t
)map
->m_lblk
;
694 end
= pgofs
+ maxblocks
;
696 if (!create
&& f2fs_lookup_extent_cache(inode
, pgofs
, &ei
)) {
697 map
->m_pblk
= ei
.blk
+ pgofs
- ei
.fofs
;
698 map
->m_len
= min((pgoff_t
)maxblocks
, ei
.fofs
+ ei
.len
- pgofs
);
699 map
->m_flags
= F2FS_MAP_MAPPED
;
707 /* When reading holes, we need its node page */
708 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
709 err
= get_dnode_of_data(&dn
, pgofs
, mode
);
711 if (flag
== F2FS_GET_BLOCK_BMAP
)
713 if (err
== -ENOENT
) {
715 if (map
->m_next_pgofs
)
717 get_next_page_offset(&dn
, pgofs
);
723 last_ofs_in_node
= ofs_in_node
= dn
.ofs_in_node
;
724 end_offset
= ADDRS_PER_PAGE(dn
.node_page
, inode
);
727 blkaddr
= datablock_addr(dn
.node_page
, dn
.ofs_in_node
);
729 if (__is_valid_data_blkaddr(blkaddr
) &&
730 !f2fs_is_valid_blkaddr(sbi
, blkaddr
, DATA_GENERIC
)) {
735 if (!is_valid_data_blkaddr(sbi
, blkaddr
)) {
737 if (unlikely(f2fs_cp_error(sbi
))) {
741 if (flag
== F2FS_GET_BLOCK_PRE_AIO
) {
742 if (blkaddr
== NULL_ADDR
) {
744 last_ofs_in_node
= dn
.ofs_in_node
;
747 err
= __allocate_data_block(&dn
);
749 set_inode_flag(inode
, FI_APPEND_WRITE
);
755 map
->m_flags
= F2FS_MAP_NEW
;
756 blkaddr
= dn
.data_blkaddr
;
758 if (flag
== F2FS_GET_BLOCK_BMAP
) {
762 if (flag
== F2FS_GET_BLOCK_FIEMAP
&&
763 blkaddr
== NULL_ADDR
) {
764 if (map
->m_next_pgofs
)
765 *map
->m_next_pgofs
= pgofs
+ 1;
767 if (flag
!= F2FS_GET_BLOCK_FIEMAP
||
773 if (flag
== F2FS_GET_BLOCK_PRE_AIO
)
776 if (map
->m_len
== 0) {
777 /* preallocated unwritten block should be mapped for fiemap. */
778 if (blkaddr
== NEW_ADDR
)
779 map
->m_flags
|= F2FS_MAP_UNWRITTEN
;
780 map
->m_flags
|= F2FS_MAP_MAPPED
;
782 map
->m_pblk
= blkaddr
;
784 } else if ((map
->m_pblk
!= NEW_ADDR
&&
785 blkaddr
== (map
->m_pblk
+ ofs
)) ||
786 (map
->m_pblk
== NEW_ADDR
&& blkaddr
== NEW_ADDR
) ||
787 flag
== F2FS_GET_BLOCK_PRE_DIO
) {
798 /* preallocate blocks in batch for one dnode page */
799 if (flag
== F2FS_GET_BLOCK_PRE_AIO
&&
800 (pgofs
== end
|| dn
.ofs_in_node
== end_offset
)) {
802 dn
.ofs_in_node
= ofs_in_node
;
803 err
= reserve_new_blocks(&dn
, prealloc
);
806 allocated
= dn
.node_changed
;
808 map
->m_len
+= dn
.ofs_in_node
- ofs_in_node
;
809 if (prealloc
&& dn
.ofs_in_node
!= last_ofs_in_node
+ 1) {
813 dn
.ofs_in_node
= end_offset
;
818 else if (dn
.ofs_in_node
< end_offset
)
825 f2fs_balance_fs(sbi
, allocated
);
835 f2fs_balance_fs(sbi
, allocated
);
838 trace_f2fs_map_blocks(inode
, map
, err
);
842 static int __get_data_block(struct inode
*inode
, sector_t iblock
,
843 struct buffer_head
*bh
, int create
, int flag
,
846 struct f2fs_map_blocks map
;
850 map
.m_len
= bh
->b_size
>> inode
->i_blkbits
;
851 map
.m_next_pgofs
= next_pgofs
;
853 ret
= f2fs_map_blocks(inode
, &map
, create
, flag
);
855 map_bh(bh
, inode
->i_sb
, map
.m_pblk
);
856 bh
->b_state
= (bh
->b_state
& ~F2FS_MAP_FLAGS
) | map
.m_flags
;
857 bh
->b_size
= (u64
)map
.m_len
<< inode
->i_blkbits
;
862 static int get_data_block(struct inode
*inode
, sector_t iblock
,
863 struct buffer_head
*bh_result
, int create
, int flag
,
866 return __get_data_block(inode
, iblock
, bh_result
, create
,
870 static int get_data_block_dio(struct inode
*inode
, sector_t iblock
,
871 struct buffer_head
*bh_result
, int create
)
873 return __get_data_block(inode
, iblock
, bh_result
, create
,
874 F2FS_GET_BLOCK_DIO
, NULL
);
877 static int get_data_block_bmap(struct inode
*inode
, sector_t iblock
,
878 struct buffer_head
*bh_result
, int create
)
880 /* Block number less than F2FS MAX BLOCKS */
881 if (unlikely(iblock
>= F2FS_I_SB(inode
)->max_file_blocks
))
884 return __get_data_block(inode
, iblock
, bh_result
, create
,
885 F2FS_GET_BLOCK_BMAP
, NULL
);
888 static inline sector_t
logical_to_blk(struct inode
*inode
, loff_t offset
)
890 return (offset
>> inode
->i_blkbits
);
893 static inline loff_t
blk_to_logical(struct inode
*inode
, sector_t blk
)
895 return (blk
<< inode
->i_blkbits
);
898 int f2fs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
901 struct buffer_head map_bh
;
902 sector_t start_blk
, last_blk
;
905 u64 logical
= 0, phys
= 0, size
= 0;
909 ret
= fiemap_check_flags(fieinfo
, FIEMAP_FLAG_SYNC
);
913 if (f2fs_has_inline_data(inode
)) {
914 ret
= f2fs_inline_data_fiemap(inode
, fieinfo
, start
, len
);
921 isize
= i_size_read(inode
);
925 if (start
+ len
> isize
)
928 if (logical_to_blk(inode
, len
) == 0)
929 len
= blk_to_logical(inode
, 1);
931 start_blk
= logical_to_blk(inode
, start
);
932 last_blk
= logical_to_blk(inode
, start
+ len
- 1);
935 memset(&map_bh
, 0, sizeof(struct buffer_head
));
938 ret
= get_data_block(inode
, start_blk
, &map_bh
, 0,
939 F2FS_GET_BLOCK_FIEMAP
, &next_pgofs
);
944 if (!buffer_mapped(&map_bh
)) {
945 start_blk
= next_pgofs
;
946 /* Go through holes util pass the EOF */
947 if (blk_to_logical(inode
, start_blk
) < isize
)
949 /* Found a hole beyond isize means no more extents.
950 * Note that the premise is that filesystems don't
951 * punch holes beyond isize and keep size unchanged.
953 flags
|= FIEMAP_EXTENT_LAST
;
957 if (f2fs_encrypted_inode(inode
))
958 flags
|= FIEMAP_EXTENT_DATA_ENCRYPTED
;
960 ret
= fiemap_fill_next_extent(fieinfo
, logical
,
964 if (start_blk
> last_blk
|| ret
)
967 logical
= blk_to_logical(inode
, start_blk
);
968 phys
= blk_to_logical(inode
, map_bh
.b_blocknr
);
969 size
= map_bh
.b_size
;
971 if (buffer_unwritten(&map_bh
))
972 flags
= FIEMAP_EXTENT_UNWRITTEN
;
974 start_blk
+= logical_to_blk(inode
, size
);
978 if (fatal_signal_pending(current
))
990 static struct bio
*f2fs_grab_bio(struct inode
*inode
, block_t blkaddr
,
993 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
994 struct fscrypt_ctx
*ctx
= NULL
;
995 struct block_device
*bdev
= sbi
->sb
->s_bdev
;
998 if (!f2fs_is_valid_blkaddr(sbi
, blkaddr
, DATA_GENERIC
))
999 return ERR_PTR(-EFAULT
);
1001 if (f2fs_encrypted_inode(inode
) && S_ISREG(inode
->i_mode
)) {
1002 ctx
= fscrypt_get_ctx(inode
, GFP_NOFS
);
1004 return ERR_CAST(ctx
);
1006 /* wait the page to be moved by cleaning */
1007 f2fs_wait_on_encrypted_page_writeback(sbi
, blkaddr
);
1010 bio
= bio_alloc(GFP_KERNEL
, min_t(int, nr_pages
, BIO_MAX_PAGES
));
1013 fscrypt_release_ctx(ctx
);
1014 return ERR_PTR(-ENOMEM
);
1016 bio
->bi_bdev
= bdev
;
1017 bio
->bi_iter
.bi_sector
= SECTOR_FROM_BLOCK(blkaddr
);
1018 bio
->bi_end_io
= f2fs_read_end_io
;
1019 bio
->bi_private
= ctx
;
1025 * This function was originally taken from fs/mpage.c, and customized for f2fs.
1026 * Major change was from block_size == page_size in f2fs by default.
1028 static int f2fs_mpage_readpages(struct address_space
*mapping
,
1029 struct list_head
*pages
, struct page
*page
,
1032 struct bio
*bio
= NULL
;
1034 sector_t last_block_in_bio
= 0;
1035 struct inode
*inode
= mapping
->host
;
1036 const unsigned blkbits
= inode
->i_blkbits
;
1037 const unsigned blocksize
= 1 << blkbits
;
1038 sector_t block_in_file
;
1039 sector_t last_block
;
1040 sector_t last_block_in_file
;
1042 struct f2fs_map_blocks map
;
1048 map
.m_next_pgofs
= NULL
;
1050 for (page_idx
= 0; nr_pages
; page_idx
++, nr_pages
--) {
1052 prefetchw(&page
->flags
);
1054 page
= list_entry(pages
->prev
, struct page
, lru
);
1055 list_del(&page
->lru
);
1056 if (add_to_page_cache_lru(page
, mapping
,
1058 readahead_gfp_mask(mapping
)))
1062 block_in_file
= (sector_t
)page
->index
;
1063 last_block
= block_in_file
+ nr_pages
;
1064 last_block_in_file
= (i_size_read(inode
) + blocksize
- 1) >>
1066 if (last_block
> last_block_in_file
)
1067 last_block
= last_block_in_file
;
1070 * Map blocks using the previous result first.
1072 if ((map
.m_flags
& F2FS_MAP_MAPPED
) &&
1073 block_in_file
> map
.m_lblk
&&
1074 block_in_file
< (map
.m_lblk
+ map
.m_len
))
1078 * Then do more f2fs_map_blocks() calls until we are
1079 * done with this page.
1083 if (block_in_file
< last_block
) {
1084 map
.m_lblk
= block_in_file
;
1085 map
.m_len
= last_block
- block_in_file
;
1087 if (f2fs_map_blocks(inode
, &map
, 0,
1088 F2FS_GET_BLOCK_READ
))
1089 goto set_error_page
;
1092 if ((map
.m_flags
& F2FS_MAP_MAPPED
)) {
1093 block_nr
= map
.m_pblk
+ block_in_file
- map
.m_lblk
;
1094 SetPageMappedToDisk(page
);
1096 if (!PageUptodate(page
) && !cleancache_get_page(page
)) {
1097 SetPageUptodate(page
);
1101 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode
), block_nr
,
1103 goto set_error_page
;
1105 zero_user_segment(page
, 0, PAGE_SIZE
);
1106 if (!PageUptodate(page
))
1107 SetPageUptodate(page
);
1113 * This page will go to BIO. Do we need to send this
1116 if (bio
&& (last_block_in_bio
!= block_nr
- 1)) {
1118 __submit_bio(F2FS_I_SB(inode
), bio
, DATA
);
1122 bio
= f2fs_grab_bio(inode
, block_nr
, nr_pages
);
1125 goto set_error_page
;
1127 bio_set_op_attrs(bio
, REQ_OP_READ
, 0);
1130 if (bio_add_page(bio
, page
, blocksize
, 0) < blocksize
)
1131 goto submit_and_realloc
;
1133 last_block_in_bio
= block_nr
;
1137 zero_user_segment(page
, 0, PAGE_SIZE
);
1142 __submit_bio(F2FS_I_SB(inode
), bio
, DATA
);
1150 BUG_ON(pages
&& !list_empty(pages
));
1152 __submit_bio(F2FS_I_SB(inode
), bio
, DATA
);
1156 static int f2fs_read_data_page(struct file
*file
, struct page
*page
)
1158 struct inode
*inode
= page
->mapping
->host
;
1161 trace_f2fs_readpage(page
, DATA
);
1163 /* If the file has inline data, try to read it directly */
1164 if (f2fs_has_inline_data(inode
))
1165 ret
= f2fs_read_inline_data(inode
, page
);
1167 ret
= f2fs_mpage_readpages(page
->mapping
, NULL
, page
, 1);
1171 static int f2fs_read_data_pages(struct file
*file
,
1172 struct address_space
*mapping
,
1173 struct list_head
*pages
, unsigned nr_pages
)
1175 struct inode
*inode
= file
->f_mapping
->host
;
1176 struct page
*page
= list_entry(pages
->prev
, struct page
, lru
);
1178 trace_f2fs_readpages(inode
, page
, nr_pages
);
1180 /* If the file has inline data, skip readpages */
1181 if (f2fs_has_inline_data(inode
))
1184 return f2fs_mpage_readpages(mapping
, pages
, NULL
, nr_pages
);
1187 int do_write_data_page(struct f2fs_io_info
*fio
)
1189 struct page
*page
= fio
->page
;
1190 struct inode
*inode
= page
->mapping
->host
;
1191 struct dnode_of_data dn
;
1194 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
1195 err
= get_dnode_of_data(&dn
, page
->index
, LOOKUP_NODE
);
1199 fio
->old_blkaddr
= dn
.data_blkaddr
;
1201 /* This page is already truncated */
1202 if (fio
->old_blkaddr
== NULL_ADDR
) {
1203 ClearPageUptodate(page
);
1204 clear_cold_data(page
);
1208 if (f2fs_encrypted_inode(inode
) && S_ISREG(inode
->i_mode
)) {
1209 gfp_t gfp_flags
= GFP_NOFS
;
1211 /* wait for GCed encrypted page writeback */
1212 f2fs_wait_on_encrypted_page_writeback(F2FS_I_SB(inode
),
1215 fio
->encrypted_page
= fscrypt_encrypt_page(inode
, fio
->page
,
1217 if (IS_ERR(fio
->encrypted_page
)) {
1218 err
= PTR_ERR(fio
->encrypted_page
);
1219 if (err
== -ENOMEM
) {
1220 /* flush pending ios and wait for a while */
1221 f2fs_flush_merged_bios(F2FS_I_SB(inode
));
1222 congestion_wait(BLK_RW_ASYNC
, HZ
/50);
1223 gfp_flags
|= __GFP_NOFAIL
;
1231 set_page_writeback(page
);
1233 if (__is_valid_data_blkaddr(fio
->old_blkaddr
) &&
1234 !f2fs_is_valid_blkaddr(fio
->sbi
, fio
->old_blkaddr
,
1240 * If current allocation needs SSR,
1241 * it had better in-place writes for updated data.
1243 if (unlikely(is_valid_data_blkaddr(fio
->sbi
, fio
->old_blkaddr
) &&
1244 !is_cold_data(page
) &&
1245 !IS_ATOMIC_WRITTEN_PAGE(page
) &&
1246 need_inplace_update(inode
))) {
1247 rewrite_data_page(fio
);
1248 set_inode_flag(inode
, FI_UPDATE_WRITE
);
1249 trace_f2fs_do_write_data_page(page
, IPU
);
1251 write_data_page(&dn
, fio
);
1252 trace_f2fs_do_write_data_page(page
, OPU
);
1253 set_inode_flag(inode
, FI_APPEND_WRITE
);
1254 if (page
->index
== 0)
1255 set_inode_flag(inode
, FI_FIRST_BLOCK_WRITTEN
);
1258 f2fs_put_dnode(&dn
);
1262 static int f2fs_write_data_page(struct page
*page
,
1263 struct writeback_control
*wbc
)
1265 struct inode
*inode
= page
->mapping
->host
;
1266 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1267 loff_t i_size
= i_size_read(inode
);
1268 const pgoff_t end_index
= ((unsigned long long) i_size
)
1270 loff_t psize
= (loff_t
)(page
->index
+ 1) << PAGE_SHIFT
;
1271 unsigned offset
= 0;
1272 bool need_balance_fs
= false;
1274 struct f2fs_io_info fio
= {
1278 .op_flags
= (wbc
->sync_mode
== WB_SYNC_ALL
) ? WRITE_SYNC
: 0,
1280 .encrypted_page
= NULL
,
1283 trace_f2fs_writepage(page
, DATA
);
1285 if (page
->index
< end_index
)
1289 * If the offset is out-of-range of file size,
1290 * this page does not have to be written to disk.
1292 offset
= i_size
& (PAGE_SIZE
- 1);
1293 if ((page
->index
>= end_index
+ 1) || !offset
)
1296 zero_user_segment(page
, offset
, PAGE_SIZE
);
1298 if (unlikely(is_sbi_flag_set(sbi
, SBI_POR_DOING
)))
1300 if (f2fs_is_drop_cache(inode
))
1302 /* we should not write 0'th page having journal header */
1303 if (f2fs_is_volatile_file(inode
) && (!page
->index
||
1304 (!wbc
->for_reclaim
&&
1305 available_free_memory(sbi
, BASE_CHECK
))))
1308 /* we should bypass data pages to proceed the kworkder jobs */
1309 if (unlikely(f2fs_cp_error(sbi
))) {
1310 mapping_set_error(page
->mapping
, -EIO
);
1314 /* Dentry blocks are controlled by checkpoint */
1315 if (S_ISDIR(inode
->i_mode
)) {
1316 err
= do_write_data_page(&fio
);
1320 if (!wbc
->for_reclaim
)
1321 need_balance_fs
= true;
1322 else if (has_not_enough_free_secs(sbi
, 0, 0))
1327 if (f2fs_has_inline_data(inode
))
1328 err
= f2fs_write_inline_data(inode
, page
);
1330 err
= do_write_data_page(&fio
);
1331 if (F2FS_I(inode
)->last_disk_size
< psize
)
1332 F2FS_I(inode
)->last_disk_size
= psize
;
1333 f2fs_unlock_op(sbi
);
1335 if (err
&& err
!= -ENOENT
)
1338 clear_cold_data(page
);
1340 inode_dec_dirty_pages(inode
);
1342 ClearPageUptodate(page
);
1343 clear_cold_data(page
);
1346 if (wbc
->for_reclaim
) {
1347 f2fs_submit_merged_bio_cond(sbi
, NULL
, page
, 0, DATA
, WRITE
);
1348 remove_dirty_inode(inode
);
1352 f2fs_balance_fs(sbi
, need_balance_fs
);
1354 if (unlikely(f2fs_cp_error(sbi
)))
1355 f2fs_submit_merged_bio(sbi
, DATA
, WRITE
);
1360 redirty_page_for_writepage(wbc
, page
);
1366 * This function was copied from write_cche_pages from mm/page-writeback.c.
1367 * The major change is making write step of cold data page separately from
1368 * warm/hot data page.
1370 static int f2fs_write_cache_pages(struct address_space
*mapping
,
1371 struct writeback_control
*wbc
)
1375 struct pagevec pvec
;
1377 pgoff_t
uninitialized_var(writeback_index
);
1379 pgoff_t end
; /* Inclusive */
1382 int range_whole
= 0;
1386 pagevec_init(&pvec
, 0);
1388 if (wbc
->range_cyclic
) {
1389 writeback_index
= mapping
->writeback_index
; /* prev offset */
1390 index
= writeback_index
;
1397 index
= wbc
->range_start
>> PAGE_SHIFT
;
1398 end
= wbc
->range_end
>> PAGE_SHIFT
;
1399 if (wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
)
1401 cycled
= 1; /* ignore range_cyclic tests */
1403 if (wbc
->sync_mode
== WB_SYNC_ALL
|| wbc
->tagged_writepages
)
1404 tag
= PAGECACHE_TAG_TOWRITE
;
1406 tag
= PAGECACHE_TAG_DIRTY
;
1408 if (wbc
->sync_mode
== WB_SYNC_ALL
|| wbc
->tagged_writepages
)
1409 tag_pages_for_writeback(mapping
, index
, end
);
1411 while (!done
&& (index
<= end
)) {
1414 nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
, tag
,
1415 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
- 1) + 1);
1419 for (i
= 0; i
< nr_pages
; i
++) {
1420 struct page
*page
= pvec
.pages
[i
];
1422 if (page
->index
> end
) {
1427 done_index
= page
->index
;
1431 if (unlikely(page
->mapping
!= mapping
)) {
1437 if (!PageDirty(page
)) {
1438 /* someone wrote it for us */
1439 goto continue_unlock
;
1442 if (PageWriteback(page
)) {
1443 if (wbc
->sync_mode
!= WB_SYNC_NONE
)
1444 f2fs_wait_on_page_writeback(page
,
1447 goto continue_unlock
;
1450 BUG_ON(PageWriteback(page
));
1451 if (!clear_page_dirty_for_io(page
))
1452 goto continue_unlock
;
1454 ret
= mapping
->a_ops
->writepage(page
, wbc
);
1455 if (unlikely(ret
)) {
1456 done_index
= page
->index
+ 1;
1463 if (--wbc
->nr_to_write
<= 0 &&
1464 wbc
->sync_mode
== WB_SYNC_NONE
) {
1469 pagevec_release(&pvec
);
1473 if (!cycled
&& !done
) {
1476 end
= writeback_index
- 1;
1479 if (wbc
->range_cyclic
|| (range_whole
&& wbc
->nr_to_write
> 0))
1480 mapping
->writeback_index
= done_index
;
1483 f2fs_submit_merged_bio_cond(F2FS_M_SB(mapping
), mapping
->host
,
1484 NULL
, 0, DATA
, WRITE
);
1489 static int f2fs_write_data_pages(struct address_space
*mapping
,
1490 struct writeback_control
*wbc
)
1492 struct inode
*inode
= mapping
->host
;
1493 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1494 struct blk_plug plug
;
1497 /* deal with chardevs and other special file */
1498 if (!mapping
->a_ops
->writepage
)
1501 /* skip writing if there is no dirty page in this inode */
1502 if (!get_dirty_pages(inode
) && wbc
->sync_mode
== WB_SYNC_NONE
)
1505 if (S_ISDIR(inode
->i_mode
) && wbc
->sync_mode
== WB_SYNC_NONE
&&
1506 get_dirty_pages(inode
) < nr_pages_to_skip(sbi
, DATA
) &&
1507 available_free_memory(sbi
, DIRTY_DENTS
))
1510 /* skip writing during file defragment */
1511 if (is_inode_flag_set(inode
, FI_DO_DEFRAG
))
1514 /* during POR, we don't need to trigger writepage at all. */
1515 if (unlikely(is_sbi_flag_set(sbi
, SBI_POR_DOING
)))
1518 trace_f2fs_writepages(mapping
->host
, wbc
, DATA
);
1520 blk_start_plug(&plug
);
1521 ret
= f2fs_write_cache_pages(mapping
, wbc
);
1522 blk_finish_plug(&plug
);
1524 * if some pages were truncated, we cannot guarantee its mapping->host
1525 * to detect pending bios.
1528 remove_dirty_inode(inode
);
1532 wbc
->pages_skipped
+= get_dirty_pages(inode
);
1533 trace_f2fs_writepages(mapping
->host
, wbc
, DATA
);
1537 static void f2fs_write_failed(struct address_space
*mapping
, loff_t to
)
1539 struct inode
*inode
= mapping
->host
;
1540 loff_t i_size
= i_size_read(inode
);
1543 truncate_pagecache(inode
, i_size
);
1544 truncate_blocks(inode
, i_size
, true);
1548 static int prepare_write_begin(struct f2fs_sb_info
*sbi
,
1549 struct page
*page
, loff_t pos
, unsigned len
,
1550 block_t
*blk_addr
, bool *node_changed
)
1552 struct inode
*inode
= page
->mapping
->host
;
1553 pgoff_t index
= page
->index
;
1554 struct dnode_of_data dn
;
1556 bool locked
= false;
1557 struct extent_info ei
;
1561 * we already allocated all the blocks, so we don't need to get
1562 * the block addresses when there is no need to fill the page.
1564 if (!f2fs_has_inline_data(inode
) && len
== PAGE_SIZE
)
1567 if (f2fs_has_inline_data(inode
) ||
1568 (pos
& PAGE_MASK
) >= i_size_read(inode
)) {
1573 /* check inline_data */
1574 ipage
= get_node_page(sbi
, inode
->i_ino
);
1575 if (IS_ERR(ipage
)) {
1576 err
= PTR_ERR(ipage
);
1580 set_new_dnode(&dn
, inode
, ipage
, ipage
, 0);
1582 if (f2fs_has_inline_data(inode
)) {
1583 if (pos
+ len
<= MAX_INLINE_DATA
) {
1584 read_inline_data(page
, ipage
);
1585 set_inode_flag(inode
, FI_DATA_EXIST
);
1587 set_inline_node(ipage
);
1589 err
= f2fs_convert_inline_page(&dn
, page
);
1592 if (dn
.data_blkaddr
== NULL_ADDR
)
1593 err
= f2fs_get_block(&dn
, index
);
1595 } else if (locked
) {
1596 err
= f2fs_get_block(&dn
, index
);
1598 if (f2fs_lookup_extent_cache(inode
, index
, &ei
)) {
1599 dn
.data_blkaddr
= ei
.blk
+ index
- ei
.fofs
;
1602 err
= get_dnode_of_data(&dn
, index
, LOOKUP_NODE
);
1603 if (err
|| dn
.data_blkaddr
== NULL_ADDR
) {
1604 f2fs_put_dnode(&dn
);
1612 /* convert_inline_page can make node_changed */
1613 *blk_addr
= dn
.data_blkaddr
;
1614 *node_changed
= dn
.node_changed
;
1616 f2fs_put_dnode(&dn
);
1619 f2fs_unlock_op(sbi
);
1623 static int f2fs_write_begin(struct file
*file
, struct address_space
*mapping
,
1624 loff_t pos
, unsigned len
, unsigned flags
,
1625 struct page
**pagep
, void **fsdata
)
1627 struct inode
*inode
= mapping
->host
;
1628 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1629 struct page
*page
= NULL
;
1630 pgoff_t index
= ((unsigned long long) pos
) >> PAGE_SHIFT
;
1631 bool need_balance
= false;
1632 block_t blkaddr
= NULL_ADDR
;
1635 trace_f2fs_write_begin(inode
, pos
, len
, flags
);
1638 * We should check this at this moment to avoid deadlock on inode page
1639 * and #0 page. The locking rule for inline_data conversion should be:
1640 * lock_page(page #0) -> lock_page(inode_page)
1643 err
= f2fs_convert_inline_inode(inode
);
1649 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
1650 * wait_for_stable_page. Will wait that below with our IO control.
1652 page
= pagecache_get_page(mapping
, index
,
1653 FGP_LOCK
| FGP_WRITE
| FGP_CREAT
, GFP_NOFS
);
1661 err
= prepare_write_begin(sbi
, page
, pos
, len
,
1662 &blkaddr
, &need_balance
);
1666 if (need_balance
&& has_not_enough_free_secs(sbi
, 0, 0)) {
1668 f2fs_balance_fs(sbi
, true);
1670 if (page
->mapping
!= mapping
) {
1671 /* The page got truncated from under us */
1672 f2fs_put_page(page
, 1);
1677 f2fs_wait_on_page_writeback(page
, DATA
, false);
1679 /* wait for GCed encrypted page writeback */
1680 if (f2fs_encrypted_inode(inode
) && S_ISREG(inode
->i_mode
))
1681 f2fs_wait_on_encrypted_page_writeback(sbi
, blkaddr
);
1683 if (len
== PAGE_SIZE
|| PageUptodate(page
))
1686 if (blkaddr
== NEW_ADDR
) {
1687 zero_user_segment(page
, 0, PAGE_SIZE
);
1688 SetPageUptodate(page
);
1692 bio
= f2fs_grab_bio(inode
, blkaddr
, 1);
1697 bio_set_op_attrs(bio
, REQ_OP_READ
, READ_SYNC
);
1698 if (bio_add_page(bio
, page
, PAGE_SIZE
, 0) < PAGE_SIZE
) {
1704 __submit_bio(sbi
, bio
, DATA
);
1707 if (unlikely(page
->mapping
!= mapping
)) {
1708 f2fs_put_page(page
, 1);
1711 if (unlikely(!PageUptodate(page
))) {
1719 f2fs_put_page(page
, 1);
1720 f2fs_write_failed(mapping
, pos
+ len
);
1724 static int f2fs_write_end(struct file
*file
,
1725 struct address_space
*mapping
,
1726 loff_t pos
, unsigned len
, unsigned copied
,
1727 struct page
*page
, void *fsdata
)
1729 struct inode
*inode
= page
->mapping
->host
;
1731 trace_f2fs_write_end(inode
, pos
, len
, copied
);
1734 * This should be come from len == PAGE_SIZE, and we expect copied
1735 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
1736 * let generic_perform_write() try to copy data again through copied=0.
1738 if (!PageUptodate(page
)) {
1739 if (unlikely(copied
!= PAGE_SIZE
))
1742 SetPageUptodate(page
);
1747 set_page_dirty(page
);
1748 clear_cold_data(page
);
1750 if (pos
+ copied
> i_size_read(inode
))
1751 f2fs_i_size_write(inode
, pos
+ copied
);
1753 f2fs_put_page(page
, 1);
1754 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
1758 static int check_direct_IO(struct inode
*inode
, struct iov_iter
*iter
,
1761 unsigned blocksize_mask
= inode
->i_sb
->s_blocksize
- 1;
1763 if (offset
& blocksize_mask
)
1766 if (iov_iter_alignment(iter
) & blocksize_mask
)
1772 static ssize_t
f2fs_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
)
1774 struct address_space
*mapping
= iocb
->ki_filp
->f_mapping
;
1775 struct inode
*inode
= mapping
->host
;
1776 size_t count
= iov_iter_count(iter
);
1777 loff_t offset
= iocb
->ki_pos
;
1778 int rw
= iov_iter_rw(iter
);
1781 err
= check_direct_IO(inode
, iter
, offset
);
1785 if (f2fs_encrypted_inode(inode
) && S_ISREG(inode
->i_mode
))
1787 if (test_opt(F2FS_I_SB(inode
), LFS
))
1790 trace_f2fs_direct_IO_enter(inode
, offset
, count
, rw
);
1792 down_read(&F2FS_I(inode
)->dio_rwsem
[rw
]);
1793 err
= blockdev_direct_IO(iocb
, inode
, iter
, get_data_block_dio
);
1794 up_read(&F2FS_I(inode
)->dio_rwsem
[rw
]);
1798 set_inode_flag(inode
, FI_UPDATE_WRITE
);
1800 f2fs_write_failed(mapping
, offset
+ count
);
1803 trace_f2fs_direct_IO_exit(inode
, offset
, count
, rw
, err
);
1808 void f2fs_invalidate_page(struct page
*page
, unsigned int offset
,
1809 unsigned int length
)
1811 struct inode
*inode
= page
->mapping
->host
;
1812 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1814 if (inode
->i_ino
>= F2FS_ROOT_INO(sbi
) &&
1815 (offset
% PAGE_SIZE
|| length
!= PAGE_SIZE
))
1818 if (PageDirty(page
)) {
1819 if (inode
->i_ino
== F2FS_META_INO(sbi
))
1820 dec_page_count(sbi
, F2FS_DIRTY_META
);
1821 else if (inode
->i_ino
== F2FS_NODE_INO(sbi
))
1822 dec_page_count(sbi
, F2FS_DIRTY_NODES
);
1824 inode_dec_dirty_pages(inode
);
1827 clear_cold_data(page
);
1829 /* This is atomic written page, keep Private */
1830 if (IS_ATOMIC_WRITTEN_PAGE(page
))
1833 set_page_private(page
, 0);
1834 ClearPagePrivate(page
);
1837 int f2fs_release_page(struct page
*page
, gfp_t wait
)
1839 /* If this is dirty page, keep PagePrivate */
1840 if (PageDirty(page
))
1843 /* This is atomic written page, keep Private */
1844 if (IS_ATOMIC_WRITTEN_PAGE(page
))
1847 clear_cold_data(page
);
1848 set_page_private(page
, 0);
1849 ClearPagePrivate(page
);
1854 * This was copied from __set_page_dirty_buffers which gives higher performance
1855 * in very high speed storages. (e.g., pmem)
1857 void f2fs_set_page_dirty_nobuffers(struct page
*page
)
1859 struct address_space
*mapping
= page
->mapping
;
1860 unsigned long flags
;
1862 if (unlikely(!mapping
))
1865 spin_lock(&mapping
->private_lock
);
1866 lock_page_memcg(page
);
1868 spin_unlock(&mapping
->private_lock
);
1870 spin_lock_irqsave(&mapping
->tree_lock
, flags
);
1871 WARN_ON_ONCE(!PageUptodate(page
));
1872 account_page_dirtied(page
, mapping
);
1873 radix_tree_tag_set(&mapping
->page_tree
,
1874 page_index(page
), PAGECACHE_TAG_DIRTY
);
1875 spin_unlock_irqrestore(&mapping
->tree_lock
, flags
);
1876 unlock_page_memcg(page
);
1878 __mark_inode_dirty(mapping
->host
, I_DIRTY_PAGES
);
1882 static int f2fs_set_data_page_dirty(struct page
*page
)
1884 struct address_space
*mapping
= page
->mapping
;
1885 struct inode
*inode
= mapping
->host
;
1887 trace_f2fs_set_page_dirty(page
, DATA
);
1889 if (!PageUptodate(page
))
1890 SetPageUptodate(page
);
1892 if (f2fs_is_atomic_file(inode
)) {
1893 if (!IS_ATOMIC_WRITTEN_PAGE(page
)) {
1894 register_inmem_page(inode
, page
);
1898 * Previously, this page has been registered, we just
1904 if (!PageDirty(page
)) {
1905 f2fs_set_page_dirty_nobuffers(page
);
1906 update_dirty_page(inode
, page
);
1912 static sector_t
f2fs_bmap(struct address_space
*mapping
, sector_t block
)
1914 struct inode
*inode
= mapping
->host
;
1916 if (f2fs_has_inline_data(inode
))
1919 /* make sure allocating whole blocks */
1920 if (mapping_tagged(mapping
, PAGECACHE_TAG_DIRTY
))
1921 filemap_write_and_wait(mapping
);
1923 return generic_block_bmap(mapping
, block
, get_data_block_bmap
);
1926 #ifdef CONFIG_MIGRATION
1927 #include <linux/migrate.h>
1929 int f2fs_migrate_page(struct address_space
*mapping
,
1930 struct page
*newpage
, struct page
*page
, enum migrate_mode mode
)
1932 int rc
, extra_count
;
1933 struct f2fs_inode_info
*fi
= F2FS_I(mapping
->host
);
1934 bool atomic_written
= IS_ATOMIC_WRITTEN_PAGE(page
);
1936 BUG_ON(PageWriteback(page
));
1938 /* migrating an atomic written page is safe with the inmem_lock hold */
1939 if (atomic_written
&& !mutex_trylock(&fi
->inmem_lock
))
1943 * A reference is expected if PagePrivate set when move mapping,
1944 * however F2FS breaks this for maintaining dirty page counts when
1945 * truncating pages. So here adjusting the 'extra_count' make it work.
1947 extra_count
= (atomic_written
? 1 : 0) - page_has_private(page
);
1948 rc
= migrate_page_move_mapping(mapping
, newpage
,
1949 page
, NULL
, mode
, extra_count
);
1950 if (rc
!= MIGRATEPAGE_SUCCESS
) {
1952 mutex_unlock(&fi
->inmem_lock
);
1956 if (atomic_written
) {
1957 struct inmem_pages
*cur
;
1958 list_for_each_entry(cur
, &fi
->inmem_pages
, list
)
1959 if (cur
->page
== page
) {
1960 cur
->page
= newpage
;
1963 mutex_unlock(&fi
->inmem_lock
);
1968 if (PagePrivate(page
))
1969 SetPagePrivate(newpage
);
1970 set_page_private(newpage
, page_private(page
));
1972 migrate_page_copy(newpage
, page
);
1974 return MIGRATEPAGE_SUCCESS
;
1978 const struct address_space_operations f2fs_dblock_aops
= {
1979 .readpage
= f2fs_read_data_page
,
1980 .readpages
= f2fs_read_data_pages
,
1981 .writepage
= f2fs_write_data_page
,
1982 .writepages
= f2fs_write_data_pages
,
1983 .write_begin
= f2fs_write_begin
,
1984 .write_end
= f2fs_write_end
,
1985 .set_page_dirty
= f2fs_set_data_page_dirty
,
1986 .invalidatepage
= f2fs_invalidate_page
,
1987 .releasepage
= f2fs_release_page
,
1988 .direct_IO
= f2fs_direct_IO
,
1990 #ifdef CONFIG_MIGRATION
1991 .migratepage
= f2fs_migrate_page
,